1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
11 * eval.c: Expression evaluation.
13 #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
14 # include "vimio.h" /* for mch_open(), must be before vim.h */
19 #if defined(FEAT_EVAL) || defined(PROTO)
22 # include <time.h> /* for strftime() */
26 # include <time.h> /* for time_t */
29 #if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
33 #define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
35 #define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
39 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
40 * This avoids adding a pointer to the hashtab item.
41 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
42 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
43 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 static dictitem_T dumdi
;
46 #define DI2HIKEY(di) ((di)->di_key)
47 #define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
48 #define HI2DI(hi) HIKEY2DI((hi)->hi_key)
51 * Structure returned by get_lval() and used by set_var_lval().
53 * "name" points to the variable name.
56 * For a magic braces name:
57 * "name" points to the expanded variable name.
58 * "exp_name" is non-NULL, to be freed later.
60 * For an index in a list:
61 * "name" points to the (expanded) variable name.
62 * "exp_name" NULL or non-NULL, to be freed later.
63 * "tv" points to the (first) list item value
64 * "li" points to the (first) list item
65 * "range", "n1", "n2" and "empty2" indicate what items are used.
66 * For an existing Dict item:
67 * "name" points to the (expanded) variable name.
68 * "exp_name" NULL or non-NULL, to be freed later.
69 * "tv" points to the dict item value
71 * For a non-existing Dict item:
72 * "name" points to the (expanded) variable name.
73 * "exp_name" NULL or non-NULL, to be freed later.
74 * "tv" points to the Dictionary typval_T
75 * "newkey" is the key for the new item.
79 char_u
*ll_name
; /* start of variable name (can be NULL) */
80 char_u
*ll_exp_name
; /* NULL or expanded name in allocated memory. */
81 typval_T
*ll_tv
; /* Typeval of item being used. If "newkey"
82 isn't NULL it's the Dict to which to add
84 listitem_T
*ll_li
; /* The list item or NULL. */
85 list_T
*ll_list
; /* The list or NULL. */
86 int ll_range
; /* TRUE when a [i:j] range was used */
87 long ll_n1
; /* First index for list */
88 long ll_n2
; /* Second index for list range */
89 int ll_empty2
; /* Second index is empty: [i:] */
90 dict_T
*ll_dict
; /* The Dictionary or NULL */
91 dictitem_T
*ll_di
; /* The dictitem or NULL */
92 char_u
*ll_newkey
; /* New key for Dict in alloc. mem or NULL. */
96 static char *e_letunexp
= N_("E18: Unexpected characters in :let");
97 static char *e_listidx
= N_("E684: list index out of range: %ld");
98 static char *e_undefvar
= N_("E121: Undefined variable: %s");
99 static char *e_missbrac
= N_("E111: Missing ']'");
100 static char *e_listarg
= N_("E686: Argument of %s must be a List");
101 static char *e_listdictarg
= N_("E712: Argument of %s must be a List or Dictionary");
102 static char *e_emptykey
= N_("E713: Cannot use empty key for Dictionary");
103 static char *e_listreq
= N_("E714: List required");
104 static char *e_dictreq
= N_("E715: Dictionary required");
105 static char *e_toomanyarg
= N_("E118: Too many arguments for function: %s");
106 static char *e_dictkey
= N_("E716: Key not present in Dictionary: %s");
107 static char *e_funcexts
= N_("E122: Function %s already exists, add ! to replace it");
108 static char *e_funcdict
= N_("E717: Dictionary entry already exists");
109 static char *e_funcref
= N_("E718: Funcref required");
110 static char *e_dictrange
= N_("E719: Cannot use [:] with a Dictionary");
111 static char *e_letwrong
= N_("E734: Wrong variable type for %s=");
112 static char *e_nofunc
= N_("E130: Unknown function: %s");
113 static char *e_illvar
= N_("E461: Illegal variable name: %s");
116 * All user-defined global variables are stored in dictionary "globvardict".
117 * "globvars_var" is the variable that is used for "g:".
119 static dict_T globvardict
;
120 static dictitem_T globvars_var
;
121 #define globvarht globvardict.dv_hashtab
124 * Old Vim variables such as "v:version" are also available without the "v:".
125 * Also in functions. We need a special hashtable for them.
127 static hashtab_T compat_hashtab
;
130 * When recursively copying lists and dicts we need to remember which ones we
131 * have done to avoid endless recursiveness. This unique ID is used for that.
133 static int current_copyID
= 0;
136 * Array to hold the hashtab with variables local to each sourced script.
137 * Each item holds a variable (nameless) that points to the dict_T.
145 static garray_T ga_scripts
= {0, 0, sizeof(scriptvar_T
), 4, NULL
};
146 #define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
147 #define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
149 static int echo_attr
= 0; /* attributes used for ":echo" */
151 /* Values for trans_function_name() argument: */
152 #define TFN_INT 1 /* internal function name OK */
153 #define TFN_QUIET 2 /* no error messages */
156 * Structure to hold info for a user function.
158 typedef struct ufunc ufunc_T
;
162 int uf_varargs
; /* variable nr of arguments */
164 int uf_calls
; /* nr of active calls */
165 garray_T uf_args
; /* arguments */
166 garray_T uf_lines
; /* function lines */
168 int uf_profiling
; /* TRUE when func is being profiled */
169 /* profiling the function as a whole */
170 int uf_tm_count
; /* nr of calls */
171 proftime_T uf_tm_total
; /* time spent in function + children */
172 proftime_T uf_tm_self
; /* time spent in function itself */
173 proftime_T uf_tm_children
; /* time spent in children this call */
174 /* profiling the function per line */
175 int *uf_tml_count
; /* nr of times line was executed */
176 proftime_T
*uf_tml_total
; /* time spent in a line + children */
177 proftime_T
*uf_tml_self
; /* time spent in a line itself */
178 proftime_T uf_tml_start
; /* start time for current line */
179 proftime_T uf_tml_children
; /* time spent in children for this line */
180 proftime_T uf_tml_wait
; /* start wait time for current line */
181 int uf_tml_idx
; /* index of line being timed; -1 if none */
182 int uf_tml_execed
; /* line being timed was executed */
184 scid_T uf_script_ID
; /* ID of script where function was defined,
185 used for s: variables */
186 int uf_refcount
; /* for numbered function: reference count */
187 char_u uf_name
[1]; /* name of function (actually longer); can
188 start with <SNR>123_ (<SNR> is K_SPECIAL
193 #define FC_ABORT 1 /* abort function on error */
194 #define FC_RANGE 2 /* function accepts range */
195 #define FC_DICT 4 /* Dict function, uses "self" */
198 * All user-defined functions are found in this hashtable.
200 static hashtab_T func_hashtab
;
202 /* The names of packages that once were loaded are remembered. */
203 static garray_T ga_loaded
= {0, 0, sizeof(char_u
*), 4, NULL
};
205 /* list heads for garbage collection */
206 static dict_T
*first_dict
= NULL
; /* list of all dicts */
207 static list_T
*first_list
= NULL
; /* list of all lists */
209 /* From user function to hashitem and back. */
210 static ufunc_T dumuf
;
211 #define UF2HIKEY(fp) ((fp)->uf_name)
212 #define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
213 #define HI2UF(hi) HIKEY2UF((hi)->hi_key)
215 #define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
216 #define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
218 #define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
219 #define VAR_SHORT_LEN 20 /* short variable name length */
220 #define FIXVAR_CNT 12 /* number of fixed variables */
222 /* structure to hold info for a function that is currently being executed. */
223 typedef struct funccall_S funccall_T
;
227 ufunc_T
*func
; /* function being called */
228 int linenr
; /* next line to be executed */
229 int returned
; /* ":return" used */
230 struct /* fixed variables for arguments */
232 dictitem_T var
; /* variable (without room for name) */
233 char_u room
[VAR_SHORT_LEN
]; /* room for the name */
234 } fixvar
[FIXVAR_CNT
];
235 dict_T l_vars
; /* l: local function variables */
236 dictitem_T l_vars_var
; /* variable for l: scope */
237 dict_T l_avars
; /* a: argument variables */
238 dictitem_T l_avars_var
; /* variable for a: scope */
239 list_T l_varlist
; /* list for a:000 */
240 listitem_T l_listitems
[MAX_FUNC_ARGS
]; /* listitems for a:000 */
241 typval_T
*rettv
; /* return value */
242 linenr_T breakpoint
; /* next line with breakpoint or zero */
243 int dbg_tick
; /* debug_tick when breakpoint was set */
244 int level
; /* top nesting level of executed function */
246 proftime_T prof_child
; /* time spent in a child */
248 funccall_T
*caller
; /* calling function or NULL */
252 * Info used by a ":for" loop.
256 int fi_semicolon
; /* TRUE if ending in '; var]' */
257 int fi_varcount
; /* nr of variables in the list */
258 listwatch_T fi_lw
; /* keep an eye on the item used. */
259 list_T
*fi_list
; /* list being used */
263 * Struct used by trans_function_name()
267 dict_T
*fd_dict
; /* Dictionary used */
268 char_u
*fd_newkey
; /* new key in "dict" in allocated memory */
269 dictitem_T
*fd_di
; /* Dictionary item used */
274 * Array to hold the value of v: variables.
275 * The value is in a dictitem, so that it can also be used in the v: scope.
276 * The reason to use this table anyway is for very quick access to the
277 * variables with the VV_ defines.
281 /* values for vv_flags: */
282 #define VV_COMPAT 1 /* compatible, also used without "v:" */
283 #define VV_RO 2 /* read-only */
284 #define VV_RO_SBX 4 /* read-only in the sandbox */
286 #define VV_NAME(s, t) s, {{t}}, {0}
290 char *vv_name
; /* name of variable, without v: */
291 dictitem_T vv_di
; /* value and name for key */
292 char vv_filler
[16]; /* space for LONGEST name below!!! */
293 char vv_flags
; /* VV_COMPAT, VV_RO, VV_RO_SBX */
297 * The order here must match the VV_ defines in vim.h!
298 * Initializing a union does not work, leave tv.vval empty to get zero's.
300 {VV_NAME("count", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
301 {VV_NAME("count1", VAR_NUMBER
), VV_RO
},
302 {VV_NAME("prevcount", VAR_NUMBER
), VV_RO
},
303 {VV_NAME("errmsg", VAR_STRING
), VV_COMPAT
},
304 {VV_NAME("warningmsg", VAR_STRING
), 0},
305 {VV_NAME("statusmsg", VAR_STRING
), 0},
306 {VV_NAME("shell_error", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
307 {VV_NAME("this_session", VAR_STRING
), VV_COMPAT
},
308 {VV_NAME("version", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
309 {VV_NAME("lnum", VAR_NUMBER
), VV_RO_SBX
},
310 {VV_NAME("termresponse", VAR_STRING
), VV_RO
},
311 {VV_NAME("fname", VAR_STRING
), VV_RO
},
312 {VV_NAME("lang", VAR_STRING
), VV_RO
},
313 {VV_NAME("lc_time", VAR_STRING
), VV_RO
},
314 {VV_NAME("ctype", VAR_STRING
), VV_RO
},
315 {VV_NAME("charconvert_from", VAR_STRING
), VV_RO
},
316 {VV_NAME("charconvert_to", VAR_STRING
), VV_RO
},
317 {VV_NAME("fname_in", VAR_STRING
), VV_RO
},
318 {VV_NAME("fname_out", VAR_STRING
), VV_RO
},
319 {VV_NAME("fname_new", VAR_STRING
), VV_RO
},
320 {VV_NAME("fname_diff", VAR_STRING
), VV_RO
},
321 {VV_NAME("cmdarg", VAR_STRING
), VV_RO
},
322 {VV_NAME("foldstart", VAR_NUMBER
), VV_RO_SBX
},
323 {VV_NAME("foldend", VAR_NUMBER
), VV_RO_SBX
},
324 {VV_NAME("folddashes", VAR_STRING
), VV_RO_SBX
},
325 {VV_NAME("foldlevel", VAR_NUMBER
), VV_RO_SBX
},
326 {VV_NAME("progname", VAR_STRING
), VV_RO
},
327 {VV_NAME("servername", VAR_STRING
), VV_RO
},
328 {VV_NAME("dying", VAR_NUMBER
), VV_RO
},
329 {VV_NAME("exception", VAR_STRING
), VV_RO
},
330 {VV_NAME("throwpoint", VAR_STRING
), VV_RO
},
331 {VV_NAME("register", VAR_STRING
), VV_RO
},
332 {VV_NAME("cmdbang", VAR_NUMBER
), VV_RO
},
333 {VV_NAME("insertmode", VAR_STRING
), VV_RO
},
334 {VV_NAME("val", VAR_UNKNOWN
), VV_RO
},
335 {VV_NAME("key", VAR_UNKNOWN
), VV_RO
},
336 {VV_NAME("profiling", VAR_NUMBER
), VV_RO
},
337 {VV_NAME("fcs_reason", VAR_STRING
), VV_RO
},
338 {VV_NAME("fcs_choice", VAR_STRING
), 0},
339 {VV_NAME("beval_bufnr", VAR_NUMBER
), VV_RO
},
340 {VV_NAME("beval_winnr", VAR_NUMBER
), VV_RO
},
341 {VV_NAME("beval_lnum", VAR_NUMBER
), VV_RO
},
342 {VV_NAME("beval_col", VAR_NUMBER
), VV_RO
},
343 {VV_NAME("beval_text", VAR_STRING
), VV_RO
},
344 {VV_NAME("scrollstart", VAR_STRING
), 0},
345 {VV_NAME("swapname", VAR_STRING
), VV_RO
},
346 {VV_NAME("swapchoice", VAR_STRING
), 0},
347 {VV_NAME("swapcommand", VAR_STRING
), VV_RO
},
348 {VV_NAME("char", VAR_STRING
), VV_RO
},
349 {VV_NAME("mouse_win", VAR_NUMBER
), 0},
350 {VV_NAME("mouse_lnum", VAR_NUMBER
), 0},
351 {VV_NAME("mouse_col", VAR_NUMBER
), 0},
352 {VV_NAME("operator", VAR_STRING
), VV_RO
},
353 {VV_NAME("searchforward", VAR_NUMBER
), 0},
354 {VV_NAME("oldfiles", VAR_LIST
), 0},
358 #define vv_type vv_di.di_tv.v_type
359 #define vv_nr vv_di.di_tv.vval.v_number
360 #define vv_float vv_di.di_tv.vval.v_float
361 #define vv_str vv_di.di_tv.vval.v_string
362 #define vv_list vv_di.di_tv.vval.v_list
363 #define vv_tv vv_di.di_tv
366 * The v: variables are stored in dictionary "vimvardict".
367 * "vimvars_var" is the variable that is used for the "l:" scope.
369 static dict_T vimvardict
;
370 static dictitem_T vimvars_var
;
371 #define vimvarht vimvardict.dv_hashtab
373 static void prepare_vimvar
__ARGS((int idx
, typval_T
*save_tv
));
374 static void restore_vimvar
__ARGS((int idx
, typval_T
*save_tv
));
375 #if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
376 static int call_vim_function
__ARGS((char_u
*func
, int argc
, char_u
**argv
, int safe
, typval_T
*rettv
));
378 static int ex_let_vars
__ARGS((char_u
*arg
, typval_T
*tv
, int copy
, int semicolon
, int var_count
, char_u
*nextchars
));
379 static char_u
*skip_var_list
__ARGS((char_u
*arg
, int *var_count
, int *semicolon
));
380 static char_u
*skip_var_one
__ARGS((char_u
*arg
));
381 static void list_hashtable_vars
__ARGS((hashtab_T
*ht
, char_u
*prefix
, int empty
, int *first
));
382 static void list_glob_vars
__ARGS((int *first
));
383 static void list_buf_vars
__ARGS((int *first
));
384 static void list_win_vars
__ARGS((int *first
));
386 static void list_tab_vars
__ARGS((int *first
));
388 static void list_vim_vars
__ARGS((int *first
));
389 static void list_script_vars
__ARGS((int *first
));
390 static void list_func_vars
__ARGS((int *first
));
391 static char_u
*list_arg_vars
__ARGS((exarg_T
*eap
, char_u
*arg
, int *first
));
392 static char_u
*ex_let_one
__ARGS((char_u
*arg
, typval_T
*tv
, int copy
, char_u
*endchars
, char_u
*op
));
393 static int check_changedtick
__ARGS((char_u
*arg
));
394 static char_u
*get_lval
__ARGS((char_u
*name
, typval_T
*rettv
, lval_T
*lp
, int unlet
, int skip
, int quiet
, int fne_flags
));
395 static void clear_lval
__ARGS((lval_T
*lp
));
396 static void set_var_lval
__ARGS((lval_T
*lp
, char_u
*endp
, typval_T
*rettv
, int copy
, char_u
*op
));
397 static int tv_op
__ARGS((typval_T
*tv1
, typval_T
*tv2
, char_u
*op
));
398 static void list_add_watch
__ARGS((list_T
*l
, listwatch_T
*lw
));
399 static void list_rem_watch
__ARGS((list_T
*l
, listwatch_T
*lwrem
));
400 static void list_fix_watch
__ARGS((list_T
*l
, listitem_T
*item
));
401 static void ex_unletlock
__ARGS((exarg_T
*eap
, char_u
*argstart
, int deep
));
402 static int do_unlet_var
__ARGS((lval_T
*lp
, char_u
*name_end
, int forceit
));
403 static int do_lock_var
__ARGS((lval_T
*lp
, char_u
*name_end
, int deep
, int lock
));
404 static void item_lock
__ARGS((typval_T
*tv
, int deep
, int lock
));
405 static int tv_islocked
__ARGS((typval_T
*tv
));
407 static int eval0
__ARGS((char_u
*arg
, typval_T
*rettv
, char_u
**nextcmd
, int evaluate
));
408 static int eval1
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
409 static int eval2
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
410 static int eval3
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
411 static int eval4
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
412 static int eval5
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
413 static int eval6
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int want_string
));
414 static int eval7
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int want_string
));
416 static int eval_index
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int verbose
));
417 static int get_option_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
418 static int get_string_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
419 static int get_lit_string_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
420 static int get_list_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
421 static int rettv_list_alloc
__ARGS((typval_T
*rettv
));
422 static listitem_T
*listitem_alloc
__ARGS((void));
423 static void listitem_free
__ARGS((listitem_T
*item
));
424 static void listitem_remove
__ARGS((list_T
*l
, listitem_T
*item
));
425 static long list_len
__ARGS((list_T
*l
));
426 static int list_equal
__ARGS((list_T
*l1
, list_T
*l2
, int ic
));
427 static int dict_equal
__ARGS((dict_T
*d1
, dict_T
*d2
, int ic
));
428 static int tv_equal
__ARGS((typval_T
*tv1
, typval_T
*tv2
, int ic
));
429 static listitem_T
*list_find
__ARGS((list_T
*l
, long n
));
430 static long list_find_nr
__ARGS((list_T
*l
, long idx
, int *errorp
));
431 static long list_idx_of_item
__ARGS((list_T
*l
, listitem_T
*item
));
432 static void list_append
__ARGS((list_T
*l
, listitem_T
*item
));
433 static int list_append_tv
__ARGS((list_T
*l
, typval_T
*tv
));
434 static int list_append_number
__ARGS((list_T
*l
, varnumber_T n
));
435 static int list_insert_tv
__ARGS((list_T
*l
, typval_T
*tv
, listitem_T
*item
));
436 static int list_extend
__ARGS((list_T
*l1
, list_T
*l2
, listitem_T
*bef
));
437 static int list_concat
__ARGS((list_T
*l1
, list_T
*l2
, typval_T
*tv
));
438 static list_T
*list_copy
__ARGS((list_T
*orig
, int deep
, int copyID
));
439 static void list_remove
__ARGS((list_T
*l
, listitem_T
*item
, listitem_T
*item2
));
440 static char_u
*list2string
__ARGS((typval_T
*tv
, int copyID
));
441 static int list_join
__ARGS((garray_T
*gap
, list_T
*l
, char_u
*sep
, int echo
, int copyID
));
442 static void set_ref_in_ht
__ARGS((hashtab_T
*ht
, int copyID
));
443 static void set_ref_in_list
__ARGS((list_T
*l
, int copyID
));
444 static void set_ref_in_item
__ARGS((typval_T
*tv
, int copyID
));
445 static void dict_unref
__ARGS((dict_T
*d
));
446 static void dict_free
__ARGS((dict_T
*d
, int recurse
));
447 static dictitem_T
*dictitem_alloc
__ARGS((char_u
*key
));
448 static dictitem_T
*dictitem_copy
__ARGS((dictitem_T
*org
));
449 static void dictitem_remove
__ARGS((dict_T
*dict
, dictitem_T
*item
));
450 static void dictitem_free
__ARGS((dictitem_T
*item
));
451 static dict_T
*dict_copy
__ARGS((dict_T
*orig
, int deep
, int copyID
));
452 static int dict_add
__ARGS((dict_T
*d
, dictitem_T
*item
));
453 static long dict_len
__ARGS((dict_T
*d
));
454 static dictitem_T
*dict_find
__ARGS((dict_T
*d
, char_u
*key
, int len
));
455 static char_u
*dict2string
__ARGS((typval_T
*tv
, int copyID
));
456 static int get_dict_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
457 static char_u
*echo_string
__ARGS((typval_T
*tv
, char_u
**tofree
, char_u
*numbuf
, int copyID
));
458 static char_u
*tv2string
__ARGS((typval_T
*tv
, char_u
**tofree
, char_u
*numbuf
, int copyID
));
459 static char_u
*string_quote
__ARGS((char_u
*str
, int function
));
461 static int string2float
__ARGS((char_u
*text
, float_T
*value
));
463 static int get_env_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
464 static int find_internal_func
__ARGS((char_u
*name
));
465 static char_u
*deref_func_name
__ARGS((char_u
*name
, int *lenp
));
466 static int get_func_tv
__ARGS((char_u
*name
, int len
, typval_T
*rettv
, char_u
**arg
, linenr_T firstline
, linenr_T lastline
, int *doesrange
, int evaluate
, dict_T
*selfdict
));
467 static int call_func
__ARGS((char_u
*name
, int len
, typval_T
*rettv
, int argcount
, typval_T
*argvars
, linenr_T firstline
, linenr_T lastline
, int *doesrange
, int evaluate
, dict_T
*selfdict
));
468 static void emsg_funcname
__ARGS((char *ermsg
, char_u
*name
));
469 static int non_zero_arg
__ARGS((typval_T
*argvars
));
472 static void f_abs
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
474 /* Below are the 10 added FP functions - I've kept them together */
475 /* here and in their definitions later on. Because the functions[] */
476 /* table must be in ASCII order, they are scattered there - WJMc */
478 static void f_acos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
479 static void f_asin
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
480 static void f_atan2
__ARGS((typval_T
*argvars
, typval_T
*rettv
)); /* 2 args */
481 static void f_cosh
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
482 static void f_exp
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
483 static void f_fmod
__ARGS((typval_T
*argvars
, typval_T
*rettv
)); /* 2 args */
484 static void f_log
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
485 static void f_sinh
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
486 static void f_tan
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
487 static void f_tanh
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
489 static void f_add
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
490 static void f_append
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
491 static void f_argc
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
492 static void f_argidx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
493 static void f_argv
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
495 static void f_atan
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
497 static void f_browse
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
498 static void f_browsedir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
499 static void f_bufexists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
500 static void f_buflisted
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
501 static void f_bufloaded
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
502 static void f_bufname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
503 static void f_bufnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
504 static void f_bufwinnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
505 static void f_byte2line
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
506 static void f_byteidx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
507 static void f_call
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
509 static void f_ceil
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
511 static void f_changenr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
512 static void f_char2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
513 static void f_cindent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
514 static void f_clearmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
515 static void f_col
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
516 #if defined(FEAT_INS_EXPAND)
517 static void f_complete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
518 static void f_complete_add
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
519 static void f_complete_check
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
521 static void f_confirm
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
522 static void f_copy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
524 static void f_cos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
526 static void f_count
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
527 static void f_cscope_connection
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
528 static void f_cursor
__ARGS((typval_T
*argsvars
, typval_T
*rettv
));
529 static void f_deepcopy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
530 static void f_delete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
531 static void f_did_filetype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
532 static void f_diff_filler
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
533 static void f_diff_hlID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
534 static void f_empty
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
535 static void f_escape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
536 static void f_eval
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
537 static void f_eventhandler
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
538 static void f_executable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
539 static void f_exists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
540 static void f_expand
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
541 static void f_extend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
542 static void f_feedkeys
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
543 static void f_filereadable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
544 static void f_filewritable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
545 static void f_filter
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
546 static void f_finddir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
547 static void f_findfile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
549 static void f_float2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
550 static void f_floor
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
552 static void f_fnameescape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
553 static void f_fnamemodify
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
554 static void f_foldclosed
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
555 static void f_foldclosedend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
556 static void f_foldlevel
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
557 static void f_foldtext
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
558 static void f_foldtextresult
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
559 static void f_foreground
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
560 static void f_function
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
561 static void f_garbagecollect
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
562 static void f_get
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
563 static void f_getbufline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
564 static void f_getbufvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
565 static void f_getchar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
566 static void f_getcharmod
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
567 static void f_getcmdline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
568 static void f_getcmdpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
569 static void f_getcmdtype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
570 static void f_getcwd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
571 static void f_getfontname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
572 static void f_getfperm
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
573 static void f_getfsize
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
574 static void f_getftime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
575 static void f_getftype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
576 static void f_getline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
577 static void f_getmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
578 static void f_getpid
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
579 static void f_getpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
580 static void f_getqflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
581 static void f_getreg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
582 static void f_getregtype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
583 static void f_gettabwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
584 static void f_getwinposx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
585 static void f_getwinposy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
586 static void f_getwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
587 static void f_glob
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
588 static void f_globpath
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
589 static void f_has
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
590 static void f_has_key
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
591 static void f_haslocaldir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
592 static void f_hasmapto
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
593 static void f_histadd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
594 static void f_histdel
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
595 static void f_histget
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
596 static void f_histnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
597 static void f_hlID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
598 static void f_hlexists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
599 static void f_hostname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
600 static void f_iconv
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
601 static void f_indent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
602 static void f_index
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
603 static void f_input
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
604 static void f_inputdialog
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
605 static void f_inputlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
606 static void f_inputrestore
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
607 static void f_inputsave
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
608 static void f_inputsecret
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
609 static void f_insert
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
610 static void f_isdirectory
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
611 static void f_islocked
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
612 static void f_items
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
613 static void f_join
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
614 static void f_keys
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
615 static void f_last_buffer_nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
616 static void f_len
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
617 static void f_libcall
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
618 static void f_libcallnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
619 static void f_line
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
620 static void f_line2byte
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
621 static void f_lispindent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
622 static void f_localtime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
624 static void f_log10
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
626 static void f_map
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
627 static void f_maparg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
628 static void f_mapcheck
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
629 static void f_match
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
630 static void f_matchadd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
631 static void f_matcharg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
632 static void f_matchdelete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
633 static void f_matchend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
634 static void f_matchlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
635 static void f_matchstr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
636 static void f_max
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
637 static void f_min
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
639 static void f_mkdir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
641 static void f_mode
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
642 static void f_nextnonblank
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
643 static void f_nr2char
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
644 static void f_pathshorten
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
646 static void f_pow
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
648 static void f_prevnonblank
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
649 static void f_printf
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
650 static void f_pumvisible
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
651 static void f_range
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
652 static void f_readfile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
653 static void f_reltime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
654 static void f_reltimestr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
655 static void f_remote_expr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
656 static void f_remote_foreground
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
657 static void f_remote_peek
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
658 static void f_remote_read
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
659 static void f_remote_send
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
660 static void f_remove
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
661 static void f_rename
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
662 static void f_repeat
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
663 static void f_resolve
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
664 static void f_reverse
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
666 static void f_round
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
668 static void f_search
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
669 static void f_searchdecl
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
670 static void f_searchpair
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
671 static void f_searchpairpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
672 static void f_searchpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
673 static void f_server2client
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
674 static void f_serverlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
675 static void f_setbufvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
676 static void f_setcmdpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
677 static void f_setline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
678 static void f_setloclist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
679 static void f_setmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
680 static void f_setpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
681 static void f_setqflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
682 static void f_setreg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
683 static void f_settabwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
684 static void f_setwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
685 static void f_shellescape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
686 static void f_simplify
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
688 static void f_sin
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
690 static void f_sort
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
691 static void f_soundfold
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
692 static void f_spellbadword
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
693 static void f_spellsuggest
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
694 static void f_split
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
696 static void f_sqrt
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
697 static void f_str2float
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
699 static void f_str2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
701 static void f_strftime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
703 static void f_stridx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
704 static void f_string
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
705 static void f_strlen
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
706 static void f_strpart
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
707 static void f_strridx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
708 static void f_strtrans
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
709 static void f_submatch
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
710 static void f_substitute
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
711 static void f_synID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
712 static void f_synIDattr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
713 static void f_synIDtrans
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
714 static void f_synstack
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
715 static void f_system
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
716 static void f_tabpagebuflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
717 static void f_tabpagenr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
718 static void f_tabpagewinnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
719 static void f_taglist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
720 static void f_tagfiles
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
721 static void f_tempname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
722 static void f_test
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
723 static void f_tolower
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
724 static void f_toupper
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
725 static void f_tr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
727 static void f_trunc
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
729 static void f_type
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
730 static void f_values
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
731 static void f_virtcol
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
732 static void f_visualmode
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
733 static void f_winbufnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
734 static void f_wincol
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
735 static void f_winheight
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
736 static void f_winline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
737 static void f_winnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
738 static void f_winrestcmd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
739 static void f_winrestview
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
740 static void f_winsaveview
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
741 static void f_winwidth
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
742 static void f_writefile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
744 static int list2fpos
__ARGS((typval_T
*arg
, pos_T
*posp
, int *fnump
));
745 static pos_T
*var2fpos
__ARGS((typval_T
*varp
, int dollar_lnum
, int *fnum
));
746 static int get_env_len
__ARGS((char_u
**arg
));
747 static int get_id_len
__ARGS((char_u
**arg
));
748 static int get_name_len
__ARGS((char_u
**arg
, char_u
**alias
, int evaluate
, int verbose
));
749 static char_u
*find_name_end
__ARGS((char_u
*arg
, char_u
**expr_start
, char_u
**expr_end
, int flags
));
750 #define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
751 #define FNE_CHECK_START 2 /* find_name_end(): check name starts with
753 static char_u
* make_expanded_name
__ARGS((char_u
*in_start
, char_u
*expr_start
, char_u
*expr_end
, char_u
*in_end
));
754 static int eval_isnamec
__ARGS((int c
));
755 static int eval_isnamec1
__ARGS((int c
));
756 static int get_var_tv
__ARGS((char_u
*name
, int len
, typval_T
*rettv
, int verbose
));
757 static int handle_subscript
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int verbose
));
758 static typval_T
*alloc_tv
__ARGS((void));
759 static typval_T
*alloc_string_tv
__ARGS((char_u
*string
));
760 static void init_tv
__ARGS((typval_T
*varp
));
761 static long get_tv_number
__ARGS((typval_T
*varp
));
762 static linenr_T get_tv_lnum
__ARGS((typval_T
*argvars
));
763 static linenr_T get_tv_lnum_buf
__ARGS((typval_T
*argvars
, buf_T
*buf
));
764 static char_u
*get_tv_string
__ARGS((typval_T
*varp
));
765 static char_u
*get_tv_string_buf
__ARGS((typval_T
*varp
, char_u
*buf
));
766 static char_u
*get_tv_string_buf_chk
__ARGS((typval_T
*varp
, char_u
*buf
));
767 static dictitem_T
*find_var
__ARGS((char_u
*name
, hashtab_T
**htp
));
768 static dictitem_T
*find_var_in_ht
__ARGS((hashtab_T
*ht
, char_u
*varname
, int writing
));
769 static hashtab_T
*find_var_ht
__ARGS((char_u
*name
, char_u
**varname
));
770 static void vars_clear_ext
__ARGS((hashtab_T
*ht
, int free_val
));
771 static void delete_var
__ARGS((hashtab_T
*ht
, hashitem_T
*hi
));
772 static void list_one_var
__ARGS((dictitem_T
*v
, char_u
*prefix
, int *first
));
773 static void list_one_var_a
__ARGS((char_u
*prefix
, char_u
*name
, int type
, char_u
*string
, int *first
));
774 static void set_var
__ARGS((char_u
*name
, typval_T
*varp
, int copy
));
775 static int var_check_ro
__ARGS((int flags
, char_u
*name
));
776 static int var_check_fixed
__ARGS((int flags
, char_u
*name
));
777 static int tv_check_lock
__ARGS((int lock
, char_u
*name
));
778 static void copy_tv
__ARGS((typval_T
*from
, typval_T
*to
));
779 static int item_copy
__ARGS((typval_T
*from
, typval_T
*to
, int deep
, int copyID
));
780 static char_u
*find_option_end
__ARGS((char_u
**arg
, int *opt_flags
));
781 static char_u
*trans_function_name
__ARGS((char_u
**pp
, int skip
, int flags
, funcdict_T
*fd
));
782 static int eval_fname_script
__ARGS((char_u
*p
));
783 static int eval_fname_sid
__ARGS((char_u
*p
));
784 static void list_func_head
__ARGS((ufunc_T
*fp
, int indent
));
785 static ufunc_T
*find_func
__ARGS((char_u
*name
));
786 static int function_exists
__ARGS((char_u
*name
));
787 static int builtin_function
__ARGS((char_u
*name
));
789 static void func_do_profile
__ARGS((ufunc_T
*fp
));
790 static void prof_sort_list
__ARGS((FILE *fd
, ufunc_T
**sorttab
, int st_len
, char *title
, int prefer_self
));
791 static void prof_func_line
__ARGS((FILE *fd
, int count
, proftime_T
*total
, proftime_T
*self
, int prefer_self
));
796 prof_total_cmp
__ARGS((const void *s1
, const void *s2
));
801 prof_self_cmp
__ARGS((const void *s1
, const void *s2
));
803 static int script_autoload
__ARGS((char_u
*name
, int reload
));
804 static char_u
*autoload_name
__ARGS((char_u
*name
));
805 static void cat_func_name
__ARGS((char_u
*buf
, ufunc_T
*fp
));
806 static void func_free
__ARGS((ufunc_T
*fp
));
807 static void func_unref
__ARGS((char_u
*name
));
808 static void func_ref
__ARGS((char_u
*name
));
809 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
));
810 static int can_free_funccal
__ARGS((funccall_T
*fc
, int copyID
)) ;
811 static void free_funccal
__ARGS((funccall_T
*fc
, int free_val
));
812 static void add_nr_var
__ARGS((dict_T
*dp
, dictitem_T
*v
, char *name
, varnumber_T nr
));
813 static win_T
*find_win_by_nr
__ARGS((typval_T
*vp
, tabpage_T
*tp
));
814 static void getwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int off
));
815 static int searchpair_cmn
__ARGS((typval_T
*argvars
, pos_T
*match_pos
));
816 static int search_cmn
__ARGS((typval_T
*argvars
, pos_T
*match_pos
, int *flagsp
));
817 static void setwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int off
));
819 /* Character used as separated in autoload function/variable names. */
820 #define AUTOLOAD_CHAR '#'
823 * Initialize the global and v: variables.
831 init_var_dict(&globvardict
, &globvars_var
);
832 init_var_dict(&vimvardict
, &vimvars_var
);
833 hash_init(&compat_hashtab
);
834 hash_init(&func_hashtab
);
836 for (i
= 0; i
< VV_LEN
; ++i
)
839 STRCPY(p
->vv_di
.di_key
, p
->vv_name
);
840 if (p
->vv_flags
& VV_RO
)
841 p
->vv_di
.di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
842 else if (p
->vv_flags
& VV_RO_SBX
)
843 p
->vv_di
.di_flags
= DI_FLAGS_RO_SBX
| DI_FLAGS_FIX
;
845 p
->vv_di
.di_flags
= DI_FLAGS_FIX
;
847 /* add to v: scope dict, unless the value is not always available */
848 if (p
->vv_type
!= VAR_UNKNOWN
)
849 hash_add(&vimvarht
, p
->vv_di
.di_key
);
850 if (p
->vv_flags
& VV_COMPAT
)
851 /* add to compat scope dict */
852 hash_add(&compat_hashtab
, p
->vv_di
.di_key
);
854 set_vim_var_nr(VV_SEARCHFORWARD
, 1L);
857 #if defined(EXITFREE) || defined(PROTO)
864 for (i
= 0; i
< VV_LEN
; ++i
)
867 if (p
->vv_di
.di_tv
.v_type
== VAR_STRING
)
872 else if (p
->vv_di
.di_tv
.v_type
== VAR_LIST
)
874 list_unref(p
->vv_list
);
878 hash_clear(&vimvarht
);
879 hash_init(&vimvarht
); /* garbage_collect() will access it */
880 hash_clear(&compat_hashtab
);
882 /* script-local variables */
883 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
884 vars_clear(&SCRIPT_VARS(i
));
885 ga_clear(&ga_scripts
);
888 /* global variables */
889 vars_clear(&globvarht
);
891 /* autoloaded script names */
892 ga_clear_strings(&ga_loaded
);
894 /* unreferenced lists and dicts */
895 (void)garbage_collect();
898 free_all_functions();
899 hash_clear(&func_hashtab
);
904 * Return the name of the executed function.
910 return ((funccall_T
*)cookie
)->func
->uf_name
;
914 * Return the address holding the next breakpoint line for a funccall cookie.
917 func_breakpoint(cookie
)
920 return &((funccall_T
*)cookie
)->breakpoint
;
924 * Return the address holding the debug tick for a funccall cookie.
927 func_dbg_tick(cookie
)
930 return &((funccall_T
*)cookie
)->dbg_tick
;
934 * Return the nesting level for a funccall cookie.
940 return ((funccall_T
*)cookie
)->level
;
943 /* pointer to funccal for currently active function */
944 funccall_T
*current_funccal
= NULL
;
946 /* pointer to list of previously used funccal, still around because some
947 * item in it is still being used. */
948 funccall_T
*previous_funccal
= NULL
;
951 * Return TRUE when a function was ended by a ":return" command.
954 current_func_returned()
956 return current_funccal
->returned
;
961 * Set an internal variable to a string value. Creates the variable if it does
965 set_internal_string_var(name
, value
)
972 val
= vim_strsave(value
);
975 tvp
= alloc_string_tv(val
);
978 set_var(name
, tvp
, FALSE
);
984 static lval_T
*redir_lval
= NULL
;
985 static garray_T redir_ga
; /* only valid when redir_lval is not NULL */
986 static char_u
*redir_endp
= NULL
;
987 static char_u
*redir_varname
= NULL
;
990 * Start recording command output to a variable
991 * Returns OK if successfully completed the setup. FAIL otherwise.
994 var_redir_start(name
, append
)
996 int append
; /* append to an existing variable */
1002 /* Make sure a valid variable name is specified */
1003 if (!eval_isnamec1(*name
))
1009 redir_varname
= vim_strsave(name
);
1010 if (redir_varname
== NULL
)
1013 redir_lval
= (lval_T
*)alloc_clear((unsigned)sizeof(lval_T
));
1014 if (redir_lval
== NULL
)
1020 /* The output is stored in growarray "redir_ga" until redirection ends. */
1021 ga_init2(&redir_ga
, (int)sizeof(char), 500);
1023 /* Parse the variable name (can be a dict or list entry). */
1024 redir_endp
= get_lval(redir_varname
, NULL
, redir_lval
, FALSE
, FALSE
, FALSE
,
1026 if (redir_endp
== NULL
|| redir_lval
->ll_name
== NULL
|| *redir_endp
!= NUL
)
1028 if (redir_endp
!= NULL
&& *redir_endp
!= NUL
)
1029 /* Trailing characters are present after the variable name */
1030 EMSG(_(e_trailing
));
1037 /* check if we can write to the variable: set it to or append an empty
1039 save_emsg
= did_emsg
;
1041 tv
.v_type
= VAR_STRING
;
1042 tv
.vval
.v_string
= (char_u
*)"";
1044 set_var_lval(redir_lval
, redir_endp
, &tv
, TRUE
, (char_u
*)".");
1046 set_var_lval(redir_lval
, redir_endp
, &tv
, TRUE
, (char_u
*)"=");
1048 did_emsg
|= save_emsg
;
1054 if (redir_lval
->ll_newkey
!= NULL
)
1056 /* Dictionary item was created, don't do it again. */
1057 vim_free(redir_lval
->ll_newkey
);
1058 redir_lval
->ll_newkey
= NULL
;
1065 * Append "value[value_len]" to the variable set by var_redir_start().
1066 * The actual appending is postponed until redirection ends, because the value
1067 * appended may in fact be the string we write to, changing it may cause freed
1068 * memory to be used:
1074 var_redir_str(value
, value_len
)
1080 if (redir_lval
== NULL
)
1083 if (value_len
== -1)
1084 len
= (int)STRLEN(value
); /* Append the entire string */
1086 len
= value_len
; /* Append only "value_len" characters */
1088 if (ga_grow(&redir_ga
, len
) == OK
)
1090 mch_memmove((char *)redir_ga
.ga_data
+ redir_ga
.ga_len
, value
, len
);
1091 redir_ga
.ga_len
+= len
;
1098 * Stop redirecting command output to a variable.
1105 if (redir_lval
!= NULL
)
1107 /* Append the trailing NUL. */
1108 ga_append(&redir_ga
, NUL
);
1110 /* Assign the text to the variable. */
1111 tv
.v_type
= VAR_STRING
;
1112 tv
.vval
.v_string
= redir_ga
.ga_data
;
1113 set_var_lval(redir_lval
, redir_endp
, &tv
, FALSE
, (char_u
*)".");
1114 vim_free(tv
.vval
.v_string
);
1116 clear_lval(redir_lval
);
1117 vim_free(redir_lval
);
1120 vim_free(redir_varname
);
1121 redir_varname
= NULL
;
1124 # if defined(FEAT_MBYTE) || defined(PROTO)
1126 eval_charconvert(enc_from
, enc_to
, fname_from
, fname_to
)
1134 set_vim_var_string(VV_CC_FROM
, enc_from
, -1);
1135 set_vim_var_string(VV_CC_TO
, enc_to
, -1);
1136 set_vim_var_string(VV_FNAME_IN
, fname_from
, -1);
1137 set_vim_var_string(VV_FNAME_OUT
, fname_to
, -1);
1138 if (eval_to_bool(p_ccv
, &err
, NULL
, FALSE
))
1140 set_vim_var_string(VV_CC_FROM
, NULL
, -1);
1141 set_vim_var_string(VV_CC_TO
, NULL
, -1);
1142 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1143 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1151 # if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1153 eval_printexpr(fname
, args
)
1159 set_vim_var_string(VV_FNAME_IN
, fname
, -1);
1160 set_vim_var_string(VV_CMDARG
, args
, -1);
1161 if (eval_to_bool(p_pexpr
, &err
, NULL
, FALSE
))
1163 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1164 set_vim_var_string(VV_CMDARG
, NULL
, -1);
1175 # if defined(FEAT_DIFF) || defined(PROTO)
1177 eval_diff(origfile
, newfile
, outfile
)
1184 set_vim_var_string(VV_FNAME_IN
, origfile
, -1);
1185 set_vim_var_string(VV_FNAME_NEW
, newfile
, -1);
1186 set_vim_var_string(VV_FNAME_OUT
, outfile
, -1);
1187 (void)eval_to_bool(p_dex
, &err
, NULL
, FALSE
);
1188 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1189 set_vim_var_string(VV_FNAME_NEW
, NULL
, -1);
1190 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1194 eval_patch(origfile
, difffile
, outfile
)
1201 set_vim_var_string(VV_FNAME_IN
, origfile
, -1);
1202 set_vim_var_string(VV_FNAME_DIFF
, difffile
, -1);
1203 set_vim_var_string(VV_FNAME_OUT
, outfile
, -1);
1204 (void)eval_to_bool(p_pex
, &err
, NULL
, FALSE
);
1205 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1206 set_vim_var_string(VV_FNAME_DIFF
, NULL
, -1);
1207 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1212 * Top level evaluation function, returning a boolean.
1213 * Sets "error" to TRUE if there was an error.
1214 * Return TRUE or FALSE.
1217 eval_to_bool(arg
, error
, nextcmd
, skip
)
1221 int skip
; /* only parse, don't execute */
1228 if (eval0(arg
, &tv
, nextcmd
, !skip
) == FAIL
)
1235 retval
= (get_tv_number_chk(&tv
, error
) != 0);
1246 * Top level evaluation function, returning a string. If "skip" is TRUE,
1247 * only parsing to "nextcmd" is done, without reporting errors. Return
1248 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1251 eval_to_string_skip(arg
, nextcmd
, skip
)
1254 int skip
; /* only parse, don't execute */
1261 if (eval0(arg
, &tv
, nextcmd
, !skip
) == FAIL
|| skip
)
1265 retval
= vim_strsave(get_tv_string(&tv
));
1275 * Skip over an expression at "*pp".
1276 * Return FAIL for an error, OK otherwise.
1284 *pp
= skipwhite(*pp
);
1285 return eval1(pp
, &rettv
, FALSE
);
1289 * Top level evaluation function, returning a string.
1290 * When "convert" is TRUE convert a List into a sequence of lines and convert
1291 * a Float to a String.
1292 * Return pointer to allocated memory, or NULL for failure.
1295 eval_to_string(arg
, nextcmd
, convert
)
1303 char_u numbuf
[NUMBUFLEN
];
1305 if (eval0(arg
, &tv
, nextcmd
, TRUE
) == FAIL
)
1309 if (convert
&& tv
.v_type
== VAR_LIST
)
1311 ga_init2(&ga
, (int)sizeof(char), 80);
1312 if (tv
.vval
.v_list
!= NULL
)
1313 list_join(&ga
, tv
.vval
.v_list
, (char_u
*)"\n", TRUE
, 0);
1314 ga_append(&ga
, NUL
);
1315 retval
= (char_u
*)ga
.ga_data
;
1318 else if (convert
&& tv
.v_type
== VAR_FLOAT
)
1320 vim_snprintf((char *)numbuf
, NUMBUFLEN
, "%g", tv
.vval
.v_float
);
1321 retval
= vim_strsave(numbuf
);
1325 retval
= vim_strsave(get_tv_string(&tv
));
1333 * Call eval_to_string() without using current local variables and using
1334 * textlock. When "use_sandbox" is TRUE use the sandbox.
1337 eval_to_string_safe(arg
, nextcmd
, use_sandbox
)
1343 void *save_funccalp
;
1345 save_funccalp
= save_funccal();
1349 retval
= eval_to_string(arg
, nextcmd
, FALSE
);
1353 restore_funccal(save_funccalp
);
1358 * Top level evaluation function, returning a number.
1359 * Evaluates "expr" silently.
1360 * Returns -1 for an error.
1363 eval_to_number(expr
)
1368 char_u
*p
= skipwhite(expr
);
1372 if (eval1(&p
, &rettv
, TRUE
) == FAIL
)
1376 retval
= get_tv_number_chk(&rettv
, NULL
);
1385 * Prepare v: variable "idx" to be used.
1386 * Save the current typeval in "save_tv".
1387 * When not used yet add the variable to the v: hashtable.
1390 prepare_vimvar(idx
, save_tv
)
1394 *save_tv
= vimvars
[idx
].vv_tv
;
1395 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1396 hash_add(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1400 * Restore v: variable "idx" to typeval "save_tv".
1401 * When no longer defined, remove the variable from the v: hashtable.
1404 restore_vimvar(idx
, save_tv
)
1410 vimvars
[idx
].vv_tv
= *save_tv
;
1411 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1413 hi
= hash_find(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1414 if (HASHITEM_EMPTY(hi
))
1415 EMSG2(_(e_intern2
), "restore_vimvar()");
1417 hash_remove(&vimvarht
, hi
);
1421 #if defined(FEAT_SPELL) || defined(PROTO)
1423 * Evaluate an expression to a list with suggestions.
1424 * For the "expr:" part of 'spellsuggest'.
1425 * Returns NULL when there is an error.
1428 eval_spell_expr(badword
, expr
)
1434 list_T
*list
= NULL
;
1435 char_u
*p
= skipwhite(expr
);
1437 /* Set "v:val" to the bad word. */
1438 prepare_vimvar(VV_VAL
, &save_val
);
1439 vimvars
[VV_VAL
].vv_type
= VAR_STRING
;
1440 vimvars
[VV_VAL
].vv_str
= badword
;
1444 if (eval1(&p
, &rettv
, TRUE
) == OK
)
1446 if (rettv
.v_type
!= VAR_LIST
)
1449 list
= rettv
.vval
.v_list
;
1454 restore_vimvar(VV_VAL
, &save_val
);
1460 * "list" is supposed to contain two items: a word and a number. Return the
1461 * word in "pp" and the number as the return value.
1462 * Return -1 if anything isn't right.
1463 * Used to get the good word and score from the eval_spell_expr() result.
1466 get_spellword(list
, pp
)
1472 li
= list
->lv_first
;
1475 *pp
= get_tv_string(&li
->li_tv
);
1480 return get_tv_number(&li
->li_tv
);
1485 * Top level evaluation function.
1486 * Returns an allocated typval_T with the result.
1487 * Returns NULL when there is an error.
1490 eval_expr(arg
, nextcmd
)
1496 tv
= (typval_T
*)alloc(sizeof(typval_T
));
1497 if (tv
!= NULL
&& eval0(arg
, tv
, nextcmd
, TRUE
) == FAIL
)
1507 #if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1508 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1510 * Call some vimL function and return the result in "*rettv".
1511 * Uses argv[argc] for the function arguments. Only Number and String
1512 * arguments are currently supported.
1513 * Returns OK or FAIL.
1516 call_vim_function(func
, argc
, argv
, safe
, rettv
)
1520 int safe
; /* use the sandbox */
1528 void *save_funccalp
= NULL
;
1531 argvars
= (typval_T
*)alloc((unsigned)((argc
+ 1) * sizeof(typval_T
)));
1532 if (argvars
== NULL
)
1535 for (i
= 0; i
< argc
; i
++)
1537 /* Pass a NULL or empty argument as an empty string */
1538 if (argv
[i
] == NULL
|| *argv
[i
] == NUL
)
1540 argvars
[i
].v_type
= VAR_STRING
;
1541 argvars
[i
].vval
.v_string
= (char_u
*)"";
1545 /* Recognize a number argument, the others must be strings. */
1546 vim_str2nr(argv
[i
], NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
1547 if (len
!= 0 && len
== (int)STRLEN(argv
[i
]))
1549 argvars
[i
].v_type
= VAR_NUMBER
;
1550 argvars
[i
].vval
.v_number
= n
;
1554 argvars
[i
].v_type
= VAR_STRING
;
1555 argvars
[i
].vval
.v_string
= argv
[i
];
1561 save_funccalp
= save_funccal();
1565 rettv
->v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
1566 ret
= call_func(func
, (int)STRLEN(func
), rettv
, argc
, argvars
,
1567 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
1568 &doesrange
, TRUE
, NULL
);
1572 restore_funccal(save_funccalp
);
1582 # if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1584 * Call vimL function "func" and return the result as a string.
1585 * Returns NULL when calling the function fails.
1586 * Uses argv[argc] for the function arguments.
1589 call_func_retstr(func
, argc
, argv
, safe
)
1593 int safe
; /* use the sandbox */
1598 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1601 retval
= vim_strsave(get_tv_string(&rettv
));
1607 # if defined(FEAT_COMPL_FUNC) || defined(PROTO)
1609 * Call vimL function "func" and return the result as a number.
1610 * Returns -1 when calling the function fails.
1611 * Uses argv[argc] for the function arguments.
1614 call_func_retnr(func
, argc
, argv
, safe
)
1618 int safe
; /* use the sandbox */
1623 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1626 retval
= get_tv_number_chk(&rettv
, NULL
);
1633 * Call vimL function "func" and return the result as a List.
1634 * Uses argv[argc] for the function arguments.
1635 * Returns NULL when there is something wrong.
1638 call_func_retlist(func
, argc
, argv
, safe
)
1642 int safe
; /* use the sandbox */
1646 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1649 if (rettv
.v_type
!= VAR_LIST
)
1655 return rettv
.vval
.v_list
;
1661 * Save the current function call pointer, and set it to NULL.
1662 * Used when executing autocommands and for ":source".
1667 funccall_T
*fc
= current_funccal
;
1669 current_funccal
= NULL
;
1674 restore_funccal(vfc
)
1677 funccall_T
*fc
= (funccall_T
*)vfc
;
1679 current_funccal
= fc
;
1682 #if defined(FEAT_PROFILE) || defined(PROTO)
1684 * Prepare profiling for entering a child or something else that is not
1685 * counted for the script/function itself.
1686 * Should always be called in pair with prof_child_exit().
1689 prof_child_enter(tm
)
1690 proftime_T
*tm
; /* place to store waittime */
1692 funccall_T
*fc
= current_funccal
;
1694 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1695 profile_start(&fc
->prof_child
);
1696 script_prof_save(tm
);
1700 * Take care of time spent in a child.
1701 * Should always be called after prof_child_enter().
1705 proftime_T
*tm
; /* where waittime was stored */
1707 funccall_T
*fc
= current_funccal
;
1709 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1711 profile_end(&fc
->prof_child
);
1712 profile_sub_wait(tm
, &fc
->prof_child
); /* don't count waiting time */
1713 profile_add(&fc
->func
->uf_tm_children
, &fc
->prof_child
);
1714 profile_add(&fc
->func
->uf_tml_children
, &fc
->prof_child
);
1716 script_prof_restore(tm
);
1723 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1724 * it in "*cp". Doesn't give error messages.
1727 eval_foldexpr(arg
, cp
)
1734 int use_sandbox
= was_set_insecurely((char_u
*)"foldexpr",
1742 if (eval0(arg
, &tv
, NULL
, TRUE
) == FAIL
)
1746 /* If the result is a number, just return the number. */
1747 if (tv
.v_type
== VAR_NUMBER
)
1748 retval
= tv
.vval
.v_number
;
1749 else if (tv
.v_type
!= VAR_STRING
|| tv
.vval
.v_string
== NULL
)
1753 /* If the result is a string, check if there is a non-digit before
1755 s
= tv
.vval
.v_string
;
1756 if (!VIM_ISDIGIT(*s
) && *s
!= '-')
1758 retval
= atol((char *)s
);
1772 * ":let" list all variable values
1773 * ":let var1 var2" list variable values
1774 * ":let var = expr" assignment command.
1775 * ":let var += expr" assignment command.
1776 * ":let var -= expr" assignment command.
1777 * ":let var .= expr" assignment command.
1778 * ":let [var1, var2] = expr" unpack list.
1784 char_u
*arg
= eap
->arg
;
1785 char_u
*expr
= NULL
;
1794 argend
= skip_var_list(arg
, &var_count
, &semicolon
);
1797 if (argend
> arg
&& argend
[-1] == '.') /* for var.='str' */
1799 expr
= vim_strchr(argend
, '=');
1803 * ":let" without "=": list variables
1807 else if (!ends_excmd(*arg
))
1808 /* ":let var1 var2" */
1809 arg
= list_arg_vars(eap
, arg
, &first
);
1810 else if (!eap
->skip
)
1813 list_glob_vars(&first
);
1814 list_buf_vars(&first
);
1815 list_win_vars(&first
);
1817 list_tab_vars(&first
);
1819 list_script_vars(&first
);
1820 list_func_vars(&first
);
1821 list_vim_vars(&first
);
1823 eap
->nextcmd
= check_nextcmd(arg
);
1831 if (vim_strchr((char_u
*)"+-.", expr
[-1]) != NULL
)
1832 op
[0] = expr
[-1]; /* +=, -= or .= */
1834 expr
= skipwhite(expr
+ 1);
1838 i
= eval0(expr
, &rettv
, &eap
->nextcmd
, !eap
->skip
);
1847 (void)ex_let_vars(eap
->arg
, &rettv
, FALSE
, semicolon
, var_count
,
1855 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1856 * Handles both "var" with any type and "[var, var; var]" with a list type.
1857 * When "nextchars" is not NULL it points to a string with characters that
1858 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1860 * Returns OK or FAIL;
1863 ex_let_vars(arg_start
, tv
, copy
, semicolon
, var_count
, nextchars
)
1866 int copy
; /* copy values from "tv", don't move */
1867 int semicolon
; /* from skip_var_list() */
1868 int var_count
; /* from skip_var_list() */
1871 char_u
*arg
= arg_start
;
1880 * ":let var = expr" or ":for var in list"
1882 if (ex_let_one(arg
, tv
, copy
, nextchars
, nextchars
) == NULL
)
1888 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1890 if (tv
->v_type
!= VAR_LIST
|| (l
= tv
->vval
.v_list
) == NULL
)
1897 if (semicolon
== 0 && var_count
< i
)
1899 EMSG(_("E687: Less targets than List items"));
1902 if (var_count
- semicolon
> i
)
1904 EMSG(_("E688: More targets than List items"));
1911 arg
= skipwhite(arg
+ 1);
1912 arg
= ex_let_one(arg
, &item
->li_tv
, TRUE
, (char_u
*)",;]", nextchars
);
1913 item
= item
->li_next
;
1917 arg
= skipwhite(arg
);
1920 /* Put the rest of the list (may be empty) in the var after ';'.
1921 * Create a new list for this. */
1925 while (item
!= NULL
)
1927 list_append_tv(l
, &item
->li_tv
);
1928 item
= item
->li_next
;
1931 ltv
.v_type
= VAR_LIST
;
1933 ltv
.vval
.v_list
= l
;
1936 arg
= ex_let_one(skipwhite(arg
+ 1), <v
, FALSE
,
1937 (char_u
*)"]", nextchars
);
1943 else if (*arg
!= ',' && *arg
!= ']')
1945 EMSG2(_(e_intern2
), "ex_let_vars()");
1954 * Skip over assignable variable "var" or list of variables "[var, var]".
1955 * Used for ":let varvar = expr" and ":for varvar in expr".
1956 * For "[var, var]" increment "*var_count" for each variable.
1957 * for "[var, var; var]" set "semicolon".
1958 * Return NULL for an error.
1961 skip_var_list(arg
, var_count
, semicolon
)
1970 /* "[var, var]": find the matching ']'. */
1974 p
= skipwhite(p
+ 1); /* skip whites after '[', ';' or ',' */
1975 s
= skip_var_one(p
);
1978 EMSG2(_(e_invarg2
), p
);
1988 if (*semicolon
== 1)
1990 EMSG(_("Double ; in list of variables"));
1997 EMSG2(_(e_invarg2
), p
);
2004 return skip_var_one(arg
);
2008 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
2015 if (*arg
== '@' && arg
[1] != NUL
)
2017 return find_name_end(*arg
== '$' || *arg
== '&' ? arg
+ 1 : arg
,
2018 NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
2022 * List variables for hashtab "ht" with prefix "prefix".
2023 * If "empty" is TRUE also list NULL strings as empty strings.
2026 list_hashtable_vars(ht
, prefix
, empty
, first
)
2036 todo
= (int)ht
->ht_used
;
2037 for (hi
= ht
->ht_array
; todo
> 0 && !got_int
; ++hi
)
2039 if (!HASHITEM_EMPTY(hi
))
2043 if (empty
|| di
->di_tv
.v_type
!= VAR_STRING
2044 || di
->di_tv
.vval
.v_string
!= NULL
)
2045 list_one_var(di
, prefix
, first
);
2051 * List global variables.
2054 list_glob_vars(first
)
2057 list_hashtable_vars(&globvarht
, (char_u
*)"", TRUE
, first
);
2061 * List buffer variables.
2064 list_buf_vars(first
)
2067 char_u numbuf
[NUMBUFLEN
];
2069 list_hashtable_vars(&curbuf
->b_vars
.dv_hashtab
, (char_u
*)"b:",
2072 sprintf((char *)numbuf
, "%ld", (long)curbuf
->b_changedtick
);
2073 list_one_var_a((char_u
*)"b:", (char_u
*)"changedtick", VAR_NUMBER
,
2078 * List window variables.
2081 list_win_vars(first
)
2084 list_hashtable_vars(&curwin
->w_vars
.dv_hashtab
,
2085 (char_u
*)"w:", TRUE
, first
);
2090 * List tab page variables.
2093 list_tab_vars(first
)
2096 list_hashtable_vars(&curtab
->tp_vars
.dv_hashtab
,
2097 (char_u
*)"t:", TRUE
, first
);
2102 * List Vim variables.
2105 list_vim_vars(first
)
2108 list_hashtable_vars(&vimvarht
, (char_u
*)"v:", FALSE
, first
);
2112 * List script-local variables, if there is a script.
2115 list_script_vars(first
)
2118 if (current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
2119 list_hashtable_vars(&SCRIPT_VARS(current_SID
),
2120 (char_u
*)"s:", FALSE
, first
);
2124 * List function variables, if there is a function.
2127 list_func_vars(first
)
2130 if (current_funccal
!= NULL
)
2131 list_hashtable_vars(¤t_funccal
->l_vars
.dv_hashtab
,
2132 (char_u
*)"l:", FALSE
, first
);
2136 * List variables in "arg".
2139 list_arg_vars(eap
, arg
, first
)
2152 while (!ends_excmd(*arg
) && !got_int
)
2154 if (error
|| eap
->skip
)
2156 arg
= find_name_end(arg
, NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
2157 if (!vim_iswhite(*arg
) && !ends_excmd(*arg
))
2160 EMSG(_(e_trailing
));
2166 /* get_name_len() takes care of expanding curly braces */
2167 name_start
= name
= arg
;
2168 len
= get_name_len(&arg
, &tofree
, TRUE
, TRUE
);
2171 /* This is mainly to keep test 49 working: when expanding
2172 * curly braces fails overrule the exception error message. */
2173 if (len
< 0 && !aborting())
2176 EMSG2(_(e_invarg2
), arg
);
2185 if (get_var_tv(name
, len
, &tv
, TRUE
) == FAIL
)
2189 /* handle d.key, l[idx], f(expr) */
2191 if (handle_subscript(&arg
, &tv
, TRUE
, TRUE
) == FAIL
)
2195 if (arg
== arg_subsc
&& len
== 2 && name
[1] == ':')
2199 case 'g': list_glob_vars(first
); break;
2200 case 'b': list_buf_vars(first
); break;
2201 case 'w': list_win_vars(first
); break;
2203 case 't': list_tab_vars(first
); break;
2205 case 'v': list_vim_vars(first
); break;
2206 case 's': list_script_vars(first
); break;
2207 case 'l': list_func_vars(first
); break;
2209 EMSG2(_("E738: Can't list variables for %s"), name
);
2214 char_u numbuf
[NUMBUFLEN
];
2219 s
= echo_string(&tv
, &tf
, numbuf
, 0);
2222 list_one_var_a((char_u
*)"",
2223 arg
== arg_subsc
? name
: name_start
,
2225 s
== NULL
? (char_u
*)"" : s
,
2238 arg
= skipwhite(arg
);
2245 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2246 * Returns a pointer to the char just after the var name.
2247 * Returns NULL if there is an error.
2250 ex_let_one(arg
, tv
, copy
, endchars
, op
)
2251 char_u
*arg
; /* points to variable name */
2252 typval_T
*tv
; /* value to assign to variable */
2253 int copy
; /* copy value from "tv" */
2254 char_u
*endchars
; /* valid chars after variable name or NULL */
2255 char_u
*op
; /* "+", "-", "." or NULL*/
2260 char_u
*arg_end
= NULL
;
2263 char_u
*tofree
= NULL
;
2266 * ":let $VAR = expr": Set environment variable.
2270 /* Find the end of the name. */
2273 len
= get_env_len(&arg
);
2275 EMSG2(_(e_invarg2
), name
- 1);
2278 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2279 EMSG2(_(e_letwrong
), op
);
2280 else if (endchars
!= NULL
2281 && vim_strchr(endchars
, *skipwhite(arg
)) == NULL
)
2282 EMSG(_(e_letunexp
));
2287 p
= get_tv_string_chk(tv
);
2288 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2290 int mustfree
= FALSE
;
2291 char_u
*s
= vim_getenv(name
, &mustfree
);
2295 p
= tofree
= concat_str(s
, p
);
2302 vim_setenv(name
, p
);
2303 if (STRICMP(name
, "HOME") == 0)
2305 else if (didset_vim
&& STRICMP(name
, "VIM") == 0)
2307 else if (didset_vimruntime
2308 && STRICMP(name
, "VIMRUNTIME") == 0)
2309 didset_vimruntime
= FALSE
;
2319 * ":let &option = expr": Set option value.
2320 * ":let &l:option = expr": Set local option value.
2321 * ":let &g:option = expr": Set global option value.
2323 else if (*arg
== '&')
2325 /* Find the end of the name. */
2326 p
= find_option_end(&arg
, &opt_flags
);
2327 if (p
== NULL
|| (endchars
!= NULL
2328 && vim_strchr(endchars
, *skipwhite(p
)) == NULL
))
2329 EMSG(_(e_letunexp
));
2335 char_u
*stringval
= NULL
;
2341 n
= get_tv_number(tv
);
2342 s
= get_tv_string_chk(tv
); /* != NULL if number or string */
2343 if (s
!= NULL
&& op
!= NULL
&& *op
!= '=')
2345 opt_type
= get_option_value(arg
, &numval
,
2346 &stringval
, opt_flags
);
2347 if ((opt_type
== 1 && *op
== '.')
2348 || (opt_type
== 0 && *op
!= '.'))
2349 EMSG2(_(e_letwrong
), op
);
2352 if (opt_type
== 1) /* number */
2359 else if (opt_type
== 0 && stringval
!= NULL
) /* string */
2361 s
= concat_str(stringval
, s
);
2362 vim_free(stringval
);
2369 set_option_value(arg
, n
, s
, opt_flags
);
2373 vim_free(stringval
);
2378 * ":let @r = expr": Set register contents.
2380 else if (*arg
== '@')
2383 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2384 EMSG2(_(e_letwrong
), op
);
2385 else if (endchars
!= NULL
2386 && vim_strchr(endchars
, *skipwhite(arg
+ 1)) == NULL
)
2387 EMSG(_(e_letunexp
));
2390 char_u
*ptofree
= NULL
;
2393 p
= get_tv_string_chk(tv
);
2394 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2396 s
= get_reg_contents(*arg
== '@' ? '"' : *arg
, TRUE
, TRUE
);
2399 p
= ptofree
= concat_str(s
, p
);
2405 write_reg_contents(*arg
== '@' ? '"' : *arg
, p
, -1, FALSE
);
2413 * ":let var = expr": Set internal variable.
2414 * ":let {expr} = expr": Idem, name made with curly braces
2416 else if (eval_isnamec1(*arg
) || *arg
== '{')
2420 p
= get_lval(arg
, tv
, &lv
, FALSE
, FALSE
, FALSE
, FNE_CHECK_START
);
2421 if (p
!= NULL
&& lv
.ll_name
!= NULL
)
2423 if (endchars
!= NULL
&& vim_strchr(endchars
, *skipwhite(p
)) == NULL
)
2424 EMSG(_(e_letunexp
));
2427 set_var_lval(&lv
, p
, tv
, copy
, op
);
2435 EMSG2(_(e_invarg2
), arg
);
2441 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2444 check_changedtick(arg
)
2447 if (STRNCMP(arg
, "b:changedtick", 13) == 0 && !eval_isnamec(arg
[13]))
2449 EMSG2(_(e_readonlyvar
), arg
);
2456 * Get an lval: variable, Dict item or List item that can be assigned a value
2457 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2458 * "name.key", "name.key[expr]" etc.
2459 * Indexing only works if "name" is an existing List or Dictionary.
2460 * "name" points to the start of the name.
2461 * If "rettv" is not NULL it points to the value to be assigned.
2462 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2463 * wrong; must end in space or cmd separator.
2465 * Returns a pointer to just after the name, including indexes.
2466 * When an evaluation error occurs "lp->ll_name" is NULL;
2467 * Returns NULL for a parsing error. Still need to free items in "lp"!
2470 get_lval(name
, rettv
, lp
, unlet
, skip
, quiet
, fne_flags
)
2476 int quiet
; /* don't give error messages */
2477 int fne_flags
; /* flags for find_name_end() */
2480 char_u
*expr_start
, *expr_end
;
2491 /* Clear everything in "lp". */
2492 vim_memset(lp
, 0, sizeof(lval_T
));
2496 /* When skipping just find the end of the name. */
2498 return find_name_end(name
, NULL
, NULL
, FNE_INCL_BR
| fne_flags
);
2501 /* Find the end of the name. */
2502 p
= find_name_end(name
, &expr_start
, &expr_end
, fne_flags
);
2503 if (expr_start
!= NULL
)
2505 /* Don't expand the name when we already know there is an error. */
2506 if (unlet
&& !vim_iswhite(*p
) && !ends_excmd(*p
)
2507 && *p
!= '[' && *p
!= '.')
2509 EMSG(_(e_trailing
));
2513 lp
->ll_exp_name
= make_expanded_name(name
, expr_start
, expr_end
, p
);
2514 if (lp
->ll_exp_name
== NULL
)
2516 /* Report an invalid expression in braces, unless the
2517 * expression evaluation has been cancelled due to an
2518 * aborting error, an interrupt, or an exception. */
2519 if (!aborting() && !quiet
)
2522 EMSG2(_(e_invarg2
), name
);
2526 lp
->ll_name
= lp
->ll_exp_name
;
2531 /* Without [idx] or .key we are done. */
2532 if ((*p
!= '[' && *p
!= '.') || lp
->ll_name
== NULL
)
2537 v
= find_var(lp
->ll_name
, &ht
);
2538 if (v
== NULL
&& !quiet
)
2539 EMSG2(_(e_undefvar
), lp
->ll_name
);
2545 * Loop until no more [idx] or .key is following.
2547 lp
->ll_tv
= &v
->di_tv
;
2548 while (*p
== '[' || (*p
== '.' && lp
->ll_tv
->v_type
== VAR_DICT
))
2550 if (!(lp
->ll_tv
->v_type
== VAR_LIST
&& lp
->ll_tv
->vval
.v_list
!= NULL
)
2551 && !(lp
->ll_tv
->v_type
== VAR_DICT
2552 && lp
->ll_tv
->vval
.v_dict
!= NULL
))
2555 EMSG(_("E689: Can only index a List or Dictionary"));
2561 EMSG(_("E708: [:] must come last"));
2569 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
2574 EMSG(_(e_emptykey
));
2581 /* Get the index [expr] or the first index [expr: ]. */
2582 p
= skipwhite(p
+ 1);
2588 if (eval1(&p
, &var1
, TRUE
) == FAIL
) /* recursive! */
2590 if (get_tv_string_chk(&var1
) == NULL
)
2592 /* not a number or string */
2598 /* Optionally get the second index [ :expr]. */
2601 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2604 EMSG(_(e_dictrange
));
2609 if (rettv
!= NULL
&& (rettv
->v_type
!= VAR_LIST
2610 || rettv
->vval
.v_list
== NULL
))
2613 EMSG(_("E709: [:] requires a List value"));
2618 p
= skipwhite(p
+ 1);
2620 lp
->ll_empty2
= TRUE
;
2623 lp
->ll_empty2
= FALSE
;
2624 if (eval1(&p
, &var2
, TRUE
) == FAIL
) /* recursive! */
2630 if (get_tv_string_chk(&var2
) == NULL
)
2632 /* not a number or string */
2639 lp
->ll_range
= TRUE
;
2642 lp
->ll_range
= FALSE
;
2647 EMSG(_(e_missbrac
));
2650 if (lp
->ll_range
&& !lp
->ll_empty2
)
2655 /* Skip to past ']'. */
2659 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2663 /* "[key]": get key from "var1" */
2664 key
= get_tv_string(&var1
); /* is number or string */
2668 EMSG(_(e_emptykey
));
2674 lp
->ll_dict
= lp
->ll_tv
->vval
.v_dict
;
2675 lp
->ll_di
= dict_find(lp
->ll_dict
, key
, len
);
2676 if (lp
->ll_di
== NULL
)
2678 /* Key does not exist in dict: may need to add it. */
2679 if (*p
== '[' || *p
== '.' || unlet
)
2682 EMSG2(_(e_dictkey
), key
);
2688 lp
->ll_newkey
= vim_strsave(key
);
2690 lp
->ll_newkey
= vim_strnsave(key
, len
);
2693 if (lp
->ll_newkey
== NULL
)
2699 lp
->ll_tv
= &lp
->ll_di
->di_tv
;
2704 * Get the number and item for the only or first index of the List.
2710 lp
->ll_n1
= get_tv_number(&var1
); /* is number or string */
2714 lp
->ll_list
= lp
->ll_tv
->vval
.v_list
;
2715 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2716 if (lp
->ll_li
== NULL
)
2721 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2724 if (lp
->ll_li
== NULL
)
2726 if (lp
->ll_range
&& !lp
->ll_empty2
)
2732 * May need to find the item or absolute index for the second
2734 * When no index given: "lp->ll_empty2" is TRUE.
2735 * Otherwise "lp->ll_n2" is set to the second index.
2737 if (lp
->ll_range
&& !lp
->ll_empty2
)
2739 lp
->ll_n2
= get_tv_number(&var2
); /* is number or string */
2743 ni
= list_find(lp
->ll_list
, lp
->ll_n2
);
2746 lp
->ll_n2
= list_idx_of_item(lp
->ll_list
, ni
);
2749 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2751 lp
->ll_n1
= list_idx_of_item(lp
->ll_list
, lp
->ll_li
);
2752 if (lp
->ll_n2
< lp
->ll_n1
)
2756 lp
->ll_tv
= &lp
->ll_li
->li_tv
;
2764 * Clear lval "lp" that was filled by get_lval().
2770 vim_free(lp
->ll_exp_name
);
2771 vim_free(lp
->ll_newkey
);
2775 * Set a variable that was parsed by get_lval() to "rettv".
2776 * "endp" points to just after the parsed name.
2777 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
2780 set_var_lval(lp
, endp
, rettv
, copy
, op
)
2791 if (lp
->ll_tv
== NULL
)
2793 if (!check_changedtick(lp
->ll_name
))
2797 if (op
!= NULL
&& *op
!= '=')
2801 /* handle +=, -= and .= */
2802 if (get_var_tv(lp
->ll_name
, (int)STRLEN(lp
->ll_name
),
2805 if (tv_op(&tv
, rettv
, op
) == OK
)
2806 set_var(lp
->ll_name
, &tv
, FALSE
);
2811 set_var(lp
->ll_name
, rettv
, copy
);
2815 else if (tv_check_lock(lp
->ll_newkey
== NULL
2817 : lp
->ll_tv
->vval
.v_dict
->dv_lock
, lp
->ll_name
))
2819 else if (lp
->ll_range
)
2822 * Assign the List values to the list items.
2824 for (ri
= rettv
->vval
.v_list
->lv_first
; ri
!= NULL
; )
2826 if (op
!= NULL
&& *op
!= '=')
2827 tv_op(&lp
->ll_li
->li_tv
, &ri
->li_tv
, op
);
2830 clear_tv(&lp
->ll_li
->li_tv
);
2831 copy_tv(&ri
->li_tv
, &lp
->ll_li
->li_tv
);
2834 if (ri
== NULL
|| (!lp
->ll_empty2
&& lp
->ll_n2
== lp
->ll_n1
))
2836 if (lp
->ll_li
->li_next
== NULL
)
2838 /* Need to add an empty item. */
2839 if (list_append_number(lp
->ll_list
, 0) == FAIL
)
2845 lp
->ll_li
= lp
->ll_li
->li_next
;
2849 EMSG(_("E710: List value has more items than target"));
2850 else if (lp
->ll_empty2
2851 ? (lp
->ll_li
!= NULL
&& lp
->ll_li
->li_next
!= NULL
)
2852 : lp
->ll_n1
!= lp
->ll_n2
)
2853 EMSG(_("E711: List value has not enough items"));
2858 * Assign to a List or Dictionary item.
2860 if (lp
->ll_newkey
!= NULL
)
2862 if (op
!= NULL
&& *op
!= '=')
2864 EMSG2(_(e_letwrong
), op
);
2868 /* Need to add an item to the Dictionary. */
2869 di
= dictitem_alloc(lp
->ll_newkey
);
2872 if (dict_add(lp
->ll_tv
->vval
.v_dict
, di
) == FAIL
)
2877 lp
->ll_tv
= &di
->di_tv
;
2879 else if (op
!= NULL
&& *op
!= '=')
2881 tv_op(lp
->ll_tv
, rettv
, op
);
2885 clear_tv(lp
->ll_tv
);
2888 * Assign the value to the variable or list item.
2891 copy_tv(rettv
, lp
->ll_tv
);
2894 *lp
->ll_tv
= *rettv
;
2895 lp
->ll_tv
->v_lock
= 0;
2902 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2903 * Returns OK or FAIL.
2912 char_u numbuf
[NUMBUFLEN
];
2915 /* Can't do anything with a Funcref or a Dict on the right. */
2916 if (tv2
->v_type
!= VAR_FUNC
&& tv2
->v_type
!= VAR_DICT
)
2918 switch (tv1
->v_type
)
2925 if (*op
!= '+' || tv2
->v_type
!= VAR_LIST
)
2928 if (tv1
->vval
.v_list
!= NULL
&& tv2
->vval
.v_list
!= NULL
)
2929 list_extend(tv1
->vval
.v_list
, tv2
->vval
.v_list
, NULL
);
2934 if (tv2
->v_type
== VAR_LIST
)
2936 if (*op
== '+' || *op
== '-')
2938 /* nr += nr or nr -= nr*/
2939 n
= get_tv_number(tv1
);
2941 if (tv2
->v_type
== VAR_FLOAT
)
2946 f
+= tv2
->vval
.v_float
;
2948 f
-= tv2
->vval
.v_float
;
2950 tv1
->v_type
= VAR_FLOAT
;
2951 tv1
->vval
.v_float
= f
;
2957 n
+= get_tv_number(tv2
);
2959 n
-= get_tv_number(tv2
);
2961 tv1
->v_type
= VAR_NUMBER
;
2962 tv1
->vval
.v_number
= n
;
2967 if (tv2
->v_type
== VAR_FLOAT
)
2971 s
= get_tv_string(tv1
);
2972 s
= concat_str(s
, get_tv_string_buf(tv2
, numbuf
));
2974 tv1
->v_type
= VAR_STRING
;
2975 tv1
->vval
.v_string
= s
;
2984 if (*op
== '.' || (tv2
->v_type
!= VAR_FLOAT
2985 && tv2
->v_type
!= VAR_NUMBER
2986 && tv2
->v_type
!= VAR_STRING
))
2988 if (tv2
->v_type
== VAR_FLOAT
)
2989 f
= tv2
->vval
.v_float
;
2991 f
= get_tv_number(tv2
);
2993 tv1
->vval
.v_float
+= f
;
2995 tv1
->vval
.v_float
-= f
;
3002 EMSG2(_(e_letwrong
), op
);
3007 * Add a watcher to a list.
3010 list_add_watch(l
, lw
)
3014 lw
->lw_next
= l
->lv_watch
;
3019 * Remove a watcher from a list.
3020 * No warning when it isn't found...
3023 list_rem_watch(l
, lwrem
)
3027 listwatch_T
*lw
, **lwp
;
3030 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
3042 * Just before removing an item from a list: advance watchers to the next
3046 list_fix_watch(l
, item
)
3052 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
3053 if (lw
->lw_item
== item
)
3054 lw
->lw_item
= item
->li_next
;
3058 * Evaluate the expression used in a ":for var in expr" command.
3059 * "arg" points to "var".
3060 * Set "*errp" to TRUE for an error, FALSE otherwise;
3061 * Return a pointer that holds the info. Null when there is an error.
3064 eval_for_line(arg
, errp
, nextcmdp
, skip
)
3075 *errp
= TRUE
; /* default: there is an error */
3077 fi
= (forinfo_T
*)alloc_clear(sizeof(forinfo_T
));
3081 expr
= skip_var_list(arg
, &fi
->fi_varcount
, &fi
->fi_semicolon
);
3085 expr
= skipwhite(expr
);
3086 if (expr
[0] != 'i' || expr
[1] != 'n' || !vim_iswhite(expr
[2]))
3088 EMSG(_("E690: Missing \"in\" after :for"));
3094 if (eval0(skipwhite(expr
+ 2), &tv
, nextcmdp
, !skip
) == OK
)
3100 if (tv
.v_type
!= VAR_LIST
|| l
== NULL
)
3107 /* No need to increment the refcount, it's already set for the
3108 * list being used in "tv". */
3110 list_add_watch(l
, &fi
->fi_lw
);
3111 fi
->fi_lw
.lw_item
= l
->lv_first
;
3122 * Use the first item in a ":for" list. Advance to the next.
3123 * Assign the values to the variable (list). "arg" points to the first one.
3124 * Return TRUE when a valid item was found, FALSE when at end of list or
3128 next_for_item(fi_void
, arg
)
3132 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3136 item
= fi
->fi_lw
.lw_item
;
3141 fi
->fi_lw
.lw_item
= item
->li_next
;
3142 result
= (ex_let_vars(arg
, &item
->li_tv
, TRUE
,
3143 fi
->fi_semicolon
, fi
->fi_varcount
, NULL
) == OK
);
3149 * Free the structure used to store info used by ":for".
3152 free_for_info(fi_void
)
3155 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3157 if (fi
!= NULL
&& fi
->fi_list
!= NULL
)
3159 list_rem_watch(fi
->fi_list
, &fi
->fi_lw
);
3160 list_unref(fi
->fi_list
);
3165 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3168 set_context_for_expression(xp
, arg
, cmdidx
)
3177 if (cmdidx
== CMD_let
)
3179 xp
->xp_context
= EXPAND_USER_VARS
;
3180 if (vim_strpbrk(arg
, (char_u
*)"\"'+-*/%.=!?~|&$([<>,#") == NULL
)
3182 /* ":let var1 var2 ...": find last space. */
3183 for (p
= arg
+ STRLEN(arg
); p
>= arg
; )
3186 mb_ptr_back(arg
, p
);
3187 if (vim_iswhite(*p
))
3194 xp
->xp_context
= cmdidx
== CMD_call
? EXPAND_FUNCTIONS
3195 : EXPAND_EXPRESSION
;
3196 while ((xp
->xp_pattern
= vim_strpbrk(arg
,
3197 (char_u
*)"\"'+-*/%.=!?~|&$([<>,#")) != NULL
)
3199 c
= *xp
->xp_pattern
;
3202 c
= xp
->xp_pattern
[1];
3206 xp
->xp_context
= cmdidx
!= CMD_let
|| got_eq
3207 ? EXPAND_EXPRESSION
: EXPAND_NOTHING
;
3211 xp
->xp_context
= EXPAND_SETTINGS
;
3212 if ((c
== 'l' || c
== 'g') && xp
->xp_pattern
[2] == ':')
3213 xp
->xp_pattern
+= 2;
3219 /* environment variable */
3220 xp
->xp_context
= EXPAND_ENV_VARS
;
3225 xp
->xp_context
= EXPAND_EXPRESSION
;
3228 && xp
->xp_context
== EXPAND_FUNCTIONS
3229 && vim_strchr(xp
->xp_pattern
, '(') == NULL
)
3231 /* Function name can start with "<SNR>" */
3234 else if (cmdidx
!= CMD_let
|| got_eq
)
3236 if (c
== '"') /* string */
3238 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '"')
3239 if (c
== '\\' && xp
->xp_pattern
[1] != NUL
)
3241 xp
->xp_context
= EXPAND_NOTHING
;
3243 else if (c
== '\'') /* literal string */
3245 /* Trick: '' is like stopping and starting a literal string. */
3246 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '\'')
3248 xp
->xp_context
= EXPAND_NOTHING
;
3252 if (xp
->xp_pattern
[1] == '|')
3255 xp
->xp_context
= EXPAND_EXPRESSION
;
3258 xp
->xp_context
= EXPAND_COMMANDS
;
3261 xp
->xp_context
= EXPAND_EXPRESSION
;
3264 /* Doesn't look like something valid, expand as an expression
3266 xp
->xp_context
= EXPAND_EXPRESSION
;
3267 arg
= xp
->xp_pattern
;
3269 while ((c
= *++arg
) != NUL
&& (c
== ' ' || c
== '\t'))
3272 xp
->xp_pattern
= arg
;
3275 #endif /* FEAT_CMDL_COMPL */
3278 * ":1,25call func(arg1, arg2)" function call.
3284 char_u
*arg
= eap
->arg
;
3295 tofree
= trans_function_name(&arg
, eap
->skip
, TFN_INT
, &fudi
);
3296 if (fudi
.fd_newkey
!= NULL
)
3298 /* Still need to give an error message for missing key. */
3299 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
3300 vim_free(fudi
.fd_newkey
);
3305 /* Increase refcount on dictionary, it could get deleted when evaluating
3307 if (fudi
.fd_dict
!= NULL
)
3308 ++fudi
.fd_dict
->dv_refcount
;
3310 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3311 len
= (int)STRLEN(tofree
);
3312 name
= deref_func_name(tofree
, &len
);
3314 /* Skip white space to allow ":call func ()". Not good, but required for
3315 * backward compatibility. */
3316 startarg
= skipwhite(arg
);
3317 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
3319 if (*startarg
!= '(')
3321 EMSG2(_("E107: Missing parentheses: %s"), eap
->arg
);
3326 * When skipping, evaluate the function once, to find the end of the
3328 * When the function takes a range, this is discovered after the first
3329 * call, and the loop is broken.
3334 lnum
= eap
->line2
; /* do it once, also with an invalid range */
3338 for ( ; lnum
<= eap
->line2
; ++lnum
)
3340 if (!eap
->skip
&& eap
->addr_count
> 0)
3342 curwin
->w_cursor
.lnum
= lnum
;
3343 curwin
->w_cursor
.col
= 0;
3346 if (get_func_tv(name
, (int)STRLEN(name
), &rettv
, &arg
,
3347 eap
->line1
, eap
->line2
, &doesrange
,
3348 !eap
->skip
, fudi
.fd_dict
) == FAIL
)
3354 /* Handle a function returning a Funcref, Dictionary or List. */
3355 if (handle_subscript(&arg
, &rettv
, !eap
->skip
, TRUE
) == FAIL
)
3362 if (doesrange
|| eap
->skip
)
3365 /* Stop when immediately aborting on error, or when an interrupt
3366 * occurred or an exception was thrown but not caught.
3367 * get_func_tv() returned OK, so that the check for trailing
3368 * characters below is executed. */
3377 /* Check for trailing illegal characters and a following command. */
3378 if (!ends_excmd(*arg
))
3381 EMSG(_(e_trailing
));
3384 eap
->nextcmd
= check_nextcmd(arg
);
3388 dict_unref(fudi
.fd_dict
);
3393 * ":unlet[!] var1 ... " command.
3399 ex_unletlock(eap
, eap
->arg
, 0);
3403 * ":lockvar" and ":unlockvar" commands
3409 char_u
*arg
= eap
->arg
;
3414 else if (vim_isdigit(*arg
))
3416 deep
= getdigits(&arg
);
3417 arg
= skipwhite(arg
);
3420 ex_unletlock(eap
, arg
, deep
);
3424 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3427 ex_unletlock(eap
, argstart
, deep
)
3432 char_u
*arg
= argstart
;
3439 /* Parse the name and find the end. */
3440 name_end
= get_lval(arg
, NULL
, &lv
, TRUE
, eap
->skip
|| error
, FALSE
,
3442 if (lv
.ll_name
== NULL
)
3443 error
= TRUE
; /* error but continue parsing */
3444 if (name_end
== NULL
|| (!vim_iswhite(*name_end
)
3445 && !ends_excmd(*name_end
)))
3447 if (name_end
!= NULL
)
3450 EMSG(_(e_trailing
));
3452 if (!(eap
->skip
|| error
))
3457 if (!error
&& !eap
->skip
)
3459 if (eap
->cmdidx
== CMD_unlet
)
3461 if (do_unlet_var(&lv
, name_end
, eap
->forceit
) == FAIL
)
3466 if (do_lock_var(&lv
, name_end
, deep
,
3467 eap
->cmdidx
== CMD_lockvar
) == FAIL
)
3475 arg
= skipwhite(name_end
);
3476 } while (!ends_excmd(*arg
));
3478 eap
->nextcmd
= check_nextcmd(arg
);
3482 do_unlet_var(lp
, name_end
, forceit
)
3490 if (lp
->ll_tv
== NULL
)
3495 /* Normal name or expanded name. */
3496 if (check_changedtick(lp
->ll_name
))
3498 else if (do_unlet(lp
->ll_name
, forceit
) == FAIL
)
3502 else if (tv_check_lock(lp
->ll_tv
->v_lock
, lp
->ll_name
))
3504 else if (lp
->ll_range
)
3508 /* Delete a range of List items. */
3509 while (lp
->ll_li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3511 li
= lp
->ll_li
->li_next
;
3512 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3519 if (lp
->ll_list
!= NULL
)
3520 /* unlet a List item. */
3521 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3523 /* unlet a Dictionary item. */
3524 dictitem_remove(lp
->ll_dict
, lp
->ll_di
);
3531 * "unlet" a variable. Return OK if it existed, FAIL if not.
3532 * When "forceit" is TRUE don't complain if the variable doesn't exist.
3535 do_unlet(name
, forceit
)
3544 ht
= find_var_ht(name
, &varname
);
3545 if (ht
!= NULL
&& *varname
!= NUL
)
3547 hi
= hash_find(ht
, varname
);
3548 if (!HASHITEM_EMPTY(hi
))
3551 if (var_check_fixed(di
->di_flags
, name
)
3552 || var_check_ro(di
->di_flags
, name
))
3560 EMSG2(_("E108: No such variable: \"%s\""), name
);
3565 * Lock or unlock variable indicated by "lp".
3566 * "deep" is the levels to go (-1 for unlimited);
3567 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3570 do_lock_var(lp
, name_end
, deep
, lock
)
3580 if (deep
== 0) /* nothing to do */
3583 if (lp
->ll_tv
== NULL
)
3588 /* Normal name or expanded name. */
3589 if (check_changedtick(lp
->ll_name
))
3593 di
= find_var(lp
->ll_name
, NULL
);
3599 di
->di_flags
|= DI_FLAGS_LOCK
;
3601 di
->di_flags
&= ~DI_FLAGS_LOCK
;
3602 item_lock(&di
->di_tv
, deep
, lock
);
3607 else if (lp
->ll_range
)
3609 listitem_T
*li
= lp
->ll_li
;
3611 /* (un)lock a range of List items. */
3612 while (li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3614 item_lock(&li
->li_tv
, deep
, lock
);
3619 else if (lp
->ll_list
!= NULL
)
3620 /* (un)lock a List item. */
3621 item_lock(&lp
->ll_li
->li_tv
, deep
, lock
);
3623 /* un(lock) a Dictionary item. */
3624 item_lock(&lp
->ll_di
->di_tv
, deep
, lock
);
3630 * Lock or unlock an item. "deep" is nr of levels to go.
3633 item_lock(tv
, deep
, lock
)
3638 static int recurse
= 0;
3645 if (recurse
>= DICT_MAXNEST
)
3647 EMSG(_("E743: variable nested too deep for (un)lock"));
3654 /* lock/unlock the item itself */
3656 tv
->v_lock
|= VAR_LOCKED
;
3658 tv
->v_lock
&= ~VAR_LOCKED
;
3663 if ((l
= tv
->vval
.v_list
) != NULL
)
3666 l
->lv_lock
|= VAR_LOCKED
;
3668 l
->lv_lock
&= ~VAR_LOCKED
;
3669 if (deep
< 0 || deep
> 1)
3670 /* recursive: lock/unlock the items the List contains */
3671 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
3672 item_lock(&li
->li_tv
, deep
- 1, lock
);
3676 if ((d
= tv
->vval
.v_dict
) != NULL
)
3679 d
->dv_lock
|= VAR_LOCKED
;
3681 d
->dv_lock
&= ~VAR_LOCKED
;
3682 if (deep
< 0 || deep
> 1)
3684 /* recursive: lock/unlock the items the List contains */
3685 todo
= (int)d
->dv_hashtab
.ht_used
;
3686 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
3688 if (!HASHITEM_EMPTY(hi
))
3691 item_lock(&HI2DI(hi
)->di_tv
, deep
- 1, lock
);
3701 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3702 * or it refers to a List or Dictionary that is locked.
3708 return (tv
->v_lock
& VAR_LOCKED
)
3709 || (tv
->v_type
== VAR_LIST
3710 && tv
->vval
.v_list
!= NULL
3711 && (tv
->vval
.v_list
->lv_lock
& VAR_LOCKED
))
3712 || (tv
->v_type
== VAR_DICT
3713 && tv
->vval
.v_dict
!= NULL
3714 && (tv
->vval
.v_dict
->dv_lock
& VAR_LOCKED
));
3717 #if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3719 * Delete all "menutrans_" variables.
3722 del_menutrans_vars()
3727 hash_lock(&globvarht
);
3728 todo
= (int)globvarht
.ht_used
;
3729 for (hi
= globvarht
.ht_array
; todo
> 0 && !got_int
; ++hi
)
3731 if (!HASHITEM_EMPTY(hi
))
3734 if (STRNCMP(HI2DI(hi
)->di_key
, "menutrans_", 10) == 0)
3735 delete_var(&globvarht
, hi
);
3738 hash_unlock(&globvarht
);
3742 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3745 * Local string buffer for the next two functions to store a variable name
3746 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3747 * get_user_var_name().
3750 static char_u
*cat_prefix_varname
__ARGS((int prefix
, char_u
*name
));
3752 static char_u
*varnamebuf
= NULL
;
3753 static int varnamebuflen
= 0;
3756 * Function to concatenate a prefix and a variable name.
3759 cat_prefix_varname(prefix
, name
)
3765 len
= (int)STRLEN(name
) + 3;
3766 if (len
> varnamebuflen
)
3768 vim_free(varnamebuf
);
3769 len
+= 10; /* some additional space */
3770 varnamebuf
= alloc(len
);
3771 if (varnamebuf
== NULL
)
3776 varnamebuflen
= len
;
3778 *varnamebuf
= prefix
;
3779 varnamebuf
[1] = ':';
3780 STRCPY(varnamebuf
+ 2, name
);
3785 * Function given to ExpandGeneric() to obtain the list of user defined
3786 * (global/buffer/window/built-in) variable names.
3790 get_user_var_name(xp
, idx
)
3794 static long_u gdone
;
3795 static long_u bdone
;
3796 static long_u wdone
;
3798 static long_u tdone
;
3801 static hashitem_T
*hi
;
3806 gdone
= bdone
= wdone
= vidx
= 0;
3812 /* Global variables */
3813 if (gdone
< globvarht
.ht_used
)
3816 hi
= globvarht
.ht_array
;
3819 while (HASHITEM_EMPTY(hi
))
3821 if (STRNCMP("g:", xp
->xp_pattern
, 2) == 0)
3822 return cat_prefix_varname('g', hi
->hi_key
);
3827 ht
= &curbuf
->b_vars
.dv_hashtab
;
3828 if (bdone
< ht
->ht_used
)
3834 while (HASHITEM_EMPTY(hi
))
3836 return cat_prefix_varname('b', hi
->hi_key
);
3838 if (bdone
== ht
->ht_used
)
3841 return (char_u
*)"b:changedtick";
3845 ht
= &curwin
->w_vars
.dv_hashtab
;
3846 if (wdone
< ht
->ht_used
)
3852 while (HASHITEM_EMPTY(hi
))
3854 return cat_prefix_varname('w', hi
->hi_key
);
3859 ht
= &curtab
->tp_vars
.dv_hashtab
;
3860 if (tdone
< ht
->ht_used
)
3866 while (HASHITEM_EMPTY(hi
))
3868 return cat_prefix_varname('t', hi
->hi_key
);
3874 return cat_prefix_varname('v', (char_u
*)vimvars
[vidx
++].vv_name
);
3876 vim_free(varnamebuf
);
3882 #endif /* FEAT_CMDL_COMPL */
3885 * types for expressions.
3890 , TYPE_EQUAL
/* == */
3891 , TYPE_NEQUAL
/* != */
3892 , TYPE_GREATER
/* > */
3893 , TYPE_GEQUAL
/* >= */
3894 , TYPE_SMALLER
/* < */
3895 , TYPE_SEQUAL
/* <= */
3896 , TYPE_MATCH
/* =~ */
3897 , TYPE_NOMATCH
/* !~ */
3901 * The "evaluate" argument: When FALSE, the argument is only parsed but not
3902 * executed. The function may return OK, but the rettv will be of type
3903 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3907 * Handle zero level expression.
3908 * This calls eval1() and handles error message and nextcmd.
3909 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
3910 * Note: "rettv.v_lock" is not set.
3911 * Return OK or FAIL.
3914 eval0(arg
, rettv
, nextcmd
, evaluate
)
3924 ret
= eval1(&p
, rettv
, evaluate
);
3925 if (ret
== FAIL
|| !ends_excmd(*p
))
3930 * Report the invalid expression unless the expression evaluation has
3931 * been cancelled due to an aborting error, an interrupt, or an
3935 EMSG2(_(e_invexpr2
), arg
);
3938 if (nextcmd
!= NULL
)
3939 *nextcmd
= check_nextcmd(p
);
3945 * Handle top level expression:
3946 * expr1 ? expr0 : expr0
3948 * "arg" must point to the first non-white of the expression.
3949 * "arg" is advanced to the next non-white after the recognized expression.
3951 * Note: "rettv.v_lock" is not set.
3953 * Return OK or FAIL.
3956 eval1(arg
, rettv
, evaluate
)
3965 * Get the first variable.
3967 if (eval2(arg
, rettv
, evaluate
) == FAIL
)
3970 if ((*arg
)[0] == '?')
3977 if (get_tv_number_chk(rettv
, &error
) != 0)
3985 * Get the second variable.
3987 *arg
= skipwhite(*arg
+ 1);
3988 if (eval1(arg
, rettv
, evaluate
&& result
) == FAIL
) /* recursive! */
3992 * Check for the ":".
3994 if ((*arg
)[0] != ':')
3996 EMSG(_("E109: Missing ':' after '?'"));
3997 if (evaluate
&& result
)
4003 * Get the third variable.
4005 *arg
= skipwhite(*arg
+ 1);
4006 if (eval1(arg
, &var2
, evaluate
&& !result
) == FAIL
) /* recursive! */
4008 if (evaluate
&& result
)
4012 if (evaluate
&& !result
)
4020 * Handle first level expression:
4021 * expr2 || expr2 || expr2 logical OR
4023 * "arg" must point to the first non-white of the expression.
4024 * "arg" is advanced to the next non-white after the recognized expression.
4026 * Return OK or FAIL.
4029 eval2(arg
, rettv
, evaluate
)
4040 * Get the first variable.
4042 if (eval3(arg
, rettv
, evaluate
) == FAIL
)
4046 * Repeat until there is no following "||".
4050 while ((*arg
)[0] == '|' && (*arg
)[1] == '|')
4052 if (evaluate
&& first
)
4054 if (get_tv_number_chk(rettv
, &error
) != 0)
4063 * Get the second variable.
4065 *arg
= skipwhite(*arg
+ 2);
4066 if (eval3(arg
, &var2
, evaluate
&& !result
) == FAIL
)
4070 * Compute the result.
4072 if (evaluate
&& !result
)
4074 if (get_tv_number_chk(&var2
, &error
) != 0)
4082 rettv
->v_type
= VAR_NUMBER
;
4083 rettv
->vval
.v_number
= result
;
4091 * Handle second level expression:
4092 * expr3 && expr3 && expr3 logical AND
4094 * "arg" must point to the first non-white of the expression.
4095 * "arg" is advanced to the next non-white after the recognized expression.
4097 * Return OK or FAIL.
4100 eval3(arg
, rettv
, evaluate
)
4111 * Get the first variable.
4113 if (eval4(arg
, rettv
, evaluate
) == FAIL
)
4117 * Repeat until there is no following "&&".
4121 while ((*arg
)[0] == '&' && (*arg
)[1] == '&')
4123 if (evaluate
&& first
)
4125 if (get_tv_number_chk(rettv
, &error
) == 0)
4134 * Get the second variable.
4136 *arg
= skipwhite(*arg
+ 2);
4137 if (eval4(arg
, &var2
, evaluate
&& result
) == FAIL
)
4141 * Compute the result.
4143 if (evaluate
&& result
)
4145 if (get_tv_number_chk(&var2
, &error
) == 0)
4153 rettv
->v_type
= VAR_NUMBER
;
4154 rettv
->vval
.v_number
= result
;
4162 * Handle third level expression:
4174 * "arg" must point to the first non-white of the expression.
4175 * "arg" is advanced to the next non-white after the recognized expression.
4177 * Return OK or FAIL.
4180 eval4(arg
, rettv
, evaluate
)
4188 exptype_T type
= TYPE_UNKNOWN
;
4189 int type_is
= FALSE
; /* TRUE for "is" and "isnot" */
4193 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4194 regmatch_T regmatch
;
4199 * Get the first variable.
4201 if (eval5(arg
, rettv
, evaluate
) == FAIL
)
4207 case '=': if (p
[1] == '=')
4209 else if (p
[1] == '~')
4212 case '!': if (p
[1] == '=')
4214 else if (p
[1] == '~')
4215 type
= TYPE_NOMATCH
;
4217 case '>': if (p
[1] != '=')
4219 type
= TYPE_GREATER
;
4225 case '<': if (p
[1] != '=')
4227 type
= TYPE_SMALLER
;
4233 case 'i': if (p
[1] == 's')
4235 if (p
[2] == 'n' && p
[3] == 'o' && p
[4] == 't')
4237 if (!vim_isIDc(p
[len
]))
4239 type
= len
== 2 ? TYPE_EQUAL
: TYPE_NEQUAL
;
4247 * If there is a comparative operator, use it.
4249 if (type
!= TYPE_UNKNOWN
)
4251 /* extra question mark appended: ignore case */
4257 /* extra '#' appended: match case */
4258 else if (p
[len
] == '#')
4263 /* nothing appended: use 'ignorecase' */
4268 * Get the second variable.
4270 *arg
= skipwhite(p
+ len
);
4271 if (eval5(arg
, &var2
, evaluate
) == FAIL
)
4279 if (type_is
&& rettv
->v_type
!= var2
.v_type
)
4281 /* For "is" a different type always means FALSE, for "notis"
4283 n1
= (type
== TYPE_NEQUAL
);
4285 else if (rettv
->v_type
== VAR_LIST
|| var2
.v_type
== VAR_LIST
)
4289 n1
= (rettv
->v_type
== var2
.v_type
4290 && rettv
->vval
.v_list
== var2
.vval
.v_list
);
4291 if (type
== TYPE_NEQUAL
)
4294 else if (rettv
->v_type
!= var2
.v_type
4295 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4297 if (rettv
->v_type
!= var2
.v_type
)
4298 EMSG(_("E691: Can only compare List with List"));
4300 EMSG(_("E692: Invalid operation for Lists"));
4307 /* Compare two Lists for being equal or unequal. */
4308 n1
= list_equal(rettv
->vval
.v_list
, var2
.vval
.v_list
, ic
);
4309 if (type
== TYPE_NEQUAL
)
4314 else if (rettv
->v_type
== VAR_DICT
|| var2
.v_type
== VAR_DICT
)
4318 n1
= (rettv
->v_type
== var2
.v_type
4319 && rettv
->vval
.v_dict
== var2
.vval
.v_dict
);
4320 if (type
== TYPE_NEQUAL
)
4323 else if (rettv
->v_type
!= var2
.v_type
4324 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4326 if (rettv
->v_type
!= var2
.v_type
)
4327 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4329 EMSG(_("E736: Invalid operation for Dictionary"));
4336 /* Compare two Dictionaries for being equal or unequal. */
4337 n1
= dict_equal(rettv
->vval
.v_dict
, var2
.vval
.v_dict
, ic
);
4338 if (type
== TYPE_NEQUAL
)
4343 else if (rettv
->v_type
== VAR_FUNC
|| var2
.v_type
== VAR_FUNC
)
4345 if (rettv
->v_type
!= var2
.v_type
4346 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4348 if (rettv
->v_type
!= var2
.v_type
)
4349 EMSG(_("E693: Can only compare Funcref with Funcref"));
4351 EMSG(_("E694: Invalid operation for Funcrefs"));
4358 /* Compare two Funcrefs for being equal or unequal. */
4359 if (rettv
->vval
.v_string
== NULL
4360 || var2
.vval
.v_string
== NULL
)
4363 n1
= STRCMP(rettv
->vval
.v_string
,
4364 var2
.vval
.v_string
) == 0;
4365 if (type
== TYPE_NEQUAL
)
4372 * If one of the two variables is a float, compare as a float.
4373 * When using "=~" or "!~", always compare as string.
4375 else if ((rettv
->v_type
== VAR_FLOAT
|| var2
.v_type
== VAR_FLOAT
)
4376 && type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4380 if (rettv
->v_type
== VAR_FLOAT
)
4381 f1
= rettv
->vval
.v_float
;
4383 f1
= get_tv_number(rettv
);
4384 if (var2
.v_type
== VAR_FLOAT
)
4385 f2
= var2
.vval
.v_float
;
4387 f2
= get_tv_number(&var2
);
4391 case TYPE_EQUAL
: n1
= (f1
== f2
); break;
4392 case TYPE_NEQUAL
: n1
= (f1
!= f2
); break;
4393 case TYPE_GREATER
: n1
= (f1
> f2
); break;
4394 case TYPE_GEQUAL
: n1
= (f1
>= f2
); break;
4395 case TYPE_SMALLER
: n1
= (f1
< f2
); break;
4396 case TYPE_SEQUAL
: n1
= (f1
<= f2
); break;
4399 case TYPE_NOMATCH
: break; /* avoid gcc warning */
4405 * If one of the two variables is a number, compare as a number.
4406 * When using "=~" or "!~", always compare as string.
4408 else if ((rettv
->v_type
== VAR_NUMBER
|| var2
.v_type
== VAR_NUMBER
)
4409 && type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4411 n1
= get_tv_number(rettv
);
4412 n2
= get_tv_number(&var2
);
4415 case TYPE_EQUAL
: n1
= (n1
== n2
); break;
4416 case TYPE_NEQUAL
: n1
= (n1
!= n2
); break;
4417 case TYPE_GREATER
: n1
= (n1
> n2
); break;
4418 case TYPE_GEQUAL
: n1
= (n1
>= n2
); break;
4419 case TYPE_SMALLER
: n1
= (n1
< n2
); break;
4420 case TYPE_SEQUAL
: n1
= (n1
<= n2
); break;
4423 case TYPE_NOMATCH
: break; /* avoid gcc warning */
4428 s1
= get_tv_string_buf(rettv
, buf1
);
4429 s2
= get_tv_string_buf(&var2
, buf2
);
4430 if (type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4431 i
= ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
);
4437 case TYPE_EQUAL
: n1
= (i
== 0); break;
4438 case TYPE_NEQUAL
: n1
= (i
!= 0); break;
4439 case TYPE_GREATER
: n1
= (i
> 0); break;
4440 case TYPE_GEQUAL
: n1
= (i
>= 0); break;
4441 case TYPE_SMALLER
: n1
= (i
< 0); break;
4442 case TYPE_SEQUAL
: n1
= (i
<= 0); break;
4446 /* avoid 'l' flag in 'cpoptions' */
4448 p_cpo
= (char_u
*)"";
4449 regmatch
.regprog
= vim_regcomp(s2
,
4450 RE_MAGIC
+ RE_STRING
);
4451 regmatch
.rm_ic
= ic
;
4452 if (regmatch
.regprog
!= NULL
)
4454 n1
= vim_regexec_nl(®match
, s1
, (colnr_T
)0);
4455 vim_free(regmatch
.regprog
);
4456 if (type
== TYPE_NOMATCH
)
4462 case TYPE_UNKNOWN
: break; /* avoid gcc warning */
4467 rettv
->v_type
= VAR_NUMBER
;
4468 rettv
->vval
.v_number
= n1
;
4476 * Handle fourth level expression:
4478 * - number subtraction
4479 * . string concatenation
4481 * "arg" must point to the first non-white of the expression.
4482 * "arg" is advanced to the next non-white after the recognized expression.
4484 * Return OK or FAIL.
4487 eval5(arg
, rettv
, evaluate
)
4497 float_T f1
= 0, f2
= 0;
4500 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4504 * Get the first variable.
4506 if (eval6(arg
, rettv
, evaluate
, FALSE
) == FAIL
)
4510 * Repeat computing, until no '+', '-' or '.' is following.
4515 if (op
!= '+' && op
!= '-' && op
!= '.')
4518 if ((op
!= '+' || rettv
->v_type
!= VAR_LIST
)
4520 && (op
== '.' || rettv
->v_type
!= VAR_FLOAT
)
4524 /* For "list + ...", an illegal use of the first operand as
4525 * a number cannot be determined before evaluating the 2nd
4526 * operand: if this is also a list, all is ok.
4527 * For "something . ...", "something - ..." or "non-list + ...",
4528 * we know that the first operand needs to be a string or number
4529 * without evaluating the 2nd operand. So check before to avoid
4530 * side effects after an error. */
4531 if (evaluate
&& get_tv_string_chk(rettv
) == NULL
)
4539 * Get the second variable.
4541 *arg
= skipwhite(*arg
+ 1);
4542 if (eval6(arg
, &var2
, evaluate
, op
== '.') == FAIL
)
4551 * Compute the result.
4555 s1
= get_tv_string_buf(rettv
, buf1
); /* already checked */
4556 s2
= get_tv_string_buf_chk(&var2
, buf2
);
4557 if (s2
== NULL
) /* type error ? */
4563 p
= concat_str(s1
, s2
);
4565 rettv
->v_type
= VAR_STRING
;
4566 rettv
->vval
.v_string
= p
;
4568 else if (op
== '+' && rettv
->v_type
== VAR_LIST
4569 && var2
.v_type
== VAR_LIST
)
4571 /* concatenate Lists */
4572 if (list_concat(rettv
->vval
.v_list
, var2
.vval
.v_list
,
4587 if (rettv
->v_type
== VAR_FLOAT
)
4589 f1
= rettv
->vval
.v_float
;
4595 n1
= get_tv_number_chk(rettv
, &error
);
4598 /* This can only happen for "list + non-list". For
4599 * "non-list + ..." or "something - ...", we returned
4600 * before evaluating the 2nd operand. */
4605 if (var2
.v_type
== VAR_FLOAT
)
4610 if (var2
.v_type
== VAR_FLOAT
)
4612 f2
= var2
.vval
.v_float
;
4618 n2
= get_tv_number_chk(&var2
, &error
);
4626 if (rettv
->v_type
== VAR_FLOAT
)
4633 /* If there is a float on either side the result is a float. */
4634 if (rettv
->v_type
== VAR_FLOAT
|| var2
.v_type
== VAR_FLOAT
)
4640 rettv
->v_type
= VAR_FLOAT
;
4641 rettv
->vval
.v_float
= f1
;
4650 rettv
->v_type
= VAR_NUMBER
;
4651 rettv
->vval
.v_number
= n1
;
4661 * Handle fifth level expression:
4662 * * number multiplication
4666 * "arg" must point to the first non-white of the expression.
4667 * "arg" is advanced to the next non-white after the recognized expression.
4669 * Return OK or FAIL.
4672 eval6(arg
, rettv
, evaluate
, want_string
)
4676 int want_string
; /* after "." operator */
4682 int use_float
= FALSE
;
4688 * Get the first variable.
4690 if (eval7(arg
, rettv
, evaluate
, want_string
) == FAIL
)
4694 * Repeat computing, until no '*', '/' or '%' is following.
4699 if (op
!= '*' && op
!= '/' && op
!= '%')
4705 if (rettv
->v_type
== VAR_FLOAT
)
4707 f1
= rettv
->vval
.v_float
;
4713 n1
= get_tv_number_chk(rettv
, &error
);
4722 * Get the second variable.
4724 *arg
= skipwhite(*arg
+ 1);
4725 if (eval7(arg
, &var2
, evaluate
, FALSE
) == FAIL
)
4731 if (var2
.v_type
== VAR_FLOAT
)
4738 f2
= var2
.vval
.v_float
;
4744 n2
= get_tv_number_chk(&var2
, &error
);
4755 * Compute the result.
4756 * When either side is a float the result is a float.
4765 /* We rely on the floating point library to handle divide
4766 * by zero to result in "inf" and not a crash. */
4771 EMSG(_("E804: Cannot use '%' with Float"));
4774 rettv
->v_type
= VAR_FLOAT
;
4775 rettv
->vval
.v_float
= f1
;
4784 if (n2
== 0) /* give an error message? */
4787 n1
= -0x7fffffffL
- 1L; /* similar to NaN */
4798 if (n2
== 0) /* give an error message? */
4803 rettv
->v_type
= VAR_NUMBER
;
4804 rettv
->vval
.v_number
= n1
;
4813 * Handle sixth level expression:
4814 * number number constant
4815 * "string" string constant
4816 * 'string' literal string constant
4817 * &option-name option value
4818 * @r register contents
4819 * identifier variable value
4820 * function() function call
4821 * $VAR environment variable
4822 * (expression) nested expression
4824 * {key: val, key: val} Dictionary
4827 * ! in front logical NOT
4828 * - in front unary minus
4829 * + in front unary plus (ignored)
4830 * trailing [] subscript in String or List
4831 * trailing .name entry in Dictionary
4833 * "arg" must point to the first non-white of the expression.
4834 * "arg" is advanced to the next non-white after the recognized expression.
4836 * Return OK or FAIL.
4839 eval7(arg
, rettv
, evaluate
, want_string
)
4843 int want_string
; /* after "." operator */
4848 char_u
*start_leader
, *end_leader
;
4853 * Initialise variable so that clear_tv() can't mistake this for a
4854 * string and free a string that isn't there.
4856 rettv
->v_type
= VAR_UNKNOWN
;
4859 * Skip '!' and '-' characters. They are handled later.
4861 start_leader
= *arg
;
4862 while (**arg
== '!' || **arg
== '-' || **arg
== '+')
4863 *arg
= skipwhite(*arg
+ 1);
4883 char_u
*p
= skipdigits(*arg
+ 1);
4884 int get_float
= FALSE
;
4886 /* We accept a float when the format matches
4887 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
4888 * strict to avoid backwards compatibility problems.
4889 * Don't look for a float after the "." operator, so that
4890 * ":let vers = 1.2.3" doesn't fail. */
4891 if (!want_string
&& p
[0] == '.' && vim_isdigit(p
[1]))
4894 p
= skipdigits(p
+ 2);
4895 if (*p
== 'e' || *p
== 'E')
4898 if (*p
== '-' || *p
== '+')
4900 if (!vim_isdigit(*p
))
4903 p
= skipdigits(p
+ 1);
4905 if (ASCII_ISALPHA(*p
) || *p
== '.')
4912 *arg
+= string2float(*arg
, &f
);
4915 rettv
->v_type
= VAR_FLOAT
;
4916 rettv
->vval
.v_float
= f
;
4922 vim_str2nr(*arg
, NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
4926 rettv
->v_type
= VAR_NUMBER
;
4927 rettv
->vval
.v_number
= n
;
4934 * String constant: "string".
4936 case '"': ret
= get_string_tv(arg
, rettv
, evaluate
);
4940 * Literal string constant: 'str''ing'.
4942 case '\'': ret
= get_lit_string_tv(arg
, rettv
, evaluate
);
4946 * List: [expr, expr]
4948 case '[': ret
= get_list_tv(arg
, rettv
, evaluate
);
4952 * Dictionary: {key: val, key: val}
4954 case '{': ret
= get_dict_tv(arg
, rettv
, evaluate
);
4958 * Option value: &name
4960 case '&': ret
= get_option_tv(arg
, rettv
, evaluate
);
4964 * Environment variable: $VAR.
4966 case '$': ret
= get_env_tv(arg
, rettv
, evaluate
);
4970 * Register contents: @r.
4975 rettv
->v_type
= VAR_STRING
;
4976 rettv
->vval
.v_string
= get_reg_contents(**arg
, TRUE
, TRUE
);
4983 * nested expression: (expression).
4985 case '(': *arg
= skipwhite(*arg
+ 1);
4986 ret
= eval1(arg
, rettv
, evaluate
); /* recursive! */
4991 EMSG(_("E110: Missing ')'"));
4997 default: ret
= NOTDONE
;
5004 * Must be a variable or function name.
5005 * Can also be a curly-braces kind of name: {expr}.
5008 len
= get_name_len(arg
, &alias
, evaluate
, TRUE
);
5016 if (**arg
== '(') /* recursive! */
5018 /* If "s" is the name of a variable of type VAR_FUNC
5019 * use its contents. */
5020 s
= deref_func_name(s
, &len
);
5022 /* Invoke the function. */
5023 ret
= get_func_tv(s
, len
, rettv
, arg
,
5024 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
5025 &len
, evaluate
, NULL
);
5026 /* Stop the expression evaluation when immediately
5027 * aborting on error, or when an interrupt occurred or
5028 * an exception was thrown but not caught. */
5037 ret
= get_var_tv(s
, len
, rettv
, TRUE
);
5046 *arg
= skipwhite(*arg
);
5048 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5051 ret
= handle_subscript(arg
, rettv
, evaluate
, TRUE
);
5054 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5056 if (ret
== OK
&& evaluate
&& end_leader
> start_leader
)
5063 if (rettv
->v_type
== VAR_FLOAT
)
5064 f
= rettv
->vval
.v_float
;
5067 val
= get_tv_number_chk(rettv
, &error
);
5075 while (end_leader
> start_leader
)
5078 if (*end_leader
== '!')
5081 if (rettv
->v_type
== VAR_FLOAT
)
5087 else if (*end_leader
== '-')
5090 if (rettv
->v_type
== VAR_FLOAT
)
5098 if (rettv
->v_type
== VAR_FLOAT
)
5101 rettv
->vval
.v_float
= f
;
5107 rettv
->v_type
= VAR_NUMBER
;
5108 rettv
->vval
.v_number
= val
;
5117 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5118 * "*arg" points to the '[' or '.'.
5119 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5122 eval_index(arg
, rettv
, evaluate
, verbose
)
5126 int verbose
; /* give error messages */
5128 int empty1
= FALSE
, empty2
= FALSE
;
5129 typval_T var1
, var2
;
5136 if (rettv
->v_type
== VAR_FUNC
5138 || rettv
->v_type
== VAR_FLOAT
5143 EMSG(_("E695: Cannot index a Funcref"));
5153 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
5157 *arg
= skipwhite(key
+ len
);
5164 * Get the (first) variable from inside the [].
5166 *arg
= skipwhite(*arg
+ 1);
5169 else if (eval1(arg
, &var1
, evaluate
) == FAIL
) /* recursive! */
5171 else if (evaluate
&& get_tv_string_chk(&var1
) == NULL
)
5173 /* not a number or string */
5179 * Get the second variable from inside the [:].
5184 *arg
= skipwhite(*arg
+ 1);
5187 else if (eval1(arg
, &var2
, evaluate
) == FAIL
) /* recursive! */
5193 else if (evaluate
&& get_tv_string_chk(&var2
) == NULL
)
5195 /* not a number or string */
5203 /* Check for the ']'. */
5207 EMSG(_(e_missbrac
));
5213 *arg
= skipwhite(*arg
+ 1); /* skip the ']' */
5219 if (!empty1
&& rettv
->v_type
!= VAR_DICT
)
5221 n1
= get_tv_number(&var1
);
5230 n2
= get_tv_number(&var2
);
5235 switch (rettv
->v_type
)
5239 s
= get_tv_string(rettv
);
5240 len
= (long)STRLEN(s
);
5243 /* The resulting variable is a substring. If the indexes
5244 * are out of range the result is empty. */
5255 if (n1
>= len
|| n2
< 0 || n1
> n2
)
5258 s
= vim_strnsave(s
+ n1
, (int)(n2
- n1
+ 1));
5262 /* The resulting variable is a string of a single
5263 * character. If the index is too big or negative the
5264 * result is empty. */
5265 if (n1
>= len
|| n1
< 0)
5268 s
= vim_strnsave(s
+ n1
, 1);
5271 rettv
->v_type
= VAR_STRING
;
5272 rettv
->vval
.v_string
= s
;
5276 len
= list_len(rettv
->vval
.v_list
);
5279 if (!empty1
&& (n1
< 0 || n1
>= len
))
5281 /* For a range we allow invalid values and return an empty
5282 * list. A list index out of range is an error. */
5286 EMSGN(_(e_listidx
), n1
);
5300 if (!empty2
&& (n2
< 0 || n2
+ 1 < n1
))
5305 for (item
= list_find(rettv
->vval
.v_list
, n1
);
5308 if (list_append_tv(l
, &item
->li_tv
) == FAIL
)
5313 item
= item
->li_next
;
5316 rettv
->v_type
= VAR_LIST
;
5317 rettv
->vval
.v_list
= l
;
5322 copy_tv(&list_find(rettv
->vval
.v_list
, n1
)->li_tv
, &var1
);
5332 EMSG(_(e_dictrange
));
5342 key
= get_tv_string(&var1
);
5346 EMSG(_(e_emptykey
));
5352 item
= dict_find(rettv
->vval
.v_dict
, key
, (int)len
);
5354 if (item
== NULL
&& verbose
)
5355 EMSG2(_(e_dictkey
), key
);
5361 copy_tv(&item
->di_tv
, &var1
);
5373 * Get an option value.
5374 * "arg" points to the '&' or '+' before the option name.
5375 * "arg" is advanced to character after the option name.
5376 * Return OK or FAIL.
5379 get_option_tv(arg
, rettv
, evaluate
)
5381 typval_T
*rettv
; /* when NULL, only check if option exists */
5389 int working
= (**arg
== '+'); /* has("+option") */
5394 * Isolate the option name and find its value.
5396 option_end
= find_option_end(arg
, &opt_flags
);
5397 if (option_end
== NULL
)
5400 EMSG2(_("E112: Option name missing: %s"), *arg
);
5412 opt_type
= get_option_value(*arg
, &numval
,
5413 rettv
== NULL
? NULL
: &stringval
, opt_flags
);
5415 if (opt_type
== -3) /* invalid name */
5418 EMSG2(_("E113: Unknown option: %s"), *arg
);
5421 else if (rettv
!= NULL
)
5423 if (opt_type
== -2) /* hidden string option */
5425 rettv
->v_type
= VAR_STRING
;
5426 rettv
->vval
.v_string
= NULL
;
5428 else if (opt_type
== -1) /* hidden number option */
5430 rettv
->v_type
= VAR_NUMBER
;
5431 rettv
->vval
.v_number
= 0;
5433 else if (opt_type
== 1) /* number option */
5435 rettv
->v_type
= VAR_NUMBER
;
5436 rettv
->vval
.v_number
= numval
;
5438 else /* string option */
5440 rettv
->v_type
= VAR_STRING
;
5441 rettv
->vval
.v_string
= stringval
;
5444 else if (working
&& (opt_type
== -2 || opt_type
== -1))
5447 *option_end
= c
; /* put back for error messages */
5454 * Allocate a variable for a string constant.
5455 * Return OK or FAIL.
5458 get_string_tv(arg
, rettv
, evaluate
)
5468 * Find the end of the string, skipping backslashed characters.
5470 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
5472 if (*p
== '\\' && p
[1] != NUL
)
5475 /* A "\<x>" form occupies at least 4 characters, and produces up
5476 * to 6 characters: reserve space for 2 extra */
5484 EMSG2(_("E114: Missing quote: %s"), *arg
);
5488 /* If only parsing, set *arg and return here */
5496 * Copy the string into allocated memory, handling backslashed
5499 name
= alloc((unsigned)(p
- *arg
+ extra
));
5502 rettv
->v_type
= VAR_STRING
;
5503 rettv
->vval
.v_string
= name
;
5505 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; )
5511 case 'b': *name
++ = BS
; ++p
; break;
5512 case 'e': *name
++ = ESC
; ++p
; break;
5513 case 'f': *name
++ = FF
; ++p
; break;
5514 case 'n': *name
++ = NL
; ++p
; break;
5515 case 'r': *name
++ = CAR
; ++p
; break;
5516 case 't': *name
++ = TAB
; ++p
; break;
5518 case 'X': /* hex: "\x1", "\x12" */
5520 case 'u': /* Unicode: "\u0023" */
5522 if (vim_isxdigit(p
[1]))
5525 int c
= toupper(*p
);
5532 while (--n
>= 0 && vim_isxdigit(p
[1]))
5535 nr
= (nr
<< 4) + hex2nr(*p
);
5539 /* For "\u" store the number according to
5542 name
+= (*mb_char2bytes
)(nr
, name
);
5549 /* octal: "\1", "\12", "\123" */
5557 case '7': *name
= *p
++ - '0';
5558 if (*p
>= '0' && *p
<= '7')
5560 *name
= (*name
<< 3) + *p
++ - '0';
5561 if (*p
>= '0' && *p
<= '7')
5562 *name
= (*name
<< 3) + *p
++ - '0';
5567 /* Special key, e.g.: "\<C-W>" */
5568 case '<': extra
= trans_special(&p
, name
, TRUE
);
5576 default: MB_COPY_CHAR(p
, name
);
5581 MB_COPY_CHAR(p
, name
);
5591 * Allocate a variable for a 'str''ing' constant.
5592 * Return OK or FAIL.
5595 get_lit_string_tv(arg
, rettv
, evaluate
)
5605 * Find the end of the string, skipping ''.
5607 for (p
= *arg
+ 1; *p
!= NUL
; mb_ptr_adv(p
))
5620 EMSG2(_("E115: Missing quote: %s"), *arg
);
5624 /* If only parsing return after setting "*arg" */
5632 * Copy the string into allocated memory, handling '' to ' reduction.
5634 str
= alloc((unsigned)((p
- *arg
) - reduce
));
5637 rettv
->v_type
= VAR_STRING
;
5638 rettv
->vval
.v_string
= str
;
5640 for (p
= *arg
+ 1; *p
!= NUL
; )
5648 MB_COPY_CHAR(p
, str
);
5657 * Allocate a variable for a List and fill it from "*arg".
5658 * Return OK or FAIL.
5661 get_list_tv(arg
, rettv
, evaluate
)
5677 *arg
= skipwhite(*arg
+ 1);
5678 while (**arg
!= ']' && **arg
!= NUL
)
5680 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
5684 item
= listitem_alloc();
5688 item
->li_tv
.v_lock
= 0;
5689 list_append(l
, item
);
5699 EMSG2(_("E696: Missing comma in List: %s"), *arg
);
5702 *arg
= skipwhite(*arg
+ 1);
5707 EMSG2(_("E697: Missing end of List ']': %s"), *arg
);
5714 *arg
= skipwhite(*arg
+ 1);
5717 rettv
->v_type
= VAR_LIST
;
5718 rettv
->vval
.v_list
= l
;
5726 * Allocate an empty header for a list.
5727 * Caller should take care of the reference count.
5734 l
= (list_T
*)alloc_clear(sizeof(list_T
));
5737 /* Prepend the list to the list of lists for garbage collection. */
5738 if (first_list
!= NULL
)
5739 first_list
->lv_used_prev
= l
;
5740 l
->lv_used_prev
= NULL
;
5741 l
->lv_used_next
= first_list
;
5748 * Allocate an empty list for a return value.
5749 * Returns OK or FAIL.
5752 rettv_list_alloc(rettv
)
5755 list_T
*l
= list_alloc();
5760 rettv
->vval
.v_list
= l
;
5761 rettv
->v_type
= VAR_LIST
;
5767 * Unreference a list: decrement the reference count and free it when it
5774 if (l
!= NULL
&& --l
->lv_refcount
<= 0)
5779 * Free a list, including all items it points to.
5780 * Ignores the reference count.
5783 list_free(l
, recurse
)
5785 int recurse
; /* Free Lists and Dictionaries recursively. */
5789 /* Remove the list from the list of lists for garbage collection. */
5790 if (l
->lv_used_prev
== NULL
)
5791 first_list
= l
->lv_used_next
;
5793 l
->lv_used_prev
->lv_used_next
= l
->lv_used_next
;
5794 if (l
->lv_used_next
!= NULL
)
5795 l
->lv_used_next
->lv_used_prev
= l
->lv_used_prev
;
5797 for (item
= l
->lv_first
; item
!= NULL
; item
= l
->lv_first
)
5799 /* Remove the item before deleting it. */
5800 l
->lv_first
= item
->li_next
;
5801 if (recurse
|| (item
->li_tv
.v_type
!= VAR_LIST
5802 && item
->li_tv
.v_type
!= VAR_DICT
))
5803 clear_tv(&item
->li_tv
);
5810 * Allocate a list item.
5815 return (listitem_T
*)alloc(sizeof(listitem_T
));
5819 * Free a list item. Also clears the value. Does not notify watchers.
5825 clear_tv(&item
->li_tv
);
5830 * Remove a list item from a List and free it. Also clears the value.
5833 listitem_remove(l
, item
)
5837 list_remove(l
, item
, item
);
5838 listitem_free(item
);
5842 * Get the number of items in a list.
5854 * Return TRUE when two lists have exactly the same values.
5857 list_equal(l1
, l2
, ic
)
5860 int ic
; /* ignore case for strings */
5862 listitem_T
*item1
, *item2
;
5864 if (l1
== NULL
|| l2
== NULL
)
5868 if (list_len(l1
) != list_len(l2
))
5871 for (item1
= l1
->lv_first
, item2
= l2
->lv_first
;
5872 item1
!= NULL
&& item2
!= NULL
;
5873 item1
= item1
->li_next
, item2
= item2
->li_next
)
5874 if (!tv_equal(&item1
->li_tv
, &item2
->li_tv
, ic
))
5876 return item1
== NULL
&& item2
== NULL
;
5879 #if defined(FEAT_PYTHON) || defined(PROTO)
5881 * Return the dictitem that an entry in a hashtable points to.
5892 * Return TRUE when two dictionaries have exactly the same key/values.
5895 dict_equal(d1
, d2
, ic
)
5898 int ic
; /* ignore case for strings */
5904 if (d1
== NULL
|| d2
== NULL
)
5908 if (dict_len(d1
) != dict_len(d2
))
5911 todo
= (int)d1
->dv_hashtab
.ht_used
;
5912 for (hi
= d1
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
5914 if (!HASHITEM_EMPTY(hi
))
5916 item2
= dict_find(d2
, hi
->hi_key
, -1);
5919 if (!tv_equal(&HI2DI(hi
)->di_tv
, &item2
->di_tv
, ic
))
5928 * Return TRUE if "tv1" and "tv2" have the same value.
5929 * Compares the items just like "==" would compare them, but strings and
5930 * numbers are different. Floats and numbers are also different.
5933 tv_equal(tv1
, tv2
, ic
)
5936 int ic
; /* ignore case */
5938 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
5940 static int recursive
= 0; /* cach recursive loops */
5943 if (tv1
->v_type
!= tv2
->v_type
)
5945 /* Catch lists and dicts that have an endless loop by limiting
5946 * recursiveness to 1000. We guess they are equal then. */
5947 if (recursive
>= 1000)
5950 switch (tv1
->v_type
)
5954 r
= list_equal(tv1
->vval
.v_list
, tv2
->vval
.v_list
, ic
);
5960 r
= dict_equal(tv1
->vval
.v_dict
, tv2
->vval
.v_dict
, ic
);
5965 return (tv1
->vval
.v_string
!= NULL
5966 && tv2
->vval
.v_string
!= NULL
5967 && STRCMP(tv1
->vval
.v_string
, tv2
->vval
.v_string
) == 0);
5970 return tv1
->vval
.v_number
== tv2
->vval
.v_number
;
5974 return tv1
->vval
.v_float
== tv2
->vval
.v_float
;
5978 s1
= get_tv_string_buf(tv1
, buf1
);
5979 s2
= get_tv_string_buf(tv2
, buf2
);
5980 return ((ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
)) == 0);
5983 EMSG2(_(e_intern2
), "tv_equal()");
5988 * Locate item with index "n" in list "l" and return it.
5989 * A negative index is counted from the end; -1 is the last item.
5990 * Returns NULL when "n" is out of range.
6003 /* Negative index is relative to the end. */
6007 /* Check for index out of range. */
6008 if (n
< 0 || n
>= l
->lv_len
)
6011 /* When there is a cached index may start search from there. */
6012 if (l
->lv_idx_item
!= NULL
)
6014 if (n
< l
->lv_idx
/ 2)
6016 /* closest to the start of the list */
6020 else if (n
> (l
->lv_idx
+ l
->lv_len
) / 2)
6022 /* closest to the end of the list */
6024 idx
= l
->lv_len
- 1;
6028 /* closest to the cached index */
6029 item
= l
->lv_idx_item
;
6035 if (n
< l
->lv_len
/ 2)
6037 /* closest to the start of the list */
6043 /* closest to the end of the list */
6045 idx
= l
->lv_len
- 1;
6051 /* search forward */
6052 item
= item
->li_next
;
6057 /* search backward */
6058 item
= item
->li_prev
;
6062 /* cache the used index */
6064 l
->lv_idx_item
= item
;
6070 * Get list item "l[idx]" as a number.
6073 list_find_nr(l
, idx
, errorp
)
6076 int *errorp
; /* set to TRUE when something wrong */
6080 li
= list_find(l
, idx
);
6087 return get_tv_number_chk(&li
->li_tv
, errorp
);
6091 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6094 list_find_str(l
, idx
)
6100 li
= list_find(l
, idx
- 1);
6103 EMSGN(_(e_listidx
), idx
);
6106 return get_tv_string(&li
->li_tv
);
6110 * Locate "item" list "l" and return its index.
6111 * Returns -1 when "item" is not in the list.
6114 list_idx_of_item(l
, item
)
6124 for (li
= l
->lv_first
; li
!= NULL
&& li
!= item
; li
= li
->li_next
)
6132 * Append item "item" to the end of list "l".
6135 list_append(l
, item
)
6139 if (l
->lv_last
== NULL
)
6144 item
->li_prev
= NULL
;
6148 l
->lv_last
->li_next
= item
;
6149 item
->li_prev
= l
->lv_last
;
6153 item
->li_next
= NULL
;
6157 * Append typval_T "tv" to the end of list "l".
6158 * Return FAIL when out of memory.
6161 list_append_tv(l
, tv
)
6165 listitem_T
*li
= listitem_alloc();
6169 copy_tv(tv
, &li
->li_tv
);
6175 * Add a dictionary to a list. Used by getqflist().
6176 * Return FAIL when out of memory.
6179 list_append_dict(list
, dict
)
6183 listitem_T
*li
= listitem_alloc();
6187 li
->li_tv
.v_type
= VAR_DICT
;
6188 li
->li_tv
.v_lock
= 0;
6189 li
->li_tv
.vval
.v_dict
= dict
;
6190 list_append(list
, li
);
6191 ++dict
->dv_refcount
;
6196 * Make a copy of "str" and append it as an item to list "l".
6197 * When "len" >= 0 use "str[len]".
6198 * Returns FAIL when out of memory.
6201 list_append_string(l
, str
, len
)
6206 listitem_T
*li
= listitem_alloc();
6211 li
->li_tv
.v_type
= VAR_STRING
;
6212 li
->li_tv
.v_lock
= 0;
6214 li
->li_tv
.vval
.v_string
= NULL
;
6215 else if ((li
->li_tv
.vval
.v_string
= (len
>= 0 ? vim_strnsave(str
, len
)
6216 : vim_strsave(str
))) == NULL
)
6222 * Append "n" to list "l".
6223 * Returns FAIL when out of memory.
6226 list_append_number(l
, n
)
6232 li
= listitem_alloc();
6235 li
->li_tv
.v_type
= VAR_NUMBER
;
6236 li
->li_tv
.v_lock
= 0;
6237 li
->li_tv
.vval
.v_number
= n
;
6243 * Insert typval_T "tv" in list "l" before "item".
6244 * If "item" is NULL append at the end.
6245 * Return FAIL when out of memory.
6248 list_insert_tv(l
, tv
, item
)
6253 listitem_T
*ni
= listitem_alloc();
6257 copy_tv(tv
, &ni
->li_tv
);
6259 /* Append new item at end of list. */
6263 /* Insert new item before existing item. */
6264 ni
->li_prev
= item
->li_prev
;
6266 if (item
->li_prev
== NULL
)
6273 item
->li_prev
->li_next
= ni
;
6274 l
->lv_idx_item
= NULL
;
6283 * Extend "l1" with "l2".
6284 * If "bef" is NULL append at the end, otherwise insert before this item.
6285 * Returns FAIL when out of memory.
6288 list_extend(l1
, l2
, bef
)
6294 int todo
= l2
->lv_len
;
6296 /* We also quit the loop when we have inserted the original item count of
6297 * the list, avoid a hang when we extend a list with itself. */
6298 for (item
= l2
->lv_first
; item
!= NULL
&& --todo
>= 0; item
= item
->li_next
)
6299 if (list_insert_tv(l1
, &item
->li_tv
, bef
) == FAIL
)
6305 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6306 * Return FAIL when out of memory.
6309 list_concat(l1
, l2
, tv
)
6316 if (l1
== NULL
|| l2
== NULL
)
6319 /* make a copy of the first list. */
6320 l
= list_copy(l1
, FALSE
, 0);
6323 tv
->v_type
= VAR_LIST
;
6324 tv
->vval
.v_list
= l
;
6326 /* append all items from the second list */
6327 return list_extend(l
, l2
, NULL
);
6331 * Make a copy of list "orig". Shallow if "deep" is FALSE.
6332 * The refcount of the new list is set to 1.
6333 * See item_copy() for "copyID".
6334 * Returns NULL when out of memory.
6337 list_copy(orig
, deep
, copyID
)
6349 copy
= list_alloc();
6354 /* Do this before adding the items, because one of the items may
6355 * refer back to this list. */
6356 orig
->lv_copyID
= copyID
;
6357 orig
->lv_copylist
= copy
;
6359 for (item
= orig
->lv_first
; item
!= NULL
&& !got_int
;
6360 item
= item
->li_next
)
6362 ni
= listitem_alloc();
6367 if (item_copy(&item
->li_tv
, &ni
->li_tv
, deep
, copyID
) == FAIL
)
6374 copy_tv(&item
->li_tv
, &ni
->li_tv
);
6375 list_append(copy
, ni
);
6377 ++copy
->lv_refcount
;
6389 * Remove items "item" to "item2" from list "l".
6390 * Does not free the listitem or the value!
6393 list_remove(l
, item
, item2
)
6400 /* notify watchers */
6401 for (ip
= item
; ip
!= NULL
; ip
= ip
->li_next
)
6404 list_fix_watch(l
, ip
);
6409 if (item2
->li_next
== NULL
)
6410 l
->lv_last
= item
->li_prev
;
6412 item2
->li_next
->li_prev
= item
->li_prev
;
6413 if (item
->li_prev
== NULL
)
6414 l
->lv_first
= item2
->li_next
;
6416 item
->li_prev
->li_next
= item2
->li_next
;
6417 l
->lv_idx_item
= NULL
;
6421 * Return an allocated string with the string representation of a list.
6425 list2string(tv
, copyID
)
6431 if (tv
->vval
.v_list
== NULL
)
6433 ga_init2(&ga
, (int)sizeof(char), 80);
6434 ga_append(&ga
, '[');
6435 if (list_join(&ga
, tv
->vval
.v_list
, (char_u
*)", ", FALSE
, copyID
) == FAIL
)
6437 vim_free(ga
.ga_data
);
6440 ga_append(&ga
, ']');
6441 ga_append(&ga
, NUL
);
6442 return (char_u
*)ga
.ga_data
;
6446 * Join list "l" into a string in "*gap", using separator "sep".
6447 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
6448 * Return FAIL or OK.
6451 list_join(gap
, l
, sep
, echo
, copyID
)
6460 char_u numbuf
[NUMBUFLEN
];
6464 for (item
= l
->lv_first
; item
!= NULL
&& !got_int
; item
= item
->li_next
)
6469 ga_concat(gap
, sep
);
6472 s
= echo_string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6474 s
= tv2string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6485 * Garbage collection for lists and dictionaries.
6487 * We use reference counts to be able to free most items right away when they
6488 * are no longer used. But for composite items it's possible that it becomes
6489 * unused while the reference count is > 0: When there is a recursive
6490 * reference. Example:
6491 * :let l = [1, 2, 3]
6495 * Since this is quite unusual we handle this with garbage collection: every
6496 * once in a while find out which lists and dicts are not referenced from any
6499 * Here is a good reference text about garbage collection (refers to Python
6500 * but it applies to all reference-counting mechanisms):
6501 * http://python.ca/nas/python/gc/
6505 * Do garbage collection for lists and dicts.
6506 * Return TRUE if some memory was freed.
6513 int copyID
= ++current_copyID
;
6517 funccall_T
*fc
, **pfc
;
6518 int did_free
= FALSE
;
6523 /* Only do this once. */
6524 want_garbage_collect
= FALSE
;
6525 may_garbage_collect
= FALSE
;
6526 garbage_collect_at_exit
= FALSE
;
6529 * 1. Go through all accessible variables and mark all lists and dicts
6532 /* script-local variables */
6533 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
6534 set_ref_in_ht(&SCRIPT_VARS(i
), copyID
);
6536 /* buffer-local variables */
6537 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
6538 set_ref_in_ht(&buf
->b_vars
.dv_hashtab
, copyID
);
6540 /* window-local variables */
6541 FOR_ALL_TAB_WINDOWS(tp
, wp
)
6542 set_ref_in_ht(&wp
->w_vars
.dv_hashtab
, copyID
);
6545 /* tabpage-local variables */
6546 for (tp
= first_tabpage
; tp
!= NULL
; tp
= tp
->tp_next
)
6547 set_ref_in_ht(&tp
->tp_vars
.dv_hashtab
, copyID
);
6550 /* global variables */
6551 set_ref_in_ht(&globvarht
, copyID
);
6553 /* function-local variables */
6554 for (fc
= current_funccal
; fc
!= NULL
; fc
= fc
->caller
)
6556 set_ref_in_ht(&fc
->l_vars
.dv_hashtab
, copyID
);
6557 set_ref_in_ht(&fc
->l_avars
.dv_hashtab
, copyID
);
6561 set_ref_in_ht(&vimvarht
, copyID
);
6564 * 2. Go through the list of dicts and free items without the copyID.
6566 for (dd
= first_dict
; dd
!= NULL
; )
6567 if (dd
->dv_copyID
!= copyID
)
6569 /* Free the Dictionary and ordinary items it contains, but don't
6570 * recurse into Lists and Dictionaries, they will be in the list
6571 * of dicts or list of lists. */
6572 dict_free(dd
, FALSE
);
6575 /* restart, next dict may also have been freed */
6579 dd
= dd
->dv_used_next
;
6582 * 3. Go through the list of lists and free items without the copyID.
6583 * But don't free a list that has a watcher (used in a for loop), these
6584 * are not referenced anywhere.
6586 for (ll
= first_list
; ll
!= NULL
; )
6587 if (ll
->lv_copyID
!= copyID
&& ll
->lv_watch
== NULL
)
6589 /* Free the List and ordinary items it contains, but don't recurse
6590 * into Lists and Dictionaries, they will be in the list of dicts
6591 * or list of lists. */
6592 list_free(ll
, FALSE
);
6595 /* restart, next list may also have been freed */
6599 ll
= ll
->lv_used_next
;
6601 /* check if any funccal can be freed now */
6602 for (pfc
= &previous_funccal
; *pfc
!= NULL
; )
6604 if (can_free_funccal(*pfc
, copyID
))
6608 free_funccal(fc
, TRUE
);
6612 pfc
= &(*pfc
)->caller
;
6619 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6622 set_ref_in_ht(ht
, copyID
)
6629 todo
= (int)ht
->ht_used
;
6630 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
6631 if (!HASHITEM_EMPTY(hi
))
6634 set_ref_in_item(&HI2DI(hi
)->di_tv
, copyID
);
6639 * Mark all lists and dicts referenced through list "l" with "copyID".
6642 set_ref_in_list(l
, copyID
)
6648 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
6649 set_ref_in_item(&li
->li_tv
, copyID
);
6653 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6656 set_ref_in_item(tv
, copyID
)
6666 dd
= tv
->vval
.v_dict
;
6667 if (dd
!= NULL
&& dd
->dv_copyID
!= copyID
)
6669 /* Didn't see this dict yet. */
6670 dd
->dv_copyID
= copyID
;
6671 set_ref_in_ht(&dd
->dv_hashtab
, copyID
);
6676 ll
= tv
->vval
.v_list
;
6677 if (ll
!= NULL
&& ll
->lv_copyID
!= copyID
)
6679 /* Didn't see this list yet. */
6680 ll
->lv_copyID
= copyID
;
6681 set_ref_in_list(ll
, copyID
);
6689 * Allocate an empty header for a dictionary.
6696 d
= (dict_T
*)alloc(sizeof(dict_T
));
6699 /* Add the list to the list of dicts for garbage collection. */
6700 if (first_dict
!= NULL
)
6701 first_dict
->dv_used_prev
= d
;
6702 d
->dv_used_next
= first_dict
;
6703 d
->dv_used_prev
= NULL
;
6706 hash_init(&d
->dv_hashtab
);
6715 * Unreference a Dictionary: decrement the reference count and free it when it
6722 if (d
!= NULL
&& --d
->dv_refcount
<= 0)
6727 * Free a Dictionary, including all items it contains.
6728 * Ignores the reference count.
6731 dict_free(d
, recurse
)
6733 int recurse
; /* Free Lists and Dictionaries recursively. */
6739 /* Remove the dict from the list of dicts for garbage collection. */
6740 if (d
->dv_used_prev
== NULL
)
6741 first_dict
= d
->dv_used_next
;
6743 d
->dv_used_prev
->dv_used_next
= d
->dv_used_next
;
6744 if (d
->dv_used_next
!= NULL
)
6745 d
->dv_used_next
->dv_used_prev
= d
->dv_used_prev
;
6747 /* Lock the hashtab, we don't want it to resize while freeing items. */
6748 hash_lock(&d
->dv_hashtab
);
6749 todo
= (int)d
->dv_hashtab
.ht_used
;
6750 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
6752 if (!HASHITEM_EMPTY(hi
))
6754 /* Remove the item before deleting it, just in case there is
6755 * something recursive causing trouble. */
6757 hash_remove(&d
->dv_hashtab
, hi
);
6758 if (recurse
|| (di
->di_tv
.v_type
!= VAR_LIST
6759 && di
->di_tv
.v_type
!= VAR_DICT
))
6760 clear_tv(&di
->di_tv
);
6765 hash_clear(&d
->dv_hashtab
);
6770 * Allocate a Dictionary item.
6771 * The "key" is copied to the new item.
6772 * Note that the value of the item "di_tv" still needs to be initialized!
6773 * Returns NULL when out of memory.
6781 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
) + STRLEN(key
)));
6784 STRCPY(di
->di_key
, key
);
6791 * Make a copy of a Dictionary item.
6799 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
6800 + STRLEN(org
->di_key
)));
6803 STRCPY(di
->di_key
, org
->di_key
);
6805 copy_tv(&org
->di_tv
, &di
->di_tv
);
6811 * Remove item "item" from Dictionary "dict" and free it.
6814 dictitem_remove(dict
, item
)
6820 hi
= hash_find(&dict
->dv_hashtab
, item
->di_key
);
6821 if (HASHITEM_EMPTY(hi
))
6822 EMSG2(_(e_intern2
), "dictitem_remove()");
6824 hash_remove(&dict
->dv_hashtab
, hi
);
6825 dictitem_free(item
);
6829 * Free a dict item. Also clears the value.
6835 clear_tv(&item
->di_tv
);
6840 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6841 * The refcount of the new dict is set to 1.
6842 * See item_copy() for "copyID".
6843 * Returns NULL when out of memory.
6846 dict_copy(orig
, deep
, copyID
)
6859 copy
= dict_alloc();
6864 orig
->dv_copyID
= copyID
;
6865 orig
->dv_copydict
= copy
;
6867 todo
= (int)orig
->dv_hashtab
.ht_used
;
6868 for (hi
= orig
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
6870 if (!HASHITEM_EMPTY(hi
))
6874 di
= dictitem_alloc(hi
->hi_key
);
6879 if (item_copy(&HI2DI(hi
)->di_tv
, &di
->di_tv
, deep
,
6887 copy_tv(&HI2DI(hi
)->di_tv
, &di
->di_tv
);
6888 if (dict_add(copy
, di
) == FAIL
)
6896 ++copy
->dv_refcount
;
6908 * Add item "item" to Dictionary "d".
6909 * Returns FAIL when out of memory and when key already existed.
6916 return hash_add(&d
->dv_hashtab
, item
->di_key
);
6920 * Add a number or string entry to dictionary "d".
6921 * When "str" is NULL use number "nr", otherwise use "str".
6922 * Returns FAIL when out of memory and when key already exists.
6925 dict_add_nr_str(d
, key
, nr
, str
)
6933 item
= dictitem_alloc((char_u
*)key
);
6936 item
->di_tv
.v_lock
= 0;
6939 item
->di_tv
.v_type
= VAR_NUMBER
;
6940 item
->di_tv
.vval
.v_number
= nr
;
6944 item
->di_tv
.v_type
= VAR_STRING
;
6945 item
->di_tv
.vval
.v_string
= vim_strsave(str
);
6947 if (dict_add(d
, item
) == FAIL
)
6949 dictitem_free(item
);
6956 * Get the number of items in a Dictionary.
6964 return (long)d
->dv_hashtab
.ht_used
;
6968 * Find item "key[len]" in Dictionary "d".
6969 * If "len" is negative use strlen(key).
6970 * Returns NULL when not found.
6973 dict_find(d
, key
, len
)
6979 char_u buf
[AKEYLEN
];
6981 char_u
*tofree
= NULL
;
6986 else if (len
>= AKEYLEN
)
6988 tofree
= akey
= vim_strnsave(key
, len
);
6994 /* Avoid a malloc/free by using buf[]. */
6995 vim_strncpy(buf
, key
, len
);
6999 hi
= hash_find(&d
->dv_hashtab
, akey
);
7001 if (HASHITEM_EMPTY(hi
))
7007 * Get a string item from a dictionary.
7008 * When "save" is TRUE allocate memory for it.
7009 * Returns NULL if the entry doesn't exist or out of memory.
7012 get_dict_string(d
, key
, save
)
7020 di
= dict_find(d
, key
, -1);
7023 s
= get_tv_string(&di
->di_tv
);
7024 if (save
&& s
!= NULL
)
7030 * Get a number item from a dictionary.
7031 * Returns 0 if the entry doesn't exist or out of memory.
7034 get_dict_number(d
, key
)
7040 di
= dict_find(d
, key
, -1);
7043 return get_tv_number(&di
->di_tv
);
7047 * Return an allocated string with the string representation of a Dictionary.
7051 dict2string(tv
, copyID
)
7058 char_u numbuf
[NUMBUFLEN
];
7064 if ((d
= tv
->vval
.v_dict
) == NULL
)
7066 ga_init2(&ga
, (int)sizeof(char), 80);
7067 ga_append(&ga
, '{');
7069 todo
= (int)d
->dv_hashtab
.ht_used
;
7070 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
7072 if (!HASHITEM_EMPTY(hi
))
7079 ga_concat(&ga
, (char_u
*)", ");
7081 tofree
= string_quote(hi
->hi_key
, FALSE
);
7084 ga_concat(&ga
, tofree
);
7087 ga_concat(&ga
, (char_u
*)": ");
7088 s
= tv2string(&HI2DI(hi
)->di_tv
, &tofree
, numbuf
, copyID
);
7098 vim_free(ga
.ga_data
);
7102 ga_append(&ga
, '}');
7103 ga_append(&ga
, NUL
);
7104 return (char_u
*)ga
.ga_data
;
7108 * Allocate a variable for a Dictionary and fill it from "*arg".
7109 * Return OK or FAIL. Returns NOTDONE for {expr}.
7112 get_dict_tv(arg
, rettv
, evaluate
)
7122 char_u
*start
= skipwhite(*arg
+ 1);
7123 char_u buf
[NUMBUFLEN
];
7126 * First check if it's not a curly-braces thing: {expr}.
7127 * Must do this without evaluating, otherwise a function may be called
7128 * twice. Unfortunately this means we need to call eval1() twice for the
7130 * But {} is an empty Dictionary.
7134 if (eval1(&start
, &tv
, FALSE
) == FAIL
) /* recursive! */
7146 tvkey
.v_type
= VAR_UNKNOWN
;
7147 tv
.v_type
= VAR_UNKNOWN
;
7149 *arg
= skipwhite(*arg
+ 1);
7150 while (**arg
!= '}' && **arg
!= NUL
)
7152 if (eval1(arg
, &tvkey
, evaluate
) == FAIL
) /* recursive! */
7156 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg
);
7162 key
= get_tv_string_buf_chk(&tvkey
, buf
);
7163 if (key
== NULL
|| *key
== NUL
)
7165 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7167 EMSG(_(e_emptykey
));
7173 *arg
= skipwhite(*arg
+ 1);
7174 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
7182 item
= dict_find(d
, key
, -1);
7185 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key
);
7190 item
= dictitem_alloc(key
);
7195 item
->di_tv
.v_lock
= 0;
7196 if (dict_add(d
, item
) == FAIL
)
7197 dictitem_free(item
);
7205 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg
);
7208 *arg
= skipwhite(*arg
+ 1);
7213 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg
);
7220 *arg
= skipwhite(*arg
+ 1);
7223 rettv
->v_type
= VAR_DICT
;
7224 rettv
->vval
.v_dict
= d
;
7232 * Return a string with the string representation of a variable.
7233 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7234 * "numbuf" is used for a number.
7235 * Does not put quotes around strings, as ":echo" displays values.
7236 * When "copyID" is not NULL replace recursive lists and dicts with "...".
7240 echo_string(tv
, tofree
, numbuf
, copyID
)
7246 static int recurse
= 0;
7249 if (recurse
>= DICT_MAXNEST
)
7251 EMSG(_("E724: variable nested too deep for displaying"));
7261 r
= tv
->vval
.v_string
;
7265 if (tv
->vval
.v_list
== NULL
)
7270 else if (copyID
!= 0 && tv
->vval
.v_list
->lv_copyID
== copyID
)
7273 r
= (char_u
*)"[...]";
7277 tv
->vval
.v_list
->lv_copyID
= copyID
;
7278 *tofree
= list2string(tv
, copyID
);
7284 if (tv
->vval
.v_dict
== NULL
)
7289 else if (copyID
!= 0 && tv
->vval
.v_dict
->dv_copyID
== copyID
)
7292 r
= (char_u
*)"{...}";
7296 tv
->vval
.v_dict
->dv_copyID
= copyID
;
7297 *tofree
= dict2string(tv
, copyID
);
7305 r
= get_tv_string_buf(tv
, numbuf
);
7311 vim_snprintf((char *)numbuf
, NUMBUFLEN
, "%g", tv
->vval
.v_float
);
7317 EMSG2(_(e_intern2
), "echo_string()");
7326 * Return a string with the string representation of a variable.
7327 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7328 * "numbuf" is used for a number.
7329 * Puts quotes around strings, so that they can be parsed back by eval().
7333 tv2string(tv
, tofree
, numbuf
, copyID
)
7342 *tofree
= string_quote(tv
->vval
.v_string
, TRUE
);
7345 *tofree
= string_quote(tv
->vval
.v_string
, FALSE
);
7350 vim_snprintf((char *)numbuf
, NUMBUFLEN
- 1, "%g", tv
->vval
.v_float
);
7358 EMSG2(_(e_intern2
), "tv2string()");
7360 return echo_string(tv
, tofree
, numbuf
, copyID
);
7364 * Return string "str" in ' quotes, doubling ' characters.
7365 * If "str" is NULL an empty string is assumed.
7366 * If "function" is TRUE make it function('string').
7369 string_quote(str
, function
)
7376 len
= (function
? 13 : 3);
7379 len
+= (unsigned)STRLEN(str
);
7380 for (p
= str
; *p
!= NUL
; mb_ptr_adv(p
))
7389 STRCPY(r
, "function('");
7395 for (p
= str
; *p
!= NUL
; )
7411 * Convert the string "text" to a floating point number.
7412 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7413 * this always uses a decimal point.
7414 * Returns the length of the text that was consumed.
7417 string2float(text
, value
)
7419 float_T
*value
; /* result stored here */
7421 char *s
= (char *)text
;
7426 return (int)((char_u
*)s
- text
);
7431 * Get the value of an environment variable.
7432 * "arg" is pointing to the '$'. It is advanced to after the name.
7433 * If the environment variable was not set, silently assume it is empty.
7437 get_env_tv(arg
, rettv
, evaluate
)
7442 char_u
*string
= NULL
;
7446 int mustfree
= FALSE
;
7450 len
= get_env_len(arg
);
7457 /* first try vim_getenv(), fast for normal environment vars */
7458 string
= vim_getenv(name
, &mustfree
);
7459 if (string
!= NULL
&& *string
!= NUL
)
7462 string
= vim_strsave(string
);
7469 /* next try expanding things like $VIM and ${HOME} */
7470 string
= expand_env_save(name
- 1);
7471 if (string
!= NULL
&& *string
== '$')
7479 rettv
->v_type
= VAR_STRING
;
7480 rettv
->vval
.v_string
= string
;
7487 * Array with names and number of arguments of all internal functions
7488 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7492 char *f_name
; /* function name */
7493 char f_min_argc
; /* minimal number of arguments */
7494 char f_max_argc
; /* maximal number of arguments */
7495 void (*f_func
) __ARGS((typval_T
*args
, typval_T
*rvar
));
7496 /* implementation of function */
7500 {"abs", 1, 1, f_abs
},
7501 {"acos", 1, 1, f_acos
}, /* WJMc */
7503 {"add", 2, 2, f_add
},
7504 {"append", 2, 2, f_append
},
7505 {"argc", 0, 0, f_argc
},
7506 {"argidx", 0, 0, f_argidx
},
7507 {"argv", 0, 1, f_argv
},
7509 {"asin", 1, 1, f_asin
}, /* WJMc */
7510 {"atan", 1, 1, f_atan
},
7511 {"atan2", 2, 2, f_atan2
}, /* WJMc */
7513 {"browse", 4, 4, f_browse
},
7514 {"browsedir", 2, 2, f_browsedir
},
7515 {"bufexists", 1, 1, f_bufexists
},
7516 {"buffer_exists", 1, 1, f_bufexists
}, /* obsolete */
7517 {"buffer_name", 1, 1, f_bufname
}, /* obsolete */
7518 {"buffer_number", 1, 1, f_bufnr
}, /* obsolete */
7519 {"buflisted", 1, 1, f_buflisted
},
7520 {"bufloaded", 1, 1, f_bufloaded
},
7521 {"bufname", 1, 1, f_bufname
},
7522 {"bufnr", 1, 2, f_bufnr
},
7523 {"bufwinnr", 1, 1, f_bufwinnr
},
7524 {"byte2line", 1, 1, f_byte2line
},
7525 {"byteidx", 2, 2, f_byteidx
},
7526 {"call", 2, 3, f_call
},
7528 {"ceil", 1, 1, f_ceil
},
7530 {"changenr", 0, 0, f_changenr
},
7531 {"char2nr", 1, 1, f_char2nr
},
7532 {"cindent", 1, 1, f_cindent
},
7533 {"clearmatches", 0, 0, f_clearmatches
},
7534 {"col", 1, 1, f_col
},
7535 #if defined(FEAT_INS_EXPAND)
7536 {"complete", 2, 2, f_complete
},
7537 {"complete_add", 1, 1, f_complete_add
},
7538 {"complete_check", 0, 0, f_complete_check
},
7540 {"confirm", 1, 4, f_confirm
},
7541 {"copy", 1, 1, f_copy
},
7543 {"cos", 1, 1, f_cos
},
7544 {"cosh", 1, 1, f_cosh
}, /* WJMc */
7546 {"count", 2, 4, f_count
},
7547 {"cscope_connection",0,3, f_cscope_connection
},
7548 {"cursor", 1, 3, f_cursor
},
7549 {"deepcopy", 1, 2, f_deepcopy
},
7550 {"delete", 1, 1, f_delete
},
7551 {"did_filetype", 0, 0, f_did_filetype
},
7552 {"diff_filler", 1, 1, f_diff_filler
},
7553 {"diff_hlID", 2, 2, f_diff_hlID
},
7554 {"empty", 1, 1, f_empty
},
7555 {"escape", 2, 2, f_escape
},
7556 {"eval", 1, 1, f_eval
},
7557 {"eventhandler", 0, 0, f_eventhandler
},
7558 {"executable", 1, 1, f_executable
},
7559 {"exists", 1, 1, f_exists
},
7561 {"exp", 1, 1, f_exp
}, /* WJMc */
7563 {"expand", 1, 2, f_expand
},
7564 {"extend", 2, 3, f_extend
},
7565 {"feedkeys", 1, 2, f_feedkeys
},
7566 {"file_readable", 1, 1, f_filereadable
}, /* obsolete */
7567 {"filereadable", 1, 1, f_filereadable
},
7568 {"filewritable", 1, 1, f_filewritable
},
7569 {"filter", 2, 2, f_filter
},
7570 {"finddir", 1, 3, f_finddir
},
7571 {"findfile", 1, 3, f_findfile
},
7573 {"float2nr", 1, 1, f_float2nr
},
7574 {"floor", 1, 1, f_floor
},
7575 {"fmod", 2, 2, f_fmod
}, /* WJMc */
7577 {"fnameescape", 1, 1, f_fnameescape
},
7578 {"fnamemodify", 2, 2, f_fnamemodify
},
7579 {"foldclosed", 1, 1, f_foldclosed
},
7580 {"foldclosedend", 1, 1, f_foldclosedend
},
7581 {"foldlevel", 1, 1, f_foldlevel
},
7582 {"foldtext", 0, 0, f_foldtext
},
7583 {"foldtextresult", 1, 1, f_foldtextresult
},
7584 {"foreground", 0, 0, f_foreground
},
7585 {"function", 1, 1, f_function
},
7586 {"garbagecollect", 0, 1, f_garbagecollect
},
7587 {"get", 2, 3, f_get
},
7588 {"getbufline", 2, 3, f_getbufline
},
7589 {"getbufvar", 2, 2, f_getbufvar
},
7590 {"getchar", 0, 1, f_getchar
},
7591 {"getcharmod", 0, 0, f_getcharmod
},
7592 {"getcmdline", 0, 0, f_getcmdline
},
7593 {"getcmdpos", 0, 0, f_getcmdpos
},
7594 {"getcmdtype", 0, 0, f_getcmdtype
},
7595 {"getcwd", 0, 0, f_getcwd
},
7596 {"getfontname", 0, 1, f_getfontname
},
7597 {"getfperm", 1, 1, f_getfperm
},
7598 {"getfsize", 1, 1, f_getfsize
},
7599 {"getftime", 1, 1, f_getftime
},
7600 {"getftype", 1, 1, f_getftype
},
7601 {"getline", 1, 2, f_getline
},
7602 {"getloclist", 1, 1, f_getqflist
},
7603 {"getmatches", 0, 0, f_getmatches
},
7604 {"getpid", 0, 0, f_getpid
},
7605 {"getpos", 1, 1, f_getpos
},
7606 {"getqflist", 0, 0, f_getqflist
},
7607 {"getreg", 0, 2, f_getreg
},
7608 {"getregtype", 0, 1, f_getregtype
},
7609 {"gettabwinvar", 3, 3, f_gettabwinvar
},
7610 {"getwinposx", 0, 0, f_getwinposx
},
7611 {"getwinposy", 0, 0, f_getwinposy
},
7612 {"getwinvar", 2, 2, f_getwinvar
},
7613 {"glob", 1, 2, f_glob
},
7614 {"globpath", 2, 3, f_globpath
},
7615 {"has", 1, 1, f_has
},
7616 {"has_key", 2, 2, f_has_key
},
7617 {"haslocaldir", 0, 0, f_haslocaldir
},
7618 {"hasmapto", 1, 3, f_hasmapto
},
7619 {"highlightID", 1, 1, f_hlID
}, /* obsolete */
7620 {"highlight_exists",1, 1, f_hlexists
}, /* obsolete */
7621 {"histadd", 2, 2, f_histadd
},
7622 {"histdel", 1, 2, f_histdel
},
7623 {"histget", 1, 2, f_histget
},
7624 {"histnr", 1, 1, f_histnr
},
7625 {"hlID", 1, 1, f_hlID
},
7626 {"hlexists", 1, 1, f_hlexists
},
7627 {"hostname", 0, 0, f_hostname
},
7628 {"iconv", 3, 3, f_iconv
},
7629 {"indent", 1, 1, f_indent
},
7630 {"index", 2, 4, f_index
},
7631 {"input", 1, 3, f_input
},
7632 {"inputdialog", 1, 3, f_inputdialog
},
7633 {"inputlist", 1, 1, f_inputlist
},
7634 {"inputrestore", 0, 0, f_inputrestore
},
7635 {"inputsave", 0, 0, f_inputsave
},
7636 {"inputsecret", 1, 2, f_inputsecret
},
7637 {"insert", 2, 3, f_insert
},
7638 {"isdirectory", 1, 1, f_isdirectory
},
7639 {"islocked", 1, 1, f_islocked
},
7640 {"items", 1, 1, f_items
},
7641 {"join", 1, 2, f_join
},
7642 {"keys", 1, 1, f_keys
},
7643 {"last_buffer_nr", 0, 0, f_last_buffer_nr
},/* obsolete */
7644 {"len", 1, 1, f_len
},
7645 {"libcall", 3, 3, f_libcall
},
7646 {"libcallnr", 3, 3, f_libcallnr
},
7647 {"line", 1, 1, f_line
},
7648 {"line2byte", 1, 1, f_line2byte
},
7649 {"lispindent", 1, 1, f_lispindent
},
7650 {"localtime", 0, 0, f_localtime
},
7652 {"log", 1, 1, f_log
}, /* WJMc */
7653 {"log10", 1, 1, f_log10
},
7655 {"map", 2, 2, f_map
},
7656 {"maparg", 1, 3, f_maparg
},
7657 {"mapcheck", 1, 3, f_mapcheck
},
7658 {"match", 2, 4, f_match
},
7659 {"matchadd", 2, 4, f_matchadd
},
7660 {"matcharg", 1, 1, f_matcharg
},
7661 {"matchdelete", 1, 1, f_matchdelete
},
7662 {"matchend", 2, 4, f_matchend
},
7663 {"matchlist", 2, 4, f_matchlist
},
7664 {"matchstr", 2, 4, f_matchstr
},
7665 {"max", 1, 1, f_max
},
7666 {"min", 1, 1, f_min
},
7668 {"mkdir", 1, 3, f_mkdir
},
7670 {"mode", 0, 1, f_mode
},
7671 {"nextnonblank", 1, 1, f_nextnonblank
},
7672 {"nr2char", 1, 1, f_nr2char
},
7673 {"pathshorten", 1, 1, f_pathshorten
},
7675 {"pow", 2, 2, f_pow
},
7677 {"prevnonblank", 1, 1, f_prevnonblank
},
7678 {"printf", 2, 19, f_printf
},
7679 {"pumvisible", 0, 0, f_pumvisible
},
7680 {"range", 1, 3, f_range
},
7681 {"readfile", 1, 3, f_readfile
},
7682 {"reltime", 0, 2, f_reltime
},
7683 {"reltimestr", 1, 1, f_reltimestr
},
7684 {"remote_expr", 2, 3, f_remote_expr
},
7685 {"remote_foreground", 1, 1, f_remote_foreground
},
7686 {"remote_peek", 1, 2, f_remote_peek
},
7687 {"remote_read", 1, 1, f_remote_read
},
7688 {"remote_send", 2, 3, f_remote_send
},
7689 {"remove", 2, 3, f_remove
},
7690 {"rename", 2, 2, f_rename
},
7691 {"repeat", 2, 2, f_repeat
},
7692 {"resolve", 1, 1, f_resolve
},
7693 {"reverse", 1, 1, f_reverse
},
7695 {"round", 1, 1, f_round
},
7697 {"search", 1, 4, f_search
},
7698 {"searchdecl", 1, 3, f_searchdecl
},
7699 {"searchpair", 3, 7, f_searchpair
},
7700 {"searchpairpos", 3, 7, f_searchpairpos
},
7701 {"searchpos", 1, 4, f_searchpos
},
7702 {"server2client", 2, 2, f_server2client
},
7703 {"serverlist", 0, 0, f_serverlist
},
7704 {"setbufvar", 3, 3, f_setbufvar
},
7705 {"setcmdpos", 1, 1, f_setcmdpos
},
7706 {"setline", 2, 2, f_setline
},
7707 {"setloclist", 2, 3, f_setloclist
},
7708 {"setmatches", 1, 1, f_setmatches
},
7709 {"setpos", 2, 2, f_setpos
},
7710 {"setqflist", 1, 2, f_setqflist
},
7711 {"setreg", 2, 3, f_setreg
},
7712 {"settabwinvar", 4, 4, f_settabwinvar
},
7713 {"setwinvar", 3, 3, f_setwinvar
},
7714 {"shellescape", 1, 2, f_shellescape
},
7715 {"simplify", 1, 1, f_simplify
},
7717 {"sin", 1, 1, f_sin
},
7718 {"sinh", 1, 1, f_sinh
}, /* WJMc */
7720 {"sort", 1, 2, f_sort
},
7721 {"soundfold", 1, 1, f_soundfold
},
7722 {"spellbadword", 0, 1, f_spellbadword
},
7723 {"spellsuggest", 1, 3, f_spellsuggest
},
7724 {"split", 1, 3, f_split
},
7726 {"sqrt", 1, 1, f_sqrt
},
7727 {"str2float", 1, 1, f_str2float
},
7729 {"str2nr", 1, 2, f_str2nr
},
7730 #ifdef HAVE_STRFTIME
7731 {"strftime", 1, 2, f_strftime
},
7733 {"stridx", 2, 3, f_stridx
},
7734 {"string", 1, 1, f_string
},
7735 {"strlen", 1, 1, f_strlen
},
7736 {"strpart", 2, 3, f_strpart
},
7737 {"strridx", 2, 3, f_strridx
},
7738 {"strtrans", 1, 1, f_strtrans
},
7739 {"submatch", 1, 1, f_submatch
},
7740 {"substitute", 4, 4, f_substitute
},
7741 {"synID", 3, 3, f_synID
},
7742 {"synIDattr", 2, 3, f_synIDattr
},
7743 {"synIDtrans", 1, 1, f_synIDtrans
},
7744 {"synstack", 2, 2, f_synstack
},
7745 {"system", 1, 2, f_system
},
7746 {"tabpagebuflist", 0, 1, f_tabpagebuflist
},
7747 {"tabpagenr", 0, 1, f_tabpagenr
},
7748 {"tabpagewinnr", 1, 2, f_tabpagewinnr
},
7749 {"tagfiles", 0, 0, f_tagfiles
},
7750 {"taglist", 1, 1, f_taglist
},
7751 {"tan", 1, 1, f_tan
}, /* WJMc */
7752 {"tanh", 1, 1, f_tanh
}, /* WJMc */
7753 {"tempname", 0, 0, f_tempname
},
7754 {"test", 1, 1, f_test
},
7755 {"tolower", 1, 1, f_tolower
},
7756 {"toupper", 1, 1, f_toupper
},
7759 {"trunc", 1, 1, f_trunc
},
7761 {"type", 1, 1, f_type
},
7762 {"values", 1, 1, f_values
},
7763 {"virtcol", 1, 1, f_virtcol
},
7764 {"visualmode", 0, 1, f_visualmode
},
7765 {"winbufnr", 1, 1, f_winbufnr
},
7766 {"wincol", 0, 0, f_wincol
},
7767 {"winheight", 1, 1, f_winheight
},
7768 {"winline", 0, 0, f_winline
},
7769 {"winnr", 0, 1, f_winnr
},
7770 {"winrestcmd", 0, 0, f_winrestcmd
},
7771 {"winrestview", 1, 1, f_winrestview
},
7772 {"winsaveview", 0, 0, f_winsaveview
},
7773 {"winwidth", 1, 1, f_winwidth
},
7774 {"writefile", 2, 3, f_writefile
},
7777 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7780 * Function given to ExpandGeneric() to obtain the list of internal
7781 * or user defined function names.
7784 get_function_name(xp
, idx
)
7788 static int intidx
= -1;
7795 name
= get_user_func_name(xp
, idx
);
7799 if (++intidx
< (int)(sizeof(functions
) / sizeof(struct fst
)))
7801 STRCPY(IObuff
, functions
[intidx
].f_name
);
7802 STRCAT(IObuff
, "(");
7803 if (functions
[intidx
].f_max_argc
== 0)
7804 STRCAT(IObuff
, ")");
7812 * Function given to ExpandGeneric() to obtain the list of internal or
7813 * user defined variable or function names.
7817 get_expr_name(xp
, idx
)
7821 static int intidx
= -1;
7828 name
= get_function_name(xp
, idx
);
7832 return get_user_var_name(xp
, ++intidx
);
7835 #endif /* FEAT_CMDL_COMPL */
7838 * Find internal function in table above.
7839 * Return index, or -1 if not found
7842 find_internal_func(name
)
7843 char_u
*name
; /* name of the function */
7846 int last
= (int)(sizeof(functions
) / sizeof(struct fst
)) - 1;
7851 * Find the function name in the table. Binary search.
7853 while (first
<= last
)
7855 x
= first
+ ((unsigned)(last
- first
) >> 1);
7856 cmp
= STRCMP(name
, functions
[x
].f_name
);
7868 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7869 * name it contains, otherwise return "name".
7872 deref_func_name(name
, lenp
)
7881 v
= find_var(name
, NULL
);
7883 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
7885 if (v
->di_tv
.vval
.v_string
== NULL
)
7888 return (char_u
*)""; /* just in case */
7890 *lenp
= (int)STRLEN(v
->di_tv
.vval
.v_string
);
7891 return v
->di_tv
.vval
.v_string
;
7898 * Allocate a variable for the result of a function.
7899 * Return OK or FAIL.
7902 get_func_tv(name
, len
, rettv
, arg
, firstline
, lastline
, doesrange
,
7904 char_u
*name
; /* name of the function */
7905 int len
; /* length of "name" */
7907 char_u
**arg
; /* argument, pointing to the '(' */
7908 linenr_T firstline
; /* first line of range */
7909 linenr_T lastline
; /* last line of range */
7910 int *doesrange
; /* return: function handled range */
7912 dict_T
*selfdict
; /* Dictionary for "self" */
7916 typval_T argvars
[MAX_FUNC_ARGS
+ 1]; /* vars for arguments */
7917 int argcount
= 0; /* number of arguments found */
7920 * Get the arguments.
7923 while (argcount
< MAX_FUNC_ARGS
)
7925 argp
= skipwhite(argp
+ 1); /* skip the '(' or ',' */
7926 if (*argp
== ')' || *argp
== ',' || *argp
== NUL
)
7928 if (eval1(&argp
, &argvars
[argcount
], evaluate
) == FAIL
)
7943 ret
= call_func(name
, len
, rettv
, argcount
, argvars
,
7944 firstline
, lastline
, doesrange
, evaluate
, selfdict
);
7945 else if (!aborting())
7947 if (argcount
== MAX_FUNC_ARGS
)
7948 emsg_funcname("E740: Too many arguments for function %s", name
);
7950 emsg_funcname("E116: Invalid arguments for function %s", name
);
7953 while (--argcount
>= 0)
7954 clear_tv(&argvars
[argcount
]);
7956 *arg
= skipwhite(argp
);
7962 * Call a function with its resolved parameters
7963 * Return OK when the function can't be called, FAIL otherwise.
7964 * Also returns OK when an error was encountered while executing the function.
7967 call_func(name
, len
, rettv
, argcount
, argvars
, firstline
, lastline
,
7968 doesrange
, evaluate
, selfdict
)
7969 char_u
*name
; /* name of the function */
7970 int len
; /* length of "name" */
7971 typval_T
*rettv
; /* return value goes here */
7972 int argcount
; /* number of "argvars" */
7973 typval_T
*argvars
; /* vars for arguments, must have "argcount"
7974 PLUS ONE elements! */
7975 linenr_T firstline
; /* first line of range */
7976 linenr_T lastline
; /* last line of range */
7977 int *doesrange
; /* return: function handled range */
7979 dict_T
*selfdict
; /* Dictionary for "self" */
7982 #define ERROR_UNKNOWN 0
7983 #define ERROR_TOOMANY 1
7984 #define ERROR_TOOFEW 2
7985 #define ERROR_SCRIPT 3
7986 #define ERROR_DICT 4
7987 #define ERROR_NONE 5
7988 #define ERROR_OTHER 6
7989 int error
= ERROR_NONE
;
7994 #define FLEN_FIXED 40
7995 char_u fname_buf
[FLEN_FIXED
+ 1];
7999 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8000 * Change <SNR>123_name() to K_SNR 123_name().
8001 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8005 llen
= eval_fname_script(name
);
8008 fname_buf
[0] = K_SPECIAL
;
8009 fname_buf
[1] = KS_EXTRA
;
8010 fname_buf
[2] = (int)KE_SNR
;
8012 if (eval_fname_sid(name
)) /* "<SID>" or "s:" */
8014 if (current_SID
<= 0)
8015 error
= ERROR_SCRIPT
;
8018 sprintf((char *)fname_buf
+ 3, "%ld_", (long)current_SID
);
8019 i
= (int)STRLEN(fname_buf
);
8022 if (i
+ STRLEN(name
+ llen
) < FLEN_FIXED
)
8024 STRCPY(fname_buf
+ i
, name
+ llen
);
8029 fname
= alloc((unsigned)(i
+ STRLEN(name
+ llen
) + 1));
8031 error
= ERROR_OTHER
;
8034 mch_memmove(fname
, fname_buf
, (size_t)i
);
8035 STRCPY(fname
+ i
, name
+ llen
);
8045 /* execute the function if no errors detected and executing */
8046 if (evaluate
&& error
== ERROR_NONE
)
8048 rettv
->v_type
= VAR_NUMBER
; /* default is number rettv */
8049 error
= ERROR_UNKNOWN
;
8051 if (!builtin_function(fname
))
8054 * User defined function.
8056 fp
= find_func(fname
);
8059 /* Trigger FuncUndefined event, may load the function. */
8061 && apply_autocmds(EVENT_FUNCUNDEFINED
,
8062 fname
, fname
, TRUE
, NULL
)
8065 /* executed an autocommand, search for the function again */
8066 fp
= find_func(fname
);
8069 /* Try loading a package. */
8070 if (fp
== NULL
&& script_autoload(fname
, TRUE
) && !aborting())
8072 /* loaded a package, search for the function again */
8073 fp
= find_func(fname
);
8078 if (fp
->uf_flags
& FC_RANGE
)
8080 if (argcount
< fp
->uf_args
.ga_len
)
8081 error
= ERROR_TOOFEW
;
8082 else if (!fp
->uf_varargs
&& argcount
> fp
->uf_args
.ga_len
)
8083 error
= ERROR_TOOMANY
;
8084 else if ((fp
->uf_flags
& FC_DICT
) && selfdict
== NULL
)
8089 * Call the user function.
8090 * Save and restore search patterns, script variables and
8093 save_search_patterns();
8096 call_user_func(fp
, argcount
, argvars
, rettv
,
8097 firstline
, lastline
,
8098 (fp
->uf_flags
& FC_DICT
) ? selfdict
: NULL
);
8099 if (--fp
->uf_calls
<= 0 && isdigit(*fp
->uf_name
)
8100 && fp
->uf_refcount
<= 0)
8101 /* Function was unreferenced while being used, free it
8105 restore_search_patterns();
8113 * Find the function name in the table, call its implementation.
8115 i
= find_internal_func(fname
);
8118 if (argcount
< functions
[i
].f_min_argc
)
8119 error
= ERROR_TOOFEW
;
8120 else if (argcount
> functions
[i
].f_max_argc
)
8121 error
= ERROR_TOOMANY
;
8124 argvars
[argcount
].v_type
= VAR_UNKNOWN
;
8125 functions
[i
].f_func(argvars
, rettv
);
8131 * The function call (or "FuncUndefined" autocommand sequence) might
8132 * have been aborted by an error, an interrupt, or an explicitly thrown
8133 * exception that has not been caught so far. This situation can be
8134 * tested for by calling aborting(). For an error in an internal
8135 * function or for the "E132" error in call_user_func(), however, the
8136 * throw point at which the "force_abort" flag (temporarily reset by
8137 * emsg()) is normally updated has not been reached yet. We need to
8138 * update that flag first to make aborting() reliable.
8140 update_force_abort();
8142 if (error
== ERROR_NONE
)
8146 * Report an error unless the argument evaluation or function call has been
8147 * cancelled due to an aborting error, an interrupt, or an exception.
8154 emsg_funcname(N_("E117: Unknown function: %s"), name
);
8157 emsg_funcname(e_toomanyarg
, name
);
8160 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
8164 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
8168 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
8175 if (fname
!= name
&& fname
!= fname_buf
)
8182 * Give an error message with a function name. Handle <SNR> things.
8185 emsg_funcname(ermsg
, name
)
8191 if (*name
== K_SPECIAL
)
8192 p
= concat_str((char_u
*)"<SNR>", name
+ 3);
8201 * Return TRUE for a non-zero Number and a non-empty String.
8204 non_zero_arg(argvars
)
8207 return ((argvars
[0].v_type
== VAR_NUMBER
8208 && argvars
[0].vval
.v_number
!= 0)
8209 || (argvars
[0].v_type
== VAR_STRING
8210 && argvars
[0].vval
.v_string
!= NULL
8211 && *argvars
[0].vval
.v_string
!= NUL
));
8214 /*********************************************
8215 * Implementation of the built-in functions
8220 * "abs(expr)" function
8223 f_abs(argvars
, rettv
)
8227 if (argvars
[0].v_type
== VAR_FLOAT
)
8229 rettv
->v_type
= VAR_FLOAT
;
8230 rettv
->vval
.v_float
= fabs(argvars
[0].vval
.v_float
);
8237 n
= get_tv_number_chk(&argvars
[0], &error
);
8239 rettv
->vval
.v_number
= -1;
8241 rettv
->vval
.v_number
= n
;
8243 rettv
->vval
.v_number
= -n
;
8249 * "add(list, item)" function
8252 f_add(argvars
, rettv
)
8258 rettv
->vval
.v_number
= 1; /* Default: Failed */
8259 if (argvars
[0].v_type
== VAR_LIST
)
8261 if ((l
= argvars
[0].vval
.v_list
) != NULL
8262 && !tv_check_lock(l
->lv_lock
, (char_u
*)"add()")
8263 && list_append_tv(l
, &argvars
[1]) == OK
)
8264 copy_tv(&argvars
[0], rettv
);
8271 * "append(lnum, string/list)" function
8274 f_append(argvars
, rettv
)
8281 listitem_T
*li
= NULL
;
8285 lnum
= get_tv_lnum(argvars
);
8287 && lnum
<= curbuf
->b_ml
.ml_line_count
8288 && u_save(lnum
, lnum
+ 1) == OK
)
8290 if (argvars
[1].v_type
== VAR_LIST
)
8292 l
= argvars
[1].vval
.v_list
;
8297 rettv
->vval
.v_number
= 0; /* Default: Success */
8301 tv
= &argvars
[1]; /* append a string */
8302 else if (li
== NULL
)
8303 break; /* end of list */
8305 tv
= &li
->li_tv
; /* append item from list */
8306 line
= get_tv_string_chk(tv
);
8307 if (line
== NULL
) /* type error */
8309 rettv
->vval
.v_number
= 1; /* Failed */
8312 ml_append(lnum
+ added
, line
, (colnr_T
)0, FALSE
);
8319 appended_lines_mark(lnum
, added
);
8320 if (curwin
->w_cursor
.lnum
> lnum
)
8321 curwin
->w_cursor
.lnum
+= added
;
8324 rettv
->vval
.v_number
= 1; /* Failed */
8332 f_argc(argvars
, rettv
)
8336 rettv
->vval
.v_number
= ARGCOUNT
;
8340 * "argidx()" function
8344 f_argidx(argvars
, rettv
)
8348 rettv
->vval
.v_number
= curwin
->w_arg_idx
;
8352 * "argv(nr)" function
8355 f_argv(argvars
, rettv
)
8361 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
8363 idx
= get_tv_number_chk(&argvars
[0], NULL
);
8364 if (idx
>= 0 && idx
< ARGCOUNT
)
8365 rettv
->vval
.v_string
= vim_strsave(alist_name(&ARGLIST
[idx
]));
8367 rettv
->vval
.v_string
= NULL
;
8368 rettv
->v_type
= VAR_STRING
;
8370 else if (rettv_list_alloc(rettv
) == OK
)
8371 for (idx
= 0; idx
< ARGCOUNT
; ++idx
)
8372 list_append_string(rettv
->vval
.v_list
,
8373 alist_name(&ARGLIST
[idx
]), -1);
8377 static int get_float_arg
__ARGS((typval_T
*argvars
, float_T
*f
));
8380 * Get the float value of "argvars[0]" into "f".
8381 * Returns FAIL when the argument is not a Number or Float.
8384 get_float_arg(argvars
, f
)
8388 if (argvars
[0].v_type
== VAR_FLOAT
)
8390 *f
= argvars
[0].vval
.v_float
;
8393 if (argvars
[0].v_type
== VAR_NUMBER
)
8395 *f
= (float_T
)argvars
[0].vval
.v_number
;
8398 EMSG(_("E808: Number or Float required"));
8402 /* The 10 added FP functions are defined immediately before atan() - WJMc */
8408 f_acos(argvars
, rettv
)
8414 rettv
->v_type
= VAR_FLOAT
;
8415 if (get_float_arg(argvars
, &f
) == OK
)
8416 rettv
->vval
.v_float
= acos(f
);
8418 rettv
->vval
.v_float
= 0.0;
8425 f_asin(argvars
, rettv
)
8431 rettv
->v_type
= VAR_FLOAT
;
8432 if (get_float_arg(argvars
, &f
) == OK
)
8433 rettv
->vval
.v_float
= asin(f
);
8435 rettv
->vval
.v_float
= 0.0;
8439 * "atan2()" function
8442 f_atan2(argvars
, rettv
)
8448 rettv
->v_type
= VAR_FLOAT
;
8449 if (get_float_arg(argvars
, &fx
) == OK
8450 && get_float_arg(&argvars
[1], &fy
) == OK
)
8451 rettv
->vval
.v_float
= atan2(fx
, fy
);
8453 rettv
->vval
.v_float
= 0.0;
8460 f_cosh(argvars
, rettv
)
8466 rettv
->v_type
= VAR_FLOAT
;
8467 if (get_float_arg(argvars
, &f
) == OK
)
8468 rettv
->vval
.v_float
= cosh(f
);
8470 rettv
->vval
.v_float
= 0.0;
8477 f_exp(argvars
, rettv
)
8483 rettv
->v_type
= VAR_FLOAT
;
8484 if (get_float_arg(argvars
, &f
) == OK
)
8485 rettv
->vval
.v_float
= exp(f
);
8487 rettv
->vval
.v_float
= 0.0;
8494 f_fmod(argvars
, rettv
)
8500 rettv
->v_type
= VAR_FLOAT
;
8501 if (get_float_arg(argvars
, &fx
) == OK
8502 && get_float_arg(&argvars
[1], &fy
) == OK
)
8503 rettv
->vval
.v_float
= fmod(fx
, fy
);
8505 rettv
->vval
.v_float
= 0.0;
8512 f_log(argvars
, rettv
)
8518 rettv
->v_type
= VAR_FLOAT
;
8519 if (get_float_arg(argvars
, &f
) == OK
)
8520 rettv
->vval
.v_float
= log(f
);
8522 rettv
->vval
.v_float
= 0.0;
8529 f_sinh(argvars
, rettv
)
8535 rettv
->v_type
= VAR_FLOAT
;
8536 if (get_float_arg(argvars
, &f
) == OK
)
8537 rettv
->vval
.v_float
= sinh(f
);
8539 rettv
->vval
.v_float
= 0.0;
8546 f_tan(argvars
, rettv
)
8552 rettv
->v_type
= VAR_FLOAT
;
8553 if (get_float_arg(argvars
, &f
) == OK
)
8554 rettv
->vval
.v_float
= tan(f
);
8556 rettv
->vval
.v_float
= 0.0;
8563 f_tanh(argvars
, rettv
)
8569 rettv
->v_type
= VAR_FLOAT
;
8570 if (get_float_arg(argvars
, &f
) == OK
)
8571 rettv
->vval
.v_float
= tanh(f
);
8573 rettv
->vval
.v_float
= 0.0;
8576 /* End of the 10 added FP functions - WJMc */
8582 f_atan(argvars
, rettv
)
8588 rettv
->v_type
= VAR_FLOAT
;
8589 if (get_float_arg(argvars
, &f
) == OK
)
8590 rettv
->vval
.v_float
= atan(f
);
8592 rettv
->vval
.v_float
= 0.0;
8597 * "browse(save, title, initdir, default)" function
8601 f_browse(argvars
, rettv
)
8610 char_u buf
[NUMBUFLEN
];
8611 char_u buf2
[NUMBUFLEN
];
8614 save
= get_tv_number_chk(&argvars
[0], &error
);
8615 title
= get_tv_string_chk(&argvars
[1]);
8616 initdir
= get_tv_string_buf_chk(&argvars
[2], buf
);
8617 defname
= get_tv_string_buf_chk(&argvars
[3], buf2
);
8619 if (error
|| title
== NULL
|| initdir
== NULL
|| defname
== NULL
)
8620 rettv
->vval
.v_string
= NULL
;
8622 rettv
->vval
.v_string
=
8623 do_browse(save
? BROWSE_SAVE
: 0,
8624 title
, defname
, NULL
, initdir
, NULL
, curbuf
);
8626 rettv
->vval
.v_string
= NULL
;
8628 rettv
->v_type
= VAR_STRING
;
8632 * "browsedir(title, initdir)" function
8636 f_browsedir(argvars
, rettv
)
8643 char_u buf
[NUMBUFLEN
];
8645 title
= get_tv_string_chk(&argvars
[0]);
8646 initdir
= get_tv_string_buf_chk(&argvars
[1], buf
);
8648 if (title
== NULL
|| initdir
== NULL
)
8649 rettv
->vval
.v_string
= NULL
;
8651 rettv
->vval
.v_string
= do_browse(BROWSE_DIR
,
8652 title
, NULL
, NULL
, initdir
, NULL
, curbuf
);
8654 rettv
->vval
.v_string
= NULL
;
8656 rettv
->v_type
= VAR_STRING
;
8659 static buf_T
*find_buffer
__ARGS((typval_T
*avar
));
8662 * Find a buffer by number or exact name.
8670 if (avar
->v_type
== VAR_NUMBER
)
8671 buf
= buflist_findnr((int)avar
->vval
.v_number
);
8672 else if (avar
->v_type
== VAR_STRING
&& avar
->vval
.v_string
!= NULL
)
8674 buf
= buflist_findname_exp(avar
->vval
.v_string
);
8677 /* No full path name match, try a match with a URL or a "nofile"
8678 * buffer, these don't use the full path. */
8679 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
8680 if (buf
->b_fname
!= NULL
8681 && (path_with_url(buf
->b_fname
)
8682 #ifdef FEAT_QUICKFIX
8686 && STRCMP(buf
->b_fname
, avar
->vval
.v_string
) == 0)
8694 * "bufexists(expr)" function
8697 f_bufexists(argvars
, rettv
)
8701 rettv
->vval
.v_number
= (find_buffer(&argvars
[0]) != NULL
);
8705 * "buflisted(expr)" function
8708 f_buflisted(argvars
, rettv
)
8714 buf
= find_buffer(&argvars
[0]);
8715 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_p_bl
);
8719 * "bufloaded(expr)" function
8722 f_bufloaded(argvars
, rettv
)
8728 buf
= find_buffer(&argvars
[0]);
8729 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_ml
.ml_mfp
!= NULL
);
8732 static buf_T
*get_buf_tv
__ARGS((typval_T
*tv
));
8735 * Get buffer by number or pattern.
8741 char_u
*name
= tv
->vval
.v_string
;
8746 if (tv
->v_type
== VAR_NUMBER
)
8747 return buflist_findnr((int)tv
->vval
.v_number
);
8748 if (tv
->v_type
!= VAR_STRING
)
8750 if (name
== NULL
|| *name
== NUL
)
8752 if (name
[0] == '$' && name
[1] == NUL
)
8755 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8756 save_magic
= p_magic
;
8759 p_cpo
= (char_u
*)"";
8761 buf
= buflist_findnr(buflist_findpat(name
, name
+ STRLEN(name
),
8764 p_magic
= save_magic
;
8767 /* If not found, try expanding the name, like done for bufexists(). */
8769 buf
= find_buffer(tv
);
8775 * "bufname(expr)" function
8778 f_bufname(argvars
, rettv
)
8784 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8786 buf
= get_buf_tv(&argvars
[0]);
8787 rettv
->v_type
= VAR_STRING
;
8788 if (buf
!= NULL
&& buf
->b_fname
!= NULL
)
8789 rettv
->vval
.v_string
= vim_strsave(buf
->b_fname
);
8791 rettv
->vval
.v_string
= NULL
;
8796 * "bufnr(expr)" function
8799 f_bufnr(argvars
, rettv
)
8807 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8809 buf
= get_buf_tv(&argvars
[0]);
8812 /* If the buffer isn't found and the second argument is not zero create a
8815 && argvars
[1].v_type
!= VAR_UNKNOWN
8816 && get_tv_number_chk(&argvars
[1], &error
) != 0
8818 && (name
= get_tv_string_chk(&argvars
[0])) != NULL
8820 buf
= buflist_new(name
, NULL
, (linenr_T
)1, 0);
8823 rettv
->vval
.v_number
= buf
->b_fnum
;
8825 rettv
->vval
.v_number
= -1;
8829 * "bufwinnr(nr)" function
8832 f_bufwinnr(argvars
, rettv
)
8842 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8844 buf
= get_buf_tv(&argvars
[0]);
8846 for (wp
= firstwin
; wp
; wp
= wp
->w_next
)
8849 if (wp
->w_buffer
== buf
)
8852 rettv
->vval
.v_number
= (wp
!= NULL
? winnr
: -1);
8854 rettv
->vval
.v_number
= (curwin
->w_buffer
== buf
? 1 : -1);
8860 * "byte2line(byte)" function
8864 f_byte2line(argvars
, rettv
)
8868 #ifndef FEAT_BYTEOFF
8869 rettv
->vval
.v_number
= -1;
8873 boff
= get_tv_number(&argvars
[0]) - 1; /* boff gets -1 on type error */
8875 rettv
->vval
.v_number
= -1;
8877 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
,
8878 (linenr_T
)0, &boff
);
8883 * "byteidx()" function
8887 f_byteidx(argvars
, rettv
)
8897 str
= get_tv_string_chk(&argvars
[0]);
8898 idx
= get_tv_number_chk(&argvars
[1], NULL
);
8899 rettv
->vval
.v_number
= -1;
8900 if (str
== NULL
|| idx
< 0)
8905 for ( ; idx
> 0; idx
--)
8907 if (*t
== NUL
) /* EOL reached */
8909 t
+= (*mb_ptr2len
)(t
);
8911 rettv
->vval
.v_number
= (varnumber_T
)(t
- str
);
8913 if ((size_t)idx
<= STRLEN(str
))
8914 rettv
->vval
.v_number
= idx
;
8919 * "call(func, arglist)" function
8922 f_call(argvars
, rettv
)
8927 typval_T argv
[MAX_FUNC_ARGS
+ 1];
8931 dict_T
*selfdict
= NULL
;
8933 rettv
->vval
.v_number
= 0;
8934 if (argvars
[1].v_type
!= VAR_LIST
)
8939 if (argvars
[1].vval
.v_list
== NULL
)
8942 if (argvars
[0].v_type
== VAR_FUNC
)
8943 func
= argvars
[0].vval
.v_string
;
8945 func
= get_tv_string(&argvars
[0]);
8947 return; /* type error or empty name */
8949 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8951 if (argvars
[2].v_type
!= VAR_DICT
)
8956 selfdict
= argvars
[2].vval
.v_dict
;
8959 for (item
= argvars
[1].vval
.v_list
->lv_first
; item
!= NULL
;
8960 item
= item
->li_next
)
8962 if (argc
== MAX_FUNC_ARGS
)
8964 EMSG(_("E699: Too many arguments"));
8967 /* Make a copy of each argument. This is needed to be able to set
8968 * v_lock to VAR_FIXED in the copy without changing the original list.
8970 copy_tv(&item
->li_tv
, &argv
[argc
++]);
8974 (void)call_func(func
, (int)STRLEN(func
), rettv
, argc
, argv
,
8975 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
8976 &dummy
, TRUE
, selfdict
);
8978 /* Free the arguments. */
8980 clear_tv(&argv
[--argc
]);
8985 * "ceil({float})" function
8988 f_ceil(argvars
, rettv
)
8994 rettv
->v_type
= VAR_FLOAT
;
8995 if (get_float_arg(argvars
, &f
) == OK
)
8996 rettv
->vval
.v_float
= ceil(f
);
8998 rettv
->vval
.v_float
= 0.0;
9003 * "changenr()" function
9007 f_changenr(argvars
, rettv
)
9011 rettv
->vval
.v_number
= curbuf
->b_u_seq_cur
;
9015 * "char2nr(string)" function
9018 f_char2nr(argvars
, rettv
)
9024 rettv
->vval
.v_number
= (*mb_ptr2char
)(get_tv_string(&argvars
[0]));
9027 rettv
->vval
.v_number
= get_tv_string(&argvars
[0])[0];
9031 * "cindent(lnum)" function
9034 f_cindent(argvars
, rettv
)
9042 pos
= curwin
->w_cursor
;
9043 lnum
= get_tv_lnum(argvars
);
9044 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
9046 curwin
->w_cursor
.lnum
= lnum
;
9047 rettv
->vval
.v_number
= get_c_indent();
9048 curwin
->w_cursor
= pos
;
9052 rettv
->vval
.v_number
= -1;
9056 * "clearmatches()" function
9060 f_clearmatches(argvars
, rettv
)
9064 #ifdef FEAT_SEARCH_EXTRA
9065 clear_matches(curwin
);
9070 * "col(string)" function
9073 f_col(argvars
, rettv
)
9079 int fnum
= curbuf
->b_fnum
;
9081 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
9082 if (fp
!= NULL
&& fnum
== curbuf
->b_fnum
)
9084 if (fp
->col
== MAXCOL
)
9086 /* '> can be MAXCOL, get the length of the line then */
9087 if (fp
->lnum
<= curbuf
->b_ml
.ml_line_count
)
9088 col
= (colnr_T
)STRLEN(ml_get(fp
->lnum
)) + 1;
9095 #ifdef FEAT_VIRTUALEDIT
9096 /* col(".") when the cursor is on the NUL at the end of the line
9097 * because of "coladd" can be seen as an extra column. */
9098 if (virtual_active() && fp
== &curwin
->w_cursor
)
9100 char_u
*p
= ml_get_cursor();
9102 if (curwin
->w_cursor
.coladd
>= (colnr_T
)chartabsize(p
,
9103 curwin
->w_virtcol
- curwin
->w_cursor
.coladd
))
9108 if (*p
!= NUL
&& p
[(l
= (*mb_ptr2len
)(p
))] == NUL
)
9111 if (*p
!= NUL
&& p
[1] == NUL
)
9119 rettv
->vval
.v_number
= col
;
9122 #if defined(FEAT_INS_EXPAND)
9124 * "complete()" function
9128 f_complete(argvars
, rettv
)
9134 if ((State
& INSERT
) == 0)
9136 EMSG(_("E785: complete() can only be used in Insert mode"));
9140 /* Check for undo allowed here, because if something was already inserted
9141 * the line was already saved for undo and this check isn't done. */
9142 if (!undo_allowed())
9145 if (argvars
[1].v_type
!= VAR_LIST
|| argvars
[1].vval
.v_list
== NULL
)
9151 startcol
= get_tv_number_chk(&argvars
[0], NULL
);
9155 set_completion(startcol
- 1, argvars
[1].vval
.v_list
);
9159 * "complete_add()" function
9163 f_complete_add(argvars
, rettv
)
9167 rettv
->vval
.v_number
= ins_compl_add_tv(&argvars
[0], 0);
9171 * "complete_check()" function
9175 f_complete_check(argvars
, rettv
)
9179 int saved
= RedrawingDisabled
;
9181 RedrawingDisabled
= 0;
9182 ins_compl_check_keys(0);
9183 rettv
->vval
.v_number
= compl_interrupted
;
9184 RedrawingDisabled
= saved
;
9189 * "confirm(message, buttons[, default [, type]])" function
9193 f_confirm(argvars
, rettv
)
9197 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9199 char_u
*buttons
= NULL
;
9200 char_u buf
[NUMBUFLEN
];
9201 char_u buf2
[NUMBUFLEN
];
9203 int type
= VIM_GENERIC
;
9207 message
= get_tv_string_chk(&argvars
[0]);
9208 if (message
== NULL
)
9210 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9212 buttons
= get_tv_string_buf_chk(&argvars
[1], buf
);
9213 if (buttons
== NULL
)
9215 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9217 def
= get_tv_number_chk(&argvars
[2], &error
);
9218 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9220 typestr
= get_tv_string_buf_chk(&argvars
[3], buf2
);
9221 if (typestr
== NULL
)
9225 switch (TOUPPER_ASC(*typestr
))
9227 case 'E': type
= VIM_ERROR
; break;
9228 case 'Q': type
= VIM_QUESTION
; break;
9229 case 'I': type
= VIM_INFO
; break;
9230 case 'W': type
= VIM_WARNING
; break;
9231 case 'G': type
= VIM_GENERIC
; break;
9238 if (buttons
== NULL
|| *buttons
== NUL
)
9239 buttons
= (char_u
*)_("&Ok");
9242 rettv
->vval
.v_number
= 0;
9244 rettv
->vval
.v_number
= do_dialog(type
, NULL
, message
, buttons
,
9247 rettv
->vval
.v_number
= 0;
9255 f_copy(argvars
, rettv
)
9259 item_copy(&argvars
[0], rettv
, FALSE
, 0);
9267 f_cos(argvars
, rettv
)
9273 rettv
->v_type
= VAR_FLOAT
;
9274 if (get_float_arg(argvars
, &f
) == OK
)
9275 rettv
->vval
.v_float
= cos(f
);
9277 rettv
->vval
.v_float
= 0.0;
9282 * "count()" function
9285 f_count(argvars
, rettv
)
9292 if (argvars
[0].v_type
== VAR_LIST
)
9298 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
9301 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9305 ic
= get_tv_number_chk(&argvars
[2], &error
);
9306 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9308 idx
= get_tv_number_chk(&argvars
[3], &error
);
9311 li
= list_find(l
, idx
);
9313 EMSGN(_(e_listidx
), idx
);
9320 for ( ; li
!= NULL
; li
= li
->li_next
)
9321 if (tv_equal(&li
->li_tv
, &argvars
[1], ic
))
9325 else if (argvars
[0].v_type
== VAR_DICT
)
9331 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
9335 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9337 ic
= get_tv_number_chk(&argvars
[2], &error
);
9338 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9342 todo
= error
? 0 : (int)d
->dv_hashtab
.ht_used
;
9343 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
9345 if (!HASHITEM_EMPTY(hi
))
9348 if (tv_equal(&HI2DI(hi
)->di_tv
, &argvars
[1], ic
))
9355 EMSG2(_(e_listdictarg
), "count()");
9356 rettv
->vval
.v_number
= n
;
9360 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9362 * Checks the existence of a cscope connection.
9366 f_cscope_connection(argvars
, rettv
)
9372 char_u
*dbpath
= NULL
;
9373 char_u
*prepend
= NULL
;
9374 char_u buf
[NUMBUFLEN
];
9376 if (argvars
[0].v_type
!= VAR_UNKNOWN
9377 && argvars
[1].v_type
!= VAR_UNKNOWN
)
9379 num
= (int)get_tv_number(&argvars
[0]);
9380 dbpath
= get_tv_string(&argvars
[1]);
9381 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9382 prepend
= get_tv_string_buf(&argvars
[2], buf
);
9385 rettv
->vval
.v_number
= cs_connection(num
, dbpath
, prepend
);
9387 rettv
->vval
.v_number
= 0;
9392 * "cursor(lnum, col)" function
9394 * Moves the cursor to the specified line and column
9398 f_cursor(argvars
, rettv
)
9403 #ifdef FEAT_VIRTUALEDIT
9407 if (argvars
[1].v_type
== VAR_UNKNOWN
)
9411 if (list2fpos(argvars
, &pos
, NULL
) == FAIL
)
9415 #ifdef FEAT_VIRTUALEDIT
9416 coladd
= pos
.coladd
;
9421 line
= get_tv_lnum(argvars
);
9422 col
= get_tv_number_chk(&argvars
[1], NULL
);
9423 #ifdef FEAT_VIRTUALEDIT
9424 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9425 coladd
= get_tv_number_chk(&argvars
[2], NULL
);
9428 if (line
< 0 || col
< 0
9429 #ifdef FEAT_VIRTUALEDIT
9433 return; /* type error; errmsg already given */
9435 curwin
->w_cursor
.lnum
= line
;
9437 curwin
->w_cursor
.col
= col
- 1;
9438 #ifdef FEAT_VIRTUALEDIT
9439 curwin
->w_cursor
.coladd
= coladd
;
9442 /* Make sure the cursor is in a valid position. */
9445 /* Correct cursor for multi-byte character. */
9450 curwin
->w_set_curswant
= TRUE
;
9454 * "deepcopy()" function
9457 f_deepcopy(argvars
, rettv
)
9463 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9464 noref
= get_tv_number_chk(&argvars
[1], NULL
);
9465 if (noref
< 0 || noref
> 1)
9468 item_copy(&argvars
[0], rettv
, TRUE
, noref
== 0 ? ++current_copyID
: 0);
9472 * "delete()" function
9475 f_delete(argvars
, rettv
)
9479 if (check_restricted() || check_secure())
9480 rettv
->vval
.v_number
= -1;
9482 rettv
->vval
.v_number
= mch_remove(get_tv_string(&argvars
[0]));
9486 * "did_filetype()" function
9490 f_did_filetype(argvars
, rettv
)
9495 rettv
->vval
.v_number
= did_filetype
;
9497 rettv
->vval
.v_number
= 0;
9502 * "diff_filler()" function
9506 f_diff_filler(argvars
, rettv
)
9511 rettv
->vval
.v_number
= diff_check_fill(curwin
, get_tv_lnum(argvars
));
9516 * "diff_hlID()" function
9520 f_diff_hlID(argvars
, rettv
)
9525 linenr_T lnum
= get_tv_lnum(argvars
);
9526 static linenr_T prev_lnum
= 0;
9527 static int changedtick
= 0;
9528 static int fnum
= 0;
9529 static int change_start
= 0;
9530 static int change_end
= 0;
9531 static hlf_T hlID
= (hlf_T
)0;
9535 if (lnum
< 0) /* ignore type error in {lnum} arg */
9537 if (lnum
!= prev_lnum
9538 || changedtick
!= curbuf
->b_changedtick
9539 || fnum
!= curbuf
->b_fnum
)
9541 /* New line, buffer, change: need to get the values. */
9542 filler_lines
= diff_check(curwin
, lnum
);
9543 if (filler_lines
< 0)
9545 if (filler_lines
== -1)
9547 change_start
= MAXCOL
;
9549 if (diff_find_change(curwin
, lnum
, &change_start
, &change_end
))
9550 hlID
= HLF_ADD
; /* added line */
9552 hlID
= HLF_CHD
; /* changed line */
9555 hlID
= HLF_ADD
; /* added line */
9560 changedtick
= curbuf
->b_changedtick
;
9561 fnum
= curbuf
->b_fnum
;
9564 if (hlID
== HLF_CHD
|| hlID
== HLF_TXD
)
9566 col
= get_tv_number(&argvars
[1]) - 1; /* ignore type error in {col} */
9567 if (col
>= change_start
&& col
<= change_end
)
9568 hlID
= HLF_TXD
; /* changed text */
9570 hlID
= HLF_CHD
; /* changed line */
9572 rettv
->vval
.v_number
= hlID
== (hlf_T
)0 ? 0 : (int)hlID
;
9577 * "empty({expr})" function
9580 f_empty(argvars
, rettv
)
9586 switch (argvars
[0].v_type
)
9590 n
= argvars
[0].vval
.v_string
== NULL
9591 || *argvars
[0].vval
.v_string
== NUL
;
9594 n
= argvars
[0].vval
.v_number
== 0;
9598 n
= argvars
[0].vval
.v_float
== 0.0;
9602 n
= argvars
[0].vval
.v_list
== NULL
9603 || argvars
[0].vval
.v_list
->lv_first
== NULL
;
9606 n
= argvars
[0].vval
.v_dict
== NULL
9607 || argvars
[0].vval
.v_dict
->dv_hashtab
.ht_used
== 0;
9610 EMSG2(_(e_intern2
), "f_empty()");
9614 rettv
->vval
.v_number
= n
;
9618 * "escape({string}, {chars})" function
9621 f_escape(argvars
, rettv
)
9625 char_u buf
[NUMBUFLEN
];
9627 rettv
->vval
.v_string
= vim_strsave_escaped(get_tv_string(&argvars
[0]),
9628 get_tv_string_buf(&argvars
[1], buf
));
9629 rettv
->v_type
= VAR_STRING
;
9637 f_eval(argvars
, rettv
)
9643 s
= get_tv_string_chk(&argvars
[0]);
9647 if (s
== NULL
|| eval1(&s
, rettv
, TRUE
) == FAIL
)
9649 rettv
->v_type
= VAR_NUMBER
;
9650 rettv
->vval
.v_number
= 0;
9653 EMSG(_(e_trailing
));
9657 * "eventhandler()" function
9661 f_eventhandler(argvars
, rettv
)
9665 rettv
->vval
.v_number
= vgetc_busy
;
9669 * "executable()" function
9672 f_executable(argvars
, rettv
)
9676 rettv
->vval
.v_number
= mch_can_exe(get_tv_string(&argvars
[0]));
9680 * "exists()" function
9683 f_exists(argvars
, rettv
)
9692 p
= get_tv_string(&argvars
[0]);
9693 if (*p
== '$') /* environment variable */
9695 /* first try "normal" environment variables (fast) */
9696 if (mch_getenv(p
+ 1) != NULL
)
9700 /* try expanding things like $VIM and ${HOME} */
9701 p
= expand_env_save(p
);
9702 if (p
!= NULL
&& *p
!= '$')
9707 else if (*p
== '&' || *p
== '+') /* option */
9709 n
= (get_option_tv(&p
, NULL
, TRUE
) == OK
);
9710 if (*skipwhite(p
) != NUL
)
9711 n
= FALSE
; /* trailing garbage */
9713 else if (*p
== '*') /* internal or user defined function */
9715 n
= function_exists(p
+ 1);
9719 n
= cmd_exists(p
+ 1);
9725 n
= autocmd_supported(p
+ 2);
9727 n
= au_exists(p
+ 1);
9730 else /* internal variable */
9735 /* get_name_len() takes care of expanding curly braces */
9737 len
= get_name_len(&p
, &tofree
, TRUE
, FALSE
);
9742 n
= (get_var_tv(name
, len
, &tv
, FALSE
) == OK
);
9745 /* handle d.key, l[idx], f(expr) */
9746 n
= (handle_subscript(&p
, &tv
, TRUE
, FALSE
) == OK
);
9757 rettv
->vval
.v_number
= n
;
9761 * "expand()" function
9764 f_expand(argvars
, rettv
)
9771 int flags
= WILD_SILENT
|WILD_USE_NL
|WILD_LIST_NOTFOUND
;
9775 rettv
->v_type
= VAR_STRING
;
9776 s
= get_tv_string(&argvars
[0]);
9777 if (*s
== '%' || *s
== '#' || *s
== '<')
9780 rettv
->vval
.v_string
= eval_vars(s
, s
, &len
, NULL
, &errormsg
, NULL
);
9785 /* When the optional second argument is non-zero, don't remove matches
9786 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
9787 if (argvars
[1].v_type
!= VAR_UNKNOWN
9788 && get_tv_number_chk(&argvars
[1], &error
))
9789 flags
|= WILD_KEEP_ALL
;
9793 xpc
.xp_context
= EXPAND_FILES
;
9794 rettv
->vval
.v_string
= ExpandOne(&xpc
, s
, NULL
, flags
, WILD_ALL
);
9797 rettv
->vval
.v_string
= NULL
;
9802 * "extend(list, list [, idx])" function
9803 * "extend(dict, dict [, action])" function
9806 f_extend(argvars
, rettv
)
9810 rettv
->vval
.v_number
= 0;
9811 if (argvars
[0].v_type
== VAR_LIST
&& argvars
[1].v_type
== VAR_LIST
)
9818 l1
= argvars
[0].vval
.v_list
;
9819 l2
= argvars
[1].vval
.v_list
;
9820 if (l1
!= NULL
&& !tv_check_lock(l1
->lv_lock
, (char_u
*)"extend()")
9823 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9825 before
= get_tv_number_chk(&argvars
[2], &error
);
9827 return; /* type error; errmsg already given */
9829 if (before
== l1
->lv_len
)
9833 item
= list_find(l1
, before
);
9836 EMSGN(_(e_listidx
), before
);
9843 list_extend(l1
, l2
, item
);
9845 copy_tv(&argvars
[0], rettv
);
9848 else if (argvars
[0].v_type
== VAR_DICT
&& argvars
[1].v_type
== VAR_DICT
)
9857 d1
= argvars
[0].vval
.v_dict
;
9858 d2
= argvars
[1].vval
.v_dict
;
9859 if (d1
!= NULL
&& !tv_check_lock(d1
->dv_lock
, (char_u
*)"extend()")
9862 /* Check the third argument. */
9863 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9865 static char *(av
[]) = {"keep", "force", "error"};
9867 action
= get_tv_string_chk(&argvars
[2]);
9869 return; /* type error; errmsg already given */
9870 for (i
= 0; i
< 3; ++i
)
9871 if (STRCMP(action
, av
[i
]) == 0)
9875 EMSG2(_(e_invarg2
), action
);
9880 action
= (char_u
*)"force";
9882 /* Go over all entries in the second dict and add them to the
9884 todo
= (int)d2
->dv_hashtab
.ht_used
;
9885 for (hi2
= d2
->dv_hashtab
.ht_array
; todo
> 0; ++hi2
)
9887 if (!HASHITEM_EMPTY(hi2
))
9890 di1
= dict_find(d1
, hi2
->hi_key
, -1);
9893 di1
= dictitem_copy(HI2DI(hi2
));
9894 if (di1
!= NULL
&& dict_add(d1
, di1
) == FAIL
)
9897 else if (*action
== 'e')
9899 EMSG2(_("E737: Key already exists: %s"), hi2
->hi_key
);
9902 else if (*action
== 'f')
9904 clear_tv(&di1
->di_tv
);
9905 copy_tv(&HI2DI(hi2
)->di_tv
, &di1
->di_tv
);
9910 copy_tv(&argvars
[0], rettv
);
9914 EMSG2(_(e_listdictarg
), "extend()");
9918 * "feedkeys()" function
9922 f_feedkeys(argvars
, rettv
)
9927 char_u
*keys
, *flags
;
9928 char_u nbuf
[NUMBUFLEN
];
9932 /* This is not allowed in the sandbox. If the commands would still be
9933 * executed in the sandbox it would be OK, but it probably happens later,
9934 * when "sandbox" is no longer set. */
9938 rettv
->vval
.v_number
= 0;
9939 keys
= get_tv_string(&argvars
[0]);
9942 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9944 flags
= get_tv_string_buf(&argvars
[1], nbuf
);
9945 for ( ; *flags
!= NUL
; ++flags
)
9949 case 'n': remap
= FALSE
; break;
9950 case 'm': remap
= TRUE
; break;
9951 case 't': typed
= TRUE
; break;
9956 /* Need to escape K_SPECIAL and CSI before putting the string in the
9957 * typeahead buffer. */
9958 keys_esc
= vim_strsave_escape_csi(keys
);
9959 if (keys_esc
!= NULL
)
9961 ins_typebuf(keys_esc
, (remap
? REMAP_YES
: REMAP_NONE
),
9962 typebuf
.tb_len
, !typed
, FALSE
);
9965 typebuf_was_filled
= TRUE
;
9971 * "filereadable()" function
9974 f_filereadable(argvars
, rettv
)
9983 # define O_NONBLOCK 0
9985 p
= get_tv_string(&argvars
[0]);
9986 if (*p
&& !mch_isdir(p
) && (fd
= mch_open((char *)p
,
9987 O_RDONLY
| O_NONBLOCK
, 0)) >= 0)
9995 rettv
->vval
.v_number
= n
;
9999 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
10000 * rights to write into.
10003 f_filewritable(argvars
, rettv
)
10007 rettv
->vval
.v_number
= filewritable(get_tv_string(&argvars
[0]));
10010 static void findfilendir
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int find_what
));
10013 findfilendir(argvars
, rettv
, find_what
)
10018 #ifdef FEAT_SEARCHPATH
10020 char_u
*fresult
= NULL
;
10021 char_u
*path
= *curbuf
->b_p_path
== NUL
? p_path
: curbuf
->b_p_path
;
10023 char_u pathbuf
[NUMBUFLEN
];
10029 rettv
->vval
.v_string
= NULL
;
10030 rettv
->v_type
= VAR_STRING
;
10032 #ifdef FEAT_SEARCHPATH
10033 fname
= get_tv_string(&argvars
[0]);
10035 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
10037 p
= get_tv_string_buf_chk(&argvars
[1], pathbuf
);
10045 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
10046 count
= get_tv_number_chk(&argvars
[2], &error
);
10050 if (count
< 0 && rettv_list_alloc(rettv
) == FAIL
)
10053 if (*fname
!= NUL
&& !error
)
10057 if (rettv
->v_type
== VAR_STRING
)
10059 fresult
= find_file_in_path_option(first
? fname
: NULL
,
10060 first
? (int)STRLEN(fname
) : 0,
10064 find_what
== FINDFILE_DIR
10065 ? (char_u
*)"" : curbuf
->b_p_sua
);
10068 if (fresult
!= NULL
&& rettv
->v_type
== VAR_LIST
)
10069 list_append_string(rettv
->vval
.v_list
, fresult
, -1);
10071 } while ((rettv
->v_type
== VAR_LIST
|| --count
> 0) && fresult
!= NULL
);
10074 if (rettv
->v_type
== VAR_STRING
)
10075 rettv
->vval
.v_string
= fresult
;
10079 static void filter_map
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int map
));
10080 static int filter_map_one
__ARGS((typval_T
*tv
, char_u
*expr
, int map
, int *remp
));
10083 * Implementation of map() and filter().
10086 filter_map(argvars
, rettv
, map
)
10091 char_u buf
[NUMBUFLEN
];
10093 listitem_T
*li
, *nli
;
10103 char_u
*ermsg
= map
? (char_u
*)"map()" : (char_u
*)"filter()";
10106 rettv
->vval
.v_number
= 0;
10107 if (argvars
[0].v_type
== VAR_LIST
)
10109 if ((l
= argvars
[0].vval
.v_list
) == NULL
10110 || (map
&& tv_check_lock(l
->lv_lock
, ermsg
)))
10113 else if (argvars
[0].v_type
== VAR_DICT
)
10115 if ((d
= argvars
[0].vval
.v_dict
) == NULL
10116 || (map
&& tv_check_lock(d
->dv_lock
, ermsg
)))
10121 EMSG2(_(e_listdictarg
), ermsg
);
10125 expr
= get_tv_string_buf_chk(&argvars
[1], buf
);
10126 /* On type errors, the preceding call has already displayed an error
10127 * message. Avoid a misleading error message for an empty string that
10128 * was not passed as argument. */
10131 prepare_vimvar(VV_VAL
, &save_val
);
10132 expr
= skipwhite(expr
);
10134 /* We reset "did_emsg" to be able to detect whether an error
10135 * occurred during evaluation of the expression. */
10136 save_did_emsg
= did_emsg
;
10139 if (argvars
[0].v_type
== VAR_DICT
)
10141 prepare_vimvar(VV_KEY
, &save_key
);
10142 vimvars
[VV_KEY
].vv_type
= VAR_STRING
;
10144 ht
= &d
->dv_hashtab
;
10146 todo
= (int)ht
->ht_used
;
10147 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
10149 if (!HASHITEM_EMPTY(hi
))
10153 if (tv_check_lock(di
->di_tv
.v_lock
, ermsg
))
10155 vimvars
[VV_KEY
].vv_str
= vim_strsave(di
->di_key
);
10156 if (filter_map_one(&di
->di_tv
, expr
, map
, &rem
) == FAIL
10160 dictitem_remove(d
, di
);
10161 clear_tv(&vimvars
[VV_KEY
].vv_tv
);
10166 restore_vimvar(VV_KEY
, &save_key
);
10170 for (li
= l
->lv_first
; li
!= NULL
; li
= nli
)
10172 if (tv_check_lock(li
->li_tv
.v_lock
, ermsg
))
10175 if (filter_map_one(&li
->li_tv
, expr
, map
, &rem
) == FAIL
10179 listitem_remove(l
, li
);
10183 restore_vimvar(VV_VAL
, &save_val
);
10185 did_emsg
|= save_did_emsg
;
10188 copy_tv(&argvars
[0], rettv
);
10192 filter_map_one(tv
, expr
, map
, remp
)
10202 copy_tv(tv
, &vimvars
[VV_VAL
].vv_tv
);
10204 if (eval1(&s
, &rettv
, TRUE
) == FAIL
)
10206 if (*s
!= NUL
) /* check for trailing chars after expr */
10208 EMSG2(_(e_invexpr2
), s
);
10213 /* map(): replace the list item value */
10222 /* filter(): when expr is zero remove the item */
10223 *remp
= (get_tv_number_chk(&rettv
, &error
) == 0);
10225 /* On type error, nothing has been removed; return FAIL to stop the
10226 * loop. The error message was given by get_tv_number_chk(). */
10232 clear_tv(&vimvars
[VV_VAL
].vv_tv
);
10237 * "filter()" function
10240 f_filter(argvars
, rettv
)
10244 filter_map(argvars
, rettv
, FALSE
);
10248 * "finddir({fname}[, {path}[, {count}]])" function
10251 f_finddir(argvars
, rettv
)
10255 findfilendir(argvars
, rettv
, FINDFILE_DIR
);
10259 * "findfile({fname}[, {path}[, {count}]])" function
10262 f_findfile(argvars
, rettv
)
10266 findfilendir(argvars
, rettv
, FINDFILE_FILE
);
10271 * "float2nr({float})" function
10274 f_float2nr(argvars
, rettv
)
10280 if (get_float_arg(argvars
, &f
) == OK
)
10282 if (f
< -0x7fffffff)
10283 rettv
->vval
.v_number
= -0x7fffffff;
10284 else if (f
> 0x7fffffff)
10285 rettv
->vval
.v_number
= 0x7fffffff;
10287 rettv
->vval
.v_number
= (varnumber_T
)f
;
10290 rettv
->vval
.v_number
= 0;
10294 * "floor({float})" function
10297 f_floor(argvars
, rettv
)
10303 rettv
->v_type
= VAR_FLOAT
;
10304 if (get_float_arg(argvars
, &f
) == OK
)
10305 rettv
->vval
.v_float
= floor(f
);
10307 rettv
->vval
.v_float
= 0.0;
10312 * "fnameescape({string})" function
10315 f_fnameescape(argvars
, rettv
)
10319 rettv
->vval
.v_string
= vim_strsave_fnameescape(
10320 get_tv_string(&argvars
[0]), FALSE
);
10321 rettv
->v_type
= VAR_STRING
;
10325 * "fnamemodify({fname}, {mods})" function
10328 f_fnamemodify(argvars
, rettv
)
10336 char_u
*fbuf
= NULL
;
10337 char_u buf
[NUMBUFLEN
];
10339 fname
= get_tv_string_chk(&argvars
[0]);
10340 mods
= get_tv_string_buf_chk(&argvars
[1], buf
);
10341 if (fname
== NULL
|| mods
== NULL
)
10345 len
= (int)STRLEN(fname
);
10346 (void)modify_fname(mods
, &usedlen
, &fname
, &fbuf
, &len
);
10349 rettv
->v_type
= VAR_STRING
;
10351 rettv
->vval
.v_string
= NULL
;
10353 rettv
->vval
.v_string
= vim_strnsave(fname
, len
);
10357 static void foldclosed_both
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int end
));
10360 * "foldclosed()" function
10363 foldclosed_both(argvars
, rettv
, end
)
10368 #ifdef FEAT_FOLDING
10370 linenr_T first
, last
;
10372 lnum
= get_tv_lnum(argvars
);
10373 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
10375 if (hasFoldingWin(curwin
, lnum
, &first
, &last
, FALSE
, NULL
))
10378 rettv
->vval
.v_number
= (varnumber_T
)last
;
10380 rettv
->vval
.v_number
= (varnumber_T
)first
;
10385 rettv
->vval
.v_number
= -1;
10389 * "foldclosed()" function
10392 f_foldclosed(argvars
, rettv
)
10396 foldclosed_both(argvars
, rettv
, FALSE
);
10400 * "foldclosedend()" function
10403 f_foldclosedend(argvars
, rettv
)
10407 foldclosed_both(argvars
, rettv
, TRUE
);
10411 * "foldlevel()" function
10414 f_foldlevel(argvars
, rettv
)
10418 #ifdef FEAT_FOLDING
10421 lnum
= get_tv_lnum(argvars
);
10422 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
10423 rettv
->vval
.v_number
= foldLevel(lnum
);
10426 rettv
->vval
.v_number
= 0;
10430 * "foldtext()" function
10434 f_foldtext(argvars
, rettv
)
10438 #ifdef FEAT_FOLDING
10446 rettv
->v_type
= VAR_STRING
;
10447 rettv
->vval
.v_string
= NULL
;
10448 #ifdef FEAT_FOLDING
10449 if ((linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
> 0
10450 && (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
10451 <= curbuf
->b_ml
.ml_line_count
10452 && vimvars
[VV_FOLDDASHES
].vv_str
!= NULL
)
10454 /* Find first non-empty line in the fold. */
10455 lnum
= (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
;
10456 while (lnum
< (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
10458 if (!linewhite(lnum
))
10463 /* Find interesting text in this line. */
10464 s
= skipwhite(ml_get(lnum
));
10465 /* skip C comment-start */
10466 if (s
[0] == '/' && (s
[1] == '*' || s
[1] == '/'))
10468 s
= skipwhite(s
+ 2);
10469 if (*skipwhite(s
) == NUL
10470 && lnum
+ 1 < (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
10472 s
= skipwhite(ml_get(lnum
+ 1));
10474 s
= skipwhite(s
+ 1);
10477 txt
= _("+-%s%3ld lines: ");
10478 r
= alloc((unsigned)(STRLEN(txt
)
10479 + STRLEN(vimvars
[VV_FOLDDASHES
].vv_str
) /* for %s */
10480 + 20 /* for %3ld */
10481 + STRLEN(s
))); /* concatenated */
10484 sprintf((char *)r
, txt
, vimvars
[VV_FOLDDASHES
].vv_str
,
10485 (long)((linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
10486 - (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
+ 1));
10487 len
= (int)STRLEN(r
);
10489 /* remove 'foldmarker' and 'commentstring' */
10490 foldtext_cleanup(r
+ len
);
10491 rettv
->vval
.v_string
= r
;
10498 * "foldtextresult(lnum)" function
10502 f_foldtextresult(argvars
, rettv
)
10506 #ifdef FEAT_FOLDING
10510 foldinfo_T foldinfo
;
10514 rettv
->v_type
= VAR_STRING
;
10515 rettv
->vval
.v_string
= NULL
;
10516 #ifdef FEAT_FOLDING
10517 lnum
= get_tv_lnum(argvars
);
10518 /* treat illegal types and illegal string values for {lnum} the same */
10521 fold_count
= foldedCount(curwin
, lnum
, &foldinfo
);
10522 if (fold_count
> 0)
10524 text
= get_foldtext(curwin
, lnum
, lnum
+ fold_count
- 1,
10527 text
= vim_strsave(text
);
10528 rettv
->vval
.v_string
= text
;
10534 * "foreground()" function
10538 f_foreground(argvars
, rettv
)
10542 rettv
->vval
.v_number
= 0;
10545 gui_mch_set_foreground();
10548 win32_set_foreground();
10554 * "function()" function
10558 f_function(argvars
, rettv
)
10564 rettv
->vval
.v_number
= 0;
10565 s
= get_tv_string(&argvars
[0]);
10566 if (s
== NULL
|| *s
== NUL
|| VIM_ISDIGIT(*s
))
10567 EMSG2(_(e_invarg2
), s
);
10568 /* Don't check an autoload name for existence here. */
10569 else if (vim_strchr(s
, AUTOLOAD_CHAR
) == NULL
&& !function_exists(s
))
10570 EMSG2(_("E700: Unknown function: %s"), s
);
10573 rettv
->vval
.v_string
= vim_strsave(s
);
10574 rettv
->v_type
= VAR_FUNC
;
10579 * "garbagecollect()" function
10583 f_garbagecollect(argvars
, rettv
)
10587 /* This is postponed until we are back at the toplevel, because we may be
10588 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10589 want_garbage_collect
= TRUE
;
10591 if (argvars
[0].v_type
!= VAR_UNKNOWN
&& get_tv_number(&argvars
[0]) == 1)
10592 garbage_collect_at_exit
= TRUE
;
10599 f_get(argvars
, rettv
)
10607 typval_T
*tv
= NULL
;
10609 if (argvars
[0].v_type
== VAR_LIST
)
10611 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
10615 li
= list_find(l
, get_tv_number_chk(&argvars
[1], &error
));
10616 if (!error
&& li
!= NULL
)
10620 else if (argvars
[0].v_type
== VAR_DICT
)
10622 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
10624 di
= dict_find(d
, get_tv_string(&argvars
[1]), -1);
10630 EMSG2(_(e_listdictarg
), "get()");
10634 if (argvars
[2].v_type
== VAR_UNKNOWN
)
10635 rettv
->vval
.v_number
= 0;
10637 copy_tv(&argvars
[2], rettv
);
10640 copy_tv(tv
, rettv
);
10643 static void get_buffer_lines
__ARGS((buf_T
*buf
, linenr_T start
, linenr_T end
, int retlist
, typval_T
*rettv
));
10646 * Get line or list of lines from buffer "buf" into "rettv".
10647 * Return a range (from start to end) of lines in rettv from the specified
10649 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
10652 get_buffer_lines(buf
, start
, end
, retlist
, rettv
)
10663 if (rettv_list_alloc(rettv
) == FAIL
)
10667 rettv
->vval
.v_number
= 0;
10669 if (buf
== NULL
|| buf
->b_ml
.ml_mfp
== NULL
|| start
< 0)
10674 if (start
>= 1 && start
<= buf
->b_ml
.ml_line_count
)
10675 p
= ml_get_buf(buf
, start
, FALSE
);
10679 rettv
->v_type
= VAR_STRING
;
10680 rettv
->vval
.v_string
= vim_strsave(p
);
10689 if (end
> buf
->b_ml
.ml_line_count
)
10690 end
= buf
->b_ml
.ml_line_count
;
10691 while (start
<= end
)
10692 if (list_append_string(rettv
->vval
.v_list
,
10693 ml_get_buf(buf
, start
++, FALSE
), -1) == FAIL
)
10699 * "getbufline()" function
10702 f_getbufline(argvars
, rettv
)
10710 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
10712 buf
= get_buf_tv(&argvars
[0]);
10715 lnum
= get_tv_lnum_buf(&argvars
[1], buf
);
10716 if (argvars
[2].v_type
== VAR_UNKNOWN
)
10719 end
= get_tv_lnum_buf(&argvars
[2], buf
);
10721 get_buffer_lines(buf
, lnum
, end
, TRUE
, rettv
);
10725 * "getbufvar()" function
10728 f_getbufvar(argvars
, rettv
)
10733 buf_T
*save_curbuf
;
10737 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
10738 varname
= get_tv_string_chk(&argvars
[1]);
10740 buf
= get_buf_tv(&argvars
[0]);
10742 rettv
->v_type
= VAR_STRING
;
10743 rettv
->vval
.v_string
= NULL
;
10745 if (buf
!= NULL
&& varname
!= NULL
)
10747 /* set curbuf to be our buf, temporarily */
10748 save_curbuf
= curbuf
;
10751 if (*varname
== '&') /* buffer-local-option */
10752 get_option_tv(&varname
, rettv
, TRUE
);
10755 if (*varname
== NUL
)
10756 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10757 * scope prefix before the NUL byte is required by
10758 * find_var_in_ht(). */
10759 varname
= (char_u
*)"b:" + 2;
10760 /* look up the variable */
10761 v
= find_var_in_ht(&curbuf
->b_vars
.dv_hashtab
, varname
, FALSE
);
10763 copy_tv(&v
->di_tv
, rettv
);
10766 /* restore previous notion of curbuf */
10767 curbuf
= save_curbuf
;
10774 * "getchar()" function
10777 f_getchar(argvars
, rettv
)
10784 /* Position the cursor. Needed after a message that ends in a space. */
10785 windgoto(msg_row
, msg_col
);
10791 if (argvars
[0].v_type
== VAR_UNKNOWN
)
10792 /* getchar(): blocking wait. */
10794 else if (get_tv_number_chk(&argvars
[0], &error
) == 1)
10795 /* getchar(1): only check if char avail */
10797 else if (error
|| vpeekc() == NUL
)
10798 /* illegal argument or getchar(0) and no char avail: return zero */
10801 /* getchar(0) and char avail: return char */
10810 vimvars
[VV_MOUSE_WIN
].vv_nr
= 0;
10811 vimvars
[VV_MOUSE_LNUM
].vv_nr
= 0;
10812 vimvars
[VV_MOUSE_COL
].vv_nr
= 0;
10814 rettv
->vval
.v_number
= n
;
10815 if (IS_SPECIAL(n
) || mod_mask
!= 0)
10817 char_u temp
[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10820 /* Turn a special key into three bytes, plus modifier. */
10823 temp
[i
++] = K_SPECIAL
;
10824 temp
[i
++] = KS_MODIFIER
;
10825 temp
[i
++] = mod_mask
;
10829 temp
[i
++] = K_SPECIAL
;
10830 temp
[i
++] = K_SECOND(n
);
10831 temp
[i
++] = K_THIRD(n
);
10834 else if (has_mbyte
)
10835 i
+= (*mb_char2bytes
)(n
, temp
+ i
);
10840 rettv
->v_type
= VAR_STRING
;
10841 rettv
->vval
.v_string
= vim_strsave(temp
);
10844 if (n
== K_LEFTMOUSE
10845 || n
== K_LEFTMOUSE_NM
10847 || n
== K_LEFTRELEASE
10848 || n
== K_LEFTRELEASE_NM
10849 || n
== K_MIDDLEMOUSE
10850 || n
== K_MIDDLEDRAG
10851 || n
== K_MIDDLERELEASE
10852 || n
== K_RIGHTMOUSE
10853 || n
== K_RIGHTDRAG
10854 || n
== K_RIGHTRELEASE
10857 || n
== K_X1RELEASE
10860 || n
== K_X2RELEASE
10861 || n
== K_MOUSEDOWN
10864 int row
= mouse_row
;
10865 int col
= mouse_col
;
10868 # ifdef FEAT_WINDOWS
10873 if (row
>= 0 && col
>= 0)
10875 /* Find the window at the mouse coordinates and compute the
10876 * text position. */
10877 win
= mouse_find_win(&row
, &col
);
10878 (void)mouse_comp_pos(win
, &row
, &col
, &lnum
);
10879 # ifdef FEAT_WINDOWS
10880 for (wp
= firstwin
; wp
!= win
; wp
= wp
->w_next
)
10883 vimvars
[VV_MOUSE_WIN
].vv_nr
= winnr
;
10884 vimvars
[VV_MOUSE_LNUM
].vv_nr
= lnum
;
10885 vimvars
[VV_MOUSE_COL
].vv_nr
= col
+ 1;
10893 * "getcharmod()" function
10897 f_getcharmod(argvars
, rettv
)
10901 rettv
->vval
.v_number
= mod_mask
;
10905 * "getcmdline()" function
10909 f_getcmdline(argvars
, rettv
)
10913 rettv
->v_type
= VAR_STRING
;
10914 rettv
->vval
.v_string
= get_cmdline_str();
10918 * "getcmdpos()" function
10922 f_getcmdpos(argvars
, rettv
)
10926 rettv
->vval
.v_number
= get_cmdline_pos() + 1;
10930 * "getcmdtype()" function
10934 f_getcmdtype(argvars
, rettv
)
10938 rettv
->v_type
= VAR_STRING
;
10939 rettv
->vval
.v_string
= alloc(2);
10940 if (rettv
->vval
.v_string
!= NULL
)
10942 rettv
->vval
.v_string
[0] = get_cmdline_type();
10943 rettv
->vval
.v_string
[1] = NUL
;
10948 * "getcwd()" function
10952 f_getcwd(argvars
, rettv
)
10956 char_u cwd
[MAXPATHL
];
10958 rettv
->v_type
= VAR_STRING
;
10959 if (mch_dirname(cwd
, MAXPATHL
) == FAIL
)
10960 rettv
->vval
.v_string
= NULL
;
10963 rettv
->vval
.v_string
= vim_strsave(cwd
);
10964 #ifdef BACKSLASH_IN_FILENAME
10965 if (rettv
->vval
.v_string
!= NULL
)
10966 slash_adjust(rettv
->vval
.v_string
);
10972 * "getfontname()" function
10976 f_getfontname(argvars
, rettv
)
10980 rettv
->v_type
= VAR_STRING
;
10981 rettv
->vval
.v_string
= NULL
;
10986 char_u
*name
= NULL
;
10988 if (argvars
[0].v_type
== VAR_UNKNOWN
)
10990 /* Get the "Normal" font. Either the name saved by
10991 * hl_set_font_name() or from the font ID. */
10992 font
= gui
.norm_font
;
10993 name
= hl_get_font_name();
10997 name
= get_tv_string(&argvars
[0]);
10998 if (STRCMP(name
, "*") == 0) /* don't use font dialog */
11000 font
= gui_mch_get_font(name
, FALSE
);
11001 if (font
== NOFONT
)
11002 return; /* Invalid font name, return empty string. */
11004 rettv
->vval
.v_string
= gui_mch_get_fontname(font
, name
);
11005 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
11006 gui_mch_free_font(font
);
11012 * "getfperm({fname})" function
11015 f_getfperm(argvars
, rettv
)
11021 char_u
*perm
= NULL
;
11022 char_u flags
[] = "rwx";
11025 fname
= get_tv_string(&argvars
[0]);
11027 rettv
->v_type
= VAR_STRING
;
11028 if (mch_stat((char *)fname
, &st
) >= 0)
11030 perm
= vim_strsave((char_u
*)"---------");
11033 for (i
= 0; i
< 9; i
++)
11035 if (st
.st_mode
& (1 << (8 - i
)))
11036 perm
[i
] = flags
[i
% 3];
11040 rettv
->vval
.v_string
= perm
;
11044 * "getfsize({fname})" function
11047 f_getfsize(argvars
, rettv
)
11054 fname
= get_tv_string(&argvars
[0]);
11056 rettv
->v_type
= VAR_NUMBER
;
11058 if (mch_stat((char *)fname
, &st
) >= 0)
11060 if (mch_isdir(fname
))
11061 rettv
->vval
.v_number
= 0;
11064 rettv
->vval
.v_number
= (varnumber_T
)st
.st_size
;
11066 /* non-perfect check for overflow */
11067 if ((off_t
)rettv
->vval
.v_number
!= (off_t
)st
.st_size
)
11068 rettv
->vval
.v_number
= -2;
11072 rettv
->vval
.v_number
= -1;
11076 * "getftime({fname})" function
11079 f_getftime(argvars
, rettv
)
11086 fname
= get_tv_string(&argvars
[0]);
11088 if (mch_stat((char *)fname
, &st
) >= 0)
11089 rettv
->vval
.v_number
= (varnumber_T
)st
.st_mtime
;
11091 rettv
->vval
.v_number
= -1;
11095 * "getftype({fname})" function
11098 f_getftype(argvars
, rettv
)
11104 char_u
*type
= NULL
;
11107 fname
= get_tv_string(&argvars
[0]);
11109 rettv
->v_type
= VAR_STRING
;
11110 if (mch_lstat((char *)fname
, &st
) >= 0)
11113 if (S_ISREG(st
.st_mode
))
11115 else if (S_ISDIR(st
.st_mode
))
11118 else if (S_ISLNK(st
.st_mode
))
11122 else if (S_ISBLK(st
.st_mode
))
11126 else if (S_ISCHR(st
.st_mode
))
11130 else if (S_ISFIFO(st
.st_mode
))
11134 else if (S_ISSOCK(st
.st_mode
))
11141 switch (st
.st_mode
& S_IFMT
)
11143 case S_IFREG
: t
= "file"; break;
11144 case S_IFDIR
: t
= "dir"; break;
11146 case S_IFLNK
: t
= "link"; break;
11149 case S_IFBLK
: t
= "bdev"; break;
11152 case S_IFCHR
: t
= "cdev"; break;
11155 case S_IFIFO
: t
= "fifo"; break;
11158 case S_IFSOCK
: t
= "socket"; break;
11160 default: t
= "other";
11163 if (mch_isdir(fname
))
11169 type
= vim_strsave((char_u
*)t
);
11171 rettv
->vval
.v_string
= type
;
11175 * "getline(lnum, [end])" function
11178 f_getline(argvars
, rettv
)
11186 lnum
= get_tv_lnum(argvars
);
11187 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11194 end
= get_tv_lnum(&argvars
[1]);
11198 get_buffer_lines(curbuf
, lnum
, end
, retlist
, rettv
);
11202 * "getmatches()" function
11206 f_getmatches(argvars
, rettv
)
11210 #ifdef FEAT_SEARCH_EXTRA
11212 matchitem_T
*cur
= curwin
->w_match_head
;
11214 rettv
->vval
.v_number
= 0;
11216 if (rettv_list_alloc(rettv
) == OK
)
11218 while (cur
!= NULL
)
11220 dict
= dict_alloc();
11223 dict_add_nr_str(dict
, "group", 0L, syn_id2name(cur
->hlg_id
));
11224 dict_add_nr_str(dict
, "pattern", 0L, cur
->pattern
);
11225 dict_add_nr_str(dict
, "priority", (long)cur
->priority
, NULL
);
11226 dict_add_nr_str(dict
, "id", (long)cur
->id
, NULL
);
11227 list_append_dict(rettv
->vval
.v_list
, dict
);
11235 * "getpid()" function
11239 f_getpid(argvars
, rettv
)
11243 rettv
->vval
.v_number
= mch_get_pid();
11247 * "getpos(string)" function
11250 f_getpos(argvars
, rettv
)
11258 if (rettv_list_alloc(rettv
) == OK
)
11260 l
= rettv
->vval
.v_list
;
11261 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
11263 list_append_number(l
, (varnumber_T
)fnum
);
11265 list_append_number(l
, (varnumber_T
)0);
11266 list_append_number(l
, (fp
!= NULL
) ? (varnumber_T
)fp
->lnum
11268 list_append_number(l
, (fp
!= NULL
)
11269 ? (varnumber_T
)(fp
->col
== MAXCOL
? MAXCOL
: fp
->col
+ 1)
11271 list_append_number(l
,
11272 #ifdef FEAT_VIRTUALEDIT
11273 (fp
!= NULL
) ? (varnumber_T
)fp
->coladd
:
11278 rettv
->vval
.v_number
= FALSE
;
11282 * "getqflist()" and "getloclist()" functions
11286 f_getqflist(argvars
, rettv
)
11290 #ifdef FEAT_QUICKFIX
11294 rettv
->vval
.v_number
= 0;
11295 #ifdef FEAT_QUICKFIX
11296 if (rettv_list_alloc(rettv
) == OK
)
11299 if (argvars
[0].v_type
!= VAR_UNKNOWN
) /* getloclist() */
11301 wp
= find_win_by_nr(&argvars
[0], NULL
);
11306 (void)get_errorlist(wp
, rettv
->vval
.v_list
);
11312 * "getreg()" function
11315 f_getreg(argvars
, rettv
)
11319 char_u
*strregname
;
11324 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
11326 strregname
= get_tv_string_chk(&argvars
[0]);
11327 error
= strregname
== NULL
;
11328 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
11329 arg2
= get_tv_number_chk(&argvars
[1], &error
);
11332 strregname
= vimvars
[VV_REG
].vv_str
;
11333 regname
= (strregname
== NULL
? '"' : *strregname
);
11337 rettv
->v_type
= VAR_STRING
;
11338 rettv
->vval
.v_string
= error
? NULL
:
11339 get_reg_contents(regname
, TRUE
, arg2
);
11343 * "getregtype()" function
11346 f_getregtype(argvars
, rettv
)
11350 char_u
*strregname
;
11352 char_u buf
[NUMBUFLEN
+ 2];
11355 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
11357 strregname
= get_tv_string_chk(&argvars
[0]);
11358 if (strregname
== NULL
) /* type error; errmsg already given */
11360 rettv
->v_type
= VAR_STRING
;
11361 rettv
->vval
.v_string
= NULL
;
11366 /* Default to v:register */
11367 strregname
= vimvars
[VV_REG
].vv_str
;
11369 regname
= (strregname
== NULL
? '"' : *strregname
);
11375 switch (get_reg_type(regname
, ®len
))
11377 case MLINE
: buf
[0] = 'V'; break;
11378 case MCHAR
: buf
[0] = 'v'; break;
11382 sprintf((char *)buf
+ 1, "%ld", reglen
+ 1);
11386 rettv
->v_type
= VAR_STRING
;
11387 rettv
->vval
.v_string
= vim_strsave(buf
);
11391 * "gettabwinvar()" function
11394 f_gettabwinvar(argvars
, rettv
)
11398 getwinvar(argvars
, rettv
, 1);
11402 * "getwinposx()" function
11406 f_getwinposx(argvars
, rettv
)
11410 rettv
->vval
.v_number
= -1;
11416 if (gui_mch_get_winpos(&x
, &y
) == OK
)
11417 rettv
->vval
.v_number
= x
;
11423 * "getwinposy()" function
11427 f_getwinposy(argvars
, rettv
)
11431 rettv
->vval
.v_number
= -1;
11437 if (gui_mch_get_winpos(&x
, &y
) == OK
)
11438 rettv
->vval
.v_number
= y
;
11444 * Find window specified by "vp" in tabpage "tp".
11447 find_win_by_nr(vp
, tp
)
11449 tabpage_T
*tp
; /* NULL for current tab page */
11451 #ifdef FEAT_WINDOWS
11456 nr
= get_tv_number_chk(vp
, NULL
);
11458 #ifdef FEAT_WINDOWS
11464 for (wp
= (tp
== NULL
|| tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
11465 wp
!= NULL
; wp
= wp
->w_next
)
11470 if (nr
== 0 || nr
== 1)
11477 * "getwinvar()" function
11480 f_getwinvar(argvars
, rettv
)
11484 getwinvar(argvars
, rettv
, 0);
11488 * getwinvar() and gettabwinvar()
11491 getwinvar(argvars
, rettv
, off
)
11494 int off
; /* 1 for gettabwinvar() */
11496 win_T
*win
, *oldcurwin
;
11501 #ifdef FEAT_WINDOWS
11503 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
11507 win
= find_win_by_nr(&argvars
[off
], tp
);
11508 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
11511 rettv
->v_type
= VAR_STRING
;
11512 rettv
->vval
.v_string
= NULL
;
11514 if (win
!= NULL
&& varname
!= NULL
)
11516 /* Set curwin to be our win, temporarily. Also set curbuf, so
11517 * that we can get buffer-local options. */
11518 oldcurwin
= curwin
;
11520 curbuf
= win
->w_buffer
;
11522 if (*varname
== '&') /* window-local-option */
11523 get_option_tv(&varname
, rettv
, 1);
11526 if (*varname
== NUL
)
11527 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11528 * scope prefix before the NUL byte is required by
11529 * find_var_in_ht(). */
11530 varname
= (char_u
*)"w:" + 2;
11531 /* look up the variable */
11532 v
= find_var_in_ht(&win
->w_vars
.dv_hashtab
, varname
, FALSE
);
11534 copy_tv(&v
->di_tv
, rettv
);
11537 /* restore previous notion of curwin */
11538 curwin
= oldcurwin
;
11539 curbuf
= curwin
->w_buffer
;
11546 * "glob()" function
11549 f_glob(argvars
, rettv
)
11553 int flags
= WILD_SILENT
|WILD_USE_NL
;
11557 /* When the optional second argument is non-zero, don't remove matches
11558 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11559 if (argvars
[1].v_type
!= VAR_UNKNOWN
11560 && get_tv_number_chk(&argvars
[1], &error
))
11561 flags
|= WILD_KEEP_ALL
;
11562 rettv
->v_type
= VAR_STRING
;
11566 xpc
.xp_context
= EXPAND_FILES
;
11567 rettv
->vval
.v_string
= ExpandOne(&xpc
, get_tv_string(&argvars
[0]),
11568 NULL
, flags
, WILD_ALL
);
11571 rettv
->vval
.v_string
= NULL
;
11575 * "globpath()" function
11578 f_globpath(argvars
, rettv
)
11583 char_u buf1
[NUMBUFLEN
];
11584 char_u
*file
= get_tv_string_buf_chk(&argvars
[1], buf1
);
11587 /* When the optional second argument is non-zero, don't remove matches
11588 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11589 if (argvars
[2].v_type
!= VAR_UNKNOWN
11590 && get_tv_number_chk(&argvars
[2], &error
))
11591 flags
|= WILD_KEEP_ALL
;
11592 rettv
->v_type
= VAR_STRING
;
11593 if (file
== NULL
|| error
)
11594 rettv
->vval
.v_string
= NULL
;
11596 rettv
->vval
.v_string
= globpath(get_tv_string(&argvars
[0]), file
,
11604 f_has(argvars
, rettv
)
11611 static char *(has_list
[]) =
11632 #if defined(MACOS_X_UNIX)
11656 #if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11665 #ifndef CASE_INSENSITIVE_FILENAME
11671 #ifdef FEAT_AUTOCMD
11676 # ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11677 "balloon_multiline",
11680 #if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11682 # ifdef ALL_BUILTIN_TCAPS
11683 "all_builtin_terms",
11686 #ifdef FEAT_BYTEOFF
11689 #ifdef FEAT_CINDENT
11692 #ifdef FEAT_CLIENTSERVER
11695 #ifdef FEAT_CLIPBOARD
11698 #ifdef FEAT_CMDL_COMPL
11701 #ifdef FEAT_CMDHIST
11704 #ifdef FEAT_COMMENTS
11713 #ifdef CURSOR_SHAPE
11719 #ifdef FEAT_CON_DIALOG
11722 #ifdef FEAT_GUI_DIALOG
11728 #ifdef FEAT_DIGRAPHS
11734 #ifdef FEAT_EMACS_TAGS
11737 "eval", /* always present, of course! */
11738 #ifdef FEAT_EX_EXTRA
11741 #ifdef FEAT_SEARCH_EXTRA
11747 #ifdef FEAT_SEARCHPATH
11750 #if defined(UNIX) && !defined(USE_SYSTEM)
11753 #ifdef FEAT_FIND_ID
11759 #ifdef FEAT_FOLDING
11765 #if !defined(USE_SYSTEM) && defined(UNIX)
11768 #ifdef FEAT_GETTEXT
11774 #ifdef FEAT_GUI_ATHENA
11775 # ifdef FEAT_GUI_NEXTAW
11781 #ifdef FEAT_GUI_GTK
11787 #ifdef FEAT_GUI_GNOME
11790 #ifdef FEAT_GUI_MAC
11793 #ifdef FEAT_GUI_MOTIF
11796 #ifdef FEAT_GUI_PHOTON
11799 #ifdef FEAT_GUI_W16
11802 #ifdef FEAT_GUI_W32
11805 #ifdef FEAT_HANGULIN
11808 #if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11811 #ifdef FEAT_INS_EXPAND
11814 #ifdef FEAT_JUMPLIST
11820 #ifdef FEAT_LANGMAP
11823 #ifdef FEAT_LIBCALL
11826 #ifdef FEAT_LINEBREAK
11832 #ifdef FEAT_LISTCMDS
11835 #ifdef FEAT_LOCALMAP
11841 #ifdef FEAT_SESSION
11844 #ifdef FEAT_MODIFY_FNAME
11850 #ifdef FEAT_MOUSESHAPE
11853 #if defined(UNIX) || defined(VMS)
11854 # ifdef FEAT_MOUSE_DEC
11857 # ifdef FEAT_MOUSE_GPM
11860 # ifdef FEAT_MOUSE_JSB
11863 # ifdef FEAT_MOUSE_NET
11866 # ifdef FEAT_MOUSE_PTERM
11869 # ifdef FEAT_SYSMOUSE
11872 # ifdef FEAT_MOUSE_XTERM
11879 #ifdef FEAT_MBYTE_IME
11882 #ifdef FEAT_MULTI_LANG
11885 #ifdef FEAT_MZSCHEME
11886 #ifndef DYNAMIC_MZSCHEME
11893 #ifdef FEAT_OSFILETYPE
11896 #ifdef FEAT_PATH_EXTRA
11900 #ifndef DYNAMIC_PERL
11905 #ifndef DYNAMIC_PYTHON
11909 #ifdef FEAT_POSTSCRIPT
11912 #ifdef FEAT_PRINTER
11915 #ifdef FEAT_PROFILE
11918 #ifdef FEAT_RELTIME
11921 #ifdef FEAT_QUICKFIX
11924 #ifdef FEAT_RIGHTLEFT
11927 #if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11930 #ifdef FEAT_SCROLLBIND
11933 #ifdef FEAT_CMDL_INFO
11940 #ifdef FEAT_SMARTINDENT
11946 #ifdef FEAT_STL_OPT
11949 #ifdef FEAT_SUN_WORKSHOP
11952 #ifdef FEAT_NETBEANS_INTG
11961 #if defined(USE_SYSTEM) || !defined(UNIX)
11964 #ifdef FEAT_TAG_BINS
11967 #ifdef FEAT_TAG_OLDSTATIC
11970 #ifdef FEAT_TAG_ANYWHITE
11974 # ifndef DYNAMIC_TCL
11981 #ifdef FEAT_TERMRESPONSE
11984 #ifdef FEAT_TEXTOBJ
11987 #ifdef HAVE_TGETENT
11993 #ifdef FEAT_TOOLBAR
11996 #ifdef FEAT_USR_CMDS
11997 "user-commands", /* was accidentally included in 5.4 */
12000 #ifdef FEAT_VIMINFO
12003 #ifdef FEAT_VERTSPLIT
12006 #ifdef FEAT_VIRTUALEDIT
12012 #ifdef FEAT_VISUALEXTRA
12015 #ifdef FEAT_VREPLACE
12018 #ifdef FEAT_WILDIGN
12021 #ifdef FEAT_WILDMENU
12024 #ifdef FEAT_WINDOWS
12030 #ifdef FEAT_WRITEBACKUP
12036 #ifdef FEAT_XFONTSET
12042 #ifdef USE_XSMP_INTERACT
12045 #ifdef FEAT_XCLIPBOARD
12048 #ifdef FEAT_XTERM_SAVE
12051 #if defined(UNIX) && defined(FEAT_X11)
12057 name
= get_tv_string(&argvars
[0]);
12058 for (i
= 0; has_list
[i
] != NULL
; ++i
)
12059 if (STRICMP(name
, has_list
[i
]) == 0)
12067 if (STRNICMP(name
, "patch", 5) == 0)
12068 n
= has_patch(atoi((char *)name
+ 5));
12069 else if (STRICMP(name
, "vim_starting") == 0)
12070 n
= (starting
!= 0);
12072 else if (STRICMP(name
, "multi_byte_encoding") == 0)
12075 #if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12076 else if (STRICMP(name
, "balloon_multiline") == 0)
12077 n
= multiline_balloon_available();
12080 else if (STRICMP(name
, "tcl") == 0)
12081 n
= tcl_enabled(FALSE
);
12083 #if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12084 else if (STRICMP(name
, "iconv") == 0)
12085 n
= iconv_enabled(FALSE
);
12087 #ifdef DYNAMIC_MZSCHEME
12088 else if (STRICMP(name
, "mzscheme") == 0)
12089 n
= mzscheme_enabled(FALSE
);
12091 #ifdef DYNAMIC_RUBY
12092 else if (STRICMP(name
, "ruby") == 0)
12093 n
= ruby_enabled(FALSE
);
12095 #ifdef DYNAMIC_PYTHON
12096 else if (STRICMP(name
, "python") == 0)
12097 n
= python_enabled(FALSE
);
12099 #ifdef DYNAMIC_PERL
12100 else if (STRICMP(name
, "perl") == 0)
12101 n
= perl_enabled(FALSE
);
12104 else if (STRICMP(name
, "gui_running") == 0)
12105 n
= (gui
.in_use
|| gui
.starting
);
12106 # ifdef FEAT_GUI_W32
12107 else if (STRICMP(name
, "gui_win32s") == 0)
12108 n
= gui_is_win32s();
12110 # ifdef FEAT_BROWSE
12111 else if (STRICMP(name
, "browse") == 0)
12112 n
= gui
.in_use
; /* gui_mch_browse() works when GUI is running */
12116 else if (STRICMP(name
, "syntax_items") == 0)
12117 n
= syntax_present(curbuf
);
12119 #if defined(WIN3264)
12120 else if (STRICMP(name
, "win95") == 0)
12121 n
= mch_windows95();
12123 #ifdef FEAT_NETBEANS_INTG
12124 else if (STRICMP(name
, "netbeans_enabled") == 0)
12129 rettv
->vval
.v_number
= n
;
12133 * "has_key()" function
12136 f_has_key(argvars
, rettv
)
12140 rettv
->vval
.v_number
= 0;
12141 if (argvars
[0].v_type
!= VAR_DICT
)
12143 EMSG(_(e_dictreq
));
12146 if (argvars
[0].vval
.v_dict
== NULL
)
12149 rettv
->vval
.v_number
= dict_find(argvars
[0].vval
.v_dict
,
12150 get_tv_string(&argvars
[1]), -1) != NULL
;
12154 * "haslocaldir()" function
12158 f_haslocaldir(argvars
, rettv
)
12162 rettv
->vval
.v_number
= (curwin
->w_localdir
!= NULL
);
12166 * "hasmapto()" function
12169 f_hasmapto(argvars
, rettv
)
12175 char_u buf
[NUMBUFLEN
];
12178 name
= get_tv_string(&argvars
[0]);
12179 if (argvars
[1].v_type
== VAR_UNKNOWN
)
12180 mode
= (char_u
*)"nvo";
12183 mode
= get_tv_string_buf(&argvars
[1], buf
);
12184 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12185 abbr
= get_tv_number(&argvars
[2]);
12188 if (map_to_exists(name
, mode
, abbr
))
12189 rettv
->vval
.v_number
= TRUE
;
12191 rettv
->vval
.v_number
= FALSE
;
12195 * "histadd()" function
12199 f_histadd(argvars
, rettv
)
12203 #ifdef FEAT_CMDHIST
12206 char_u buf
[NUMBUFLEN
];
12209 rettv
->vval
.v_number
= FALSE
;
12210 if (check_restricted() || check_secure())
12212 #ifdef FEAT_CMDHIST
12213 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
12214 histype
= str
!= NULL
? get_histtype(str
) : -1;
12217 str
= get_tv_string_buf(&argvars
[1], buf
);
12220 add_to_history(histype
, str
, FALSE
, NUL
);
12221 rettv
->vval
.v_number
= TRUE
;
12229 * "histdel()" function
12233 f_histdel(argvars
, rettv
)
12237 #ifdef FEAT_CMDHIST
12239 char_u buf
[NUMBUFLEN
];
12242 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
12245 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
12246 /* only one argument: clear entire history */
12247 n
= clr_history(get_histtype(str
));
12248 else if (argvars
[1].v_type
== VAR_NUMBER
)
12249 /* index given: remove that entry */
12250 n
= del_history_idx(get_histtype(str
),
12251 (int)get_tv_number(&argvars
[1]));
12253 /* string given: remove all matching entries */
12254 n
= del_history_entry(get_histtype(str
),
12255 get_tv_string_buf(&argvars
[1], buf
));
12256 rettv
->vval
.v_number
= n
;
12258 rettv
->vval
.v_number
= 0;
12263 * "histget()" function
12267 f_histget(argvars
, rettv
)
12271 #ifdef FEAT_CMDHIST
12276 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
12278 rettv
->vval
.v_string
= NULL
;
12281 type
= get_histtype(str
);
12282 if (argvars
[1].v_type
== VAR_UNKNOWN
)
12283 idx
= get_history_idx(type
);
12285 idx
= (int)get_tv_number_chk(&argvars
[1], NULL
);
12286 /* -1 on type error */
12287 rettv
->vval
.v_string
= vim_strsave(get_history_entry(type
, idx
));
12290 rettv
->vval
.v_string
= NULL
;
12292 rettv
->v_type
= VAR_STRING
;
12296 * "histnr()" function
12300 f_histnr(argvars
, rettv
)
12306 #ifdef FEAT_CMDHIST
12307 char_u
*history
= get_tv_string_chk(&argvars
[0]);
12309 i
= history
== NULL
? HIST_CMD
- 1 : get_histtype(history
);
12310 if (i
>= HIST_CMD
&& i
< HIST_COUNT
)
12311 i
= get_history_idx(i
);
12315 rettv
->vval
.v_number
= i
;
12319 * "highlightID(name)" function
12322 f_hlID(argvars
, rettv
)
12326 rettv
->vval
.v_number
= syn_name2id(get_tv_string(&argvars
[0]));
12330 * "highlight_exists()" function
12333 f_hlexists(argvars
, rettv
)
12337 rettv
->vval
.v_number
= highlight_exists(get_tv_string(&argvars
[0]));
12341 * "hostname()" function
12345 f_hostname(argvars
, rettv
)
12349 char_u hostname
[256];
12351 mch_get_host_name(hostname
, 256);
12352 rettv
->v_type
= VAR_STRING
;
12353 rettv
->vval
.v_string
= vim_strsave(hostname
);
12361 f_iconv(argvars
, rettv
)
12366 char_u buf1
[NUMBUFLEN
];
12367 char_u buf2
[NUMBUFLEN
];
12368 char_u
*from
, *to
, *str
;
12372 rettv
->v_type
= VAR_STRING
;
12373 rettv
->vval
.v_string
= NULL
;
12376 str
= get_tv_string(&argvars
[0]);
12377 from
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[1], buf1
)));
12378 to
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[2], buf2
)));
12379 vimconv
.vc_type
= CONV_NONE
;
12380 convert_setup(&vimconv
, from
, to
);
12382 /* If the encodings are equal, no conversion needed. */
12383 if (vimconv
.vc_type
== CONV_NONE
)
12384 rettv
->vval
.v_string
= vim_strsave(str
);
12386 rettv
->vval
.v_string
= string_convert(&vimconv
, str
, NULL
);
12388 convert_setup(&vimconv
, NULL
, NULL
);
12395 * "indent()" function
12398 f_indent(argvars
, rettv
)
12404 lnum
= get_tv_lnum(argvars
);
12405 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
12406 rettv
->vval
.v_number
= get_indent_lnum(lnum
);
12408 rettv
->vval
.v_number
= -1;
12412 * "index()" function
12415 f_index(argvars
, rettv
)
12424 rettv
->vval
.v_number
= -1;
12425 if (argvars
[0].v_type
!= VAR_LIST
)
12427 EMSG(_(e_listreq
));
12430 l
= argvars
[0].vval
.v_list
;
12433 item
= l
->lv_first
;
12434 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12438 /* Start at specified item. Use the cached index that list_find()
12439 * sets, so that a negative number also works. */
12440 item
= list_find(l
, get_tv_number_chk(&argvars
[2], &error
));
12442 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
12443 ic
= get_tv_number_chk(&argvars
[3], &error
);
12448 for ( ; item
!= NULL
; item
= item
->li_next
, ++idx
)
12449 if (tv_equal(&item
->li_tv
, &argvars
[1], ic
))
12451 rettv
->vval
.v_number
= idx
;
12457 static int inputsecret_flag
= 0;
12459 static void get_user_input
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int inputdialog
));
12462 * This function is used by f_input() and f_inputdialog() functions. The third
12463 * argument to f_input() specifies the type of completion to use at the
12464 * prompt. The third argument to f_inputdialog() specifies the value to return
12465 * when the user cancels the prompt.
12468 get_user_input(argvars
, rettv
, inputdialog
)
12473 char_u
*prompt
= get_tv_string_chk(&argvars
[0]);
12476 char_u buf
[NUMBUFLEN
];
12477 int cmd_silent_save
= cmd_silent
;
12478 char_u
*defstr
= (char_u
*)"";
12479 int xp_type
= EXPAND_NOTHING
;
12480 char_u
*xp_arg
= NULL
;
12482 rettv
->v_type
= VAR_STRING
;
12483 rettv
->vval
.v_string
= NULL
;
12485 #ifdef NO_CONSOLE_INPUT
12486 /* While starting up, there is no place to enter text. */
12487 if (no_console_input())
12491 cmd_silent
= FALSE
; /* Want to see the prompt. */
12492 if (prompt
!= NULL
)
12494 /* Only the part of the message after the last NL is considered as
12495 * prompt for the command line */
12496 p
= vim_strrchr(prompt
, '\n');
12506 msg_puts_attr(prompt
, echo_attr
);
12507 msg_didout
= FALSE
;
12511 cmdline_row
= msg_row
;
12513 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12515 defstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
12516 if (defstr
!= NULL
)
12517 stuffReadbuffSpec(defstr
);
12519 if (!inputdialog
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
12525 rettv
->vval
.v_string
= NULL
;
12527 xp_name
= get_tv_string_buf_chk(&argvars
[2], buf
);
12528 if (xp_name
== NULL
)
12531 xp_namelen
= (int)STRLEN(xp_name
);
12533 if (parse_compl_arg(xp_name
, xp_namelen
, &xp_type
, &argt
,
12539 if (defstr
!= NULL
)
12540 rettv
->vval
.v_string
=
12541 getcmdline_prompt(inputsecret_flag
? NUL
: '@', p
, echo_attr
,
12546 /* since the user typed this, no need to wait for return */
12547 need_wait_return
= FALSE
;
12548 msg_didout
= FALSE
;
12550 cmd_silent
= cmd_silent_save
;
12554 * "input()" function
12555 * Also handles inputsecret() when inputsecret is set.
12558 f_input(argvars
, rettv
)
12562 get_user_input(argvars
, rettv
, FALSE
);
12566 * "inputdialog()" function
12569 f_inputdialog(argvars
, rettv
)
12573 #if defined(FEAT_GUI_TEXTDIALOG)
12574 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12575 if (gui
.in_use
&& vim_strchr(p_go
, GO_CONDIALOG
) == NULL
)
12578 char_u buf
[NUMBUFLEN
];
12579 char_u
*defstr
= (char_u
*)"";
12581 message
= get_tv_string_chk(&argvars
[0]);
12582 if (argvars
[1].v_type
!= VAR_UNKNOWN
12583 && (defstr
= get_tv_string_buf_chk(&argvars
[1], buf
)) != NULL
)
12584 vim_strncpy(IObuff
, defstr
, IOSIZE
- 1);
12587 if (message
!= NULL
&& defstr
!= NULL
12588 && do_dialog(VIM_QUESTION
, NULL
, message
,
12589 (char_u
*)_("&OK\n&Cancel"), 1, IObuff
) == 1)
12590 rettv
->vval
.v_string
= vim_strsave(IObuff
);
12593 if (message
!= NULL
&& defstr
!= NULL
12594 && argvars
[1].v_type
!= VAR_UNKNOWN
12595 && argvars
[2].v_type
!= VAR_UNKNOWN
)
12596 rettv
->vval
.v_string
= vim_strsave(
12597 get_tv_string_buf(&argvars
[2], buf
));
12599 rettv
->vval
.v_string
= NULL
;
12601 rettv
->v_type
= VAR_STRING
;
12605 get_user_input(argvars
, rettv
, TRUE
);
12609 * "inputlist()" function
12612 f_inputlist(argvars
, rettv
)
12620 rettv
->vval
.v_number
= 0;
12621 #ifdef NO_CONSOLE_INPUT
12622 /* While starting up, there is no place to enter text. */
12623 if (no_console_input())
12626 if (argvars
[0].v_type
!= VAR_LIST
|| argvars
[0].vval
.v_list
== NULL
)
12628 EMSG2(_(e_listarg
), "inputlist()");
12633 msg_row
= Rows
- 1; /* for when 'cmdheight' > 1 */
12634 lines_left
= Rows
; /* avoid more prompt */
12638 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
; li
= li
->li_next
)
12640 msg_puts(get_tv_string(&li
->li_tv
));
12644 /* Ask for choice. */
12645 selected
= prompt_for_number(&mouse_used
);
12647 selected
-= lines_left
;
12649 rettv
->vval
.v_number
= selected
;
12653 static garray_T ga_userinput
= {0, 0, sizeof(tasave_T
), 4, NULL
};
12656 * "inputrestore()" function
12660 f_inputrestore(argvars
, rettv
)
12664 if (ga_userinput
.ga_len
> 0)
12666 --ga_userinput
.ga_len
;
12667 restore_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
12668 + ga_userinput
.ga_len
);
12669 rettv
->vval
.v_number
= 0; /* OK */
12671 else if (p_verbose
> 1)
12673 verb_msg((char_u
*)_("called inputrestore() more often than inputsave()"));
12674 rettv
->vval
.v_number
= 1; /* Failed */
12679 * "inputsave()" function
12683 f_inputsave(argvars
, rettv
)
12687 /* Add an entry to the stack of typeahead storage. */
12688 if (ga_grow(&ga_userinput
, 1) == OK
)
12690 save_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
12691 + ga_userinput
.ga_len
);
12692 ++ga_userinput
.ga_len
;
12693 rettv
->vval
.v_number
= 0; /* OK */
12696 rettv
->vval
.v_number
= 1; /* Failed */
12700 * "inputsecret()" function
12703 f_inputsecret(argvars
, rettv
)
12708 ++inputsecret_flag
;
12709 f_input(argvars
, rettv
);
12711 --inputsecret_flag
;
12715 * "insert()" function
12718 f_insert(argvars
, rettv
)
12727 rettv
->vval
.v_number
= 0;
12728 if (argvars
[0].v_type
!= VAR_LIST
)
12729 EMSG2(_(e_listarg
), "insert()");
12730 else if ((l
= argvars
[0].vval
.v_list
) != NULL
12731 && !tv_check_lock(l
->lv_lock
, (char_u
*)"insert()"))
12733 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12734 before
= get_tv_number_chk(&argvars
[2], &error
);
12736 return; /* type error; errmsg already given */
12738 if (before
== l
->lv_len
)
12742 item
= list_find(l
, before
);
12745 EMSGN(_(e_listidx
), before
);
12751 list_insert_tv(l
, &argvars
[1], item
);
12752 copy_tv(&argvars
[0], rettv
);
12758 * "isdirectory()" function
12761 f_isdirectory(argvars
, rettv
)
12765 rettv
->vval
.v_number
= mch_isdir(get_tv_string(&argvars
[0]));
12769 * "islocked()" function
12772 f_islocked(argvars
, rettv
)
12780 rettv
->vval
.v_number
= -1;
12781 end
= get_lval(get_tv_string(&argvars
[0]), NULL
, &lv
, FALSE
, FALSE
, FALSE
,
12783 if (end
!= NULL
&& lv
.ll_name
!= NULL
)
12786 EMSG(_(e_trailing
));
12789 if (lv
.ll_tv
== NULL
)
12791 if (check_changedtick(lv
.ll_name
))
12792 rettv
->vval
.v_number
= 1; /* always locked */
12795 di
= find_var(lv
.ll_name
, NULL
);
12798 /* Consider a variable locked when:
12799 * 1. the variable itself is locked
12800 * 2. the value of the variable is locked.
12801 * 3. the List or Dict value is locked.
12803 rettv
->vval
.v_number
= ((di
->di_flags
& DI_FLAGS_LOCK
)
12804 || tv_islocked(&di
->di_tv
));
12808 else if (lv
.ll_range
)
12809 EMSG(_("E786: Range not allowed"));
12810 else if (lv
.ll_newkey
!= NULL
)
12811 EMSG2(_(e_dictkey
), lv
.ll_newkey
);
12812 else if (lv
.ll_list
!= NULL
)
12814 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_li
->li_tv
);
12816 /* Dictionary item. */
12817 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_di
->di_tv
);
12824 static void dict_list
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int what
));
12827 * Turn a dict into a list:
12828 * "what" == 0: list of keys
12829 * "what" == 1: list of values
12830 * "what" == 2: list of items
12833 dict_list(argvars
, rettv
, what
)
12846 rettv
->vval
.v_number
= 0;
12847 if (argvars
[0].v_type
!= VAR_DICT
)
12849 EMSG(_(e_dictreq
));
12852 if ((d
= argvars
[0].vval
.v_dict
) == NULL
)
12855 if (rettv_list_alloc(rettv
) == FAIL
)
12858 todo
= (int)d
->dv_hashtab
.ht_used
;
12859 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
12861 if (!HASHITEM_EMPTY(hi
))
12866 li
= listitem_alloc();
12869 list_append(rettv
->vval
.v_list
, li
);
12874 li
->li_tv
.v_type
= VAR_STRING
;
12875 li
->li_tv
.v_lock
= 0;
12876 li
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
12878 else if (what
== 1)
12881 copy_tv(&di
->di_tv
, &li
->li_tv
);
12887 li
->li_tv
.v_type
= VAR_LIST
;
12888 li
->li_tv
.v_lock
= 0;
12889 li
->li_tv
.vval
.v_list
= l2
;
12894 li2
= listitem_alloc();
12897 list_append(l2
, li2
);
12898 li2
->li_tv
.v_type
= VAR_STRING
;
12899 li2
->li_tv
.v_lock
= 0;
12900 li2
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
12902 li2
= listitem_alloc();
12905 list_append(l2
, li2
);
12906 copy_tv(&di
->di_tv
, &li2
->li_tv
);
12913 * "items(dict)" function
12916 f_items(argvars
, rettv
)
12920 dict_list(argvars
, rettv
, 2);
12924 * "join()" function
12927 f_join(argvars
, rettv
)
12934 rettv
->vval
.v_number
= 0;
12935 if (argvars
[0].v_type
!= VAR_LIST
)
12937 EMSG(_(e_listreq
));
12940 if (argvars
[0].vval
.v_list
== NULL
)
12942 if (argvars
[1].v_type
== VAR_UNKNOWN
)
12943 sep
= (char_u
*)" ";
12945 sep
= get_tv_string_chk(&argvars
[1]);
12947 rettv
->v_type
= VAR_STRING
;
12951 ga_init2(&ga
, (int)sizeof(char), 80);
12952 list_join(&ga
, argvars
[0].vval
.v_list
, sep
, TRUE
, 0);
12953 ga_append(&ga
, NUL
);
12954 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
12957 rettv
->vval
.v_string
= NULL
;
12961 * "keys()" function
12964 f_keys(argvars
, rettv
)
12968 dict_list(argvars
, rettv
, 0);
12972 * "last_buffer_nr()" function.
12976 f_last_buffer_nr(argvars
, rettv
)
12983 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
12984 if (n
< buf
->b_fnum
)
12987 rettv
->vval
.v_number
= n
;
12994 f_len(argvars
, rettv
)
12998 switch (argvars
[0].v_type
)
13002 rettv
->vval
.v_number
= (varnumber_T
)STRLEN(
13003 get_tv_string(&argvars
[0]));
13006 rettv
->vval
.v_number
= list_len(argvars
[0].vval
.v_list
);
13009 rettv
->vval
.v_number
= dict_len(argvars
[0].vval
.v_dict
);
13012 EMSG(_("E701: Invalid type for len()"));
13017 static void libcall_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int type
));
13020 libcall_common(argvars
, rettv
, type
)
13025 #ifdef FEAT_LIBCALL
13027 char_u
**string_result
;
13031 rettv
->v_type
= type
;
13032 if (type
== VAR_NUMBER
)
13033 rettv
->vval
.v_number
= 0;
13035 rettv
->vval
.v_string
= NULL
;
13037 if (check_restricted() || check_secure())
13040 #ifdef FEAT_LIBCALL
13041 /* The first two args must be strings, otherwise its meaningless */
13042 if (argvars
[0].v_type
== VAR_STRING
&& argvars
[1].v_type
== VAR_STRING
)
13045 if (argvars
[2].v_type
== VAR_STRING
)
13046 string_in
= argvars
[2].vval
.v_string
;
13047 if (type
== VAR_NUMBER
)
13048 string_result
= NULL
;
13050 string_result
= &rettv
->vval
.v_string
;
13051 if (mch_libcall(argvars
[0].vval
.v_string
,
13052 argvars
[1].vval
.v_string
,
13054 argvars
[2].vval
.v_number
,
13057 && type
== VAR_NUMBER
)
13058 rettv
->vval
.v_number
= nr_result
;
13064 * "libcall()" function
13067 f_libcall(argvars
, rettv
)
13071 libcall_common(argvars
, rettv
, VAR_STRING
);
13075 * "libcallnr()" function
13078 f_libcallnr(argvars
, rettv
)
13082 libcall_common(argvars
, rettv
, VAR_NUMBER
);
13086 * "line(string)" function
13089 f_line(argvars
, rettv
)
13097 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
13100 rettv
->vval
.v_number
= lnum
;
13104 * "line2byte(lnum)" function
13108 f_line2byte(argvars
, rettv
)
13112 #ifndef FEAT_BYTEOFF
13113 rettv
->vval
.v_number
= -1;
13117 lnum
= get_tv_lnum(argvars
);
13118 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
13119 rettv
->vval
.v_number
= -1;
13121 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
, lnum
, NULL
);
13122 if (rettv
->vval
.v_number
>= 0)
13123 ++rettv
->vval
.v_number
;
13128 * "lispindent(lnum)" function
13131 f_lispindent(argvars
, rettv
)
13139 pos
= curwin
->w_cursor
;
13140 lnum
= get_tv_lnum(argvars
);
13141 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
13143 curwin
->w_cursor
.lnum
= lnum
;
13144 rettv
->vval
.v_number
= get_lisp_indent();
13145 curwin
->w_cursor
= pos
;
13149 rettv
->vval
.v_number
= -1;
13153 * "localtime()" function
13157 f_localtime(argvars
, rettv
)
13161 rettv
->vval
.v_number
= (varnumber_T
)time(NULL
);
13164 static void get_maparg
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int exact
));
13167 get_maparg(argvars
, rettv
, exact
)
13174 char_u buf
[NUMBUFLEN
];
13175 char_u
*keys_buf
= NULL
;
13181 /* return empty string for failure */
13182 rettv
->v_type
= VAR_STRING
;
13183 rettv
->vval
.v_string
= NULL
;
13185 keys
= get_tv_string(&argvars
[0]);
13189 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
13191 which
= get_tv_string_buf_chk(&argvars
[1], buf
);
13192 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13193 abbr
= get_tv_number(&argvars
[2]);
13196 which
= (char_u
*)"";
13200 mode
= get_map_mode(&which
, 0);
13202 keys
= replace_termcodes(keys
, &keys_buf
, TRUE
, TRUE
, FALSE
);
13203 rhs
= check_map(keys
, mode
, exact
, FALSE
, abbr
);
13204 vim_free(keys_buf
);
13208 ga
.ga_itemsize
= 1;
13209 ga
.ga_growsize
= 40;
13211 while (*rhs
!= NUL
)
13212 ga_concat(&ga
, str2special(&rhs
, FALSE
));
13214 ga_append(&ga
, NUL
);
13215 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
13221 * "log10()" function
13224 f_log10(argvars
, rettv
)
13230 rettv
->v_type
= VAR_FLOAT
;
13231 if (get_float_arg(argvars
, &f
) == OK
)
13232 rettv
->vval
.v_float
= log10(f
);
13234 rettv
->vval
.v_float
= 0.0;
13242 f_map(argvars
, rettv
)
13246 filter_map(argvars
, rettv
, TRUE
);
13250 * "maparg()" function
13253 f_maparg(argvars
, rettv
)
13257 get_maparg(argvars
, rettv
, TRUE
);
13261 * "mapcheck()" function
13264 f_mapcheck(argvars
, rettv
)
13268 get_maparg(argvars
, rettv
, FALSE
);
13271 static void find_some_match
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int start
));
13274 find_some_match(argvars
, rettv
, type
)
13279 char_u
*str
= NULL
;
13280 char_u
*expr
= NULL
;
13282 regmatch_T regmatch
;
13283 char_u patbuf
[NUMBUFLEN
];
13284 char_u strbuf
[NUMBUFLEN
];
13288 colnr_T startcol
= 0;
13291 listitem_T
*li
= NULL
;
13293 char_u
*tofree
= NULL
;
13295 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13297 p_cpo
= (char_u
*)"";
13299 rettv
->vval
.v_number
= -1;
13302 /* return empty list when there are no matches */
13303 if (rettv_list_alloc(rettv
) == FAIL
)
13306 else if (type
== 2)
13308 rettv
->v_type
= VAR_STRING
;
13309 rettv
->vval
.v_string
= NULL
;
13312 if (argvars
[0].v_type
== VAR_LIST
)
13314 if ((l
= argvars
[0].vval
.v_list
) == NULL
)
13319 expr
= str
= get_tv_string(&argvars
[0]);
13321 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
13325 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13329 start
= get_tv_number_chk(&argvars
[2], &error
);
13334 li
= list_find(l
, start
);
13337 idx
= l
->lv_idx
; /* use the cached index */
13343 if (start
> (long)STRLEN(str
))
13345 /* When "count" argument is there ignore matches before "start",
13346 * otherwise skip part of the string. Differs when pattern is "^"
13348 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13354 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13355 nth
= get_tv_number_chk(&argvars
[3], &error
);
13360 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
13361 if (regmatch
.regprog
!= NULL
)
13363 regmatch
.rm_ic
= p_ic
;
13375 str
= echo_string(&li
->li_tv
, &tofree
, strbuf
, 0);
13380 match
= vim_regexec_nl(®match
, str
, (colnr_T
)startcol
);
13382 if (match
&& --nth
<= 0)
13384 if (l
== NULL
&& !match
)
13387 /* Advance to just after the match. */
13396 startcol
= (colnr_T
)(regmatch
.startp
[0]
13397 + (*mb_ptr2len
)(regmatch
.startp
[0]) - str
);
13399 startcol
= regmatch
.startp
[0] + 1 - str
;
13410 /* return list with matched string and submatches */
13411 for (i
= 0; i
< NSUBEXP
; ++i
)
13413 if (regmatch
.endp
[i
] == NULL
)
13415 if (list_append_string(rettv
->vval
.v_list
,
13416 (char_u
*)"", 0) == FAIL
)
13419 else if (list_append_string(rettv
->vval
.v_list
,
13420 regmatch
.startp
[i
],
13421 (int)(regmatch
.endp
[i
] - regmatch
.startp
[i
]))
13426 else if (type
== 2)
13428 /* return matched string */
13430 copy_tv(&li
->li_tv
, rettv
);
13432 rettv
->vval
.v_string
= vim_strnsave(regmatch
.startp
[0],
13433 (int)(regmatch
.endp
[0] - regmatch
.startp
[0]));
13435 else if (l
!= NULL
)
13436 rettv
->vval
.v_number
= idx
;
13440 rettv
->vval
.v_number
=
13441 (varnumber_T
)(regmatch
.startp
[0] - str
);
13443 rettv
->vval
.v_number
=
13444 (varnumber_T
)(regmatch
.endp
[0] - str
);
13445 rettv
->vval
.v_number
+= (varnumber_T
)(str
- expr
);
13448 vim_free(regmatch
.regprog
);
13457 * "match()" function
13460 f_match(argvars
, rettv
)
13464 find_some_match(argvars
, rettv
, 1);
13468 * "matchadd()" function
13471 f_matchadd(argvars
, rettv
)
13475 #ifdef FEAT_SEARCH_EXTRA
13476 char_u buf
[NUMBUFLEN
];
13477 char_u
*grp
= get_tv_string_buf_chk(&argvars
[0], buf
); /* group */
13478 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], buf
); /* pattern */
13479 int prio
= 10; /* default priority */
13483 rettv
->vval
.v_number
= -1;
13485 if (grp
== NULL
|| pat
== NULL
)
13487 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13489 prio
= get_tv_number_chk(&argvars
[2], &error
);
13490 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13491 id
= get_tv_number_chk(&argvars
[3], &error
);
13495 if (id
>= 1 && id
<= 3)
13497 EMSGN("E798: ID is reserved for \":match\": %ld", id
);
13501 rettv
->vval
.v_number
= match_add(curwin
, grp
, pat
, prio
, id
);
13506 * "matcharg()" function
13509 f_matcharg(argvars
, rettv
)
13513 if (rettv_list_alloc(rettv
) == OK
)
13515 #ifdef FEAT_SEARCH_EXTRA
13516 int id
= get_tv_number(&argvars
[0]);
13519 if (id
>= 1 && id
<= 3)
13521 if ((m
= (matchitem_T
*)get_match(curwin
, id
)) != NULL
)
13523 list_append_string(rettv
->vval
.v_list
,
13524 syn_id2name(m
->hlg_id
), -1);
13525 list_append_string(rettv
->vval
.v_list
, m
->pattern
, -1);
13529 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
13530 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
13538 * "matchdelete()" function
13541 f_matchdelete(argvars
, rettv
)
13545 #ifdef FEAT_SEARCH_EXTRA
13546 rettv
->vval
.v_number
= match_delete(curwin
,
13547 (int)get_tv_number(&argvars
[0]), TRUE
);
13552 * "matchend()" function
13555 f_matchend(argvars
, rettv
)
13559 find_some_match(argvars
, rettv
, 0);
13563 * "matchlist()" function
13566 f_matchlist(argvars
, rettv
)
13570 find_some_match(argvars
, rettv
, 3);
13574 * "matchstr()" function
13577 f_matchstr(argvars
, rettv
)
13581 find_some_match(argvars
, rettv
, 2);
13584 static void max_min
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int domax
));
13587 max_min(argvars
, rettv
, domax
)
13596 if (argvars
[0].v_type
== VAR_LIST
)
13601 l
= argvars
[0].vval
.v_list
;
13607 n
= get_tv_number_chk(&li
->li_tv
, &error
);
13613 i
= get_tv_number_chk(&li
->li_tv
, &error
);
13614 if (domax
? i
> n
: i
< n
)
13620 else if (argvars
[0].v_type
== VAR_DICT
)
13627 d
= argvars
[0].vval
.v_dict
;
13630 todo
= (int)d
->dv_hashtab
.ht_used
;
13631 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
13633 if (!HASHITEM_EMPTY(hi
))
13636 i
= get_tv_number_chk(&HI2DI(hi
)->di_tv
, &error
);
13642 else if (domax
? i
> n
: i
< n
)
13649 EMSG(_(e_listdictarg
));
13650 rettv
->vval
.v_number
= error
? 0 : n
;
13657 f_max(argvars
, rettv
)
13661 max_min(argvars
, rettv
, TRUE
);
13668 f_min(argvars
, rettv
)
13672 max_min(argvars
, rettv
, FALSE
);
13675 static int mkdir_recurse
__ARGS((char_u
*dir
, int prot
));
13678 * Create the directory in which "dir" is located, and higher levels when
13682 mkdir_recurse(dir
, prot
)
13690 /* Get end of directory name in "dir".
13691 * We're done when it's "/" or "c:/". */
13692 p
= gettail_sep(dir
);
13693 if (p
<= get_past_head(dir
))
13696 /* If the directory exists we're done. Otherwise: create it.*/
13697 updir
= vim_strnsave(dir
, (int)(p
- dir
));
13700 if (mch_isdir(updir
))
13702 else if (mkdir_recurse(updir
, prot
) == OK
)
13703 r
= vim_mkdir_emsg(updir
, prot
);
13710 * "mkdir()" function
13713 f_mkdir(argvars
, rettv
)
13718 char_u buf
[NUMBUFLEN
];
13721 rettv
->vval
.v_number
= FAIL
;
13722 if (check_restricted() || check_secure())
13725 dir
= get_tv_string_buf(&argvars
[0], buf
);
13726 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
13728 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13729 prot
= get_tv_number_chk(&argvars
[2], NULL
);
13730 if (prot
!= -1 && STRCMP(get_tv_string(&argvars
[1]), "p") == 0)
13731 mkdir_recurse(dir
, prot
);
13733 rettv
->vval
.v_number
= prot
!= -1 ? vim_mkdir_emsg(dir
, prot
) : 0;
13738 * "mode()" function
13742 f_mode(argvars
, rettv
)
13755 buf
[0] = VIsual_mode
+ 's' - 'v';
13757 buf
[0] = VIsual_mode
;
13761 if (State
== HITRETURN
|| State
== ASKMORE
|| State
== SETWSIZE
13762 || State
== CONFIRM
)
13765 if (State
== ASKMORE
)
13767 else if (State
== CONFIRM
)
13770 else if (State
== EXTERNCMD
)
13772 else if (State
& INSERT
)
13774 #ifdef FEAT_VREPLACE
13775 if (State
& VREPLACE_FLAG
)
13782 if (State
& REPLACE_FLAG
)
13787 else if (State
& CMDLINE
)
13793 else if (exmode_active
)
13805 /* Clear out the minor mode when the argument is not a non-zero number or
13806 * non-empty string. */
13807 if (!non_zero_arg(&argvars
[0]))
13810 rettv
->vval
.v_string
= vim_strsave(buf
);
13811 rettv
->v_type
= VAR_STRING
;
13815 * "nextnonblank()" function
13818 f_nextnonblank(argvars
, rettv
)
13824 for (lnum
= get_tv_lnum(argvars
); ; ++lnum
)
13826 if (lnum
< 0 || lnum
> curbuf
->b_ml
.ml_line_count
)
13831 if (*skipwhite(ml_get(lnum
)) != NUL
)
13834 rettv
->vval
.v_number
= lnum
;
13838 * "nr2char()" function
13841 f_nr2char(argvars
, rettv
)
13845 char_u buf
[NUMBUFLEN
];
13849 buf
[(*mb_char2bytes
)((int)get_tv_number(&argvars
[0]), buf
)] = NUL
;
13853 buf
[0] = (char_u
)get_tv_number(&argvars
[0]);
13856 rettv
->v_type
= VAR_STRING
;
13857 rettv
->vval
.v_string
= vim_strsave(buf
);
13861 * "pathshorten()" function
13864 f_pathshorten(argvars
, rettv
)
13870 rettv
->v_type
= VAR_STRING
;
13871 p
= get_tv_string_chk(&argvars
[0]);
13873 rettv
->vval
.v_string
= NULL
;
13876 p
= vim_strsave(p
);
13877 rettv
->vval
.v_string
= p
;
13888 f_pow(argvars
, rettv
)
13894 rettv
->v_type
= VAR_FLOAT
;
13895 if (get_float_arg(argvars
, &fx
) == OK
13896 && get_float_arg(&argvars
[1], &fy
) == OK
)
13897 rettv
->vval
.v_float
= pow(fx
, fy
);
13899 rettv
->vval
.v_float
= 0.0;
13904 * "prevnonblank()" function
13907 f_prevnonblank(argvars
, rettv
)
13913 lnum
= get_tv_lnum(argvars
);
13914 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
)
13917 while (lnum
>= 1 && *skipwhite(ml_get(lnum
)) == NUL
)
13919 rettv
->vval
.v_number
= lnum
;
13922 #ifdef HAVE_STDARG_H
13923 /* This dummy va_list is here because:
13924 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13925 * - locally in the function results in a "used before set" warning
13926 * - using va_start() to initialize it gives "function with fixed args" error */
13931 * "printf()" function
13934 f_printf(argvars
, rettv
)
13938 rettv
->v_type
= VAR_STRING
;
13939 rettv
->vval
.v_string
= NULL
;
13940 #ifdef HAVE_STDARG_H /* only very old compilers can't do this */
13942 char_u buf
[NUMBUFLEN
];
13945 int saved_did_emsg
= did_emsg
;
13948 /* Get the required length, allocate the buffer and do it for real. */
13950 fmt
= (char *)get_tv_string_buf(&argvars
[0], buf
);
13951 len
= vim_vsnprintf(NULL
, 0, fmt
, ap
, argvars
+ 1);
13954 s
= alloc(len
+ 1);
13957 rettv
->vval
.v_string
= s
;
13958 (void)vim_vsnprintf((char *)s
, len
+ 1, fmt
, ap
, argvars
+ 1);
13961 did_emsg
|= saved_did_emsg
;
13967 * "pumvisible()" function
13971 f_pumvisible(argvars
, rettv
)
13975 rettv
->vval
.v_number
= 0;
13976 #ifdef FEAT_INS_EXPAND
13978 rettv
->vval
.v_number
= 1;
13983 * "range()" function
13986 f_range(argvars
, rettv
)
13996 start
= get_tv_number_chk(&argvars
[0], &error
);
13997 if (argvars
[1].v_type
== VAR_UNKNOWN
)
14004 end
= get_tv_number_chk(&argvars
[1], &error
);
14005 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14006 stride
= get_tv_number_chk(&argvars
[2], &error
);
14009 rettv
->vval
.v_number
= 0;
14011 return; /* type error; errmsg already given */
14013 EMSG(_("E726: Stride is zero"));
14014 else if (stride
> 0 ? end
+ 1 < start
: end
- 1 > start
)
14015 EMSG(_("E727: Start past end"));
14018 if (rettv_list_alloc(rettv
) == OK
)
14019 for (i
= start
; stride
> 0 ? i
<= end
: i
>= end
; i
+= stride
)
14020 if (list_append_number(rettv
->vval
.v_list
,
14021 (varnumber_T
)i
) == FAIL
)
14027 * "readfile()" function
14030 f_readfile(argvars
, rettv
)
14034 int binary
= FALSE
;
14038 #define FREAD_SIZE 200 /* optimized for text lines */
14039 char_u buf
[FREAD_SIZE
];
14040 int readlen
; /* size of last fread() */
14041 int buflen
; /* nr of valid chars in buf[] */
14042 int filtd
; /* how much in buf[] was NUL -> '\n' filtered */
14043 int tolist
; /* first byte in buf[] still to be put in list */
14044 int chop
; /* how many CR to chop off */
14045 char_u
*prev
= NULL
; /* previously read bytes, if any */
14046 int prevlen
= 0; /* length of "prev" if not NULL */
14049 long maxline
= MAXLNUM
;
14052 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
14054 if (STRCMP(get_tv_string(&argvars
[1]), "b") == 0)
14056 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14057 maxline
= get_tv_number(&argvars
[2]);
14060 if (rettv_list_alloc(rettv
) == FAIL
)
14063 /* Always open the file in binary mode, library functions have a mind of
14064 * their own about CR-LF conversion. */
14065 fname
= get_tv_string(&argvars
[0]);
14066 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, READBIN
)) == NULL
)
14068 EMSG2(_(e_notopen
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
14073 while (cnt
< maxline
|| maxline
< 0)
14075 readlen
= (int)fread(buf
+ filtd
, 1, FREAD_SIZE
- filtd
, fd
);
14076 buflen
= filtd
+ readlen
;
14078 for ( ; filtd
< buflen
|| readlen
<= 0; ++filtd
)
14080 if (buf
[filtd
] == '\n' || readlen
<= 0)
14082 /* Only when in binary mode add an empty list item when the
14083 * last line ends in a '\n'. */
14084 if (!binary
&& readlen
== 0 && filtd
== 0)
14087 /* Found end-of-line or end-of-file: add a text line to the
14091 while (filtd
- chop
- 1 >= tolist
14092 && buf
[filtd
- chop
- 1] == '\r')
14094 len
= filtd
- tolist
- chop
;
14096 s
= vim_strnsave(buf
+ tolist
, len
);
14099 s
= alloc((unsigned)(prevlen
+ len
+ 1));
14102 mch_memmove(s
, prev
, prevlen
);
14105 mch_memmove(s
+ prevlen
, buf
+ tolist
, len
);
14106 s
[prevlen
+ len
] = NUL
;
14109 tolist
= filtd
+ 1;
14111 li
= listitem_alloc();
14117 li
->li_tv
.v_type
= VAR_STRING
;
14118 li
->li_tv
.v_lock
= 0;
14119 li
->li_tv
.vval
.v_string
= s
;
14120 list_append(rettv
->vval
.v_list
, li
);
14122 if (++cnt
>= maxline
&& maxline
>= 0)
14127 else if (buf
[filtd
] == NUL
)
14135 /* "buf" is full, need to move text to an allocated buffer */
14138 prev
= vim_strnsave(buf
, buflen
);
14143 s
= alloc((unsigned)(prevlen
+ buflen
));
14146 mch_memmove(s
, prev
, prevlen
);
14147 mch_memmove(s
+ prevlen
, buf
, buflen
);
14157 mch_memmove(buf
, buf
+ tolist
, buflen
- tolist
);
14163 * For a negative line count use only the lines at the end of the file,
14167 while (cnt
> -maxline
)
14169 listitem_remove(rettv
->vval
.v_list
, rettv
->vval
.v_list
->lv_first
);
14177 #if defined(FEAT_RELTIME)
14178 static int list2proftime
__ARGS((typval_T
*arg
, proftime_T
*tm
));
14181 * Convert a List to proftime_T.
14182 * Return FAIL when there is something wrong.
14185 list2proftime(arg
, tm
)
14192 if (arg
->v_type
!= VAR_LIST
|| arg
->vval
.v_list
== NULL
14193 || arg
->vval
.v_list
->lv_len
!= 2)
14195 n1
= list_find_nr(arg
->vval
.v_list
, 0L, &error
);
14196 n2
= list_find_nr(arg
->vval
.v_list
, 1L, &error
);
14204 return error
? FAIL
: OK
;
14206 #endif /* FEAT_RELTIME */
14209 * "reltime()" function
14212 f_reltime(argvars
, rettv
)
14216 #ifdef FEAT_RELTIME
14220 if (argvars
[0].v_type
== VAR_UNKNOWN
)
14222 /* No arguments: get current time. */
14223 profile_start(&res
);
14225 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
14227 if (list2proftime(&argvars
[0], &res
) == FAIL
)
14233 /* Two arguments: compute the difference. */
14234 if (list2proftime(&argvars
[0], &start
) == FAIL
14235 || list2proftime(&argvars
[1], &res
) == FAIL
)
14237 profile_sub(&res
, &start
);
14240 if (rettv_list_alloc(rettv
) == OK
)
14251 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n1
);
14252 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n2
);
14258 * "reltimestr()" function
14261 f_reltimestr(argvars
, rettv
)
14265 #ifdef FEAT_RELTIME
14269 rettv
->v_type
= VAR_STRING
;
14270 rettv
->vval
.v_string
= NULL
;
14271 #ifdef FEAT_RELTIME
14272 if (list2proftime(&argvars
[0], &tm
) == OK
)
14273 rettv
->vval
.v_string
= vim_strsave((char_u
*)profile_msg(&tm
));
14277 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14278 static void make_connection
__ARGS((void));
14279 static int check_connection
__ARGS((void));
14284 if (X_DISPLAY
== NULL
14290 x_force_connect
= TRUE
;
14292 x_force_connect
= FALSE
;
14300 if (X_DISPLAY
== NULL
)
14302 EMSG(_("E240: No connection to Vim server"));
14309 #ifdef FEAT_CLIENTSERVER
14310 static void remote_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int expr
));
14313 remote_common(argvars
, rettv
, expr
)
14318 char_u
*server_name
;
14321 char_u buf
[NUMBUFLEN
];
14328 if (check_restricted() || check_secure())
14332 if (check_connection() == FAIL
)
14336 server_name
= get_tv_string_chk(&argvars
[0]);
14337 if (server_name
== NULL
)
14338 return; /* type error; errmsg already given */
14339 keys
= get_tv_string_buf(&argvars
[1], buf
);
14341 if (serverSendToVim(server_name
, keys
, &r
, &w
, expr
, TRUE
) < 0)
14343 if (serverSendToVim(X_DISPLAY
, server_name
, keys
, &r
, &w
, expr
, 0, TRUE
)
14348 EMSG(r
); /* sending worked but evaluation failed */
14350 EMSG2(_("E241: Unable to send to %s"), server_name
);
14354 rettv
->vval
.v_string
= r
;
14356 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14362 sprintf((char *)str
, PRINTF_HEX_LONG_U
, (long_u
)w
);
14363 v
.di_tv
.v_type
= VAR_STRING
;
14364 v
.di_tv
.vval
.v_string
= vim_strsave(str
);
14365 idvar
= get_tv_string_chk(&argvars
[2]);
14367 set_var(idvar
, &v
.di_tv
, FALSE
);
14368 vim_free(v
.di_tv
.vval
.v_string
);
14374 * "remote_expr()" function
14378 f_remote_expr(argvars
, rettv
)
14382 rettv
->v_type
= VAR_STRING
;
14383 rettv
->vval
.v_string
= NULL
;
14384 #ifdef FEAT_CLIENTSERVER
14385 remote_common(argvars
, rettv
, TRUE
);
14390 * "remote_foreground()" function
14394 f_remote_foreground(argvars
, rettv
)
14398 rettv
->vval
.v_number
= 0;
14399 #ifdef FEAT_CLIENTSERVER
14401 /* On Win32 it's done in this application. */
14403 char_u
*server_name
= get_tv_string_chk(&argvars
[0]);
14405 if (server_name
!= NULL
)
14406 serverForeground(server_name
);
14409 /* Send a foreground() expression to the server. */
14410 argvars
[1].v_type
= VAR_STRING
;
14411 argvars
[1].vval
.v_string
= vim_strsave((char_u
*)"foreground()");
14412 argvars
[2].v_type
= VAR_UNKNOWN
;
14413 remote_common(argvars
, rettv
, TRUE
);
14414 vim_free(argvars
[1].vval
.v_string
);
14421 f_remote_peek(argvars
, rettv
)
14425 #ifdef FEAT_CLIENTSERVER
14433 if (check_restricted() || check_secure())
14435 rettv
->vval
.v_number
= -1;
14438 serverid
= get_tv_string_chk(&argvars
[0]);
14439 if (serverid
== NULL
)
14441 rettv
->vval
.v_number
= -1;
14442 return; /* type error; errmsg already given */
14445 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
14447 rettv
->vval
.v_number
= -1;
14450 s
= serverGetReply((HWND
)n
, FALSE
, FALSE
, FALSE
);
14451 rettv
->vval
.v_number
= (s
!= NULL
);
14454 rettv
->vval
.v_number
= 0;
14455 if (check_connection() == FAIL
)
14458 rettv
->vval
.v_number
= serverPeekReply(X_DISPLAY
,
14459 serverStrToWin(serverid
), &s
);
14462 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& rettv
->vval
.v_number
> 0)
14466 v
.di_tv
.v_type
= VAR_STRING
;
14467 v
.di_tv
.vval
.v_string
= vim_strsave(s
);
14468 retvar
= get_tv_string_chk(&argvars
[1]);
14469 if (retvar
!= NULL
)
14470 set_var(retvar
, &v
.di_tv
, FALSE
);
14471 vim_free(v
.di_tv
.vval
.v_string
);
14474 rettv
->vval
.v_number
= -1;
14480 f_remote_read(argvars
, rettv
)
14486 #ifdef FEAT_CLIENTSERVER
14487 char_u
*serverid
= get_tv_string_chk(&argvars
[0]);
14489 if (serverid
!= NULL
&& !check_restricted() && !check_secure())
14492 /* The server's HWND is encoded in the 'id' parameter */
14495 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
14497 r
= serverGetReply((HWND
)n
, FALSE
, TRUE
, TRUE
);
14500 if (check_connection() == FAIL
|| serverReadReply(X_DISPLAY
,
14501 serverStrToWin(serverid
), &r
, FALSE
) < 0)
14503 EMSG(_("E277: Unable to read a server reply"));
14506 rettv
->v_type
= VAR_STRING
;
14507 rettv
->vval
.v_string
= r
;
14511 * "remote_send()" function
14515 f_remote_send(argvars
, rettv
)
14519 rettv
->v_type
= VAR_STRING
;
14520 rettv
->vval
.v_string
= NULL
;
14521 #ifdef FEAT_CLIENTSERVER
14522 remote_common(argvars
, rettv
, FALSE
);
14527 * "remove()" function
14530 f_remove(argvars
, rettv
)
14535 listitem_T
*item
, *item2
;
14543 rettv
->vval
.v_number
= 0;
14544 if (argvars
[0].v_type
== VAR_DICT
)
14546 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14547 EMSG2(_(e_toomanyarg
), "remove()");
14548 else if ((d
= argvars
[0].vval
.v_dict
) != NULL
14549 && !tv_check_lock(d
->dv_lock
, (char_u
*)"remove() argument"))
14551 key
= get_tv_string_chk(&argvars
[1]);
14554 di
= dict_find(d
, key
, -1);
14556 EMSG2(_(e_dictkey
), key
);
14559 *rettv
= di
->di_tv
;
14560 init_tv(&di
->di_tv
);
14561 dictitem_remove(d
, di
);
14566 else if (argvars
[0].v_type
!= VAR_LIST
)
14567 EMSG2(_(e_listdictarg
), "remove()");
14568 else if ((l
= argvars
[0].vval
.v_list
) != NULL
14569 && !tv_check_lock(l
->lv_lock
, (char_u
*)"remove() argument"))
14573 idx
= get_tv_number_chk(&argvars
[1], &error
);
14575 ; /* type error: do nothing, errmsg already given */
14576 else if ((item
= list_find(l
, idx
)) == NULL
)
14577 EMSGN(_(e_listidx
), idx
);
14580 if (argvars
[2].v_type
== VAR_UNKNOWN
)
14582 /* Remove one item, return its value. */
14583 list_remove(l
, item
, item
);
14584 *rettv
= item
->li_tv
;
14589 /* Remove range of items, return list with values. */
14590 end
= get_tv_number_chk(&argvars
[2], &error
);
14592 ; /* type error: do nothing */
14593 else if ((item2
= list_find(l
, end
)) == NULL
)
14594 EMSGN(_(e_listidx
), end
);
14599 for (li
= item
; li
!= NULL
; li
= li
->li_next
)
14605 if (li
== NULL
) /* didn't find "item2" after "item" */
14606 EMSG(_(e_invrange
));
14609 list_remove(l
, item
, item2
);
14610 if (rettv_list_alloc(rettv
) == OK
)
14612 l
= rettv
->vval
.v_list
;
14613 l
->lv_first
= item
;
14614 l
->lv_last
= item2
;
14615 item
->li_prev
= NULL
;
14616 item2
->li_next
= NULL
;
14627 * "rename({from}, {to})" function
14630 f_rename(argvars
, rettv
)
14634 char_u buf
[NUMBUFLEN
];
14636 if (check_restricted() || check_secure())
14637 rettv
->vval
.v_number
= -1;
14639 rettv
->vval
.v_number
= vim_rename(get_tv_string(&argvars
[0]),
14640 get_tv_string_buf(&argvars
[1], buf
));
14644 * "repeat()" function
14648 f_repeat(argvars
, rettv
)
14659 n
= get_tv_number(&argvars
[1]);
14660 if (argvars
[0].v_type
== VAR_LIST
)
14662 if (rettv_list_alloc(rettv
) == OK
&& argvars
[0].vval
.v_list
!= NULL
)
14664 if (list_extend(rettv
->vval
.v_list
,
14665 argvars
[0].vval
.v_list
, NULL
) == FAIL
)
14670 p
= get_tv_string(&argvars
[0]);
14671 rettv
->v_type
= VAR_STRING
;
14672 rettv
->vval
.v_string
= NULL
;
14674 slen
= (int)STRLEN(p
);
14679 r
= alloc(len
+ 1);
14682 for (i
= 0; i
< n
; i
++)
14683 mch_memmove(r
+ i
* slen
, p
, (size_t)slen
);
14687 rettv
->vval
.v_string
= r
;
14692 * "resolve()" function
14695 f_resolve(argvars
, rettv
)
14701 p
= get_tv_string(&argvars
[0]);
14702 #ifdef FEAT_SHORTCUT
14706 v
= mch_resolve_shortcut(p
);
14708 rettv
->vval
.v_string
= v
;
14710 rettv
->vval
.v_string
= vim_strsave(p
);
14713 # ifdef HAVE_READLINK
14715 char_u buf
[MAXPATHL
+ 1];
14718 char_u
*remain
= NULL
;
14720 int is_relative_to_current
= FALSE
;
14721 int has_trailing_pathsep
= FALSE
;
14724 p
= vim_strsave(p
);
14726 if (p
[0] == '.' && (vim_ispathsep(p
[1])
14727 || (p
[1] == '.' && (vim_ispathsep(p
[2])))))
14728 is_relative_to_current
= TRUE
;
14731 if (len
> 0 && after_pathsep(p
, p
+ len
))
14732 has_trailing_pathsep
= TRUE
;
14734 q
= getnextcomp(p
);
14737 /* Separate the first path component in "p", and keep the
14738 * remainder (beginning with the path separator). */
14739 remain
= vim_strsave(q
- 1);
14747 len
= readlink((char *)p
, (char *)buf
, MAXPATHL
);
14756 EMSG(_("E655: Too many symbolic links (cycle?)"));
14757 rettv
->vval
.v_string
= NULL
;
14761 /* Ensure that the result will have a trailing path separator
14762 * if the argument has one. */
14763 if (remain
== NULL
&& has_trailing_pathsep
)
14766 /* Separate the first path component in the link value and
14767 * concatenate the remainders. */
14768 q
= getnextcomp(vim_ispathsep(*buf
) ? buf
+ 1 : buf
);
14771 if (remain
== NULL
)
14772 remain
= vim_strsave(q
- 1);
14775 cpy
= concat_str(q
- 1, remain
);
14786 if (q
> p
&& *q
== NUL
)
14788 /* Ignore trailing path separator. */
14792 if (q
> p
&& !mch_isFullName(buf
))
14794 /* symlink is relative to directory of argument */
14795 cpy
= alloc((unsigned)(STRLEN(p
) + STRLEN(buf
) + 1));
14799 STRCPY(gettail(cpy
), buf
);
14807 p
= vim_strsave(buf
);
14811 if (remain
== NULL
)
14814 /* Append the first path component of "remain" to "p". */
14815 q
= getnextcomp(remain
+ 1);
14816 len
= q
- remain
- (*q
!= NUL
);
14817 cpy
= vim_strnsave(p
, STRLEN(p
) + len
);
14820 STRNCAT(cpy
, remain
, len
);
14824 /* Shorten "remain". */
14826 STRMOVE(remain
, q
- 1);
14834 /* If the result is a relative path name, make it explicitly relative to
14835 * the current directory if and only if the argument had this form. */
14836 if (!vim_ispathsep(*p
))
14838 if (is_relative_to_current
14842 || vim_ispathsep(p
[1])
14845 || vim_ispathsep(p
[2]))))))
14847 /* Prepend "./". */
14848 cpy
= concat_str((char_u
*)"./", p
);
14855 else if (!is_relative_to_current
)
14857 /* Strip leading "./". */
14859 while (q
[0] == '.' && vim_ispathsep(q
[1]))
14866 /* Ensure that the result will have no trailing path separator
14867 * if the argument had none. But keep "/" or "//". */
14868 if (!has_trailing_pathsep
)
14871 if (after_pathsep(p
, q
))
14872 *gettail_sep(p
) = NUL
;
14875 rettv
->vval
.v_string
= p
;
14878 rettv
->vval
.v_string
= vim_strsave(p
);
14882 simplify_filename(rettv
->vval
.v_string
);
14884 #ifdef HAVE_READLINK
14887 rettv
->v_type
= VAR_STRING
;
14891 * "reverse({list})" function
14894 f_reverse(argvars
, rettv
)
14899 listitem_T
*li
, *ni
;
14901 rettv
->vval
.v_number
= 0;
14902 if (argvars
[0].v_type
!= VAR_LIST
)
14903 EMSG2(_(e_listarg
), "reverse()");
14904 else if ((l
= argvars
[0].vval
.v_list
) != NULL
14905 && !tv_check_lock(l
->lv_lock
, (char_u
*)"reverse()"))
14908 l
->lv_first
= l
->lv_last
= NULL
;
14913 list_append(l
, li
);
14916 rettv
->vval
.v_list
= l
;
14917 rettv
->v_type
= VAR_LIST
;
14919 l
->lv_idx
= l
->lv_len
- l
->lv_idx
- 1;
14923 #define SP_NOMOVE 0x01 /* don't move cursor */
14924 #define SP_REPEAT 0x02 /* repeat to find outer pair */
14925 #define SP_RETCOUNT 0x04 /* return matchcount */
14926 #define SP_SETPCMARK 0x08 /* set previous context mark */
14927 #define SP_START 0x10 /* accept match at start position */
14928 #define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14929 #define SP_END 0x40 /* leave cursor at end of match */
14931 static int get_search_arg
__ARGS((typval_T
*varp
, int *flagsp
));
14934 * Get flags for a search function.
14935 * Possibly sets "p_ws".
14936 * Returns BACKWARD, FORWARD or zero (for an error).
14939 get_search_arg(varp
, flagsp
)
14945 char_u nbuf
[NUMBUFLEN
];
14948 if (varp
->v_type
!= VAR_UNKNOWN
)
14950 flags
= get_tv_string_buf_chk(varp
, nbuf
);
14952 return 0; /* type error; errmsg already given */
14953 while (*flags
!= NUL
)
14957 case 'b': dir
= BACKWARD
; break;
14958 case 'w': p_ws
= TRUE
; break;
14959 case 'W': p_ws
= FALSE
; break;
14961 if (flagsp
!= NULL
)
14964 case 'c': mask
= SP_START
; break;
14965 case 'e': mask
= SP_END
; break;
14966 case 'm': mask
= SP_RETCOUNT
; break;
14967 case 'n': mask
= SP_NOMOVE
; break;
14968 case 'p': mask
= SP_SUBPAT
; break;
14969 case 'r': mask
= SP_REPEAT
; break;
14970 case 's': mask
= SP_SETPCMARK
; break;
14974 EMSG2(_(e_invarg2
), flags
);
14989 * Shared by search() and searchpos() functions
14992 search_cmn(argvars
, match_pos
, flagsp
)
15001 int save_p_ws
= p_ws
;
15003 int retval
= 0; /* default: FAIL */
15004 long lnum_stop
= 0;
15006 #ifdef FEAT_RELTIME
15007 long time_limit
= 0;
15009 int options
= SEARCH_KEEP
;
15012 pat
= get_tv_string(&argvars
[0]);
15013 dir
= get_search_arg(&argvars
[1], flagsp
); /* may set p_ws */
15017 if (flags
& SP_START
)
15018 options
|= SEARCH_START
;
15019 if (flags
& SP_END
)
15020 options
|= SEARCH_END
;
15022 /* Optional arguments: line number to stop searching and timeout. */
15023 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
15025 lnum_stop
= get_tv_number_chk(&argvars
[2], NULL
);
15028 #ifdef FEAT_RELTIME
15029 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
15031 time_limit
= get_tv_number_chk(&argvars
[3], NULL
);
15032 if (time_limit
< 0)
15038 #ifdef FEAT_RELTIME
15039 /* Set the time limit, if there is one. */
15040 profile_setlimit(time_limit
, &tm
);
15044 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
15045 * Check to make sure only those flags are set.
15046 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15047 * flags cannot be set. Check for that condition also.
15049 if (((flags
& (SP_REPEAT
| SP_RETCOUNT
)) != 0)
15050 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
15052 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[1]));
15056 pos
= save_cursor
= curwin
->w_cursor
;
15057 subpatnum
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
15058 options
, RE_SEARCH
, (linenr_T
)lnum_stop
, &tm
);
15059 if (subpatnum
!= FAIL
)
15061 if (flags
& SP_SUBPAT
)
15062 retval
= subpatnum
;
15065 if (flags
& SP_SETPCMARK
)
15067 curwin
->w_cursor
= pos
;
15068 if (match_pos
!= NULL
)
15070 /* Store the match cursor position */
15071 match_pos
->lnum
= pos
.lnum
;
15072 match_pos
->col
= pos
.col
+ 1;
15074 /* "/$" will put the cursor after the end of the line, may need to
15075 * correct that here */
15079 /* If 'n' flag is used: restore cursor position. */
15080 if (flags
& SP_NOMOVE
)
15081 curwin
->w_cursor
= save_cursor
;
15083 curwin
->w_set_curswant
= TRUE
;
15092 * "round({float})" function
15095 f_round(argvars
, rettv
)
15101 rettv
->v_type
= VAR_FLOAT
;
15102 if (get_float_arg(argvars
, &f
) == OK
)
15103 /* round() is not in C90, use ceil() or floor() instead. */
15104 rettv
->vval
.v_float
= f
> 0 ? floor(f
+ 0.5) : ceil(f
- 0.5);
15106 rettv
->vval
.v_float
= 0.0;
15111 * "search()" function
15114 f_search(argvars
, rettv
)
15120 rettv
->vval
.v_number
= search_cmn(argvars
, NULL
, &flags
);
15124 * "searchdecl()" function
15127 f_searchdecl(argvars
, rettv
)
15136 rettv
->vval
.v_number
= 1; /* default: FAIL */
15138 name
= get_tv_string_chk(&argvars
[0]);
15139 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15141 locally
= get_tv_number_chk(&argvars
[1], &error
) == 0;
15142 if (!error
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
15143 thisblock
= get_tv_number_chk(&argvars
[2], &error
) != 0;
15145 if (!error
&& name
!= NULL
)
15146 rettv
->vval
.v_number
= find_decl(name
, (int)STRLEN(name
),
15147 locally
, thisblock
, SEARCH_KEEP
) == FAIL
;
15151 * Used by searchpair() and searchpairpos()
15154 searchpair_cmn(argvars
, match_pos
)
15158 char_u
*spat
, *mpat
, *epat
;
15160 int save_p_ws
= p_ws
;
15163 char_u nbuf1
[NUMBUFLEN
];
15164 char_u nbuf2
[NUMBUFLEN
];
15165 char_u nbuf3
[NUMBUFLEN
];
15166 int retval
= 0; /* default: FAIL */
15167 long lnum_stop
= 0;
15168 long time_limit
= 0;
15170 /* Get the three pattern arguments: start, middle, end. */
15171 spat
= get_tv_string_chk(&argvars
[0]);
15172 mpat
= get_tv_string_buf_chk(&argvars
[1], nbuf1
);
15173 epat
= get_tv_string_buf_chk(&argvars
[2], nbuf2
);
15174 if (spat
== NULL
|| mpat
== NULL
|| epat
== NULL
)
15175 goto theend
; /* type error */
15177 /* Handle the optional fourth argument: flags */
15178 dir
= get_search_arg(&argvars
[3], &flags
); /* may set p_ws */
15182 /* Don't accept SP_END or SP_SUBPAT.
15183 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15185 if ((flags
& (SP_END
| SP_SUBPAT
)) != 0
15186 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
15188 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[3]));
15192 /* Using 'r' implies 'W', otherwise it doesn't work. */
15193 if (flags
& SP_REPEAT
)
15196 /* Optional fifth argument: skip expression */
15197 if (argvars
[3].v_type
== VAR_UNKNOWN
15198 || argvars
[4].v_type
== VAR_UNKNOWN
)
15199 skip
= (char_u
*)"";
15202 skip
= get_tv_string_buf_chk(&argvars
[4], nbuf3
);
15203 if (argvars
[5].v_type
!= VAR_UNKNOWN
)
15205 lnum_stop
= get_tv_number_chk(&argvars
[5], NULL
);
15208 #ifdef FEAT_RELTIME
15209 if (argvars
[6].v_type
!= VAR_UNKNOWN
)
15211 time_limit
= get_tv_number_chk(&argvars
[6], NULL
);
15212 if (time_limit
< 0)
15219 goto theend
; /* type error */
15221 retval
= do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
,
15222 match_pos
, lnum_stop
, time_limit
);
15231 * "searchpair()" function
15234 f_searchpair(argvars
, rettv
)
15238 rettv
->vval
.v_number
= searchpair_cmn(argvars
, NULL
);
15242 * "searchpairpos()" function
15245 f_searchpairpos(argvars
, rettv
)
15253 rettv
->vval
.v_number
= 0;
15255 if (rettv_list_alloc(rettv
) == FAIL
)
15258 if (searchpair_cmn(argvars
, &match_pos
) > 0)
15260 lnum
= match_pos
.lnum
;
15261 col
= match_pos
.col
;
15264 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
15265 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
15269 * Search for a start/middle/end thing.
15270 * Used by searchpair(), see its documentation for the details.
15271 * Returns 0 or -1 for no match,
15274 do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
, match_pos
,
15275 lnum_stop
, time_limit
)
15276 char_u
*spat
; /* start pattern */
15277 char_u
*mpat
; /* middle pattern */
15278 char_u
*epat
; /* end pattern */
15279 int dir
; /* BACKWARD or FORWARD */
15280 char_u
*skip
; /* skip expression */
15281 int flags
; /* SP_SETPCMARK and other SP_ values */
15283 linenr_T lnum_stop
; /* stop at this line if not zero */
15284 long time_limit
; /* stop after this many msec */
15287 char_u
*pat
, *pat2
= NULL
, *pat3
= NULL
;
15298 int options
= SEARCH_KEEP
;
15301 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15303 p_cpo
= empty_option
;
15305 #ifdef FEAT_RELTIME
15306 /* Set the time limit, if there is one. */
15307 profile_setlimit(time_limit
, &tm
);
15310 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15311 * start/middle/end (pat3, for the top pair). */
15312 pat2
= alloc((unsigned)(STRLEN(spat
) + STRLEN(epat
) + 15));
15313 pat3
= alloc((unsigned)(STRLEN(spat
) + STRLEN(mpat
) + STRLEN(epat
) + 23));
15314 if (pat2
== NULL
|| pat3
== NULL
)
15316 sprintf((char *)pat2
, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat
, epat
);
15318 STRCPY(pat3
, pat2
);
15320 sprintf((char *)pat3
, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15322 if (flags
& SP_START
)
15323 options
|= SEARCH_START
;
15325 save_cursor
= curwin
->w_cursor
;
15326 pos
= curwin
->w_cursor
;
15327 clearpos(&firstpos
);
15328 clearpos(&foundpos
);
15332 n
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
15333 options
, RE_SEARCH
, lnum_stop
, &tm
);
15334 if (n
== FAIL
|| (firstpos
.lnum
!= 0 && equalpos(pos
, firstpos
)))
15335 /* didn't find it or found the first match again: FAIL */
15338 if (firstpos
.lnum
== 0)
15340 if (equalpos(pos
, foundpos
))
15342 /* Found the same position again. Can happen with a pattern that
15343 * has "\zs" at the end and searching backwards. Advance one
15344 * character and try again. */
15345 if (dir
== BACKWARD
)
15352 /* clear the start flag to avoid getting stuck here */
15353 options
&= ~SEARCH_START
;
15355 /* If the skip pattern matches, ignore this match. */
15358 save_pos
= curwin
->w_cursor
;
15359 curwin
->w_cursor
= pos
;
15360 r
= eval_to_bool(skip
, &err
, NULL
, FALSE
);
15361 curwin
->w_cursor
= save_pos
;
15364 /* Evaluating {skip} caused an error, break here. */
15365 curwin
->w_cursor
= save_cursor
;
15373 if ((dir
== BACKWARD
&& n
== 3) || (dir
== FORWARD
&& n
== 2))
15375 /* Found end when searching backwards or start when searching
15376 * forward: nested pair. */
15378 pat
= pat2
; /* nested, don't search for middle */
15382 /* Found end when searching forward or start when searching
15383 * backward: end of (nested) pair; or found middle in outer pair. */
15385 pat
= pat3
; /* outer level, search for middle */
15390 /* Found the match: return matchcount or line number. */
15391 if (flags
& SP_RETCOUNT
)
15395 if (flags
& SP_SETPCMARK
)
15397 curwin
->w_cursor
= pos
;
15398 if (!(flags
& SP_REPEAT
))
15400 nest
= 1; /* search for next unmatched */
15404 if (match_pos
!= NULL
)
15406 /* Store the match cursor position */
15407 match_pos
->lnum
= curwin
->w_cursor
.lnum
;
15408 match_pos
->col
= curwin
->w_cursor
.col
+ 1;
15411 /* If 'n' flag is used or search failed: restore cursor position. */
15412 if ((flags
& SP_NOMOVE
) || retval
== 0)
15413 curwin
->w_cursor
= save_cursor
;
15418 if (p_cpo
== empty_option
)
15421 /* Darn, evaluating the {skip} expression changed the value. */
15422 free_string_option(save_cpo
);
15428 * "searchpos()" function
15431 f_searchpos(argvars
, rettv
)
15441 rettv
->vval
.v_number
= 0;
15443 if (rettv_list_alloc(rettv
) == FAIL
)
15446 n
= search_cmn(argvars
, &match_pos
, &flags
);
15449 lnum
= match_pos
.lnum
;
15450 col
= match_pos
.col
;
15453 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
15454 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
15455 if (flags
& SP_SUBPAT
)
15456 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n
);
15462 f_server2client(argvars
, rettv
)
15466 #ifdef FEAT_CLIENTSERVER
15467 char_u buf
[NUMBUFLEN
];
15468 char_u
*server
= get_tv_string_chk(&argvars
[0]);
15469 char_u
*reply
= get_tv_string_buf_chk(&argvars
[1], buf
);
15471 rettv
->vval
.v_number
= -1;
15472 if (server
== NULL
|| reply
== NULL
)
15474 if (check_restricted() || check_secure())
15477 if (check_connection() == FAIL
)
15481 if (serverSendReply(server
, reply
) < 0)
15483 EMSG(_("E258: Unable to send to client"));
15486 rettv
->vval
.v_number
= 0;
15488 rettv
->vval
.v_number
= -1;
15494 f_serverlist(argvars
, rettv
)
15500 #ifdef FEAT_CLIENTSERVER
15502 r
= serverGetVimNames();
15505 if (X_DISPLAY
!= NULL
)
15506 r
= serverGetVimNames(X_DISPLAY
);
15509 rettv
->v_type
= VAR_STRING
;
15510 rettv
->vval
.v_string
= r
;
15514 * "setbufvar()" function
15518 f_setbufvar(argvars
, rettv
)
15524 char_u
*varname
, *bufvarname
;
15526 char_u nbuf
[NUMBUFLEN
];
15528 rettv
->vval
.v_number
= 0;
15530 if (check_restricted() || check_secure())
15532 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
15533 varname
= get_tv_string_chk(&argvars
[1]);
15534 buf
= get_buf_tv(&argvars
[0]);
15535 varp
= &argvars
[2];
15537 if (buf
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
15539 /* set curbuf to be our buf, temporarily */
15540 aucmd_prepbuf(&aco
, buf
);
15542 if (*varname
== '&')
15549 numval
= get_tv_number_chk(varp
, &error
);
15550 strval
= get_tv_string_buf_chk(varp
, nbuf
);
15551 if (!error
&& strval
!= NULL
)
15552 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
15556 bufvarname
= alloc((unsigned)STRLEN(varname
) + 3);
15557 if (bufvarname
!= NULL
)
15559 STRCPY(bufvarname
, "b:");
15560 STRCPY(bufvarname
+ 2, varname
);
15561 set_var(bufvarname
, varp
, TRUE
);
15562 vim_free(bufvarname
);
15566 /* reset notion of buffer */
15567 aucmd_restbuf(&aco
);
15572 * "setcmdpos()" function
15575 f_setcmdpos(argvars
, rettv
)
15579 int pos
= (int)get_tv_number(&argvars
[0]) - 1;
15582 rettv
->vval
.v_number
= set_cmdline_pos(pos
);
15586 * "setline()" function
15589 f_setline(argvars
, rettv
)
15594 char_u
*line
= NULL
;
15596 listitem_T
*li
= NULL
;
15598 linenr_T lcount
= curbuf
->b_ml
.ml_line_count
;
15600 lnum
= get_tv_lnum(&argvars
[0]);
15601 if (argvars
[1].v_type
== VAR_LIST
)
15603 l
= argvars
[1].vval
.v_list
;
15607 line
= get_tv_string_chk(&argvars
[1]);
15609 rettv
->vval
.v_number
= 0; /* OK */
15614 /* list argument, get next string */
15617 line
= get_tv_string_chk(&li
->li_tv
);
15621 rettv
->vval
.v_number
= 1; /* FAIL */
15622 if (line
== NULL
|| lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
15624 if (lnum
<= curbuf
->b_ml
.ml_line_count
)
15626 /* existing line, replace it */
15627 if (u_savesub(lnum
) == OK
&& ml_replace(lnum
, line
, TRUE
) == OK
)
15629 changed_bytes(lnum
, 0);
15630 if (lnum
== curwin
->w_cursor
.lnum
)
15631 check_cursor_col();
15632 rettv
->vval
.v_number
= 0; /* OK */
15635 else if (added
> 0 || u_save(lnum
- 1, lnum
) == OK
)
15637 /* lnum is one past the last line, append the line */
15639 if (ml_append(lnum
- 1, line
, (colnr_T
)0, FALSE
) == OK
)
15640 rettv
->vval
.v_number
= 0; /* OK */
15643 if (l
== NULL
) /* only one string argument */
15649 appended_lines_mark(lcount
, added
);
15652 static void set_qf_ll_list
__ARGS((win_T
*wp
, typval_T
*list_arg
, typval_T
*action_arg
, typval_T
*rettv
));
15655 * Used by "setqflist()" and "setloclist()" functions
15659 set_qf_ll_list(wp
, list_arg
, action_arg
, rettv
)
15661 typval_T
*list_arg
;
15662 typval_T
*action_arg
;
15665 #ifdef FEAT_QUICKFIX
15670 rettv
->vval
.v_number
= -1;
15672 #ifdef FEAT_QUICKFIX
15673 if (list_arg
->v_type
!= VAR_LIST
)
15674 EMSG(_(e_listreq
));
15677 list_T
*l
= list_arg
->vval
.v_list
;
15679 if (action_arg
->v_type
== VAR_STRING
)
15681 act
= get_tv_string_chk(action_arg
);
15683 return; /* type error; errmsg already given */
15684 if (*act
== 'a' || *act
== 'r')
15688 if (l
!= NULL
&& set_errorlist(wp
, l
, action
) == OK
)
15689 rettv
->vval
.v_number
= 0;
15695 * "setloclist()" function
15699 f_setloclist(argvars
, rettv
)
15705 rettv
->vval
.v_number
= -1;
15707 win
= find_win_by_nr(&argvars
[0], NULL
);
15709 set_qf_ll_list(win
, &argvars
[1], &argvars
[2], rettv
);
15713 * "setmatches()" function
15716 f_setmatches(argvars
, rettv
)
15720 #ifdef FEAT_SEARCH_EXTRA
15725 rettv
->vval
.v_number
= -1;
15726 if (argvars
[0].v_type
!= VAR_LIST
)
15728 EMSG(_(e_listreq
));
15731 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
15734 /* To some extent make sure that we are dealing with a list from
15735 * "getmatches()". */
15739 if (li
->li_tv
.v_type
!= VAR_DICT
15740 || (d
= li
->li_tv
.vval
.v_dict
) == NULL
)
15745 if (!(dict_find(d
, (char_u
*)"group", -1) != NULL
15746 && dict_find(d
, (char_u
*)"pattern", -1) != NULL
15747 && dict_find(d
, (char_u
*)"priority", -1) != NULL
15748 && dict_find(d
, (char_u
*)"id", -1) != NULL
))
15756 clear_matches(curwin
);
15760 d
= li
->li_tv
.vval
.v_dict
;
15761 match_add(curwin
, get_dict_string(d
, (char_u
*)"group", FALSE
),
15762 get_dict_string(d
, (char_u
*)"pattern", FALSE
),
15763 (int)get_dict_number(d
, (char_u
*)"priority"),
15764 (int)get_dict_number(d
, (char_u
*)"id"));
15767 rettv
->vval
.v_number
= 0;
15773 * "setpos()" function
15777 f_setpos(argvars
, rettv
)
15785 rettv
->vval
.v_number
= -1;
15786 name
= get_tv_string_chk(argvars
);
15789 if (list2fpos(&argvars
[1], &pos
, &fnum
) == OK
)
15792 if (name
[0] == '.' && name
[1] == NUL
)
15795 if (fnum
== curbuf
->b_fnum
)
15797 curwin
->w_cursor
= pos
;
15799 rettv
->vval
.v_number
= 0;
15804 else if (name
[0] == '\'' && name
[1] != NUL
&& name
[2] == NUL
)
15807 if (setmark_pos(name
[1], &pos
, fnum
) == OK
)
15808 rettv
->vval
.v_number
= 0;
15817 * "setqflist()" function
15821 f_setqflist(argvars
, rettv
)
15825 set_qf_ll_list(NULL
, &argvars
[0], &argvars
[1], rettv
);
15829 * "setreg()" function
15832 f_setreg(argvars
, rettv
)
15837 char_u
*strregname
;
15848 strregname
= get_tv_string_chk(argvars
);
15849 rettv
->vval
.v_number
= 1; /* FAIL is default */
15851 if (strregname
== NULL
)
15852 return; /* type error; errmsg already given */
15853 regname
= *strregname
;
15854 if (regname
== 0 || regname
== '@')
15856 else if (regname
== '=')
15859 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15861 stropt
= get_tv_string_chk(&argvars
[2]);
15862 if (stropt
== NULL
)
15863 return; /* type error */
15864 for (; *stropt
!= NUL
; ++stropt
)
15867 case 'a': case 'A': /* append */
15870 case 'v': case 'c': /* character-wise selection */
15873 case 'V': case 'l': /* line-wise selection */
15877 case 'b': case Ctrl_V
: /* block-wise selection */
15878 yank_type
= MBLOCK
;
15879 if (VIM_ISDIGIT(stropt
[1]))
15882 block_len
= getdigits(&stropt
) - 1;
15890 strval
= get_tv_string_chk(&argvars
[1]);
15891 if (strval
!= NULL
)
15892 write_reg_contents_ex(regname
, strval
, -1,
15893 append
, yank_type
, block_len
);
15894 rettv
->vval
.v_number
= 0;
15898 * "settabwinvar()" function
15901 f_settabwinvar(argvars
, rettv
)
15905 setwinvar(argvars
, rettv
, 1);
15909 * "setwinvar()" function
15912 f_setwinvar(argvars
, rettv
)
15916 setwinvar(argvars
, rettv
, 0);
15920 * "setwinvar()" and "settabwinvar()" functions
15923 setwinvar(argvars
, rettv
, off
)
15929 #ifdef FEAT_WINDOWS
15930 win_T
*save_curwin
;
15931 tabpage_T
*save_curtab
;
15933 char_u
*varname
, *winvarname
;
15935 char_u nbuf
[NUMBUFLEN
];
15938 rettv
->vval
.v_number
= 0;
15940 if (check_restricted() || check_secure())
15943 #ifdef FEAT_WINDOWS
15945 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
15949 win
= find_win_by_nr(&argvars
[off
], tp
);
15950 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
15951 varp
= &argvars
[off
+ 2];
15953 if (win
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
15955 #ifdef FEAT_WINDOWS
15956 /* set curwin to be our win, temporarily */
15957 save_curwin
= curwin
;
15958 save_curtab
= curtab
;
15959 goto_tabpage_tp(tp
);
15960 if (!win_valid(win
))
15963 curbuf
= curwin
->w_buffer
;
15966 if (*varname
== '&')
15973 numval
= get_tv_number_chk(varp
, &error
);
15974 strval
= get_tv_string_buf_chk(varp
, nbuf
);
15975 if (!error
&& strval
!= NULL
)
15976 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
15980 winvarname
= alloc((unsigned)STRLEN(varname
) + 3);
15981 if (winvarname
!= NULL
)
15983 STRCPY(winvarname
, "w:");
15984 STRCPY(winvarname
+ 2, varname
);
15985 set_var(winvarname
, varp
, TRUE
);
15986 vim_free(winvarname
);
15990 #ifdef FEAT_WINDOWS
15991 /* Restore current tabpage and window, if still valid (autocomands can
15992 * make them invalid). */
15993 if (valid_tabpage(save_curtab
))
15994 goto_tabpage_tp(save_curtab
);
15995 if (win_valid(save_curwin
))
15997 curwin
= save_curwin
;
15998 curbuf
= curwin
->w_buffer
;
16005 * "shellescape({string})" function
16008 f_shellescape(argvars
, rettv
)
16012 rettv
->vval
.v_string
= vim_strsave_shellescape(
16013 get_tv_string(&argvars
[0]), non_zero_arg(&argvars
[1]));
16014 rettv
->v_type
= VAR_STRING
;
16018 * "simplify()" function
16021 f_simplify(argvars
, rettv
)
16027 p
= get_tv_string(&argvars
[0]);
16028 rettv
->vval
.v_string
= vim_strsave(p
);
16029 simplify_filename(rettv
->vval
.v_string
); /* simplify in place */
16030 rettv
->v_type
= VAR_STRING
;
16038 f_sin(argvars
, rettv
)
16044 rettv
->v_type
= VAR_FLOAT
;
16045 if (get_float_arg(argvars
, &f
) == OK
)
16046 rettv
->vval
.v_float
= sin(f
);
16048 rettv
->vval
.v_float
= 0.0;
16053 #ifdef __BORLANDC__
16056 item_compare
__ARGS((const void *s1
, const void *s2
));
16058 #ifdef __BORLANDC__
16061 item_compare2
__ARGS((const void *s1
, const void *s2
));
16063 static int item_compare_ic
;
16064 static char_u
*item_compare_func
;
16065 static int item_compare_func_err
;
16066 #define ITEM_COMPARE_FAIL 999
16069 * Compare functions for f_sort() below.
16072 #ifdef __BORLANDC__
16075 item_compare(s1
, s2
)
16080 char_u
*tofree1
, *tofree2
;
16082 char_u numbuf1
[NUMBUFLEN
];
16083 char_u numbuf2
[NUMBUFLEN
];
16085 p1
= tv2string(&(*(listitem_T
**)s1
)->li_tv
, &tofree1
, numbuf1
, 0);
16086 p2
= tv2string(&(*(listitem_T
**)s2
)->li_tv
, &tofree2
, numbuf2
, 0);
16091 if (item_compare_ic
)
16092 res
= STRICMP(p1
, p2
);
16094 res
= STRCMP(p1
, p2
);
16101 #ifdef __BORLANDC__
16104 item_compare2(s1
, s2
)
16113 /* shortcut after failure in previous call; compare all items equal */
16114 if (item_compare_func_err
)
16117 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16118 * in the copy without changing the original list items. */
16119 copy_tv(&(*(listitem_T
**)s1
)->li_tv
, &argv
[0]);
16120 copy_tv(&(*(listitem_T
**)s2
)->li_tv
, &argv
[1]);
16122 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
16123 res
= call_func(item_compare_func
, (int)STRLEN(item_compare_func
),
16124 &rettv
, 2, argv
, 0L, 0L, &dummy
, TRUE
, NULL
);
16125 clear_tv(&argv
[0]);
16126 clear_tv(&argv
[1]);
16129 res
= ITEM_COMPARE_FAIL
;
16131 res
= get_tv_number_chk(&rettv
, &item_compare_func_err
);
16132 if (item_compare_func_err
)
16133 res
= ITEM_COMPARE_FAIL
; /* return value has wrong type */
16139 * "sort({list})" function
16142 f_sort(argvars
, rettv
)
16152 rettv
->vval
.v_number
= 0;
16153 if (argvars
[0].v_type
!= VAR_LIST
)
16154 EMSG2(_(e_listarg
), "sort()");
16157 l
= argvars
[0].vval
.v_list
;
16158 if (l
== NULL
|| tv_check_lock(l
->lv_lock
, (char_u
*)"sort()"))
16160 rettv
->vval
.v_list
= l
;
16161 rettv
->v_type
= VAR_LIST
;
16166 return; /* short list sorts pretty quickly */
16168 item_compare_ic
= FALSE
;
16169 item_compare_func
= NULL
;
16170 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16172 if (argvars
[1].v_type
== VAR_FUNC
)
16173 item_compare_func
= argvars
[1].vval
.v_string
;
16178 i
= get_tv_number_chk(&argvars
[1], &error
);
16180 return; /* type error; errmsg already given */
16182 item_compare_ic
= TRUE
;
16184 item_compare_func
= get_tv_string(&argvars
[1]);
16188 /* Make an array with each entry pointing to an item in the List. */
16189 ptrs
= (listitem_T
**)alloc((int)(len
* sizeof(listitem_T
*)));
16193 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
16196 item_compare_func_err
= FALSE
;
16197 /* test the compare function */
16198 if (item_compare_func
!= NULL
16199 && item_compare2((void *)&ptrs
[0], (void *)&ptrs
[1])
16200 == ITEM_COMPARE_FAIL
)
16201 EMSG(_("E702: Sort compare function failed"));
16204 /* Sort the array with item pointers. */
16205 qsort((void *)ptrs
, (size_t)len
, sizeof(listitem_T
*),
16206 item_compare_func
== NULL
? item_compare
: item_compare2
);
16208 if (!item_compare_func_err
)
16210 /* Clear the List and append the items in the sorted order. */
16211 l
->lv_first
= l
->lv_last
= l
->lv_idx_item
= NULL
;
16213 for (i
= 0; i
< len
; ++i
)
16214 list_append(l
, ptrs
[i
]);
16223 * "soundfold({word})" function
16226 f_soundfold(argvars
, rettv
)
16232 rettv
->v_type
= VAR_STRING
;
16233 s
= get_tv_string(&argvars
[0]);
16235 rettv
->vval
.v_string
= eval_soundfold(s
);
16237 rettv
->vval
.v_string
= vim_strsave(s
);
16242 * "spellbadword()" function
16246 f_spellbadword(argvars
, rettv
)
16250 char_u
*word
= (char_u
*)"";
16251 hlf_T attr
= HLF_COUNT
;
16254 if (rettv_list_alloc(rettv
) == FAIL
)
16258 if (argvars
[0].v_type
== VAR_UNKNOWN
)
16260 /* Find the start and length of the badly spelled word. */
16261 len
= spell_move_to(curwin
, FORWARD
, TRUE
, TRUE
, &attr
);
16263 word
= ml_get_cursor();
16265 else if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
16267 char_u
*str
= get_tv_string_chk(&argvars
[0]);
16272 /* Check the argument for spelling. */
16273 while (*str
!= NUL
)
16275 len
= spell_check(curwin
, str
, &attr
, &capcol
, FALSE
);
16276 if (attr
!= HLF_COUNT
)
16287 list_append_string(rettv
->vval
.v_list
, word
, len
);
16288 list_append_string(rettv
->vval
.v_list
, (char_u
*)(
16289 attr
== HLF_SPB
? "bad" :
16290 attr
== HLF_SPR
? "rare" :
16291 attr
== HLF_SPL
? "local" :
16292 attr
== HLF_SPC
? "caps" :
16297 * "spellsuggest()" function
16301 f_spellsuggest(argvars
, rettv
)
16307 int typeerr
= FALSE
;
16312 int need_capital
= FALSE
;
16315 if (rettv_list_alloc(rettv
) == FAIL
)
16319 if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
16321 str
= get_tv_string(&argvars
[0]);
16322 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16324 maxcount
= get_tv_number_chk(&argvars
[1], &typeerr
);
16327 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16329 need_capital
= get_tv_number_chk(&argvars
[2], &typeerr
);
16337 spell_suggest_list(&ga
, str
, maxcount
, need_capital
, FALSE
);
16339 for (i
= 0; i
< ga
.ga_len
; ++i
)
16341 str
= ((char_u
**)ga
.ga_data
)[i
];
16343 li
= listitem_alloc();
16348 li
->li_tv
.v_type
= VAR_STRING
;
16349 li
->li_tv
.v_lock
= 0;
16350 li
->li_tv
.vval
.v_string
= str
;
16351 list_append(rettv
->vval
.v_list
, li
);
16360 f_split(argvars
, rettv
)
16366 char_u
*pat
= NULL
;
16367 regmatch_T regmatch
;
16368 char_u patbuf
[NUMBUFLEN
];
16372 int keepempty
= FALSE
;
16373 int typeerr
= FALSE
;
16375 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16377 p_cpo
= (char_u
*)"";
16379 str
= get_tv_string(&argvars
[0]);
16380 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16382 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
16385 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16386 keepempty
= get_tv_number_chk(&argvars
[2], &typeerr
);
16388 if (pat
== NULL
|| *pat
== NUL
)
16389 pat
= (char_u
*)"[\\x01- ]\\+";
16391 if (rettv_list_alloc(rettv
) == FAIL
)
16396 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
16397 if (regmatch
.regprog
!= NULL
)
16399 regmatch
.rm_ic
= FALSE
;
16400 while (*str
!= NUL
|| keepempty
)
16403 match
= FALSE
; /* empty item at the end */
16405 match
= vim_regexec_nl(®match
, str
, col
);
16407 end
= regmatch
.startp
[0];
16409 end
= str
+ STRLEN(str
);
16410 if (keepempty
|| end
> str
|| (rettv
->vval
.v_list
->lv_len
> 0
16411 && *str
!= NUL
&& match
&& end
< regmatch
.endp
[0]))
16413 if (list_append_string(rettv
->vval
.v_list
, str
,
16414 (int)(end
- str
)) == FAIL
)
16419 /* Advance to just after the match. */
16420 if (regmatch
.endp
[0] > str
)
16424 /* Don't get stuck at the same match. */
16426 col
= (*mb_ptr2len
)(regmatch
.endp
[0]);
16431 str
= regmatch
.endp
[0];
16434 vim_free(regmatch
.regprog
);
16442 * "sqrt()" function
16445 f_sqrt(argvars
, rettv
)
16451 rettv
->v_type
= VAR_FLOAT
;
16452 if (get_float_arg(argvars
, &f
) == OK
)
16453 rettv
->vval
.v_float
= sqrt(f
);
16455 rettv
->vval
.v_float
= 0.0;
16459 * "str2float()" function
16462 f_str2float(argvars
, rettv
)
16466 char_u
*p
= skipwhite(get_tv_string(&argvars
[0]));
16469 p
= skipwhite(p
+ 1);
16470 (void)string2float(p
, &rettv
->vval
.v_float
);
16471 rettv
->v_type
= VAR_FLOAT
;
16476 * "str2nr()" function
16479 f_str2nr(argvars
, rettv
)
16487 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16489 base
= get_tv_number(&argvars
[1]);
16490 if (base
!= 8 && base
!= 10 && base
!= 16)
16497 p
= skipwhite(get_tv_string(&argvars
[0]));
16499 p
= skipwhite(p
+ 1);
16500 vim_str2nr(p
, NULL
, NULL
, base
== 8 ? 2 : 0, base
== 16 ? 2 : 0, &n
, NULL
);
16501 rettv
->vval
.v_number
= n
;
16504 #ifdef HAVE_STRFTIME
16506 * "strftime({format}[, {time}])" function
16509 f_strftime(argvars
, rettv
)
16513 char_u result_buf
[256];
16514 struct tm
*curtime
;
16518 rettv
->v_type
= VAR_STRING
;
16520 p
= get_tv_string(&argvars
[0]);
16521 if (argvars
[1].v_type
== VAR_UNKNOWN
)
16522 seconds
= time(NULL
);
16524 seconds
= (time_t)get_tv_number(&argvars
[1]);
16525 curtime
= localtime(&seconds
);
16526 /* MSVC returns NULL for an invalid value of seconds. */
16527 if (curtime
== NULL
)
16528 rettv
->vval
.v_string
= vim_strsave((char_u
*)_("(Invalid)"));
16535 conv
.vc_type
= CONV_NONE
;
16536 enc
= enc_locale();
16537 convert_setup(&conv
, p_enc
, enc
);
16538 if (conv
.vc_type
!= CONV_NONE
)
16539 p
= string_convert(&conv
, p
, NULL
);
16542 (void)strftime((char *)result_buf
, sizeof(result_buf
),
16543 (char *)p
, curtime
);
16545 result_buf
[0] = NUL
;
16548 if (conv
.vc_type
!= CONV_NONE
)
16550 convert_setup(&conv
, enc
, p_enc
);
16551 if (conv
.vc_type
!= CONV_NONE
)
16552 rettv
->vval
.v_string
= string_convert(&conv
, result_buf
, NULL
);
16555 rettv
->vval
.v_string
= vim_strsave(result_buf
);
16558 /* Release conversion descriptors */
16559 convert_setup(&conv
, NULL
, NULL
);
16567 * "stridx()" function
16570 f_stridx(argvars
, rettv
)
16574 char_u buf
[NUMBUFLEN
];
16577 char_u
*save_haystack
;
16581 needle
= get_tv_string_chk(&argvars
[1]);
16582 save_haystack
= haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
16583 rettv
->vval
.v_number
= -1;
16584 if (needle
== NULL
|| haystack
== NULL
)
16585 return; /* type error; errmsg already given */
16587 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16591 start_idx
= get_tv_number_chk(&argvars
[2], &error
);
16592 if (error
|| start_idx
>= (int)STRLEN(haystack
))
16594 if (start_idx
>= 0)
16595 haystack
+= start_idx
;
16598 pos
= (char_u
*)strstr((char *)haystack
, (char *)needle
);
16600 rettv
->vval
.v_number
= (varnumber_T
)(pos
- save_haystack
);
16604 * "string()" function
16607 f_string(argvars
, rettv
)
16612 char_u numbuf
[NUMBUFLEN
];
16614 rettv
->v_type
= VAR_STRING
;
16615 rettv
->vval
.v_string
= tv2string(&argvars
[0], &tofree
, numbuf
, 0);
16616 /* Make a copy if we have a value but it's not in allocated memory. */
16617 if (rettv
->vval
.v_string
!= NULL
&& tofree
== NULL
)
16618 rettv
->vval
.v_string
= vim_strsave(rettv
->vval
.v_string
);
16622 * "strlen()" function
16625 f_strlen(argvars
, rettv
)
16629 rettv
->vval
.v_number
= (varnumber_T
)(STRLEN(
16630 get_tv_string(&argvars
[0])));
16634 * "strpart()" function
16637 f_strpart(argvars
, rettv
)
16647 p
= get_tv_string(&argvars
[0]);
16648 slen
= (int)STRLEN(p
);
16650 n
= get_tv_number_chk(&argvars
[1], &error
);
16653 else if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16654 len
= get_tv_number(&argvars
[2]);
16656 len
= slen
- n
; /* default len: all bytes that are available. */
16659 * Only return the overlap between the specified part and the actual
16671 else if (n
+ len
> slen
)
16674 rettv
->v_type
= VAR_STRING
;
16675 rettv
->vval
.v_string
= vim_strnsave(p
+ n
, len
);
16679 * "strridx()" function
16682 f_strridx(argvars
, rettv
)
16686 char_u buf
[NUMBUFLEN
];
16690 char_u
*lastmatch
= NULL
;
16691 int haystack_len
, end_idx
;
16693 needle
= get_tv_string_chk(&argvars
[1]);
16694 haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
16696 rettv
->vval
.v_number
= -1;
16697 if (needle
== NULL
|| haystack
== NULL
)
16698 return; /* type error; errmsg already given */
16700 haystack_len
= (int)STRLEN(haystack
);
16701 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16703 /* Third argument: upper limit for index */
16704 end_idx
= get_tv_number_chk(&argvars
[2], NULL
);
16706 return; /* can never find a match */
16709 end_idx
= haystack_len
;
16711 if (*needle
== NUL
)
16713 /* Empty string matches past the end. */
16714 lastmatch
= haystack
+ end_idx
;
16718 for (rest
= haystack
; *rest
!= '\0'; ++rest
)
16720 rest
= (char_u
*)strstr((char *)rest
, (char *)needle
);
16721 if (rest
== NULL
|| rest
> haystack
+ end_idx
)
16727 if (lastmatch
== NULL
)
16728 rettv
->vval
.v_number
= -1;
16730 rettv
->vval
.v_number
= (varnumber_T
)(lastmatch
- haystack
);
16734 * "strtrans()" function
16737 f_strtrans(argvars
, rettv
)
16741 rettv
->v_type
= VAR_STRING
;
16742 rettv
->vval
.v_string
= transstr(get_tv_string(&argvars
[0]));
16746 * "submatch()" function
16749 f_submatch(argvars
, rettv
)
16753 rettv
->v_type
= VAR_STRING
;
16754 rettv
->vval
.v_string
=
16755 reg_submatch((int)get_tv_number_chk(&argvars
[0], NULL
));
16759 * "substitute()" function
16762 f_substitute(argvars
, rettv
)
16766 char_u patbuf
[NUMBUFLEN
];
16767 char_u subbuf
[NUMBUFLEN
];
16768 char_u flagsbuf
[NUMBUFLEN
];
16770 char_u
*str
= get_tv_string_chk(&argvars
[0]);
16771 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
16772 char_u
*sub
= get_tv_string_buf_chk(&argvars
[2], subbuf
);
16773 char_u
*flg
= get_tv_string_buf_chk(&argvars
[3], flagsbuf
);
16775 rettv
->v_type
= VAR_STRING
;
16776 if (str
== NULL
|| pat
== NULL
|| sub
== NULL
|| flg
== NULL
)
16777 rettv
->vval
.v_string
= NULL
;
16779 rettv
->vval
.v_string
= do_string_sub(str
, pat
, sub
, flg
);
16783 * "synID(lnum, col, trans)" function
16787 f_synID(argvars
, rettv
)
16796 int transerr
= FALSE
;
16798 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
16799 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
16800 trans
= get_tv_number_chk(&argvars
[2], &transerr
);
16802 if (!transerr
&& lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
16803 && col
>= 0 && col
< (long)STRLEN(ml_get(lnum
)))
16804 id
= syn_get_id(curwin
, lnum
, (colnr_T
)col
, trans
, NULL
, FALSE
);
16807 rettv
->vval
.v_number
= id
;
16811 * "synIDattr(id, what [, mode])" function
16815 f_synIDattr(argvars
, rettv
)
16824 char_u modebuf
[NUMBUFLEN
];
16827 id
= get_tv_number(&argvars
[0]);
16828 what
= get_tv_string(&argvars
[1]);
16829 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16831 mode
= get_tv_string_buf(&argvars
[2], modebuf
);
16832 modec
= TOLOWER_ASC(mode
[0]);
16833 if (modec
!= 't' && modec
!= 'c'
16838 modec
= 0; /* replace invalid with current */
16854 switch (TOLOWER_ASC(what
[0]))
16857 if (TOLOWER_ASC(what
[1]) == 'g') /* bg[#] */
16858 p
= highlight_color(id
, what
, modec
);
16860 p
= highlight_has_attr(id
, HL_BOLD
, modec
);
16863 case 'f': /* fg[#] */
16864 p
= highlight_color(id
, what
, modec
);
16868 if (TOLOWER_ASC(what
[1]) == 'n') /* inverse */
16869 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
16871 p
= highlight_has_attr(id
, HL_ITALIC
, modec
);
16874 case 'n': /* name */
16875 p
= get_highlight_name(NULL
, id
- 1);
16878 case 'r': /* reverse */
16879 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
16883 if (TOLOWER_ASC(what
[1]) == 'p') /* sp[#] */
16884 p
= highlight_color(id
, what
, modec
);
16885 else /* standout */
16886 p
= highlight_has_attr(id
, HL_STANDOUT
, modec
);
16890 if (STRLEN(what
) <= 5 || TOLOWER_ASC(what
[5]) != 'c')
16892 p
= highlight_has_attr(id
, HL_UNDERLINE
, modec
);
16895 p
= highlight_has_attr(id
, HL_UNDERCURL
, modec
);
16900 p
= vim_strsave(p
);
16902 rettv
->v_type
= VAR_STRING
;
16903 rettv
->vval
.v_string
= p
;
16907 * "synIDtrans(id)" function
16911 f_synIDtrans(argvars
, rettv
)
16918 id
= get_tv_number(&argvars
[0]);
16921 id
= syn_get_final_id(id
);
16926 rettv
->vval
.v_number
= id
;
16930 * "synstack(lnum, col)" function
16934 f_synstack(argvars
, rettv
)
16945 rettv
->v_type
= VAR_LIST
;
16946 rettv
->vval
.v_list
= NULL
;
16949 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
16950 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
16952 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
16953 && col
>= 0 && (col
== 0 || col
< (long)STRLEN(ml_get(lnum
)))
16954 && rettv_list_alloc(rettv
) != FAIL
)
16956 (void)syn_get_id(curwin
, lnum
, (colnr_T
)col
, FALSE
, NULL
, TRUE
);
16959 id
= syn_get_stack_item(i
);
16962 if (list_append_number(rettv
->vval
.v_list
, id
) == FAIL
)
16970 * "system()" function
16973 f_system(argvars
, rettv
)
16977 char_u
*res
= NULL
;
16979 char_u
*infile
= NULL
;
16980 char_u buf
[NUMBUFLEN
];
16984 if (check_restricted() || check_secure())
16987 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16990 * Write the string to a temp file, to be used for input of the shell
16993 if ((infile
= vim_tempname('i')) == NULL
)
16999 fd
= mch_fopen((char *)infile
, WRITEBIN
);
17002 EMSG2(_(e_notopen
), infile
);
17005 p
= get_tv_string_buf_chk(&argvars
[1], buf
);
17009 goto done
; /* type error; errmsg already given */
17011 if (fwrite(p
, STRLEN(p
), 1, fd
) != 1)
17013 if (fclose(fd
) != 0)
17017 EMSG(_("E677: Error writing temp file"));
17022 res
= get_cmd_output(get_tv_string(&argvars
[0]), infile
,
17023 SHELL_SILENT
| SHELL_COOKED
);
17026 /* translate <CR> into <NL> */
17031 for (s
= res
; *s
; ++s
)
17039 /* translate <CR><NL> into <NL> */
17045 for (s
= res
; *s
; ++s
)
17047 if (s
[0] == CAR
&& s
[1] == NL
)
17057 if (infile
!= NULL
)
17059 mch_remove(infile
);
17062 rettv
->v_type
= VAR_STRING
;
17063 rettv
->vval
.v_string
= res
;
17067 * "tabpagebuflist()" function
17071 f_tabpagebuflist(argvars
, rettv
)
17075 #ifndef FEAT_WINDOWS
17076 rettv
->vval
.v_number
= 0;
17081 if (argvars
[0].v_type
== VAR_UNKNOWN
)
17085 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
17087 wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
17090 rettv
->vval
.v_number
= 0;
17093 if (rettv_list_alloc(rettv
) == FAIL
)
17094 rettv
->vval
.v_number
= 0;
17097 for (; wp
!= NULL
; wp
= wp
->w_next
)
17098 if (list_append_number(rettv
->vval
.v_list
,
17099 wp
->w_buffer
->b_fnum
) == FAIL
)
17108 * "tabpagenr()" function
17112 f_tabpagenr(argvars
, rettv
)
17117 #ifdef FEAT_WINDOWS
17120 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
17122 arg
= get_tv_string_chk(&argvars
[0]);
17126 if (STRCMP(arg
, "$") == 0)
17127 nr
= tabpage_index(NULL
) - 1;
17129 EMSG2(_(e_invexpr2
), arg
);
17133 nr
= tabpage_index(curtab
);
17135 rettv
->vval
.v_number
= nr
;
17139 #ifdef FEAT_WINDOWS
17140 static int get_winnr
__ARGS((tabpage_T
*tp
, typval_T
*argvar
));
17143 * Common code for tabpagewinnr() and winnr().
17146 get_winnr(tp
, argvar
)
17155 twin
= (tp
== curtab
) ? curwin
: tp
->tp_curwin
;
17156 if (argvar
->v_type
!= VAR_UNKNOWN
)
17158 arg
= get_tv_string_chk(argvar
);
17160 nr
= 0; /* type error; errmsg already given */
17161 else if (STRCMP(arg
, "$") == 0)
17162 twin
= (tp
== curtab
) ? lastwin
: tp
->tp_lastwin
;
17163 else if (STRCMP(arg
, "#") == 0)
17165 twin
= (tp
== curtab
) ? prevwin
: tp
->tp_prevwin
;
17171 EMSG2(_(e_invexpr2
), arg
);
17177 for (wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
17178 wp
!= twin
; wp
= wp
->w_next
)
17182 /* didn't find it in this tabpage */
17193 * "tabpagewinnr()" function
17197 f_tabpagewinnr(argvars
, rettv
)
17202 #ifdef FEAT_WINDOWS
17205 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
17209 nr
= get_winnr(tp
, &argvars
[1]);
17211 rettv
->vval
.v_number
= nr
;
17216 * "tagfiles()" function
17220 f_tagfiles(argvars
, rettv
)
17224 char_u fname
[MAXPATHL
+ 1];
17228 if (rettv_list_alloc(rettv
) == FAIL
)
17230 rettv
->vval
.v_number
= 0;
17234 for (first
= TRUE
; ; first
= FALSE
)
17235 if (get_tagfname(&tn
, first
, fname
) == FAIL
17236 || list_append_string(rettv
->vval
.v_list
, fname
, -1) == FAIL
)
17242 * "taglist()" function
17245 f_taglist(argvars
, rettv
)
17249 char_u
*tag_pattern
;
17251 tag_pattern
= get_tv_string(&argvars
[0]);
17253 rettv
->vval
.v_number
= FALSE
;
17254 if (*tag_pattern
== NUL
)
17257 if (rettv_list_alloc(rettv
) == OK
)
17258 (void)get_tags(rettv
->vval
.v_list
, tag_pattern
);
17262 * "tempname()" function
17266 f_tempname(argvars
, rettv
)
17270 static int x
= 'A';
17272 rettv
->v_type
= VAR_STRING
;
17273 rettv
->vval
.v_string
= vim_tempname(x
);
17275 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17276 * names. Skip 'I' and 'O', they are used for shell redirection. */
17294 } while (x
== 'I' || x
== 'O');
17298 * "test(list)" function: Just checking the walls...
17302 f_test(argvars
, rettv
)
17306 /* Used for unit testing. Change the code below to your liking. */
17310 char_u
*bad
, *good
;
17312 if (argvars
[0].v_type
!= VAR_LIST
)
17314 l
= argvars
[0].vval
.v_list
;
17320 bad
= get_tv_string(&li
->li_tv
);
17324 good
= get_tv_string(&li
->li_tv
);
17325 rettv
->vval
.v_number
= test_edit_score(bad
, good
);
17330 * "tolower(string)" function
17333 f_tolower(argvars
, rettv
)
17339 p
= vim_strsave(get_tv_string(&argvars
[0]));
17340 rettv
->v_type
= VAR_STRING
;
17341 rettv
->vval
.v_string
= p
;
17353 c
= utf_ptr2char(p
);
17354 lc
= utf_tolower(c
);
17355 l
= utf_ptr2len(p
);
17356 /* TODO: reallocate string when byte count changes. */
17357 if (utf_char2len(lc
) == l
)
17358 utf_char2bytes(lc
, p
);
17361 else if (has_mbyte
&& (l
= (*mb_ptr2len
)(p
)) > 1)
17362 p
+= l
; /* skip multi-byte character */
17366 *p
= TOLOWER_LOC(*p
); /* note that tolower() can be a macro */
17373 * "toupper(string)" function
17376 f_toupper(argvars
, rettv
)
17380 rettv
->v_type
= VAR_STRING
;
17381 rettv
->vval
.v_string
= strup_save(get_tv_string(&argvars
[0]));
17385 * "tr(string, fromstr, tostr)" function
17388 f_tr(argvars
, rettv
)
17405 char_u buf
[NUMBUFLEN
];
17406 char_u buf2
[NUMBUFLEN
];
17409 instr
= get_tv_string(&argvars
[0]);
17410 fromstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
17411 tostr
= get_tv_string_buf_chk(&argvars
[2], buf2
);
17413 /* Default return value: empty string. */
17414 rettv
->v_type
= VAR_STRING
;
17415 rettv
->vval
.v_string
= NULL
;
17416 if (fromstr
== NULL
|| tostr
== NULL
)
17417 return; /* type error; errmsg already given */
17418 ga_init2(&ga
, (int)sizeof(char), 80);
17423 /* not multi-byte: fromstr and tostr must be the same length */
17424 if (STRLEN(fromstr
) != STRLEN(tostr
))
17429 EMSG2(_(e_invarg2
), fromstr
);
17434 /* fromstr and tostr have to contain the same number of chars */
17435 while (*instr
!= NUL
)
17440 inlen
= (*mb_ptr2len
)(instr
);
17444 for (p
= fromstr
; *p
!= NUL
; p
+= fromlen
)
17446 fromlen
= (*mb_ptr2len
)(p
);
17447 if (fromlen
== inlen
&& STRNCMP(instr
, p
, inlen
) == 0)
17449 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
17451 tolen
= (*mb_ptr2len
)(p
);
17459 if (*p
== NUL
) /* tostr is shorter than fromstr */
17466 if (first
&& cpstr
== instr
)
17468 /* Check that fromstr and tostr have the same number of
17469 * (multi-byte) characters. Done only once when a character
17470 * of instr doesn't appear in fromstr. */
17472 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
17474 tolen
= (*mb_ptr2len
)(p
);
17481 ga_grow(&ga
, cplen
);
17482 mch_memmove((char *)ga
.ga_data
+ ga
.ga_len
, cpstr
, (size_t)cplen
);
17483 ga
.ga_len
+= cplen
;
17490 /* When not using multi-byte chars we can do it faster. */
17491 p
= vim_strchr(fromstr
, *instr
);
17493 ga_append(&ga
, tostr
[p
- fromstr
]);
17495 ga_append(&ga
, *instr
);
17500 /* add a terminating NUL */
17502 ga_append(&ga
, NUL
);
17504 rettv
->vval
.v_string
= ga
.ga_data
;
17509 * "trunc({float})" function
17512 f_trunc(argvars
, rettv
)
17518 rettv
->v_type
= VAR_FLOAT
;
17519 if (get_float_arg(argvars
, &f
) == OK
)
17520 /* trunc() is not in C90, use floor() or ceil() instead. */
17521 rettv
->vval
.v_float
= f
> 0 ? floor(f
) : ceil(f
);
17523 rettv
->vval
.v_float
= 0.0;
17528 * "type(expr)" function
17531 f_type(argvars
, rettv
)
17537 switch (argvars
[0].v_type
)
17539 case VAR_NUMBER
: n
= 0; break;
17540 case VAR_STRING
: n
= 1; break;
17541 case VAR_FUNC
: n
= 2; break;
17542 case VAR_LIST
: n
= 3; break;
17543 case VAR_DICT
: n
= 4; break;
17545 case VAR_FLOAT
: n
= 5; break;
17547 default: EMSG2(_(e_intern2
), "f_type()"); n
= 0; break;
17549 rettv
->vval
.v_number
= n
;
17553 * "values(dict)" function
17556 f_values(argvars
, rettv
)
17560 dict_list(argvars
, rettv
, 1);
17564 * "virtcol(string)" function
17567 f_virtcol(argvars
, rettv
)
17573 int fnum
= curbuf
->b_fnum
;
17575 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
17576 if (fp
!= NULL
&& fp
->lnum
<= curbuf
->b_ml
.ml_line_count
17577 && fnum
== curbuf
->b_fnum
)
17579 getvvcol(curwin
, fp
, NULL
, NULL
, &vcol
);
17583 rettv
->vval
.v_number
= vcol
;
17587 * "visualmode()" function
17591 f_visualmode(argvars
, rettv
)
17598 rettv
->v_type
= VAR_STRING
;
17599 str
[0] = curbuf
->b_visual_mode_eval
;
17601 rettv
->vval
.v_string
= vim_strsave(str
);
17603 /* A non-zero number or non-empty string argument: reset mode. */
17604 if (non_zero_arg(&argvars
[0]))
17605 curbuf
->b_visual_mode_eval
= NUL
;
17607 rettv
->vval
.v_number
= 0; /* return anything, it won't work anyway */
17612 * "winbufnr(nr)" function
17615 f_winbufnr(argvars
, rettv
)
17621 wp
= find_win_by_nr(&argvars
[0], NULL
);
17623 rettv
->vval
.v_number
= -1;
17625 rettv
->vval
.v_number
= wp
->w_buffer
->b_fnum
;
17629 * "wincol()" function
17633 f_wincol(argvars
, rettv
)
17638 rettv
->vval
.v_number
= curwin
->w_wcol
+ 1;
17642 * "winheight(nr)" function
17645 f_winheight(argvars
, rettv
)
17651 wp
= find_win_by_nr(&argvars
[0], NULL
);
17653 rettv
->vval
.v_number
= -1;
17655 rettv
->vval
.v_number
= wp
->w_height
;
17659 * "winline()" function
17663 f_winline(argvars
, rettv
)
17668 rettv
->vval
.v_number
= curwin
->w_wrow
+ 1;
17672 * "winnr()" function
17676 f_winnr(argvars
, rettv
)
17682 #ifdef FEAT_WINDOWS
17683 nr
= get_winnr(curtab
, &argvars
[0]);
17685 rettv
->vval
.v_number
= nr
;
17689 * "winrestcmd()" function
17693 f_winrestcmd(argvars
, rettv
)
17697 #ifdef FEAT_WINDOWS
17703 ga_init2(&ga
, (int)sizeof(char), 70);
17704 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
17706 sprintf((char *)buf
, "%dresize %d|", winnr
, wp
->w_height
);
17707 ga_concat(&ga
, buf
);
17708 # ifdef FEAT_VERTSPLIT
17709 sprintf((char *)buf
, "vert %dresize %d|", winnr
, wp
->w_width
);
17710 ga_concat(&ga
, buf
);
17714 ga_append(&ga
, NUL
);
17716 rettv
->vval
.v_string
= ga
.ga_data
;
17718 rettv
->vval
.v_string
= NULL
;
17720 rettv
->v_type
= VAR_STRING
;
17724 * "winrestview()" function
17728 f_winrestview(argvars
, rettv
)
17734 if (argvars
[0].v_type
!= VAR_DICT
17735 || (dict
= argvars
[0].vval
.v_dict
) == NULL
)
17739 curwin
->w_cursor
.lnum
= get_dict_number(dict
, (char_u
*)"lnum");
17740 curwin
->w_cursor
.col
= get_dict_number(dict
, (char_u
*)"col");
17741 #ifdef FEAT_VIRTUALEDIT
17742 curwin
->w_cursor
.coladd
= get_dict_number(dict
, (char_u
*)"coladd");
17744 curwin
->w_curswant
= get_dict_number(dict
, (char_u
*)"curswant");
17745 curwin
->w_set_curswant
= FALSE
;
17747 set_topline(curwin
, get_dict_number(dict
, (char_u
*)"topline"));
17749 curwin
->w_topfill
= get_dict_number(dict
, (char_u
*)"topfill");
17751 curwin
->w_leftcol
= get_dict_number(dict
, (char_u
*)"leftcol");
17752 curwin
->w_skipcol
= get_dict_number(dict
, (char_u
*)"skipcol");
17755 changed_cline_bef_curs();
17756 invalidate_botline();
17757 redraw_later(VALID
);
17759 if (curwin
->w_topline
== 0)
17760 curwin
->w_topline
= 1;
17761 if (curwin
->w_topline
> curbuf
->b_ml
.ml_line_count
)
17762 curwin
->w_topline
= curbuf
->b_ml
.ml_line_count
;
17764 check_topfill(curwin
, TRUE
);
17770 * "winsaveview()" function
17774 f_winsaveview(argvars
, rettv
)
17780 dict
= dict_alloc();
17783 rettv
->v_type
= VAR_DICT
;
17784 rettv
->vval
.v_dict
= dict
;
17785 ++dict
->dv_refcount
;
17787 dict_add_nr_str(dict
, "lnum", (long)curwin
->w_cursor
.lnum
, NULL
);
17788 dict_add_nr_str(dict
, "col", (long)curwin
->w_cursor
.col
, NULL
);
17789 #ifdef FEAT_VIRTUALEDIT
17790 dict_add_nr_str(dict
, "coladd", (long)curwin
->w_cursor
.coladd
, NULL
);
17793 dict_add_nr_str(dict
, "curswant", (long)curwin
->w_curswant
, NULL
);
17795 dict_add_nr_str(dict
, "topline", (long)curwin
->w_topline
, NULL
);
17797 dict_add_nr_str(dict
, "topfill", (long)curwin
->w_topfill
, NULL
);
17799 dict_add_nr_str(dict
, "leftcol", (long)curwin
->w_leftcol
, NULL
);
17800 dict_add_nr_str(dict
, "skipcol", (long)curwin
->w_skipcol
, NULL
);
17804 * "winwidth(nr)" function
17807 f_winwidth(argvars
, rettv
)
17813 wp
= find_win_by_nr(&argvars
[0], NULL
);
17815 rettv
->vval
.v_number
= -1;
17817 #ifdef FEAT_VERTSPLIT
17818 rettv
->vval
.v_number
= wp
->w_width
;
17820 rettv
->vval
.v_number
= Columns
;
17825 * "writefile()" function
17828 f_writefile(argvars
, rettv
)
17832 int binary
= FALSE
;
17840 if (check_restricted() || check_secure())
17843 if (argvars
[0].v_type
!= VAR_LIST
)
17845 EMSG2(_(e_listarg
), "writefile()");
17848 if (argvars
[0].vval
.v_list
== NULL
)
17851 if (argvars
[2].v_type
!= VAR_UNKNOWN
17852 && STRCMP(get_tv_string(&argvars
[2]), "b") == 0)
17855 /* Always open the file in binary mode, library functions have a mind of
17856 * their own about CR-LF conversion. */
17857 fname
= get_tv_string(&argvars
[1]);
17858 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, WRITEBIN
)) == NULL
)
17860 EMSG2(_(e_notcreate
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
17865 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
;
17868 for (s
= get_tv_string(&li
->li_tv
); *s
!= NUL
; ++s
)
17880 if (!binary
|| li
->li_next
!= NULL
)
17881 if (putc('\n', fd
) == EOF
)
17895 rettv
->vval
.v_number
= ret
;
17899 * Translate a String variable into a position.
17900 * Returns NULL when there is an error.
17903 var2fpos(varp
, dollar_lnum
, fnum
)
17905 int dollar_lnum
; /* TRUE when $ is last line */
17906 int *fnum
; /* set to fnum for '0, 'A, etc. */
17912 /* Argument can be [lnum, col, coladd]. */
17913 if (varp
->v_type
== VAR_LIST
)
17920 l
= varp
->vval
.v_list
;
17924 /* Get the line number */
17925 pos
.lnum
= list_find_nr(l
, 0L, &error
);
17926 if (error
|| pos
.lnum
<= 0 || pos
.lnum
> curbuf
->b_ml
.ml_line_count
)
17927 return NULL
; /* invalid line number */
17929 /* Get the column number */
17930 pos
.col
= list_find_nr(l
, 1L, &error
);
17933 len
= (long)STRLEN(ml_get(pos
.lnum
));
17935 /* We accept "$" for the column number: last column. */
17936 li
= list_find(l
, 1L);
17937 if (li
!= NULL
&& li
->li_tv
.v_type
== VAR_STRING
17938 && li
->li_tv
.vval
.v_string
!= NULL
17939 && STRCMP(li
->li_tv
.vval
.v_string
, "$") == 0)
17942 /* Accept a position up to the NUL after the line. */
17943 if (pos
.col
== 0 || (int)pos
.col
> len
+ 1)
17944 return NULL
; /* invalid column number */
17947 #ifdef FEAT_VIRTUALEDIT
17948 /* Get the virtual offset. Defaults to zero. */
17949 pos
.coladd
= list_find_nr(l
, 2L, &error
);
17957 name
= get_tv_string_chk(varp
);
17960 if (name
[0] == '.') /* cursor */
17961 return &curwin
->w_cursor
;
17963 if (name
[0] == 'v' && name
[1] == NUL
) /* Visual start */
17967 return &curwin
->w_cursor
;
17970 if (name
[0] == '\'') /* mark */
17972 pp
= getmark_fnum(name
[1], FALSE
, fnum
);
17973 if (pp
== NULL
|| pp
== (pos_T
*)-1 || pp
->lnum
<= 0)
17978 #ifdef FEAT_VIRTUALEDIT
17982 if (name
[0] == 'w' && dollar_lnum
)
17985 if (name
[1] == '0') /* "w0": first visible line */
17988 pos
.lnum
= curwin
->w_topline
;
17991 else if (name
[1] == '$') /* "w$": last visible line */
17993 validate_botline();
17994 pos
.lnum
= curwin
->w_botline
- 1;
17998 else if (name
[0] == '$') /* last column or line */
18002 pos
.lnum
= curbuf
->b_ml
.ml_line_count
;
18007 pos
.lnum
= curwin
->w_cursor
.lnum
;
18008 pos
.col
= (colnr_T
)STRLEN(ml_get_curline());
18016 * Convert list in "arg" into a position and optional file number.
18017 * When "fnump" is NULL there is no file number, only 3 items.
18018 * Note that the column is passed on as-is, the caller may want to decrement
18019 * it to use 1 for the first column.
18020 * Return FAIL when conversion is not possible, doesn't check the position for
18024 list2fpos(arg
, posp
, fnump
)
18029 list_T
*l
= arg
->vval
.v_list
;
18033 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
18034 * when "fnump" isn't NULL and "coladd" is optional. */
18035 if (arg
->v_type
!= VAR_LIST
18037 || l
->lv_len
< (fnump
== NULL
? 2 : 3)
18038 || l
->lv_len
> (fnump
== NULL
? 3 : 4))
18043 n
= list_find_nr(l
, i
++, NULL
); /* fnum */
18047 n
= curbuf
->b_fnum
; /* current buffer */
18051 n
= list_find_nr(l
, i
++, NULL
); /* lnum */
18056 n
= list_find_nr(l
, i
++, NULL
); /* col */
18061 #ifdef FEAT_VIRTUALEDIT
18062 n
= list_find_nr(l
, i
, NULL
);
18073 * Get the length of an environment variable name.
18074 * Advance "arg" to the first character after the name.
18075 * Return 0 for error.
18084 for (p
= *arg
; vim_isIDc(*p
); ++p
)
18086 if (p
== *arg
) /* no name found */
18089 len
= (int)(p
- *arg
);
18095 * Get the length of the name of a function or internal variable.
18096 * "arg" is advanced to the first non-white character after the name.
18097 * Return 0 if something is wrong.
18106 /* Find the end of the name. */
18107 for (p
= *arg
; eval_isnamec(*p
); ++p
)
18109 if (p
== *arg
) /* no name found */
18112 len
= (int)(p
- *arg
);
18113 *arg
= skipwhite(p
);
18119 * Get the length of the name of a variable or function.
18120 * Only the name is recognized, does not handle ".key" or "[idx]".
18121 * "arg" is advanced to the first non-white character after the name.
18122 * Return -1 if curly braces expansion failed.
18123 * Return 0 if something else is wrong.
18124 * If the name contains 'magic' {}'s, expand them and return the
18125 * expanded name in an allocated string via 'alias' - caller must free.
18128 get_name_len(arg
, alias
, evaluate
, verbose
)
18136 char_u
*expr_start
;
18139 *alias
= NULL
; /* default to no alias */
18141 if ((*arg
)[0] == K_SPECIAL
&& (*arg
)[1] == KS_EXTRA
18142 && (*arg
)[2] == (int)KE_SNR
)
18144 /* hard coded <SNR>, already translated */
18146 return get_id_len(arg
) + 3;
18148 len
= eval_fname_script(*arg
);
18151 /* literal "<SID>", "s:" or "<SNR>" */
18156 * Find the end of the name; check for {} construction.
18158 p
= find_name_end(*arg
, &expr_start
, &expr_end
,
18159 len
> 0 ? 0 : FNE_CHECK_START
);
18160 if (expr_start
!= NULL
)
18162 char_u
*temp_string
;
18166 len
+= (int)(p
- *arg
);
18167 *arg
= skipwhite(p
);
18172 * Include any <SID> etc in the expanded string:
18173 * Thus the -len here.
18175 temp_string
= make_expanded_name(*arg
- len
, expr_start
, expr_end
, p
);
18176 if (temp_string
== NULL
)
18178 *alias
= temp_string
;
18179 *arg
= skipwhite(p
);
18180 return (int)STRLEN(temp_string
);
18183 len
+= get_id_len(arg
);
18184 if (len
== 0 && verbose
)
18185 EMSG2(_(e_invexpr2
), *arg
);
18191 * Find the end of a variable or function name, taking care of magic braces.
18192 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18193 * start and end of the first magic braces item.
18194 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
18195 * Return a pointer to just after the name. Equal to "arg" if there is no
18199 find_name_end(arg
, expr_start
, expr_end
, flags
)
18201 char_u
**expr_start
;
18209 if (expr_start
!= NULL
)
18211 *expr_start
= NULL
;
18215 /* Quick check for valid starting character. */
18216 if ((flags
& FNE_CHECK_START
) && !eval_isnamec1(*arg
) && *arg
!= '{')
18219 for (p
= arg
; *p
!= NUL
18220 && (eval_isnamec(*p
)
18222 || ((flags
& FNE_INCL_BR
) && (*p
== '[' || *p
== '.'))
18224 || br_nest
!= 0); mb_ptr_adv(p
))
18228 /* skip over 'string' to avoid counting [ and ] inside it. */
18229 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '\''; mb_ptr_adv(p
))
18234 else if (*p
== '"')
18236 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18237 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
18238 if (*p
== '\\' && p
[1] != NUL
)
18248 else if (*p
== ']')
18257 if (expr_start
!= NULL
&& *expr_start
== NULL
)
18260 else if (*p
== '}')
18263 if (expr_start
!= NULL
&& mb_nest
== 0 && *expr_end
== NULL
)
18273 * Expands out the 'magic' {}'s in a variable/function name.
18274 * Note that this can call itself recursively, to deal with
18275 * constructs like foo{bar}{baz}{bam}
18276 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18282 * Returns a new allocated string, which the caller must free.
18283 * Returns NULL for failure.
18286 make_expanded_name(in_start
, expr_start
, expr_end
, in_end
)
18288 char_u
*expr_start
;
18293 char_u
*retval
= NULL
;
18294 char_u
*temp_result
;
18295 char_u
*nextcmd
= NULL
;
18297 if (expr_end
== NULL
|| in_end
== NULL
)
18304 temp_result
= eval_to_string(expr_start
+ 1, &nextcmd
, FALSE
);
18305 if (temp_result
!= NULL
&& nextcmd
== NULL
)
18307 retval
= alloc((unsigned)(STRLEN(temp_result
) + (expr_start
- in_start
)
18308 + (in_end
- expr_end
) + 1));
18309 if (retval
!= NULL
)
18311 STRCPY(retval
, in_start
);
18312 STRCAT(retval
, temp_result
);
18313 STRCAT(retval
, expr_end
+ 1);
18316 vim_free(temp_result
);
18318 *in_end
= c1
; /* put char back for error messages */
18322 if (retval
!= NULL
)
18324 temp_result
= find_name_end(retval
, &expr_start
, &expr_end
, 0);
18325 if (expr_start
!= NULL
)
18327 /* Further expansion! */
18328 temp_result
= make_expanded_name(retval
, expr_start
,
18329 expr_end
, temp_result
);
18331 retval
= temp_result
;
18339 * Return TRUE if character "c" can be used in a variable or function name.
18340 * Does not include '{' or '}' for magic braces.
18346 return (ASCII_ISALNUM(c
) || c
== '_' || c
== ':' || c
== AUTOLOAD_CHAR
);
18350 * Return TRUE if character "c" can be used as the first character in a
18351 * variable or function name (excluding '{' and '}').
18357 return (ASCII_ISALPHA(c
) || c
== '_');
18361 * Set number v: variable to "val".
18364 set_vim_var_nr(idx
, val
)
18368 vimvars
[idx
].vv_nr
= val
;
18372 * Get number v: variable value.
18375 get_vim_var_nr(idx
)
18378 return vimvars
[idx
].vv_nr
;
18382 * Get string v: variable value. Uses a static buffer, can only be used once.
18385 get_vim_var_str(idx
)
18388 return get_tv_string(&vimvars
[idx
].vv_tv
);
18392 * Get List v: variable value. Caller must take care of reference count when
18396 get_vim_var_list(idx
)
18399 return vimvars
[idx
].vv_list
;
18403 * Set v:count to "count" and v:count1 to "count1".
18404 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
18407 set_vcount(count
, count1
, set_prevcount
)
18413 vimvars
[VV_PREVCOUNT
].vv_nr
= vimvars
[VV_COUNT
].vv_nr
;
18414 vimvars
[VV_COUNT
].vv_nr
= count
;
18415 vimvars
[VV_COUNT1
].vv_nr
= count1
;
18419 * Set string v: variable to a copy of "val".
18422 set_vim_var_string(idx
, val
, len
)
18425 int len
; /* length of "val" to use or -1 (whole string) */
18427 /* Need to do this (at least) once, since we can't initialize a union.
18428 * Will always be invoked when "v:progname" is set. */
18429 vimvars
[VV_VERSION
].vv_nr
= VIM_VERSION_100
;
18431 vim_free(vimvars
[idx
].vv_str
);
18433 vimvars
[idx
].vv_str
= NULL
;
18434 else if (len
== -1)
18435 vimvars
[idx
].vv_str
= vim_strsave(val
);
18437 vimvars
[idx
].vv_str
= vim_strnsave(val
, len
);
18441 * Set List v: variable to "val".
18444 set_vim_var_list(idx
, val
)
18448 list_unref(vimvars
[idx
].vv_list
);
18449 vimvars
[idx
].vv_list
= val
;
18451 ++val
->lv_refcount
;
18455 * Set v:register if needed.
18463 if (c
== 0 || c
== ' ')
18467 /* Avoid free/alloc when the value is already right. */
18468 if (vimvars
[VV_REG
].vv_str
== NULL
|| vimvars
[VV_REG
].vv_str
[0] != c
)
18469 set_vim_var_string(VV_REG
, ®name
, 1);
18473 * Get or set v:exception. If "oldval" == NULL, return the current value.
18474 * Otherwise, restore the value to "oldval" and return NULL.
18475 * Must always be called in pairs to save and restore v:exception! Does not
18476 * take care of memory allocations.
18479 v_exception(oldval
)
18482 if (oldval
== NULL
)
18483 return vimvars
[VV_EXCEPTION
].vv_str
;
18485 vimvars
[VV_EXCEPTION
].vv_str
= oldval
;
18490 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18491 * Otherwise, restore the value to "oldval" and return NULL.
18492 * Must always be called in pairs to save and restore v:throwpoint! Does not
18493 * take care of memory allocations.
18496 v_throwpoint(oldval
)
18499 if (oldval
== NULL
)
18500 return vimvars
[VV_THROWPOINT
].vv_str
;
18502 vimvars
[VV_THROWPOINT
].vv_str
= oldval
;
18506 #if defined(FEAT_AUTOCMD) || defined(PROTO)
18509 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18510 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18511 * Must always be called in pairs!
18514 set_cmdarg(eap
, oldarg
)
18522 oldval
= vimvars
[VV_CMDARG
].vv_str
;
18526 vimvars
[VV_CMDARG
].vv_str
= oldarg
;
18530 if (eap
->force_bin
== FORCE_BIN
)
18532 else if (eap
->force_bin
== FORCE_NOBIN
)
18537 if (eap
->read_edit
)
18540 if (eap
->force_ff
!= 0)
18541 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_ff
) + 6;
18543 if (eap
->force_enc
!= 0)
18544 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_enc
) + 7;
18545 if (eap
->bad_char
!= 0)
18546 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->bad_char
) + 7;
18549 newval
= alloc(len
+ 1);
18550 if (newval
== NULL
)
18553 if (eap
->force_bin
== FORCE_BIN
)
18554 sprintf((char *)newval
, " ++bin");
18555 else if (eap
->force_bin
== FORCE_NOBIN
)
18556 sprintf((char *)newval
, " ++nobin");
18560 if (eap
->read_edit
)
18561 STRCAT(newval
, " ++edit");
18563 if (eap
->force_ff
!= 0)
18564 sprintf((char *)newval
+ STRLEN(newval
), " ++ff=%s",
18565 eap
->cmd
+ eap
->force_ff
);
18567 if (eap
->force_enc
!= 0)
18568 sprintf((char *)newval
+ STRLEN(newval
), " ++enc=%s",
18569 eap
->cmd
+ eap
->force_enc
);
18570 if (eap
->bad_char
!= 0)
18571 sprintf((char *)newval
+ STRLEN(newval
), " ++bad=%s",
18572 eap
->cmd
+ eap
->bad_char
);
18574 vimvars
[VV_CMDARG
].vv_str
= newval
;
18580 * Get the value of internal variable "name".
18581 * Return OK or FAIL.
18584 get_var_tv(name
, len
, rettv
, verbose
)
18586 int len
; /* length of "name" */
18587 typval_T
*rettv
; /* NULL when only checking existence */
18588 int verbose
; /* may give error message */
18591 typval_T
*tv
= NULL
;
18596 /* truncate the name, so that we can use strcmp() */
18601 * Check for "b:changedtick".
18603 if (STRCMP(name
, "b:changedtick") == 0)
18605 atv
.v_type
= VAR_NUMBER
;
18606 atv
.vval
.v_number
= curbuf
->b_changedtick
;
18611 * Check for user-defined variables.
18615 v
= find_var(name
, NULL
);
18622 if (rettv
!= NULL
&& verbose
)
18623 EMSG2(_(e_undefvar
), name
);
18626 else if (rettv
!= NULL
)
18627 copy_tv(tv
, rettv
);
18635 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18636 * Also handle function call with Funcref variable: func(expr)
18637 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18640 handle_subscript(arg
, rettv
, evaluate
, verbose
)
18643 int evaluate
; /* do more than finding the end */
18644 int verbose
; /* give error messages */
18647 dict_T
*selfdict
= NULL
;
18654 || (**arg
== '.' && rettv
->v_type
== VAR_DICT
)
18655 || (**arg
== '(' && rettv
->v_type
== VAR_FUNC
))
18656 && !vim_iswhite(*(*arg
- 1)))
18660 /* need to copy the funcref so that we can clear rettv */
18662 rettv
->v_type
= VAR_UNKNOWN
;
18664 /* Invoke the function. Recursive! */
18665 s
= functv
.vval
.v_string
;
18666 ret
= get_func_tv(s
, (int)STRLEN(s
), rettv
, arg
,
18667 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
18668 &len
, evaluate
, selfdict
);
18670 /* Clear the funcref afterwards, so that deleting it while
18671 * evaluating the arguments is possible (see test55). */
18674 /* Stop the expression evaluation when immediately aborting on
18675 * error, or when an interrupt occurred or an exception was thrown
18676 * but not caught. */
18683 dict_unref(selfdict
);
18686 else /* **arg == '[' || **arg == '.' */
18688 dict_unref(selfdict
);
18689 if (rettv
->v_type
== VAR_DICT
)
18691 selfdict
= rettv
->vval
.v_dict
;
18692 if (selfdict
!= NULL
)
18693 ++selfdict
->dv_refcount
;
18697 if (eval_index(arg
, rettv
, evaluate
, verbose
) == FAIL
)
18704 dict_unref(selfdict
);
18709 * Allocate memory for a variable type-value, and make it empty (0 or NULL
18715 return (typval_T
*)alloc_clear((unsigned)sizeof(typval_T
));
18719 * Allocate memory for a variable type-value, and assign a string to it.
18720 * The string "s" must have been allocated, it is consumed.
18721 * Return NULL for out of memory, the variable otherwise.
18729 rettv
= alloc_tv();
18732 rettv
->v_type
= VAR_STRING
;
18733 rettv
->vval
.v_string
= s
;
18741 * Free the memory for a variable type-value.
18749 switch (varp
->v_type
)
18752 func_unref(varp
->vval
.v_string
);
18755 vim_free(varp
->vval
.v_string
);
18758 list_unref(varp
->vval
.v_list
);
18761 dict_unref(varp
->vval
.v_dict
);
18770 EMSG2(_(e_intern2
), "free_tv()");
18778 * Free the memory for a variable value and set the value to NULL or 0.
18786 switch (varp
->v_type
)
18789 func_unref(varp
->vval
.v_string
);
18792 vim_free(varp
->vval
.v_string
);
18793 varp
->vval
.v_string
= NULL
;
18796 list_unref(varp
->vval
.v_list
);
18797 varp
->vval
.v_list
= NULL
;
18800 dict_unref(varp
->vval
.v_dict
);
18801 varp
->vval
.v_dict
= NULL
;
18804 varp
->vval
.v_number
= 0;
18808 varp
->vval
.v_float
= 0.0;
18814 EMSG2(_(e_intern2
), "clear_tv()");
18821 * Set the value of a variable to NULL without freeing items.
18828 vim_memset(varp
, 0, sizeof(typval_T
));
18832 * Get the number value of a variable.
18833 * If it is a String variable, uses vim_str2nr().
18834 * For incompatible types, return 0.
18835 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18836 * caller of incompatible types: it sets *denote to TRUE if "denote"
18837 * is not NULL or returns -1 otherwise.
18840 get_tv_number(varp
)
18845 return get_tv_number_chk(varp
, &error
); /* return 0L on error */
18849 get_tv_number_chk(varp
, denote
)
18855 switch (varp
->v_type
)
18858 return (long)(varp
->vval
.v_number
);
18861 EMSG(_("E805: Using a Float as a Number"));
18865 EMSG(_("E703: Using a Funcref as a Number"));
18868 if (varp
->vval
.v_string
!= NULL
)
18869 vim_str2nr(varp
->vval
.v_string
, NULL
, NULL
,
18870 TRUE
, TRUE
, &n
, NULL
);
18873 EMSG(_("E745: Using a List as a Number"));
18876 EMSG(_("E728: Using a Dictionary as a Number"));
18879 EMSG2(_(e_intern2
), "get_tv_number()");
18882 if (denote
== NULL
) /* useful for values that must be unsigned */
18890 * Get the lnum from the first argument.
18891 * Also accepts ".", "$", etc., but that only works for the current buffer.
18892 * Returns -1 on error.
18895 get_tv_lnum(argvars
)
18901 lnum
= get_tv_number_chk(&argvars
[0], NULL
);
18902 if (lnum
== 0) /* no valid number, try using line() */
18904 rettv
.v_type
= VAR_NUMBER
;
18905 f_line(argvars
, &rettv
);
18906 lnum
= rettv
.vval
.v_number
;
18913 * Get the lnum from the first argument.
18914 * Also accepts "$", then "buf" is used.
18915 * Returns 0 on error.
18918 get_tv_lnum_buf(argvars
, buf
)
18922 if (argvars
[0].v_type
== VAR_STRING
18923 && argvars
[0].vval
.v_string
!= NULL
18924 && argvars
[0].vval
.v_string
[0] == '$'
18926 return buf
->b_ml
.ml_line_count
;
18927 return get_tv_number_chk(&argvars
[0], NULL
);
18931 * Get the string value of a variable.
18932 * If it is a Number variable, the number is converted into a string.
18933 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18934 * get_tv_string_buf() uses a given buffer.
18935 * If the String variable has never been set, return an empty string.
18936 * Never returns NULL;
18937 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18941 get_tv_string(varp
)
18944 static char_u mybuf
[NUMBUFLEN
];
18946 return get_tv_string_buf(varp
, mybuf
);
18950 get_tv_string_buf(varp
, buf
)
18954 char_u
*res
= get_tv_string_buf_chk(varp
, buf
);
18956 return res
!= NULL
? res
: (char_u
*)"";
18960 get_tv_string_chk(varp
)
18963 static char_u mybuf
[NUMBUFLEN
];
18965 return get_tv_string_buf_chk(varp
, mybuf
);
18969 get_tv_string_buf_chk(varp
, buf
)
18973 switch (varp
->v_type
)
18976 sprintf((char *)buf
, "%ld", (long)varp
->vval
.v_number
);
18979 EMSG(_("E729: using Funcref as a String"));
18982 EMSG(_("E730: using List as a String"));
18985 EMSG(_("E731: using Dictionary as a String"));
18989 EMSG(_("E806: using Float as a String"));
18993 if (varp
->vval
.v_string
!= NULL
)
18994 return varp
->vval
.v_string
;
18995 return (char_u
*)"";
18997 EMSG2(_(e_intern2
), "get_tv_string_buf()");
19004 * Find variable "name" in the list of variables.
19005 * Return a pointer to it if found, NULL if not found.
19006 * Careful: "a:0" variables don't have a name.
19007 * When "htp" is not NULL we are writing to the variable, set "htp" to the
19010 static dictitem_T
*
19011 find_var(name
, htp
)
19018 ht
= find_var_ht(name
, &varname
);
19023 return find_var_in_ht(ht
, varname
, htp
!= NULL
);
19027 * Find variable "varname" in hashtab "ht".
19028 * Returns NULL if not found.
19030 static dictitem_T
*
19031 find_var_in_ht(ht
, varname
, writing
)
19038 if (*varname
== NUL
)
19040 /* Must be something like "s:", otherwise "ht" would be NULL. */
19041 switch (varname
[-2])
19043 case 's': return &SCRIPT_SV(current_SID
).sv_var
;
19044 case 'g': return &globvars_var
;
19045 case 'v': return &vimvars_var
;
19046 case 'b': return &curbuf
->b_bufvar
;
19047 case 'w': return &curwin
->w_winvar
;
19048 #ifdef FEAT_WINDOWS
19049 case 't': return &curtab
->tp_winvar
;
19051 case 'l': return current_funccal
== NULL
19052 ? NULL
: ¤t_funccal
->l_vars_var
;
19053 case 'a': return current_funccal
== NULL
19054 ? NULL
: ¤t_funccal
->l_avars_var
;
19059 hi
= hash_find(ht
, varname
);
19060 if (HASHITEM_EMPTY(hi
))
19062 /* For global variables we may try auto-loading the script. If it
19063 * worked find the variable again. Don't auto-load a script if it was
19064 * loaded already, otherwise it would be loaded every time when
19065 * checking if a function name is a Funcref variable. */
19066 if (ht
== &globvarht
&& !writing
19067 && script_autoload(varname
, FALSE
) && !aborting())
19068 hi
= hash_find(ht
, varname
);
19069 if (HASHITEM_EMPTY(hi
))
19076 * Find the hashtab used for a variable name.
19077 * Set "varname" to the start of name without ':'.
19080 find_var_ht(name
, varname
)
19086 if (name
[1] != ':')
19088 /* The name must not start with a colon or #. */
19089 if (name
[0] == ':' || name
[0] == AUTOLOAD_CHAR
)
19093 /* "version" is "v:version" in all scopes */
19094 hi
= hash_find(&compat_hashtab
, name
);
19095 if (!HASHITEM_EMPTY(hi
))
19096 return &compat_hashtab
;
19098 if (current_funccal
== NULL
)
19099 return &globvarht
; /* global variable */
19100 return ¤t_funccal
->l_vars
.dv_hashtab
; /* l: variable */
19102 *varname
= name
+ 2;
19103 if (*name
== 'g') /* global variable */
19105 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19107 if (vim_strchr(name
+ 2, ':') != NULL
19108 || vim_strchr(name
+ 2, AUTOLOAD_CHAR
) != NULL
)
19110 if (*name
== 'b') /* buffer variable */
19111 return &curbuf
->b_vars
.dv_hashtab
;
19112 if (*name
== 'w') /* window variable */
19113 return &curwin
->w_vars
.dv_hashtab
;
19114 #ifdef FEAT_WINDOWS
19115 if (*name
== 't') /* tab page variable */
19116 return &curtab
->tp_vars
.dv_hashtab
;
19118 if (*name
== 'v') /* v: variable */
19120 if (*name
== 'a' && current_funccal
!= NULL
) /* function argument */
19121 return ¤t_funccal
->l_avars
.dv_hashtab
;
19122 if (*name
== 'l' && current_funccal
!= NULL
) /* local function variable */
19123 return ¤t_funccal
->l_vars
.dv_hashtab
;
19124 if (*name
== 's' /* script variable */
19125 && current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
19126 return &SCRIPT_VARS(current_SID
);
19131 * Get the string value of a (global/local) variable.
19132 * Returns NULL when it doesn't exist.
19135 get_var_value(name
)
19140 v
= find_var(name
, NULL
);
19143 return get_tv_string(&v
->di_tv
);
19147 * Allocate a new hashtab for a sourced script. It will be used while
19148 * sourcing this script and when executing functions defined in the script.
19151 new_script_vars(id
)
19158 if (ga_grow(&ga_scripts
, (int)(id
- ga_scripts
.ga_len
)) == OK
)
19160 /* Re-allocating ga_data means that an ht_array pointing to
19161 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
19162 * at its init value. Also reset "v_dict", it's always the same. */
19163 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
19165 ht
= &SCRIPT_VARS(i
);
19166 if (ht
->ht_mask
== HT_INIT_SIZE
- 1)
19167 ht
->ht_array
= ht
->ht_smallarray
;
19168 sv
= &SCRIPT_SV(i
);
19169 sv
->sv_var
.di_tv
.vval
.v_dict
= &sv
->sv_dict
;
19172 while (ga_scripts
.ga_len
< id
)
19174 sv
= &SCRIPT_SV(ga_scripts
.ga_len
+ 1);
19175 init_var_dict(&sv
->sv_dict
, &sv
->sv_var
);
19176 ++ga_scripts
.ga_len
;
19182 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19186 init_var_dict(dict
, dict_var
)
19188 dictitem_T
*dict_var
;
19190 hash_init(&dict
->dv_hashtab
);
19191 dict
->dv_refcount
= DO_NOT_FREE_CNT
;
19192 dict_var
->di_tv
.vval
.v_dict
= dict
;
19193 dict_var
->di_tv
.v_type
= VAR_DICT
;
19194 dict_var
->di_tv
.v_lock
= VAR_FIXED
;
19195 dict_var
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
19196 dict_var
->di_key
[0] = NUL
;
19200 * Clean up a list of internal variables.
19201 * Frees all allocated variables and the value they contain.
19202 * Clears hashtab "ht", does not free it.
19208 vars_clear_ext(ht
, TRUE
);
19212 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19215 vars_clear_ext(ht
, free_val
)
19224 todo
= (int)ht
->ht_used
;
19225 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
19227 if (!HASHITEM_EMPTY(hi
))
19231 /* Free the variable. Don't remove it from the hashtab,
19232 * ht_array might change then. hash_clear() takes care of it
19236 clear_tv(&v
->di_tv
);
19237 if ((v
->di_flags
& DI_FLAGS_FIX
) == 0)
19246 * Delete a variable from hashtab "ht" at item "hi".
19247 * Clear the variable value and free the dictitem.
19254 dictitem_T
*di
= HI2DI(hi
);
19256 hash_remove(ht
, hi
);
19257 clear_tv(&di
->di_tv
);
19262 * List the value of one internal variable.
19265 list_one_var(v
, prefix
, first
)
19272 char_u numbuf
[NUMBUFLEN
];
19274 s
= echo_string(&v
->di_tv
, &tofree
, numbuf
, ++current_copyID
);
19275 list_one_var_a(prefix
, v
->di_key
, v
->di_tv
.v_type
,
19276 s
== NULL
? (char_u
*)"" : s
, first
);
19281 list_one_var_a(prefix
, name
, type
, string
, first
)
19286 int *first
; /* when TRUE clear rest of screen and set to FALSE */
19288 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19291 if (name
!= NULL
) /* "a:" vars don't have a name stored */
19295 if (type
== VAR_NUMBER
)
19297 else if (type
== VAR_FUNC
)
19299 else if (type
== VAR_LIST
)
19302 if (*string
== '[')
19305 else if (type
== VAR_DICT
)
19308 if (*string
== '{')
19314 msg_outtrans(string
);
19316 if (type
== VAR_FUNC
)
19317 msg_puts((char_u
*)"()");
19326 * Set variable "name" to value in "tv".
19327 * If the variable already exists, the value is updated.
19328 * Otherwise the variable is created.
19331 set_var(name
, tv
, copy
)
19334 int copy
; /* make copy of value in "tv" */
19341 if (tv
->v_type
== VAR_FUNC
)
19343 if (!(vim_strchr((char_u
*)"wbs", name
[0]) != NULL
&& name
[1] == ':')
19344 && !ASCII_ISUPPER((name
[0] != NUL
&& name
[1] == ':')
19345 ? name
[2] : name
[0]))
19347 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name
);
19350 if (function_exists(name
))
19352 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
19358 ht
= find_var_ht(name
, &varname
);
19359 if (ht
== NULL
|| *varname
== NUL
)
19361 EMSG2(_(e_illvar
), name
);
19365 v
= find_var_in_ht(ht
, varname
, TRUE
);
19368 /* existing variable, need to clear the value */
19369 if (var_check_ro(v
->di_flags
, name
)
19370 || tv_check_lock(v
->di_tv
.v_lock
, name
))
19372 if (v
->di_tv
.v_type
!= tv
->v_type
19373 && !((v
->di_tv
.v_type
== VAR_STRING
19374 || v
->di_tv
.v_type
== VAR_NUMBER
)
19375 && (tv
->v_type
== VAR_STRING
19376 || tv
->v_type
== VAR_NUMBER
))
19378 && !((v
->di_tv
.v_type
== VAR_NUMBER
19379 || v
->di_tv
.v_type
== VAR_FLOAT
)
19380 && (tv
->v_type
== VAR_NUMBER
19381 || tv
->v_type
== VAR_FLOAT
))
19385 EMSG2(_("E706: Variable type mismatch for: %s"), name
);
19390 * Handle setting internal v: variables separately: we don't change
19393 if (ht
== &vimvarht
)
19395 if (v
->di_tv
.v_type
== VAR_STRING
)
19397 vim_free(v
->di_tv
.vval
.v_string
);
19398 if (copy
|| tv
->v_type
!= VAR_STRING
)
19399 v
->di_tv
.vval
.v_string
= vim_strsave(get_tv_string(tv
));
19402 /* Take over the string to avoid an extra alloc/free. */
19403 v
->di_tv
.vval
.v_string
= tv
->vval
.v_string
;
19404 tv
->vval
.v_string
= NULL
;
19407 else if (v
->di_tv
.v_type
!= VAR_NUMBER
)
19408 EMSG2(_(e_intern2
), "set_var()");
19411 v
->di_tv
.vval
.v_number
= get_tv_number(tv
);
19412 if (STRCMP(varname
, "searchforward") == 0)
19413 set_search_direction(v
->di_tv
.vval
.v_number
? '/' : '?');
19418 clear_tv(&v
->di_tv
);
19420 else /* add a new variable */
19422 /* Can't add "v:" variable. */
19423 if (ht
== &vimvarht
)
19425 EMSG2(_(e_illvar
), name
);
19429 /* Make sure the variable name is valid. */
19430 for (p
= varname
; *p
!= NUL
; ++p
)
19431 if (!eval_isnamec1(*p
) && (p
== varname
|| !VIM_ISDIGIT(*p
))
19432 && *p
!= AUTOLOAD_CHAR
)
19434 EMSG2(_(e_illvar
), varname
);
19438 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
19439 + STRLEN(varname
)));
19442 STRCPY(v
->di_key
, varname
);
19443 if (hash_add(ht
, DI2HIKEY(v
)) == FAIL
)
19451 if (copy
|| tv
->v_type
== VAR_NUMBER
|| tv
->v_type
== VAR_FLOAT
)
19452 copy_tv(tv
, &v
->di_tv
);
19456 v
->di_tv
.v_lock
= 0;
19462 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
19463 * Also give an error message.
19466 var_check_ro(flags
, name
)
19470 if (flags
& DI_FLAGS_RO
)
19472 EMSG2(_(e_readonlyvar
), name
);
19475 if ((flags
& DI_FLAGS_RO_SBX
) && sandbox
)
19477 EMSG2(_(e_readonlysbx
), name
);
19484 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19485 * Also give an error message.
19488 var_check_fixed(flags
, name
)
19492 if (flags
& DI_FLAGS_FIX
)
19494 EMSG2(_("E795: Cannot delete variable %s"), name
);
19501 * Return TRUE if typeval "tv" is set to be locked (immutable).
19502 * Also give an error message, using "name".
19505 tv_check_lock(lock
, name
)
19509 if (lock
& VAR_LOCKED
)
19511 EMSG2(_("E741: Value is locked: %s"),
19512 name
== NULL
? (char_u
*)_("Unknown") : name
);
19515 if (lock
& VAR_FIXED
)
19517 EMSG2(_("E742: Cannot change value of %s"),
19518 name
== NULL
? (char_u
*)_("Unknown") : name
);
19525 * Copy the values from typval_T "from" to typval_T "to".
19526 * When needed allocates string or increases reference count.
19527 * Does not make a copy of a list or dict but copies the reference!
19528 * It is OK for "from" and "to" to point to the same item. This is used to
19529 * make a copy later.
19536 to
->v_type
= from
->v_type
;
19538 switch (from
->v_type
)
19541 to
->vval
.v_number
= from
->vval
.v_number
;
19545 to
->vval
.v_float
= from
->vval
.v_float
;
19550 if (from
->vval
.v_string
== NULL
)
19551 to
->vval
.v_string
= NULL
;
19554 to
->vval
.v_string
= vim_strsave(from
->vval
.v_string
);
19555 if (from
->v_type
== VAR_FUNC
)
19556 func_ref(to
->vval
.v_string
);
19560 if (from
->vval
.v_list
== NULL
)
19561 to
->vval
.v_list
= NULL
;
19564 to
->vval
.v_list
= from
->vval
.v_list
;
19565 ++to
->vval
.v_list
->lv_refcount
;
19569 if (from
->vval
.v_dict
== NULL
)
19570 to
->vval
.v_dict
= NULL
;
19573 to
->vval
.v_dict
= from
->vval
.v_dict
;
19574 ++to
->vval
.v_dict
->dv_refcount
;
19578 EMSG2(_(e_intern2
), "copy_tv()");
19584 * Make a copy of an item.
19585 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
19586 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19587 * reference to an already copied list/dict can be used.
19588 * Returns FAIL or OK.
19591 item_copy(from
, to
, deep
, copyID
)
19597 static int recurse
= 0;
19600 if (recurse
>= DICT_MAXNEST
)
19602 EMSG(_("E698: variable nested too deep for making a copy"));
19607 switch (from
->v_type
)
19618 to
->v_type
= VAR_LIST
;
19620 if (from
->vval
.v_list
== NULL
)
19621 to
->vval
.v_list
= NULL
;
19622 else if (copyID
!= 0 && from
->vval
.v_list
->lv_copyID
== copyID
)
19624 /* use the copy made earlier */
19625 to
->vval
.v_list
= from
->vval
.v_list
->lv_copylist
;
19626 ++to
->vval
.v_list
->lv_refcount
;
19629 to
->vval
.v_list
= list_copy(from
->vval
.v_list
, deep
, copyID
);
19630 if (to
->vval
.v_list
== NULL
)
19634 to
->v_type
= VAR_DICT
;
19636 if (from
->vval
.v_dict
== NULL
)
19637 to
->vval
.v_dict
= NULL
;
19638 else if (copyID
!= 0 && from
->vval
.v_dict
->dv_copyID
== copyID
)
19640 /* use the copy made earlier */
19641 to
->vval
.v_dict
= from
->vval
.v_dict
->dv_copydict
;
19642 ++to
->vval
.v_dict
->dv_refcount
;
19645 to
->vval
.v_dict
= dict_copy(from
->vval
.v_dict
, deep
, copyID
);
19646 if (to
->vval
.v_dict
== NULL
)
19650 EMSG2(_(e_intern2
), "item_copy()");
19658 * ":echo expr1 ..." print each argument separated with a space, add a
19659 * newline at the end.
19660 * ":echon expr1 ..." print each argument plain.
19666 char_u
*arg
= eap
->arg
;
19670 int needclr
= TRUE
;
19671 int atstart
= TRUE
;
19672 char_u numbuf
[NUMBUFLEN
];
19676 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n' && !got_int
)
19678 /* If eval1() causes an error message the text from the command may
19679 * still need to be cleared. E.g., "echo 22,44". */
19680 need_clr_eos
= needclr
;
19683 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
19686 * Report the invalid expression unless the expression evaluation
19687 * has been cancelled due to an aborting error, an interrupt, or an
19691 EMSG2(_(e_invexpr2
), p
);
19692 need_clr_eos
= FALSE
;
19695 need_clr_eos
= FALSE
;
19702 /* Call msg_start() after eval1(), evaluating the expression
19703 * may cause a message to appear. */
19704 if (eap
->cmdidx
== CMD_echo
)
19707 else if (eap
->cmdidx
== CMD_echo
)
19708 msg_puts_attr((char_u
*)" ", echo_attr
);
19709 p
= echo_string(&rettv
, &tofree
, numbuf
, ++current_copyID
);
19711 for ( ; *p
!= NUL
&& !got_int
; ++p
)
19713 if (*p
== '\n' || *p
== '\r' || *p
== TAB
)
19715 if (*p
!= TAB
&& needclr
)
19717 /* remove any text still there from the command */
19721 msg_putchar_attr(*p
, echo_attr
);
19728 int i
= (*mb_ptr2len
)(p
);
19730 (void)msg_outtrans_len_attr(p
, i
, echo_attr
);
19735 (void)msg_outtrans_len_attr(p
, 1, echo_attr
);
19741 arg
= skipwhite(arg
);
19743 eap
->nextcmd
= check_nextcmd(arg
);
19749 /* remove text that may still be there from the command */
19752 if (eap
->cmdidx
== CMD_echo
)
19758 * ":echohl {name}".
19766 id
= syn_name2id(eap
->arg
);
19770 echo_attr
= syn_id2attr(id
);
19774 * ":execute expr1 ..." execute the result of an expression.
19775 * ":echomsg expr1 ..." Print a message
19776 * ":echoerr expr1 ..." Print an error
19777 * Each gets spaces around each argument and a newline at the end for
19784 char_u
*arg
= eap
->arg
;
19792 ga_init2(&ga
, 1, 80);
19796 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
19799 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
19802 * Report the invalid expression unless the expression evaluation
19803 * has been cancelled due to an aborting error, an interrupt, or an
19807 EMSG2(_(e_invexpr2
), p
);
19814 p
= get_tv_string(&rettv
);
19815 len
= (int)STRLEN(p
);
19816 if (ga_grow(&ga
, len
+ 2) == FAIL
)
19823 ((char_u
*)(ga
.ga_data
))[ga
.ga_len
++] = ' ';
19824 STRCPY((char_u
*)(ga
.ga_data
) + ga
.ga_len
, p
);
19829 arg
= skipwhite(arg
);
19832 if (ret
!= FAIL
&& ga
.ga_data
!= NULL
)
19834 if (eap
->cmdidx
== CMD_echomsg
)
19836 MSG_ATTR(ga
.ga_data
, echo_attr
);
19839 else if (eap
->cmdidx
== CMD_echoerr
)
19841 /* We don't want to abort following commands, restore did_emsg. */
19842 save_did_emsg
= did_emsg
;
19843 EMSG((char_u
*)ga
.ga_data
);
19845 did_emsg
= save_did_emsg
;
19847 else if (eap
->cmdidx
== CMD_execute
)
19848 do_cmdline((char_u
*)ga
.ga_data
,
19849 eap
->getline
, eap
->cookie
, DOCMD_NOWAIT
|DOCMD_VERBOSE
);
19857 eap
->nextcmd
= check_nextcmd(arg
);
19861 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19862 * "arg" points to the "&" or '+' when called, to "option" when returning.
19863 * Returns NULL when no option name found. Otherwise pointer to the char
19864 * after the option name.
19867 find_option_end(arg
, opt_flags
)
19874 if (*p
== 'g' && p
[1] == ':')
19876 *opt_flags
= OPT_GLOBAL
;
19879 else if (*p
== 'l' && p
[1] == ':')
19881 *opt_flags
= OPT_LOCAL
;
19887 if (!ASCII_ISALPHA(*p
))
19891 if (p
[0] == 't' && p
[1] == '_' && p
[2] != NUL
&& p
[3] != NUL
)
19892 p
+= 4; /* termcap option */
19894 while (ASCII_ISALPHA(*p
))
19909 int saved_did_emsg
;
19910 char_u
*name
= NULL
;
19913 char_u
*line_arg
= NULL
;
19916 int varargs
= FALSE
;
19917 int mustend
= FALSE
;
19922 char_u
*skip_until
= NULL
;
19925 static int func_nr
= 0; /* number for nameless function */
19930 int sourcing_lnum_off
;
19933 * ":function" without argument: list functions.
19935 if (ends_excmd(*eap
->arg
))
19939 todo
= (int)func_hashtab
.ht_used
;
19940 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
19942 if (!HASHITEM_EMPTY(hi
))
19946 if (!isdigit(*fp
->uf_name
))
19947 list_func_head(fp
, FALSE
);
19951 eap
->nextcmd
= check_nextcmd(eap
->arg
);
19956 * ":function /pat": list functions matching pattern.
19958 if (*eap
->arg
== '/')
19960 p
= skip_regexp(eap
->arg
+ 1, '/', TRUE
, NULL
);
19963 regmatch_T regmatch
;
19967 regmatch
.regprog
= vim_regcomp(eap
->arg
+ 1, RE_MAGIC
);
19969 if (regmatch
.regprog
!= NULL
)
19971 regmatch
.rm_ic
= p_ic
;
19973 todo
= (int)func_hashtab
.ht_used
;
19974 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
19976 if (!HASHITEM_EMPTY(hi
))
19980 if (!isdigit(*fp
->uf_name
)
19981 && vim_regexec(®match
, fp
->uf_name
, 0))
19982 list_func_head(fp
, FALSE
);
19989 eap
->nextcmd
= check_nextcmd(p
);
19994 * Get the function name. There are these situations:
19995 * func normal function name
19996 * "name" == func, "fudi.fd_dict" == NULL
19997 * dict.func new dictionary entry
19998 * "name" == NULL, "fudi.fd_dict" set,
19999 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
20000 * dict.func existing dict entry with a Funcref
20001 * "name" == func, "fudi.fd_dict" set,
20002 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20003 * dict.func existing dict entry that's not a Funcref
20004 * "name" == NULL, "fudi.fd_dict" set,
20005 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
20008 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
20009 paren
= (vim_strchr(p
, '(') != NULL
);
20010 if (name
== NULL
&& (fudi
.fd_dict
== NULL
|| !paren
) && !eap
->skip
)
20013 * Return on an invalid expression in braces, unless the expression
20014 * evaluation has been cancelled due to an aborting error, an
20015 * interrupt, or an exception.
20019 if (!eap
->skip
&& fudi
.fd_newkey
!= NULL
)
20020 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
20021 vim_free(fudi
.fd_newkey
);
20028 /* An error in a function call during evaluation of an expression in magic
20029 * braces should not cause the function not to be defined. */
20030 saved_did_emsg
= did_emsg
;
20034 * ":function func" with only function name: list function.
20038 if (!ends_excmd(*skipwhite(p
)))
20040 EMSG(_(e_trailing
));
20043 eap
->nextcmd
= check_nextcmd(p
);
20044 if (eap
->nextcmd
!= NULL
)
20046 if (!eap
->skip
&& !got_int
)
20048 fp
= find_func(name
);
20051 list_func_head(fp
, TRUE
);
20052 for (j
= 0; j
< fp
->uf_lines
.ga_len
&& !got_int
; ++j
)
20054 if (FUNCLINE(fp
, j
) == NULL
)
20057 msg_outnum((long)(j
+ 1));
20062 msg_prt_line(FUNCLINE(fp
, j
), FALSE
);
20063 out_flush(); /* show a line at a time */
20069 msg_puts((char_u
*)" endfunction");
20073 emsg_funcname("E123: Undefined function: %s", name
);
20079 * ":function name(arg1, arg2)" Define function.
20086 EMSG2(_("E124: Missing '(': %s"), eap
->arg
);
20089 /* attempt to continue by skipping some text */
20090 if (vim_strchr(p
, '(') != NULL
)
20091 p
= vim_strchr(p
, '(');
20093 p
= skipwhite(p
+ 1);
20095 ga_init2(&newargs
, (int)sizeof(char_u
*), 3);
20096 ga_init2(&newlines
, (int)sizeof(char_u
*), 3);
20100 /* Check the name of the function. Unless it's a dictionary function
20101 * (that we are overwriting). */
20105 arg
= fudi
.fd_newkey
;
20106 if (arg
!= NULL
&& (fudi
.fd_di
== NULL
20107 || fudi
.fd_di
->di_tv
.v_type
!= VAR_FUNC
))
20109 if (*arg
== K_SPECIAL
)
20113 while (arg
[j
] != NUL
&& (j
== 0 ? eval_isnamec1(arg
[j
])
20114 : eval_isnamec(arg
[j
])))
20117 emsg_funcname(_(e_invarg2
), arg
);
20122 * Isolate the arguments: "arg1, arg2, ...)"
20126 if (p
[0] == '.' && p
[1] == '.' && p
[2] == '.')
20135 while (ASCII_ISALNUM(*p
) || *p
== '_')
20137 if (arg
== p
|| isdigit(*arg
)
20138 || (p
- arg
== 9 && STRNCMP(arg
, "firstline", 9) == 0)
20139 || (p
- arg
== 8 && STRNCMP(arg
, "lastline", 8) == 0))
20142 EMSG2(_("E125: Illegal argument: %s"), arg
);
20145 if (ga_grow(&newargs
, 1) == FAIL
)
20149 arg
= vim_strsave(arg
);
20152 ((char_u
**)(newargs
.ga_data
))[newargs
.ga_len
] = arg
;
20161 if (mustend
&& *p
!= ')')
20164 EMSG2(_(e_invarg2
), eap
->arg
);
20168 ++p
; /* skip the ')' */
20170 /* find extra arguments "range", "dict" and "abort" */
20174 if (STRNCMP(p
, "range", 5) == 0)
20179 else if (STRNCMP(p
, "dict", 4) == 0)
20184 else if (STRNCMP(p
, "abort", 5) == 0)
20193 /* When there is a line break use what follows for the function body.
20194 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20197 else if (*p
!= NUL
&& *p
!= '"' && !eap
->skip
&& !did_emsg
)
20198 EMSG(_(e_trailing
));
20201 * Read the body of the function, until ":endfunction" is found.
20205 /* Check if the function already exists, don't let the user type the
20206 * whole function before telling him it doesn't work! For a script we
20207 * need to skip the body to be able to find what follows. */
20208 if (!eap
->skip
&& !eap
->forceit
)
20210 if (fudi
.fd_dict
!= NULL
&& fudi
.fd_newkey
== NULL
)
20211 EMSG(_(e_funcdict
));
20212 else if (name
!= NULL
&& find_func(name
) != NULL
)
20213 emsg_funcname(e_funcexts
, name
);
20216 if (!eap
->skip
&& did_emsg
)
20219 msg_putchar('\n'); /* don't overwrite the function name */
20220 cmdline_row
= msg_row
;
20228 need_wait_return
= FALSE
;
20229 sourcing_lnum_off
= sourcing_lnum
;
20231 if (line_arg
!= NULL
)
20233 /* Use eap->arg, split up in parts by line breaks. */
20234 theline
= line_arg
;
20235 p
= vim_strchr(theline
, '\n');
20237 line_arg
+= STRLEN(line_arg
);
20244 else if (eap
->getline
== NULL
)
20245 theline
= getcmdline(':', 0L, indent
);
20247 theline
= eap
->getline(':', eap
->cookie
, indent
);
20249 lines_left
= Rows
- 1;
20250 if (theline
== NULL
)
20252 EMSG(_("E126: Missing :endfunction"));
20256 /* Detect line continuation: sourcing_lnum increased more than one. */
20257 if (sourcing_lnum
> sourcing_lnum_off
+ 1)
20258 sourcing_lnum_off
= sourcing_lnum
- sourcing_lnum_off
- 1;
20260 sourcing_lnum_off
= 0;
20262 if (skip_until
!= NULL
)
20264 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20265 * don't check for ":endfunc". */
20266 if (STRCMP(theline
, skip_until
) == 0)
20268 vim_free(skip_until
);
20274 /* skip ':' and blanks*/
20275 for (p
= theline
; vim_iswhite(*p
) || *p
== ':'; ++p
)
20278 /* Check for "endfunction". */
20279 if (checkforcmd(&p
, "endfunction", 4) && nesting
-- == 0)
20281 if (line_arg
== NULL
)
20286 /* Increase indent inside "if", "while", "for" and "try", decrease
20288 if (indent
> 2 && STRNCMP(p
, "end", 3) == 0)
20290 else if (STRNCMP(p
, "if", 2) == 0
20291 || STRNCMP(p
, "wh", 2) == 0
20292 || STRNCMP(p
, "for", 3) == 0
20293 || STRNCMP(p
, "try", 3) == 0)
20296 /* Check for defining a function inside this function. */
20297 if (checkforcmd(&p
, "function", 2))
20300 p
= skipwhite(p
+ 1);
20301 p
+= eval_fname_script(p
);
20302 if (ASCII_ISALPHA(*p
))
20304 vim_free(trans_function_name(&p
, TRUE
, 0, NULL
));
20305 if (*skipwhite(p
) == '(')
20313 /* Check for ":append" or ":insert". */
20314 p
= skip_range(p
, NULL
);
20315 if ((p
[0] == 'a' && (!ASCII_ISALPHA(p
[1]) || p
[1] == 'p'))
20317 && (!ASCII_ISALPHA(p
[1]) || (p
[1] == 'n'
20318 && (!ASCII_ISALPHA(p
[2]) || (p
[2] == 's'))))))
20319 skip_until
= vim_strsave((char_u
*)".");
20321 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20322 arg
= skipwhite(skiptowhite(p
));
20323 if (arg
[0] == '<' && arg
[1] =='<'
20324 && ((p
[0] == 'p' && p
[1] == 'y'
20325 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 't'))
20326 || (p
[0] == 'p' && p
[1] == 'e'
20327 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'r'))
20328 || (p
[0] == 't' && p
[1] == 'c'
20329 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'l'))
20330 || (p
[0] == 'r' && p
[1] == 'u' && p
[2] == 'b'
20331 && (!ASCII_ISALPHA(p
[3]) || p
[3] == 'y'))
20332 || (p
[0] == 'm' && p
[1] == 'z'
20333 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 's'))
20336 /* ":python <<" continues until a dot, like ":append" */
20337 p
= skipwhite(arg
+ 2);
20339 skip_until
= vim_strsave((char_u
*)".");
20341 skip_until
= vim_strsave(p
);
20345 /* Add the line to the function. */
20346 if (ga_grow(&newlines
, 1 + sourcing_lnum_off
) == FAIL
)
20348 if (line_arg
== NULL
)
20353 /* Copy the line to newly allocated memory. get_one_sourceline()
20354 * allocates 250 bytes per line, this saves 80% on average. The cost
20355 * is an extra alloc/free. */
20356 p
= vim_strsave(theline
);
20359 if (line_arg
== NULL
)
20364 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = theline
;
20366 /* Add NULL lines for continuation lines, so that the line count is
20367 * equal to the index in the growarray. */
20368 while (sourcing_lnum_off
-- > 0)
20369 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = NULL
;
20371 /* Check for end of eap->arg. */
20372 if (line_arg
!= NULL
&& *line_arg
== NUL
)
20376 /* Don't define the function when skipping commands or when an error was
20378 if (eap
->skip
|| did_emsg
)
20382 * If there are no errors, add the function
20384 if (fudi
.fd_dict
== NULL
)
20386 v
= find_var(name
, &ht
);
20387 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
20389 emsg_funcname("E707: Function name conflicts with variable: %s",
20394 fp
= find_func(name
);
20399 emsg_funcname(e_funcexts
, name
);
20402 if (fp
->uf_calls
> 0)
20404 emsg_funcname("E127: Cannot redefine function %s: It is in use",
20408 /* redefine existing function */
20409 ga_clear_strings(&(fp
->uf_args
));
20410 ga_clear_strings(&(fp
->uf_lines
));
20420 if (fudi
.fd_newkey
== NULL
&& !eap
->forceit
)
20422 EMSG(_(e_funcdict
));
20425 if (fudi
.fd_di
== NULL
)
20427 /* Can't add a function to a locked dictionary */
20428 if (tv_check_lock(fudi
.fd_dict
->dv_lock
, eap
->arg
))
20431 /* Can't change an existing function if it is locked */
20432 else if (tv_check_lock(fudi
.fd_di
->di_tv
.v_lock
, eap
->arg
))
20435 /* Give the function a sequential number. Can only be used with a
20438 sprintf(numbuf
, "%d", ++func_nr
);
20439 name
= vim_strsave((char_u
*)numbuf
);
20446 if (fudi
.fd_dict
== NULL
&& vim_strchr(name
, AUTOLOAD_CHAR
) != NULL
)
20449 char_u
*scriptname
;
20451 /* Check that the autoload name matches the script name. */
20453 if (sourcing_name
!= NULL
)
20455 scriptname
= autoload_name(name
);
20456 if (scriptname
!= NULL
)
20458 p
= vim_strchr(scriptname
, '/');
20459 plen
= (int)STRLEN(p
);
20460 slen
= (int)STRLEN(sourcing_name
);
20461 if (slen
> plen
&& fnamecmp(p
,
20462 sourcing_name
+ slen
- plen
) == 0)
20464 vim_free(scriptname
);
20469 EMSG2(_("E746: Function name does not match script file name: %s"), name
);
20474 fp
= (ufunc_T
*)alloc((unsigned)(sizeof(ufunc_T
) + STRLEN(name
)));
20478 if (fudi
.fd_dict
!= NULL
)
20480 if (fudi
.fd_di
== NULL
)
20482 /* add new dict entry */
20483 fudi
.fd_di
= dictitem_alloc(fudi
.fd_newkey
);
20484 if (fudi
.fd_di
== NULL
)
20489 if (dict_add(fudi
.fd_dict
, fudi
.fd_di
) == FAIL
)
20491 vim_free(fudi
.fd_di
);
20497 /* overwrite existing dict entry */
20498 clear_tv(&fudi
.fd_di
->di_tv
);
20499 fudi
.fd_di
->di_tv
.v_type
= VAR_FUNC
;
20500 fudi
.fd_di
->di_tv
.v_lock
= 0;
20501 fudi
.fd_di
->di_tv
.vval
.v_string
= vim_strsave(name
);
20502 fp
->uf_refcount
= 1;
20504 /* behave like "dict" was used */
20508 /* insert the new function in the function list */
20509 STRCPY(fp
->uf_name
, name
);
20510 hash_add(&func_hashtab
, UF2HIKEY(fp
));
20512 fp
->uf_args
= newargs
;
20513 fp
->uf_lines
= newlines
;
20514 #ifdef FEAT_PROFILE
20515 fp
->uf_tml_count
= NULL
;
20516 fp
->uf_tml_total
= NULL
;
20517 fp
->uf_tml_self
= NULL
;
20518 fp
->uf_profiling
= FALSE
;
20519 if (prof_def_func())
20520 func_do_profile(fp
);
20522 fp
->uf_varargs
= varargs
;
20523 fp
->uf_flags
= flags
;
20525 fp
->uf_script_ID
= current_SID
;
20529 ga_clear_strings(&newargs
);
20530 ga_clear_strings(&newlines
);
20532 vim_free(skip_until
);
20533 vim_free(fudi
.fd_newkey
);
20535 did_emsg
|= saved_did_emsg
;
20539 * Get a function name, translating "<SID>" and "<SNR>".
20540 * Also handles a Funcref in a List or Dictionary.
20541 * Returns the function name in allocated memory, or NULL for failure.
20543 * TFN_INT: internal function name OK
20544 * TFN_QUIET: be quiet
20545 * Advances "pp" to just after the function name (if no error).
20548 trans_function_name(pp
, skip
, flags
, fdp
)
20550 int skip
; /* only find the end, don't evaluate */
20552 funcdict_T
*fdp
; /* return: info about dictionary used */
20554 char_u
*name
= NULL
;
20558 char_u sid_buf
[20];
20563 vim_memset(fdp
, 0, sizeof(funcdict_T
));
20566 /* Check for hard coded <SNR>: already translated function ID (from a user
20568 if ((*pp
)[0] == K_SPECIAL
&& (*pp
)[1] == KS_EXTRA
20569 && (*pp
)[2] == (int)KE_SNR
)
20572 len
= get_id_len(pp
) + 3;
20573 return vim_strnsave(start
, len
);
20576 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20577 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
20578 lead
= eval_fname_script(start
);
20582 end
= get_lval(start
, NULL
, &lv
, FALSE
, skip
, flags
& TFN_QUIET
,
20583 lead
> 2 ? 0 : FNE_CHECK_START
);
20587 EMSG(_("E129: Function name required"));
20590 if (end
== NULL
|| (lv
.ll_tv
!= NULL
&& (lead
> 2 || lv
.ll_range
)))
20593 * Report an invalid expression in braces, unless the expression
20594 * evaluation has been cancelled due to an aborting error, an
20595 * interrupt, or an exception.
20600 EMSG2(_(e_invarg2
), start
);
20603 *pp
= find_name_end(start
, NULL
, NULL
, FNE_INCL_BR
);
20607 if (lv
.ll_tv
!= NULL
)
20611 fdp
->fd_dict
= lv
.ll_dict
;
20612 fdp
->fd_newkey
= lv
.ll_newkey
;
20613 lv
.ll_newkey
= NULL
;
20614 fdp
->fd_di
= lv
.ll_di
;
20616 if (lv
.ll_tv
->v_type
== VAR_FUNC
&& lv
.ll_tv
->vval
.v_string
!= NULL
)
20618 name
= vim_strsave(lv
.ll_tv
->vval
.v_string
);
20623 if (!skip
&& !(flags
& TFN_QUIET
) && (fdp
== NULL
20624 || lv
.ll_dict
== NULL
|| fdp
->fd_newkey
== NULL
))
20625 EMSG(_(e_funcref
));
20633 if (lv
.ll_name
== NULL
)
20635 /* Error found, but continue after the function name. */
20640 /* Check if the name is a Funcref. If so, use the value. */
20641 if (lv
.ll_exp_name
!= NULL
)
20643 len
= (int)STRLEN(lv
.ll_exp_name
);
20644 name
= deref_func_name(lv
.ll_exp_name
, &len
);
20645 if (name
== lv
.ll_exp_name
)
20650 len
= (int)(end
- *pp
);
20651 name
= deref_func_name(*pp
, &len
);
20657 name
= vim_strsave(name
);
20662 if (lv
.ll_exp_name
!= NULL
)
20664 len
= (int)STRLEN(lv
.ll_exp_name
);
20665 if (lead
<= 2 && lv
.ll_name
== lv
.ll_exp_name
20666 && STRNCMP(lv
.ll_name
, "s:", 2) == 0)
20668 /* When there was "s:" already or the name expanded to get a
20669 * leading "s:" then remove it. */
20677 if (lead
== 2) /* skip over "s:" */
20679 len
= (int)(end
- lv
.ll_name
);
20683 * Copy the function name to allocated memory.
20684 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20685 * Accept <SNR>123_name() outside a script.
20688 lead
= 0; /* do nothing */
20692 if ((lv
.ll_exp_name
!= NULL
&& eval_fname_sid(lv
.ll_exp_name
))
20693 || eval_fname_sid(*pp
))
20695 /* It's "s:" or "<SID>" */
20696 if (current_SID
<= 0)
20698 EMSG(_(e_usingsid
));
20701 sprintf((char *)sid_buf
, "%ld_", (long)current_SID
);
20702 lead
+= (int)STRLEN(sid_buf
);
20705 else if (!(flags
& TFN_INT
) && builtin_function(lv
.ll_name
))
20707 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv
.ll_name
);
20710 name
= alloc((unsigned)(len
+ lead
+ 1));
20715 name
[0] = K_SPECIAL
;
20716 name
[1] = KS_EXTRA
;
20717 name
[2] = (int)KE_SNR
;
20718 if (lead
> 3) /* If it's "<SID>" */
20719 STRCPY(name
+ 3, sid_buf
);
20721 mch_memmove(name
+ lead
, lv
.ll_name
, (size_t)len
);
20722 name
[len
+ lead
] = NUL
;
20732 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20733 * Return 2 if "p" starts with "s:".
20734 * Return 0 otherwise.
20737 eval_fname_script(p
)
20740 if (p
[0] == '<' && (STRNICMP(p
+ 1, "SID>", 4) == 0
20741 || STRNICMP(p
+ 1, "SNR>", 4) == 0))
20743 if (p
[0] == 's' && p
[1] == ':')
20749 * Return TRUE if "p" starts with "<SID>" or "s:".
20750 * Only works if eval_fname_script() returned non-zero for "p"!
20756 return (*p
== 's' || TOUPPER_ASC(p
[2]) == 'I');
20760 * List the head of the function: "name(arg1, arg2)".
20763 list_func_head(fp
, indent
)
20772 MSG_PUTS("function ");
20773 if (fp
->uf_name
[0] == K_SPECIAL
)
20775 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8
));
20776 msg_puts(fp
->uf_name
+ 3);
20779 msg_puts(fp
->uf_name
);
20781 for (j
= 0; j
< fp
->uf_args
.ga_len
; ++j
)
20785 msg_puts(FUNCARG(fp
, j
));
20787 if (fp
->uf_varargs
)
20796 last_set_msg(fp
->uf_script_ID
);
20800 * Find a function by name, return pointer to it in ufuncs.
20801 * Return NULL for unknown function.
20809 hi
= hash_find(&func_hashtab
, name
);
20810 if (!HASHITEM_EMPTY(hi
))
20815 #if defined(EXITFREE) || defined(PROTO)
20817 free_all_functions()
20821 /* Need to start all over every time, because func_free() may change the
20823 while (func_hashtab
.ht_used
> 0)
20824 for (hi
= func_hashtab
.ht_array
; ; ++hi
)
20825 if (!HASHITEM_EMPTY(hi
))
20827 func_free(HI2UF(hi
));
20834 * Return TRUE if a function "name" exists.
20837 function_exists(name
)
20844 p
= trans_function_name(&nm
, FALSE
, TFN_INT
|TFN_QUIET
, NULL
);
20845 nm
= skipwhite(nm
);
20847 /* Only accept "funcname", "funcname ", "funcname (..." and
20848 * "funcname(...", not "funcname!...". */
20849 if (p
!= NULL
&& (*nm
== NUL
|| *nm
== '('))
20851 if (builtin_function(p
))
20852 n
= (find_internal_func(p
) >= 0);
20854 n
= (find_func(p
) != NULL
);
20861 * Return TRUE if "name" looks like a builtin function name: starts with a
20862 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
20865 builtin_function(name
)
20868 return ASCII_ISLOWER(name
[0]) && vim_strchr(name
, ':') == NULL
20869 && vim_strchr(name
, AUTOLOAD_CHAR
) == NULL
;
20872 #if defined(FEAT_PROFILE) || defined(PROTO)
20874 * Start profiling function "fp".
20877 func_do_profile(fp
)
20880 fp
->uf_tm_count
= 0;
20881 profile_zero(&fp
->uf_tm_self
);
20882 profile_zero(&fp
->uf_tm_total
);
20883 if (fp
->uf_tml_count
== NULL
)
20884 fp
->uf_tml_count
= (int *)alloc_clear((unsigned)
20885 (sizeof(int) * fp
->uf_lines
.ga_len
));
20886 if (fp
->uf_tml_total
== NULL
)
20887 fp
->uf_tml_total
= (proftime_T
*)alloc_clear((unsigned)
20888 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
20889 if (fp
->uf_tml_self
== NULL
)
20890 fp
->uf_tml_self
= (proftime_T
*)alloc_clear((unsigned)
20891 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
20892 fp
->uf_tml_idx
= -1;
20893 if (fp
->uf_tml_count
== NULL
|| fp
->uf_tml_total
== NULL
20894 || fp
->uf_tml_self
== NULL
)
20895 return; /* out of memory */
20897 fp
->uf_profiling
= TRUE
;
20901 * Dump the profiling results for all functions in file "fd".
20904 func_dump_profile(fd
)
20914 todo
= (int)func_hashtab
.ht_used
;
20916 return; /* nothing to dump */
20918 sorttab
= (ufunc_T
**)alloc((unsigned)(sizeof(ufunc_T
) * todo
));
20920 for (hi
= func_hashtab
.ht_array
; todo
> 0; ++hi
)
20922 if (!HASHITEM_EMPTY(hi
))
20926 if (fp
->uf_profiling
)
20928 if (sorttab
!= NULL
)
20929 sorttab
[st_len
++] = fp
;
20931 if (fp
->uf_name
[0] == K_SPECIAL
)
20932 fprintf(fd
, "FUNCTION <SNR>%s()\n", fp
->uf_name
+ 3);
20934 fprintf(fd
, "FUNCTION %s()\n", fp
->uf_name
);
20935 if (fp
->uf_tm_count
== 1)
20936 fprintf(fd
, "Called 1 time\n");
20938 fprintf(fd
, "Called %d times\n", fp
->uf_tm_count
);
20939 fprintf(fd
, "Total time: %s\n", profile_msg(&fp
->uf_tm_total
));
20940 fprintf(fd
, " Self time: %s\n", profile_msg(&fp
->uf_tm_self
));
20942 fprintf(fd
, "count total (s) self (s)\n");
20944 for (i
= 0; i
< fp
->uf_lines
.ga_len
; ++i
)
20946 if (FUNCLINE(fp
, i
) == NULL
)
20948 prof_func_line(fd
, fp
->uf_tml_count
[i
],
20949 &fp
->uf_tml_total
[i
], &fp
->uf_tml_self
[i
], TRUE
);
20950 fprintf(fd
, "%s\n", FUNCLINE(fp
, i
));
20957 if (sorttab
!= NULL
&& st_len
> 0)
20959 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
20961 prof_sort_list(fd
, sorttab
, st_len
, "TOTAL", FALSE
);
20962 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
20964 prof_sort_list(fd
, sorttab
, st_len
, "SELF", TRUE
);
20971 prof_sort_list(fd
, sorttab
, st_len
, title
, prefer_self
)
20976 int prefer_self
; /* when equal print only self time */
20981 fprintf(fd
, "FUNCTIONS SORTED ON %s TIME\n", title
);
20982 fprintf(fd
, "count total (s) self (s) function\n");
20983 for (i
= 0; i
< 20 && i
< st_len
; ++i
)
20986 prof_func_line(fd
, fp
->uf_tm_count
, &fp
->uf_tm_total
, &fp
->uf_tm_self
,
20988 if (fp
->uf_name
[0] == K_SPECIAL
)
20989 fprintf(fd
, " <SNR>%s()\n", fp
->uf_name
+ 3);
20991 fprintf(fd
, " %s()\n", fp
->uf_name
);
20997 * Print the count and times for one function or function line.
21000 prof_func_line(fd
, count
, total
, self
, prefer_self
)
21005 int prefer_self
; /* when equal print only self time */
21009 fprintf(fd
, "%5d ", count
);
21010 if (prefer_self
&& profile_equal(total
, self
))
21013 fprintf(fd
, "%s ", profile_msg(total
));
21014 if (!prefer_self
&& profile_equal(total
, self
))
21017 fprintf(fd
, "%s ", profile_msg(self
));
21024 * Compare function for total time sorting.
21027 #ifdef __BORLANDC__
21030 prof_total_cmp(s1
, s2
)
21036 p1
= *(ufunc_T
**)s1
;
21037 p2
= *(ufunc_T
**)s2
;
21038 return profile_cmp(&p1
->uf_tm_total
, &p2
->uf_tm_total
);
21042 * Compare function for self time sorting.
21045 #ifdef __BORLANDC__
21048 prof_self_cmp(s1
, s2
)
21054 p1
= *(ufunc_T
**)s1
;
21055 p2
= *(ufunc_T
**)s2
;
21056 return profile_cmp(&p1
->uf_tm_self
, &p2
->uf_tm_self
);
21062 * If "name" has a package name try autoloading the script for it.
21063 * Return TRUE if a package was loaded.
21066 script_autoload(name
, reload
)
21068 int reload
; /* load script again when already loaded */
21071 char_u
*scriptname
, *tofree
;
21075 /* If there is no '#' after name[0] there is no package name. */
21076 p
= vim_strchr(name
, AUTOLOAD_CHAR
);
21077 if (p
== NULL
|| p
== name
)
21080 tofree
= scriptname
= autoload_name(name
);
21082 /* Find the name in the list of previously loaded package names. Skip
21083 * "autoload/", it's always the same. */
21084 for (i
= 0; i
< ga_loaded
.ga_len
; ++i
)
21085 if (STRCMP(((char_u
**)ga_loaded
.ga_data
)[i
] + 9, scriptname
+ 9) == 0)
21087 if (!reload
&& i
< ga_loaded
.ga_len
)
21088 ret
= FALSE
; /* was loaded already */
21091 /* Remember the name if it wasn't loaded already. */
21092 if (i
== ga_loaded
.ga_len
&& ga_grow(&ga_loaded
, 1) == OK
)
21094 ((char_u
**)ga_loaded
.ga_data
)[ga_loaded
.ga_len
++] = scriptname
;
21098 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
21099 if (source_runtime(scriptname
, FALSE
) == OK
)
21108 * Return the autoload script name for a function or variable name.
21109 * Returns NULL when out of memory.
21112 autoload_name(name
)
21116 char_u
*scriptname
;
21118 /* Get the script file name: replace '#' with '/', append ".vim". */
21119 scriptname
= alloc((unsigned)(STRLEN(name
) + 14));
21120 if (scriptname
== NULL
)
21122 STRCPY(scriptname
, "autoload/");
21123 STRCAT(scriptname
, name
);
21124 *vim_strrchr(scriptname
, AUTOLOAD_CHAR
) = NUL
;
21125 STRCAT(scriptname
, ".vim");
21126 while ((p
= vim_strchr(scriptname
, AUTOLOAD_CHAR
)) != NULL
)
21131 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21134 * Function given to ExpandGeneric() to obtain the list of user defined
21138 get_user_func_name(xp
, idx
)
21142 static long_u done
;
21143 static hashitem_T
*hi
;
21149 hi
= func_hashtab
.ht_array
;
21151 if (done
< func_hashtab
.ht_used
)
21155 while (HASHITEM_EMPTY(hi
))
21159 if (STRLEN(fp
->uf_name
) + 4 >= IOSIZE
)
21160 return fp
->uf_name
; /* prevents overflow */
21162 cat_func_name(IObuff
, fp
);
21163 if (xp
->xp_context
!= EXPAND_USER_FUNC
)
21165 STRCAT(IObuff
, "(");
21166 if (!fp
->uf_varargs
&& fp
->uf_args
.ga_len
== 0)
21167 STRCAT(IObuff
, ")");
21174 #endif /* FEAT_CMDL_COMPL */
21177 * Copy the function name of "fp" to buffer "buf".
21178 * "buf" must be able to hold the function name plus three bytes.
21179 * Takes care of script-local function names.
21182 cat_func_name(buf
, fp
)
21186 if (fp
->uf_name
[0] == K_SPECIAL
)
21188 STRCPY(buf
, "<SNR>");
21189 STRCAT(buf
, fp
->uf_name
+ 3);
21192 STRCPY(buf
, fp
->uf_name
);
21196 * ":delfunction {name}"
21199 ex_delfunction(eap
)
21202 ufunc_T
*fp
= NULL
;
21208 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
21209 vim_free(fudi
.fd_newkey
);
21212 if (fudi
.fd_dict
!= NULL
&& !eap
->skip
)
21213 EMSG(_(e_funcref
));
21216 if (!ends_excmd(*skipwhite(p
)))
21219 EMSG(_(e_trailing
));
21222 eap
->nextcmd
= check_nextcmd(p
);
21223 if (eap
->nextcmd
!= NULL
)
21227 fp
= find_func(name
);
21234 EMSG2(_(e_nofunc
), eap
->arg
);
21237 if (fp
->uf_calls
> 0)
21239 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap
->arg
);
21243 if (fudi
.fd_dict
!= NULL
)
21245 /* Delete the dict item that refers to the function, it will
21246 * invoke func_unref() and possibly delete the function. */
21247 dictitem_remove(fudi
.fd_dict
, fudi
.fd_di
);
21255 * Free a function and remove it from the list of functions.
21263 /* clear this function */
21264 ga_clear_strings(&(fp
->uf_args
));
21265 ga_clear_strings(&(fp
->uf_lines
));
21266 #ifdef FEAT_PROFILE
21267 vim_free(fp
->uf_tml_count
);
21268 vim_free(fp
->uf_tml_total
);
21269 vim_free(fp
->uf_tml_self
);
21272 /* remove the function from the function hashtable */
21273 hi
= hash_find(&func_hashtab
, UF2HIKEY(fp
));
21274 if (HASHITEM_EMPTY(hi
))
21275 EMSG2(_(e_intern2
), "func_free()");
21277 hash_remove(&func_hashtab
, hi
);
21283 * Unreference a Function: decrement the reference count and free it when it
21284 * becomes zero. Only for numbered functions.
21292 if (name
!= NULL
&& isdigit(*name
))
21294 fp
= find_func(name
);
21296 EMSG2(_(e_intern2
), "func_unref()");
21297 else if (--fp
->uf_refcount
<= 0)
21299 /* Only delete it when it's not being used. Otherwise it's done
21300 * when "uf_calls" becomes zero. */
21301 if (fp
->uf_calls
== 0)
21308 * Count a reference to a Function.
21316 if (name
!= NULL
&& isdigit(*name
))
21318 fp
= find_func(name
);
21320 EMSG2(_(e_intern2
), "func_ref()");
21327 * Call a user function.
21330 call_user_func(fp
, argcount
, argvars
, rettv
, firstline
, lastline
, selfdict
)
21331 ufunc_T
*fp
; /* pointer to function */
21332 int argcount
; /* nr of args */
21333 typval_T
*argvars
; /* arguments */
21334 typval_T
*rettv
; /* return value */
21335 linenr_T firstline
; /* first line of range */
21336 linenr_T lastline
; /* last line of range */
21337 dict_T
*selfdict
; /* Dictionary for "self" */
21339 char_u
*save_sourcing_name
;
21340 linenr_T save_sourcing_lnum
;
21341 scid_T save_current_SID
;
21344 static int depth
= 0;
21346 int fixvar_idx
= 0; /* index in fixvar[] */
21349 char_u numbuf
[NUMBUFLEN
];
21351 #ifdef FEAT_PROFILE
21352 proftime_T wait_start
;
21353 proftime_T call_start
;
21356 /* If depth of calling is getting too high, don't execute the function */
21357 if (depth
>= p_mfd
)
21359 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
21360 rettv
->v_type
= VAR_NUMBER
;
21361 rettv
->vval
.v_number
= -1;
21366 line_breakcheck(); /* check for CTRL-C hit */
21368 fc
= (funccall_T
*)alloc(sizeof(funccall_T
));
21369 fc
->caller
= current_funccal
;
21370 current_funccal
= fc
;
21373 rettv
->vval
.v_number
= 0;
21375 fc
->returned
= FALSE
;
21376 fc
->level
= ex_nesting_level
;
21377 /* Check if this function has a breakpoint. */
21378 fc
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
, (linenr_T
)0);
21379 fc
->dbg_tick
= debug_tick
;
21382 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
21383 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21384 * each argument variable and saves a lot of time.
21387 * Init l: variables.
21389 init_var_dict(&fc
->l_vars
, &fc
->l_vars_var
);
21390 if (selfdict
!= NULL
)
21392 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21393 * some compiler that checks the destination size. */
21394 v
= &fc
->fixvar
[fixvar_idx
++].var
;
21396 STRCPY(name
, "self");
21397 v
->di_flags
= DI_FLAGS_RO
+ DI_FLAGS_FIX
;
21398 hash_add(&fc
->l_vars
.dv_hashtab
, DI2HIKEY(v
));
21399 v
->di_tv
.v_type
= VAR_DICT
;
21400 v
->di_tv
.v_lock
= 0;
21401 v
->di_tv
.vval
.v_dict
= selfdict
;
21402 ++selfdict
->dv_refcount
;
21406 * Init a: variables.
21407 * Set a:0 to "argcount".
21408 * Set a:000 to a list with room for the "..." arguments.
21410 init_var_dict(&fc
->l_avars
, &fc
->l_avars_var
);
21411 add_nr_var(&fc
->l_avars
, &fc
->fixvar
[fixvar_idx
++].var
, "0",
21412 (varnumber_T
)(argcount
- fp
->uf_args
.ga_len
));
21413 /* Use "name" to avoid a warning from some compiler that checks the
21414 * destination size. */
21415 v
= &fc
->fixvar
[fixvar_idx
++].var
;
21417 STRCPY(name
, "000");
21418 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21419 hash_add(&fc
->l_avars
.dv_hashtab
, DI2HIKEY(v
));
21420 v
->di_tv
.v_type
= VAR_LIST
;
21421 v
->di_tv
.v_lock
= VAR_FIXED
;
21422 v
->di_tv
.vval
.v_list
= &fc
->l_varlist
;
21423 vim_memset(&fc
->l_varlist
, 0, sizeof(list_T
));
21424 fc
->l_varlist
.lv_refcount
= DO_NOT_FREE_CNT
;
21425 fc
->l_varlist
.lv_lock
= VAR_FIXED
;
21428 * Set a:firstline to "firstline" and a:lastline to "lastline".
21429 * Set a:name to named arguments.
21430 * Set a:N to the "..." arguments.
21432 add_nr_var(&fc
->l_avars
, &fc
->fixvar
[fixvar_idx
++].var
, "firstline",
21433 (varnumber_T
)firstline
);
21434 add_nr_var(&fc
->l_avars
, &fc
->fixvar
[fixvar_idx
++].var
, "lastline",
21435 (varnumber_T
)lastline
);
21436 for (i
= 0; i
< argcount
; ++i
)
21438 ai
= i
- fp
->uf_args
.ga_len
;
21440 /* named argument a:name */
21441 name
= FUNCARG(fp
, i
);
21444 /* "..." argument a:1, a:2, etc. */
21445 sprintf((char *)numbuf
, "%d", ai
+ 1);
21448 if (fixvar_idx
< FIXVAR_CNT
&& STRLEN(name
) <= VAR_SHORT_LEN
)
21450 v
= &fc
->fixvar
[fixvar_idx
++].var
;
21451 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21455 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
21459 v
->di_flags
= DI_FLAGS_RO
;
21461 STRCPY(v
->di_key
, name
);
21462 hash_add(&fc
->l_avars
.dv_hashtab
, DI2HIKEY(v
));
21464 /* Note: the values are copied directly to avoid alloc/free.
21465 * "argvars" must have VAR_FIXED for v_lock. */
21466 v
->di_tv
= argvars
[i
];
21467 v
->di_tv
.v_lock
= VAR_FIXED
;
21469 if (ai
>= 0 && ai
< MAX_FUNC_ARGS
)
21471 list_append(&fc
->l_varlist
, &fc
->l_listitems
[ai
]);
21472 fc
->l_listitems
[ai
].li_tv
= argvars
[i
];
21473 fc
->l_listitems
[ai
].li_tv
.v_lock
= VAR_FIXED
;
21477 /* Don't redraw while executing the function. */
21478 ++RedrawingDisabled
;
21479 save_sourcing_name
= sourcing_name
;
21480 save_sourcing_lnum
= sourcing_lnum
;
21482 sourcing_name
= alloc((unsigned)((save_sourcing_name
== NULL
? 0
21483 : STRLEN(save_sourcing_name
)) + STRLEN(fp
->uf_name
) + 13));
21484 if (sourcing_name
!= NULL
)
21486 if (save_sourcing_name
!= NULL
21487 && STRNCMP(save_sourcing_name
, "function ", 9) == 0)
21488 sprintf((char *)sourcing_name
, "%s..", save_sourcing_name
);
21490 STRCPY(sourcing_name
, "function ");
21491 cat_func_name(sourcing_name
+ STRLEN(sourcing_name
), fp
);
21493 if (p_verbose
>= 12)
21496 verbose_enter_scroll();
21498 smsg((char_u
*)_("calling %s"), sourcing_name
);
21499 if (p_verbose
>= 14)
21501 char_u buf
[MSG_BUF_LEN
];
21502 char_u numbuf2
[NUMBUFLEN
];
21506 msg_puts((char_u
*)"(");
21507 for (i
= 0; i
< argcount
; ++i
)
21510 msg_puts((char_u
*)", ");
21511 if (argvars
[i
].v_type
== VAR_NUMBER
)
21512 msg_outnum((long)argvars
[i
].vval
.v_number
);
21515 s
= tv2string(&argvars
[i
], &tofree
, numbuf2
, 0);
21518 trunc_string(s
, buf
, MSG_BUF_CLEN
);
21524 msg_puts((char_u
*)")");
21526 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21528 verbose_leave_scroll();
21532 #ifdef FEAT_PROFILE
21533 if (do_profiling
== PROF_YES
)
21535 if (!fp
->uf_profiling
&& has_profiling(FALSE
, fp
->uf_name
, NULL
))
21536 func_do_profile(fp
);
21537 if (fp
->uf_profiling
21538 || (fc
->caller
!= NULL
&& fc
->caller
->func
->uf_profiling
))
21541 profile_start(&call_start
);
21542 profile_zero(&fp
->uf_tm_children
);
21544 script_prof_save(&wait_start
);
21548 save_current_SID
= current_SID
;
21549 current_SID
= fp
->uf_script_ID
;
21550 save_did_emsg
= did_emsg
;
21553 /* call do_cmdline() to execute the lines */
21554 do_cmdline(NULL
, get_func_line
, (void *)fc
,
21555 DOCMD_NOWAIT
|DOCMD_VERBOSE
|DOCMD_REPEAT
);
21557 --RedrawingDisabled
;
21559 /* when the function was aborted because of an error, return -1 */
21560 if ((did_emsg
&& (fp
->uf_flags
& FC_ABORT
)) || rettv
->v_type
== VAR_UNKNOWN
)
21563 rettv
->v_type
= VAR_NUMBER
;
21564 rettv
->vval
.v_number
= -1;
21567 #ifdef FEAT_PROFILE
21568 if (do_profiling
== PROF_YES
&& (fp
->uf_profiling
21569 || (fc
->caller
!= NULL
&& fc
->caller
->func
->uf_profiling
)))
21571 profile_end(&call_start
);
21572 profile_sub_wait(&wait_start
, &call_start
);
21573 profile_add(&fp
->uf_tm_total
, &call_start
);
21574 profile_self(&fp
->uf_tm_self
, &call_start
, &fp
->uf_tm_children
);
21575 if (fc
->caller
!= NULL
&& fc
->caller
->func
->uf_profiling
)
21577 profile_add(&fc
->caller
->func
->uf_tm_children
, &call_start
);
21578 profile_add(&fc
->caller
->func
->uf_tml_children
, &call_start
);
21583 /* when being verbose, mention the return value */
21584 if (p_verbose
>= 12)
21587 verbose_enter_scroll();
21590 smsg((char_u
*)_("%s aborted"), sourcing_name
);
21591 else if (fc
->rettv
->v_type
== VAR_NUMBER
)
21592 smsg((char_u
*)_("%s returning #%ld"), sourcing_name
,
21593 (long)fc
->rettv
->vval
.v_number
);
21596 char_u buf
[MSG_BUF_LEN
];
21597 char_u numbuf2
[NUMBUFLEN
];
21601 /* The value may be very long. Skip the middle part, so that we
21602 * have some idea how it starts and ends. smsg() would always
21603 * truncate it at the end. */
21604 s
= tv2string(fc
->rettv
, &tofree
, numbuf2
, 0);
21607 trunc_string(s
, buf
, MSG_BUF_CLEN
);
21608 smsg((char_u
*)_("%s returning %s"), sourcing_name
, buf
);
21612 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21614 verbose_leave_scroll();
21618 vim_free(sourcing_name
);
21619 sourcing_name
= save_sourcing_name
;
21620 sourcing_lnum
= save_sourcing_lnum
;
21621 current_SID
= save_current_SID
;
21622 #ifdef FEAT_PROFILE
21623 if (do_profiling
== PROF_YES
)
21624 script_prof_restore(&wait_start
);
21627 if (p_verbose
>= 12 && sourcing_name
!= NULL
)
21630 verbose_enter_scroll();
21632 smsg((char_u
*)_("continuing in %s"), sourcing_name
);
21633 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21635 verbose_leave_scroll();
21639 did_emsg
|= save_did_emsg
;
21640 current_funccal
= fc
->caller
;
21643 /* if the a:000 list and the a: dict are not referenced we can free the
21644 * funccall_T and what's in it. */
21645 if (fc
->l_varlist
.lv_refcount
== DO_NOT_FREE_CNT
21646 && fc
->l_vars
.dv_refcount
== DO_NOT_FREE_CNT
21647 && fc
->l_avars
.dv_refcount
== DO_NOT_FREE_CNT
)
21649 free_funccal(fc
, FALSE
);
21657 /* "fc" is still in use. This can happen when returning "a:000" or
21658 * assigning "l:" to a global variable.
21659 * Link "fc" in the list for garbage collection later. */
21660 fc
->caller
= previous_funccal
;
21661 previous_funccal
= fc
;
21663 /* Make a copy of the a: variables, since we didn't do that above. */
21664 todo
= (int)fc
->l_avars
.dv_hashtab
.ht_used
;
21665 for (hi
= fc
->l_avars
.dv_hashtab
.ht_array
; todo
> 0; ++hi
)
21667 if (!HASHITEM_EMPTY(hi
))
21671 copy_tv(&v
->di_tv
, &v
->di_tv
);
21675 /* Make a copy of the a:000 items, since we didn't do that above. */
21676 for (li
= fc
->l_varlist
.lv_first
; li
!= NULL
; li
= li
->li_next
)
21677 copy_tv(&li
->li_tv
, &li
->li_tv
);
21682 * Return TRUE if items in "fc" do not have "copyID". That means they are not
21683 * referenced from anywyere.
21686 can_free_funccal(fc
, copyID
)
21690 return (fc
->l_varlist
.lv_copyID
!= copyID
21691 && fc
->l_vars
.dv_copyID
!= copyID
21692 && fc
->l_avars
.dv_copyID
!= copyID
);
21696 * Free "fc" and what it contains.
21699 free_funccal(fc
, free_val
)
21701 int free_val
; /* a: vars were allocated */
21705 /* The a: variables typevals may not have been allocated, only free the
21706 * allocated variables. */
21707 vars_clear_ext(&fc
->l_avars
.dv_hashtab
, free_val
);
21709 /* free all l: variables */
21710 vars_clear(&fc
->l_vars
.dv_hashtab
);
21712 /* Free the a:000 variables if they were allocated. */
21714 for (li
= fc
->l_varlist
.lv_first
; li
!= NULL
; li
= li
->li_next
)
21715 clear_tv(&li
->li_tv
);
21721 * Add a number variable "name" to dict "dp" with value "nr".
21724 add_nr_var(dp
, v
, name
, nr
)
21730 STRCPY(v
->di_key
, name
);
21731 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21732 hash_add(&dp
->dv_hashtab
, DI2HIKEY(v
));
21733 v
->di_tv
.v_type
= VAR_NUMBER
;
21734 v
->di_tv
.v_lock
= VAR_FIXED
;
21735 v
->di_tv
.vval
.v_number
= nr
;
21745 char_u
*arg
= eap
->arg
;
21747 int returning
= FALSE
;
21749 if (current_funccal
== NULL
)
21751 EMSG(_("E133: :return not inside a function"));
21758 eap
->nextcmd
= NULL
;
21759 if ((*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
21760 && eval0(arg
, &rettv
, &eap
->nextcmd
, !eap
->skip
) != FAIL
)
21763 returning
= do_return(eap
, FALSE
, TRUE
, &rettv
);
21767 /* It's safer to return also on error. */
21768 else if (!eap
->skip
)
21771 * Return unless the expression evaluation has been cancelled due to an
21772 * aborting error, an interrupt, or an exception.
21775 returning
= do_return(eap
, FALSE
, TRUE
, NULL
);
21778 /* When skipping or the return gets pending, advance to the next command
21779 * in this line (!returning). Otherwise, ignore the rest of the line.
21780 * Following lines will be ignored by get_func_line(). */
21782 eap
->nextcmd
= NULL
;
21783 else if (eap
->nextcmd
== NULL
) /* no argument */
21784 eap
->nextcmd
= check_nextcmd(arg
);
21791 * Return from a function. Possibly makes the return pending. Also called
21792 * for a pending return at the ":endtry" or after returning from an extra
21793 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
21794 * when called due to a ":return" command. "rettv" may point to a typval_T
21795 * with the return rettv. Returns TRUE when the return can be carried out,
21796 * FALSE when the return gets pending.
21799 do_return(eap
, reanimate
, is_cmd
, rettv
)
21806 struct condstack
*cstack
= eap
->cstack
;
21809 /* Undo the return. */
21810 current_funccal
->returned
= FALSE
;
21813 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21814 * not in its finally clause (which then is to be executed next) is found.
21815 * In this case, make the ":return" pending for execution at the ":endtry".
21816 * Otherwise, return normally.
21818 idx
= cleanup_conditionals(eap
->cstack
, 0, TRUE
);
21821 cstack
->cs_pending
[idx
] = CSTP_RETURN
;
21823 if (!is_cmd
&& !reanimate
)
21824 /* A pending return again gets pending. "rettv" points to an
21825 * allocated variable with the rettv of the original ":return"'s
21826 * argument if present or is NULL else. */
21827 cstack
->cs_rettv
[idx
] = rettv
;
21830 /* When undoing a return in order to make it pending, get the stored
21833 rettv
= current_funccal
->rettv
;
21837 /* Store the value of the pending return. */
21838 if ((cstack
->cs_rettv
[idx
] = alloc_tv()) != NULL
)
21839 *(typval_T
*)cstack
->cs_rettv
[idx
] = *(typval_T
*)rettv
;
21841 EMSG(_(e_outofmem
));
21844 cstack
->cs_rettv
[idx
] = NULL
;
21848 /* The pending return value could be overwritten by a ":return"
21849 * without argument in a finally clause; reset the default
21851 current_funccal
->rettv
->v_type
= VAR_NUMBER
;
21852 current_funccal
->rettv
->vval
.v_number
= 0;
21855 report_make_pending(CSTP_RETURN
, rettv
);
21859 current_funccal
->returned
= TRUE
;
21861 /* If the return is carried out now, store the return value. For
21862 * a return immediately after reanimation, the value is already
21864 if (!reanimate
&& rettv
!= NULL
)
21866 clear_tv(current_funccal
->rettv
);
21867 *current_funccal
->rettv
= *(typval_T
*)rettv
;
21877 * Free the variable with a pending return value.
21880 discard_pending_return(rettv
)
21883 free_tv((typval_T
*)rettv
);
21887 * Generate a return command for producing the value of "rettv". The result
21888 * is an allocated string. Used by report_pending() for verbose messages.
21891 get_return_cmd(rettv
)
21895 char_u
*tofree
= NULL
;
21896 char_u numbuf
[NUMBUFLEN
];
21899 s
= echo_string((typval_T
*)rettv
, &tofree
, numbuf
, 0);
21903 STRCPY(IObuff
, ":return ");
21904 STRNCPY(IObuff
+ 8, s
, IOSIZE
- 8);
21905 if (STRLEN(s
) + 8 >= IOSIZE
)
21906 STRCPY(IObuff
+ IOSIZE
- 4, "...");
21908 return vim_strsave(IObuff
);
21912 * Get next function line.
21913 * Called by do_cmdline() to get the next line.
21914 * Returns allocated string, or NULL for end of function.
21918 get_func_line(c
, cookie
, indent
)
21919 int c
; /* not used */
21921 int indent
; /* not used */
21923 funccall_T
*fcp
= (funccall_T
*)cookie
;
21924 ufunc_T
*fp
= fcp
->func
;
21926 garray_T
*gap
; /* growarray with function lines */
21928 /* If breakpoints have been added/deleted need to check for it. */
21929 if (fcp
->dbg_tick
!= debug_tick
)
21931 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
21933 fcp
->dbg_tick
= debug_tick
;
21935 #ifdef FEAT_PROFILE
21936 if (do_profiling
== PROF_YES
)
21937 func_line_end(cookie
);
21940 gap
= &fp
->uf_lines
;
21941 if (((fp
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
21946 /* Skip NULL lines (continuation lines). */
21947 while (fcp
->linenr
< gap
->ga_len
21948 && ((char_u
**)(gap
->ga_data
))[fcp
->linenr
] == NULL
)
21950 if (fcp
->linenr
>= gap
->ga_len
)
21954 retval
= vim_strsave(((char_u
**)(gap
->ga_data
))[fcp
->linenr
++]);
21955 sourcing_lnum
= fcp
->linenr
;
21956 #ifdef FEAT_PROFILE
21957 if (do_profiling
== PROF_YES
)
21958 func_line_start(cookie
);
21963 /* Did we encounter a breakpoint? */
21964 if (fcp
->breakpoint
!= 0 && fcp
->breakpoint
<= sourcing_lnum
)
21966 dbg_breakpoint(fp
->uf_name
, sourcing_lnum
);
21967 /* Find next breakpoint. */
21968 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
21970 fcp
->dbg_tick
= debug_tick
;
21976 #if defined(FEAT_PROFILE) || defined(PROTO)
21978 * Called when starting to read a function line.
21979 * "sourcing_lnum" must be correct!
21980 * When skipping lines it may not actually be executed, but we won't find out
21981 * until later and we need to store the time now.
21984 func_line_start(cookie
)
21987 funccall_T
*fcp
= (funccall_T
*)cookie
;
21988 ufunc_T
*fp
= fcp
->func
;
21990 if (fp
->uf_profiling
&& sourcing_lnum
>= 1
21991 && sourcing_lnum
<= fp
->uf_lines
.ga_len
)
21993 fp
->uf_tml_idx
= sourcing_lnum
- 1;
21994 /* Skip continuation lines. */
21995 while (fp
->uf_tml_idx
> 0 && FUNCLINE(fp
, fp
->uf_tml_idx
) == NULL
)
21997 fp
->uf_tml_execed
= FALSE
;
21998 profile_start(&fp
->uf_tml_start
);
21999 profile_zero(&fp
->uf_tml_children
);
22000 profile_get_wait(&fp
->uf_tml_wait
);
22005 * Called when actually executing a function line.
22008 func_line_exec(cookie
)
22011 funccall_T
*fcp
= (funccall_T
*)cookie
;
22012 ufunc_T
*fp
= fcp
->func
;
22014 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
22015 fp
->uf_tml_execed
= TRUE
;
22019 * Called when done with a function line.
22022 func_line_end(cookie
)
22025 funccall_T
*fcp
= (funccall_T
*)cookie
;
22026 ufunc_T
*fp
= fcp
->func
;
22028 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
22030 if (fp
->uf_tml_execed
)
22032 ++fp
->uf_tml_count
[fp
->uf_tml_idx
];
22033 profile_end(&fp
->uf_tml_start
);
22034 profile_sub_wait(&fp
->uf_tml_wait
, &fp
->uf_tml_start
);
22035 profile_add(&fp
->uf_tml_total
[fp
->uf_tml_idx
], &fp
->uf_tml_start
);
22036 profile_self(&fp
->uf_tml_self
[fp
->uf_tml_idx
], &fp
->uf_tml_start
,
22037 &fp
->uf_tml_children
);
22039 fp
->uf_tml_idx
= -1;
22045 * Return TRUE if the currently active function should be ended, because a
22046 * return was encountered or an error occurred. Used inside a ":while".
22049 func_has_ended(cookie
)
22052 funccall_T
*fcp
= (funccall_T
*)cookie
;
22054 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22055 * an error inside a try conditional. */
22056 return (((fcp
->func
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
22061 * return TRUE if cookie indicates a function which "abort"s on errors.
22064 func_has_abort(cookie
)
22067 return ((funccall_T
*)cookie
)->func
->uf_flags
& FC_ABORT
;
22070 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22073 VAR_FLAVOUR_DEFAULT
, /* doesn't start with uppercase */
22074 VAR_FLAVOUR_SESSION
, /* starts with uppercase, some lower */
22075 VAR_FLAVOUR_VIMINFO
/* all uppercase */
22078 static var_flavour_T var_flavour
__ARGS((char_u
*varname
));
22080 static var_flavour_T
22081 var_flavour(varname
)
22084 char_u
*p
= varname
;
22086 if (ASCII_ISUPPER(*p
))
22089 if (ASCII_ISLOWER(*p
))
22090 return VAR_FLAVOUR_SESSION
;
22091 return VAR_FLAVOUR_VIMINFO
;
22094 return VAR_FLAVOUR_DEFAULT
;
22098 #if defined(FEAT_VIMINFO) || defined(PROTO)
22100 * Restore global vars that start with a capital from the viminfo file
22103 read_viminfo_varlist(virp
, writing
)
22108 int type
= VAR_NUMBER
;
22111 if (!writing
&& (find_viminfo_parameter('!') != NULL
))
22113 tab
= vim_strchr(virp
->vir_line
+ 1, '\t');
22116 *tab
++ = '\0'; /* isolate the variable name */
22117 if (*tab
== 'S') /* string var */
22120 else if (*tab
== 'F')
22124 tab
= vim_strchr(tab
, '\t');
22128 if (type
== VAR_STRING
)
22129 tv
.vval
.v_string
= viminfo_readstring(virp
,
22130 (int)(tab
- virp
->vir_line
+ 1), TRUE
);
22132 else if (type
== VAR_FLOAT
)
22133 (void)string2float(tab
+ 1, &tv
.vval
.v_float
);
22136 tv
.vval
.v_number
= atol((char *)tab
+ 1);
22137 set_var(virp
->vir_line
+ 1, &tv
, FALSE
);
22138 if (type
== VAR_STRING
)
22139 vim_free(tv
.vval
.v_string
);
22144 return viminfo_readline(virp
);
22148 * Write global vars that start with a capital to the viminfo file
22151 write_viminfo_varlist(fp
)
22155 dictitem_T
*this_var
;
22160 char_u numbuf
[NUMBUFLEN
];
22162 if (find_viminfo_parameter('!') == NULL
)
22165 fprintf(fp
, _("\n# global variables:\n"));
22167 todo
= (int)globvarht
.ht_used
;
22168 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
22170 if (!HASHITEM_EMPTY(hi
))
22173 this_var
= HI2DI(hi
);
22174 if (var_flavour(this_var
->di_key
) == VAR_FLAVOUR_VIMINFO
)
22176 switch (this_var
->di_tv
.v_type
)
22178 case VAR_STRING
: s
= "STR"; break;
22179 case VAR_NUMBER
: s
= "NUM"; break;
22181 case VAR_FLOAT
: s
= "FLO"; break;
22185 fprintf(fp
, "!%s\t%s\t", this_var
->di_key
, s
);
22186 p
= echo_string(&this_var
->di_tv
, &tofree
, numbuf
, 0);
22188 viminfo_writestring(fp
, p
);
22196 #if defined(FEAT_SESSION) || defined(PROTO)
22198 store_session_globals(fd
)
22202 dictitem_T
*this_var
;
22206 todo
= (int)globvarht
.ht_used
;
22207 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
22209 if (!HASHITEM_EMPTY(hi
))
22212 this_var
= HI2DI(hi
);
22213 if ((this_var
->di_tv
.v_type
== VAR_NUMBER
22214 || this_var
->di_tv
.v_type
== VAR_STRING
)
22215 && var_flavour(this_var
->di_key
) == VAR_FLAVOUR_SESSION
)
22217 /* Escape special characters with a backslash. Turn a LF and
22218 * CR into \n and \r. */
22219 p
= vim_strsave_escaped(get_tv_string(&this_var
->di_tv
),
22220 (char_u
*)"\\\"\n\r");
22221 if (p
== NULL
) /* out of memory */
22223 for (t
= p
; *t
!= NUL
; ++t
)
22226 else if (*t
== '\r')
22228 if ((fprintf(fd
, "let %s = %c%s%c",
22230 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
22233 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
22235 || put_eol(fd
) == FAIL
)
22243 else if (this_var
->di_tv
.v_type
== VAR_FLOAT
22244 && var_flavour(this_var
->di_key
) == VAR_FLAVOUR_SESSION
)
22246 float_T f
= this_var
->di_tv
.vval
.v_float
;
22254 if ((fprintf(fd
, "let %s = %c&%f",
22255 this_var
->di_key
, sign
, f
) < 0)
22256 || put_eol(fd
) == FAIL
)
22267 * Display script name where an item was last set.
22268 * Should only be invoked when 'verbose' is non-zero.
22271 last_set_msg(scriptID
)
22278 p
= home_replace_save(NULL
, get_scriptname(scriptID
));
22282 MSG_PUTS(_("\n\tLast set from "));
22291 * List v:oldfiles in a nice way.
22298 list_T
*l
= vimvars
[VV_OLDFILES
].vv_list
;
22303 msg((char_u
*)_("No old files"));
22308 for (li
= l
->lv_first
; li
!= NULL
&& !got_int
; li
= li
->li_next
)
22310 msg_outnum((long)++nr
);
22312 msg_outtrans(get_tv_string(&li
->li_tv
));
22314 out_flush(); /* output one line at a time */
22317 /* Assume "got_int" was set to truncate the listing. */
22320 #ifdef FEAT_BROWSE_CMD
22324 nr
= prompt_for_number(FALSE
);
22328 char_u
*p
= list_find_str(get_vim_var_list(VV_OLDFILES
),
22333 p
= expand_env_save(p
);
22335 eap
->cmdidx
= CMD_edit
;
22336 cmdmod
.browse
= FALSE
;
22337 do_exedit(eap
, NULL
);
22346 #endif /* FEAT_EVAL */
22349 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
22353 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22355 static int get_short_pathname
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
22356 static int shortpath_for_invalid_fname
__ARGS((char_u
**fname
, char_u
**bufp
, int *fnamelen
));
22357 static int shortpath_for_partial
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
22360 * Get the short path (8.3) for the filename in "fnamep".
22361 * Only works for a valid file name.
22362 * When the path gets longer "fnamep" is changed and the allocated buffer
22363 * is put in "bufp".
22364 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22365 * Returns OK on success, FAIL on failure.
22368 get_short_pathname(fnamep
, bufp
, fnamelen
)
22377 l
= GetShortPathName(*fnamep
, *fnamep
, len
);
22380 /* If that doesn't work (not enough space), then save the string
22381 * and try again with a new buffer big enough. */
22382 newbuf
= vim_strnsave(*fnamep
, l
);
22383 if (newbuf
== NULL
)
22387 *fnamep
= *bufp
= newbuf
;
22389 /* Really should always succeed, as the buffer is big enough. */
22390 l
= GetShortPathName(*fnamep
, *fnamep
, l
+1);
22398 * Get the short path (8.3) for the filename in "fname". The converted
22399 * path is returned in "bufp".
22401 * Some of the directories specified in "fname" may not exist. This function
22402 * will shorten the existing directories at the beginning of the path and then
22403 * append the remaining non-existing path.
22405 * fname - Pointer to the filename to shorten. On return, contains the
22406 * pointer to the shortened pathname
22407 * bufp - Pointer to an allocated buffer for the filename.
22408 * fnamelen - Length of the filename pointed to by fname
22410 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
22413 shortpath_for_invalid_fname(fname
, bufp
, fnamelen
)
22418 char_u
*short_fname
, *save_fname
, *pbuf_unused
;
22419 char_u
*endp
, *save_endp
;
22422 int new_len
, sfx_len
;
22426 old_len
= *fnamelen
;
22427 save_fname
= vim_strnsave(*fname
, old_len
);
22428 pbuf_unused
= NULL
;
22429 short_fname
= NULL
;
22431 endp
= save_fname
+ old_len
- 1; /* Find the end of the copy */
22435 * Try shortening the supplied path till it succeeds by removing one
22436 * directory at a time from the tail of the path.
22441 /* go back one path-separator */
22442 while (endp
> save_fname
&& !after_pathsep(save_fname
, endp
+ 1))
22444 if (endp
<= save_fname
)
22445 break; /* processed the complete path */
22448 * Replace the path separator with a NUL and try to shorten the
22453 short_fname
= save_fname
;
22454 len
= (int)STRLEN(short_fname
) + 1;
22455 if (get_short_pathname(&short_fname
, &pbuf_unused
, &len
) == FAIL
)
22460 *endp
= ch
; /* preserve the string */
22463 break; /* successfully shortened the path */
22465 /* failed to shorten the path. Skip the path separator */
22472 * Succeeded in shortening the path. Now concatenate the shortened
22473 * path with the remaining path at the tail.
22476 /* Compute the length of the new path. */
22477 sfx_len
= (int)(save_endp
- endp
) + 1;
22478 new_len
= len
+ sfx_len
;
22480 *fnamelen
= new_len
;
22482 if (new_len
> old_len
)
22484 /* There is not enough space in the currently allocated string,
22485 * copy it to a buffer big enough. */
22486 *fname
= *bufp
= vim_strnsave(short_fname
, new_len
);
22487 if (*fname
== NULL
)
22495 /* Transfer short_fname to the main buffer (it's big enough),
22496 * unless get_short_pathname() did its work in-place. */
22497 *fname
= *bufp
= save_fname
;
22498 if (short_fname
!= save_fname
)
22499 vim_strncpy(save_fname
, short_fname
, len
);
22503 /* concat the not-shortened part of the path */
22504 vim_strncpy(*fname
+ len
, endp
, sfx_len
);
22505 (*fname
)[new_len
] = NUL
;
22509 vim_free(pbuf_unused
);
22510 vim_free(save_fname
);
22516 * Get a pathname for a partial path.
22517 * Returns OK for success, FAIL for failure.
22520 shortpath_for_partial(fnamep
, bufp
, fnamelen
)
22525 int sepcount
, len
, tflen
;
22527 char_u
*pbuf
, *tfname
;
22530 /* Count up the path separators from the RHS.. so we know which part
22531 * of the path to return. */
22533 for (p
= *fnamep
; p
< *fnamep
+ *fnamelen
; mb_ptr_adv(p
))
22534 if (vim_ispathsep(*p
))
22537 /* Need full path first (use expand_env() to remove a "~/") */
22538 hasTilde
= (**fnamep
== '~');
22540 pbuf
= tfname
= expand_env_save(*fnamep
);
22542 pbuf
= tfname
= FullName_save(*fnamep
, FALSE
);
22544 len
= tflen
= (int)STRLEN(tfname
);
22546 if (get_short_pathname(&tfname
, &pbuf
, &len
) == FAIL
)
22551 /* Don't have a valid filename, so shorten the rest of the
22552 * path if we can. This CAN give us invalid 8.3 filenames, but
22553 * there's not a lot of point in guessing what it might be.
22556 if (shortpath_for_invalid_fname(&tfname
, &pbuf
, &len
) == FAIL
)
22560 /* Count the paths backward to find the beginning of the desired string. */
22561 for (p
= tfname
+ len
- 1; p
>= tfname
; --p
)
22565 p
-= mb_head_off(tfname
, p
);
22567 if (vim_ispathsep(*p
))
22569 if (sepcount
== 0 || (hasTilde
&& sepcount
== 1))
22586 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22588 *fnamelen
= (int)STRLEN(p
);
22594 #endif /* WIN3264 */
22597 * Adjust a filename, according to a string of modifiers.
22598 * *fnamep must be NUL terminated when called. When returning, the length is
22599 * determined by *fnamelen.
22600 * Returns VALID_ flags or -1 for failure.
22601 * When there is an error, *fnamep is set to NULL.
22604 modify_fname(src
, usedlen
, fnamep
, bufp
, fnamelen
)
22605 char_u
*src
; /* string with modifiers */
22606 int *usedlen
; /* characters after src that are used */
22607 char_u
**fnamep
; /* file name so far */
22608 char_u
**bufp
; /* buffer for allocated file name or NULL */
22609 int *fnamelen
; /* length of fnamep */
22613 char_u
*s
, *p
, *pbuf
;
22614 char_u dirname
[MAXPATHL
];
22616 int has_fullname
= 0;
22618 int has_shortname
= 0;
22622 /* ":p" - full path/file_name */
22623 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'p')
22627 valid
|= VALID_PATH
;
22630 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22631 if ((*fnamep
)[0] == '~'
22632 #if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22633 && ((*fnamep
)[1] == '/'
22634 # ifdef BACKSLASH_IN_FILENAME
22635 || (*fnamep
)[1] == '\\'
22637 || (*fnamep
)[1] == NUL
)
22642 *fnamep
= expand_env_save(*fnamep
);
22643 vim_free(*bufp
); /* free any allocated file name */
22645 if (*fnamep
== NULL
)
22649 /* When "/." or "/.." is used: force expansion to get rid of it. */
22650 for (p
= *fnamep
; *p
!= NUL
; mb_ptr_adv(p
))
22652 if (vim_ispathsep(*p
)
22655 || vim_ispathsep(p
[2])
22657 && (p
[3] == NUL
|| vim_ispathsep(p
[3])))))
22661 /* FullName_save() is slow, don't use it when not needed. */
22662 if (*p
!= NUL
|| !vim_isAbsName(*fnamep
))
22664 *fnamep
= FullName_save(*fnamep
, *p
!= NUL
);
22665 vim_free(*bufp
); /* free any allocated file name */
22667 if (*fnamep
== NULL
)
22671 /* Append a path separator to a directory. */
22672 if (mch_isdir(*fnamep
))
22674 /* Make room for one or two extra characters. */
22675 *fnamep
= vim_strnsave(*fnamep
, (int)STRLEN(*fnamep
) + 2);
22676 vim_free(*bufp
); /* free any allocated file name */
22678 if (*fnamep
== NULL
)
22680 add_pathsep(*fnamep
);
22684 /* ":." - path relative to the current directory */
22685 /* ":~" - path relative to the home directory */
22686 /* ":8" - shortname path - postponed till after */
22687 while (src
[*usedlen
] == ':'
22688 && ((c
= src
[*usedlen
+ 1]) == '.' || c
== '~' || c
== '8'))
22694 has_shortname
= 1; /* Postpone this. */
22699 /* Need full path first (use expand_env() to remove a "~/") */
22702 if (c
== '.' && **fnamep
== '~')
22703 p
= pbuf
= expand_env_save(*fnamep
);
22705 p
= pbuf
= FullName_save(*fnamep
, FALSE
);
22716 mch_dirname(dirname
, MAXPATHL
);
22717 s
= shorten_fname(p
, dirname
);
22723 vim_free(*bufp
); /* free any allocated file name */
22731 home_replace(NULL
, p
, dirname
, MAXPATHL
, TRUE
);
22732 /* Only replace it when it starts with '~' */
22733 if (*dirname
== '~')
22735 s
= vim_strsave(dirname
);
22748 tail
= gettail(*fnamep
);
22749 *fnamelen
= (int)STRLEN(*fnamep
);
22751 /* ":h" - head, remove "/file_name", can be repeated */
22752 /* Don't remove the first "/" or "c:\" */
22753 while (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'h')
22755 valid
|= VALID_HEAD
;
22757 s
= get_past_head(*fnamep
);
22758 while (tail
> s
&& after_pathsep(s
, tail
))
22759 mb_ptr_back(*fnamep
, tail
);
22760 *fnamelen
= (int)(tail
- *fnamep
);
22763 *fnamelen
+= 1; /* the path separator is part of the path */
22765 if (*fnamelen
== 0)
22767 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22768 p
= vim_strsave((char_u
*)".");
22772 *bufp
= *fnamep
= tail
= p
;
22777 while (tail
> s
&& !after_pathsep(s
, tail
))
22778 mb_ptr_back(*fnamep
, tail
);
22782 /* ":8" - shortname */
22783 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == '8')
22792 /* Check shortname after we have done 'heads' and before we do 'tails'
22797 /* Copy the string if it is shortened by :h */
22798 if (*fnamelen
< (int)STRLEN(*fnamep
))
22800 p
= vim_strnsave(*fnamep
, *fnamelen
);
22804 *bufp
= *fnamep
= p
;
22807 /* Split into two implementations - makes it easier. First is where
22808 * there isn't a full name already, second is where there is.
22810 if (!has_fullname
&& !vim_isAbsName(*fnamep
))
22812 if (shortpath_for_partial(fnamep
, bufp
, fnamelen
) == FAIL
)
22819 /* Simple case, already have the full-name
22820 * Nearly always shorter, so try first time. */
22822 if (get_short_pathname(fnamep
, bufp
, &l
) == FAIL
)
22827 /* Couldn't find the filename.. search the paths.
22830 if (shortpath_for_invalid_fname(fnamep
, bufp
, &l
) == FAIL
)
22836 #endif /* WIN3264 */
22838 /* ":t" - tail, just the basename */
22839 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 't')
22842 *fnamelen
-= (int)(tail
- *fnamep
);
22846 /* ":e" - extension, can be repeated */
22847 /* ":r" - root, without extension, can be repeated */
22848 while (src
[*usedlen
] == ':'
22849 && (src
[*usedlen
+ 1] == 'e' || src
[*usedlen
+ 1] == 'r'))
22851 /* find a '.' in the tail:
22852 * - for second :e: before the current fname
22853 * - otherwise: The last '.'
22855 if (src
[*usedlen
+ 1] == 'e' && *fnamep
> tail
)
22858 s
= *fnamep
+ *fnamelen
- 1;
22859 for ( ; s
> tail
; --s
)
22862 if (src
[*usedlen
+ 1] == 'e') /* :e */
22866 *fnamelen
+= (int)(*fnamep
- (s
+ 1));
22869 /* cut version from the extension */
22870 s
= *fnamep
+ *fnamelen
- 1;
22871 for ( ; s
> *fnamep
; --s
)
22875 *fnamelen
= s
- *fnamep
;
22878 else if (*fnamep
<= tail
)
22883 if (s
> tail
) /* remove one extension */
22884 *fnamelen
= (int)(s
- *fnamep
);
22889 /* ":s?pat?foo?" - substitute */
22890 /* ":gs?pat?foo?" - global substitute */
22891 if (src
[*usedlen
] == ':'
22892 && (src
[*usedlen
+ 1] == 's'
22893 || (src
[*usedlen
+ 1] == 'g' && src
[*usedlen
+ 2] == 's')))
22902 flags
= (char_u
*)"";
22903 s
= src
+ *usedlen
+ 2;
22904 if (src
[*usedlen
+ 1] == 'g')
22906 flags
= (char_u
*)"g";
22913 /* find end of pattern */
22914 p
= vim_strchr(s
, sep
);
22917 pat
= vim_strnsave(s
, (int)(p
- s
));
22921 /* find end of substitution */
22922 p
= vim_strchr(s
, sep
);
22925 sub
= vim_strnsave(s
, (int)(p
- s
));
22926 str
= vim_strnsave(*fnamep
, *fnamelen
);
22927 if (sub
!= NULL
&& str
!= NULL
)
22929 *usedlen
= (int)(p
+ 1 - src
);
22930 s
= do_string_sub(str
, pat
, sub
, flags
);
22934 *fnamelen
= (int)STRLEN(s
);
22946 /* after using ":s", repeat all the modifiers */
22956 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22957 * "flags" can be "g" to do a global substitute.
22958 * Returns an allocated string, NULL for error.
22961 do_string_sub(str
, pat
, sub
, flags
)
22968 regmatch_T regmatch
;
22976 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22978 p_cpo
= empty_option
;
22980 ga_init2(&ga
, 1, 200);
22982 do_all
= (flags
[0] == 'g');
22984 regmatch
.rm_ic
= p_ic
;
22985 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
22986 if (regmatch
.regprog
!= NULL
)
22989 while (vim_regexec_nl(®match
, str
, (colnr_T
)(tail
- str
)))
22992 * Get some space for a temporary buffer to do the substitution
22993 * into. It will contain:
22994 * - The text up to where the match is.
22995 * - The substituted text.
22996 * - The text after the match.
22998 sublen
= vim_regsub(®match
, sub
, tail
, FALSE
, TRUE
, FALSE
);
22999 if (ga_grow(&ga
, (int)(STRLEN(tail
) + sublen
-
23000 (regmatch
.endp
[0] - regmatch
.startp
[0]))) == FAIL
)
23006 /* copy the text up to where the match is */
23007 i
= (int)(regmatch
.startp
[0] - tail
);
23008 mch_memmove((char_u
*)ga
.ga_data
+ ga
.ga_len
, tail
, (size_t)i
);
23009 /* add the substituted text */
23010 (void)vim_regsub(®match
, sub
, (char_u
*)ga
.ga_data
23011 + ga
.ga_len
+ i
, TRUE
, TRUE
, FALSE
);
23012 ga
.ga_len
+= i
+ sublen
- 1;
23013 /* avoid getting stuck on a match with an empty string */
23014 if (tail
== regmatch
.endp
[0])
23018 *((char_u
*)ga
.ga_data
+ ga
.ga_len
) = *tail
++;
23023 tail
= regmatch
.endp
[0];
23031 if (ga
.ga_data
!= NULL
)
23032 STRCPY((char *)ga
.ga_data
+ ga
.ga_len
, tail
);
23034 vim_free(regmatch
.regprog
);
23037 ret
= vim_strsave(ga
.ga_data
== NULL
? str
: (char_u
*)ga
.ga_data
);
23039 if (p_cpo
== empty_option
)
23042 /* Darn, evaluating {sub} expression changed the value. */
23043 free_string_option(save_cpo
);
23048 #endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */