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
*func_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
);
886 /* global variables */
887 vars_clear(&globvarht
);
889 /* autoloaded script names */
890 ga_clear_strings(&ga_loaded
);
892 /* script-local variables */
893 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
895 vars_clear(&SCRIPT_VARS(i
));
896 vim_free(SCRIPT_SV(i
));
898 ga_clear(&ga_scripts
);
900 /* unreferenced lists and dicts */
901 (void)garbage_collect();
904 free_all_functions();
905 hash_clear(&func_hashtab
);
910 * Return the name of the executed function.
916 return ((funccall_T
*)cookie
)->func
->uf_name
;
920 * Return the address holding the next breakpoint line for a funccall cookie.
923 func_breakpoint(cookie
)
926 return &((funccall_T
*)cookie
)->breakpoint
;
930 * Return the address holding the debug tick for a funccall cookie.
933 func_dbg_tick(cookie
)
936 return &((funccall_T
*)cookie
)->dbg_tick
;
940 * Return the nesting level for a funccall cookie.
946 return ((funccall_T
*)cookie
)->level
;
949 /* pointer to funccal for currently active function */
950 funccall_T
*current_funccal
= NULL
;
952 /* pointer to list of previously used funccal, still around because some
953 * item in it is still being used. */
954 funccall_T
*previous_funccal
= NULL
;
957 * Return TRUE when a function was ended by a ":return" command.
960 current_func_returned()
962 return current_funccal
->returned
;
967 * Set an internal variable to a string value. Creates the variable if it does
971 set_internal_string_var(name
, value
)
978 val
= vim_strsave(value
);
981 tvp
= alloc_string_tv(val
);
984 set_var(name
, tvp
, FALSE
);
990 static lval_T
*redir_lval
= NULL
;
991 static garray_T redir_ga
; /* only valid when redir_lval is not NULL */
992 static char_u
*redir_endp
= NULL
;
993 static char_u
*redir_varname
= NULL
;
996 * Start recording command output to a variable
997 * Returns OK if successfully completed the setup. FAIL otherwise.
1000 var_redir_start(name
, append
)
1002 int append
; /* append to an existing variable */
1008 /* Catch a bad name early. */
1009 if (!eval_isnamec1(*name
))
1015 /* Make a copy of the name, it is used in redir_lval until redir ends. */
1016 redir_varname
= vim_strsave(name
);
1017 if (redir_varname
== NULL
)
1020 redir_lval
= (lval_T
*)alloc_clear((unsigned)sizeof(lval_T
));
1021 if (redir_lval
== NULL
)
1027 /* The output is stored in growarray "redir_ga" until redirection ends. */
1028 ga_init2(&redir_ga
, (int)sizeof(char), 500);
1030 /* Parse the variable name (can be a dict or list entry). */
1031 redir_endp
= get_lval(redir_varname
, NULL
, redir_lval
, FALSE
, FALSE
, FALSE
,
1033 if (redir_endp
== NULL
|| redir_lval
->ll_name
== NULL
|| *redir_endp
!= NUL
)
1035 if (redir_endp
!= NULL
&& *redir_endp
!= NUL
)
1036 /* Trailing characters are present after the variable name */
1037 EMSG(_(e_trailing
));
1040 redir_endp
= NULL
; /* don't store a value, only cleanup */
1045 /* check if we can write to the variable: set it to or append an empty
1047 save_emsg
= did_emsg
;
1049 tv
.v_type
= VAR_STRING
;
1050 tv
.vval
.v_string
= (char_u
*)"";
1052 set_var_lval(redir_lval
, redir_endp
, &tv
, TRUE
, (char_u
*)".");
1054 set_var_lval(redir_lval
, redir_endp
, &tv
, TRUE
, (char_u
*)"=");
1056 did_emsg
|= save_emsg
;
1059 redir_endp
= NULL
; /* don't store a value, only cleanup */
1063 if (redir_lval
->ll_newkey
!= NULL
)
1065 /* Dictionary item was created, don't do it again. */
1066 vim_free(redir_lval
->ll_newkey
);
1067 redir_lval
->ll_newkey
= NULL
;
1074 * Append "value[value_len]" to the variable set by var_redir_start().
1075 * The actual appending is postponed until redirection ends, because the value
1076 * appended may in fact be the string we write to, changing it may cause freed
1077 * memory to be used:
1083 var_redir_str(value
, value_len
)
1089 if (redir_lval
== NULL
)
1092 if (value_len
== -1)
1093 len
= (int)STRLEN(value
); /* Append the entire string */
1095 len
= value_len
; /* Append only "value_len" characters */
1097 if (ga_grow(&redir_ga
, len
) == OK
)
1099 mch_memmove((char *)redir_ga
.ga_data
+ redir_ga
.ga_len
, value
, len
);
1100 redir_ga
.ga_len
+= len
;
1107 * Stop redirecting command output to a variable.
1108 * Frees the allocated memory.
1115 if (redir_lval
!= NULL
)
1117 /* If there was no error: assign the text to the variable. */
1118 if (redir_endp
!= NULL
)
1120 ga_append(&redir_ga
, NUL
); /* Append the trailing NUL. */
1121 tv
.v_type
= VAR_STRING
;
1122 tv
.vval
.v_string
= redir_ga
.ga_data
;
1123 set_var_lval(redir_lval
, redir_endp
, &tv
, FALSE
, (char_u
*)".");
1126 /* free the collected output */
1127 vim_free(redir_ga
.ga_data
);
1128 redir_ga
.ga_data
= NULL
;
1130 clear_lval(redir_lval
);
1131 vim_free(redir_lval
);
1134 vim_free(redir_varname
);
1135 redir_varname
= NULL
;
1138 # if defined(FEAT_MBYTE) || defined(PROTO)
1140 eval_charconvert(enc_from
, enc_to
, fname_from
, fname_to
)
1148 set_vim_var_string(VV_CC_FROM
, enc_from
, -1);
1149 set_vim_var_string(VV_CC_TO
, enc_to
, -1);
1150 set_vim_var_string(VV_FNAME_IN
, fname_from
, -1);
1151 set_vim_var_string(VV_FNAME_OUT
, fname_to
, -1);
1152 if (eval_to_bool(p_ccv
, &err
, NULL
, FALSE
))
1154 set_vim_var_string(VV_CC_FROM
, NULL
, -1);
1155 set_vim_var_string(VV_CC_TO
, NULL
, -1);
1156 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1157 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1165 # if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1167 eval_printexpr(fname
, args
)
1173 set_vim_var_string(VV_FNAME_IN
, fname
, -1);
1174 set_vim_var_string(VV_CMDARG
, args
, -1);
1175 if (eval_to_bool(p_pexpr
, &err
, NULL
, FALSE
))
1177 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1178 set_vim_var_string(VV_CMDARG
, NULL
, -1);
1189 # if defined(FEAT_DIFF) || defined(PROTO)
1191 eval_diff(origfile
, newfile
, outfile
)
1198 set_vim_var_string(VV_FNAME_IN
, origfile
, -1);
1199 set_vim_var_string(VV_FNAME_NEW
, newfile
, -1);
1200 set_vim_var_string(VV_FNAME_OUT
, outfile
, -1);
1201 (void)eval_to_bool(p_dex
, &err
, NULL
, FALSE
);
1202 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1203 set_vim_var_string(VV_FNAME_NEW
, NULL
, -1);
1204 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1208 eval_patch(origfile
, difffile
, outfile
)
1215 set_vim_var_string(VV_FNAME_IN
, origfile
, -1);
1216 set_vim_var_string(VV_FNAME_DIFF
, difffile
, -1);
1217 set_vim_var_string(VV_FNAME_OUT
, outfile
, -1);
1218 (void)eval_to_bool(p_pex
, &err
, NULL
, FALSE
);
1219 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1220 set_vim_var_string(VV_FNAME_DIFF
, NULL
, -1);
1221 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1226 * Top level evaluation function, returning a boolean.
1227 * Sets "error" to TRUE if there was an error.
1228 * Return TRUE or FALSE.
1231 eval_to_bool(arg
, error
, nextcmd
, skip
)
1235 int skip
; /* only parse, don't execute */
1242 if (eval0(arg
, &tv
, nextcmd
, !skip
) == FAIL
)
1249 retval
= (get_tv_number_chk(&tv
, error
) != 0);
1260 * Top level evaluation function, returning a string. If "skip" is TRUE,
1261 * only parsing to "nextcmd" is done, without reporting errors. Return
1262 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1265 eval_to_string_skip(arg
, nextcmd
, skip
)
1268 int skip
; /* only parse, don't execute */
1275 if (eval0(arg
, &tv
, nextcmd
, !skip
) == FAIL
|| skip
)
1279 retval
= vim_strsave(get_tv_string(&tv
));
1289 * Skip over an expression at "*pp".
1290 * Return FAIL for an error, OK otherwise.
1298 *pp
= skipwhite(*pp
);
1299 return eval1(pp
, &rettv
, FALSE
);
1303 * Top level evaluation function, returning a string.
1304 * When "convert" is TRUE convert a List into a sequence of lines and convert
1305 * a Float to a String.
1306 * Return pointer to allocated memory, or NULL for failure.
1309 eval_to_string(arg
, nextcmd
, convert
)
1318 char_u numbuf
[NUMBUFLEN
];
1321 if (eval0(arg
, &tv
, nextcmd
, TRUE
) == FAIL
)
1325 if (convert
&& tv
.v_type
== VAR_LIST
)
1327 ga_init2(&ga
, (int)sizeof(char), 80);
1328 if (tv
.vval
.v_list
!= NULL
)
1329 list_join(&ga
, tv
.vval
.v_list
, (char_u
*)"\n", TRUE
, 0);
1330 ga_append(&ga
, NUL
);
1331 retval
= (char_u
*)ga
.ga_data
;
1334 else if (convert
&& tv
.v_type
== VAR_FLOAT
)
1336 vim_snprintf((char *)numbuf
, NUMBUFLEN
, "%g", tv
.vval
.v_float
);
1337 retval
= vim_strsave(numbuf
);
1341 retval
= vim_strsave(get_tv_string(&tv
));
1349 * Call eval_to_string() without using current local variables and using
1350 * textlock. When "use_sandbox" is TRUE use the sandbox.
1353 eval_to_string_safe(arg
, nextcmd
, use_sandbox
)
1359 void *save_funccalp
;
1361 save_funccalp
= save_funccal();
1365 retval
= eval_to_string(arg
, nextcmd
, FALSE
);
1369 restore_funccal(save_funccalp
);
1374 * Top level evaluation function, returning a number.
1375 * Evaluates "expr" silently.
1376 * Returns -1 for an error.
1379 eval_to_number(expr
)
1384 char_u
*p
= skipwhite(expr
);
1388 if (eval1(&p
, &rettv
, TRUE
) == FAIL
)
1392 retval
= get_tv_number_chk(&rettv
, NULL
);
1401 * Prepare v: variable "idx" to be used.
1402 * Save the current typeval in "save_tv".
1403 * When not used yet add the variable to the v: hashtable.
1406 prepare_vimvar(idx
, save_tv
)
1410 *save_tv
= vimvars
[idx
].vv_tv
;
1411 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1412 hash_add(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1416 * Restore v: variable "idx" to typeval "save_tv".
1417 * When no longer defined, remove the variable from the v: hashtable.
1420 restore_vimvar(idx
, save_tv
)
1426 vimvars
[idx
].vv_tv
= *save_tv
;
1427 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1429 hi
= hash_find(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1430 if (HASHITEM_EMPTY(hi
))
1431 EMSG2(_(e_intern2
), "restore_vimvar()");
1433 hash_remove(&vimvarht
, hi
);
1437 #if defined(FEAT_SPELL) || defined(PROTO)
1439 * Evaluate an expression to a list with suggestions.
1440 * For the "expr:" part of 'spellsuggest'.
1441 * Returns NULL when there is an error.
1444 eval_spell_expr(badword
, expr
)
1450 list_T
*list
= NULL
;
1451 char_u
*p
= skipwhite(expr
);
1453 /* Set "v:val" to the bad word. */
1454 prepare_vimvar(VV_VAL
, &save_val
);
1455 vimvars
[VV_VAL
].vv_type
= VAR_STRING
;
1456 vimvars
[VV_VAL
].vv_str
= badword
;
1460 if (eval1(&p
, &rettv
, TRUE
) == OK
)
1462 if (rettv
.v_type
!= VAR_LIST
)
1465 list
= rettv
.vval
.v_list
;
1470 restore_vimvar(VV_VAL
, &save_val
);
1476 * "list" is supposed to contain two items: a word and a number. Return the
1477 * word in "pp" and the number as the return value.
1478 * Return -1 if anything isn't right.
1479 * Used to get the good word and score from the eval_spell_expr() result.
1482 get_spellword(list
, pp
)
1488 li
= list
->lv_first
;
1491 *pp
= get_tv_string(&li
->li_tv
);
1496 return get_tv_number(&li
->li_tv
);
1501 * Top level evaluation function.
1502 * Returns an allocated typval_T with the result.
1503 * Returns NULL when there is an error.
1506 eval_expr(arg
, nextcmd
)
1512 tv
= (typval_T
*)alloc(sizeof(typval_T
));
1513 if (tv
!= NULL
&& eval0(arg
, tv
, nextcmd
, TRUE
) == FAIL
)
1523 #if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1524 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1526 * Call some vimL function and return the result in "*rettv".
1527 * Uses argv[argc] for the function arguments. Only Number and String
1528 * arguments are currently supported.
1529 * Returns OK or FAIL.
1532 call_vim_function(func
, argc
, argv
, safe
, rettv
)
1536 int safe
; /* use the sandbox */
1544 void *save_funccalp
= NULL
;
1547 argvars
= (typval_T
*)alloc((unsigned)((argc
+ 1) * sizeof(typval_T
)));
1548 if (argvars
== NULL
)
1551 for (i
= 0; i
< argc
; i
++)
1553 /* Pass a NULL or empty argument as an empty string */
1554 if (argv
[i
] == NULL
|| *argv
[i
] == NUL
)
1556 argvars
[i
].v_type
= VAR_STRING
;
1557 argvars
[i
].vval
.v_string
= (char_u
*)"";
1561 /* Recognize a number argument, the others must be strings. */
1562 vim_str2nr(argv
[i
], NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
1563 if (len
!= 0 && len
== (int)STRLEN(argv
[i
]))
1565 argvars
[i
].v_type
= VAR_NUMBER
;
1566 argvars
[i
].vval
.v_number
= n
;
1570 argvars
[i
].v_type
= VAR_STRING
;
1571 argvars
[i
].vval
.v_string
= argv
[i
];
1577 save_funccalp
= save_funccal();
1581 rettv
->v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
1582 ret
= call_func(func
, (int)STRLEN(func
), rettv
, argc
, argvars
,
1583 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
1584 &doesrange
, TRUE
, NULL
);
1588 restore_funccal(save_funccalp
);
1598 # if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1600 * Call vimL function "func" and return the result as a string.
1601 * Returns NULL when calling the function fails.
1602 * Uses argv[argc] for the function arguments.
1605 call_func_retstr(func
, argc
, argv
, safe
)
1609 int safe
; /* use the sandbox */
1614 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1617 retval
= vim_strsave(get_tv_string(&rettv
));
1623 # if defined(FEAT_COMPL_FUNC) || defined(PROTO)
1625 * Call vimL function "func" and return the result as a number.
1626 * Returns -1 when calling the function fails.
1627 * Uses argv[argc] for the function arguments.
1630 call_func_retnr(func
, argc
, argv
, safe
)
1634 int safe
; /* use the sandbox */
1639 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1642 retval
= get_tv_number_chk(&rettv
, NULL
);
1649 * Call vimL function "func" and return the result as a List.
1650 * Uses argv[argc] for the function arguments.
1651 * Returns NULL when there is something wrong.
1654 call_func_retlist(func
, argc
, argv
, safe
)
1658 int safe
; /* use the sandbox */
1662 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1665 if (rettv
.v_type
!= VAR_LIST
)
1671 return rettv
.vval
.v_list
;
1677 * Save the current function call pointer, and set it to NULL.
1678 * Used when executing autocommands and for ":source".
1683 funccall_T
*fc
= current_funccal
;
1685 current_funccal
= NULL
;
1690 restore_funccal(vfc
)
1693 funccall_T
*fc
= (funccall_T
*)vfc
;
1695 current_funccal
= fc
;
1698 #if defined(FEAT_PROFILE) || defined(PROTO)
1700 * Prepare profiling for entering a child or something else that is not
1701 * counted for the script/function itself.
1702 * Should always be called in pair with prof_child_exit().
1705 prof_child_enter(tm
)
1706 proftime_T
*tm
; /* place to store waittime */
1708 funccall_T
*fc
= current_funccal
;
1710 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1711 profile_start(&fc
->prof_child
);
1712 script_prof_save(tm
);
1716 * Take care of time spent in a child.
1717 * Should always be called after prof_child_enter().
1721 proftime_T
*tm
; /* where waittime was stored */
1723 funccall_T
*fc
= current_funccal
;
1725 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1727 profile_end(&fc
->prof_child
);
1728 profile_sub_wait(tm
, &fc
->prof_child
); /* don't count waiting time */
1729 profile_add(&fc
->func
->uf_tm_children
, &fc
->prof_child
);
1730 profile_add(&fc
->func
->uf_tml_children
, &fc
->prof_child
);
1732 script_prof_restore(tm
);
1739 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1740 * it in "*cp". Doesn't give error messages.
1743 eval_foldexpr(arg
, cp
)
1750 int use_sandbox
= was_set_insecurely((char_u
*)"foldexpr",
1758 if (eval0(arg
, &tv
, NULL
, TRUE
) == FAIL
)
1762 /* If the result is a number, just return the number. */
1763 if (tv
.v_type
== VAR_NUMBER
)
1764 retval
= tv
.vval
.v_number
;
1765 else if (tv
.v_type
!= VAR_STRING
|| tv
.vval
.v_string
== NULL
)
1769 /* If the result is a string, check if there is a non-digit before
1771 s
= tv
.vval
.v_string
;
1772 if (!VIM_ISDIGIT(*s
) && *s
!= '-')
1774 retval
= atol((char *)s
);
1788 * ":let" list all variable values
1789 * ":let var1 var2" list variable values
1790 * ":let var = expr" assignment command.
1791 * ":let var += expr" assignment command.
1792 * ":let var -= expr" assignment command.
1793 * ":let var .= expr" assignment command.
1794 * ":let [var1, var2] = expr" unpack list.
1800 char_u
*arg
= eap
->arg
;
1801 char_u
*expr
= NULL
;
1810 argend
= skip_var_list(arg
, &var_count
, &semicolon
);
1813 if (argend
> arg
&& argend
[-1] == '.') /* for var.='str' */
1815 expr
= vim_strchr(argend
, '=');
1819 * ":let" without "=": list variables
1823 else if (!ends_excmd(*arg
))
1824 /* ":let var1 var2" */
1825 arg
= list_arg_vars(eap
, arg
, &first
);
1826 else if (!eap
->skip
)
1829 list_glob_vars(&first
);
1830 list_buf_vars(&first
);
1831 list_win_vars(&first
);
1833 list_tab_vars(&first
);
1835 list_script_vars(&first
);
1836 list_func_vars(&first
);
1837 list_vim_vars(&first
);
1839 eap
->nextcmd
= check_nextcmd(arg
);
1847 if (vim_strchr((char_u
*)"+-.", expr
[-1]) != NULL
)
1848 op
[0] = expr
[-1]; /* +=, -= or .= */
1850 expr
= skipwhite(expr
+ 1);
1854 i
= eval0(expr
, &rettv
, &eap
->nextcmd
, !eap
->skip
);
1863 (void)ex_let_vars(eap
->arg
, &rettv
, FALSE
, semicolon
, var_count
,
1871 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1872 * Handles both "var" with any type and "[var, var; var]" with a list type.
1873 * When "nextchars" is not NULL it points to a string with characters that
1874 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1876 * Returns OK or FAIL;
1879 ex_let_vars(arg_start
, tv
, copy
, semicolon
, var_count
, nextchars
)
1882 int copy
; /* copy values from "tv", don't move */
1883 int semicolon
; /* from skip_var_list() */
1884 int var_count
; /* from skip_var_list() */
1887 char_u
*arg
= arg_start
;
1896 * ":let var = expr" or ":for var in list"
1898 if (ex_let_one(arg
, tv
, copy
, nextchars
, nextchars
) == NULL
)
1904 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1906 if (tv
->v_type
!= VAR_LIST
|| (l
= tv
->vval
.v_list
) == NULL
)
1913 if (semicolon
== 0 && var_count
< i
)
1915 EMSG(_("E687: Less targets than List items"));
1918 if (var_count
- semicolon
> i
)
1920 EMSG(_("E688: More targets than List items"));
1927 arg
= skipwhite(arg
+ 1);
1928 arg
= ex_let_one(arg
, &item
->li_tv
, TRUE
, (char_u
*)",;]", nextchars
);
1929 item
= item
->li_next
;
1933 arg
= skipwhite(arg
);
1936 /* Put the rest of the list (may be empty) in the var after ';'.
1937 * Create a new list for this. */
1941 while (item
!= NULL
)
1943 list_append_tv(l
, &item
->li_tv
);
1944 item
= item
->li_next
;
1947 ltv
.v_type
= VAR_LIST
;
1949 ltv
.vval
.v_list
= l
;
1952 arg
= ex_let_one(skipwhite(arg
+ 1), <v
, FALSE
,
1953 (char_u
*)"]", nextchars
);
1959 else if (*arg
!= ',' && *arg
!= ']')
1961 EMSG2(_(e_intern2
), "ex_let_vars()");
1970 * Skip over assignable variable "var" or list of variables "[var, var]".
1971 * Used for ":let varvar = expr" and ":for varvar in expr".
1972 * For "[var, var]" increment "*var_count" for each variable.
1973 * for "[var, var; var]" set "semicolon".
1974 * Return NULL for an error.
1977 skip_var_list(arg
, var_count
, semicolon
)
1986 /* "[var, var]": find the matching ']'. */
1990 p
= skipwhite(p
+ 1); /* skip whites after '[', ';' or ',' */
1991 s
= skip_var_one(p
);
1994 EMSG2(_(e_invarg2
), p
);
2004 if (*semicolon
== 1)
2006 EMSG(_("Double ; in list of variables"));
2013 EMSG2(_(e_invarg2
), p
);
2020 return skip_var_one(arg
);
2024 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
2031 if (*arg
== '@' && arg
[1] != NUL
)
2033 return find_name_end(*arg
== '$' || *arg
== '&' ? arg
+ 1 : arg
,
2034 NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
2038 * List variables for hashtab "ht" with prefix "prefix".
2039 * If "empty" is TRUE also list NULL strings as empty strings.
2042 list_hashtable_vars(ht
, prefix
, empty
, first
)
2052 todo
= (int)ht
->ht_used
;
2053 for (hi
= ht
->ht_array
; todo
> 0 && !got_int
; ++hi
)
2055 if (!HASHITEM_EMPTY(hi
))
2059 if (empty
|| di
->di_tv
.v_type
!= VAR_STRING
2060 || di
->di_tv
.vval
.v_string
!= NULL
)
2061 list_one_var(di
, prefix
, first
);
2067 * List global variables.
2070 list_glob_vars(first
)
2073 list_hashtable_vars(&globvarht
, (char_u
*)"", TRUE
, first
);
2077 * List buffer variables.
2080 list_buf_vars(first
)
2083 char_u numbuf
[NUMBUFLEN
];
2085 list_hashtable_vars(&curbuf
->b_vars
.dv_hashtab
, (char_u
*)"b:",
2088 sprintf((char *)numbuf
, "%ld", (long)curbuf
->b_changedtick
);
2089 list_one_var_a((char_u
*)"b:", (char_u
*)"changedtick", VAR_NUMBER
,
2094 * List window variables.
2097 list_win_vars(first
)
2100 list_hashtable_vars(&curwin
->w_vars
.dv_hashtab
,
2101 (char_u
*)"w:", TRUE
, first
);
2106 * List tab page variables.
2109 list_tab_vars(first
)
2112 list_hashtable_vars(&curtab
->tp_vars
.dv_hashtab
,
2113 (char_u
*)"t:", TRUE
, first
);
2118 * List Vim variables.
2121 list_vim_vars(first
)
2124 list_hashtable_vars(&vimvarht
, (char_u
*)"v:", FALSE
, first
);
2128 * List script-local variables, if there is a script.
2131 list_script_vars(first
)
2134 if (current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
2135 list_hashtable_vars(&SCRIPT_VARS(current_SID
),
2136 (char_u
*)"s:", FALSE
, first
);
2140 * List function variables, if there is a function.
2143 list_func_vars(first
)
2146 if (current_funccal
!= NULL
)
2147 list_hashtable_vars(¤t_funccal
->l_vars
.dv_hashtab
,
2148 (char_u
*)"l:", FALSE
, first
);
2152 * List variables in "arg".
2155 list_arg_vars(eap
, arg
, first
)
2168 while (!ends_excmd(*arg
) && !got_int
)
2170 if (error
|| eap
->skip
)
2172 arg
= find_name_end(arg
, NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
2173 if (!vim_iswhite(*arg
) && !ends_excmd(*arg
))
2176 EMSG(_(e_trailing
));
2182 /* get_name_len() takes care of expanding curly braces */
2183 name_start
= name
= arg
;
2184 len
= get_name_len(&arg
, &tofree
, TRUE
, TRUE
);
2187 /* This is mainly to keep test 49 working: when expanding
2188 * curly braces fails overrule the exception error message. */
2189 if (len
< 0 && !aborting())
2192 EMSG2(_(e_invarg2
), arg
);
2201 if (get_var_tv(name
, len
, &tv
, TRUE
) == FAIL
)
2205 /* handle d.key, l[idx], f(expr) */
2207 if (handle_subscript(&arg
, &tv
, TRUE
, TRUE
) == FAIL
)
2211 if (arg
== arg_subsc
&& len
== 2 && name
[1] == ':')
2215 case 'g': list_glob_vars(first
); break;
2216 case 'b': list_buf_vars(first
); break;
2217 case 'w': list_win_vars(first
); break;
2219 case 't': list_tab_vars(first
); break;
2221 case 'v': list_vim_vars(first
); break;
2222 case 's': list_script_vars(first
); break;
2223 case 'l': list_func_vars(first
); break;
2225 EMSG2(_("E738: Can't list variables for %s"), name
);
2230 char_u numbuf
[NUMBUFLEN
];
2235 s
= echo_string(&tv
, &tf
, numbuf
, 0);
2238 list_one_var_a((char_u
*)"",
2239 arg
== arg_subsc
? name
: name_start
,
2241 s
== NULL
? (char_u
*)"" : s
,
2254 arg
= skipwhite(arg
);
2261 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2262 * Returns a pointer to the char just after the var name.
2263 * Returns NULL if there is an error.
2266 ex_let_one(arg
, tv
, copy
, endchars
, op
)
2267 char_u
*arg
; /* points to variable name */
2268 typval_T
*tv
; /* value to assign to variable */
2269 int copy
; /* copy value from "tv" */
2270 char_u
*endchars
; /* valid chars after variable name or NULL */
2271 char_u
*op
; /* "+", "-", "." or NULL*/
2276 char_u
*arg_end
= NULL
;
2279 char_u
*tofree
= NULL
;
2282 * ":let $VAR = expr": Set environment variable.
2286 /* Find the end of the name. */
2289 len
= get_env_len(&arg
);
2291 EMSG2(_(e_invarg2
), name
- 1);
2294 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2295 EMSG2(_(e_letwrong
), op
);
2296 else if (endchars
!= NULL
2297 && vim_strchr(endchars
, *skipwhite(arg
)) == NULL
)
2298 EMSG(_(e_letunexp
));
2303 p
= get_tv_string_chk(tv
);
2304 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2306 int mustfree
= FALSE
;
2307 char_u
*s
= vim_getenv(name
, &mustfree
);
2311 p
= tofree
= concat_str(s
, p
);
2318 vim_setenv(name
, p
);
2319 if (STRICMP(name
, "HOME") == 0)
2321 else if (didset_vim
&& STRICMP(name
, "VIM") == 0)
2323 else if (didset_vimruntime
2324 && STRICMP(name
, "VIMRUNTIME") == 0)
2325 didset_vimruntime
= FALSE
;
2335 * ":let &option = expr": Set option value.
2336 * ":let &l:option = expr": Set local option value.
2337 * ":let &g:option = expr": Set global option value.
2339 else if (*arg
== '&')
2341 /* Find the end of the name. */
2342 p
= find_option_end(&arg
, &opt_flags
);
2343 if (p
== NULL
|| (endchars
!= NULL
2344 && vim_strchr(endchars
, *skipwhite(p
)) == NULL
))
2345 EMSG(_(e_letunexp
));
2351 char_u
*stringval
= NULL
;
2357 n
= get_tv_number(tv
);
2358 s
= get_tv_string_chk(tv
); /* != NULL if number or string */
2359 if (s
!= NULL
&& op
!= NULL
&& *op
!= '=')
2361 opt_type
= get_option_value(arg
, &numval
,
2362 &stringval
, opt_flags
);
2363 if ((opt_type
== 1 && *op
== '.')
2364 || (opt_type
== 0 && *op
!= '.'))
2365 EMSG2(_(e_letwrong
), op
);
2368 if (opt_type
== 1) /* number */
2375 else if (opt_type
== 0 && stringval
!= NULL
) /* string */
2377 s
= concat_str(stringval
, s
);
2378 vim_free(stringval
);
2385 set_option_value(arg
, n
, s
, opt_flags
);
2389 vim_free(stringval
);
2394 * ":let @r = expr": Set register contents.
2396 else if (*arg
== '@')
2399 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2400 EMSG2(_(e_letwrong
), op
);
2401 else if (endchars
!= NULL
2402 && vim_strchr(endchars
, *skipwhite(arg
+ 1)) == NULL
)
2403 EMSG(_(e_letunexp
));
2406 char_u
*ptofree
= NULL
;
2409 p
= get_tv_string_chk(tv
);
2410 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2412 s
= get_reg_contents(*arg
== '@' ? '"' : *arg
, TRUE
, TRUE
);
2415 p
= ptofree
= concat_str(s
, p
);
2421 write_reg_contents(*arg
== '@' ? '"' : *arg
, p
, -1, FALSE
);
2429 * ":let var = expr": Set internal variable.
2430 * ":let {expr} = expr": Idem, name made with curly braces
2432 else if (eval_isnamec1(*arg
) || *arg
== '{')
2436 p
= get_lval(arg
, tv
, &lv
, FALSE
, FALSE
, FALSE
, FNE_CHECK_START
);
2437 if (p
!= NULL
&& lv
.ll_name
!= NULL
)
2439 if (endchars
!= NULL
&& vim_strchr(endchars
, *skipwhite(p
)) == NULL
)
2440 EMSG(_(e_letunexp
));
2443 set_var_lval(&lv
, p
, tv
, copy
, op
);
2451 EMSG2(_(e_invarg2
), arg
);
2457 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2460 check_changedtick(arg
)
2463 if (STRNCMP(arg
, "b:changedtick", 13) == 0 && !eval_isnamec(arg
[13]))
2465 EMSG2(_(e_readonlyvar
), arg
);
2472 * Get an lval: variable, Dict item or List item that can be assigned a value
2473 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2474 * "name.key", "name.key[expr]" etc.
2475 * Indexing only works if "name" is an existing List or Dictionary.
2476 * "name" points to the start of the name.
2477 * If "rettv" is not NULL it points to the value to be assigned.
2478 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2479 * wrong; must end in space or cmd separator.
2481 * Returns a pointer to just after the name, including indexes.
2482 * When an evaluation error occurs "lp->ll_name" is NULL;
2483 * Returns NULL for a parsing error. Still need to free items in "lp"!
2486 get_lval(name
, rettv
, lp
, unlet
, skip
, quiet
, fne_flags
)
2492 int quiet
; /* don't give error messages */
2493 int fne_flags
; /* flags for find_name_end() */
2496 char_u
*expr_start
, *expr_end
;
2507 /* Clear everything in "lp". */
2508 vim_memset(lp
, 0, sizeof(lval_T
));
2512 /* When skipping just find the end of the name. */
2514 return find_name_end(name
, NULL
, NULL
, FNE_INCL_BR
| fne_flags
);
2517 /* Find the end of the name. */
2518 p
= find_name_end(name
, &expr_start
, &expr_end
, fne_flags
);
2519 if (expr_start
!= NULL
)
2521 /* Don't expand the name when we already know there is an error. */
2522 if (unlet
&& !vim_iswhite(*p
) && !ends_excmd(*p
)
2523 && *p
!= '[' && *p
!= '.')
2525 EMSG(_(e_trailing
));
2529 lp
->ll_exp_name
= make_expanded_name(name
, expr_start
, expr_end
, p
);
2530 if (lp
->ll_exp_name
== NULL
)
2532 /* Report an invalid expression in braces, unless the
2533 * expression evaluation has been cancelled due to an
2534 * aborting error, an interrupt, or an exception. */
2535 if (!aborting() && !quiet
)
2538 EMSG2(_(e_invarg2
), name
);
2542 lp
->ll_name
= lp
->ll_exp_name
;
2547 /* Without [idx] or .key we are done. */
2548 if ((*p
!= '[' && *p
!= '.') || lp
->ll_name
== NULL
)
2553 v
= find_var(lp
->ll_name
, &ht
);
2554 if (v
== NULL
&& !quiet
)
2555 EMSG2(_(e_undefvar
), lp
->ll_name
);
2561 * Loop until no more [idx] or .key is following.
2563 lp
->ll_tv
= &v
->di_tv
;
2564 while (*p
== '[' || (*p
== '.' && lp
->ll_tv
->v_type
== VAR_DICT
))
2566 if (!(lp
->ll_tv
->v_type
== VAR_LIST
&& lp
->ll_tv
->vval
.v_list
!= NULL
)
2567 && !(lp
->ll_tv
->v_type
== VAR_DICT
2568 && lp
->ll_tv
->vval
.v_dict
!= NULL
))
2571 EMSG(_("E689: Can only index a List or Dictionary"));
2577 EMSG(_("E708: [:] must come last"));
2585 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
2590 EMSG(_(e_emptykey
));
2597 /* Get the index [expr] or the first index [expr: ]. */
2598 p
= skipwhite(p
+ 1);
2604 if (eval1(&p
, &var1
, TRUE
) == FAIL
) /* recursive! */
2606 if (get_tv_string_chk(&var1
) == NULL
)
2608 /* not a number or string */
2614 /* Optionally get the second index [ :expr]. */
2617 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2620 EMSG(_(e_dictrange
));
2625 if (rettv
!= NULL
&& (rettv
->v_type
!= VAR_LIST
2626 || rettv
->vval
.v_list
== NULL
))
2629 EMSG(_("E709: [:] requires a List value"));
2634 p
= skipwhite(p
+ 1);
2636 lp
->ll_empty2
= TRUE
;
2639 lp
->ll_empty2
= FALSE
;
2640 if (eval1(&p
, &var2
, TRUE
) == FAIL
) /* recursive! */
2646 if (get_tv_string_chk(&var2
) == NULL
)
2648 /* not a number or string */
2655 lp
->ll_range
= TRUE
;
2658 lp
->ll_range
= FALSE
;
2663 EMSG(_(e_missbrac
));
2666 if (lp
->ll_range
&& !lp
->ll_empty2
)
2671 /* Skip to past ']'. */
2675 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2679 /* "[key]": get key from "var1" */
2680 key
= get_tv_string(&var1
); /* is number or string */
2684 EMSG(_(e_emptykey
));
2690 lp
->ll_dict
= lp
->ll_tv
->vval
.v_dict
;
2691 lp
->ll_di
= dict_find(lp
->ll_dict
, key
, len
);
2692 if (lp
->ll_di
== NULL
)
2694 /* Key does not exist in dict: may need to add it. */
2695 if (*p
== '[' || *p
== '.' || unlet
)
2698 EMSG2(_(e_dictkey
), key
);
2704 lp
->ll_newkey
= vim_strsave(key
);
2706 lp
->ll_newkey
= vim_strnsave(key
, len
);
2709 if (lp
->ll_newkey
== NULL
)
2715 lp
->ll_tv
= &lp
->ll_di
->di_tv
;
2720 * Get the number and item for the only or first index of the List.
2726 lp
->ll_n1
= get_tv_number(&var1
); /* is number or string */
2730 lp
->ll_list
= lp
->ll_tv
->vval
.v_list
;
2731 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2732 if (lp
->ll_li
== NULL
)
2737 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2740 if (lp
->ll_li
== NULL
)
2742 if (lp
->ll_range
&& !lp
->ll_empty2
)
2748 * May need to find the item or absolute index for the second
2750 * When no index given: "lp->ll_empty2" is TRUE.
2751 * Otherwise "lp->ll_n2" is set to the second index.
2753 if (lp
->ll_range
&& !lp
->ll_empty2
)
2755 lp
->ll_n2
= get_tv_number(&var2
); /* is number or string */
2759 ni
= list_find(lp
->ll_list
, lp
->ll_n2
);
2762 lp
->ll_n2
= list_idx_of_item(lp
->ll_list
, ni
);
2765 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2767 lp
->ll_n1
= list_idx_of_item(lp
->ll_list
, lp
->ll_li
);
2768 if (lp
->ll_n2
< lp
->ll_n1
)
2772 lp
->ll_tv
= &lp
->ll_li
->li_tv
;
2780 * Clear lval "lp" that was filled by get_lval().
2786 vim_free(lp
->ll_exp_name
);
2787 vim_free(lp
->ll_newkey
);
2791 * Set a variable that was parsed by get_lval() to "rettv".
2792 * "endp" points to just after the parsed name.
2793 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
2796 set_var_lval(lp
, endp
, rettv
, copy
, op
)
2807 if (lp
->ll_tv
== NULL
)
2809 if (!check_changedtick(lp
->ll_name
))
2813 if (op
!= NULL
&& *op
!= '=')
2817 /* handle +=, -= and .= */
2818 if (get_var_tv(lp
->ll_name
, (int)STRLEN(lp
->ll_name
),
2821 if (tv_op(&tv
, rettv
, op
) == OK
)
2822 set_var(lp
->ll_name
, &tv
, FALSE
);
2827 set_var(lp
->ll_name
, rettv
, copy
);
2831 else if (tv_check_lock(lp
->ll_newkey
== NULL
2833 : lp
->ll_tv
->vval
.v_dict
->dv_lock
, lp
->ll_name
))
2835 else if (lp
->ll_range
)
2838 * Assign the List values to the list items.
2840 for (ri
= rettv
->vval
.v_list
->lv_first
; ri
!= NULL
; )
2842 if (op
!= NULL
&& *op
!= '=')
2843 tv_op(&lp
->ll_li
->li_tv
, &ri
->li_tv
, op
);
2846 clear_tv(&lp
->ll_li
->li_tv
);
2847 copy_tv(&ri
->li_tv
, &lp
->ll_li
->li_tv
);
2850 if (ri
== NULL
|| (!lp
->ll_empty2
&& lp
->ll_n2
== lp
->ll_n1
))
2852 if (lp
->ll_li
->li_next
== NULL
)
2854 /* Need to add an empty item. */
2855 if (list_append_number(lp
->ll_list
, 0) == FAIL
)
2861 lp
->ll_li
= lp
->ll_li
->li_next
;
2865 EMSG(_("E710: List value has more items than target"));
2866 else if (lp
->ll_empty2
2867 ? (lp
->ll_li
!= NULL
&& lp
->ll_li
->li_next
!= NULL
)
2868 : lp
->ll_n1
!= lp
->ll_n2
)
2869 EMSG(_("E711: List value has not enough items"));
2874 * Assign to a List or Dictionary item.
2876 if (lp
->ll_newkey
!= NULL
)
2878 if (op
!= NULL
&& *op
!= '=')
2880 EMSG2(_(e_letwrong
), op
);
2884 /* Need to add an item to the Dictionary. */
2885 di
= dictitem_alloc(lp
->ll_newkey
);
2888 if (dict_add(lp
->ll_tv
->vval
.v_dict
, di
) == FAIL
)
2893 lp
->ll_tv
= &di
->di_tv
;
2895 else if (op
!= NULL
&& *op
!= '=')
2897 tv_op(lp
->ll_tv
, rettv
, op
);
2901 clear_tv(lp
->ll_tv
);
2904 * Assign the value to the variable or list item.
2907 copy_tv(rettv
, lp
->ll_tv
);
2910 *lp
->ll_tv
= *rettv
;
2911 lp
->ll_tv
->v_lock
= 0;
2918 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2919 * Returns OK or FAIL.
2928 char_u numbuf
[NUMBUFLEN
];
2931 /* Can't do anything with a Funcref or a Dict on the right. */
2932 if (tv2
->v_type
!= VAR_FUNC
&& tv2
->v_type
!= VAR_DICT
)
2934 switch (tv1
->v_type
)
2941 if (*op
!= '+' || tv2
->v_type
!= VAR_LIST
)
2944 if (tv1
->vval
.v_list
!= NULL
&& tv2
->vval
.v_list
!= NULL
)
2945 list_extend(tv1
->vval
.v_list
, tv2
->vval
.v_list
, NULL
);
2950 if (tv2
->v_type
== VAR_LIST
)
2952 if (*op
== '+' || *op
== '-')
2954 /* nr += nr or nr -= nr*/
2955 n
= get_tv_number(tv1
);
2957 if (tv2
->v_type
== VAR_FLOAT
)
2962 f
+= tv2
->vval
.v_float
;
2964 f
-= tv2
->vval
.v_float
;
2966 tv1
->v_type
= VAR_FLOAT
;
2967 tv1
->vval
.v_float
= f
;
2973 n
+= get_tv_number(tv2
);
2975 n
-= get_tv_number(tv2
);
2977 tv1
->v_type
= VAR_NUMBER
;
2978 tv1
->vval
.v_number
= n
;
2983 if (tv2
->v_type
== VAR_FLOAT
)
2987 s
= get_tv_string(tv1
);
2988 s
= concat_str(s
, get_tv_string_buf(tv2
, numbuf
));
2990 tv1
->v_type
= VAR_STRING
;
2991 tv1
->vval
.v_string
= s
;
3000 if (*op
== '.' || (tv2
->v_type
!= VAR_FLOAT
3001 && tv2
->v_type
!= VAR_NUMBER
3002 && tv2
->v_type
!= VAR_STRING
))
3004 if (tv2
->v_type
== VAR_FLOAT
)
3005 f
= tv2
->vval
.v_float
;
3007 f
= get_tv_number(tv2
);
3009 tv1
->vval
.v_float
+= f
;
3011 tv1
->vval
.v_float
-= f
;
3018 EMSG2(_(e_letwrong
), op
);
3023 * Add a watcher to a list.
3026 list_add_watch(l
, lw
)
3030 lw
->lw_next
= l
->lv_watch
;
3035 * Remove a watcher from a list.
3036 * No warning when it isn't found...
3039 list_rem_watch(l
, lwrem
)
3043 listwatch_T
*lw
, **lwp
;
3046 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
3058 * Just before removing an item from a list: advance watchers to the next
3062 list_fix_watch(l
, item
)
3068 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
3069 if (lw
->lw_item
== item
)
3070 lw
->lw_item
= item
->li_next
;
3074 * Evaluate the expression used in a ":for var in expr" command.
3075 * "arg" points to "var".
3076 * Set "*errp" to TRUE for an error, FALSE otherwise;
3077 * Return a pointer that holds the info. Null when there is an error.
3080 eval_for_line(arg
, errp
, nextcmdp
, skip
)
3091 *errp
= TRUE
; /* default: there is an error */
3093 fi
= (forinfo_T
*)alloc_clear(sizeof(forinfo_T
));
3097 expr
= skip_var_list(arg
, &fi
->fi_varcount
, &fi
->fi_semicolon
);
3101 expr
= skipwhite(expr
);
3102 if (expr
[0] != 'i' || expr
[1] != 'n' || !vim_iswhite(expr
[2]))
3104 EMSG(_("E690: Missing \"in\" after :for"));
3110 if (eval0(skipwhite(expr
+ 2), &tv
, nextcmdp
, !skip
) == OK
)
3116 if (tv
.v_type
!= VAR_LIST
|| l
== NULL
)
3123 /* No need to increment the refcount, it's already set for the
3124 * list being used in "tv". */
3126 list_add_watch(l
, &fi
->fi_lw
);
3127 fi
->fi_lw
.lw_item
= l
->lv_first
;
3138 * Use the first item in a ":for" list. Advance to the next.
3139 * Assign the values to the variable (list). "arg" points to the first one.
3140 * Return TRUE when a valid item was found, FALSE when at end of list or
3144 next_for_item(fi_void
, arg
)
3148 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3152 item
= fi
->fi_lw
.lw_item
;
3157 fi
->fi_lw
.lw_item
= item
->li_next
;
3158 result
= (ex_let_vars(arg
, &item
->li_tv
, TRUE
,
3159 fi
->fi_semicolon
, fi
->fi_varcount
, NULL
) == OK
);
3165 * Free the structure used to store info used by ":for".
3168 free_for_info(fi_void
)
3171 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3173 if (fi
!= NULL
&& fi
->fi_list
!= NULL
)
3175 list_rem_watch(fi
->fi_list
, &fi
->fi_lw
);
3176 list_unref(fi
->fi_list
);
3181 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3184 set_context_for_expression(xp
, arg
, cmdidx
)
3193 if (cmdidx
== CMD_let
)
3195 xp
->xp_context
= EXPAND_USER_VARS
;
3196 if (vim_strpbrk(arg
, (char_u
*)"\"'+-*/%.=!?~|&$([<>,#") == NULL
)
3198 /* ":let var1 var2 ...": find last space. */
3199 for (p
= arg
+ STRLEN(arg
); p
>= arg
; )
3202 mb_ptr_back(arg
, p
);
3203 if (vim_iswhite(*p
))
3210 xp
->xp_context
= cmdidx
== CMD_call
? EXPAND_FUNCTIONS
3211 : EXPAND_EXPRESSION
;
3212 while ((xp
->xp_pattern
= vim_strpbrk(arg
,
3213 (char_u
*)"\"'+-*/%.=!?~|&$([<>,#")) != NULL
)
3215 c
= *xp
->xp_pattern
;
3218 c
= xp
->xp_pattern
[1];
3222 xp
->xp_context
= cmdidx
!= CMD_let
|| got_eq
3223 ? EXPAND_EXPRESSION
: EXPAND_NOTHING
;
3227 xp
->xp_context
= EXPAND_SETTINGS
;
3228 if ((c
== 'l' || c
== 'g') && xp
->xp_pattern
[2] == ':')
3229 xp
->xp_pattern
+= 2;
3235 /* environment variable */
3236 xp
->xp_context
= EXPAND_ENV_VARS
;
3241 xp
->xp_context
= EXPAND_EXPRESSION
;
3244 && xp
->xp_context
== EXPAND_FUNCTIONS
3245 && vim_strchr(xp
->xp_pattern
, '(') == NULL
)
3247 /* Function name can start with "<SNR>" */
3250 else if (cmdidx
!= CMD_let
|| got_eq
)
3252 if (c
== '"') /* string */
3254 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '"')
3255 if (c
== '\\' && xp
->xp_pattern
[1] != NUL
)
3257 xp
->xp_context
= EXPAND_NOTHING
;
3259 else if (c
== '\'') /* literal string */
3261 /* Trick: '' is like stopping and starting a literal string. */
3262 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '\'')
3264 xp
->xp_context
= EXPAND_NOTHING
;
3268 if (xp
->xp_pattern
[1] == '|')
3271 xp
->xp_context
= EXPAND_EXPRESSION
;
3274 xp
->xp_context
= EXPAND_COMMANDS
;
3277 xp
->xp_context
= EXPAND_EXPRESSION
;
3280 /* Doesn't look like something valid, expand as an expression
3282 xp
->xp_context
= EXPAND_EXPRESSION
;
3283 arg
= xp
->xp_pattern
;
3285 while ((c
= *++arg
) != NUL
&& (c
== ' ' || c
== '\t'))
3288 xp
->xp_pattern
= arg
;
3291 #endif /* FEAT_CMDL_COMPL */
3294 * ":1,25call func(arg1, arg2)" function call.
3300 char_u
*arg
= eap
->arg
;
3311 tofree
= trans_function_name(&arg
, eap
->skip
, TFN_INT
, &fudi
);
3312 if (fudi
.fd_newkey
!= NULL
)
3314 /* Still need to give an error message for missing key. */
3315 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
3316 vim_free(fudi
.fd_newkey
);
3321 /* Increase refcount on dictionary, it could get deleted when evaluating
3323 if (fudi
.fd_dict
!= NULL
)
3324 ++fudi
.fd_dict
->dv_refcount
;
3326 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3327 len
= (int)STRLEN(tofree
);
3328 name
= deref_func_name(tofree
, &len
);
3330 /* Skip white space to allow ":call func ()". Not good, but required for
3331 * backward compatibility. */
3332 startarg
= skipwhite(arg
);
3333 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
3335 if (*startarg
!= '(')
3337 EMSG2(_("E107: Missing parentheses: %s"), eap
->arg
);
3342 * When skipping, evaluate the function once, to find the end of the
3344 * When the function takes a range, this is discovered after the first
3345 * call, and the loop is broken.
3350 lnum
= eap
->line2
; /* do it once, also with an invalid range */
3354 for ( ; lnum
<= eap
->line2
; ++lnum
)
3356 if (!eap
->skip
&& eap
->addr_count
> 0)
3358 curwin
->w_cursor
.lnum
= lnum
;
3359 curwin
->w_cursor
.col
= 0;
3362 if (get_func_tv(name
, (int)STRLEN(name
), &rettv
, &arg
,
3363 eap
->line1
, eap
->line2
, &doesrange
,
3364 !eap
->skip
, fudi
.fd_dict
) == FAIL
)
3370 /* Handle a function returning a Funcref, Dictionary or List. */
3371 if (handle_subscript(&arg
, &rettv
, !eap
->skip
, TRUE
) == FAIL
)
3378 if (doesrange
|| eap
->skip
)
3381 /* Stop when immediately aborting on error, or when an interrupt
3382 * occurred or an exception was thrown but not caught.
3383 * get_func_tv() returned OK, so that the check for trailing
3384 * characters below is executed. */
3393 /* Check for trailing illegal characters and a following command. */
3394 if (!ends_excmd(*arg
))
3397 EMSG(_(e_trailing
));
3400 eap
->nextcmd
= check_nextcmd(arg
);
3404 dict_unref(fudi
.fd_dict
);
3409 * ":unlet[!] var1 ... " command.
3415 ex_unletlock(eap
, eap
->arg
, 0);
3419 * ":lockvar" and ":unlockvar" commands
3425 char_u
*arg
= eap
->arg
;
3430 else if (vim_isdigit(*arg
))
3432 deep
= getdigits(&arg
);
3433 arg
= skipwhite(arg
);
3436 ex_unletlock(eap
, arg
, deep
);
3440 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3443 ex_unletlock(eap
, argstart
, deep
)
3448 char_u
*arg
= argstart
;
3455 /* Parse the name and find the end. */
3456 name_end
= get_lval(arg
, NULL
, &lv
, TRUE
, eap
->skip
|| error
, FALSE
,
3458 if (lv
.ll_name
== NULL
)
3459 error
= TRUE
; /* error but continue parsing */
3460 if (name_end
== NULL
|| (!vim_iswhite(*name_end
)
3461 && !ends_excmd(*name_end
)))
3463 if (name_end
!= NULL
)
3466 EMSG(_(e_trailing
));
3468 if (!(eap
->skip
|| error
))
3473 if (!error
&& !eap
->skip
)
3475 if (eap
->cmdidx
== CMD_unlet
)
3477 if (do_unlet_var(&lv
, name_end
, eap
->forceit
) == FAIL
)
3482 if (do_lock_var(&lv
, name_end
, deep
,
3483 eap
->cmdidx
== CMD_lockvar
) == FAIL
)
3491 arg
= skipwhite(name_end
);
3492 } while (!ends_excmd(*arg
));
3494 eap
->nextcmd
= check_nextcmd(arg
);
3498 do_unlet_var(lp
, name_end
, forceit
)
3506 if (lp
->ll_tv
== NULL
)
3511 /* Normal name or expanded name. */
3512 if (check_changedtick(lp
->ll_name
))
3514 else if (do_unlet(lp
->ll_name
, forceit
) == FAIL
)
3518 else if (tv_check_lock(lp
->ll_tv
->v_lock
, lp
->ll_name
))
3520 else if (lp
->ll_range
)
3524 /* Delete a range of List items. */
3525 while (lp
->ll_li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3527 li
= lp
->ll_li
->li_next
;
3528 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3535 if (lp
->ll_list
!= NULL
)
3536 /* unlet a List item. */
3537 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3539 /* unlet a Dictionary item. */
3540 dictitem_remove(lp
->ll_dict
, lp
->ll_di
);
3547 * "unlet" a variable. Return OK if it existed, FAIL if not.
3548 * When "forceit" is TRUE don't complain if the variable doesn't exist.
3551 do_unlet(name
, forceit
)
3560 ht
= find_var_ht(name
, &varname
);
3561 if (ht
!= NULL
&& *varname
!= NUL
)
3563 hi
= hash_find(ht
, varname
);
3564 if (!HASHITEM_EMPTY(hi
))
3567 if (var_check_fixed(di
->di_flags
, name
)
3568 || var_check_ro(di
->di_flags
, name
))
3576 EMSG2(_("E108: No such variable: \"%s\""), name
);
3581 * Lock or unlock variable indicated by "lp".
3582 * "deep" is the levels to go (-1 for unlimited);
3583 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3586 do_lock_var(lp
, name_end
, deep
, lock
)
3596 if (deep
== 0) /* nothing to do */
3599 if (lp
->ll_tv
== NULL
)
3604 /* Normal name or expanded name. */
3605 if (check_changedtick(lp
->ll_name
))
3609 di
= find_var(lp
->ll_name
, NULL
);
3615 di
->di_flags
|= DI_FLAGS_LOCK
;
3617 di
->di_flags
&= ~DI_FLAGS_LOCK
;
3618 item_lock(&di
->di_tv
, deep
, lock
);
3623 else if (lp
->ll_range
)
3625 listitem_T
*li
= lp
->ll_li
;
3627 /* (un)lock a range of List items. */
3628 while (li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3630 item_lock(&li
->li_tv
, deep
, lock
);
3635 else if (lp
->ll_list
!= NULL
)
3636 /* (un)lock a List item. */
3637 item_lock(&lp
->ll_li
->li_tv
, deep
, lock
);
3639 /* un(lock) a Dictionary item. */
3640 item_lock(&lp
->ll_di
->di_tv
, deep
, lock
);
3646 * Lock or unlock an item. "deep" is nr of levels to go.
3649 item_lock(tv
, deep
, lock
)
3654 static int recurse
= 0;
3661 if (recurse
>= DICT_MAXNEST
)
3663 EMSG(_("E743: variable nested too deep for (un)lock"));
3670 /* lock/unlock the item itself */
3672 tv
->v_lock
|= VAR_LOCKED
;
3674 tv
->v_lock
&= ~VAR_LOCKED
;
3679 if ((l
= tv
->vval
.v_list
) != NULL
)
3682 l
->lv_lock
|= VAR_LOCKED
;
3684 l
->lv_lock
&= ~VAR_LOCKED
;
3685 if (deep
< 0 || deep
> 1)
3686 /* recursive: lock/unlock the items the List contains */
3687 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
3688 item_lock(&li
->li_tv
, deep
- 1, lock
);
3692 if ((d
= tv
->vval
.v_dict
) != NULL
)
3695 d
->dv_lock
|= VAR_LOCKED
;
3697 d
->dv_lock
&= ~VAR_LOCKED
;
3698 if (deep
< 0 || deep
> 1)
3700 /* recursive: lock/unlock the items the List contains */
3701 todo
= (int)d
->dv_hashtab
.ht_used
;
3702 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
3704 if (!HASHITEM_EMPTY(hi
))
3707 item_lock(&HI2DI(hi
)->di_tv
, deep
- 1, lock
);
3717 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3718 * or it refers to a List or Dictionary that is locked.
3724 return (tv
->v_lock
& VAR_LOCKED
)
3725 || (tv
->v_type
== VAR_LIST
3726 && tv
->vval
.v_list
!= NULL
3727 && (tv
->vval
.v_list
->lv_lock
& VAR_LOCKED
))
3728 || (tv
->v_type
== VAR_DICT
3729 && tv
->vval
.v_dict
!= NULL
3730 && (tv
->vval
.v_dict
->dv_lock
& VAR_LOCKED
));
3733 #if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3735 * Delete all "menutrans_" variables.
3738 del_menutrans_vars()
3743 hash_lock(&globvarht
);
3744 todo
= (int)globvarht
.ht_used
;
3745 for (hi
= globvarht
.ht_array
; todo
> 0 && !got_int
; ++hi
)
3747 if (!HASHITEM_EMPTY(hi
))
3750 if (STRNCMP(HI2DI(hi
)->di_key
, "menutrans_", 10) == 0)
3751 delete_var(&globvarht
, hi
);
3754 hash_unlock(&globvarht
);
3758 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3761 * Local string buffer for the next two functions to store a variable name
3762 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3763 * get_user_var_name().
3766 static char_u
*cat_prefix_varname
__ARGS((int prefix
, char_u
*name
));
3768 static char_u
*varnamebuf
= NULL
;
3769 static int varnamebuflen
= 0;
3772 * Function to concatenate a prefix and a variable name.
3775 cat_prefix_varname(prefix
, name
)
3781 len
= (int)STRLEN(name
) + 3;
3782 if (len
> varnamebuflen
)
3784 vim_free(varnamebuf
);
3785 len
+= 10; /* some additional space */
3786 varnamebuf
= alloc(len
);
3787 if (varnamebuf
== NULL
)
3792 varnamebuflen
= len
;
3794 *varnamebuf
= prefix
;
3795 varnamebuf
[1] = ':';
3796 STRCPY(varnamebuf
+ 2, name
);
3801 * Function given to ExpandGeneric() to obtain the list of user defined
3802 * (global/buffer/window/built-in) variable names.
3805 get_user_var_name(xp
, idx
)
3809 static long_u gdone
;
3810 static long_u bdone
;
3811 static long_u wdone
;
3813 static long_u tdone
;
3816 static hashitem_T
*hi
;
3821 gdone
= bdone
= wdone
= vidx
= 0;
3827 /* Global variables */
3828 if (gdone
< globvarht
.ht_used
)
3831 hi
= globvarht
.ht_array
;
3834 while (HASHITEM_EMPTY(hi
))
3836 if (STRNCMP("g:", xp
->xp_pattern
, 2) == 0)
3837 return cat_prefix_varname('g', hi
->hi_key
);
3842 ht
= &curbuf
->b_vars
.dv_hashtab
;
3843 if (bdone
< ht
->ht_used
)
3849 while (HASHITEM_EMPTY(hi
))
3851 return cat_prefix_varname('b', hi
->hi_key
);
3853 if (bdone
== ht
->ht_used
)
3856 return (char_u
*)"b:changedtick";
3860 ht
= &curwin
->w_vars
.dv_hashtab
;
3861 if (wdone
< ht
->ht_used
)
3867 while (HASHITEM_EMPTY(hi
))
3869 return cat_prefix_varname('w', hi
->hi_key
);
3874 ht
= &curtab
->tp_vars
.dv_hashtab
;
3875 if (tdone
< ht
->ht_used
)
3881 while (HASHITEM_EMPTY(hi
))
3883 return cat_prefix_varname('t', hi
->hi_key
);
3889 return cat_prefix_varname('v', (char_u
*)vimvars
[vidx
++].vv_name
);
3891 vim_free(varnamebuf
);
3897 #endif /* FEAT_CMDL_COMPL */
3900 * types for expressions.
3905 , TYPE_EQUAL
/* == */
3906 , TYPE_NEQUAL
/* != */
3907 , TYPE_GREATER
/* > */
3908 , TYPE_GEQUAL
/* >= */
3909 , TYPE_SMALLER
/* < */
3910 , TYPE_SEQUAL
/* <= */
3911 , TYPE_MATCH
/* =~ */
3912 , TYPE_NOMATCH
/* !~ */
3916 * The "evaluate" argument: When FALSE, the argument is only parsed but not
3917 * executed. The function may return OK, but the rettv will be of type
3918 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3922 * Handle zero level expression.
3923 * This calls eval1() and handles error message and nextcmd.
3924 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
3925 * Note: "rettv.v_lock" is not set.
3926 * Return OK or FAIL.
3929 eval0(arg
, rettv
, nextcmd
, evaluate
)
3939 ret
= eval1(&p
, rettv
, evaluate
);
3940 if (ret
== FAIL
|| !ends_excmd(*p
))
3945 * Report the invalid expression unless the expression evaluation has
3946 * been cancelled due to an aborting error, an interrupt, or an
3950 EMSG2(_(e_invexpr2
), arg
);
3953 if (nextcmd
!= NULL
)
3954 *nextcmd
= check_nextcmd(p
);
3960 * Handle top level expression:
3961 * expr2 ? expr1 : expr1
3963 * "arg" must point to the first non-white of the expression.
3964 * "arg" is advanced to the next non-white after the recognized expression.
3966 * Note: "rettv.v_lock" is not set.
3968 * Return OK or FAIL.
3971 eval1(arg
, rettv
, evaluate
)
3980 * Get the first variable.
3982 if (eval2(arg
, rettv
, evaluate
) == FAIL
)
3985 if ((*arg
)[0] == '?')
3992 if (get_tv_number_chk(rettv
, &error
) != 0)
4000 * Get the second variable.
4002 *arg
= skipwhite(*arg
+ 1);
4003 if (eval1(arg
, rettv
, evaluate
&& result
) == FAIL
) /* recursive! */
4007 * Check for the ":".
4009 if ((*arg
)[0] != ':')
4011 EMSG(_("E109: Missing ':' after '?'"));
4012 if (evaluate
&& result
)
4018 * Get the third variable.
4020 *arg
= skipwhite(*arg
+ 1);
4021 if (eval1(arg
, &var2
, evaluate
&& !result
) == FAIL
) /* recursive! */
4023 if (evaluate
&& result
)
4027 if (evaluate
&& !result
)
4035 * Handle first level expression:
4036 * expr2 || expr2 || expr2 logical OR
4038 * "arg" must point to the first non-white of the expression.
4039 * "arg" is advanced to the next non-white after the recognized expression.
4041 * Return OK or FAIL.
4044 eval2(arg
, rettv
, evaluate
)
4055 * Get the first variable.
4057 if (eval3(arg
, rettv
, evaluate
) == FAIL
)
4061 * Repeat until there is no following "||".
4065 while ((*arg
)[0] == '|' && (*arg
)[1] == '|')
4067 if (evaluate
&& first
)
4069 if (get_tv_number_chk(rettv
, &error
) != 0)
4078 * Get the second variable.
4080 *arg
= skipwhite(*arg
+ 2);
4081 if (eval3(arg
, &var2
, evaluate
&& !result
) == FAIL
)
4085 * Compute the result.
4087 if (evaluate
&& !result
)
4089 if (get_tv_number_chk(&var2
, &error
) != 0)
4097 rettv
->v_type
= VAR_NUMBER
;
4098 rettv
->vval
.v_number
= result
;
4106 * Handle second level expression:
4107 * expr3 && expr3 && expr3 logical AND
4109 * "arg" must point to the first non-white of the expression.
4110 * "arg" is advanced to the next non-white after the recognized expression.
4112 * Return OK or FAIL.
4115 eval3(arg
, rettv
, evaluate
)
4126 * Get the first variable.
4128 if (eval4(arg
, rettv
, evaluate
) == FAIL
)
4132 * Repeat until there is no following "&&".
4136 while ((*arg
)[0] == '&' && (*arg
)[1] == '&')
4138 if (evaluate
&& first
)
4140 if (get_tv_number_chk(rettv
, &error
) == 0)
4149 * Get the second variable.
4151 *arg
= skipwhite(*arg
+ 2);
4152 if (eval4(arg
, &var2
, evaluate
&& result
) == FAIL
)
4156 * Compute the result.
4158 if (evaluate
&& result
)
4160 if (get_tv_number_chk(&var2
, &error
) == 0)
4168 rettv
->v_type
= VAR_NUMBER
;
4169 rettv
->vval
.v_number
= result
;
4177 * Handle third level expression:
4189 * "arg" must point to the first non-white of the expression.
4190 * "arg" is advanced to the next non-white after the recognized expression.
4192 * Return OK or FAIL.
4195 eval4(arg
, rettv
, evaluate
)
4203 exptype_T type
= TYPE_UNKNOWN
;
4204 int type_is
= FALSE
; /* TRUE for "is" and "isnot" */
4208 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4209 regmatch_T regmatch
;
4214 * Get the first variable.
4216 if (eval5(arg
, rettv
, evaluate
) == FAIL
)
4222 case '=': if (p
[1] == '=')
4224 else if (p
[1] == '~')
4227 case '!': if (p
[1] == '=')
4229 else if (p
[1] == '~')
4230 type
= TYPE_NOMATCH
;
4232 case '>': if (p
[1] != '=')
4234 type
= TYPE_GREATER
;
4240 case '<': if (p
[1] != '=')
4242 type
= TYPE_SMALLER
;
4248 case 'i': if (p
[1] == 's')
4250 if (p
[2] == 'n' && p
[3] == 'o' && p
[4] == 't')
4252 if (!vim_isIDc(p
[len
]))
4254 type
= len
== 2 ? TYPE_EQUAL
: TYPE_NEQUAL
;
4262 * If there is a comparative operator, use it.
4264 if (type
!= TYPE_UNKNOWN
)
4266 /* extra question mark appended: ignore case */
4272 /* extra '#' appended: match case */
4273 else if (p
[len
] == '#')
4278 /* nothing appended: use 'ignorecase' */
4283 * Get the second variable.
4285 *arg
= skipwhite(p
+ len
);
4286 if (eval5(arg
, &var2
, evaluate
) == FAIL
)
4294 if (type_is
&& rettv
->v_type
!= var2
.v_type
)
4296 /* For "is" a different type always means FALSE, for "notis"
4298 n1
= (type
== TYPE_NEQUAL
);
4300 else if (rettv
->v_type
== VAR_LIST
|| var2
.v_type
== VAR_LIST
)
4304 n1
= (rettv
->v_type
== var2
.v_type
4305 && rettv
->vval
.v_list
== var2
.vval
.v_list
);
4306 if (type
== TYPE_NEQUAL
)
4309 else if (rettv
->v_type
!= var2
.v_type
4310 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4312 if (rettv
->v_type
!= var2
.v_type
)
4313 EMSG(_("E691: Can only compare List with List"));
4315 EMSG(_("E692: Invalid operation for Lists"));
4322 /* Compare two Lists for being equal or unequal. */
4323 n1
= list_equal(rettv
->vval
.v_list
, var2
.vval
.v_list
, ic
);
4324 if (type
== TYPE_NEQUAL
)
4329 else if (rettv
->v_type
== VAR_DICT
|| var2
.v_type
== VAR_DICT
)
4333 n1
= (rettv
->v_type
== var2
.v_type
4334 && rettv
->vval
.v_dict
== var2
.vval
.v_dict
);
4335 if (type
== TYPE_NEQUAL
)
4338 else if (rettv
->v_type
!= var2
.v_type
4339 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4341 if (rettv
->v_type
!= var2
.v_type
)
4342 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4344 EMSG(_("E736: Invalid operation for Dictionary"));
4351 /* Compare two Dictionaries for being equal or unequal. */
4352 n1
= dict_equal(rettv
->vval
.v_dict
, var2
.vval
.v_dict
, ic
);
4353 if (type
== TYPE_NEQUAL
)
4358 else if (rettv
->v_type
== VAR_FUNC
|| var2
.v_type
== VAR_FUNC
)
4360 if (rettv
->v_type
!= var2
.v_type
4361 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4363 if (rettv
->v_type
!= var2
.v_type
)
4364 EMSG(_("E693: Can only compare Funcref with Funcref"));
4366 EMSG(_("E694: Invalid operation for Funcrefs"));
4373 /* Compare two Funcrefs for being equal or unequal. */
4374 if (rettv
->vval
.v_string
== NULL
4375 || var2
.vval
.v_string
== NULL
)
4378 n1
= STRCMP(rettv
->vval
.v_string
,
4379 var2
.vval
.v_string
) == 0;
4380 if (type
== TYPE_NEQUAL
)
4387 * If one of the two variables is a float, compare as a float.
4388 * When using "=~" or "!~", always compare as string.
4390 else if ((rettv
->v_type
== VAR_FLOAT
|| var2
.v_type
== VAR_FLOAT
)
4391 && type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4395 if (rettv
->v_type
== VAR_FLOAT
)
4396 f1
= rettv
->vval
.v_float
;
4398 f1
= get_tv_number(rettv
);
4399 if (var2
.v_type
== VAR_FLOAT
)
4400 f2
= var2
.vval
.v_float
;
4402 f2
= get_tv_number(&var2
);
4406 case TYPE_EQUAL
: n1
= (f1
== f2
); break;
4407 case TYPE_NEQUAL
: n1
= (f1
!= f2
); break;
4408 case TYPE_GREATER
: n1
= (f1
> f2
); break;
4409 case TYPE_GEQUAL
: n1
= (f1
>= f2
); break;
4410 case TYPE_SMALLER
: n1
= (f1
< f2
); break;
4411 case TYPE_SEQUAL
: n1
= (f1
<= f2
); break;
4414 case TYPE_NOMATCH
: break; /* avoid gcc warning */
4420 * If one of the two variables is a number, compare as a number.
4421 * When using "=~" or "!~", always compare as string.
4423 else if ((rettv
->v_type
== VAR_NUMBER
|| var2
.v_type
== VAR_NUMBER
)
4424 && type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4426 n1
= get_tv_number(rettv
);
4427 n2
= get_tv_number(&var2
);
4430 case TYPE_EQUAL
: n1
= (n1
== n2
); break;
4431 case TYPE_NEQUAL
: n1
= (n1
!= n2
); break;
4432 case TYPE_GREATER
: n1
= (n1
> n2
); break;
4433 case TYPE_GEQUAL
: n1
= (n1
>= n2
); break;
4434 case TYPE_SMALLER
: n1
= (n1
< n2
); break;
4435 case TYPE_SEQUAL
: n1
= (n1
<= n2
); break;
4438 case TYPE_NOMATCH
: break; /* avoid gcc warning */
4443 s1
= get_tv_string_buf(rettv
, buf1
);
4444 s2
= get_tv_string_buf(&var2
, buf2
);
4445 if (type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4446 i
= ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
);
4452 case TYPE_EQUAL
: n1
= (i
== 0); break;
4453 case TYPE_NEQUAL
: n1
= (i
!= 0); break;
4454 case TYPE_GREATER
: n1
= (i
> 0); break;
4455 case TYPE_GEQUAL
: n1
= (i
>= 0); break;
4456 case TYPE_SMALLER
: n1
= (i
< 0); break;
4457 case TYPE_SEQUAL
: n1
= (i
<= 0); break;
4461 /* avoid 'l' flag in 'cpoptions' */
4463 p_cpo
= (char_u
*)"";
4464 regmatch
.regprog
= vim_regcomp(s2
,
4465 RE_MAGIC
+ RE_STRING
);
4466 regmatch
.rm_ic
= ic
;
4467 if (regmatch
.regprog
!= NULL
)
4469 n1
= vim_regexec_nl(®match
, s1
, (colnr_T
)0);
4470 vim_free(regmatch
.regprog
);
4471 if (type
== TYPE_NOMATCH
)
4477 case TYPE_UNKNOWN
: break; /* avoid gcc warning */
4482 rettv
->v_type
= VAR_NUMBER
;
4483 rettv
->vval
.v_number
= n1
;
4491 * Handle fourth level expression:
4493 * - number subtraction
4494 * . string concatenation
4496 * "arg" must point to the first non-white of the expression.
4497 * "arg" is advanced to the next non-white after the recognized expression.
4499 * Return OK or FAIL.
4502 eval5(arg
, rettv
, evaluate
)
4512 float_T f1
= 0, f2
= 0;
4515 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4519 * Get the first variable.
4521 if (eval6(arg
, rettv
, evaluate
, FALSE
) == FAIL
)
4525 * Repeat computing, until no '+', '-' or '.' is following.
4530 if (op
!= '+' && op
!= '-' && op
!= '.')
4533 if ((op
!= '+' || rettv
->v_type
!= VAR_LIST
)
4535 && (op
== '.' || rettv
->v_type
!= VAR_FLOAT
)
4539 /* For "list + ...", an illegal use of the first operand as
4540 * a number cannot be determined before evaluating the 2nd
4541 * operand: if this is also a list, all is ok.
4542 * For "something . ...", "something - ..." or "non-list + ...",
4543 * we know that the first operand needs to be a string or number
4544 * without evaluating the 2nd operand. So check before to avoid
4545 * side effects after an error. */
4546 if (evaluate
&& get_tv_string_chk(rettv
) == NULL
)
4554 * Get the second variable.
4556 *arg
= skipwhite(*arg
+ 1);
4557 if (eval6(arg
, &var2
, evaluate
, op
== '.') == FAIL
)
4566 * Compute the result.
4570 s1
= get_tv_string_buf(rettv
, buf1
); /* already checked */
4571 s2
= get_tv_string_buf_chk(&var2
, buf2
);
4572 if (s2
== NULL
) /* type error ? */
4578 p
= concat_str(s1
, s2
);
4580 rettv
->v_type
= VAR_STRING
;
4581 rettv
->vval
.v_string
= p
;
4583 else if (op
== '+' && rettv
->v_type
== VAR_LIST
4584 && var2
.v_type
== VAR_LIST
)
4586 /* concatenate Lists */
4587 if (list_concat(rettv
->vval
.v_list
, var2
.vval
.v_list
,
4602 if (rettv
->v_type
== VAR_FLOAT
)
4604 f1
= rettv
->vval
.v_float
;
4610 n1
= get_tv_number_chk(rettv
, &error
);
4613 /* This can only happen for "list + non-list". For
4614 * "non-list + ..." or "something - ...", we returned
4615 * before evaluating the 2nd operand. */
4620 if (var2
.v_type
== VAR_FLOAT
)
4625 if (var2
.v_type
== VAR_FLOAT
)
4627 f2
= var2
.vval
.v_float
;
4633 n2
= get_tv_number_chk(&var2
, &error
);
4641 if (rettv
->v_type
== VAR_FLOAT
)
4648 /* If there is a float on either side the result is a float. */
4649 if (rettv
->v_type
== VAR_FLOAT
|| var2
.v_type
== VAR_FLOAT
)
4655 rettv
->v_type
= VAR_FLOAT
;
4656 rettv
->vval
.v_float
= f1
;
4665 rettv
->v_type
= VAR_NUMBER
;
4666 rettv
->vval
.v_number
= n1
;
4676 * Handle fifth level expression:
4677 * * number multiplication
4681 * "arg" must point to the first non-white of the expression.
4682 * "arg" is advanced to the next non-white after the recognized expression.
4684 * Return OK or FAIL.
4687 eval6(arg
, rettv
, evaluate
, want_string
)
4691 int want_string
; /* after "." operator */
4697 int use_float
= FALSE
;
4703 * Get the first variable.
4705 if (eval7(arg
, rettv
, evaluate
, want_string
) == FAIL
)
4709 * Repeat computing, until no '*', '/' or '%' is following.
4714 if (op
!= '*' && op
!= '/' && op
!= '%')
4720 if (rettv
->v_type
== VAR_FLOAT
)
4722 f1
= rettv
->vval
.v_float
;
4728 n1
= get_tv_number_chk(rettv
, &error
);
4737 * Get the second variable.
4739 *arg
= skipwhite(*arg
+ 1);
4740 if (eval7(arg
, &var2
, evaluate
, FALSE
) == FAIL
)
4746 if (var2
.v_type
== VAR_FLOAT
)
4753 f2
= var2
.vval
.v_float
;
4759 n2
= get_tv_number_chk(&var2
, &error
);
4770 * Compute the result.
4771 * When either side is a float the result is a float.
4780 /* We rely on the floating point library to handle divide
4781 * by zero to result in "inf" and not a crash. */
4786 EMSG(_("E804: Cannot use '%' with Float"));
4789 rettv
->v_type
= VAR_FLOAT
;
4790 rettv
->vval
.v_float
= f1
;
4799 if (n2
== 0) /* give an error message? */
4802 n1
= -0x7fffffffL
- 1L; /* similar to NaN */
4813 if (n2
== 0) /* give an error message? */
4818 rettv
->v_type
= VAR_NUMBER
;
4819 rettv
->vval
.v_number
= n1
;
4828 * Handle sixth level expression:
4829 * number number constant
4830 * "string" string constant
4831 * 'string' literal string constant
4832 * &option-name option value
4833 * @r register contents
4834 * identifier variable value
4835 * function() function call
4836 * $VAR environment variable
4837 * (expression) nested expression
4839 * {key: val, key: val} Dictionary
4842 * ! in front logical NOT
4843 * - in front unary minus
4844 * + in front unary plus (ignored)
4845 * trailing [] subscript in String or List
4846 * trailing .name entry in Dictionary
4848 * "arg" must point to the first non-white of the expression.
4849 * "arg" is advanced to the next non-white after the recognized expression.
4851 * Return OK or FAIL.
4854 eval7(arg
, rettv
, evaluate
, want_string
)
4858 int want_string
; /* after "." operator */
4863 char_u
*start_leader
, *end_leader
;
4868 * Initialise variable so that clear_tv() can't mistake this for a
4869 * string and free a string that isn't there.
4871 rettv
->v_type
= VAR_UNKNOWN
;
4874 * Skip '!' and '-' characters. They are handled later.
4876 start_leader
= *arg
;
4877 while (**arg
== '!' || **arg
== '-' || **arg
== '+')
4878 *arg
= skipwhite(*arg
+ 1);
4898 char_u
*p
= skipdigits(*arg
+ 1);
4899 int get_float
= FALSE
;
4901 /* We accept a float when the format matches
4902 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
4903 * strict to avoid backwards compatibility problems.
4904 * Don't look for a float after the "." operator, so that
4905 * ":let vers = 1.2.3" doesn't fail. */
4906 if (!want_string
&& p
[0] == '.' && vim_isdigit(p
[1]))
4909 p
= skipdigits(p
+ 2);
4910 if (*p
== 'e' || *p
== 'E')
4913 if (*p
== '-' || *p
== '+')
4915 if (!vim_isdigit(*p
))
4918 p
= skipdigits(p
+ 1);
4920 if (ASCII_ISALPHA(*p
) || *p
== '.')
4927 *arg
+= string2float(*arg
, &f
);
4930 rettv
->v_type
= VAR_FLOAT
;
4931 rettv
->vval
.v_float
= f
;
4937 vim_str2nr(*arg
, NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
4941 rettv
->v_type
= VAR_NUMBER
;
4942 rettv
->vval
.v_number
= n
;
4949 * String constant: "string".
4951 case '"': ret
= get_string_tv(arg
, rettv
, evaluate
);
4955 * Literal string constant: 'str''ing'.
4957 case '\'': ret
= get_lit_string_tv(arg
, rettv
, evaluate
);
4961 * List: [expr, expr]
4963 case '[': ret
= get_list_tv(arg
, rettv
, evaluate
);
4967 * Dictionary: {key: val, key: val}
4969 case '{': ret
= get_dict_tv(arg
, rettv
, evaluate
);
4973 * Option value: &name
4975 case '&': ret
= get_option_tv(arg
, rettv
, evaluate
);
4979 * Environment variable: $VAR.
4981 case '$': ret
= get_env_tv(arg
, rettv
, evaluate
);
4985 * Register contents: @r.
4990 rettv
->v_type
= VAR_STRING
;
4991 rettv
->vval
.v_string
= get_reg_contents(**arg
, TRUE
, TRUE
);
4998 * nested expression: (expression).
5000 case '(': *arg
= skipwhite(*arg
+ 1);
5001 ret
= eval1(arg
, rettv
, evaluate
); /* recursive! */
5006 EMSG(_("E110: Missing ')'"));
5012 default: ret
= NOTDONE
;
5019 * Must be a variable or function name.
5020 * Can also be a curly-braces kind of name: {expr}.
5023 len
= get_name_len(arg
, &alias
, evaluate
, TRUE
);
5031 if (**arg
== '(') /* recursive! */
5033 /* If "s" is the name of a variable of type VAR_FUNC
5034 * use its contents. */
5035 s
= deref_func_name(s
, &len
);
5037 /* Invoke the function. */
5038 ret
= get_func_tv(s
, len
, rettv
, arg
,
5039 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
5040 &len
, evaluate
, NULL
);
5041 /* Stop the expression evaluation when immediately
5042 * aborting on error, or when an interrupt occurred or
5043 * an exception was thrown but not caught. */
5052 ret
= get_var_tv(s
, len
, rettv
, TRUE
);
5061 *arg
= skipwhite(*arg
);
5063 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5066 ret
= handle_subscript(arg
, rettv
, evaluate
, TRUE
);
5069 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5071 if (ret
== OK
&& evaluate
&& end_leader
> start_leader
)
5078 if (rettv
->v_type
== VAR_FLOAT
)
5079 f
= rettv
->vval
.v_float
;
5082 val
= get_tv_number_chk(rettv
, &error
);
5090 while (end_leader
> start_leader
)
5093 if (*end_leader
== '!')
5096 if (rettv
->v_type
== VAR_FLOAT
)
5102 else if (*end_leader
== '-')
5105 if (rettv
->v_type
== VAR_FLOAT
)
5113 if (rettv
->v_type
== VAR_FLOAT
)
5116 rettv
->vval
.v_float
= f
;
5122 rettv
->v_type
= VAR_NUMBER
;
5123 rettv
->vval
.v_number
= val
;
5132 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5133 * "*arg" points to the '[' or '.'.
5134 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5137 eval_index(arg
, rettv
, evaluate
, verbose
)
5141 int verbose
; /* give error messages */
5143 int empty1
= FALSE
, empty2
= FALSE
;
5144 typval_T var1
, var2
;
5151 if (rettv
->v_type
== VAR_FUNC
5153 || rettv
->v_type
== VAR_FLOAT
5158 EMSG(_("E695: Cannot index a Funcref"));
5168 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
5172 *arg
= skipwhite(key
+ len
);
5179 * Get the (first) variable from inside the [].
5181 *arg
= skipwhite(*arg
+ 1);
5184 else if (eval1(arg
, &var1
, evaluate
) == FAIL
) /* recursive! */
5186 else if (evaluate
&& get_tv_string_chk(&var1
) == NULL
)
5188 /* not a number or string */
5194 * Get the second variable from inside the [:].
5199 *arg
= skipwhite(*arg
+ 1);
5202 else if (eval1(arg
, &var2
, evaluate
) == FAIL
) /* recursive! */
5208 else if (evaluate
&& get_tv_string_chk(&var2
) == NULL
)
5210 /* not a number or string */
5218 /* Check for the ']'. */
5222 EMSG(_(e_missbrac
));
5228 *arg
= skipwhite(*arg
+ 1); /* skip the ']' */
5234 if (!empty1
&& rettv
->v_type
!= VAR_DICT
)
5236 n1
= get_tv_number(&var1
);
5245 n2
= get_tv_number(&var2
);
5250 switch (rettv
->v_type
)
5254 s
= get_tv_string(rettv
);
5255 len
= (long)STRLEN(s
);
5258 /* The resulting variable is a substring. If the indexes
5259 * are out of range the result is empty. */
5270 if (n1
>= len
|| n2
< 0 || n1
> n2
)
5273 s
= vim_strnsave(s
+ n1
, (int)(n2
- n1
+ 1));
5277 /* The resulting variable is a string of a single
5278 * character. If the index is too big or negative the
5279 * result is empty. */
5280 if (n1
>= len
|| n1
< 0)
5283 s
= vim_strnsave(s
+ n1
, 1);
5286 rettv
->v_type
= VAR_STRING
;
5287 rettv
->vval
.v_string
= s
;
5291 len
= list_len(rettv
->vval
.v_list
);
5294 if (!empty1
&& (n1
< 0 || n1
>= len
))
5296 /* For a range we allow invalid values and return an empty
5297 * list. A list index out of range is an error. */
5301 EMSGN(_(e_listidx
), n1
);
5315 if (!empty2
&& (n2
< 0 || n2
+ 1 < n1
))
5320 for (item
= list_find(rettv
->vval
.v_list
, n1
);
5323 if (list_append_tv(l
, &item
->li_tv
) == FAIL
)
5328 item
= item
->li_next
;
5331 rettv
->v_type
= VAR_LIST
;
5332 rettv
->vval
.v_list
= l
;
5337 copy_tv(&list_find(rettv
->vval
.v_list
, n1
)->li_tv
, &var1
);
5347 EMSG(_(e_dictrange
));
5357 key
= get_tv_string(&var1
);
5361 EMSG(_(e_emptykey
));
5367 item
= dict_find(rettv
->vval
.v_dict
, key
, (int)len
);
5369 if (item
== NULL
&& verbose
)
5370 EMSG2(_(e_dictkey
), key
);
5376 copy_tv(&item
->di_tv
, &var1
);
5388 * Get an option value.
5389 * "arg" points to the '&' or '+' before the option name.
5390 * "arg" is advanced to character after the option name.
5391 * Return OK or FAIL.
5394 get_option_tv(arg
, rettv
, evaluate
)
5396 typval_T
*rettv
; /* when NULL, only check if option exists */
5404 int working
= (**arg
== '+'); /* has("+option") */
5409 * Isolate the option name and find its value.
5411 option_end
= find_option_end(arg
, &opt_flags
);
5412 if (option_end
== NULL
)
5415 EMSG2(_("E112: Option name missing: %s"), *arg
);
5427 opt_type
= get_option_value(*arg
, &numval
,
5428 rettv
== NULL
? NULL
: &stringval
, opt_flags
);
5430 if (opt_type
== -3) /* invalid name */
5433 EMSG2(_("E113: Unknown option: %s"), *arg
);
5436 else if (rettv
!= NULL
)
5438 if (opt_type
== -2) /* hidden string option */
5440 rettv
->v_type
= VAR_STRING
;
5441 rettv
->vval
.v_string
= NULL
;
5443 else if (opt_type
== -1) /* hidden number option */
5445 rettv
->v_type
= VAR_NUMBER
;
5446 rettv
->vval
.v_number
= 0;
5448 else if (opt_type
== 1) /* number option */
5450 rettv
->v_type
= VAR_NUMBER
;
5451 rettv
->vval
.v_number
= numval
;
5453 else /* string option */
5455 rettv
->v_type
= VAR_STRING
;
5456 rettv
->vval
.v_string
= stringval
;
5459 else if (working
&& (opt_type
== -2 || opt_type
== -1))
5462 *option_end
= c
; /* put back for error messages */
5469 * Allocate a variable for a string constant.
5470 * Return OK or FAIL.
5473 get_string_tv(arg
, rettv
, evaluate
)
5483 * Find the end of the string, skipping backslashed characters.
5485 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
5487 if (*p
== '\\' && p
[1] != NUL
)
5490 /* A "\<x>" form occupies at least 4 characters, and produces up
5491 * to 6 characters: reserve space for 2 extra */
5499 EMSG2(_("E114: Missing quote: %s"), *arg
);
5503 /* If only parsing, set *arg and return here */
5511 * Copy the string into allocated memory, handling backslashed
5514 name
= alloc((unsigned)(p
- *arg
+ extra
));
5517 rettv
->v_type
= VAR_STRING
;
5518 rettv
->vval
.v_string
= name
;
5520 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; )
5526 case 'b': *name
++ = BS
; ++p
; break;
5527 case 'e': *name
++ = ESC
; ++p
; break;
5528 case 'f': *name
++ = FF
; ++p
; break;
5529 case 'n': *name
++ = NL
; ++p
; break;
5530 case 'r': *name
++ = CAR
; ++p
; break;
5531 case 't': *name
++ = TAB
; ++p
; break;
5533 case 'X': /* hex: "\x1", "\x12" */
5535 case 'u': /* Unicode: "\u0023" */
5537 if (vim_isxdigit(p
[1]))
5540 int c
= toupper(*p
);
5547 while (--n
>= 0 && vim_isxdigit(p
[1]))
5550 nr
= (nr
<< 4) + hex2nr(*p
);
5554 /* For "\u" store the number according to
5557 name
+= (*mb_char2bytes
)(nr
, name
);
5564 /* octal: "\1", "\12", "\123" */
5572 case '7': *name
= *p
++ - '0';
5573 if (*p
>= '0' && *p
<= '7')
5575 *name
= (*name
<< 3) + *p
++ - '0';
5576 if (*p
>= '0' && *p
<= '7')
5577 *name
= (*name
<< 3) + *p
++ - '0';
5582 /* Special key, e.g.: "\<C-W>" */
5583 case '<': extra
= trans_special(&p
, name
, TRUE
);
5591 default: MB_COPY_CHAR(p
, name
);
5596 MB_COPY_CHAR(p
, name
);
5606 * Allocate a variable for a 'str''ing' constant.
5607 * Return OK or FAIL.
5610 get_lit_string_tv(arg
, rettv
, evaluate
)
5620 * Find the end of the string, skipping ''.
5622 for (p
= *arg
+ 1; *p
!= NUL
; mb_ptr_adv(p
))
5635 EMSG2(_("E115: Missing quote: %s"), *arg
);
5639 /* If only parsing return after setting "*arg" */
5647 * Copy the string into allocated memory, handling '' to ' reduction.
5649 str
= alloc((unsigned)((p
- *arg
) - reduce
));
5652 rettv
->v_type
= VAR_STRING
;
5653 rettv
->vval
.v_string
= str
;
5655 for (p
= *arg
+ 1; *p
!= NUL
; )
5663 MB_COPY_CHAR(p
, str
);
5672 * Allocate a variable for a List and fill it from "*arg".
5673 * Return OK or FAIL.
5676 get_list_tv(arg
, rettv
, evaluate
)
5692 *arg
= skipwhite(*arg
+ 1);
5693 while (**arg
!= ']' && **arg
!= NUL
)
5695 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
5699 item
= listitem_alloc();
5703 item
->li_tv
.v_lock
= 0;
5704 list_append(l
, item
);
5714 EMSG2(_("E696: Missing comma in List: %s"), *arg
);
5717 *arg
= skipwhite(*arg
+ 1);
5722 EMSG2(_("E697: Missing end of List ']': %s"), *arg
);
5729 *arg
= skipwhite(*arg
+ 1);
5732 rettv
->v_type
= VAR_LIST
;
5733 rettv
->vval
.v_list
= l
;
5741 * Allocate an empty header for a list.
5742 * Caller should take care of the reference count.
5749 l
= (list_T
*)alloc_clear(sizeof(list_T
));
5752 /* Prepend the list to the list of lists for garbage collection. */
5753 if (first_list
!= NULL
)
5754 first_list
->lv_used_prev
= l
;
5755 l
->lv_used_prev
= NULL
;
5756 l
->lv_used_next
= first_list
;
5763 * Allocate an empty list for a return value.
5764 * Returns OK or FAIL.
5767 rettv_list_alloc(rettv
)
5770 list_T
*l
= list_alloc();
5775 rettv
->vval
.v_list
= l
;
5776 rettv
->v_type
= VAR_LIST
;
5782 * Unreference a list: decrement the reference count and free it when it
5789 if (l
!= NULL
&& --l
->lv_refcount
<= 0)
5794 * Free a list, including all items it points to.
5795 * Ignores the reference count.
5798 list_free(l
, recurse
)
5800 int recurse
; /* Free Lists and Dictionaries recursively. */
5804 /* Remove the list from the list of lists for garbage collection. */
5805 if (l
->lv_used_prev
== NULL
)
5806 first_list
= l
->lv_used_next
;
5808 l
->lv_used_prev
->lv_used_next
= l
->lv_used_next
;
5809 if (l
->lv_used_next
!= NULL
)
5810 l
->lv_used_next
->lv_used_prev
= l
->lv_used_prev
;
5812 for (item
= l
->lv_first
; item
!= NULL
; item
= l
->lv_first
)
5814 /* Remove the item before deleting it. */
5815 l
->lv_first
= item
->li_next
;
5816 if (recurse
|| (item
->li_tv
.v_type
!= VAR_LIST
5817 && item
->li_tv
.v_type
!= VAR_DICT
))
5818 clear_tv(&item
->li_tv
);
5825 * Allocate a list item.
5830 return (listitem_T
*)alloc(sizeof(listitem_T
));
5834 * Free a list item. Also clears the value. Does not notify watchers.
5840 clear_tv(&item
->li_tv
);
5845 * Remove a list item from a List and free it. Also clears the value.
5848 listitem_remove(l
, item
)
5852 list_remove(l
, item
, item
);
5853 listitem_free(item
);
5857 * Get the number of items in a list.
5869 * Return TRUE when two lists have exactly the same values.
5872 list_equal(l1
, l2
, ic
)
5875 int ic
; /* ignore case for strings */
5877 listitem_T
*item1
, *item2
;
5879 if (l1
== NULL
|| l2
== NULL
)
5883 if (list_len(l1
) != list_len(l2
))
5886 for (item1
= l1
->lv_first
, item2
= l2
->lv_first
;
5887 item1
!= NULL
&& item2
!= NULL
;
5888 item1
= item1
->li_next
, item2
= item2
->li_next
)
5889 if (!tv_equal(&item1
->li_tv
, &item2
->li_tv
, ic
))
5891 return item1
== NULL
&& item2
== NULL
;
5894 #if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_MZSCHEME) \
5897 * Return the dictitem that an entry in a hashtable points to.
5908 * Return TRUE when two dictionaries have exactly the same key/values.
5911 dict_equal(d1
, d2
, ic
)
5914 int ic
; /* ignore case for strings */
5920 if (d1
== NULL
|| d2
== NULL
)
5924 if (dict_len(d1
) != dict_len(d2
))
5927 todo
= (int)d1
->dv_hashtab
.ht_used
;
5928 for (hi
= d1
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
5930 if (!HASHITEM_EMPTY(hi
))
5932 item2
= dict_find(d2
, hi
->hi_key
, -1);
5935 if (!tv_equal(&HI2DI(hi
)->di_tv
, &item2
->di_tv
, ic
))
5944 * Return TRUE if "tv1" and "tv2" have the same value.
5945 * Compares the items just like "==" would compare them, but strings and
5946 * numbers are different. Floats and numbers are also different.
5949 tv_equal(tv1
, tv2
, ic
)
5952 int ic
; /* ignore case */
5954 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
5956 static int recursive
= 0; /* cach recursive loops */
5959 if (tv1
->v_type
!= tv2
->v_type
)
5961 /* Catch lists and dicts that have an endless loop by limiting
5962 * recursiveness to 1000. We guess they are equal then. */
5963 if (recursive
>= 1000)
5966 switch (tv1
->v_type
)
5970 r
= list_equal(tv1
->vval
.v_list
, tv2
->vval
.v_list
, ic
);
5976 r
= dict_equal(tv1
->vval
.v_dict
, tv2
->vval
.v_dict
, ic
);
5981 return (tv1
->vval
.v_string
!= NULL
5982 && tv2
->vval
.v_string
!= NULL
5983 && STRCMP(tv1
->vval
.v_string
, tv2
->vval
.v_string
) == 0);
5986 return tv1
->vval
.v_number
== tv2
->vval
.v_number
;
5990 return tv1
->vval
.v_float
== tv2
->vval
.v_float
;
5994 s1
= get_tv_string_buf(tv1
, buf1
);
5995 s2
= get_tv_string_buf(tv2
, buf2
);
5996 return ((ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
)) == 0);
5999 EMSG2(_(e_intern2
), "tv_equal()");
6004 * Locate item with index "n" in list "l" and return it.
6005 * A negative index is counted from the end; -1 is the last item.
6006 * Returns NULL when "n" is out of range.
6019 /* Negative index is relative to the end. */
6023 /* Check for index out of range. */
6024 if (n
< 0 || n
>= l
->lv_len
)
6027 /* When there is a cached index may start search from there. */
6028 if (l
->lv_idx_item
!= NULL
)
6030 if (n
< l
->lv_idx
/ 2)
6032 /* closest to the start of the list */
6036 else if (n
> (l
->lv_idx
+ l
->lv_len
) / 2)
6038 /* closest to the end of the list */
6040 idx
= l
->lv_len
- 1;
6044 /* closest to the cached index */
6045 item
= l
->lv_idx_item
;
6051 if (n
< l
->lv_len
/ 2)
6053 /* closest to the start of the list */
6059 /* closest to the end of the list */
6061 idx
= l
->lv_len
- 1;
6067 /* search forward */
6068 item
= item
->li_next
;
6073 /* search backward */
6074 item
= item
->li_prev
;
6078 /* cache the used index */
6080 l
->lv_idx_item
= item
;
6086 * Get list item "l[idx]" as a number.
6089 list_find_nr(l
, idx
, errorp
)
6092 int *errorp
; /* set to TRUE when something wrong */
6096 li
= list_find(l
, idx
);
6103 return get_tv_number_chk(&li
->li_tv
, errorp
);
6107 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6110 list_find_str(l
, idx
)
6116 li
= list_find(l
, idx
- 1);
6119 EMSGN(_(e_listidx
), idx
);
6122 return get_tv_string(&li
->li_tv
);
6126 * Locate "item" list "l" and return its index.
6127 * Returns -1 when "item" is not in the list.
6130 list_idx_of_item(l
, item
)
6140 for (li
= l
->lv_first
; li
!= NULL
&& li
!= item
; li
= li
->li_next
)
6148 * Append item "item" to the end of list "l".
6151 list_append(l
, item
)
6155 if (l
->lv_last
== NULL
)
6160 item
->li_prev
= NULL
;
6164 l
->lv_last
->li_next
= item
;
6165 item
->li_prev
= l
->lv_last
;
6169 item
->li_next
= NULL
;
6173 * Append typval_T "tv" to the end of list "l".
6174 * Return FAIL when out of memory.
6177 list_append_tv(l
, tv
)
6181 listitem_T
*li
= listitem_alloc();
6185 copy_tv(tv
, &li
->li_tv
);
6191 * Add a dictionary to a list. Used by getqflist().
6192 * Return FAIL when out of memory.
6195 list_append_dict(list
, dict
)
6199 listitem_T
*li
= listitem_alloc();
6203 li
->li_tv
.v_type
= VAR_DICT
;
6204 li
->li_tv
.v_lock
= 0;
6205 li
->li_tv
.vval
.v_dict
= dict
;
6206 list_append(list
, li
);
6207 ++dict
->dv_refcount
;
6212 * Make a copy of "str" and append it as an item to list "l".
6213 * When "len" >= 0 use "str[len]".
6214 * Returns FAIL when out of memory.
6217 list_append_string(l
, str
, len
)
6222 listitem_T
*li
= listitem_alloc();
6227 li
->li_tv
.v_type
= VAR_STRING
;
6228 li
->li_tv
.v_lock
= 0;
6230 li
->li_tv
.vval
.v_string
= NULL
;
6231 else if ((li
->li_tv
.vval
.v_string
= (len
>= 0 ? vim_strnsave(str
, len
)
6232 : vim_strsave(str
))) == NULL
)
6238 * Append "n" to list "l".
6239 * Returns FAIL when out of memory.
6242 list_append_number(l
, n
)
6248 li
= listitem_alloc();
6251 li
->li_tv
.v_type
= VAR_NUMBER
;
6252 li
->li_tv
.v_lock
= 0;
6253 li
->li_tv
.vval
.v_number
= n
;
6259 * Insert typval_T "tv" in list "l" before "item".
6260 * If "item" is NULL append at the end.
6261 * Return FAIL when out of memory.
6264 list_insert_tv(l
, tv
, item
)
6269 listitem_T
*ni
= listitem_alloc();
6273 copy_tv(tv
, &ni
->li_tv
);
6275 /* Append new item at end of list. */
6279 /* Insert new item before existing item. */
6280 ni
->li_prev
= item
->li_prev
;
6282 if (item
->li_prev
== NULL
)
6289 item
->li_prev
->li_next
= ni
;
6290 l
->lv_idx_item
= NULL
;
6299 * Extend "l1" with "l2".
6300 * If "bef" is NULL append at the end, otherwise insert before this item.
6301 * Returns FAIL when out of memory.
6304 list_extend(l1
, l2
, bef
)
6310 int todo
= l2
->lv_len
;
6312 /* We also quit the loop when we have inserted the original item count of
6313 * the list, avoid a hang when we extend a list with itself. */
6314 for (item
= l2
->lv_first
; item
!= NULL
&& --todo
>= 0; item
= item
->li_next
)
6315 if (list_insert_tv(l1
, &item
->li_tv
, bef
) == FAIL
)
6321 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6322 * Return FAIL when out of memory.
6325 list_concat(l1
, l2
, tv
)
6332 if (l1
== NULL
|| l2
== NULL
)
6335 /* make a copy of the first list. */
6336 l
= list_copy(l1
, FALSE
, 0);
6339 tv
->v_type
= VAR_LIST
;
6340 tv
->vval
.v_list
= l
;
6342 /* append all items from the second list */
6343 return list_extend(l
, l2
, NULL
);
6347 * Make a copy of list "orig". Shallow if "deep" is FALSE.
6348 * The refcount of the new list is set to 1.
6349 * See item_copy() for "copyID".
6350 * Returns NULL when out of memory.
6353 list_copy(orig
, deep
, copyID
)
6365 copy
= list_alloc();
6370 /* Do this before adding the items, because one of the items may
6371 * refer back to this list. */
6372 orig
->lv_copyID
= copyID
;
6373 orig
->lv_copylist
= copy
;
6375 for (item
= orig
->lv_first
; item
!= NULL
&& !got_int
;
6376 item
= item
->li_next
)
6378 ni
= listitem_alloc();
6383 if (item_copy(&item
->li_tv
, &ni
->li_tv
, deep
, copyID
) == FAIL
)
6390 copy_tv(&item
->li_tv
, &ni
->li_tv
);
6391 list_append(copy
, ni
);
6393 ++copy
->lv_refcount
;
6405 * Remove items "item" to "item2" from list "l".
6406 * Does not free the listitem or the value!
6409 list_remove(l
, item
, item2
)
6416 /* notify watchers */
6417 for (ip
= item
; ip
!= NULL
; ip
= ip
->li_next
)
6420 list_fix_watch(l
, ip
);
6425 if (item2
->li_next
== NULL
)
6426 l
->lv_last
= item
->li_prev
;
6428 item2
->li_next
->li_prev
= item
->li_prev
;
6429 if (item
->li_prev
== NULL
)
6430 l
->lv_first
= item2
->li_next
;
6432 item
->li_prev
->li_next
= item2
->li_next
;
6433 l
->lv_idx_item
= NULL
;
6437 * Return an allocated string with the string representation of a list.
6441 list2string(tv
, copyID
)
6447 if (tv
->vval
.v_list
== NULL
)
6449 ga_init2(&ga
, (int)sizeof(char), 80);
6450 ga_append(&ga
, '[');
6451 if (list_join(&ga
, tv
->vval
.v_list
, (char_u
*)", ", FALSE
, copyID
) == FAIL
)
6453 vim_free(ga
.ga_data
);
6456 ga_append(&ga
, ']');
6457 ga_append(&ga
, NUL
);
6458 return (char_u
*)ga
.ga_data
;
6462 * Join list "l" into a string in "*gap", using separator "sep".
6463 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
6464 * Return FAIL or OK.
6467 list_join(gap
, l
, sep
, echo
, copyID
)
6476 char_u numbuf
[NUMBUFLEN
];
6480 for (item
= l
->lv_first
; item
!= NULL
&& !got_int
; item
= item
->li_next
)
6485 ga_concat(gap
, sep
);
6488 s
= echo_string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6490 s
= tv2string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6502 * Garbage collection for lists and dictionaries.
6504 * We use reference counts to be able to free most items right away when they
6505 * are no longer used. But for composite items it's possible that it becomes
6506 * unused while the reference count is > 0: When there is a recursive
6507 * reference. Example:
6508 * :let l = [1, 2, 3]
6512 * Since this is quite unusual we handle this with garbage collection: every
6513 * once in a while find out which lists and dicts are not referenced from any
6516 * Here is a good reference text about garbage collection (refers to Python
6517 * but it applies to all reference-counting mechanisms):
6518 * http://python.ca/nas/python/gc/
6522 * Do garbage collection for lists and dicts.
6523 * Return TRUE if some memory was freed.
6532 funccall_T
*fc
, **pfc
;
6534 int did_free_funccal
= FALSE
;
6539 /* Only do this once. */
6540 want_garbage_collect
= FALSE
;
6541 may_garbage_collect
= FALSE
;
6542 garbage_collect_at_exit
= FALSE
;
6544 /* We advance by two because we add one for items referenced through
6545 * previous_funccal. */
6546 current_copyID
+= COPYID_INC
;
6547 copyID
= current_copyID
;
6550 * 1. Go through all accessible variables and mark all lists and dicts
6554 /* Don't free variables in the previous_funccal list unless they are only
6555 * referenced through previous_funccal. This must be first, because if
6556 * the item is referenced elsewhere the funccal must not be freed. */
6557 for (fc
= previous_funccal
; fc
!= NULL
; fc
= fc
->caller
)
6559 set_ref_in_ht(&fc
->l_vars
.dv_hashtab
, copyID
+ 1);
6560 set_ref_in_ht(&fc
->l_avars
.dv_hashtab
, copyID
+ 1);
6563 /* script-local variables */
6564 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
6565 set_ref_in_ht(&SCRIPT_VARS(i
), copyID
);
6567 /* buffer-local variables */
6568 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
6569 set_ref_in_ht(&buf
->b_vars
.dv_hashtab
, copyID
);
6571 /* window-local variables */
6572 FOR_ALL_TAB_WINDOWS(tp
, wp
)
6573 set_ref_in_ht(&wp
->w_vars
.dv_hashtab
, copyID
);
6576 /* tabpage-local variables */
6577 for (tp
= first_tabpage
; tp
!= NULL
; tp
= tp
->tp_next
)
6578 set_ref_in_ht(&tp
->tp_vars
.dv_hashtab
, copyID
);
6581 /* global variables */
6582 set_ref_in_ht(&globvarht
, copyID
);
6584 /* function-local variables */
6585 for (fc
= current_funccal
; fc
!= NULL
; fc
= fc
->caller
)
6587 set_ref_in_ht(&fc
->l_vars
.dv_hashtab
, copyID
);
6588 set_ref_in_ht(&fc
->l_avars
.dv_hashtab
, copyID
);
6592 set_ref_in_ht(&vimvarht
, copyID
);
6595 * 2. Free lists and dictionaries that are not referenced.
6597 did_free
= free_unref_items(copyID
);
6600 * 3. Check if any funccal can be freed now.
6602 for (pfc
= &previous_funccal
; *pfc
!= NULL
; )
6604 if (can_free_funccal(*pfc
, copyID
))
6608 free_funccal(fc
, TRUE
);
6610 did_free_funccal
= TRUE
;
6613 pfc
= &(*pfc
)->caller
;
6615 if (did_free_funccal
)
6616 /* When a funccal was freed some more items might be garbage
6617 * collected, so run again. */
6618 (void)garbage_collect();
6624 * Free lists and dictionaries that are no longer referenced.
6627 free_unref_items(copyID
)
6632 int did_free
= FALSE
;
6635 * Go through the list of dicts and free items without the copyID.
6637 for (dd
= first_dict
; dd
!= NULL
; )
6638 if ((dd
->dv_copyID
& COPYID_MASK
) != (copyID
& COPYID_MASK
))
6640 /* Free the Dictionary and ordinary items it contains, but don't
6641 * recurse into Lists and Dictionaries, they will be in the list
6642 * of dicts or list of lists. */
6643 dict_free(dd
, FALSE
);
6646 /* restart, next dict may also have been freed */
6650 dd
= dd
->dv_used_next
;
6653 * Go through the list of lists and free items without the copyID.
6654 * But don't free a list that has a watcher (used in a for loop), these
6655 * are not referenced anywhere.
6657 for (ll
= first_list
; ll
!= NULL
; )
6658 if ((ll
->lv_copyID
& COPYID_MASK
) != (copyID
& COPYID_MASK
)
6659 && ll
->lv_watch
== NULL
)
6661 /* Free the List and ordinary items it contains, but don't recurse
6662 * into Lists and Dictionaries, they will be in the list of dicts
6663 * or list of lists. */
6664 list_free(ll
, FALSE
);
6667 /* restart, next list may also have been freed */
6671 ll
= ll
->lv_used_next
;
6677 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6680 set_ref_in_ht(ht
, copyID
)
6687 todo
= (int)ht
->ht_used
;
6688 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
6689 if (!HASHITEM_EMPTY(hi
))
6692 set_ref_in_item(&HI2DI(hi
)->di_tv
, copyID
);
6697 * Mark all lists and dicts referenced through list "l" with "copyID".
6700 set_ref_in_list(l
, copyID
)
6706 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
6707 set_ref_in_item(&li
->li_tv
, copyID
);
6711 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6714 set_ref_in_item(tv
, copyID
)
6724 dd
= tv
->vval
.v_dict
;
6725 if (dd
!= NULL
&& dd
->dv_copyID
!= copyID
)
6727 /* Didn't see this dict yet. */
6728 dd
->dv_copyID
= copyID
;
6729 set_ref_in_ht(&dd
->dv_hashtab
, copyID
);
6734 ll
= tv
->vval
.v_list
;
6735 if (ll
!= NULL
&& ll
->lv_copyID
!= copyID
)
6737 /* Didn't see this list yet. */
6738 ll
->lv_copyID
= copyID
;
6739 set_ref_in_list(ll
, copyID
);
6747 * Allocate an empty header for a dictionary.
6754 d
= (dict_T
*)alloc(sizeof(dict_T
));
6757 /* Add the list to the list of dicts for garbage collection. */
6758 if (first_dict
!= NULL
)
6759 first_dict
->dv_used_prev
= d
;
6760 d
->dv_used_next
= first_dict
;
6761 d
->dv_used_prev
= NULL
;
6764 hash_init(&d
->dv_hashtab
);
6773 * Unreference a Dictionary: decrement the reference count and free it when it
6780 if (d
!= NULL
&& --d
->dv_refcount
<= 0)
6785 * Free a Dictionary, including all items it contains.
6786 * Ignores the reference count.
6789 dict_free(d
, recurse
)
6791 int recurse
; /* Free Lists and Dictionaries recursively. */
6797 /* Remove the dict from the list of dicts for garbage collection. */
6798 if (d
->dv_used_prev
== NULL
)
6799 first_dict
= d
->dv_used_next
;
6801 d
->dv_used_prev
->dv_used_next
= d
->dv_used_next
;
6802 if (d
->dv_used_next
!= NULL
)
6803 d
->dv_used_next
->dv_used_prev
= d
->dv_used_prev
;
6805 /* Lock the hashtab, we don't want it to resize while freeing items. */
6806 hash_lock(&d
->dv_hashtab
);
6807 todo
= (int)d
->dv_hashtab
.ht_used
;
6808 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
6810 if (!HASHITEM_EMPTY(hi
))
6812 /* Remove the item before deleting it, just in case there is
6813 * something recursive causing trouble. */
6815 hash_remove(&d
->dv_hashtab
, hi
);
6816 if (recurse
|| (di
->di_tv
.v_type
!= VAR_LIST
6817 && di
->di_tv
.v_type
!= VAR_DICT
))
6818 clear_tv(&di
->di_tv
);
6823 hash_clear(&d
->dv_hashtab
);
6828 * Allocate a Dictionary item.
6829 * The "key" is copied to the new item.
6830 * Note that the value of the item "di_tv" still needs to be initialized!
6831 * Returns NULL when out of memory.
6839 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
) + STRLEN(key
)));
6842 STRCPY(di
->di_key
, key
);
6849 * Make a copy of a Dictionary item.
6857 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
6858 + STRLEN(org
->di_key
)));
6861 STRCPY(di
->di_key
, org
->di_key
);
6863 copy_tv(&org
->di_tv
, &di
->di_tv
);
6869 * Remove item "item" from Dictionary "dict" and free it.
6872 dictitem_remove(dict
, item
)
6878 hi
= hash_find(&dict
->dv_hashtab
, item
->di_key
);
6879 if (HASHITEM_EMPTY(hi
))
6880 EMSG2(_(e_intern2
), "dictitem_remove()");
6882 hash_remove(&dict
->dv_hashtab
, hi
);
6883 dictitem_free(item
);
6887 * Free a dict item. Also clears the value.
6893 clear_tv(&item
->di_tv
);
6898 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6899 * The refcount of the new dict is set to 1.
6900 * See item_copy() for "copyID".
6901 * Returns NULL when out of memory.
6904 dict_copy(orig
, deep
, copyID
)
6917 copy
= dict_alloc();
6922 orig
->dv_copyID
= copyID
;
6923 orig
->dv_copydict
= copy
;
6925 todo
= (int)orig
->dv_hashtab
.ht_used
;
6926 for (hi
= orig
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
6928 if (!HASHITEM_EMPTY(hi
))
6932 di
= dictitem_alloc(hi
->hi_key
);
6937 if (item_copy(&HI2DI(hi
)->di_tv
, &di
->di_tv
, deep
,
6945 copy_tv(&HI2DI(hi
)->di_tv
, &di
->di_tv
);
6946 if (dict_add(copy
, di
) == FAIL
)
6954 ++copy
->dv_refcount
;
6966 * Add item "item" to Dictionary "d".
6967 * Returns FAIL when out of memory and when key already existed.
6974 return hash_add(&d
->dv_hashtab
, item
->di_key
);
6978 * Add a number or string entry to dictionary "d".
6979 * When "str" is NULL use number "nr", otherwise use "str".
6980 * Returns FAIL when out of memory and when key already exists.
6983 dict_add_nr_str(d
, key
, nr
, str
)
6991 item
= dictitem_alloc((char_u
*)key
);
6994 item
->di_tv
.v_lock
= 0;
6997 item
->di_tv
.v_type
= VAR_NUMBER
;
6998 item
->di_tv
.vval
.v_number
= nr
;
7002 item
->di_tv
.v_type
= VAR_STRING
;
7003 item
->di_tv
.vval
.v_string
= vim_strsave(str
);
7005 if (dict_add(d
, item
) == FAIL
)
7007 dictitem_free(item
);
7014 * Get the number of items in a Dictionary.
7022 return (long)d
->dv_hashtab
.ht_used
;
7026 * Find item "key[len]" in Dictionary "d".
7027 * If "len" is negative use strlen(key).
7028 * Returns NULL when not found.
7031 dict_find(d
, key
, len
)
7037 char_u buf
[AKEYLEN
];
7039 char_u
*tofree
= NULL
;
7044 else if (len
>= AKEYLEN
)
7046 tofree
= akey
= vim_strnsave(key
, len
);
7052 /* Avoid a malloc/free by using buf[]. */
7053 vim_strncpy(buf
, key
, len
);
7057 hi
= hash_find(&d
->dv_hashtab
, akey
);
7059 if (HASHITEM_EMPTY(hi
))
7065 * Get a string item from a dictionary.
7066 * When "save" is TRUE allocate memory for it.
7067 * Returns NULL if the entry doesn't exist or out of memory.
7070 get_dict_string(d
, key
, save
)
7078 di
= dict_find(d
, key
, -1);
7081 s
= get_tv_string(&di
->di_tv
);
7082 if (save
&& s
!= NULL
)
7088 * Get a number item from a dictionary.
7089 * Returns 0 if the entry doesn't exist or out of memory.
7092 get_dict_number(d
, key
)
7098 di
= dict_find(d
, key
, -1);
7101 return get_tv_number(&di
->di_tv
);
7105 * Return an allocated string with the string representation of a Dictionary.
7109 dict2string(tv
, copyID
)
7116 char_u numbuf
[NUMBUFLEN
];
7122 if ((d
= tv
->vval
.v_dict
) == NULL
)
7124 ga_init2(&ga
, (int)sizeof(char), 80);
7125 ga_append(&ga
, '{');
7127 todo
= (int)d
->dv_hashtab
.ht_used
;
7128 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
7130 if (!HASHITEM_EMPTY(hi
))
7137 ga_concat(&ga
, (char_u
*)", ");
7139 tofree
= string_quote(hi
->hi_key
, FALSE
);
7142 ga_concat(&ga
, tofree
);
7145 ga_concat(&ga
, (char_u
*)": ");
7146 s
= tv2string(&HI2DI(hi
)->di_tv
, &tofree
, numbuf
, copyID
);
7156 vim_free(ga
.ga_data
);
7160 ga_append(&ga
, '}');
7161 ga_append(&ga
, NUL
);
7162 return (char_u
*)ga
.ga_data
;
7166 * Allocate a variable for a Dictionary and fill it from "*arg".
7167 * Return OK or FAIL. Returns NOTDONE for {expr}.
7170 get_dict_tv(arg
, rettv
, evaluate
)
7180 char_u
*start
= skipwhite(*arg
+ 1);
7181 char_u buf
[NUMBUFLEN
];
7184 * First check if it's not a curly-braces thing: {expr}.
7185 * Must do this without evaluating, otherwise a function may be called
7186 * twice. Unfortunately this means we need to call eval1() twice for the
7188 * But {} is an empty Dictionary.
7192 if (eval1(&start
, &tv
, FALSE
) == FAIL
) /* recursive! */
7204 tvkey
.v_type
= VAR_UNKNOWN
;
7205 tv
.v_type
= VAR_UNKNOWN
;
7207 *arg
= skipwhite(*arg
+ 1);
7208 while (**arg
!= '}' && **arg
!= NUL
)
7210 if (eval1(arg
, &tvkey
, evaluate
) == FAIL
) /* recursive! */
7214 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg
);
7220 key
= get_tv_string_buf_chk(&tvkey
, buf
);
7221 if (key
== NULL
|| *key
== NUL
)
7223 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7225 EMSG(_(e_emptykey
));
7231 *arg
= skipwhite(*arg
+ 1);
7232 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
7240 item
= dict_find(d
, key
, -1);
7243 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key
);
7248 item
= dictitem_alloc(key
);
7253 item
->di_tv
.v_lock
= 0;
7254 if (dict_add(d
, item
) == FAIL
)
7255 dictitem_free(item
);
7263 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg
);
7266 *arg
= skipwhite(*arg
+ 1);
7271 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg
);
7278 *arg
= skipwhite(*arg
+ 1);
7281 rettv
->v_type
= VAR_DICT
;
7282 rettv
->vval
.v_dict
= d
;
7290 * Return a string with the string representation of a variable.
7291 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7292 * "numbuf" is used for a number.
7293 * Does not put quotes around strings, as ":echo" displays values.
7294 * When "copyID" is not NULL replace recursive lists and dicts with "...".
7298 echo_string(tv
, tofree
, numbuf
, copyID
)
7304 static int recurse
= 0;
7307 if (recurse
>= DICT_MAXNEST
)
7309 EMSG(_("E724: variable nested too deep for displaying"));
7319 r
= tv
->vval
.v_string
;
7323 if (tv
->vval
.v_list
== NULL
)
7328 else if (copyID
!= 0 && tv
->vval
.v_list
->lv_copyID
== copyID
)
7331 r
= (char_u
*)"[...]";
7335 tv
->vval
.v_list
->lv_copyID
= copyID
;
7336 *tofree
= list2string(tv
, copyID
);
7342 if (tv
->vval
.v_dict
== NULL
)
7347 else if (copyID
!= 0 && tv
->vval
.v_dict
->dv_copyID
== copyID
)
7350 r
= (char_u
*)"{...}";
7354 tv
->vval
.v_dict
->dv_copyID
= copyID
;
7355 *tofree
= dict2string(tv
, copyID
);
7363 r
= get_tv_string_buf(tv
, numbuf
);
7369 vim_snprintf((char *)numbuf
, NUMBUFLEN
, "%g", tv
->vval
.v_float
);
7375 EMSG2(_(e_intern2
), "echo_string()");
7384 * Return a string with the string representation of a variable.
7385 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7386 * "numbuf" is used for a number.
7387 * Puts quotes around strings, so that they can be parsed back by eval().
7391 tv2string(tv
, tofree
, numbuf
, copyID
)
7400 *tofree
= string_quote(tv
->vval
.v_string
, TRUE
);
7403 *tofree
= string_quote(tv
->vval
.v_string
, FALSE
);
7408 vim_snprintf((char *)numbuf
, NUMBUFLEN
- 1, "%g", tv
->vval
.v_float
);
7416 EMSG2(_(e_intern2
), "tv2string()");
7418 return echo_string(tv
, tofree
, numbuf
, copyID
);
7422 * Return string "str" in ' quotes, doubling ' characters.
7423 * If "str" is NULL an empty string is assumed.
7424 * If "function" is TRUE make it function('string').
7427 string_quote(str
, function
)
7434 len
= (function
? 13 : 3);
7437 len
+= (unsigned)STRLEN(str
);
7438 for (p
= str
; *p
!= NUL
; mb_ptr_adv(p
))
7447 STRCPY(r
, "function('");
7453 for (p
= str
; *p
!= NUL
; )
7469 * Convert the string "text" to a floating point number.
7470 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7471 * this always uses a decimal point.
7472 * Returns the length of the text that was consumed.
7475 string2float(text
, value
)
7477 float_T
*value
; /* result stored here */
7479 char *s
= (char *)text
;
7484 return (int)((char_u
*)s
- text
);
7489 * Get the value of an environment variable.
7490 * "arg" is pointing to the '$'. It is advanced to after the name.
7491 * If the environment variable was not set, silently assume it is empty.
7495 get_env_tv(arg
, rettv
, evaluate
)
7500 char_u
*string
= NULL
;
7504 int mustfree
= FALSE
;
7508 len
= get_env_len(arg
);
7515 /* first try vim_getenv(), fast for normal environment vars */
7516 string
= vim_getenv(name
, &mustfree
);
7517 if (string
!= NULL
&& *string
!= NUL
)
7520 string
= vim_strsave(string
);
7527 /* next try expanding things like $VIM and ${HOME} */
7528 string
= expand_env_save(name
- 1);
7529 if (string
!= NULL
&& *string
== '$')
7537 rettv
->v_type
= VAR_STRING
;
7538 rettv
->vval
.v_string
= string
;
7545 * Array with names and number of arguments of all internal functions
7546 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7550 char *f_name
; /* function name */
7551 char f_min_argc
; /* minimal number of arguments */
7552 char f_max_argc
; /* maximal number of arguments */
7553 void (*f_func
) __ARGS((typval_T
*args
, typval_T
*rvar
));
7554 /* implementation of function */
7558 {"abs", 1, 1, f_abs
},
7559 {"acos", 1, 1, f_acos
}, /* WJMc */
7561 {"add", 2, 2, f_add
},
7562 {"append", 2, 2, f_append
},
7563 {"argc", 0, 0, f_argc
},
7564 {"argidx", 0, 0, f_argidx
},
7565 {"argv", 0, 1, f_argv
},
7567 {"asin", 1, 1, f_asin
}, /* WJMc */
7568 {"atan", 1, 1, f_atan
},
7569 {"atan2", 2, 2, f_atan2
}, /* WJMc */
7571 {"browse", 4, 4, f_browse
},
7572 {"browsedir", 2, 2, f_browsedir
},
7573 {"bufexists", 1, 1, f_bufexists
},
7574 {"buffer_exists", 1, 1, f_bufexists
}, /* obsolete */
7575 {"buffer_name", 1, 1, f_bufname
}, /* obsolete */
7576 {"buffer_number", 1, 1, f_bufnr
}, /* obsolete */
7577 {"buflisted", 1, 1, f_buflisted
},
7578 {"bufloaded", 1, 1, f_bufloaded
},
7579 {"bufname", 1, 1, f_bufname
},
7580 {"bufnr", 1, 2, f_bufnr
},
7581 {"bufwinnr", 1, 1, f_bufwinnr
},
7582 {"byte2line", 1, 1, f_byte2line
},
7583 {"byteidx", 2, 2, f_byteidx
},
7584 {"call", 2, 3, f_call
},
7586 {"ceil", 1, 1, f_ceil
},
7588 {"changenr", 0, 0, f_changenr
},
7589 {"char2nr", 1, 1, f_char2nr
},
7590 {"cindent", 1, 1, f_cindent
},
7591 {"clearmatches", 0, 0, f_clearmatches
},
7592 {"col", 1, 1, f_col
},
7593 #if defined(FEAT_INS_EXPAND)
7594 {"complete", 2, 2, f_complete
},
7595 {"complete_add", 1, 1, f_complete_add
},
7596 {"complete_check", 0, 0, f_complete_check
},
7598 {"confirm", 1, 4, f_confirm
},
7599 {"copy", 1, 1, f_copy
},
7601 {"cos", 1, 1, f_cos
},
7602 {"cosh", 1, 1, f_cosh
}, /* WJMc */
7604 {"count", 2, 4, f_count
},
7605 {"cscope_connection",0,3, f_cscope_connection
},
7606 {"cursor", 1, 3, f_cursor
},
7607 {"deepcopy", 1, 2, f_deepcopy
},
7608 {"delete", 1, 1, f_delete
},
7609 {"did_filetype", 0, 0, f_did_filetype
},
7610 {"diff_filler", 1, 1, f_diff_filler
},
7611 {"diff_hlID", 2, 2, f_diff_hlID
},
7612 {"empty", 1, 1, f_empty
},
7613 {"escape", 2, 2, f_escape
},
7614 {"eval", 1, 1, f_eval
},
7615 {"eventhandler", 0, 0, f_eventhandler
},
7616 {"executable", 1, 1, f_executable
},
7617 {"exists", 1, 1, f_exists
},
7619 {"exp", 1, 1, f_exp
}, /* WJMc */
7621 {"expand", 1, 2, f_expand
},
7622 {"extend", 2, 3, f_extend
},
7623 {"feedkeys", 1, 2, f_feedkeys
},
7624 {"file_readable", 1, 1, f_filereadable
}, /* obsolete */
7625 {"filereadable", 1, 1, f_filereadable
},
7626 {"filewritable", 1, 1, f_filewritable
},
7627 {"filter", 2, 2, f_filter
},
7628 {"finddir", 1, 3, f_finddir
},
7629 {"findfile", 1, 3, f_findfile
},
7631 {"float2nr", 1, 1, f_float2nr
},
7632 {"floor", 1, 1, f_floor
},
7633 {"fmod", 2, 2, f_fmod
}, /* WJMc */
7635 {"fnameescape", 1, 1, f_fnameescape
},
7636 {"fnamemodify", 2, 2, f_fnamemodify
},
7637 {"foldclosed", 1, 1, f_foldclosed
},
7638 {"foldclosedend", 1, 1, f_foldclosedend
},
7639 {"foldlevel", 1, 1, f_foldlevel
},
7640 {"foldtext", 0, 0, f_foldtext
},
7641 {"foldtextresult", 1, 1, f_foldtextresult
},
7642 {"foreground", 0, 0, f_foreground
},
7643 {"function", 1, 1, f_function
},
7644 {"garbagecollect", 0, 1, f_garbagecollect
},
7645 {"get", 2, 3, f_get
},
7646 {"getbufline", 2, 3, f_getbufline
},
7647 {"getbufvar", 2, 2, f_getbufvar
},
7648 {"getchar", 0, 1, f_getchar
},
7649 {"getcharmod", 0, 0, f_getcharmod
},
7650 {"getcmdline", 0, 0, f_getcmdline
},
7651 {"getcmdpos", 0, 0, f_getcmdpos
},
7652 {"getcmdtype", 0, 0, f_getcmdtype
},
7653 {"getcwd", 0, 0, f_getcwd
},
7654 {"getfontname", 0, 1, f_getfontname
},
7655 {"getfperm", 1, 1, f_getfperm
},
7656 {"getfsize", 1, 1, f_getfsize
},
7657 {"getftime", 1, 1, f_getftime
},
7658 {"getftype", 1, 1, f_getftype
},
7659 {"getline", 1, 2, f_getline
},
7660 {"getloclist", 1, 1, f_getqflist
},
7661 {"getmatches", 0, 0, f_getmatches
},
7662 {"getpid", 0, 0, f_getpid
},
7663 {"getpos", 1, 1, f_getpos
},
7664 {"getqflist", 0, 0, f_getqflist
},
7665 {"getreg", 0, 2, f_getreg
},
7666 {"getregtype", 0, 1, f_getregtype
},
7667 {"gettabwinvar", 3, 3, f_gettabwinvar
},
7668 {"getwinposx", 0, 0, f_getwinposx
},
7669 {"getwinposy", 0, 0, f_getwinposy
},
7670 {"getwinvar", 2, 2, f_getwinvar
},
7671 {"glob", 1, 2, f_glob
},
7672 {"globpath", 2, 3, f_globpath
},
7673 {"has", 1, 1, f_has
},
7674 {"has_key", 2, 2, f_has_key
},
7675 {"haslocaldir", 0, 0, f_haslocaldir
},
7676 {"hasmapto", 1, 3, f_hasmapto
},
7677 {"highlightID", 1, 1, f_hlID
}, /* obsolete */
7678 {"highlight_exists",1, 1, f_hlexists
}, /* obsolete */
7679 {"histadd", 2, 2, f_histadd
},
7680 {"histdel", 1, 2, f_histdel
},
7681 {"histget", 1, 2, f_histget
},
7682 {"histnr", 1, 1, f_histnr
},
7683 {"hlID", 1, 1, f_hlID
},
7684 {"hlexists", 1, 1, f_hlexists
},
7685 {"hostname", 0, 0, f_hostname
},
7686 {"iconv", 3, 3, f_iconv
},
7687 {"indent", 1, 1, f_indent
},
7688 {"index", 2, 4, f_index
},
7689 {"input", 1, 3, f_input
},
7690 {"inputdialog", 1, 3, f_inputdialog
},
7691 {"inputlist", 1, 1, f_inputlist
},
7692 {"inputrestore", 0, 0, f_inputrestore
},
7693 {"inputsave", 0, 0, f_inputsave
},
7694 {"inputsecret", 1, 2, f_inputsecret
},
7695 {"insert", 2, 3, f_insert
},
7696 {"isdirectory", 1, 1, f_isdirectory
},
7697 {"islocked", 1, 1, f_islocked
},
7698 {"items", 1, 1, f_items
},
7699 {"join", 1, 2, f_join
},
7700 {"keys", 1, 1, f_keys
},
7701 {"last_buffer_nr", 0, 0, f_last_buffer_nr
},/* obsolete */
7702 {"len", 1, 1, f_len
},
7703 {"libcall", 3, 3, f_libcall
},
7704 {"libcallnr", 3, 3, f_libcallnr
},
7705 {"line", 1, 1, f_line
},
7706 {"line2byte", 1, 1, f_line2byte
},
7707 {"lispindent", 1, 1, f_lispindent
},
7708 {"localtime", 0, 0, f_localtime
},
7710 {"log", 1, 1, f_log
}, /* WJMc */
7711 {"log10", 1, 1, f_log10
},
7713 {"map", 2, 2, f_map
},
7714 {"maparg", 1, 3, f_maparg
},
7715 {"mapcheck", 1, 3, f_mapcheck
},
7716 {"match", 2, 4, f_match
},
7717 {"matchadd", 2, 4, f_matchadd
},
7718 {"matcharg", 1, 1, f_matcharg
},
7719 {"matchdelete", 1, 1, f_matchdelete
},
7720 {"matchend", 2, 4, f_matchend
},
7721 {"matchlist", 2, 4, f_matchlist
},
7722 {"matchstr", 2, 4, f_matchstr
},
7723 {"max", 1, 1, f_max
},
7724 {"min", 1, 1, f_min
},
7726 {"mkdir", 1, 3, f_mkdir
},
7728 {"mode", 0, 1, f_mode
},
7729 #ifdef FEAT_MZSCHEME
7730 {"mzeval", 1, 1, f_mzeval
},
7732 {"nextnonblank", 1, 1, f_nextnonblank
},
7733 {"nr2char", 1, 1, f_nr2char
},
7734 {"pathshorten", 1, 1, f_pathshorten
},
7736 {"pow", 2, 2, f_pow
},
7738 {"prevnonblank", 1, 1, f_prevnonblank
},
7739 {"printf", 2, 19, f_printf
},
7740 {"pumvisible", 0, 0, f_pumvisible
},
7741 {"range", 1, 3, f_range
},
7742 {"readfile", 1, 3, f_readfile
},
7743 {"reltime", 0, 2, f_reltime
},
7744 {"reltimestr", 1, 1, f_reltimestr
},
7745 {"remote_expr", 2, 3, f_remote_expr
},
7746 {"remote_foreground", 1, 1, f_remote_foreground
},
7747 {"remote_peek", 1, 2, f_remote_peek
},
7748 {"remote_read", 1, 1, f_remote_read
},
7749 {"remote_send", 2, 3, f_remote_send
},
7750 {"remove", 2, 3, f_remove
},
7751 {"rename", 2, 2, f_rename
},
7752 {"repeat", 2, 2, f_repeat
},
7753 {"resolve", 1, 1, f_resolve
},
7754 {"reverse", 1, 1, f_reverse
},
7756 {"round", 1, 1, f_round
},
7758 {"search", 1, 4, f_search
},
7759 {"searchdecl", 1, 3, f_searchdecl
},
7760 {"searchpair", 3, 7, f_searchpair
},
7761 {"searchpairpos", 3, 7, f_searchpairpos
},
7762 {"searchpos", 1, 4, f_searchpos
},
7763 {"server2client", 2, 2, f_server2client
},
7764 {"serverlist", 0, 0, f_serverlist
},
7765 {"setbufvar", 3, 3, f_setbufvar
},
7766 {"setcmdpos", 1, 1, f_setcmdpos
},
7767 {"setline", 2, 2, f_setline
},
7768 {"setloclist", 2, 3, f_setloclist
},
7769 {"setmatches", 1, 1, f_setmatches
},
7770 {"setpos", 2, 2, f_setpos
},
7771 {"setqflist", 1, 2, f_setqflist
},
7772 {"setreg", 2, 3, f_setreg
},
7773 {"settabwinvar", 4, 4, f_settabwinvar
},
7774 {"setwinvar", 3, 3, f_setwinvar
},
7775 {"shellescape", 1, 2, f_shellescape
},
7776 {"simplify", 1, 1, f_simplify
},
7778 {"sin", 1, 1, f_sin
},
7779 {"sinh", 1, 1, f_sinh
}, /* WJMc */
7781 {"sort", 1, 2, f_sort
},
7782 {"soundfold", 1, 1, f_soundfold
},
7783 {"spellbadword", 0, 1, f_spellbadword
},
7784 {"spellsuggest", 1, 3, f_spellsuggest
},
7785 {"split", 1, 3, f_split
},
7787 {"sqrt", 1, 1, f_sqrt
},
7788 {"str2float", 1, 1, f_str2float
},
7790 {"str2nr", 1, 2, f_str2nr
},
7791 #ifdef HAVE_STRFTIME
7792 {"strftime", 1, 2, f_strftime
},
7794 {"stridx", 2, 3, f_stridx
},
7795 {"string", 1, 1, f_string
},
7796 {"strlen", 1, 1, f_strlen
},
7797 {"strpart", 2, 3, f_strpart
},
7798 {"strridx", 2, 3, f_strridx
},
7799 {"strtrans", 1, 1, f_strtrans
},
7800 {"submatch", 1, 1, f_submatch
},
7801 {"substitute", 4, 4, f_substitute
},
7802 {"synID", 3, 3, f_synID
},
7803 {"synIDattr", 2, 3, f_synIDattr
},
7804 {"synIDtrans", 1, 1, f_synIDtrans
},
7805 {"synstack", 2, 2, f_synstack
},
7806 {"system", 1, 2, f_system
},
7807 {"tabpagebuflist", 0, 1, f_tabpagebuflist
},
7808 {"tabpagenr", 0, 1, f_tabpagenr
},
7809 {"tabpagewinnr", 1, 2, f_tabpagewinnr
},
7810 {"tagfiles", 0, 0, f_tagfiles
},
7811 {"taglist", 1, 1, f_taglist
},
7812 {"tan", 1, 1, f_tan
}, /* WJMc */
7813 {"tanh", 1, 1, f_tanh
}, /* WJMc */
7814 {"tempname", 0, 0, f_tempname
},
7815 {"test", 1, 1, f_test
},
7816 {"tolower", 1, 1, f_tolower
},
7817 {"toupper", 1, 1, f_toupper
},
7820 {"trunc", 1, 1, f_trunc
},
7822 {"type", 1, 1, f_type
},
7823 {"values", 1, 1, f_values
},
7824 {"virtcol", 1, 1, f_virtcol
},
7825 {"visualmode", 0, 1, f_visualmode
},
7826 {"winbufnr", 1, 1, f_winbufnr
},
7827 {"wincol", 0, 0, f_wincol
},
7828 {"winheight", 1, 1, f_winheight
},
7829 {"winline", 0, 0, f_winline
},
7830 {"winnr", 0, 1, f_winnr
},
7831 {"winrestcmd", 0, 0, f_winrestcmd
},
7832 {"winrestview", 1, 1, f_winrestview
},
7833 {"winsaveview", 0, 0, f_winsaveview
},
7834 {"winwidth", 1, 1, f_winwidth
},
7835 {"writefile", 2, 3, f_writefile
},
7838 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7841 * Function given to ExpandGeneric() to obtain the list of internal
7842 * or user defined function names.
7845 get_function_name(xp
, idx
)
7849 static int intidx
= -1;
7856 name
= get_user_func_name(xp
, idx
);
7860 if (++intidx
< (int)(sizeof(functions
) / sizeof(struct fst
)))
7862 STRCPY(IObuff
, functions
[intidx
].f_name
);
7863 STRCAT(IObuff
, "(");
7864 if (functions
[intidx
].f_max_argc
== 0)
7865 STRCAT(IObuff
, ")");
7873 * Function given to ExpandGeneric() to obtain the list of internal or
7874 * user defined variable or function names.
7877 get_expr_name(xp
, idx
)
7881 static int intidx
= -1;
7888 name
= get_function_name(xp
, idx
);
7892 return get_user_var_name(xp
, ++intidx
);
7895 #endif /* FEAT_CMDL_COMPL */
7898 * Find internal function in table above.
7899 * Return index, or -1 if not found
7902 find_internal_func(name
)
7903 char_u
*name
; /* name of the function */
7906 int last
= (int)(sizeof(functions
) / sizeof(struct fst
)) - 1;
7911 * Find the function name in the table. Binary search.
7913 while (first
<= last
)
7915 x
= first
+ ((unsigned)(last
- first
) >> 1);
7916 cmp
= STRCMP(name
, functions
[x
].f_name
);
7928 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7929 * name it contains, otherwise return "name".
7932 deref_func_name(name
, lenp
)
7941 v
= find_var(name
, NULL
);
7943 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
7945 if (v
->di_tv
.vval
.v_string
== NULL
)
7948 return (char_u
*)""; /* just in case */
7950 *lenp
= (int)STRLEN(v
->di_tv
.vval
.v_string
);
7951 return v
->di_tv
.vval
.v_string
;
7958 * Allocate a variable for the result of a function.
7959 * Return OK or FAIL.
7962 get_func_tv(name
, len
, rettv
, arg
, firstline
, lastline
, doesrange
,
7964 char_u
*name
; /* name of the function */
7965 int len
; /* length of "name" */
7967 char_u
**arg
; /* argument, pointing to the '(' */
7968 linenr_T firstline
; /* first line of range */
7969 linenr_T lastline
; /* last line of range */
7970 int *doesrange
; /* return: function handled range */
7972 dict_T
*selfdict
; /* Dictionary for "self" */
7976 typval_T argvars
[MAX_FUNC_ARGS
+ 1]; /* vars for arguments */
7977 int argcount
= 0; /* number of arguments found */
7980 * Get the arguments.
7983 while (argcount
< MAX_FUNC_ARGS
)
7985 argp
= skipwhite(argp
+ 1); /* skip the '(' or ',' */
7986 if (*argp
== ')' || *argp
== ',' || *argp
== NUL
)
7988 if (eval1(&argp
, &argvars
[argcount
], evaluate
) == FAIL
)
8003 ret
= call_func(name
, len
, rettv
, argcount
, argvars
,
8004 firstline
, lastline
, doesrange
, evaluate
, selfdict
);
8005 else if (!aborting())
8007 if (argcount
== MAX_FUNC_ARGS
)
8008 emsg_funcname(N_("E740: Too many arguments for function %s"), name
);
8010 emsg_funcname(N_("E116: Invalid arguments for function %s"), name
);
8013 while (--argcount
>= 0)
8014 clear_tv(&argvars
[argcount
]);
8016 *arg
= skipwhite(argp
);
8022 * Call a function with its resolved parameters
8023 * Return OK when the function can't be called, FAIL otherwise.
8024 * Also returns OK when an error was encountered while executing the function.
8027 call_func(func_name
, len
, rettv
, argcount
, argvars
, firstline
, lastline
,
8028 doesrange
, evaluate
, selfdict
)
8029 char_u
*func_name
; /* name of the function */
8030 int len
; /* length of "name" */
8031 typval_T
*rettv
; /* return value goes here */
8032 int argcount
; /* number of "argvars" */
8033 typval_T
*argvars
; /* vars for arguments, must have "argcount"
8034 PLUS ONE elements! */
8035 linenr_T firstline
; /* first line of range */
8036 linenr_T lastline
; /* last line of range */
8037 int *doesrange
; /* return: function handled range */
8039 dict_T
*selfdict
; /* Dictionary for "self" */
8042 #define ERROR_UNKNOWN 0
8043 #define ERROR_TOOMANY 1
8044 #define ERROR_TOOFEW 2
8045 #define ERROR_SCRIPT 3
8046 #define ERROR_DICT 4
8047 #define ERROR_NONE 5
8048 #define ERROR_OTHER 6
8049 int error
= ERROR_NONE
;
8053 #define FLEN_FIXED 40
8054 char_u fname_buf
[FLEN_FIXED
+ 1];
8058 /* Make a copy of the name, if it comes from a funcref variable it could
8059 * be changed or deleted in the called function. */
8060 name
= vim_strnsave(func_name
, len
);
8065 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8066 * Change <SNR>123_name() to K_SNR 123_name().
8067 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8069 llen
= eval_fname_script(name
);
8072 fname_buf
[0] = K_SPECIAL
;
8073 fname_buf
[1] = KS_EXTRA
;
8074 fname_buf
[2] = (int)KE_SNR
;
8076 if (eval_fname_sid(name
)) /* "<SID>" or "s:" */
8078 if (current_SID
<= 0)
8079 error
= ERROR_SCRIPT
;
8082 sprintf((char *)fname_buf
+ 3, "%ld_", (long)current_SID
);
8083 i
= (int)STRLEN(fname_buf
);
8086 if (i
+ STRLEN(name
+ llen
) < FLEN_FIXED
)
8088 STRCPY(fname_buf
+ i
, name
+ llen
);
8093 fname
= alloc((unsigned)(i
+ STRLEN(name
+ llen
) + 1));
8095 error
= ERROR_OTHER
;
8098 mch_memmove(fname
, fname_buf
, (size_t)i
);
8099 STRCPY(fname
+ i
, name
+ llen
);
8109 /* execute the function if no errors detected and executing */
8110 if (evaluate
&& error
== ERROR_NONE
)
8112 rettv
->v_type
= VAR_NUMBER
; /* default rettv is number zero */
8113 rettv
->vval
.v_number
= 0;
8114 error
= ERROR_UNKNOWN
;
8116 if (!builtin_function(fname
))
8119 * User defined function.
8121 fp
= find_func(fname
);
8124 /* Trigger FuncUndefined event, may load the function. */
8126 && apply_autocmds(EVENT_FUNCUNDEFINED
,
8127 fname
, fname
, TRUE
, NULL
)
8130 /* executed an autocommand, search for the function again */
8131 fp
= find_func(fname
);
8134 /* Try loading a package. */
8135 if (fp
== NULL
&& script_autoload(fname
, TRUE
) && !aborting())
8137 /* loaded a package, search for the function again */
8138 fp
= find_func(fname
);
8143 if (fp
->uf_flags
& FC_RANGE
)
8145 if (argcount
< fp
->uf_args
.ga_len
)
8146 error
= ERROR_TOOFEW
;
8147 else if (!fp
->uf_varargs
&& argcount
> fp
->uf_args
.ga_len
)
8148 error
= ERROR_TOOMANY
;
8149 else if ((fp
->uf_flags
& FC_DICT
) && selfdict
== NULL
)
8154 * Call the user function.
8155 * Save and restore search patterns, script variables and
8158 save_search_patterns();
8161 call_user_func(fp
, argcount
, argvars
, rettv
,
8162 firstline
, lastline
,
8163 (fp
->uf_flags
& FC_DICT
) ? selfdict
: NULL
);
8164 if (--fp
->uf_calls
<= 0 && isdigit(*fp
->uf_name
)
8165 && fp
->uf_refcount
<= 0)
8166 /* Function was unreferenced while being used, free it
8170 restore_search_patterns();
8178 * Find the function name in the table, call its implementation.
8180 i
= find_internal_func(fname
);
8183 if (argcount
< functions
[i
].f_min_argc
)
8184 error
= ERROR_TOOFEW
;
8185 else if (argcount
> functions
[i
].f_max_argc
)
8186 error
= ERROR_TOOMANY
;
8189 argvars
[argcount
].v_type
= VAR_UNKNOWN
;
8190 functions
[i
].f_func(argvars
, rettv
);
8196 * The function call (or "FuncUndefined" autocommand sequence) might
8197 * have been aborted by an error, an interrupt, or an explicitly thrown
8198 * exception that has not been caught so far. This situation can be
8199 * tested for by calling aborting(). For an error in an internal
8200 * function or for the "E132" error in call_user_func(), however, the
8201 * throw point at which the "force_abort" flag (temporarily reset by
8202 * emsg()) is normally updated has not been reached yet. We need to
8203 * update that flag first to make aborting() reliable.
8205 update_force_abort();
8207 if (error
== ERROR_NONE
)
8211 * Report an error unless the argument evaluation or function call has been
8212 * cancelled due to an aborting error, an interrupt, or an exception.
8219 emsg_funcname(N_("E117: Unknown function: %s"), name
);
8222 emsg_funcname(e_toomanyarg
, name
);
8225 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
8229 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
8233 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
8239 if (fname
!= name
&& fname
!= fname_buf
)
8247 * Give an error message with a function name. Handle <SNR> things.
8248 * "ermsg" is to be passed without translation, use N_() instead of _().
8251 emsg_funcname(ermsg
, name
)
8257 if (*name
== K_SPECIAL
)
8258 p
= concat_str((char_u
*)"<SNR>", name
+ 3);
8267 * Return TRUE for a non-zero Number and a non-empty String.
8270 non_zero_arg(argvars
)
8273 return ((argvars
[0].v_type
== VAR_NUMBER
8274 && argvars
[0].vval
.v_number
!= 0)
8275 || (argvars
[0].v_type
== VAR_STRING
8276 && argvars
[0].vval
.v_string
!= NULL
8277 && *argvars
[0].vval
.v_string
!= NUL
));
8280 /*********************************************
8281 * Implementation of the built-in functions
8286 * "abs(expr)" function
8289 f_abs(argvars
, rettv
)
8293 if (argvars
[0].v_type
== VAR_FLOAT
)
8295 rettv
->v_type
= VAR_FLOAT
;
8296 rettv
->vval
.v_float
= fabs(argvars
[0].vval
.v_float
);
8303 n
= get_tv_number_chk(&argvars
[0], &error
);
8305 rettv
->vval
.v_number
= -1;
8307 rettv
->vval
.v_number
= n
;
8309 rettv
->vval
.v_number
= -n
;
8315 * "add(list, item)" function
8318 f_add(argvars
, rettv
)
8324 rettv
->vval
.v_number
= 1; /* Default: Failed */
8325 if (argvars
[0].v_type
== VAR_LIST
)
8327 if ((l
= argvars
[0].vval
.v_list
) != NULL
8328 && !tv_check_lock(l
->lv_lock
, (char_u
*)"add()")
8329 && list_append_tv(l
, &argvars
[1]) == OK
)
8330 copy_tv(&argvars
[0], rettv
);
8337 * "append(lnum, string/list)" function
8340 f_append(argvars
, rettv
)
8347 listitem_T
*li
= NULL
;
8351 lnum
= get_tv_lnum(argvars
);
8353 && lnum
<= curbuf
->b_ml
.ml_line_count
8354 && u_save(lnum
, lnum
+ 1) == OK
)
8356 if (argvars
[1].v_type
== VAR_LIST
)
8358 l
= argvars
[1].vval
.v_list
;
8366 tv
= &argvars
[1]; /* append a string */
8367 else if (li
== NULL
)
8368 break; /* end of list */
8370 tv
= &li
->li_tv
; /* append item from list */
8371 line
= get_tv_string_chk(tv
);
8372 if (line
== NULL
) /* type error */
8374 rettv
->vval
.v_number
= 1; /* Failed */
8377 ml_append(lnum
+ added
, line
, (colnr_T
)0, FALSE
);
8384 appended_lines_mark(lnum
, added
);
8385 if (curwin
->w_cursor
.lnum
> lnum
)
8386 curwin
->w_cursor
.lnum
+= added
;
8389 rettv
->vval
.v_number
= 1; /* Failed */
8396 f_argc(argvars
, rettv
)
8397 typval_T
*argvars UNUSED
;
8400 rettv
->vval
.v_number
= ARGCOUNT
;
8404 * "argidx()" function
8407 f_argidx(argvars
, rettv
)
8408 typval_T
*argvars UNUSED
;
8411 rettv
->vval
.v_number
= curwin
->w_arg_idx
;
8415 * "argv(nr)" function
8418 f_argv(argvars
, rettv
)
8424 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
8426 idx
= get_tv_number_chk(&argvars
[0], NULL
);
8427 if (idx
>= 0 && idx
< ARGCOUNT
)
8428 rettv
->vval
.v_string
= vim_strsave(alist_name(&ARGLIST
[idx
]));
8430 rettv
->vval
.v_string
= NULL
;
8431 rettv
->v_type
= VAR_STRING
;
8433 else if (rettv_list_alloc(rettv
) == OK
)
8434 for (idx
= 0; idx
< ARGCOUNT
; ++idx
)
8435 list_append_string(rettv
->vval
.v_list
,
8436 alist_name(&ARGLIST
[idx
]), -1);
8440 static int get_float_arg
__ARGS((typval_T
*argvars
, float_T
*f
));
8443 * Get the float value of "argvars[0]" into "f".
8444 * Returns FAIL when the argument is not a Number or Float.
8447 get_float_arg(argvars
, f
)
8451 if (argvars
[0].v_type
== VAR_FLOAT
)
8453 *f
= argvars
[0].vval
.v_float
;
8456 if (argvars
[0].v_type
== VAR_NUMBER
)
8458 *f
= (float_T
)argvars
[0].vval
.v_number
;
8461 EMSG(_("E808: Number or Float required"));
8465 /* The 10 added FP functions are defined immediately before atan() - WJMc */
8471 f_acos(argvars
, rettv
)
8477 rettv
->v_type
= VAR_FLOAT
;
8478 if (get_float_arg(argvars
, &f
) == OK
)
8479 rettv
->vval
.v_float
= acos(f
);
8481 rettv
->vval
.v_float
= 0.0;
8488 f_asin(argvars
, rettv
)
8494 rettv
->v_type
= VAR_FLOAT
;
8495 if (get_float_arg(argvars
, &f
) == OK
)
8496 rettv
->vval
.v_float
= asin(f
);
8498 rettv
->vval
.v_float
= 0.0;
8502 * "atan2()" function
8505 f_atan2(argvars
, rettv
)
8511 rettv
->v_type
= VAR_FLOAT
;
8512 if (get_float_arg(argvars
, &fx
) == OK
8513 && get_float_arg(&argvars
[1], &fy
) == OK
)
8514 rettv
->vval
.v_float
= atan2(fx
, fy
);
8516 rettv
->vval
.v_float
= 0.0;
8523 f_cosh(argvars
, rettv
)
8529 rettv
->v_type
= VAR_FLOAT
;
8530 if (get_float_arg(argvars
, &f
) == OK
)
8531 rettv
->vval
.v_float
= cosh(f
);
8533 rettv
->vval
.v_float
= 0.0;
8540 f_exp(argvars
, rettv
)
8546 rettv
->v_type
= VAR_FLOAT
;
8547 if (get_float_arg(argvars
, &f
) == OK
)
8548 rettv
->vval
.v_float
= exp(f
);
8550 rettv
->vval
.v_float
= 0.0;
8557 f_fmod(argvars
, rettv
)
8563 rettv
->v_type
= VAR_FLOAT
;
8564 if (get_float_arg(argvars
, &fx
) == OK
8565 && get_float_arg(&argvars
[1], &fy
) == OK
)
8566 rettv
->vval
.v_float
= fmod(fx
, fy
);
8568 rettv
->vval
.v_float
= 0.0;
8575 f_log(argvars
, rettv
)
8581 rettv
->v_type
= VAR_FLOAT
;
8582 if (get_float_arg(argvars
, &f
) == OK
)
8583 rettv
->vval
.v_float
= log(f
);
8585 rettv
->vval
.v_float
= 0.0;
8592 f_sinh(argvars
, rettv
)
8598 rettv
->v_type
= VAR_FLOAT
;
8599 if (get_float_arg(argvars
, &f
) == OK
)
8600 rettv
->vval
.v_float
= sinh(f
);
8602 rettv
->vval
.v_float
= 0.0;
8609 f_tan(argvars
, rettv
)
8615 rettv
->v_type
= VAR_FLOAT
;
8616 if (get_float_arg(argvars
, &f
) == OK
)
8617 rettv
->vval
.v_float
= tan(f
);
8619 rettv
->vval
.v_float
= 0.0;
8626 f_tanh(argvars
, rettv
)
8632 rettv
->v_type
= VAR_FLOAT
;
8633 if (get_float_arg(argvars
, &f
) == OK
)
8634 rettv
->vval
.v_float
= tanh(f
);
8636 rettv
->vval
.v_float
= 0.0;
8639 /* End of the 10 added FP functions - WJMc */
8645 f_atan(argvars
, rettv
)
8651 rettv
->v_type
= VAR_FLOAT
;
8652 if (get_float_arg(argvars
, &f
) == OK
)
8653 rettv
->vval
.v_float
= atan(f
);
8655 rettv
->vval
.v_float
= 0.0;
8660 * "browse(save, title, initdir, default)" function
8663 f_browse(argvars
, rettv
)
8664 typval_T
*argvars UNUSED
;
8672 char_u buf
[NUMBUFLEN
];
8673 char_u buf2
[NUMBUFLEN
];
8676 save
= get_tv_number_chk(&argvars
[0], &error
);
8677 title
= get_tv_string_chk(&argvars
[1]);
8678 initdir
= get_tv_string_buf_chk(&argvars
[2], buf
);
8679 defname
= get_tv_string_buf_chk(&argvars
[3], buf2
);
8681 if (error
|| title
== NULL
|| initdir
== NULL
|| defname
== NULL
)
8682 rettv
->vval
.v_string
= NULL
;
8684 rettv
->vval
.v_string
=
8685 do_browse(save
? BROWSE_SAVE
: 0,
8686 title
, defname
, NULL
, initdir
, NULL
, curbuf
);
8688 rettv
->vval
.v_string
= NULL
;
8690 rettv
->v_type
= VAR_STRING
;
8694 * "browsedir(title, initdir)" function
8697 f_browsedir(argvars
, rettv
)
8698 typval_T
*argvars UNUSED
;
8704 char_u buf
[NUMBUFLEN
];
8706 title
= get_tv_string_chk(&argvars
[0]);
8707 initdir
= get_tv_string_buf_chk(&argvars
[1], buf
);
8709 if (title
== NULL
|| initdir
== NULL
)
8710 rettv
->vval
.v_string
= NULL
;
8712 rettv
->vval
.v_string
= do_browse(BROWSE_DIR
,
8713 title
, NULL
, NULL
, initdir
, NULL
, curbuf
);
8715 rettv
->vval
.v_string
= NULL
;
8717 rettv
->v_type
= VAR_STRING
;
8720 static buf_T
*find_buffer
__ARGS((typval_T
*avar
));
8723 * Find a buffer by number or exact name.
8731 if (avar
->v_type
== VAR_NUMBER
)
8732 buf
= buflist_findnr((int)avar
->vval
.v_number
);
8733 else if (avar
->v_type
== VAR_STRING
&& avar
->vval
.v_string
!= NULL
)
8735 buf
= buflist_findname_exp(avar
->vval
.v_string
);
8738 /* No full path name match, try a match with a URL or a "nofile"
8739 * buffer, these don't use the full path. */
8740 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
8741 if (buf
->b_fname
!= NULL
8742 && (path_with_url(buf
->b_fname
)
8743 #ifdef FEAT_QUICKFIX
8747 && STRCMP(buf
->b_fname
, avar
->vval
.v_string
) == 0)
8755 * "bufexists(expr)" function
8758 f_bufexists(argvars
, rettv
)
8762 rettv
->vval
.v_number
= (find_buffer(&argvars
[0]) != NULL
);
8766 * "buflisted(expr)" function
8769 f_buflisted(argvars
, rettv
)
8775 buf
= find_buffer(&argvars
[0]);
8776 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_p_bl
);
8780 * "bufloaded(expr)" function
8783 f_bufloaded(argvars
, rettv
)
8789 buf
= find_buffer(&argvars
[0]);
8790 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_ml
.ml_mfp
!= NULL
);
8793 static buf_T
*get_buf_tv
__ARGS((typval_T
*tv
));
8796 * Get buffer by number or pattern.
8802 char_u
*name
= tv
->vval
.v_string
;
8807 if (tv
->v_type
== VAR_NUMBER
)
8808 return buflist_findnr((int)tv
->vval
.v_number
);
8809 if (tv
->v_type
!= VAR_STRING
)
8811 if (name
== NULL
|| *name
== NUL
)
8813 if (name
[0] == '$' && name
[1] == NUL
)
8816 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8817 save_magic
= p_magic
;
8820 p_cpo
= (char_u
*)"";
8822 buf
= buflist_findnr(buflist_findpat(name
, name
+ STRLEN(name
),
8825 p_magic
= save_magic
;
8828 /* If not found, try expanding the name, like done for bufexists(). */
8830 buf
= find_buffer(tv
);
8836 * "bufname(expr)" function
8839 f_bufname(argvars
, rettv
)
8845 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8847 buf
= get_buf_tv(&argvars
[0]);
8848 rettv
->v_type
= VAR_STRING
;
8849 if (buf
!= NULL
&& buf
->b_fname
!= NULL
)
8850 rettv
->vval
.v_string
= vim_strsave(buf
->b_fname
);
8852 rettv
->vval
.v_string
= NULL
;
8857 * "bufnr(expr)" function
8860 f_bufnr(argvars
, rettv
)
8868 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8870 buf
= get_buf_tv(&argvars
[0]);
8873 /* If the buffer isn't found and the second argument is not zero create a
8876 && argvars
[1].v_type
!= VAR_UNKNOWN
8877 && get_tv_number_chk(&argvars
[1], &error
) != 0
8879 && (name
= get_tv_string_chk(&argvars
[0])) != NULL
8881 buf
= buflist_new(name
, NULL
, (linenr_T
)1, 0);
8884 rettv
->vval
.v_number
= buf
->b_fnum
;
8886 rettv
->vval
.v_number
= -1;
8890 * "bufwinnr(nr)" function
8893 f_bufwinnr(argvars
, rettv
)
8903 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8905 buf
= get_buf_tv(&argvars
[0]);
8907 for (wp
= firstwin
; wp
; wp
= wp
->w_next
)
8910 if (wp
->w_buffer
== buf
)
8913 rettv
->vval
.v_number
= (wp
!= NULL
? winnr
: -1);
8915 rettv
->vval
.v_number
= (curwin
->w_buffer
== buf
? 1 : -1);
8921 * "byte2line(byte)" function
8924 f_byte2line(argvars
, rettv
)
8925 typval_T
*argvars UNUSED
;
8928 #ifndef FEAT_BYTEOFF
8929 rettv
->vval
.v_number
= -1;
8933 boff
= get_tv_number(&argvars
[0]) - 1; /* boff gets -1 on type error */
8935 rettv
->vval
.v_number
= -1;
8937 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
,
8938 (linenr_T
)0, &boff
);
8943 * "byteidx()" function
8946 f_byteidx(argvars
, rettv
)
8956 str
= get_tv_string_chk(&argvars
[0]);
8957 idx
= get_tv_number_chk(&argvars
[1], NULL
);
8958 rettv
->vval
.v_number
= -1;
8959 if (str
== NULL
|| idx
< 0)
8964 for ( ; idx
> 0; idx
--)
8966 if (*t
== NUL
) /* EOL reached */
8968 t
+= (*mb_ptr2len
)(t
);
8970 rettv
->vval
.v_number
= (varnumber_T
)(t
- str
);
8972 if ((size_t)idx
<= STRLEN(str
))
8973 rettv
->vval
.v_number
= idx
;
8978 * "call(func, arglist)" function
8981 f_call(argvars
, rettv
)
8986 typval_T argv
[MAX_FUNC_ARGS
+ 1];
8990 dict_T
*selfdict
= NULL
;
8992 if (argvars
[1].v_type
!= VAR_LIST
)
8997 if (argvars
[1].vval
.v_list
== NULL
)
9000 if (argvars
[0].v_type
== VAR_FUNC
)
9001 func
= argvars
[0].vval
.v_string
;
9003 func
= get_tv_string(&argvars
[0]);
9005 return; /* type error or empty name */
9007 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9009 if (argvars
[2].v_type
!= VAR_DICT
)
9014 selfdict
= argvars
[2].vval
.v_dict
;
9017 for (item
= argvars
[1].vval
.v_list
->lv_first
; item
!= NULL
;
9018 item
= item
->li_next
)
9020 if (argc
== MAX_FUNC_ARGS
)
9022 EMSG(_("E699: Too many arguments"));
9025 /* Make a copy of each argument. This is needed to be able to set
9026 * v_lock to VAR_FIXED in the copy without changing the original list.
9028 copy_tv(&item
->li_tv
, &argv
[argc
++]);
9032 (void)call_func(func
, (int)STRLEN(func
), rettv
, argc
, argv
,
9033 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
9034 &dummy
, TRUE
, selfdict
);
9036 /* Free the arguments. */
9038 clear_tv(&argv
[--argc
]);
9043 * "ceil({float})" function
9046 f_ceil(argvars
, rettv
)
9052 rettv
->v_type
= VAR_FLOAT
;
9053 if (get_float_arg(argvars
, &f
) == OK
)
9054 rettv
->vval
.v_float
= ceil(f
);
9056 rettv
->vval
.v_float
= 0.0;
9061 * "changenr()" function
9064 f_changenr(argvars
, rettv
)
9065 typval_T
*argvars UNUSED
;
9068 rettv
->vval
.v_number
= curbuf
->b_u_seq_cur
;
9072 * "char2nr(string)" function
9075 f_char2nr(argvars
, rettv
)
9081 rettv
->vval
.v_number
= (*mb_ptr2char
)(get_tv_string(&argvars
[0]));
9084 rettv
->vval
.v_number
= get_tv_string(&argvars
[0])[0];
9088 * "cindent(lnum)" function
9091 f_cindent(argvars
, rettv
)
9099 pos
= curwin
->w_cursor
;
9100 lnum
= get_tv_lnum(argvars
);
9101 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
9103 curwin
->w_cursor
.lnum
= lnum
;
9104 rettv
->vval
.v_number
= get_c_indent();
9105 curwin
->w_cursor
= pos
;
9109 rettv
->vval
.v_number
= -1;
9113 * "clearmatches()" function
9116 f_clearmatches(argvars
, rettv
)
9117 typval_T
*argvars UNUSED
;
9118 typval_T
*rettv UNUSED
;
9120 #ifdef FEAT_SEARCH_EXTRA
9121 clear_matches(curwin
);
9126 * "col(string)" function
9129 f_col(argvars
, rettv
)
9135 int fnum
= curbuf
->b_fnum
;
9137 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
9138 if (fp
!= NULL
&& fnum
== curbuf
->b_fnum
)
9140 if (fp
->col
== MAXCOL
)
9142 /* '> can be MAXCOL, get the length of the line then */
9143 if (fp
->lnum
<= curbuf
->b_ml
.ml_line_count
)
9144 col
= (colnr_T
)STRLEN(ml_get(fp
->lnum
)) + 1;
9151 #ifdef FEAT_VIRTUALEDIT
9152 /* col(".") when the cursor is on the NUL at the end of the line
9153 * because of "coladd" can be seen as an extra column. */
9154 if (virtual_active() && fp
== &curwin
->w_cursor
)
9156 char_u
*p
= ml_get_cursor();
9158 if (curwin
->w_cursor
.coladd
>= (colnr_T
)chartabsize(p
,
9159 curwin
->w_virtcol
- curwin
->w_cursor
.coladd
))
9164 if (*p
!= NUL
&& p
[(l
= (*mb_ptr2len
)(p
))] == NUL
)
9167 if (*p
!= NUL
&& p
[1] == NUL
)
9175 rettv
->vval
.v_number
= col
;
9178 #if defined(FEAT_INS_EXPAND)
9180 * "complete()" function
9183 f_complete(argvars
, rettv
)
9185 typval_T
*rettv UNUSED
;
9189 if ((State
& INSERT
) == 0)
9191 EMSG(_("E785: complete() can only be used in Insert mode"));
9195 /* Check for undo allowed here, because if something was already inserted
9196 * the line was already saved for undo and this check isn't done. */
9197 if (!undo_allowed())
9200 if (argvars
[1].v_type
!= VAR_LIST
|| argvars
[1].vval
.v_list
== NULL
)
9206 startcol
= get_tv_number_chk(&argvars
[0], NULL
);
9210 set_completion(startcol
- 1, argvars
[1].vval
.v_list
);
9214 * "complete_add()" function
9217 f_complete_add(argvars
, rettv
)
9221 rettv
->vval
.v_number
= ins_compl_add_tv(&argvars
[0], 0);
9225 * "complete_check()" function
9228 f_complete_check(argvars
, rettv
)
9229 typval_T
*argvars UNUSED
;
9232 int saved
= RedrawingDisabled
;
9234 RedrawingDisabled
= 0;
9235 ins_compl_check_keys(0);
9236 rettv
->vval
.v_number
= compl_interrupted
;
9237 RedrawingDisabled
= saved
;
9242 * "confirm(message, buttons[, default [, type]])" function
9245 f_confirm(argvars
, rettv
)
9246 typval_T
*argvars UNUSED
;
9247 typval_T
*rettv UNUSED
;
9249 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9251 char_u
*buttons
= NULL
;
9252 char_u buf
[NUMBUFLEN
];
9253 char_u buf2
[NUMBUFLEN
];
9255 int type
= VIM_GENERIC
;
9259 message
= get_tv_string_chk(&argvars
[0]);
9260 if (message
== NULL
)
9262 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9264 buttons
= get_tv_string_buf_chk(&argvars
[1], buf
);
9265 if (buttons
== NULL
)
9267 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9269 def
= get_tv_number_chk(&argvars
[2], &error
);
9270 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9272 typestr
= get_tv_string_buf_chk(&argvars
[3], buf2
);
9273 if (typestr
== NULL
)
9277 switch (TOUPPER_ASC(*typestr
))
9279 case 'E': type
= VIM_ERROR
; break;
9280 case 'Q': type
= VIM_QUESTION
; break;
9281 case 'I': type
= VIM_INFO
; break;
9282 case 'W': type
= VIM_WARNING
; break;
9283 case 'G': type
= VIM_GENERIC
; break;
9290 if (buttons
== NULL
|| *buttons
== NUL
)
9291 buttons
= (char_u
*)_("&Ok");
9294 rettv
->vval
.v_number
= do_dialog(type
, NULL
, message
, buttons
,
9303 f_copy(argvars
, rettv
)
9307 item_copy(&argvars
[0], rettv
, FALSE
, 0);
9315 f_cos(argvars
, rettv
)
9321 rettv
->v_type
= VAR_FLOAT
;
9322 if (get_float_arg(argvars
, &f
) == OK
)
9323 rettv
->vval
.v_float
= cos(f
);
9325 rettv
->vval
.v_float
= 0.0;
9330 * "count()" function
9333 f_count(argvars
, rettv
)
9340 if (argvars
[0].v_type
== VAR_LIST
)
9346 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
9349 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9353 ic
= get_tv_number_chk(&argvars
[2], &error
);
9354 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9356 idx
= get_tv_number_chk(&argvars
[3], &error
);
9359 li
= list_find(l
, idx
);
9361 EMSGN(_(e_listidx
), idx
);
9368 for ( ; li
!= NULL
; li
= li
->li_next
)
9369 if (tv_equal(&li
->li_tv
, &argvars
[1], ic
))
9373 else if (argvars
[0].v_type
== VAR_DICT
)
9379 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
9383 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9385 ic
= get_tv_number_chk(&argvars
[2], &error
);
9386 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9390 todo
= error
? 0 : (int)d
->dv_hashtab
.ht_used
;
9391 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
9393 if (!HASHITEM_EMPTY(hi
))
9396 if (tv_equal(&HI2DI(hi
)->di_tv
, &argvars
[1], ic
))
9403 EMSG2(_(e_listdictarg
), "count()");
9404 rettv
->vval
.v_number
= n
;
9408 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9410 * Checks the existence of a cscope connection.
9413 f_cscope_connection(argvars
, rettv
)
9414 typval_T
*argvars UNUSED
;
9415 typval_T
*rettv UNUSED
;
9419 char_u
*dbpath
= NULL
;
9420 char_u
*prepend
= NULL
;
9421 char_u buf
[NUMBUFLEN
];
9423 if (argvars
[0].v_type
!= VAR_UNKNOWN
9424 && argvars
[1].v_type
!= VAR_UNKNOWN
)
9426 num
= (int)get_tv_number(&argvars
[0]);
9427 dbpath
= get_tv_string(&argvars
[1]);
9428 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9429 prepend
= get_tv_string_buf(&argvars
[2], buf
);
9432 rettv
->vval
.v_number
= cs_connection(num
, dbpath
, prepend
);
9437 * "cursor(lnum, col)" function
9439 * Moves the cursor to the specified line and column.
9440 * Returns 0 when the position could be set, -1 otherwise.
9443 f_cursor(argvars
, rettv
)
9448 #ifdef FEAT_VIRTUALEDIT
9452 rettv
->vval
.v_number
= -1;
9453 if (argvars
[1].v_type
== VAR_UNKNOWN
)
9457 if (list2fpos(argvars
, &pos
, NULL
) == FAIL
)
9461 #ifdef FEAT_VIRTUALEDIT
9462 coladd
= pos
.coladd
;
9467 line
= get_tv_lnum(argvars
);
9468 col
= get_tv_number_chk(&argvars
[1], NULL
);
9469 #ifdef FEAT_VIRTUALEDIT
9470 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9471 coladd
= get_tv_number_chk(&argvars
[2], NULL
);
9474 if (line
< 0 || col
< 0
9475 #ifdef FEAT_VIRTUALEDIT
9479 return; /* type error; errmsg already given */
9481 curwin
->w_cursor
.lnum
= line
;
9483 curwin
->w_cursor
.col
= col
- 1;
9484 #ifdef FEAT_VIRTUALEDIT
9485 curwin
->w_cursor
.coladd
= coladd
;
9488 /* Make sure the cursor is in a valid position. */
9491 /* Correct cursor for multi-byte character. */
9496 curwin
->w_set_curswant
= TRUE
;
9497 rettv
->vval
.v_number
= 0;
9501 * "deepcopy()" function
9504 f_deepcopy(argvars
, rettv
)
9510 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9511 noref
= get_tv_number_chk(&argvars
[1], NULL
);
9512 if (noref
< 0 || noref
> 1)
9516 current_copyID
+= COPYID_INC
;
9517 item_copy(&argvars
[0], rettv
, TRUE
, noref
== 0 ? current_copyID
: 0);
9522 * "delete()" function
9525 f_delete(argvars
, rettv
)
9529 if (check_restricted() || check_secure())
9530 rettv
->vval
.v_number
= -1;
9532 rettv
->vval
.v_number
= mch_remove(get_tv_string(&argvars
[0]));
9536 * "did_filetype()" function
9539 f_did_filetype(argvars
, rettv
)
9540 typval_T
*argvars UNUSED
;
9541 typval_T
*rettv UNUSED
;
9544 rettv
->vval
.v_number
= did_filetype
;
9549 * "diff_filler()" function
9552 f_diff_filler(argvars
, rettv
)
9553 typval_T
*argvars UNUSED
;
9554 typval_T
*rettv UNUSED
;
9557 rettv
->vval
.v_number
= diff_check_fill(curwin
, get_tv_lnum(argvars
));
9562 * "diff_hlID()" function
9565 f_diff_hlID(argvars
, rettv
)
9566 typval_T
*argvars UNUSED
;
9567 typval_T
*rettv UNUSED
;
9570 linenr_T lnum
= get_tv_lnum(argvars
);
9571 static linenr_T prev_lnum
= 0;
9572 static int changedtick
= 0;
9573 static int fnum
= 0;
9574 static int change_start
= 0;
9575 static int change_end
= 0;
9576 static hlf_T hlID
= (hlf_T
)0;
9580 if (lnum
< 0) /* ignore type error in {lnum} arg */
9582 if (lnum
!= prev_lnum
9583 || changedtick
!= curbuf
->b_changedtick
9584 || fnum
!= curbuf
->b_fnum
)
9586 /* New line, buffer, change: need to get the values. */
9587 filler_lines
= diff_check(curwin
, lnum
);
9588 if (filler_lines
< 0)
9590 if (filler_lines
== -1)
9592 change_start
= MAXCOL
;
9594 if (diff_find_change(curwin
, lnum
, &change_start
, &change_end
))
9595 hlID
= HLF_ADD
; /* added line */
9597 hlID
= HLF_CHD
; /* changed line */
9600 hlID
= HLF_ADD
; /* added line */
9605 changedtick
= curbuf
->b_changedtick
;
9606 fnum
= curbuf
->b_fnum
;
9609 if (hlID
== HLF_CHD
|| hlID
== HLF_TXD
)
9611 col
= get_tv_number(&argvars
[1]) - 1; /* ignore type error in {col} */
9612 if (col
>= change_start
&& col
<= change_end
)
9613 hlID
= HLF_TXD
; /* changed text */
9615 hlID
= HLF_CHD
; /* changed line */
9617 rettv
->vval
.v_number
= hlID
== (hlf_T
)0 ? 0 : (int)hlID
;
9622 * "empty({expr})" function
9625 f_empty(argvars
, rettv
)
9631 switch (argvars
[0].v_type
)
9635 n
= argvars
[0].vval
.v_string
== NULL
9636 || *argvars
[0].vval
.v_string
== NUL
;
9639 n
= argvars
[0].vval
.v_number
== 0;
9643 n
= argvars
[0].vval
.v_float
== 0.0;
9647 n
= argvars
[0].vval
.v_list
== NULL
9648 || argvars
[0].vval
.v_list
->lv_first
== NULL
;
9651 n
= argvars
[0].vval
.v_dict
== NULL
9652 || argvars
[0].vval
.v_dict
->dv_hashtab
.ht_used
== 0;
9655 EMSG2(_(e_intern2
), "f_empty()");
9659 rettv
->vval
.v_number
= n
;
9663 * "escape({string}, {chars})" function
9666 f_escape(argvars
, rettv
)
9670 char_u buf
[NUMBUFLEN
];
9672 rettv
->vval
.v_string
= vim_strsave_escaped(get_tv_string(&argvars
[0]),
9673 get_tv_string_buf(&argvars
[1], buf
));
9674 rettv
->v_type
= VAR_STRING
;
9681 f_eval(argvars
, rettv
)
9687 s
= get_tv_string_chk(&argvars
[0]);
9691 if (s
== NULL
|| eval1(&s
, rettv
, TRUE
) == FAIL
)
9693 rettv
->v_type
= VAR_NUMBER
;
9694 rettv
->vval
.v_number
= 0;
9697 EMSG(_(e_trailing
));
9701 * "eventhandler()" function
9704 f_eventhandler(argvars
, rettv
)
9705 typval_T
*argvars UNUSED
;
9708 rettv
->vval
.v_number
= vgetc_busy
;
9712 * "executable()" function
9715 f_executable(argvars
, rettv
)
9719 rettv
->vval
.v_number
= mch_can_exe(get_tv_string(&argvars
[0]));
9723 * "exists()" function
9726 f_exists(argvars
, rettv
)
9735 p
= get_tv_string(&argvars
[0]);
9736 if (*p
== '$') /* environment variable */
9738 /* first try "normal" environment variables (fast) */
9739 if (mch_getenv(p
+ 1) != NULL
)
9743 /* try expanding things like $VIM and ${HOME} */
9744 p
= expand_env_save(p
);
9745 if (p
!= NULL
&& *p
!= '$')
9750 else if (*p
== '&' || *p
== '+') /* option */
9752 n
= (get_option_tv(&p
, NULL
, TRUE
) == OK
);
9753 if (*skipwhite(p
) != NUL
)
9754 n
= FALSE
; /* trailing garbage */
9756 else if (*p
== '*') /* internal or user defined function */
9758 n
= function_exists(p
+ 1);
9762 n
= cmd_exists(p
+ 1);
9768 n
= autocmd_supported(p
+ 2);
9770 n
= au_exists(p
+ 1);
9773 else /* internal variable */
9778 /* get_name_len() takes care of expanding curly braces */
9780 len
= get_name_len(&p
, &tofree
, TRUE
, FALSE
);
9785 n
= (get_var_tv(name
, len
, &tv
, FALSE
) == OK
);
9788 /* handle d.key, l[idx], f(expr) */
9789 n
= (handle_subscript(&p
, &tv
, TRUE
, FALSE
) == OK
);
9800 rettv
->vval
.v_number
= n
;
9804 * "expand()" function
9807 f_expand(argvars
, rettv
)
9814 int flags
= WILD_SILENT
|WILD_USE_NL
|WILD_LIST_NOTFOUND
;
9818 rettv
->v_type
= VAR_STRING
;
9819 s
= get_tv_string(&argvars
[0]);
9820 if (*s
== '%' || *s
== '#' || *s
== '<')
9823 rettv
->vval
.v_string
= eval_vars(s
, s
, &len
, NULL
, &errormsg
, NULL
);
9828 /* When the optional second argument is non-zero, don't remove matches
9829 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
9830 if (argvars
[1].v_type
!= VAR_UNKNOWN
9831 && get_tv_number_chk(&argvars
[1], &error
))
9832 flags
|= WILD_KEEP_ALL
;
9836 xpc
.xp_context
= EXPAND_FILES
;
9837 rettv
->vval
.v_string
= ExpandOne(&xpc
, s
, NULL
, flags
, WILD_ALL
);
9840 rettv
->vval
.v_string
= NULL
;
9845 * "extend(list, list [, idx])" function
9846 * "extend(dict, dict [, action])" function
9849 f_extend(argvars
, rettv
)
9853 if (argvars
[0].v_type
== VAR_LIST
&& argvars
[1].v_type
== VAR_LIST
)
9860 l1
= argvars
[0].vval
.v_list
;
9861 l2
= argvars
[1].vval
.v_list
;
9862 if (l1
!= NULL
&& !tv_check_lock(l1
->lv_lock
, (char_u
*)"extend()")
9865 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9867 before
= get_tv_number_chk(&argvars
[2], &error
);
9869 return; /* type error; errmsg already given */
9871 if (before
== l1
->lv_len
)
9875 item
= list_find(l1
, before
);
9878 EMSGN(_(e_listidx
), before
);
9885 list_extend(l1
, l2
, item
);
9887 copy_tv(&argvars
[0], rettv
);
9890 else if (argvars
[0].v_type
== VAR_DICT
&& argvars
[1].v_type
== VAR_DICT
)
9899 d1
= argvars
[0].vval
.v_dict
;
9900 d2
= argvars
[1].vval
.v_dict
;
9901 if (d1
!= NULL
&& !tv_check_lock(d1
->dv_lock
, (char_u
*)"extend()")
9904 /* Check the third argument. */
9905 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9907 static char *(av
[]) = {"keep", "force", "error"};
9909 action
= get_tv_string_chk(&argvars
[2]);
9911 return; /* type error; errmsg already given */
9912 for (i
= 0; i
< 3; ++i
)
9913 if (STRCMP(action
, av
[i
]) == 0)
9917 EMSG2(_(e_invarg2
), action
);
9922 action
= (char_u
*)"force";
9924 /* Go over all entries in the second dict and add them to the
9926 todo
= (int)d2
->dv_hashtab
.ht_used
;
9927 for (hi2
= d2
->dv_hashtab
.ht_array
; todo
> 0; ++hi2
)
9929 if (!HASHITEM_EMPTY(hi2
))
9932 di1
= dict_find(d1
, hi2
->hi_key
, -1);
9935 di1
= dictitem_copy(HI2DI(hi2
));
9936 if (di1
!= NULL
&& dict_add(d1
, di1
) == FAIL
)
9939 else if (*action
== 'e')
9941 EMSG2(_("E737: Key already exists: %s"), hi2
->hi_key
);
9944 else if (*action
== 'f')
9946 clear_tv(&di1
->di_tv
);
9947 copy_tv(&HI2DI(hi2
)->di_tv
, &di1
->di_tv
);
9952 copy_tv(&argvars
[0], rettv
);
9956 EMSG2(_(e_listdictarg
), "extend()");
9960 * "feedkeys()" function
9963 f_feedkeys(argvars
, rettv
)
9965 typval_T
*rettv UNUSED
;
9968 char_u
*keys
, *flags
;
9969 char_u nbuf
[NUMBUFLEN
];
9973 /* This is not allowed in the sandbox. If the commands would still be
9974 * executed in the sandbox it would be OK, but it probably happens later,
9975 * when "sandbox" is no longer set. */
9979 keys
= get_tv_string(&argvars
[0]);
9982 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9984 flags
= get_tv_string_buf(&argvars
[1], nbuf
);
9985 for ( ; *flags
!= NUL
; ++flags
)
9989 case 'n': remap
= FALSE
; break;
9990 case 'm': remap
= TRUE
; break;
9991 case 't': typed
= TRUE
; break;
9996 /* Need to escape K_SPECIAL and CSI before putting the string in the
9997 * typeahead buffer. */
9998 keys_esc
= vim_strsave_escape_csi(keys
);
9999 if (keys_esc
!= NULL
)
10001 ins_typebuf(keys_esc
, (remap
? REMAP_YES
: REMAP_NONE
),
10002 typebuf
.tb_len
, !typed
, FALSE
);
10003 vim_free(keys_esc
);
10005 typebuf_was_filled
= TRUE
;
10011 * "filereadable()" function
10014 f_filereadable(argvars
, rettv
)
10023 # define O_NONBLOCK 0
10025 p
= get_tv_string(&argvars
[0]);
10026 if (*p
&& !mch_isdir(p
) && (fd
= mch_open((char *)p
,
10027 O_RDONLY
| O_NONBLOCK
, 0)) >= 0)
10035 rettv
->vval
.v_number
= n
;
10039 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
10040 * rights to write into.
10043 f_filewritable(argvars
, rettv
)
10047 rettv
->vval
.v_number
= filewritable(get_tv_string(&argvars
[0]));
10050 static void findfilendir
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int find_what
));
10053 findfilendir(argvars
, rettv
, find_what
)
10058 #ifdef FEAT_SEARCHPATH
10060 char_u
*fresult
= NULL
;
10061 char_u
*path
= *curbuf
->b_p_path
== NUL
? p_path
: curbuf
->b_p_path
;
10063 char_u pathbuf
[NUMBUFLEN
];
10069 rettv
->vval
.v_string
= NULL
;
10070 rettv
->v_type
= VAR_STRING
;
10072 #ifdef FEAT_SEARCHPATH
10073 fname
= get_tv_string(&argvars
[0]);
10075 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
10077 p
= get_tv_string_buf_chk(&argvars
[1], pathbuf
);
10085 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
10086 count
= get_tv_number_chk(&argvars
[2], &error
);
10090 if (count
< 0 && rettv_list_alloc(rettv
) == FAIL
)
10093 if (*fname
!= NUL
&& !error
)
10097 if (rettv
->v_type
== VAR_STRING
)
10099 fresult
= find_file_in_path_option(first
? fname
: NULL
,
10100 first
? (int)STRLEN(fname
) : 0,
10104 find_what
== FINDFILE_DIR
10105 ? (char_u
*)"" : curbuf
->b_p_sua
);
10108 if (fresult
!= NULL
&& rettv
->v_type
== VAR_LIST
)
10109 list_append_string(rettv
->vval
.v_list
, fresult
, -1);
10111 } while ((rettv
->v_type
== VAR_LIST
|| --count
> 0) && fresult
!= NULL
);
10114 if (rettv
->v_type
== VAR_STRING
)
10115 rettv
->vval
.v_string
= fresult
;
10119 static void filter_map
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int map
));
10120 static int filter_map_one
__ARGS((typval_T
*tv
, char_u
*expr
, int map
, int *remp
));
10123 * Implementation of map() and filter().
10126 filter_map(argvars
, rettv
, map
)
10131 char_u buf
[NUMBUFLEN
];
10133 listitem_T
*li
, *nli
;
10143 char_u
*ermsg
= map
? (char_u
*)"map()" : (char_u
*)"filter()";
10147 if (argvars
[0].v_type
== VAR_LIST
)
10149 if ((l
= argvars
[0].vval
.v_list
) == NULL
10150 || (map
&& tv_check_lock(l
->lv_lock
, ermsg
)))
10153 else if (argvars
[0].v_type
== VAR_DICT
)
10155 if ((d
= argvars
[0].vval
.v_dict
) == NULL
10156 || (map
&& tv_check_lock(d
->dv_lock
, ermsg
)))
10161 EMSG2(_(e_listdictarg
), ermsg
);
10165 expr
= get_tv_string_buf_chk(&argvars
[1], buf
);
10166 /* On type errors, the preceding call has already displayed an error
10167 * message. Avoid a misleading error message for an empty string that
10168 * was not passed as argument. */
10171 prepare_vimvar(VV_VAL
, &save_val
);
10172 expr
= skipwhite(expr
);
10174 /* We reset "did_emsg" to be able to detect whether an error
10175 * occurred during evaluation of the expression. */
10176 save_did_emsg
= did_emsg
;
10179 prepare_vimvar(VV_KEY
, &save_key
);
10180 if (argvars
[0].v_type
== VAR_DICT
)
10182 vimvars
[VV_KEY
].vv_type
= VAR_STRING
;
10184 ht
= &d
->dv_hashtab
;
10186 todo
= (int)ht
->ht_used
;
10187 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
10189 if (!HASHITEM_EMPTY(hi
))
10193 if (tv_check_lock(di
->di_tv
.v_lock
, ermsg
))
10195 vimvars
[VV_KEY
].vv_str
= vim_strsave(di
->di_key
);
10196 if (filter_map_one(&di
->di_tv
, expr
, map
, &rem
) == FAIL
10200 dictitem_remove(d
, di
);
10201 clear_tv(&vimvars
[VV_KEY
].vv_tv
);
10208 vimvars
[VV_KEY
].vv_type
= VAR_NUMBER
;
10210 for (li
= l
->lv_first
; li
!= NULL
; li
= nli
)
10212 if (tv_check_lock(li
->li_tv
.v_lock
, ermsg
))
10215 vimvars
[VV_KEY
].vv_nr
= index
;
10216 if (filter_map_one(&li
->li_tv
, expr
, map
, &rem
) == FAIL
10220 listitem_remove(l
, li
);
10225 restore_vimvar(VV_KEY
, &save_key
);
10226 restore_vimvar(VV_VAL
, &save_val
);
10228 did_emsg
|= save_did_emsg
;
10231 copy_tv(&argvars
[0], rettv
);
10235 filter_map_one(tv
, expr
, map
, remp
)
10245 copy_tv(tv
, &vimvars
[VV_VAL
].vv_tv
);
10247 if (eval1(&s
, &rettv
, TRUE
) == FAIL
)
10249 if (*s
!= NUL
) /* check for trailing chars after expr */
10251 EMSG2(_(e_invexpr2
), s
);
10256 /* map(): replace the list item value */
10265 /* filter(): when expr is zero remove the item */
10266 *remp
= (get_tv_number_chk(&rettv
, &error
) == 0);
10268 /* On type error, nothing has been removed; return FAIL to stop the
10269 * loop. The error message was given by get_tv_number_chk(). */
10275 clear_tv(&vimvars
[VV_VAL
].vv_tv
);
10280 * "filter()" function
10283 f_filter(argvars
, rettv
)
10287 filter_map(argvars
, rettv
, FALSE
);
10291 * "finddir({fname}[, {path}[, {count}]])" function
10294 f_finddir(argvars
, rettv
)
10298 findfilendir(argvars
, rettv
, FINDFILE_DIR
);
10302 * "findfile({fname}[, {path}[, {count}]])" function
10305 f_findfile(argvars
, rettv
)
10309 findfilendir(argvars
, rettv
, FINDFILE_FILE
);
10314 * "float2nr({float})" function
10317 f_float2nr(argvars
, rettv
)
10323 if (get_float_arg(argvars
, &f
) == OK
)
10325 if (f
< -0x7fffffff)
10326 rettv
->vval
.v_number
= -0x7fffffff;
10327 else if (f
> 0x7fffffff)
10328 rettv
->vval
.v_number
= 0x7fffffff;
10330 rettv
->vval
.v_number
= (varnumber_T
)f
;
10335 * "floor({float})" function
10338 f_floor(argvars
, rettv
)
10344 rettv
->v_type
= VAR_FLOAT
;
10345 if (get_float_arg(argvars
, &f
) == OK
)
10346 rettv
->vval
.v_float
= floor(f
);
10348 rettv
->vval
.v_float
= 0.0;
10353 * "fnameescape({string})" function
10356 f_fnameescape(argvars
, rettv
)
10360 rettv
->vval
.v_string
= vim_strsave_fnameescape(
10361 get_tv_string(&argvars
[0]), FALSE
);
10362 rettv
->v_type
= VAR_STRING
;
10366 * "fnamemodify({fname}, {mods})" function
10369 f_fnamemodify(argvars
, rettv
)
10377 char_u
*fbuf
= NULL
;
10378 char_u buf
[NUMBUFLEN
];
10380 fname
= get_tv_string_chk(&argvars
[0]);
10381 mods
= get_tv_string_buf_chk(&argvars
[1], buf
);
10382 if (fname
== NULL
|| mods
== NULL
)
10386 len
= (int)STRLEN(fname
);
10387 (void)modify_fname(mods
, &usedlen
, &fname
, &fbuf
, &len
);
10390 rettv
->v_type
= VAR_STRING
;
10392 rettv
->vval
.v_string
= NULL
;
10394 rettv
->vval
.v_string
= vim_strnsave(fname
, len
);
10398 static void foldclosed_both
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int end
));
10401 * "foldclosed()" function
10404 foldclosed_both(argvars
, rettv
, end
)
10409 #ifdef FEAT_FOLDING
10411 linenr_T first
, last
;
10413 lnum
= get_tv_lnum(argvars
);
10414 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
10416 if (hasFoldingWin(curwin
, lnum
, &first
, &last
, FALSE
, NULL
))
10419 rettv
->vval
.v_number
= (varnumber_T
)last
;
10421 rettv
->vval
.v_number
= (varnumber_T
)first
;
10426 rettv
->vval
.v_number
= -1;
10430 * "foldclosed()" function
10433 f_foldclosed(argvars
, rettv
)
10437 foldclosed_both(argvars
, rettv
, FALSE
);
10441 * "foldclosedend()" function
10444 f_foldclosedend(argvars
, rettv
)
10448 foldclosed_both(argvars
, rettv
, TRUE
);
10452 * "foldlevel()" function
10455 f_foldlevel(argvars
, rettv
)
10459 #ifdef FEAT_FOLDING
10462 lnum
= get_tv_lnum(argvars
);
10463 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
10464 rettv
->vval
.v_number
= foldLevel(lnum
);
10469 * "foldtext()" function
10472 f_foldtext(argvars
, rettv
)
10473 typval_T
*argvars UNUSED
;
10476 #ifdef FEAT_FOLDING
10484 rettv
->v_type
= VAR_STRING
;
10485 rettv
->vval
.v_string
= NULL
;
10486 #ifdef FEAT_FOLDING
10487 if ((linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
> 0
10488 && (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
10489 <= curbuf
->b_ml
.ml_line_count
10490 && vimvars
[VV_FOLDDASHES
].vv_str
!= NULL
)
10492 /* Find first non-empty line in the fold. */
10493 lnum
= (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
;
10494 while (lnum
< (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
10496 if (!linewhite(lnum
))
10501 /* Find interesting text in this line. */
10502 s
= skipwhite(ml_get(lnum
));
10503 /* skip C comment-start */
10504 if (s
[0] == '/' && (s
[1] == '*' || s
[1] == '/'))
10506 s
= skipwhite(s
+ 2);
10507 if (*skipwhite(s
) == NUL
10508 && lnum
+ 1 < (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
10510 s
= skipwhite(ml_get(lnum
+ 1));
10512 s
= skipwhite(s
+ 1);
10515 txt
= _("+-%s%3ld lines: ");
10516 r
= alloc((unsigned)(STRLEN(txt
)
10517 + STRLEN(vimvars
[VV_FOLDDASHES
].vv_str
) /* for %s */
10518 + 20 /* for %3ld */
10519 + STRLEN(s
))); /* concatenated */
10522 sprintf((char *)r
, txt
, vimvars
[VV_FOLDDASHES
].vv_str
,
10523 (long)((linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
10524 - (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
+ 1));
10525 len
= (int)STRLEN(r
);
10527 /* remove 'foldmarker' and 'commentstring' */
10528 foldtext_cleanup(r
+ len
);
10529 rettv
->vval
.v_string
= r
;
10536 * "foldtextresult(lnum)" function
10539 f_foldtextresult(argvars
, rettv
)
10540 typval_T
*argvars UNUSED
;
10543 #ifdef FEAT_FOLDING
10547 foldinfo_T foldinfo
;
10551 rettv
->v_type
= VAR_STRING
;
10552 rettv
->vval
.v_string
= NULL
;
10553 #ifdef FEAT_FOLDING
10554 lnum
= get_tv_lnum(argvars
);
10555 /* treat illegal types and illegal string values for {lnum} the same */
10558 fold_count
= foldedCount(curwin
, lnum
, &foldinfo
);
10559 if (fold_count
> 0)
10561 text
= get_foldtext(curwin
, lnum
, lnum
+ fold_count
- 1,
10564 text
= vim_strsave(text
);
10565 rettv
->vval
.v_string
= text
;
10571 * "foreground()" function
10574 f_foreground(argvars
, rettv
)
10575 typval_T
*argvars UNUSED
;
10576 typval_T
*rettv UNUSED
;
10580 gui_mch_set_foreground();
10583 win32_set_foreground();
10589 * "function()" function
10592 f_function(argvars
, rettv
)
10598 s
= get_tv_string(&argvars
[0]);
10599 if (s
== NULL
|| *s
== NUL
|| VIM_ISDIGIT(*s
))
10600 EMSG2(_(e_invarg2
), s
);
10601 /* Don't check an autoload name for existence here. */
10602 else if (vim_strchr(s
, AUTOLOAD_CHAR
) == NULL
&& !function_exists(s
))
10603 EMSG2(_("E700: Unknown function: %s"), s
);
10606 rettv
->vval
.v_string
= vim_strsave(s
);
10607 rettv
->v_type
= VAR_FUNC
;
10612 * "garbagecollect()" function
10615 f_garbagecollect(argvars
, rettv
)
10617 typval_T
*rettv UNUSED
;
10619 /* This is postponed until we are back at the toplevel, because we may be
10620 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10621 want_garbage_collect
= TRUE
;
10623 if (argvars
[0].v_type
!= VAR_UNKNOWN
&& get_tv_number(&argvars
[0]) == 1)
10624 garbage_collect_at_exit
= TRUE
;
10631 f_get(argvars
, rettv
)
10639 typval_T
*tv
= NULL
;
10641 if (argvars
[0].v_type
== VAR_LIST
)
10643 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
10647 li
= list_find(l
, get_tv_number_chk(&argvars
[1], &error
));
10648 if (!error
&& li
!= NULL
)
10652 else if (argvars
[0].v_type
== VAR_DICT
)
10654 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
10656 di
= dict_find(d
, get_tv_string(&argvars
[1]), -1);
10662 EMSG2(_(e_listdictarg
), "get()");
10666 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
10667 copy_tv(&argvars
[2], rettv
);
10670 copy_tv(tv
, rettv
);
10673 static void get_buffer_lines
__ARGS((buf_T
*buf
, linenr_T start
, linenr_T end
, int retlist
, typval_T
*rettv
));
10676 * Get line or list of lines from buffer "buf" into "rettv".
10677 * Return a range (from start to end) of lines in rettv from the specified
10679 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
10682 get_buffer_lines(buf
, start
, end
, retlist
, rettv
)
10691 if (retlist
&& rettv_list_alloc(rettv
) == FAIL
)
10694 if (buf
== NULL
|| buf
->b_ml
.ml_mfp
== NULL
|| start
< 0)
10699 if (start
>= 1 && start
<= buf
->b_ml
.ml_line_count
)
10700 p
= ml_get_buf(buf
, start
, FALSE
);
10704 rettv
->v_type
= VAR_STRING
;
10705 rettv
->vval
.v_string
= vim_strsave(p
);
10714 if (end
> buf
->b_ml
.ml_line_count
)
10715 end
= buf
->b_ml
.ml_line_count
;
10716 while (start
<= end
)
10717 if (list_append_string(rettv
->vval
.v_list
,
10718 ml_get_buf(buf
, start
++, FALSE
), -1) == FAIL
)
10724 * "getbufline()" function
10727 f_getbufline(argvars
, rettv
)
10735 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
10737 buf
= get_buf_tv(&argvars
[0]);
10740 lnum
= get_tv_lnum_buf(&argvars
[1], buf
);
10741 if (argvars
[2].v_type
== VAR_UNKNOWN
)
10744 end
= get_tv_lnum_buf(&argvars
[2], buf
);
10746 get_buffer_lines(buf
, lnum
, end
, TRUE
, rettv
);
10750 * "getbufvar()" function
10753 f_getbufvar(argvars
, rettv
)
10758 buf_T
*save_curbuf
;
10762 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
10763 varname
= get_tv_string_chk(&argvars
[1]);
10765 buf
= get_buf_tv(&argvars
[0]);
10767 rettv
->v_type
= VAR_STRING
;
10768 rettv
->vval
.v_string
= NULL
;
10770 if (buf
!= NULL
&& varname
!= NULL
)
10772 /* set curbuf to be our buf, temporarily */
10773 save_curbuf
= curbuf
;
10776 if (*varname
== '&') /* buffer-local-option */
10777 get_option_tv(&varname
, rettv
, TRUE
);
10780 if (*varname
== NUL
)
10781 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10782 * scope prefix before the NUL byte is required by
10783 * find_var_in_ht(). */
10784 varname
= (char_u
*)"b:" + 2;
10785 /* look up the variable */
10786 v
= find_var_in_ht(&curbuf
->b_vars
.dv_hashtab
, varname
, FALSE
);
10788 copy_tv(&v
->di_tv
, rettv
);
10791 /* restore previous notion of curbuf */
10792 curbuf
= save_curbuf
;
10799 * "getchar()" function
10802 f_getchar(argvars
, rettv
)
10809 /* Position the cursor. Needed after a message that ends in a space. */
10810 windgoto(msg_row
, msg_col
);
10816 if (argvars
[0].v_type
== VAR_UNKNOWN
)
10817 /* getchar(): blocking wait. */
10819 else if (get_tv_number_chk(&argvars
[0], &error
) == 1)
10820 /* getchar(1): only check if char avail */
10822 else if (error
|| vpeekc() == NUL
)
10823 /* illegal argument or getchar(0) and no char avail: return zero */
10826 /* getchar(0) and char avail: return char */
10835 vimvars
[VV_MOUSE_WIN
].vv_nr
= 0;
10836 vimvars
[VV_MOUSE_LNUM
].vv_nr
= 0;
10837 vimvars
[VV_MOUSE_COL
].vv_nr
= 0;
10839 rettv
->vval
.v_number
= n
;
10840 if (IS_SPECIAL(n
) || mod_mask
!= 0)
10842 char_u temp
[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10845 /* Turn a special key into three bytes, plus modifier. */
10848 temp
[i
++] = K_SPECIAL
;
10849 temp
[i
++] = KS_MODIFIER
;
10850 temp
[i
++] = mod_mask
;
10854 temp
[i
++] = K_SPECIAL
;
10855 temp
[i
++] = K_SECOND(n
);
10856 temp
[i
++] = K_THIRD(n
);
10859 else if (has_mbyte
)
10860 i
+= (*mb_char2bytes
)(n
, temp
+ i
);
10865 rettv
->v_type
= VAR_STRING
;
10866 rettv
->vval
.v_string
= vim_strsave(temp
);
10869 if (n
== K_LEFTMOUSE
10870 || n
== K_LEFTMOUSE_NM
10872 || n
== K_LEFTRELEASE
10873 || n
== K_LEFTRELEASE_NM
10874 || n
== K_MIDDLEMOUSE
10875 || n
== K_MIDDLEDRAG
10876 || n
== K_MIDDLERELEASE
10877 || n
== K_RIGHTMOUSE
10878 || n
== K_RIGHTDRAG
10879 || n
== K_RIGHTRELEASE
10882 || n
== K_X1RELEASE
10885 || n
== K_X2RELEASE
10886 || n
== K_MOUSEDOWN
10889 int row
= mouse_row
;
10890 int col
= mouse_col
;
10893 # ifdef FEAT_WINDOWS
10898 if (row
>= 0 && col
>= 0)
10900 /* Find the window at the mouse coordinates and compute the
10901 * text position. */
10902 win
= mouse_find_win(&row
, &col
);
10903 (void)mouse_comp_pos(win
, &row
, &col
, &lnum
);
10904 # ifdef FEAT_WINDOWS
10905 for (wp
= firstwin
; wp
!= win
; wp
= wp
->w_next
)
10908 vimvars
[VV_MOUSE_WIN
].vv_nr
= winnr
;
10909 vimvars
[VV_MOUSE_LNUM
].vv_nr
= lnum
;
10910 vimvars
[VV_MOUSE_COL
].vv_nr
= col
+ 1;
10918 * "getcharmod()" function
10921 f_getcharmod(argvars
, rettv
)
10922 typval_T
*argvars UNUSED
;
10925 rettv
->vval
.v_number
= mod_mask
;
10929 * "getcmdline()" function
10932 f_getcmdline(argvars
, rettv
)
10933 typval_T
*argvars UNUSED
;
10936 rettv
->v_type
= VAR_STRING
;
10937 rettv
->vval
.v_string
= get_cmdline_str();
10941 * "getcmdpos()" function
10944 f_getcmdpos(argvars
, rettv
)
10945 typval_T
*argvars UNUSED
;
10948 rettv
->vval
.v_number
= get_cmdline_pos() + 1;
10952 * "getcmdtype()" function
10955 f_getcmdtype(argvars
, rettv
)
10956 typval_T
*argvars UNUSED
;
10959 rettv
->v_type
= VAR_STRING
;
10960 rettv
->vval
.v_string
= alloc(2);
10961 if (rettv
->vval
.v_string
!= NULL
)
10963 rettv
->vval
.v_string
[0] = get_cmdline_type();
10964 rettv
->vval
.v_string
[1] = NUL
;
10969 * "getcwd()" function
10972 f_getcwd(argvars
, rettv
)
10973 typval_T
*argvars UNUSED
;
10976 char_u cwd
[MAXPATHL
];
10978 rettv
->v_type
= VAR_STRING
;
10979 if (mch_dirname(cwd
, MAXPATHL
) == FAIL
)
10980 rettv
->vval
.v_string
= NULL
;
10983 rettv
->vval
.v_string
= vim_strsave(cwd
);
10984 #ifdef BACKSLASH_IN_FILENAME
10985 if (rettv
->vval
.v_string
!= NULL
)
10986 slash_adjust(rettv
->vval
.v_string
);
10992 * "getfontname()" function
10995 f_getfontname(argvars
, rettv
)
10996 typval_T
*argvars UNUSED
;
10999 rettv
->v_type
= VAR_STRING
;
11000 rettv
->vval
.v_string
= NULL
;
11005 char_u
*name
= NULL
;
11007 if (argvars
[0].v_type
== VAR_UNKNOWN
)
11009 /* Get the "Normal" font. Either the name saved by
11010 * hl_set_font_name() or from the font ID. */
11011 font
= gui
.norm_font
;
11012 name
= hl_get_font_name();
11016 name
= get_tv_string(&argvars
[0]);
11017 if (STRCMP(name
, "*") == 0) /* don't use font dialog */
11019 font
= gui_mch_get_font(name
, FALSE
);
11020 if (font
== NOFONT
)
11021 return; /* Invalid font name, return empty string. */
11023 rettv
->vval
.v_string
= gui_mch_get_fontname(font
, name
);
11024 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
11025 gui_mch_free_font(font
);
11031 * "getfperm({fname})" function
11034 f_getfperm(argvars
, rettv
)
11040 char_u
*perm
= NULL
;
11041 char_u flags
[] = "rwx";
11044 fname
= get_tv_string(&argvars
[0]);
11046 rettv
->v_type
= VAR_STRING
;
11047 if (mch_stat((char *)fname
, &st
) >= 0)
11049 perm
= vim_strsave((char_u
*)"---------");
11052 for (i
= 0; i
< 9; i
++)
11054 if (st
.st_mode
& (1 << (8 - i
)))
11055 perm
[i
] = flags
[i
% 3];
11059 rettv
->vval
.v_string
= perm
;
11063 * "getfsize({fname})" function
11066 f_getfsize(argvars
, rettv
)
11073 fname
= get_tv_string(&argvars
[0]);
11075 rettv
->v_type
= VAR_NUMBER
;
11077 if (mch_stat((char *)fname
, &st
) >= 0)
11079 if (mch_isdir(fname
))
11080 rettv
->vval
.v_number
= 0;
11083 rettv
->vval
.v_number
= (varnumber_T
)st
.st_size
;
11085 /* non-perfect check for overflow */
11086 if ((off_t
)rettv
->vval
.v_number
!= (off_t
)st
.st_size
)
11087 rettv
->vval
.v_number
= -2;
11091 rettv
->vval
.v_number
= -1;
11095 * "getftime({fname})" function
11098 f_getftime(argvars
, rettv
)
11105 fname
= get_tv_string(&argvars
[0]);
11107 if (mch_stat((char *)fname
, &st
) >= 0)
11108 rettv
->vval
.v_number
= (varnumber_T
)st
.st_mtime
;
11110 rettv
->vval
.v_number
= -1;
11114 * "getftype({fname})" function
11117 f_getftype(argvars
, rettv
)
11123 char_u
*type
= NULL
;
11126 fname
= get_tv_string(&argvars
[0]);
11128 rettv
->v_type
= VAR_STRING
;
11129 if (mch_lstat((char *)fname
, &st
) >= 0)
11132 if (S_ISREG(st
.st_mode
))
11134 else if (S_ISDIR(st
.st_mode
))
11137 else if (S_ISLNK(st
.st_mode
))
11141 else if (S_ISBLK(st
.st_mode
))
11145 else if (S_ISCHR(st
.st_mode
))
11149 else if (S_ISFIFO(st
.st_mode
))
11153 else if (S_ISSOCK(st
.st_mode
))
11160 switch (st
.st_mode
& S_IFMT
)
11162 case S_IFREG
: t
= "file"; break;
11163 case S_IFDIR
: t
= "dir"; break;
11165 case S_IFLNK
: t
= "link"; break;
11168 case S_IFBLK
: t
= "bdev"; break;
11171 case S_IFCHR
: t
= "cdev"; break;
11174 case S_IFIFO
: t
= "fifo"; break;
11177 case S_IFSOCK
: t
= "socket"; break;
11179 default: t
= "other";
11182 if (mch_isdir(fname
))
11188 type
= vim_strsave((char_u
*)t
);
11190 rettv
->vval
.v_string
= type
;
11194 * "getline(lnum, [end])" function
11197 f_getline(argvars
, rettv
)
11205 lnum
= get_tv_lnum(argvars
);
11206 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11213 end
= get_tv_lnum(&argvars
[1]);
11217 get_buffer_lines(curbuf
, lnum
, end
, retlist
, rettv
);
11221 * "getmatches()" function
11224 f_getmatches(argvars
, rettv
)
11225 typval_T
*argvars UNUSED
;
11228 #ifdef FEAT_SEARCH_EXTRA
11230 matchitem_T
*cur
= curwin
->w_match_head
;
11232 if (rettv_list_alloc(rettv
) == OK
)
11234 while (cur
!= NULL
)
11236 dict
= dict_alloc();
11239 dict_add_nr_str(dict
, "group", 0L, syn_id2name(cur
->hlg_id
));
11240 dict_add_nr_str(dict
, "pattern", 0L, cur
->pattern
);
11241 dict_add_nr_str(dict
, "priority", (long)cur
->priority
, NULL
);
11242 dict_add_nr_str(dict
, "id", (long)cur
->id
, NULL
);
11243 list_append_dict(rettv
->vval
.v_list
, dict
);
11251 * "getpid()" function
11254 f_getpid(argvars
, rettv
)
11255 typval_T
*argvars UNUSED
;
11258 rettv
->vval
.v_number
= mch_get_pid();
11262 * "getpos(string)" function
11265 f_getpos(argvars
, rettv
)
11273 if (rettv_list_alloc(rettv
) == OK
)
11275 l
= rettv
->vval
.v_list
;
11276 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
11278 list_append_number(l
, (varnumber_T
)fnum
);
11280 list_append_number(l
, (varnumber_T
)0);
11281 list_append_number(l
, (fp
!= NULL
) ? (varnumber_T
)fp
->lnum
11283 list_append_number(l
, (fp
!= NULL
)
11284 ? (varnumber_T
)(fp
->col
== MAXCOL
? MAXCOL
: fp
->col
+ 1)
11286 list_append_number(l
,
11287 #ifdef FEAT_VIRTUALEDIT
11288 (fp
!= NULL
) ? (varnumber_T
)fp
->coladd
:
11293 rettv
->vval
.v_number
= FALSE
;
11297 * "getqflist()" and "getloclist()" functions
11300 f_getqflist(argvars
, rettv
)
11301 typval_T
*argvars UNUSED
;
11302 typval_T
*rettv UNUSED
;
11304 #ifdef FEAT_QUICKFIX
11308 #ifdef FEAT_QUICKFIX
11309 if (rettv_list_alloc(rettv
) == OK
)
11312 if (argvars
[0].v_type
!= VAR_UNKNOWN
) /* getloclist() */
11314 wp
= find_win_by_nr(&argvars
[0], NULL
);
11319 (void)get_errorlist(wp
, rettv
->vval
.v_list
);
11325 * "getreg()" function
11328 f_getreg(argvars
, rettv
)
11332 char_u
*strregname
;
11337 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
11339 strregname
= get_tv_string_chk(&argvars
[0]);
11340 error
= strregname
== NULL
;
11341 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
11342 arg2
= get_tv_number_chk(&argvars
[1], &error
);
11345 strregname
= vimvars
[VV_REG
].vv_str
;
11346 regname
= (strregname
== NULL
? '"' : *strregname
);
11350 rettv
->v_type
= VAR_STRING
;
11351 rettv
->vval
.v_string
= error
? NULL
:
11352 get_reg_contents(regname
, TRUE
, arg2
);
11356 * "getregtype()" function
11359 f_getregtype(argvars
, rettv
)
11363 char_u
*strregname
;
11365 char_u buf
[NUMBUFLEN
+ 2];
11368 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
11370 strregname
= get_tv_string_chk(&argvars
[0]);
11371 if (strregname
== NULL
) /* type error; errmsg already given */
11373 rettv
->v_type
= VAR_STRING
;
11374 rettv
->vval
.v_string
= NULL
;
11379 /* Default to v:register */
11380 strregname
= vimvars
[VV_REG
].vv_str
;
11382 regname
= (strregname
== NULL
? '"' : *strregname
);
11388 switch (get_reg_type(regname
, ®len
))
11390 case MLINE
: buf
[0] = 'V'; break;
11391 case MCHAR
: buf
[0] = 'v'; break;
11395 sprintf((char *)buf
+ 1, "%ld", reglen
+ 1);
11399 rettv
->v_type
= VAR_STRING
;
11400 rettv
->vval
.v_string
= vim_strsave(buf
);
11404 * "gettabwinvar()" function
11407 f_gettabwinvar(argvars
, rettv
)
11411 getwinvar(argvars
, rettv
, 1);
11415 * "getwinposx()" function
11418 f_getwinposx(argvars
, rettv
)
11419 typval_T
*argvars UNUSED
;
11422 rettv
->vval
.v_number
= -1;
11428 if (gui_mch_get_winpos(&x
, &y
) == OK
)
11429 rettv
->vval
.v_number
= x
;
11435 * "getwinposy()" function
11438 f_getwinposy(argvars
, rettv
)
11439 typval_T
*argvars UNUSED
;
11442 rettv
->vval
.v_number
= -1;
11448 if (gui_mch_get_winpos(&x
, &y
) == OK
)
11449 rettv
->vval
.v_number
= y
;
11455 * Find window specified by "vp" in tabpage "tp".
11458 find_win_by_nr(vp
, tp
)
11460 tabpage_T
*tp
; /* NULL for current tab page */
11462 #ifdef FEAT_WINDOWS
11467 nr
= get_tv_number_chk(vp
, NULL
);
11469 #ifdef FEAT_WINDOWS
11475 for (wp
= (tp
== NULL
|| tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
11476 wp
!= NULL
; wp
= wp
->w_next
)
11481 if (nr
== 0 || nr
== 1)
11488 * "getwinvar()" function
11491 f_getwinvar(argvars
, rettv
)
11495 getwinvar(argvars
, rettv
, 0);
11499 * getwinvar() and gettabwinvar()
11502 getwinvar(argvars
, rettv
, off
)
11505 int off
; /* 1 for gettabwinvar() */
11507 win_T
*win
, *oldcurwin
;
11512 #ifdef FEAT_WINDOWS
11514 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
11518 win
= find_win_by_nr(&argvars
[off
], tp
);
11519 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
11522 rettv
->v_type
= VAR_STRING
;
11523 rettv
->vval
.v_string
= NULL
;
11525 if (win
!= NULL
&& varname
!= NULL
)
11527 /* Set curwin to be our win, temporarily. Also set curbuf, so
11528 * that we can get buffer-local options. */
11529 oldcurwin
= curwin
;
11531 curbuf
= win
->w_buffer
;
11533 if (*varname
== '&') /* window-local-option */
11534 get_option_tv(&varname
, rettv
, 1);
11537 if (*varname
== NUL
)
11538 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11539 * scope prefix before the NUL byte is required by
11540 * find_var_in_ht(). */
11541 varname
= (char_u
*)"w:" + 2;
11542 /* look up the variable */
11543 v
= find_var_in_ht(&win
->w_vars
.dv_hashtab
, varname
, FALSE
);
11545 copy_tv(&v
->di_tv
, rettv
);
11548 /* restore previous notion of curwin */
11549 curwin
= oldcurwin
;
11550 curbuf
= curwin
->w_buffer
;
11557 * "glob()" function
11560 f_glob(argvars
, rettv
)
11564 int flags
= WILD_SILENT
|WILD_USE_NL
;
11568 /* When the optional second argument is non-zero, don't remove matches
11569 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11570 if (argvars
[1].v_type
!= VAR_UNKNOWN
11571 && get_tv_number_chk(&argvars
[1], &error
))
11572 flags
|= WILD_KEEP_ALL
;
11573 rettv
->v_type
= VAR_STRING
;
11577 xpc
.xp_context
= EXPAND_FILES
;
11578 rettv
->vval
.v_string
= ExpandOne(&xpc
, get_tv_string(&argvars
[0]),
11579 NULL
, flags
, WILD_ALL
);
11582 rettv
->vval
.v_string
= NULL
;
11586 * "globpath()" function
11589 f_globpath(argvars
, rettv
)
11594 char_u buf1
[NUMBUFLEN
];
11595 char_u
*file
= get_tv_string_buf_chk(&argvars
[1], buf1
);
11598 /* When the optional second argument is non-zero, don't remove matches
11599 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11600 if (argvars
[2].v_type
!= VAR_UNKNOWN
11601 && get_tv_number_chk(&argvars
[2], &error
))
11602 flags
|= WILD_KEEP_ALL
;
11603 rettv
->v_type
= VAR_STRING
;
11604 if (file
== NULL
|| error
)
11605 rettv
->vval
.v_string
= NULL
;
11607 rettv
->vval
.v_string
= globpath(get_tv_string(&argvars
[0]), file
,
11615 f_has(argvars
, rettv
)
11622 static char *(has_list
[]) =
11643 #if defined(MACOS_X_UNIX)
11667 #if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11670 #if defined(WIN64) || defined(_WIN64)
11676 #ifndef CASE_INSENSITIVE_FILENAME
11682 #ifdef FEAT_AUTOCMD
11687 # ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11688 "balloon_multiline",
11691 #if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11693 # ifdef ALL_BUILTIN_TCAPS
11694 "all_builtin_terms",
11697 #ifdef FEAT_BYTEOFF
11700 #ifdef FEAT_CINDENT
11703 #ifdef FEAT_CLIENTSERVER
11706 #ifdef FEAT_CLIPBOARD
11709 #ifdef FEAT_CMDL_COMPL
11712 #ifdef FEAT_CMDHIST
11715 #ifdef FEAT_COMMENTS
11724 #ifdef CURSOR_SHAPE
11730 #ifdef FEAT_CON_DIALOG
11733 #ifdef FEAT_GUI_DIALOG
11739 #ifdef FEAT_DIGRAPHS
11745 #ifdef FEAT_EMACS_TAGS
11748 "eval", /* always present, of course! */
11749 #ifdef FEAT_EX_EXTRA
11752 #ifdef FEAT_SEARCH_EXTRA
11758 #ifdef FEAT_SEARCHPATH
11761 #if defined(UNIX) && !defined(USE_SYSTEM)
11764 #ifdef FEAT_FIND_ID
11770 #ifdef FEAT_FOLDING
11776 #if !defined(USE_SYSTEM) && defined(UNIX)
11779 #ifdef FEAT_GETTEXT
11785 #ifdef FEAT_GUI_ATHENA
11786 # ifdef FEAT_GUI_NEXTAW
11792 #ifdef FEAT_GUI_GTK
11798 #ifdef FEAT_GUI_GNOME
11801 #ifdef FEAT_GUI_MAC
11804 #ifdef FEAT_GUI_MOTIF
11807 #ifdef FEAT_GUI_PHOTON
11810 #ifdef FEAT_GUI_W16
11813 #ifdef FEAT_GUI_W32
11816 #ifdef FEAT_HANGULIN
11819 #if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11822 #ifdef FEAT_INS_EXPAND
11825 #ifdef FEAT_JUMPLIST
11831 #ifdef FEAT_LANGMAP
11834 #ifdef FEAT_LIBCALL
11837 #ifdef FEAT_LINEBREAK
11843 #ifdef FEAT_LISTCMDS
11846 #ifdef FEAT_LOCALMAP
11852 #ifdef FEAT_SESSION
11855 #ifdef FEAT_MODIFY_FNAME
11861 #ifdef FEAT_MOUSESHAPE
11864 #if defined(UNIX) || defined(VMS)
11865 # ifdef FEAT_MOUSE_DEC
11868 # ifdef FEAT_MOUSE_GPM
11871 # ifdef FEAT_MOUSE_JSB
11874 # ifdef FEAT_MOUSE_NET
11877 # ifdef FEAT_MOUSE_PTERM
11880 # ifdef FEAT_SYSMOUSE
11883 # ifdef FEAT_MOUSE_XTERM
11890 #ifdef FEAT_MBYTE_IME
11893 #ifdef FEAT_MULTI_LANG
11896 #ifdef FEAT_MZSCHEME
11897 #ifndef DYNAMIC_MZSCHEME
11904 #ifdef FEAT_OSFILETYPE
11907 #ifdef FEAT_PATH_EXTRA
11911 #ifndef DYNAMIC_PERL
11916 #ifndef DYNAMIC_PYTHON
11920 #ifdef FEAT_POSTSCRIPT
11923 #ifdef FEAT_PRINTER
11926 #ifdef FEAT_PROFILE
11929 #ifdef FEAT_RELTIME
11932 #ifdef FEAT_QUICKFIX
11935 #ifdef FEAT_RIGHTLEFT
11938 #if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11941 #ifdef FEAT_SCROLLBIND
11944 #ifdef FEAT_CMDL_INFO
11951 #ifdef FEAT_SMARTINDENT
11960 #ifdef FEAT_STL_OPT
11963 #ifdef FEAT_SUN_WORKSHOP
11966 #ifdef FEAT_NETBEANS_INTG
11975 #if defined(USE_SYSTEM) || !defined(UNIX)
11978 #ifdef FEAT_TAG_BINS
11981 #ifdef FEAT_TAG_OLDSTATIC
11984 #ifdef FEAT_TAG_ANYWHITE
11988 # ifndef DYNAMIC_TCL
11995 #ifdef FEAT_TERMRESPONSE
11998 #ifdef FEAT_TEXTOBJ
12001 #ifdef HAVE_TGETENT
12007 #ifdef FEAT_TOOLBAR
12010 #ifdef FEAT_USR_CMDS
12011 "user-commands", /* was accidentally included in 5.4 */
12014 #ifdef FEAT_VIMINFO
12017 #ifdef FEAT_VERTSPLIT
12020 #ifdef FEAT_VIRTUALEDIT
12026 #ifdef FEAT_VISUALEXTRA
12029 #ifdef FEAT_VREPLACE
12032 #ifdef FEAT_WILDIGN
12035 #ifdef FEAT_WILDMENU
12038 #ifdef FEAT_WINDOWS
12044 #ifdef FEAT_WRITEBACKUP
12050 #ifdef FEAT_XFONTSET
12056 #ifdef USE_XSMP_INTERACT
12059 #ifdef FEAT_XCLIPBOARD
12062 #ifdef FEAT_XTERM_SAVE
12065 #if defined(UNIX) && defined(FEAT_X11)
12071 name
= get_tv_string(&argvars
[0]);
12072 for (i
= 0; has_list
[i
] != NULL
; ++i
)
12073 if (STRICMP(name
, has_list
[i
]) == 0)
12081 if (STRNICMP(name
, "patch", 5) == 0)
12082 n
= has_patch(atoi((char *)name
+ 5));
12083 else if (STRICMP(name
, "vim_starting") == 0)
12084 n
= (starting
!= 0);
12086 else if (STRICMP(name
, "multi_byte_encoding") == 0)
12089 #if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12090 else if (STRICMP(name
, "balloon_multiline") == 0)
12091 n
= multiline_balloon_available();
12094 else if (STRICMP(name
, "tcl") == 0)
12095 n
= tcl_enabled(FALSE
);
12097 #if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12098 else if (STRICMP(name
, "iconv") == 0)
12099 n
= iconv_enabled(FALSE
);
12101 #ifdef DYNAMIC_MZSCHEME
12102 else if (STRICMP(name
, "mzscheme") == 0)
12103 n
= mzscheme_enabled(FALSE
);
12105 #ifdef DYNAMIC_RUBY
12106 else if (STRICMP(name
, "ruby") == 0)
12107 n
= ruby_enabled(FALSE
);
12109 #ifdef DYNAMIC_PYTHON
12110 else if (STRICMP(name
, "python") == 0)
12111 n
= python_enabled(FALSE
);
12113 #ifdef DYNAMIC_PERL
12114 else if (STRICMP(name
, "perl") == 0)
12115 n
= perl_enabled(FALSE
);
12118 else if (STRICMP(name
, "gui_running") == 0)
12119 n
= (gui
.in_use
|| gui
.starting
);
12120 # ifdef FEAT_GUI_W32
12121 else if (STRICMP(name
, "gui_win32s") == 0)
12122 n
= gui_is_win32s();
12124 # ifdef FEAT_BROWSE
12125 else if (STRICMP(name
, "browse") == 0)
12126 n
= gui
.in_use
; /* gui_mch_browse() works when GUI is running */
12130 else if (STRICMP(name
, "syntax_items") == 0)
12131 n
= syntax_present(curbuf
);
12133 #if defined(WIN3264)
12134 else if (STRICMP(name
, "win95") == 0)
12135 n
= mch_windows95();
12137 #ifdef FEAT_NETBEANS_INTG
12138 else if (STRICMP(name
, "netbeans_enabled") == 0)
12143 rettv
->vval
.v_number
= n
;
12147 * "has_key()" function
12150 f_has_key(argvars
, rettv
)
12154 if (argvars
[0].v_type
!= VAR_DICT
)
12156 EMSG(_(e_dictreq
));
12159 if (argvars
[0].vval
.v_dict
== NULL
)
12162 rettv
->vval
.v_number
= dict_find(argvars
[0].vval
.v_dict
,
12163 get_tv_string(&argvars
[1]), -1) != NULL
;
12167 * "haslocaldir()" function
12170 f_haslocaldir(argvars
, rettv
)
12171 typval_T
*argvars UNUSED
;
12174 rettv
->vval
.v_number
= (curwin
->w_localdir
!= NULL
);
12178 * "hasmapto()" function
12181 f_hasmapto(argvars
, rettv
)
12187 char_u buf
[NUMBUFLEN
];
12190 name
= get_tv_string(&argvars
[0]);
12191 if (argvars
[1].v_type
== VAR_UNKNOWN
)
12192 mode
= (char_u
*)"nvo";
12195 mode
= get_tv_string_buf(&argvars
[1], buf
);
12196 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12197 abbr
= get_tv_number(&argvars
[2]);
12200 if (map_to_exists(name
, mode
, abbr
))
12201 rettv
->vval
.v_number
= TRUE
;
12203 rettv
->vval
.v_number
= FALSE
;
12207 * "histadd()" function
12210 f_histadd(argvars
, rettv
)
12211 typval_T
*argvars UNUSED
;
12214 #ifdef FEAT_CMDHIST
12217 char_u buf
[NUMBUFLEN
];
12220 rettv
->vval
.v_number
= FALSE
;
12221 if (check_restricted() || check_secure())
12223 #ifdef FEAT_CMDHIST
12224 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
12225 histype
= str
!= NULL
? get_histtype(str
) : -1;
12228 str
= get_tv_string_buf(&argvars
[1], buf
);
12232 add_to_history(histype
, str
, FALSE
, NUL
);
12233 rettv
->vval
.v_number
= TRUE
;
12241 * "histdel()" function
12244 f_histdel(argvars
, rettv
)
12245 typval_T
*argvars UNUSED
;
12246 typval_T
*rettv UNUSED
;
12248 #ifdef FEAT_CMDHIST
12250 char_u buf
[NUMBUFLEN
];
12253 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
12256 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
12257 /* only one argument: clear entire history */
12258 n
= clr_history(get_histtype(str
));
12259 else if (argvars
[1].v_type
== VAR_NUMBER
)
12260 /* index given: remove that entry */
12261 n
= del_history_idx(get_histtype(str
),
12262 (int)get_tv_number(&argvars
[1]));
12264 /* string given: remove all matching entries */
12265 n
= del_history_entry(get_histtype(str
),
12266 get_tv_string_buf(&argvars
[1], buf
));
12267 rettv
->vval
.v_number
= n
;
12272 * "histget()" function
12275 f_histget(argvars
, rettv
)
12276 typval_T
*argvars UNUSED
;
12279 #ifdef FEAT_CMDHIST
12284 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
12286 rettv
->vval
.v_string
= NULL
;
12289 type
= get_histtype(str
);
12290 if (argvars
[1].v_type
== VAR_UNKNOWN
)
12291 idx
= get_history_idx(type
);
12293 idx
= (int)get_tv_number_chk(&argvars
[1], NULL
);
12294 /* -1 on type error */
12295 rettv
->vval
.v_string
= vim_strsave(get_history_entry(type
, idx
));
12298 rettv
->vval
.v_string
= NULL
;
12300 rettv
->v_type
= VAR_STRING
;
12304 * "histnr()" function
12307 f_histnr(argvars
, rettv
)
12308 typval_T
*argvars UNUSED
;
12313 #ifdef FEAT_CMDHIST
12314 char_u
*history
= get_tv_string_chk(&argvars
[0]);
12316 i
= history
== NULL
? HIST_CMD
- 1 : get_histtype(history
);
12317 if (i
>= HIST_CMD
&& i
< HIST_COUNT
)
12318 i
= get_history_idx(i
);
12322 rettv
->vval
.v_number
= i
;
12326 * "highlightID(name)" function
12329 f_hlID(argvars
, rettv
)
12333 rettv
->vval
.v_number
= syn_name2id(get_tv_string(&argvars
[0]));
12337 * "highlight_exists()" function
12340 f_hlexists(argvars
, rettv
)
12344 rettv
->vval
.v_number
= highlight_exists(get_tv_string(&argvars
[0]));
12348 * "hostname()" function
12351 f_hostname(argvars
, rettv
)
12352 typval_T
*argvars UNUSED
;
12355 char_u hostname
[256];
12357 mch_get_host_name(hostname
, 256);
12358 rettv
->v_type
= VAR_STRING
;
12359 rettv
->vval
.v_string
= vim_strsave(hostname
);
12366 f_iconv(argvars
, rettv
)
12367 typval_T
*argvars UNUSED
;
12371 char_u buf1
[NUMBUFLEN
];
12372 char_u buf2
[NUMBUFLEN
];
12373 char_u
*from
, *to
, *str
;
12377 rettv
->v_type
= VAR_STRING
;
12378 rettv
->vval
.v_string
= NULL
;
12381 str
= get_tv_string(&argvars
[0]);
12382 from
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[1], buf1
)));
12383 to
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[2], buf2
)));
12384 vimconv
.vc_type
= CONV_NONE
;
12385 convert_setup(&vimconv
, from
, to
);
12387 /* If the encodings are equal, no conversion needed. */
12388 if (vimconv
.vc_type
== CONV_NONE
)
12389 rettv
->vval
.v_string
= vim_strsave(str
);
12391 rettv
->vval
.v_string
= string_convert(&vimconv
, str
, NULL
);
12393 convert_setup(&vimconv
, NULL
, NULL
);
12400 * "indent()" function
12403 f_indent(argvars
, rettv
)
12409 lnum
= get_tv_lnum(argvars
);
12410 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
12411 rettv
->vval
.v_number
= get_indent_lnum(lnum
);
12413 rettv
->vval
.v_number
= -1;
12417 * "index()" function
12420 f_index(argvars
, rettv
)
12429 rettv
->vval
.v_number
= -1;
12430 if (argvars
[0].v_type
!= VAR_LIST
)
12432 EMSG(_(e_listreq
));
12435 l
= argvars
[0].vval
.v_list
;
12438 item
= l
->lv_first
;
12439 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12443 /* Start at specified item. Use the cached index that list_find()
12444 * sets, so that a negative number also works. */
12445 item
= list_find(l
, get_tv_number_chk(&argvars
[2], &error
));
12447 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
12448 ic
= get_tv_number_chk(&argvars
[3], &error
);
12453 for ( ; item
!= NULL
; item
= item
->li_next
, ++idx
)
12454 if (tv_equal(&item
->li_tv
, &argvars
[1], ic
))
12456 rettv
->vval
.v_number
= idx
;
12462 static int inputsecret_flag
= 0;
12464 static void get_user_input
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int inputdialog
));
12467 * This function is used by f_input() and f_inputdialog() functions. The third
12468 * argument to f_input() specifies the type of completion to use at the
12469 * prompt. The third argument to f_inputdialog() specifies the value to return
12470 * when the user cancels the prompt.
12473 get_user_input(argvars
, rettv
, inputdialog
)
12478 char_u
*prompt
= get_tv_string_chk(&argvars
[0]);
12481 char_u buf
[NUMBUFLEN
];
12482 int cmd_silent_save
= cmd_silent
;
12483 char_u
*defstr
= (char_u
*)"";
12484 int xp_type
= EXPAND_NOTHING
;
12485 char_u
*xp_arg
= NULL
;
12487 rettv
->v_type
= VAR_STRING
;
12488 rettv
->vval
.v_string
= NULL
;
12490 #ifdef NO_CONSOLE_INPUT
12491 /* While starting up, there is no place to enter text. */
12492 if (no_console_input())
12496 cmd_silent
= FALSE
; /* Want to see the prompt. */
12497 if (prompt
!= NULL
)
12499 /* Only the part of the message after the last NL is considered as
12500 * prompt for the command line */
12501 p
= vim_strrchr(prompt
, '\n');
12511 msg_puts_attr(prompt
, echo_attr
);
12512 msg_didout
= FALSE
;
12516 cmdline_row
= msg_row
;
12518 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12520 defstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
12521 if (defstr
!= NULL
)
12522 stuffReadbuffSpec(defstr
);
12524 if (!inputdialog
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
12530 rettv
->vval
.v_string
= NULL
;
12532 xp_name
= get_tv_string_buf_chk(&argvars
[2], buf
);
12533 if (xp_name
== NULL
)
12536 xp_namelen
= (int)STRLEN(xp_name
);
12538 if (parse_compl_arg(xp_name
, xp_namelen
, &xp_type
, &argt
,
12544 if (defstr
!= NULL
)
12545 rettv
->vval
.v_string
=
12546 getcmdline_prompt(inputsecret_flag
? NUL
: '@', p
, echo_attr
,
12551 /* since the user typed this, no need to wait for return */
12552 need_wait_return
= FALSE
;
12553 msg_didout
= FALSE
;
12555 cmd_silent
= cmd_silent_save
;
12559 * "input()" function
12560 * Also handles inputsecret() when inputsecret is set.
12563 f_input(argvars
, rettv
)
12567 get_user_input(argvars
, rettv
, FALSE
);
12571 * "inputdialog()" function
12574 f_inputdialog(argvars
, rettv
)
12578 #if defined(FEAT_GUI_TEXTDIALOG)
12579 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12580 if (gui
.in_use
&& vim_strchr(p_go
, GO_CONDIALOG
) == NULL
)
12583 char_u buf
[NUMBUFLEN
];
12584 char_u
*defstr
= (char_u
*)"";
12586 message
= get_tv_string_chk(&argvars
[0]);
12587 if (argvars
[1].v_type
!= VAR_UNKNOWN
12588 && (defstr
= get_tv_string_buf_chk(&argvars
[1], buf
)) != NULL
)
12589 vim_strncpy(IObuff
, defstr
, IOSIZE
- 1);
12592 if (message
!= NULL
&& defstr
!= NULL
12593 && do_dialog(VIM_QUESTION
, NULL
, message
,
12594 (char_u
*)_("&OK\n&Cancel"), 1, IObuff
) == 1)
12595 rettv
->vval
.v_string
= vim_strsave(IObuff
);
12598 if (message
!= NULL
&& defstr
!= NULL
12599 && argvars
[1].v_type
!= VAR_UNKNOWN
12600 && argvars
[2].v_type
!= VAR_UNKNOWN
)
12601 rettv
->vval
.v_string
= vim_strsave(
12602 get_tv_string_buf(&argvars
[2], buf
));
12604 rettv
->vval
.v_string
= NULL
;
12606 rettv
->v_type
= VAR_STRING
;
12610 get_user_input(argvars
, rettv
, TRUE
);
12614 * "inputlist()" function
12617 f_inputlist(argvars
, rettv
)
12625 #ifdef NO_CONSOLE_INPUT
12626 /* While starting up, there is no place to enter text. */
12627 if (no_console_input())
12630 if (argvars
[0].v_type
!= VAR_LIST
|| argvars
[0].vval
.v_list
== NULL
)
12632 EMSG2(_(e_listarg
), "inputlist()");
12637 msg_row
= Rows
- 1; /* for when 'cmdheight' > 1 */
12638 lines_left
= Rows
; /* avoid more prompt */
12642 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
; li
= li
->li_next
)
12644 msg_puts(get_tv_string(&li
->li_tv
));
12648 /* Ask for choice. */
12649 selected
= prompt_for_number(&mouse_used
);
12651 selected
-= lines_left
;
12653 rettv
->vval
.v_number
= selected
;
12657 static garray_T ga_userinput
= {0, 0, sizeof(tasave_T
), 4, NULL
};
12660 * "inputrestore()" function
12663 f_inputrestore(argvars
, rettv
)
12664 typval_T
*argvars UNUSED
;
12667 if (ga_userinput
.ga_len
> 0)
12669 --ga_userinput
.ga_len
;
12670 restore_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
12671 + ga_userinput
.ga_len
);
12672 /* default return is zero == OK */
12674 else if (p_verbose
> 1)
12676 verb_msg((char_u
*)_("called inputrestore() more often than inputsave()"));
12677 rettv
->vval
.v_number
= 1; /* Failed */
12682 * "inputsave()" function
12685 f_inputsave(argvars
, rettv
)
12686 typval_T
*argvars UNUSED
;
12689 /* Add an entry to the stack of typeahead storage. */
12690 if (ga_grow(&ga_userinput
, 1) == OK
)
12692 save_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
12693 + ga_userinput
.ga_len
);
12694 ++ga_userinput
.ga_len
;
12695 /* default return is zero == OK */
12698 rettv
->vval
.v_number
= 1; /* Failed */
12702 * "inputsecret()" function
12705 f_inputsecret(argvars
, rettv
)
12710 ++inputsecret_flag
;
12711 f_input(argvars
, rettv
);
12713 --inputsecret_flag
;
12717 * "insert()" function
12720 f_insert(argvars
, rettv
)
12729 if (argvars
[0].v_type
!= VAR_LIST
)
12730 EMSG2(_(e_listarg
), "insert()");
12731 else if ((l
= argvars
[0].vval
.v_list
) != NULL
12732 && !tv_check_lock(l
->lv_lock
, (char_u
*)"insert()"))
12734 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12735 before
= get_tv_number_chk(&argvars
[2], &error
);
12737 return; /* type error; errmsg already given */
12739 if (before
== l
->lv_len
)
12743 item
= list_find(l
, before
);
12746 EMSGN(_(e_listidx
), before
);
12752 list_insert_tv(l
, &argvars
[1], item
);
12753 copy_tv(&argvars
[0], rettv
);
12759 * "isdirectory()" function
12762 f_isdirectory(argvars
, rettv
)
12766 rettv
->vval
.v_number
= mch_isdir(get_tv_string(&argvars
[0]));
12770 * "islocked()" function
12773 f_islocked(argvars
, rettv
)
12781 rettv
->vval
.v_number
= -1;
12782 end
= get_lval(get_tv_string(&argvars
[0]), NULL
, &lv
, FALSE
, FALSE
, FALSE
,
12784 if (end
!= NULL
&& lv
.ll_name
!= NULL
)
12787 EMSG(_(e_trailing
));
12790 if (lv
.ll_tv
== NULL
)
12792 if (check_changedtick(lv
.ll_name
))
12793 rettv
->vval
.v_number
= 1; /* always locked */
12796 di
= find_var(lv
.ll_name
, NULL
);
12799 /* Consider a variable locked when:
12800 * 1. the variable itself is locked
12801 * 2. the value of the variable is locked.
12802 * 3. the List or Dict value is locked.
12804 rettv
->vval
.v_number
= ((di
->di_flags
& DI_FLAGS_LOCK
)
12805 || tv_islocked(&di
->di_tv
));
12809 else if (lv
.ll_range
)
12810 EMSG(_("E786: Range not allowed"));
12811 else if (lv
.ll_newkey
!= NULL
)
12812 EMSG2(_(e_dictkey
), lv
.ll_newkey
);
12813 else if (lv
.ll_list
!= NULL
)
12815 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_li
->li_tv
);
12817 /* Dictionary item. */
12818 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_di
->di_tv
);
12825 static void dict_list
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int what
));
12828 * Turn a dict into a list:
12829 * "what" == 0: list of keys
12830 * "what" == 1: list of values
12831 * "what" == 2: list of items
12834 dict_list(argvars
, rettv
, what
)
12847 if (argvars
[0].v_type
!= VAR_DICT
)
12849 EMSG(_(e_dictreq
));
12852 if ((d
= argvars
[0].vval
.v_dict
) == NULL
)
12855 if (rettv_list_alloc(rettv
) == FAIL
)
12858 todo
= (int)d
->dv_hashtab
.ht_used
;
12859 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
12861 if (!HASHITEM_EMPTY(hi
))
12866 li
= listitem_alloc();
12869 list_append(rettv
->vval
.v_list
, li
);
12874 li
->li_tv
.v_type
= VAR_STRING
;
12875 li
->li_tv
.v_lock
= 0;
12876 li
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
12878 else if (what
== 1)
12881 copy_tv(&di
->di_tv
, &li
->li_tv
);
12887 li
->li_tv
.v_type
= VAR_LIST
;
12888 li
->li_tv
.v_lock
= 0;
12889 li
->li_tv
.vval
.v_list
= l2
;
12894 li2
= listitem_alloc();
12897 list_append(l2
, li2
);
12898 li2
->li_tv
.v_type
= VAR_STRING
;
12899 li2
->li_tv
.v_lock
= 0;
12900 li2
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
12902 li2
= listitem_alloc();
12905 list_append(l2
, li2
);
12906 copy_tv(&di
->di_tv
, &li2
->li_tv
);
12913 * "items(dict)" function
12916 f_items(argvars
, rettv
)
12920 dict_list(argvars
, rettv
, 2);
12924 * "join()" function
12927 f_join(argvars
, rettv
)
12934 if (argvars
[0].v_type
!= VAR_LIST
)
12936 EMSG(_(e_listreq
));
12939 if (argvars
[0].vval
.v_list
== NULL
)
12941 if (argvars
[1].v_type
== VAR_UNKNOWN
)
12942 sep
= (char_u
*)" ";
12944 sep
= get_tv_string_chk(&argvars
[1]);
12946 rettv
->v_type
= VAR_STRING
;
12950 ga_init2(&ga
, (int)sizeof(char), 80);
12951 list_join(&ga
, argvars
[0].vval
.v_list
, sep
, TRUE
, 0);
12952 ga_append(&ga
, NUL
);
12953 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
12956 rettv
->vval
.v_string
= NULL
;
12960 * "keys()" function
12963 f_keys(argvars
, rettv
)
12967 dict_list(argvars
, rettv
, 0);
12971 * "last_buffer_nr()" function.
12974 f_last_buffer_nr(argvars
, rettv
)
12975 typval_T
*argvars UNUSED
;
12981 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
12982 if (n
< buf
->b_fnum
)
12985 rettv
->vval
.v_number
= n
;
12992 f_len(argvars
, rettv
)
12996 switch (argvars
[0].v_type
)
13000 rettv
->vval
.v_number
= (varnumber_T
)STRLEN(
13001 get_tv_string(&argvars
[0]));
13004 rettv
->vval
.v_number
= list_len(argvars
[0].vval
.v_list
);
13007 rettv
->vval
.v_number
= dict_len(argvars
[0].vval
.v_dict
);
13010 EMSG(_("E701: Invalid type for len()"));
13015 static void libcall_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int type
));
13018 libcall_common(argvars
, rettv
, type
)
13023 #ifdef FEAT_LIBCALL
13025 char_u
**string_result
;
13029 rettv
->v_type
= type
;
13030 if (type
!= VAR_NUMBER
)
13031 rettv
->vval
.v_string
= NULL
;
13033 if (check_restricted() || check_secure())
13036 #ifdef FEAT_LIBCALL
13037 /* The first two args must be strings, otherwise its meaningless */
13038 if (argvars
[0].v_type
== VAR_STRING
&& argvars
[1].v_type
== VAR_STRING
)
13041 if (argvars
[2].v_type
== VAR_STRING
)
13042 string_in
= argvars
[2].vval
.v_string
;
13043 if (type
== VAR_NUMBER
)
13044 string_result
= NULL
;
13046 string_result
= &rettv
->vval
.v_string
;
13047 if (mch_libcall(argvars
[0].vval
.v_string
,
13048 argvars
[1].vval
.v_string
,
13050 argvars
[2].vval
.v_number
,
13053 && type
== VAR_NUMBER
)
13054 rettv
->vval
.v_number
= nr_result
;
13060 * "libcall()" function
13063 f_libcall(argvars
, rettv
)
13067 libcall_common(argvars
, rettv
, VAR_STRING
);
13071 * "libcallnr()" function
13074 f_libcallnr(argvars
, rettv
)
13078 libcall_common(argvars
, rettv
, VAR_NUMBER
);
13082 * "line(string)" function
13085 f_line(argvars
, rettv
)
13093 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
13096 rettv
->vval
.v_number
= lnum
;
13100 * "line2byte(lnum)" function
13103 f_line2byte(argvars
, rettv
)
13104 typval_T
*argvars UNUSED
;
13107 #ifndef FEAT_BYTEOFF
13108 rettv
->vval
.v_number
= -1;
13112 lnum
= get_tv_lnum(argvars
);
13113 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
13114 rettv
->vval
.v_number
= -1;
13116 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
, lnum
, NULL
);
13117 if (rettv
->vval
.v_number
>= 0)
13118 ++rettv
->vval
.v_number
;
13123 * "lispindent(lnum)" function
13126 f_lispindent(argvars
, rettv
)
13134 pos
= curwin
->w_cursor
;
13135 lnum
= get_tv_lnum(argvars
);
13136 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
13138 curwin
->w_cursor
.lnum
= lnum
;
13139 rettv
->vval
.v_number
= get_lisp_indent();
13140 curwin
->w_cursor
= pos
;
13144 rettv
->vval
.v_number
= -1;
13148 * "localtime()" function
13151 f_localtime(argvars
, rettv
)
13152 typval_T
*argvars UNUSED
;
13155 rettv
->vval
.v_number
= (varnumber_T
)time(NULL
);
13158 static void get_maparg
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int exact
));
13161 get_maparg(argvars
, rettv
, exact
)
13168 char_u buf
[NUMBUFLEN
];
13169 char_u
*keys_buf
= NULL
;
13175 /* return empty string for failure */
13176 rettv
->v_type
= VAR_STRING
;
13177 rettv
->vval
.v_string
= NULL
;
13179 keys
= get_tv_string(&argvars
[0]);
13183 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
13185 which
= get_tv_string_buf_chk(&argvars
[1], buf
);
13186 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13187 abbr
= get_tv_number(&argvars
[2]);
13190 which
= (char_u
*)"";
13194 mode
= get_map_mode(&which
, 0);
13196 keys
= replace_termcodes(keys
, &keys_buf
, TRUE
, TRUE
, FALSE
);
13197 rhs
= check_map(keys
, mode
, exact
, FALSE
, abbr
);
13198 vim_free(keys_buf
);
13202 ga
.ga_itemsize
= 1;
13203 ga
.ga_growsize
= 40;
13205 while (*rhs
!= NUL
)
13206 ga_concat(&ga
, str2special(&rhs
, FALSE
));
13208 ga_append(&ga
, NUL
);
13209 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
13215 * "log10()" function
13218 f_log10(argvars
, rettv
)
13224 rettv
->v_type
= VAR_FLOAT
;
13225 if (get_float_arg(argvars
, &f
) == OK
)
13226 rettv
->vval
.v_float
= log10(f
);
13228 rettv
->vval
.v_float
= 0.0;
13236 f_map(argvars
, rettv
)
13240 filter_map(argvars
, rettv
, TRUE
);
13244 * "maparg()" function
13247 f_maparg(argvars
, rettv
)
13251 get_maparg(argvars
, rettv
, TRUE
);
13255 * "mapcheck()" function
13258 f_mapcheck(argvars
, rettv
)
13262 get_maparg(argvars
, rettv
, FALSE
);
13265 static void find_some_match
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int start
));
13268 find_some_match(argvars
, rettv
, type
)
13273 char_u
*str
= NULL
;
13274 char_u
*expr
= NULL
;
13276 regmatch_T regmatch
;
13277 char_u patbuf
[NUMBUFLEN
];
13278 char_u strbuf
[NUMBUFLEN
];
13282 colnr_T startcol
= 0;
13285 listitem_T
*li
= NULL
;
13287 char_u
*tofree
= NULL
;
13289 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13291 p_cpo
= (char_u
*)"";
13293 rettv
->vval
.v_number
= -1;
13296 /* return empty list when there are no matches */
13297 if (rettv_list_alloc(rettv
) == FAIL
)
13300 else if (type
== 2)
13302 rettv
->v_type
= VAR_STRING
;
13303 rettv
->vval
.v_string
= NULL
;
13306 if (argvars
[0].v_type
== VAR_LIST
)
13308 if ((l
= argvars
[0].vval
.v_list
) == NULL
)
13313 expr
= str
= get_tv_string(&argvars
[0]);
13315 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
13319 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13323 start
= get_tv_number_chk(&argvars
[2], &error
);
13328 li
= list_find(l
, start
);
13331 idx
= l
->lv_idx
; /* use the cached index */
13337 if (start
> (long)STRLEN(str
))
13339 /* When "count" argument is there ignore matches before "start",
13340 * otherwise skip part of the string. Differs when pattern is "^"
13342 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13348 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13349 nth
= get_tv_number_chk(&argvars
[3], &error
);
13354 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
13355 if (regmatch
.regprog
!= NULL
)
13357 regmatch
.rm_ic
= p_ic
;
13369 str
= echo_string(&li
->li_tv
, &tofree
, strbuf
, 0);
13374 match
= vim_regexec_nl(®match
, str
, (colnr_T
)startcol
);
13376 if (match
&& --nth
<= 0)
13378 if (l
== NULL
&& !match
)
13381 /* Advance to just after the match. */
13390 startcol
= (colnr_T
)(regmatch
.startp
[0]
13391 + (*mb_ptr2len
)(regmatch
.startp
[0]) - str
);
13393 startcol
= regmatch
.startp
[0] + 1 - str
;
13404 /* return list with matched string and submatches */
13405 for (i
= 0; i
< NSUBEXP
; ++i
)
13407 if (regmatch
.endp
[i
] == NULL
)
13409 if (list_append_string(rettv
->vval
.v_list
,
13410 (char_u
*)"", 0) == FAIL
)
13413 else if (list_append_string(rettv
->vval
.v_list
,
13414 regmatch
.startp
[i
],
13415 (int)(regmatch
.endp
[i
] - regmatch
.startp
[i
]))
13420 else if (type
== 2)
13422 /* return matched string */
13424 copy_tv(&li
->li_tv
, rettv
);
13426 rettv
->vval
.v_string
= vim_strnsave(regmatch
.startp
[0],
13427 (int)(regmatch
.endp
[0] - regmatch
.startp
[0]));
13429 else if (l
!= NULL
)
13430 rettv
->vval
.v_number
= idx
;
13434 rettv
->vval
.v_number
=
13435 (varnumber_T
)(regmatch
.startp
[0] - str
);
13437 rettv
->vval
.v_number
=
13438 (varnumber_T
)(regmatch
.endp
[0] - str
);
13439 rettv
->vval
.v_number
+= (varnumber_T
)(str
- expr
);
13442 vim_free(regmatch
.regprog
);
13451 * "match()" function
13454 f_match(argvars
, rettv
)
13458 find_some_match(argvars
, rettv
, 1);
13462 * "matchadd()" function
13465 f_matchadd(argvars
, rettv
)
13469 #ifdef FEAT_SEARCH_EXTRA
13470 char_u buf
[NUMBUFLEN
];
13471 char_u
*grp
= get_tv_string_buf_chk(&argvars
[0], buf
); /* group */
13472 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], buf
); /* pattern */
13473 int prio
= 10; /* default priority */
13477 rettv
->vval
.v_number
= -1;
13479 if (grp
== NULL
|| pat
== NULL
)
13481 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13483 prio
= get_tv_number_chk(&argvars
[2], &error
);
13484 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13485 id
= get_tv_number_chk(&argvars
[3], &error
);
13489 if (id
>= 1 && id
<= 3)
13491 EMSGN("E798: ID is reserved for \":match\": %ld", id
);
13495 rettv
->vval
.v_number
= match_add(curwin
, grp
, pat
, prio
, id
);
13500 * "matcharg()" function
13503 f_matcharg(argvars
, rettv
)
13507 if (rettv_list_alloc(rettv
) == OK
)
13509 #ifdef FEAT_SEARCH_EXTRA
13510 int id
= get_tv_number(&argvars
[0]);
13513 if (id
>= 1 && id
<= 3)
13515 if ((m
= (matchitem_T
*)get_match(curwin
, id
)) != NULL
)
13517 list_append_string(rettv
->vval
.v_list
,
13518 syn_id2name(m
->hlg_id
), -1);
13519 list_append_string(rettv
->vval
.v_list
, m
->pattern
, -1);
13523 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
13524 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
13532 * "matchdelete()" function
13535 f_matchdelete(argvars
, rettv
)
13539 #ifdef FEAT_SEARCH_EXTRA
13540 rettv
->vval
.v_number
= match_delete(curwin
,
13541 (int)get_tv_number(&argvars
[0]), TRUE
);
13546 * "matchend()" function
13549 f_matchend(argvars
, rettv
)
13553 find_some_match(argvars
, rettv
, 0);
13557 * "matchlist()" function
13560 f_matchlist(argvars
, rettv
)
13564 find_some_match(argvars
, rettv
, 3);
13568 * "matchstr()" function
13571 f_matchstr(argvars
, rettv
)
13575 find_some_match(argvars
, rettv
, 2);
13578 static void max_min
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int domax
));
13581 max_min(argvars
, rettv
, domax
)
13590 if (argvars
[0].v_type
== VAR_LIST
)
13595 l
= argvars
[0].vval
.v_list
;
13601 n
= get_tv_number_chk(&li
->li_tv
, &error
);
13607 i
= get_tv_number_chk(&li
->li_tv
, &error
);
13608 if (domax
? i
> n
: i
< n
)
13614 else if (argvars
[0].v_type
== VAR_DICT
)
13621 d
= argvars
[0].vval
.v_dict
;
13624 todo
= (int)d
->dv_hashtab
.ht_used
;
13625 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
13627 if (!HASHITEM_EMPTY(hi
))
13630 i
= get_tv_number_chk(&HI2DI(hi
)->di_tv
, &error
);
13636 else if (domax
? i
> n
: i
< n
)
13643 EMSG(_(e_listdictarg
));
13644 rettv
->vval
.v_number
= error
? 0 : n
;
13651 f_max(argvars
, rettv
)
13655 max_min(argvars
, rettv
, TRUE
);
13662 f_min(argvars
, rettv
)
13666 max_min(argvars
, rettv
, FALSE
);
13669 static int mkdir_recurse
__ARGS((char_u
*dir
, int prot
));
13672 * Create the directory in which "dir" is located, and higher levels when
13676 mkdir_recurse(dir
, prot
)
13684 /* Get end of directory name in "dir".
13685 * We're done when it's "/" or "c:/". */
13686 p
= gettail_sep(dir
);
13687 if (p
<= get_past_head(dir
))
13690 /* If the directory exists we're done. Otherwise: create it.*/
13691 updir
= vim_strnsave(dir
, (int)(p
- dir
));
13694 if (mch_isdir(updir
))
13696 else if (mkdir_recurse(updir
, prot
) == OK
)
13697 r
= vim_mkdir_emsg(updir
, prot
);
13704 * "mkdir()" function
13707 f_mkdir(argvars
, rettv
)
13712 char_u buf
[NUMBUFLEN
];
13715 rettv
->vval
.v_number
= FAIL
;
13716 if (check_restricted() || check_secure())
13719 dir
= get_tv_string_buf(&argvars
[0], buf
);
13720 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
13722 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13723 prot
= get_tv_number_chk(&argvars
[2], NULL
);
13724 if (prot
!= -1 && STRCMP(get_tv_string(&argvars
[1]), "p") == 0)
13725 mkdir_recurse(dir
, prot
);
13727 rettv
->vval
.v_number
= prot
!= -1 ? vim_mkdir_emsg(dir
, prot
) : 0;
13732 * "mode()" function
13735 f_mode(argvars
, rettv
)
13748 buf
[0] = VIsual_mode
+ 's' - 'v';
13750 buf
[0] = VIsual_mode
;
13754 if (State
== HITRETURN
|| State
== ASKMORE
|| State
== SETWSIZE
13755 || State
== CONFIRM
)
13758 if (State
== ASKMORE
)
13760 else if (State
== CONFIRM
)
13763 else if (State
== EXTERNCMD
)
13765 else if (State
& INSERT
)
13767 #ifdef FEAT_VREPLACE
13768 if (State
& VREPLACE_FLAG
)
13775 if (State
& REPLACE_FLAG
)
13780 else if (State
& CMDLINE
)
13786 else if (exmode_active
)
13798 /* Clear out the minor mode when the argument is not a non-zero number or
13799 * non-empty string. */
13800 if (!non_zero_arg(&argvars
[0]))
13803 rettv
->vval
.v_string
= vim_strsave(buf
);
13804 rettv
->v_type
= VAR_STRING
;
13807 #ifdef FEAT_MZSCHEME
13809 * "mzeval()" function
13812 f_mzeval(argvars
, rettv
)
13817 char_u buf
[NUMBUFLEN
];
13819 str
= get_tv_string_buf(&argvars
[0], buf
);
13820 do_mzeval(str
, rettv
);
13825 * "nextnonblank()" function
13828 f_nextnonblank(argvars
, rettv
)
13834 for (lnum
= get_tv_lnum(argvars
); ; ++lnum
)
13836 if (lnum
< 0 || lnum
> curbuf
->b_ml
.ml_line_count
)
13841 if (*skipwhite(ml_get(lnum
)) != NUL
)
13844 rettv
->vval
.v_number
= lnum
;
13848 * "nr2char()" function
13851 f_nr2char(argvars
, rettv
)
13855 char_u buf
[NUMBUFLEN
];
13859 buf
[(*mb_char2bytes
)((int)get_tv_number(&argvars
[0]), buf
)] = NUL
;
13863 buf
[0] = (char_u
)get_tv_number(&argvars
[0]);
13866 rettv
->v_type
= VAR_STRING
;
13867 rettv
->vval
.v_string
= vim_strsave(buf
);
13871 * "pathshorten()" function
13874 f_pathshorten(argvars
, rettv
)
13880 rettv
->v_type
= VAR_STRING
;
13881 p
= get_tv_string_chk(&argvars
[0]);
13883 rettv
->vval
.v_string
= NULL
;
13886 p
= vim_strsave(p
);
13887 rettv
->vval
.v_string
= p
;
13898 f_pow(argvars
, rettv
)
13904 rettv
->v_type
= VAR_FLOAT
;
13905 if (get_float_arg(argvars
, &fx
) == OK
13906 && get_float_arg(&argvars
[1], &fy
) == OK
)
13907 rettv
->vval
.v_float
= pow(fx
, fy
);
13909 rettv
->vval
.v_float
= 0.0;
13914 * "prevnonblank()" function
13917 f_prevnonblank(argvars
, rettv
)
13923 lnum
= get_tv_lnum(argvars
);
13924 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
)
13927 while (lnum
>= 1 && *skipwhite(ml_get(lnum
)) == NUL
)
13929 rettv
->vval
.v_number
= lnum
;
13932 #ifdef HAVE_STDARG_H
13933 /* This dummy va_list is here because:
13934 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13935 * - locally in the function results in a "used before set" warning
13936 * - using va_start() to initialize it gives "function with fixed args" error */
13941 * "printf()" function
13944 f_printf(argvars
, rettv
)
13948 rettv
->v_type
= VAR_STRING
;
13949 rettv
->vval
.v_string
= NULL
;
13950 #ifdef HAVE_STDARG_H /* only very old compilers can't do this */
13952 char_u buf
[NUMBUFLEN
];
13955 int saved_did_emsg
= did_emsg
;
13958 /* Get the required length, allocate the buffer and do it for real. */
13960 fmt
= (char *)get_tv_string_buf(&argvars
[0], buf
);
13961 len
= vim_vsnprintf(NULL
, 0, fmt
, ap
, argvars
+ 1);
13964 s
= alloc(len
+ 1);
13967 rettv
->vval
.v_string
= s
;
13968 (void)vim_vsnprintf((char *)s
, len
+ 1, fmt
, ap
, argvars
+ 1);
13971 did_emsg
|= saved_did_emsg
;
13977 * "pumvisible()" function
13980 f_pumvisible(argvars
, rettv
)
13981 typval_T
*argvars UNUSED
;
13982 typval_T
*rettv UNUSED
;
13984 #ifdef FEAT_INS_EXPAND
13986 rettv
->vval
.v_number
= 1;
13991 * "range()" function
13994 f_range(argvars
, rettv
)
14004 start
= get_tv_number_chk(&argvars
[0], &error
);
14005 if (argvars
[1].v_type
== VAR_UNKNOWN
)
14012 end
= get_tv_number_chk(&argvars
[1], &error
);
14013 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14014 stride
= get_tv_number_chk(&argvars
[2], &error
);
14018 return; /* type error; errmsg already given */
14020 EMSG(_("E726: Stride is zero"));
14021 else if (stride
> 0 ? end
+ 1 < start
: end
- 1 > start
)
14022 EMSG(_("E727: Start past end"));
14025 if (rettv_list_alloc(rettv
) == OK
)
14026 for (i
= start
; stride
> 0 ? i
<= end
: i
>= end
; i
+= stride
)
14027 if (list_append_number(rettv
->vval
.v_list
,
14028 (varnumber_T
)i
) == FAIL
)
14034 * "readfile()" function
14037 f_readfile(argvars
, rettv
)
14041 int binary
= FALSE
;
14045 #define FREAD_SIZE 200 /* optimized for text lines */
14046 char_u buf
[FREAD_SIZE
];
14047 int readlen
; /* size of last fread() */
14048 int buflen
; /* nr of valid chars in buf[] */
14049 int filtd
; /* how much in buf[] was NUL -> '\n' filtered */
14050 int tolist
; /* first byte in buf[] still to be put in list */
14051 int chop
; /* how many CR to chop off */
14052 char_u
*prev
= NULL
; /* previously read bytes, if any */
14053 int prevlen
= 0; /* length of "prev" if not NULL */
14056 long maxline
= MAXLNUM
;
14059 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
14061 if (STRCMP(get_tv_string(&argvars
[1]), "b") == 0)
14063 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14064 maxline
= get_tv_number(&argvars
[2]);
14067 if (rettv_list_alloc(rettv
) == FAIL
)
14070 /* Always open the file in binary mode, library functions have a mind of
14071 * their own about CR-LF conversion. */
14072 fname
= get_tv_string(&argvars
[0]);
14073 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, READBIN
)) == NULL
)
14075 EMSG2(_(e_notopen
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
14080 while (cnt
< maxline
|| maxline
< 0)
14082 readlen
= (int)fread(buf
+ filtd
, 1, FREAD_SIZE
- filtd
, fd
);
14083 buflen
= filtd
+ readlen
;
14085 for ( ; filtd
< buflen
|| readlen
<= 0; ++filtd
)
14087 if (buf
[filtd
] == '\n' || readlen
<= 0)
14089 /* Only when in binary mode add an empty list item when the
14090 * last line ends in a '\n'. */
14091 if (!binary
&& readlen
== 0 && filtd
== 0)
14094 /* Found end-of-line or end-of-file: add a text line to the
14098 while (filtd
- chop
- 1 >= tolist
14099 && buf
[filtd
- chop
- 1] == '\r')
14101 len
= filtd
- tolist
- chop
;
14103 s
= vim_strnsave(buf
+ tolist
, len
);
14106 s
= alloc((unsigned)(prevlen
+ len
+ 1));
14109 mch_memmove(s
, prev
, prevlen
);
14112 mch_memmove(s
+ prevlen
, buf
+ tolist
, len
);
14113 s
[prevlen
+ len
] = NUL
;
14116 tolist
= filtd
+ 1;
14118 li
= listitem_alloc();
14124 li
->li_tv
.v_type
= VAR_STRING
;
14125 li
->li_tv
.v_lock
= 0;
14126 li
->li_tv
.vval
.v_string
= s
;
14127 list_append(rettv
->vval
.v_list
, li
);
14129 if (++cnt
>= maxline
&& maxline
>= 0)
14134 else if (buf
[filtd
] == NUL
)
14142 /* "buf" is full, need to move text to an allocated buffer */
14145 prev
= vim_strnsave(buf
, buflen
);
14150 s
= alloc((unsigned)(prevlen
+ buflen
));
14153 mch_memmove(s
, prev
, prevlen
);
14154 mch_memmove(s
+ prevlen
, buf
, buflen
);
14164 mch_memmove(buf
, buf
+ tolist
, buflen
- tolist
);
14170 * For a negative line count use only the lines at the end of the file,
14174 while (cnt
> -maxline
)
14176 listitem_remove(rettv
->vval
.v_list
, rettv
->vval
.v_list
->lv_first
);
14184 #if defined(FEAT_RELTIME)
14185 static int list2proftime
__ARGS((typval_T
*arg
, proftime_T
*tm
));
14188 * Convert a List to proftime_T.
14189 * Return FAIL when there is something wrong.
14192 list2proftime(arg
, tm
)
14199 if (arg
->v_type
!= VAR_LIST
|| arg
->vval
.v_list
== NULL
14200 || arg
->vval
.v_list
->lv_len
!= 2)
14202 n1
= list_find_nr(arg
->vval
.v_list
, 0L, &error
);
14203 n2
= list_find_nr(arg
->vval
.v_list
, 1L, &error
);
14211 return error
? FAIL
: OK
;
14213 #endif /* FEAT_RELTIME */
14216 * "reltime()" function
14219 f_reltime(argvars
, rettv
)
14223 #ifdef FEAT_RELTIME
14227 if (argvars
[0].v_type
== VAR_UNKNOWN
)
14229 /* No arguments: get current time. */
14230 profile_start(&res
);
14232 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
14234 if (list2proftime(&argvars
[0], &res
) == FAIL
)
14240 /* Two arguments: compute the difference. */
14241 if (list2proftime(&argvars
[0], &start
) == FAIL
14242 || list2proftime(&argvars
[1], &res
) == FAIL
)
14244 profile_sub(&res
, &start
);
14247 if (rettv_list_alloc(rettv
) == OK
)
14258 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n1
);
14259 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n2
);
14265 * "reltimestr()" function
14268 f_reltimestr(argvars
, rettv
)
14272 #ifdef FEAT_RELTIME
14276 rettv
->v_type
= VAR_STRING
;
14277 rettv
->vval
.v_string
= NULL
;
14278 #ifdef FEAT_RELTIME
14279 if (list2proftime(&argvars
[0], &tm
) == OK
)
14280 rettv
->vval
.v_string
= vim_strsave((char_u
*)profile_msg(&tm
));
14284 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14285 static void make_connection
__ARGS((void));
14286 static int check_connection
__ARGS((void));
14291 if (X_DISPLAY
== NULL
14297 x_force_connect
= TRUE
;
14299 x_force_connect
= FALSE
;
14307 if (X_DISPLAY
== NULL
)
14309 EMSG(_("E240: No connection to Vim server"));
14316 #ifdef FEAT_CLIENTSERVER
14317 static void remote_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int expr
));
14320 remote_common(argvars
, rettv
, expr
)
14325 char_u
*server_name
;
14328 char_u buf
[NUMBUFLEN
];
14335 if (check_restricted() || check_secure())
14339 if (check_connection() == FAIL
)
14343 server_name
= get_tv_string_chk(&argvars
[0]);
14344 if (server_name
== NULL
)
14345 return; /* type error; errmsg already given */
14346 keys
= get_tv_string_buf(&argvars
[1], buf
);
14348 if (serverSendToVim(server_name
, keys
, &r
, &w
, expr
, TRUE
) < 0)
14350 if (serverSendToVim(X_DISPLAY
, server_name
, keys
, &r
, &w
, expr
, 0, TRUE
)
14355 EMSG(r
); /* sending worked but evaluation failed */
14357 EMSG2(_("E241: Unable to send to %s"), server_name
);
14361 rettv
->vval
.v_string
= r
;
14363 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14369 sprintf((char *)str
, PRINTF_HEX_LONG_U
, (long_u
)w
);
14370 v
.di_tv
.v_type
= VAR_STRING
;
14371 v
.di_tv
.vval
.v_string
= vim_strsave(str
);
14372 idvar
= get_tv_string_chk(&argvars
[2]);
14374 set_var(idvar
, &v
.di_tv
, FALSE
);
14375 vim_free(v
.di_tv
.vval
.v_string
);
14381 * "remote_expr()" function
14384 f_remote_expr(argvars
, rettv
)
14385 typval_T
*argvars UNUSED
;
14388 rettv
->v_type
= VAR_STRING
;
14389 rettv
->vval
.v_string
= NULL
;
14390 #ifdef FEAT_CLIENTSERVER
14391 remote_common(argvars
, rettv
, TRUE
);
14396 * "remote_foreground()" function
14399 f_remote_foreground(argvars
, rettv
)
14400 typval_T
*argvars UNUSED
;
14401 typval_T
*rettv UNUSED
;
14403 #ifdef FEAT_CLIENTSERVER
14405 /* On Win32 it's done in this application. */
14407 char_u
*server_name
= get_tv_string_chk(&argvars
[0]);
14409 if (server_name
!= NULL
)
14410 serverForeground(server_name
);
14413 /* Send a foreground() expression to the server. */
14414 argvars
[1].v_type
= VAR_STRING
;
14415 argvars
[1].vval
.v_string
= vim_strsave((char_u
*)"foreground()");
14416 argvars
[2].v_type
= VAR_UNKNOWN
;
14417 remote_common(argvars
, rettv
, TRUE
);
14418 vim_free(argvars
[1].vval
.v_string
);
14424 f_remote_peek(argvars
, rettv
)
14425 typval_T
*argvars UNUSED
;
14428 #ifdef FEAT_CLIENTSERVER
14436 if (check_restricted() || check_secure())
14438 rettv
->vval
.v_number
= -1;
14441 serverid
= get_tv_string_chk(&argvars
[0]);
14442 if (serverid
== NULL
)
14444 rettv
->vval
.v_number
= -1;
14445 return; /* type error; errmsg already given */
14448 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
14450 rettv
->vval
.v_number
= -1;
14453 s
= serverGetReply((HWND
)n
, FALSE
, FALSE
, FALSE
);
14454 rettv
->vval
.v_number
= (s
!= NULL
);
14457 if (check_connection() == FAIL
)
14460 rettv
->vval
.v_number
= serverPeekReply(X_DISPLAY
,
14461 serverStrToWin(serverid
), &s
);
14464 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& rettv
->vval
.v_number
> 0)
14468 v
.di_tv
.v_type
= VAR_STRING
;
14469 v
.di_tv
.vval
.v_string
= vim_strsave(s
);
14470 retvar
= get_tv_string_chk(&argvars
[1]);
14471 if (retvar
!= NULL
)
14472 set_var(retvar
, &v
.di_tv
, FALSE
);
14473 vim_free(v
.di_tv
.vval
.v_string
);
14476 rettv
->vval
.v_number
= -1;
14481 f_remote_read(argvars
, rettv
)
14482 typval_T
*argvars UNUSED
;
14487 #ifdef FEAT_CLIENTSERVER
14488 char_u
*serverid
= get_tv_string_chk(&argvars
[0]);
14490 if (serverid
!= NULL
&& !check_restricted() && !check_secure())
14493 /* The server's HWND is encoded in the 'id' parameter */
14496 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
14498 r
= serverGetReply((HWND
)n
, FALSE
, TRUE
, TRUE
);
14501 if (check_connection() == FAIL
|| serverReadReply(X_DISPLAY
,
14502 serverStrToWin(serverid
), &r
, FALSE
) < 0)
14504 EMSG(_("E277: Unable to read a server reply"));
14507 rettv
->v_type
= VAR_STRING
;
14508 rettv
->vval
.v_string
= r
;
14512 * "remote_send()" function
14515 f_remote_send(argvars
, rettv
)
14516 typval_T
*argvars UNUSED
;
14519 rettv
->v_type
= VAR_STRING
;
14520 rettv
->vval
.v_string
= NULL
;
14521 #ifdef FEAT_CLIENTSERVER
14522 remote_common(argvars
, rettv
, FALSE
);
14527 * "remove()" function
14530 f_remove(argvars
, rettv
)
14535 listitem_T
*item
, *item2
;
14543 if (argvars
[0].v_type
== VAR_DICT
)
14545 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14546 EMSG2(_(e_toomanyarg
), "remove()");
14547 else if ((d
= argvars
[0].vval
.v_dict
) != NULL
14548 && !tv_check_lock(d
->dv_lock
, (char_u
*)"remove() argument"))
14550 key
= get_tv_string_chk(&argvars
[1]);
14553 di
= dict_find(d
, key
, -1);
14555 EMSG2(_(e_dictkey
), key
);
14558 *rettv
= di
->di_tv
;
14559 init_tv(&di
->di_tv
);
14560 dictitem_remove(d
, di
);
14565 else if (argvars
[0].v_type
!= VAR_LIST
)
14566 EMSG2(_(e_listdictarg
), "remove()");
14567 else if ((l
= argvars
[0].vval
.v_list
) != NULL
14568 && !tv_check_lock(l
->lv_lock
, (char_u
*)"remove() argument"))
14572 idx
= get_tv_number_chk(&argvars
[1], &error
);
14574 ; /* type error: do nothing, errmsg already given */
14575 else if ((item
= list_find(l
, idx
)) == NULL
)
14576 EMSGN(_(e_listidx
), idx
);
14579 if (argvars
[2].v_type
== VAR_UNKNOWN
)
14581 /* Remove one item, return its value. */
14582 list_remove(l
, item
, item
);
14583 *rettv
= item
->li_tv
;
14588 /* Remove range of items, return list with values. */
14589 end
= get_tv_number_chk(&argvars
[2], &error
);
14591 ; /* type error: do nothing */
14592 else if ((item2
= list_find(l
, end
)) == NULL
)
14593 EMSGN(_(e_listidx
), end
);
14598 for (li
= item
; li
!= NULL
; li
= li
->li_next
)
14604 if (li
== NULL
) /* didn't find "item2" after "item" */
14605 EMSG(_(e_invrange
));
14608 list_remove(l
, item
, item2
);
14609 if (rettv_list_alloc(rettv
) == OK
)
14611 l
= rettv
->vval
.v_list
;
14612 l
->lv_first
= item
;
14613 l
->lv_last
= item2
;
14614 item
->li_prev
= NULL
;
14615 item2
->li_next
= NULL
;
14626 * "rename({from}, {to})" function
14629 f_rename(argvars
, rettv
)
14633 char_u buf
[NUMBUFLEN
];
14635 if (check_restricted() || check_secure())
14636 rettv
->vval
.v_number
= -1;
14638 rettv
->vval
.v_number
= vim_rename(get_tv_string(&argvars
[0]),
14639 get_tv_string_buf(&argvars
[1], buf
));
14643 * "repeat()" function
14646 f_repeat(argvars
, rettv
)
14657 n
= get_tv_number(&argvars
[1]);
14658 if (argvars
[0].v_type
== VAR_LIST
)
14660 if (rettv_list_alloc(rettv
) == OK
&& argvars
[0].vval
.v_list
!= NULL
)
14662 if (list_extend(rettv
->vval
.v_list
,
14663 argvars
[0].vval
.v_list
, NULL
) == FAIL
)
14668 p
= get_tv_string(&argvars
[0]);
14669 rettv
->v_type
= VAR_STRING
;
14670 rettv
->vval
.v_string
= NULL
;
14672 slen
= (int)STRLEN(p
);
14677 r
= alloc(len
+ 1);
14680 for (i
= 0; i
< n
; i
++)
14681 mch_memmove(r
+ i
* slen
, p
, (size_t)slen
);
14685 rettv
->vval
.v_string
= r
;
14690 * "resolve()" function
14693 f_resolve(argvars
, rettv
)
14699 p
= get_tv_string(&argvars
[0]);
14700 #ifdef FEAT_SHORTCUT
14704 v
= mch_resolve_shortcut(p
);
14706 rettv
->vval
.v_string
= v
;
14708 rettv
->vval
.v_string
= vim_strsave(p
);
14711 # ifdef HAVE_READLINK
14713 char_u buf
[MAXPATHL
+ 1];
14716 char_u
*remain
= NULL
;
14718 int is_relative_to_current
= FALSE
;
14719 int has_trailing_pathsep
= FALSE
;
14722 p
= vim_strsave(p
);
14724 if (p
[0] == '.' && (vim_ispathsep(p
[1])
14725 || (p
[1] == '.' && (vim_ispathsep(p
[2])))))
14726 is_relative_to_current
= TRUE
;
14729 if (len
> 0 && after_pathsep(p
, p
+ len
))
14730 has_trailing_pathsep
= TRUE
;
14732 q
= getnextcomp(p
);
14735 /* Separate the first path component in "p", and keep the
14736 * remainder (beginning with the path separator). */
14737 remain
= vim_strsave(q
- 1);
14745 len
= readlink((char *)p
, (char *)buf
, MAXPATHL
);
14754 EMSG(_("E655: Too many symbolic links (cycle?)"));
14755 rettv
->vval
.v_string
= NULL
;
14759 /* Ensure that the result will have a trailing path separator
14760 * if the argument has one. */
14761 if (remain
== NULL
&& has_trailing_pathsep
)
14764 /* Separate the first path component in the link value and
14765 * concatenate the remainders. */
14766 q
= getnextcomp(vim_ispathsep(*buf
) ? buf
+ 1 : buf
);
14769 if (remain
== NULL
)
14770 remain
= vim_strsave(q
- 1);
14773 cpy
= concat_str(q
- 1, remain
);
14784 if (q
> p
&& *q
== NUL
)
14786 /* Ignore trailing path separator. */
14790 if (q
> p
&& !mch_isFullName(buf
))
14792 /* symlink is relative to directory of argument */
14793 cpy
= alloc((unsigned)(STRLEN(p
) + STRLEN(buf
) + 1));
14797 STRCPY(gettail(cpy
), buf
);
14805 p
= vim_strsave(buf
);
14809 if (remain
== NULL
)
14812 /* Append the first path component of "remain" to "p". */
14813 q
= getnextcomp(remain
+ 1);
14814 len
= q
- remain
- (*q
!= NUL
);
14815 cpy
= vim_strnsave(p
, STRLEN(p
) + len
);
14818 STRNCAT(cpy
, remain
, len
);
14822 /* Shorten "remain". */
14824 STRMOVE(remain
, q
- 1);
14832 /* If the result is a relative path name, make it explicitly relative to
14833 * the current directory if and only if the argument had this form. */
14834 if (!vim_ispathsep(*p
))
14836 if (is_relative_to_current
14840 || vim_ispathsep(p
[1])
14843 || vim_ispathsep(p
[2]))))))
14845 /* Prepend "./". */
14846 cpy
= concat_str((char_u
*)"./", p
);
14853 else if (!is_relative_to_current
)
14855 /* Strip leading "./". */
14857 while (q
[0] == '.' && vim_ispathsep(q
[1]))
14864 /* Ensure that the result will have no trailing path separator
14865 * if the argument had none. But keep "/" or "//". */
14866 if (!has_trailing_pathsep
)
14869 if (after_pathsep(p
, q
))
14870 *gettail_sep(p
) = NUL
;
14873 rettv
->vval
.v_string
= p
;
14876 rettv
->vval
.v_string
= vim_strsave(p
);
14880 simplify_filename(rettv
->vval
.v_string
);
14882 #ifdef HAVE_READLINK
14885 rettv
->v_type
= VAR_STRING
;
14889 * "reverse({list})" function
14892 f_reverse(argvars
, rettv
)
14897 listitem_T
*li
, *ni
;
14899 if (argvars
[0].v_type
!= VAR_LIST
)
14900 EMSG2(_(e_listarg
), "reverse()");
14901 else if ((l
= argvars
[0].vval
.v_list
) != NULL
14902 && !tv_check_lock(l
->lv_lock
, (char_u
*)"reverse()"))
14905 l
->lv_first
= l
->lv_last
= NULL
;
14910 list_append(l
, li
);
14913 rettv
->vval
.v_list
= l
;
14914 rettv
->v_type
= VAR_LIST
;
14916 l
->lv_idx
= l
->lv_len
- l
->lv_idx
- 1;
14920 #define SP_NOMOVE 0x01 /* don't move cursor */
14921 #define SP_REPEAT 0x02 /* repeat to find outer pair */
14922 #define SP_RETCOUNT 0x04 /* return matchcount */
14923 #define SP_SETPCMARK 0x08 /* set previous context mark */
14924 #define SP_START 0x10 /* accept match at start position */
14925 #define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14926 #define SP_END 0x40 /* leave cursor at end of match */
14928 static int get_search_arg
__ARGS((typval_T
*varp
, int *flagsp
));
14931 * Get flags for a search function.
14932 * Possibly sets "p_ws".
14933 * Returns BACKWARD, FORWARD or zero (for an error).
14936 get_search_arg(varp
, flagsp
)
14942 char_u nbuf
[NUMBUFLEN
];
14945 if (varp
->v_type
!= VAR_UNKNOWN
)
14947 flags
= get_tv_string_buf_chk(varp
, nbuf
);
14949 return 0; /* type error; errmsg already given */
14950 while (*flags
!= NUL
)
14954 case 'b': dir
= BACKWARD
; break;
14955 case 'w': p_ws
= TRUE
; break;
14956 case 'W': p_ws
= FALSE
; break;
14958 if (flagsp
!= NULL
)
14961 case 'c': mask
= SP_START
; break;
14962 case 'e': mask
= SP_END
; break;
14963 case 'm': mask
= SP_RETCOUNT
; break;
14964 case 'n': mask
= SP_NOMOVE
; break;
14965 case 'p': mask
= SP_SUBPAT
; break;
14966 case 'r': mask
= SP_REPEAT
; break;
14967 case 's': mask
= SP_SETPCMARK
; break;
14971 EMSG2(_(e_invarg2
), flags
);
14986 * Shared by search() and searchpos() functions
14989 search_cmn(argvars
, match_pos
, flagsp
)
14998 int save_p_ws
= p_ws
;
15000 int retval
= 0; /* default: FAIL */
15001 long lnum_stop
= 0;
15003 #ifdef FEAT_RELTIME
15004 long time_limit
= 0;
15006 int options
= SEARCH_KEEP
;
15009 pat
= get_tv_string(&argvars
[0]);
15010 dir
= get_search_arg(&argvars
[1], flagsp
); /* may set p_ws */
15014 if (flags
& SP_START
)
15015 options
|= SEARCH_START
;
15016 if (flags
& SP_END
)
15017 options
|= SEARCH_END
;
15019 /* Optional arguments: line number to stop searching and timeout. */
15020 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
15022 lnum_stop
= get_tv_number_chk(&argvars
[2], NULL
);
15025 #ifdef FEAT_RELTIME
15026 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
15028 time_limit
= get_tv_number_chk(&argvars
[3], NULL
);
15029 if (time_limit
< 0)
15035 #ifdef FEAT_RELTIME
15036 /* Set the time limit, if there is one. */
15037 profile_setlimit(time_limit
, &tm
);
15041 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
15042 * Check to make sure only those flags are set.
15043 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15044 * flags cannot be set. Check for that condition also.
15046 if (((flags
& (SP_REPEAT
| SP_RETCOUNT
)) != 0)
15047 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
15049 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[1]));
15053 pos
= save_cursor
= curwin
->w_cursor
;
15054 subpatnum
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
15055 options
, RE_SEARCH
, (linenr_T
)lnum_stop
, &tm
);
15056 if (subpatnum
!= FAIL
)
15058 if (flags
& SP_SUBPAT
)
15059 retval
= subpatnum
;
15062 if (flags
& SP_SETPCMARK
)
15064 curwin
->w_cursor
= pos
;
15065 if (match_pos
!= NULL
)
15067 /* Store the match cursor position */
15068 match_pos
->lnum
= pos
.lnum
;
15069 match_pos
->col
= pos
.col
+ 1;
15071 /* "/$" will put the cursor after the end of the line, may need to
15072 * correct that here */
15076 /* If 'n' flag is used: restore cursor position. */
15077 if (flags
& SP_NOMOVE
)
15078 curwin
->w_cursor
= save_cursor
;
15080 curwin
->w_set_curswant
= TRUE
;
15089 * "round({float})" function
15092 f_round(argvars
, rettv
)
15098 rettv
->v_type
= VAR_FLOAT
;
15099 if (get_float_arg(argvars
, &f
) == OK
)
15100 /* round() is not in C90, use ceil() or floor() instead. */
15101 rettv
->vval
.v_float
= f
> 0 ? floor(f
+ 0.5) : ceil(f
- 0.5);
15103 rettv
->vval
.v_float
= 0.0;
15108 * "search()" function
15111 f_search(argvars
, rettv
)
15117 rettv
->vval
.v_number
= search_cmn(argvars
, NULL
, &flags
);
15121 * "searchdecl()" function
15124 f_searchdecl(argvars
, rettv
)
15133 rettv
->vval
.v_number
= 1; /* default: FAIL */
15135 name
= get_tv_string_chk(&argvars
[0]);
15136 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15138 locally
= get_tv_number_chk(&argvars
[1], &error
) == 0;
15139 if (!error
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
15140 thisblock
= get_tv_number_chk(&argvars
[2], &error
) != 0;
15142 if (!error
&& name
!= NULL
)
15143 rettv
->vval
.v_number
= find_decl(name
, (int)STRLEN(name
),
15144 locally
, thisblock
, SEARCH_KEEP
) == FAIL
;
15148 * Used by searchpair() and searchpairpos()
15151 searchpair_cmn(argvars
, match_pos
)
15155 char_u
*spat
, *mpat
, *epat
;
15157 int save_p_ws
= p_ws
;
15160 char_u nbuf1
[NUMBUFLEN
];
15161 char_u nbuf2
[NUMBUFLEN
];
15162 char_u nbuf3
[NUMBUFLEN
];
15163 int retval
= 0; /* default: FAIL */
15164 long lnum_stop
= 0;
15165 long time_limit
= 0;
15167 /* Get the three pattern arguments: start, middle, end. */
15168 spat
= get_tv_string_chk(&argvars
[0]);
15169 mpat
= get_tv_string_buf_chk(&argvars
[1], nbuf1
);
15170 epat
= get_tv_string_buf_chk(&argvars
[2], nbuf2
);
15171 if (spat
== NULL
|| mpat
== NULL
|| epat
== NULL
)
15172 goto theend
; /* type error */
15174 /* Handle the optional fourth argument: flags */
15175 dir
= get_search_arg(&argvars
[3], &flags
); /* may set p_ws */
15179 /* Don't accept SP_END or SP_SUBPAT.
15180 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15182 if ((flags
& (SP_END
| SP_SUBPAT
)) != 0
15183 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
15185 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[3]));
15189 /* Using 'r' implies 'W', otherwise it doesn't work. */
15190 if (flags
& SP_REPEAT
)
15193 /* Optional fifth argument: skip expression */
15194 if (argvars
[3].v_type
== VAR_UNKNOWN
15195 || argvars
[4].v_type
== VAR_UNKNOWN
)
15196 skip
= (char_u
*)"";
15199 skip
= get_tv_string_buf_chk(&argvars
[4], nbuf3
);
15200 if (argvars
[5].v_type
!= VAR_UNKNOWN
)
15202 lnum_stop
= get_tv_number_chk(&argvars
[5], NULL
);
15205 #ifdef FEAT_RELTIME
15206 if (argvars
[6].v_type
!= VAR_UNKNOWN
)
15208 time_limit
= get_tv_number_chk(&argvars
[6], NULL
);
15209 if (time_limit
< 0)
15216 goto theend
; /* type error */
15218 retval
= do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
,
15219 match_pos
, lnum_stop
, time_limit
);
15228 * "searchpair()" function
15231 f_searchpair(argvars
, rettv
)
15235 rettv
->vval
.v_number
= searchpair_cmn(argvars
, NULL
);
15239 * "searchpairpos()" function
15242 f_searchpairpos(argvars
, rettv
)
15250 if (rettv_list_alloc(rettv
) == FAIL
)
15253 if (searchpair_cmn(argvars
, &match_pos
) > 0)
15255 lnum
= match_pos
.lnum
;
15256 col
= match_pos
.col
;
15259 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
15260 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
15264 * Search for a start/middle/end thing.
15265 * Used by searchpair(), see its documentation for the details.
15266 * Returns 0 or -1 for no match,
15269 do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
, match_pos
,
15270 lnum_stop
, time_limit
)
15271 char_u
*spat
; /* start pattern */
15272 char_u
*mpat
; /* middle pattern */
15273 char_u
*epat
; /* end pattern */
15274 int dir
; /* BACKWARD or FORWARD */
15275 char_u
*skip
; /* skip expression */
15276 int flags
; /* SP_SETPCMARK and other SP_ values */
15278 linenr_T lnum_stop
; /* stop at this line if not zero */
15279 long time_limit
; /* stop after this many msec */
15282 char_u
*pat
, *pat2
= NULL
, *pat3
= NULL
;
15293 int options
= SEARCH_KEEP
;
15296 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15298 p_cpo
= empty_option
;
15300 #ifdef FEAT_RELTIME
15301 /* Set the time limit, if there is one. */
15302 profile_setlimit(time_limit
, &tm
);
15305 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15306 * start/middle/end (pat3, for the top pair). */
15307 pat2
= alloc((unsigned)(STRLEN(spat
) + STRLEN(epat
) + 15));
15308 pat3
= alloc((unsigned)(STRLEN(spat
) + STRLEN(mpat
) + STRLEN(epat
) + 23));
15309 if (pat2
== NULL
|| pat3
== NULL
)
15311 sprintf((char *)pat2
, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat
, epat
);
15313 STRCPY(pat3
, pat2
);
15315 sprintf((char *)pat3
, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15317 if (flags
& SP_START
)
15318 options
|= SEARCH_START
;
15320 save_cursor
= curwin
->w_cursor
;
15321 pos
= curwin
->w_cursor
;
15322 clearpos(&firstpos
);
15323 clearpos(&foundpos
);
15327 n
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
15328 options
, RE_SEARCH
, lnum_stop
, &tm
);
15329 if (n
== FAIL
|| (firstpos
.lnum
!= 0 && equalpos(pos
, firstpos
)))
15330 /* didn't find it or found the first match again: FAIL */
15333 if (firstpos
.lnum
== 0)
15335 if (equalpos(pos
, foundpos
))
15337 /* Found the same position again. Can happen with a pattern that
15338 * has "\zs" at the end and searching backwards. Advance one
15339 * character and try again. */
15340 if (dir
== BACKWARD
)
15347 /* clear the start flag to avoid getting stuck here */
15348 options
&= ~SEARCH_START
;
15350 /* If the skip pattern matches, ignore this match. */
15353 save_pos
= curwin
->w_cursor
;
15354 curwin
->w_cursor
= pos
;
15355 r
= eval_to_bool(skip
, &err
, NULL
, FALSE
);
15356 curwin
->w_cursor
= save_pos
;
15359 /* Evaluating {skip} caused an error, break here. */
15360 curwin
->w_cursor
= save_cursor
;
15368 if ((dir
== BACKWARD
&& n
== 3) || (dir
== FORWARD
&& n
== 2))
15370 /* Found end when searching backwards or start when searching
15371 * forward: nested pair. */
15373 pat
= pat2
; /* nested, don't search for middle */
15377 /* Found end when searching forward or start when searching
15378 * backward: end of (nested) pair; or found middle in outer pair. */
15380 pat
= pat3
; /* outer level, search for middle */
15385 /* Found the match: return matchcount or line number. */
15386 if (flags
& SP_RETCOUNT
)
15390 if (flags
& SP_SETPCMARK
)
15392 curwin
->w_cursor
= pos
;
15393 if (!(flags
& SP_REPEAT
))
15395 nest
= 1; /* search for next unmatched */
15399 if (match_pos
!= NULL
)
15401 /* Store the match cursor position */
15402 match_pos
->lnum
= curwin
->w_cursor
.lnum
;
15403 match_pos
->col
= curwin
->w_cursor
.col
+ 1;
15406 /* If 'n' flag is used or search failed: restore cursor position. */
15407 if ((flags
& SP_NOMOVE
) || retval
== 0)
15408 curwin
->w_cursor
= save_cursor
;
15413 if (p_cpo
== empty_option
)
15416 /* Darn, evaluating the {skip} expression changed the value. */
15417 free_string_option(save_cpo
);
15423 * "searchpos()" function
15426 f_searchpos(argvars
, rettv
)
15436 if (rettv_list_alloc(rettv
) == FAIL
)
15439 n
= search_cmn(argvars
, &match_pos
, &flags
);
15442 lnum
= match_pos
.lnum
;
15443 col
= match_pos
.col
;
15446 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
15447 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
15448 if (flags
& SP_SUBPAT
)
15449 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n
);
15454 f_server2client(argvars
, rettv
)
15455 typval_T
*argvars UNUSED
;
15458 #ifdef FEAT_CLIENTSERVER
15459 char_u buf
[NUMBUFLEN
];
15460 char_u
*server
= get_tv_string_chk(&argvars
[0]);
15461 char_u
*reply
= get_tv_string_buf_chk(&argvars
[1], buf
);
15463 rettv
->vval
.v_number
= -1;
15464 if (server
== NULL
|| reply
== NULL
)
15466 if (check_restricted() || check_secure())
15469 if (check_connection() == FAIL
)
15473 if (serverSendReply(server
, reply
) < 0)
15475 EMSG(_("E258: Unable to send to client"));
15478 rettv
->vval
.v_number
= 0;
15480 rettv
->vval
.v_number
= -1;
15485 f_serverlist(argvars
, rettv
)
15486 typval_T
*argvars UNUSED
;
15491 #ifdef FEAT_CLIENTSERVER
15493 r
= serverGetVimNames();
15496 if (X_DISPLAY
!= NULL
)
15497 r
= serverGetVimNames(X_DISPLAY
);
15500 rettv
->v_type
= VAR_STRING
;
15501 rettv
->vval
.v_string
= r
;
15505 * "setbufvar()" function
15508 f_setbufvar(argvars
, rettv
)
15510 typval_T
*rettv UNUSED
;
15514 char_u
*varname
, *bufvarname
;
15516 char_u nbuf
[NUMBUFLEN
];
15518 if (check_restricted() || check_secure())
15520 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
15521 varname
= get_tv_string_chk(&argvars
[1]);
15522 buf
= get_buf_tv(&argvars
[0]);
15523 varp
= &argvars
[2];
15525 if (buf
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
15527 /* set curbuf to be our buf, temporarily */
15528 aucmd_prepbuf(&aco
, buf
);
15530 if (*varname
== '&')
15537 numval
= get_tv_number_chk(varp
, &error
);
15538 strval
= get_tv_string_buf_chk(varp
, nbuf
);
15539 if (!error
&& strval
!= NULL
)
15540 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
15544 bufvarname
= alloc((unsigned)STRLEN(varname
) + 3);
15545 if (bufvarname
!= NULL
)
15547 STRCPY(bufvarname
, "b:");
15548 STRCPY(bufvarname
+ 2, varname
);
15549 set_var(bufvarname
, varp
, TRUE
);
15550 vim_free(bufvarname
);
15554 /* reset notion of buffer */
15555 aucmd_restbuf(&aco
);
15560 * "setcmdpos()" function
15563 f_setcmdpos(argvars
, rettv
)
15567 int pos
= (int)get_tv_number(&argvars
[0]) - 1;
15570 rettv
->vval
.v_number
= set_cmdline_pos(pos
);
15574 * "setline()" function
15577 f_setline(argvars
, rettv
)
15582 char_u
*line
= NULL
;
15584 listitem_T
*li
= NULL
;
15586 linenr_T lcount
= curbuf
->b_ml
.ml_line_count
;
15588 lnum
= get_tv_lnum(&argvars
[0]);
15589 if (argvars
[1].v_type
== VAR_LIST
)
15591 l
= argvars
[1].vval
.v_list
;
15595 line
= get_tv_string_chk(&argvars
[1]);
15597 /* default result is zero == OK */
15602 /* list argument, get next string */
15605 line
= get_tv_string_chk(&li
->li_tv
);
15609 rettv
->vval
.v_number
= 1; /* FAIL */
15610 if (line
== NULL
|| lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
15612 if (lnum
<= curbuf
->b_ml
.ml_line_count
)
15614 /* existing line, replace it */
15615 if (u_savesub(lnum
) == OK
&& ml_replace(lnum
, line
, TRUE
) == OK
)
15617 changed_bytes(lnum
, 0);
15618 if (lnum
== curwin
->w_cursor
.lnum
)
15619 check_cursor_col();
15620 rettv
->vval
.v_number
= 0; /* OK */
15623 else if (added
> 0 || u_save(lnum
- 1, lnum
) == OK
)
15625 /* lnum is one past the last line, append the line */
15627 if (ml_append(lnum
- 1, line
, (colnr_T
)0, FALSE
) == OK
)
15628 rettv
->vval
.v_number
= 0; /* OK */
15631 if (l
== NULL
) /* only one string argument */
15637 appended_lines_mark(lcount
, added
);
15640 static void set_qf_ll_list
__ARGS((win_T
*wp
, typval_T
*list_arg
, typval_T
*action_arg
, typval_T
*rettv
));
15643 * Used by "setqflist()" and "setloclist()" functions
15646 set_qf_ll_list(wp
, list_arg
, action_arg
, rettv
)
15648 typval_T
*list_arg UNUSED
;
15649 typval_T
*action_arg UNUSED
;
15652 #ifdef FEAT_QUICKFIX
15657 rettv
->vval
.v_number
= -1;
15659 #ifdef FEAT_QUICKFIX
15660 if (list_arg
->v_type
!= VAR_LIST
)
15661 EMSG(_(e_listreq
));
15664 list_T
*l
= list_arg
->vval
.v_list
;
15666 if (action_arg
->v_type
== VAR_STRING
)
15668 act
= get_tv_string_chk(action_arg
);
15670 return; /* type error; errmsg already given */
15671 if (*act
== 'a' || *act
== 'r')
15675 if (l
!= NULL
&& set_errorlist(wp
, l
, action
) == OK
)
15676 rettv
->vval
.v_number
= 0;
15682 * "setloclist()" function
15685 f_setloclist(argvars
, rettv
)
15691 rettv
->vval
.v_number
= -1;
15693 win
= find_win_by_nr(&argvars
[0], NULL
);
15695 set_qf_ll_list(win
, &argvars
[1], &argvars
[2], rettv
);
15699 * "setmatches()" function
15702 f_setmatches(argvars
, rettv
)
15706 #ifdef FEAT_SEARCH_EXTRA
15711 rettv
->vval
.v_number
= -1;
15712 if (argvars
[0].v_type
!= VAR_LIST
)
15714 EMSG(_(e_listreq
));
15717 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
15720 /* To some extent make sure that we are dealing with a list from
15721 * "getmatches()". */
15725 if (li
->li_tv
.v_type
!= VAR_DICT
15726 || (d
= li
->li_tv
.vval
.v_dict
) == NULL
)
15731 if (!(dict_find(d
, (char_u
*)"group", -1) != NULL
15732 && dict_find(d
, (char_u
*)"pattern", -1) != NULL
15733 && dict_find(d
, (char_u
*)"priority", -1) != NULL
15734 && dict_find(d
, (char_u
*)"id", -1) != NULL
))
15742 clear_matches(curwin
);
15746 d
= li
->li_tv
.vval
.v_dict
;
15747 match_add(curwin
, get_dict_string(d
, (char_u
*)"group", FALSE
),
15748 get_dict_string(d
, (char_u
*)"pattern", FALSE
),
15749 (int)get_dict_number(d
, (char_u
*)"priority"),
15750 (int)get_dict_number(d
, (char_u
*)"id"));
15753 rettv
->vval
.v_number
= 0;
15759 * "setpos()" function
15762 f_setpos(argvars
, rettv
)
15770 rettv
->vval
.v_number
= -1;
15771 name
= get_tv_string_chk(argvars
);
15774 if (list2fpos(&argvars
[1], &pos
, &fnum
) == OK
)
15778 if (name
[0] == '.' && name
[1] == NUL
)
15781 if (fnum
== curbuf
->b_fnum
)
15783 curwin
->w_cursor
= pos
;
15785 rettv
->vval
.v_number
= 0;
15790 else if (name
[0] == '\'' && name
[1] != NUL
&& name
[2] == NUL
)
15793 if (setmark_pos(name
[1], &pos
, fnum
) == OK
)
15794 rettv
->vval
.v_number
= 0;
15803 * "setqflist()" function
15806 f_setqflist(argvars
, rettv
)
15810 set_qf_ll_list(NULL
, &argvars
[0], &argvars
[1], rettv
);
15814 * "setreg()" function
15817 f_setreg(argvars
, rettv
)
15822 char_u
*strregname
;
15833 strregname
= get_tv_string_chk(argvars
);
15834 rettv
->vval
.v_number
= 1; /* FAIL is default */
15836 if (strregname
== NULL
)
15837 return; /* type error; errmsg already given */
15838 regname
= *strregname
;
15839 if (regname
== 0 || regname
== '@')
15841 else if (regname
== '=')
15844 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15846 stropt
= get_tv_string_chk(&argvars
[2]);
15847 if (stropt
== NULL
)
15848 return; /* type error */
15849 for (; *stropt
!= NUL
; ++stropt
)
15852 case 'a': case 'A': /* append */
15855 case 'v': case 'c': /* character-wise selection */
15858 case 'V': case 'l': /* line-wise selection */
15862 case 'b': case Ctrl_V
: /* block-wise selection */
15863 yank_type
= MBLOCK
;
15864 if (VIM_ISDIGIT(stropt
[1]))
15867 block_len
= getdigits(&stropt
) - 1;
15875 strval
= get_tv_string_chk(&argvars
[1]);
15876 if (strval
!= NULL
)
15877 write_reg_contents_ex(regname
, strval
, -1,
15878 append
, yank_type
, block_len
);
15879 rettv
->vval
.v_number
= 0;
15883 * "settabwinvar()" function
15886 f_settabwinvar(argvars
, rettv
)
15890 setwinvar(argvars
, rettv
, 1);
15894 * "setwinvar()" function
15897 f_setwinvar(argvars
, rettv
)
15901 setwinvar(argvars
, rettv
, 0);
15905 * "setwinvar()" and "settabwinvar()" functions
15908 setwinvar(argvars
, rettv
, off
)
15910 typval_T
*rettv UNUSED
;
15914 #ifdef FEAT_WINDOWS
15915 win_T
*save_curwin
;
15916 tabpage_T
*save_curtab
;
15918 char_u
*varname
, *winvarname
;
15920 char_u nbuf
[NUMBUFLEN
];
15923 if (check_restricted() || check_secure())
15926 #ifdef FEAT_WINDOWS
15928 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
15932 win
= find_win_by_nr(&argvars
[off
], tp
);
15933 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
15934 varp
= &argvars
[off
+ 2];
15936 if (win
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
15938 #ifdef FEAT_WINDOWS
15939 /* set curwin to be our win, temporarily */
15940 save_curwin
= curwin
;
15941 save_curtab
= curtab
;
15942 goto_tabpage_tp(tp
);
15943 if (!win_valid(win
))
15946 curbuf
= curwin
->w_buffer
;
15949 if (*varname
== '&')
15956 numval
= get_tv_number_chk(varp
, &error
);
15957 strval
= get_tv_string_buf_chk(varp
, nbuf
);
15958 if (!error
&& strval
!= NULL
)
15959 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
15963 winvarname
= alloc((unsigned)STRLEN(varname
) + 3);
15964 if (winvarname
!= NULL
)
15966 STRCPY(winvarname
, "w:");
15967 STRCPY(winvarname
+ 2, varname
);
15968 set_var(winvarname
, varp
, TRUE
);
15969 vim_free(winvarname
);
15973 #ifdef FEAT_WINDOWS
15974 /* Restore current tabpage and window, if still valid (autocomands can
15975 * make them invalid). */
15976 if (valid_tabpage(save_curtab
))
15977 goto_tabpage_tp(save_curtab
);
15978 if (win_valid(save_curwin
))
15980 curwin
= save_curwin
;
15981 curbuf
= curwin
->w_buffer
;
15988 * "shellescape({string})" function
15991 f_shellescape(argvars
, rettv
)
15995 rettv
->vval
.v_string
= vim_strsave_shellescape(
15996 get_tv_string(&argvars
[0]), non_zero_arg(&argvars
[1]));
15997 rettv
->v_type
= VAR_STRING
;
16001 * "simplify()" function
16004 f_simplify(argvars
, rettv
)
16010 p
= get_tv_string(&argvars
[0]);
16011 rettv
->vval
.v_string
= vim_strsave(p
);
16012 simplify_filename(rettv
->vval
.v_string
); /* simplify in place */
16013 rettv
->v_type
= VAR_STRING
;
16021 f_sin(argvars
, rettv
)
16027 rettv
->v_type
= VAR_FLOAT
;
16028 if (get_float_arg(argvars
, &f
) == OK
)
16029 rettv
->vval
.v_float
= sin(f
);
16031 rettv
->vval
.v_float
= 0.0;
16036 #ifdef __BORLANDC__
16039 item_compare
__ARGS((const void *s1
, const void *s2
));
16041 #ifdef __BORLANDC__
16044 item_compare2
__ARGS((const void *s1
, const void *s2
));
16046 static int item_compare_ic
;
16047 static char_u
*item_compare_func
;
16048 static int item_compare_func_err
;
16049 #define ITEM_COMPARE_FAIL 999
16052 * Compare functions for f_sort() below.
16055 #ifdef __BORLANDC__
16058 item_compare(s1
, s2
)
16063 char_u
*tofree1
, *tofree2
;
16065 char_u numbuf1
[NUMBUFLEN
];
16066 char_u numbuf2
[NUMBUFLEN
];
16068 p1
= tv2string(&(*(listitem_T
**)s1
)->li_tv
, &tofree1
, numbuf1
, 0);
16069 p2
= tv2string(&(*(listitem_T
**)s2
)->li_tv
, &tofree2
, numbuf2
, 0);
16074 if (item_compare_ic
)
16075 res
= STRICMP(p1
, p2
);
16077 res
= STRCMP(p1
, p2
);
16084 #ifdef __BORLANDC__
16087 item_compare2(s1
, s2
)
16096 /* shortcut after failure in previous call; compare all items equal */
16097 if (item_compare_func_err
)
16100 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16101 * in the copy without changing the original list items. */
16102 copy_tv(&(*(listitem_T
**)s1
)->li_tv
, &argv
[0]);
16103 copy_tv(&(*(listitem_T
**)s2
)->li_tv
, &argv
[1]);
16105 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
16106 res
= call_func(item_compare_func
, (int)STRLEN(item_compare_func
),
16107 &rettv
, 2, argv
, 0L, 0L, &dummy
, TRUE
, NULL
);
16108 clear_tv(&argv
[0]);
16109 clear_tv(&argv
[1]);
16112 res
= ITEM_COMPARE_FAIL
;
16114 res
= get_tv_number_chk(&rettv
, &item_compare_func_err
);
16115 if (item_compare_func_err
)
16116 res
= ITEM_COMPARE_FAIL
; /* return value has wrong type */
16122 * "sort({list})" function
16125 f_sort(argvars
, rettv
)
16135 if (argvars
[0].v_type
!= VAR_LIST
)
16136 EMSG2(_(e_listarg
), "sort()");
16139 l
= argvars
[0].vval
.v_list
;
16140 if (l
== NULL
|| tv_check_lock(l
->lv_lock
, (char_u
*)"sort()"))
16142 rettv
->vval
.v_list
= l
;
16143 rettv
->v_type
= VAR_LIST
;
16148 return; /* short list sorts pretty quickly */
16150 item_compare_ic
= FALSE
;
16151 item_compare_func
= NULL
;
16152 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16154 if (argvars
[1].v_type
== VAR_FUNC
)
16155 item_compare_func
= argvars
[1].vval
.v_string
;
16160 i
= get_tv_number_chk(&argvars
[1], &error
);
16162 return; /* type error; errmsg already given */
16164 item_compare_ic
= TRUE
;
16166 item_compare_func
= get_tv_string(&argvars
[1]);
16170 /* Make an array with each entry pointing to an item in the List. */
16171 ptrs
= (listitem_T
**)alloc((int)(len
* sizeof(listitem_T
*)));
16175 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
16178 item_compare_func_err
= FALSE
;
16179 /* test the compare function */
16180 if (item_compare_func
!= NULL
16181 && item_compare2((void *)&ptrs
[0], (void *)&ptrs
[1])
16182 == ITEM_COMPARE_FAIL
)
16183 EMSG(_("E702: Sort compare function failed"));
16186 /* Sort the array with item pointers. */
16187 qsort((void *)ptrs
, (size_t)len
, sizeof(listitem_T
*),
16188 item_compare_func
== NULL
? item_compare
: item_compare2
);
16190 if (!item_compare_func_err
)
16192 /* Clear the List and append the items in the sorted order. */
16193 l
->lv_first
= l
->lv_last
= l
->lv_idx_item
= NULL
;
16195 for (i
= 0; i
< len
; ++i
)
16196 list_append(l
, ptrs
[i
]);
16205 * "soundfold({word})" function
16208 f_soundfold(argvars
, rettv
)
16214 rettv
->v_type
= VAR_STRING
;
16215 s
= get_tv_string(&argvars
[0]);
16217 rettv
->vval
.v_string
= eval_soundfold(s
);
16219 rettv
->vval
.v_string
= vim_strsave(s
);
16224 * "spellbadword()" function
16227 f_spellbadword(argvars
, rettv
)
16228 typval_T
*argvars UNUSED
;
16231 char_u
*word
= (char_u
*)"";
16232 hlf_T attr
= HLF_COUNT
;
16235 if (rettv_list_alloc(rettv
) == FAIL
)
16239 if (argvars
[0].v_type
== VAR_UNKNOWN
)
16241 /* Find the start and length of the badly spelled word. */
16242 len
= spell_move_to(curwin
, FORWARD
, TRUE
, TRUE
, &attr
);
16244 word
= ml_get_cursor();
16246 else if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
16248 char_u
*str
= get_tv_string_chk(&argvars
[0]);
16253 /* Check the argument for spelling. */
16254 while (*str
!= NUL
)
16256 len
= spell_check(curwin
, str
, &attr
, &capcol
, FALSE
);
16257 if (attr
!= HLF_COUNT
)
16268 list_append_string(rettv
->vval
.v_list
, word
, len
);
16269 list_append_string(rettv
->vval
.v_list
, (char_u
*)(
16270 attr
== HLF_SPB
? "bad" :
16271 attr
== HLF_SPR
? "rare" :
16272 attr
== HLF_SPL
? "local" :
16273 attr
== HLF_SPC
? "caps" :
16278 * "spellsuggest()" function
16281 f_spellsuggest(argvars
, rettv
)
16282 typval_T
*argvars UNUSED
;
16287 int typeerr
= FALSE
;
16292 int need_capital
= FALSE
;
16295 if (rettv_list_alloc(rettv
) == FAIL
)
16299 if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
16301 str
= get_tv_string(&argvars
[0]);
16302 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16304 maxcount
= get_tv_number_chk(&argvars
[1], &typeerr
);
16307 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16309 need_capital
= get_tv_number_chk(&argvars
[2], &typeerr
);
16317 spell_suggest_list(&ga
, str
, maxcount
, need_capital
, FALSE
);
16319 for (i
= 0; i
< ga
.ga_len
; ++i
)
16321 str
= ((char_u
**)ga
.ga_data
)[i
];
16323 li
= listitem_alloc();
16328 li
->li_tv
.v_type
= VAR_STRING
;
16329 li
->li_tv
.v_lock
= 0;
16330 li
->li_tv
.vval
.v_string
= str
;
16331 list_append(rettv
->vval
.v_list
, li
);
16340 f_split(argvars
, rettv
)
16346 char_u
*pat
= NULL
;
16347 regmatch_T regmatch
;
16348 char_u patbuf
[NUMBUFLEN
];
16352 int keepempty
= FALSE
;
16353 int typeerr
= FALSE
;
16355 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16357 p_cpo
= (char_u
*)"";
16359 str
= get_tv_string(&argvars
[0]);
16360 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16362 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
16365 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16366 keepempty
= get_tv_number_chk(&argvars
[2], &typeerr
);
16368 if (pat
== NULL
|| *pat
== NUL
)
16369 pat
= (char_u
*)"[\\x01- ]\\+";
16371 if (rettv_list_alloc(rettv
) == FAIL
)
16376 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
16377 if (regmatch
.regprog
!= NULL
)
16379 regmatch
.rm_ic
= FALSE
;
16380 while (*str
!= NUL
|| keepempty
)
16383 match
= FALSE
; /* empty item at the end */
16385 match
= vim_regexec_nl(®match
, str
, col
);
16387 end
= regmatch
.startp
[0];
16389 end
= str
+ STRLEN(str
);
16390 if (keepempty
|| end
> str
|| (rettv
->vval
.v_list
->lv_len
> 0
16391 && *str
!= NUL
&& match
&& end
< regmatch
.endp
[0]))
16393 if (list_append_string(rettv
->vval
.v_list
, str
,
16394 (int)(end
- str
)) == FAIL
)
16399 /* Advance to just after the match. */
16400 if (regmatch
.endp
[0] > str
)
16404 /* Don't get stuck at the same match. */
16406 col
= (*mb_ptr2len
)(regmatch
.endp
[0]);
16411 str
= regmatch
.endp
[0];
16414 vim_free(regmatch
.regprog
);
16422 * "sqrt()" function
16425 f_sqrt(argvars
, rettv
)
16431 rettv
->v_type
= VAR_FLOAT
;
16432 if (get_float_arg(argvars
, &f
) == OK
)
16433 rettv
->vval
.v_float
= sqrt(f
);
16435 rettv
->vval
.v_float
= 0.0;
16439 * "str2float()" function
16442 f_str2float(argvars
, rettv
)
16446 char_u
*p
= skipwhite(get_tv_string(&argvars
[0]));
16449 p
= skipwhite(p
+ 1);
16450 (void)string2float(p
, &rettv
->vval
.v_float
);
16451 rettv
->v_type
= VAR_FLOAT
;
16456 * "str2nr()" function
16459 f_str2nr(argvars
, rettv
)
16467 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16469 base
= get_tv_number(&argvars
[1]);
16470 if (base
!= 8 && base
!= 10 && base
!= 16)
16477 p
= skipwhite(get_tv_string(&argvars
[0]));
16479 p
= skipwhite(p
+ 1);
16480 vim_str2nr(p
, NULL
, NULL
, base
== 8 ? 2 : 0, base
== 16 ? 2 : 0, &n
, NULL
);
16481 rettv
->vval
.v_number
= n
;
16484 #ifdef HAVE_STRFTIME
16486 * "strftime({format}[, {time}])" function
16489 f_strftime(argvars
, rettv
)
16493 char_u result_buf
[256];
16494 struct tm
*curtime
;
16498 rettv
->v_type
= VAR_STRING
;
16500 p
= get_tv_string(&argvars
[0]);
16501 if (argvars
[1].v_type
== VAR_UNKNOWN
)
16502 seconds
= time(NULL
);
16504 seconds
= (time_t)get_tv_number(&argvars
[1]);
16505 curtime
= localtime(&seconds
);
16506 /* MSVC returns NULL for an invalid value of seconds. */
16507 if (curtime
== NULL
)
16508 rettv
->vval
.v_string
= vim_strsave((char_u
*)_("(Invalid)"));
16515 conv
.vc_type
= CONV_NONE
;
16516 enc
= enc_locale();
16517 convert_setup(&conv
, p_enc
, enc
);
16518 if (conv
.vc_type
!= CONV_NONE
)
16519 p
= string_convert(&conv
, p
, NULL
);
16522 (void)strftime((char *)result_buf
, sizeof(result_buf
),
16523 (char *)p
, curtime
);
16525 result_buf
[0] = NUL
;
16528 if (conv
.vc_type
!= CONV_NONE
)
16530 convert_setup(&conv
, enc
, p_enc
);
16531 if (conv
.vc_type
!= CONV_NONE
)
16532 rettv
->vval
.v_string
= string_convert(&conv
, result_buf
, NULL
);
16535 rettv
->vval
.v_string
= vim_strsave(result_buf
);
16538 /* Release conversion descriptors */
16539 convert_setup(&conv
, NULL
, NULL
);
16547 * "stridx()" function
16550 f_stridx(argvars
, rettv
)
16554 char_u buf
[NUMBUFLEN
];
16557 char_u
*save_haystack
;
16561 needle
= get_tv_string_chk(&argvars
[1]);
16562 save_haystack
= haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
16563 rettv
->vval
.v_number
= -1;
16564 if (needle
== NULL
|| haystack
== NULL
)
16565 return; /* type error; errmsg already given */
16567 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16571 start_idx
= get_tv_number_chk(&argvars
[2], &error
);
16572 if (error
|| start_idx
>= (int)STRLEN(haystack
))
16574 if (start_idx
>= 0)
16575 haystack
+= start_idx
;
16578 pos
= (char_u
*)strstr((char *)haystack
, (char *)needle
);
16580 rettv
->vval
.v_number
= (varnumber_T
)(pos
- save_haystack
);
16584 * "string()" function
16587 f_string(argvars
, rettv
)
16592 char_u numbuf
[NUMBUFLEN
];
16594 rettv
->v_type
= VAR_STRING
;
16595 rettv
->vval
.v_string
= tv2string(&argvars
[0], &tofree
, numbuf
, 0);
16596 /* Make a copy if we have a value but it's not in allocated memory. */
16597 if (rettv
->vval
.v_string
!= NULL
&& tofree
== NULL
)
16598 rettv
->vval
.v_string
= vim_strsave(rettv
->vval
.v_string
);
16602 * "strlen()" function
16605 f_strlen(argvars
, rettv
)
16609 rettv
->vval
.v_number
= (varnumber_T
)(STRLEN(
16610 get_tv_string(&argvars
[0])));
16614 * "strpart()" function
16617 f_strpart(argvars
, rettv
)
16627 p
= get_tv_string(&argvars
[0]);
16628 slen
= (int)STRLEN(p
);
16630 n
= get_tv_number_chk(&argvars
[1], &error
);
16633 else if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16634 len
= get_tv_number(&argvars
[2]);
16636 len
= slen
- n
; /* default len: all bytes that are available. */
16639 * Only return the overlap between the specified part and the actual
16651 else if (n
+ len
> slen
)
16654 rettv
->v_type
= VAR_STRING
;
16655 rettv
->vval
.v_string
= vim_strnsave(p
+ n
, len
);
16659 * "strridx()" function
16662 f_strridx(argvars
, rettv
)
16666 char_u buf
[NUMBUFLEN
];
16670 char_u
*lastmatch
= NULL
;
16671 int haystack_len
, end_idx
;
16673 needle
= get_tv_string_chk(&argvars
[1]);
16674 haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
16676 rettv
->vval
.v_number
= -1;
16677 if (needle
== NULL
|| haystack
== NULL
)
16678 return; /* type error; errmsg already given */
16680 haystack_len
= (int)STRLEN(haystack
);
16681 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16683 /* Third argument: upper limit for index */
16684 end_idx
= get_tv_number_chk(&argvars
[2], NULL
);
16686 return; /* can never find a match */
16689 end_idx
= haystack_len
;
16691 if (*needle
== NUL
)
16693 /* Empty string matches past the end. */
16694 lastmatch
= haystack
+ end_idx
;
16698 for (rest
= haystack
; *rest
!= '\0'; ++rest
)
16700 rest
= (char_u
*)strstr((char *)rest
, (char *)needle
);
16701 if (rest
== NULL
|| rest
> haystack
+ end_idx
)
16707 if (lastmatch
== NULL
)
16708 rettv
->vval
.v_number
= -1;
16710 rettv
->vval
.v_number
= (varnumber_T
)(lastmatch
- haystack
);
16714 * "strtrans()" function
16717 f_strtrans(argvars
, rettv
)
16721 rettv
->v_type
= VAR_STRING
;
16722 rettv
->vval
.v_string
= transstr(get_tv_string(&argvars
[0]));
16726 * "submatch()" function
16729 f_submatch(argvars
, rettv
)
16733 rettv
->v_type
= VAR_STRING
;
16734 rettv
->vval
.v_string
=
16735 reg_submatch((int)get_tv_number_chk(&argvars
[0], NULL
));
16739 * "substitute()" function
16742 f_substitute(argvars
, rettv
)
16746 char_u patbuf
[NUMBUFLEN
];
16747 char_u subbuf
[NUMBUFLEN
];
16748 char_u flagsbuf
[NUMBUFLEN
];
16750 char_u
*str
= get_tv_string_chk(&argvars
[0]);
16751 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
16752 char_u
*sub
= get_tv_string_buf_chk(&argvars
[2], subbuf
);
16753 char_u
*flg
= get_tv_string_buf_chk(&argvars
[3], flagsbuf
);
16755 rettv
->v_type
= VAR_STRING
;
16756 if (str
== NULL
|| pat
== NULL
|| sub
== NULL
|| flg
== NULL
)
16757 rettv
->vval
.v_string
= NULL
;
16759 rettv
->vval
.v_string
= do_string_sub(str
, pat
, sub
, flg
);
16763 * "synID(lnum, col, trans)" function
16766 f_synID(argvars
, rettv
)
16767 typval_T
*argvars UNUSED
;
16775 int transerr
= FALSE
;
16777 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
16778 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
16779 trans
= get_tv_number_chk(&argvars
[2], &transerr
);
16781 if (!transerr
&& lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
16782 && col
>= 0 && col
< (long)STRLEN(ml_get(lnum
)))
16783 id
= syn_get_id(curwin
, lnum
, (colnr_T
)col
, trans
, NULL
, FALSE
);
16786 rettv
->vval
.v_number
= id
;
16790 * "synIDattr(id, what [, mode])" function
16793 f_synIDattr(argvars
, rettv
)
16794 typval_T
*argvars UNUSED
;
16802 char_u modebuf
[NUMBUFLEN
];
16805 id
= get_tv_number(&argvars
[0]);
16806 what
= get_tv_string(&argvars
[1]);
16807 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16809 mode
= get_tv_string_buf(&argvars
[2], modebuf
);
16810 modec
= TOLOWER_ASC(mode
[0]);
16811 if (modec
!= 't' && modec
!= 'c'
16816 modec
= 0; /* replace invalid with current */
16832 switch (TOLOWER_ASC(what
[0]))
16835 if (TOLOWER_ASC(what
[1]) == 'g') /* bg[#] */
16836 p
= highlight_color(id
, what
, modec
);
16838 p
= highlight_has_attr(id
, HL_BOLD
, modec
);
16841 case 'f': /* fg[#] or font */
16842 p
= highlight_color(id
, what
, modec
);
16846 if (TOLOWER_ASC(what
[1]) == 'n') /* inverse */
16847 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
16849 p
= highlight_has_attr(id
, HL_ITALIC
, modec
);
16852 case 'n': /* name */
16853 p
= get_highlight_name(NULL
, id
- 1);
16856 case 'r': /* reverse */
16857 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
16861 if (TOLOWER_ASC(what
[1]) == 'p') /* sp[#] */
16862 p
= highlight_color(id
, what
, modec
);
16863 else /* standout */
16864 p
= highlight_has_attr(id
, HL_STANDOUT
, modec
);
16868 if (STRLEN(what
) <= 5 || TOLOWER_ASC(what
[5]) != 'c')
16870 p
= highlight_has_attr(id
, HL_UNDERLINE
, modec
);
16873 p
= highlight_has_attr(id
, HL_UNDERCURL
, modec
);
16878 p
= vim_strsave(p
);
16880 rettv
->v_type
= VAR_STRING
;
16881 rettv
->vval
.v_string
= p
;
16885 * "synIDtrans(id)" function
16888 f_synIDtrans(argvars
, rettv
)
16889 typval_T
*argvars UNUSED
;
16895 id
= get_tv_number(&argvars
[0]);
16898 id
= syn_get_final_id(id
);
16903 rettv
->vval
.v_number
= id
;
16907 * "synstack(lnum, col)" function
16910 f_synstack(argvars
, rettv
)
16911 typval_T
*argvars UNUSED
;
16921 rettv
->v_type
= VAR_LIST
;
16922 rettv
->vval
.v_list
= NULL
;
16925 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
16926 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
16928 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
16929 && col
>= 0 && (col
== 0 || col
< (long)STRLEN(ml_get(lnum
)))
16930 && rettv_list_alloc(rettv
) != FAIL
)
16932 (void)syn_get_id(curwin
, lnum
, (colnr_T
)col
, FALSE
, NULL
, TRUE
);
16935 id
= syn_get_stack_item(i
);
16938 if (list_append_number(rettv
->vval
.v_list
, id
) == FAIL
)
16946 * "system()" function
16949 f_system(argvars
, rettv
)
16953 char_u
*res
= NULL
;
16955 char_u
*infile
= NULL
;
16956 char_u buf
[NUMBUFLEN
];
16960 if (check_restricted() || check_secure())
16963 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16966 * Write the string to a temp file, to be used for input of the shell
16969 if ((infile
= vim_tempname('i')) == NULL
)
16975 fd
= mch_fopen((char *)infile
, WRITEBIN
);
16978 EMSG2(_(e_notopen
), infile
);
16981 p
= get_tv_string_buf_chk(&argvars
[1], buf
);
16985 goto done
; /* type error; errmsg already given */
16987 if (fwrite(p
, STRLEN(p
), 1, fd
) != 1)
16989 if (fclose(fd
) != 0)
16993 EMSG(_("E677: Error writing temp file"));
16998 res
= get_cmd_output(get_tv_string(&argvars
[0]), infile
,
16999 SHELL_SILENT
| SHELL_COOKED
);
17002 /* translate <CR> into <NL> */
17007 for (s
= res
; *s
; ++s
)
17015 /* translate <CR><NL> into <NL> */
17021 for (s
= res
; *s
; ++s
)
17023 if (s
[0] == CAR
&& s
[1] == NL
)
17033 if (infile
!= NULL
)
17035 mch_remove(infile
);
17038 rettv
->v_type
= VAR_STRING
;
17039 rettv
->vval
.v_string
= res
;
17043 * "tabpagebuflist()" function
17046 f_tabpagebuflist(argvars
, rettv
)
17047 typval_T
*argvars UNUSED
;
17048 typval_T
*rettv UNUSED
;
17050 #ifdef FEAT_WINDOWS
17054 if (argvars
[0].v_type
== VAR_UNKNOWN
)
17058 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
17060 wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
17062 if (wp
!= NULL
&& rettv_list_alloc(rettv
) != FAIL
)
17064 for (; wp
!= NULL
; wp
= wp
->w_next
)
17065 if (list_append_number(rettv
->vval
.v_list
,
17066 wp
->w_buffer
->b_fnum
) == FAIL
)
17074 * "tabpagenr()" function
17077 f_tabpagenr(argvars
, rettv
)
17078 typval_T
*argvars UNUSED
;
17082 #ifdef FEAT_WINDOWS
17085 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
17087 arg
= get_tv_string_chk(&argvars
[0]);
17091 if (STRCMP(arg
, "$") == 0)
17092 nr
= tabpage_index(NULL
) - 1;
17094 EMSG2(_(e_invexpr2
), arg
);
17098 nr
= tabpage_index(curtab
);
17100 rettv
->vval
.v_number
= nr
;
17104 #ifdef FEAT_WINDOWS
17105 static int get_winnr
__ARGS((tabpage_T
*tp
, typval_T
*argvar
));
17108 * Common code for tabpagewinnr() and winnr().
17111 get_winnr(tp
, argvar
)
17120 twin
= (tp
== curtab
) ? curwin
: tp
->tp_curwin
;
17121 if (argvar
->v_type
!= VAR_UNKNOWN
)
17123 arg
= get_tv_string_chk(argvar
);
17125 nr
= 0; /* type error; errmsg already given */
17126 else if (STRCMP(arg
, "$") == 0)
17127 twin
= (tp
== curtab
) ? lastwin
: tp
->tp_lastwin
;
17128 else if (STRCMP(arg
, "#") == 0)
17130 twin
= (tp
== curtab
) ? prevwin
: tp
->tp_prevwin
;
17136 EMSG2(_(e_invexpr2
), arg
);
17142 for (wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
17143 wp
!= twin
; wp
= wp
->w_next
)
17147 /* didn't find it in this tabpage */
17158 * "tabpagewinnr()" function
17161 f_tabpagewinnr(argvars
, rettv
)
17162 typval_T
*argvars UNUSED
;
17166 #ifdef FEAT_WINDOWS
17169 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
17173 nr
= get_winnr(tp
, &argvars
[1]);
17175 rettv
->vval
.v_number
= nr
;
17180 * "tagfiles()" function
17183 f_tagfiles(argvars
, rettv
)
17184 typval_T
*argvars UNUSED
;
17187 char_u fname
[MAXPATHL
+ 1];
17191 if (rettv_list_alloc(rettv
) == FAIL
)
17194 for (first
= TRUE
; ; first
= FALSE
)
17195 if (get_tagfname(&tn
, first
, fname
) == FAIL
17196 || list_append_string(rettv
->vval
.v_list
, fname
, -1) == FAIL
)
17202 * "taglist()" function
17205 f_taglist(argvars
, rettv
)
17209 char_u
*tag_pattern
;
17211 tag_pattern
= get_tv_string(&argvars
[0]);
17213 rettv
->vval
.v_number
= FALSE
;
17214 if (*tag_pattern
== NUL
)
17217 if (rettv_list_alloc(rettv
) == OK
)
17218 (void)get_tags(rettv
->vval
.v_list
, tag_pattern
);
17222 * "tempname()" function
17225 f_tempname(argvars
, rettv
)
17226 typval_T
*argvars UNUSED
;
17229 static int x
= 'A';
17231 rettv
->v_type
= VAR_STRING
;
17232 rettv
->vval
.v_string
= vim_tempname(x
);
17234 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17235 * names. Skip 'I' and 'O', they are used for shell redirection. */
17253 } while (x
== 'I' || x
== 'O');
17257 * "test(list)" function: Just checking the walls...
17260 f_test(argvars
, rettv
)
17261 typval_T
*argvars UNUSED
;
17262 typval_T
*rettv UNUSED
;
17264 /* Used for unit testing. Change the code below to your liking. */
17268 char_u
*bad
, *good
;
17270 if (argvars
[0].v_type
!= VAR_LIST
)
17272 l
= argvars
[0].vval
.v_list
;
17278 bad
= get_tv_string(&li
->li_tv
);
17282 good
= get_tv_string(&li
->li_tv
);
17283 rettv
->vval
.v_number
= test_edit_score(bad
, good
);
17288 * "tolower(string)" function
17291 f_tolower(argvars
, rettv
)
17297 p
= vim_strsave(get_tv_string(&argvars
[0]));
17298 rettv
->v_type
= VAR_STRING
;
17299 rettv
->vval
.v_string
= p
;
17311 c
= utf_ptr2char(p
);
17312 lc
= utf_tolower(c
);
17313 l
= utf_ptr2len(p
);
17314 /* TODO: reallocate string when byte count changes. */
17315 if (utf_char2len(lc
) == l
)
17316 utf_char2bytes(lc
, p
);
17319 else if (has_mbyte
&& (l
= (*mb_ptr2len
)(p
)) > 1)
17320 p
+= l
; /* skip multi-byte character */
17324 *p
= TOLOWER_LOC(*p
); /* note that tolower() can be a macro */
17331 * "toupper(string)" function
17334 f_toupper(argvars
, rettv
)
17338 rettv
->v_type
= VAR_STRING
;
17339 rettv
->vval
.v_string
= strup_save(get_tv_string(&argvars
[0]));
17343 * "tr(string, fromstr, tostr)" function
17346 f_tr(argvars
, rettv
)
17363 char_u buf
[NUMBUFLEN
];
17364 char_u buf2
[NUMBUFLEN
];
17367 instr
= get_tv_string(&argvars
[0]);
17368 fromstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
17369 tostr
= get_tv_string_buf_chk(&argvars
[2], buf2
);
17371 /* Default return value: empty string. */
17372 rettv
->v_type
= VAR_STRING
;
17373 rettv
->vval
.v_string
= NULL
;
17374 if (fromstr
== NULL
|| tostr
== NULL
)
17375 return; /* type error; errmsg already given */
17376 ga_init2(&ga
, (int)sizeof(char), 80);
17381 /* not multi-byte: fromstr and tostr must be the same length */
17382 if (STRLEN(fromstr
) != STRLEN(tostr
))
17387 EMSG2(_(e_invarg2
), fromstr
);
17392 /* fromstr and tostr have to contain the same number of chars */
17393 while (*instr
!= NUL
)
17398 inlen
= (*mb_ptr2len
)(instr
);
17402 for (p
= fromstr
; *p
!= NUL
; p
+= fromlen
)
17404 fromlen
= (*mb_ptr2len
)(p
);
17405 if (fromlen
== inlen
&& STRNCMP(instr
, p
, inlen
) == 0)
17407 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
17409 tolen
= (*mb_ptr2len
)(p
);
17417 if (*p
== NUL
) /* tostr is shorter than fromstr */
17424 if (first
&& cpstr
== instr
)
17426 /* Check that fromstr and tostr have the same number of
17427 * (multi-byte) characters. Done only once when a character
17428 * of instr doesn't appear in fromstr. */
17430 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
17432 tolen
= (*mb_ptr2len
)(p
);
17439 ga_grow(&ga
, cplen
);
17440 mch_memmove((char *)ga
.ga_data
+ ga
.ga_len
, cpstr
, (size_t)cplen
);
17441 ga
.ga_len
+= cplen
;
17448 /* When not using multi-byte chars we can do it faster. */
17449 p
= vim_strchr(fromstr
, *instr
);
17451 ga_append(&ga
, tostr
[p
- fromstr
]);
17453 ga_append(&ga
, *instr
);
17458 /* add a terminating NUL */
17460 ga_append(&ga
, NUL
);
17462 rettv
->vval
.v_string
= ga
.ga_data
;
17467 * "trunc({float})" function
17470 f_trunc(argvars
, rettv
)
17476 rettv
->v_type
= VAR_FLOAT
;
17477 if (get_float_arg(argvars
, &f
) == OK
)
17478 /* trunc() is not in C90, use floor() or ceil() instead. */
17479 rettv
->vval
.v_float
= f
> 0 ? floor(f
) : ceil(f
);
17481 rettv
->vval
.v_float
= 0.0;
17486 * "type(expr)" function
17489 f_type(argvars
, rettv
)
17495 switch (argvars
[0].v_type
)
17497 case VAR_NUMBER
: n
= 0; break;
17498 case VAR_STRING
: n
= 1; break;
17499 case VAR_FUNC
: n
= 2; break;
17500 case VAR_LIST
: n
= 3; break;
17501 case VAR_DICT
: n
= 4; break;
17503 case VAR_FLOAT
: n
= 5; break;
17505 default: EMSG2(_(e_intern2
), "f_type()"); n
= 0; break;
17507 rettv
->vval
.v_number
= n
;
17511 * "values(dict)" function
17514 f_values(argvars
, rettv
)
17518 dict_list(argvars
, rettv
, 1);
17522 * "virtcol(string)" function
17525 f_virtcol(argvars
, rettv
)
17531 int fnum
= curbuf
->b_fnum
;
17533 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
17534 if (fp
!= NULL
&& fp
->lnum
<= curbuf
->b_ml
.ml_line_count
17535 && fnum
== curbuf
->b_fnum
)
17537 getvvcol(curwin
, fp
, NULL
, NULL
, &vcol
);
17541 rettv
->vval
.v_number
= vcol
;
17545 * "visualmode()" function
17548 f_visualmode(argvars
, rettv
)
17549 typval_T
*argvars UNUSED
;
17550 typval_T
*rettv UNUSED
;
17555 rettv
->v_type
= VAR_STRING
;
17556 str
[0] = curbuf
->b_visual_mode_eval
;
17558 rettv
->vval
.v_string
= vim_strsave(str
);
17560 /* A non-zero number or non-empty string argument: reset mode. */
17561 if (non_zero_arg(&argvars
[0]))
17562 curbuf
->b_visual_mode_eval
= NUL
;
17567 * "winbufnr(nr)" function
17570 f_winbufnr(argvars
, rettv
)
17576 wp
= find_win_by_nr(&argvars
[0], NULL
);
17578 rettv
->vval
.v_number
= -1;
17580 rettv
->vval
.v_number
= wp
->w_buffer
->b_fnum
;
17584 * "wincol()" function
17587 f_wincol(argvars
, rettv
)
17588 typval_T
*argvars UNUSED
;
17592 rettv
->vval
.v_number
= curwin
->w_wcol
+ 1;
17596 * "winheight(nr)" function
17599 f_winheight(argvars
, rettv
)
17605 wp
= find_win_by_nr(&argvars
[0], NULL
);
17607 rettv
->vval
.v_number
= -1;
17609 rettv
->vval
.v_number
= wp
->w_height
;
17613 * "winline()" function
17616 f_winline(argvars
, rettv
)
17617 typval_T
*argvars UNUSED
;
17621 rettv
->vval
.v_number
= curwin
->w_wrow
+ 1;
17625 * "winnr()" function
17628 f_winnr(argvars
, rettv
)
17629 typval_T
*argvars UNUSED
;
17634 #ifdef FEAT_WINDOWS
17635 nr
= get_winnr(curtab
, &argvars
[0]);
17637 rettv
->vval
.v_number
= nr
;
17641 * "winrestcmd()" function
17644 f_winrestcmd(argvars
, rettv
)
17645 typval_T
*argvars UNUSED
;
17648 #ifdef FEAT_WINDOWS
17654 ga_init2(&ga
, (int)sizeof(char), 70);
17655 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
17657 sprintf((char *)buf
, "%dresize %d|", winnr
, wp
->w_height
);
17658 ga_concat(&ga
, buf
);
17659 # ifdef FEAT_VERTSPLIT
17660 sprintf((char *)buf
, "vert %dresize %d|", winnr
, wp
->w_width
);
17661 ga_concat(&ga
, buf
);
17665 ga_append(&ga
, NUL
);
17667 rettv
->vval
.v_string
= ga
.ga_data
;
17669 rettv
->vval
.v_string
= NULL
;
17671 rettv
->v_type
= VAR_STRING
;
17675 * "winrestview()" function
17678 f_winrestview(argvars
, rettv
)
17680 typval_T
*rettv UNUSED
;
17684 if (argvars
[0].v_type
!= VAR_DICT
17685 || (dict
= argvars
[0].vval
.v_dict
) == NULL
)
17689 curwin
->w_cursor
.lnum
= get_dict_number(dict
, (char_u
*)"lnum");
17690 curwin
->w_cursor
.col
= get_dict_number(dict
, (char_u
*)"col");
17691 #ifdef FEAT_VIRTUALEDIT
17692 curwin
->w_cursor
.coladd
= get_dict_number(dict
, (char_u
*)"coladd");
17694 curwin
->w_curswant
= get_dict_number(dict
, (char_u
*)"curswant");
17695 curwin
->w_set_curswant
= FALSE
;
17697 set_topline(curwin
, get_dict_number(dict
, (char_u
*)"topline"));
17699 curwin
->w_topfill
= get_dict_number(dict
, (char_u
*)"topfill");
17701 curwin
->w_leftcol
= get_dict_number(dict
, (char_u
*)"leftcol");
17702 curwin
->w_skipcol
= get_dict_number(dict
, (char_u
*)"skipcol");
17705 changed_cline_bef_curs();
17706 invalidate_botline();
17707 redraw_later(VALID
);
17709 if (curwin
->w_topline
== 0)
17710 curwin
->w_topline
= 1;
17711 if (curwin
->w_topline
> curbuf
->b_ml
.ml_line_count
)
17712 curwin
->w_topline
= curbuf
->b_ml
.ml_line_count
;
17714 check_topfill(curwin
, TRUE
);
17720 * "winsaveview()" function
17723 f_winsaveview(argvars
, rettv
)
17724 typval_T
*argvars UNUSED
;
17729 dict
= dict_alloc();
17732 rettv
->v_type
= VAR_DICT
;
17733 rettv
->vval
.v_dict
= dict
;
17734 ++dict
->dv_refcount
;
17736 dict_add_nr_str(dict
, "lnum", (long)curwin
->w_cursor
.lnum
, NULL
);
17737 dict_add_nr_str(dict
, "col", (long)curwin
->w_cursor
.col
, NULL
);
17738 #ifdef FEAT_VIRTUALEDIT
17739 dict_add_nr_str(dict
, "coladd", (long)curwin
->w_cursor
.coladd
, NULL
);
17742 dict_add_nr_str(dict
, "curswant", (long)curwin
->w_curswant
, NULL
);
17744 dict_add_nr_str(dict
, "topline", (long)curwin
->w_topline
, NULL
);
17746 dict_add_nr_str(dict
, "topfill", (long)curwin
->w_topfill
, NULL
);
17748 dict_add_nr_str(dict
, "leftcol", (long)curwin
->w_leftcol
, NULL
);
17749 dict_add_nr_str(dict
, "skipcol", (long)curwin
->w_skipcol
, NULL
);
17753 * "winwidth(nr)" function
17756 f_winwidth(argvars
, rettv
)
17762 wp
= find_win_by_nr(&argvars
[0], NULL
);
17764 rettv
->vval
.v_number
= -1;
17766 #ifdef FEAT_VERTSPLIT
17767 rettv
->vval
.v_number
= wp
->w_width
;
17769 rettv
->vval
.v_number
= Columns
;
17774 * "writefile()" function
17777 f_writefile(argvars
, rettv
)
17781 int binary
= FALSE
;
17789 if (check_restricted() || check_secure())
17792 if (argvars
[0].v_type
!= VAR_LIST
)
17794 EMSG2(_(e_listarg
), "writefile()");
17797 if (argvars
[0].vval
.v_list
== NULL
)
17800 if (argvars
[2].v_type
!= VAR_UNKNOWN
17801 && STRCMP(get_tv_string(&argvars
[2]), "b") == 0)
17804 /* Always open the file in binary mode, library functions have a mind of
17805 * their own about CR-LF conversion. */
17806 fname
= get_tv_string(&argvars
[1]);
17807 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, WRITEBIN
)) == NULL
)
17809 EMSG2(_(e_notcreate
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
17814 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
;
17817 for (s
= get_tv_string(&li
->li_tv
); *s
!= NUL
; ++s
)
17829 if (!binary
|| li
->li_next
!= NULL
)
17830 if (putc('\n', fd
) == EOF
)
17844 rettv
->vval
.v_number
= ret
;
17848 * Translate a String variable into a position.
17849 * Returns NULL when there is an error.
17852 var2fpos(varp
, dollar_lnum
, fnum
)
17854 int dollar_lnum
; /* TRUE when $ is last line */
17855 int *fnum
; /* set to fnum for '0, 'A, etc. */
17861 /* Argument can be [lnum, col, coladd]. */
17862 if (varp
->v_type
== VAR_LIST
)
17869 l
= varp
->vval
.v_list
;
17873 /* Get the line number */
17874 pos
.lnum
= list_find_nr(l
, 0L, &error
);
17875 if (error
|| pos
.lnum
<= 0 || pos
.lnum
> curbuf
->b_ml
.ml_line_count
)
17876 return NULL
; /* invalid line number */
17878 /* Get the column number */
17879 pos
.col
= list_find_nr(l
, 1L, &error
);
17882 len
= (long)STRLEN(ml_get(pos
.lnum
));
17884 /* We accept "$" for the column number: last column. */
17885 li
= list_find(l
, 1L);
17886 if (li
!= NULL
&& li
->li_tv
.v_type
== VAR_STRING
17887 && li
->li_tv
.vval
.v_string
!= NULL
17888 && STRCMP(li
->li_tv
.vval
.v_string
, "$") == 0)
17891 /* Accept a position up to the NUL after the line. */
17892 if (pos
.col
== 0 || (int)pos
.col
> len
+ 1)
17893 return NULL
; /* invalid column number */
17896 #ifdef FEAT_VIRTUALEDIT
17897 /* Get the virtual offset. Defaults to zero. */
17898 pos
.coladd
= list_find_nr(l
, 2L, &error
);
17906 name
= get_tv_string_chk(varp
);
17909 if (name
[0] == '.') /* cursor */
17910 return &curwin
->w_cursor
;
17912 if (name
[0] == 'v' && name
[1] == NUL
) /* Visual start */
17916 return &curwin
->w_cursor
;
17919 if (name
[0] == '\'') /* mark */
17921 pp
= getmark_fnum(name
[1], FALSE
, fnum
);
17922 if (pp
== NULL
|| pp
== (pos_T
*)-1 || pp
->lnum
<= 0)
17927 #ifdef FEAT_VIRTUALEDIT
17931 if (name
[0] == 'w' && dollar_lnum
)
17934 if (name
[1] == '0') /* "w0": first visible line */
17937 pos
.lnum
= curwin
->w_topline
;
17940 else if (name
[1] == '$') /* "w$": last visible line */
17942 validate_botline();
17943 pos
.lnum
= curwin
->w_botline
- 1;
17947 else if (name
[0] == '$') /* last column or line */
17951 pos
.lnum
= curbuf
->b_ml
.ml_line_count
;
17956 pos
.lnum
= curwin
->w_cursor
.lnum
;
17957 pos
.col
= (colnr_T
)STRLEN(ml_get_curline());
17965 * Convert list in "arg" into a position and optional file number.
17966 * When "fnump" is NULL there is no file number, only 3 items.
17967 * Note that the column is passed on as-is, the caller may want to decrement
17968 * it to use 1 for the first column.
17969 * Return FAIL when conversion is not possible, doesn't check the position for
17973 list2fpos(arg
, posp
, fnump
)
17978 list_T
*l
= arg
->vval
.v_list
;
17982 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17983 * when "fnump" isn't NULL and "coladd" is optional. */
17984 if (arg
->v_type
!= VAR_LIST
17986 || l
->lv_len
< (fnump
== NULL
? 2 : 3)
17987 || l
->lv_len
> (fnump
== NULL
? 3 : 4))
17992 n
= list_find_nr(l
, i
++, NULL
); /* fnum */
17996 n
= curbuf
->b_fnum
; /* current buffer */
18000 n
= list_find_nr(l
, i
++, NULL
); /* lnum */
18005 n
= list_find_nr(l
, i
++, NULL
); /* col */
18010 #ifdef FEAT_VIRTUALEDIT
18011 n
= list_find_nr(l
, i
, NULL
);
18022 * Get the length of an environment variable name.
18023 * Advance "arg" to the first character after the name.
18024 * Return 0 for error.
18033 for (p
= *arg
; vim_isIDc(*p
); ++p
)
18035 if (p
== *arg
) /* no name found */
18038 len
= (int)(p
- *arg
);
18044 * Get the length of the name of a function or internal variable.
18045 * "arg" is advanced to the first non-white character after the name.
18046 * Return 0 if something is wrong.
18055 /* Find the end of the name. */
18056 for (p
= *arg
; eval_isnamec(*p
); ++p
)
18058 if (p
== *arg
) /* no name found */
18061 len
= (int)(p
- *arg
);
18062 *arg
= skipwhite(p
);
18068 * Get the length of the name of a variable or function.
18069 * Only the name is recognized, does not handle ".key" or "[idx]".
18070 * "arg" is advanced to the first non-white character after the name.
18071 * Return -1 if curly braces expansion failed.
18072 * Return 0 if something else is wrong.
18073 * If the name contains 'magic' {}'s, expand them and return the
18074 * expanded name in an allocated string via 'alias' - caller must free.
18077 get_name_len(arg
, alias
, evaluate
, verbose
)
18085 char_u
*expr_start
;
18088 *alias
= NULL
; /* default to no alias */
18090 if ((*arg
)[0] == K_SPECIAL
&& (*arg
)[1] == KS_EXTRA
18091 && (*arg
)[2] == (int)KE_SNR
)
18093 /* hard coded <SNR>, already translated */
18095 return get_id_len(arg
) + 3;
18097 len
= eval_fname_script(*arg
);
18100 /* literal "<SID>", "s:" or "<SNR>" */
18105 * Find the end of the name; check for {} construction.
18107 p
= find_name_end(*arg
, &expr_start
, &expr_end
,
18108 len
> 0 ? 0 : FNE_CHECK_START
);
18109 if (expr_start
!= NULL
)
18111 char_u
*temp_string
;
18115 len
+= (int)(p
- *arg
);
18116 *arg
= skipwhite(p
);
18121 * Include any <SID> etc in the expanded string:
18122 * Thus the -len here.
18124 temp_string
= make_expanded_name(*arg
- len
, expr_start
, expr_end
, p
);
18125 if (temp_string
== NULL
)
18127 *alias
= temp_string
;
18128 *arg
= skipwhite(p
);
18129 return (int)STRLEN(temp_string
);
18132 len
+= get_id_len(arg
);
18133 if (len
== 0 && verbose
)
18134 EMSG2(_(e_invexpr2
), *arg
);
18140 * Find the end of a variable or function name, taking care of magic braces.
18141 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
18142 * start and end of the first magic braces item.
18143 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
18144 * Return a pointer to just after the name. Equal to "arg" if there is no
18148 find_name_end(arg
, expr_start
, expr_end
, flags
)
18150 char_u
**expr_start
;
18158 if (expr_start
!= NULL
)
18160 *expr_start
= NULL
;
18164 /* Quick check for valid starting character. */
18165 if ((flags
& FNE_CHECK_START
) && !eval_isnamec1(*arg
) && *arg
!= '{')
18168 for (p
= arg
; *p
!= NUL
18169 && (eval_isnamec(*p
)
18171 || ((flags
& FNE_INCL_BR
) && (*p
== '[' || *p
== '.'))
18173 || br_nest
!= 0); mb_ptr_adv(p
))
18177 /* skip over 'string' to avoid counting [ and ] inside it. */
18178 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '\''; mb_ptr_adv(p
))
18183 else if (*p
== '"')
18185 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18186 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
18187 if (*p
== '\\' && p
[1] != NUL
)
18197 else if (*p
== ']')
18206 if (expr_start
!= NULL
&& *expr_start
== NULL
)
18209 else if (*p
== '}')
18212 if (expr_start
!= NULL
&& mb_nest
== 0 && *expr_end
== NULL
)
18222 * Expands out the 'magic' {}'s in a variable/function name.
18223 * Note that this can call itself recursively, to deal with
18224 * constructs like foo{bar}{baz}{bam}
18225 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18231 * Returns a new allocated string, which the caller must free.
18232 * Returns NULL for failure.
18235 make_expanded_name(in_start
, expr_start
, expr_end
, in_end
)
18237 char_u
*expr_start
;
18242 char_u
*retval
= NULL
;
18243 char_u
*temp_result
;
18244 char_u
*nextcmd
= NULL
;
18246 if (expr_end
== NULL
|| in_end
== NULL
)
18253 temp_result
= eval_to_string(expr_start
+ 1, &nextcmd
, FALSE
);
18254 if (temp_result
!= NULL
&& nextcmd
== NULL
)
18256 retval
= alloc((unsigned)(STRLEN(temp_result
) + (expr_start
- in_start
)
18257 + (in_end
- expr_end
) + 1));
18258 if (retval
!= NULL
)
18260 STRCPY(retval
, in_start
);
18261 STRCAT(retval
, temp_result
);
18262 STRCAT(retval
, expr_end
+ 1);
18265 vim_free(temp_result
);
18267 *in_end
= c1
; /* put char back for error messages */
18271 if (retval
!= NULL
)
18273 temp_result
= find_name_end(retval
, &expr_start
, &expr_end
, 0);
18274 if (expr_start
!= NULL
)
18276 /* Further expansion! */
18277 temp_result
= make_expanded_name(retval
, expr_start
,
18278 expr_end
, temp_result
);
18280 retval
= temp_result
;
18288 * Return TRUE if character "c" can be used in a variable or function name.
18289 * Does not include '{' or '}' for magic braces.
18295 return (ASCII_ISALNUM(c
) || c
== '_' || c
== ':' || c
== AUTOLOAD_CHAR
);
18299 * Return TRUE if character "c" can be used as the first character in a
18300 * variable or function name (excluding '{' and '}').
18306 return (ASCII_ISALPHA(c
) || c
== '_');
18310 * Set number v: variable to "val".
18313 set_vim_var_nr(idx
, val
)
18317 vimvars
[idx
].vv_nr
= val
;
18321 * Get number v: variable value.
18324 get_vim_var_nr(idx
)
18327 return vimvars
[idx
].vv_nr
;
18331 * Get string v: variable value. Uses a static buffer, can only be used once.
18334 get_vim_var_str(idx
)
18337 return get_tv_string(&vimvars
[idx
].vv_tv
);
18341 * Get List v: variable value. Caller must take care of reference count when
18345 get_vim_var_list(idx
)
18348 return vimvars
[idx
].vv_list
;
18352 * Set v:char to character "c".
18355 set_vim_var_char(c
)
18359 char_u buf
[MB_MAXBYTES
];
18366 buf
[(*mb_char2bytes
)(c
, buf
)] = NUL
;
18373 set_vim_var_string(VV_CHAR
, buf
, -1);
18377 * Set v:count to "count" and v:count1 to "count1".
18378 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
18381 set_vcount(count
, count1
, set_prevcount
)
18387 vimvars
[VV_PREVCOUNT
].vv_nr
= vimvars
[VV_COUNT
].vv_nr
;
18388 vimvars
[VV_COUNT
].vv_nr
= count
;
18389 vimvars
[VV_COUNT1
].vv_nr
= count1
;
18393 * Set string v: variable to a copy of "val".
18396 set_vim_var_string(idx
, val
, len
)
18399 int len
; /* length of "val" to use or -1 (whole string) */
18401 /* Need to do this (at least) once, since we can't initialize a union.
18402 * Will always be invoked when "v:progname" is set. */
18403 vimvars
[VV_VERSION
].vv_nr
= VIM_VERSION_100
;
18405 vim_free(vimvars
[idx
].vv_str
);
18407 vimvars
[idx
].vv_str
= NULL
;
18408 else if (len
== -1)
18409 vimvars
[idx
].vv_str
= vim_strsave(val
);
18411 vimvars
[idx
].vv_str
= vim_strnsave(val
, len
);
18415 * Set List v: variable to "val".
18418 set_vim_var_list(idx
, val
)
18422 list_unref(vimvars
[idx
].vv_list
);
18423 vimvars
[idx
].vv_list
= val
;
18425 ++val
->lv_refcount
;
18429 * Set v:register if needed.
18437 if (c
== 0 || c
== ' ')
18441 /* Avoid free/alloc when the value is already right. */
18442 if (vimvars
[VV_REG
].vv_str
== NULL
|| vimvars
[VV_REG
].vv_str
[0] != c
)
18443 set_vim_var_string(VV_REG
, ®name
, 1);
18447 * Get or set v:exception. If "oldval" == NULL, return the current value.
18448 * Otherwise, restore the value to "oldval" and return NULL.
18449 * Must always be called in pairs to save and restore v:exception! Does not
18450 * take care of memory allocations.
18453 v_exception(oldval
)
18456 if (oldval
== NULL
)
18457 return vimvars
[VV_EXCEPTION
].vv_str
;
18459 vimvars
[VV_EXCEPTION
].vv_str
= oldval
;
18464 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18465 * Otherwise, restore the value to "oldval" and return NULL.
18466 * Must always be called in pairs to save and restore v:throwpoint! Does not
18467 * take care of memory allocations.
18470 v_throwpoint(oldval
)
18473 if (oldval
== NULL
)
18474 return vimvars
[VV_THROWPOINT
].vv_str
;
18476 vimvars
[VV_THROWPOINT
].vv_str
= oldval
;
18480 #if defined(FEAT_AUTOCMD) || defined(PROTO)
18483 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18484 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18485 * Must always be called in pairs!
18488 set_cmdarg(eap
, oldarg
)
18496 oldval
= vimvars
[VV_CMDARG
].vv_str
;
18500 vimvars
[VV_CMDARG
].vv_str
= oldarg
;
18504 if (eap
->force_bin
== FORCE_BIN
)
18506 else if (eap
->force_bin
== FORCE_NOBIN
)
18511 if (eap
->read_edit
)
18514 if (eap
->force_ff
!= 0)
18515 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_ff
) + 6;
18517 if (eap
->force_enc
!= 0)
18518 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_enc
) + 7;
18519 if (eap
->bad_char
!= 0)
18520 len
+= 7 + 4; /* " ++bad=" + "keep" or "drop" */
18523 newval
= alloc(len
+ 1);
18524 if (newval
== NULL
)
18527 if (eap
->force_bin
== FORCE_BIN
)
18528 sprintf((char *)newval
, " ++bin");
18529 else if (eap
->force_bin
== FORCE_NOBIN
)
18530 sprintf((char *)newval
, " ++nobin");
18534 if (eap
->read_edit
)
18535 STRCAT(newval
, " ++edit");
18537 if (eap
->force_ff
!= 0)
18538 sprintf((char *)newval
+ STRLEN(newval
), " ++ff=%s",
18539 eap
->cmd
+ eap
->force_ff
);
18541 if (eap
->force_enc
!= 0)
18542 sprintf((char *)newval
+ STRLEN(newval
), " ++enc=%s",
18543 eap
->cmd
+ eap
->force_enc
);
18544 if (eap
->bad_char
== BAD_KEEP
)
18545 STRCPY(newval
+ STRLEN(newval
), " ++bad=keep");
18546 else if (eap
->bad_char
== BAD_DROP
)
18547 STRCPY(newval
+ STRLEN(newval
), " ++bad=drop");
18548 else if (eap
->bad_char
!= 0)
18549 sprintf((char *)newval
+ STRLEN(newval
), " ++bad=%c", eap
->bad_char
);
18551 vimvars
[VV_CMDARG
].vv_str
= newval
;
18557 * Get the value of internal variable "name".
18558 * Return OK or FAIL.
18561 get_var_tv(name
, len
, rettv
, verbose
)
18563 int len
; /* length of "name" */
18564 typval_T
*rettv
; /* NULL when only checking existence */
18565 int verbose
; /* may give error message */
18568 typval_T
*tv
= NULL
;
18573 /* truncate the name, so that we can use strcmp() */
18578 * Check for "b:changedtick".
18580 if (STRCMP(name
, "b:changedtick") == 0)
18582 atv
.v_type
= VAR_NUMBER
;
18583 atv
.vval
.v_number
= curbuf
->b_changedtick
;
18588 * Check for user-defined variables.
18592 v
= find_var(name
, NULL
);
18599 if (rettv
!= NULL
&& verbose
)
18600 EMSG2(_(e_undefvar
), name
);
18603 else if (rettv
!= NULL
)
18604 copy_tv(tv
, rettv
);
18612 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18613 * Also handle function call with Funcref variable: func(expr)
18614 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18617 handle_subscript(arg
, rettv
, evaluate
, verbose
)
18620 int evaluate
; /* do more than finding the end */
18621 int verbose
; /* give error messages */
18624 dict_T
*selfdict
= NULL
;
18631 || (**arg
== '.' && rettv
->v_type
== VAR_DICT
)
18632 || (**arg
== '(' && rettv
->v_type
== VAR_FUNC
))
18633 && !vim_iswhite(*(*arg
- 1)))
18637 /* need to copy the funcref so that we can clear rettv */
18639 rettv
->v_type
= VAR_UNKNOWN
;
18641 /* Invoke the function. Recursive! */
18642 s
= functv
.vval
.v_string
;
18643 ret
= get_func_tv(s
, (int)STRLEN(s
), rettv
, arg
,
18644 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
18645 &len
, evaluate
, selfdict
);
18647 /* Clear the funcref afterwards, so that deleting it while
18648 * evaluating the arguments is possible (see test55). */
18651 /* Stop the expression evaluation when immediately aborting on
18652 * error, or when an interrupt occurred or an exception was thrown
18653 * but not caught. */
18660 dict_unref(selfdict
);
18663 else /* **arg == '[' || **arg == '.' */
18665 dict_unref(selfdict
);
18666 if (rettv
->v_type
== VAR_DICT
)
18668 selfdict
= rettv
->vval
.v_dict
;
18669 if (selfdict
!= NULL
)
18670 ++selfdict
->dv_refcount
;
18674 if (eval_index(arg
, rettv
, evaluate
, verbose
) == FAIL
)
18681 dict_unref(selfdict
);
18686 * Allocate memory for a variable type-value, and make it empty (0 or NULL
18692 return (typval_T
*)alloc_clear((unsigned)sizeof(typval_T
));
18696 * Allocate memory for a variable type-value, and assign a string to it.
18697 * The string "s" must have been allocated, it is consumed.
18698 * Return NULL for out of memory, the variable otherwise.
18706 rettv
= alloc_tv();
18709 rettv
->v_type
= VAR_STRING
;
18710 rettv
->vval
.v_string
= s
;
18718 * Free the memory for a variable type-value.
18726 switch (varp
->v_type
)
18729 func_unref(varp
->vval
.v_string
);
18732 vim_free(varp
->vval
.v_string
);
18735 list_unref(varp
->vval
.v_list
);
18738 dict_unref(varp
->vval
.v_dict
);
18747 EMSG2(_(e_intern2
), "free_tv()");
18755 * Free the memory for a variable value and set the value to NULL or 0.
18763 switch (varp
->v_type
)
18766 func_unref(varp
->vval
.v_string
);
18769 vim_free(varp
->vval
.v_string
);
18770 varp
->vval
.v_string
= NULL
;
18773 list_unref(varp
->vval
.v_list
);
18774 varp
->vval
.v_list
= NULL
;
18777 dict_unref(varp
->vval
.v_dict
);
18778 varp
->vval
.v_dict
= NULL
;
18781 varp
->vval
.v_number
= 0;
18785 varp
->vval
.v_float
= 0.0;
18791 EMSG2(_(e_intern2
), "clear_tv()");
18798 * Set the value of a variable to NULL without freeing items.
18805 vim_memset(varp
, 0, sizeof(typval_T
));
18809 * Get the number value of a variable.
18810 * If it is a String variable, uses vim_str2nr().
18811 * For incompatible types, return 0.
18812 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18813 * caller of incompatible types: it sets *denote to TRUE if "denote"
18814 * is not NULL or returns -1 otherwise.
18817 get_tv_number(varp
)
18822 return get_tv_number_chk(varp
, &error
); /* return 0L on error */
18826 get_tv_number_chk(varp
, denote
)
18832 switch (varp
->v_type
)
18835 return (long)(varp
->vval
.v_number
);
18838 EMSG(_("E805: Using a Float as a Number"));
18842 EMSG(_("E703: Using a Funcref as a Number"));
18845 if (varp
->vval
.v_string
!= NULL
)
18846 vim_str2nr(varp
->vval
.v_string
, NULL
, NULL
,
18847 TRUE
, TRUE
, &n
, NULL
);
18850 EMSG(_("E745: Using a List as a Number"));
18853 EMSG(_("E728: Using a Dictionary as a Number"));
18856 EMSG2(_(e_intern2
), "get_tv_number()");
18859 if (denote
== NULL
) /* useful for values that must be unsigned */
18867 * Get the lnum from the first argument.
18868 * Also accepts ".", "$", etc., but that only works for the current buffer.
18869 * Returns -1 on error.
18872 get_tv_lnum(argvars
)
18878 lnum
= get_tv_number_chk(&argvars
[0], NULL
);
18879 if (lnum
== 0) /* no valid number, try using line() */
18881 rettv
.v_type
= VAR_NUMBER
;
18882 f_line(argvars
, &rettv
);
18883 lnum
= rettv
.vval
.v_number
;
18890 * Get the lnum from the first argument.
18891 * Also accepts "$", then "buf" is used.
18892 * Returns 0 on error.
18895 get_tv_lnum_buf(argvars
, buf
)
18899 if (argvars
[0].v_type
== VAR_STRING
18900 && argvars
[0].vval
.v_string
!= NULL
18901 && argvars
[0].vval
.v_string
[0] == '$'
18903 return buf
->b_ml
.ml_line_count
;
18904 return get_tv_number_chk(&argvars
[0], NULL
);
18908 * Get the string value of a variable.
18909 * If it is a Number variable, the number is converted into a string.
18910 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18911 * get_tv_string_buf() uses a given buffer.
18912 * If the String variable has never been set, return an empty string.
18913 * Never returns NULL;
18914 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18918 get_tv_string(varp
)
18921 static char_u mybuf
[NUMBUFLEN
];
18923 return get_tv_string_buf(varp
, mybuf
);
18927 get_tv_string_buf(varp
, buf
)
18931 char_u
*res
= get_tv_string_buf_chk(varp
, buf
);
18933 return res
!= NULL
? res
: (char_u
*)"";
18937 get_tv_string_chk(varp
)
18940 static char_u mybuf
[NUMBUFLEN
];
18942 return get_tv_string_buf_chk(varp
, mybuf
);
18946 get_tv_string_buf_chk(varp
, buf
)
18950 switch (varp
->v_type
)
18953 sprintf((char *)buf
, "%ld", (long)varp
->vval
.v_number
);
18956 EMSG(_("E729: using Funcref as a String"));
18959 EMSG(_("E730: using List as a String"));
18962 EMSG(_("E731: using Dictionary as a String"));
18966 EMSG(_("E806: using Float as a String"));
18970 if (varp
->vval
.v_string
!= NULL
)
18971 return varp
->vval
.v_string
;
18972 return (char_u
*)"";
18974 EMSG2(_(e_intern2
), "get_tv_string_buf()");
18981 * Find variable "name" in the list of variables.
18982 * Return a pointer to it if found, NULL if not found.
18983 * Careful: "a:0" variables don't have a name.
18984 * When "htp" is not NULL we are writing to the variable, set "htp" to the
18987 static dictitem_T
*
18988 find_var(name
, htp
)
18995 ht
= find_var_ht(name
, &varname
);
19000 return find_var_in_ht(ht
, varname
, htp
!= NULL
);
19004 * Find variable "varname" in hashtab "ht".
19005 * Returns NULL if not found.
19007 static dictitem_T
*
19008 find_var_in_ht(ht
, varname
, writing
)
19015 if (*varname
== NUL
)
19017 /* Must be something like "s:", otherwise "ht" would be NULL. */
19018 switch (varname
[-2])
19020 case 's': return &SCRIPT_SV(current_SID
)->sv_var
;
19021 case 'g': return &globvars_var
;
19022 case 'v': return &vimvars_var
;
19023 case 'b': return &curbuf
->b_bufvar
;
19024 case 'w': return &curwin
->w_winvar
;
19025 #ifdef FEAT_WINDOWS
19026 case 't': return &curtab
->tp_winvar
;
19028 case 'l': return current_funccal
== NULL
19029 ? NULL
: ¤t_funccal
->l_vars_var
;
19030 case 'a': return current_funccal
== NULL
19031 ? NULL
: ¤t_funccal
->l_avars_var
;
19036 hi
= hash_find(ht
, varname
);
19037 if (HASHITEM_EMPTY(hi
))
19039 /* For global variables we may try auto-loading the script. If it
19040 * worked find the variable again. Don't auto-load a script if it was
19041 * loaded already, otherwise it would be loaded every time when
19042 * checking if a function name is a Funcref variable. */
19043 if (ht
== &globvarht
&& !writing
19044 && script_autoload(varname
, FALSE
) && !aborting())
19045 hi
= hash_find(ht
, varname
);
19046 if (HASHITEM_EMPTY(hi
))
19053 * Find the hashtab used for a variable name.
19054 * Set "varname" to the start of name without ':'.
19057 find_var_ht(name
, varname
)
19063 if (name
[1] != ':')
19065 /* The name must not start with a colon or #. */
19066 if (name
[0] == ':' || name
[0] == AUTOLOAD_CHAR
)
19070 /* "version" is "v:version" in all scopes */
19071 hi
= hash_find(&compat_hashtab
, name
);
19072 if (!HASHITEM_EMPTY(hi
))
19073 return &compat_hashtab
;
19075 if (current_funccal
== NULL
)
19076 return &globvarht
; /* global variable */
19077 return ¤t_funccal
->l_vars
.dv_hashtab
; /* l: variable */
19079 *varname
= name
+ 2;
19080 if (*name
== 'g') /* global variable */
19082 /* There must be no ':' or '#' in the rest of the name, unless g: is used
19084 if (vim_strchr(name
+ 2, ':') != NULL
19085 || vim_strchr(name
+ 2, AUTOLOAD_CHAR
) != NULL
)
19087 if (*name
== 'b') /* buffer variable */
19088 return &curbuf
->b_vars
.dv_hashtab
;
19089 if (*name
== 'w') /* window variable */
19090 return &curwin
->w_vars
.dv_hashtab
;
19091 #ifdef FEAT_WINDOWS
19092 if (*name
== 't') /* tab page variable */
19093 return &curtab
->tp_vars
.dv_hashtab
;
19095 if (*name
== 'v') /* v: variable */
19097 if (*name
== 'a' && current_funccal
!= NULL
) /* function argument */
19098 return ¤t_funccal
->l_avars
.dv_hashtab
;
19099 if (*name
== 'l' && current_funccal
!= NULL
) /* local function variable */
19100 return ¤t_funccal
->l_vars
.dv_hashtab
;
19101 if (*name
== 's' /* script variable */
19102 && current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
19103 return &SCRIPT_VARS(current_SID
);
19108 * Get the string value of a (global/local) variable.
19109 * Returns NULL when it doesn't exist.
19112 get_var_value(name
)
19117 v
= find_var(name
, NULL
);
19120 return get_tv_string(&v
->di_tv
);
19124 * Allocate a new hashtab for a sourced script. It will be used while
19125 * sourcing this script and when executing functions defined in the script.
19128 new_script_vars(id
)
19135 if (ga_grow(&ga_scripts
, (int)(id
- ga_scripts
.ga_len
)) == OK
)
19137 /* Re-allocating ga_data means that an ht_array pointing to
19138 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
19139 * at its init value. Also reset "v_dict", it's always the same. */
19140 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
19142 ht
= &SCRIPT_VARS(i
);
19143 if (ht
->ht_mask
== HT_INIT_SIZE
- 1)
19144 ht
->ht_array
= ht
->ht_smallarray
;
19146 sv
->sv_var
.di_tv
.vval
.v_dict
= &sv
->sv_dict
;
19149 while (ga_scripts
.ga_len
< id
)
19151 sv
= SCRIPT_SV(ga_scripts
.ga_len
+ 1) =
19152 (scriptvar_T
*)alloc_clear(sizeof(scriptvar_T
));
19153 init_var_dict(&sv
->sv_dict
, &sv
->sv_var
);
19154 ++ga_scripts
.ga_len
;
19160 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
19164 init_var_dict(dict
, dict_var
)
19166 dictitem_T
*dict_var
;
19168 hash_init(&dict
->dv_hashtab
);
19169 dict
->dv_refcount
= DO_NOT_FREE_CNT
;
19170 dict
->dv_copyID
= 0;
19171 dict_var
->di_tv
.vval
.v_dict
= dict
;
19172 dict_var
->di_tv
.v_type
= VAR_DICT
;
19173 dict_var
->di_tv
.v_lock
= VAR_FIXED
;
19174 dict_var
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
19175 dict_var
->di_key
[0] = NUL
;
19179 * Clean up a list of internal variables.
19180 * Frees all allocated variables and the value they contain.
19181 * Clears hashtab "ht", does not free it.
19187 vars_clear_ext(ht
, TRUE
);
19191 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19194 vars_clear_ext(ht
, free_val
)
19203 todo
= (int)ht
->ht_used
;
19204 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
19206 if (!HASHITEM_EMPTY(hi
))
19210 /* Free the variable. Don't remove it from the hashtab,
19211 * ht_array might change then. hash_clear() takes care of it
19215 clear_tv(&v
->di_tv
);
19216 if ((v
->di_flags
& DI_FLAGS_FIX
) == 0)
19225 * Delete a variable from hashtab "ht" at item "hi".
19226 * Clear the variable value and free the dictitem.
19233 dictitem_T
*di
= HI2DI(hi
);
19235 hash_remove(ht
, hi
);
19236 clear_tv(&di
->di_tv
);
19241 * List the value of one internal variable.
19244 list_one_var(v
, prefix
, first
)
19251 char_u numbuf
[NUMBUFLEN
];
19253 current_copyID
+= COPYID_INC
;
19254 s
= echo_string(&v
->di_tv
, &tofree
, numbuf
, current_copyID
);
19255 list_one_var_a(prefix
, v
->di_key
, v
->di_tv
.v_type
,
19256 s
== NULL
? (char_u
*)"" : s
, first
);
19261 list_one_var_a(prefix
, name
, type
, string
, first
)
19266 int *first
; /* when TRUE clear rest of screen and set to FALSE */
19268 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19271 if (name
!= NULL
) /* "a:" vars don't have a name stored */
19275 if (type
== VAR_NUMBER
)
19277 else if (type
== VAR_FUNC
)
19279 else if (type
== VAR_LIST
)
19282 if (*string
== '[')
19285 else if (type
== VAR_DICT
)
19288 if (*string
== '{')
19294 msg_outtrans(string
);
19296 if (type
== VAR_FUNC
)
19297 msg_puts((char_u
*)"()");
19306 * Set variable "name" to value in "tv".
19307 * If the variable already exists, the value is updated.
19308 * Otherwise the variable is created.
19311 set_var(name
, tv
, copy
)
19314 int copy
; /* make copy of value in "tv" */
19321 ht
= find_var_ht(name
, &varname
);
19322 if (ht
== NULL
|| *varname
== NUL
)
19324 EMSG2(_(e_illvar
), name
);
19327 v
= find_var_in_ht(ht
, varname
, TRUE
);
19329 if (tv
->v_type
== VAR_FUNC
)
19331 if (!(vim_strchr((char_u
*)"wbs", name
[0]) != NULL
&& name
[1] == ':')
19332 && !ASCII_ISUPPER((name
[0] != NUL
&& name
[1] == ':')
19333 ? name
[2] : name
[0]))
19335 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name
);
19338 /* Don't allow hiding a function. When "v" is not NULL we migth be
19339 * assigning another function to the same var, the type is checked
19341 if (v
== NULL
&& function_exists(name
))
19343 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
19351 /* existing variable, need to clear the value */
19352 if (var_check_ro(v
->di_flags
, name
)
19353 || tv_check_lock(v
->di_tv
.v_lock
, name
))
19355 if (v
->di_tv
.v_type
!= tv
->v_type
19356 && !((v
->di_tv
.v_type
== VAR_STRING
19357 || v
->di_tv
.v_type
== VAR_NUMBER
)
19358 && (tv
->v_type
== VAR_STRING
19359 || tv
->v_type
== VAR_NUMBER
))
19361 && !((v
->di_tv
.v_type
== VAR_NUMBER
19362 || v
->di_tv
.v_type
== VAR_FLOAT
)
19363 && (tv
->v_type
== VAR_NUMBER
19364 || tv
->v_type
== VAR_FLOAT
))
19368 EMSG2(_("E706: Variable type mismatch for: %s"), name
);
19373 * Handle setting internal v: variables separately: we don't change
19376 if (ht
== &vimvarht
)
19378 if (v
->di_tv
.v_type
== VAR_STRING
)
19380 vim_free(v
->di_tv
.vval
.v_string
);
19381 if (copy
|| tv
->v_type
!= VAR_STRING
)
19382 v
->di_tv
.vval
.v_string
= vim_strsave(get_tv_string(tv
));
19385 /* Take over the string to avoid an extra alloc/free. */
19386 v
->di_tv
.vval
.v_string
= tv
->vval
.v_string
;
19387 tv
->vval
.v_string
= NULL
;
19390 else if (v
->di_tv
.v_type
!= VAR_NUMBER
)
19391 EMSG2(_(e_intern2
), "set_var()");
19394 v
->di_tv
.vval
.v_number
= get_tv_number(tv
);
19395 if (STRCMP(varname
, "searchforward") == 0)
19396 set_search_direction(v
->di_tv
.vval
.v_number
? '/' : '?');
19401 clear_tv(&v
->di_tv
);
19403 else /* add a new variable */
19405 /* Can't add "v:" variable. */
19406 if (ht
== &vimvarht
)
19408 EMSG2(_(e_illvar
), name
);
19412 /* Make sure the variable name is valid. */
19413 for (p
= varname
; *p
!= NUL
; ++p
)
19414 if (!eval_isnamec1(*p
) && (p
== varname
|| !VIM_ISDIGIT(*p
))
19415 && *p
!= AUTOLOAD_CHAR
)
19417 EMSG2(_(e_illvar
), varname
);
19421 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
19422 + STRLEN(varname
)));
19425 STRCPY(v
->di_key
, varname
);
19426 if (hash_add(ht
, DI2HIKEY(v
)) == FAIL
)
19434 if (copy
|| tv
->v_type
== VAR_NUMBER
|| tv
->v_type
== VAR_FLOAT
)
19435 copy_tv(tv
, &v
->di_tv
);
19439 v
->di_tv
.v_lock
= 0;
19445 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
19446 * Also give an error message.
19449 var_check_ro(flags
, name
)
19453 if (flags
& DI_FLAGS_RO
)
19455 EMSG2(_(e_readonlyvar
), name
);
19458 if ((flags
& DI_FLAGS_RO_SBX
) && sandbox
)
19460 EMSG2(_(e_readonlysbx
), name
);
19467 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19468 * Also give an error message.
19471 var_check_fixed(flags
, name
)
19475 if (flags
& DI_FLAGS_FIX
)
19477 EMSG2(_("E795: Cannot delete variable %s"), name
);
19484 * Return TRUE if typeval "tv" is set to be locked (immutable).
19485 * Also give an error message, using "name".
19488 tv_check_lock(lock
, name
)
19492 if (lock
& VAR_LOCKED
)
19494 EMSG2(_("E741: Value is locked: %s"),
19495 name
== NULL
? (char_u
*)_("Unknown") : name
);
19498 if (lock
& VAR_FIXED
)
19500 EMSG2(_("E742: Cannot change value of %s"),
19501 name
== NULL
? (char_u
*)_("Unknown") : name
);
19508 * Copy the values from typval_T "from" to typval_T "to".
19509 * When needed allocates string or increases reference count.
19510 * Does not make a copy of a list or dict but copies the reference!
19511 * It is OK for "from" and "to" to point to the same item. This is used to
19512 * make a copy later.
19519 to
->v_type
= from
->v_type
;
19521 switch (from
->v_type
)
19524 to
->vval
.v_number
= from
->vval
.v_number
;
19528 to
->vval
.v_float
= from
->vval
.v_float
;
19533 if (from
->vval
.v_string
== NULL
)
19534 to
->vval
.v_string
= NULL
;
19537 to
->vval
.v_string
= vim_strsave(from
->vval
.v_string
);
19538 if (from
->v_type
== VAR_FUNC
)
19539 func_ref(to
->vval
.v_string
);
19543 if (from
->vval
.v_list
== NULL
)
19544 to
->vval
.v_list
= NULL
;
19547 to
->vval
.v_list
= from
->vval
.v_list
;
19548 ++to
->vval
.v_list
->lv_refcount
;
19552 if (from
->vval
.v_dict
== NULL
)
19553 to
->vval
.v_dict
= NULL
;
19556 to
->vval
.v_dict
= from
->vval
.v_dict
;
19557 ++to
->vval
.v_dict
->dv_refcount
;
19561 EMSG2(_(e_intern2
), "copy_tv()");
19567 * Make a copy of an item.
19568 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
19569 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19570 * reference to an already copied list/dict can be used.
19571 * Returns FAIL or OK.
19574 item_copy(from
, to
, deep
, copyID
)
19580 static int recurse
= 0;
19583 if (recurse
>= DICT_MAXNEST
)
19585 EMSG(_("E698: variable nested too deep for making a copy"));
19590 switch (from
->v_type
)
19601 to
->v_type
= VAR_LIST
;
19603 if (from
->vval
.v_list
== NULL
)
19604 to
->vval
.v_list
= NULL
;
19605 else if (copyID
!= 0 && from
->vval
.v_list
->lv_copyID
== copyID
)
19607 /* use the copy made earlier */
19608 to
->vval
.v_list
= from
->vval
.v_list
->lv_copylist
;
19609 ++to
->vval
.v_list
->lv_refcount
;
19612 to
->vval
.v_list
= list_copy(from
->vval
.v_list
, deep
, copyID
);
19613 if (to
->vval
.v_list
== NULL
)
19617 to
->v_type
= VAR_DICT
;
19619 if (from
->vval
.v_dict
== NULL
)
19620 to
->vval
.v_dict
= NULL
;
19621 else if (copyID
!= 0 && from
->vval
.v_dict
->dv_copyID
== copyID
)
19623 /* use the copy made earlier */
19624 to
->vval
.v_dict
= from
->vval
.v_dict
->dv_copydict
;
19625 ++to
->vval
.v_dict
->dv_refcount
;
19628 to
->vval
.v_dict
= dict_copy(from
->vval
.v_dict
, deep
, copyID
);
19629 if (to
->vval
.v_dict
== NULL
)
19633 EMSG2(_(e_intern2
), "item_copy()");
19641 * ":echo expr1 ..." print each argument separated with a space, add a
19642 * newline at the end.
19643 * ":echon expr1 ..." print each argument plain.
19649 char_u
*arg
= eap
->arg
;
19653 int needclr
= TRUE
;
19654 int atstart
= TRUE
;
19655 char_u numbuf
[NUMBUFLEN
];
19659 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n' && !got_int
)
19661 /* If eval1() causes an error message the text from the command may
19662 * still need to be cleared. E.g., "echo 22,44". */
19663 need_clr_eos
= needclr
;
19666 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
19669 * Report the invalid expression unless the expression evaluation
19670 * has been cancelled due to an aborting error, an interrupt, or an
19674 EMSG2(_(e_invexpr2
), p
);
19675 need_clr_eos
= FALSE
;
19678 need_clr_eos
= FALSE
;
19685 /* Call msg_start() after eval1(), evaluating the expression
19686 * may cause a message to appear. */
19687 if (eap
->cmdidx
== CMD_echo
)
19690 else if (eap
->cmdidx
== CMD_echo
)
19691 msg_puts_attr((char_u
*)" ", echo_attr
);
19692 current_copyID
+= COPYID_INC
;
19693 p
= echo_string(&rettv
, &tofree
, numbuf
, current_copyID
);
19695 for ( ; *p
!= NUL
&& !got_int
; ++p
)
19697 if (*p
== '\n' || *p
== '\r' || *p
== TAB
)
19699 if (*p
!= TAB
&& needclr
)
19701 /* remove any text still there from the command */
19705 msg_putchar_attr(*p
, echo_attr
);
19712 int i
= (*mb_ptr2len
)(p
);
19714 (void)msg_outtrans_len_attr(p
, i
, echo_attr
);
19719 (void)msg_outtrans_len_attr(p
, 1, echo_attr
);
19725 arg
= skipwhite(arg
);
19727 eap
->nextcmd
= check_nextcmd(arg
);
19733 /* remove text that may still be there from the command */
19736 if (eap
->cmdidx
== CMD_echo
)
19742 * ":echohl {name}".
19750 id
= syn_name2id(eap
->arg
);
19754 echo_attr
= syn_id2attr(id
);
19758 * ":execute expr1 ..." execute the result of an expression.
19759 * ":echomsg expr1 ..." Print a message
19760 * ":echoerr expr1 ..." Print an error
19761 * Each gets spaces around each argument and a newline at the end for
19768 char_u
*arg
= eap
->arg
;
19776 ga_init2(&ga
, 1, 80);
19780 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
19783 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
19786 * Report the invalid expression unless the expression evaluation
19787 * has been cancelled due to an aborting error, an interrupt, or an
19791 EMSG2(_(e_invexpr2
), p
);
19798 p
= get_tv_string(&rettv
);
19799 len
= (int)STRLEN(p
);
19800 if (ga_grow(&ga
, len
+ 2) == FAIL
)
19807 ((char_u
*)(ga
.ga_data
))[ga
.ga_len
++] = ' ';
19808 STRCPY((char_u
*)(ga
.ga_data
) + ga
.ga_len
, p
);
19813 arg
= skipwhite(arg
);
19816 if (ret
!= FAIL
&& ga
.ga_data
!= NULL
)
19818 if (eap
->cmdidx
== CMD_echomsg
)
19820 MSG_ATTR(ga
.ga_data
, echo_attr
);
19823 else if (eap
->cmdidx
== CMD_echoerr
)
19825 /* We don't want to abort following commands, restore did_emsg. */
19826 save_did_emsg
= did_emsg
;
19827 EMSG((char_u
*)ga
.ga_data
);
19829 did_emsg
= save_did_emsg
;
19831 else if (eap
->cmdidx
== CMD_execute
)
19832 do_cmdline((char_u
*)ga
.ga_data
,
19833 eap
->getline
, eap
->cookie
, DOCMD_NOWAIT
|DOCMD_VERBOSE
);
19841 eap
->nextcmd
= check_nextcmd(arg
);
19845 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19846 * "arg" points to the "&" or '+' when called, to "option" when returning.
19847 * Returns NULL when no option name found. Otherwise pointer to the char
19848 * after the option name.
19851 find_option_end(arg
, opt_flags
)
19858 if (*p
== 'g' && p
[1] == ':')
19860 *opt_flags
= OPT_GLOBAL
;
19863 else if (*p
== 'l' && p
[1] == ':')
19865 *opt_flags
= OPT_LOCAL
;
19871 if (!ASCII_ISALPHA(*p
))
19875 if (p
[0] == 't' && p
[1] == '_' && p
[2] != NUL
&& p
[3] != NUL
)
19876 p
+= 4; /* termcap option */
19878 while (ASCII_ISALPHA(*p
))
19893 int saved_did_emsg
;
19894 char_u
*name
= NULL
;
19897 char_u
*line_arg
= NULL
;
19900 int varargs
= FALSE
;
19901 int mustend
= FALSE
;
19906 char_u
*skip_until
= NULL
;
19909 static int func_nr
= 0; /* number for nameless function */
19914 int sourcing_lnum_off
;
19917 * ":function" without argument: list functions.
19919 if (ends_excmd(*eap
->arg
))
19923 todo
= (int)func_hashtab
.ht_used
;
19924 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
19926 if (!HASHITEM_EMPTY(hi
))
19930 if (!isdigit(*fp
->uf_name
))
19931 list_func_head(fp
, FALSE
);
19935 eap
->nextcmd
= check_nextcmd(eap
->arg
);
19940 * ":function /pat": list functions matching pattern.
19942 if (*eap
->arg
== '/')
19944 p
= skip_regexp(eap
->arg
+ 1, '/', TRUE
, NULL
);
19947 regmatch_T regmatch
;
19951 regmatch
.regprog
= vim_regcomp(eap
->arg
+ 1, RE_MAGIC
);
19953 if (regmatch
.regprog
!= NULL
)
19955 regmatch
.rm_ic
= p_ic
;
19957 todo
= (int)func_hashtab
.ht_used
;
19958 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
19960 if (!HASHITEM_EMPTY(hi
))
19964 if (!isdigit(*fp
->uf_name
)
19965 && vim_regexec(®match
, fp
->uf_name
, 0))
19966 list_func_head(fp
, FALSE
);
19969 vim_free(regmatch
.regprog
);
19974 eap
->nextcmd
= check_nextcmd(p
);
19979 * Get the function name. There are these situations:
19980 * func normal function name
19981 * "name" == func, "fudi.fd_dict" == NULL
19982 * dict.func new dictionary entry
19983 * "name" == NULL, "fudi.fd_dict" set,
19984 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19985 * dict.func existing dict entry with a Funcref
19986 * "name" == func, "fudi.fd_dict" set,
19987 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19988 * dict.func existing dict entry that's not a Funcref
19989 * "name" == NULL, "fudi.fd_dict" set,
19990 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19993 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
19994 paren
= (vim_strchr(p
, '(') != NULL
);
19995 if (name
== NULL
&& (fudi
.fd_dict
== NULL
|| !paren
) && !eap
->skip
)
19998 * Return on an invalid expression in braces, unless the expression
19999 * evaluation has been cancelled due to an aborting error, an
20000 * interrupt, or an exception.
20004 if (!eap
->skip
&& fudi
.fd_newkey
!= NULL
)
20005 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
20006 vim_free(fudi
.fd_newkey
);
20013 /* An error in a function call during evaluation of an expression in magic
20014 * braces should not cause the function not to be defined. */
20015 saved_did_emsg
= did_emsg
;
20019 * ":function func" with only function name: list function.
20023 if (!ends_excmd(*skipwhite(p
)))
20025 EMSG(_(e_trailing
));
20028 eap
->nextcmd
= check_nextcmd(p
);
20029 if (eap
->nextcmd
!= NULL
)
20031 if (!eap
->skip
&& !got_int
)
20033 fp
= find_func(name
);
20036 list_func_head(fp
, TRUE
);
20037 for (j
= 0; j
< fp
->uf_lines
.ga_len
&& !got_int
; ++j
)
20039 if (FUNCLINE(fp
, j
) == NULL
)
20042 msg_outnum((long)(j
+ 1));
20047 msg_prt_line(FUNCLINE(fp
, j
), FALSE
);
20048 out_flush(); /* show a line at a time */
20054 msg_puts((char_u
*)" endfunction");
20058 emsg_funcname(N_("E123: Undefined function: %s"), name
);
20064 * ":function name(arg1, arg2)" Define function.
20071 EMSG2(_("E124: Missing '(': %s"), eap
->arg
);
20074 /* attempt to continue by skipping some text */
20075 if (vim_strchr(p
, '(') != NULL
)
20076 p
= vim_strchr(p
, '(');
20078 p
= skipwhite(p
+ 1);
20080 ga_init2(&newargs
, (int)sizeof(char_u
*), 3);
20081 ga_init2(&newlines
, (int)sizeof(char_u
*), 3);
20085 /* Check the name of the function. Unless it's a dictionary function
20086 * (that we are overwriting). */
20090 arg
= fudi
.fd_newkey
;
20091 if (arg
!= NULL
&& (fudi
.fd_di
== NULL
20092 || fudi
.fd_di
->di_tv
.v_type
!= VAR_FUNC
))
20094 if (*arg
== K_SPECIAL
)
20098 while (arg
[j
] != NUL
&& (j
== 0 ? eval_isnamec1(arg
[j
])
20099 : eval_isnamec(arg
[j
])))
20102 emsg_funcname((char *)e_invarg2
, arg
);
20107 * Isolate the arguments: "arg1, arg2, ...)"
20111 if (p
[0] == '.' && p
[1] == '.' && p
[2] == '.')
20120 while (ASCII_ISALNUM(*p
) || *p
== '_')
20122 if (arg
== p
|| isdigit(*arg
)
20123 || (p
- arg
== 9 && STRNCMP(arg
, "firstline", 9) == 0)
20124 || (p
- arg
== 8 && STRNCMP(arg
, "lastline", 8) == 0))
20127 EMSG2(_("E125: Illegal argument: %s"), arg
);
20130 if (ga_grow(&newargs
, 1) == FAIL
)
20134 arg
= vim_strsave(arg
);
20137 ((char_u
**)(newargs
.ga_data
))[newargs
.ga_len
] = arg
;
20146 if (mustend
&& *p
!= ')')
20149 EMSG2(_(e_invarg2
), eap
->arg
);
20153 ++p
; /* skip the ')' */
20155 /* find extra arguments "range", "dict" and "abort" */
20159 if (STRNCMP(p
, "range", 5) == 0)
20164 else if (STRNCMP(p
, "dict", 4) == 0)
20169 else if (STRNCMP(p
, "abort", 5) == 0)
20178 /* When there is a line break use what follows for the function body.
20179 * Makes 'exe "func Test()\n...\nendfunc"' work. */
20182 else if (*p
!= NUL
&& *p
!= '"' && !eap
->skip
&& !did_emsg
)
20183 EMSG(_(e_trailing
));
20186 * Read the body of the function, until ":endfunction" is found.
20190 /* Check if the function already exists, don't let the user type the
20191 * whole function before telling him it doesn't work! For a script we
20192 * need to skip the body to be able to find what follows. */
20193 if (!eap
->skip
&& !eap
->forceit
)
20195 if (fudi
.fd_dict
!= NULL
&& fudi
.fd_newkey
== NULL
)
20196 EMSG(_(e_funcdict
));
20197 else if (name
!= NULL
&& find_func(name
) != NULL
)
20198 emsg_funcname(e_funcexts
, name
);
20201 if (!eap
->skip
&& did_emsg
)
20204 msg_putchar('\n'); /* don't overwrite the function name */
20205 cmdline_row
= msg_row
;
20213 need_wait_return
= FALSE
;
20214 sourcing_lnum_off
= sourcing_lnum
;
20216 if (line_arg
!= NULL
)
20218 /* Use eap->arg, split up in parts by line breaks. */
20219 theline
= line_arg
;
20220 p
= vim_strchr(theline
, '\n');
20222 line_arg
+= STRLEN(line_arg
);
20229 else if (eap
->getline
== NULL
)
20230 theline
= getcmdline(':', 0L, indent
);
20232 theline
= eap
->getline(':', eap
->cookie
, indent
);
20234 lines_left
= Rows
- 1;
20235 if (theline
== NULL
)
20237 EMSG(_("E126: Missing :endfunction"));
20241 /* Detect line continuation: sourcing_lnum increased more than one. */
20242 if (sourcing_lnum
> sourcing_lnum_off
+ 1)
20243 sourcing_lnum_off
= sourcing_lnum
- sourcing_lnum_off
- 1;
20245 sourcing_lnum_off
= 0;
20247 if (skip_until
!= NULL
)
20249 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20250 * don't check for ":endfunc". */
20251 if (STRCMP(theline
, skip_until
) == 0)
20253 vim_free(skip_until
);
20259 /* skip ':' and blanks*/
20260 for (p
= theline
; vim_iswhite(*p
) || *p
== ':'; ++p
)
20263 /* Check for "endfunction". */
20264 if (checkforcmd(&p
, "endfunction", 4) && nesting
-- == 0)
20266 if (line_arg
== NULL
)
20271 /* Increase indent inside "if", "while", "for" and "try", decrease
20273 if (indent
> 2 && STRNCMP(p
, "end", 3) == 0)
20275 else if (STRNCMP(p
, "if", 2) == 0
20276 || STRNCMP(p
, "wh", 2) == 0
20277 || STRNCMP(p
, "for", 3) == 0
20278 || STRNCMP(p
, "try", 3) == 0)
20281 /* Check for defining a function inside this function. */
20282 if (checkforcmd(&p
, "function", 2))
20285 p
= skipwhite(p
+ 1);
20286 p
+= eval_fname_script(p
);
20287 if (ASCII_ISALPHA(*p
))
20289 vim_free(trans_function_name(&p
, TRUE
, 0, NULL
));
20290 if (*skipwhite(p
) == '(')
20298 /* Check for ":append" or ":insert". */
20299 p
= skip_range(p
, NULL
);
20300 if ((p
[0] == 'a' && (!ASCII_ISALPHA(p
[1]) || p
[1] == 'p'))
20302 && (!ASCII_ISALPHA(p
[1]) || (p
[1] == 'n'
20303 && (!ASCII_ISALPHA(p
[2]) || (p
[2] == 's'))))))
20304 skip_until
= vim_strsave((char_u
*)".");
20306 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20307 arg
= skipwhite(skiptowhite(p
));
20308 if (arg
[0] == '<' && arg
[1] =='<'
20309 && ((p
[0] == 'p' && p
[1] == 'y'
20310 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 't'))
20311 || (p
[0] == 'p' && p
[1] == 'e'
20312 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'r'))
20313 || (p
[0] == 't' && p
[1] == 'c'
20314 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'l'))
20315 || (p
[0] == 'r' && p
[1] == 'u' && p
[2] == 'b'
20316 && (!ASCII_ISALPHA(p
[3]) || p
[3] == 'y'))
20317 || (p
[0] == 'm' && p
[1] == 'z'
20318 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 's'))
20321 /* ":python <<" continues until a dot, like ":append" */
20322 p
= skipwhite(arg
+ 2);
20324 skip_until
= vim_strsave((char_u
*)".");
20326 skip_until
= vim_strsave(p
);
20330 /* Add the line to the function. */
20331 if (ga_grow(&newlines
, 1 + sourcing_lnum_off
) == FAIL
)
20333 if (line_arg
== NULL
)
20338 /* Copy the line to newly allocated memory. get_one_sourceline()
20339 * allocates 250 bytes per line, this saves 80% on average. The cost
20340 * is an extra alloc/free. */
20341 p
= vim_strsave(theline
);
20344 if (line_arg
== NULL
)
20349 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = theline
;
20351 /* Add NULL lines for continuation lines, so that the line count is
20352 * equal to the index in the growarray. */
20353 while (sourcing_lnum_off
-- > 0)
20354 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = NULL
;
20356 /* Check for end of eap->arg. */
20357 if (line_arg
!= NULL
&& *line_arg
== NUL
)
20361 /* Don't define the function when skipping commands or when an error was
20363 if (eap
->skip
|| did_emsg
)
20367 * If there are no errors, add the function
20369 if (fudi
.fd_dict
== NULL
)
20371 v
= find_var(name
, &ht
);
20372 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
20374 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
20379 fp
= find_func(name
);
20384 emsg_funcname(e_funcexts
, name
);
20387 if (fp
->uf_calls
> 0)
20389 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
20393 /* redefine existing function */
20394 ga_clear_strings(&(fp
->uf_args
));
20395 ga_clear_strings(&(fp
->uf_lines
));
20405 if (fudi
.fd_newkey
== NULL
&& !eap
->forceit
)
20407 EMSG(_(e_funcdict
));
20410 if (fudi
.fd_di
== NULL
)
20412 /* Can't add a function to a locked dictionary */
20413 if (tv_check_lock(fudi
.fd_dict
->dv_lock
, eap
->arg
))
20416 /* Can't change an existing function if it is locked */
20417 else if (tv_check_lock(fudi
.fd_di
->di_tv
.v_lock
, eap
->arg
))
20420 /* Give the function a sequential number. Can only be used with a
20423 sprintf(numbuf
, "%d", ++func_nr
);
20424 name
= vim_strsave((char_u
*)numbuf
);
20431 if (fudi
.fd_dict
== NULL
&& vim_strchr(name
, AUTOLOAD_CHAR
) != NULL
)
20434 char_u
*scriptname
;
20436 /* Check that the autoload name matches the script name. */
20438 if (sourcing_name
!= NULL
)
20440 scriptname
= autoload_name(name
);
20441 if (scriptname
!= NULL
)
20443 p
= vim_strchr(scriptname
, '/');
20444 plen
= (int)STRLEN(p
);
20445 slen
= (int)STRLEN(sourcing_name
);
20446 if (slen
> plen
&& fnamecmp(p
,
20447 sourcing_name
+ slen
- plen
) == 0)
20449 vim_free(scriptname
);
20454 EMSG2(_("E746: Function name does not match script file name: %s"), name
);
20459 fp
= (ufunc_T
*)alloc((unsigned)(sizeof(ufunc_T
) + STRLEN(name
)));
20463 if (fudi
.fd_dict
!= NULL
)
20465 if (fudi
.fd_di
== NULL
)
20467 /* add new dict entry */
20468 fudi
.fd_di
= dictitem_alloc(fudi
.fd_newkey
);
20469 if (fudi
.fd_di
== NULL
)
20474 if (dict_add(fudi
.fd_dict
, fudi
.fd_di
) == FAIL
)
20476 vim_free(fudi
.fd_di
);
20482 /* overwrite existing dict entry */
20483 clear_tv(&fudi
.fd_di
->di_tv
);
20484 fudi
.fd_di
->di_tv
.v_type
= VAR_FUNC
;
20485 fudi
.fd_di
->di_tv
.v_lock
= 0;
20486 fudi
.fd_di
->di_tv
.vval
.v_string
= vim_strsave(name
);
20487 fp
->uf_refcount
= 1;
20489 /* behave like "dict" was used */
20493 /* insert the new function in the function list */
20494 STRCPY(fp
->uf_name
, name
);
20495 hash_add(&func_hashtab
, UF2HIKEY(fp
));
20497 fp
->uf_args
= newargs
;
20498 fp
->uf_lines
= newlines
;
20499 #ifdef FEAT_PROFILE
20500 fp
->uf_tml_count
= NULL
;
20501 fp
->uf_tml_total
= NULL
;
20502 fp
->uf_tml_self
= NULL
;
20503 fp
->uf_profiling
= FALSE
;
20504 if (prof_def_func())
20505 func_do_profile(fp
);
20507 fp
->uf_varargs
= varargs
;
20508 fp
->uf_flags
= flags
;
20510 fp
->uf_script_ID
= current_SID
;
20514 ga_clear_strings(&newargs
);
20515 ga_clear_strings(&newlines
);
20517 vim_free(skip_until
);
20518 vim_free(fudi
.fd_newkey
);
20520 did_emsg
|= saved_did_emsg
;
20524 * Get a function name, translating "<SID>" and "<SNR>".
20525 * Also handles a Funcref in a List or Dictionary.
20526 * Returns the function name in allocated memory, or NULL for failure.
20528 * TFN_INT: internal function name OK
20529 * TFN_QUIET: be quiet
20530 * Advances "pp" to just after the function name (if no error).
20533 trans_function_name(pp
, skip
, flags
, fdp
)
20535 int skip
; /* only find the end, don't evaluate */
20537 funcdict_T
*fdp
; /* return: info about dictionary used */
20539 char_u
*name
= NULL
;
20543 char_u sid_buf
[20];
20548 vim_memset(fdp
, 0, sizeof(funcdict_T
));
20551 /* Check for hard coded <SNR>: already translated function ID (from a user
20553 if ((*pp
)[0] == K_SPECIAL
&& (*pp
)[1] == KS_EXTRA
20554 && (*pp
)[2] == (int)KE_SNR
)
20557 len
= get_id_len(pp
) + 3;
20558 return vim_strnsave(start
, len
);
20561 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20562 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
20563 lead
= eval_fname_script(start
);
20567 end
= get_lval(start
, NULL
, &lv
, FALSE
, skip
, flags
& TFN_QUIET
,
20568 lead
> 2 ? 0 : FNE_CHECK_START
);
20572 EMSG(_("E129: Function name required"));
20575 if (end
== NULL
|| (lv
.ll_tv
!= NULL
&& (lead
> 2 || lv
.ll_range
)))
20578 * Report an invalid expression in braces, unless the expression
20579 * evaluation has been cancelled due to an aborting error, an
20580 * interrupt, or an exception.
20585 EMSG2(_(e_invarg2
), start
);
20588 *pp
= find_name_end(start
, NULL
, NULL
, FNE_INCL_BR
);
20592 if (lv
.ll_tv
!= NULL
)
20596 fdp
->fd_dict
= lv
.ll_dict
;
20597 fdp
->fd_newkey
= lv
.ll_newkey
;
20598 lv
.ll_newkey
= NULL
;
20599 fdp
->fd_di
= lv
.ll_di
;
20601 if (lv
.ll_tv
->v_type
== VAR_FUNC
&& lv
.ll_tv
->vval
.v_string
!= NULL
)
20603 name
= vim_strsave(lv
.ll_tv
->vval
.v_string
);
20608 if (!skip
&& !(flags
& TFN_QUIET
) && (fdp
== NULL
20609 || lv
.ll_dict
== NULL
|| fdp
->fd_newkey
== NULL
))
20610 EMSG(_(e_funcref
));
20618 if (lv
.ll_name
== NULL
)
20620 /* Error found, but continue after the function name. */
20625 /* Check if the name is a Funcref. If so, use the value. */
20626 if (lv
.ll_exp_name
!= NULL
)
20628 len
= (int)STRLEN(lv
.ll_exp_name
);
20629 name
= deref_func_name(lv
.ll_exp_name
, &len
);
20630 if (name
== lv
.ll_exp_name
)
20635 len
= (int)(end
- *pp
);
20636 name
= deref_func_name(*pp
, &len
);
20642 name
= vim_strsave(name
);
20647 if (lv
.ll_exp_name
!= NULL
)
20649 len
= (int)STRLEN(lv
.ll_exp_name
);
20650 if (lead
<= 2 && lv
.ll_name
== lv
.ll_exp_name
20651 && STRNCMP(lv
.ll_name
, "s:", 2) == 0)
20653 /* When there was "s:" already or the name expanded to get a
20654 * leading "s:" then remove it. */
20662 if (lead
== 2) /* skip over "s:" */
20664 len
= (int)(end
- lv
.ll_name
);
20668 * Copy the function name to allocated memory.
20669 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20670 * Accept <SNR>123_name() outside a script.
20673 lead
= 0; /* do nothing */
20677 if ((lv
.ll_exp_name
!= NULL
&& eval_fname_sid(lv
.ll_exp_name
))
20678 || eval_fname_sid(*pp
))
20680 /* It's "s:" or "<SID>" */
20681 if (current_SID
<= 0)
20683 EMSG(_(e_usingsid
));
20686 sprintf((char *)sid_buf
, "%ld_", (long)current_SID
);
20687 lead
+= (int)STRLEN(sid_buf
);
20690 else if (!(flags
& TFN_INT
) && builtin_function(lv
.ll_name
))
20692 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv
.ll_name
);
20695 name
= alloc((unsigned)(len
+ lead
+ 1));
20700 name
[0] = K_SPECIAL
;
20701 name
[1] = KS_EXTRA
;
20702 name
[2] = (int)KE_SNR
;
20703 if (lead
> 3) /* If it's "<SID>" */
20704 STRCPY(name
+ 3, sid_buf
);
20706 mch_memmove(name
+ lead
, lv
.ll_name
, (size_t)len
);
20707 name
[len
+ lead
] = NUL
;
20717 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20718 * Return 2 if "p" starts with "s:".
20719 * Return 0 otherwise.
20722 eval_fname_script(p
)
20725 if (p
[0] == '<' && (STRNICMP(p
+ 1, "SID>", 4) == 0
20726 || STRNICMP(p
+ 1, "SNR>", 4) == 0))
20728 if (p
[0] == 's' && p
[1] == ':')
20734 * Return TRUE if "p" starts with "<SID>" or "s:".
20735 * Only works if eval_fname_script() returned non-zero for "p"!
20741 return (*p
== 's' || TOUPPER_ASC(p
[2]) == 'I');
20745 * List the head of the function: "name(arg1, arg2)".
20748 list_func_head(fp
, indent
)
20757 MSG_PUTS("function ");
20758 if (fp
->uf_name
[0] == K_SPECIAL
)
20760 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8
));
20761 msg_puts(fp
->uf_name
+ 3);
20764 msg_puts(fp
->uf_name
);
20766 for (j
= 0; j
< fp
->uf_args
.ga_len
; ++j
)
20770 msg_puts(FUNCARG(fp
, j
));
20772 if (fp
->uf_varargs
)
20781 last_set_msg(fp
->uf_script_ID
);
20785 * Find a function by name, return pointer to it in ufuncs.
20786 * Return NULL for unknown function.
20794 hi
= hash_find(&func_hashtab
, name
);
20795 if (!HASHITEM_EMPTY(hi
))
20800 #if defined(EXITFREE) || defined(PROTO)
20802 free_all_functions()
20806 /* Need to start all over every time, because func_free() may change the
20808 while (func_hashtab
.ht_used
> 0)
20809 for (hi
= func_hashtab
.ht_array
; ; ++hi
)
20810 if (!HASHITEM_EMPTY(hi
))
20812 func_free(HI2UF(hi
));
20819 * Return TRUE if a function "name" exists.
20822 function_exists(name
)
20829 p
= trans_function_name(&nm
, FALSE
, TFN_INT
|TFN_QUIET
, NULL
);
20830 nm
= skipwhite(nm
);
20832 /* Only accept "funcname", "funcname ", "funcname (..." and
20833 * "funcname(...", not "funcname!...". */
20834 if (p
!= NULL
&& (*nm
== NUL
|| *nm
== '('))
20836 if (builtin_function(p
))
20837 n
= (find_internal_func(p
) >= 0);
20839 n
= (find_func(p
) != NULL
);
20846 * Return TRUE if "name" looks like a builtin function name: starts with a
20847 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
20850 builtin_function(name
)
20853 return ASCII_ISLOWER(name
[0]) && vim_strchr(name
, ':') == NULL
20854 && vim_strchr(name
, AUTOLOAD_CHAR
) == NULL
;
20857 #if defined(FEAT_PROFILE) || defined(PROTO)
20859 * Start profiling function "fp".
20862 func_do_profile(fp
)
20865 fp
->uf_tm_count
= 0;
20866 profile_zero(&fp
->uf_tm_self
);
20867 profile_zero(&fp
->uf_tm_total
);
20868 if (fp
->uf_tml_count
== NULL
)
20869 fp
->uf_tml_count
= (int *)alloc_clear((unsigned)
20870 (sizeof(int) * fp
->uf_lines
.ga_len
));
20871 if (fp
->uf_tml_total
== NULL
)
20872 fp
->uf_tml_total
= (proftime_T
*)alloc_clear((unsigned)
20873 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
20874 if (fp
->uf_tml_self
== NULL
)
20875 fp
->uf_tml_self
= (proftime_T
*)alloc_clear((unsigned)
20876 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
20877 fp
->uf_tml_idx
= -1;
20878 if (fp
->uf_tml_count
== NULL
|| fp
->uf_tml_total
== NULL
20879 || fp
->uf_tml_self
== NULL
)
20880 return; /* out of memory */
20882 fp
->uf_profiling
= TRUE
;
20886 * Dump the profiling results for all functions in file "fd".
20889 func_dump_profile(fd
)
20899 todo
= (int)func_hashtab
.ht_used
;
20901 return; /* nothing to dump */
20903 sorttab
= (ufunc_T
**)alloc((unsigned)(sizeof(ufunc_T
) * todo
));
20905 for (hi
= func_hashtab
.ht_array
; todo
> 0; ++hi
)
20907 if (!HASHITEM_EMPTY(hi
))
20911 if (fp
->uf_profiling
)
20913 if (sorttab
!= NULL
)
20914 sorttab
[st_len
++] = fp
;
20916 if (fp
->uf_name
[0] == K_SPECIAL
)
20917 fprintf(fd
, "FUNCTION <SNR>%s()\n", fp
->uf_name
+ 3);
20919 fprintf(fd
, "FUNCTION %s()\n", fp
->uf_name
);
20920 if (fp
->uf_tm_count
== 1)
20921 fprintf(fd
, "Called 1 time\n");
20923 fprintf(fd
, "Called %d times\n", fp
->uf_tm_count
);
20924 fprintf(fd
, "Total time: %s\n", profile_msg(&fp
->uf_tm_total
));
20925 fprintf(fd
, " Self time: %s\n", profile_msg(&fp
->uf_tm_self
));
20927 fprintf(fd
, "count total (s) self (s)\n");
20929 for (i
= 0; i
< fp
->uf_lines
.ga_len
; ++i
)
20931 if (FUNCLINE(fp
, i
) == NULL
)
20933 prof_func_line(fd
, fp
->uf_tml_count
[i
],
20934 &fp
->uf_tml_total
[i
], &fp
->uf_tml_self
[i
], TRUE
);
20935 fprintf(fd
, "%s\n", FUNCLINE(fp
, i
));
20942 if (sorttab
!= NULL
&& st_len
> 0)
20944 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
20946 prof_sort_list(fd
, sorttab
, st_len
, "TOTAL", FALSE
);
20947 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
20949 prof_sort_list(fd
, sorttab
, st_len
, "SELF", TRUE
);
20956 prof_sort_list(fd
, sorttab
, st_len
, title
, prefer_self
)
20961 int prefer_self
; /* when equal print only self time */
20966 fprintf(fd
, "FUNCTIONS SORTED ON %s TIME\n", title
);
20967 fprintf(fd
, "count total (s) self (s) function\n");
20968 for (i
= 0; i
< 20 && i
< st_len
; ++i
)
20971 prof_func_line(fd
, fp
->uf_tm_count
, &fp
->uf_tm_total
, &fp
->uf_tm_self
,
20973 if (fp
->uf_name
[0] == K_SPECIAL
)
20974 fprintf(fd
, " <SNR>%s()\n", fp
->uf_name
+ 3);
20976 fprintf(fd
, " %s()\n", fp
->uf_name
);
20982 * Print the count and times for one function or function line.
20985 prof_func_line(fd
, count
, total
, self
, prefer_self
)
20990 int prefer_self
; /* when equal print only self time */
20994 fprintf(fd
, "%5d ", count
);
20995 if (prefer_self
&& profile_equal(total
, self
))
20998 fprintf(fd
, "%s ", profile_msg(total
));
20999 if (!prefer_self
&& profile_equal(total
, self
))
21002 fprintf(fd
, "%s ", profile_msg(self
));
21009 * Compare function for total time sorting.
21012 #ifdef __BORLANDC__
21015 prof_total_cmp(s1
, s2
)
21021 p1
= *(ufunc_T
**)s1
;
21022 p2
= *(ufunc_T
**)s2
;
21023 return profile_cmp(&p1
->uf_tm_total
, &p2
->uf_tm_total
);
21027 * Compare function for self time sorting.
21030 #ifdef __BORLANDC__
21033 prof_self_cmp(s1
, s2
)
21039 p1
= *(ufunc_T
**)s1
;
21040 p2
= *(ufunc_T
**)s2
;
21041 return profile_cmp(&p1
->uf_tm_self
, &p2
->uf_tm_self
);
21047 * If "name" has a package name try autoloading the script for it.
21048 * Return TRUE if a package was loaded.
21051 script_autoload(name
, reload
)
21053 int reload
; /* load script again when already loaded */
21056 char_u
*scriptname
, *tofree
;
21060 /* If there is no '#' after name[0] there is no package name. */
21061 p
= vim_strchr(name
, AUTOLOAD_CHAR
);
21062 if (p
== NULL
|| p
== name
)
21065 tofree
= scriptname
= autoload_name(name
);
21067 /* Find the name in the list of previously loaded package names. Skip
21068 * "autoload/", it's always the same. */
21069 for (i
= 0; i
< ga_loaded
.ga_len
; ++i
)
21070 if (STRCMP(((char_u
**)ga_loaded
.ga_data
)[i
] + 9, scriptname
+ 9) == 0)
21072 if (!reload
&& i
< ga_loaded
.ga_len
)
21073 ret
= FALSE
; /* was loaded already */
21076 /* Remember the name if it wasn't loaded already. */
21077 if (i
== ga_loaded
.ga_len
&& ga_grow(&ga_loaded
, 1) == OK
)
21079 ((char_u
**)ga_loaded
.ga_data
)[ga_loaded
.ga_len
++] = scriptname
;
21083 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
21084 if (source_runtime(scriptname
, FALSE
) == OK
)
21093 * Return the autoload script name for a function or variable name.
21094 * Returns NULL when out of memory.
21097 autoload_name(name
)
21101 char_u
*scriptname
;
21103 /* Get the script file name: replace '#' with '/', append ".vim". */
21104 scriptname
= alloc((unsigned)(STRLEN(name
) + 14));
21105 if (scriptname
== NULL
)
21107 STRCPY(scriptname
, "autoload/");
21108 STRCAT(scriptname
, name
);
21109 *vim_strrchr(scriptname
, AUTOLOAD_CHAR
) = NUL
;
21110 STRCAT(scriptname
, ".vim");
21111 while ((p
= vim_strchr(scriptname
, AUTOLOAD_CHAR
)) != NULL
)
21116 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
21119 * Function given to ExpandGeneric() to obtain the list of user defined
21123 get_user_func_name(xp
, idx
)
21127 static long_u done
;
21128 static hashitem_T
*hi
;
21134 hi
= func_hashtab
.ht_array
;
21136 if (done
< func_hashtab
.ht_used
)
21140 while (HASHITEM_EMPTY(hi
))
21144 if (STRLEN(fp
->uf_name
) + 4 >= IOSIZE
)
21145 return fp
->uf_name
; /* prevents overflow */
21147 cat_func_name(IObuff
, fp
);
21148 if (xp
->xp_context
!= EXPAND_USER_FUNC
)
21150 STRCAT(IObuff
, "(");
21151 if (!fp
->uf_varargs
&& fp
->uf_args
.ga_len
== 0)
21152 STRCAT(IObuff
, ")");
21159 #endif /* FEAT_CMDL_COMPL */
21162 * Copy the function name of "fp" to buffer "buf".
21163 * "buf" must be able to hold the function name plus three bytes.
21164 * Takes care of script-local function names.
21167 cat_func_name(buf
, fp
)
21171 if (fp
->uf_name
[0] == K_SPECIAL
)
21173 STRCPY(buf
, "<SNR>");
21174 STRCAT(buf
, fp
->uf_name
+ 3);
21177 STRCPY(buf
, fp
->uf_name
);
21181 * ":delfunction {name}"
21184 ex_delfunction(eap
)
21187 ufunc_T
*fp
= NULL
;
21193 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
21194 vim_free(fudi
.fd_newkey
);
21197 if (fudi
.fd_dict
!= NULL
&& !eap
->skip
)
21198 EMSG(_(e_funcref
));
21201 if (!ends_excmd(*skipwhite(p
)))
21204 EMSG(_(e_trailing
));
21207 eap
->nextcmd
= check_nextcmd(p
);
21208 if (eap
->nextcmd
!= NULL
)
21212 fp
= find_func(name
);
21219 EMSG2(_(e_nofunc
), eap
->arg
);
21222 if (fp
->uf_calls
> 0)
21224 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap
->arg
);
21228 if (fudi
.fd_dict
!= NULL
)
21230 /* Delete the dict item that refers to the function, it will
21231 * invoke func_unref() and possibly delete the function. */
21232 dictitem_remove(fudi
.fd_dict
, fudi
.fd_di
);
21240 * Free a function and remove it from the list of functions.
21248 /* clear this function */
21249 ga_clear_strings(&(fp
->uf_args
));
21250 ga_clear_strings(&(fp
->uf_lines
));
21251 #ifdef FEAT_PROFILE
21252 vim_free(fp
->uf_tml_count
);
21253 vim_free(fp
->uf_tml_total
);
21254 vim_free(fp
->uf_tml_self
);
21257 /* remove the function from the function hashtable */
21258 hi
= hash_find(&func_hashtab
, UF2HIKEY(fp
));
21259 if (HASHITEM_EMPTY(hi
))
21260 EMSG2(_(e_intern2
), "func_free()");
21262 hash_remove(&func_hashtab
, hi
);
21268 * Unreference a Function: decrement the reference count and free it when it
21269 * becomes zero. Only for numbered functions.
21277 if (name
!= NULL
&& isdigit(*name
))
21279 fp
= find_func(name
);
21281 EMSG2(_(e_intern2
), "func_unref()");
21282 else if (--fp
->uf_refcount
<= 0)
21284 /* Only delete it when it's not being used. Otherwise it's done
21285 * when "uf_calls" becomes zero. */
21286 if (fp
->uf_calls
== 0)
21293 * Count a reference to a Function.
21301 if (name
!= NULL
&& isdigit(*name
))
21303 fp
= find_func(name
);
21305 EMSG2(_(e_intern2
), "func_ref()");
21312 * Call a user function.
21315 call_user_func(fp
, argcount
, argvars
, rettv
, firstline
, lastline
, selfdict
)
21316 ufunc_T
*fp
; /* pointer to function */
21317 int argcount
; /* nr of args */
21318 typval_T
*argvars
; /* arguments */
21319 typval_T
*rettv
; /* return value */
21320 linenr_T firstline
; /* first line of range */
21321 linenr_T lastline
; /* last line of range */
21322 dict_T
*selfdict
; /* Dictionary for "self" */
21324 char_u
*save_sourcing_name
;
21325 linenr_T save_sourcing_lnum
;
21326 scid_T save_current_SID
;
21329 static int depth
= 0;
21331 int fixvar_idx
= 0; /* index in fixvar[] */
21334 char_u numbuf
[NUMBUFLEN
];
21336 #ifdef FEAT_PROFILE
21337 proftime_T wait_start
;
21338 proftime_T call_start
;
21341 /* If depth of calling is getting too high, don't execute the function */
21342 if (depth
>= p_mfd
)
21344 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
21345 rettv
->v_type
= VAR_NUMBER
;
21346 rettv
->vval
.v_number
= -1;
21351 line_breakcheck(); /* check for CTRL-C hit */
21353 fc
= (funccall_T
*)alloc(sizeof(funccall_T
));
21354 fc
->caller
= current_funccal
;
21355 current_funccal
= fc
;
21358 rettv
->vval
.v_number
= 0;
21360 fc
->returned
= FALSE
;
21361 fc
->level
= ex_nesting_level
;
21362 /* Check if this function has a breakpoint. */
21363 fc
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
, (linenr_T
)0);
21364 fc
->dbg_tick
= debug_tick
;
21367 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
21368 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21369 * each argument variable and saves a lot of time.
21372 * Init l: variables.
21374 init_var_dict(&fc
->l_vars
, &fc
->l_vars_var
);
21375 if (selfdict
!= NULL
)
21377 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21378 * some compiler that checks the destination size. */
21379 v
= &fc
->fixvar
[fixvar_idx
++].var
;
21381 STRCPY(name
, "self");
21382 v
->di_flags
= DI_FLAGS_RO
+ DI_FLAGS_FIX
;
21383 hash_add(&fc
->l_vars
.dv_hashtab
, DI2HIKEY(v
));
21384 v
->di_tv
.v_type
= VAR_DICT
;
21385 v
->di_tv
.v_lock
= 0;
21386 v
->di_tv
.vval
.v_dict
= selfdict
;
21387 ++selfdict
->dv_refcount
;
21391 * Init a: variables.
21392 * Set a:0 to "argcount".
21393 * Set a:000 to a list with room for the "..." arguments.
21395 init_var_dict(&fc
->l_avars
, &fc
->l_avars_var
);
21396 add_nr_var(&fc
->l_avars
, &fc
->fixvar
[fixvar_idx
++].var
, "0",
21397 (varnumber_T
)(argcount
- fp
->uf_args
.ga_len
));
21398 /* Use "name" to avoid a warning from some compiler that checks the
21399 * destination size. */
21400 v
= &fc
->fixvar
[fixvar_idx
++].var
;
21402 STRCPY(name
, "000");
21403 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21404 hash_add(&fc
->l_avars
.dv_hashtab
, DI2HIKEY(v
));
21405 v
->di_tv
.v_type
= VAR_LIST
;
21406 v
->di_tv
.v_lock
= VAR_FIXED
;
21407 v
->di_tv
.vval
.v_list
= &fc
->l_varlist
;
21408 vim_memset(&fc
->l_varlist
, 0, sizeof(list_T
));
21409 fc
->l_varlist
.lv_refcount
= DO_NOT_FREE_CNT
;
21410 fc
->l_varlist
.lv_lock
= VAR_FIXED
;
21413 * Set a:firstline to "firstline" and a:lastline to "lastline".
21414 * Set a:name to named arguments.
21415 * Set a:N to the "..." arguments.
21417 add_nr_var(&fc
->l_avars
, &fc
->fixvar
[fixvar_idx
++].var
, "firstline",
21418 (varnumber_T
)firstline
);
21419 add_nr_var(&fc
->l_avars
, &fc
->fixvar
[fixvar_idx
++].var
, "lastline",
21420 (varnumber_T
)lastline
);
21421 for (i
= 0; i
< argcount
; ++i
)
21423 ai
= i
- fp
->uf_args
.ga_len
;
21425 /* named argument a:name */
21426 name
= FUNCARG(fp
, i
);
21429 /* "..." argument a:1, a:2, etc. */
21430 sprintf((char *)numbuf
, "%d", ai
+ 1);
21433 if (fixvar_idx
< FIXVAR_CNT
&& STRLEN(name
) <= VAR_SHORT_LEN
)
21435 v
= &fc
->fixvar
[fixvar_idx
++].var
;
21436 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21440 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
21444 v
->di_flags
= DI_FLAGS_RO
;
21446 STRCPY(v
->di_key
, name
);
21447 hash_add(&fc
->l_avars
.dv_hashtab
, DI2HIKEY(v
));
21449 /* Note: the values are copied directly to avoid alloc/free.
21450 * "argvars" must have VAR_FIXED for v_lock. */
21451 v
->di_tv
= argvars
[i
];
21452 v
->di_tv
.v_lock
= VAR_FIXED
;
21454 if (ai
>= 0 && ai
< MAX_FUNC_ARGS
)
21456 list_append(&fc
->l_varlist
, &fc
->l_listitems
[ai
]);
21457 fc
->l_listitems
[ai
].li_tv
= argvars
[i
];
21458 fc
->l_listitems
[ai
].li_tv
.v_lock
= VAR_FIXED
;
21462 /* Don't redraw while executing the function. */
21463 ++RedrawingDisabled
;
21464 save_sourcing_name
= sourcing_name
;
21465 save_sourcing_lnum
= sourcing_lnum
;
21467 sourcing_name
= alloc((unsigned)((save_sourcing_name
== NULL
? 0
21468 : STRLEN(save_sourcing_name
)) + STRLEN(fp
->uf_name
) + 13));
21469 if (sourcing_name
!= NULL
)
21471 if (save_sourcing_name
!= NULL
21472 && STRNCMP(save_sourcing_name
, "function ", 9) == 0)
21473 sprintf((char *)sourcing_name
, "%s..", save_sourcing_name
);
21475 STRCPY(sourcing_name
, "function ");
21476 cat_func_name(sourcing_name
+ STRLEN(sourcing_name
), fp
);
21478 if (p_verbose
>= 12)
21481 verbose_enter_scroll();
21483 smsg((char_u
*)_("calling %s"), sourcing_name
);
21484 if (p_verbose
>= 14)
21486 char_u buf
[MSG_BUF_LEN
];
21487 char_u numbuf2
[NUMBUFLEN
];
21491 msg_puts((char_u
*)"(");
21492 for (i
= 0; i
< argcount
; ++i
)
21495 msg_puts((char_u
*)", ");
21496 if (argvars
[i
].v_type
== VAR_NUMBER
)
21497 msg_outnum((long)argvars
[i
].vval
.v_number
);
21500 s
= tv2string(&argvars
[i
], &tofree
, numbuf2
, 0);
21503 trunc_string(s
, buf
, MSG_BUF_CLEN
);
21509 msg_puts((char_u
*)")");
21511 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21513 verbose_leave_scroll();
21517 #ifdef FEAT_PROFILE
21518 if (do_profiling
== PROF_YES
)
21520 if (!fp
->uf_profiling
&& has_profiling(FALSE
, fp
->uf_name
, NULL
))
21521 func_do_profile(fp
);
21522 if (fp
->uf_profiling
21523 || (fc
->caller
!= NULL
&& fc
->caller
->func
->uf_profiling
))
21526 profile_start(&call_start
);
21527 profile_zero(&fp
->uf_tm_children
);
21529 script_prof_save(&wait_start
);
21533 save_current_SID
= current_SID
;
21534 current_SID
= fp
->uf_script_ID
;
21535 save_did_emsg
= did_emsg
;
21538 /* call do_cmdline() to execute the lines */
21539 do_cmdline(NULL
, get_func_line
, (void *)fc
,
21540 DOCMD_NOWAIT
|DOCMD_VERBOSE
|DOCMD_REPEAT
);
21542 --RedrawingDisabled
;
21544 /* when the function was aborted because of an error, return -1 */
21545 if ((did_emsg
&& (fp
->uf_flags
& FC_ABORT
)) || rettv
->v_type
== VAR_UNKNOWN
)
21548 rettv
->v_type
= VAR_NUMBER
;
21549 rettv
->vval
.v_number
= -1;
21552 #ifdef FEAT_PROFILE
21553 if (do_profiling
== PROF_YES
&& (fp
->uf_profiling
21554 || (fc
->caller
!= NULL
&& fc
->caller
->func
->uf_profiling
)))
21556 profile_end(&call_start
);
21557 profile_sub_wait(&wait_start
, &call_start
);
21558 profile_add(&fp
->uf_tm_total
, &call_start
);
21559 profile_self(&fp
->uf_tm_self
, &call_start
, &fp
->uf_tm_children
);
21560 if (fc
->caller
!= NULL
&& fc
->caller
->func
->uf_profiling
)
21562 profile_add(&fc
->caller
->func
->uf_tm_children
, &call_start
);
21563 profile_add(&fc
->caller
->func
->uf_tml_children
, &call_start
);
21568 /* when being verbose, mention the return value */
21569 if (p_verbose
>= 12)
21572 verbose_enter_scroll();
21575 smsg((char_u
*)_("%s aborted"), sourcing_name
);
21576 else if (fc
->rettv
->v_type
== VAR_NUMBER
)
21577 smsg((char_u
*)_("%s returning #%ld"), sourcing_name
,
21578 (long)fc
->rettv
->vval
.v_number
);
21581 char_u buf
[MSG_BUF_LEN
];
21582 char_u numbuf2
[NUMBUFLEN
];
21586 /* The value may be very long. Skip the middle part, so that we
21587 * have some idea how it starts and ends. smsg() would always
21588 * truncate it at the end. */
21589 s
= tv2string(fc
->rettv
, &tofree
, numbuf2
, 0);
21592 trunc_string(s
, buf
, MSG_BUF_CLEN
);
21593 smsg((char_u
*)_("%s returning %s"), sourcing_name
, buf
);
21597 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21599 verbose_leave_scroll();
21603 vim_free(sourcing_name
);
21604 sourcing_name
= save_sourcing_name
;
21605 sourcing_lnum
= save_sourcing_lnum
;
21606 current_SID
= save_current_SID
;
21607 #ifdef FEAT_PROFILE
21608 if (do_profiling
== PROF_YES
)
21609 script_prof_restore(&wait_start
);
21612 if (p_verbose
>= 12 && sourcing_name
!= NULL
)
21615 verbose_enter_scroll();
21617 smsg((char_u
*)_("continuing in %s"), sourcing_name
);
21618 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21620 verbose_leave_scroll();
21624 did_emsg
|= save_did_emsg
;
21625 current_funccal
= fc
->caller
;
21628 /* If the a:000 list and the l: and a: dicts are not referenced we can
21629 * free the funccall_T and what's in it. */
21630 if (fc
->l_varlist
.lv_refcount
== DO_NOT_FREE_CNT
21631 && fc
->l_vars
.dv_refcount
== DO_NOT_FREE_CNT
21632 && fc
->l_avars
.dv_refcount
== DO_NOT_FREE_CNT
)
21634 free_funccal(fc
, FALSE
);
21642 /* "fc" is still in use. This can happen when returning "a:000" or
21643 * assigning "l:" to a global variable.
21644 * Link "fc" in the list for garbage collection later. */
21645 fc
->caller
= previous_funccal
;
21646 previous_funccal
= fc
;
21648 /* Make a copy of the a: variables, since we didn't do that above. */
21649 todo
= (int)fc
->l_avars
.dv_hashtab
.ht_used
;
21650 for (hi
= fc
->l_avars
.dv_hashtab
.ht_array
; todo
> 0; ++hi
)
21652 if (!HASHITEM_EMPTY(hi
))
21656 copy_tv(&v
->di_tv
, &v
->di_tv
);
21660 /* Make a copy of the a:000 items, since we didn't do that above. */
21661 for (li
= fc
->l_varlist
.lv_first
; li
!= NULL
; li
= li
->li_next
)
21662 copy_tv(&li
->li_tv
, &li
->li_tv
);
21667 * Return TRUE if items in "fc" do not have "copyID". That means they are not
21668 * referenced from anywhere that is in use.
21671 can_free_funccal(fc
, copyID
)
21675 return (fc
->l_varlist
.lv_copyID
!= copyID
21676 && fc
->l_vars
.dv_copyID
!= copyID
21677 && fc
->l_avars
.dv_copyID
!= copyID
);
21681 * Free "fc" and what it contains.
21684 free_funccal(fc
, free_val
)
21686 int free_val
; /* a: vars were allocated */
21690 /* The a: variables typevals may not have been allocated, only free the
21691 * allocated variables. */
21692 vars_clear_ext(&fc
->l_avars
.dv_hashtab
, free_val
);
21694 /* free all l: variables */
21695 vars_clear(&fc
->l_vars
.dv_hashtab
);
21697 /* Free the a:000 variables if they were allocated. */
21699 for (li
= fc
->l_varlist
.lv_first
; li
!= NULL
; li
= li
->li_next
)
21700 clear_tv(&li
->li_tv
);
21706 * Add a number variable "name" to dict "dp" with value "nr".
21709 add_nr_var(dp
, v
, name
, nr
)
21715 STRCPY(v
->di_key
, name
);
21716 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21717 hash_add(&dp
->dv_hashtab
, DI2HIKEY(v
));
21718 v
->di_tv
.v_type
= VAR_NUMBER
;
21719 v
->di_tv
.v_lock
= VAR_FIXED
;
21720 v
->di_tv
.vval
.v_number
= nr
;
21730 char_u
*arg
= eap
->arg
;
21732 int returning
= FALSE
;
21734 if (current_funccal
== NULL
)
21736 EMSG(_("E133: :return not inside a function"));
21743 eap
->nextcmd
= NULL
;
21744 if ((*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
21745 && eval0(arg
, &rettv
, &eap
->nextcmd
, !eap
->skip
) != FAIL
)
21748 returning
= do_return(eap
, FALSE
, TRUE
, &rettv
);
21752 /* It's safer to return also on error. */
21753 else if (!eap
->skip
)
21756 * Return unless the expression evaluation has been cancelled due to an
21757 * aborting error, an interrupt, or an exception.
21760 returning
= do_return(eap
, FALSE
, TRUE
, NULL
);
21763 /* When skipping or the return gets pending, advance to the next command
21764 * in this line (!returning). Otherwise, ignore the rest of the line.
21765 * Following lines will be ignored by get_func_line(). */
21767 eap
->nextcmd
= NULL
;
21768 else if (eap
->nextcmd
== NULL
) /* no argument */
21769 eap
->nextcmd
= check_nextcmd(arg
);
21776 * Return from a function. Possibly makes the return pending. Also called
21777 * for a pending return at the ":endtry" or after returning from an extra
21778 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
21779 * when called due to a ":return" command. "rettv" may point to a typval_T
21780 * with the return rettv. Returns TRUE when the return can be carried out,
21781 * FALSE when the return gets pending.
21784 do_return(eap
, reanimate
, is_cmd
, rettv
)
21791 struct condstack
*cstack
= eap
->cstack
;
21794 /* Undo the return. */
21795 current_funccal
->returned
= FALSE
;
21798 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21799 * not in its finally clause (which then is to be executed next) is found.
21800 * In this case, make the ":return" pending for execution at the ":endtry".
21801 * Otherwise, return normally.
21803 idx
= cleanup_conditionals(eap
->cstack
, 0, TRUE
);
21806 cstack
->cs_pending
[idx
] = CSTP_RETURN
;
21808 if (!is_cmd
&& !reanimate
)
21809 /* A pending return again gets pending. "rettv" points to an
21810 * allocated variable with the rettv of the original ":return"'s
21811 * argument if present or is NULL else. */
21812 cstack
->cs_rettv
[idx
] = rettv
;
21815 /* When undoing a return in order to make it pending, get the stored
21818 rettv
= current_funccal
->rettv
;
21822 /* Store the value of the pending return. */
21823 if ((cstack
->cs_rettv
[idx
] = alloc_tv()) != NULL
)
21824 *(typval_T
*)cstack
->cs_rettv
[idx
] = *(typval_T
*)rettv
;
21826 EMSG(_(e_outofmem
));
21829 cstack
->cs_rettv
[idx
] = NULL
;
21833 /* The pending return value could be overwritten by a ":return"
21834 * without argument in a finally clause; reset the default
21836 current_funccal
->rettv
->v_type
= VAR_NUMBER
;
21837 current_funccal
->rettv
->vval
.v_number
= 0;
21840 report_make_pending(CSTP_RETURN
, rettv
);
21844 current_funccal
->returned
= TRUE
;
21846 /* If the return is carried out now, store the return value. For
21847 * a return immediately after reanimation, the value is already
21849 if (!reanimate
&& rettv
!= NULL
)
21851 clear_tv(current_funccal
->rettv
);
21852 *current_funccal
->rettv
= *(typval_T
*)rettv
;
21862 * Free the variable with a pending return value.
21865 discard_pending_return(rettv
)
21868 free_tv((typval_T
*)rettv
);
21872 * Generate a return command for producing the value of "rettv". The result
21873 * is an allocated string. Used by report_pending() for verbose messages.
21876 get_return_cmd(rettv
)
21880 char_u
*tofree
= NULL
;
21881 char_u numbuf
[NUMBUFLEN
];
21884 s
= echo_string((typval_T
*)rettv
, &tofree
, numbuf
, 0);
21888 STRCPY(IObuff
, ":return ");
21889 STRNCPY(IObuff
+ 8, s
, IOSIZE
- 8);
21890 if (STRLEN(s
) + 8 >= IOSIZE
)
21891 STRCPY(IObuff
+ IOSIZE
- 4, "...");
21893 return vim_strsave(IObuff
);
21897 * Get next function line.
21898 * Called by do_cmdline() to get the next line.
21899 * Returns allocated string, or NULL for end of function.
21902 get_func_line(c
, cookie
, indent
)
21907 funccall_T
*fcp
= (funccall_T
*)cookie
;
21908 ufunc_T
*fp
= fcp
->func
;
21910 garray_T
*gap
; /* growarray with function lines */
21912 /* If breakpoints have been added/deleted need to check for it. */
21913 if (fcp
->dbg_tick
!= debug_tick
)
21915 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
21917 fcp
->dbg_tick
= debug_tick
;
21919 #ifdef FEAT_PROFILE
21920 if (do_profiling
== PROF_YES
)
21921 func_line_end(cookie
);
21924 gap
= &fp
->uf_lines
;
21925 if (((fp
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
21930 /* Skip NULL lines (continuation lines). */
21931 while (fcp
->linenr
< gap
->ga_len
21932 && ((char_u
**)(gap
->ga_data
))[fcp
->linenr
] == NULL
)
21934 if (fcp
->linenr
>= gap
->ga_len
)
21938 retval
= vim_strsave(((char_u
**)(gap
->ga_data
))[fcp
->linenr
++]);
21939 sourcing_lnum
= fcp
->linenr
;
21940 #ifdef FEAT_PROFILE
21941 if (do_profiling
== PROF_YES
)
21942 func_line_start(cookie
);
21947 /* Did we encounter a breakpoint? */
21948 if (fcp
->breakpoint
!= 0 && fcp
->breakpoint
<= sourcing_lnum
)
21950 dbg_breakpoint(fp
->uf_name
, sourcing_lnum
);
21951 /* Find next breakpoint. */
21952 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
21954 fcp
->dbg_tick
= debug_tick
;
21960 #if defined(FEAT_PROFILE) || defined(PROTO)
21962 * Called when starting to read a function line.
21963 * "sourcing_lnum" must be correct!
21964 * When skipping lines it may not actually be executed, but we won't find out
21965 * until later and we need to store the time now.
21968 func_line_start(cookie
)
21971 funccall_T
*fcp
= (funccall_T
*)cookie
;
21972 ufunc_T
*fp
= fcp
->func
;
21974 if (fp
->uf_profiling
&& sourcing_lnum
>= 1
21975 && sourcing_lnum
<= fp
->uf_lines
.ga_len
)
21977 fp
->uf_tml_idx
= sourcing_lnum
- 1;
21978 /* Skip continuation lines. */
21979 while (fp
->uf_tml_idx
> 0 && FUNCLINE(fp
, fp
->uf_tml_idx
) == NULL
)
21981 fp
->uf_tml_execed
= FALSE
;
21982 profile_start(&fp
->uf_tml_start
);
21983 profile_zero(&fp
->uf_tml_children
);
21984 profile_get_wait(&fp
->uf_tml_wait
);
21989 * Called when actually executing a function line.
21992 func_line_exec(cookie
)
21995 funccall_T
*fcp
= (funccall_T
*)cookie
;
21996 ufunc_T
*fp
= fcp
->func
;
21998 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
21999 fp
->uf_tml_execed
= TRUE
;
22003 * Called when done with a function line.
22006 func_line_end(cookie
)
22009 funccall_T
*fcp
= (funccall_T
*)cookie
;
22010 ufunc_T
*fp
= fcp
->func
;
22012 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
22014 if (fp
->uf_tml_execed
)
22016 ++fp
->uf_tml_count
[fp
->uf_tml_idx
];
22017 profile_end(&fp
->uf_tml_start
);
22018 profile_sub_wait(&fp
->uf_tml_wait
, &fp
->uf_tml_start
);
22019 profile_add(&fp
->uf_tml_total
[fp
->uf_tml_idx
], &fp
->uf_tml_start
);
22020 profile_self(&fp
->uf_tml_self
[fp
->uf_tml_idx
], &fp
->uf_tml_start
,
22021 &fp
->uf_tml_children
);
22023 fp
->uf_tml_idx
= -1;
22029 * Return TRUE if the currently active function should be ended, because a
22030 * return was encountered or an error occurred. Used inside a ":while".
22033 func_has_ended(cookie
)
22036 funccall_T
*fcp
= (funccall_T
*)cookie
;
22038 /* Ignore the "abort" flag if the abortion behavior has been changed due to
22039 * an error inside a try conditional. */
22040 return (((fcp
->func
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
22045 * return TRUE if cookie indicates a function which "abort"s on errors.
22048 func_has_abort(cookie
)
22051 return ((funccall_T
*)cookie
)->func
->uf_flags
& FC_ABORT
;
22054 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
22057 VAR_FLAVOUR_DEFAULT
, /* doesn't start with uppercase */
22058 VAR_FLAVOUR_SESSION
, /* starts with uppercase, some lower */
22059 VAR_FLAVOUR_VIMINFO
/* all uppercase */
22062 static var_flavour_T var_flavour
__ARGS((char_u
*varname
));
22064 static var_flavour_T
22065 var_flavour(varname
)
22068 char_u
*p
= varname
;
22070 if (ASCII_ISUPPER(*p
))
22073 if (ASCII_ISLOWER(*p
))
22074 return VAR_FLAVOUR_SESSION
;
22075 return VAR_FLAVOUR_VIMINFO
;
22078 return VAR_FLAVOUR_DEFAULT
;
22082 #if defined(FEAT_VIMINFO) || defined(PROTO)
22084 * Restore global vars that start with a capital from the viminfo file
22087 read_viminfo_varlist(virp
, writing
)
22092 int type
= VAR_NUMBER
;
22095 if (!writing
&& (find_viminfo_parameter('!') != NULL
))
22097 tab
= vim_strchr(virp
->vir_line
+ 1, '\t');
22100 *tab
++ = '\0'; /* isolate the variable name */
22101 if (*tab
== 'S') /* string var */
22104 else if (*tab
== 'F')
22108 tab
= vim_strchr(tab
, '\t');
22112 if (type
== VAR_STRING
)
22113 tv
.vval
.v_string
= viminfo_readstring(virp
,
22114 (int)(tab
- virp
->vir_line
+ 1), TRUE
);
22116 else if (type
== VAR_FLOAT
)
22117 (void)string2float(tab
+ 1, &tv
.vval
.v_float
);
22120 tv
.vval
.v_number
= atol((char *)tab
+ 1);
22121 set_var(virp
->vir_line
+ 1, &tv
, FALSE
);
22122 if (type
== VAR_STRING
)
22123 vim_free(tv
.vval
.v_string
);
22128 return viminfo_readline(virp
);
22132 * Write global vars that start with a capital to the viminfo file
22135 write_viminfo_varlist(fp
)
22139 dictitem_T
*this_var
;
22144 char_u numbuf
[NUMBUFLEN
];
22146 if (find_viminfo_parameter('!') == NULL
)
22149 fputs(_("\n# global variables:\n"), fp
);
22151 todo
= (int)globvarht
.ht_used
;
22152 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
22154 if (!HASHITEM_EMPTY(hi
))
22157 this_var
= HI2DI(hi
);
22158 if (var_flavour(this_var
->di_key
) == VAR_FLAVOUR_VIMINFO
)
22160 switch (this_var
->di_tv
.v_type
)
22162 case VAR_STRING
: s
= "STR"; break;
22163 case VAR_NUMBER
: s
= "NUM"; break;
22165 case VAR_FLOAT
: s
= "FLO"; break;
22169 fprintf(fp
, "!%s\t%s\t", this_var
->di_key
, s
);
22170 p
= echo_string(&this_var
->di_tv
, &tofree
, numbuf
, 0);
22172 viminfo_writestring(fp
, p
);
22180 #if defined(FEAT_SESSION) || defined(PROTO)
22182 store_session_globals(fd
)
22186 dictitem_T
*this_var
;
22190 todo
= (int)globvarht
.ht_used
;
22191 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
22193 if (!HASHITEM_EMPTY(hi
))
22196 this_var
= HI2DI(hi
);
22197 if ((this_var
->di_tv
.v_type
== VAR_NUMBER
22198 || this_var
->di_tv
.v_type
== VAR_STRING
)
22199 && var_flavour(this_var
->di_key
) == VAR_FLAVOUR_SESSION
)
22201 /* Escape special characters with a backslash. Turn a LF and
22202 * CR into \n and \r. */
22203 p
= vim_strsave_escaped(get_tv_string(&this_var
->di_tv
),
22204 (char_u
*)"\\\"\n\r");
22205 if (p
== NULL
) /* out of memory */
22207 for (t
= p
; *t
!= NUL
; ++t
)
22210 else if (*t
== '\r')
22212 if ((fprintf(fd
, "let %s = %c%s%c",
22214 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
22217 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
22219 || put_eol(fd
) == FAIL
)
22227 else if (this_var
->di_tv
.v_type
== VAR_FLOAT
22228 && var_flavour(this_var
->di_key
) == VAR_FLAVOUR_SESSION
)
22230 float_T f
= this_var
->di_tv
.vval
.v_float
;
22238 if ((fprintf(fd
, "let %s = %c&%f",
22239 this_var
->di_key
, sign
, f
) < 0)
22240 || put_eol(fd
) == FAIL
)
22251 * Display script name where an item was last set.
22252 * Should only be invoked when 'verbose' is non-zero.
22255 last_set_msg(scriptID
)
22262 p
= home_replace_save(NULL
, get_scriptname(scriptID
));
22266 MSG_PUTS(_("\n\tLast set from "));
22275 * List v:oldfiles in a nice way.
22279 exarg_T
*eap UNUSED
;
22281 list_T
*l
= vimvars
[VV_OLDFILES
].vv_list
;
22286 msg((char_u
*)_("No old files"));
22291 for (li
= l
->lv_first
; li
!= NULL
&& !got_int
; li
= li
->li_next
)
22293 msg_outnum((long)++nr
);
22295 msg_outtrans(get_tv_string(&li
->li_tv
));
22297 out_flush(); /* output one line at a time */
22300 /* Assume "got_int" was set to truncate the listing. */
22303 #ifdef FEAT_BROWSE_CMD
22307 nr
= prompt_for_number(FALSE
);
22311 char_u
*p
= list_find_str(get_vim_var_list(VV_OLDFILES
),
22316 p
= expand_env_save(p
);
22318 eap
->cmdidx
= CMD_edit
;
22319 cmdmod
.browse
= FALSE
;
22320 do_exedit(eap
, NULL
);
22329 #endif /* FEAT_EVAL */
22332 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
22336 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22338 static int get_short_pathname
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
22339 static int shortpath_for_invalid_fname
__ARGS((char_u
**fname
, char_u
**bufp
, int *fnamelen
));
22340 static int shortpath_for_partial
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
22343 * Get the short path (8.3) for the filename in "fnamep".
22344 * Only works for a valid file name.
22345 * When the path gets longer "fnamep" is changed and the allocated buffer
22346 * is put in "bufp".
22347 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22348 * Returns OK on success, FAIL on failure.
22351 get_short_pathname(fnamep
, bufp
, fnamelen
)
22360 l
= GetShortPathName(*fnamep
, *fnamep
, len
);
22363 /* If that doesn't work (not enough space), then save the string
22364 * and try again with a new buffer big enough. */
22365 newbuf
= vim_strnsave(*fnamep
, l
);
22366 if (newbuf
== NULL
)
22370 *fnamep
= *bufp
= newbuf
;
22372 /* Really should always succeed, as the buffer is big enough. */
22373 l
= GetShortPathName(*fnamep
, *fnamep
, l
+1);
22381 * Get the short path (8.3) for the filename in "fname". The converted
22382 * path is returned in "bufp".
22384 * Some of the directories specified in "fname" may not exist. This function
22385 * will shorten the existing directories at the beginning of the path and then
22386 * append the remaining non-existing path.
22388 * fname - Pointer to the filename to shorten. On return, contains the
22389 * pointer to the shortened pathname
22390 * bufp - Pointer to an allocated buffer for the filename.
22391 * fnamelen - Length of the filename pointed to by fname
22393 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
22396 shortpath_for_invalid_fname(fname
, bufp
, fnamelen
)
22401 char_u
*short_fname
, *save_fname
, *pbuf_unused
;
22402 char_u
*endp
, *save_endp
;
22405 int new_len
, sfx_len
;
22409 old_len
= *fnamelen
;
22410 save_fname
= vim_strnsave(*fname
, old_len
);
22411 pbuf_unused
= NULL
;
22412 short_fname
= NULL
;
22414 endp
= save_fname
+ old_len
- 1; /* Find the end of the copy */
22418 * Try shortening the supplied path till it succeeds by removing one
22419 * directory at a time from the tail of the path.
22424 /* go back one path-separator */
22425 while (endp
> save_fname
&& !after_pathsep(save_fname
, endp
+ 1))
22427 if (endp
<= save_fname
)
22428 break; /* processed the complete path */
22431 * Replace the path separator with a NUL and try to shorten the
22436 short_fname
= save_fname
;
22437 len
= (int)STRLEN(short_fname
) + 1;
22438 if (get_short_pathname(&short_fname
, &pbuf_unused
, &len
) == FAIL
)
22443 *endp
= ch
; /* preserve the string */
22446 break; /* successfully shortened the path */
22448 /* failed to shorten the path. Skip the path separator */
22455 * Succeeded in shortening the path. Now concatenate the shortened
22456 * path with the remaining path at the tail.
22459 /* Compute the length of the new path. */
22460 sfx_len
= (int)(save_endp
- endp
) + 1;
22461 new_len
= len
+ sfx_len
;
22463 *fnamelen
= new_len
;
22465 if (new_len
> old_len
)
22467 /* There is not enough space in the currently allocated string,
22468 * copy it to a buffer big enough. */
22469 *fname
= *bufp
= vim_strnsave(short_fname
, new_len
);
22470 if (*fname
== NULL
)
22478 /* Transfer short_fname to the main buffer (it's big enough),
22479 * unless get_short_pathname() did its work in-place. */
22480 *fname
= *bufp
= save_fname
;
22481 if (short_fname
!= save_fname
)
22482 vim_strncpy(save_fname
, short_fname
, len
);
22486 /* concat the not-shortened part of the path */
22487 vim_strncpy(*fname
+ len
, endp
, sfx_len
);
22488 (*fname
)[new_len
] = NUL
;
22492 vim_free(pbuf_unused
);
22493 vim_free(save_fname
);
22499 * Get a pathname for a partial path.
22500 * Returns OK for success, FAIL for failure.
22503 shortpath_for_partial(fnamep
, bufp
, fnamelen
)
22508 int sepcount
, len
, tflen
;
22510 char_u
*pbuf
, *tfname
;
22513 /* Count up the path separators from the RHS.. so we know which part
22514 * of the path to return. */
22516 for (p
= *fnamep
; p
< *fnamep
+ *fnamelen
; mb_ptr_adv(p
))
22517 if (vim_ispathsep(*p
))
22520 /* Need full path first (use expand_env() to remove a "~/") */
22521 hasTilde
= (**fnamep
== '~');
22523 pbuf
= tfname
= expand_env_save(*fnamep
);
22525 pbuf
= tfname
= FullName_save(*fnamep
, FALSE
);
22527 len
= tflen
= (int)STRLEN(tfname
);
22529 if (get_short_pathname(&tfname
, &pbuf
, &len
) == FAIL
)
22534 /* Don't have a valid filename, so shorten the rest of the
22535 * path if we can. This CAN give us invalid 8.3 filenames, but
22536 * there's not a lot of point in guessing what it might be.
22539 if (shortpath_for_invalid_fname(&tfname
, &pbuf
, &len
) == FAIL
)
22543 /* Count the paths backward to find the beginning of the desired string. */
22544 for (p
= tfname
+ len
- 1; p
>= tfname
; --p
)
22548 p
-= mb_head_off(tfname
, p
);
22550 if (vim_ispathsep(*p
))
22552 if (sepcount
== 0 || (hasTilde
&& sepcount
== 1))
22569 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22571 *fnamelen
= (int)STRLEN(p
);
22577 #endif /* WIN3264 */
22580 * Adjust a filename, according to a string of modifiers.
22581 * *fnamep must be NUL terminated when called. When returning, the length is
22582 * determined by *fnamelen.
22583 * Returns VALID_ flags or -1 for failure.
22584 * When there is an error, *fnamep is set to NULL.
22587 modify_fname(src
, usedlen
, fnamep
, bufp
, fnamelen
)
22588 char_u
*src
; /* string with modifiers */
22589 int *usedlen
; /* characters after src that are used */
22590 char_u
**fnamep
; /* file name so far */
22591 char_u
**bufp
; /* buffer for allocated file name or NULL */
22592 int *fnamelen
; /* length of fnamep */
22596 char_u
*s
, *p
, *pbuf
;
22597 char_u dirname
[MAXPATHL
];
22599 int has_fullname
= 0;
22601 int has_shortname
= 0;
22605 /* ":p" - full path/file_name */
22606 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'p')
22610 valid
|= VALID_PATH
;
22613 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22614 if ((*fnamep
)[0] == '~'
22615 #if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22616 && ((*fnamep
)[1] == '/'
22617 # ifdef BACKSLASH_IN_FILENAME
22618 || (*fnamep
)[1] == '\\'
22620 || (*fnamep
)[1] == NUL
)
22625 *fnamep
= expand_env_save(*fnamep
);
22626 vim_free(*bufp
); /* free any allocated file name */
22628 if (*fnamep
== NULL
)
22632 /* When "/." or "/.." is used: force expansion to get rid of it. */
22633 for (p
= *fnamep
; *p
!= NUL
; mb_ptr_adv(p
))
22635 if (vim_ispathsep(*p
)
22638 || vim_ispathsep(p
[2])
22640 && (p
[3] == NUL
|| vim_ispathsep(p
[3])))))
22644 /* FullName_save() is slow, don't use it when not needed. */
22645 if (*p
!= NUL
|| !vim_isAbsName(*fnamep
))
22647 *fnamep
= FullName_save(*fnamep
, *p
!= NUL
);
22648 vim_free(*bufp
); /* free any allocated file name */
22650 if (*fnamep
== NULL
)
22654 /* Append a path separator to a directory. */
22655 if (mch_isdir(*fnamep
))
22657 /* Make room for one or two extra characters. */
22658 *fnamep
= vim_strnsave(*fnamep
, (int)STRLEN(*fnamep
) + 2);
22659 vim_free(*bufp
); /* free any allocated file name */
22661 if (*fnamep
== NULL
)
22663 add_pathsep(*fnamep
);
22667 /* ":." - path relative to the current directory */
22668 /* ":~" - path relative to the home directory */
22669 /* ":8" - shortname path - postponed till after */
22670 while (src
[*usedlen
] == ':'
22671 && ((c
= src
[*usedlen
+ 1]) == '.' || c
== '~' || c
== '8'))
22677 has_shortname
= 1; /* Postpone this. */
22682 /* Need full path first (use expand_env() to remove a "~/") */
22685 if (c
== '.' && **fnamep
== '~')
22686 p
= pbuf
= expand_env_save(*fnamep
);
22688 p
= pbuf
= FullName_save(*fnamep
, FALSE
);
22699 mch_dirname(dirname
, MAXPATHL
);
22700 s
= shorten_fname(p
, dirname
);
22706 vim_free(*bufp
); /* free any allocated file name */
22714 home_replace(NULL
, p
, dirname
, MAXPATHL
, TRUE
);
22715 /* Only replace it when it starts with '~' */
22716 if (*dirname
== '~')
22718 s
= vim_strsave(dirname
);
22731 tail
= gettail(*fnamep
);
22732 *fnamelen
= (int)STRLEN(*fnamep
);
22734 /* ":h" - head, remove "/file_name", can be repeated */
22735 /* Don't remove the first "/" or "c:\" */
22736 while (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'h')
22738 valid
|= VALID_HEAD
;
22740 s
= get_past_head(*fnamep
);
22741 while (tail
> s
&& after_pathsep(s
, tail
))
22742 mb_ptr_back(*fnamep
, tail
);
22743 *fnamelen
= (int)(tail
- *fnamep
);
22746 *fnamelen
+= 1; /* the path separator is part of the path */
22748 if (*fnamelen
== 0)
22750 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22751 p
= vim_strsave((char_u
*)".");
22755 *bufp
= *fnamep
= tail
= p
;
22760 while (tail
> s
&& !after_pathsep(s
, tail
))
22761 mb_ptr_back(*fnamep
, tail
);
22765 /* ":8" - shortname */
22766 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == '8')
22775 /* Check shortname after we have done 'heads' and before we do 'tails'
22780 /* Copy the string if it is shortened by :h */
22781 if (*fnamelen
< (int)STRLEN(*fnamep
))
22783 p
= vim_strnsave(*fnamep
, *fnamelen
);
22787 *bufp
= *fnamep
= p
;
22790 /* Split into two implementations - makes it easier. First is where
22791 * there isn't a full name already, second is where there is.
22793 if (!has_fullname
&& !vim_isAbsName(*fnamep
))
22795 if (shortpath_for_partial(fnamep
, bufp
, fnamelen
) == FAIL
)
22802 /* Simple case, already have the full-name
22803 * Nearly always shorter, so try first time. */
22805 if (get_short_pathname(fnamep
, bufp
, &l
) == FAIL
)
22810 /* Couldn't find the filename.. search the paths.
22813 if (shortpath_for_invalid_fname(fnamep
, bufp
, &l
) == FAIL
)
22819 #endif /* WIN3264 */
22821 /* ":t" - tail, just the basename */
22822 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 't')
22825 *fnamelen
-= (int)(tail
- *fnamep
);
22829 /* ":e" - extension, can be repeated */
22830 /* ":r" - root, without extension, can be repeated */
22831 while (src
[*usedlen
] == ':'
22832 && (src
[*usedlen
+ 1] == 'e' || src
[*usedlen
+ 1] == 'r'))
22834 /* find a '.' in the tail:
22835 * - for second :e: before the current fname
22836 * - otherwise: The last '.'
22838 if (src
[*usedlen
+ 1] == 'e' && *fnamep
> tail
)
22841 s
= *fnamep
+ *fnamelen
- 1;
22842 for ( ; s
> tail
; --s
)
22845 if (src
[*usedlen
+ 1] == 'e') /* :e */
22849 *fnamelen
+= (int)(*fnamep
- (s
+ 1));
22852 /* cut version from the extension */
22853 s
= *fnamep
+ *fnamelen
- 1;
22854 for ( ; s
> *fnamep
; --s
)
22858 *fnamelen
= s
- *fnamep
;
22861 else if (*fnamep
<= tail
)
22866 if (s
> tail
) /* remove one extension */
22867 *fnamelen
= (int)(s
- *fnamep
);
22872 /* ":s?pat?foo?" - substitute */
22873 /* ":gs?pat?foo?" - global substitute */
22874 if (src
[*usedlen
] == ':'
22875 && (src
[*usedlen
+ 1] == 's'
22876 || (src
[*usedlen
+ 1] == 'g' && src
[*usedlen
+ 2] == 's')))
22885 flags
= (char_u
*)"";
22886 s
= src
+ *usedlen
+ 2;
22887 if (src
[*usedlen
+ 1] == 'g')
22889 flags
= (char_u
*)"g";
22896 /* find end of pattern */
22897 p
= vim_strchr(s
, sep
);
22900 pat
= vim_strnsave(s
, (int)(p
- s
));
22904 /* find end of substitution */
22905 p
= vim_strchr(s
, sep
);
22908 sub
= vim_strnsave(s
, (int)(p
- s
));
22909 str
= vim_strnsave(*fnamep
, *fnamelen
);
22910 if (sub
!= NULL
&& str
!= NULL
)
22912 *usedlen
= (int)(p
+ 1 - src
);
22913 s
= do_string_sub(str
, pat
, sub
, flags
);
22917 *fnamelen
= (int)STRLEN(s
);
22929 /* after using ":s", repeat all the modifiers */
22939 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22940 * "flags" can be "g" to do a global substitute.
22941 * Returns an allocated string, NULL for error.
22944 do_string_sub(str
, pat
, sub
, flags
)
22951 regmatch_T regmatch
;
22959 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22961 p_cpo
= empty_option
;
22963 ga_init2(&ga
, 1, 200);
22965 do_all
= (flags
[0] == 'g');
22967 regmatch
.rm_ic
= p_ic
;
22968 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
22969 if (regmatch
.regprog
!= NULL
)
22972 while (vim_regexec_nl(®match
, str
, (colnr_T
)(tail
- str
)))
22975 * Get some space for a temporary buffer to do the substitution
22976 * into. It will contain:
22977 * - The text up to where the match is.
22978 * - The substituted text.
22979 * - The text after the match.
22981 sublen
= vim_regsub(®match
, sub
, tail
, FALSE
, TRUE
, FALSE
);
22982 if (ga_grow(&ga
, (int)(STRLEN(tail
) + sublen
-
22983 (regmatch
.endp
[0] - regmatch
.startp
[0]))) == FAIL
)
22989 /* copy the text up to where the match is */
22990 i
= (int)(regmatch
.startp
[0] - tail
);
22991 mch_memmove((char_u
*)ga
.ga_data
+ ga
.ga_len
, tail
, (size_t)i
);
22992 /* add the substituted text */
22993 (void)vim_regsub(®match
, sub
, (char_u
*)ga
.ga_data
22994 + ga
.ga_len
+ i
, TRUE
, TRUE
, FALSE
);
22995 ga
.ga_len
+= i
+ sublen
- 1;
22996 /* avoid getting stuck on a match with an empty string */
22997 if (tail
== regmatch
.endp
[0])
23001 *((char_u
*)ga
.ga_data
+ ga
.ga_len
) = *tail
++;
23006 tail
= regmatch
.endp
[0];
23014 if (ga
.ga_data
!= NULL
)
23015 STRCPY((char *)ga
.ga_data
+ ga
.ga_len
, tail
);
23017 vim_free(regmatch
.regprog
);
23020 ret
= vim_strsave(ga
.ga_data
== NULL
? str
: (char_u
*)ga
.ga_data
);
23022 if (p_cpo
== empty_option
)
23025 /* Darn, evaluating {sub} expression changed the value. */
23026 free_string_option(save_cpo
);
23031 #endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */