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.
132 * The last bit is used for previous_funccal, ignored when comparing.
134 static int current_copyID
= 0;
136 #define COPYID_MASK (~0x1)
139 * Array to hold the hashtab with variables local to each sourced script.
140 * Each item holds a variable (nameless) that points to the dict_T.
148 static garray_T ga_scripts
= {0, 0, sizeof(scriptvar_T
), 4, NULL
};
149 #define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
150 #define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
152 static int echo_attr
= 0; /* attributes used for ":echo" */
154 /* Values for trans_function_name() argument: */
155 #define TFN_INT 1 /* internal function name OK */
156 #define TFN_QUIET 2 /* no error messages */
159 * Structure to hold info for a user function.
161 typedef struct ufunc ufunc_T
;
165 int uf_varargs
; /* variable nr of arguments */
167 int uf_calls
; /* nr of active calls */
168 garray_T uf_args
; /* arguments */
169 garray_T uf_lines
; /* function lines */
171 int uf_profiling
; /* TRUE when func is being profiled */
172 /* profiling the function as a whole */
173 int uf_tm_count
; /* nr of calls */
174 proftime_T uf_tm_total
; /* time spent in function + children */
175 proftime_T uf_tm_self
; /* time spent in function itself */
176 proftime_T uf_tm_children
; /* time spent in children this call */
177 /* profiling the function per line */
178 int *uf_tml_count
; /* nr of times line was executed */
179 proftime_T
*uf_tml_total
; /* time spent in a line + children */
180 proftime_T
*uf_tml_self
; /* time spent in a line itself */
181 proftime_T uf_tml_start
; /* start time for current line */
182 proftime_T uf_tml_children
; /* time spent in children for this line */
183 proftime_T uf_tml_wait
; /* start wait time for current line */
184 int uf_tml_idx
; /* index of line being timed; -1 if none */
185 int uf_tml_execed
; /* line being timed was executed */
187 scid_T uf_script_ID
; /* ID of script where function was defined,
188 used for s: variables */
189 int uf_refcount
; /* for numbered function: reference count */
190 char_u uf_name
[1]; /* name of function (actually longer); can
191 start with <SNR>123_ (<SNR> is K_SPECIAL
196 #define FC_ABORT 1 /* abort function on error */
197 #define FC_RANGE 2 /* function accepts range */
198 #define FC_DICT 4 /* Dict function, uses "self" */
201 * All user-defined functions are found in this hashtable.
203 static hashtab_T func_hashtab
;
205 /* The names of packages that once were loaded are remembered. */
206 static garray_T ga_loaded
= {0, 0, sizeof(char_u
*), 4, NULL
};
208 /* list heads for garbage collection */
209 static dict_T
*first_dict
= NULL
; /* list of all dicts */
210 static list_T
*first_list
= NULL
; /* list of all lists */
212 /* From user function to hashitem and back. */
213 static ufunc_T dumuf
;
214 #define UF2HIKEY(fp) ((fp)->uf_name)
215 #define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
216 #define HI2UF(hi) HIKEY2UF((hi)->hi_key)
218 #define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
219 #define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
221 #define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
222 #define VAR_SHORT_LEN 20 /* short variable name length */
223 #define FIXVAR_CNT 12 /* number of fixed variables */
225 /* structure to hold info for a function that is currently being executed. */
226 typedef struct funccall_S funccall_T
;
230 ufunc_T
*func
; /* function being called */
231 int linenr
; /* next line to be executed */
232 int returned
; /* ":return" used */
233 struct /* fixed variables for arguments */
235 dictitem_T var
; /* variable (without room for name) */
236 char_u room
[VAR_SHORT_LEN
]; /* room for the name */
237 } fixvar
[FIXVAR_CNT
];
238 dict_T l_vars
; /* l: local function variables */
239 dictitem_T l_vars_var
; /* variable for l: scope */
240 dict_T l_avars
; /* a: argument variables */
241 dictitem_T l_avars_var
; /* variable for a: scope */
242 list_T l_varlist
; /* list for a:000 */
243 listitem_T l_listitems
[MAX_FUNC_ARGS
]; /* listitems for a:000 */
244 typval_T
*rettv
; /* return value */
245 linenr_T breakpoint
; /* next line with breakpoint or zero */
246 int dbg_tick
; /* debug_tick when breakpoint was set */
247 int level
; /* top nesting level of executed function */
249 proftime_T prof_child
; /* time spent in a child */
251 funccall_T
*caller
; /* calling function or NULL */
255 * Info used by a ":for" loop.
259 int fi_semicolon
; /* TRUE if ending in '; var]' */
260 int fi_varcount
; /* nr of variables in the list */
261 listwatch_T fi_lw
; /* keep an eye on the item used. */
262 list_T
*fi_list
; /* list being used */
266 * Struct used by trans_function_name()
270 dict_T
*fd_dict
; /* Dictionary used */
271 char_u
*fd_newkey
; /* new key in "dict" in allocated memory */
272 dictitem_T
*fd_di
; /* Dictionary item used */
277 * Array to hold the value of v: variables.
278 * The value is in a dictitem, so that it can also be used in the v: scope.
279 * The reason to use this table anyway is for very quick access to the
280 * variables with the VV_ defines.
284 /* values for vv_flags: */
285 #define VV_COMPAT 1 /* compatible, also used without "v:" */
286 #define VV_RO 2 /* read-only */
287 #define VV_RO_SBX 4 /* read-only in the sandbox */
289 #define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
293 char *vv_name
; /* name of variable, without v: */
294 dictitem_T vv_di
; /* value and name for key */
295 char vv_filler
[16]; /* space for LONGEST name below!!! */
296 char vv_flags
; /* VV_COMPAT, VV_RO, VV_RO_SBX */
300 * The order here must match the VV_ defines in vim.h!
301 * Initializing a union does not work, leave tv.vval empty to get zero's.
303 {VV_NAME("count", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
304 {VV_NAME("count1", VAR_NUMBER
), VV_RO
},
305 {VV_NAME("prevcount", VAR_NUMBER
), VV_RO
},
306 {VV_NAME("errmsg", VAR_STRING
), VV_COMPAT
},
307 {VV_NAME("warningmsg", VAR_STRING
), 0},
308 {VV_NAME("statusmsg", VAR_STRING
), 0},
309 {VV_NAME("shell_error", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
310 {VV_NAME("this_session", VAR_STRING
), VV_COMPAT
},
311 {VV_NAME("version", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
312 {VV_NAME("lnum", VAR_NUMBER
), VV_RO_SBX
},
313 {VV_NAME("termresponse", VAR_STRING
), VV_RO
},
314 {VV_NAME("fname", VAR_STRING
), VV_RO
},
315 {VV_NAME("lang", VAR_STRING
), VV_RO
},
316 {VV_NAME("lc_time", VAR_STRING
), VV_RO
},
317 {VV_NAME("ctype", VAR_STRING
), VV_RO
},
318 {VV_NAME("charconvert_from", VAR_STRING
), VV_RO
},
319 {VV_NAME("charconvert_to", VAR_STRING
), VV_RO
},
320 {VV_NAME("fname_in", VAR_STRING
), VV_RO
},
321 {VV_NAME("fname_out", VAR_STRING
), VV_RO
},
322 {VV_NAME("fname_new", VAR_STRING
), VV_RO
},
323 {VV_NAME("fname_diff", VAR_STRING
), VV_RO
},
324 {VV_NAME("cmdarg", VAR_STRING
), VV_RO
},
325 {VV_NAME("foldstart", VAR_NUMBER
), VV_RO_SBX
},
326 {VV_NAME("foldend", VAR_NUMBER
), VV_RO_SBX
},
327 {VV_NAME("folddashes", VAR_STRING
), VV_RO_SBX
},
328 {VV_NAME("foldlevel", VAR_NUMBER
), VV_RO_SBX
},
329 {VV_NAME("progname", VAR_STRING
), VV_RO
},
330 {VV_NAME("servername", VAR_STRING
), VV_RO
},
331 {VV_NAME("dying", VAR_NUMBER
), VV_RO
},
332 {VV_NAME("exception", VAR_STRING
), VV_RO
},
333 {VV_NAME("throwpoint", VAR_STRING
), VV_RO
},
334 {VV_NAME("register", VAR_STRING
), VV_RO
},
335 {VV_NAME("cmdbang", VAR_NUMBER
), VV_RO
},
336 {VV_NAME("insertmode", VAR_STRING
), VV_RO
},
337 {VV_NAME("val", VAR_UNKNOWN
), VV_RO
},
338 {VV_NAME("key", VAR_UNKNOWN
), VV_RO
},
339 {VV_NAME("profiling", VAR_NUMBER
), VV_RO
},
340 {VV_NAME("fcs_reason", VAR_STRING
), VV_RO
},
341 {VV_NAME("fcs_choice", VAR_STRING
), 0},
342 {VV_NAME("beval_bufnr", VAR_NUMBER
), VV_RO
},
343 {VV_NAME("beval_winnr", VAR_NUMBER
), VV_RO
},
344 {VV_NAME("beval_lnum", VAR_NUMBER
), VV_RO
},
345 {VV_NAME("beval_col", VAR_NUMBER
), VV_RO
},
346 {VV_NAME("beval_text", VAR_STRING
), VV_RO
},
347 {VV_NAME("scrollstart", VAR_STRING
), 0},
348 {VV_NAME("swapname", VAR_STRING
), VV_RO
},
349 {VV_NAME("swapchoice", VAR_STRING
), 0},
350 {VV_NAME("swapcommand", VAR_STRING
), VV_RO
},
351 {VV_NAME("char", VAR_STRING
), VV_RO
},
352 {VV_NAME("mouse_win", VAR_NUMBER
), 0},
353 {VV_NAME("mouse_lnum", VAR_NUMBER
), 0},
354 {VV_NAME("mouse_col", VAR_NUMBER
), 0},
355 {VV_NAME("operator", VAR_STRING
), VV_RO
},
356 {VV_NAME("searchforward", VAR_NUMBER
), 0},
357 {VV_NAME("oldfiles", VAR_LIST
), 0},
361 #define vv_type vv_di.di_tv.v_type
362 #define vv_nr vv_di.di_tv.vval.v_number
363 #define vv_float vv_di.di_tv.vval.v_float
364 #define vv_str vv_di.di_tv.vval.v_string
365 #define vv_list vv_di.di_tv.vval.v_list
366 #define vv_tv vv_di.di_tv
369 * The v: variables are stored in dictionary "vimvardict".
370 * "vimvars_var" is the variable that is used for the "l:" scope.
372 static dict_T vimvardict
;
373 static dictitem_T vimvars_var
;
374 #define vimvarht vimvardict.dv_hashtab
376 static void prepare_vimvar
__ARGS((int idx
, typval_T
*save_tv
));
377 static void restore_vimvar
__ARGS((int idx
, typval_T
*save_tv
));
378 #if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
379 static int call_vim_function
__ARGS((char_u
*func
, int argc
, char_u
**argv
, int safe
, typval_T
*rettv
));
381 static int ex_let_vars
__ARGS((char_u
*arg
, typval_T
*tv
, int copy
, int semicolon
, int var_count
, char_u
*nextchars
));
382 static char_u
*skip_var_list
__ARGS((char_u
*arg
, int *var_count
, int *semicolon
));
383 static char_u
*skip_var_one
__ARGS((char_u
*arg
));
384 static void list_hashtable_vars
__ARGS((hashtab_T
*ht
, char_u
*prefix
, int empty
, int *first
));
385 static void list_glob_vars
__ARGS((int *first
));
386 static void list_buf_vars
__ARGS((int *first
));
387 static void list_win_vars
__ARGS((int *first
));
389 static void list_tab_vars
__ARGS((int *first
));
391 static void list_vim_vars
__ARGS((int *first
));
392 static void list_script_vars
__ARGS((int *first
));
393 static void list_func_vars
__ARGS((int *first
));
394 static char_u
*list_arg_vars
__ARGS((exarg_T
*eap
, char_u
*arg
, int *first
));
395 static char_u
*ex_let_one
__ARGS((char_u
*arg
, typval_T
*tv
, int copy
, char_u
*endchars
, char_u
*op
));
396 static int check_changedtick
__ARGS((char_u
*arg
));
397 static char_u
*get_lval
__ARGS((char_u
*name
, typval_T
*rettv
, lval_T
*lp
, int unlet
, int skip
, int quiet
, int fne_flags
));
398 static void clear_lval
__ARGS((lval_T
*lp
));
399 static void set_var_lval
__ARGS((lval_T
*lp
, char_u
*endp
, typval_T
*rettv
, int copy
, char_u
*op
));
400 static int tv_op
__ARGS((typval_T
*tv1
, typval_T
*tv2
, char_u
*op
));
401 static void list_add_watch
__ARGS((list_T
*l
, listwatch_T
*lw
));
402 static void list_rem_watch
__ARGS((list_T
*l
, listwatch_T
*lwrem
));
403 static void list_fix_watch
__ARGS((list_T
*l
, listitem_T
*item
));
404 static void ex_unletlock
__ARGS((exarg_T
*eap
, char_u
*argstart
, int deep
));
405 static int do_unlet_var
__ARGS((lval_T
*lp
, char_u
*name_end
, int forceit
));
406 static int do_lock_var
__ARGS((lval_T
*lp
, char_u
*name_end
, int deep
, int lock
));
407 static void item_lock
__ARGS((typval_T
*tv
, int deep
, int lock
));
408 static int tv_islocked
__ARGS((typval_T
*tv
));
410 static int eval0
__ARGS((char_u
*arg
, typval_T
*rettv
, char_u
**nextcmd
, int evaluate
));
411 static int eval1
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
412 static int eval2
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
413 static int eval3
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
414 static int eval4
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
415 static int eval5
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
416 static int eval6
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int want_string
));
417 static int eval7
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int want_string
));
419 static int eval_index
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int verbose
));
420 static int get_option_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
421 static int get_string_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
422 static int get_lit_string_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
423 static int get_list_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
424 static int rettv_list_alloc
__ARGS((typval_T
*rettv
));
425 static listitem_T
*listitem_alloc
__ARGS((void));
426 static void listitem_free
__ARGS((listitem_T
*item
));
427 static void listitem_remove
__ARGS((list_T
*l
, listitem_T
*item
));
428 static long list_len
__ARGS((list_T
*l
));
429 static int list_equal
__ARGS((list_T
*l1
, list_T
*l2
, int ic
));
430 static int dict_equal
__ARGS((dict_T
*d1
, dict_T
*d2
, int ic
));
431 static int tv_equal
__ARGS((typval_T
*tv1
, typval_T
*tv2
, int ic
));
432 static listitem_T
*list_find
__ARGS((list_T
*l
, long n
));
433 static long list_find_nr
__ARGS((list_T
*l
, long idx
, int *errorp
));
434 static long list_idx_of_item
__ARGS((list_T
*l
, listitem_T
*item
));
435 static void list_append
__ARGS((list_T
*l
, listitem_T
*item
));
436 static int list_append_number
__ARGS((list_T
*l
, varnumber_T n
));
437 static int list_insert_tv
__ARGS((list_T
*l
, typval_T
*tv
, listitem_T
*item
));
438 static int list_extend
__ARGS((list_T
*l1
, list_T
*l2
, listitem_T
*bef
));
439 static int list_concat
__ARGS((list_T
*l1
, list_T
*l2
, typval_T
*tv
));
440 static list_T
*list_copy
__ARGS((list_T
*orig
, int deep
, int copyID
));
441 static void list_remove
__ARGS((list_T
*l
, listitem_T
*item
, listitem_T
*item2
));
442 static char_u
*list2string
__ARGS((typval_T
*tv
, int copyID
));
443 static int list_join
__ARGS((garray_T
*gap
, list_T
*l
, char_u
*sep
, int echo
, int copyID
));
444 static int free_unref_items
__ARGS((int copyID
));
445 static void set_ref_in_ht
__ARGS((hashtab_T
*ht
, int copyID
));
446 static void set_ref_in_list
__ARGS((list_T
*l
, int copyID
));
447 static void set_ref_in_item
__ARGS((typval_T
*tv
, int copyID
));
448 static void dict_unref
__ARGS((dict_T
*d
));
449 static void dict_free
__ARGS((dict_T
*d
, int recurse
));
450 static dictitem_T
*dictitem_copy
__ARGS((dictitem_T
*org
));
451 static void dictitem_remove
__ARGS((dict_T
*dict
, dictitem_T
*item
));
452 static dict_T
*dict_copy
__ARGS((dict_T
*orig
, int deep
, int copyID
));
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
));
643 static void f_mzeval
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
645 static void f_nextnonblank
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
646 static void f_nr2char
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
647 static void f_pathshorten
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
649 static void f_pow
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
651 static void f_prevnonblank
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
652 static void f_printf
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
653 static void f_pumvisible
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
654 static void f_range
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
655 static void f_readfile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
656 static void f_reltime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
657 static void f_reltimestr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
658 static void f_remote_expr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
659 static void f_remote_foreground
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
660 static void f_remote_peek
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
661 static void f_remote_read
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
662 static void f_remote_send
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
663 static void f_remove
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
664 static void f_rename
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
665 static void f_repeat
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
666 static void f_resolve
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
667 static void f_reverse
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
669 static void f_round
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
671 static void f_search
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
672 static void f_searchdecl
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
673 static void f_searchpair
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
674 static void f_searchpairpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
675 static void f_searchpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
676 static void f_server2client
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
677 static void f_serverlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
678 static void f_setbufvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
679 static void f_setcmdpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
680 static void f_setline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
681 static void f_setloclist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
682 static void f_setmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
683 static void f_setpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
684 static void f_setqflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
685 static void f_setreg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
686 static void f_settabwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
687 static void f_setwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
688 static void f_shellescape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
689 static void f_simplify
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
691 static void f_sin
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
693 static void f_sort
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
694 static void f_soundfold
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
695 static void f_spellbadword
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
696 static void f_spellsuggest
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
697 static void f_split
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
699 static void f_sqrt
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
700 static void f_str2float
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
702 static void f_str2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
704 static void f_strftime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
706 static void f_stridx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
707 static void f_string
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
708 static void f_strlen
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
709 static void f_strpart
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
710 static void f_strridx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
711 static void f_strtrans
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
712 static void f_submatch
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
713 static void f_substitute
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
714 static void f_synID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
715 static void f_synIDattr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
716 static void f_synIDtrans
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
717 static void f_synstack
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
718 static void f_system
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
719 static void f_tabpagebuflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
720 static void f_tabpagenr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
721 static void f_tabpagewinnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
722 static void f_taglist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
723 static void f_tagfiles
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
724 static void f_tempname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
725 static void f_test
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
726 static void f_tolower
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
727 static void f_toupper
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
728 static void f_tr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
730 static void f_trunc
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
732 static void f_type
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
733 static void f_values
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
734 static void f_virtcol
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
735 static void f_visualmode
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
736 static void f_winbufnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
737 static void f_wincol
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
738 static void f_winheight
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
739 static void f_winline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
740 static void f_winnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
741 static void f_winrestcmd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
742 static void f_winrestview
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
743 static void f_winsaveview
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
744 static void f_winwidth
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
745 static void f_writefile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
747 static int list2fpos
__ARGS((typval_T
*arg
, pos_T
*posp
, int *fnump
));
748 static pos_T
*var2fpos
__ARGS((typval_T
*varp
, int dollar_lnum
, int *fnum
));
749 static int get_env_len
__ARGS((char_u
**arg
));
750 static int get_id_len
__ARGS((char_u
**arg
));
751 static int get_name_len
__ARGS((char_u
**arg
, char_u
**alias
, int evaluate
, int verbose
));
752 static char_u
*find_name_end
__ARGS((char_u
*arg
, char_u
**expr_start
, char_u
**expr_end
, int flags
));
753 #define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
754 #define FNE_CHECK_START 2 /* find_name_end(): check name starts with
756 static char_u
* make_expanded_name
__ARGS((char_u
*in_start
, char_u
*expr_start
, char_u
*expr_end
, char_u
*in_end
));
757 static int eval_isnamec
__ARGS((int c
));
758 static int eval_isnamec1
__ARGS((int c
));
759 static int get_var_tv
__ARGS((char_u
*name
, int len
, typval_T
*rettv
, int verbose
));
760 static int handle_subscript
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int verbose
));
761 static typval_T
*alloc_tv
__ARGS((void));
762 static typval_T
*alloc_string_tv
__ARGS((char_u
*string
));
763 static void init_tv
__ARGS((typval_T
*varp
));
764 static long get_tv_number
__ARGS((typval_T
*varp
));
765 static linenr_T get_tv_lnum
__ARGS((typval_T
*argvars
));
766 static linenr_T get_tv_lnum_buf
__ARGS((typval_T
*argvars
, buf_T
*buf
));
767 static char_u
*get_tv_string
__ARGS((typval_T
*varp
));
768 static char_u
*get_tv_string_buf
__ARGS((typval_T
*varp
, char_u
*buf
));
769 static char_u
*get_tv_string_buf_chk
__ARGS((typval_T
*varp
, char_u
*buf
));
770 static dictitem_T
*find_var
__ARGS((char_u
*name
, hashtab_T
**htp
));
771 static dictitem_T
*find_var_in_ht
__ARGS((hashtab_T
*ht
, char_u
*varname
, int writing
));
772 static hashtab_T
*find_var_ht
__ARGS((char_u
*name
, char_u
**varname
));
773 static void vars_clear_ext
__ARGS((hashtab_T
*ht
, int free_val
));
774 static void delete_var
__ARGS((hashtab_T
*ht
, hashitem_T
*hi
));
775 static void list_one_var
__ARGS((dictitem_T
*v
, char_u
*prefix
, int *first
));
776 static void list_one_var_a
__ARGS((char_u
*prefix
, char_u
*name
, int type
, char_u
*string
, int *first
));
777 static void set_var
__ARGS((char_u
*name
, typval_T
*varp
, int copy
));
778 static int var_check_ro
__ARGS((int flags
, char_u
*name
));
779 static int var_check_fixed
__ARGS((int flags
, char_u
*name
));
780 static int tv_check_lock
__ARGS((int lock
, char_u
*name
));
781 static int item_copy
__ARGS((typval_T
*from
, typval_T
*to
, int deep
, int copyID
));
782 static char_u
*find_option_end
__ARGS((char_u
**arg
, int *opt_flags
));
783 static char_u
*trans_function_name
__ARGS((char_u
**pp
, int skip
, int flags
, funcdict_T
*fd
));
784 static int eval_fname_script
__ARGS((char_u
*p
));
785 static int eval_fname_sid
__ARGS((char_u
*p
));
786 static void list_func_head
__ARGS((ufunc_T
*fp
, int indent
));
787 static ufunc_T
*find_func
__ARGS((char_u
*name
));
788 static int function_exists
__ARGS((char_u
*name
));
789 static int builtin_function
__ARGS((char_u
*name
));
791 static void func_do_profile
__ARGS((ufunc_T
*fp
));
792 static void prof_sort_list
__ARGS((FILE *fd
, ufunc_T
**sorttab
, int st_len
, char *title
, int prefer_self
));
793 static void prof_func_line
__ARGS((FILE *fd
, int count
, proftime_T
*total
, proftime_T
*self
, int prefer_self
));
798 prof_total_cmp
__ARGS((const void *s1
, const void *s2
));
803 prof_self_cmp
__ARGS((const void *s1
, const void *s2
));
805 static int script_autoload
__ARGS((char_u
*name
, int reload
));
806 static char_u
*autoload_name
__ARGS((char_u
*name
));
807 static void cat_func_name
__ARGS((char_u
*buf
, ufunc_T
*fp
));
808 static void func_free
__ARGS((ufunc_T
*fp
));
809 static void func_unref
__ARGS((char_u
*name
));
810 static void func_ref
__ARGS((char_u
*name
));
811 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
));
812 static int can_free_funccal
__ARGS((funccall_T
*fc
, int copyID
)) ;
813 static void free_funccal
__ARGS((funccall_T
*fc
, int free_val
));
814 static void add_nr_var
__ARGS((dict_T
*dp
, dictitem_T
*v
, char *name
, varnumber_T nr
));
815 static win_T
*find_win_by_nr
__ARGS((typval_T
*vp
, tabpage_T
*tp
));
816 static void getwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int off
));
817 static int searchpair_cmn
__ARGS((typval_T
*argvars
, pos_T
*match_pos
));
818 static int search_cmn
__ARGS((typval_T
*argvars
, pos_T
*match_pos
, int *flagsp
));
819 static void setwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int off
));
821 /* Character used as separated in autoload function/variable names. */
822 #define AUTOLOAD_CHAR '#'
825 * Initialize the global and v: variables.
833 init_var_dict(&globvardict
, &globvars_var
);
834 init_var_dict(&vimvardict
, &vimvars_var
);
835 hash_init(&compat_hashtab
);
836 hash_init(&func_hashtab
);
838 for (i
= 0; i
< VV_LEN
; ++i
)
841 STRCPY(p
->vv_di
.di_key
, p
->vv_name
);
842 if (p
->vv_flags
& VV_RO
)
843 p
->vv_di
.di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
844 else if (p
->vv_flags
& VV_RO_SBX
)
845 p
->vv_di
.di_flags
= DI_FLAGS_RO_SBX
| DI_FLAGS_FIX
;
847 p
->vv_di
.di_flags
= DI_FLAGS_FIX
;
849 /* add to v: scope dict, unless the value is not always available */
850 if (p
->vv_type
!= VAR_UNKNOWN
)
851 hash_add(&vimvarht
, p
->vv_di
.di_key
);
852 if (p
->vv_flags
& VV_COMPAT
)
853 /* add to compat scope dict */
854 hash_add(&compat_hashtab
, p
->vv_di
.di_key
);
856 set_vim_var_nr(VV_SEARCHFORWARD
, 1L);
859 #if defined(EXITFREE) || defined(PROTO)
866 for (i
= 0; i
< VV_LEN
; ++i
)
869 if (p
->vv_di
.di_tv
.v_type
== VAR_STRING
)
874 else if (p
->vv_di
.di_tv
.v_type
== VAR_LIST
)
876 list_unref(p
->vv_list
);
880 hash_clear(&vimvarht
);
881 hash_init(&vimvarht
); /* garbage_collect() will access it */
882 hash_clear(&compat_hashtab
);
884 /* script-local variables */
885 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
886 vars_clear(&SCRIPT_VARS(i
));
887 ga_clear(&ga_scripts
);
890 /* global variables */
891 vars_clear(&globvarht
);
893 /* autoloaded script names */
894 ga_clear_strings(&ga_loaded
);
896 /* unreferenced lists and dicts */
897 (void)garbage_collect();
900 free_all_functions();
901 hash_clear(&func_hashtab
);
906 * Return the name of the executed function.
912 return ((funccall_T
*)cookie
)->func
->uf_name
;
916 * Return the address holding the next breakpoint line for a funccall cookie.
919 func_breakpoint(cookie
)
922 return &((funccall_T
*)cookie
)->breakpoint
;
926 * Return the address holding the debug tick for a funccall cookie.
929 func_dbg_tick(cookie
)
932 return &((funccall_T
*)cookie
)->dbg_tick
;
936 * Return the nesting level for a funccall cookie.
942 return ((funccall_T
*)cookie
)->level
;
945 /* pointer to funccal for currently active function */
946 funccall_T
*current_funccal
= NULL
;
948 /* pointer to list of previously used funccal, still around because some
949 * item in it is still being used. */
950 funccall_T
*previous_funccal
= NULL
;
953 * Return TRUE when a function was ended by a ":return" command.
956 current_func_returned()
958 return current_funccal
->returned
;
963 * Set an internal variable to a string value. Creates the variable if it does
967 set_internal_string_var(name
, value
)
974 val
= vim_strsave(value
);
977 tvp
= alloc_string_tv(val
);
980 set_var(name
, tvp
, FALSE
);
986 static lval_T
*redir_lval
= NULL
;
987 static garray_T redir_ga
; /* only valid when redir_lval is not NULL */
988 static char_u
*redir_endp
= NULL
;
989 static char_u
*redir_varname
= NULL
;
992 * Start recording command output to a variable
993 * Returns OK if successfully completed the setup. FAIL otherwise.
996 var_redir_start(name
, append
)
998 int append
; /* append to an existing variable */
1004 /* Catch a bad name early. */
1005 if (!eval_isnamec1(*name
))
1011 /* Make a copy of the name, it is used in redir_lval until redir ends. */
1012 redir_varname
= vim_strsave(name
);
1013 if (redir_varname
== NULL
)
1016 redir_lval
= (lval_T
*)alloc_clear((unsigned)sizeof(lval_T
));
1017 if (redir_lval
== NULL
)
1023 /* The output is stored in growarray "redir_ga" until redirection ends. */
1024 ga_init2(&redir_ga
, (int)sizeof(char), 500);
1026 /* Parse the variable name (can be a dict or list entry). */
1027 redir_endp
= get_lval(redir_varname
, NULL
, redir_lval
, FALSE
, FALSE
, FALSE
,
1029 if (redir_endp
== NULL
|| redir_lval
->ll_name
== NULL
|| *redir_endp
!= NUL
)
1031 if (redir_endp
!= NULL
&& *redir_endp
!= NUL
)
1032 /* Trailing characters are present after the variable name */
1033 EMSG(_(e_trailing
));
1036 redir_endp
= NULL
; /* don't store a value, only cleanup */
1041 /* check if we can write to the variable: set it to or append an empty
1043 save_emsg
= did_emsg
;
1045 tv
.v_type
= VAR_STRING
;
1046 tv
.vval
.v_string
= (char_u
*)"";
1048 set_var_lval(redir_lval
, redir_endp
, &tv
, TRUE
, (char_u
*)".");
1050 set_var_lval(redir_lval
, redir_endp
, &tv
, TRUE
, (char_u
*)"=");
1052 did_emsg
|= save_emsg
;
1055 redir_endp
= NULL
; /* don't store a value, only cleanup */
1059 if (redir_lval
->ll_newkey
!= NULL
)
1061 /* Dictionary item was created, don't do it again. */
1062 vim_free(redir_lval
->ll_newkey
);
1063 redir_lval
->ll_newkey
= NULL
;
1070 * Append "value[value_len]" to the variable set by var_redir_start().
1071 * The actual appending is postponed until redirection ends, because the value
1072 * appended may in fact be the string we write to, changing it may cause freed
1073 * memory to be used:
1079 var_redir_str(value
, value_len
)
1085 if (redir_lval
== NULL
)
1088 if (value_len
== -1)
1089 len
= (int)STRLEN(value
); /* Append the entire string */
1091 len
= value_len
; /* Append only "value_len" characters */
1093 if (ga_grow(&redir_ga
, len
) == OK
)
1095 mch_memmove((char *)redir_ga
.ga_data
+ redir_ga
.ga_len
, value
, len
);
1096 redir_ga
.ga_len
+= len
;
1103 * Stop redirecting command output to a variable.
1104 * Frees the allocated memory.
1111 if (redir_lval
!= NULL
)
1113 /* If there was no error: assign the text to the variable. */
1114 if (redir_endp
!= NULL
)
1116 ga_append(&redir_ga
, NUL
); /* Append the trailing NUL. */
1117 tv
.v_type
= VAR_STRING
;
1118 tv
.vval
.v_string
= redir_ga
.ga_data
;
1119 set_var_lval(redir_lval
, redir_endp
, &tv
, FALSE
, (char_u
*)".");
1122 /* free the collected output */
1123 vim_free(redir_ga
.ga_data
);
1124 redir_ga
.ga_data
= NULL
;
1126 clear_lval(redir_lval
);
1127 vim_free(redir_lval
);
1130 vim_free(redir_varname
);
1131 redir_varname
= NULL
;
1134 # if defined(FEAT_MBYTE) || defined(PROTO)
1136 eval_charconvert(enc_from
, enc_to
, fname_from
, fname_to
)
1144 set_vim_var_string(VV_CC_FROM
, enc_from
, -1);
1145 set_vim_var_string(VV_CC_TO
, enc_to
, -1);
1146 set_vim_var_string(VV_FNAME_IN
, fname_from
, -1);
1147 set_vim_var_string(VV_FNAME_OUT
, fname_to
, -1);
1148 if (eval_to_bool(p_ccv
, &err
, NULL
, FALSE
))
1150 set_vim_var_string(VV_CC_FROM
, NULL
, -1);
1151 set_vim_var_string(VV_CC_TO
, NULL
, -1);
1152 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1153 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1161 # if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1163 eval_printexpr(fname
, args
)
1169 set_vim_var_string(VV_FNAME_IN
, fname
, -1);
1170 set_vim_var_string(VV_CMDARG
, args
, -1);
1171 if (eval_to_bool(p_pexpr
, &err
, NULL
, FALSE
))
1173 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1174 set_vim_var_string(VV_CMDARG
, NULL
, -1);
1185 # if defined(FEAT_DIFF) || defined(PROTO)
1187 eval_diff(origfile
, newfile
, outfile
)
1194 set_vim_var_string(VV_FNAME_IN
, origfile
, -1);
1195 set_vim_var_string(VV_FNAME_NEW
, newfile
, -1);
1196 set_vim_var_string(VV_FNAME_OUT
, outfile
, -1);
1197 (void)eval_to_bool(p_dex
, &err
, NULL
, FALSE
);
1198 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1199 set_vim_var_string(VV_FNAME_NEW
, NULL
, -1);
1200 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1204 eval_patch(origfile
, difffile
, outfile
)
1211 set_vim_var_string(VV_FNAME_IN
, origfile
, -1);
1212 set_vim_var_string(VV_FNAME_DIFF
, difffile
, -1);
1213 set_vim_var_string(VV_FNAME_OUT
, outfile
, -1);
1214 (void)eval_to_bool(p_pex
, &err
, NULL
, FALSE
);
1215 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1216 set_vim_var_string(VV_FNAME_DIFF
, NULL
, -1);
1217 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1222 * Top level evaluation function, returning a boolean.
1223 * Sets "error" to TRUE if there was an error.
1224 * Return TRUE or FALSE.
1227 eval_to_bool(arg
, error
, nextcmd
, skip
)
1231 int skip
; /* only parse, don't execute */
1238 if (eval0(arg
, &tv
, nextcmd
, !skip
) == FAIL
)
1245 retval
= (get_tv_number_chk(&tv
, error
) != 0);
1256 * Top level evaluation function, returning a string. If "skip" is TRUE,
1257 * only parsing to "nextcmd" is done, without reporting errors. Return
1258 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1261 eval_to_string_skip(arg
, nextcmd
, skip
)
1264 int skip
; /* only parse, don't execute */
1271 if (eval0(arg
, &tv
, nextcmd
, !skip
) == FAIL
|| skip
)
1275 retval
= vim_strsave(get_tv_string(&tv
));
1285 * Skip over an expression at "*pp".
1286 * Return FAIL for an error, OK otherwise.
1294 *pp
= skipwhite(*pp
);
1295 return eval1(pp
, &rettv
, FALSE
);
1299 * Top level evaluation function, returning a string.
1300 * When "convert" is TRUE convert a List into a sequence of lines and convert
1301 * a Float to a String.
1302 * Return pointer to allocated memory, or NULL for failure.
1305 eval_to_string(arg
, nextcmd
, convert
)
1314 char_u numbuf
[NUMBUFLEN
];
1317 if (eval0(arg
, &tv
, nextcmd
, TRUE
) == FAIL
)
1321 if (convert
&& tv
.v_type
== VAR_LIST
)
1323 ga_init2(&ga
, (int)sizeof(char), 80);
1324 if (tv
.vval
.v_list
!= NULL
)
1325 list_join(&ga
, tv
.vval
.v_list
, (char_u
*)"\n", TRUE
, 0);
1326 ga_append(&ga
, NUL
);
1327 retval
= (char_u
*)ga
.ga_data
;
1330 else if (convert
&& tv
.v_type
== VAR_FLOAT
)
1332 vim_snprintf((char *)numbuf
, NUMBUFLEN
, "%g", tv
.vval
.v_float
);
1333 retval
= vim_strsave(numbuf
);
1337 retval
= vim_strsave(get_tv_string(&tv
));
1345 * Call eval_to_string() without using current local variables and using
1346 * textlock. When "use_sandbox" is TRUE use the sandbox.
1349 eval_to_string_safe(arg
, nextcmd
, use_sandbox
)
1355 void *save_funccalp
;
1357 save_funccalp
= save_funccal();
1361 retval
= eval_to_string(arg
, nextcmd
, FALSE
);
1365 restore_funccal(save_funccalp
);
1370 * Top level evaluation function, returning a number.
1371 * Evaluates "expr" silently.
1372 * Returns -1 for an error.
1375 eval_to_number(expr
)
1380 char_u
*p
= skipwhite(expr
);
1384 if (eval1(&p
, &rettv
, TRUE
) == FAIL
)
1388 retval
= get_tv_number_chk(&rettv
, NULL
);
1397 * Prepare v: variable "idx" to be used.
1398 * Save the current typeval in "save_tv".
1399 * When not used yet add the variable to the v: hashtable.
1402 prepare_vimvar(idx
, save_tv
)
1406 *save_tv
= vimvars
[idx
].vv_tv
;
1407 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1408 hash_add(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1412 * Restore v: variable "idx" to typeval "save_tv".
1413 * When no longer defined, remove the variable from the v: hashtable.
1416 restore_vimvar(idx
, save_tv
)
1422 vimvars
[idx
].vv_tv
= *save_tv
;
1423 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1425 hi
= hash_find(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1426 if (HASHITEM_EMPTY(hi
))
1427 EMSG2(_(e_intern2
), "restore_vimvar()");
1429 hash_remove(&vimvarht
, hi
);
1433 #if defined(FEAT_SPELL) || defined(PROTO)
1435 * Evaluate an expression to a list with suggestions.
1436 * For the "expr:" part of 'spellsuggest'.
1437 * Returns NULL when there is an error.
1440 eval_spell_expr(badword
, expr
)
1446 list_T
*list
= NULL
;
1447 char_u
*p
= skipwhite(expr
);
1449 /* Set "v:val" to the bad word. */
1450 prepare_vimvar(VV_VAL
, &save_val
);
1451 vimvars
[VV_VAL
].vv_type
= VAR_STRING
;
1452 vimvars
[VV_VAL
].vv_str
= badword
;
1456 if (eval1(&p
, &rettv
, TRUE
) == OK
)
1458 if (rettv
.v_type
!= VAR_LIST
)
1461 list
= rettv
.vval
.v_list
;
1466 restore_vimvar(VV_VAL
, &save_val
);
1472 * "list" is supposed to contain two items: a word and a number. Return the
1473 * word in "pp" and the number as the return value.
1474 * Return -1 if anything isn't right.
1475 * Used to get the good word and score from the eval_spell_expr() result.
1478 get_spellword(list
, pp
)
1484 li
= list
->lv_first
;
1487 *pp
= get_tv_string(&li
->li_tv
);
1492 return get_tv_number(&li
->li_tv
);
1497 * Top level evaluation function.
1498 * Returns an allocated typval_T with the result.
1499 * Returns NULL when there is an error.
1502 eval_expr(arg
, nextcmd
)
1508 tv
= (typval_T
*)alloc(sizeof(typval_T
));
1509 if (tv
!= NULL
&& eval0(arg
, tv
, nextcmd
, TRUE
) == FAIL
)
1519 #if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1520 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1522 * Call some vimL function and return the result in "*rettv".
1523 * Uses argv[argc] for the function arguments. Only Number and String
1524 * arguments are currently supported.
1525 * Returns OK or FAIL.
1528 call_vim_function(func
, argc
, argv
, safe
, rettv
)
1532 int safe
; /* use the sandbox */
1540 void *save_funccalp
= NULL
;
1543 argvars
= (typval_T
*)alloc((unsigned)((argc
+ 1) * sizeof(typval_T
)));
1544 if (argvars
== NULL
)
1547 for (i
= 0; i
< argc
; i
++)
1549 /* Pass a NULL or empty argument as an empty string */
1550 if (argv
[i
] == NULL
|| *argv
[i
] == NUL
)
1552 argvars
[i
].v_type
= VAR_STRING
;
1553 argvars
[i
].vval
.v_string
= (char_u
*)"";
1557 /* Recognize a number argument, the others must be strings. */
1558 vim_str2nr(argv
[i
], NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
1559 if (len
!= 0 && len
== (int)STRLEN(argv
[i
]))
1561 argvars
[i
].v_type
= VAR_NUMBER
;
1562 argvars
[i
].vval
.v_number
= n
;
1566 argvars
[i
].v_type
= VAR_STRING
;
1567 argvars
[i
].vval
.v_string
= argv
[i
];
1573 save_funccalp
= save_funccal();
1577 rettv
->v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
1578 ret
= call_func(func
, (int)STRLEN(func
), rettv
, argc
, argvars
,
1579 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
1580 &doesrange
, TRUE
, NULL
);
1584 restore_funccal(save_funccalp
);
1594 # if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1596 * Call vimL function "func" and return the result as a string.
1597 * Returns NULL when calling the function fails.
1598 * Uses argv[argc] for the function arguments.
1601 call_func_retstr(func
, argc
, argv
, safe
)
1605 int safe
; /* use the sandbox */
1610 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1613 retval
= vim_strsave(get_tv_string(&rettv
));
1619 # if defined(FEAT_COMPL_FUNC) || defined(PROTO)
1621 * Call vimL function "func" and return the result as a number.
1622 * Returns -1 when calling the function fails.
1623 * Uses argv[argc] for the function arguments.
1626 call_func_retnr(func
, argc
, argv
, safe
)
1630 int safe
; /* use the sandbox */
1635 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1638 retval
= get_tv_number_chk(&rettv
, NULL
);
1645 * Call vimL function "func" and return the result as a List.
1646 * Uses argv[argc] for the function arguments.
1647 * Returns NULL when there is something wrong.
1650 call_func_retlist(func
, argc
, argv
, safe
)
1654 int safe
; /* use the sandbox */
1658 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1661 if (rettv
.v_type
!= VAR_LIST
)
1667 return rettv
.vval
.v_list
;
1673 * Save the current function call pointer, and set it to NULL.
1674 * Used when executing autocommands and for ":source".
1679 funccall_T
*fc
= current_funccal
;
1681 current_funccal
= NULL
;
1686 restore_funccal(vfc
)
1689 funccall_T
*fc
= (funccall_T
*)vfc
;
1691 current_funccal
= fc
;
1694 #if defined(FEAT_PROFILE) || defined(PROTO)
1696 * Prepare profiling for entering a child or something else that is not
1697 * counted for the script/function itself.
1698 * Should always be called in pair with prof_child_exit().
1701 prof_child_enter(tm
)
1702 proftime_T
*tm
; /* place to store waittime */
1704 funccall_T
*fc
= current_funccal
;
1706 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1707 profile_start(&fc
->prof_child
);
1708 script_prof_save(tm
);
1712 * Take care of time spent in a child.
1713 * Should always be called after prof_child_enter().
1717 proftime_T
*tm
; /* where waittime was stored */
1719 funccall_T
*fc
= current_funccal
;
1721 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1723 profile_end(&fc
->prof_child
);
1724 profile_sub_wait(tm
, &fc
->prof_child
); /* don't count waiting time */
1725 profile_add(&fc
->func
->uf_tm_children
, &fc
->prof_child
);
1726 profile_add(&fc
->func
->uf_tml_children
, &fc
->prof_child
);
1728 script_prof_restore(tm
);
1735 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1736 * it in "*cp". Doesn't give error messages.
1739 eval_foldexpr(arg
, cp
)
1746 int use_sandbox
= was_set_insecurely((char_u
*)"foldexpr",
1754 if (eval0(arg
, &tv
, NULL
, TRUE
) == FAIL
)
1758 /* If the result is a number, just return the number. */
1759 if (tv
.v_type
== VAR_NUMBER
)
1760 retval
= tv
.vval
.v_number
;
1761 else if (tv
.v_type
!= VAR_STRING
|| tv
.vval
.v_string
== NULL
)
1765 /* If the result is a string, check if there is a non-digit before
1767 s
= tv
.vval
.v_string
;
1768 if (!VIM_ISDIGIT(*s
) && *s
!= '-')
1770 retval
= atol((char *)s
);
1784 * ":let" list all variable values
1785 * ":let var1 var2" list variable values
1786 * ":let var = expr" assignment command.
1787 * ":let var += expr" assignment command.
1788 * ":let var -= expr" assignment command.
1789 * ":let var .= expr" assignment command.
1790 * ":let [var1, var2] = expr" unpack list.
1796 char_u
*arg
= eap
->arg
;
1797 char_u
*expr
= NULL
;
1806 argend
= skip_var_list(arg
, &var_count
, &semicolon
);
1809 if (argend
> arg
&& argend
[-1] == '.') /* for var.='str' */
1811 expr
= vim_strchr(argend
, '=');
1815 * ":let" without "=": list variables
1819 else if (!ends_excmd(*arg
))
1820 /* ":let var1 var2" */
1821 arg
= list_arg_vars(eap
, arg
, &first
);
1822 else if (!eap
->skip
)
1825 list_glob_vars(&first
);
1826 list_buf_vars(&first
);
1827 list_win_vars(&first
);
1829 list_tab_vars(&first
);
1831 list_script_vars(&first
);
1832 list_func_vars(&first
);
1833 list_vim_vars(&first
);
1835 eap
->nextcmd
= check_nextcmd(arg
);
1843 if (vim_strchr((char_u
*)"+-.", expr
[-1]) != NULL
)
1844 op
[0] = expr
[-1]; /* +=, -= or .= */
1846 expr
= skipwhite(expr
+ 1);
1850 i
= eval0(expr
, &rettv
, &eap
->nextcmd
, !eap
->skip
);
1859 (void)ex_let_vars(eap
->arg
, &rettv
, FALSE
, semicolon
, var_count
,
1867 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1868 * Handles both "var" with any type and "[var, var; var]" with a list type.
1869 * When "nextchars" is not NULL it points to a string with characters that
1870 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1872 * Returns OK or FAIL;
1875 ex_let_vars(arg_start
, tv
, copy
, semicolon
, var_count
, nextchars
)
1878 int copy
; /* copy values from "tv", don't move */
1879 int semicolon
; /* from skip_var_list() */
1880 int var_count
; /* from skip_var_list() */
1883 char_u
*arg
= arg_start
;
1892 * ":let var = expr" or ":for var in list"
1894 if (ex_let_one(arg
, tv
, copy
, nextchars
, nextchars
) == NULL
)
1900 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1902 if (tv
->v_type
!= VAR_LIST
|| (l
= tv
->vval
.v_list
) == NULL
)
1909 if (semicolon
== 0 && var_count
< i
)
1911 EMSG(_("E687: Less targets than List items"));
1914 if (var_count
- semicolon
> i
)
1916 EMSG(_("E688: More targets than List items"));
1923 arg
= skipwhite(arg
+ 1);
1924 arg
= ex_let_one(arg
, &item
->li_tv
, TRUE
, (char_u
*)",;]", nextchars
);
1925 item
= item
->li_next
;
1929 arg
= skipwhite(arg
);
1932 /* Put the rest of the list (may be empty) in the var after ';'.
1933 * Create a new list for this. */
1937 while (item
!= NULL
)
1939 list_append_tv(l
, &item
->li_tv
);
1940 item
= item
->li_next
;
1943 ltv
.v_type
= VAR_LIST
;
1945 ltv
.vval
.v_list
= l
;
1948 arg
= ex_let_one(skipwhite(arg
+ 1), <v
, FALSE
,
1949 (char_u
*)"]", nextchars
);
1955 else if (*arg
!= ',' && *arg
!= ']')
1957 EMSG2(_(e_intern2
), "ex_let_vars()");
1966 * Skip over assignable variable "var" or list of variables "[var, var]".
1967 * Used for ":let varvar = expr" and ":for varvar in expr".
1968 * For "[var, var]" increment "*var_count" for each variable.
1969 * for "[var, var; var]" set "semicolon".
1970 * Return NULL for an error.
1973 skip_var_list(arg
, var_count
, semicolon
)
1982 /* "[var, var]": find the matching ']'. */
1986 p
= skipwhite(p
+ 1); /* skip whites after '[', ';' or ',' */
1987 s
= skip_var_one(p
);
1990 EMSG2(_(e_invarg2
), p
);
2000 if (*semicolon
== 1)
2002 EMSG(_("Double ; in list of variables"));
2009 EMSG2(_(e_invarg2
), p
);
2016 return skip_var_one(arg
);
2020 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
2027 if (*arg
== '@' && arg
[1] != NUL
)
2029 return find_name_end(*arg
== '$' || *arg
== '&' ? arg
+ 1 : arg
,
2030 NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
2034 * List variables for hashtab "ht" with prefix "prefix".
2035 * If "empty" is TRUE also list NULL strings as empty strings.
2038 list_hashtable_vars(ht
, prefix
, empty
, first
)
2048 todo
= (int)ht
->ht_used
;
2049 for (hi
= ht
->ht_array
; todo
> 0 && !got_int
; ++hi
)
2051 if (!HASHITEM_EMPTY(hi
))
2055 if (empty
|| di
->di_tv
.v_type
!= VAR_STRING
2056 || di
->di_tv
.vval
.v_string
!= NULL
)
2057 list_one_var(di
, prefix
, first
);
2063 * List global variables.
2066 list_glob_vars(first
)
2069 list_hashtable_vars(&globvarht
, (char_u
*)"", TRUE
, first
);
2073 * List buffer variables.
2076 list_buf_vars(first
)
2079 char_u numbuf
[NUMBUFLEN
];
2081 list_hashtable_vars(&curbuf
->b_vars
.dv_hashtab
, (char_u
*)"b:",
2084 sprintf((char *)numbuf
, "%ld", (long)curbuf
->b_changedtick
);
2085 list_one_var_a((char_u
*)"b:", (char_u
*)"changedtick", VAR_NUMBER
,
2090 * List window variables.
2093 list_win_vars(first
)
2096 list_hashtable_vars(&curwin
->w_vars
.dv_hashtab
,
2097 (char_u
*)"w:", TRUE
, first
);
2102 * List tab page variables.
2105 list_tab_vars(first
)
2108 list_hashtable_vars(&curtab
->tp_vars
.dv_hashtab
,
2109 (char_u
*)"t:", TRUE
, first
);
2114 * List Vim variables.
2117 list_vim_vars(first
)
2120 list_hashtable_vars(&vimvarht
, (char_u
*)"v:", FALSE
, first
);
2124 * List script-local variables, if there is a script.
2127 list_script_vars(first
)
2130 if (current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
2131 list_hashtable_vars(&SCRIPT_VARS(current_SID
),
2132 (char_u
*)"s:", FALSE
, first
);
2136 * List function variables, if there is a function.
2139 list_func_vars(first
)
2142 if (current_funccal
!= NULL
)
2143 list_hashtable_vars(¤t_funccal
->l_vars
.dv_hashtab
,
2144 (char_u
*)"l:", FALSE
, first
);
2148 * List variables in "arg".
2151 list_arg_vars(eap
, arg
, first
)
2164 while (!ends_excmd(*arg
) && !got_int
)
2166 if (error
|| eap
->skip
)
2168 arg
= find_name_end(arg
, NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
2169 if (!vim_iswhite(*arg
) && !ends_excmd(*arg
))
2172 EMSG(_(e_trailing
));
2178 /* get_name_len() takes care of expanding curly braces */
2179 name_start
= name
= arg
;
2180 len
= get_name_len(&arg
, &tofree
, TRUE
, TRUE
);
2183 /* This is mainly to keep test 49 working: when expanding
2184 * curly braces fails overrule the exception error message. */
2185 if (len
< 0 && !aborting())
2188 EMSG2(_(e_invarg2
), arg
);
2197 if (get_var_tv(name
, len
, &tv
, TRUE
) == FAIL
)
2201 /* handle d.key, l[idx], f(expr) */
2203 if (handle_subscript(&arg
, &tv
, TRUE
, TRUE
) == FAIL
)
2207 if (arg
== arg_subsc
&& len
== 2 && name
[1] == ':')
2211 case 'g': list_glob_vars(first
); break;
2212 case 'b': list_buf_vars(first
); break;
2213 case 'w': list_win_vars(first
); break;
2215 case 't': list_tab_vars(first
); break;
2217 case 'v': list_vim_vars(first
); break;
2218 case 's': list_script_vars(first
); break;
2219 case 'l': list_func_vars(first
); break;
2221 EMSG2(_("E738: Can't list variables for %s"), name
);
2226 char_u numbuf
[NUMBUFLEN
];
2231 s
= echo_string(&tv
, &tf
, numbuf
, 0);
2234 list_one_var_a((char_u
*)"",
2235 arg
== arg_subsc
? name
: name_start
,
2237 s
== NULL
? (char_u
*)"" : s
,
2250 arg
= skipwhite(arg
);
2257 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2258 * Returns a pointer to the char just after the var name.
2259 * Returns NULL if there is an error.
2262 ex_let_one(arg
, tv
, copy
, endchars
, op
)
2263 char_u
*arg
; /* points to variable name */
2264 typval_T
*tv
; /* value to assign to variable */
2265 int copy
; /* copy value from "tv" */
2266 char_u
*endchars
; /* valid chars after variable name or NULL */
2267 char_u
*op
; /* "+", "-", "." or NULL*/
2272 char_u
*arg_end
= NULL
;
2275 char_u
*tofree
= NULL
;
2278 * ":let $VAR = expr": Set environment variable.
2282 /* Find the end of the name. */
2285 len
= get_env_len(&arg
);
2287 EMSG2(_(e_invarg2
), name
- 1);
2290 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2291 EMSG2(_(e_letwrong
), op
);
2292 else if (endchars
!= NULL
2293 && vim_strchr(endchars
, *skipwhite(arg
)) == NULL
)
2294 EMSG(_(e_letunexp
));
2299 p
= get_tv_string_chk(tv
);
2300 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2302 int mustfree
= FALSE
;
2303 char_u
*s
= vim_getenv(name
, &mustfree
);
2307 p
= tofree
= concat_str(s
, p
);
2314 vim_setenv(name
, p
);
2315 if (STRICMP(name
, "HOME") == 0)
2317 else if (didset_vim
&& STRICMP(name
, "VIM") == 0)
2319 else if (didset_vimruntime
2320 && STRICMP(name
, "VIMRUNTIME") == 0)
2321 didset_vimruntime
= FALSE
;
2331 * ":let &option = expr": Set option value.
2332 * ":let &l:option = expr": Set local option value.
2333 * ":let &g:option = expr": Set global option value.
2335 else if (*arg
== '&')
2337 /* Find the end of the name. */
2338 p
= find_option_end(&arg
, &opt_flags
);
2339 if (p
== NULL
|| (endchars
!= NULL
2340 && vim_strchr(endchars
, *skipwhite(p
)) == NULL
))
2341 EMSG(_(e_letunexp
));
2347 char_u
*stringval
= NULL
;
2353 n
= get_tv_number(tv
);
2354 s
= get_tv_string_chk(tv
); /* != NULL if number or string */
2355 if (s
!= NULL
&& op
!= NULL
&& *op
!= '=')
2357 opt_type
= get_option_value(arg
, &numval
,
2358 &stringval
, opt_flags
);
2359 if ((opt_type
== 1 && *op
== '.')
2360 || (opt_type
== 0 && *op
!= '.'))
2361 EMSG2(_(e_letwrong
), op
);
2364 if (opt_type
== 1) /* number */
2371 else if (opt_type
== 0 && stringval
!= NULL
) /* string */
2373 s
= concat_str(stringval
, s
);
2374 vim_free(stringval
);
2381 set_option_value(arg
, n
, s
, opt_flags
);
2385 vim_free(stringval
);
2390 * ":let @r = expr": Set register contents.
2392 else if (*arg
== '@')
2395 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2396 EMSG2(_(e_letwrong
), op
);
2397 else if (endchars
!= NULL
2398 && vim_strchr(endchars
, *skipwhite(arg
+ 1)) == NULL
)
2399 EMSG(_(e_letunexp
));
2402 char_u
*ptofree
= NULL
;
2405 p
= get_tv_string_chk(tv
);
2406 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2408 s
= get_reg_contents(*arg
== '@' ? '"' : *arg
, TRUE
, TRUE
);
2411 p
= ptofree
= concat_str(s
, p
);
2417 write_reg_contents(*arg
== '@' ? '"' : *arg
, p
, -1, FALSE
);
2425 * ":let var = expr": Set internal variable.
2426 * ":let {expr} = expr": Idem, name made with curly braces
2428 else if (eval_isnamec1(*arg
) || *arg
== '{')
2432 p
= get_lval(arg
, tv
, &lv
, FALSE
, FALSE
, FALSE
, FNE_CHECK_START
);
2433 if (p
!= NULL
&& lv
.ll_name
!= NULL
)
2435 if (endchars
!= NULL
&& vim_strchr(endchars
, *skipwhite(p
)) == NULL
)
2436 EMSG(_(e_letunexp
));
2439 set_var_lval(&lv
, p
, tv
, copy
, op
);
2447 EMSG2(_(e_invarg2
), arg
);
2453 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2456 check_changedtick(arg
)
2459 if (STRNCMP(arg
, "b:changedtick", 13) == 0 && !eval_isnamec(arg
[13]))
2461 EMSG2(_(e_readonlyvar
), arg
);
2468 * Get an lval: variable, Dict item or List item that can be assigned a value
2469 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2470 * "name.key", "name.key[expr]" etc.
2471 * Indexing only works if "name" is an existing List or Dictionary.
2472 * "name" points to the start of the name.
2473 * If "rettv" is not NULL it points to the value to be assigned.
2474 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2475 * wrong; must end in space or cmd separator.
2477 * Returns a pointer to just after the name, including indexes.
2478 * When an evaluation error occurs "lp->ll_name" is NULL;
2479 * Returns NULL for a parsing error. Still need to free items in "lp"!
2482 get_lval(name
, rettv
, lp
, unlet
, skip
, quiet
, fne_flags
)
2488 int quiet
; /* don't give error messages */
2489 int fne_flags
; /* flags for find_name_end() */
2492 char_u
*expr_start
, *expr_end
;
2503 /* Clear everything in "lp". */
2504 vim_memset(lp
, 0, sizeof(lval_T
));
2508 /* When skipping just find the end of the name. */
2510 return find_name_end(name
, NULL
, NULL
, FNE_INCL_BR
| fne_flags
);
2513 /* Find the end of the name. */
2514 p
= find_name_end(name
, &expr_start
, &expr_end
, fne_flags
);
2515 if (expr_start
!= NULL
)
2517 /* Don't expand the name when we already know there is an error. */
2518 if (unlet
&& !vim_iswhite(*p
) && !ends_excmd(*p
)
2519 && *p
!= '[' && *p
!= '.')
2521 EMSG(_(e_trailing
));
2525 lp
->ll_exp_name
= make_expanded_name(name
, expr_start
, expr_end
, p
);
2526 if (lp
->ll_exp_name
== NULL
)
2528 /* Report an invalid expression in braces, unless the
2529 * expression evaluation has been cancelled due to an
2530 * aborting error, an interrupt, or an exception. */
2531 if (!aborting() && !quiet
)
2534 EMSG2(_(e_invarg2
), name
);
2538 lp
->ll_name
= lp
->ll_exp_name
;
2543 /* Without [idx] or .key we are done. */
2544 if ((*p
!= '[' && *p
!= '.') || lp
->ll_name
== NULL
)
2549 v
= find_var(lp
->ll_name
, &ht
);
2550 if (v
== NULL
&& !quiet
)
2551 EMSG2(_(e_undefvar
), lp
->ll_name
);
2557 * Loop until no more [idx] or .key is following.
2559 lp
->ll_tv
= &v
->di_tv
;
2560 while (*p
== '[' || (*p
== '.' && lp
->ll_tv
->v_type
== VAR_DICT
))
2562 if (!(lp
->ll_tv
->v_type
== VAR_LIST
&& lp
->ll_tv
->vval
.v_list
!= NULL
)
2563 && !(lp
->ll_tv
->v_type
== VAR_DICT
2564 && lp
->ll_tv
->vval
.v_dict
!= NULL
))
2567 EMSG(_("E689: Can only index a List or Dictionary"));
2573 EMSG(_("E708: [:] must come last"));
2581 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
2586 EMSG(_(e_emptykey
));
2593 /* Get the index [expr] or the first index [expr: ]. */
2594 p
= skipwhite(p
+ 1);
2600 if (eval1(&p
, &var1
, TRUE
) == FAIL
) /* recursive! */
2602 if (get_tv_string_chk(&var1
) == NULL
)
2604 /* not a number or string */
2610 /* Optionally get the second index [ :expr]. */
2613 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2616 EMSG(_(e_dictrange
));
2621 if (rettv
!= NULL
&& (rettv
->v_type
!= VAR_LIST
2622 || rettv
->vval
.v_list
== NULL
))
2625 EMSG(_("E709: [:] requires a List value"));
2630 p
= skipwhite(p
+ 1);
2632 lp
->ll_empty2
= TRUE
;
2635 lp
->ll_empty2
= FALSE
;
2636 if (eval1(&p
, &var2
, TRUE
) == FAIL
) /* recursive! */
2642 if (get_tv_string_chk(&var2
) == NULL
)
2644 /* not a number or string */
2651 lp
->ll_range
= TRUE
;
2654 lp
->ll_range
= FALSE
;
2659 EMSG(_(e_missbrac
));
2662 if (lp
->ll_range
&& !lp
->ll_empty2
)
2667 /* Skip to past ']'. */
2671 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2675 /* "[key]": get key from "var1" */
2676 key
= get_tv_string(&var1
); /* is number or string */
2680 EMSG(_(e_emptykey
));
2686 lp
->ll_dict
= lp
->ll_tv
->vval
.v_dict
;
2687 lp
->ll_di
= dict_find(lp
->ll_dict
, key
, len
);
2688 if (lp
->ll_di
== NULL
)
2690 /* Key does not exist in dict: may need to add it. */
2691 if (*p
== '[' || *p
== '.' || unlet
)
2694 EMSG2(_(e_dictkey
), key
);
2700 lp
->ll_newkey
= vim_strsave(key
);
2702 lp
->ll_newkey
= vim_strnsave(key
, len
);
2705 if (lp
->ll_newkey
== NULL
)
2711 lp
->ll_tv
= &lp
->ll_di
->di_tv
;
2716 * Get the number and item for the only or first index of the List.
2722 lp
->ll_n1
= get_tv_number(&var1
); /* is number or string */
2726 lp
->ll_list
= lp
->ll_tv
->vval
.v_list
;
2727 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2728 if (lp
->ll_li
== NULL
)
2733 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2736 if (lp
->ll_li
== NULL
)
2738 if (lp
->ll_range
&& !lp
->ll_empty2
)
2744 * May need to find the item or absolute index for the second
2746 * When no index given: "lp->ll_empty2" is TRUE.
2747 * Otherwise "lp->ll_n2" is set to the second index.
2749 if (lp
->ll_range
&& !lp
->ll_empty2
)
2751 lp
->ll_n2
= get_tv_number(&var2
); /* is number or string */
2755 ni
= list_find(lp
->ll_list
, lp
->ll_n2
);
2758 lp
->ll_n2
= list_idx_of_item(lp
->ll_list
, ni
);
2761 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2763 lp
->ll_n1
= list_idx_of_item(lp
->ll_list
, lp
->ll_li
);
2764 if (lp
->ll_n2
< lp
->ll_n1
)
2768 lp
->ll_tv
= &lp
->ll_li
->li_tv
;
2776 * Clear lval "lp" that was filled by get_lval().
2782 vim_free(lp
->ll_exp_name
);
2783 vim_free(lp
->ll_newkey
);
2787 * Set a variable that was parsed by get_lval() to "rettv".
2788 * "endp" points to just after the parsed name.
2789 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
2792 set_var_lval(lp
, endp
, rettv
, copy
, op
)
2803 if (lp
->ll_tv
== NULL
)
2805 if (!check_changedtick(lp
->ll_name
))
2809 if (op
!= NULL
&& *op
!= '=')
2813 /* handle +=, -= and .= */
2814 if (get_var_tv(lp
->ll_name
, (int)STRLEN(lp
->ll_name
),
2817 if (tv_op(&tv
, rettv
, op
) == OK
)
2818 set_var(lp
->ll_name
, &tv
, FALSE
);
2823 set_var(lp
->ll_name
, rettv
, copy
);
2827 else if (tv_check_lock(lp
->ll_newkey
== NULL
2829 : lp
->ll_tv
->vval
.v_dict
->dv_lock
, lp
->ll_name
))
2831 else if (lp
->ll_range
)
2834 * Assign the List values to the list items.
2836 for (ri
= rettv
->vval
.v_list
->lv_first
; ri
!= NULL
; )
2838 if (op
!= NULL
&& *op
!= '=')
2839 tv_op(&lp
->ll_li
->li_tv
, &ri
->li_tv
, op
);
2842 clear_tv(&lp
->ll_li
->li_tv
);
2843 copy_tv(&ri
->li_tv
, &lp
->ll_li
->li_tv
);
2846 if (ri
== NULL
|| (!lp
->ll_empty2
&& lp
->ll_n2
== lp
->ll_n1
))
2848 if (lp
->ll_li
->li_next
== NULL
)
2850 /* Need to add an empty item. */
2851 if (list_append_number(lp
->ll_list
, 0) == FAIL
)
2857 lp
->ll_li
= lp
->ll_li
->li_next
;
2861 EMSG(_("E710: List value has more items than target"));
2862 else if (lp
->ll_empty2
2863 ? (lp
->ll_li
!= NULL
&& lp
->ll_li
->li_next
!= NULL
)
2864 : lp
->ll_n1
!= lp
->ll_n2
)
2865 EMSG(_("E711: List value has not enough items"));
2870 * Assign to a List or Dictionary item.
2872 if (lp
->ll_newkey
!= NULL
)
2874 if (op
!= NULL
&& *op
!= '=')
2876 EMSG2(_(e_letwrong
), op
);
2880 /* Need to add an item to the Dictionary. */
2881 di
= dictitem_alloc(lp
->ll_newkey
);
2884 if (dict_add(lp
->ll_tv
->vval
.v_dict
, di
) == FAIL
)
2889 lp
->ll_tv
= &di
->di_tv
;
2891 else if (op
!= NULL
&& *op
!= '=')
2893 tv_op(lp
->ll_tv
, rettv
, op
);
2897 clear_tv(lp
->ll_tv
);
2900 * Assign the value to the variable or list item.
2903 copy_tv(rettv
, lp
->ll_tv
);
2906 *lp
->ll_tv
= *rettv
;
2907 lp
->ll_tv
->v_lock
= 0;
2914 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2915 * Returns OK or FAIL.
2924 char_u numbuf
[NUMBUFLEN
];
2927 /* Can't do anything with a Funcref or a Dict on the right. */
2928 if (tv2
->v_type
!= VAR_FUNC
&& tv2
->v_type
!= VAR_DICT
)
2930 switch (tv1
->v_type
)
2937 if (*op
!= '+' || tv2
->v_type
!= VAR_LIST
)
2940 if (tv1
->vval
.v_list
!= NULL
&& tv2
->vval
.v_list
!= NULL
)
2941 list_extend(tv1
->vval
.v_list
, tv2
->vval
.v_list
, NULL
);
2946 if (tv2
->v_type
== VAR_LIST
)
2948 if (*op
== '+' || *op
== '-')
2950 /* nr += nr or nr -= nr*/
2951 n
= get_tv_number(tv1
);
2953 if (tv2
->v_type
== VAR_FLOAT
)
2958 f
+= tv2
->vval
.v_float
;
2960 f
-= tv2
->vval
.v_float
;
2962 tv1
->v_type
= VAR_FLOAT
;
2963 tv1
->vval
.v_float
= f
;
2969 n
+= get_tv_number(tv2
);
2971 n
-= get_tv_number(tv2
);
2973 tv1
->v_type
= VAR_NUMBER
;
2974 tv1
->vval
.v_number
= n
;
2979 if (tv2
->v_type
== VAR_FLOAT
)
2983 s
= get_tv_string(tv1
);
2984 s
= concat_str(s
, get_tv_string_buf(tv2
, numbuf
));
2986 tv1
->v_type
= VAR_STRING
;
2987 tv1
->vval
.v_string
= s
;
2996 if (*op
== '.' || (tv2
->v_type
!= VAR_FLOAT
2997 && tv2
->v_type
!= VAR_NUMBER
2998 && tv2
->v_type
!= VAR_STRING
))
3000 if (tv2
->v_type
== VAR_FLOAT
)
3001 f
= tv2
->vval
.v_float
;
3003 f
= get_tv_number(tv2
);
3005 tv1
->vval
.v_float
+= f
;
3007 tv1
->vval
.v_float
-= f
;
3014 EMSG2(_(e_letwrong
), op
);
3019 * Add a watcher to a list.
3022 list_add_watch(l
, lw
)
3026 lw
->lw_next
= l
->lv_watch
;
3031 * Remove a watcher from a list.
3032 * No warning when it isn't found...
3035 list_rem_watch(l
, lwrem
)
3039 listwatch_T
*lw
, **lwp
;
3042 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
3054 * Just before removing an item from a list: advance watchers to the next
3058 list_fix_watch(l
, item
)
3064 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
3065 if (lw
->lw_item
== item
)
3066 lw
->lw_item
= item
->li_next
;
3070 * Evaluate the expression used in a ":for var in expr" command.
3071 * "arg" points to "var".
3072 * Set "*errp" to TRUE for an error, FALSE otherwise;
3073 * Return a pointer that holds the info. Null when there is an error.
3076 eval_for_line(arg
, errp
, nextcmdp
, skip
)
3087 *errp
= TRUE
; /* default: there is an error */
3089 fi
= (forinfo_T
*)alloc_clear(sizeof(forinfo_T
));
3093 expr
= skip_var_list(arg
, &fi
->fi_varcount
, &fi
->fi_semicolon
);
3097 expr
= skipwhite(expr
);
3098 if (expr
[0] != 'i' || expr
[1] != 'n' || !vim_iswhite(expr
[2]))
3100 EMSG(_("E690: Missing \"in\" after :for"));
3106 if (eval0(skipwhite(expr
+ 2), &tv
, nextcmdp
, !skip
) == OK
)
3112 if (tv
.v_type
!= VAR_LIST
|| l
== NULL
)
3119 /* No need to increment the refcount, it's already set for the
3120 * list being used in "tv". */
3122 list_add_watch(l
, &fi
->fi_lw
);
3123 fi
->fi_lw
.lw_item
= l
->lv_first
;
3134 * Use the first item in a ":for" list. Advance to the next.
3135 * Assign the values to the variable (list). "arg" points to the first one.
3136 * Return TRUE when a valid item was found, FALSE when at end of list or
3140 next_for_item(fi_void
, arg
)
3144 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3148 item
= fi
->fi_lw
.lw_item
;
3153 fi
->fi_lw
.lw_item
= item
->li_next
;
3154 result
= (ex_let_vars(arg
, &item
->li_tv
, TRUE
,
3155 fi
->fi_semicolon
, fi
->fi_varcount
, NULL
) == OK
);
3161 * Free the structure used to store info used by ":for".
3164 free_for_info(fi_void
)
3167 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3169 if (fi
!= NULL
&& fi
->fi_list
!= NULL
)
3171 list_rem_watch(fi
->fi_list
, &fi
->fi_lw
);
3172 list_unref(fi
->fi_list
);
3177 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3180 set_context_for_expression(xp
, arg
, cmdidx
)
3189 if (cmdidx
== CMD_let
)
3191 xp
->xp_context
= EXPAND_USER_VARS
;
3192 if (vim_strpbrk(arg
, (char_u
*)"\"'+-*/%.=!?~|&$([<>,#") == NULL
)
3194 /* ":let var1 var2 ...": find last space. */
3195 for (p
= arg
+ STRLEN(arg
); p
>= arg
; )
3198 mb_ptr_back(arg
, p
);
3199 if (vim_iswhite(*p
))
3206 xp
->xp_context
= cmdidx
== CMD_call
? EXPAND_FUNCTIONS
3207 : EXPAND_EXPRESSION
;
3208 while ((xp
->xp_pattern
= vim_strpbrk(arg
,
3209 (char_u
*)"\"'+-*/%.=!?~|&$([<>,#")) != NULL
)
3211 c
= *xp
->xp_pattern
;
3214 c
= xp
->xp_pattern
[1];
3218 xp
->xp_context
= cmdidx
!= CMD_let
|| got_eq
3219 ? EXPAND_EXPRESSION
: EXPAND_NOTHING
;
3223 xp
->xp_context
= EXPAND_SETTINGS
;
3224 if ((c
== 'l' || c
== 'g') && xp
->xp_pattern
[2] == ':')
3225 xp
->xp_pattern
+= 2;
3231 /* environment variable */
3232 xp
->xp_context
= EXPAND_ENV_VARS
;
3237 xp
->xp_context
= EXPAND_EXPRESSION
;
3240 && xp
->xp_context
== EXPAND_FUNCTIONS
3241 && vim_strchr(xp
->xp_pattern
, '(') == NULL
)
3243 /* Function name can start with "<SNR>" */
3246 else if (cmdidx
!= CMD_let
|| got_eq
)
3248 if (c
== '"') /* string */
3250 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '"')
3251 if (c
== '\\' && xp
->xp_pattern
[1] != NUL
)
3253 xp
->xp_context
= EXPAND_NOTHING
;
3255 else if (c
== '\'') /* literal string */
3257 /* Trick: '' is like stopping and starting a literal string. */
3258 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '\'')
3260 xp
->xp_context
= EXPAND_NOTHING
;
3264 if (xp
->xp_pattern
[1] == '|')
3267 xp
->xp_context
= EXPAND_EXPRESSION
;
3270 xp
->xp_context
= EXPAND_COMMANDS
;
3273 xp
->xp_context
= EXPAND_EXPRESSION
;
3276 /* Doesn't look like something valid, expand as an expression
3278 xp
->xp_context
= EXPAND_EXPRESSION
;
3279 arg
= xp
->xp_pattern
;
3281 while ((c
= *++arg
) != NUL
&& (c
== ' ' || c
== '\t'))
3284 xp
->xp_pattern
= arg
;
3287 #endif /* FEAT_CMDL_COMPL */
3290 * ":1,25call func(arg1, arg2)" function call.
3296 char_u
*arg
= eap
->arg
;
3307 tofree
= trans_function_name(&arg
, eap
->skip
, TFN_INT
, &fudi
);
3308 if (fudi
.fd_newkey
!= NULL
)
3310 /* Still need to give an error message for missing key. */
3311 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
3312 vim_free(fudi
.fd_newkey
);
3317 /* Increase refcount on dictionary, it could get deleted when evaluating
3319 if (fudi
.fd_dict
!= NULL
)
3320 ++fudi
.fd_dict
->dv_refcount
;
3322 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3323 len
= (int)STRLEN(tofree
);
3324 name
= deref_func_name(tofree
, &len
);
3326 /* Skip white space to allow ":call func ()". Not good, but required for
3327 * backward compatibility. */
3328 startarg
= skipwhite(arg
);
3329 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
3331 if (*startarg
!= '(')
3333 EMSG2(_("E107: Missing parentheses: %s"), eap
->arg
);
3338 * When skipping, evaluate the function once, to find the end of the
3340 * When the function takes a range, this is discovered after the first
3341 * call, and the loop is broken.
3346 lnum
= eap
->line2
; /* do it once, also with an invalid range */
3350 for ( ; lnum
<= eap
->line2
; ++lnum
)
3352 if (!eap
->skip
&& eap
->addr_count
> 0)
3354 curwin
->w_cursor
.lnum
= lnum
;
3355 curwin
->w_cursor
.col
= 0;
3358 if (get_func_tv(name
, (int)STRLEN(name
), &rettv
, &arg
,
3359 eap
->line1
, eap
->line2
, &doesrange
,
3360 !eap
->skip
, fudi
.fd_dict
) == FAIL
)
3366 /* Handle a function returning a Funcref, Dictionary or List. */
3367 if (handle_subscript(&arg
, &rettv
, !eap
->skip
, TRUE
) == FAIL
)
3374 if (doesrange
|| eap
->skip
)
3377 /* Stop when immediately aborting on error, or when an interrupt
3378 * occurred or an exception was thrown but not caught.
3379 * get_func_tv() returned OK, so that the check for trailing
3380 * characters below is executed. */
3389 /* Check for trailing illegal characters and a following command. */
3390 if (!ends_excmd(*arg
))
3393 EMSG(_(e_trailing
));
3396 eap
->nextcmd
= check_nextcmd(arg
);
3400 dict_unref(fudi
.fd_dict
);
3405 * ":unlet[!] var1 ... " command.
3411 ex_unletlock(eap
, eap
->arg
, 0);
3415 * ":lockvar" and ":unlockvar" commands
3421 char_u
*arg
= eap
->arg
;
3426 else if (vim_isdigit(*arg
))
3428 deep
= getdigits(&arg
);
3429 arg
= skipwhite(arg
);
3432 ex_unletlock(eap
, arg
, deep
);
3436 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3439 ex_unletlock(eap
, argstart
, deep
)
3444 char_u
*arg
= argstart
;
3451 /* Parse the name and find the end. */
3452 name_end
= get_lval(arg
, NULL
, &lv
, TRUE
, eap
->skip
|| error
, FALSE
,
3454 if (lv
.ll_name
== NULL
)
3455 error
= TRUE
; /* error but continue parsing */
3456 if (name_end
== NULL
|| (!vim_iswhite(*name_end
)
3457 && !ends_excmd(*name_end
)))
3459 if (name_end
!= NULL
)
3462 EMSG(_(e_trailing
));
3464 if (!(eap
->skip
|| error
))
3469 if (!error
&& !eap
->skip
)
3471 if (eap
->cmdidx
== CMD_unlet
)
3473 if (do_unlet_var(&lv
, name_end
, eap
->forceit
) == FAIL
)
3478 if (do_lock_var(&lv
, name_end
, deep
,
3479 eap
->cmdidx
== CMD_lockvar
) == FAIL
)
3487 arg
= skipwhite(name_end
);
3488 } while (!ends_excmd(*arg
));
3490 eap
->nextcmd
= check_nextcmd(arg
);
3494 do_unlet_var(lp
, name_end
, forceit
)
3502 if (lp
->ll_tv
== NULL
)
3507 /* Normal name or expanded name. */
3508 if (check_changedtick(lp
->ll_name
))
3510 else if (do_unlet(lp
->ll_name
, forceit
) == FAIL
)
3514 else if (tv_check_lock(lp
->ll_tv
->v_lock
, lp
->ll_name
))
3516 else if (lp
->ll_range
)
3520 /* Delete a range of List items. */
3521 while (lp
->ll_li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3523 li
= lp
->ll_li
->li_next
;
3524 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3531 if (lp
->ll_list
!= NULL
)
3532 /* unlet a List item. */
3533 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3535 /* unlet a Dictionary item. */
3536 dictitem_remove(lp
->ll_dict
, lp
->ll_di
);
3543 * "unlet" a variable. Return OK if it existed, FAIL if not.
3544 * When "forceit" is TRUE don't complain if the variable doesn't exist.
3547 do_unlet(name
, forceit
)
3556 ht
= find_var_ht(name
, &varname
);
3557 if (ht
!= NULL
&& *varname
!= NUL
)
3559 hi
= hash_find(ht
, varname
);
3560 if (!HASHITEM_EMPTY(hi
))
3563 if (var_check_fixed(di
->di_flags
, name
)
3564 || var_check_ro(di
->di_flags
, name
))
3572 EMSG2(_("E108: No such variable: \"%s\""), name
);
3577 * Lock or unlock variable indicated by "lp".
3578 * "deep" is the levels to go (-1 for unlimited);
3579 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3582 do_lock_var(lp
, name_end
, deep
, lock
)
3592 if (deep
== 0) /* nothing to do */
3595 if (lp
->ll_tv
== NULL
)
3600 /* Normal name or expanded name. */
3601 if (check_changedtick(lp
->ll_name
))
3605 di
= find_var(lp
->ll_name
, NULL
);
3611 di
->di_flags
|= DI_FLAGS_LOCK
;
3613 di
->di_flags
&= ~DI_FLAGS_LOCK
;
3614 item_lock(&di
->di_tv
, deep
, lock
);
3619 else if (lp
->ll_range
)
3621 listitem_T
*li
= lp
->ll_li
;
3623 /* (un)lock a range of List items. */
3624 while (li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3626 item_lock(&li
->li_tv
, deep
, lock
);
3631 else if (lp
->ll_list
!= NULL
)
3632 /* (un)lock a List item. */
3633 item_lock(&lp
->ll_li
->li_tv
, deep
, lock
);
3635 /* un(lock) a Dictionary item. */
3636 item_lock(&lp
->ll_di
->di_tv
, deep
, lock
);
3642 * Lock or unlock an item. "deep" is nr of levels to go.
3645 item_lock(tv
, deep
, lock
)
3650 static int recurse
= 0;
3657 if (recurse
>= DICT_MAXNEST
)
3659 EMSG(_("E743: variable nested too deep for (un)lock"));
3666 /* lock/unlock the item itself */
3668 tv
->v_lock
|= VAR_LOCKED
;
3670 tv
->v_lock
&= ~VAR_LOCKED
;
3675 if ((l
= tv
->vval
.v_list
) != NULL
)
3678 l
->lv_lock
|= VAR_LOCKED
;
3680 l
->lv_lock
&= ~VAR_LOCKED
;
3681 if (deep
< 0 || deep
> 1)
3682 /* recursive: lock/unlock the items the List contains */
3683 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
3684 item_lock(&li
->li_tv
, deep
- 1, lock
);
3688 if ((d
= tv
->vval
.v_dict
) != NULL
)
3691 d
->dv_lock
|= VAR_LOCKED
;
3693 d
->dv_lock
&= ~VAR_LOCKED
;
3694 if (deep
< 0 || deep
> 1)
3696 /* recursive: lock/unlock the items the List contains */
3697 todo
= (int)d
->dv_hashtab
.ht_used
;
3698 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
3700 if (!HASHITEM_EMPTY(hi
))
3703 item_lock(&HI2DI(hi
)->di_tv
, deep
- 1, lock
);
3713 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3714 * or it refers to a List or Dictionary that is locked.
3720 return (tv
->v_lock
& VAR_LOCKED
)
3721 || (tv
->v_type
== VAR_LIST
3722 && tv
->vval
.v_list
!= NULL
3723 && (tv
->vval
.v_list
->lv_lock
& VAR_LOCKED
))
3724 || (tv
->v_type
== VAR_DICT
3725 && tv
->vval
.v_dict
!= NULL
3726 && (tv
->vval
.v_dict
->dv_lock
& VAR_LOCKED
));
3729 #if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3731 * Delete all "menutrans_" variables.
3734 del_menutrans_vars()
3739 hash_lock(&globvarht
);
3740 todo
= (int)globvarht
.ht_used
;
3741 for (hi
= globvarht
.ht_array
; todo
> 0 && !got_int
; ++hi
)
3743 if (!HASHITEM_EMPTY(hi
))
3746 if (STRNCMP(HI2DI(hi
)->di_key
, "menutrans_", 10) == 0)
3747 delete_var(&globvarht
, hi
);
3750 hash_unlock(&globvarht
);
3754 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3757 * Local string buffer for the next two functions to store a variable name
3758 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3759 * get_user_var_name().
3762 static char_u
*cat_prefix_varname
__ARGS((int prefix
, char_u
*name
));
3764 static char_u
*varnamebuf
= NULL
;
3765 static int varnamebuflen
= 0;
3768 * Function to concatenate a prefix and a variable name.
3771 cat_prefix_varname(prefix
, name
)
3777 len
= (int)STRLEN(name
) + 3;
3778 if (len
> varnamebuflen
)
3780 vim_free(varnamebuf
);
3781 len
+= 10; /* some additional space */
3782 varnamebuf
= alloc(len
);
3783 if (varnamebuf
== NULL
)
3788 varnamebuflen
= len
;
3790 *varnamebuf
= prefix
;
3791 varnamebuf
[1] = ':';
3792 STRCPY(varnamebuf
+ 2, name
);
3797 * Function given to ExpandGeneric() to obtain the list of user defined
3798 * (global/buffer/window/built-in) variable names.
3801 get_user_var_name(xp
, idx
)
3805 static long_u gdone
;
3806 static long_u bdone
;
3807 static long_u wdone
;
3809 static long_u tdone
;
3812 static hashitem_T
*hi
;
3817 gdone
= bdone
= wdone
= vidx
= 0;
3823 /* Global variables */
3824 if (gdone
< globvarht
.ht_used
)
3827 hi
= globvarht
.ht_array
;
3830 while (HASHITEM_EMPTY(hi
))
3832 if (STRNCMP("g:", xp
->xp_pattern
, 2) == 0)
3833 return cat_prefix_varname('g', hi
->hi_key
);
3838 ht
= &curbuf
->b_vars
.dv_hashtab
;
3839 if (bdone
< ht
->ht_used
)
3845 while (HASHITEM_EMPTY(hi
))
3847 return cat_prefix_varname('b', hi
->hi_key
);
3849 if (bdone
== ht
->ht_used
)
3852 return (char_u
*)"b:changedtick";
3856 ht
= &curwin
->w_vars
.dv_hashtab
;
3857 if (wdone
< ht
->ht_used
)
3863 while (HASHITEM_EMPTY(hi
))
3865 return cat_prefix_varname('w', hi
->hi_key
);
3870 ht
= &curtab
->tp_vars
.dv_hashtab
;
3871 if (tdone
< ht
->ht_used
)
3877 while (HASHITEM_EMPTY(hi
))
3879 return cat_prefix_varname('t', hi
->hi_key
);
3885 return cat_prefix_varname('v', (char_u
*)vimvars
[vidx
++].vv_name
);
3887 vim_free(varnamebuf
);
3893 #endif /* FEAT_CMDL_COMPL */
3896 * types for expressions.
3901 , TYPE_EQUAL
/* == */
3902 , TYPE_NEQUAL
/* != */
3903 , TYPE_GREATER
/* > */
3904 , TYPE_GEQUAL
/* >= */
3905 , TYPE_SMALLER
/* < */
3906 , TYPE_SEQUAL
/* <= */
3907 , TYPE_MATCH
/* =~ */
3908 , TYPE_NOMATCH
/* !~ */
3912 * The "evaluate" argument: When FALSE, the argument is only parsed but not
3913 * executed. The function may return OK, but the rettv will be of type
3914 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3918 * Handle zero level expression.
3919 * This calls eval1() and handles error message and nextcmd.
3920 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
3921 * Note: "rettv.v_lock" is not set.
3922 * Return OK or FAIL.
3925 eval0(arg
, rettv
, nextcmd
, evaluate
)
3935 ret
= eval1(&p
, rettv
, evaluate
);
3936 if (ret
== FAIL
|| !ends_excmd(*p
))
3941 * Report the invalid expression unless the expression evaluation has
3942 * been cancelled due to an aborting error, an interrupt, or an
3946 EMSG2(_(e_invexpr2
), arg
);
3949 if (nextcmd
!= NULL
)
3950 *nextcmd
= check_nextcmd(p
);
3956 * Handle top level expression:
3957 * expr2 ? expr1 : expr1
3959 * "arg" must point to the first non-white of the expression.
3960 * "arg" is advanced to the next non-white after the recognized expression.
3962 * Note: "rettv.v_lock" is not set.
3964 * Return OK or FAIL.
3967 eval1(arg
, rettv
, evaluate
)
3976 * Get the first variable.
3978 if (eval2(arg
, rettv
, evaluate
) == FAIL
)
3981 if ((*arg
)[0] == '?')
3988 if (get_tv_number_chk(rettv
, &error
) != 0)
3996 * Get the second variable.
3998 *arg
= skipwhite(*arg
+ 1);
3999 if (eval1(arg
, rettv
, evaluate
&& result
) == FAIL
) /* recursive! */
4003 * Check for the ":".
4005 if ((*arg
)[0] != ':')
4007 EMSG(_("E109: Missing ':' after '?'"));
4008 if (evaluate
&& result
)
4014 * Get the third variable.
4016 *arg
= skipwhite(*arg
+ 1);
4017 if (eval1(arg
, &var2
, evaluate
&& !result
) == FAIL
) /* recursive! */
4019 if (evaluate
&& result
)
4023 if (evaluate
&& !result
)
4031 * Handle first level expression:
4032 * expr2 || expr2 || expr2 logical OR
4034 * "arg" must point to the first non-white of the expression.
4035 * "arg" is advanced to the next non-white after the recognized expression.
4037 * Return OK or FAIL.
4040 eval2(arg
, rettv
, evaluate
)
4051 * Get the first variable.
4053 if (eval3(arg
, rettv
, evaluate
) == FAIL
)
4057 * Repeat until there is no following "||".
4061 while ((*arg
)[0] == '|' && (*arg
)[1] == '|')
4063 if (evaluate
&& first
)
4065 if (get_tv_number_chk(rettv
, &error
) != 0)
4074 * Get the second variable.
4076 *arg
= skipwhite(*arg
+ 2);
4077 if (eval3(arg
, &var2
, evaluate
&& !result
) == FAIL
)
4081 * Compute the result.
4083 if (evaluate
&& !result
)
4085 if (get_tv_number_chk(&var2
, &error
) != 0)
4093 rettv
->v_type
= VAR_NUMBER
;
4094 rettv
->vval
.v_number
= result
;
4102 * Handle second level expression:
4103 * expr3 && expr3 && expr3 logical AND
4105 * "arg" must point to the first non-white of the expression.
4106 * "arg" is advanced to the next non-white after the recognized expression.
4108 * Return OK or FAIL.
4111 eval3(arg
, rettv
, evaluate
)
4122 * Get the first variable.
4124 if (eval4(arg
, rettv
, evaluate
) == FAIL
)
4128 * Repeat until there is no following "&&".
4132 while ((*arg
)[0] == '&' && (*arg
)[1] == '&')
4134 if (evaluate
&& first
)
4136 if (get_tv_number_chk(rettv
, &error
) == 0)
4145 * Get the second variable.
4147 *arg
= skipwhite(*arg
+ 2);
4148 if (eval4(arg
, &var2
, evaluate
&& result
) == FAIL
)
4152 * Compute the result.
4154 if (evaluate
&& result
)
4156 if (get_tv_number_chk(&var2
, &error
) == 0)
4164 rettv
->v_type
= VAR_NUMBER
;
4165 rettv
->vval
.v_number
= result
;
4173 * Handle third level expression:
4185 * "arg" must point to the first non-white of the expression.
4186 * "arg" is advanced to the next non-white after the recognized expression.
4188 * Return OK or FAIL.
4191 eval4(arg
, rettv
, evaluate
)
4199 exptype_T type
= TYPE_UNKNOWN
;
4200 int type_is
= FALSE
; /* TRUE for "is" and "isnot" */
4204 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4205 regmatch_T regmatch
;
4210 * Get the first variable.
4212 if (eval5(arg
, rettv
, evaluate
) == FAIL
)
4218 case '=': if (p
[1] == '=')
4220 else if (p
[1] == '~')
4223 case '!': if (p
[1] == '=')
4225 else if (p
[1] == '~')
4226 type
= TYPE_NOMATCH
;
4228 case '>': if (p
[1] != '=')
4230 type
= TYPE_GREATER
;
4236 case '<': if (p
[1] != '=')
4238 type
= TYPE_SMALLER
;
4244 case 'i': if (p
[1] == 's')
4246 if (p
[2] == 'n' && p
[3] == 'o' && p
[4] == 't')
4248 if (!vim_isIDc(p
[len
]))
4250 type
= len
== 2 ? TYPE_EQUAL
: TYPE_NEQUAL
;
4258 * If there is a comparative operator, use it.
4260 if (type
!= TYPE_UNKNOWN
)
4262 /* extra question mark appended: ignore case */
4268 /* extra '#' appended: match case */
4269 else if (p
[len
] == '#')
4274 /* nothing appended: use 'ignorecase' */
4279 * Get the second variable.
4281 *arg
= skipwhite(p
+ len
);
4282 if (eval5(arg
, &var2
, evaluate
) == FAIL
)
4290 if (type_is
&& rettv
->v_type
!= var2
.v_type
)
4292 /* For "is" a different type always means FALSE, for "notis"
4294 n1
= (type
== TYPE_NEQUAL
);
4296 else if (rettv
->v_type
== VAR_LIST
|| var2
.v_type
== VAR_LIST
)
4300 n1
= (rettv
->v_type
== var2
.v_type
4301 && rettv
->vval
.v_list
== var2
.vval
.v_list
);
4302 if (type
== TYPE_NEQUAL
)
4305 else if (rettv
->v_type
!= var2
.v_type
4306 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4308 if (rettv
->v_type
!= var2
.v_type
)
4309 EMSG(_("E691: Can only compare List with List"));
4311 EMSG(_("E692: Invalid operation for Lists"));
4318 /* Compare two Lists for being equal or unequal. */
4319 n1
= list_equal(rettv
->vval
.v_list
, var2
.vval
.v_list
, ic
);
4320 if (type
== TYPE_NEQUAL
)
4325 else if (rettv
->v_type
== VAR_DICT
|| var2
.v_type
== VAR_DICT
)
4329 n1
= (rettv
->v_type
== var2
.v_type
4330 && rettv
->vval
.v_dict
== var2
.vval
.v_dict
);
4331 if (type
== TYPE_NEQUAL
)
4334 else if (rettv
->v_type
!= var2
.v_type
4335 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4337 if (rettv
->v_type
!= var2
.v_type
)
4338 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4340 EMSG(_("E736: Invalid operation for Dictionary"));
4347 /* Compare two Dictionaries for being equal or unequal. */
4348 n1
= dict_equal(rettv
->vval
.v_dict
, var2
.vval
.v_dict
, ic
);
4349 if (type
== TYPE_NEQUAL
)
4354 else if (rettv
->v_type
== VAR_FUNC
|| var2
.v_type
== VAR_FUNC
)
4356 if (rettv
->v_type
!= var2
.v_type
4357 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4359 if (rettv
->v_type
!= var2
.v_type
)
4360 EMSG(_("E693: Can only compare Funcref with Funcref"));
4362 EMSG(_("E694: Invalid operation for Funcrefs"));
4369 /* Compare two Funcrefs for being equal or unequal. */
4370 if (rettv
->vval
.v_string
== NULL
4371 || var2
.vval
.v_string
== NULL
)
4374 n1
= STRCMP(rettv
->vval
.v_string
,
4375 var2
.vval
.v_string
) == 0;
4376 if (type
== TYPE_NEQUAL
)
4383 * If one of the two variables is a float, compare as a float.
4384 * When using "=~" or "!~", always compare as string.
4386 else if ((rettv
->v_type
== VAR_FLOAT
|| var2
.v_type
== VAR_FLOAT
)
4387 && type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4391 if (rettv
->v_type
== VAR_FLOAT
)
4392 f1
= rettv
->vval
.v_float
;
4394 f1
= get_tv_number(rettv
);
4395 if (var2
.v_type
== VAR_FLOAT
)
4396 f2
= var2
.vval
.v_float
;
4398 f2
= get_tv_number(&var2
);
4402 case TYPE_EQUAL
: n1
= (f1
== f2
); break;
4403 case TYPE_NEQUAL
: n1
= (f1
!= f2
); break;
4404 case TYPE_GREATER
: n1
= (f1
> f2
); break;
4405 case TYPE_GEQUAL
: n1
= (f1
>= f2
); break;
4406 case TYPE_SMALLER
: n1
= (f1
< f2
); break;
4407 case TYPE_SEQUAL
: n1
= (f1
<= f2
); break;
4410 case TYPE_NOMATCH
: break; /* avoid gcc warning */
4416 * If one of the two variables is a number, compare as a number.
4417 * When using "=~" or "!~", always compare as string.
4419 else if ((rettv
->v_type
== VAR_NUMBER
|| var2
.v_type
== VAR_NUMBER
)
4420 && type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4422 n1
= get_tv_number(rettv
);
4423 n2
= get_tv_number(&var2
);
4426 case TYPE_EQUAL
: n1
= (n1
== n2
); break;
4427 case TYPE_NEQUAL
: n1
= (n1
!= n2
); break;
4428 case TYPE_GREATER
: n1
= (n1
> n2
); break;
4429 case TYPE_GEQUAL
: n1
= (n1
>= n2
); break;
4430 case TYPE_SMALLER
: n1
= (n1
< n2
); break;
4431 case TYPE_SEQUAL
: n1
= (n1
<= n2
); break;
4434 case TYPE_NOMATCH
: break; /* avoid gcc warning */
4439 s1
= get_tv_string_buf(rettv
, buf1
);
4440 s2
= get_tv_string_buf(&var2
, buf2
);
4441 if (type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4442 i
= ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
);
4448 case TYPE_EQUAL
: n1
= (i
== 0); break;
4449 case TYPE_NEQUAL
: n1
= (i
!= 0); break;
4450 case TYPE_GREATER
: n1
= (i
> 0); break;
4451 case TYPE_GEQUAL
: n1
= (i
>= 0); break;
4452 case TYPE_SMALLER
: n1
= (i
< 0); break;
4453 case TYPE_SEQUAL
: n1
= (i
<= 0); break;
4457 /* avoid 'l' flag in 'cpoptions' */
4459 p_cpo
= (char_u
*)"";
4460 regmatch
.regprog
= vim_regcomp(s2
,
4461 RE_MAGIC
+ RE_STRING
);
4462 regmatch
.rm_ic
= ic
;
4463 if (regmatch
.regprog
!= NULL
)
4465 n1
= vim_regexec_nl(®match
, s1
, (colnr_T
)0);
4466 vim_free(regmatch
.regprog
);
4467 if (type
== TYPE_NOMATCH
)
4473 case TYPE_UNKNOWN
: break; /* avoid gcc warning */
4478 rettv
->v_type
= VAR_NUMBER
;
4479 rettv
->vval
.v_number
= n1
;
4487 * Handle fourth level expression:
4489 * - number subtraction
4490 * . string concatenation
4492 * "arg" must point to the first non-white of the expression.
4493 * "arg" is advanced to the next non-white after the recognized expression.
4495 * Return OK or FAIL.
4498 eval5(arg
, rettv
, evaluate
)
4508 float_T f1
= 0, f2
= 0;
4511 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4515 * Get the first variable.
4517 if (eval6(arg
, rettv
, evaluate
, FALSE
) == FAIL
)
4521 * Repeat computing, until no '+', '-' or '.' is following.
4526 if (op
!= '+' && op
!= '-' && op
!= '.')
4529 if ((op
!= '+' || rettv
->v_type
!= VAR_LIST
)
4531 && (op
== '.' || rettv
->v_type
!= VAR_FLOAT
)
4535 /* For "list + ...", an illegal use of the first operand as
4536 * a number cannot be determined before evaluating the 2nd
4537 * operand: if this is also a list, all is ok.
4538 * For "something . ...", "something - ..." or "non-list + ...",
4539 * we know that the first operand needs to be a string or number
4540 * without evaluating the 2nd operand. So check before to avoid
4541 * side effects after an error. */
4542 if (evaluate
&& get_tv_string_chk(rettv
) == NULL
)
4550 * Get the second variable.
4552 *arg
= skipwhite(*arg
+ 1);
4553 if (eval6(arg
, &var2
, evaluate
, op
== '.') == FAIL
)
4562 * Compute the result.
4566 s1
= get_tv_string_buf(rettv
, buf1
); /* already checked */
4567 s2
= get_tv_string_buf_chk(&var2
, buf2
);
4568 if (s2
== NULL
) /* type error ? */
4574 p
= concat_str(s1
, s2
);
4576 rettv
->v_type
= VAR_STRING
;
4577 rettv
->vval
.v_string
= p
;
4579 else if (op
== '+' && rettv
->v_type
== VAR_LIST
4580 && var2
.v_type
== VAR_LIST
)
4582 /* concatenate Lists */
4583 if (list_concat(rettv
->vval
.v_list
, var2
.vval
.v_list
,
4598 if (rettv
->v_type
== VAR_FLOAT
)
4600 f1
= rettv
->vval
.v_float
;
4606 n1
= get_tv_number_chk(rettv
, &error
);
4609 /* This can only happen for "list + non-list". For
4610 * "non-list + ..." or "something - ...", we returned
4611 * before evaluating the 2nd operand. */
4616 if (var2
.v_type
== VAR_FLOAT
)
4621 if (var2
.v_type
== VAR_FLOAT
)
4623 f2
= var2
.vval
.v_float
;
4629 n2
= get_tv_number_chk(&var2
, &error
);
4637 if (rettv
->v_type
== VAR_FLOAT
)
4644 /* If there is a float on either side the result is a float. */
4645 if (rettv
->v_type
== VAR_FLOAT
|| var2
.v_type
== VAR_FLOAT
)
4651 rettv
->v_type
= VAR_FLOAT
;
4652 rettv
->vval
.v_float
= f1
;
4661 rettv
->v_type
= VAR_NUMBER
;
4662 rettv
->vval
.v_number
= n1
;
4672 * Handle fifth level expression:
4673 * * number multiplication
4677 * "arg" must point to the first non-white of the expression.
4678 * "arg" is advanced to the next non-white after the recognized expression.
4680 * Return OK or FAIL.
4683 eval6(arg
, rettv
, evaluate
, want_string
)
4687 int want_string
; /* after "." operator */
4693 int use_float
= FALSE
;
4699 * Get the first variable.
4701 if (eval7(arg
, rettv
, evaluate
, want_string
) == FAIL
)
4705 * Repeat computing, until no '*', '/' or '%' is following.
4710 if (op
!= '*' && op
!= '/' && op
!= '%')
4716 if (rettv
->v_type
== VAR_FLOAT
)
4718 f1
= rettv
->vval
.v_float
;
4724 n1
= get_tv_number_chk(rettv
, &error
);
4733 * Get the second variable.
4735 *arg
= skipwhite(*arg
+ 1);
4736 if (eval7(arg
, &var2
, evaluate
, FALSE
) == FAIL
)
4742 if (var2
.v_type
== VAR_FLOAT
)
4749 f2
= var2
.vval
.v_float
;
4755 n2
= get_tv_number_chk(&var2
, &error
);
4766 * Compute the result.
4767 * When either side is a float the result is a float.
4776 /* We rely on the floating point library to handle divide
4777 * by zero to result in "inf" and not a crash. */
4782 EMSG(_("E804: Cannot use '%' with Float"));
4785 rettv
->v_type
= VAR_FLOAT
;
4786 rettv
->vval
.v_float
= f1
;
4795 if (n2
== 0) /* give an error message? */
4798 n1
= -0x7fffffffL
- 1L; /* similar to NaN */
4809 if (n2
== 0) /* give an error message? */
4814 rettv
->v_type
= VAR_NUMBER
;
4815 rettv
->vval
.v_number
= n1
;
4824 * Handle sixth level expression:
4825 * number number constant
4826 * "string" string constant
4827 * 'string' literal string constant
4828 * &option-name option value
4829 * @r register contents
4830 * identifier variable value
4831 * function() function call
4832 * $VAR environment variable
4833 * (expression) nested expression
4835 * {key: val, key: val} Dictionary
4838 * ! in front logical NOT
4839 * - in front unary minus
4840 * + in front unary plus (ignored)
4841 * trailing [] subscript in String or List
4842 * trailing .name entry in Dictionary
4844 * "arg" must point to the first non-white of the expression.
4845 * "arg" is advanced to the next non-white after the recognized expression.
4847 * Return OK or FAIL.
4850 eval7(arg
, rettv
, evaluate
, want_string
)
4854 int want_string
; /* after "." operator */
4859 char_u
*start_leader
, *end_leader
;
4864 * Initialise variable so that clear_tv() can't mistake this for a
4865 * string and free a string that isn't there.
4867 rettv
->v_type
= VAR_UNKNOWN
;
4870 * Skip '!' and '-' characters. They are handled later.
4872 start_leader
= *arg
;
4873 while (**arg
== '!' || **arg
== '-' || **arg
== '+')
4874 *arg
= skipwhite(*arg
+ 1);
4894 char_u
*p
= skipdigits(*arg
+ 1);
4895 int get_float
= FALSE
;
4897 /* We accept a float when the format matches
4898 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
4899 * strict to avoid backwards compatibility problems.
4900 * Don't look for a float after the "." operator, so that
4901 * ":let vers = 1.2.3" doesn't fail. */
4902 if (!want_string
&& p
[0] == '.' && vim_isdigit(p
[1]))
4905 p
= skipdigits(p
+ 2);
4906 if (*p
== 'e' || *p
== 'E')
4909 if (*p
== '-' || *p
== '+')
4911 if (!vim_isdigit(*p
))
4914 p
= skipdigits(p
+ 1);
4916 if (ASCII_ISALPHA(*p
) || *p
== '.')
4923 *arg
+= string2float(*arg
, &f
);
4926 rettv
->v_type
= VAR_FLOAT
;
4927 rettv
->vval
.v_float
= f
;
4933 vim_str2nr(*arg
, NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
4937 rettv
->v_type
= VAR_NUMBER
;
4938 rettv
->vval
.v_number
= n
;
4945 * String constant: "string".
4947 case '"': ret
= get_string_tv(arg
, rettv
, evaluate
);
4951 * Literal string constant: 'str''ing'.
4953 case '\'': ret
= get_lit_string_tv(arg
, rettv
, evaluate
);
4957 * List: [expr, expr]
4959 case '[': ret
= get_list_tv(arg
, rettv
, evaluate
);
4963 * Dictionary: {key: val, key: val}
4965 case '{': ret
= get_dict_tv(arg
, rettv
, evaluate
);
4969 * Option value: &name
4971 case '&': ret
= get_option_tv(arg
, rettv
, evaluate
);
4975 * Environment variable: $VAR.
4977 case '$': ret
= get_env_tv(arg
, rettv
, evaluate
);
4981 * Register contents: @r.
4986 rettv
->v_type
= VAR_STRING
;
4987 rettv
->vval
.v_string
= get_reg_contents(**arg
, TRUE
, TRUE
);
4994 * nested expression: (expression).
4996 case '(': *arg
= skipwhite(*arg
+ 1);
4997 ret
= eval1(arg
, rettv
, evaluate
); /* recursive! */
5002 EMSG(_("E110: Missing ')'"));
5008 default: ret
= NOTDONE
;
5015 * Must be a variable or function name.
5016 * Can also be a curly-braces kind of name: {expr}.
5019 len
= get_name_len(arg
, &alias
, evaluate
, TRUE
);
5027 if (**arg
== '(') /* recursive! */
5029 /* If "s" is the name of a variable of type VAR_FUNC
5030 * use its contents. */
5031 s
= deref_func_name(s
, &len
);
5033 /* Invoke the function. */
5034 ret
= get_func_tv(s
, len
, rettv
, arg
,
5035 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
5036 &len
, evaluate
, NULL
);
5037 /* Stop the expression evaluation when immediately
5038 * aborting on error, or when an interrupt occurred or
5039 * an exception was thrown but not caught. */
5048 ret
= get_var_tv(s
, len
, rettv
, TRUE
);
5057 *arg
= skipwhite(*arg
);
5059 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5062 ret
= handle_subscript(arg
, rettv
, evaluate
, TRUE
);
5065 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5067 if (ret
== OK
&& evaluate
&& end_leader
> start_leader
)
5074 if (rettv
->v_type
== VAR_FLOAT
)
5075 f
= rettv
->vval
.v_float
;
5078 val
= get_tv_number_chk(rettv
, &error
);
5086 while (end_leader
> start_leader
)
5089 if (*end_leader
== '!')
5092 if (rettv
->v_type
== VAR_FLOAT
)
5098 else if (*end_leader
== '-')
5101 if (rettv
->v_type
== VAR_FLOAT
)
5109 if (rettv
->v_type
== VAR_FLOAT
)
5112 rettv
->vval
.v_float
= f
;
5118 rettv
->v_type
= VAR_NUMBER
;
5119 rettv
->vval
.v_number
= val
;
5128 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5129 * "*arg" points to the '[' or '.'.
5130 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5133 eval_index(arg
, rettv
, evaluate
, verbose
)
5137 int verbose
; /* give error messages */
5139 int empty1
= FALSE
, empty2
= FALSE
;
5140 typval_T var1
, var2
;
5147 if (rettv
->v_type
== VAR_FUNC
5149 || rettv
->v_type
== VAR_FLOAT
5154 EMSG(_("E695: Cannot index a Funcref"));
5164 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
5168 *arg
= skipwhite(key
+ len
);
5175 * Get the (first) variable from inside the [].
5177 *arg
= skipwhite(*arg
+ 1);
5180 else if (eval1(arg
, &var1
, evaluate
) == FAIL
) /* recursive! */
5182 else if (evaluate
&& get_tv_string_chk(&var1
) == NULL
)
5184 /* not a number or string */
5190 * Get the second variable from inside the [:].
5195 *arg
= skipwhite(*arg
+ 1);
5198 else if (eval1(arg
, &var2
, evaluate
) == FAIL
) /* recursive! */
5204 else if (evaluate
&& get_tv_string_chk(&var2
) == NULL
)
5206 /* not a number or string */
5214 /* Check for the ']'. */
5218 EMSG(_(e_missbrac
));
5224 *arg
= skipwhite(*arg
+ 1); /* skip the ']' */
5230 if (!empty1
&& rettv
->v_type
!= VAR_DICT
)
5232 n1
= get_tv_number(&var1
);
5241 n2
= get_tv_number(&var2
);
5246 switch (rettv
->v_type
)
5250 s
= get_tv_string(rettv
);
5251 len
= (long)STRLEN(s
);
5254 /* The resulting variable is a substring. If the indexes
5255 * are out of range the result is empty. */
5266 if (n1
>= len
|| n2
< 0 || n1
> n2
)
5269 s
= vim_strnsave(s
+ n1
, (int)(n2
- n1
+ 1));
5273 /* The resulting variable is a string of a single
5274 * character. If the index is too big or negative the
5275 * result is empty. */
5276 if (n1
>= len
|| n1
< 0)
5279 s
= vim_strnsave(s
+ n1
, 1);
5282 rettv
->v_type
= VAR_STRING
;
5283 rettv
->vval
.v_string
= s
;
5287 len
= list_len(rettv
->vval
.v_list
);
5290 if (!empty1
&& (n1
< 0 || n1
>= len
))
5292 /* For a range we allow invalid values and return an empty
5293 * list. A list index out of range is an error. */
5297 EMSGN(_(e_listidx
), n1
);
5311 if (!empty2
&& (n2
< 0 || n2
+ 1 < n1
))
5316 for (item
= list_find(rettv
->vval
.v_list
, n1
);
5319 if (list_append_tv(l
, &item
->li_tv
) == FAIL
)
5324 item
= item
->li_next
;
5327 rettv
->v_type
= VAR_LIST
;
5328 rettv
->vval
.v_list
= l
;
5333 copy_tv(&list_find(rettv
->vval
.v_list
, n1
)->li_tv
, &var1
);
5343 EMSG(_(e_dictrange
));
5353 key
= get_tv_string(&var1
);
5357 EMSG(_(e_emptykey
));
5363 item
= dict_find(rettv
->vval
.v_dict
, key
, (int)len
);
5365 if (item
== NULL
&& verbose
)
5366 EMSG2(_(e_dictkey
), key
);
5372 copy_tv(&item
->di_tv
, &var1
);
5384 * Get an option value.
5385 * "arg" points to the '&' or '+' before the option name.
5386 * "arg" is advanced to character after the option name.
5387 * Return OK or FAIL.
5390 get_option_tv(arg
, rettv
, evaluate
)
5392 typval_T
*rettv
; /* when NULL, only check if option exists */
5400 int working
= (**arg
== '+'); /* has("+option") */
5405 * Isolate the option name and find its value.
5407 option_end
= find_option_end(arg
, &opt_flags
);
5408 if (option_end
== NULL
)
5411 EMSG2(_("E112: Option name missing: %s"), *arg
);
5423 opt_type
= get_option_value(*arg
, &numval
,
5424 rettv
== NULL
? NULL
: &stringval
, opt_flags
);
5426 if (opt_type
== -3) /* invalid name */
5429 EMSG2(_("E113: Unknown option: %s"), *arg
);
5432 else if (rettv
!= NULL
)
5434 if (opt_type
== -2) /* hidden string option */
5436 rettv
->v_type
= VAR_STRING
;
5437 rettv
->vval
.v_string
= NULL
;
5439 else if (opt_type
== -1) /* hidden number option */
5441 rettv
->v_type
= VAR_NUMBER
;
5442 rettv
->vval
.v_number
= 0;
5444 else if (opt_type
== 1) /* number option */
5446 rettv
->v_type
= VAR_NUMBER
;
5447 rettv
->vval
.v_number
= numval
;
5449 else /* string option */
5451 rettv
->v_type
= VAR_STRING
;
5452 rettv
->vval
.v_string
= stringval
;
5455 else if (working
&& (opt_type
== -2 || opt_type
== -1))
5458 *option_end
= c
; /* put back for error messages */
5465 * Allocate a variable for a string constant.
5466 * Return OK or FAIL.
5469 get_string_tv(arg
, rettv
, evaluate
)
5479 * Find the end of the string, skipping backslashed characters.
5481 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
5483 if (*p
== '\\' && p
[1] != NUL
)
5486 /* A "\<x>" form occupies at least 4 characters, and produces up
5487 * to 6 characters: reserve space for 2 extra */
5495 EMSG2(_("E114: Missing quote: %s"), *arg
);
5499 /* If only parsing, set *arg and return here */
5507 * Copy the string into allocated memory, handling backslashed
5510 name
= alloc((unsigned)(p
- *arg
+ extra
));
5513 rettv
->v_type
= VAR_STRING
;
5514 rettv
->vval
.v_string
= name
;
5516 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; )
5522 case 'b': *name
++ = BS
; ++p
; break;
5523 case 'e': *name
++ = ESC
; ++p
; break;
5524 case 'f': *name
++ = FF
; ++p
; break;
5525 case 'n': *name
++ = NL
; ++p
; break;
5526 case 'r': *name
++ = CAR
; ++p
; break;
5527 case 't': *name
++ = TAB
; ++p
; break;
5529 case 'X': /* hex: "\x1", "\x12" */
5531 case 'u': /* Unicode: "\u0023" */
5533 if (vim_isxdigit(p
[1]))
5536 int c
= toupper(*p
);
5543 while (--n
>= 0 && vim_isxdigit(p
[1]))
5546 nr
= (nr
<< 4) + hex2nr(*p
);
5550 /* For "\u" store the number according to
5553 name
+= (*mb_char2bytes
)(nr
, name
);
5560 /* octal: "\1", "\12", "\123" */
5568 case '7': *name
= *p
++ - '0';
5569 if (*p
>= '0' && *p
<= '7')
5571 *name
= (*name
<< 3) + *p
++ - '0';
5572 if (*p
>= '0' && *p
<= '7')
5573 *name
= (*name
<< 3) + *p
++ - '0';
5578 /* Special key, e.g.: "\<C-W>" */
5579 case '<': extra
= trans_special(&p
, name
, TRUE
);
5587 default: MB_COPY_CHAR(p
, name
);
5592 MB_COPY_CHAR(p
, name
);
5602 * Allocate a variable for a 'str''ing' constant.
5603 * Return OK or FAIL.
5606 get_lit_string_tv(arg
, rettv
, evaluate
)
5616 * Find the end of the string, skipping ''.
5618 for (p
= *arg
+ 1; *p
!= NUL
; mb_ptr_adv(p
))
5631 EMSG2(_("E115: Missing quote: %s"), *arg
);
5635 /* If only parsing return after setting "*arg" */
5643 * Copy the string into allocated memory, handling '' to ' reduction.
5645 str
= alloc((unsigned)((p
- *arg
) - reduce
));
5648 rettv
->v_type
= VAR_STRING
;
5649 rettv
->vval
.v_string
= str
;
5651 for (p
= *arg
+ 1; *p
!= NUL
; )
5659 MB_COPY_CHAR(p
, str
);
5668 * Allocate a variable for a List and fill it from "*arg".
5669 * Return OK or FAIL.
5672 get_list_tv(arg
, rettv
, evaluate
)
5688 *arg
= skipwhite(*arg
+ 1);
5689 while (**arg
!= ']' && **arg
!= NUL
)
5691 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
5695 item
= listitem_alloc();
5699 item
->li_tv
.v_lock
= 0;
5700 list_append(l
, item
);
5710 EMSG2(_("E696: Missing comma in List: %s"), *arg
);
5713 *arg
= skipwhite(*arg
+ 1);
5718 EMSG2(_("E697: Missing end of List ']': %s"), *arg
);
5725 *arg
= skipwhite(*arg
+ 1);
5728 rettv
->v_type
= VAR_LIST
;
5729 rettv
->vval
.v_list
= l
;
5737 * Allocate an empty header for a list.
5738 * Caller should take care of the reference count.
5745 l
= (list_T
*)alloc_clear(sizeof(list_T
));
5748 /* Prepend the list to the list of lists for garbage collection. */
5749 if (first_list
!= NULL
)
5750 first_list
->lv_used_prev
= l
;
5751 l
->lv_used_prev
= NULL
;
5752 l
->lv_used_next
= first_list
;
5759 * Allocate an empty list for a return value.
5760 * Returns OK or FAIL.
5763 rettv_list_alloc(rettv
)
5766 list_T
*l
= list_alloc();
5771 rettv
->vval
.v_list
= l
;
5772 rettv
->v_type
= VAR_LIST
;
5778 * Unreference a list: decrement the reference count and free it when it
5785 if (l
!= NULL
&& --l
->lv_refcount
<= 0)
5790 * Free a list, including all items it points to.
5791 * Ignores the reference count.
5794 list_free(l
, recurse
)
5796 int recurse
; /* Free Lists and Dictionaries recursively. */
5800 /* Remove the list from the list of lists for garbage collection. */
5801 if (l
->lv_used_prev
== NULL
)
5802 first_list
= l
->lv_used_next
;
5804 l
->lv_used_prev
->lv_used_next
= l
->lv_used_next
;
5805 if (l
->lv_used_next
!= NULL
)
5806 l
->lv_used_next
->lv_used_prev
= l
->lv_used_prev
;
5808 for (item
= l
->lv_first
; item
!= NULL
; item
= l
->lv_first
)
5810 /* Remove the item before deleting it. */
5811 l
->lv_first
= item
->li_next
;
5812 if (recurse
|| (item
->li_tv
.v_type
!= VAR_LIST
5813 && item
->li_tv
.v_type
!= VAR_DICT
))
5814 clear_tv(&item
->li_tv
);
5821 * Allocate a list item.
5826 return (listitem_T
*)alloc(sizeof(listitem_T
));
5830 * Free a list item. Also clears the value. Does not notify watchers.
5836 clear_tv(&item
->li_tv
);
5841 * Remove a list item from a List and free it. Also clears the value.
5844 listitem_remove(l
, item
)
5848 list_remove(l
, item
, item
);
5849 listitem_free(item
);
5853 * Get the number of items in a list.
5865 * Return TRUE when two lists have exactly the same values.
5868 list_equal(l1
, l2
, ic
)
5871 int ic
; /* ignore case for strings */
5873 listitem_T
*item1
, *item2
;
5875 if (l1
== NULL
|| l2
== NULL
)
5879 if (list_len(l1
) != list_len(l2
))
5882 for (item1
= l1
->lv_first
, item2
= l2
->lv_first
;
5883 item1
!= NULL
&& item2
!= NULL
;
5884 item1
= item1
->li_next
, item2
= item2
->li_next
)
5885 if (!tv_equal(&item1
->li_tv
, &item2
->li_tv
, ic
))
5887 return item1
== NULL
&& item2
== NULL
;
5890 #if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_MZSCHEME) \
5893 * Return the dictitem that an entry in a hashtable points to.
5904 * Return TRUE when two dictionaries have exactly the same key/values.
5907 dict_equal(d1
, d2
, ic
)
5910 int ic
; /* ignore case for strings */
5916 if (d1
== NULL
|| d2
== NULL
)
5920 if (dict_len(d1
) != dict_len(d2
))
5923 todo
= (int)d1
->dv_hashtab
.ht_used
;
5924 for (hi
= d1
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
5926 if (!HASHITEM_EMPTY(hi
))
5928 item2
= dict_find(d2
, hi
->hi_key
, -1);
5931 if (!tv_equal(&HI2DI(hi
)->di_tv
, &item2
->di_tv
, ic
))
5940 * Return TRUE if "tv1" and "tv2" have the same value.
5941 * Compares the items just like "==" would compare them, but strings and
5942 * numbers are different. Floats and numbers are also different.
5945 tv_equal(tv1
, tv2
, ic
)
5948 int ic
; /* ignore case */
5950 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
5952 static int recursive
= 0; /* cach recursive loops */
5955 if (tv1
->v_type
!= tv2
->v_type
)
5957 /* Catch lists and dicts that have an endless loop by limiting
5958 * recursiveness to 1000. We guess they are equal then. */
5959 if (recursive
>= 1000)
5962 switch (tv1
->v_type
)
5966 r
= list_equal(tv1
->vval
.v_list
, tv2
->vval
.v_list
, ic
);
5972 r
= dict_equal(tv1
->vval
.v_dict
, tv2
->vval
.v_dict
, ic
);
5977 return (tv1
->vval
.v_string
!= NULL
5978 && tv2
->vval
.v_string
!= NULL
5979 && STRCMP(tv1
->vval
.v_string
, tv2
->vval
.v_string
) == 0);
5982 return tv1
->vval
.v_number
== tv2
->vval
.v_number
;
5986 return tv1
->vval
.v_float
== tv2
->vval
.v_float
;
5990 s1
= get_tv_string_buf(tv1
, buf1
);
5991 s2
= get_tv_string_buf(tv2
, buf2
);
5992 return ((ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
)) == 0);
5995 EMSG2(_(e_intern2
), "tv_equal()");
6000 * Locate item with index "n" in list "l" and return it.
6001 * A negative index is counted from the end; -1 is the last item.
6002 * Returns NULL when "n" is out of range.
6015 /* Negative index is relative to the end. */
6019 /* Check for index out of range. */
6020 if (n
< 0 || n
>= l
->lv_len
)
6023 /* When there is a cached index may start search from there. */
6024 if (l
->lv_idx_item
!= NULL
)
6026 if (n
< l
->lv_idx
/ 2)
6028 /* closest to the start of the list */
6032 else if (n
> (l
->lv_idx
+ l
->lv_len
) / 2)
6034 /* closest to the end of the list */
6036 idx
= l
->lv_len
- 1;
6040 /* closest to the cached index */
6041 item
= l
->lv_idx_item
;
6047 if (n
< l
->lv_len
/ 2)
6049 /* closest to the start of the list */
6055 /* closest to the end of the list */
6057 idx
= l
->lv_len
- 1;
6063 /* search forward */
6064 item
= item
->li_next
;
6069 /* search backward */
6070 item
= item
->li_prev
;
6074 /* cache the used index */
6076 l
->lv_idx_item
= item
;
6082 * Get list item "l[idx]" as a number.
6085 list_find_nr(l
, idx
, errorp
)
6088 int *errorp
; /* set to TRUE when something wrong */
6092 li
= list_find(l
, idx
);
6099 return get_tv_number_chk(&li
->li_tv
, errorp
);
6103 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6106 list_find_str(l
, idx
)
6112 li
= list_find(l
, idx
- 1);
6115 EMSGN(_(e_listidx
), idx
);
6118 return get_tv_string(&li
->li_tv
);
6122 * Locate "item" list "l" and return its index.
6123 * Returns -1 when "item" is not in the list.
6126 list_idx_of_item(l
, item
)
6136 for (li
= l
->lv_first
; li
!= NULL
&& li
!= item
; li
= li
->li_next
)
6144 * Append item "item" to the end of list "l".
6147 list_append(l
, item
)
6151 if (l
->lv_last
== NULL
)
6156 item
->li_prev
= NULL
;
6160 l
->lv_last
->li_next
= item
;
6161 item
->li_prev
= l
->lv_last
;
6165 item
->li_next
= NULL
;
6169 * Append typval_T "tv" to the end of list "l".
6170 * Return FAIL when out of memory.
6173 list_append_tv(l
, tv
)
6177 listitem_T
*li
= listitem_alloc();
6181 copy_tv(tv
, &li
->li_tv
);
6187 * Add a dictionary to a list. Used by getqflist().
6188 * Return FAIL when out of memory.
6191 list_append_dict(list
, dict
)
6195 listitem_T
*li
= listitem_alloc();
6199 li
->li_tv
.v_type
= VAR_DICT
;
6200 li
->li_tv
.v_lock
= 0;
6201 li
->li_tv
.vval
.v_dict
= dict
;
6202 list_append(list
, li
);
6203 ++dict
->dv_refcount
;
6208 * Make a copy of "str" and append it as an item to list "l".
6209 * When "len" >= 0 use "str[len]".
6210 * Returns FAIL when out of memory.
6213 list_append_string(l
, str
, len
)
6218 listitem_T
*li
= listitem_alloc();
6223 li
->li_tv
.v_type
= VAR_STRING
;
6224 li
->li_tv
.v_lock
= 0;
6226 li
->li_tv
.vval
.v_string
= NULL
;
6227 else if ((li
->li_tv
.vval
.v_string
= (len
>= 0 ? vim_strnsave(str
, len
)
6228 : vim_strsave(str
))) == NULL
)
6234 * Append "n" to list "l".
6235 * Returns FAIL when out of memory.
6238 list_append_number(l
, n
)
6244 li
= listitem_alloc();
6247 li
->li_tv
.v_type
= VAR_NUMBER
;
6248 li
->li_tv
.v_lock
= 0;
6249 li
->li_tv
.vval
.v_number
= n
;
6255 * Insert typval_T "tv" in list "l" before "item".
6256 * If "item" is NULL append at the end.
6257 * Return FAIL when out of memory.
6260 list_insert_tv(l
, tv
, item
)
6265 listitem_T
*ni
= listitem_alloc();
6269 copy_tv(tv
, &ni
->li_tv
);
6271 /* Append new item at end of list. */
6275 /* Insert new item before existing item. */
6276 ni
->li_prev
= item
->li_prev
;
6278 if (item
->li_prev
== NULL
)
6285 item
->li_prev
->li_next
= ni
;
6286 l
->lv_idx_item
= NULL
;
6295 * Extend "l1" with "l2".
6296 * If "bef" is NULL append at the end, otherwise insert before this item.
6297 * Returns FAIL when out of memory.
6300 list_extend(l1
, l2
, bef
)
6306 int todo
= l2
->lv_len
;
6308 /* We also quit the loop when we have inserted the original item count of
6309 * the list, avoid a hang when we extend a list with itself. */
6310 for (item
= l2
->lv_first
; item
!= NULL
&& --todo
>= 0; item
= item
->li_next
)
6311 if (list_insert_tv(l1
, &item
->li_tv
, bef
) == FAIL
)
6317 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6318 * Return FAIL when out of memory.
6321 list_concat(l1
, l2
, tv
)
6328 if (l1
== NULL
|| l2
== NULL
)
6331 /* make a copy of the first list. */
6332 l
= list_copy(l1
, FALSE
, 0);
6335 tv
->v_type
= VAR_LIST
;
6336 tv
->vval
.v_list
= l
;
6338 /* append all items from the second list */
6339 return list_extend(l
, l2
, NULL
);
6343 * Make a copy of list "orig". Shallow if "deep" is FALSE.
6344 * The refcount of the new list is set to 1.
6345 * See item_copy() for "copyID".
6346 * Returns NULL when out of memory.
6349 list_copy(orig
, deep
, copyID
)
6361 copy
= list_alloc();
6366 /* Do this before adding the items, because one of the items may
6367 * refer back to this list. */
6368 orig
->lv_copyID
= copyID
;
6369 orig
->lv_copylist
= copy
;
6371 for (item
= orig
->lv_first
; item
!= NULL
&& !got_int
;
6372 item
= item
->li_next
)
6374 ni
= listitem_alloc();
6379 if (item_copy(&item
->li_tv
, &ni
->li_tv
, deep
, copyID
) == FAIL
)
6386 copy_tv(&item
->li_tv
, &ni
->li_tv
);
6387 list_append(copy
, ni
);
6389 ++copy
->lv_refcount
;
6401 * Remove items "item" to "item2" from list "l".
6402 * Does not free the listitem or the value!
6405 list_remove(l
, item
, item2
)
6412 /* notify watchers */
6413 for (ip
= item
; ip
!= NULL
; ip
= ip
->li_next
)
6416 list_fix_watch(l
, ip
);
6421 if (item2
->li_next
== NULL
)
6422 l
->lv_last
= item
->li_prev
;
6424 item2
->li_next
->li_prev
= item
->li_prev
;
6425 if (item
->li_prev
== NULL
)
6426 l
->lv_first
= item2
->li_next
;
6428 item
->li_prev
->li_next
= item2
->li_next
;
6429 l
->lv_idx_item
= NULL
;
6433 * Return an allocated string with the string representation of a list.
6437 list2string(tv
, copyID
)
6443 if (tv
->vval
.v_list
== NULL
)
6445 ga_init2(&ga
, (int)sizeof(char), 80);
6446 ga_append(&ga
, '[');
6447 if (list_join(&ga
, tv
->vval
.v_list
, (char_u
*)", ", FALSE
, copyID
) == FAIL
)
6449 vim_free(ga
.ga_data
);
6452 ga_append(&ga
, ']');
6453 ga_append(&ga
, NUL
);
6454 return (char_u
*)ga
.ga_data
;
6458 * Join list "l" into a string in "*gap", using separator "sep".
6459 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
6460 * Return FAIL or OK.
6463 list_join(gap
, l
, sep
, echo
, copyID
)
6472 char_u numbuf
[NUMBUFLEN
];
6476 for (item
= l
->lv_first
; item
!= NULL
&& !got_int
; item
= item
->li_next
)
6481 ga_concat(gap
, sep
);
6484 s
= echo_string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6486 s
= tv2string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6498 * Garbage collection for lists and dictionaries.
6500 * We use reference counts to be able to free most items right away when they
6501 * are no longer used. But for composite items it's possible that it becomes
6502 * unused while the reference count is > 0: When there is a recursive
6503 * reference. Example:
6504 * :let l = [1, 2, 3]
6508 * Since this is quite unusual we handle this with garbage collection: every
6509 * once in a while find out which lists and dicts are not referenced from any
6512 * Here is a good reference text about garbage collection (refers to Python
6513 * but it applies to all reference-counting mechanisms):
6514 * http://python.ca/nas/python/gc/
6518 * Do garbage collection for lists and dicts.
6519 * Return TRUE if some memory was freed.
6528 funccall_T
*fc
, **pfc
;
6530 int did_free_funccal
= FALSE
;
6535 /* Only do this once. */
6536 want_garbage_collect
= FALSE
;
6537 may_garbage_collect
= FALSE
;
6538 garbage_collect_at_exit
= FALSE
;
6540 /* We advance by two because we add one for items referenced through
6541 * previous_funccal. */
6542 current_copyID
+= COPYID_INC
;
6543 copyID
= current_copyID
;
6546 * 1. Go through all accessible variables and mark all lists and dicts
6550 /* Don't free variables in the previous_funccal list unless they are only
6551 * referenced through previous_funccal. This must be first, because if
6552 * the item is referenced elsewhere the funccal must not be freed. */
6553 for (fc
= previous_funccal
; fc
!= NULL
; fc
= fc
->caller
)
6555 set_ref_in_ht(&fc
->l_vars
.dv_hashtab
, copyID
+ 1);
6556 set_ref_in_ht(&fc
->l_avars
.dv_hashtab
, copyID
+ 1);
6559 /* script-local variables */
6560 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
6561 set_ref_in_ht(&SCRIPT_VARS(i
), copyID
);
6563 /* buffer-local variables */
6564 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
6565 set_ref_in_ht(&buf
->b_vars
.dv_hashtab
, copyID
);
6567 /* window-local variables */
6568 FOR_ALL_TAB_WINDOWS(tp
, wp
)
6569 set_ref_in_ht(&wp
->w_vars
.dv_hashtab
, copyID
);
6572 /* tabpage-local variables */
6573 for (tp
= first_tabpage
; tp
!= NULL
; tp
= tp
->tp_next
)
6574 set_ref_in_ht(&tp
->tp_vars
.dv_hashtab
, copyID
);
6577 /* global variables */
6578 set_ref_in_ht(&globvarht
, copyID
);
6580 /* function-local variables */
6581 for (fc
= current_funccal
; fc
!= NULL
; fc
= fc
->caller
)
6583 set_ref_in_ht(&fc
->l_vars
.dv_hashtab
, copyID
);
6584 set_ref_in_ht(&fc
->l_avars
.dv_hashtab
, copyID
);
6588 set_ref_in_ht(&vimvarht
, copyID
);
6591 * 2. Free lists and dictionaries that are not referenced.
6593 did_free
= free_unref_items(copyID
);
6596 * 3. Check if any funccal can be freed now.
6598 for (pfc
= &previous_funccal
; *pfc
!= NULL
; )
6600 if (can_free_funccal(*pfc
, copyID
))
6604 free_funccal(fc
, TRUE
);
6606 did_free_funccal
= TRUE
;
6609 pfc
= &(*pfc
)->caller
;
6611 if (did_free_funccal
)
6612 /* When a funccal was freed some more items might be garbage
6613 * collected, so run again. */
6614 (void)garbage_collect();
6620 * Free lists and dictionaries that are no longer referenced.
6623 free_unref_items(copyID
)
6628 int did_free
= FALSE
;
6631 * Go through the list of dicts and free items without the copyID.
6633 for (dd
= first_dict
; dd
!= NULL
; )
6634 if ((dd
->dv_copyID
& COPYID_MASK
) != (copyID
& COPYID_MASK
))
6636 /* Free the Dictionary and ordinary items it contains, but don't
6637 * recurse into Lists and Dictionaries, they will be in the list
6638 * of dicts or list of lists. */
6639 dict_free(dd
, FALSE
);
6642 /* restart, next dict may also have been freed */
6646 dd
= dd
->dv_used_next
;
6649 * Go through the list of lists and free items without the copyID.
6650 * But don't free a list that has a watcher (used in a for loop), these
6651 * are not referenced anywhere.
6653 for (ll
= first_list
; ll
!= NULL
; )
6654 if ((ll
->lv_copyID
& COPYID_MASK
) != (copyID
& COPYID_MASK
)
6655 && ll
->lv_watch
== NULL
)
6657 /* Free the List and ordinary items it contains, but don't recurse
6658 * into Lists and Dictionaries, they will be in the list of dicts
6659 * or list of lists. */
6660 list_free(ll
, FALSE
);
6663 /* restart, next list may also have been freed */
6667 ll
= ll
->lv_used_next
;
6673 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6676 set_ref_in_ht(ht
, copyID
)
6683 todo
= (int)ht
->ht_used
;
6684 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
6685 if (!HASHITEM_EMPTY(hi
))
6688 set_ref_in_item(&HI2DI(hi
)->di_tv
, copyID
);
6693 * Mark all lists and dicts referenced through list "l" with "copyID".
6696 set_ref_in_list(l
, copyID
)
6702 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
6703 set_ref_in_item(&li
->li_tv
, copyID
);
6707 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6710 set_ref_in_item(tv
, copyID
)
6720 dd
= tv
->vval
.v_dict
;
6721 if (dd
!= NULL
&& dd
->dv_copyID
!= copyID
)
6723 /* Didn't see this dict yet. */
6724 dd
->dv_copyID
= copyID
;
6725 set_ref_in_ht(&dd
->dv_hashtab
, copyID
);
6730 ll
= tv
->vval
.v_list
;
6731 if (ll
!= NULL
&& ll
->lv_copyID
!= copyID
)
6733 /* Didn't see this list yet. */
6734 ll
->lv_copyID
= copyID
;
6735 set_ref_in_list(ll
, copyID
);
6743 * Allocate an empty header for a dictionary.
6750 d
= (dict_T
*)alloc(sizeof(dict_T
));
6753 /* Add the list to the list of dicts for garbage collection. */
6754 if (first_dict
!= NULL
)
6755 first_dict
->dv_used_prev
= d
;
6756 d
->dv_used_next
= first_dict
;
6757 d
->dv_used_prev
= NULL
;
6760 hash_init(&d
->dv_hashtab
);
6769 * Unreference a Dictionary: decrement the reference count and free it when it
6776 if (d
!= NULL
&& --d
->dv_refcount
<= 0)
6781 * Free a Dictionary, including all items it contains.
6782 * Ignores the reference count.
6785 dict_free(d
, recurse
)
6787 int recurse
; /* Free Lists and Dictionaries recursively. */
6793 /* Remove the dict from the list of dicts for garbage collection. */
6794 if (d
->dv_used_prev
== NULL
)
6795 first_dict
= d
->dv_used_next
;
6797 d
->dv_used_prev
->dv_used_next
= d
->dv_used_next
;
6798 if (d
->dv_used_next
!= NULL
)
6799 d
->dv_used_next
->dv_used_prev
= d
->dv_used_prev
;
6801 /* Lock the hashtab, we don't want it to resize while freeing items. */
6802 hash_lock(&d
->dv_hashtab
);
6803 todo
= (int)d
->dv_hashtab
.ht_used
;
6804 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
6806 if (!HASHITEM_EMPTY(hi
))
6808 /* Remove the item before deleting it, just in case there is
6809 * something recursive causing trouble. */
6811 hash_remove(&d
->dv_hashtab
, hi
);
6812 if (recurse
|| (di
->di_tv
.v_type
!= VAR_LIST
6813 && di
->di_tv
.v_type
!= VAR_DICT
))
6814 clear_tv(&di
->di_tv
);
6819 hash_clear(&d
->dv_hashtab
);
6824 * Allocate a Dictionary item.
6825 * The "key" is copied to the new item.
6826 * Note that the value of the item "di_tv" still needs to be initialized!
6827 * Returns NULL when out of memory.
6835 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
) + STRLEN(key
)));
6838 STRCPY(di
->di_key
, key
);
6845 * Make a copy of a Dictionary item.
6853 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
6854 + STRLEN(org
->di_key
)));
6857 STRCPY(di
->di_key
, org
->di_key
);
6859 copy_tv(&org
->di_tv
, &di
->di_tv
);
6865 * Remove item "item" from Dictionary "dict" and free it.
6868 dictitem_remove(dict
, item
)
6874 hi
= hash_find(&dict
->dv_hashtab
, item
->di_key
);
6875 if (HASHITEM_EMPTY(hi
))
6876 EMSG2(_(e_intern2
), "dictitem_remove()");
6878 hash_remove(&dict
->dv_hashtab
, hi
);
6879 dictitem_free(item
);
6883 * Free a dict item. Also clears the value.
6889 clear_tv(&item
->di_tv
);
6894 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6895 * The refcount of the new dict is set to 1.
6896 * See item_copy() for "copyID".
6897 * Returns NULL when out of memory.
6900 dict_copy(orig
, deep
, copyID
)
6913 copy
= dict_alloc();
6918 orig
->dv_copyID
= copyID
;
6919 orig
->dv_copydict
= copy
;
6921 todo
= (int)orig
->dv_hashtab
.ht_used
;
6922 for (hi
= orig
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
6924 if (!HASHITEM_EMPTY(hi
))
6928 di
= dictitem_alloc(hi
->hi_key
);
6933 if (item_copy(&HI2DI(hi
)->di_tv
, &di
->di_tv
, deep
,
6941 copy_tv(&HI2DI(hi
)->di_tv
, &di
->di_tv
);
6942 if (dict_add(copy
, di
) == FAIL
)
6950 ++copy
->dv_refcount
;
6962 * Add item "item" to Dictionary "d".
6963 * Returns FAIL when out of memory and when key already existed.
6970 return hash_add(&d
->dv_hashtab
, item
->di_key
);
6974 * Add a number or string entry to dictionary "d".
6975 * When "str" is NULL use number "nr", otherwise use "str".
6976 * Returns FAIL when out of memory and when key already exists.
6979 dict_add_nr_str(d
, key
, nr
, str
)
6987 item
= dictitem_alloc((char_u
*)key
);
6990 item
->di_tv
.v_lock
= 0;
6993 item
->di_tv
.v_type
= VAR_NUMBER
;
6994 item
->di_tv
.vval
.v_number
= nr
;
6998 item
->di_tv
.v_type
= VAR_STRING
;
6999 item
->di_tv
.vval
.v_string
= vim_strsave(str
);
7001 if (dict_add(d
, item
) == FAIL
)
7003 dictitem_free(item
);
7010 * Get the number of items in a Dictionary.
7018 return (long)d
->dv_hashtab
.ht_used
;
7022 * Find item "key[len]" in Dictionary "d".
7023 * If "len" is negative use strlen(key).
7024 * Returns NULL when not found.
7027 dict_find(d
, key
, len
)
7033 char_u buf
[AKEYLEN
];
7035 char_u
*tofree
= NULL
;
7040 else if (len
>= AKEYLEN
)
7042 tofree
= akey
= vim_strnsave(key
, len
);
7048 /* Avoid a malloc/free by using buf[]. */
7049 vim_strncpy(buf
, key
, len
);
7053 hi
= hash_find(&d
->dv_hashtab
, akey
);
7055 if (HASHITEM_EMPTY(hi
))
7061 * Get a string item from a dictionary.
7062 * When "save" is TRUE allocate memory for it.
7063 * Returns NULL if the entry doesn't exist or out of memory.
7066 get_dict_string(d
, key
, save
)
7074 di
= dict_find(d
, key
, -1);
7077 s
= get_tv_string(&di
->di_tv
);
7078 if (save
&& s
!= NULL
)
7084 * Get a number item from a dictionary.
7085 * Returns 0 if the entry doesn't exist or out of memory.
7088 get_dict_number(d
, key
)
7094 di
= dict_find(d
, key
, -1);
7097 return get_tv_number(&di
->di_tv
);
7101 * Return an allocated string with the string representation of a Dictionary.
7105 dict2string(tv
, copyID
)
7112 char_u numbuf
[NUMBUFLEN
];
7118 if ((d
= tv
->vval
.v_dict
) == NULL
)
7120 ga_init2(&ga
, (int)sizeof(char), 80);
7121 ga_append(&ga
, '{');
7123 todo
= (int)d
->dv_hashtab
.ht_used
;
7124 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
7126 if (!HASHITEM_EMPTY(hi
))
7133 ga_concat(&ga
, (char_u
*)", ");
7135 tofree
= string_quote(hi
->hi_key
, FALSE
);
7138 ga_concat(&ga
, tofree
);
7141 ga_concat(&ga
, (char_u
*)": ");
7142 s
= tv2string(&HI2DI(hi
)->di_tv
, &tofree
, numbuf
, copyID
);
7152 vim_free(ga
.ga_data
);
7156 ga_append(&ga
, '}');
7157 ga_append(&ga
, NUL
);
7158 return (char_u
*)ga
.ga_data
;
7162 * Allocate a variable for a Dictionary and fill it from "*arg".
7163 * Return OK or FAIL. Returns NOTDONE for {expr}.
7166 get_dict_tv(arg
, rettv
, evaluate
)
7176 char_u
*start
= skipwhite(*arg
+ 1);
7177 char_u buf
[NUMBUFLEN
];
7180 * First check if it's not a curly-braces thing: {expr}.
7181 * Must do this without evaluating, otherwise a function may be called
7182 * twice. Unfortunately this means we need to call eval1() twice for the
7184 * But {} is an empty Dictionary.
7188 if (eval1(&start
, &tv
, FALSE
) == FAIL
) /* recursive! */
7200 tvkey
.v_type
= VAR_UNKNOWN
;
7201 tv
.v_type
= VAR_UNKNOWN
;
7203 *arg
= skipwhite(*arg
+ 1);
7204 while (**arg
!= '}' && **arg
!= NUL
)
7206 if (eval1(arg
, &tvkey
, evaluate
) == FAIL
) /* recursive! */
7210 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg
);
7216 key
= get_tv_string_buf_chk(&tvkey
, buf
);
7217 if (key
== NULL
|| *key
== NUL
)
7219 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7221 EMSG(_(e_emptykey
));
7227 *arg
= skipwhite(*arg
+ 1);
7228 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
7236 item
= dict_find(d
, key
, -1);
7239 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key
);
7244 item
= dictitem_alloc(key
);
7249 item
->di_tv
.v_lock
= 0;
7250 if (dict_add(d
, item
) == FAIL
)
7251 dictitem_free(item
);
7259 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg
);
7262 *arg
= skipwhite(*arg
+ 1);
7267 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg
);
7274 *arg
= skipwhite(*arg
+ 1);
7277 rettv
->v_type
= VAR_DICT
;
7278 rettv
->vval
.v_dict
= d
;
7286 * Return a string with the string representation of a variable.
7287 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7288 * "numbuf" is used for a number.
7289 * Does not put quotes around strings, as ":echo" displays values.
7290 * When "copyID" is not NULL replace recursive lists and dicts with "...".
7294 echo_string(tv
, tofree
, numbuf
, copyID
)
7300 static int recurse
= 0;
7303 if (recurse
>= DICT_MAXNEST
)
7305 EMSG(_("E724: variable nested too deep for displaying"));
7315 r
= tv
->vval
.v_string
;
7319 if (tv
->vval
.v_list
== NULL
)
7324 else if (copyID
!= 0 && tv
->vval
.v_list
->lv_copyID
== copyID
)
7327 r
= (char_u
*)"[...]";
7331 tv
->vval
.v_list
->lv_copyID
= copyID
;
7332 *tofree
= list2string(tv
, copyID
);
7338 if (tv
->vval
.v_dict
== NULL
)
7343 else if (copyID
!= 0 && tv
->vval
.v_dict
->dv_copyID
== copyID
)
7346 r
= (char_u
*)"{...}";
7350 tv
->vval
.v_dict
->dv_copyID
= copyID
;
7351 *tofree
= dict2string(tv
, copyID
);
7359 r
= get_tv_string_buf(tv
, numbuf
);
7365 vim_snprintf((char *)numbuf
, NUMBUFLEN
, "%g", tv
->vval
.v_float
);
7371 EMSG2(_(e_intern2
), "echo_string()");
7380 * Return a string with the string representation of a variable.
7381 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7382 * "numbuf" is used for a number.
7383 * Puts quotes around strings, so that they can be parsed back by eval().
7387 tv2string(tv
, tofree
, numbuf
, copyID
)
7396 *tofree
= string_quote(tv
->vval
.v_string
, TRUE
);
7399 *tofree
= string_quote(tv
->vval
.v_string
, FALSE
);
7404 vim_snprintf((char *)numbuf
, NUMBUFLEN
- 1, "%g", tv
->vval
.v_float
);
7412 EMSG2(_(e_intern2
), "tv2string()");
7414 return echo_string(tv
, tofree
, numbuf
, copyID
);
7418 * Return string "str" in ' quotes, doubling ' characters.
7419 * If "str" is NULL an empty string is assumed.
7420 * If "function" is TRUE make it function('string').
7423 string_quote(str
, function
)
7430 len
= (function
? 13 : 3);
7433 len
+= (unsigned)STRLEN(str
);
7434 for (p
= str
; *p
!= NUL
; mb_ptr_adv(p
))
7443 STRCPY(r
, "function('");
7449 for (p
= str
; *p
!= NUL
; )
7465 * Convert the string "text" to a floating point number.
7466 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7467 * this always uses a decimal point.
7468 * Returns the length of the text that was consumed.
7471 string2float(text
, value
)
7473 float_T
*value
; /* result stored here */
7475 char *s
= (char *)text
;
7480 return (int)((char_u
*)s
- text
);
7485 * Get the value of an environment variable.
7486 * "arg" is pointing to the '$'. It is advanced to after the name.
7487 * If the environment variable was not set, silently assume it is empty.
7491 get_env_tv(arg
, rettv
, evaluate
)
7496 char_u
*string
= NULL
;
7500 int mustfree
= FALSE
;
7504 len
= get_env_len(arg
);
7511 /* first try vim_getenv(), fast for normal environment vars */
7512 string
= vim_getenv(name
, &mustfree
);
7513 if (string
!= NULL
&& *string
!= NUL
)
7516 string
= vim_strsave(string
);
7523 /* next try expanding things like $VIM and ${HOME} */
7524 string
= expand_env_save(name
- 1);
7525 if (string
!= NULL
&& *string
== '$')
7533 rettv
->v_type
= VAR_STRING
;
7534 rettv
->vval
.v_string
= string
;
7541 * Array with names and number of arguments of all internal functions
7542 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7546 char *f_name
; /* function name */
7547 char f_min_argc
; /* minimal number of arguments */
7548 char f_max_argc
; /* maximal number of arguments */
7549 void (*f_func
) __ARGS((typval_T
*args
, typval_T
*rvar
));
7550 /* implementation of function */
7554 {"abs", 1, 1, f_abs
},
7555 {"acos", 1, 1, f_acos
}, /* WJMc */
7557 {"add", 2, 2, f_add
},
7558 {"append", 2, 2, f_append
},
7559 {"argc", 0, 0, f_argc
},
7560 {"argidx", 0, 0, f_argidx
},
7561 {"argv", 0, 1, f_argv
},
7563 {"asin", 1, 1, f_asin
}, /* WJMc */
7564 {"atan", 1, 1, f_atan
},
7565 {"atan2", 2, 2, f_atan2
}, /* WJMc */
7567 {"browse", 4, 4, f_browse
},
7568 {"browsedir", 2, 2, f_browsedir
},
7569 {"bufexists", 1, 1, f_bufexists
},
7570 {"buffer_exists", 1, 1, f_bufexists
}, /* obsolete */
7571 {"buffer_name", 1, 1, f_bufname
}, /* obsolete */
7572 {"buffer_number", 1, 1, f_bufnr
}, /* obsolete */
7573 {"buflisted", 1, 1, f_buflisted
},
7574 {"bufloaded", 1, 1, f_bufloaded
},
7575 {"bufname", 1, 1, f_bufname
},
7576 {"bufnr", 1, 2, f_bufnr
},
7577 {"bufwinnr", 1, 1, f_bufwinnr
},
7578 {"byte2line", 1, 1, f_byte2line
},
7579 {"byteidx", 2, 2, f_byteidx
},
7580 {"call", 2, 3, f_call
},
7582 {"ceil", 1, 1, f_ceil
},
7584 {"changenr", 0, 0, f_changenr
},
7585 {"char2nr", 1, 1, f_char2nr
},
7586 {"cindent", 1, 1, f_cindent
},
7587 {"clearmatches", 0, 0, f_clearmatches
},
7588 {"col", 1, 1, f_col
},
7589 #if defined(FEAT_INS_EXPAND)
7590 {"complete", 2, 2, f_complete
},
7591 {"complete_add", 1, 1, f_complete_add
},
7592 {"complete_check", 0, 0, f_complete_check
},
7594 {"confirm", 1, 4, f_confirm
},
7595 {"copy", 1, 1, f_copy
},
7597 {"cos", 1, 1, f_cos
},
7598 {"cosh", 1, 1, f_cosh
}, /* WJMc */
7600 {"count", 2, 4, f_count
},
7601 {"cscope_connection",0,3, f_cscope_connection
},
7602 {"cursor", 1, 3, f_cursor
},
7603 {"deepcopy", 1, 2, f_deepcopy
},
7604 {"delete", 1, 1, f_delete
},
7605 {"did_filetype", 0, 0, f_did_filetype
},
7606 {"diff_filler", 1, 1, f_diff_filler
},
7607 {"diff_hlID", 2, 2, f_diff_hlID
},
7608 {"empty", 1, 1, f_empty
},
7609 {"escape", 2, 2, f_escape
},
7610 {"eval", 1, 1, f_eval
},
7611 {"eventhandler", 0, 0, f_eventhandler
},
7612 {"executable", 1, 1, f_executable
},
7613 {"exists", 1, 1, f_exists
},
7615 {"exp", 1, 1, f_exp
}, /* WJMc */
7617 {"expand", 1, 2, f_expand
},
7618 {"extend", 2, 3, f_extend
},
7619 {"feedkeys", 1, 2, f_feedkeys
},
7620 {"file_readable", 1, 1, f_filereadable
}, /* obsolete */
7621 {"filereadable", 1, 1, f_filereadable
},
7622 {"filewritable", 1, 1, f_filewritable
},
7623 {"filter", 2, 2, f_filter
},
7624 {"finddir", 1, 3, f_finddir
},
7625 {"findfile", 1, 3, f_findfile
},
7627 {"float2nr", 1, 1, f_float2nr
},
7628 {"floor", 1, 1, f_floor
},
7629 {"fmod", 2, 2, f_fmod
}, /* WJMc */
7631 {"fnameescape", 1, 1, f_fnameescape
},
7632 {"fnamemodify", 2, 2, f_fnamemodify
},
7633 {"foldclosed", 1, 1, f_foldclosed
},
7634 {"foldclosedend", 1, 1, f_foldclosedend
},
7635 {"foldlevel", 1, 1, f_foldlevel
},
7636 {"foldtext", 0, 0, f_foldtext
},
7637 {"foldtextresult", 1, 1, f_foldtextresult
},
7638 {"foreground", 0, 0, f_foreground
},
7639 {"function", 1, 1, f_function
},
7640 {"garbagecollect", 0, 1, f_garbagecollect
},
7641 {"get", 2, 3, f_get
},
7642 {"getbufline", 2, 3, f_getbufline
},
7643 {"getbufvar", 2, 2, f_getbufvar
},
7644 {"getchar", 0, 1, f_getchar
},
7645 {"getcharmod", 0, 0, f_getcharmod
},
7646 {"getcmdline", 0, 0, f_getcmdline
},
7647 {"getcmdpos", 0, 0, f_getcmdpos
},
7648 {"getcmdtype", 0, 0, f_getcmdtype
},
7649 {"getcwd", 0, 0, f_getcwd
},
7650 {"getfontname", 0, 1, f_getfontname
},
7651 {"getfperm", 1, 1, f_getfperm
},
7652 {"getfsize", 1, 1, f_getfsize
},
7653 {"getftime", 1, 1, f_getftime
},
7654 {"getftype", 1, 1, f_getftype
},
7655 {"getline", 1, 2, f_getline
},
7656 {"getloclist", 1, 1, f_getqflist
},
7657 {"getmatches", 0, 0, f_getmatches
},
7658 {"getpid", 0, 0, f_getpid
},
7659 {"getpos", 1, 1, f_getpos
},
7660 {"getqflist", 0, 0, f_getqflist
},
7661 {"getreg", 0, 2, f_getreg
},
7662 {"getregtype", 0, 1, f_getregtype
},
7663 {"gettabwinvar", 3, 3, f_gettabwinvar
},
7664 {"getwinposx", 0, 0, f_getwinposx
},
7665 {"getwinposy", 0, 0, f_getwinposy
},
7666 {"getwinvar", 2, 2, f_getwinvar
},
7667 {"glob", 1, 2, f_glob
},
7668 {"globpath", 2, 3, f_globpath
},
7669 {"has", 1, 1, f_has
},
7670 {"has_key", 2, 2, f_has_key
},
7671 {"haslocaldir", 0, 0, f_haslocaldir
},
7672 {"hasmapto", 1, 3, f_hasmapto
},
7673 {"highlightID", 1, 1, f_hlID
}, /* obsolete */
7674 {"highlight_exists",1, 1, f_hlexists
}, /* obsolete */
7675 {"histadd", 2, 2, f_histadd
},
7676 {"histdel", 1, 2, f_histdel
},
7677 {"histget", 1, 2, f_histget
},
7678 {"histnr", 1, 1, f_histnr
},
7679 {"hlID", 1, 1, f_hlID
},
7680 {"hlexists", 1, 1, f_hlexists
},
7681 {"hostname", 0, 0, f_hostname
},
7682 {"iconv", 3, 3, f_iconv
},
7683 {"indent", 1, 1, f_indent
},
7684 {"index", 2, 4, f_index
},
7685 {"input", 1, 3, f_input
},
7686 {"inputdialog", 1, 3, f_inputdialog
},
7687 {"inputlist", 1, 1, f_inputlist
},
7688 {"inputrestore", 0, 0, f_inputrestore
},
7689 {"inputsave", 0, 0, f_inputsave
},
7690 {"inputsecret", 1, 2, f_inputsecret
},
7691 {"insert", 2, 3, f_insert
},
7692 {"isdirectory", 1, 1, f_isdirectory
},
7693 {"islocked", 1, 1, f_islocked
},
7694 {"items", 1, 1, f_items
},
7695 {"join", 1, 2, f_join
},
7696 {"keys", 1, 1, f_keys
},
7697 {"last_buffer_nr", 0, 0, f_last_buffer_nr
},/* obsolete */
7698 {"len", 1, 1, f_len
},
7699 {"libcall", 3, 3, f_libcall
},
7700 {"libcallnr", 3, 3, f_libcallnr
},
7701 {"line", 1, 1, f_line
},
7702 {"line2byte", 1, 1, f_line2byte
},
7703 {"lispindent", 1, 1, f_lispindent
},
7704 {"localtime", 0, 0, f_localtime
},
7706 {"log", 1, 1, f_log
}, /* WJMc */
7707 {"log10", 1, 1, f_log10
},
7709 {"map", 2, 2, f_map
},
7710 {"maparg", 1, 3, f_maparg
},
7711 {"mapcheck", 1, 3, f_mapcheck
},
7712 {"match", 2, 4, f_match
},
7713 {"matchadd", 2, 4, f_matchadd
},
7714 {"matcharg", 1, 1, f_matcharg
},
7715 {"matchdelete", 1, 1, f_matchdelete
},
7716 {"matchend", 2, 4, f_matchend
},
7717 {"matchlist", 2, 4, f_matchlist
},
7718 {"matchstr", 2, 4, f_matchstr
},
7719 {"max", 1, 1, f_max
},
7720 {"min", 1, 1, f_min
},
7722 {"mkdir", 1, 3, f_mkdir
},
7724 {"mode", 0, 1, f_mode
},
7725 #ifdef FEAT_MZSCHEME
7726 {"mzeval", 1, 1, f_mzeval
},
7728 {"nextnonblank", 1, 1, f_nextnonblank
},
7729 {"nr2char", 1, 1, f_nr2char
},
7730 {"pathshorten", 1, 1, f_pathshorten
},
7732 {"pow", 2, 2, f_pow
},
7734 {"prevnonblank", 1, 1, f_prevnonblank
},
7735 {"printf", 2, 19, f_printf
},
7736 {"pumvisible", 0, 0, f_pumvisible
},
7737 {"range", 1, 3, f_range
},
7738 {"readfile", 1, 3, f_readfile
},
7739 {"reltime", 0, 2, f_reltime
},
7740 {"reltimestr", 1, 1, f_reltimestr
},
7741 {"remote_expr", 2, 3, f_remote_expr
},
7742 {"remote_foreground", 1, 1, f_remote_foreground
},
7743 {"remote_peek", 1, 2, f_remote_peek
},
7744 {"remote_read", 1, 1, f_remote_read
},
7745 {"remote_send", 2, 3, f_remote_send
},
7746 {"remove", 2, 3, f_remove
},
7747 {"rename", 2, 2, f_rename
},
7748 {"repeat", 2, 2, f_repeat
},
7749 {"resolve", 1, 1, f_resolve
},
7750 {"reverse", 1, 1, f_reverse
},
7752 {"round", 1, 1, f_round
},
7754 {"search", 1, 4, f_search
},
7755 {"searchdecl", 1, 3, f_searchdecl
},
7756 {"searchpair", 3, 7, f_searchpair
},
7757 {"searchpairpos", 3, 7, f_searchpairpos
},
7758 {"searchpos", 1, 4, f_searchpos
},
7759 {"server2client", 2, 2, f_server2client
},
7760 {"serverlist", 0, 0, f_serverlist
},
7761 {"setbufvar", 3, 3, f_setbufvar
},
7762 {"setcmdpos", 1, 1, f_setcmdpos
},
7763 {"setline", 2, 2, f_setline
},
7764 {"setloclist", 2, 3, f_setloclist
},
7765 {"setmatches", 1, 1, f_setmatches
},
7766 {"setpos", 2, 2, f_setpos
},
7767 {"setqflist", 1, 2, f_setqflist
},
7768 {"setreg", 2, 3, f_setreg
},
7769 {"settabwinvar", 4, 4, f_settabwinvar
},
7770 {"setwinvar", 3, 3, f_setwinvar
},
7771 {"shellescape", 1, 2, f_shellescape
},
7772 {"simplify", 1, 1, f_simplify
},
7774 {"sin", 1, 1, f_sin
},
7775 {"sinh", 1, 1, f_sinh
}, /* WJMc */
7777 {"sort", 1, 2, f_sort
},
7778 {"soundfold", 1, 1, f_soundfold
},
7779 {"spellbadword", 0, 1, f_spellbadword
},
7780 {"spellsuggest", 1, 3, f_spellsuggest
},
7781 {"split", 1, 3, f_split
},
7783 {"sqrt", 1, 1, f_sqrt
},
7784 {"str2float", 1, 1, f_str2float
},
7786 {"str2nr", 1, 2, f_str2nr
},
7787 #ifdef HAVE_STRFTIME
7788 {"strftime", 1, 2, f_strftime
},
7790 {"stridx", 2, 3, f_stridx
},
7791 {"string", 1, 1, f_string
},
7792 {"strlen", 1, 1, f_strlen
},
7793 {"strpart", 2, 3, f_strpart
},
7794 {"strridx", 2, 3, f_strridx
},
7795 {"strtrans", 1, 1, f_strtrans
},
7796 {"submatch", 1, 1, f_submatch
},
7797 {"substitute", 4, 4, f_substitute
},
7798 {"synID", 3, 3, f_synID
},
7799 {"synIDattr", 2, 3, f_synIDattr
},
7800 {"synIDtrans", 1, 1, f_synIDtrans
},
7801 {"synstack", 2, 2, f_synstack
},
7802 {"system", 1, 2, f_system
},
7803 {"tabpagebuflist", 0, 1, f_tabpagebuflist
},
7804 {"tabpagenr", 0, 1, f_tabpagenr
},
7805 {"tabpagewinnr", 1, 2, f_tabpagewinnr
},
7806 {"tagfiles", 0, 0, f_tagfiles
},
7807 {"taglist", 1, 1, f_taglist
},
7808 {"tan", 1, 1, f_tan
}, /* WJMc */
7809 {"tanh", 1, 1, f_tanh
}, /* WJMc */
7810 {"tempname", 0, 0, f_tempname
},
7811 {"test", 1, 1, f_test
},
7812 {"tolower", 1, 1, f_tolower
},
7813 {"toupper", 1, 1, f_toupper
},
7816 {"trunc", 1, 1, f_trunc
},
7818 {"type", 1, 1, f_type
},
7819 {"values", 1, 1, f_values
},
7820 {"virtcol", 1, 1, f_virtcol
},
7821 {"visualmode", 0, 1, f_visualmode
},
7822 {"winbufnr", 1, 1, f_winbufnr
},
7823 {"wincol", 0, 0, f_wincol
},
7824 {"winheight", 1, 1, f_winheight
},
7825 {"winline", 0, 0, f_winline
},
7826 {"winnr", 0, 1, f_winnr
},
7827 {"winrestcmd", 0, 0, f_winrestcmd
},
7828 {"winrestview", 1, 1, f_winrestview
},
7829 {"winsaveview", 0, 0, f_winsaveview
},
7830 {"winwidth", 1, 1, f_winwidth
},
7831 {"writefile", 2, 3, f_writefile
},
7834 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7837 * Function given to ExpandGeneric() to obtain the list of internal
7838 * or user defined function names.
7841 get_function_name(xp
, idx
)
7845 static int intidx
= -1;
7852 name
= get_user_func_name(xp
, idx
);
7856 if (++intidx
< (int)(sizeof(functions
) / sizeof(struct fst
)))
7858 STRCPY(IObuff
, functions
[intidx
].f_name
);
7859 STRCAT(IObuff
, "(");
7860 if (functions
[intidx
].f_max_argc
== 0)
7861 STRCAT(IObuff
, ")");
7869 * Function given to ExpandGeneric() to obtain the list of internal or
7870 * user defined variable or function names.
7873 get_expr_name(xp
, idx
)
7877 static int intidx
= -1;
7884 name
= get_function_name(xp
, idx
);
7888 return get_user_var_name(xp
, ++intidx
);
7891 #endif /* FEAT_CMDL_COMPL */
7894 * Find internal function in table above.
7895 * Return index, or -1 if not found
7898 find_internal_func(name
)
7899 char_u
*name
; /* name of the function */
7902 int last
= (int)(sizeof(functions
) / sizeof(struct fst
)) - 1;
7907 * Find the function name in the table. Binary search.
7909 while (first
<= last
)
7911 x
= first
+ ((unsigned)(last
- first
) >> 1);
7912 cmp
= STRCMP(name
, functions
[x
].f_name
);
7924 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7925 * name it contains, otherwise return "name".
7928 deref_func_name(name
, lenp
)
7937 v
= find_var(name
, NULL
);
7939 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
7941 if (v
->di_tv
.vval
.v_string
== NULL
)
7944 return (char_u
*)""; /* just in case */
7946 *lenp
= (int)STRLEN(v
->di_tv
.vval
.v_string
);
7947 return v
->di_tv
.vval
.v_string
;
7954 * Allocate a variable for the result of a function.
7955 * Return OK or FAIL.
7958 get_func_tv(name
, len
, rettv
, arg
, firstline
, lastline
, doesrange
,
7960 char_u
*name
; /* name of the function */
7961 int len
; /* length of "name" */
7963 char_u
**arg
; /* argument, pointing to the '(' */
7964 linenr_T firstline
; /* first line of range */
7965 linenr_T lastline
; /* last line of range */
7966 int *doesrange
; /* return: function handled range */
7968 dict_T
*selfdict
; /* Dictionary for "self" */
7972 typval_T argvars
[MAX_FUNC_ARGS
+ 1]; /* vars for arguments */
7973 int argcount
= 0; /* number of arguments found */
7976 * Get the arguments.
7979 while (argcount
< MAX_FUNC_ARGS
)
7981 argp
= skipwhite(argp
+ 1); /* skip the '(' or ',' */
7982 if (*argp
== ')' || *argp
== ',' || *argp
== NUL
)
7984 if (eval1(&argp
, &argvars
[argcount
], evaluate
) == FAIL
)
7999 ret
= call_func(name
, len
, rettv
, argcount
, argvars
,
8000 firstline
, lastline
, doesrange
, evaluate
, selfdict
);
8001 else if (!aborting())
8003 if (argcount
== MAX_FUNC_ARGS
)
8004 emsg_funcname(N_("E740: Too many arguments for function %s"), name
);
8006 emsg_funcname(N_("E116: Invalid arguments for function %s"), name
);
8009 while (--argcount
>= 0)
8010 clear_tv(&argvars
[argcount
]);
8012 *arg
= skipwhite(argp
);
8018 * Call a function with its resolved parameters
8019 * Return OK when the function can't be called, FAIL otherwise.
8020 * Also returns OK when an error was encountered while executing the function.
8023 call_func(name
, len
, rettv
, argcount
, argvars
, firstline
, lastline
,
8024 doesrange
, evaluate
, selfdict
)
8025 char_u
*name
; /* name of the function */
8026 int len
; /* length of "name" */
8027 typval_T
*rettv
; /* return value goes here */
8028 int argcount
; /* number of "argvars" */
8029 typval_T
*argvars
; /* vars for arguments, must have "argcount"
8030 PLUS ONE elements! */
8031 linenr_T firstline
; /* first line of range */
8032 linenr_T lastline
; /* last line of range */
8033 int *doesrange
; /* return: function handled range */
8035 dict_T
*selfdict
; /* Dictionary for "self" */
8038 #define ERROR_UNKNOWN 0
8039 #define ERROR_TOOMANY 1
8040 #define ERROR_TOOFEW 2
8041 #define ERROR_SCRIPT 3
8042 #define ERROR_DICT 4
8043 #define ERROR_NONE 5
8044 #define ERROR_OTHER 6
8045 int error
= ERROR_NONE
;
8050 #define FLEN_FIXED 40
8051 char_u fname_buf
[FLEN_FIXED
+ 1];
8055 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8056 * Change <SNR>123_name() to K_SNR 123_name().
8057 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8061 llen
= eval_fname_script(name
);
8064 fname_buf
[0] = K_SPECIAL
;
8065 fname_buf
[1] = KS_EXTRA
;
8066 fname_buf
[2] = (int)KE_SNR
;
8068 if (eval_fname_sid(name
)) /* "<SID>" or "s:" */
8070 if (current_SID
<= 0)
8071 error
= ERROR_SCRIPT
;
8074 sprintf((char *)fname_buf
+ 3, "%ld_", (long)current_SID
);
8075 i
= (int)STRLEN(fname_buf
);
8078 if (i
+ STRLEN(name
+ llen
) < FLEN_FIXED
)
8080 STRCPY(fname_buf
+ i
, name
+ llen
);
8085 fname
= alloc((unsigned)(i
+ STRLEN(name
+ llen
) + 1));
8087 error
= ERROR_OTHER
;
8090 mch_memmove(fname
, fname_buf
, (size_t)i
);
8091 STRCPY(fname
+ i
, name
+ llen
);
8101 /* execute the function if no errors detected and executing */
8102 if (evaluate
&& error
== ERROR_NONE
)
8104 rettv
->v_type
= VAR_NUMBER
; /* default rettv is number zero */
8105 rettv
->vval
.v_number
= 0;
8106 error
= ERROR_UNKNOWN
;
8108 if (!builtin_function(fname
))
8111 * User defined function.
8113 fp
= find_func(fname
);
8116 /* Trigger FuncUndefined event, may load the function. */
8118 && apply_autocmds(EVENT_FUNCUNDEFINED
,
8119 fname
, fname
, TRUE
, NULL
)
8122 /* executed an autocommand, search for the function again */
8123 fp
= find_func(fname
);
8126 /* Try loading a package. */
8127 if (fp
== NULL
&& script_autoload(fname
, TRUE
) && !aborting())
8129 /* loaded a package, search for the function again */
8130 fp
= find_func(fname
);
8135 if (fp
->uf_flags
& FC_RANGE
)
8137 if (argcount
< fp
->uf_args
.ga_len
)
8138 error
= ERROR_TOOFEW
;
8139 else if (!fp
->uf_varargs
&& argcount
> fp
->uf_args
.ga_len
)
8140 error
= ERROR_TOOMANY
;
8141 else if ((fp
->uf_flags
& FC_DICT
) && selfdict
== NULL
)
8146 * Call the user function.
8147 * Save and restore search patterns, script variables and
8150 save_search_patterns();
8153 call_user_func(fp
, argcount
, argvars
, rettv
,
8154 firstline
, lastline
,
8155 (fp
->uf_flags
& FC_DICT
) ? selfdict
: NULL
);
8156 if (--fp
->uf_calls
<= 0 && isdigit(*fp
->uf_name
)
8157 && fp
->uf_refcount
<= 0)
8158 /* Function was unreferenced while being used, free it
8162 restore_search_patterns();
8170 * Find the function name in the table, call its implementation.
8172 i
= find_internal_func(fname
);
8175 if (argcount
< functions
[i
].f_min_argc
)
8176 error
= ERROR_TOOFEW
;
8177 else if (argcount
> functions
[i
].f_max_argc
)
8178 error
= ERROR_TOOMANY
;
8181 argvars
[argcount
].v_type
= VAR_UNKNOWN
;
8182 functions
[i
].f_func(argvars
, rettv
);
8188 * The function call (or "FuncUndefined" autocommand sequence) might
8189 * have been aborted by an error, an interrupt, or an explicitly thrown
8190 * exception that has not been caught so far. This situation can be
8191 * tested for by calling aborting(). For an error in an internal
8192 * function or for the "E132" error in call_user_func(), however, the
8193 * throw point at which the "force_abort" flag (temporarily reset by
8194 * emsg()) is normally updated has not been reached yet. We need to
8195 * update that flag first to make aborting() reliable.
8197 update_force_abort();
8199 if (error
== ERROR_NONE
)
8203 * Report an error unless the argument evaluation or function call has been
8204 * cancelled due to an aborting error, an interrupt, or an exception.
8211 emsg_funcname(N_("E117: Unknown function: %s"), name
);
8214 emsg_funcname(e_toomanyarg
, name
);
8217 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
8221 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
8225 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
8232 if (fname
!= name
&& fname
!= fname_buf
)
8239 * Give an error message with a function name. Handle <SNR> things.
8240 * "ermsg" is to be passed without translation, use N_() instead of _().
8243 emsg_funcname(ermsg
, name
)
8249 if (*name
== K_SPECIAL
)
8250 p
= concat_str((char_u
*)"<SNR>", name
+ 3);
8259 * Return TRUE for a non-zero Number and a non-empty String.
8262 non_zero_arg(argvars
)
8265 return ((argvars
[0].v_type
== VAR_NUMBER
8266 && argvars
[0].vval
.v_number
!= 0)
8267 || (argvars
[0].v_type
== VAR_STRING
8268 && argvars
[0].vval
.v_string
!= NULL
8269 && *argvars
[0].vval
.v_string
!= NUL
));
8272 /*********************************************
8273 * Implementation of the built-in functions
8278 * "abs(expr)" function
8281 f_abs(argvars
, rettv
)
8285 if (argvars
[0].v_type
== VAR_FLOAT
)
8287 rettv
->v_type
= VAR_FLOAT
;
8288 rettv
->vval
.v_float
= fabs(argvars
[0].vval
.v_float
);
8295 n
= get_tv_number_chk(&argvars
[0], &error
);
8297 rettv
->vval
.v_number
= -1;
8299 rettv
->vval
.v_number
= n
;
8301 rettv
->vval
.v_number
= -n
;
8307 * "add(list, item)" function
8310 f_add(argvars
, rettv
)
8316 rettv
->vval
.v_number
= 1; /* Default: Failed */
8317 if (argvars
[0].v_type
== VAR_LIST
)
8319 if ((l
= argvars
[0].vval
.v_list
) != NULL
8320 && !tv_check_lock(l
->lv_lock
, (char_u
*)"add()")
8321 && list_append_tv(l
, &argvars
[1]) == OK
)
8322 copy_tv(&argvars
[0], rettv
);
8329 * "append(lnum, string/list)" function
8332 f_append(argvars
, rettv
)
8339 listitem_T
*li
= NULL
;
8343 lnum
= get_tv_lnum(argvars
);
8345 && lnum
<= curbuf
->b_ml
.ml_line_count
8346 && u_save(lnum
, lnum
+ 1) == OK
)
8348 if (argvars
[1].v_type
== VAR_LIST
)
8350 l
= argvars
[1].vval
.v_list
;
8358 tv
= &argvars
[1]; /* append a string */
8359 else if (li
== NULL
)
8360 break; /* end of list */
8362 tv
= &li
->li_tv
; /* append item from list */
8363 line
= get_tv_string_chk(tv
);
8364 if (line
== NULL
) /* type error */
8366 rettv
->vval
.v_number
= 1; /* Failed */
8369 ml_append(lnum
+ added
, line
, (colnr_T
)0, FALSE
);
8376 appended_lines_mark(lnum
, added
);
8377 if (curwin
->w_cursor
.lnum
> lnum
)
8378 curwin
->w_cursor
.lnum
+= added
;
8381 rettv
->vval
.v_number
= 1; /* Failed */
8388 f_argc(argvars
, rettv
)
8389 typval_T
*argvars UNUSED
;
8392 rettv
->vval
.v_number
= ARGCOUNT
;
8396 * "argidx()" function
8399 f_argidx(argvars
, rettv
)
8400 typval_T
*argvars UNUSED
;
8403 rettv
->vval
.v_number
= curwin
->w_arg_idx
;
8407 * "argv(nr)" function
8410 f_argv(argvars
, rettv
)
8416 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
8418 idx
= get_tv_number_chk(&argvars
[0], NULL
);
8419 if (idx
>= 0 && idx
< ARGCOUNT
)
8420 rettv
->vval
.v_string
= vim_strsave(alist_name(&ARGLIST
[idx
]));
8422 rettv
->vval
.v_string
= NULL
;
8423 rettv
->v_type
= VAR_STRING
;
8425 else if (rettv_list_alloc(rettv
) == OK
)
8426 for (idx
= 0; idx
< ARGCOUNT
; ++idx
)
8427 list_append_string(rettv
->vval
.v_list
,
8428 alist_name(&ARGLIST
[idx
]), -1);
8432 static int get_float_arg
__ARGS((typval_T
*argvars
, float_T
*f
));
8435 * Get the float value of "argvars[0]" into "f".
8436 * Returns FAIL when the argument is not a Number or Float.
8439 get_float_arg(argvars
, f
)
8443 if (argvars
[0].v_type
== VAR_FLOAT
)
8445 *f
= argvars
[0].vval
.v_float
;
8448 if (argvars
[0].v_type
== VAR_NUMBER
)
8450 *f
= (float_T
)argvars
[0].vval
.v_number
;
8453 EMSG(_("E808: Number or Float required"));
8457 /* The 10 added FP functions are defined immediately before atan() - WJMc */
8463 f_acos(argvars
, rettv
)
8469 rettv
->v_type
= VAR_FLOAT
;
8470 if (get_float_arg(argvars
, &f
) == OK
)
8471 rettv
->vval
.v_float
= acos(f
);
8473 rettv
->vval
.v_float
= 0.0;
8480 f_asin(argvars
, rettv
)
8486 rettv
->v_type
= VAR_FLOAT
;
8487 if (get_float_arg(argvars
, &f
) == OK
)
8488 rettv
->vval
.v_float
= asin(f
);
8490 rettv
->vval
.v_float
= 0.0;
8494 * "atan2()" function
8497 f_atan2(argvars
, rettv
)
8503 rettv
->v_type
= VAR_FLOAT
;
8504 if (get_float_arg(argvars
, &fx
) == OK
8505 && get_float_arg(&argvars
[1], &fy
) == OK
)
8506 rettv
->vval
.v_float
= atan2(fx
, fy
);
8508 rettv
->vval
.v_float
= 0.0;
8515 f_cosh(argvars
, rettv
)
8521 rettv
->v_type
= VAR_FLOAT
;
8522 if (get_float_arg(argvars
, &f
) == OK
)
8523 rettv
->vval
.v_float
= cosh(f
);
8525 rettv
->vval
.v_float
= 0.0;
8532 f_exp(argvars
, rettv
)
8538 rettv
->v_type
= VAR_FLOAT
;
8539 if (get_float_arg(argvars
, &f
) == OK
)
8540 rettv
->vval
.v_float
= exp(f
);
8542 rettv
->vval
.v_float
= 0.0;
8549 f_fmod(argvars
, rettv
)
8555 rettv
->v_type
= VAR_FLOAT
;
8556 if (get_float_arg(argvars
, &fx
) == OK
8557 && get_float_arg(&argvars
[1], &fy
) == OK
)
8558 rettv
->vval
.v_float
= fmod(fx
, fy
);
8560 rettv
->vval
.v_float
= 0.0;
8567 f_log(argvars
, rettv
)
8573 rettv
->v_type
= VAR_FLOAT
;
8574 if (get_float_arg(argvars
, &f
) == OK
)
8575 rettv
->vval
.v_float
= log(f
);
8577 rettv
->vval
.v_float
= 0.0;
8584 f_sinh(argvars
, rettv
)
8590 rettv
->v_type
= VAR_FLOAT
;
8591 if (get_float_arg(argvars
, &f
) == OK
)
8592 rettv
->vval
.v_float
= sinh(f
);
8594 rettv
->vval
.v_float
= 0.0;
8601 f_tan(argvars
, rettv
)
8607 rettv
->v_type
= VAR_FLOAT
;
8608 if (get_float_arg(argvars
, &f
) == OK
)
8609 rettv
->vval
.v_float
= tan(f
);
8611 rettv
->vval
.v_float
= 0.0;
8618 f_tanh(argvars
, rettv
)
8624 rettv
->v_type
= VAR_FLOAT
;
8625 if (get_float_arg(argvars
, &f
) == OK
)
8626 rettv
->vval
.v_float
= tanh(f
);
8628 rettv
->vval
.v_float
= 0.0;
8631 /* End of the 10 added FP functions - WJMc */
8637 f_atan(argvars
, rettv
)
8643 rettv
->v_type
= VAR_FLOAT
;
8644 if (get_float_arg(argvars
, &f
) == OK
)
8645 rettv
->vval
.v_float
= atan(f
);
8647 rettv
->vval
.v_float
= 0.0;
8652 * "browse(save, title, initdir, default)" function
8655 f_browse(argvars
, rettv
)
8656 typval_T
*argvars UNUSED
;
8664 char_u buf
[NUMBUFLEN
];
8665 char_u buf2
[NUMBUFLEN
];
8668 save
= get_tv_number_chk(&argvars
[0], &error
);
8669 title
= get_tv_string_chk(&argvars
[1]);
8670 initdir
= get_tv_string_buf_chk(&argvars
[2], buf
);
8671 defname
= get_tv_string_buf_chk(&argvars
[3], buf2
);
8673 if (error
|| title
== NULL
|| initdir
== NULL
|| defname
== NULL
)
8674 rettv
->vval
.v_string
= NULL
;
8676 rettv
->vval
.v_string
=
8677 do_browse(save
? BROWSE_SAVE
: 0,
8678 title
, defname
, NULL
, initdir
, NULL
, curbuf
);
8680 rettv
->vval
.v_string
= NULL
;
8682 rettv
->v_type
= VAR_STRING
;
8686 * "browsedir(title, initdir)" function
8689 f_browsedir(argvars
, rettv
)
8690 typval_T
*argvars UNUSED
;
8696 char_u buf
[NUMBUFLEN
];
8698 title
= get_tv_string_chk(&argvars
[0]);
8699 initdir
= get_tv_string_buf_chk(&argvars
[1], buf
);
8701 if (title
== NULL
|| initdir
== NULL
)
8702 rettv
->vval
.v_string
= NULL
;
8704 rettv
->vval
.v_string
= do_browse(BROWSE_DIR
,
8705 title
, NULL
, NULL
, initdir
, NULL
, curbuf
);
8707 rettv
->vval
.v_string
= NULL
;
8709 rettv
->v_type
= VAR_STRING
;
8712 static buf_T
*find_buffer
__ARGS((typval_T
*avar
));
8715 * Find a buffer by number or exact name.
8723 if (avar
->v_type
== VAR_NUMBER
)
8724 buf
= buflist_findnr((int)avar
->vval
.v_number
);
8725 else if (avar
->v_type
== VAR_STRING
&& avar
->vval
.v_string
!= NULL
)
8727 buf
= buflist_findname_exp(avar
->vval
.v_string
);
8730 /* No full path name match, try a match with a URL or a "nofile"
8731 * buffer, these don't use the full path. */
8732 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
8733 if (buf
->b_fname
!= NULL
8734 && (path_with_url(buf
->b_fname
)
8735 #ifdef FEAT_QUICKFIX
8739 && STRCMP(buf
->b_fname
, avar
->vval
.v_string
) == 0)
8747 * "bufexists(expr)" function
8750 f_bufexists(argvars
, rettv
)
8754 rettv
->vval
.v_number
= (find_buffer(&argvars
[0]) != NULL
);
8758 * "buflisted(expr)" function
8761 f_buflisted(argvars
, rettv
)
8767 buf
= find_buffer(&argvars
[0]);
8768 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_p_bl
);
8772 * "bufloaded(expr)" function
8775 f_bufloaded(argvars
, rettv
)
8781 buf
= find_buffer(&argvars
[0]);
8782 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_ml
.ml_mfp
!= NULL
);
8785 static buf_T
*get_buf_tv
__ARGS((typval_T
*tv
));
8788 * Get buffer by number or pattern.
8794 char_u
*name
= tv
->vval
.v_string
;
8799 if (tv
->v_type
== VAR_NUMBER
)
8800 return buflist_findnr((int)tv
->vval
.v_number
);
8801 if (tv
->v_type
!= VAR_STRING
)
8803 if (name
== NULL
|| *name
== NUL
)
8805 if (name
[0] == '$' && name
[1] == NUL
)
8808 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8809 save_magic
= p_magic
;
8812 p_cpo
= (char_u
*)"";
8814 buf
= buflist_findnr(buflist_findpat(name
, name
+ STRLEN(name
),
8817 p_magic
= save_magic
;
8820 /* If not found, try expanding the name, like done for bufexists(). */
8822 buf
= find_buffer(tv
);
8828 * "bufname(expr)" function
8831 f_bufname(argvars
, rettv
)
8837 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8839 buf
= get_buf_tv(&argvars
[0]);
8840 rettv
->v_type
= VAR_STRING
;
8841 if (buf
!= NULL
&& buf
->b_fname
!= NULL
)
8842 rettv
->vval
.v_string
= vim_strsave(buf
->b_fname
);
8844 rettv
->vval
.v_string
= NULL
;
8849 * "bufnr(expr)" function
8852 f_bufnr(argvars
, rettv
)
8860 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8862 buf
= get_buf_tv(&argvars
[0]);
8865 /* If the buffer isn't found and the second argument is not zero create a
8868 && argvars
[1].v_type
!= VAR_UNKNOWN
8869 && get_tv_number_chk(&argvars
[1], &error
) != 0
8871 && (name
= get_tv_string_chk(&argvars
[0])) != NULL
8873 buf
= buflist_new(name
, NULL
, (linenr_T
)1, 0);
8876 rettv
->vval
.v_number
= buf
->b_fnum
;
8878 rettv
->vval
.v_number
= -1;
8882 * "bufwinnr(nr)" function
8885 f_bufwinnr(argvars
, rettv
)
8895 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8897 buf
= get_buf_tv(&argvars
[0]);
8899 for (wp
= firstwin
; wp
; wp
= wp
->w_next
)
8902 if (wp
->w_buffer
== buf
)
8905 rettv
->vval
.v_number
= (wp
!= NULL
? winnr
: -1);
8907 rettv
->vval
.v_number
= (curwin
->w_buffer
== buf
? 1 : -1);
8913 * "byte2line(byte)" function
8916 f_byte2line(argvars
, rettv
)
8917 typval_T
*argvars UNUSED
;
8920 #ifndef FEAT_BYTEOFF
8921 rettv
->vval
.v_number
= -1;
8925 boff
= get_tv_number(&argvars
[0]) - 1; /* boff gets -1 on type error */
8927 rettv
->vval
.v_number
= -1;
8929 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
,
8930 (linenr_T
)0, &boff
);
8935 * "byteidx()" function
8938 f_byteidx(argvars
, rettv
)
8948 str
= get_tv_string_chk(&argvars
[0]);
8949 idx
= get_tv_number_chk(&argvars
[1], NULL
);
8950 rettv
->vval
.v_number
= -1;
8951 if (str
== NULL
|| idx
< 0)
8956 for ( ; idx
> 0; idx
--)
8958 if (*t
== NUL
) /* EOL reached */
8960 t
+= (*mb_ptr2len
)(t
);
8962 rettv
->vval
.v_number
= (varnumber_T
)(t
- str
);
8964 if ((size_t)idx
<= STRLEN(str
))
8965 rettv
->vval
.v_number
= idx
;
8970 * "call(func, arglist)" function
8973 f_call(argvars
, rettv
)
8978 typval_T argv
[MAX_FUNC_ARGS
+ 1];
8982 dict_T
*selfdict
= NULL
;
8984 if (argvars
[1].v_type
!= VAR_LIST
)
8989 if (argvars
[1].vval
.v_list
== NULL
)
8992 if (argvars
[0].v_type
== VAR_FUNC
)
8993 func
= argvars
[0].vval
.v_string
;
8995 func
= get_tv_string(&argvars
[0]);
8997 return; /* type error or empty name */
8999 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9001 if (argvars
[2].v_type
!= VAR_DICT
)
9006 selfdict
= argvars
[2].vval
.v_dict
;
9009 for (item
= argvars
[1].vval
.v_list
->lv_first
; item
!= NULL
;
9010 item
= item
->li_next
)
9012 if (argc
== MAX_FUNC_ARGS
)
9014 EMSG(_("E699: Too many arguments"));
9017 /* Make a copy of each argument. This is needed to be able to set
9018 * v_lock to VAR_FIXED in the copy without changing the original list.
9020 copy_tv(&item
->li_tv
, &argv
[argc
++]);
9024 (void)call_func(func
, (int)STRLEN(func
), rettv
, argc
, argv
,
9025 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
9026 &dummy
, TRUE
, selfdict
);
9028 /* Free the arguments. */
9030 clear_tv(&argv
[--argc
]);
9035 * "ceil({float})" function
9038 f_ceil(argvars
, rettv
)
9044 rettv
->v_type
= VAR_FLOAT
;
9045 if (get_float_arg(argvars
, &f
) == OK
)
9046 rettv
->vval
.v_float
= ceil(f
);
9048 rettv
->vval
.v_float
= 0.0;
9053 * "changenr()" function
9056 f_changenr(argvars
, rettv
)
9057 typval_T
*argvars UNUSED
;
9060 rettv
->vval
.v_number
= curbuf
->b_u_seq_cur
;
9064 * "char2nr(string)" function
9067 f_char2nr(argvars
, rettv
)
9073 rettv
->vval
.v_number
= (*mb_ptr2char
)(get_tv_string(&argvars
[0]));
9076 rettv
->vval
.v_number
= get_tv_string(&argvars
[0])[0];
9080 * "cindent(lnum)" function
9083 f_cindent(argvars
, rettv
)
9091 pos
= curwin
->w_cursor
;
9092 lnum
= get_tv_lnum(argvars
);
9093 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
9095 curwin
->w_cursor
.lnum
= lnum
;
9096 rettv
->vval
.v_number
= get_c_indent();
9097 curwin
->w_cursor
= pos
;
9101 rettv
->vval
.v_number
= -1;
9105 * "clearmatches()" function
9108 f_clearmatches(argvars
, rettv
)
9109 typval_T
*argvars UNUSED
;
9110 typval_T
*rettv UNUSED
;
9112 #ifdef FEAT_SEARCH_EXTRA
9113 clear_matches(curwin
);
9118 * "col(string)" function
9121 f_col(argvars
, rettv
)
9127 int fnum
= curbuf
->b_fnum
;
9129 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
9130 if (fp
!= NULL
&& fnum
== curbuf
->b_fnum
)
9132 if (fp
->col
== MAXCOL
)
9134 /* '> can be MAXCOL, get the length of the line then */
9135 if (fp
->lnum
<= curbuf
->b_ml
.ml_line_count
)
9136 col
= (colnr_T
)STRLEN(ml_get(fp
->lnum
)) + 1;
9143 #ifdef FEAT_VIRTUALEDIT
9144 /* col(".") when the cursor is on the NUL at the end of the line
9145 * because of "coladd" can be seen as an extra column. */
9146 if (virtual_active() && fp
== &curwin
->w_cursor
)
9148 char_u
*p
= ml_get_cursor();
9150 if (curwin
->w_cursor
.coladd
>= (colnr_T
)chartabsize(p
,
9151 curwin
->w_virtcol
- curwin
->w_cursor
.coladd
))
9156 if (*p
!= NUL
&& p
[(l
= (*mb_ptr2len
)(p
))] == NUL
)
9159 if (*p
!= NUL
&& p
[1] == NUL
)
9167 rettv
->vval
.v_number
= col
;
9170 #if defined(FEAT_INS_EXPAND)
9172 * "complete()" function
9175 f_complete(argvars
, rettv
)
9177 typval_T
*rettv UNUSED
;
9181 if ((State
& INSERT
) == 0)
9183 EMSG(_("E785: complete() can only be used in Insert mode"));
9187 /* Check for undo allowed here, because if something was already inserted
9188 * the line was already saved for undo and this check isn't done. */
9189 if (!undo_allowed())
9192 if (argvars
[1].v_type
!= VAR_LIST
|| argvars
[1].vval
.v_list
== NULL
)
9198 startcol
= get_tv_number_chk(&argvars
[0], NULL
);
9202 set_completion(startcol
- 1, argvars
[1].vval
.v_list
);
9206 * "complete_add()" function
9209 f_complete_add(argvars
, rettv
)
9213 rettv
->vval
.v_number
= ins_compl_add_tv(&argvars
[0], 0);
9217 * "complete_check()" function
9220 f_complete_check(argvars
, rettv
)
9221 typval_T
*argvars UNUSED
;
9224 int saved
= RedrawingDisabled
;
9226 RedrawingDisabled
= 0;
9227 ins_compl_check_keys(0);
9228 rettv
->vval
.v_number
= compl_interrupted
;
9229 RedrawingDisabled
= saved
;
9234 * "confirm(message, buttons[, default [, type]])" function
9237 f_confirm(argvars
, rettv
)
9238 typval_T
*argvars UNUSED
;
9239 typval_T
*rettv UNUSED
;
9241 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9243 char_u
*buttons
= NULL
;
9244 char_u buf
[NUMBUFLEN
];
9245 char_u buf2
[NUMBUFLEN
];
9247 int type
= VIM_GENERIC
;
9251 message
= get_tv_string_chk(&argvars
[0]);
9252 if (message
== NULL
)
9254 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9256 buttons
= get_tv_string_buf_chk(&argvars
[1], buf
);
9257 if (buttons
== NULL
)
9259 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9261 def
= get_tv_number_chk(&argvars
[2], &error
);
9262 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9264 typestr
= get_tv_string_buf_chk(&argvars
[3], buf2
);
9265 if (typestr
== NULL
)
9269 switch (TOUPPER_ASC(*typestr
))
9271 case 'E': type
= VIM_ERROR
; break;
9272 case 'Q': type
= VIM_QUESTION
; break;
9273 case 'I': type
= VIM_INFO
; break;
9274 case 'W': type
= VIM_WARNING
; break;
9275 case 'G': type
= VIM_GENERIC
; break;
9282 if (buttons
== NULL
|| *buttons
== NUL
)
9283 buttons
= (char_u
*)_("&Ok");
9286 rettv
->vval
.v_number
= do_dialog(type
, NULL
, message
, buttons
,
9295 f_copy(argvars
, rettv
)
9299 item_copy(&argvars
[0], rettv
, FALSE
, 0);
9307 f_cos(argvars
, rettv
)
9313 rettv
->v_type
= VAR_FLOAT
;
9314 if (get_float_arg(argvars
, &f
) == OK
)
9315 rettv
->vval
.v_float
= cos(f
);
9317 rettv
->vval
.v_float
= 0.0;
9322 * "count()" function
9325 f_count(argvars
, rettv
)
9332 if (argvars
[0].v_type
== VAR_LIST
)
9338 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
9341 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9345 ic
= get_tv_number_chk(&argvars
[2], &error
);
9346 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9348 idx
= get_tv_number_chk(&argvars
[3], &error
);
9351 li
= list_find(l
, idx
);
9353 EMSGN(_(e_listidx
), idx
);
9360 for ( ; li
!= NULL
; li
= li
->li_next
)
9361 if (tv_equal(&li
->li_tv
, &argvars
[1], ic
))
9365 else if (argvars
[0].v_type
== VAR_DICT
)
9371 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
9375 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9377 ic
= get_tv_number_chk(&argvars
[2], &error
);
9378 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9382 todo
= error
? 0 : (int)d
->dv_hashtab
.ht_used
;
9383 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
9385 if (!HASHITEM_EMPTY(hi
))
9388 if (tv_equal(&HI2DI(hi
)->di_tv
, &argvars
[1], ic
))
9395 EMSG2(_(e_listdictarg
), "count()");
9396 rettv
->vval
.v_number
= n
;
9400 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9402 * Checks the existence of a cscope connection.
9405 f_cscope_connection(argvars
, rettv
)
9406 typval_T
*argvars UNUSED
;
9407 typval_T
*rettv UNUSED
;
9411 char_u
*dbpath
= NULL
;
9412 char_u
*prepend
= NULL
;
9413 char_u buf
[NUMBUFLEN
];
9415 if (argvars
[0].v_type
!= VAR_UNKNOWN
9416 && argvars
[1].v_type
!= VAR_UNKNOWN
)
9418 num
= (int)get_tv_number(&argvars
[0]);
9419 dbpath
= get_tv_string(&argvars
[1]);
9420 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9421 prepend
= get_tv_string_buf(&argvars
[2], buf
);
9424 rettv
->vval
.v_number
= cs_connection(num
, dbpath
, prepend
);
9429 * "cursor(lnum, col)" function
9431 * Moves the cursor to the specified line and column.
9432 * Returns 0 when the position could be set, -1 otherwise.
9435 f_cursor(argvars
, rettv
)
9440 #ifdef FEAT_VIRTUALEDIT
9444 rettv
->vval
.v_number
= -1;
9445 if (argvars
[1].v_type
== VAR_UNKNOWN
)
9449 if (list2fpos(argvars
, &pos
, NULL
) == FAIL
)
9453 #ifdef FEAT_VIRTUALEDIT
9454 coladd
= pos
.coladd
;
9459 line
= get_tv_lnum(argvars
);
9460 col
= get_tv_number_chk(&argvars
[1], NULL
);
9461 #ifdef FEAT_VIRTUALEDIT
9462 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9463 coladd
= get_tv_number_chk(&argvars
[2], NULL
);
9466 if (line
< 0 || col
< 0
9467 #ifdef FEAT_VIRTUALEDIT
9471 return; /* type error; errmsg already given */
9473 curwin
->w_cursor
.lnum
= line
;
9475 curwin
->w_cursor
.col
= col
- 1;
9476 #ifdef FEAT_VIRTUALEDIT
9477 curwin
->w_cursor
.coladd
= coladd
;
9480 /* Make sure the cursor is in a valid position. */
9483 /* Correct cursor for multi-byte character. */
9488 curwin
->w_set_curswant
= TRUE
;
9489 rettv
->vval
.v_number
= 0;
9493 * "deepcopy()" function
9496 f_deepcopy(argvars
, rettv
)
9502 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9503 noref
= get_tv_number_chk(&argvars
[1], NULL
);
9504 if (noref
< 0 || noref
> 1)
9508 current_copyID
+= COPYID_INC
;
9509 item_copy(&argvars
[0], rettv
, TRUE
, noref
== 0 ? current_copyID
: 0);
9514 * "delete()" function
9517 f_delete(argvars
, rettv
)
9521 if (check_restricted() || check_secure())
9522 rettv
->vval
.v_number
= -1;
9524 rettv
->vval
.v_number
= mch_remove(get_tv_string(&argvars
[0]));
9528 * "did_filetype()" function
9531 f_did_filetype(argvars
, rettv
)
9532 typval_T
*argvars UNUSED
;
9533 typval_T
*rettv UNUSED
;
9536 rettv
->vval
.v_number
= did_filetype
;
9541 * "diff_filler()" function
9544 f_diff_filler(argvars
, rettv
)
9545 typval_T
*argvars UNUSED
;
9546 typval_T
*rettv UNUSED
;
9549 rettv
->vval
.v_number
= diff_check_fill(curwin
, get_tv_lnum(argvars
));
9554 * "diff_hlID()" function
9557 f_diff_hlID(argvars
, rettv
)
9558 typval_T
*argvars UNUSED
;
9559 typval_T
*rettv UNUSED
;
9562 linenr_T lnum
= get_tv_lnum(argvars
);
9563 static linenr_T prev_lnum
= 0;
9564 static int changedtick
= 0;
9565 static int fnum
= 0;
9566 static int change_start
= 0;
9567 static int change_end
= 0;
9568 static hlf_T hlID
= (hlf_T
)0;
9572 if (lnum
< 0) /* ignore type error in {lnum} arg */
9574 if (lnum
!= prev_lnum
9575 || changedtick
!= curbuf
->b_changedtick
9576 || fnum
!= curbuf
->b_fnum
)
9578 /* New line, buffer, change: need to get the values. */
9579 filler_lines
= diff_check(curwin
, lnum
);
9580 if (filler_lines
< 0)
9582 if (filler_lines
== -1)
9584 change_start
= MAXCOL
;
9586 if (diff_find_change(curwin
, lnum
, &change_start
, &change_end
))
9587 hlID
= HLF_ADD
; /* added line */
9589 hlID
= HLF_CHD
; /* changed line */
9592 hlID
= HLF_ADD
; /* added line */
9597 changedtick
= curbuf
->b_changedtick
;
9598 fnum
= curbuf
->b_fnum
;
9601 if (hlID
== HLF_CHD
|| hlID
== HLF_TXD
)
9603 col
= get_tv_number(&argvars
[1]) - 1; /* ignore type error in {col} */
9604 if (col
>= change_start
&& col
<= change_end
)
9605 hlID
= HLF_TXD
; /* changed text */
9607 hlID
= HLF_CHD
; /* changed line */
9609 rettv
->vval
.v_number
= hlID
== (hlf_T
)0 ? 0 : (int)hlID
;
9614 * "empty({expr})" function
9617 f_empty(argvars
, rettv
)
9623 switch (argvars
[0].v_type
)
9627 n
= argvars
[0].vval
.v_string
== NULL
9628 || *argvars
[0].vval
.v_string
== NUL
;
9631 n
= argvars
[0].vval
.v_number
== 0;
9635 n
= argvars
[0].vval
.v_float
== 0.0;
9639 n
= argvars
[0].vval
.v_list
== NULL
9640 || argvars
[0].vval
.v_list
->lv_first
== NULL
;
9643 n
= argvars
[0].vval
.v_dict
== NULL
9644 || argvars
[0].vval
.v_dict
->dv_hashtab
.ht_used
== 0;
9647 EMSG2(_(e_intern2
), "f_empty()");
9651 rettv
->vval
.v_number
= n
;
9655 * "escape({string}, {chars})" function
9658 f_escape(argvars
, rettv
)
9662 char_u buf
[NUMBUFLEN
];
9664 rettv
->vval
.v_string
= vim_strsave_escaped(get_tv_string(&argvars
[0]),
9665 get_tv_string_buf(&argvars
[1], buf
));
9666 rettv
->v_type
= VAR_STRING
;
9673 f_eval(argvars
, rettv
)
9679 s
= get_tv_string_chk(&argvars
[0]);
9683 if (s
== NULL
|| eval1(&s
, rettv
, TRUE
) == FAIL
)
9685 rettv
->v_type
= VAR_NUMBER
;
9686 rettv
->vval
.v_number
= 0;
9689 EMSG(_(e_trailing
));
9693 * "eventhandler()" function
9696 f_eventhandler(argvars
, rettv
)
9697 typval_T
*argvars UNUSED
;
9700 rettv
->vval
.v_number
= vgetc_busy
;
9704 * "executable()" function
9707 f_executable(argvars
, rettv
)
9711 rettv
->vval
.v_number
= mch_can_exe(get_tv_string(&argvars
[0]));
9715 * "exists()" function
9718 f_exists(argvars
, rettv
)
9727 p
= get_tv_string(&argvars
[0]);
9728 if (*p
== '$') /* environment variable */
9730 /* first try "normal" environment variables (fast) */
9731 if (mch_getenv(p
+ 1) != NULL
)
9735 /* try expanding things like $VIM and ${HOME} */
9736 p
= expand_env_save(p
);
9737 if (p
!= NULL
&& *p
!= '$')
9742 else if (*p
== '&' || *p
== '+') /* option */
9744 n
= (get_option_tv(&p
, NULL
, TRUE
) == OK
);
9745 if (*skipwhite(p
) != NUL
)
9746 n
= FALSE
; /* trailing garbage */
9748 else if (*p
== '*') /* internal or user defined function */
9750 n
= function_exists(p
+ 1);
9754 n
= cmd_exists(p
+ 1);
9760 n
= autocmd_supported(p
+ 2);
9762 n
= au_exists(p
+ 1);
9765 else /* internal variable */
9770 /* get_name_len() takes care of expanding curly braces */
9772 len
= get_name_len(&p
, &tofree
, TRUE
, FALSE
);
9777 n
= (get_var_tv(name
, len
, &tv
, FALSE
) == OK
);
9780 /* handle d.key, l[idx], f(expr) */
9781 n
= (handle_subscript(&p
, &tv
, TRUE
, FALSE
) == OK
);
9792 rettv
->vval
.v_number
= n
;
9796 * "expand()" function
9799 f_expand(argvars
, rettv
)
9806 int flags
= WILD_SILENT
|WILD_USE_NL
|WILD_LIST_NOTFOUND
;
9810 rettv
->v_type
= VAR_STRING
;
9811 s
= get_tv_string(&argvars
[0]);
9812 if (*s
== '%' || *s
== '#' || *s
== '<')
9815 rettv
->vval
.v_string
= eval_vars(s
, s
, &len
, NULL
, &errormsg
, NULL
);
9820 /* When the optional second argument is non-zero, don't remove matches
9821 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
9822 if (argvars
[1].v_type
!= VAR_UNKNOWN
9823 && get_tv_number_chk(&argvars
[1], &error
))
9824 flags
|= WILD_KEEP_ALL
;
9828 xpc
.xp_context
= EXPAND_FILES
;
9829 rettv
->vval
.v_string
= ExpandOne(&xpc
, s
, NULL
, flags
, WILD_ALL
);
9832 rettv
->vval
.v_string
= NULL
;
9837 * "extend(list, list [, idx])" function
9838 * "extend(dict, dict [, action])" function
9841 f_extend(argvars
, rettv
)
9845 if (argvars
[0].v_type
== VAR_LIST
&& argvars
[1].v_type
== VAR_LIST
)
9852 l1
= argvars
[0].vval
.v_list
;
9853 l2
= argvars
[1].vval
.v_list
;
9854 if (l1
!= NULL
&& !tv_check_lock(l1
->lv_lock
, (char_u
*)"extend()")
9857 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9859 before
= get_tv_number_chk(&argvars
[2], &error
);
9861 return; /* type error; errmsg already given */
9863 if (before
== l1
->lv_len
)
9867 item
= list_find(l1
, before
);
9870 EMSGN(_(e_listidx
), before
);
9877 list_extend(l1
, l2
, item
);
9879 copy_tv(&argvars
[0], rettv
);
9882 else if (argvars
[0].v_type
== VAR_DICT
&& argvars
[1].v_type
== VAR_DICT
)
9891 d1
= argvars
[0].vval
.v_dict
;
9892 d2
= argvars
[1].vval
.v_dict
;
9893 if (d1
!= NULL
&& !tv_check_lock(d1
->dv_lock
, (char_u
*)"extend()")
9896 /* Check the third argument. */
9897 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9899 static char *(av
[]) = {"keep", "force", "error"};
9901 action
= get_tv_string_chk(&argvars
[2]);
9903 return; /* type error; errmsg already given */
9904 for (i
= 0; i
< 3; ++i
)
9905 if (STRCMP(action
, av
[i
]) == 0)
9909 EMSG2(_(e_invarg2
), action
);
9914 action
= (char_u
*)"force";
9916 /* Go over all entries in the second dict and add them to the
9918 todo
= (int)d2
->dv_hashtab
.ht_used
;
9919 for (hi2
= d2
->dv_hashtab
.ht_array
; todo
> 0; ++hi2
)
9921 if (!HASHITEM_EMPTY(hi2
))
9924 di1
= dict_find(d1
, hi2
->hi_key
, -1);
9927 di1
= dictitem_copy(HI2DI(hi2
));
9928 if (di1
!= NULL
&& dict_add(d1
, di1
) == FAIL
)
9931 else if (*action
== 'e')
9933 EMSG2(_("E737: Key already exists: %s"), hi2
->hi_key
);
9936 else if (*action
== 'f')
9938 clear_tv(&di1
->di_tv
);
9939 copy_tv(&HI2DI(hi2
)->di_tv
, &di1
->di_tv
);
9944 copy_tv(&argvars
[0], rettv
);
9948 EMSG2(_(e_listdictarg
), "extend()");
9952 * "feedkeys()" function
9955 f_feedkeys(argvars
, rettv
)
9957 typval_T
*rettv UNUSED
;
9960 char_u
*keys
, *flags
;
9961 char_u nbuf
[NUMBUFLEN
];
9965 /* This is not allowed in the sandbox. If the commands would still be
9966 * executed in the sandbox it would be OK, but it probably happens later,
9967 * when "sandbox" is no longer set. */
9971 keys
= get_tv_string(&argvars
[0]);
9974 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9976 flags
= get_tv_string_buf(&argvars
[1], nbuf
);
9977 for ( ; *flags
!= NUL
; ++flags
)
9981 case 'n': remap
= FALSE
; break;
9982 case 'm': remap
= TRUE
; break;
9983 case 't': typed
= TRUE
; break;
9988 /* Need to escape K_SPECIAL and CSI before putting the string in the
9989 * typeahead buffer. */
9990 keys_esc
= vim_strsave_escape_csi(keys
);
9991 if (keys_esc
!= NULL
)
9993 ins_typebuf(keys_esc
, (remap
? REMAP_YES
: REMAP_NONE
),
9994 typebuf
.tb_len
, !typed
, FALSE
);
9997 typebuf_was_filled
= TRUE
;
10003 * "filereadable()" function
10006 f_filereadable(argvars
, rettv
)
10015 # define O_NONBLOCK 0
10017 p
= get_tv_string(&argvars
[0]);
10018 if (*p
&& !mch_isdir(p
) && (fd
= mch_open((char *)p
,
10019 O_RDONLY
| O_NONBLOCK
, 0)) >= 0)
10027 rettv
->vval
.v_number
= n
;
10031 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
10032 * rights to write into.
10035 f_filewritable(argvars
, rettv
)
10039 rettv
->vval
.v_number
= filewritable(get_tv_string(&argvars
[0]));
10042 static void findfilendir
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int find_what
));
10045 findfilendir(argvars
, rettv
, find_what
)
10050 #ifdef FEAT_SEARCHPATH
10052 char_u
*fresult
= NULL
;
10053 char_u
*path
= *curbuf
->b_p_path
== NUL
? p_path
: curbuf
->b_p_path
;
10055 char_u pathbuf
[NUMBUFLEN
];
10061 rettv
->vval
.v_string
= NULL
;
10062 rettv
->v_type
= VAR_STRING
;
10064 #ifdef FEAT_SEARCHPATH
10065 fname
= get_tv_string(&argvars
[0]);
10067 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
10069 p
= get_tv_string_buf_chk(&argvars
[1], pathbuf
);
10077 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
10078 count
= get_tv_number_chk(&argvars
[2], &error
);
10082 if (count
< 0 && rettv_list_alloc(rettv
) == FAIL
)
10085 if (*fname
!= NUL
&& !error
)
10089 if (rettv
->v_type
== VAR_STRING
)
10091 fresult
= find_file_in_path_option(first
? fname
: NULL
,
10092 first
? (int)STRLEN(fname
) : 0,
10096 find_what
== FINDFILE_DIR
10097 ? (char_u
*)"" : curbuf
->b_p_sua
);
10100 if (fresult
!= NULL
&& rettv
->v_type
== VAR_LIST
)
10101 list_append_string(rettv
->vval
.v_list
, fresult
, -1);
10103 } while ((rettv
->v_type
== VAR_LIST
|| --count
> 0) && fresult
!= NULL
);
10106 if (rettv
->v_type
== VAR_STRING
)
10107 rettv
->vval
.v_string
= fresult
;
10111 static void filter_map
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int map
));
10112 static int filter_map_one
__ARGS((typval_T
*tv
, char_u
*expr
, int map
, int *remp
));
10115 * Implementation of map() and filter().
10118 filter_map(argvars
, rettv
, map
)
10123 char_u buf
[NUMBUFLEN
];
10125 listitem_T
*li
, *nli
;
10135 char_u
*ermsg
= map
? (char_u
*)"map()" : (char_u
*)"filter()";
10139 if (argvars
[0].v_type
== VAR_LIST
)
10141 if ((l
= argvars
[0].vval
.v_list
) == NULL
10142 || (map
&& tv_check_lock(l
->lv_lock
, ermsg
)))
10145 else if (argvars
[0].v_type
== VAR_DICT
)
10147 if ((d
= argvars
[0].vval
.v_dict
) == NULL
10148 || (map
&& tv_check_lock(d
->dv_lock
, ermsg
)))
10153 EMSG2(_(e_listdictarg
), ermsg
);
10157 expr
= get_tv_string_buf_chk(&argvars
[1], buf
);
10158 /* On type errors, the preceding call has already displayed an error
10159 * message. Avoid a misleading error message for an empty string that
10160 * was not passed as argument. */
10163 prepare_vimvar(VV_VAL
, &save_val
);
10164 expr
= skipwhite(expr
);
10166 /* We reset "did_emsg" to be able to detect whether an error
10167 * occurred during evaluation of the expression. */
10168 save_did_emsg
= did_emsg
;
10171 prepare_vimvar(VV_KEY
, &save_key
);
10172 if (argvars
[0].v_type
== VAR_DICT
)
10174 vimvars
[VV_KEY
].vv_type
= VAR_STRING
;
10176 ht
= &d
->dv_hashtab
;
10178 todo
= (int)ht
->ht_used
;
10179 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
10181 if (!HASHITEM_EMPTY(hi
))
10185 if (tv_check_lock(di
->di_tv
.v_lock
, ermsg
))
10187 vimvars
[VV_KEY
].vv_str
= vim_strsave(di
->di_key
);
10188 if (filter_map_one(&di
->di_tv
, expr
, map
, &rem
) == FAIL
10192 dictitem_remove(d
, di
);
10193 clear_tv(&vimvars
[VV_KEY
].vv_tv
);
10200 vimvars
[VV_KEY
].vv_type
= VAR_NUMBER
;
10202 for (li
= l
->lv_first
; li
!= NULL
; li
= nli
)
10204 if (tv_check_lock(li
->li_tv
.v_lock
, ermsg
))
10207 vimvars
[VV_KEY
].vv_nr
= index
;
10208 if (filter_map_one(&li
->li_tv
, expr
, map
, &rem
) == FAIL
10212 listitem_remove(l
, li
);
10217 restore_vimvar(VV_KEY
, &save_key
);
10218 restore_vimvar(VV_VAL
, &save_val
);
10220 did_emsg
|= save_did_emsg
;
10223 copy_tv(&argvars
[0], rettv
);
10227 filter_map_one(tv
, expr
, map
, remp
)
10237 copy_tv(tv
, &vimvars
[VV_VAL
].vv_tv
);
10239 if (eval1(&s
, &rettv
, TRUE
) == FAIL
)
10241 if (*s
!= NUL
) /* check for trailing chars after expr */
10243 EMSG2(_(e_invexpr2
), s
);
10248 /* map(): replace the list item value */
10257 /* filter(): when expr is zero remove the item */
10258 *remp
= (get_tv_number_chk(&rettv
, &error
) == 0);
10260 /* On type error, nothing has been removed; return FAIL to stop the
10261 * loop. The error message was given by get_tv_number_chk(). */
10267 clear_tv(&vimvars
[VV_VAL
].vv_tv
);
10272 * "filter()" function
10275 f_filter(argvars
, rettv
)
10279 filter_map(argvars
, rettv
, FALSE
);
10283 * "finddir({fname}[, {path}[, {count}]])" function
10286 f_finddir(argvars
, rettv
)
10290 findfilendir(argvars
, rettv
, FINDFILE_DIR
);
10294 * "findfile({fname}[, {path}[, {count}]])" function
10297 f_findfile(argvars
, rettv
)
10301 findfilendir(argvars
, rettv
, FINDFILE_FILE
);
10306 * "float2nr({float})" function
10309 f_float2nr(argvars
, rettv
)
10315 if (get_float_arg(argvars
, &f
) == OK
)
10317 if (f
< -0x7fffffff)
10318 rettv
->vval
.v_number
= -0x7fffffff;
10319 else if (f
> 0x7fffffff)
10320 rettv
->vval
.v_number
= 0x7fffffff;
10322 rettv
->vval
.v_number
= (varnumber_T
)f
;
10327 * "floor({float})" function
10330 f_floor(argvars
, rettv
)
10336 rettv
->v_type
= VAR_FLOAT
;
10337 if (get_float_arg(argvars
, &f
) == OK
)
10338 rettv
->vval
.v_float
= floor(f
);
10340 rettv
->vval
.v_float
= 0.0;
10345 * "fnameescape({string})" function
10348 f_fnameescape(argvars
, rettv
)
10352 rettv
->vval
.v_string
= vim_strsave_fnameescape(
10353 get_tv_string(&argvars
[0]), FALSE
);
10354 rettv
->v_type
= VAR_STRING
;
10358 * "fnamemodify({fname}, {mods})" function
10361 f_fnamemodify(argvars
, rettv
)
10369 char_u
*fbuf
= NULL
;
10370 char_u buf
[NUMBUFLEN
];
10372 fname
= get_tv_string_chk(&argvars
[0]);
10373 mods
= get_tv_string_buf_chk(&argvars
[1], buf
);
10374 if (fname
== NULL
|| mods
== NULL
)
10378 len
= (int)STRLEN(fname
);
10379 (void)modify_fname(mods
, &usedlen
, &fname
, &fbuf
, &len
);
10382 rettv
->v_type
= VAR_STRING
;
10384 rettv
->vval
.v_string
= NULL
;
10386 rettv
->vval
.v_string
= vim_strnsave(fname
, len
);
10390 static void foldclosed_both
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int end
));
10393 * "foldclosed()" function
10396 foldclosed_both(argvars
, rettv
, end
)
10401 #ifdef FEAT_FOLDING
10403 linenr_T first
, last
;
10405 lnum
= get_tv_lnum(argvars
);
10406 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
10408 if (hasFoldingWin(curwin
, lnum
, &first
, &last
, FALSE
, NULL
))
10411 rettv
->vval
.v_number
= (varnumber_T
)last
;
10413 rettv
->vval
.v_number
= (varnumber_T
)first
;
10418 rettv
->vval
.v_number
= -1;
10422 * "foldclosed()" function
10425 f_foldclosed(argvars
, rettv
)
10429 foldclosed_both(argvars
, rettv
, FALSE
);
10433 * "foldclosedend()" function
10436 f_foldclosedend(argvars
, rettv
)
10440 foldclosed_both(argvars
, rettv
, TRUE
);
10444 * "foldlevel()" function
10447 f_foldlevel(argvars
, rettv
)
10451 #ifdef FEAT_FOLDING
10454 lnum
= get_tv_lnum(argvars
);
10455 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
10456 rettv
->vval
.v_number
= foldLevel(lnum
);
10461 * "foldtext()" function
10464 f_foldtext(argvars
, rettv
)
10465 typval_T
*argvars UNUSED
;
10468 #ifdef FEAT_FOLDING
10476 rettv
->v_type
= VAR_STRING
;
10477 rettv
->vval
.v_string
= NULL
;
10478 #ifdef FEAT_FOLDING
10479 if ((linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
> 0
10480 && (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
10481 <= curbuf
->b_ml
.ml_line_count
10482 && vimvars
[VV_FOLDDASHES
].vv_str
!= NULL
)
10484 /* Find first non-empty line in the fold. */
10485 lnum
= (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
;
10486 while (lnum
< (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
10488 if (!linewhite(lnum
))
10493 /* Find interesting text in this line. */
10494 s
= skipwhite(ml_get(lnum
));
10495 /* skip C comment-start */
10496 if (s
[0] == '/' && (s
[1] == '*' || s
[1] == '/'))
10498 s
= skipwhite(s
+ 2);
10499 if (*skipwhite(s
) == NUL
10500 && lnum
+ 1 < (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
10502 s
= skipwhite(ml_get(lnum
+ 1));
10504 s
= skipwhite(s
+ 1);
10507 txt
= _("+-%s%3ld lines: ");
10508 r
= alloc((unsigned)(STRLEN(txt
)
10509 + STRLEN(vimvars
[VV_FOLDDASHES
].vv_str
) /* for %s */
10510 + 20 /* for %3ld */
10511 + STRLEN(s
))); /* concatenated */
10514 sprintf((char *)r
, txt
, vimvars
[VV_FOLDDASHES
].vv_str
,
10515 (long)((linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
10516 - (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
+ 1));
10517 len
= (int)STRLEN(r
);
10519 /* remove 'foldmarker' and 'commentstring' */
10520 foldtext_cleanup(r
+ len
);
10521 rettv
->vval
.v_string
= r
;
10528 * "foldtextresult(lnum)" function
10531 f_foldtextresult(argvars
, rettv
)
10532 typval_T
*argvars UNUSED
;
10535 #ifdef FEAT_FOLDING
10539 foldinfo_T foldinfo
;
10543 rettv
->v_type
= VAR_STRING
;
10544 rettv
->vval
.v_string
= NULL
;
10545 #ifdef FEAT_FOLDING
10546 lnum
= get_tv_lnum(argvars
);
10547 /* treat illegal types and illegal string values for {lnum} the same */
10550 fold_count
= foldedCount(curwin
, lnum
, &foldinfo
);
10551 if (fold_count
> 0)
10553 text
= get_foldtext(curwin
, lnum
, lnum
+ fold_count
- 1,
10556 text
= vim_strsave(text
);
10557 rettv
->vval
.v_string
= text
;
10563 * "foreground()" function
10566 f_foreground(argvars
, rettv
)
10567 typval_T
*argvars UNUSED
;
10568 typval_T
*rettv UNUSED
;
10572 gui_mch_set_foreground();
10575 win32_set_foreground();
10581 * "function()" function
10584 f_function(argvars
, rettv
)
10590 s
= get_tv_string(&argvars
[0]);
10591 if (s
== NULL
|| *s
== NUL
|| VIM_ISDIGIT(*s
))
10592 EMSG2(_(e_invarg2
), s
);
10593 /* Don't check an autoload name for existence here. */
10594 else if (vim_strchr(s
, AUTOLOAD_CHAR
) == NULL
&& !function_exists(s
))
10595 EMSG2(_("E700: Unknown function: %s"), s
);
10598 rettv
->vval
.v_string
= vim_strsave(s
);
10599 rettv
->v_type
= VAR_FUNC
;
10604 * "garbagecollect()" function
10607 f_garbagecollect(argvars
, rettv
)
10609 typval_T
*rettv UNUSED
;
10611 /* This is postponed until we are back at the toplevel, because we may be
10612 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10613 want_garbage_collect
= TRUE
;
10615 if (argvars
[0].v_type
!= VAR_UNKNOWN
&& get_tv_number(&argvars
[0]) == 1)
10616 garbage_collect_at_exit
= TRUE
;
10623 f_get(argvars
, rettv
)
10631 typval_T
*tv
= NULL
;
10633 if (argvars
[0].v_type
== VAR_LIST
)
10635 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
10639 li
= list_find(l
, get_tv_number_chk(&argvars
[1], &error
));
10640 if (!error
&& li
!= NULL
)
10644 else if (argvars
[0].v_type
== VAR_DICT
)
10646 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
10648 di
= dict_find(d
, get_tv_string(&argvars
[1]), -1);
10654 EMSG2(_(e_listdictarg
), "get()");
10658 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
10659 copy_tv(&argvars
[2], rettv
);
10662 copy_tv(tv
, rettv
);
10665 static void get_buffer_lines
__ARGS((buf_T
*buf
, linenr_T start
, linenr_T end
, int retlist
, typval_T
*rettv
));
10668 * Get line or list of lines from buffer "buf" into "rettv".
10669 * Return a range (from start to end) of lines in rettv from the specified
10671 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
10674 get_buffer_lines(buf
, start
, end
, retlist
, rettv
)
10683 if (retlist
&& rettv_list_alloc(rettv
) == FAIL
)
10686 if (buf
== NULL
|| buf
->b_ml
.ml_mfp
== NULL
|| start
< 0)
10691 if (start
>= 1 && start
<= buf
->b_ml
.ml_line_count
)
10692 p
= ml_get_buf(buf
, start
, FALSE
);
10696 rettv
->v_type
= VAR_STRING
;
10697 rettv
->vval
.v_string
= vim_strsave(p
);
10706 if (end
> buf
->b_ml
.ml_line_count
)
10707 end
= buf
->b_ml
.ml_line_count
;
10708 while (start
<= end
)
10709 if (list_append_string(rettv
->vval
.v_list
,
10710 ml_get_buf(buf
, start
++, FALSE
), -1) == FAIL
)
10716 * "getbufline()" function
10719 f_getbufline(argvars
, rettv
)
10727 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
10729 buf
= get_buf_tv(&argvars
[0]);
10732 lnum
= get_tv_lnum_buf(&argvars
[1], buf
);
10733 if (argvars
[2].v_type
== VAR_UNKNOWN
)
10736 end
= get_tv_lnum_buf(&argvars
[2], buf
);
10738 get_buffer_lines(buf
, lnum
, end
, TRUE
, rettv
);
10742 * "getbufvar()" function
10745 f_getbufvar(argvars
, rettv
)
10750 buf_T
*save_curbuf
;
10754 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
10755 varname
= get_tv_string_chk(&argvars
[1]);
10757 buf
= get_buf_tv(&argvars
[0]);
10759 rettv
->v_type
= VAR_STRING
;
10760 rettv
->vval
.v_string
= NULL
;
10762 if (buf
!= NULL
&& varname
!= NULL
)
10764 /* set curbuf to be our buf, temporarily */
10765 save_curbuf
= curbuf
;
10768 if (*varname
== '&') /* buffer-local-option */
10769 get_option_tv(&varname
, rettv
, TRUE
);
10772 if (*varname
== NUL
)
10773 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10774 * scope prefix before the NUL byte is required by
10775 * find_var_in_ht(). */
10776 varname
= (char_u
*)"b:" + 2;
10777 /* look up the variable */
10778 v
= find_var_in_ht(&curbuf
->b_vars
.dv_hashtab
, varname
, FALSE
);
10780 copy_tv(&v
->di_tv
, rettv
);
10783 /* restore previous notion of curbuf */
10784 curbuf
= save_curbuf
;
10791 * "getchar()" function
10794 f_getchar(argvars
, rettv
)
10801 /* Position the cursor. Needed after a message that ends in a space. */
10802 windgoto(msg_row
, msg_col
);
10808 if (argvars
[0].v_type
== VAR_UNKNOWN
)
10809 /* getchar(): blocking wait. */
10811 else if (get_tv_number_chk(&argvars
[0], &error
) == 1)
10812 /* getchar(1): only check if char avail */
10814 else if (error
|| vpeekc() == NUL
)
10815 /* illegal argument or getchar(0) and no char avail: return zero */
10818 /* getchar(0) and char avail: return char */
10827 vimvars
[VV_MOUSE_WIN
].vv_nr
= 0;
10828 vimvars
[VV_MOUSE_LNUM
].vv_nr
= 0;
10829 vimvars
[VV_MOUSE_COL
].vv_nr
= 0;
10831 rettv
->vval
.v_number
= n
;
10832 if (IS_SPECIAL(n
) || mod_mask
!= 0)
10834 char_u temp
[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10837 /* Turn a special key into three bytes, plus modifier. */
10840 temp
[i
++] = K_SPECIAL
;
10841 temp
[i
++] = KS_MODIFIER
;
10842 temp
[i
++] = mod_mask
;
10846 temp
[i
++] = K_SPECIAL
;
10847 temp
[i
++] = K_SECOND(n
);
10848 temp
[i
++] = K_THIRD(n
);
10851 else if (has_mbyte
)
10852 i
+= (*mb_char2bytes
)(n
, temp
+ i
);
10857 rettv
->v_type
= VAR_STRING
;
10858 rettv
->vval
.v_string
= vim_strsave(temp
);
10861 if (n
== K_LEFTMOUSE
10862 || n
== K_LEFTMOUSE_NM
10864 || n
== K_LEFTRELEASE
10865 || n
== K_LEFTRELEASE_NM
10866 || n
== K_MIDDLEMOUSE
10867 || n
== K_MIDDLEDRAG
10868 || n
== K_MIDDLERELEASE
10869 || n
== K_RIGHTMOUSE
10870 || n
== K_RIGHTDRAG
10871 || n
== K_RIGHTRELEASE
10874 || n
== K_X1RELEASE
10877 || n
== K_X2RELEASE
10878 || n
== K_MOUSEDOWN
10881 int row
= mouse_row
;
10882 int col
= mouse_col
;
10885 # ifdef FEAT_WINDOWS
10890 if (row
>= 0 && col
>= 0)
10892 /* Find the window at the mouse coordinates and compute the
10893 * text position. */
10894 win
= mouse_find_win(&row
, &col
);
10895 (void)mouse_comp_pos(win
, &row
, &col
, &lnum
);
10896 # ifdef FEAT_WINDOWS
10897 for (wp
= firstwin
; wp
!= win
; wp
= wp
->w_next
)
10900 vimvars
[VV_MOUSE_WIN
].vv_nr
= winnr
;
10901 vimvars
[VV_MOUSE_LNUM
].vv_nr
= lnum
;
10902 vimvars
[VV_MOUSE_COL
].vv_nr
= col
+ 1;
10910 * "getcharmod()" function
10913 f_getcharmod(argvars
, rettv
)
10914 typval_T
*argvars UNUSED
;
10917 rettv
->vval
.v_number
= mod_mask
;
10921 * "getcmdline()" function
10924 f_getcmdline(argvars
, rettv
)
10925 typval_T
*argvars UNUSED
;
10928 rettv
->v_type
= VAR_STRING
;
10929 rettv
->vval
.v_string
= get_cmdline_str();
10933 * "getcmdpos()" function
10936 f_getcmdpos(argvars
, rettv
)
10937 typval_T
*argvars UNUSED
;
10940 rettv
->vval
.v_number
= get_cmdline_pos() + 1;
10944 * "getcmdtype()" function
10947 f_getcmdtype(argvars
, rettv
)
10948 typval_T
*argvars UNUSED
;
10951 rettv
->v_type
= VAR_STRING
;
10952 rettv
->vval
.v_string
= alloc(2);
10953 if (rettv
->vval
.v_string
!= NULL
)
10955 rettv
->vval
.v_string
[0] = get_cmdline_type();
10956 rettv
->vval
.v_string
[1] = NUL
;
10961 * "getcwd()" function
10964 f_getcwd(argvars
, rettv
)
10965 typval_T
*argvars UNUSED
;
10968 char_u cwd
[MAXPATHL
];
10970 rettv
->v_type
= VAR_STRING
;
10971 if (mch_dirname(cwd
, MAXPATHL
) == FAIL
)
10972 rettv
->vval
.v_string
= NULL
;
10975 rettv
->vval
.v_string
= vim_strsave(cwd
);
10976 #ifdef BACKSLASH_IN_FILENAME
10977 if (rettv
->vval
.v_string
!= NULL
)
10978 slash_adjust(rettv
->vval
.v_string
);
10984 * "getfontname()" function
10987 f_getfontname(argvars
, rettv
)
10988 typval_T
*argvars UNUSED
;
10991 rettv
->v_type
= VAR_STRING
;
10992 rettv
->vval
.v_string
= NULL
;
10997 char_u
*name
= NULL
;
10999 if (argvars
[0].v_type
== VAR_UNKNOWN
)
11001 /* Get the "Normal" font. Either the name saved by
11002 * hl_set_font_name() or from the font ID. */
11003 font
= gui
.norm_font
;
11004 name
= hl_get_font_name();
11008 name
= get_tv_string(&argvars
[0]);
11009 if (STRCMP(name
, "*") == 0) /* don't use font dialog */
11011 font
= gui_mch_get_font(name
, FALSE
);
11012 if (font
== NOFONT
)
11013 return; /* Invalid font name, return empty string. */
11015 rettv
->vval
.v_string
= gui_mch_get_fontname(font
, name
);
11016 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
11017 gui_mch_free_font(font
);
11023 * "getfperm({fname})" function
11026 f_getfperm(argvars
, rettv
)
11032 char_u
*perm
= NULL
;
11033 char_u flags
[] = "rwx";
11036 fname
= get_tv_string(&argvars
[0]);
11038 rettv
->v_type
= VAR_STRING
;
11039 if (mch_stat((char *)fname
, &st
) >= 0)
11041 perm
= vim_strsave((char_u
*)"---------");
11044 for (i
= 0; i
< 9; i
++)
11046 if (st
.st_mode
& (1 << (8 - i
)))
11047 perm
[i
] = flags
[i
% 3];
11051 rettv
->vval
.v_string
= perm
;
11055 * "getfsize({fname})" function
11058 f_getfsize(argvars
, rettv
)
11065 fname
= get_tv_string(&argvars
[0]);
11067 rettv
->v_type
= VAR_NUMBER
;
11069 if (mch_stat((char *)fname
, &st
) >= 0)
11071 if (mch_isdir(fname
))
11072 rettv
->vval
.v_number
= 0;
11075 rettv
->vval
.v_number
= (varnumber_T
)st
.st_size
;
11077 /* non-perfect check for overflow */
11078 if ((off_t
)rettv
->vval
.v_number
!= (off_t
)st
.st_size
)
11079 rettv
->vval
.v_number
= -2;
11083 rettv
->vval
.v_number
= -1;
11087 * "getftime({fname})" function
11090 f_getftime(argvars
, rettv
)
11097 fname
= get_tv_string(&argvars
[0]);
11099 if (mch_stat((char *)fname
, &st
) >= 0)
11100 rettv
->vval
.v_number
= (varnumber_T
)st
.st_mtime
;
11102 rettv
->vval
.v_number
= -1;
11106 * "getftype({fname})" function
11109 f_getftype(argvars
, rettv
)
11115 char_u
*type
= NULL
;
11118 fname
= get_tv_string(&argvars
[0]);
11120 rettv
->v_type
= VAR_STRING
;
11121 if (mch_lstat((char *)fname
, &st
) >= 0)
11124 if (S_ISREG(st
.st_mode
))
11126 else if (S_ISDIR(st
.st_mode
))
11129 else if (S_ISLNK(st
.st_mode
))
11133 else if (S_ISBLK(st
.st_mode
))
11137 else if (S_ISCHR(st
.st_mode
))
11141 else if (S_ISFIFO(st
.st_mode
))
11145 else if (S_ISSOCK(st
.st_mode
))
11152 switch (st
.st_mode
& S_IFMT
)
11154 case S_IFREG
: t
= "file"; break;
11155 case S_IFDIR
: t
= "dir"; break;
11157 case S_IFLNK
: t
= "link"; break;
11160 case S_IFBLK
: t
= "bdev"; break;
11163 case S_IFCHR
: t
= "cdev"; break;
11166 case S_IFIFO
: t
= "fifo"; break;
11169 case S_IFSOCK
: t
= "socket"; break;
11171 default: t
= "other";
11174 if (mch_isdir(fname
))
11180 type
= vim_strsave((char_u
*)t
);
11182 rettv
->vval
.v_string
= type
;
11186 * "getline(lnum, [end])" function
11189 f_getline(argvars
, rettv
)
11197 lnum
= get_tv_lnum(argvars
);
11198 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11205 end
= get_tv_lnum(&argvars
[1]);
11209 get_buffer_lines(curbuf
, lnum
, end
, retlist
, rettv
);
11213 * "getmatches()" function
11216 f_getmatches(argvars
, rettv
)
11217 typval_T
*argvars UNUSED
;
11220 #ifdef FEAT_SEARCH_EXTRA
11222 matchitem_T
*cur
= curwin
->w_match_head
;
11224 if (rettv_list_alloc(rettv
) == OK
)
11226 while (cur
!= NULL
)
11228 dict
= dict_alloc();
11231 dict_add_nr_str(dict
, "group", 0L, syn_id2name(cur
->hlg_id
));
11232 dict_add_nr_str(dict
, "pattern", 0L, cur
->pattern
);
11233 dict_add_nr_str(dict
, "priority", (long)cur
->priority
, NULL
);
11234 dict_add_nr_str(dict
, "id", (long)cur
->id
, NULL
);
11235 list_append_dict(rettv
->vval
.v_list
, dict
);
11243 * "getpid()" function
11246 f_getpid(argvars
, rettv
)
11247 typval_T
*argvars UNUSED
;
11250 rettv
->vval
.v_number
= mch_get_pid();
11254 * "getpos(string)" function
11257 f_getpos(argvars
, rettv
)
11265 if (rettv_list_alloc(rettv
) == OK
)
11267 l
= rettv
->vval
.v_list
;
11268 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
11270 list_append_number(l
, (varnumber_T
)fnum
);
11272 list_append_number(l
, (varnumber_T
)0);
11273 list_append_number(l
, (fp
!= NULL
) ? (varnumber_T
)fp
->lnum
11275 list_append_number(l
, (fp
!= NULL
)
11276 ? (varnumber_T
)(fp
->col
== MAXCOL
? MAXCOL
: fp
->col
+ 1)
11278 list_append_number(l
,
11279 #ifdef FEAT_VIRTUALEDIT
11280 (fp
!= NULL
) ? (varnumber_T
)fp
->coladd
:
11285 rettv
->vval
.v_number
= FALSE
;
11289 * "getqflist()" and "getloclist()" functions
11292 f_getqflist(argvars
, rettv
)
11293 typval_T
*argvars UNUSED
;
11294 typval_T
*rettv UNUSED
;
11296 #ifdef FEAT_QUICKFIX
11300 #ifdef FEAT_QUICKFIX
11301 if (rettv_list_alloc(rettv
) == OK
)
11304 if (argvars
[0].v_type
!= VAR_UNKNOWN
) /* getloclist() */
11306 wp
= find_win_by_nr(&argvars
[0], NULL
);
11311 (void)get_errorlist(wp
, rettv
->vval
.v_list
);
11317 * "getreg()" function
11320 f_getreg(argvars
, rettv
)
11324 char_u
*strregname
;
11329 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
11331 strregname
= get_tv_string_chk(&argvars
[0]);
11332 error
= strregname
== NULL
;
11333 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
11334 arg2
= get_tv_number_chk(&argvars
[1], &error
);
11337 strregname
= vimvars
[VV_REG
].vv_str
;
11338 regname
= (strregname
== NULL
? '"' : *strregname
);
11342 rettv
->v_type
= VAR_STRING
;
11343 rettv
->vval
.v_string
= error
? NULL
:
11344 get_reg_contents(regname
, TRUE
, arg2
);
11348 * "getregtype()" function
11351 f_getregtype(argvars
, rettv
)
11355 char_u
*strregname
;
11357 char_u buf
[NUMBUFLEN
+ 2];
11360 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
11362 strregname
= get_tv_string_chk(&argvars
[0]);
11363 if (strregname
== NULL
) /* type error; errmsg already given */
11365 rettv
->v_type
= VAR_STRING
;
11366 rettv
->vval
.v_string
= NULL
;
11371 /* Default to v:register */
11372 strregname
= vimvars
[VV_REG
].vv_str
;
11374 regname
= (strregname
== NULL
? '"' : *strregname
);
11380 switch (get_reg_type(regname
, ®len
))
11382 case MLINE
: buf
[0] = 'V'; break;
11383 case MCHAR
: buf
[0] = 'v'; break;
11387 sprintf((char *)buf
+ 1, "%ld", reglen
+ 1);
11391 rettv
->v_type
= VAR_STRING
;
11392 rettv
->vval
.v_string
= vim_strsave(buf
);
11396 * "gettabwinvar()" function
11399 f_gettabwinvar(argvars
, rettv
)
11403 getwinvar(argvars
, rettv
, 1);
11407 * "getwinposx()" function
11410 f_getwinposx(argvars
, rettv
)
11411 typval_T
*argvars UNUSED
;
11414 rettv
->vval
.v_number
= -1;
11420 if (gui_mch_get_winpos(&x
, &y
) == OK
)
11421 rettv
->vval
.v_number
= x
;
11427 * "getwinposy()" function
11430 f_getwinposy(argvars
, rettv
)
11431 typval_T
*argvars UNUSED
;
11434 rettv
->vval
.v_number
= -1;
11440 if (gui_mch_get_winpos(&x
, &y
) == OK
)
11441 rettv
->vval
.v_number
= y
;
11447 * Find window specified by "vp" in tabpage "tp".
11450 find_win_by_nr(vp
, tp
)
11452 tabpage_T
*tp
; /* NULL for current tab page */
11454 #ifdef FEAT_WINDOWS
11459 nr
= get_tv_number_chk(vp
, NULL
);
11461 #ifdef FEAT_WINDOWS
11467 for (wp
= (tp
== NULL
|| tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
11468 wp
!= NULL
; wp
= wp
->w_next
)
11473 if (nr
== 0 || nr
== 1)
11480 * "getwinvar()" function
11483 f_getwinvar(argvars
, rettv
)
11487 getwinvar(argvars
, rettv
, 0);
11491 * getwinvar() and gettabwinvar()
11494 getwinvar(argvars
, rettv
, off
)
11497 int off
; /* 1 for gettabwinvar() */
11499 win_T
*win
, *oldcurwin
;
11504 #ifdef FEAT_WINDOWS
11506 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
11510 win
= find_win_by_nr(&argvars
[off
], tp
);
11511 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
11514 rettv
->v_type
= VAR_STRING
;
11515 rettv
->vval
.v_string
= NULL
;
11517 if (win
!= NULL
&& varname
!= NULL
)
11519 /* Set curwin to be our win, temporarily. Also set curbuf, so
11520 * that we can get buffer-local options. */
11521 oldcurwin
= curwin
;
11523 curbuf
= win
->w_buffer
;
11525 if (*varname
== '&') /* window-local-option */
11526 get_option_tv(&varname
, rettv
, 1);
11529 if (*varname
== NUL
)
11530 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11531 * scope prefix before the NUL byte is required by
11532 * find_var_in_ht(). */
11533 varname
= (char_u
*)"w:" + 2;
11534 /* look up the variable */
11535 v
= find_var_in_ht(&win
->w_vars
.dv_hashtab
, varname
, FALSE
);
11537 copy_tv(&v
->di_tv
, rettv
);
11540 /* restore previous notion of curwin */
11541 curwin
= oldcurwin
;
11542 curbuf
= curwin
->w_buffer
;
11549 * "glob()" function
11552 f_glob(argvars
, rettv
)
11556 int flags
= WILD_SILENT
|WILD_USE_NL
;
11560 /* When the optional second argument is non-zero, don't remove matches
11561 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11562 if (argvars
[1].v_type
!= VAR_UNKNOWN
11563 && get_tv_number_chk(&argvars
[1], &error
))
11564 flags
|= WILD_KEEP_ALL
;
11565 rettv
->v_type
= VAR_STRING
;
11569 xpc
.xp_context
= EXPAND_FILES
;
11570 rettv
->vval
.v_string
= ExpandOne(&xpc
, get_tv_string(&argvars
[0]),
11571 NULL
, flags
, WILD_ALL
);
11574 rettv
->vval
.v_string
= NULL
;
11578 * "globpath()" function
11581 f_globpath(argvars
, rettv
)
11586 char_u buf1
[NUMBUFLEN
];
11587 char_u
*file
= get_tv_string_buf_chk(&argvars
[1], buf1
);
11590 /* When the optional second argument is non-zero, don't remove matches
11591 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11592 if (argvars
[2].v_type
!= VAR_UNKNOWN
11593 && get_tv_number_chk(&argvars
[2], &error
))
11594 flags
|= WILD_KEEP_ALL
;
11595 rettv
->v_type
= VAR_STRING
;
11596 if (file
== NULL
|| error
)
11597 rettv
->vval
.v_string
= NULL
;
11599 rettv
->vval
.v_string
= globpath(get_tv_string(&argvars
[0]), file
,
11607 f_has(argvars
, rettv
)
11614 static char *(has_list
[]) =
11635 #if defined(MACOS_X_UNIX)
11659 #if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11662 #if defined(WIN64) || defined(_WIN64)
11668 #ifndef CASE_INSENSITIVE_FILENAME
11674 #ifdef FEAT_AUTOCMD
11679 # ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11680 "balloon_multiline",
11683 #if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11685 # ifdef ALL_BUILTIN_TCAPS
11686 "all_builtin_terms",
11689 #ifdef FEAT_BYTEOFF
11692 #ifdef FEAT_CINDENT
11695 #ifdef FEAT_CLIENTSERVER
11698 #ifdef FEAT_CLIPBOARD
11701 #ifdef FEAT_CMDL_COMPL
11704 #ifdef FEAT_CMDHIST
11707 #ifdef FEAT_COMMENTS
11716 #ifdef CURSOR_SHAPE
11722 #ifdef FEAT_CON_DIALOG
11725 #ifdef FEAT_GUI_DIALOG
11731 #ifdef FEAT_DIGRAPHS
11737 #ifdef FEAT_EMACS_TAGS
11740 "eval", /* always present, of course! */
11741 #ifdef FEAT_EX_EXTRA
11744 #ifdef FEAT_SEARCH_EXTRA
11750 #ifdef FEAT_SEARCHPATH
11753 #if defined(UNIX) && !defined(USE_SYSTEM)
11756 #ifdef FEAT_FIND_ID
11762 #ifdef FEAT_FOLDING
11768 #if !defined(USE_SYSTEM) && defined(UNIX)
11771 #ifdef FEAT_GETTEXT
11777 #ifdef FEAT_GUI_ATHENA
11778 # ifdef FEAT_GUI_NEXTAW
11784 #ifdef FEAT_GUI_GTK
11790 #ifdef FEAT_GUI_GNOME
11793 #ifdef FEAT_GUI_MAC
11796 #ifdef FEAT_GUI_MOTIF
11799 #ifdef FEAT_GUI_PHOTON
11802 #ifdef FEAT_GUI_W16
11805 #ifdef FEAT_GUI_W32
11808 #ifdef FEAT_HANGULIN
11811 #if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11814 #ifdef FEAT_INS_EXPAND
11817 #ifdef FEAT_JUMPLIST
11823 #ifdef FEAT_LANGMAP
11826 #ifdef FEAT_LIBCALL
11829 #ifdef FEAT_LINEBREAK
11835 #ifdef FEAT_LISTCMDS
11838 #ifdef FEAT_LOCALMAP
11844 #ifdef FEAT_SESSION
11847 #ifdef FEAT_MODIFY_FNAME
11853 #ifdef FEAT_MOUSESHAPE
11856 #if defined(UNIX) || defined(VMS)
11857 # ifdef FEAT_MOUSE_DEC
11860 # ifdef FEAT_MOUSE_GPM
11863 # ifdef FEAT_MOUSE_JSB
11866 # ifdef FEAT_MOUSE_NET
11869 # ifdef FEAT_MOUSE_PTERM
11872 # ifdef FEAT_SYSMOUSE
11875 # ifdef FEAT_MOUSE_XTERM
11882 #ifdef FEAT_MBYTE_IME
11885 #ifdef FEAT_MULTI_LANG
11888 #ifdef FEAT_MZSCHEME
11889 #ifndef DYNAMIC_MZSCHEME
11896 #ifdef FEAT_OSFILETYPE
11899 #ifdef FEAT_PATH_EXTRA
11903 #ifndef DYNAMIC_PERL
11908 #ifndef DYNAMIC_PYTHON
11912 #ifdef FEAT_POSTSCRIPT
11915 #ifdef FEAT_PRINTER
11918 #ifdef FEAT_PROFILE
11921 #ifdef FEAT_RELTIME
11924 #ifdef FEAT_QUICKFIX
11927 #ifdef FEAT_RIGHTLEFT
11930 #if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11933 #ifdef FEAT_SCROLLBIND
11936 #ifdef FEAT_CMDL_INFO
11943 #ifdef FEAT_SMARTINDENT
11952 #ifdef FEAT_STL_OPT
11955 #ifdef FEAT_SUN_WORKSHOP
11958 #ifdef FEAT_NETBEANS_INTG
11967 #if defined(USE_SYSTEM) || !defined(UNIX)
11970 #ifdef FEAT_TAG_BINS
11973 #ifdef FEAT_TAG_OLDSTATIC
11976 #ifdef FEAT_TAG_ANYWHITE
11980 # ifndef DYNAMIC_TCL
11987 #ifdef FEAT_TERMRESPONSE
11990 #ifdef FEAT_TEXTOBJ
11993 #ifdef HAVE_TGETENT
11999 #ifdef FEAT_TOOLBAR
12002 #ifdef FEAT_USR_CMDS
12003 "user-commands", /* was accidentally included in 5.4 */
12006 #ifdef FEAT_VIMINFO
12009 #ifdef FEAT_VERTSPLIT
12012 #ifdef FEAT_VIRTUALEDIT
12018 #ifdef FEAT_VISUALEXTRA
12021 #ifdef FEAT_VREPLACE
12024 #ifdef FEAT_WILDIGN
12027 #ifdef FEAT_WILDMENU
12030 #ifdef FEAT_WINDOWS
12036 #ifdef FEAT_WRITEBACKUP
12042 #ifdef FEAT_XFONTSET
12048 #ifdef USE_XSMP_INTERACT
12051 #ifdef FEAT_XCLIPBOARD
12054 #ifdef FEAT_XTERM_SAVE
12057 #if defined(UNIX) && defined(FEAT_X11)
12063 name
= get_tv_string(&argvars
[0]);
12064 for (i
= 0; has_list
[i
] != NULL
; ++i
)
12065 if (STRICMP(name
, has_list
[i
]) == 0)
12073 if (STRNICMP(name
, "patch", 5) == 0)
12074 n
= has_patch(atoi((char *)name
+ 5));
12075 else if (STRICMP(name
, "vim_starting") == 0)
12076 n
= (starting
!= 0);
12078 else if (STRICMP(name
, "multi_byte_encoding") == 0)
12081 #if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12082 else if (STRICMP(name
, "balloon_multiline") == 0)
12083 n
= multiline_balloon_available();
12086 else if (STRICMP(name
, "tcl") == 0)
12087 n
= tcl_enabled(FALSE
);
12089 #if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12090 else if (STRICMP(name
, "iconv") == 0)
12091 n
= iconv_enabled(FALSE
);
12093 #ifdef DYNAMIC_MZSCHEME
12094 else if (STRICMP(name
, "mzscheme") == 0)
12095 n
= mzscheme_enabled(FALSE
);
12097 #ifdef DYNAMIC_RUBY
12098 else if (STRICMP(name
, "ruby") == 0)
12099 n
= ruby_enabled(FALSE
);
12101 #ifdef DYNAMIC_PYTHON
12102 else if (STRICMP(name
, "python") == 0)
12103 n
= python_enabled(FALSE
);
12105 #ifdef DYNAMIC_PERL
12106 else if (STRICMP(name
, "perl") == 0)
12107 n
= perl_enabled(FALSE
);
12110 else if (STRICMP(name
, "gui_running") == 0)
12111 n
= (gui
.in_use
|| gui
.starting
);
12112 # ifdef FEAT_GUI_W32
12113 else if (STRICMP(name
, "gui_win32s") == 0)
12114 n
= gui_is_win32s();
12116 # ifdef FEAT_BROWSE
12117 else if (STRICMP(name
, "browse") == 0)
12118 n
= gui
.in_use
; /* gui_mch_browse() works when GUI is running */
12122 else if (STRICMP(name
, "syntax_items") == 0)
12123 n
= syntax_present(curbuf
);
12125 #if defined(WIN3264)
12126 else if (STRICMP(name
, "win95") == 0)
12127 n
= mch_windows95();
12129 #ifdef FEAT_NETBEANS_INTG
12130 else if (STRICMP(name
, "netbeans_enabled") == 0)
12135 rettv
->vval
.v_number
= n
;
12139 * "has_key()" function
12142 f_has_key(argvars
, rettv
)
12146 if (argvars
[0].v_type
!= VAR_DICT
)
12148 EMSG(_(e_dictreq
));
12151 if (argvars
[0].vval
.v_dict
== NULL
)
12154 rettv
->vval
.v_number
= dict_find(argvars
[0].vval
.v_dict
,
12155 get_tv_string(&argvars
[1]), -1) != NULL
;
12159 * "haslocaldir()" function
12162 f_haslocaldir(argvars
, rettv
)
12163 typval_T
*argvars UNUSED
;
12166 rettv
->vval
.v_number
= (curwin
->w_localdir
!= NULL
);
12170 * "hasmapto()" function
12173 f_hasmapto(argvars
, rettv
)
12179 char_u buf
[NUMBUFLEN
];
12182 name
= get_tv_string(&argvars
[0]);
12183 if (argvars
[1].v_type
== VAR_UNKNOWN
)
12184 mode
= (char_u
*)"nvo";
12187 mode
= get_tv_string_buf(&argvars
[1], buf
);
12188 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12189 abbr
= get_tv_number(&argvars
[2]);
12192 if (map_to_exists(name
, mode
, abbr
))
12193 rettv
->vval
.v_number
= TRUE
;
12195 rettv
->vval
.v_number
= FALSE
;
12199 * "histadd()" function
12202 f_histadd(argvars
, rettv
)
12203 typval_T
*argvars UNUSED
;
12206 #ifdef FEAT_CMDHIST
12209 char_u buf
[NUMBUFLEN
];
12212 rettv
->vval
.v_number
= FALSE
;
12213 if (check_restricted() || check_secure())
12215 #ifdef FEAT_CMDHIST
12216 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
12217 histype
= str
!= NULL
? get_histtype(str
) : -1;
12220 str
= get_tv_string_buf(&argvars
[1], buf
);
12224 add_to_history(histype
, str
, FALSE
, NUL
);
12225 rettv
->vval
.v_number
= TRUE
;
12233 * "histdel()" function
12236 f_histdel(argvars
, rettv
)
12237 typval_T
*argvars UNUSED
;
12238 typval_T
*rettv UNUSED
;
12240 #ifdef FEAT_CMDHIST
12242 char_u buf
[NUMBUFLEN
];
12245 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
12248 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
12249 /* only one argument: clear entire history */
12250 n
= clr_history(get_histtype(str
));
12251 else if (argvars
[1].v_type
== VAR_NUMBER
)
12252 /* index given: remove that entry */
12253 n
= del_history_idx(get_histtype(str
),
12254 (int)get_tv_number(&argvars
[1]));
12256 /* string given: remove all matching entries */
12257 n
= del_history_entry(get_histtype(str
),
12258 get_tv_string_buf(&argvars
[1], buf
));
12259 rettv
->vval
.v_number
= n
;
12264 * "histget()" function
12267 f_histget(argvars
, rettv
)
12268 typval_T
*argvars UNUSED
;
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
12299 f_histnr(argvars
, rettv
)
12300 typval_T
*argvars UNUSED
;
12305 #ifdef FEAT_CMDHIST
12306 char_u
*history
= get_tv_string_chk(&argvars
[0]);
12308 i
= history
== NULL
? HIST_CMD
- 1 : get_histtype(history
);
12309 if (i
>= HIST_CMD
&& i
< HIST_COUNT
)
12310 i
= get_history_idx(i
);
12314 rettv
->vval
.v_number
= i
;
12318 * "highlightID(name)" function
12321 f_hlID(argvars
, rettv
)
12325 rettv
->vval
.v_number
= syn_name2id(get_tv_string(&argvars
[0]));
12329 * "highlight_exists()" function
12332 f_hlexists(argvars
, rettv
)
12336 rettv
->vval
.v_number
= highlight_exists(get_tv_string(&argvars
[0]));
12340 * "hostname()" function
12343 f_hostname(argvars
, rettv
)
12344 typval_T
*argvars UNUSED
;
12347 char_u hostname
[256];
12349 mch_get_host_name(hostname
, 256);
12350 rettv
->v_type
= VAR_STRING
;
12351 rettv
->vval
.v_string
= vim_strsave(hostname
);
12358 f_iconv(argvars
, rettv
)
12359 typval_T
*argvars UNUSED
;
12363 char_u buf1
[NUMBUFLEN
];
12364 char_u buf2
[NUMBUFLEN
];
12365 char_u
*from
, *to
, *str
;
12369 rettv
->v_type
= VAR_STRING
;
12370 rettv
->vval
.v_string
= NULL
;
12373 str
= get_tv_string(&argvars
[0]);
12374 from
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[1], buf1
)));
12375 to
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[2], buf2
)));
12376 vimconv
.vc_type
= CONV_NONE
;
12377 convert_setup(&vimconv
, from
, to
);
12379 /* If the encodings are equal, no conversion needed. */
12380 if (vimconv
.vc_type
== CONV_NONE
)
12381 rettv
->vval
.v_string
= vim_strsave(str
);
12383 rettv
->vval
.v_string
= string_convert(&vimconv
, str
, NULL
);
12385 convert_setup(&vimconv
, NULL
, NULL
);
12392 * "indent()" function
12395 f_indent(argvars
, rettv
)
12401 lnum
= get_tv_lnum(argvars
);
12402 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
12403 rettv
->vval
.v_number
= get_indent_lnum(lnum
);
12405 rettv
->vval
.v_number
= -1;
12409 * "index()" function
12412 f_index(argvars
, rettv
)
12421 rettv
->vval
.v_number
= -1;
12422 if (argvars
[0].v_type
!= VAR_LIST
)
12424 EMSG(_(e_listreq
));
12427 l
= argvars
[0].vval
.v_list
;
12430 item
= l
->lv_first
;
12431 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12435 /* Start at specified item. Use the cached index that list_find()
12436 * sets, so that a negative number also works. */
12437 item
= list_find(l
, get_tv_number_chk(&argvars
[2], &error
));
12439 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
12440 ic
= get_tv_number_chk(&argvars
[3], &error
);
12445 for ( ; item
!= NULL
; item
= item
->li_next
, ++idx
)
12446 if (tv_equal(&item
->li_tv
, &argvars
[1], ic
))
12448 rettv
->vval
.v_number
= idx
;
12454 static int inputsecret_flag
= 0;
12456 static void get_user_input
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int inputdialog
));
12459 * This function is used by f_input() and f_inputdialog() functions. The third
12460 * argument to f_input() specifies the type of completion to use at the
12461 * prompt. The third argument to f_inputdialog() specifies the value to return
12462 * when the user cancels the prompt.
12465 get_user_input(argvars
, rettv
, inputdialog
)
12470 char_u
*prompt
= get_tv_string_chk(&argvars
[0]);
12473 char_u buf
[NUMBUFLEN
];
12474 int cmd_silent_save
= cmd_silent
;
12475 char_u
*defstr
= (char_u
*)"";
12476 int xp_type
= EXPAND_NOTHING
;
12477 char_u
*xp_arg
= NULL
;
12479 rettv
->v_type
= VAR_STRING
;
12480 rettv
->vval
.v_string
= NULL
;
12482 #ifdef NO_CONSOLE_INPUT
12483 /* While starting up, there is no place to enter text. */
12484 if (no_console_input())
12488 cmd_silent
= FALSE
; /* Want to see the prompt. */
12489 if (prompt
!= NULL
)
12491 /* Only the part of the message after the last NL is considered as
12492 * prompt for the command line */
12493 p
= vim_strrchr(prompt
, '\n');
12503 msg_puts_attr(prompt
, echo_attr
);
12504 msg_didout
= FALSE
;
12508 cmdline_row
= msg_row
;
12510 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12512 defstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
12513 if (defstr
!= NULL
)
12514 stuffReadbuffSpec(defstr
);
12516 if (!inputdialog
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
12522 rettv
->vval
.v_string
= NULL
;
12524 xp_name
= get_tv_string_buf_chk(&argvars
[2], buf
);
12525 if (xp_name
== NULL
)
12528 xp_namelen
= (int)STRLEN(xp_name
);
12530 if (parse_compl_arg(xp_name
, xp_namelen
, &xp_type
, &argt
,
12536 if (defstr
!= NULL
)
12537 rettv
->vval
.v_string
=
12538 getcmdline_prompt(inputsecret_flag
? NUL
: '@', p
, echo_attr
,
12543 /* since the user typed this, no need to wait for return */
12544 need_wait_return
= FALSE
;
12545 msg_didout
= FALSE
;
12547 cmd_silent
= cmd_silent_save
;
12551 * "input()" function
12552 * Also handles inputsecret() when inputsecret is set.
12555 f_input(argvars
, rettv
)
12559 get_user_input(argvars
, rettv
, FALSE
);
12563 * "inputdialog()" function
12566 f_inputdialog(argvars
, rettv
)
12570 #if defined(FEAT_GUI_TEXTDIALOG)
12571 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12572 if (gui
.in_use
&& vim_strchr(p_go
, GO_CONDIALOG
) == NULL
)
12575 char_u buf
[NUMBUFLEN
];
12576 char_u
*defstr
= (char_u
*)"";
12578 message
= get_tv_string_chk(&argvars
[0]);
12579 if (argvars
[1].v_type
!= VAR_UNKNOWN
12580 && (defstr
= get_tv_string_buf_chk(&argvars
[1], buf
)) != NULL
)
12581 vim_strncpy(IObuff
, defstr
, IOSIZE
- 1);
12584 if (message
!= NULL
&& defstr
!= NULL
12585 && do_dialog(VIM_QUESTION
, NULL
, message
,
12586 (char_u
*)_("&OK\n&Cancel"), 1, IObuff
) == 1)
12587 rettv
->vval
.v_string
= vim_strsave(IObuff
);
12590 if (message
!= NULL
&& defstr
!= NULL
12591 && argvars
[1].v_type
!= VAR_UNKNOWN
12592 && argvars
[2].v_type
!= VAR_UNKNOWN
)
12593 rettv
->vval
.v_string
= vim_strsave(
12594 get_tv_string_buf(&argvars
[2], buf
));
12596 rettv
->vval
.v_string
= NULL
;
12598 rettv
->v_type
= VAR_STRING
;
12602 get_user_input(argvars
, rettv
, TRUE
);
12606 * "inputlist()" function
12609 f_inputlist(argvars
, rettv
)
12617 #ifdef NO_CONSOLE_INPUT
12618 /* While starting up, there is no place to enter text. */
12619 if (no_console_input())
12622 if (argvars
[0].v_type
!= VAR_LIST
|| argvars
[0].vval
.v_list
== NULL
)
12624 EMSG2(_(e_listarg
), "inputlist()");
12629 msg_row
= Rows
- 1; /* for when 'cmdheight' > 1 */
12630 lines_left
= Rows
; /* avoid more prompt */
12634 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
; li
= li
->li_next
)
12636 msg_puts(get_tv_string(&li
->li_tv
));
12640 /* Ask for choice. */
12641 selected
= prompt_for_number(&mouse_used
);
12643 selected
-= lines_left
;
12645 rettv
->vval
.v_number
= selected
;
12649 static garray_T ga_userinput
= {0, 0, sizeof(tasave_T
), 4, NULL
};
12652 * "inputrestore()" function
12655 f_inputrestore(argvars
, rettv
)
12656 typval_T
*argvars UNUSED
;
12659 if (ga_userinput
.ga_len
> 0)
12661 --ga_userinput
.ga_len
;
12662 restore_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
12663 + ga_userinput
.ga_len
);
12664 /* default return is zero == OK */
12666 else if (p_verbose
> 1)
12668 verb_msg((char_u
*)_("called inputrestore() more often than inputsave()"));
12669 rettv
->vval
.v_number
= 1; /* Failed */
12674 * "inputsave()" function
12677 f_inputsave(argvars
, rettv
)
12678 typval_T
*argvars UNUSED
;
12681 /* Add an entry to the stack of typeahead storage. */
12682 if (ga_grow(&ga_userinput
, 1) == OK
)
12684 save_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
12685 + ga_userinput
.ga_len
);
12686 ++ga_userinput
.ga_len
;
12687 /* default return is zero == OK */
12690 rettv
->vval
.v_number
= 1; /* Failed */
12694 * "inputsecret()" function
12697 f_inputsecret(argvars
, rettv
)
12702 ++inputsecret_flag
;
12703 f_input(argvars
, rettv
);
12705 --inputsecret_flag
;
12709 * "insert()" function
12712 f_insert(argvars
, rettv
)
12721 if (argvars
[0].v_type
!= VAR_LIST
)
12722 EMSG2(_(e_listarg
), "insert()");
12723 else if ((l
= argvars
[0].vval
.v_list
) != NULL
12724 && !tv_check_lock(l
->lv_lock
, (char_u
*)"insert()"))
12726 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12727 before
= get_tv_number_chk(&argvars
[2], &error
);
12729 return; /* type error; errmsg already given */
12731 if (before
== l
->lv_len
)
12735 item
= list_find(l
, before
);
12738 EMSGN(_(e_listidx
), before
);
12744 list_insert_tv(l
, &argvars
[1], item
);
12745 copy_tv(&argvars
[0], rettv
);
12751 * "isdirectory()" function
12754 f_isdirectory(argvars
, rettv
)
12758 rettv
->vval
.v_number
= mch_isdir(get_tv_string(&argvars
[0]));
12762 * "islocked()" function
12765 f_islocked(argvars
, rettv
)
12773 rettv
->vval
.v_number
= -1;
12774 end
= get_lval(get_tv_string(&argvars
[0]), NULL
, &lv
, FALSE
, FALSE
, FALSE
,
12776 if (end
!= NULL
&& lv
.ll_name
!= NULL
)
12779 EMSG(_(e_trailing
));
12782 if (lv
.ll_tv
== NULL
)
12784 if (check_changedtick(lv
.ll_name
))
12785 rettv
->vval
.v_number
= 1; /* always locked */
12788 di
= find_var(lv
.ll_name
, NULL
);
12791 /* Consider a variable locked when:
12792 * 1. the variable itself is locked
12793 * 2. the value of the variable is locked.
12794 * 3. the List or Dict value is locked.
12796 rettv
->vval
.v_number
= ((di
->di_flags
& DI_FLAGS_LOCK
)
12797 || tv_islocked(&di
->di_tv
));
12801 else if (lv
.ll_range
)
12802 EMSG(_("E786: Range not allowed"));
12803 else if (lv
.ll_newkey
!= NULL
)
12804 EMSG2(_(e_dictkey
), lv
.ll_newkey
);
12805 else if (lv
.ll_list
!= NULL
)
12807 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_li
->li_tv
);
12809 /* Dictionary item. */
12810 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_di
->di_tv
);
12817 static void dict_list
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int what
));
12820 * Turn a dict into a list:
12821 * "what" == 0: list of keys
12822 * "what" == 1: list of values
12823 * "what" == 2: list of items
12826 dict_list(argvars
, rettv
, what
)
12839 if (argvars
[0].v_type
!= VAR_DICT
)
12841 EMSG(_(e_dictreq
));
12844 if ((d
= argvars
[0].vval
.v_dict
) == NULL
)
12847 if (rettv_list_alloc(rettv
) == FAIL
)
12850 todo
= (int)d
->dv_hashtab
.ht_used
;
12851 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
12853 if (!HASHITEM_EMPTY(hi
))
12858 li
= listitem_alloc();
12861 list_append(rettv
->vval
.v_list
, li
);
12866 li
->li_tv
.v_type
= VAR_STRING
;
12867 li
->li_tv
.v_lock
= 0;
12868 li
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
12870 else if (what
== 1)
12873 copy_tv(&di
->di_tv
, &li
->li_tv
);
12879 li
->li_tv
.v_type
= VAR_LIST
;
12880 li
->li_tv
.v_lock
= 0;
12881 li
->li_tv
.vval
.v_list
= l2
;
12886 li2
= listitem_alloc();
12889 list_append(l2
, li2
);
12890 li2
->li_tv
.v_type
= VAR_STRING
;
12891 li2
->li_tv
.v_lock
= 0;
12892 li2
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
12894 li2
= listitem_alloc();
12897 list_append(l2
, li2
);
12898 copy_tv(&di
->di_tv
, &li2
->li_tv
);
12905 * "items(dict)" function
12908 f_items(argvars
, rettv
)
12912 dict_list(argvars
, rettv
, 2);
12916 * "join()" function
12919 f_join(argvars
, rettv
)
12926 if (argvars
[0].v_type
!= VAR_LIST
)
12928 EMSG(_(e_listreq
));
12931 if (argvars
[0].vval
.v_list
== NULL
)
12933 if (argvars
[1].v_type
== VAR_UNKNOWN
)
12934 sep
= (char_u
*)" ";
12936 sep
= get_tv_string_chk(&argvars
[1]);
12938 rettv
->v_type
= VAR_STRING
;
12942 ga_init2(&ga
, (int)sizeof(char), 80);
12943 list_join(&ga
, argvars
[0].vval
.v_list
, sep
, TRUE
, 0);
12944 ga_append(&ga
, NUL
);
12945 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
12948 rettv
->vval
.v_string
= NULL
;
12952 * "keys()" function
12955 f_keys(argvars
, rettv
)
12959 dict_list(argvars
, rettv
, 0);
12963 * "last_buffer_nr()" function.
12966 f_last_buffer_nr(argvars
, rettv
)
12967 typval_T
*argvars UNUSED
;
12973 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
12974 if (n
< buf
->b_fnum
)
12977 rettv
->vval
.v_number
= n
;
12984 f_len(argvars
, rettv
)
12988 switch (argvars
[0].v_type
)
12992 rettv
->vval
.v_number
= (varnumber_T
)STRLEN(
12993 get_tv_string(&argvars
[0]));
12996 rettv
->vval
.v_number
= list_len(argvars
[0].vval
.v_list
);
12999 rettv
->vval
.v_number
= dict_len(argvars
[0].vval
.v_dict
);
13002 EMSG(_("E701: Invalid type for len()"));
13007 static void libcall_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int type
));
13010 libcall_common(argvars
, rettv
, type
)
13015 #ifdef FEAT_LIBCALL
13017 char_u
**string_result
;
13021 rettv
->v_type
= type
;
13022 if (type
!= VAR_NUMBER
)
13023 rettv
->vval
.v_string
= NULL
;
13025 if (check_restricted() || check_secure())
13028 #ifdef FEAT_LIBCALL
13029 /* The first two args must be strings, otherwise its meaningless */
13030 if (argvars
[0].v_type
== VAR_STRING
&& argvars
[1].v_type
== VAR_STRING
)
13033 if (argvars
[2].v_type
== VAR_STRING
)
13034 string_in
= argvars
[2].vval
.v_string
;
13035 if (type
== VAR_NUMBER
)
13036 string_result
= NULL
;
13038 string_result
= &rettv
->vval
.v_string
;
13039 if (mch_libcall(argvars
[0].vval
.v_string
,
13040 argvars
[1].vval
.v_string
,
13042 argvars
[2].vval
.v_number
,
13045 && type
== VAR_NUMBER
)
13046 rettv
->vval
.v_number
= nr_result
;
13052 * "libcall()" function
13055 f_libcall(argvars
, rettv
)
13059 libcall_common(argvars
, rettv
, VAR_STRING
);
13063 * "libcallnr()" function
13066 f_libcallnr(argvars
, rettv
)
13070 libcall_common(argvars
, rettv
, VAR_NUMBER
);
13074 * "line(string)" function
13077 f_line(argvars
, rettv
)
13085 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
13088 rettv
->vval
.v_number
= lnum
;
13092 * "line2byte(lnum)" function
13095 f_line2byte(argvars
, rettv
)
13096 typval_T
*argvars UNUSED
;
13099 #ifndef FEAT_BYTEOFF
13100 rettv
->vval
.v_number
= -1;
13104 lnum
= get_tv_lnum(argvars
);
13105 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
13106 rettv
->vval
.v_number
= -1;
13108 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
, lnum
, NULL
);
13109 if (rettv
->vval
.v_number
>= 0)
13110 ++rettv
->vval
.v_number
;
13115 * "lispindent(lnum)" function
13118 f_lispindent(argvars
, rettv
)
13126 pos
= curwin
->w_cursor
;
13127 lnum
= get_tv_lnum(argvars
);
13128 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
13130 curwin
->w_cursor
.lnum
= lnum
;
13131 rettv
->vval
.v_number
= get_lisp_indent();
13132 curwin
->w_cursor
= pos
;
13136 rettv
->vval
.v_number
= -1;
13140 * "localtime()" function
13143 f_localtime(argvars
, rettv
)
13144 typval_T
*argvars UNUSED
;
13147 rettv
->vval
.v_number
= (varnumber_T
)time(NULL
);
13150 static void get_maparg
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int exact
));
13153 get_maparg(argvars
, rettv
, exact
)
13160 char_u buf
[NUMBUFLEN
];
13161 char_u
*keys_buf
= NULL
;
13167 /* return empty string for failure */
13168 rettv
->v_type
= VAR_STRING
;
13169 rettv
->vval
.v_string
= NULL
;
13171 keys
= get_tv_string(&argvars
[0]);
13175 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
13177 which
= get_tv_string_buf_chk(&argvars
[1], buf
);
13178 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13179 abbr
= get_tv_number(&argvars
[2]);
13182 which
= (char_u
*)"";
13186 mode
= get_map_mode(&which
, 0);
13188 keys
= replace_termcodes(keys
, &keys_buf
, TRUE
, TRUE
, FALSE
);
13189 rhs
= check_map(keys
, mode
, exact
, FALSE
, abbr
);
13190 vim_free(keys_buf
);
13194 ga
.ga_itemsize
= 1;
13195 ga
.ga_growsize
= 40;
13197 while (*rhs
!= NUL
)
13198 ga_concat(&ga
, str2special(&rhs
, FALSE
));
13200 ga_append(&ga
, NUL
);
13201 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
13207 * "log10()" function
13210 f_log10(argvars
, rettv
)
13216 rettv
->v_type
= VAR_FLOAT
;
13217 if (get_float_arg(argvars
, &f
) == OK
)
13218 rettv
->vval
.v_float
= log10(f
);
13220 rettv
->vval
.v_float
= 0.0;
13228 f_map(argvars
, rettv
)
13232 filter_map(argvars
, rettv
, TRUE
);
13236 * "maparg()" function
13239 f_maparg(argvars
, rettv
)
13243 get_maparg(argvars
, rettv
, TRUE
);
13247 * "mapcheck()" function
13250 f_mapcheck(argvars
, rettv
)
13254 get_maparg(argvars
, rettv
, FALSE
);
13257 static void find_some_match
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int start
));
13260 find_some_match(argvars
, rettv
, type
)
13265 char_u
*str
= NULL
;
13266 char_u
*expr
= NULL
;
13268 regmatch_T regmatch
;
13269 char_u patbuf
[NUMBUFLEN
];
13270 char_u strbuf
[NUMBUFLEN
];
13274 colnr_T startcol
= 0;
13277 listitem_T
*li
= NULL
;
13279 char_u
*tofree
= NULL
;
13281 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13283 p_cpo
= (char_u
*)"";
13285 rettv
->vval
.v_number
= -1;
13288 /* return empty list when there are no matches */
13289 if (rettv_list_alloc(rettv
) == FAIL
)
13292 else if (type
== 2)
13294 rettv
->v_type
= VAR_STRING
;
13295 rettv
->vval
.v_string
= NULL
;
13298 if (argvars
[0].v_type
== VAR_LIST
)
13300 if ((l
= argvars
[0].vval
.v_list
) == NULL
)
13305 expr
= str
= get_tv_string(&argvars
[0]);
13307 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
13311 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13315 start
= get_tv_number_chk(&argvars
[2], &error
);
13320 li
= list_find(l
, start
);
13323 idx
= l
->lv_idx
; /* use the cached index */
13329 if (start
> (long)STRLEN(str
))
13331 /* When "count" argument is there ignore matches before "start",
13332 * otherwise skip part of the string. Differs when pattern is "^"
13334 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13340 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13341 nth
= get_tv_number_chk(&argvars
[3], &error
);
13346 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
13347 if (regmatch
.regprog
!= NULL
)
13349 regmatch
.rm_ic
= p_ic
;
13361 str
= echo_string(&li
->li_tv
, &tofree
, strbuf
, 0);
13366 match
= vim_regexec_nl(®match
, str
, (colnr_T
)startcol
);
13368 if (match
&& --nth
<= 0)
13370 if (l
== NULL
&& !match
)
13373 /* Advance to just after the match. */
13382 startcol
= (colnr_T
)(regmatch
.startp
[0]
13383 + (*mb_ptr2len
)(regmatch
.startp
[0]) - str
);
13385 startcol
= regmatch
.startp
[0] + 1 - str
;
13396 /* return list with matched string and submatches */
13397 for (i
= 0; i
< NSUBEXP
; ++i
)
13399 if (regmatch
.endp
[i
] == NULL
)
13401 if (list_append_string(rettv
->vval
.v_list
,
13402 (char_u
*)"", 0) == FAIL
)
13405 else if (list_append_string(rettv
->vval
.v_list
,
13406 regmatch
.startp
[i
],
13407 (int)(regmatch
.endp
[i
] - regmatch
.startp
[i
]))
13412 else if (type
== 2)
13414 /* return matched string */
13416 copy_tv(&li
->li_tv
, rettv
);
13418 rettv
->vval
.v_string
= vim_strnsave(regmatch
.startp
[0],
13419 (int)(regmatch
.endp
[0] - regmatch
.startp
[0]));
13421 else if (l
!= NULL
)
13422 rettv
->vval
.v_number
= idx
;
13426 rettv
->vval
.v_number
=
13427 (varnumber_T
)(regmatch
.startp
[0] - str
);
13429 rettv
->vval
.v_number
=
13430 (varnumber_T
)(regmatch
.endp
[0] - str
);
13431 rettv
->vval
.v_number
+= (varnumber_T
)(str
- expr
);
13434 vim_free(regmatch
.regprog
);
13443 * "match()" function
13446 f_match(argvars
, rettv
)
13450 find_some_match(argvars
, rettv
, 1);
13454 * "matchadd()" function
13457 f_matchadd(argvars
, rettv
)
13461 #ifdef FEAT_SEARCH_EXTRA
13462 char_u buf
[NUMBUFLEN
];
13463 char_u
*grp
= get_tv_string_buf_chk(&argvars
[0], buf
); /* group */
13464 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], buf
); /* pattern */
13465 int prio
= 10; /* default priority */
13469 rettv
->vval
.v_number
= -1;
13471 if (grp
== NULL
|| pat
== NULL
)
13473 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13475 prio
= get_tv_number_chk(&argvars
[2], &error
);
13476 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13477 id
= get_tv_number_chk(&argvars
[3], &error
);
13481 if (id
>= 1 && id
<= 3)
13483 EMSGN("E798: ID is reserved for \":match\": %ld", id
);
13487 rettv
->vval
.v_number
= match_add(curwin
, grp
, pat
, prio
, id
);
13492 * "matcharg()" function
13495 f_matcharg(argvars
, rettv
)
13499 if (rettv_list_alloc(rettv
) == OK
)
13501 #ifdef FEAT_SEARCH_EXTRA
13502 int id
= get_tv_number(&argvars
[0]);
13505 if (id
>= 1 && id
<= 3)
13507 if ((m
= (matchitem_T
*)get_match(curwin
, id
)) != NULL
)
13509 list_append_string(rettv
->vval
.v_list
,
13510 syn_id2name(m
->hlg_id
), -1);
13511 list_append_string(rettv
->vval
.v_list
, m
->pattern
, -1);
13515 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
13516 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
13524 * "matchdelete()" function
13527 f_matchdelete(argvars
, rettv
)
13531 #ifdef FEAT_SEARCH_EXTRA
13532 rettv
->vval
.v_number
= match_delete(curwin
,
13533 (int)get_tv_number(&argvars
[0]), TRUE
);
13538 * "matchend()" function
13541 f_matchend(argvars
, rettv
)
13545 find_some_match(argvars
, rettv
, 0);
13549 * "matchlist()" function
13552 f_matchlist(argvars
, rettv
)
13556 find_some_match(argvars
, rettv
, 3);
13560 * "matchstr()" function
13563 f_matchstr(argvars
, rettv
)
13567 find_some_match(argvars
, rettv
, 2);
13570 static void max_min
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int domax
));
13573 max_min(argvars
, rettv
, domax
)
13582 if (argvars
[0].v_type
== VAR_LIST
)
13587 l
= argvars
[0].vval
.v_list
;
13593 n
= get_tv_number_chk(&li
->li_tv
, &error
);
13599 i
= get_tv_number_chk(&li
->li_tv
, &error
);
13600 if (domax
? i
> n
: i
< n
)
13606 else if (argvars
[0].v_type
== VAR_DICT
)
13613 d
= argvars
[0].vval
.v_dict
;
13616 todo
= (int)d
->dv_hashtab
.ht_used
;
13617 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
13619 if (!HASHITEM_EMPTY(hi
))
13622 i
= get_tv_number_chk(&HI2DI(hi
)->di_tv
, &error
);
13628 else if (domax
? i
> n
: i
< n
)
13635 EMSG(_(e_listdictarg
));
13636 rettv
->vval
.v_number
= error
? 0 : n
;
13643 f_max(argvars
, rettv
)
13647 max_min(argvars
, rettv
, TRUE
);
13654 f_min(argvars
, rettv
)
13658 max_min(argvars
, rettv
, FALSE
);
13661 static int mkdir_recurse
__ARGS((char_u
*dir
, int prot
));
13664 * Create the directory in which "dir" is located, and higher levels when
13668 mkdir_recurse(dir
, prot
)
13676 /* Get end of directory name in "dir".
13677 * We're done when it's "/" or "c:/". */
13678 p
= gettail_sep(dir
);
13679 if (p
<= get_past_head(dir
))
13682 /* If the directory exists we're done. Otherwise: create it.*/
13683 updir
= vim_strnsave(dir
, (int)(p
- dir
));
13686 if (mch_isdir(updir
))
13688 else if (mkdir_recurse(updir
, prot
) == OK
)
13689 r
= vim_mkdir_emsg(updir
, prot
);
13696 * "mkdir()" function
13699 f_mkdir(argvars
, rettv
)
13704 char_u buf
[NUMBUFLEN
];
13707 rettv
->vval
.v_number
= FAIL
;
13708 if (check_restricted() || check_secure())
13711 dir
= get_tv_string_buf(&argvars
[0], buf
);
13712 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
13714 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13715 prot
= get_tv_number_chk(&argvars
[2], NULL
);
13716 if (prot
!= -1 && STRCMP(get_tv_string(&argvars
[1]), "p") == 0)
13717 mkdir_recurse(dir
, prot
);
13719 rettv
->vval
.v_number
= prot
!= -1 ? vim_mkdir_emsg(dir
, prot
) : 0;
13724 * "mode()" function
13727 f_mode(argvars
, rettv
)
13740 buf
[0] = VIsual_mode
+ 's' - 'v';
13742 buf
[0] = VIsual_mode
;
13746 if (State
== HITRETURN
|| State
== ASKMORE
|| State
== SETWSIZE
13747 || State
== CONFIRM
)
13750 if (State
== ASKMORE
)
13752 else if (State
== CONFIRM
)
13755 else if (State
== EXTERNCMD
)
13757 else if (State
& INSERT
)
13759 #ifdef FEAT_VREPLACE
13760 if (State
& VREPLACE_FLAG
)
13767 if (State
& REPLACE_FLAG
)
13772 else if (State
& CMDLINE
)
13778 else if (exmode_active
)
13790 /* Clear out the minor mode when the argument is not a non-zero number or
13791 * non-empty string. */
13792 if (!non_zero_arg(&argvars
[0]))
13795 rettv
->vval
.v_string
= vim_strsave(buf
);
13796 rettv
->v_type
= VAR_STRING
;
13799 #ifdef FEAT_MZSCHEME
13801 * "mzeval()" function
13804 f_mzeval(argvars
, rettv
)
13809 char_u buf
[NUMBUFLEN
];
13811 str
= get_tv_string_buf(&argvars
[0], buf
);
13812 do_mzeval(str
, rettv
);
13817 * "nextnonblank()" function
13820 f_nextnonblank(argvars
, rettv
)
13826 for (lnum
= get_tv_lnum(argvars
); ; ++lnum
)
13828 if (lnum
< 0 || lnum
> curbuf
->b_ml
.ml_line_count
)
13833 if (*skipwhite(ml_get(lnum
)) != NUL
)
13836 rettv
->vval
.v_number
= lnum
;
13840 * "nr2char()" function
13843 f_nr2char(argvars
, rettv
)
13847 char_u buf
[NUMBUFLEN
];
13851 buf
[(*mb_char2bytes
)((int)get_tv_number(&argvars
[0]), buf
)] = NUL
;
13855 buf
[0] = (char_u
)get_tv_number(&argvars
[0]);
13858 rettv
->v_type
= VAR_STRING
;
13859 rettv
->vval
.v_string
= vim_strsave(buf
);
13863 * "pathshorten()" function
13866 f_pathshorten(argvars
, rettv
)
13872 rettv
->v_type
= VAR_STRING
;
13873 p
= get_tv_string_chk(&argvars
[0]);
13875 rettv
->vval
.v_string
= NULL
;
13878 p
= vim_strsave(p
);
13879 rettv
->vval
.v_string
= p
;
13890 f_pow(argvars
, rettv
)
13896 rettv
->v_type
= VAR_FLOAT
;
13897 if (get_float_arg(argvars
, &fx
) == OK
13898 && get_float_arg(&argvars
[1], &fy
) == OK
)
13899 rettv
->vval
.v_float
= pow(fx
, fy
);
13901 rettv
->vval
.v_float
= 0.0;
13906 * "prevnonblank()" function
13909 f_prevnonblank(argvars
, rettv
)
13915 lnum
= get_tv_lnum(argvars
);
13916 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
)
13919 while (lnum
>= 1 && *skipwhite(ml_get(lnum
)) == NUL
)
13921 rettv
->vval
.v_number
= lnum
;
13924 #ifdef HAVE_STDARG_H
13925 /* This dummy va_list is here because:
13926 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13927 * - locally in the function results in a "used before set" warning
13928 * - using va_start() to initialize it gives "function with fixed args" error */
13933 * "printf()" function
13936 f_printf(argvars
, rettv
)
13940 rettv
->v_type
= VAR_STRING
;
13941 rettv
->vval
.v_string
= NULL
;
13942 #ifdef HAVE_STDARG_H /* only very old compilers can't do this */
13944 char_u buf
[NUMBUFLEN
];
13947 int saved_did_emsg
= did_emsg
;
13950 /* Get the required length, allocate the buffer and do it for real. */
13952 fmt
= (char *)get_tv_string_buf(&argvars
[0], buf
);
13953 len
= vim_vsnprintf(NULL
, 0, fmt
, ap
, argvars
+ 1);
13956 s
= alloc(len
+ 1);
13959 rettv
->vval
.v_string
= s
;
13960 (void)vim_vsnprintf((char *)s
, len
+ 1, fmt
, ap
, argvars
+ 1);
13963 did_emsg
|= saved_did_emsg
;
13969 * "pumvisible()" function
13972 f_pumvisible(argvars
, rettv
)
13973 typval_T
*argvars UNUSED
;
13974 typval_T
*rettv UNUSED
;
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
);
14010 return; /* type error; errmsg already given */
14012 EMSG(_("E726: Stride is zero"));
14013 else if (stride
> 0 ? end
+ 1 < start
: end
- 1 > start
)
14014 EMSG(_("E727: Start past end"));
14017 if (rettv_list_alloc(rettv
) == OK
)
14018 for (i
= start
; stride
> 0 ? i
<= end
: i
>= end
; i
+= stride
)
14019 if (list_append_number(rettv
->vval
.v_list
,
14020 (varnumber_T
)i
) == FAIL
)
14026 * "readfile()" function
14029 f_readfile(argvars
, rettv
)
14033 int binary
= FALSE
;
14037 #define FREAD_SIZE 200 /* optimized for text lines */
14038 char_u buf
[FREAD_SIZE
];
14039 int readlen
; /* size of last fread() */
14040 int buflen
; /* nr of valid chars in buf[] */
14041 int filtd
; /* how much in buf[] was NUL -> '\n' filtered */
14042 int tolist
; /* first byte in buf[] still to be put in list */
14043 int chop
; /* how many CR to chop off */
14044 char_u
*prev
= NULL
; /* previously read bytes, if any */
14045 int prevlen
= 0; /* length of "prev" if not NULL */
14048 long maxline
= MAXLNUM
;
14051 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
14053 if (STRCMP(get_tv_string(&argvars
[1]), "b") == 0)
14055 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14056 maxline
= get_tv_number(&argvars
[2]);
14059 if (rettv_list_alloc(rettv
) == FAIL
)
14062 /* Always open the file in binary mode, library functions have a mind of
14063 * their own about CR-LF conversion. */
14064 fname
= get_tv_string(&argvars
[0]);
14065 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, READBIN
)) == NULL
)
14067 EMSG2(_(e_notopen
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
14072 while (cnt
< maxline
|| maxline
< 0)
14074 readlen
= (int)fread(buf
+ filtd
, 1, FREAD_SIZE
- filtd
, fd
);
14075 buflen
= filtd
+ readlen
;
14077 for ( ; filtd
< buflen
|| readlen
<= 0; ++filtd
)
14079 if (buf
[filtd
] == '\n' || readlen
<= 0)
14081 /* Only when in binary mode add an empty list item when the
14082 * last line ends in a '\n'. */
14083 if (!binary
&& readlen
== 0 && filtd
== 0)
14086 /* Found end-of-line or end-of-file: add a text line to the
14090 while (filtd
- chop
- 1 >= tolist
14091 && buf
[filtd
- chop
- 1] == '\r')
14093 len
= filtd
- tolist
- chop
;
14095 s
= vim_strnsave(buf
+ tolist
, len
);
14098 s
= alloc((unsigned)(prevlen
+ len
+ 1));
14101 mch_memmove(s
, prev
, prevlen
);
14104 mch_memmove(s
+ prevlen
, buf
+ tolist
, len
);
14105 s
[prevlen
+ len
] = NUL
;
14108 tolist
= filtd
+ 1;
14110 li
= listitem_alloc();
14116 li
->li_tv
.v_type
= VAR_STRING
;
14117 li
->li_tv
.v_lock
= 0;
14118 li
->li_tv
.vval
.v_string
= s
;
14119 list_append(rettv
->vval
.v_list
, li
);
14121 if (++cnt
>= maxline
&& maxline
>= 0)
14126 else if (buf
[filtd
] == NUL
)
14134 /* "buf" is full, need to move text to an allocated buffer */
14137 prev
= vim_strnsave(buf
, buflen
);
14142 s
= alloc((unsigned)(prevlen
+ buflen
));
14145 mch_memmove(s
, prev
, prevlen
);
14146 mch_memmove(s
+ prevlen
, buf
, buflen
);
14156 mch_memmove(buf
, buf
+ tolist
, buflen
- tolist
);
14162 * For a negative line count use only the lines at the end of the file,
14166 while (cnt
> -maxline
)
14168 listitem_remove(rettv
->vval
.v_list
, rettv
->vval
.v_list
->lv_first
);
14176 #if defined(FEAT_RELTIME)
14177 static int list2proftime
__ARGS((typval_T
*arg
, proftime_T
*tm
));
14180 * Convert a List to proftime_T.
14181 * Return FAIL when there is something wrong.
14184 list2proftime(arg
, tm
)
14191 if (arg
->v_type
!= VAR_LIST
|| arg
->vval
.v_list
== NULL
14192 || arg
->vval
.v_list
->lv_len
!= 2)
14194 n1
= list_find_nr(arg
->vval
.v_list
, 0L, &error
);
14195 n2
= list_find_nr(arg
->vval
.v_list
, 1L, &error
);
14203 return error
? FAIL
: OK
;
14205 #endif /* FEAT_RELTIME */
14208 * "reltime()" function
14211 f_reltime(argvars
, rettv
)
14215 #ifdef FEAT_RELTIME
14219 if (argvars
[0].v_type
== VAR_UNKNOWN
)
14221 /* No arguments: get current time. */
14222 profile_start(&res
);
14224 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
14226 if (list2proftime(&argvars
[0], &res
) == FAIL
)
14232 /* Two arguments: compute the difference. */
14233 if (list2proftime(&argvars
[0], &start
) == FAIL
14234 || list2proftime(&argvars
[1], &res
) == FAIL
)
14236 profile_sub(&res
, &start
);
14239 if (rettv_list_alloc(rettv
) == OK
)
14250 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n1
);
14251 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n2
);
14257 * "reltimestr()" function
14260 f_reltimestr(argvars
, rettv
)
14264 #ifdef FEAT_RELTIME
14268 rettv
->v_type
= VAR_STRING
;
14269 rettv
->vval
.v_string
= NULL
;
14270 #ifdef FEAT_RELTIME
14271 if (list2proftime(&argvars
[0], &tm
) == OK
)
14272 rettv
->vval
.v_string
= vim_strsave((char_u
*)profile_msg(&tm
));
14276 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14277 static void make_connection
__ARGS((void));
14278 static int check_connection
__ARGS((void));
14283 if (X_DISPLAY
== NULL
14289 x_force_connect
= TRUE
;
14291 x_force_connect
= FALSE
;
14299 if (X_DISPLAY
== NULL
)
14301 EMSG(_("E240: No connection to Vim server"));
14308 #ifdef FEAT_CLIENTSERVER
14309 static void remote_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int expr
));
14312 remote_common(argvars
, rettv
, expr
)
14317 char_u
*server_name
;
14320 char_u buf
[NUMBUFLEN
];
14327 if (check_restricted() || check_secure())
14331 if (check_connection() == FAIL
)
14335 server_name
= get_tv_string_chk(&argvars
[0]);
14336 if (server_name
== NULL
)
14337 return; /* type error; errmsg already given */
14338 keys
= get_tv_string_buf(&argvars
[1], buf
);
14340 if (serverSendToVim(server_name
, keys
, &r
, &w
, expr
, TRUE
) < 0)
14342 if (serverSendToVim(X_DISPLAY
, server_name
, keys
, &r
, &w
, expr
, 0, TRUE
)
14347 EMSG(r
); /* sending worked but evaluation failed */
14349 EMSG2(_("E241: Unable to send to %s"), server_name
);
14353 rettv
->vval
.v_string
= r
;
14355 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14361 sprintf((char *)str
, PRINTF_HEX_LONG_U
, (long_u
)w
);
14362 v
.di_tv
.v_type
= VAR_STRING
;
14363 v
.di_tv
.vval
.v_string
= vim_strsave(str
);
14364 idvar
= get_tv_string_chk(&argvars
[2]);
14366 set_var(idvar
, &v
.di_tv
, FALSE
);
14367 vim_free(v
.di_tv
.vval
.v_string
);
14373 * "remote_expr()" function
14376 f_remote_expr(argvars
, rettv
)
14377 typval_T
*argvars UNUSED
;
14380 rettv
->v_type
= VAR_STRING
;
14381 rettv
->vval
.v_string
= NULL
;
14382 #ifdef FEAT_CLIENTSERVER
14383 remote_common(argvars
, rettv
, TRUE
);
14388 * "remote_foreground()" function
14391 f_remote_foreground(argvars
, rettv
)
14392 typval_T
*argvars UNUSED
;
14393 typval_T
*rettv UNUSED
;
14395 #ifdef FEAT_CLIENTSERVER
14397 /* On Win32 it's done in this application. */
14399 char_u
*server_name
= get_tv_string_chk(&argvars
[0]);
14401 if (server_name
!= NULL
)
14402 serverForeground(server_name
);
14405 /* Send a foreground() expression to the server. */
14406 argvars
[1].v_type
= VAR_STRING
;
14407 argvars
[1].vval
.v_string
= vim_strsave((char_u
*)"foreground()");
14408 argvars
[2].v_type
= VAR_UNKNOWN
;
14409 remote_common(argvars
, rettv
, TRUE
);
14410 vim_free(argvars
[1].vval
.v_string
);
14416 f_remote_peek(argvars
, rettv
)
14417 typval_T
*argvars UNUSED
;
14420 #ifdef FEAT_CLIENTSERVER
14428 if (check_restricted() || check_secure())
14430 rettv
->vval
.v_number
= -1;
14433 serverid
= get_tv_string_chk(&argvars
[0]);
14434 if (serverid
== NULL
)
14436 rettv
->vval
.v_number
= -1;
14437 return; /* type error; errmsg already given */
14440 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
14442 rettv
->vval
.v_number
= -1;
14445 s
= serverGetReply((HWND
)n
, FALSE
, FALSE
, FALSE
);
14446 rettv
->vval
.v_number
= (s
!= NULL
);
14449 if (check_connection() == FAIL
)
14452 rettv
->vval
.v_number
= serverPeekReply(X_DISPLAY
,
14453 serverStrToWin(serverid
), &s
);
14456 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& rettv
->vval
.v_number
> 0)
14460 v
.di_tv
.v_type
= VAR_STRING
;
14461 v
.di_tv
.vval
.v_string
= vim_strsave(s
);
14462 retvar
= get_tv_string_chk(&argvars
[1]);
14463 if (retvar
!= NULL
)
14464 set_var(retvar
, &v
.di_tv
, FALSE
);
14465 vim_free(v
.di_tv
.vval
.v_string
);
14468 rettv
->vval
.v_number
= -1;
14473 f_remote_read(argvars
, rettv
)
14474 typval_T
*argvars UNUSED
;
14479 #ifdef FEAT_CLIENTSERVER
14480 char_u
*serverid
= get_tv_string_chk(&argvars
[0]);
14482 if (serverid
!= NULL
&& !check_restricted() && !check_secure())
14485 /* The server's HWND is encoded in the 'id' parameter */
14488 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
14490 r
= serverGetReply((HWND
)n
, FALSE
, TRUE
, TRUE
);
14493 if (check_connection() == FAIL
|| serverReadReply(X_DISPLAY
,
14494 serverStrToWin(serverid
), &r
, FALSE
) < 0)
14496 EMSG(_("E277: Unable to read a server reply"));
14499 rettv
->v_type
= VAR_STRING
;
14500 rettv
->vval
.v_string
= r
;
14504 * "remote_send()" function
14507 f_remote_send(argvars
, rettv
)
14508 typval_T
*argvars UNUSED
;
14511 rettv
->v_type
= VAR_STRING
;
14512 rettv
->vval
.v_string
= NULL
;
14513 #ifdef FEAT_CLIENTSERVER
14514 remote_common(argvars
, rettv
, FALSE
);
14519 * "remove()" function
14522 f_remove(argvars
, rettv
)
14527 listitem_T
*item
, *item2
;
14535 if (argvars
[0].v_type
== VAR_DICT
)
14537 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14538 EMSG2(_(e_toomanyarg
), "remove()");
14539 else if ((d
= argvars
[0].vval
.v_dict
) != NULL
14540 && !tv_check_lock(d
->dv_lock
, (char_u
*)"remove() argument"))
14542 key
= get_tv_string_chk(&argvars
[1]);
14545 di
= dict_find(d
, key
, -1);
14547 EMSG2(_(e_dictkey
), key
);
14550 *rettv
= di
->di_tv
;
14551 init_tv(&di
->di_tv
);
14552 dictitem_remove(d
, di
);
14557 else if (argvars
[0].v_type
!= VAR_LIST
)
14558 EMSG2(_(e_listdictarg
), "remove()");
14559 else if ((l
= argvars
[0].vval
.v_list
) != NULL
14560 && !tv_check_lock(l
->lv_lock
, (char_u
*)"remove() argument"))
14564 idx
= get_tv_number_chk(&argvars
[1], &error
);
14566 ; /* type error: do nothing, errmsg already given */
14567 else if ((item
= list_find(l
, idx
)) == NULL
)
14568 EMSGN(_(e_listidx
), idx
);
14571 if (argvars
[2].v_type
== VAR_UNKNOWN
)
14573 /* Remove one item, return its value. */
14574 list_remove(l
, item
, item
);
14575 *rettv
= item
->li_tv
;
14580 /* Remove range of items, return list with values. */
14581 end
= get_tv_number_chk(&argvars
[2], &error
);
14583 ; /* type error: do nothing */
14584 else if ((item2
= list_find(l
, end
)) == NULL
)
14585 EMSGN(_(e_listidx
), end
);
14590 for (li
= item
; li
!= NULL
; li
= li
->li_next
)
14596 if (li
== NULL
) /* didn't find "item2" after "item" */
14597 EMSG(_(e_invrange
));
14600 list_remove(l
, item
, item2
);
14601 if (rettv_list_alloc(rettv
) == OK
)
14603 l
= rettv
->vval
.v_list
;
14604 l
->lv_first
= item
;
14605 l
->lv_last
= item2
;
14606 item
->li_prev
= NULL
;
14607 item2
->li_next
= NULL
;
14618 * "rename({from}, {to})" function
14621 f_rename(argvars
, rettv
)
14625 char_u buf
[NUMBUFLEN
];
14627 if (check_restricted() || check_secure())
14628 rettv
->vval
.v_number
= -1;
14630 rettv
->vval
.v_number
= vim_rename(get_tv_string(&argvars
[0]),
14631 get_tv_string_buf(&argvars
[1], buf
));
14635 * "repeat()" function
14638 f_repeat(argvars
, rettv
)
14649 n
= get_tv_number(&argvars
[1]);
14650 if (argvars
[0].v_type
== VAR_LIST
)
14652 if (rettv_list_alloc(rettv
) == OK
&& argvars
[0].vval
.v_list
!= NULL
)
14654 if (list_extend(rettv
->vval
.v_list
,
14655 argvars
[0].vval
.v_list
, NULL
) == FAIL
)
14660 p
= get_tv_string(&argvars
[0]);
14661 rettv
->v_type
= VAR_STRING
;
14662 rettv
->vval
.v_string
= NULL
;
14664 slen
= (int)STRLEN(p
);
14669 r
= alloc(len
+ 1);
14672 for (i
= 0; i
< n
; i
++)
14673 mch_memmove(r
+ i
* slen
, p
, (size_t)slen
);
14677 rettv
->vval
.v_string
= r
;
14682 * "resolve()" function
14685 f_resolve(argvars
, rettv
)
14691 p
= get_tv_string(&argvars
[0]);
14692 #ifdef FEAT_SHORTCUT
14696 v
= mch_resolve_shortcut(p
);
14698 rettv
->vval
.v_string
= v
;
14700 rettv
->vval
.v_string
= vim_strsave(p
);
14703 # ifdef HAVE_READLINK
14705 char_u buf
[MAXPATHL
+ 1];
14708 char_u
*remain
= NULL
;
14710 int is_relative_to_current
= FALSE
;
14711 int has_trailing_pathsep
= FALSE
;
14714 p
= vim_strsave(p
);
14716 if (p
[0] == '.' && (vim_ispathsep(p
[1])
14717 || (p
[1] == '.' && (vim_ispathsep(p
[2])))))
14718 is_relative_to_current
= TRUE
;
14721 if (len
> 0 && after_pathsep(p
, p
+ len
))
14722 has_trailing_pathsep
= TRUE
;
14724 q
= getnextcomp(p
);
14727 /* Separate the first path component in "p", and keep the
14728 * remainder (beginning with the path separator). */
14729 remain
= vim_strsave(q
- 1);
14737 len
= readlink((char *)p
, (char *)buf
, MAXPATHL
);
14746 EMSG(_("E655: Too many symbolic links (cycle?)"));
14747 rettv
->vval
.v_string
= NULL
;
14751 /* Ensure that the result will have a trailing path separator
14752 * if the argument has one. */
14753 if (remain
== NULL
&& has_trailing_pathsep
)
14756 /* Separate the first path component in the link value and
14757 * concatenate the remainders. */
14758 q
= getnextcomp(vim_ispathsep(*buf
) ? buf
+ 1 : buf
);
14761 if (remain
== NULL
)
14762 remain
= vim_strsave(q
- 1);
14765 cpy
= concat_str(q
- 1, remain
);
14776 if (q
> p
&& *q
== NUL
)
14778 /* Ignore trailing path separator. */
14782 if (q
> p
&& !mch_isFullName(buf
))
14784 /* symlink is relative to directory of argument */
14785 cpy
= alloc((unsigned)(STRLEN(p
) + STRLEN(buf
) + 1));
14789 STRCPY(gettail(cpy
), buf
);
14797 p
= vim_strsave(buf
);
14801 if (remain
== NULL
)
14804 /* Append the first path component of "remain" to "p". */
14805 q
= getnextcomp(remain
+ 1);
14806 len
= q
- remain
- (*q
!= NUL
);
14807 cpy
= vim_strnsave(p
, STRLEN(p
) + len
);
14810 STRNCAT(cpy
, remain
, len
);
14814 /* Shorten "remain". */
14816 STRMOVE(remain
, q
- 1);
14824 /* If the result is a relative path name, make it explicitly relative to
14825 * the current directory if and only if the argument had this form. */
14826 if (!vim_ispathsep(*p
))
14828 if (is_relative_to_current
14832 || vim_ispathsep(p
[1])
14835 || vim_ispathsep(p
[2]))))))
14837 /* Prepend "./". */
14838 cpy
= concat_str((char_u
*)"./", p
);
14845 else if (!is_relative_to_current
)
14847 /* Strip leading "./". */
14849 while (q
[0] == '.' && vim_ispathsep(q
[1]))
14856 /* Ensure that the result will have no trailing path separator
14857 * if the argument had none. But keep "/" or "//". */
14858 if (!has_trailing_pathsep
)
14861 if (after_pathsep(p
, q
))
14862 *gettail_sep(p
) = NUL
;
14865 rettv
->vval
.v_string
= p
;
14868 rettv
->vval
.v_string
= vim_strsave(p
);
14872 simplify_filename(rettv
->vval
.v_string
);
14874 #ifdef HAVE_READLINK
14877 rettv
->v_type
= VAR_STRING
;
14881 * "reverse({list})" function
14884 f_reverse(argvars
, rettv
)
14889 listitem_T
*li
, *ni
;
14891 if (argvars
[0].v_type
!= VAR_LIST
)
14892 EMSG2(_(e_listarg
), "reverse()");
14893 else if ((l
= argvars
[0].vval
.v_list
) != NULL
14894 && !tv_check_lock(l
->lv_lock
, (char_u
*)"reverse()"))
14897 l
->lv_first
= l
->lv_last
= NULL
;
14902 list_append(l
, li
);
14905 rettv
->vval
.v_list
= l
;
14906 rettv
->v_type
= VAR_LIST
;
14908 l
->lv_idx
= l
->lv_len
- l
->lv_idx
- 1;
14912 #define SP_NOMOVE 0x01 /* don't move cursor */
14913 #define SP_REPEAT 0x02 /* repeat to find outer pair */
14914 #define SP_RETCOUNT 0x04 /* return matchcount */
14915 #define SP_SETPCMARK 0x08 /* set previous context mark */
14916 #define SP_START 0x10 /* accept match at start position */
14917 #define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14918 #define SP_END 0x40 /* leave cursor at end of match */
14920 static int get_search_arg
__ARGS((typval_T
*varp
, int *flagsp
));
14923 * Get flags for a search function.
14924 * Possibly sets "p_ws".
14925 * Returns BACKWARD, FORWARD or zero (for an error).
14928 get_search_arg(varp
, flagsp
)
14934 char_u nbuf
[NUMBUFLEN
];
14937 if (varp
->v_type
!= VAR_UNKNOWN
)
14939 flags
= get_tv_string_buf_chk(varp
, nbuf
);
14941 return 0; /* type error; errmsg already given */
14942 while (*flags
!= NUL
)
14946 case 'b': dir
= BACKWARD
; break;
14947 case 'w': p_ws
= TRUE
; break;
14948 case 'W': p_ws
= FALSE
; break;
14950 if (flagsp
!= NULL
)
14953 case 'c': mask
= SP_START
; break;
14954 case 'e': mask
= SP_END
; break;
14955 case 'm': mask
= SP_RETCOUNT
; break;
14956 case 'n': mask
= SP_NOMOVE
; break;
14957 case 'p': mask
= SP_SUBPAT
; break;
14958 case 'r': mask
= SP_REPEAT
; break;
14959 case 's': mask
= SP_SETPCMARK
; break;
14963 EMSG2(_(e_invarg2
), flags
);
14978 * Shared by search() and searchpos() functions
14981 search_cmn(argvars
, match_pos
, flagsp
)
14990 int save_p_ws
= p_ws
;
14992 int retval
= 0; /* default: FAIL */
14993 long lnum_stop
= 0;
14995 #ifdef FEAT_RELTIME
14996 long time_limit
= 0;
14998 int options
= SEARCH_KEEP
;
15001 pat
= get_tv_string(&argvars
[0]);
15002 dir
= get_search_arg(&argvars
[1], flagsp
); /* may set p_ws */
15006 if (flags
& SP_START
)
15007 options
|= SEARCH_START
;
15008 if (flags
& SP_END
)
15009 options
|= SEARCH_END
;
15011 /* Optional arguments: line number to stop searching and timeout. */
15012 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
15014 lnum_stop
= get_tv_number_chk(&argvars
[2], NULL
);
15017 #ifdef FEAT_RELTIME
15018 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
15020 time_limit
= get_tv_number_chk(&argvars
[3], NULL
);
15021 if (time_limit
< 0)
15027 #ifdef FEAT_RELTIME
15028 /* Set the time limit, if there is one. */
15029 profile_setlimit(time_limit
, &tm
);
15033 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
15034 * Check to make sure only those flags are set.
15035 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15036 * flags cannot be set. Check for that condition also.
15038 if (((flags
& (SP_REPEAT
| SP_RETCOUNT
)) != 0)
15039 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
15041 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[1]));
15045 pos
= save_cursor
= curwin
->w_cursor
;
15046 subpatnum
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
15047 options
, RE_SEARCH
, (linenr_T
)lnum_stop
, &tm
);
15048 if (subpatnum
!= FAIL
)
15050 if (flags
& SP_SUBPAT
)
15051 retval
= subpatnum
;
15054 if (flags
& SP_SETPCMARK
)
15056 curwin
->w_cursor
= pos
;
15057 if (match_pos
!= NULL
)
15059 /* Store the match cursor position */
15060 match_pos
->lnum
= pos
.lnum
;
15061 match_pos
->col
= pos
.col
+ 1;
15063 /* "/$" will put the cursor after the end of the line, may need to
15064 * correct that here */
15068 /* If 'n' flag is used: restore cursor position. */
15069 if (flags
& SP_NOMOVE
)
15070 curwin
->w_cursor
= save_cursor
;
15072 curwin
->w_set_curswant
= TRUE
;
15081 * "round({float})" function
15084 f_round(argvars
, rettv
)
15090 rettv
->v_type
= VAR_FLOAT
;
15091 if (get_float_arg(argvars
, &f
) == OK
)
15092 /* round() is not in C90, use ceil() or floor() instead. */
15093 rettv
->vval
.v_float
= f
> 0 ? floor(f
+ 0.5) : ceil(f
- 0.5);
15095 rettv
->vval
.v_float
= 0.0;
15100 * "search()" function
15103 f_search(argvars
, rettv
)
15109 rettv
->vval
.v_number
= search_cmn(argvars
, NULL
, &flags
);
15113 * "searchdecl()" function
15116 f_searchdecl(argvars
, rettv
)
15125 rettv
->vval
.v_number
= 1; /* default: FAIL */
15127 name
= get_tv_string_chk(&argvars
[0]);
15128 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15130 locally
= get_tv_number_chk(&argvars
[1], &error
) == 0;
15131 if (!error
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
15132 thisblock
= get_tv_number_chk(&argvars
[2], &error
) != 0;
15134 if (!error
&& name
!= NULL
)
15135 rettv
->vval
.v_number
= find_decl(name
, (int)STRLEN(name
),
15136 locally
, thisblock
, SEARCH_KEEP
) == FAIL
;
15140 * Used by searchpair() and searchpairpos()
15143 searchpair_cmn(argvars
, match_pos
)
15147 char_u
*spat
, *mpat
, *epat
;
15149 int save_p_ws
= p_ws
;
15152 char_u nbuf1
[NUMBUFLEN
];
15153 char_u nbuf2
[NUMBUFLEN
];
15154 char_u nbuf3
[NUMBUFLEN
];
15155 int retval
= 0; /* default: FAIL */
15156 long lnum_stop
= 0;
15157 long time_limit
= 0;
15159 /* Get the three pattern arguments: start, middle, end. */
15160 spat
= get_tv_string_chk(&argvars
[0]);
15161 mpat
= get_tv_string_buf_chk(&argvars
[1], nbuf1
);
15162 epat
= get_tv_string_buf_chk(&argvars
[2], nbuf2
);
15163 if (spat
== NULL
|| mpat
== NULL
|| epat
== NULL
)
15164 goto theend
; /* type error */
15166 /* Handle the optional fourth argument: flags */
15167 dir
= get_search_arg(&argvars
[3], &flags
); /* may set p_ws */
15171 /* Don't accept SP_END or SP_SUBPAT.
15172 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15174 if ((flags
& (SP_END
| SP_SUBPAT
)) != 0
15175 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
15177 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[3]));
15181 /* Using 'r' implies 'W', otherwise it doesn't work. */
15182 if (flags
& SP_REPEAT
)
15185 /* Optional fifth argument: skip expression */
15186 if (argvars
[3].v_type
== VAR_UNKNOWN
15187 || argvars
[4].v_type
== VAR_UNKNOWN
)
15188 skip
= (char_u
*)"";
15191 skip
= get_tv_string_buf_chk(&argvars
[4], nbuf3
);
15192 if (argvars
[5].v_type
!= VAR_UNKNOWN
)
15194 lnum_stop
= get_tv_number_chk(&argvars
[5], NULL
);
15197 #ifdef FEAT_RELTIME
15198 if (argvars
[6].v_type
!= VAR_UNKNOWN
)
15200 time_limit
= get_tv_number_chk(&argvars
[6], NULL
);
15201 if (time_limit
< 0)
15208 goto theend
; /* type error */
15210 retval
= do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
,
15211 match_pos
, lnum_stop
, time_limit
);
15220 * "searchpair()" function
15223 f_searchpair(argvars
, rettv
)
15227 rettv
->vval
.v_number
= searchpair_cmn(argvars
, NULL
);
15231 * "searchpairpos()" function
15234 f_searchpairpos(argvars
, rettv
)
15242 if (rettv_list_alloc(rettv
) == FAIL
)
15245 if (searchpair_cmn(argvars
, &match_pos
) > 0)
15247 lnum
= match_pos
.lnum
;
15248 col
= match_pos
.col
;
15251 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
15252 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
15256 * Search for a start/middle/end thing.
15257 * Used by searchpair(), see its documentation for the details.
15258 * Returns 0 or -1 for no match,
15261 do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
, match_pos
,
15262 lnum_stop
, time_limit
)
15263 char_u
*spat
; /* start pattern */
15264 char_u
*mpat
; /* middle pattern */
15265 char_u
*epat
; /* end pattern */
15266 int dir
; /* BACKWARD or FORWARD */
15267 char_u
*skip
; /* skip expression */
15268 int flags
; /* SP_SETPCMARK and other SP_ values */
15270 linenr_T lnum_stop
; /* stop at this line if not zero */
15271 long time_limit
; /* stop after this many msec */
15274 char_u
*pat
, *pat2
= NULL
, *pat3
= NULL
;
15285 int options
= SEARCH_KEEP
;
15288 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15290 p_cpo
= empty_option
;
15292 #ifdef FEAT_RELTIME
15293 /* Set the time limit, if there is one. */
15294 profile_setlimit(time_limit
, &tm
);
15297 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15298 * start/middle/end (pat3, for the top pair). */
15299 pat2
= alloc((unsigned)(STRLEN(spat
) + STRLEN(epat
) + 15));
15300 pat3
= alloc((unsigned)(STRLEN(spat
) + STRLEN(mpat
) + STRLEN(epat
) + 23));
15301 if (pat2
== NULL
|| pat3
== NULL
)
15303 sprintf((char *)pat2
, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat
, epat
);
15305 STRCPY(pat3
, pat2
);
15307 sprintf((char *)pat3
, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15309 if (flags
& SP_START
)
15310 options
|= SEARCH_START
;
15312 save_cursor
= curwin
->w_cursor
;
15313 pos
= curwin
->w_cursor
;
15314 clearpos(&firstpos
);
15315 clearpos(&foundpos
);
15319 n
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
15320 options
, RE_SEARCH
, lnum_stop
, &tm
);
15321 if (n
== FAIL
|| (firstpos
.lnum
!= 0 && equalpos(pos
, firstpos
)))
15322 /* didn't find it or found the first match again: FAIL */
15325 if (firstpos
.lnum
== 0)
15327 if (equalpos(pos
, foundpos
))
15329 /* Found the same position again. Can happen with a pattern that
15330 * has "\zs" at the end and searching backwards. Advance one
15331 * character and try again. */
15332 if (dir
== BACKWARD
)
15339 /* clear the start flag to avoid getting stuck here */
15340 options
&= ~SEARCH_START
;
15342 /* If the skip pattern matches, ignore this match. */
15345 save_pos
= curwin
->w_cursor
;
15346 curwin
->w_cursor
= pos
;
15347 r
= eval_to_bool(skip
, &err
, NULL
, FALSE
);
15348 curwin
->w_cursor
= save_pos
;
15351 /* Evaluating {skip} caused an error, break here. */
15352 curwin
->w_cursor
= save_cursor
;
15360 if ((dir
== BACKWARD
&& n
== 3) || (dir
== FORWARD
&& n
== 2))
15362 /* Found end when searching backwards or start when searching
15363 * forward: nested pair. */
15365 pat
= pat2
; /* nested, don't search for middle */
15369 /* Found end when searching forward or start when searching
15370 * backward: end of (nested) pair; or found middle in outer pair. */
15372 pat
= pat3
; /* outer level, search for middle */
15377 /* Found the match: return matchcount or line number. */
15378 if (flags
& SP_RETCOUNT
)
15382 if (flags
& SP_SETPCMARK
)
15384 curwin
->w_cursor
= pos
;
15385 if (!(flags
& SP_REPEAT
))
15387 nest
= 1; /* search for next unmatched */
15391 if (match_pos
!= NULL
)
15393 /* Store the match cursor position */
15394 match_pos
->lnum
= curwin
->w_cursor
.lnum
;
15395 match_pos
->col
= curwin
->w_cursor
.col
+ 1;
15398 /* If 'n' flag is used or search failed: restore cursor position. */
15399 if ((flags
& SP_NOMOVE
) || retval
== 0)
15400 curwin
->w_cursor
= save_cursor
;
15405 if (p_cpo
== empty_option
)
15408 /* Darn, evaluating the {skip} expression changed the value. */
15409 free_string_option(save_cpo
);
15415 * "searchpos()" function
15418 f_searchpos(argvars
, rettv
)
15428 if (rettv_list_alloc(rettv
) == FAIL
)
15431 n
= search_cmn(argvars
, &match_pos
, &flags
);
15434 lnum
= match_pos
.lnum
;
15435 col
= match_pos
.col
;
15438 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
15439 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
15440 if (flags
& SP_SUBPAT
)
15441 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n
);
15446 f_server2client(argvars
, rettv
)
15447 typval_T
*argvars UNUSED
;
15450 #ifdef FEAT_CLIENTSERVER
15451 char_u buf
[NUMBUFLEN
];
15452 char_u
*server
= get_tv_string_chk(&argvars
[0]);
15453 char_u
*reply
= get_tv_string_buf_chk(&argvars
[1], buf
);
15455 rettv
->vval
.v_number
= -1;
15456 if (server
== NULL
|| reply
== NULL
)
15458 if (check_restricted() || check_secure())
15461 if (check_connection() == FAIL
)
15465 if (serverSendReply(server
, reply
) < 0)
15467 EMSG(_("E258: Unable to send to client"));
15470 rettv
->vval
.v_number
= 0;
15472 rettv
->vval
.v_number
= -1;
15477 f_serverlist(argvars
, rettv
)
15478 typval_T
*argvars UNUSED
;
15483 #ifdef FEAT_CLIENTSERVER
15485 r
= serverGetVimNames();
15488 if (X_DISPLAY
!= NULL
)
15489 r
= serverGetVimNames(X_DISPLAY
);
15492 rettv
->v_type
= VAR_STRING
;
15493 rettv
->vval
.v_string
= r
;
15497 * "setbufvar()" function
15500 f_setbufvar(argvars
, rettv
)
15502 typval_T
*rettv UNUSED
;
15506 char_u
*varname
, *bufvarname
;
15508 char_u nbuf
[NUMBUFLEN
];
15510 if (check_restricted() || check_secure())
15512 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
15513 varname
= get_tv_string_chk(&argvars
[1]);
15514 buf
= get_buf_tv(&argvars
[0]);
15515 varp
= &argvars
[2];
15517 if (buf
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
15519 /* set curbuf to be our buf, temporarily */
15520 aucmd_prepbuf(&aco
, buf
);
15522 if (*varname
== '&')
15529 numval
= get_tv_number_chk(varp
, &error
);
15530 strval
= get_tv_string_buf_chk(varp
, nbuf
);
15531 if (!error
&& strval
!= NULL
)
15532 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
15536 bufvarname
= alloc((unsigned)STRLEN(varname
) + 3);
15537 if (bufvarname
!= NULL
)
15539 STRCPY(bufvarname
, "b:");
15540 STRCPY(bufvarname
+ 2, varname
);
15541 set_var(bufvarname
, varp
, TRUE
);
15542 vim_free(bufvarname
);
15546 /* reset notion of buffer */
15547 aucmd_restbuf(&aco
);
15552 * "setcmdpos()" function
15555 f_setcmdpos(argvars
, rettv
)
15559 int pos
= (int)get_tv_number(&argvars
[0]) - 1;
15562 rettv
->vval
.v_number
= set_cmdline_pos(pos
);
15566 * "setline()" function
15569 f_setline(argvars
, rettv
)
15574 char_u
*line
= NULL
;
15576 listitem_T
*li
= NULL
;
15578 linenr_T lcount
= curbuf
->b_ml
.ml_line_count
;
15580 lnum
= get_tv_lnum(&argvars
[0]);
15581 if (argvars
[1].v_type
== VAR_LIST
)
15583 l
= argvars
[1].vval
.v_list
;
15587 line
= get_tv_string_chk(&argvars
[1]);
15589 /* default result is zero == OK */
15594 /* list argument, get next string */
15597 line
= get_tv_string_chk(&li
->li_tv
);
15601 rettv
->vval
.v_number
= 1; /* FAIL */
15602 if (line
== NULL
|| lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
15604 if (lnum
<= curbuf
->b_ml
.ml_line_count
)
15606 /* existing line, replace it */
15607 if (u_savesub(lnum
) == OK
&& ml_replace(lnum
, line
, TRUE
) == OK
)
15609 changed_bytes(lnum
, 0);
15610 if (lnum
== curwin
->w_cursor
.lnum
)
15611 check_cursor_col();
15612 rettv
->vval
.v_number
= 0; /* OK */
15615 else if (added
> 0 || u_save(lnum
- 1, lnum
) == OK
)
15617 /* lnum is one past the last line, append the line */
15619 if (ml_append(lnum
- 1, line
, (colnr_T
)0, FALSE
) == OK
)
15620 rettv
->vval
.v_number
= 0; /* OK */
15623 if (l
== NULL
) /* only one string argument */
15629 appended_lines_mark(lcount
, added
);
15632 static void set_qf_ll_list
__ARGS((win_T
*wp
, typval_T
*list_arg
, typval_T
*action_arg
, typval_T
*rettv
));
15635 * Used by "setqflist()" and "setloclist()" functions
15638 set_qf_ll_list(wp
, list_arg
, action_arg
, rettv
)
15640 typval_T
*list_arg UNUSED
;
15641 typval_T
*action_arg UNUSED
;
15644 #ifdef FEAT_QUICKFIX
15649 rettv
->vval
.v_number
= -1;
15651 #ifdef FEAT_QUICKFIX
15652 if (list_arg
->v_type
!= VAR_LIST
)
15653 EMSG(_(e_listreq
));
15656 list_T
*l
= list_arg
->vval
.v_list
;
15658 if (action_arg
->v_type
== VAR_STRING
)
15660 act
= get_tv_string_chk(action_arg
);
15662 return; /* type error; errmsg already given */
15663 if (*act
== 'a' || *act
== 'r')
15667 if (l
!= NULL
&& set_errorlist(wp
, l
, action
) == OK
)
15668 rettv
->vval
.v_number
= 0;
15674 * "setloclist()" function
15677 f_setloclist(argvars
, rettv
)
15683 rettv
->vval
.v_number
= -1;
15685 win
= find_win_by_nr(&argvars
[0], NULL
);
15687 set_qf_ll_list(win
, &argvars
[1], &argvars
[2], rettv
);
15691 * "setmatches()" function
15694 f_setmatches(argvars
, rettv
)
15698 #ifdef FEAT_SEARCH_EXTRA
15703 rettv
->vval
.v_number
= -1;
15704 if (argvars
[0].v_type
!= VAR_LIST
)
15706 EMSG(_(e_listreq
));
15709 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
15712 /* To some extent make sure that we are dealing with a list from
15713 * "getmatches()". */
15717 if (li
->li_tv
.v_type
!= VAR_DICT
15718 || (d
= li
->li_tv
.vval
.v_dict
) == NULL
)
15723 if (!(dict_find(d
, (char_u
*)"group", -1) != NULL
15724 && dict_find(d
, (char_u
*)"pattern", -1) != NULL
15725 && dict_find(d
, (char_u
*)"priority", -1) != NULL
15726 && dict_find(d
, (char_u
*)"id", -1) != NULL
))
15734 clear_matches(curwin
);
15738 d
= li
->li_tv
.vval
.v_dict
;
15739 match_add(curwin
, get_dict_string(d
, (char_u
*)"group", FALSE
),
15740 get_dict_string(d
, (char_u
*)"pattern", FALSE
),
15741 (int)get_dict_number(d
, (char_u
*)"priority"),
15742 (int)get_dict_number(d
, (char_u
*)"id"));
15745 rettv
->vval
.v_number
= 0;
15751 * "setpos()" function
15754 f_setpos(argvars
, rettv
)
15762 rettv
->vval
.v_number
= -1;
15763 name
= get_tv_string_chk(argvars
);
15766 if (list2fpos(&argvars
[1], &pos
, &fnum
) == OK
)
15770 if (name
[0] == '.' && name
[1] == NUL
)
15773 if (fnum
== curbuf
->b_fnum
)
15775 curwin
->w_cursor
= pos
;
15777 rettv
->vval
.v_number
= 0;
15782 else if (name
[0] == '\'' && name
[1] != NUL
&& name
[2] == NUL
)
15785 if (setmark_pos(name
[1], &pos
, fnum
) == OK
)
15786 rettv
->vval
.v_number
= 0;
15795 * "setqflist()" function
15798 f_setqflist(argvars
, rettv
)
15802 set_qf_ll_list(NULL
, &argvars
[0], &argvars
[1], rettv
);
15806 * "setreg()" function
15809 f_setreg(argvars
, rettv
)
15814 char_u
*strregname
;
15825 strregname
= get_tv_string_chk(argvars
);
15826 rettv
->vval
.v_number
= 1; /* FAIL is default */
15828 if (strregname
== NULL
)
15829 return; /* type error; errmsg already given */
15830 regname
= *strregname
;
15831 if (regname
== 0 || regname
== '@')
15833 else if (regname
== '=')
15836 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15838 stropt
= get_tv_string_chk(&argvars
[2]);
15839 if (stropt
== NULL
)
15840 return; /* type error */
15841 for (; *stropt
!= NUL
; ++stropt
)
15844 case 'a': case 'A': /* append */
15847 case 'v': case 'c': /* character-wise selection */
15850 case 'V': case 'l': /* line-wise selection */
15854 case 'b': case Ctrl_V
: /* block-wise selection */
15855 yank_type
= MBLOCK
;
15856 if (VIM_ISDIGIT(stropt
[1]))
15859 block_len
= getdigits(&stropt
) - 1;
15867 strval
= get_tv_string_chk(&argvars
[1]);
15868 if (strval
!= NULL
)
15869 write_reg_contents_ex(regname
, strval
, -1,
15870 append
, yank_type
, block_len
);
15871 rettv
->vval
.v_number
= 0;
15875 * "settabwinvar()" function
15878 f_settabwinvar(argvars
, rettv
)
15882 setwinvar(argvars
, rettv
, 1);
15886 * "setwinvar()" function
15889 f_setwinvar(argvars
, rettv
)
15893 setwinvar(argvars
, rettv
, 0);
15897 * "setwinvar()" and "settabwinvar()" functions
15900 setwinvar(argvars
, rettv
, off
)
15902 typval_T
*rettv UNUSED
;
15906 #ifdef FEAT_WINDOWS
15907 win_T
*save_curwin
;
15908 tabpage_T
*save_curtab
;
15910 char_u
*varname
, *winvarname
;
15912 char_u nbuf
[NUMBUFLEN
];
15915 if (check_restricted() || check_secure())
15918 #ifdef FEAT_WINDOWS
15920 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
15924 win
= find_win_by_nr(&argvars
[off
], tp
);
15925 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
15926 varp
= &argvars
[off
+ 2];
15928 if (win
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
15930 #ifdef FEAT_WINDOWS
15931 /* set curwin to be our win, temporarily */
15932 save_curwin
= curwin
;
15933 save_curtab
= curtab
;
15934 goto_tabpage_tp(tp
);
15935 if (!win_valid(win
))
15938 curbuf
= curwin
->w_buffer
;
15941 if (*varname
== '&')
15948 numval
= get_tv_number_chk(varp
, &error
);
15949 strval
= get_tv_string_buf_chk(varp
, nbuf
);
15950 if (!error
&& strval
!= NULL
)
15951 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
15955 winvarname
= alloc((unsigned)STRLEN(varname
) + 3);
15956 if (winvarname
!= NULL
)
15958 STRCPY(winvarname
, "w:");
15959 STRCPY(winvarname
+ 2, varname
);
15960 set_var(winvarname
, varp
, TRUE
);
15961 vim_free(winvarname
);
15965 #ifdef FEAT_WINDOWS
15966 /* Restore current tabpage and window, if still valid (autocomands can
15967 * make them invalid). */
15968 if (valid_tabpage(save_curtab
))
15969 goto_tabpage_tp(save_curtab
);
15970 if (win_valid(save_curwin
))
15972 curwin
= save_curwin
;
15973 curbuf
= curwin
->w_buffer
;
15980 * "shellescape({string})" function
15983 f_shellescape(argvars
, rettv
)
15987 rettv
->vval
.v_string
= vim_strsave_shellescape(
15988 get_tv_string(&argvars
[0]), non_zero_arg(&argvars
[1]));
15989 rettv
->v_type
= VAR_STRING
;
15993 * "simplify()" function
15996 f_simplify(argvars
, rettv
)
16002 p
= get_tv_string(&argvars
[0]);
16003 rettv
->vval
.v_string
= vim_strsave(p
);
16004 simplify_filename(rettv
->vval
.v_string
); /* simplify in place */
16005 rettv
->v_type
= VAR_STRING
;
16013 f_sin(argvars
, rettv
)
16019 rettv
->v_type
= VAR_FLOAT
;
16020 if (get_float_arg(argvars
, &f
) == OK
)
16021 rettv
->vval
.v_float
= sin(f
);
16023 rettv
->vval
.v_float
= 0.0;
16028 #ifdef __BORLANDC__
16031 item_compare
__ARGS((const void *s1
, const void *s2
));
16033 #ifdef __BORLANDC__
16036 item_compare2
__ARGS((const void *s1
, const void *s2
));
16038 static int item_compare_ic
;
16039 static char_u
*item_compare_func
;
16040 static int item_compare_func_err
;
16041 #define ITEM_COMPARE_FAIL 999
16044 * Compare functions for f_sort() below.
16047 #ifdef __BORLANDC__
16050 item_compare(s1
, s2
)
16055 char_u
*tofree1
, *tofree2
;
16057 char_u numbuf1
[NUMBUFLEN
];
16058 char_u numbuf2
[NUMBUFLEN
];
16060 p1
= tv2string(&(*(listitem_T
**)s1
)->li_tv
, &tofree1
, numbuf1
, 0);
16061 p2
= tv2string(&(*(listitem_T
**)s2
)->li_tv
, &tofree2
, numbuf2
, 0);
16066 if (item_compare_ic
)
16067 res
= STRICMP(p1
, p2
);
16069 res
= STRCMP(p1
, p2
);
16076 #ifdef __BORLANDC__
16079 item_compare2(s1
, s2
)
16088 /* shortcut after failure in previous call; compare all items equal */
16089 if (item_compare_func_err
)
16092 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16093 * in the copy without changing the original list items. */
16094 copy_tv(&(*(listitem_T
**)s1
)->li_tv
, &argv
[0]);
16095 copy_tv(&(*(listitem_T
**)s2
)->li_tv
, &argv
[1]);
16097 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
16098 res
= call_func(item_compare_func
, (int)STRLEN(item_compare_func
),
16099 &rettv
, 2, argv
, 0L, 0L, &dummy
, TRUE
, NULL
);
16100 clear_tv(&argv
[0]);
16101 clear_tv(&argv
[1]);
16104 res
= ITEM_COMPARE_FAIL
;
16106 res
= get_tv_number_chk(&rettv
, &item_compare_func_err
);
16107 if (item_compare_func_err
)
16108 res
= ITEM_COMPARE_FAIL
; /* return value has wrong type */
16114 * "sort({list})" function
16117 f_sort(argvars
, rettv
)
16127 if (argvars
[0].v_type
!= VAR_LIST
)
16128 EMSG2(_(e_listarg
), "sort()");
16131 l
= argvars
[0].vval
.v_list
;
16132 if (l
== NULL
|| tv_check_lock(l
->lv_lock
, (char_u
*)"sort()"))
16134 rettv
->vval
.v_list
= l
;
16135 rettv
->v_type
= VAR_LIST
;
16140 return; /* short list sorts pretty quickly */
16142 item_compare_ic
= FALSE
;
16143 item_compare_func
= NULL
;
16144 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16146 if (argvars
[1].v_type
== VAR_FUNC
)
16147 item_compare_func
= argvars
[1].vval
.v_string
;
16152 i
= get_tv_number_chk(&argvars
[1], &error
);
16154 return; /* type error; errmsg already given */
16156 item_compare_ic
= TRUE
;
16158 item_compare_func
= get_tv_string(&argvars
[1]);
16162 /* Make an array with each entry pointing to an item in the List. */
16163 ptrs
= (listitem_T
**)alloc((int)(len
* sizeof(listitem_T
*)));
16167 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
16170 item_compare_func_err
= FALSE
;
16171 /* test the compare function */
16172 if (item_compare_func
!= NULL
16173 && item_compare2((void *)&ptrs
[0], (void *)&ptrs
[1])
16174 == ITEM_COMPARE_FAIL
)
16175 EMSG(_("E702: Sort compare function failed"));
16178 /* Sort the array with item pointers. */
16179 qsort((void *)ptrs
, (size_t)len
, sizeof(listitem_T
*),
16180 item_compare_func
== NULL
? item_compare
: item_compare2
);
16182 if (!item_compare_func_err
)
16184 /* Clear the List and append the items in the sorted order. */
16185 l
->lv_first
= l
->lv_last
= l
->lv_idx_item
= NULL
;
16187 for (i
= 0; i
< len
; ++i
)
16188 list_append(l
, ptrs
[i
]);
16197 * "soundfold({word})" function
16200 f_soundfold(argvars
, rettv
)
16206 rettv
->v_type
= VAR_STRING
;
16207 s
= get_tv_string(&argvars
[0]);
16209 rettv
->vval
.v_string
= eval_soundfold(s
);
16211 rettv
->vval
.v_string
= vim_strsave(s
);
16216 * "spellbadword()" function
16219 f_spellbadword(argvars
, rettv
)
16220 typval_T
*argvars UNUSED
;
16223 char_u
*word
= (char_u
*)"";
16224 hlf_T attr
= HLF_COUNT
;
16227 if (rettv_list_alloc(rettv
) == FAIL
)
16231 if (argvars
[0].v_type
== VAR_UNKNOWN
)
16233 /* Find the start and length of the badly spelled word. */
16234 len
= spell_move_to(curwin
, FORWARD
, TRUE
, TRUE
, &attr
);
16236 word
= ml_get_cursor();
16238 else if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
16240 char_u
*str
= get_tv_string_chk(&argvars
[0]);
16245 /* Check the argument for spelling. */
16246 while (*str
!= NUL
)
16248 len
= spell_check(curwin
, str
, &attr
, &capcol
, FALSE
);
16249 if (attr
!= HLF_COUNT
)
16260 list_append_string(rettv
->vval
.v_list
, word
, len
);
16261 list_append_string(rettv
->vval
.v_list
, (char_u
*)(
16262 attr
== HLF_SPB
? "bad" :
16263 attr
== HLF_SPR
? "rare" :
16264 attr
== HLF_SPL
? "local" :
16265 attr
== HLF_SPC
? "caps" :
16270 * "spellsuggest()" function
16273 f_spellsuggest(argvars
, rettv
)
16274 typval_T
*argvars UNUSED
;
16279 int typeerr
= FALSE
;
16284 int need_capital
= FALSE
;
16287 if (rettv_list_alloc(rettv
) == FAIL
)
16291 if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
16293 str
= get_tv_string(&argvars
[0]);
16294 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16296 maxcount
= get_tv_number_chk(&argvars
[1], &typeerr
);
16299 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16301 need_capital
= get_tv_number_chk(&argvars
[2], &typeerr
);
16309 spell_suggest_list(&ga
, str
, maxcount
, need_capital
, FALSE
);
16311 for (i
= 0; i
< ga
.ga_len
; ++i
)
16313 str
= ((char_u
**)ga
.ga_data
)[i
];
16315 li
= listitem_alloc();
16320 li
->li_tv
.v_type
= VAR_STRING
;
16321 li
->li_tv
.v_lock
= 0;
16322 li
->li_tv
.vval
.v_string
= str
;
16323 list_append(rettv
->vval
.v_list
, li
);
16332 f_split(argvars
, rettv
)
16338 char_u
*pat
= NULL
;
16339 regmatch_T regmatch
;
16340 char_u patbuf
[NUMBUFLEN
];
16344 int keepempty
= FALSE
;
16345 int typeerr
= FALSE
;
16347 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16349 p_cpo
= (char_u
*)"";
16351 str
= get_tv_string(&argvars
[0]);
16352 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16354 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
16357 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16358 keepempty
= get_tv_number_chk(&argvars
[2], &typeerr
);
16360 if (pat
== NULL
|| *pat
== NUL
)
16361 pat
= (char_u
*)"[\\x01- ]\\+";
16363 if (rettv_list_alloc(rettv
) == FAIL
)
16368 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
16369 if (regmatch
.regprog
!= NULL
)
16371 regmatch
.rm_ic
= FALSE
;
16372 while (*str
!= NUL
|| keepempty
)
16375 match
= FALSE
; /* empty item at the end */
16377 match
= vim_regexec_nl(®match
, str
, col
);
16379 end
= regmatch
.startp
[0];
16381 end
= str
+ STRLEN(str
);
16382 if (keepempty
|| end
> str
|| (rettv
->vval
.v_list
->lv_len
> 0
16383 && *str
!= NUL
&& match
&& end
< regmatch
.endp
[0]))
16385 if (list_append_string(rettv
->vval
.v_list
, str
,
16386 (int)(end
- str
)) == FAIL
)
16391 /* Advance to just after the match. */
16392 if (regmatch
.endp
[0] > str
)
16396 /* Don't get stuck at the same match. */
16398 col
= (*mb_ptr2len
)(regmatch
.endp
[0]);
16403 str
= regmatch
.endp
[0];
16406 vim_free(regmatch
.regprog
);
16414 * "sqrt()" function
16417 f_sqrt(argvars
, rettv
)
16423 rettv
->v_type
= VAR_FLOAT
;
16424 if (get_float_arg(argvars
, &f
) == OK
)
16425 rettv
->vval
.v_float
= sqrt(f
);
16427 rettv
->vval
.v_float
= 0.0;
16431 * "str2float()" function
16434 f_str2float(argvars
, rettv
)
16438 char_u
*p
= skipwhite(get_tv_string(&argvars
[0]));
16441 p
= skipwhite(p
+ 1);
16442 (void)string2float(p
, &rettv
->vval
.v_float
);
16443 rettv
->v_type
= VAR_FLOAT
;
16448 * "str2nr()" function
16451 f_str2nr(argvars
, rettv
)
16459 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16461 base
= get_tv_number(&argvars
[1]);
16462 if (base
!= 8 && base
!= 10 && base
!= 16)
16469 p
= skipwhite(get_tv_string(&argvars
[0]));
16471 p
= skipwhite(p
+ 1);
16472 vim_str2nr(p
, NULL
, NULL
, base
== 8 ? 2 : 0, base
== 16 ? 2 : 0, &n
, NULL
);
16473 rettv
->vval
.v_number
= n
;
16476 #ifdef HAVE_STRFTIME
16478 * "strftime({format}[, {time}])" function
16481 f_strftime(argvars
, rettv
)
16485 char_u result_buf
[256];
16486 struct tm
*curtime
;
16490 rettv
->v_type
= VAR_STRING
;
16492 p
= get_tv_string(&argvars
[0]);
16493 if (argvars
[1].v_type
== VAR_UNKNOWN
)
16494 seconds
= time(NULL
);
16496 seconds
= (time_t)get_tv_number(&argvars
[1]);
16497 curtime
= localtime(&seconds
);
16498 /* MSVC returns NULL for an invalid value of seconds. */
16499 if (curtime
== NULL
)
16500 rettv
->vval
.v_string
= vim_strsave((char_u
*)_("(Invalid)"));
16507 conv
.vc_type
= CONV_NONE
;
16508 enc
= enc_locale();
16509 convert_setup(&conv
, p_enc
, enc
);
16510 if (conv
.vc_type
!= CONV_NONE
)
16511 p
= string_convert(&conv
, p
, NULL
);
16514 (void)strftime((char *)result_buf
, sizeof(result_buf
),
16515 (char *)p
, curtime
);
16517 result_buf
[0] = NUL
;
16520 if (conv
.vc_type
!= CONV_NONE
)
16522 convert_setup(&conv
, enc
, p_enc
);
16523 if (conv
.vc_type
!= CONV_NONE
)
16524 rettv
->vval
.v_string
= string_convert(&conv
, result_buf
, NULL
);
16527 rettv
->vval
.v_string
= vim_strsave(result_buf
);
16530 /* Release conversion descriptors */
16531 convert_setup(&conv
, NULL
, NULL
);
16539 * "stridx()" function
16542 f_stridx(argvars
, rettv
)
16546 char_u buf
[NUMBUFLEN
];
16549 char_u
*save_haystack
;
16553 needle
= get_tv_string_chk(&argvars
[1]);
16554 save_haystack
= haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
16555 rettv
->vval
.v_number
= -1;
16556 if (needle
== NULL
|| haystack
== NULL
)
16557 return; /* type error; errmsg already given */
16559 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16563 start_idx
= get_tv_number_chk(&argvars
[2], &error
);
16564 if (error
|| start_idx
>= (int)STRLEN(haystack
))
16566 if (start_idx
>= 0)
16567 haystack
+= start_idx
;
16570 pos
= (char_u
*)strstr((char *)haystack
, (char *)needle
);
16572 rettv
->vval
.v_number
= (varnumber_T
)(pos
- save_haystack
);
16576 * "string()" function
16579 f_string(argvars
, rettv
)
16584 char_u numbuf
[NUMBUFLEN
];
16586 rettv
->v_type
= VAR_STRING
;
16587 rettv
->vval
.v_string
= tv2string(&argvars
[0], &tofree
, numbuf
, 0);
16588 /* Make a copy if we have a value but it's not in allocated memory. */
16589 if (rettv
->vval
.v_string
!= NULL
&& tofree
== NULL
)
16590 rettv
->vval
.v_string
= vim_strsave(rettv
->vval
.v_string
);
16594 * "strlen()" function
16597 f_strlen(argvars
, rettv
)
16601 rettv
->vval
.v_number
= (varnumber_T
)(STRLEN(
16602 get_tv_string(&argvars
[0])));
16606 * "strpart()" function
16609 f_strpart(argvars
, rettv
)
16619 p
= get_tv_string(&argvars
[0]);
16620 slen
= (int)STRLEN(p
);
16622 n
= get_tv_number_chk(&argvars
[1], &error
);
16625 else if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16626 len
= get_tv_number(&argvars
[2]);
16628 len
= slen
- n
; /* default len: all bytes that are available. */
16631 * Only return the overlap between the specified part and the actual
16643 else if (n
+ len
> slen
)
16646 rettv
->v_type
= VAR_STRING
;
16647 rettv
->vval
.v_string
= vim_strnsave(p
+ n
, len
);
16651 * "strridx()" function
16654 f_strridx(argvars
, rettv
)
16658 char_u buf
[NUMBUFLEN
];
16662 char_u
*lastmatch
= NULL
;
16663 int haystack_len
, end_idx
;
16665 needle
= get_tv_string_chk(&argvars
[1]);
16666 haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
16668 rettv
->vval
.v_number
= -1;
16669 if (needle
== NULL
|| haystack
== NULL
)
16670 return; /* type error; errmsg already given */
16672 haystack_len
= (int)STRLEN(haystack
);
16673 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16675 /* Third argument: upper limit for index */
16676 end_idx
= get_tv_number_chk(&argvars
[2], NULL
);
16678 return; /* can never find a match */
16681 end_idx
= haystack_len
;
16683 if (*needle
== NUL
)
16685 /* Empty string matches past the end. */
16686 lastmatch
= haystack
+ end_idx
;
16690 for (rest
= haystack
; *rest
!= '\0'; ++rest
)
16692 rest
= (char_u
*)strstr((char *)rest
, (char *)needle
);
16693 if (rest
== NULL
|| rest
> haystack
+ end_idx
)
16699 if (lastmatch
== NULL
)
16700 rettv
->vval
.v_number
= -1;
16702 rettv
->vval
.v_number
= (varnumber_T
)(lastmatch
- haystack
);
16706 * "strtrans()" function
16709 f_strtrans(argvars
, rettv
)
16713 rettv
->v_type
= VAR_STRING
;
16714 rettv
->vval
.v_string
= transstr(get_tv_string(&argvars
[0]));
16718 * "submatch()" function
16721 f_submatch(argvars
, rettv
)
16725 rettv
->v_type
= VAR_STRING
;
16726 rettv
->vval
.v_string
=
16727 reg_submatch((int)get_tv_number_chk(&argvars
[0], NULL
));
16731 * "substitute()" function
16734 f_substitute(argvars
, rettv
)
16738 char_u patbuf
[NUMBUFLEN
];
16739 char_u subbuf
[NUMBUFLEN
];
16740 char_u flagsbuf
[NUMBUFLEN
];
16742 char_u
*str
= get_tv_string_chk(&argvars
[0]);
16743 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
16744 char_u
*sub
= get_tv_string_buf_chk(&argvars
[2], subbuf
);
16745 char_u
*flg
= get_tv_string_buf_chk(&argvars
[3], flagsbuf
);
16747 rettv
->v_type
= VAR_STRING
;
16748 if (str
== NULL
|| pat
== NULL
|| sub
== NULL
|| flg
== NULL
)
16749 rettv
->vval
.v_string
= NULL
;
16751 rettv
->vval
.v_string
= do_string_sub(str
, pat
, sub
, flg
);
16755 * "synID(lnum, col, trans)" function
16758 f_synID(argvars
, rettv
)
16759 typval_T
*argvars UNUSED
;
16767 int transerr
= FALSE
;
16769 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
16770 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
16771 trans
= get_tv_number_chk(&argvars
[2], &transerr
);
16773 if (!transerr
&& lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
16774 && col
>= 0 && col
< (long)STRLEN(ml_get(lnum
)))
16775 id
= syn_get_id(curwin
, lnum
, (colnr_T
)col
, trans
, NULL
, FALSE
);
16778 rettv
->vval
.v_number
= id
;
16782 * "synIDattr(id, what [, mode])" function
16785 f_synIDattr(argvars
, rettv
)
16786 typval_T
*argvars UNUSED
;
16794 char_u modebuf
[NUMBUFLEN
];
16797 id
= get_tv_number(&argvars
[0]);
16798 what
= get_tv_string(&argvars
[1]);
16799 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16801 mode
= get_tv_string_buf(&argvars
[2], modebuf
);
16802 modec
= TOLOWER_ASC(mode
[0]);
16803 if (modec
!= 't' && modec
!= 'c'
16808 modec
= 0; /* replace invalid with current */
16824 switch (TOLOWER_ASC(what
[0]))
16827 if (TOLOWER_ASC(what
[1]) == 'g') /* bg[#] */
16828 p
= highlight_color(id
, what
, modec
);
16830 p
= highlight_has_attr(id
, HL_BOLD
, modec
);
16833 case 'f': /* fg[#] or font */
16834 p
= highlight_color(id
, what
, modec
);
16838 if (TOLOWER_ASC(what
[1]) == 'n') /* inverse */
16839 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
16841 p
= highlight_has_attr(id
, HL_ITALIC
, modec
);
16844 case 'n': /* name */
16845 p
= get_highlight_name(NULL
, id
- 1);
16848 case 'r': /* reverse */
16849 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
16853 if (TOLOWER_ASC(what
[1]) == 'p') /* sp[#] */
16854 p
= highlight_color(id
, what
, modec
);
16855 else /* standout */
16856 p
= highlight_has_attr(id
, HL_STANDOUT
, modec
);
16860 if (STRLEN(what
) <= 5 || TOLOWER_ASC(what
[5]) != 'c')
16862 p
= highlight_has_attr(id
, HL_UNDERLINE
, modec
);
16865 p
= highlight_has_attr(id
, HL_UNDERCURL
, modec
);
16870 p
= vim_strsave(p
);
16872 rettv
->v_type
= VAR_STRING
;
16873 rettv
->vval
.v_string
= p
;
16877 * "synIDtrans(id)" function
16880 f_synIDtrans(argvars
, rettv
)
16881 typval_T
*argvars UNUSED
;
16887 id
= get_tv_number(&argvars
[0]);
16890 id
= syn_get_final_id(id
);
16895 rettv
->vval
.v_number
= id
;
16899 * "synstack(lnum, col)" function
16902 f_synstack(argvars
, rettv
)
16903 typval_T
*argvars UNUSED
;
16913 rettv
->v_type
= VAR_LIST
;
16914 rettv
->vval
.v_list
= NULL
;
16917 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
16918 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
16920 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
16921 && col
>= 0 && (col
== 0 || col
< (long)STRLEN(ml_get(lnum
)))
16922 && rettv_list_alloc(rettv
) != FAIL
)
16924 (void)syn_get_id(curwin
, lnum
, (colnr_T
)col
, FALSE
, NULL
, TRUE
);
16927 id
= syn_get_stack_item(i
);
16930 if (list_append_number(rettv
->vval
.v_list
, id
) == FAIL
)
16938 * "system()" function
16941 f_system(argvars
, rettv
)
16945 char_u
*res
= NULL
;
16947 char_u
*infile
= NULL
;
16948 char_u buf
[NUMBUFLEN
];
16952 if (check_restricted() || check_secure())
16955 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16958 * Write the string to a temp file, to be used for input of the shell
16961 if ((infile
= vim_tempname('i')) == NULL
)
16967 fd
= mch_fopen((char *)infile
, WRITEBIN
);
16970 EMSG2(_(e_notopen
), infile
);
16973 p
= get_tv_string_buf_chk(&argvars
[1], buf
);
16977 goto done
; /* type error; errmsg already given */
16979 if (fwrite(p
, STRLEN(p
), 1, fd
) != 1)
16981 if (fclose(fd
) != 0)
16985 EMSG(_("E677: Error writing temp file"));
16990 res
= get_cmd_output(get_tv_string(&argvars
[0]), infile
,
16991 SHELL_SILENT
| SHELL_COOKED
);
16994 /* translate <CR> into <NL> */
16999 for (s
= res
; *s
; ++s
)
17007 /* translate <CR><NL> into <NL> */
17013 for (s
= res
; *s
; ++s
)
17015 if (s
[0] == CAR
&& s
[1] == NL
)
17025 if (infile
!= NULL
)
17027 mch_remove(infile
);
17030 rettv
->v_type
= VAR_STRING
;
17031 rettv
->vval
.v_string
= res
;
17035 * "tabpagebuflist()" function
17038 f_tabpagebuflist(argvars
, rettv
)
17039 typval_T
*argvars UNUSED
;
17040 typval_T
*rettv UNUSED
;
17042 #ifdef FEAT_WINDOWS
17046 if (argvars
[0].v_type
== VAR_UNKNOWN
)
17050 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
17052 wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
17054 if (wp
!= NULL
&& rettv_list_alloc(rettv
) != FAIL
)
17056 for (; wp
!= NULL
; wp
= wp
->w_next
)
17057 if (list_append_number(rettv
->vval
.v_list
,
17058 wp
->w_buffer
->b_fnum
) == FAIL
)
17066 * "tabpagenr()" function
17069 f_tabpagenr(argvars
, rettv
)
17070 typval_T
*argvars UNUSED
;
17074 #ifdef FEAT_WINDOWS
17077 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
17079 arg
= get_tv_string_chk(&argvars
[0]);
17083 if (STRCMP(arg
, "$") == 0)
17084 nr
= tabpage_index(NULL
) - 1;
17086 EMSG2(_(e_invexpr2
), arg
);
17090 nr
= tabpage_index(curtab
);
17092 rettv
->vval
.v_number
= nr
;
17096 #ifdef FEAT_WINDOWS
17097 static int get_winnr
__ARGS((tabpage_T
*tp
, typval_T
*argvar
));
17100 * Common code for tabpagewinnr() and winnr().
17103 get_winnr(tp
, argvar
)
17112 twin
= (tp
== curtab
) ? curwin
: tp
->tp_curwin
;
17113 if (argvar
->v_type
!= VAR_UNKNOWN
)
17115 arg
= get_tv_string_chk(argvar
);
17117 nr
= 0; /* type error; errmsg already given */
17118 else if (STRCMP(arg
, "$") == 0)
17119 twin
= (tp
== curtab
) ? lastwin
: tp
->tp_lastwin
;
17120 else if (STRCMP(arg
, "#") == 0)
17122 twin
= (tp
== curtab
) ? prevwin
: tp
->tp_prevwin
;
17128 EMSG2(_(e_invexpr2
), arg
);
17134 for (wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
17135 wp
!= twin
; wp
= wp
->w_next
)
17139 /* didn't find it in this tabpage */
17150 * "tabpagewinnr()" function
17153 f_tabpagewinnr(argvars
, rettv
)
17154 typval_T
*argvars UNUSED
;
17158 #ifdef FEAT_WINDOWS
17161 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
17165 nr
= get_winnr(tp
, &argvars
[1]);
17167 rettv
->vval
.v_number
= nr
;
17172 * "tagfiles()" function
17175 f_tagfiles(argvars
, rettv
)
17176 typval_T
*argvars UNUSED
;
17179 char_u fname
[MAXPATHL
+ 1];
17183 if (rettv_list_alloc(rettv
) == FAIL
)
17186 for (first
= TRUE
; ; first
= FALSE
)
17187 if (get_tagfname(&tn
, first
, fname
) == FAIL
17188 || list_append_string(rettv
->vval
.v_list
, fname
, -1) == FAIL
)
17194 * "taglist()" function
17197 f_taglist(argvars
, rettv
)
17201 char_u
*tag_pattern
;
17203 tag_pattern
= get_tv_string(&argvars
[0]);
17205 rettv
->vval
.v_number
= FALSE
;
17206 if (*tag_pattern
== NUL
)
17209 if (rettv_list_alloc(rettv
) == OK
)
17210 (void)get_tags(rettv
->vval
.v_list
, tag_pattern
);
17214 * "tempname()" function
17217 f_tempname(argvars
, rettv
)
17218 typval_T
*argvars UNUSED
;
17221 static int x
= 'A';
17223 rettv
->v_type
= VAR_STRING
;
17224 rettv
->vval
.v_string
= vim_tempname(x
);
17226 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17227 * names. Skip 'I' and 'O', they are used for shell redirection. */
17245 } while (x
== 'I' || x
== 'O');
17249 * "test(list)" function: Just checking the walls...
17252 f_test(argvars
, rettv
)
17253 typval_T
*argvars UNUSED
;
17254 typval_T
*rettv UNUSED
;
17256 /* Used for unit testing. Change the code below to your liking. */
17260 char_u
*bad
, *good
;
17262 if (argvars
[0].v_type
!= VAR_LIST
)
17264 l
= argvars
[0].vval
.v_list
;
17270 bad
= get_tv_string(&li
->li_tv
);
17274 good
= get_tv_string(&li
->li_tv
);
17275 rettv
->vval
.v_number
= test_edit_score(bad
, good
);
17280 * "tolower(string)" function
17283 f_tolower(argvars
, rettv
)
17289 p
= vim_strsave(get_tv_string(&argvars
[0]));
17290 rettv
->v_type
= VAR_STRING
;
17291 rettv
->vval
.v_string
= p
;
17303 c
= utf_ptr2char(p
);
17304 lc
= utf_tolower(c
);
17305 l
= utf_ptr2len(p
);
17306 /* TODO: reallocate string when byte count changes. */
17307 if (utf_char2len(lc
) == l
)
17308 utf_char2bytes(lc
, p
);
17311 else if (has_mbyte
&& (l
= (*mb_ptr2len
)(p
)) > 1)
17312 p
+= l
; /* skip multi-byte character */
17316 *p
= TOLOWER_LOC(*p
); /* note that tolower() can be a macro */
17323 * "toupper(string)" function
17326 f_toupper(argvars
, rettv
)
17330 rettv
->v_type
= VAR_STRING
;
17331 rettv
->vval
.v_string
= strup_save(get_tv_string(&argvars
[0]));
17335 * "tr(string, fromstr, tostr)" function
17338 f_tr(argvars
, rettv
)
17355 char_u buf
[NUMBUFLEN
];
17356 char_u buf2
[NUMBUFLEN
];
17359 instr
= get_tv_string(&argvars
[0]);
17360 fromstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
17361 tostr
= get_tv_string_buf_chk(&argvars
[2], buf2
);
17363 /* Default return value: empty string. */
17364 rettv
->v_type
= VAR_STRING
;
17365 rettv
->vval
.v_string
= NULL
;
17366 if (fromstr
== NULL
|| tostr
== NULL
)
17367 return; /* type error; errmsg already given */
17368 ga_init2(&ga
, (int)sizeof(char), 80);
17373 /* not multi-byte: fromstr and tostr must be the same length */
17374 if (STRLEN(fromstr
) != STRLEN(tostr
))
17379 EMSG2(_(e_invarg2
), fromstr
);
17384 /* fromstr and tostr have to contain the same number of chars */
17385 while (*instr
!= NUL
)
17390 inlen
= (*mb_ptr2len
)(instr
);
17394 for (p
= fromstr
; *p
!= NUL
; p
+= fromlen
)
17396 fromlen
= (*mb_ptr2len
)(p
);
17397 if (fromlen
== inlen
&& STRNCMP(instr
, p
, inlen
) == 0)
17399 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
17401 tolen
= (*mb_ptr2len
)(p
);
17409 if (*p
== NUL
) /* tostr is shorter than fromstr */
17416 if (first
&& cpstr
== instr
)
17418 /* Check that fromstr and tostr have the same number of
17419 * (multi-byte) characters. Done only once when a character
17420 * of instr doesn't appear in fromstr. */
17422 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
17424 tolen
= (*mb_ptr2len
)(p
);
17431 ga_grow(&ga
, cplen
);
17432 mch_memmove((char *)ga
.ga_data
+ ga
.ga_len
, cpstr
, (size_t)cplen
);
17433 ga
.ga_len
+= cplen
;
17440 /* When not using multi-byte chars we can do it faster. */
17441 p
= vim_strchr(fromstr
, *instr
);
17443 ga_append(&ga
, tostr
[p
- fromstr
]);
17445 ga_append(&ga
, *instr
);
17450 /* add a terminating NUL */
17452 ga_append(&ga
, NUL
);
17454 rettv
->vval
.v_string
= ga
.ga_data
;
17459 * "trunc({float})" function
17462 f_trunc(argvars
, rettv
)
17468 rettv
->v_type
= VAR_FLOAT
;
17469 if (get_float_arg(argvars
, &f
) == OK
)
17470 /* trunc() is not in C90, use floor() or ceil() instead. */
17471 rettv
->vval
.v_float
= f
> 0 ? floor(f
) : ceil(f
);
17473 rettv
->vval
.v_float
= 0.0;
17478 * "type(expr)" function
17481 f_type(argvars
, rettv
)
17487 switch (argvars
[0].v_type
)
17489 case VAR_NUMBER
: n
= 0; break;
17490 case VAR_STRING
: n
= 1; break;
17491 case VAR_FUNC
: n
= 2; break;
17492 case VAR_LIST
: n
= 3; break;
17493 case VAR_DICT
: n
= 4; break;
17495 case VAR_FLOAT
: n
= 5; break;
17497 default: EMSG2(_(e_intern2
), "f_type()"); n
= 0; break;
17499 rettv
->vval
.v_number
= n
;
17503 * "values(dict)" function
17506 f_values(argvars
, rettv
)
17510 dict_list(argvars
, rettv
, 1);
17514 * "virtcol(string)" function
17517 f_virtcol(argvars
, rettv
)
17523 int fnum
= curbuf
->b_fnum
;
17525 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
17526 if (fp
!= NULL
&& fp
->lnum
<= curbuf
->b_ml
.ml_line_count
17527 && fnum
== curbuf
->b_fnum
)
17529 getvvcol(curwin
, fp
, NULL
, NULL
, &vcol
);
17533 rettv
->vval
.v_number
= vcol
;
17537 * "visualmode()" function
17540 f_visualmode(argvars
, rettv
)
17541 typval_T
*argvars UNUSED
;
17542 typval_T
*rettv UNUSED
;
17547 rettv
->v_type
= VAR_STRING
;
17548 str
[0] = curbuf
->b_visual_mode_eval
;
17550 rettv
->vval
.v_string
= vim_strsave(str
);
17552 /* A non-zero number or non-empty string argument: reset mode. */
17553 if (non_zero_arg(&argvars
[0]))
17554 curbuf
->b_visual_mode_eval
= NUL
;
17559 * "winbufnr(nr)" function
17562 f_winbufnr(argvars
, rettv
)
17568 wp
= find_win_by_nr(&argvars
[0], NULL
);
17570 rettv
->vval
.v_number
= -1;
17572 rettv
->vval
.v_number
= wp
->w_buffer
->b_fnum
;
17576 * "wincol()" function
17579 f_wincol(argvars
, rettv
)
17580 typval_T
*argvars UNUSED
;
17584 rettv
->vval
.v_number
= curwin
->w_wcol
+ 1;
17588 * "winheight(nr)" function
17591 f_winheight(argvars
, rettv
)
17597 wp
= find_win_by_nr(&argvars
[0], NULL
);
17599 rettv
->vval
.v_number
= -1;
17601 rettv
->vval
.v_number
= wp
->w_height
;
17605 * "winline()" function
17608 f_winline(argvars
, rettv
)
17609 typval_T
*argvars UNUSED
;
17613 rettv
->vval
.v_number
= curwin
->w_wrow
+ 1;
17617 * "winnr()" function
17620 f_winnr(argvars
, rettv
)
17621 typval_T
*argvars UNUSED
;
17626 #ifdef FEAT_WINDOWS
17627 nr
= get_winnr(curtab
, &argvars
[0]);
17629 rettv
->vval
.v_number
= nr
;
17633 * "winrestcmd()" function
17636 f_winrestcmd(argvars
, rettv
)
17637 typval_T
*argvars UNUSED
;
17640 #ifdef FEAT_WINDOWS
17646 ga_init2(&ga
, (int)sizeof(char), 70);
17647 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
17649 sprintf((char *)buf
, "%dresize %d|", winnr
, wp
->w_height
);
17650 ga_concat(&ga
, buf
);
17651 # ifdef FEAT_VERTSPLIT
17652 sprintf((char *)buf
, "vert %dresize %d|", winnr
, wp
->w_width
);
17653 ga_concat(&ga
, buf
);
17657 ga_append(&ga
, NUL
);
17659 rettv
->vval
.v_string
= ga
.ga_data
;
17661 rettv
->vval
.v_string
= NULL
;
17663 rettv
->v_type
= VAR_STRING
;
17667 * "winrestview()" function
17670 f_winrestview(argvars
, rettv
)
17672 typval_T
*rettv UNUSED
;
17676 if (argvars
[0].v_type
!= VAR_DICT
17677 || (dict
= argvars
[0].vval
.v_dict
) == NULL
)
17681 curwin
->w_cursor
.lnum
= get_dict_number(dict
, (char_u
*)"lnum");
17682 curwin
->w_cursor
.col
= get_dict_number(dict
, (char_u
*)"col");
17683 #ifdef FEAT_VIRTUALEDIT
17684 curwin
->w_cursor
.coladd
= get_dict_number(dict
, (char_u
*)"coladd");
17686 curwin
->w_curswant
= get_dict_number(dict
, (char_u
*)"curswant");
17687 curwin
->w_set_curswant
= FALSE
;
17689 set_topline(curwin
, get_dict_number(dict
, (char_u
*)"topline"));
17691 curwin
->w_topfill
= get_dict_number(dict
, (char_u
*)"topfill");
17693 curwin
->w_leftcol
= get_dict_number(dict
, (char_u
*)"leftcol");
17694 curwin
->w_skipcol
= get_dict_number(dict
, (char_u
*)"skipcol");
17697 changed_cline_bef_curs();
17698 invalidate_botline();
17699 redraw_later(VALID
);
17701 if (curwin
->w_topline
== 0)
17702 curwin
->w_topline
= 1;
17703 if (curwin
->w_topline
> curbuf
->b_ml
.ml_line_count
)
17704 curwin
->w_topline
= curbuf
->b_ml
.ml_line_count
;
17706 check_topfill(curwin
, TRUE
);
17712 * "winsaveview()" function
17715 f_winsaveview(argvars
, rettv
)
17716 typval_T
*argvars UNUSED
;
17721 dict
= dict_alloc();
17724 rettv
->v_type
= VAR_DICT
;
17725 rettv
->vval
.v_dict
= dict
;
17726 ++dict
->dv_refcount
;
17728 dict_add_nr_str(dict
, "lnum", (long)curwin
->w_cursor
.lnum
, NULL
);
17729 dict_add_nr_str(dict
, "col", (long)curwin
->w_cursor
.col
, NULL
);
17730 #ifdef FEAT_VIRTUALEDIT
17731 dict_add_nr_str(dict
, "coladd", (long)curwin
->w_cursor
.coladd
, NULL
);
17734 dict_add_nr_str(dict
, "curswant", (long)curwin
->w_curswant
, NULL
);
17736 dict_add_nr_str(dict
, "topline", (long)curwin
->w_topline
, NULL
);
17738 dict_add_nr_str(dict
, "topfill", (long)curwin
->w_topfill
, NULL
);
17740 dict_add_nr_str(dict
, "leftcol", (long)curwin
->w_leftcol
, NULL
);
17741 dict_add_nr_str(dict
, "skipcol", (long)curwin
->w_skipcol
, NULL
);
17745 * "winwidth(nr)" function
17748 f_winwidth(argvars
, rettv
)
17754 wp
= find_win_by_nr(&argvars
[0], NULL
);
17756 rettv
->vval
.v_number
= -1;
17758 #ifdef FEAT_VERTSPLIT
17759 rettv
->vval
.v_number
= wp
->w_width
;
17761 rettv
->vval
.v_number
= Columns
;
17766 * "writefile()" function
17769 f_writefile(argvars
, rettv
)
17773 int binary
= FALSE
;
17781 if (check_restricted() || check_secure())
17784 if (argvars
[0].v_type
!= VAR_LIST
)
17786 EMSG2(_(e_listarg
), "writefile()");
17789 if (argvars
[0].vval
.v_list
== NULL
)
17792 if (argvars
[2].v_type
!= VAR_UNKNOWN
17793 && STRCMP(get_tv_string(&argvars
[2]), "b") == 0)
17796 /* Always open the file in binary mode, library functions have a mind of
17797 * their own about CR-LF conversion. */
17798 fname
= get_tv_string(&argvars
[1]);
17799 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, WRITEBIN
)) == NULL
)
17801 EMSG2(_(e_notcreate
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
17806 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
;
17809 for (s
= get_tv_string(&li
->li_tv
); *s
!= NUL
; ++s
)
17821 if (!binary
|| li
->li_next
!= NULL
)
17822 if (putc('\n', fd
) == EOF
)
17836 rettv
->vval
.v_number
= ret
;
17840 * Translate a String variable into a position.
17841 * Returns NULL when there is an error.
17844 var2fpos(varp
, dollar_lnum
, fnum
)
17846 int dollar_lnum
; /* TRUE when $ is last line */
17847 int *fnum
; /* set to fnum for '0, 'A, etc. */
17853 /* Argument can be [lnum, col, coladd]. */
17854 if (varp
->v_type
== VAR_LIST
)
17861 l
= varp
->vval
.v_list
;
17865 /* Get the line number */
17866 pos
.lnum
= list_find_nr(l
, 0L, &error
);
17867 if (error
|| pos
.lnum
<= 0 || pos
.lnum
> curbuf
->b_ml
.ml_line_count
)
17868 return NULL
; /* invalid line number */
17870 /* Get the column number */
17871 pos
.col
= list_find_nr(l
, 1L, &error
);
17874 len
= (long)STRLEN(ml_get(pos
.lnum
));
17876 /* We accept "$" for the column number: last column. */
17877 li
= list_find(l
, 1L);
17878 if (li
!= NULL
&& li
->li_tv
.v_type
== VAR_STRING
17879 && li
->li_tv
.vval
.v_string
!= NULL
17880 && STRCMP(li
->li_tv
.vval
.v_string
, "$") == 0)
17883 /* Accept a position up to the NUL after the line. */
17884 if (pos
.col
== 0 || (int)pos
.col
> len
+ 1)
17885 return NULL
; /* invalid column number */
17888 #ifdef FEAT_VIRTUALEDIT
17889 /* Get the virtual offset. Defaults to zero. */
17890 pos
.coladd
= list_find_nr(l
, 2L, &error
);
17898 name
= get_tv_string_chk(varp
);
17901 if (name
[0] == '.') /* cursor */
17902 return &curwin
->w_cursor
;
17904 if (name
[0] == 'v' && name
[1] == NUL
) /* Visual start */
17908 return &curwin
->w_cursor
;
17911 if (name
[0] == '\'') /* mark */
17913 pp
= getmark_fnum(name
[1], FALSE
, fnum
);
17914 if (pp
== NULL
|| pp
== (pos_T
*)-1 || pp
->lnum
<= 0)
17919 #ifdef FEAT_VIRTUALEDIT
17923 if (name
[0] == 'w' && dollar_lnum
)
17926 if (name
[1] == '0') /* "w0": first visible line */
17929 pos
.lnum
= curwin
->w_topline
;
17932 else if (name
[1] == '$') /* "w$": last visible line */
17934 validate_botline();
17935 pos
.lnum
= curwin
->w_botline
- 1;
17939 else if (name
[0] == '$') /* last column or line */
17943 pos
.lnum
= curbuf
->b_ml
.ml_line_count
;
17948 pos
.lnum
= curwin
->w_cursor
.lnum
;
17949 pos
.col
= (colnr_T
)STRLEN(ml_get_curline());
17957 * Convert list in "arg" into a position and optional file number.
17958 * When "fnump" is NULL there is no file number, only 3 items.
17959 * Note that the column is passed on as-is, the caller may want to decrement
17960 * it to use 1 for the first column.
17961 * Return FAIL when conversion is not possible, doesn't check the position for
17965 list2fpos(arg
, posp
, fnump
)
17970 list_T
*l
= arg
->vval
.v_list
;
17974 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17975 * when "fnump" isn't NULL and "coladd" is optional. */
17976 if (arg
->v_type
!= VAR_LIST
17978 || l
->lv_len
< (fnump
== NULL
? 2 : 3)
17979 || l
->lv_len
> (fnump
== NULL
? 3 : 4))
17984 n
= list_find_nr(l
, i
++, NULL
); /* fnum */
17988 n
= curbuf
->b_fnum
; /* current buffer */
17992 n
= list_find_nr(l
, i
++, NULL
); /* lnum */
17997 n
= list_find_nr(l
, i
++, NULL
); /* col */
18002 #ifdef FEAT_VIRTUALEDIT
18003 n
= list_find_nr(l
, i
, NULL
);
18014 * Get the length of an environment variable name.
18015 * Advance "arg" to the first character after the name.
18016 * Return 0 for error.
18025 for (p
= *arg
; vim_isIDc(*p
); ++p
)
18027 if (p
== *arg
) /* no name found */
18030 len
= (int)(p
- *arg
);
18036 * Get the length of the name of a function or internal variable.
18037 * "arg" is advanced to the first non-white character after the name.
18038 * Return 0 if something is wrong.
18047 /* Find the end of the name. */
18048 for (p
= *arg
; eval_isnamec(*p
); ++p
)
18050 if (p
== *arg
) /* no name found */
18053 len
= (int)(p
- *arg
);
18054 *arg
= skipwhite(p
);
18060 * Get the length of the name of a variable or function.
18061 * Only the name is recognized, does not handle ".key" or "[idx]".
18062 * "arg" is advanced to the first non-white character after the name.
18063 * Return -1 if curly braces expansion failed.
18064 * Return 0 if something else is wrong.
18065 * If the name contains 'magic' {}'s, expand them and return the
18066 * expanded name in an allocated string via 'alias' - caller must free.
18069 get_name_len(arg
, alias
, evaluate
, verbose
)
18077 char_u
*expr_start
;
18080 *alias
= NULL
; /* default to no alias */
18082 if ((*arg
)[0] == K_SPECIAL
&& (*arg
)[1] == KS_EXTRA
18083 && (*arg
)[2] == (int)KE_SNR
)
18085 /* hard coded <SNR>, already translated */
18087 return get_id_len(arg
) + 3;
18089 len
= eval_fname_script(*arg
);
18092 /* literal "<SID>", "s:" or "<SNR>" */
18097 * Find the end of the name; check for {} construction.
18099 p
= find_name_end(*arg
, &expr_start
, &expr_end
,
18100 len
> 0 ? 0 : FNE_CHECK_START
);
18101 if (expr_start
!= NULL
)
18103 char_u
*temp_string
;
18107 len
+= (int)(p
- *arg
);
18108 *arg
= skipwhite(p
);
18113 * Include any <SID> etc in the expanded string:
18114 * Thus the -len here.
18116 temp_string
= make_expanded_name(*arg
- len
, expr_start
, expr_end
, p
);
18117 if (temp_string
== NULL
)
18119 *alias
= temp_string
;
18120 *arg
= skipwhite(p
);
18121 return (int)STRLEN(temp_string
);
18124 len
+= get_id_len(arg
);
18125 if (len
== 0 && verbose
)
18126 EMSG2(_(e_invexpr2
), *arg
);
18132 * Find the end of a variable or function name, taking care of magic braces.
18133 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18134 * start and end of the first magic braces item.
18135 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
18136 * Return a pointer to just after the name. Equal to "arg" if there is no
18140 find_name_end(arg
, expr_start
, expr_end
, flags
)
18142 char_u
**expr_start
;
18150 if (expr_start
!= NULL
)
18152 *expr_start
= NULL
;
18156 /* Quick check for valid starting character. */
18157 if ((flags
& FNE_CHECK_START
) && !eval_isnamec1(*arg
) && *arg
!= '{')
18160 for (p
= arg
; *p
!= NUL
18161 && (eval_isnamec(*p
)
18163 || ((flags
& FNE_INCL_BR
) && (*p
== '[' || *p
== '.'))
18165 || br_nest
!= 0); mb_ptr_adv(p
))
18169 /* skip over 'string' to avoid counting [ and ] inside it. */
18170 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '\''; mb_ptr_adv(p
))
18175 else if (*p
== '"')
18177 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18178 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
18179 if (*p
== '\\' && p
[1] != NUL
)
18189 else if (*p
== ']')
18198 if (expr_start
!= NULL
&& *expr_start
== NULL
)
18201 else if (*p
== '}')
18204 if (expr_start
!= NULL
&& mb_nest
== 0 && *expr_end
== NULL
)
18214 * Expands out the 'magic' {}'s in a variable/function name.
18215 * Note that this can call itself recursively, to deal with
18216 * constructs like foo{bar}{baz}{bam}
18217 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18223 * Returns a new allocated string, which the caller must free.
18224 * Returns NULL for failure.
18227 make_expanded_name(in_start
, expr_start
, expr_end
, in_end
)
18229 char_u
*expr_start
;
18234 char_u
*retval
= NULL
;
18235 char_u
*temp_result
;
18236 char_u
*nextcmd
= NULL
;
18238 if (expr_end
== NULL
|| in_end
== NULL
)
18245 temp_result
= eval_to_string(expr_start
+ 1, &nextcmd
, FALSE
);
18246 if (temp_result
!= NULL
&& nextcmd
== NULL
)
18248 retval
= alloc((unsigned)(STRLEN(temp_result
) + (expr_start
- in_start
)
18249 + (in_end
- expr_end
) + 1));
18250 if (retval
!= NULL
)
18252 STRCPY(retval
, in_start
);
18253 STRCAT(retval
, temp_result
);
18254 STRCAT(retval
, expr_end
+ 1);
18257 vim_free(temp_result
);
18259 *in_end
= c1
; /* put char back for error messages */
18263 if (retval
!= NULL
)
18265 temp_result
= find_name_end(retval
, &expr_start
, &expr_end
, 0);
18266 if (expr_start
!= NULL
)
18268 /* Further expansion! */
18269 temp_result
= make_expanded_name(retval
, expr_start
,
18270 expr_end
, temp_result
);
18272 retval
= temp_result
;
18280 * Return TRUE if character "c" can be used in a variable or function name.
18281 * Does not include '{' or '}' for magic braces.
18287 return (ASCII_ISALNUM(c
) || c
== '_' || c
== ':' || c
== AUTOLOAD_CHAR
);
18291 * Return TRUE if character "c" can be used as the first character in a
18292 * variable or function name (excluding '{' and '}').
18298 return (ASCII_ISALPHA(c
) || c
== '_');
18302 * Set number v: variable to "val".
18305 set_vim_var_nr(idx
, val
)
18309 vimvars
[idx
].vv_nr
= val
;
18313 * Get number v: variable value.
18316 get_vim_var_nr(idx
)
18319 return vimvars
[idx
].vv_nr
;
18323 * Get string v: variable value. Uses a static buffer, can only be used once.
18326 get_vim_var_str(idx
)
18329 return get_tv_string(&vimvars
[idx
].vv_tv
);
18333 * Get List v: variable value. Caller must take care of reference count when
18337 get_vim_var_list(idx
)
18340 return vimvars
[idx
].vv_list
;
18344 * Set v:char to character "c".
18347 set_vim_var_char(c
)
18351 char_u buf
[MB_MAXBYTES
];
18358 buf
[(*mb_char2bytes
)(c
, buf
)] = NUL
;
18365 set_vim_var_string(VV_CHAR
, buf
, -1);
18369 * Set v:count to "count" and v:count1 to "count1".
18370 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
18373 set_vcount(count
, count1
, set_prevcount
)
18379 vimvars
[VV_PREVCOUNT
].vv_nr
= vimvars
[VV_COUNT
].vv_nr
;
18380 vimvars
[VV_COUNT
].vv_nr
= count
;
18381 vimvars
[VV_COUNT1
].vv_nr
= count1
;
18385 * Set string v: variable to a copy of "val".
18388 set_vim_var_string(idx
, val
, len
)
18391 int len
; /* length of "val" to use or -1 (whole string) */
18393 /* Need to do this (at least) once, since we can't initialize a union.
18394 * Will always be invoked when "v:progname" is set. */
18395 vimvars
[VV_VERSION
].vv_nr
= VIM_VERSION_100
;
18397 vim_free(vimvars
[idx
].vv_str
);
18399 vimvars
[idx
].vv_str
= NULL
;
18400 else if (len
== -1)
18401 vimvars
[idx
].vv_str
= vim_strsave(val
);
18403 vimvars
[idx
].vv_str
= vim_strnsave(val
, len
);
18407 * Set List v: variable to "val".
18410 set_vim_var_list(idx
, val
)
18414 list_unref(vimvars
[idx
].vv_list
);
18415 vimvars
[idx
].vv_list
= val
;
18417 ++val
->lv_refcount
;
18421 * Set v:register if needed.
18429 if (c
== 0 || c
== ' ')
18433 /* Avoid free/alloc when the value is already right. */
18434 if (vimvars
[VV_REG
].vv_str
== NULL
|| vimvars
[VV_REG
].vv_str
[0] != c
)
18435 set_vim_var_string(VV_REG
, ®name
, 1);
18439 * Get or set v:exception. If "oldval" == NULL, return the current value.
18440 * Otherwise, restore the value to "oldval" and return NULL.
18441 * Must always be called in pairs to save and restore v:exception! Does not
18442 * take care of memory allocations.
18445 v_exception(oldval
)
18448 if (oldval
== NULL
)
18449 return vimvars
[VV_EXCEPTION
].vv_str
;
18451 vimvars
[VV_EXCEPTION
].vv_str
= oldval
;
18456 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18457 * Otherwise, restore the value to "oldval" and return NULL.
18458 * Must always be called in pairs to save and restore v:throwpoint! Does not
18459 * take care of memory allocations.
18462 v_throwpoint(oldval
)
18465 if (oldval
== NULL
)
18466 return vimvars
[VV_THROWPOINT
].vv_str
;
18468 vimvars
[VV_THROWPOINT
].vv_str
= oldval
;
18472 #if defined(FEAT_AUTOCMD) || defined(PROTO)
18475 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18476 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18477 * Must always be called in pairs!
18480 set_cmdarg(eap
, oldarg
)
18488 oldval
= vimvars
[VV_CMDARG
].vv_str
;
18492 vimvars
[VV_CMDARG
].vv_str
= oldarg
;
18496 if (eap
->force_bin
== FORCE_BIN
)
18498 else if (eap
->force_bin
== FORCE_NOBIN
)
18503 if (eap
->read_edit
)
18506 if (eap
->force_ff
!= 0)
18507 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_ff
) + 6;
18509 if (eap
->force_enc
!= 0)
18510 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_enc
) + 7;
18511 if (eap
->bad_char
!= 0)
18512 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->bad_char
) + 7;
18515 newval
= alloc(len
+ 1);
18516 if (newval
== NULL
)
18519 if (eap
->force_bin
== FORCE_BIN
)
18520 sprintf((char *)newval
, " ++bin");
18521 else if (eap
->force_bin
== FORCE_NOBIN
)
18522 sprintf((char *)newval
, " ++nobin");
18526 if (eap
->read_edit
)
18527 STRCAT(newval
, " ++edit");
18529 if (eap
->force_ff
!= 0)
18530 sprintf((char *)newval
+ STRLEN(newval
), " ++ff=%s",
18531 eap
->cmd
+ eap
->force_ff
);
18533 if (eap
->force_enc
!= 0)
18534 sprintf((char *)newval
+ STRLEN(newval
), " ++enc=%s",
18535 eap
->cmd
+ eap
->force_enc
);
18536 if (eap
->bad_char
!= 0)
18537 sprintf((char *)newval
+ STRLEN(newval
), " ++bad=%s",
18538 eap
->cmd
+ eap
->bad_char
);
18540 vimvars
[VV_CMDARG
].vv_str
= newval
;
18546 * Get the value of internal variable "name".
18547 * Return OK or FAIL.
18550 get_var_tv(name
, len
, rettv
, verbose
)
18552 int len
; /* length of "name" */
18553 typval_T
*rettv
; /* NULL when only checking existence */
18554 int verbose
; /* may give error message */
18557 typval_T
*tv
= NULL
;
18562 /* truncate the name, so that we can use strcmp() */
18567 * Check for "b:changedtick".
18569 if (STRCMP(name
, "b:changedtick") == 0)
18571 atv
.v_type
= VAR_NUMBER
;
18572 atv
.vval
.v_number
= curbuf
->b_changedtick
;
18577 * Check for user-defined variables.
18581 v
= find_var(name
, NULL
);
18588 if (rettv
!= NULL
&& verbose
)
18589 EMSG2(_(e_undefvar
), name
);
18592 else if (rettv
!= NULL
)
18593 copy_tv(tv
, rettv
);
18601 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18602 * Also handle function call with Funcref variable: func(expr)
18603 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18606 handle_subscript(arg
, rettv
, evaluate
, verbose
)
18609 int evaluate
; /* do more than finding the end */
18610 int verbose
; /* give error messages */
18613 dict_T
*selfdict
= NULL
;
18620 || (**arg
== '.' && rettv
->v_type
== VAR_DICT
)
18621 || (**arg
== '(' && rettv
->v_type
== VAR_FUNC
))
18622 && !vim_iswhite(*(*arg
- 1)))
18626 /* need to copy the funcref so that we can clear rettv */
18628 rettv
->v_type
= VAR_UNKNOWN
;
18630 /* Invoke the function. Recursive! */
18631 s
= functv
.vval
.v_string
;
18632 ret
= get_func_tv(s
, (int)STRLEN(s
), rettv
, arg
,
18633 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
18634 &len
, evaluate
, selfdict
);
18636 /* Clear the funcref afterwards, so that deleting it while
18637 * evaluating the arguments is possible (see test55). */
18640 /* Stop the expression evaluation when immediately aborting on
18641 * error, or when an interrupt occurred or an exception was thrown
18642 * but not caught. */
18649 dict_unref(selfdict
);
18652 else /* **arg == '[' || **arg == '.' */
18654 dict_unref(selfdict
);
18655 if (rettv
->v_type
== VAR_DICT
)
18657 selfdict
= rettv
->vval
.v_dict
;
18658 if (selfdict
!= NULL
)
18659 ++selfdict
->dv_refcount
;
18663 if (eval_index(arg
, rettv
, evaluate
, verbose
) == FAIL
)
18670 dict_unref(selfdict
);
18675 * Allocate memory for a variable type-value, and make it empty (0 or NULL
18681 return (typval_T
*)alloc_clear((unsigned)sizeof(typval_T
));
18685 * Allocate memory for a variable type-value, and assign a string to it.
18686 * The string "s" must have been allocated, it is consumed.
18687 * Return NULL for out of memory, the variable otherwise.
18695 rettv
= alloc_tv();
18698 rettv
->v_type
= VAR_STRING
;
18699 rettv
->vval
.v_string
= s
;
18707 * Free the memory for a variable type-value.
18715 switch (varp
->v_type
)
18718 func_unref(varp
->vval
.v_string
);
18721 vim_free(varp
->vval
.v_string
);
18724 list_unref(varp
->vval
.v_list
);
18727 dict_unref(varp
->vval
.v_dict
);
18736 EMSG2(_(e_intern2
), "free_tv()");
18744 * Free the memory for a variable value and set the value to NULL or 0.
18752 switch (varp
->v_type
)
18755 func_unref(varp
->vval
.v_string
);
18758 vim_free(varp
->vval
.v_string
);
18759 varp
->vval
.v_string
= NULL
;
18762 list_unref(varp
->vval
.v_list
);
18763 varp
->vval
.v_list
= NULL
;
18766 dict_unref(varp
->vval
.v_dict
);
18767 varp
->vval
.v_dict
= NULL
;
18770 varp
->vval
.v_number
= 0;
18774 varp
->vval
.v_float
= 0.0;
18780 EMSG2(_(e_intern2
), "clear_tv()");
18787 * Set the value of a variable to NULL without freeing items.
18794 vim_memset(varp
, 0, sizeof(typval_T
));
18798 * Get the number value of a variable.
18799 * If it is a String variable, uses vim_str2nr().
18800 * For incompatible types, return 0.
18801 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18802 * caller of incompatible types: it sets *denote to TRUE if "denote"
18803 * is not NULL or returns -1 otherwise.
18806 get_tv_number(varp
)
18811 return get_tv_number_chk(varp
, &error
); /* return 0L on error */
18815 get_tv_number_chk(varp
, denote
)
18821 switch (varp
->v_type
)
18824 return (long)(varp
->vval
.v_number
);
18827 EMSG(_("E805: Using a Float as a Number"));
18831 EMSG(_("E703: Using a Funcref as a Number"));
18834 if (varp
->vval
.v_string
!= NULL
)
18835 vim_str2nr(varp
->vval
.v_string
, NULL
, NULL
,
18836 TRUE
, TRUE
, &n
, NULL
);
18839 EMSG(_("E745: Using a List as a Number"));
18842 EMSG(_("E728: Using a Dictionary as a Number"));
18845 EMSG2(_(e_intern2
), "get_tv_number()");
18848 if (denote
== NULL
) /* useful for values that must be unsigned */
18856 * Get the lnum from the first argument.
18857 * Also accepts ".", "$", etc., but that only works for the current buffer.
18858 * Returns -1 on error.
18861 get_tv_lnum(argvars
)
18867 lnum
= get_tv_number_chk(&argvars
[0], NULL
);
18868 if (lnum
== 0) /* no valid number, try using line() */
18870 rettv
.v_type
= VAR_NUMBER
;
18871 f_line(argvars
, &rettv
);
18872 lnum
= rettv
.vval
.v_number
;
18879 * Get the lnum from the first argument.
18880 * Also accepts "$", then "buf" is used.
18881 * Returns 0 on error.
18884 get_tv_lnum_buf(argvars
, buf
)
18888 if (argvars
[0].v_type
== VAR_STRING
18889 && argvars
[0].vval
.v_string
!= NULL
18890 && argvars
[0].vval
.v_string
[0] == '$'
18892 return buf
->b_ml
.ml_line_count
;
18893 return get_tv_number_chk(&argvars
[0], NULL
);
18897 * Get the string value of a variable.
18898 * If it is a Number variable, the number is converted into a string.
18899 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18900 * get_tv_string_buf() uses a given buffer.
18901 * If the String variable has never been set, return an empty string.
18902 * Never returns NULL;
18903 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18907 get_tv_string(varp
)
18910 static char_u mybuf
[NUMBUFLEN
];
18912 return get_tv_string_buf(varp
, mybuf
);
18916 get_tv_string_buf(varp
, buf
)
18920 char_u
*res
= get_tv_string_buf_chk(varp
, buf
);
18922 return res
!= NULL
? res
: (char_u
*)"";
18926 get_tv_string_chk(varp
)
18929 static char_u mybuf
[NUMBUFLEN
];
18931 return get_tv_string_buf_chk(varp
, mybuf
);
18935 get_tv_string_buf_chk(varp
, buf
)
18939 switch (varp
->v_type
)
18942 sprintf((char *)buf
, "%ld", (long)varp
->vval
.v_number
);
18945 EMSG(_("E729: using Funcref as a String"));
18948 EMSG(_("E730: using List as a String"));
18951 EMSG(_("E731: using Dictionary as a String"));
18955 EMSG(_("E806: using Float as a String"));
18959 if (varp
->vval
.v_string
!= NULL
)
18960 return varp
->vval
.v_string
;
18961 return (char_u
*)"";
18963 EMSG2(_(e_intern2
), "get_tv_string_buf()");
18970 * Find variable "name" in the list of variables.
18971 * Return a pointer to it if found, NULL if not found.
18972 * Careful: "a:0" variables don't have a name.
18973 * When "htp" is not NULL we are writing to the variable, set "htp" to the
18976 static dictitem_T
*
18977 find_var(name
, htp
)
18984 ht
= find_var_ht(name
, &varname
);
18989 return find_var_in_ht(ht
, varname
, htp
!= NULL
);
18993 * Find variable "varname" in hashtab "ht".
18994 * Returns NULL if not found.
18996 static dictitem_T
*
18997 find_var_in_ht(ht
, varname
, writing
)
19004 if (*varname
== NUL
)
19006 /* Must be something like "s:", otherwise "ht" would be NULL. */
19007 switch (varname
[-2])
19009 case 's': return &SCRIPT_SV(current_SID
).sv_var
;
19010 case 'g': return &globvars_var
;
19011 case 'v': return &vimvars_var
;
19012 case 'b': return &curbuf
->b_bufvar
;
19013 case 'w': return &curwin
->w_winvar
;
19014 #ifdef FEAT_WINDOWS
19015 case 't': return &curtab
->tp_winvar
;
19017 case 'l': return current_funccal
== NULL
19018 ? NULL
: ¤t_funccal
->l_vars_var
;
19019 case 'a': return current_funccal
== NULL
19020 ? NULL
: ¤t_funccal
->l_avars_var
;
19025 hi
= hash_find(ht
, varname
);
19026 if (HASHITEM_EMPTY(hi
))
19028 /* For global variables we may try auto-loading the script. If it
19029 * worked find the variable again. Don't auto-load a script if it was
19030 * loaded already, otherwise it would be loaded every time when
19031 * checking if a function name is a Funcref variable. */
19032 if (ht
== &globvarht
&& !writing
19033 && script_autoload(varname
, FALSE
) && !aborting())
19034 hi
= hash_find(ht
, varname
);
19035 if (HASHITEM_EMPTY(hi
))
19042 * Find the hashtab used for a variable name.
19043 * Set "varname" to the start of name without ':'.
19046 find_var_ht(name
, varname
)
19052 if (name
[1] != ':')
19054 /* The name must not start with a colon or #. */
19055 if (name
[0] == ':' || name
[0] == AUTOLOAD_CHAR
)
19059 /* "version" is "v:version" in all scopes */
19060 hi
= hash_find(&compat_hashtab
, name
);
19061 if (!HASHITEM_EMPTY(hi
))
19062 return &compat_hashtab
;
19064 if (current_funccal
== NULL
)
19065 return &globvarht
; /* global variable */
19066 return ¤t_funccal
->l_vars
.dv_hashtab
; /* l: variable */
19068 *varname
= name
+ 2;
19069 if (*name
== 'g') /* global variable */
19071 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19073 if (vim_strchr(name
+ 2, ':') != NULL
19074 || vim_strchr(name
+ 2, AUTOLOAD_CHAR
) != NULL
)
19076 if (*name
== 'b') /* buffer variable */
19077 return &curbuf
->b_vars
.dv_hashtab
;
19078 if (*name
== 'w') /* window variable */
19079 return &curwin
->w_vars
.dv_hashtab
;
19080 #ifdef FEAT_WINDOWS
19081 if (*name
== 't') /* tab page variable */
19082 return &curtab
->tp_vars
.dv_hashtab
;
19084 if (*name
== 'v') /* v: variable */
19086 if (*name
== 'a' && current_funccal
!= NULL
) /* function argument */
19087 return ¤t_funccal
->l_avars
.dv_hashtab
;
19088 if (*name
== 'l' && current_funccal
!= NULL
) /* local function variable */
19089 return ¤t_funccal
->l_vars
.dv_hashtab
;
19090 if (*name
== 's' /* script variable */
19091 && current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
19092 return &SCRIPT_VARS(current_SID
);
19097 * Get the string value of a (global/local) variable.
19098 * Returns NULL when it doesn't exist.
19101 get_var_value(name
)
19106 v
= find_var(name
, NULL
);
19109 return get_tv_string(&v
->di_tv
);
19113 * Allocate a new hashtab for a sourced script. It will be used while
19114 * sourcing this script and when executing functions defined in the script.
19117 new_script_vars(id
)
19124 if (ga_grow(&ga_scripts
, (int)(id
- ga_scripts
.ga_len
)) == OK
)
19126 /* Re-allocating ga_data means that an ht_array pointing to
19127 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
19128 * at its init value. Also reset "v_dict", it's always the same. */
19129 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
19131 ht
= &SCRIPT_VARS(i
);
19132 if (ht
->ht_mask
== HT_INIT_SIZE
- 1)
19133 ht
->ht_array
= ht
->ht_smallarray
;
19134 sv
= &SCRIPT_SV(i
);
19135 sv
->sv_var
.di_tv
.vval
.v_dict
= &sv
->sv_dict
;
19138 while (ga_scripts
.ga_len
< id
)
19140 sv
= &SCRIPT_SV(ga_scripts
.ga_len
+ 1);
19141 init_var_dict(&sv
->sv_dict
, &sv
->sv_var
);
19142 ++ga_scripts
.ga_len
;
19148 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19152 init_var_dict(dict
, dict_var
)
19154 dictitem_T
*dict_var
;
19156 hash_init(&dict
->dv_hashtab
);
19157 dict
->dv_refcount
= DO_NOT_FREE_CNT
;
19158 dict
->dv_copyID
= 0;
19159 dict_var
->di_tv
.vval
.v_dict
= dict
;
19160 dict_var
->di_tv
.v_type
= VAR_DICT
;
19161 dict_var
->di_tv
.v_lock
= VAR_FIXED
;
19162 dict_var
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
19163 dict_var
->di_key
[0] = NUL
;
19167 * Clean up a list of internal variables.
19168 * Frees all allocated variables and the value they contain.
19169 * Clears hashtab "ht", does not free it.
19175 vars_clear_ext(ht
, TRUE
);
19179 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19182 vars_clear_ext(ht
, free_val
)
19191 todo
= (int)ht
->ht_used
;
19192 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
19194 if (!HASHITEM_EMPTY(hi
))
19198 /* Free the variable. Don't remove it from the hashtab,
19199 * ht_array might change then. hash_clear() takes care of it
19203 clear_tv(&v
->di_tv
);
19204 if ((v
->di_flags
& DI_FLAGS_FIX
) == 0)
19213 * Delete a variable from hashtab "ht" at item "hi".
19214 * Clear the variable value and free the dictitem.
19221 dictitem_T
*di
= HI2DI(hi
);
19223 hash_remove(ht
, hi
);
19224 clear_tv(&di
->di_tv
);
19229 * List the value of one internal variable.
19232 list_one_var(v
, prefix
, first
)
19239 char_u numbuf
[NUMBUFLEN
];
19241 current_copyID
+= COPYID_INC
;
19242 s
= echo_string(&v
->di_tv
, &tofree
, numbuf
, current_copyID
);
19243 list_one_var_a(prefix
, v
->di_key
, v
->di_tv
.v_type
,
19244 s
== NULL
? (char_u
*)"" : s
, first
);
19249 list_one_var_a(prefix
, name
, type
, string
, first
)
19254 int *first
; /* when TRUE clear rest of screen and set to FALSE */
19256 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19259 if (name
!= NULL
) /* "a:" vars don't have a name stored */
19263 if (type
== VAR_NUMBER
)
19265 else if (type
== VAR_FUNC
)
19267 else if (type
== VAR_LIST
)
19270 if (*string
== '[')
19273 else if (type
== VAR_DICT
)
19276 if (*string
== '{')
19282 msg_outtrans(string
);
19284 if (type
== VAR_FUNC
)
19285 msg_puts((char_u
*)"()");
19294 * Set variable "name" to value in "tv".
19295 * If the variable already exists, the value is updated.
19296 * Otherwise the variable is created.
19299 set_var(name
, tv
, copy
)
19302 int copy
; /* make copy of value in "tv" */
19309 ht
= find_var_ht(name
, &varname
);
19310 if (ht
== NULL
|| *varname
== NUL
)
19312 EMSG2(_(e_illvar
), name
);
19315 v
= find_var_in_ht(ht
, varname
, TRUE
);
19317 if (tv
->v_type
== VAR_FUNC
)
19319 if (!(vim_strchr((char_u
*)"wbs", name
[0]) != NULL
&& name
[1] == ':')
19320 && !ASCII_ISUPPER((name
[0] != NUL
&& name
[1] == ':')
19321 ? name
[2] : name
[0]))
19323 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name
);
19326 /* Don't allow hiding a function. When "v" is not NULL we migth be
19327 * assigning another function to the same var, the type is checked
19329 if (v
== NULL
&& function_exists(name
))
19331 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
19339 /* existing variable, need to clear the value */
19340 if (var_check_ro(v
->di_flags
, name
)
19341 || tv_check_lock(v
->di_tv
.v_lock
, name
))
19343 if (v
->di_tv
.v_type
!= tv
->v_type
19344 && !((v
->di_tv
.v_type
== VAR_STRING
19345 || v
->di_tv
.v_type
== VAR_NUMBER
)
19346 && (tv
->v_type
== VAR_STRING
19347 || tv
->v_type
== VAR_NUMBER
))
19349 && !((v
->di_tv
.v_type
== VAR_NUMBER
19350 || v
->di_tv
.v_type
== VAR_FLOAT
)
19351 && (tv
->v_type
== VAR_NUMBER
19352 || tv
->v_type
== VAR_FLOAT
))
19356 EMSG2(_("E706: Variable type mismatch for: %s"), name
);
19361 * Handle setting internal v: variables separately: we don't change
19364 if (ht
== &vimvarht
)
19366 if (v
->di_tv
.v_type
== VAR_STRING
)
19368 vim_free(v
->di_tv
.vval
.v_string
);
19369 if (copy
|| tv
->v_type
!= VAR_STRING
)
19370 v
->di_tv
.vval
.v_string
= vim_strsave(get_tv_string(tv
));
19373 /* Take over the string to avoid an extra alloc/free. */
19374 v
->di_tv
.vval
.v_string
= tv
->vval
.v_string
;
19375 tv
->vval
.v_string
= NULL
;
19378 else if (v
->di_tv
.v_type
!= VAR_NUMBER
)
19379 EMSG2(_(e_intern2
), "set_var()");
19382 v
->di_tv
.vval
.v_number
= get_tv_number(tv
);
19383 if (STRCMP(varname
, "searchforward") == 0)
19384 set_search_direction(v
->di_tv
.vval
.v_number
? '/' : '?');
19389 clear_tv(&v
->di_tv
);
19391 else /* add a new variable */
19393 /* Can't add "v:" variable. */
19394 if (ht
== &vimvarht
)
19396 EMSG2(_(e_illvar
), name
);
19400 /* Make sure the variable name is valid. */
19401 for (p
= varname
; *p
!= NUL
; ++p
)
19402 if (!eval_isnamec1(*p
) && (p
== varname
|| !VIM_ISDIGIT(*p
))
19403 && *p
!= AUTOLOAD_CHAR
)
19405 EMSG2(_(e_illvar
), varname
);
19409 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
19410 + STRLEN(varname
)));
19413 STRCPY(v
->di_key
, varname
);
19414 if (hash_add(ht
, DI2HIKEY(v
)) == FAIL
)
19422 if (copy
|| tv
->v_type
== VAR_NUMBER
|| tv
->v_type
== VAR_FLOAT
)
19423 copy_tv(tv
, &v
->di_tv
);
19427 v
->di_tv
.v_lock
= 0;
19433 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
19434 * Also give an error message.
19437 var_check_ro(flags
, name
)
19441 if (flags
& DI_FLAGS_RO
)
19443 EMSG2(_(e_readonlyvar
), name
);
19446 if ((flags
& DI_FLAGS_RO_SBX
) && sandbox
)
19448 EMSG2(_(e_readonlysbx
), name
);
19455 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19456 * Also give an error message.
19459 var_check_fixed(flags
, name
)
19463 if (flags
& DI_FLAGS_FIX
)
19465 EMSG2(_("E795: Cannot delete variable %s"), name
);
19472 * Return TRUE if typeval "tv" is set to be locked (immutable).
19473 * Also give an error message, using "name".
19476 tv_check_lock(lock
, name
)
19480 if (lock
& VAR_LOCKED
)
19482 EMSG2(_("E741: Value is locked: %s"),
19483 name
== NULL
? (char_u
*)_("Unknown") : name
);
19486 if (lock
& VAR_FIXED
)
19488 EMSG2(_("E742: Cannot change value of %s"),
19489 name
== NULL
? (char_u
*)_("Unknown") : name
);
19496 * Copy the values from typval_T "from" to typval_T "to".
19497 * When needed allocates string or increases reference count.
19498 * Does not make a copy of a list or dict but copies the reference!
19499 * It is OK for "from" and "to" to point to the same item. This is used to
19500 * make a copy later.
19507 to
->v_type
= from
->v_type
;
19509 switch (from
->v_type
)
19512 to
->vval
.v_number
= from
->vval
.v_number
;
19516 to
->vval
.v_float
= from
->vval
.v_float
;
19521 if (from
->vval
.v_string
== NULL
)
19522 to
->vval
.v_string
= NULL
;
19525 to
->vval
.v_string
= vim_strsave(from
->vval
.v_string
);
19526 if (from
->v_type
== VAR_FUNC
)
19527 func_ref(to
->vval
.v_string
);
19531 if (from
->vval
.v_list
== NULL
)
19532 to
->vval
.v_list
= NULL
;
19535 to
->vval
.v_list
= from
->vval
.v_list
;
19536 ++to
->vval
.v_list
->lv_refcount
;
19540 if (from
->vval
.v_dict
== NULL
)
19541 to
->vval
.v_dict
= NULL
;
19544 to
->vval
.v_dict
= from
->vval
.v_dict
;
19545 ++to
->vval
.v_dict
->dv_refcount
;
19549 EMSG2(_(e_intern2
), "copy_tv()");
19555 * Make a copy of an item.
19556 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
19557 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19558 * reference to an already copied list/dict can be used.
19559 * Returns FAIL or OK.
19562 item_copy(from
, to
, deep
, copyID
)
19568 static int recurse
= 0;
19571 if (recurse
>= DICT_MAXNEST
)
19573 EMSG(_("E698: variable nested too deep for making a copy"));
19578 switch (from
->v_type
)
19589 to
->v_type
= VAR_LIST
;
19591 if (from
->vval
.v_list
== NULL
)
19592 to
->vval
.v_list
= NULL
;
19593 else if (copyID
!= 0 && from
->vval
.v_list
->lv_copyID
== copyID
)
19595 /* use the copy made earlier */
19596 to
->vval
.v_list
= from
->vval
.v_list
->lv_copylist
;
19597 ++to
->vval
.v_list
->lv_refcount
;
19600 to
->vval
.v_list
= list_copy(from
->vval
.v_list
, deep
, copyID
);
19601 if (to
->vval
.v_list
== NULL
)
19605 to
->v_type
= VAR_DICT
;
19607 if (from
->vval
.v_dict
== NULL
)
19608 to
->vval
.v_dict
= NULL
;
19609 else if (copyID
!= 0 && from
->vval
.v_dict
->dv_copyID
== copyID
)
19611 /* use the copy made earlier */
19612 to
->vval
.v_dict
= from
->vval
.v_dict
->dv_copydict
;
19613 ++to
->vval
.v_dict
->dv_refcount
;
19616 to
->vval
.v_dict
= dict_copy(from
->vval
.v_dict
, deep
, copyID
);
19617 if (to
->vval
.v_dict
== NULL
)
19621 EMSG2(_(e_intern2
), "item_copy()");
19629 * ":echo expr1 ..." print each argument separated with a space, add a
19630 * newline at the end.
19631 * ":echon expr1 ..." print each argument plain.
19637 char_u
*arg
= eap
->arg
;
19641 int needclr
= TRUE
;
19642 int atstart
= TRUE
;
19643 char_u numbuf
[NUMBUFLEN
];
19647 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n' && !got_int
)
19649 /* If eval1() causes an error message the text from the command may
19650 * still need to be cleared. E.g., "echo 22,44". */
19651 need_clr_eos
= needclr
;
19654 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
19657 * Report the invalid expression unless the expression evaluation
19658 * has been cancelled due to an aborting error, an interrupt, or an
19662 EMSG2(_(e_invexpr2
), p
);
19663 need_clr_eos
= FALSE
;
19666 need_clr_eos
= FALSE
;
19673 /* Call msg_start() after eval1(), evaluating the expression
19674 * may cause a message to appear. */
19675 if (eap
->cmdidx
== CMD_echo
)
19678 else if (eap
->cmdidx
== CMD_echo
)
19679 msg_puts_attr((char_u
*)" ", echo_attr
);
19680 current_copyID
+= COPYID_INC
;
19681 p
= echo_string(&rettv
, &tofree
, numbuf
, current_copyID
);
19683 for ( ; *p
!= NUL
&& !got_int
; ++p
)
19685 if (*p
== '\n' || *p
== '\r' || *p
== TAB
)
19687 if (*p
!= TAB
&& needclr
)
19689 /* remove any text still there from the command */
19693 msg_putchar_attr(*p
, echo_attr
);
19700 int i
= (*mb_ptr2len
)(p
);
19702 (void)msg_outtrans_len_attr(p
, i
, echo_attr
);
19707 (void)msg_outtrans_len_attr(p
, 1, echo_attr
);
19713 arg
= skipwhite(arg
);
19715 eap
->nextcmd
= check_nextcmd(arg
);
19721 /* remove text that may still be there from the command */
19724 if (eap
->cmdidx
== CMD_echo
)
19730 * ":echohl {name}".
19738 id
= syn_name2id(eap
->arg
);
19742 echo_attr
= syn_id2attr(id
);
19746 * ":execute expr1 ..." execute the result of an expression.
19747 * ":echomsg expr1 ..." Print a message
19748 * ":echoerr expr1 ..." Print an error
19749 * Each gets spaces around each argument and a newline at the end for
19756 char_u
*arg
= eap
->arg
;
19764 ga_init2(&ga
, 1, 80);
19768 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
19771 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
19774 * Report the invalid expression unless the expression evaluation
19775 * has been cancelled due to an aborting error, an interrupt, or an
19779 EMSG2(_(e_invexpr2
), p
);
19786 p
= get_tv_string(&rettv
);
19787 len
= (int)STRLEN(p
);
19788 if (ga_grow(&ga
, len
+ 2) == FAIL
)
19795 ((char_u
*)(ga
.ga_data
))[ga
.ga_len
++] = ' ';
19796 STRCPY((char_u
*)(ga
.ga_data
) + ga
.ga_len
, p
);
19801 arg
= skipwhite(arg
);
19804 if (ret
!= FAIL
&& ga
.ga_data
!= NULL
)
19806 if (eap
->cmdidx
== CMD_echomsg
)
19808 MSG_ATTR(ga
.ga_data
, echo_attr
);
19811 else if (eap
->cmdidx
== CMD_echoerr
)
19813 /* We don't want to abort following commands, restore did_emsg. */
19814 save_did_emsg
= did_emsg
;
19815 EMSG((char_u
*)ga
.ga_data
);
19817 did_emsg
= save_did_emsg
;
19819 else if (eap
->cmdidx
== CMD_execute
)
19820 do_cmdline((char_u
*)ga
.ga_data
,
19821 eap
->getline
, eap
->cookie
, DOCMD_NOWAIT
|DOCMD_VERBOSE
);
19829 eap
->nextcmd
= check_nextcmd(arg
);
19833 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19834 * "arg" points to the "&" or '+' when called, to "option" when returning.
19835 * Returns NULL when no option name found. Otherwise pointer to the char
19836 * after the option name.
19839 find_option_end(arg
, opt_flags
)
19846 if (*p
== 'g' && p
[1] == ':')
19848 *opt_flags
= OPT_GLOBAL
;
19851 else if (*p
== 'l' && p
[1] == ':')
19853 *opt_flags
= OPT_LOCAL
;
19859 if (!ASCII_ISALPHA(*p
))
19863 if (p
[0] == 't' && p
[1] == '_' && p
[2] != NUL
&& p
[3] != NUL
)
19864 p
+= 4; /* termcap option */
19866 while (ASCII_ISALPHA(*p
))
19881 int saved_did_emsg
;
19882 char_u
*name
= NULL
;
19885 char_u
*line_arg
= NULL
;
19888 int varargs
= FALSE
;
19889 int mustend
= FALSE
;
19894 char_u
*skip_until
= NULL
;
19897 static int func_nr
= 0; /* number for nameless function */
19902 int sourcing_lnum_off
;
19905 * ":function" without argument: list functions.
19907 if (ends_excmd(*eap
->arg
))
19911 todo
= (int)func_hashtab
.ht_used
;
19912 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
19914 if (!HASHITEM_EMPTY(hi
))
19918 if (!isdigit(*fp
->uf_name
))
19919 list_func_head(fp
, FALSE
);
19923 eap
->nextcmd
= check_nextcmd(eap
->arg
);
19928 * ":function /pat": list functions matching pattern.
19930 if (*eap
->arg
== '/')
19932 p
= skip_regexp(eap
->arg
+ 1, '/', TRUE
, NULL
);
19935 regmatch_T regmatch
;
19939 regmatch
.regprog
= vim_regcomp(eap
->arg
+ 1, RE_MAGIC
);
19941 if (regmatch
.regprog
!= NULL
)
19943 regmatch
.rm_ic
= p_ic
;
19945 todo
= (int)func_hashtab
.ht_used
;
19946 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
19948 if (!HASHITEM_EMPTY(hi
))
19952 if (!isdigit(*fp
->uf_name
)
19953 && vim_regexec(®match
, fp
->uf_name
, 0))
19954 list_func_head(fp
, FALSE
);
19957 vim_free(regmatch
.regprog
);
19962 eap
->nextcmd
= check_nextcmd(p
);
19967 * Get the function name. There are these situations:
19968 * func normal function name
19969 * "name" == func, "fudi.fd_dict" == NULL
19970 * dict.func new dictionary entry
19971 * "name" == NULL, "fudi.fd_dict" set,
19972 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19973 * dict.func existing dict entry with a Funcref
19974 * "name" == func, "fudi.fd_dict" set,
19975 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19976 * dict.func existing dict entry that's not a Funcref
19977 * "name" == NULL, "fudi.fd_dict" set,
19978 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19981 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
19982 paren
= (vim_strchr(p
, '(') != NULL
);
19983 if (name
== NULL
&& (fudi
.fd_dict
== NULL
|| !paren
) && !eap
->skip
)
19986 * Return on an invalid expression in braces, unless the expression
19987 * evaluation has been cancelled due to an aborting error, an
19988 * interrupt, or an exception.
19992 if (!eap
->skip
&& fudi
.fd_newkey
!= NULL
)
19993 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
19994 vim_free(fudi
.fd_newkey
);
20001 /* An error in a function call during evaluation of an expression in magic
20002 * braces should not cause the function not to be defined. */
20003 saved_did_emsg
= did_emsg
;
20007 * ":function func" with only function name: list function.
20011 if (!ends_excmd(*skipwhite(p
)))
20013 EMSG(_(e_trailing
));
20016 eap
->nextcmd
= check_nextcmd(p
);
20017 if (eap
->nextcmd
!= NULL
)
20019 if (!eap
->skip
&& !got_int
)
20021 fp
= find_func(name
);
20024 list_func_head(fp
, TRUE
);
20025 for (j
= 0; j
< fp
->uf_lines
.ga_len
&& !got_int
; ++j
)
20027 if (FUNCLINE(fp
, j
) == NULL
)
20030 msg_outnum((long)(j
+ 1));
20035 msg_prt_line(FUNCLINE(fp
, j
), FALSE
);
20036 out_flush(); /* show a line at a time */
20042 msg_puts((char_u
*)" endfunction");
20046 emsg_funcname(N_("E123: Undefined function: %s"), name
);
20052 * ":function name(arg1, arg2)" Define function.
20059 EMSG2(_("E124: Missing '(': %s"), eap
->arg
);
20062 /* attempt to continue by skipping some text */
20063 if (vim_strchr(p
, '(') != NULL
)
20064 p
= vim_strchr(p
, '(');
20066 p
= skipwhite(p
+ 1);
20068 ga_init2(&newargs
, (int)sizeof(char_u
*), 3);
20069 ga_init2(&newlines
, (int)sizeof(char_u
*), 3);
20073 /* Check the name of the function. Unless it's a dictionary function
20074 * (that we are overwriting). */
20078 arg
= fudi
.fd_newkey
;
20079 if (arg
!= NULL
&& (fudi
.fd_di
== NULL
20080 || fudi
.fd_di
->di_tv
.v_type
!= VAR_FUNC
))
20082 if (*arg
== K_SPECIAL
)
20086 while (arg
[j
] != NUL
&& (j
== 0 ? eval_isnamec1(arg
[j
])
20087 : eval_isnamec(arg
[j
])))
20090 emsg_funcname((char *)e_invarg2
, arg
);
20095 * Isolate the arguments: "arg1, arg2, ...)"
20099 if (p
[0] == '.' && p
[1] == '.' && p
[2] == '.')
20108 while (ASCII_ISALNUM(*p
) || *p
== '_')
20110 if (arg
== p
|| isdigit(*arg
)
20111 || (p
- arg
== 9 && STRNCMP(arg
, "firstline", 9) == 0)
20112 || (p
- arg
== 8 && STRNCMP(arg
, "lastline", 8) == 0))
20115 EMSG2(_("E125: Illegal argument: %s"), arg
);
20118 if (ga_grow(&newargs
, 1) == FAIL
)
20122 arg
= vim_strsave(arg
);
20125 ((char_u
**)(newargs
.ga_data
))[newargs
.ga_len
] = arg
;
20134 if (mustend
&& *p
!= ')')
20137 EMSG2(_(e_invarg2
), eap
->arg
);
20141 ++p
; /* skip the ')' */
20143 /* find extra arguments "range", "dict" and "abort" */
20147 if (STRNCMP(p
, "range", 5) == 0)
20152 else if (STRNCMP(p
, "dict", 4) == 0)
20157 else if (STRNCMP(p
, "abort", 5) == 0)
20166 /* When there is a line break use what follows for the function body.
20167 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20170 else if (*p
!= NUL
&& *p
!= '"' && !eap
->skip
&& !did_emsg
)
20171 EMSG(_(e_trailing
));
20174 * Read the body of the function, until ":endfunction" is found.
20178 /* Check if the function already exists, don't let the user type the
20179 * whole function before telling him it doesn't work! For a script we
20180 * need to skip the body to be able to find what follows. */
20181 if (!eap
->skip
&& !eap
->forceit
)
20183 if (fudi
.fd_dict
!= NULL
&& fudi
.fd_newkey
== NULL
)
20184 EMSG(_(e_funcdict
));
20185 else if (name
!= NULL
&& find_func(name
) != NULL
)
20186 emsg_funcname(e_funcexts
, name
);
20189 if (!eap
->skip
&& did_emsg
)
20192 msg_putchar('\n'); /* don't overwrite the function name */
20193 cmdline_row
= msg_row
;
20201 need_wait_return
= FALSE
;
20202 sourcing_lnum_off
= sourcing_lnum
;
20204 if (line_arg
!= NULL
)
20206 /* Use eap->arg, split up in parts by line breaks. */
20207 theline
= line_arg
;
20208 p
= vim_strchr(theline
, '\n');
20210 line_arg
+= STRLEN(line_arg
);
20217 else if (eap
->getline
== NULL
)
20218 theline
= getcmdline(':', 0L, indent
);
20220 theline
= eap
->getline(':', eap
->cookie
, indent
);
20222 lines_left
= Rows
- 1;
20223 if (theline
== NULL
)
20225 EMSG(_("E126: Missing :endfunction"));
20229 /* Detect line continuation: sourcing_lnum increased more than one. */
20230 if (sourcing_lnum
> sourcing_lnum_off
+ 1)
20231 sourcing_lnum_off
= sourcing_lnum
- sourcing_lnum_off
- 1;
20233 sourcing_lnum_off
= 0;
20235 if (skip_until
!= NULL
)
20237 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20238 * don't check for ":endfunc". */
20239 if (STRCMP(theline
, skip_until
) == 0)
20241 vim_free(skip_until
);
20247 /* skip ':' and blanks*/
20248 for (p
= theline
; vim_iswhite(*p
) || *p
== ':'; ++p
)
20251 /* Check for "endfunction". */
20252 if (checkforcmd(&p
, "endfunction", 4) && nesting
-- == 0)
20254 if (line_arg
== NULL
)
20259 /* Increase indent inside "if", "while", "for" and "try", decrease
20261 if (indent
> 2 && STRNCMP(p
, "end", 3) == 0)
20263 else if (STRNCMP(p
, "if", 2) == 0
20264 || STRNCMP(p
, "wh", 2) == 0
20265 || STRNCMP(p
, "for", 3) == 0
20266 || STRNCMP(p
, "try", 3) == 0)
20269 /* Check for defining a function inside this function. */
20270 if (checkforcmd(&p
, "function", 2))
20273 p
= skipwhite(p
+ 1);
20274 p
+= eval_fname_script(p
);
20275 if (ASCII_ISALPHA(*p
))
20277 vim_free(trans_function_name(&p
, TRUE
, 0, NULL
));
20278 if (*skipwhite(p
) == '(')
20286 /* Check for ":append" or ":insert". */
20287 p
= skip_range(p
, NULL
);
20288 if ((p
[0] == 'a' && (!ASCII_ISALPHA(p
[1]) || p
[1] == 'p'))
20290 && (!ASCII_ISALPHA(p
[1]) || (p
[1] == 'n'
20291 && (!ASCII_ISALPHA(p
[2]) || (p
[2] == 's'))))))
20292 skip_until
= vim_strsave((char_u
*)".");
20294 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20295 arg
= skipwhite(skiptowhite(p
));
20296 if (arg
[0] == '<' && arg
[1] =='<'
20297 && ((p
[0] == 'p' && p
[1] == 'y'
20298 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 't'))
20299 || (p
[0] == 'p' && p
[1] == 'e'
20300 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'r'))
20301 || (p
[0] == 't' && p
[1] == 'c'
20302 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'l'))
20303 || (p
[0] == 'r' && p
[1] == 'u' && p
[2] == 'b'
20304 && (!ASCII_ISALPHA(p
[3]) || p
[3] == 'y'))
20305 || (p
[0] == 'm' && p
[1] == 'z'
20306 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 's'))
20309 /* ":python <<" continues until a dot, like ":append" */
20310 p
= skipwhite(arg
+ 2);
20312 skip_until
= vim_strsave((char_u
*)".");
20314 skip_until
= vim_strsave(p
);
20318 /* Add the line to the function. */
20319 if (ga_grow(&newlines
, 1 + sourcing_lnum_off
) == FAIL
)
20321 if (line_arg
== NULL
)
20326 /* Copy the line to newly allocated memory. get_one_sourceline()
20327 * allocates 250 bytes per line, this saves 80% on average. The cost
20328 * is an extra alloc/free. */
20329 p
= vim_strsave(theline
);
20332 if (line_arg
== NULL
)
20337 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = theline
;
20339 /* Add NULL lines for continuation lines, so that the line count is
20340 * equal to the index in the growarray. */
20341 while (sourcing_lnum_off
-- > 0)
20342 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = NULL
;
20344 /* Check for end of eap->arg. */
20345 if (line_arg
!= NULL
&& *line_arg
== NUL
)
20349 /* Don't define the function when skipping commands or when an error was
20351 if (eap
->skip
|| did_emsg
)
20355 * If there are no errors, add the function
20357 if (fudi
.fd_dict
== NULL
)
20359 v
= find_var(name
, &ht
);
20360 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
20362 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
20367 fp
= find_func(name
);
20372 emsg_funcname(e_funcexts
, name
);
20375 if (fp
->uf_calls
> 0)
20377 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
20381 /* redefine existing function */
20382 ga_clear_strings(&(fp
->uf_args
));
20383 ga_clear_strings(&(fp
->uf_lines
));
20393 if (fudi
.fd_newkey
== NULL
&& !eap
->forceit
)
20395 EMSG(_(e_funcdict
));
20398 if (fudi
.fd_di
== NULL
)
20400 /* Can't add a function to a locked dictionary */
20401 if (tv_check_lock(fudi
.fd_dict
->dv_lock
, eap
->arg
))
20404 /* Can't change an existing function if it is locked */
20405 else if (tv_check_lock(fudi
.fd_di
->di_tv
.v_lock
, eap
->arg
))
20408 /* Give the function a sequential number. Can only be used with a
20411 sprintf(numbuf
, "%d", ++func_nr
);
20412 name
= vim_strsave((char_u
*)numbuf
);
20419 if (fudi
.fd_dict
== NULL
&& vim_strchr(name
, AUTOLOAD_CHAR
) != NULL
)
20422 char_u
*scriptname
;
20424 /* Check that the autoload name matches the script name. */
20426 if (sourcing_name
!= NULL
)
20428 scriptname
= autoload_name(name
);
20429 if (scriptname
!= NULL
)
20431 p
= vim_strchr(scriptname
, '/');
20432 plen
= (int)STRLEN(p
);
20433 slen
= (int)STRLEN(sourcing_name
);
20434 if (slen
> plen
&& fnamecmp(p
,
20435 sourcing_name
+ slen
- plen
) == 0)
20437 vim_free(scriptname
);
20442 EMSG2(_("E746: Function name does not match script file name: %s"), name
);
20447 fp
= (ufunc_T
*)alloc((unsigned)(sizeof(ufunc_T
) + STRLEN(name
)));
20451 if (fudi
.fd_dict
!= NULL
)
20453 if (fudi
.fd_di
== NULL
)
20455 /* add new dict entry */
20456 fudi
.fd_di
= dictitem_alloc(fudi
.fd_newkey
);
20457 if (fudi
.fd_di
== NULL
)
20462 if (dict_add(fudi
.fd_dict
, fudi
.fd_di
) == FAIL
)
20464 vim_free(fudi
.fd_di
);
20470 /* overwrite existing dict entry */
20471 clear_tv(&fudi
.fd_di
->di_tv
);
20472 fudi
.fd_di
->di_tv
.v_type
= VAR_FUNC
;
20473 fudi
.fd_di
->di_tv
.v_lock
= 0;
20474 fudi
.fd_di
->di_tv
.vval
.v_string
= vim_strsave(name
);
20475 fp
->uf_refcount
= 1;
20477 /* behave like "dict" was used */
20481 /* insert the new function in the function list */
20482 STRCPY(fp
->uf_name
, name
);
20483 hash_add(&func_hashtab
, UF2HIKEY(fp
));
20485 fp
->uf_args
= newargs
;
20486 fp
->uf_lines
= newlines
;
20487 #ifdef FEAT_PROFILE
20488 fp
->uf_tml_count
= NULL
;
20489 fp
->uf_tml_total
= NULL
;
20490 fp
->uf_tml_self
= NULL
;
20491 fp
->uf_profiling
= FALSE
;
20492 if (prof_def_func())
20493 func_do_profile(fp
);
20495 fp
->uf_varargs
= varargs
;
20496 fp
->uf_flags
= flags
;
20498 fp
->uf_script_ID
= current_SID
;
20502 ga_clear_strings(&newargs
);
20503 ga_clear_strings(&newlines
);
20505 vim_free(skip_until
);
20506 vim_free(fudi
.fd_newkey
);
20508 did_emsg
|= saved_did_emsg
;
20512 * Get a function name, translating "<SID>" and "<SNR>".
20513 * Also handles a Funcref in a List or Dictionary.
20514 * Returns the function name in allocated memory, or NULL for failure.
20516 * TFN_INT: internal function name OK
20517 * TFN_QUIET: be quiet
20518 * Advances "pp" to just after the function name (if no error).
20521 trans_function_name(pp
, skip
, flags
, fdp
)
20523 int skip
; /* only find the end, don't evaluate */
20525 funcdict_T
*fdp
; /* return: info about dictionary used */
20527 char_u
*name
= NULL
;
20531 char_u sid_buf
[20];
20536 vim_memset(fdp
, 0, sizeof(funcdict_T
));
20539 /* Check for hard coded <SNR>: already translated function ID (from a user
20541 if ((*pp
)[0] == K_SPECIAL
&& (*pp
)[1] == KS_EXTRA
20542 && (*pp
)[2] == (int)KE_SNR
)
20545 len
= get_id_len(pp
) + 3;
20546 return vim_strnsave(start
, len
);
20549 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20550 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
20551 lead
= eval_fname_script(start
);
20555 end
= get_lval(start
, NULL
, &lv
, FALSE
, skip
, flags
& TFN_QUIET
,
20556 lead
> 2 ? 0 : FNE_CHECK_START
);
20560 EMSG(_("E129: Function name required"));
20563 if (end
== NULL
|| (lv
.ll_tv
!= NULL
&& (lead
> 2 || lv
.ll_range
)))
20566 * Report an invalid expression in braces, unless the expression
20567 * evaluation has been cancelled due to an aborting error, an
20568 * interrupt, or an exception.
20573 EMSG2(_(e_invarg2
), start
);
20576 *pp
= find_name_end(start
, NULL
, NULL
, FNE_INCL_BR
);
20580 if (lv
.ll_tv
!= NULL
)
20584 fdp
->fd_dict
= lv
.ll_dict
;
20585 fdp
->fd_newkey
= lv
.ll_newkey
;
20586 lv
.ll_newkey
= NULL
;
20587 fdp
->fd_di
= lv
.ll_di
;
20589 if (lv
.ll_tv
->v_type
== VAR_FUNC
&& lv
.ll_tv
->vval
.v_string
!= NULL
)
20591 name
= vim_strsave(lv
.ll_tv
->vval
.v_string
);
20596 if (!skip
&& !(flags
& TFN_QUIET
) && (fdp
== NULL
20597 || lv
.ll_dict
== NULL
|| fdp
->fd_newkey
== NULL
))
20598 EMSG(_(e_funcref
));
20606 if (lv
.ll_name
== NULL
)
20608 /* Error found, but continue after the function name. */
20613 /* Check if the name is a Funcref. If so, use the value. */
20614 if (lv
.ll_exp_name
!= NULL
)
20616 len
= (int)STRLEN(lv
.ll_exp_name
);
20617 name
= deref_func_name(lv
.ll_exp_name
, &len
);
20618 if (name
== lv
.ll_exp_name
)
20623 len
= (int)(end
- *pp
);
20624 name
= deref_func_name(*pp
, &len
);
20630 name
= vim_strsave(name
);
20635 if (lv
.ll_exp_name
!= NULL
)
20637 len
= (int)STRLEN(lv
.ll_exp_name
);
20638 if (lead
<= 2 && lv
.ll_name
== lv
.ll_exp_name
20639 && STRNCMP(lv
.ll_name
, "s:", 2) == 0)
20641 /* When there was "s:" already or the name expanded to get a
20642 * leading "s:" then remove it. */
20650 if (lead
== 2) /* skip over "s:" */
20652 len
= (int)(end
- lv
.ll_name
);
20656 * Copy the function name to allocated memory.
20657 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20658 * Accept <SNR>123_name() outside a script.
20661 lead
= 0; /* do nothing */
20665 if ((lv
.ll_exp_name
!= NULL
&& eval_fname_sid(lv
.ll_exp_name
))
20666 || eval_fname_sid(*pp
))
20668 /* It's "s:" or "<SID>" */
20669 if (current_SID
<= 0)
20671 EMSG(_(e_usingsid
));
20674 sprintf((char *)sid_buf
, "%ld_", (long)current_SID
);
20675 lead
+= (int)STRLEN(sid_buf
);
20678 else if (!(flags
& TFN_INT
) && builtin_function(lv
.ll_name
))
20680 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv
.ll_name
);
20683 name
= alloc((unsigned)(len
+ lead
+ 1));
20688 name
[0] = K_SPECIAL
;
20689 name
[1] = KS_EXTRA
;
20690 name
[2] = (int)KE_SNR
;
20691 if (lead
> 3) /* If it's "<SID>" */
20692 STRCPY(name
+ 3, sid_buf
);
20694 mch_memmove(name
+ lead
, lv
.ll_name
, (size_t)len
);
20695 name
[len
+ lead
] = NUL
;
20705 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20706 * Return 2 if "p" starts with "s:".
20707 * Return 0 otherwise.
20710 eval_fname_script(p
)
20713 if (p
[0] == '<' && (STRNICMP(p
+ 1, "SID>", 4) == 0
20714 || STRNICMP(p
+ 1, "SNR>", 4) == 0))
20716 if (p
[0] == 's' && p
[1] == ':')
20722 * Return TRUE if "p" starts with "<SID>" or "s:".
20723 * Only works if eval_fname_script() returned non-zero for "p"!
20729 return (*p
== 's' || TOUPPER_ASC(p
[2]) == 'I');
20733 * List the head of the function: "name(arg1, arg2)".
20736 list_func_head(fp
, indent
)
20745 MSG_PUTS("function ");
20746 if (fp
->uf_name
[0] == K_SPECIAL
)
20748 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8
));
20749 msg_puts(fp
->uf_name
+ 3);
20752 msg_puts(fp
->uf_name
);
20754 for (j
= 0; j
< fp
->uf_args
.ga_len
; ++j
)
20758 msg_puts(FUNCARG(fp
, j
));
20760 if (fp
->uf_varargs
)
20769 last_set_msg(fp
->uf_script_ID
);
20773 * Find a function by name, return pointer to it in ufuncs.
20774 * Return NULL for unknown function.
20782 hi
= hash_find(&func_hashtab
, name
);
20783 if (!HASHITEM_EMPTY(hi
))
20788 #if defined(EXITFREE) || defined(PROTO)
20790 free_all_functions()
20794 /* Need to start all over every time, because func_free() may change the
20796 while (func_hashtab
.ht_used
> 0)
20797 for (hi
= func_hashtab
.ht_array
; ; ++hi
)
20798 if (!HASHITEM_EMPTY(hi
))
20800 func_free(HI2UF(hi
));
20807 * Return TRUE if a function "name" exists.
20810 function_exists(name
)
20817 p
= trans_function_name(&nm
, FALSE
, TFN_INT
|TFN_QUIET
, NULL
);
20818 nm
= skipwhite(nm
);
20820 /* Only accept "funcname", "funcname ", "funcname (..." and
20821 * "funcname(...", not "funcname!...". */
20822 if (p
!= NULL
&& (*nm
== NUL
|| *nm
== '('))
20824 if (builtin_function(p
))
20825 n
= (find_internal_func(p
) >= 0);
20827 n
= (find_func(p
) != NULL
);
20834 * Return TRUE if "name" looks like a builtin function name: starts with a
20835 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
20838 builtin_function(name
)
20841 return ASCII_ISLOWER(name
[0]) && vim_strchr(name
, ':') == NULL
20842 && vim_strchr(name
, AUTOLOAD_CHAR
) == NULL
;
20845 #if defined(FEAT_PROFILE) || defined(PROTO)
20847 * Start profiling function "fp".
20850 func_do_profile(fp
)
20853 fp
->uf_tm_count
= 0;
20854 profile_zero(&fp
->uf_tm_self
);
20855 profile_zero(&fp
->uf_tm_total
);
20856 if (fp
->uf_tml_count
== NULL
)
20857 fp
->uf_tml_count
= (int *)alloc_clear((unsigned)
20858 (sizeof(int) * fp
->uf_lines
.ga_len
));
20859 if (fp
->uf_tml_total
== NULL
)
20860 fp
->uf_tml_total
= (proftime_T
*)alloc_clear((unsigned)
20861 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
20862 if (fp
->uf_tml_self
== NULL
)
20863 fp
->uf_tml_self
= (proftime_T
*)alloc_clear((unsigned)
20864 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
20865 fp
->uf_tml_idx
= -1;
20866 if (fp
->uf_tml_count
== NULL
|| fp
->uf_tml_total
== NULL
20867 || fp
->uf_tml_self
== NULL
)
20868 return; /* out of memory */
20870 fp
->uf_profiling
= TRUE
;
20874 * Dump the profiling results for all functions in file "fd".
20877 func_dump_profile(fd
)
20887 todo
= (int)func_hashtab
.ht_used
;
20889 return; /* nothing to dump */
20891 sorttab
= (ufunc_T
**)alloc((unsigned)(sizeof(ufunc_T
) * todo
));
20893 for (hi
= func_hashtab
.ht_array
; todo
> 0; ++hi
)
20895 if (!HASHITEM_EMPTY(hi
))
20899 if (fp
->uf_profiling
)
20901 if (sorttab
!= NULL
)
20902 sorttab
[st_len
++] = fp
;
20904 if (fp
->uf_name
[0] == K_SPECIAL
)
20905 fprintf(fd
, "FUNCTION <SNR>%s()\n", fp
->uf_name
+ 3);
20907 fprintf(fd
, "FUNCTION %s()\n", fp
->uf_name
);
20908 if (fp
->uf_tm_count
== 1)
20909 fprintf(fd
, "Called 1 time\n");
20911 fprintf(fd
, "Called %d times\n", fp
->uf_tm_count
);
20912 fprintf(fd
, "Total time: %s\n", profile_msg(&fp
->uf_tm_total
));
20913 fprintf(fd
, " Self time: %s\n", profile_msg(&fp
->uf_tm_self
));
20915 fprintf(fd
, "count total (s) self (s)\n");
20917 for (i
= 0; i
< fp
->uf_lines
.ga_len
; ++i
)
20919 if (FUNCLINE(fp
, i
) == NULL
)
20921 prof_func_line(fd
, fp
->uf_tml_count
[i
],
20922 &fp
->uf_tml_total
[i
], &fp
->uf_tml_self
[i
], TRUE
);
20923 fprintf(fd
, "%s\n", FUNCLINE(fp
, i
));
20930 if (sorttab
!= NULL
&& st_len
> 0)
20932 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
20934 prof_sort_list(fd
, sorttab
, st_len
, "TOTAL", FALSE
);
20935 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
20937 prof_sort_list(fd
, sorttab
, st_len
, "SELF", TRUE
);
20944 prof_sort_list(fd
, sorttab
, st_len
, title
, prefer_self
)
20949 int prefer_self
; /* when equal print only self time */
20954 fprintf(fd
, "FUNCTIONS SORTED ON %s TIME\n", title
);
20955 fprintf(fd
, "count total (s) self (s) function\n");
20956 for (i
= 0; i
< 20 && i
< st_len
; ++i
)
20959 prof_func_line(fd
, fp
->uf_tm_count
, &fp
->uf_tm_total
, &fp
->uf_tm_self
,
20961 if (fp
->uf_name
[0] == K_SPECIAL
)
20962 fprintf(fd
, " <SNR>%s()\n", fp
->uf_name
+ 3);
20964 fprintf(fd
, " %s()\n", fp
->uf_name
);
20970 * Print the count and times for one function or function line.
20973 prof_func_line(fd
, count
, total
, self
, prefer_self
)
20978 int prefer_self
; /* when equal print only self time */
20982 fprintf(fd
, "%5d ", count
);
20983 if (prefer_self
&& profile_equal(total
, self
))
20986 fprintf(fd
, "%s ", profile_msg(total
));
20987 if (!prefer_self
&& profile_equal(total
, self
))
20990 fprintf(fd
, "%s ", profile_msg(self
));
20997 * Compare function for total time sorting.
21000 #ifdef __BORLANDC__
21003 prof_total_cmp(s1
, s2
)
21009 p1
= *(ufunc_T
**)s1
;
21010 p2
= *(ufunc_T
**)s2
;
21011 return profile_cmp(&p1
->uf_tm_total
, &p2
->uf_tm_total
);
21015 * Compare function for self time sorting.
21018 #ifdef __BORLANDC__
21021 prof_self_cmp(s1
, s2
)
21027 p1
= *(ufunc_T
**)s1
;
21028 p2
= *(ufunc_T
**)s2
;
21029 return profile_cmp(&p1
->uf_tm_self
, &p2
->uf_tm_self
);
21035 * If "name" has a package name try autoloading the script for it.
21036 * Return TRUE if a package was loaded.
21039 script_autoload(name
, reload
)
21041 int reload
; /* load script again when already loaded */
21044 char_u
*scriptname
, *tofree
;
21048 /* If there is no '#' after name[0] there is no package name. */
21049 p
= vim_strchr(name
, AUTOLOAD_CHAR
);
21050 if (p
== NULL
|| p
== name
)
21053 tofree
= scriptname
= autoload_name(name
);
21055 /* Find the name in the list of previously loaded package names. Skip
21056 * "autoload/", it's always the same. */
21057 for (i
= 0; i
< ga_loaded
.ga_len
; ++i
)
21058 if (STRCMP(((char_u
**)ga_loaded
.ga_data
)[i
] + 9, scriptname
+ 9) == 0)
21060 if (!reload
&& i
< ga_loaded
.ga_len
)
21061 ret
= FALSE
; /* was loaded already */
21064 /* Remember the name if it wasn't loaded already. */
21065 if (i
== ga_loaded
.ga_len
&& ga_grow(&ga_loaded
, 1) == OK
)
21067 ((char_u
**)ga_loaded
.ga_data
)[ga_loaded
.ga_len
++] = scriptname
;
21071 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
21072 if (source_runtime(scriptname
, FALSE
) == OK
)
21081 * Return the autoload script name for a function or variable name.
21082 * Returns NULL when out of memory.
21085 autoload_name(name
)
21089 char_u
*scriptname
;
21091 /* Get the script file name: replace '#' with '/', append ".vim". */
21092 scriptname
= alloc((unsigned)(STRLEN(name
) + 14));
21093 if (scriptname
== NULL
)
21095 STRCPY(scriptname
, "autoload/");
21096 STRCAT(scriptname
, name
);
21097 *vim_strrchr(scriptname
, AUTOLOAD_CHAR
) = NUL
;
21098 STRCAT(scriptname
, ".vim");
21099 while ((p
= vim_strchr(scriptname
, AUTOLOAD_CHAR
)) != NULL
)
21104 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21107 * Function given to ExpandGeneric() to obtain the list of user defined
21111 get_user_func_name(xp
, idx
)
21115 static long_u done
;
21116 static hashitem_T
*hi
;
21122 hi
= func_hashtab
.ht_array
;
21124 if (done
< func_hashtab
.ht_used
)
21128 while (HASHITEM_EMPTY(hi
))
21132 if (STRLEN(fp
->uf_name
) + 4 >= IOSIZE
)
21133 return fp
->uf_name
; /* prevents overflow */
21135 cat_func_name(IObuff
, fp
);
21136 if (xp
->xp_context
!= EXPAND_USER_FUNC
)
21138 STRCAT(IObuff
, "(");
21139 if (!fp
->uf_varargs
&& fp
->uf_args
.ga_len
== 0)
21140 STRCAT(IObuff
, ")");
21147 #endif /* FEAT_CMDL_COMPL */
21150 * Copy the function name of "fp" to buffer "buf".
21151 * "buf" must be able to hold the function name plus three bytes.
21152 * Takes care of script-local function names.
21155 cat_func_name(buf
, fp
)
21159 if (fp
->uf_name
[0] == K_SPECIAL
)
21161 STRCPY(buf
, "<SNR>");
21162 STRCAT(buf
, fp
->uf_name
+ 3);
21165 STRCPY(buf
, fp
->uf_name
);
21169 * ":delfunction {name}"
21172 ex_delfunction(eap
)
21175 ufunc_T
*fp
= NULL
;
21181 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
21182 vim_free(fudi
.fd_newkey
);
21185 if (fudi
.fd_dict
!= NULL
&& !eap
->skip
)
21186 EMSG(_(e_funcref
));
21189 if (!ends_excmd(*skipwhite(p
)))
21192 EMSG(_(e_trailing
));
21195 eap
->nextcmd
= check_nextcmd(p
);
21196 if (eap
->nextcmd
!= NULL
)
21200 fp
= find_func(name
);
21207 EMSG2(_(e_nofunc
), eap
->arg
);
21210 if (fp
->uf_calls
> 0)
21212 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap
->arg
);
21216 if (fudi
.fd_dict
!= NULL
)
21218 /* Delete the dict item that refers to the function, it will
21219 * invoke func_unref() and possibly delete the function. */
21220 dictitem_remove(fudi
.fd_dict
, fudi
.fd_di
);
21228 * Free a function and remove it from the list of functions.
21236 /* clear this function */
21237 ga_clear_strings(&(fp
->uf_args
));
21238 ga_clear_strings(&(fp
->uf_lines
));
21239 #ifdef FEAT_PROFILE
21240 vim_free(fp
->uf_tml_count
);
21241 vim_free(fp
->uf_tml_total
);
21242 vim_free(fp
->uf_tml_self
);
21245 /* remove the function from the function hashtable */
21246 hi
= hash_find(&func_hashtab
, UF2HIKEY(fp
));
21247 if (HASHITEM_EMPTY(hi
))
21248 EMSG2(_(e_intern2
), "func_free()");
21250 hash_remove(&func_hashtab
, hi
);
21256 * Unreference a Function: decrement the reference count and free it when it
21257 * becomes zero. Only for numbered functions.
21265 if (name
!= NULL
&& isdigit(*name
))
21267 fp
= find_func(name
);
21269 EMSG2(_(e_intern2
), "func_unref()");
21270 else if (--fp
->uf_refcount
<= 0)
21272 /* Only delete it when it's not being used. Otherwise it's done
21273 * when "uf_calls" becomes zero. */
21274 if (fp
->uf_calls
== 0)
21281 * Count a reference to a Function.
21289 if (name
!= NULL
&& isdigit(*name
))
21291 fp
= find_func(name
);
21293 EMSG2(_(e_intern2
), "func_ref()");
21300 * Call a user function.
21303 call_user_func(fp
, argcount
, argvars
, rettv
, firstline
, lastline
, selfdict
)
21304 ufunc_T
*fp
; /* pointer to function */
21305 int argcount
; /* nr of args */
21306 typval_T
*argvars
; /* arguments */
21307 typval_T
*rettv
; /* return value */
21308 linenr_T firstline
; /* first line of range */
21309 linenr_T lastline
; /* last line of range */
21310 dict_T
*selfdict
; /* Dictionary for "self" */
21312 char_u
*save_sourcing_name
;
21313 linenr_T save_sourcing_lnum
;
21314 scid_T save_current_SID
;
21317 static int depth
= 0;
21319 int fixvar_idx
= 0; /* index in fixvar[] */
21322 char_u numbuf
[NUMBUFLEN
];
21324 #ifdef FEAT_PROFILE
21325 proftime_T wait_start
;
21326 proftime_T call_start
;
21329 /* If depth of calling is getting too high, don't execute the function */
21330 if (depth
>= p_mfd
)
21332 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
21333 rettv
->v_type
= VAR_NUMBER
;
21334 rettv
->vval
.v_number
= -1;
21339 line_breakcheck(); /* check for CTRL-C hit */
21341 fc
= (funccall_T
*)alloc(sizeof(funccall_T
));
21342 fc
->caller
= current_funccal
;
21343 current_funccal
= fc
;
21346 rettv
->vval
.v_number
= 0;
21348 fc
->returned
= FALSE
;
21349 fc
->level
= ex_nesting_level
;
21350 /* Check if this function has a breakpoint. */
21351 fc
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
, (linenr_T
)0);
21352 fc
->dbg_tick
= debug_tick
;
21355 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
21356 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21357 * each argument variable and saves a lot of time.
21360 * Init l: variables.
21362 init_var_dict(&fc
->l_vars
, &fc
->l_vars_var
);
21363 if (selfdict
!= NULL
)
21365 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21366 * some compiler that checks the destination size. */
21367 v
= &fc
->fixvar
[fixvar_idx
++].var
;
21369 STRCPY(name
, "self");
21370 v
->di_flags
= DI_FLAGS_RO
+ DI_FLAGS_FIX
;
21371 hash_add(&fc
->l_vars
.dv_hashtab
, DI2HIKEY(v
));
21372 v
->di_tv
.v_type
= VAR_DICT
;
21373 v
->di_tv
.v_lock
= 0;
21374 v
->di_tv
.vval
.v_dict
= selfdict
;
21375 ++selfdict
->dv_refcount
;
21379 * Init a: variables.
21380 * Set a:0 to "argcount".
21381 * Set a:000 to a list with room for the "..." arguments.
21383 init_var_dict(&fc
->l_avars
, &fc
->l_avars_var
);
21384 add_nr_var(&fc
->l_avars
, &fc
->fixvar
[fixvar_idx
++].var
, "0",
21385 (varnumber_T
)(argcount
- fp
->uf_args
.ga_len
));
21386 /* Use "name" to avoid a warning from some compiler that checks the
21387 * destination size. */
21388 v
= &fc
->fixvar
[fixvar_idx
++].var
;
21390 STRCPY(name
, "000");
21391 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21392 hash_add(&fc
->l_avars
.dv_hashtab
, DI2HIKEY(v
));
21393 v
->di_tv
.v_type
= VAR_LIST
;
21394 v
->di_tv
.v_lock
= VAR_FIXED
;
21395 v
->di_tv
.vval
.v_list
= &fc
->l_varlist
;
21396 vim_memset(&fc
->l_varlist
, 0, sizeof(list_T
));
21397 fc
->l_varlist
.lv_refcount
= DO_NOT_FREE_CNT
;
21398 fc
->l_varlist
.lv_lock
= VAR_FIXED
;
21401 * Set a:firstline to "firstline" and a:lastline to "lastline".
21402 * Set a:name to named arguments.
21403 * Set a:N to the "..." arguments.
21405 add_nr_var(&fc
->l_avars
, &fc
->fixvar
[fixvar_idx
++].var
, "firstline",
21406 (varnumber_T
)firstline
);
21407 add_nr_var(&fc
->l_avars
, &fc
->fixvar
[fixvar_idx
++].var
, "lastline",
21408 (varnumber_T
)lastline
);
21409 for (i
= 0; i
< argcount
; ++i
)
21411 ai
= i
- fp
->uf_args
.ga_len
;
21413 /* named argument a:name */
21414 name
= FUNCARG(fp
, i
);
21417 /* "..." argument a:1, a:2, etc. */
21418 sprintf((char *)numbuf
, "%d", ai
+ 1);
21421 if (fixvar_idx
< FIXVAR_CNT
&& STRLEN(name
) <= VAR_SHORT_LEN
)
21423 v
= &fc
->fixvar
[fixvar_idx
++].var
;
21424 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21428 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
21432 v
->di_flags
= DI_FLAGS_RO
;
21434 STRCPY(v
->di_key
, name
);
21435 hash_add(&fc
->l_avars
.dv_hashtab
, DI2HIKEY(v
));
21437 /* Note: the values are copied directly to avoid alloc/free.
21438 * "argvars" must have VAR_FIXED for v_lock. */
21439 v
->di_tv
= argvars
[i
];
21440 v
->di_tv
.v_lock
= VAR_FIXED
;
21442 if (ai
>= 0 && ai
< MAX_FUNC_ARGS
)
21444 list_append(&fc
->l_varlist
, &fc
->l_listitems
[ai
]);
21445 fc
->l_listitems
[ai
].li_tv
= argvars
[i
];
21446 fc
->l_listitems
[ai
].li_tv
.v_lock
= VAR_FIXED
;
21450 /* Don't redraw while executing the function. */
21451 ++RedrawingDisabled
;
21452 save_sourcing_name
= sourcing_name
;
21453 save_sourcing_lnum
= sourcing_lnum
;
21455 sourcing_name
= alloc((unsigned)((save_sourcing_name
== NULL
? 0
21456 : STRLEN(save_sourcing_name
)) + STRLEN(fp
->uf_name
) + 13));
21457 if (sourcing_name
!= NULL
)
21459 if (save_sourcing_name
!= NULL
21460 && STRNCMP(save_sourcing_name
, "function ", 9) == 0)
21461 sprintf((char *)sourcing_name
, "%s..", save_sourcing_name
);
21463 STRCPY(sourcing_name
, "function ");
21464 cat_func_name(sourcing_name
+ STRLEN(sourcing_name
), fp
);
21466 if (p_verbose
>= 12)
21469 verbose_enter_scroll();
21471 smsg((char_u
*)_("calling %s"), sourcing_name
);
21472 if (p_verbose
>= 14)
21474 char_u buf
[MSG_BUF_LEN
];
21475 char_u numbuf2
[NUMBUFLEN
];
21479 msg_puts((char_u
*)"(");
21480 for (i
= 0; i
< argcount
; ++i
)
21483 msg_puts((char_u
*)", ");
21484 if (argvars
[i
].v_type
== VAR_NUMBER
)
21485 msg_outnum((long)argvars
[i
].vval
.v_number
);
21488 s
= tv2string(&argvars
[i
], &tofree
, numbuf2
, 0);
21491 trunc_string(s
, buf
, MSG_BUF_CLEN
);
21497 msg_puts((char_u
*)")");
21499 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21501 verbose_leave_scroll();
21505 #ifdef FEAT_PROFILE
21506 if (do_profiling
== PROF_YES
)
21508 if (!fp
->uf_profiling
&& has_profiling(FALSE
, fp
->uf_name
, NULL
))
21509 func_do_profile(fp
);
21510 if (fp
->uf_profiling
21511 || (fc
->caller
!= NULL
&& fc
->caller
->func
->uf_profiling
))
21514 profile_start(&call_start
);
21515 profile_zero(&fp
->uf_tm_children
);
21517 script_prof_save(&wait_start
);
21521 save_current_SID
= current_SID
;
21522 current_SID
= fp
->uf_script_ID
;
21523 save_did_emsg
= did_emsg
;
21526 /* call do_cmdline() to execute the lines */
21527 do_cmdline(NULL
, get_func_line
, (void *)fc
,
21528 DOCMD_NOWAIT
|DOCMD_VERBOSE
|DOCMD_REPEAT
);
21530 --RedrawingDisabled
;
21532 /* when the function was aborted because of an error, return -1 */
21533 if ((did_emsg
&& (fp
->uf_flags
& FC_ABORT
)) || rettv
->v_type
== VAR_UNKNOWN
)
21536 rettv
->v_type
= VAR_NUMBER
;
21537 rettv
->vval
.v_number
= -1;
21540 #ifdef FEAT_PROFILE
21541 if (do_profiling
== PROF_YES
&& (fp
->uf_profiling
21542 || (fc
->caller
!= NULL
&& fc
->caller
->func
->uf_profiling
)))
21544 profile_end(&call_start
);
21545 profile_sub_wait(&wait_start
, &call_start
);
21546 profile_add(&fp
->uf_tm_total
, &call_start
);
21547 profile_self(&fp
->uf_tm_self
, &call_start
, &fp
->uf_tm_children
);
21548 if (fc
->caller
!= NULL
&& fc
->caller
->func
->uf_profiling
)
21550 profile_add(&fc
->caller
->func
->uf_tm_children
, &call_start
);
21551 profile_add(&fc
->caller
->func
->uf_tml_children
, &call_start
);
21556 /* when being verbose, mention the return value */
21557 if (p_verbose
>= 12)
21560 verbose_enter_scroll();
21563 smsg((char_u
*)_("%s aborted"), sourcing_name
);
21564 else if (fc
->rettv
->v_type
== VAR_NUMBER
)
21565 smsg((char_u
*)_("%s returning #%ld"), sourcing_name
,
21566 (long)fc
->rettv
->vval
.v_number
);
21569 char_u buf
[MSG_BUF_LEN
];
21570 char_u numbuf2
[NUMBUFLEN
];
21574 /* The value may be very long. Skip the middle part, so that we
21575 * have some idea how it starts and ends. smsg() would always
21576 * truncate it at the end. */
21577 s
= tv2string(fc
->rettv
, &tofree
, numbuf2
, 0);
21580 trunc_string(s
, buf
, MSG_BUF_CLEN
);
21581 smsg((char_u
*)_("%s returning %s"), sourcing_name
, buf
);
21585 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21587 verbose_leave_scroll();
21591 vim_free(sourcing_name
);
21592 sourcing_name
= save_sourcing_name
;
21593 sourcing_lnum
= save_sourcing_lnum
;
21594 current_SID
= save_current_SID
;
21595 #ifdef FEAT_PROFILE
21596 if (do_profiling
== PROF_YES
)
21597 script_prof_restore(&wait_start
);
21600 if (p_verbose
>= 12 && sourcing_name
!= NULL
)
21603 verbose_enter_scroll();
21605 smsg((char_u
*)_("continuing in %s"), sourcing_name
);
21606 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21608 verbose_leave_scroll();
21612 did_emsg
|= save_did_emsg
;
21613 current_funccal
= fc
->caller
;
21616 /* If the a:000 list and the l: and a: dicts are not referenced we can
21617 * free the funccall_T and what's in it. */
21618 if (fc
->l_varlist
.lv_refcount
== DO_NOT_FREE_CNT
21619 && fc
->l_vars
.dv_refcount
== DO_NOT_FREE_CNT
21620 && fc
->l_avars
.dv_refcount
== DO_NOT_FREE_CNT
)
21622 free_funccal(fc
, FALSE
);
21630 /* "fc" is still in use. This can happen when returning "a:000" or
21631 * assigning "l:" to a global variable.
21632 * Link "fc" in the list for garbage collection later. */
21633 fc
->caller
= previous_funccal
;
21634 previous_funccal
= fc
;
21636 /* Make a copy of the a: variables, since we didn't do that above. */
21637 todo
= (int)fc
->l_avars
.dv_hashtab
.ht_used
;
21638 for (hi
= fc
->l_avars
.dv_hashtab
.ht_array
; todo
> 0; ++hi
)
21640 if (!HASHITEM_EMPTY(hi
))
21644 copy_tv(&v
->di_tv
, &v
->di_tv
);
21648 /* Make a copy of the a:000 items, since we didn't do that above. */
21649 for (li
= fc
->l_varlist
.lv_first
; li
!= NULL
; li
= li
->li_next
)
21650 copy_tv(&li
->li_tv
, &li
->li_tv
);
21655 * Return TRUE if items in "fc" do not have "copyID". That means they are not
21656 * referenced from anywhere that is in use.
21659 can_free_funccal(fc
, copyID
)
21663 return (fc
->l_varlist
.lv_copyID
!= copyID
21664 && fc
->l_vars
.dv_copyID
!= copyID
21665 && fc
->l_avars
.dv_copyID
!= copyID
);
21669 * Free "fc" and what it contains.
21672 free_funccal(fc
, free_val
)
21674 int free_val
; /* a: vars were allocated */
21678 /* The a: variables typevals may not have been allocated, only free the
21679 * allocated variables. */
21680 vars_clear_ext(&fc
->l_avars
.dv_hashtab
, free_val
);
21682 /* free all l: variables */
21683 vars_clear(&fc
->l_vars
.dv_hashtab
);
21685 /* Free the a:000 variables if they were allocated. */
21687 for (li
= fc
->l_varlist
.lv_first
; li
!= NULL
; li
= li
->li_next
)
21688 clear_tv(&li
->li_tv
);
21694 * Add a number variable "name" to dict "dp" with value "nr".
21697 add_nr_var(dp
, v
, name
, nr
)
21703 STRCPY(v
->di_key
, name
);
21704 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21705 hash_add(&dp
->dv_hashtab
, DI2HIKEY(v
));
21706 v
->di_tv
.v_type
= VAR_NUMBER
;
21707 v
->di_tv
.v_lock
= VAR_FIXED
;
21708 v
->di_tv
.vval
.v_number
= nr
;
21718 char_u
*arg
= eap
->arg
;
21720 int returning
= FALSE
;
21722 if (current_funccal
== NULL
)
21724 EMSG(_("E133: :return not inside a function"));
21731 eap
->nextcmd
= NULL
;
21732 if ((*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
21733 && eval0(arg
, &rettv
, &eap
->nextcmd
, !eap
->skip
) != FAIL
)
21736 returning
= do_return(eap
, FALSE
, TRUE
, &rettv
);
21740 /* It's safer to return also on error. */
21741 else if (!eap
->skip
)
21744 * Return unless the expression evaluation has been cancelled due to an
21745 * aborting error, an interrupt, or an exception.
21748 returning
= do_return(eap
, FALSE
, TRUE
, NULL
);
21751 /* When skipping or the return gets pending, advance to the next command
21752 * in this line (!returning). Otherwise, ignore the rest of the line.
21753 * Following lines will be ignored by get_func_line(). */
21755 eap
->nextcmd
= NULL
;
21756 else if (eap
->nextcmd
== NULL
) /* no argument */
21757 eap
->nextcmd
= check_nextcmd(arg
);
21764 * Return from a function. Possibly makes the return pending. Also called
21765 * for a pending return at the ":endtry" or after returning from an extra
21766 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
21767 * when called due to a ":return" command. "rettv" may point to a typval_T
21768 * with the return rettv. Returns TRUE when the return can be carried out,
21769 * FALSE when the return gets pending.
21772 do_return(eap
, reanimate
, is_cmd
, rettv
)
21779 struct condstack
*cstack
= eap
->cstack
;
21782 /* Undo the return. */
21783 current_funccal
->returned
= FALSE
;
21786 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21787 * not in its finally clause (which then is to be executed next) is found.
21788 * In this case, make the ":return" pending for execution at the ":endtry".
21789 * Otherwise, return normally.
21791 idx
= cleanup_conditionals(eap
->cstack
, 0, TRUE
);
21794 cstack
->cs_pending
[idx
] = CSTP_RETURN
;
21796 if (!is_cmd
&& !reanimate
)
21797 /* A pending return again gets pending. "rettv" points to an
21798 * allocated variable with the rettv of the original ":return"'s
21799 * argument if present or is NULL else. */
21800 cstack
->cs_rettv
[idx
] = rettv
;
21803 /* When undoing a return in order to make it pending, get the stored
21806 rettv
= current_funccal
->rettv
;
21810 /* Store the value of the pending return. */
21811 if ((cstack
->cs_rettv
[idx
] = alloc_tv()) != NULL
)
21812 *(typval_T
*)cstack
->cs_rettv
[idx
] = *(typval_T
*)rettv
;
21814 EMSG(_(e_outofmem
));
21817 cstack
->cs_rettv
[idx
] = NULL
;
21821 /* The pending return value could be overwritten by a ":return"
21822 * without argument in a finally clause; reset the default
21824 current_funccal
->rettv
->v_type
= VAR_NUMBER
;
21825 current_funccal
->rettv
->vval
.v_number
= 0;
21828 report_make_pending(CSTP_RETURN
, rettv
);
21832 current_funccal
->returned
= TRUE
;
21834 /* If the return is carried out now, store the return value. For
21835 * a return immediately after reanimation, the value is already
21837 if (!reanimate
&& rettv
!= NULL
)
21839 clear_tv(current_funccal
->rettv
);
21840 *current_funccal
->rettv
= *(typval_T
*)rettv
;
21850 * Free the variable with a pending return value.
21853 discard_pending_return(rettv
)
21856 free_tv((typval_T
*)rettv
);
21860 * Generate a return command for producing the value of "rettv". The result
21861 * is an allocated string. Used by report_pending() for verbose messages.
21864 get_return_cmd(rettv
)
21868 char_u
*tofree
= NULL
;
21869 char_u numbuf
[NUMBUFLEN
];
21872 s
= echo_string((typval_T
*)rettv
, &tofree
, numbuf
, 0);
21876 STRCPY(IObuff
, ":return ");
21877 STRNCPY(IObuff
+ 8, s
, IOSIZE
- 8);
21878 if (STRLEN(s
) + 8 >= IOSIZE
)
21879 STRCPY(IObuff
+ IOSIZE
- 4, "...");
21881 return vim_strsave(IObuff
);
21885 * Get next function line.
21886 * Called by do_cmdline() to get the next line.
21887 * Returns allocated string, or NULL for end of function.
21890 get_func_line(c
, cookie
, indent
)
21895 funccall_T
*fcp
= (funccall_T
*)cookie
;
21896 ufunc_T
*fp
= fcp
->func
;
21898 garray_T
*gap
; /* growarray with function lines */
21900 /* If breakpoints have been added/deleted need to check for it. */
21901 if (fcp
->dbg_tick
!= debug_tick
)
21903 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
21905 fcp
->dbg_tick
= debug_tick
;
21907 #ifdef FEAT_PROFILE
21908 if (do_profiling
== PROF_YES
)
21909 func_line_end(cookie
);
21912 gap
= &fp
->uf_lines
;
21913 if (((fp
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
21918 /* Skip NULL lines (continuation lines). */
21919 while (fcp
->linenr
< gap
->ga_len
21920 && ((char_u
**)(gap
->ga_data
))[fcp
->linenr
] == NULL
)
21922 if (fcp
->linenr
>= gap
->ga_len
)
21926 retval
= vim_strsave(((char_u
**)(gap
->ga_data
))[fcp
->linenr
++]);
21927 sourcing_lnum
= fcp
->linenr
;
21928 #ifdef FEAT_PROFILE
21929 if (do_profiling
== PROF_YES
)
21930 func_line_start(cookie
);
21935 /* Did we encounter a breakpoint? */
21936 if (fcp
->breakpoint
!= 0 && fcp
->breakpoint
<= sourcing_lnum
)
21938 dbg_breakpoint(fp
->uf_name
, sourcing_lnum
);
21939 /* Find next breakpoint. */
21940 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
21942 fcp
->dbg_tick
= debug_tick
;
21948 #if defined(FEAT_PROFILE) || defined(PROTO)
21950 * Called when starting to read a function line.
21951 * "sourcing_lnum" must be correct!
21952 * When skipping lines it may not actually be executed, but we won't find out
21953 * until later and we need to store the time now.
21956 func_line_start(cookie
)
21959 funccall_T
*fcp
= (funccall_T
*)cookie
;
21960 ufunc_T
*fp
= fcp
->func
;
21962 if (fp
->uf_profiling
&& sourcing_lnum
>= 1
21963 && sourcing_lnum
<= fp
->uf_lines
.ga_len
)
21965 fp
->uf_tml_idx
= sourcing_lnum
- 1;
21966 /* Skip continuation lines. */
21967 while (fp
->uf_tml_idx
> 0 && FUNCLINE(fp
, fp
->uf_tml_idx
) == NULL
)
21969 fp
->uf_tml_execed
= FALSE
;
21970 profile_start(&fp
->uf_tml_start
);
21971 profile_zero(&fp
->uf_tml_children
);
21972 profile_get_wait(&fp
->uf_tml_wait
);
21977 * Called when actually executing a function line.
21980 func_line_exec(cookie
)
21983 funccall_T
*fcp
= (funccall_T
*)cookie
;
21984 ufunc_T
*fp
= fcp
->func
;
21986 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
21987 fp
->uf_tml_execed
= TRUE
;
21991 * Called when done with a function line.
21994 func_line_end(cookie
)
21997 funccall_T
*fcp
= (funccall_T
*)cookie
;
21998 ufunc_T
*fp
= fcp
->func
;
22000 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
22002 if (fp
->uf_tml_execed
)
22004 ++fp
->uf_tml_count
[fp
->uf_tml_idx
];
22005 profile_end(&fp
->uf_tml_start
);
22006 profile_sub_wait(&fp
->uf_tml_wait
, &fp
->uf_tml_start
);
22007 profile_add(&fp
->uf_tml_total
[fp
->uf_tml_idx
], &fp
->uf_tml_start
);
22008 profile_self(&fp
->uf_tml_self
[fp
->uf_tml_idx
], &fp
->uf_tml_start
,
22009 &fp
->uf_tml_children
);
22011 fp
->uf_tml_idx
= -1;
22017 * Return TRUE if the currently active function should be ended, because a
22018 * return was encountered or an error occurred. Used inside a ":while".
22021 func_has_ended(cookie
)
22024 funccall_T
*fcp
= (funccall_T
*)cookie
;
22026 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22027 * an error inside a try conditional. */
22028 return (((fcp
->func
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
22033 * return TRUE if cookie indicates a function which "abort"s on errors.
22036 func_has_abort(cookie
)
22039 return ((funccall_T
*)cookie
)->func
->uf_flags
& FC_ABORT
;
22042 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22045 VAR_FLAVOUR_DEFAULT
, /* doesn't start with uppercase */
22046 VAR_FLAVOUR_SESSION
, /* starts with uppercase, some lower */
22047 VAR_FLAVOUR_VIMINFO
/* all uppercase */
22050 static var_flavour_T var_flavour
__ARGS((char_u
*varname
));
22052 static var_flavour_T
22053 var_flavour(varname
)
22056 char_u
*p
= varname
;
22058 if (ASCII_ISUPPER(*p
))
22061 if (ASCII_ISLOWER(*p
))
22062 return VAR_FLAVOUR_SESSION
;
22063 return VAR_FLAVOUR_VIMINFO
;
22066 return VAR_FLAVOUR_DEFAULT
;
22070 #if defined(FEAT_VIMINFO) || defined(PROTO)
22072 * Restore global vars that start with a capital from the viminfo file
22075 read_viminfo_varlist(virp
, writing
)
22080 int type
= VAR_NUMBER
;
22083 if (!writing
&& (find_viminfo_parameter('!') != NULL
))
22085 tab
= vim_strchr(virp
->vir_line
+ 1, '\t');
22088 *tab
++ = '\0'; /* isolate the variable name */
22089 if (*tab
== 'S') /* string var */
22092 else if (*tab
== 'F')
22096 tab
= vim_strchr(tab
, '\t');
22100 if (type
== VAR_STRING
)
22101 tv
.vval
.v_string
= viminfo_readstring(virp
,
22102 (int)(tab
- virp
->vir_line
+ 1), TRUE
);
22104 else if (type
== VAR_FLOAT
)
22105 (void)string2float(tab
+ 1, &tv
.vval
.v_float
);
22108 tv
.vval
.v_number
= atol((char *)tab
+ 1);
22109 set_var(virp
->vir_line
+ 1, &tv
, FALSE
);
22110 if (type
== VAR_STRING
)
22111 vim_free(tv
.vval
.v_string
);
22116 return viminfo_readline(virp
);
22120 * Write global vars that start with a capital to the viminfo file
22123 write_viminfo_varlist(fp
)
22127 dictitem_T
*this_var
;
22132 char_u numbuf
[NUMBUFLEN
];
22134 if (find_viminfo_parameter('!') == NULL
)
22137 fprintf(fp
, _("\n# global variables:\n"));
22139 todo
= (int)globvarht
.ht_used
;
22140 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
22142 if (!HASHITEM_EMPTY(hi
))
22145 this_var
= HI2DI(hi
);
22146 if (var_flavour(this_var
->di_key
) == VAR_FLAVOUR_VIMINFO
)
22148 switch (this_var
->di_tv
.v_type
)
22150 case VAR_STRING
: s
= "STR"; break;
22151 case VAR_NUMBER
: s
= "NUM"; break;
22153 case VAR_FLOAT
: s
= "FLO"; break;
22157 fprintf(fp
, "!%s\t%s\t", this_var
->di_key
, s
);
22158 p
= echo_string(&this_var
->di_tv
, &tofree
, numbuf
, 0);
22160 viminfo_writestring(fp
, p
);
22168 #if defined(FEAT_SESSION) || defined(PROTO)
22170 store_session_globals(fd
)
22174 dictitem_T
*this_var
;
22178 todo
= (int)globvarht
.ht_used
;
22179 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
22181 if (!HASHITEM_EMPTY(hi
))
22184 this_var
= HI2DI(hi
);
22185 if ((this_var
->di_tv
.v_type
== VAR_NUMBER
22186 || this_var
->di_tv
.v_type
== VAR_STRING
)
22187 && var_flavour(this_var
->di_key
) == VAR_FLAVOUR_SESSION
)
22189 /* Escape special characters with a backslash. Turn a LF and
22190 * CR into \n and \r. */
22191 p
= vim_strsave_escaped(get_tv_string(&this_var
->di_tv
),
22192 (char_u
*)"\\\"\n\r");
22193 if (p
== NULL
) /* out of memory */
22195 for (t
= p
; *t
!= NUL
; ++t
)
22198 else if (*t
== '\r')
22200 if ((fprintf(fd
, "let %s = %c%s%c",
22202 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
22205 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
22207 || put_eol(fd
) == FAIL
)
22215 else if (this_var
->di_tv
.v_type
== VAR_FLOAT
22216 && var_flavour(this_var
->di_key
) == VAR_FLAVOUR_SESSION
)
22218 float_T f
= this_var
->di_tv
.vval
.v_float
;
22226 if ((fprintf(fd
, "let %s = %c&%f",
22227 this_var
->di_key
, sign
, f
) < 0)
22228 || put_eol(fd
) == FAIL
)
22239 * Display script name where an item was last set.
22240 * Should only be invoked when 'verbose' is non-zero.
22243 last_set_msg(scriptID
)
22250 p
= home_replace_save(NULL
, get_scriptname(scriptID
));
22254 MSG_PUTS(_("\n\tLast set from "));
22263 * List v:oldfiles in a nice way.
22267 exarg_T
*eap UNUSED
;
22269 list_T
*l
= vimvars
[VV_OLDFILES
].vv_list
;
22274 msg((char_u
*)_("No old files"));
22279 for (li
= l
->lv_first
; li
!= NULL
&& !got_int
; li
= li
->li_next
)
22281 msg_outnum((long)++nr
);
22283 msg_outtrans(get_tv_string(&li
->li_tv
));
22285 out_flush(); /* output one line at a time */
22288 /* Assume "got_int" was set to truncate the listing. */
22291 #ifdef FEAT_BROWSE_CMD
22295 nr
= prompt_for_number(FALSE
);
22299 char_u
*p
= list_find_str(get_vim_var_list(VV_OLDFILES
),
22304 p
= expand_env_save(p
);
22306 eap
->cmdidx
= CMD_edit
;
22307 cmdmod
.browse
= FALSE
;
22308 do_exedit(eap
, NULL
);
22317 #endif /* FEAT_EVAL */
22320 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
22324 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22326 static int get_short_pathname
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
22327 static int shortpath_for_invalid_fname
__ARGS((char_u
**fname
, char_u
**bufp
, int *fnamelen
));
22328 static int shortpath_for_partial
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
22331 * Get the short path (8.3) for the filename in "fnamep".
22332 * Only works for a valid file name.
22333 * When the path gets longer "fnamep" is changed and the allocated buffer
22334 * is put in "bufp".
22335 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22336 * Returns OK on success, FAIL on failure.
22339 get_short_pathname(fnamep
, bufp
, fnamelen
)
22348 l
= GetShortPathName(*fnamep
, *fnamep
, len
);
22351 /* If that doesn't work (not enough space), then save the string
22352 * and try again with a new buffer big enough. */
22353 newbuf
= vim_strnsave(*fnamep
, l
);
22354 if (newbuf
== NULL
)
22358 *fnamep
= *bufp
= newbuf
;
22360 /* Really should always succeed, as the buffer is big enough. */
22361 l
= GetShortPathName(*fnamep
, *fnamep
, l
+1);
22369 * Get the short path (8.3) for the filename in "fname". The converted
22370 * path is returned in "bufp".
22372 * Some of the directories specified in "fname" may not exist. This function
22373 * will shorten the existing directories at the beginning of the path and then
22374 * append the remaining non-existing path.
22376 * fname - Pointer to the filename to shorten. On return, contains the
22377 * pointer to the shortened pathname
22378 * bufp - Pointer to an allocated buffer for the filename.
22379 * fnamelen - Length of the filename pointed to by fname
22381 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
22384 shortpath_for_invalid_fname(fname
, bufp
, fnamelen
)
22389 char_u
*short_fname
, *save_fname
, *pbuf_unused
;
22390 char_u
*endp
, *save_endp
;
22393 int new_len
, sfx_len
;
22397 old_len
= *fnamelen
;
22398 save_fname
= vim_strnsave(*fname
, old_len
);
22399 pbuf_unused
= NULL
;
22400 short_fname
= NULL
;
22402 endp
= save_fname
+ old_len
- 1; /* Find the end of the copy */
22406 * Try shortening the supplied path till it succeeds by removing one
22407 * directory at a time from the tail of the path.
22412 /* go back one path-separator */
22413 while (endp
> save_fname
&& !after_pathsep(save_fname
, endp
+ 1))
22415 if (endp
<= save_fname
)
22416 break; /* processed the complete path */
22419 * Replace the path separator with a NUL and try to shorten the
22424 short_fname
= save_fname
;
22425 len
= (int)STRLEN(short_fname
) + 1;
22426 if (get_short_pathname(&short_fname
, &pbuf_unused
, &len
) == FAIL
)
22431 *endp
= ch
; /* preserve the string */
22434 break; /* successfully shortened the path */
22436 /* failed to shorten the path. Skip the path separator */
22443 * Succeeded in shortening the path. Now concatenate the shortened
22444 * path with the remaining path at the tail.
22447 /* Compute the length of the new path. */
22448 sfx_len
= (int)(save_endp
- endp
) + 1;
22449 new_len
= len
+ sfx_len
;
22451 *fnamelen
= new_len
;
22453 if (new_len
> old_len
)
22455 /* There is not enough space in the currently allocated string,
22456 * copy it to a buffer big enough. */
22457 *fname
= *bufp
= vim_strnsave(short_fname
, new_len
);
22458 if (*fname
== NULL
)
22466 /* Transfer short_fname to the main buffer (it's big enough),
22467 * unless get_short_pathname() did its work in-place. */
22468 *fname
= *bufp
= save_fname
;
22469 if (short_fname
!= save_fname
)
22470 vim_strncpy(save_fname
, short_fname
, len
);
22474 /* concat the not-shortened part of the path */
22475 vim_strncpy(*fname
+ len
, endp
, sfx_len
);
22476 (*fname
)[new_len
] = NUL
;
22480 vim_free(pbuf_unused
);
22481 vim_free(save_fname
);
22487 * Get a pathname for a partial path.
22488 * Returns OK for success, FAIL for failure.
22491 shortpath_for_partial(fnamep
, bufp
, fnamelen
)
22496 int sepcount
, len
, tflen
;
22498 char_u
*pbuf
, *tfname
;
22501 /* Count up the path separators from the RHS.. so we know which part
22502 * of the path to return. */
22504 for (p
= *fnamep
; p
< *fnamep
+ *fnamelen
; mb_ptr_adv(p
))
22505 if (vim_ispathsep(*p
))
22508 /* Need full path first (use expand_env() to remove a "~/") */
22509 hasTilde
= (**fnamep
== '~');
22511 pbuf
= tfname
= expand_env_save(*fnamep
);
22513 pbuf
= tfname
= FullName_save(*fnamep
, FALSE
);
22515 len
= tflen
= (int)STRLEN(tfname
);
22517 if (get_short_pathname(&tfname
, &pbuf
, &len
) == FAIL
)
22522 /* Don't have a valid filename, so shorten the rest of the
22523 * path if we can. This CAN give us invalid 8.3 filenames, but
22524 * there's not a lot of point in guessing what it might be.
22527 if (shortpath_for_invalid_fname(&tfname
, &pbuf
, &len
) == FAIL
)
22531 /* Count the paths backward to find the beginning of the desired string. */
22532 for (p
= tfname
+ len
- 1; p
>= tfname
; --p
)
22536 p
-= mb_head_off(tfname
, p
);
22538 if (vim_ispathsep(*p
))
22540 if (sepcount
== 0 || (hasTilde
&& sepcount
== 1))
22557 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22559 *fnamelen
= (int)STRLEN(p
);
22565 #endif /* WIN3264 */
22568 * Adjust a filename, according to a string of modifiers.
22569 * *fnamep must be NUL terminated when called. When returning, the length is
22570 * determined by *fnamelen.
22571 * Returns VALID_ flags or -1 for failure.
22572 * When there is an error, *fnamep is set to NULL.
22575 modify_fname(src
, usedlen
, fnamep
, bufp
, fnamelen
)
22576 char_u
*src
; /* string with modifiers */
22577 int *usedlen
; /* characters after src that are used */
22578 char_u
**fnamep
; /* file name so far */
22579 char_u
**bufp
; /* buffer for allocated file name or NULL */
22580 int *fnamelen
; /* length of fnamep */
22584 char_u
*s
, *p
, *pbuf
;
22585 char_u dirname
[MAXPATHL
];
22587 int has_fullname
= 0;
22589 int has_shortname
= 0;
22593 /* ":p" - full path/file_name */
22594 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'p')
22598 valid
|= VALID_PATH
;
22601 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22602 if ((*fnamep
)[0] == '~'
22603 #if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22604 && ((*fnamep
)[1] == '/'
22605 # ifdef BACKSLASH_IN_FILENAME
22606 || (*fnamep
)[1] == '\\'
22608 || (*fnamep
)[1] == NUL
)
22613 *fnamep
= expand_env_save(*fnamep
);
22614 vim_free(*bufp
); /* free any allocated file name */
22616 if (*fnamep
== NULL
)
22620 /* When "/." or "/.." is used: force expansion to get rid of it. */
22621 for (p
= *fnamep
; *p
!= NUL
; mb_ptr_adv(p
))
22623 if (vim_ispathsep(*p
)
22626 || vim_ispathsep(p
[2])
22628 && (p
[3] == NUL
|| vim_ispathsep(p
[3])))))
22632 /* FullName_save() is slow, don't use it when not needed. */
22633 if (*p
!= NUL
|| !vim_isAbsName(*fnamep
))
22635 *fnamep
= FullName_save(*fnamep
, *p
!= NUL
);
22636 vim_free(*bufp
); /* free any allocated file name */
22638 if (*fnamep
== NULL
)
22642 /* Append a path separator to a directory. */
22643 if (mch_isdir(*fnamep
))
22645 /* Make room for one or two extra characters. */
22646 *fnamep
= vim_strnsave(*fnamep
, (int)STRLEN(*fnamep
) + 2);
22647 vim_free(*bufp
); /* free any allocated file name */
22649 if (*fnamep
== NULL
)
22651 add_pathsep(*fnamep
);
22655 /* ":." - path relative to the current directory */
22656 /* ":~" - path relative to the home directory */
22657 /* ":8" - shortname path - postponed till after */
22658 while (src
[*usedlen
] == ':'
22659 && ((c
= src
[*usedlen
+ 1]) == '.' || c
== '~' || c
== '8'))
22665 has_shortname
= 1; /* Postpone this. */
22670 /* Need full path first (use expand_env() to remove a "~/") */
22673 if (c
== '.' && **fnamep
== '~')
22674 p
= pbuf
= expand_env_save(*fnamep
);
22676 p
= pbuf
= FullName_save(*fnamep
, FALSE
);
22687 mch_dirname(dirname
, MAXPATHL
);
22688 s
= shorten_fname(p
, dirname
);
22694 vim_free(*bufp
); /* free any allocated file name */
22702 home_replace(NULL
, p
, dirname
, MAXPATHL
, TRUE
);
22703 /* Only replace it when it starts with '~' */
22704 if (*dirname
== '~')
22706 s
= vim_strsave(dirname
);
22719 tail
= gettail(*fnamep
);
22720 *fnamelen
= (int)STRLEN(*fnamep
);
22722 /* ":h" - head, remove "/file_name", can be repeated */
22723 /* Don't remove the first "/" or "c:\" */
22724 while (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'h')
22726 valid
|= VALID_HEAD
;
22728 s
= get_past_head(*fnamep
);
22729 while (tail
> s
&& after_pathsep(s
, tail
))
22730 mb_ptr_back(*fnamep
, tail
);
22731 *fnamelen
= (int)(tail
- *fnamep
);
22734 *fnamelen
+= 1; /* the path separator is part of the path */
22736 if (*fnamelen
== 0)
22738 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22739 p
= vim_strsave((char_u
*)".");
22743 *bufp
= *fnamep
= tail
= p
;
22748 while (tail
> s
&& !after_pathsep(s
, tail
))
22749 mb_ptr_back(*fnamep
, tail
);
22753 /* ":8" - shortname */
22754 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == '8')
22763 /* Check shortname after we have done 'heads' and before we do 'tails'
22768 /* Copy the string if it is shortened by :h */
22769 if (*fnamelen
< (int)STRLEN(*fnamep
))
22771 p
= vim_strnsave(*fnamep
, *fnamelen
);
22775 *bufp
= *fnamep
= p
;
22778 /* Split into two implementations - makes it easier. First is where
22779 * there isn't a full name already, second is where there is.
22781 if (!has_fullname
&& !vim_isAbsName(*fnamep
))
22783 if (shortpath_for_partial(fnamep
, bufp
, fnamelen
) == FAIL
)
22790 /* Simple case, already have the full-name
22791 * Nearly always shorter, so try first time. */
22793 if (get_short_pathname(fnamep
, bufp
, &l
) == FAIL
)
22798 /* Couldn't find the filename.. search the paths.
22801 if (shortpath_for_invalid_fname(fnamep
, bufp
, &l
) == FAIL
)
22807 #endif /* WIN3264 */
22809 /* ":t" - tail, just the basename */
22810 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 't')
22813 *fnamelen
-= (int)(tail
- *fnamep
);
22817 /* ":e" - extension, can be repeated */
22818 /* ":r" - root, without extension, can be repeated */
22819 while (src
[*usedlen
] == ':'
22820 && (src
[*usedlen
+ 1] == 'e' || src
[*usedlen
+ 1] == 'r'))
22822 /* find a '.' in the tail:
22823 * - for second :e: before the current fname
22824 * - otherwise: The last '.'
22826 if (src
[*usedlen
+ 1] == 'e' && *fnamep
> tail
)
22829 s
= *fnamep
+ *fnamelen
- 1;
22830 for ( ; s
> tail
; --s
)
22833 if (src
[*usedlen
+ 1] == 'e') /* :e */
22837 *fnamelen
+= (int)(*fnamep
- (s
+ 1));
22840 /* cut version from the extension */
22841 s
= *fnamep
+ *fnamelen
- 1;
22842 for ( ; s
> *fnamep
; --s
)
22846 *fnamelen
= s
- *fnamep
;
22849 else if (*fnamep
<= tail
)
22854 if (s
> tail
) /* remove one extension */
22855 *fnamelen
= (int)(s
- *fnamep
);
22860 /* ":s?pat?foo?" - substitute */
22861 /* ":gs?pat?foo?" - global substitute */
22862 if (src
[*usedlen
] == ':'
22863 && (src
[*usedlen
+ 1] == 's'
22864 || (src
[*usedlen
+ 1] == 'g' && src
[*usedlen
+ 2] == 's')))
22873 flags
= (char_u
*)"";
22874 s
= src
+ *usedlen
+ 2;
22875 if (src
[*usedlen
+ 1] == 'g')
22877 flags
= (char_u
*)"g";
22884 /* find end of pattern */
22885 p
= vim_strchr(s
, sep
);
22888 pat
= vim_strnsave(s
, (int)(p
- s
));
22892 /* find end of substitution */
22893 p
= vim_strchr(s
, sep
);
22896 sub
= vim_strnsave(s
, (int)(p
- s
));
22897 str
= vim_strnsave(*fnamep
, *fnamelen
);
22898 if (sub
!= NULL
&& str
!= NULL
)
22900 *usedlen
= (int)(p
+ 1 - src
);
22901 s
= do_string_sub(str
, pat
, sub
, flags
);
22905 *fnamelen
= (int)STRLEN(s
);
22917 /* after using ":s", repeat all the modifiers */
22927 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22928 * "flags" can be "g" to do a global substitute.
22929 * Returns an allocated string, NULL for error.
22932 do_string_sub(str
, pat
, sub
, flags
)
22939 regmatch_T regmatch
;
22947 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22949 p_cpo
= empty_option
;
22951 ga_init2(&ga
, 1, 200);
22953 do_all
= (flags
[0] == 'g');
22955 regmatch
.rm_ic
= p_ic
;
22956 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
22957 if (regmatch
.regprog
!= NULL
)
22960 while (vim_regexec_nl(®match
, str
, (colnr_T
)(tail
- str
)))
22963 * Get some space for a temporary buffer to do the substitution
22964 * into. It will contain:
22965 * - The text up to where the match is.
22966 * - The substituted text.
22967 * - The text after the match.
22969 sublen
= vim_regsub(®match
, sub
, tail
, FALSE
, TRUE
, FALSE
);
22970 if (ga_grow(&ga
, (int)(STRLEN(tail
) + sublen
-
22971 (regmatch
.endp
[0] - regmatch
.startp
[0]))) == FAIL
)
22977 /* copy the text up to where the match is */
22978 i
= (int)(regmatch
.startp
[0] - tail
);
22979 mch_memmove((char_u
*)ga
.ga_data
+ ga
.ga_len
, tail
, (size_t)i
);
22980 /* add the substituted text */
22981 (void)vim_regsub(®match
, sub
, (char_u
*)ga
.ga_data
22982 + ga
.ga_len
+ i
, TRUE
, TRUE
, FALSE
);
22983 ga
.ga_len
+= i
+ sublen
- 1;
22984 /* avoid getting stuck on a match with an empty string */
22985 if (tail
== regmatch
.endp
[0])
22989 *((char_u
*)ga
.ga_data
+ ga
.ga_len
) = *tail
++;
22994 tail
= regmatch
.endp
[0];
23002 if (ga
.ga_data
!= NULL
)
23003 STRCPY((char *)ga
.ga_data
+ ga
.ga_len
, tail
);
23005 vim_free(regmatch
.regprog
);
23008 ret
= vim_strsave(ga
.ga_data
== NULL
? str
: (char_u
*)ga
.ga_data
);
23010 if (p_cpo
== empty_option
)
23013 /* Darn, evaluating {sub} expression changed the value. */
23014 free_string_option(save_cpo
);
23019 #endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */