1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
11 * eval.c: Expression evaluation.
13 #if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
14 # include "vimio.h" /* for mch_open(), must be before vim.h */
19 #if defined(FEAT_EVAL) || defined(PROTO)
22 # include <time.h> /* for strftime() */
26 # include <time.h> /* for time_t */
29 #if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
33 #define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
35 #define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
39 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
40 * This avoids adding a pointer to the hashtab item.
41 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
42 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
43 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 static dictitem_T dumdi
;
46 #define DI2HIKEY(di) ((di)->di_key)
47 #define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
48 #define HI2DI(hi) HIKEY2DI((hi)->hi_key)
51 * Structure returned by get_lval() and used by set_var_lval().
53 * "name" points to the variable name.
56 * For a magic braces name:
57 * "name" points to the expanded variable name.
58 * "exp_name" is non-NULL, to be freed later.
60 * For an index in a list:
61 * "name" points to the (expanded) variable name.
62 * "exp_name" NULL or non-NULL, to be freed later.
63 * "tv" points to the (first) list item value
64 * "li" points to the (first) list item
65 * "range", "n1", "n2" and "empty2" indicate what items are used.
66 * For an existing Dict item:
67 * "name" points to the (expanded) variable name.
68 * "exp_name" NULL or non-NULL, to be freed later.
69 * "tv" points to the dict item value
71 * For a non-existing Dict item:
72 * "name" points to the (expanded) variable name.
73 * "exp_name" NULL or non-NULL, to be freed later.
74 * "tv" points to the Dictionary typval_T
75 * "newkey" is the key for the new item.
79 char_u
*ll_name
; /* start of variable name (can be NULL) */
80 char_u
*ll_exp_name
; /* NULL or expanded name in allocated memory. */
81 typval_T
*ll_tv
; /* Typeval of item being used. If "newkey"
82 isn't NULL it's the Dict to which to add
84 listitem_T
*ll_li
; /* The list item or NULL. */
85 list_T
*ll_list
; /* The list or NULL. */
86 int ll_range
; /* TRUE when a [i:j] range was used */
87 long ll_n1
; /* First index for list */
88 long ll_n2
; /* Second index for list range */
89 int ll_empty2
; /* Second index is empty: [i:] */
90 dict_T
*ll_dict
; /* The Dictionary or NULL */
91 dictitem_T
*ll_di
; /* The dictitem or NULL */
92 char_u
*ll_newkey
; /* New key for Dict in alloc. mem or NULL. */
96 static char *e_letunexp
= N_("E18: Unexpected characters in :let");
97 static char *e_listidx
= N_("E684: list index out of range: %ld");
98 static char *e_undefvar
= N_("E121: Undefined variable: %s");
99 static char *e_missbrac
= N_("E111: Missing ']'");
100 static char *e_listarg
= N_("E686: Argument of %s must be a List");
101 static char *e_listdictarg
= N_("E712: Argument of %s must be a List or Dictionary");
102 static char *e_emptykey
= N_("E713: Cannot use empty key for Dictionary");
103 static char *e_listreq
= N_("E714: List required");
104 static char *e_dictreq
= N_("E715: Dictionary required");
105 static char *e_toomanyarg
= N_("E118: Too many arguments for function: %s");
106 static char *e_dictkey
= N_("E716: Key not present in Dictionary: %s");
107 static char *e_funcexts
= N_("E122: Function %s already exists, add ! to replace it");
108 static char *e_funcdict
= N_("E717: Dictionary entry already exists");
109 static char *e_funcref
= N_("E718: Funcref required");
110 static char *e_dictrange
= N_("E719: Cannot use [:] with a Dictionary");
111 static char *e_letwrong
= N_("E734: Wrong variable type for %s=");
112 static char *e_nofunc
= N_("E130: Unknown function: %s");
113 static char *e_illvar
= N_("E461: Illegal variable name: %s");
116 * All user-defined global variables are stored in dictionary "globvardict".
117 * "globvars_var" is the variable that is used for "g:".
119 static dict_T globvardict
;
120 static dictitem_T globvars_var
;
121 #define globvarht globvardict.dv_hashtab
124 * Old Vim variables such as "v:version" are also available without the "v:".
125 * Also in functions. We need a special hashtable for them.
127 static hashtab_T compat_hashtab
;
130 * When recursively copying lists and dicts we need to remember which ones we
131 * have done to avoid endless recursiveness. This unique ID is used for that.
132 * The last bit is used for previous_funccal, ignored when comparing.
134 static int current_copyID
= 0;
136 #define COPYID_MASK (~0x1)
139 * Array to hold the hashtab with variables local to each sourced script.
140 * Each item holds a variable (nameless) that points to the dict_T.
148 static garray_T ga_scripts
= {0, 0, sizeof(scriptvar_T
), 4, NULL
};
149 #define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
150 #define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
152 static int echo_attr
= 0; /* attributes used for ":echo" */
154 /* Values for trans_function_name() argument: */
155 #define TFN_INT 1 /* internal function name OK */
156 #define TFN_QUIET 2 /* no error messages */
159 * Structure to hold info for a user function.
161 typedef struct ufunc ufunc_T
;
165 int uf_varargs
; /* variable nr of arguments */
167 int uf_calls
; /* nr of active calls */
168 garray_T uf_args
; /* arguments */
169 garray_T uf_lines
; /* function lines */
171 int uf_profiling
; /* TRUE when func is being profiled */
172 /* profiling the function as a whole */
173 int uf_tm_count
; /* nr of calls */
174 proftime_T uf_tm_total
; /* time spent in function + children */
175 proftime_T uf_tm_self
; /* time spent in function itself */
176 proftime_T uf_tm_children
; /* time spent in children this call */
177 /* profiling the function per line */
178 int *uf_tml_count
; /* nr of times line was executed */
179 proftime_T
*uf_tml_total
; /* time spent in a line + children */
180 proftime_T
*uf_tml_self
; /* time spent in a line itself */
181 proftime_T uf_tml_start
; /* start time for current line */
182 proftime_T uf_tml_children
; /* time spent in children for this line */
183 proftime_T uf_tml_wait
; /* start wait time for current line */
184 int uf_tml_idx
; /* index of line being timed; -1 if none */
185 int uf_tml_execed
; /* line being timed was executed */
187 scid_T uf_script_ID
; /* ID of script where function was defined,
188 used for s: variables */
189 int uf_refcount
; /* for numbered function: reference count */
190 char_u uf_name
[1]; /* name of function (actually longer); can
191 start with <SNR>123_ (<SNR> is K_SPECIAL
196 #define FC_ABORT 1 /* abort function on error */
197 #define FC_RANGE 2 /* function accepts range */
198 #define FC_DICT 4 /* Dict function, uses "self" */
201 * All user-defined functions are found in this hashtable.
203 static hashtab_T func_hashtab
;
205 /* The names of packages that once were loaded are remembered. */
206 static garray_T ga_loaded
= {0, 0, sizeof(char_u
*), 4, NULL
};
208 /* list heads for garbage collection */
209 static dict_T
*first_dict
= NULL
; /* list of all dicts */
210 static list_T
*first_list
= NULL
; /* list of all lists */
212 /* From user function to hashitem and back. */
213 static ufunc_T dumuf
;
214 #define UF2HIKEY(fp) ((fp)->uf_name)
215 #define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
216 #define HI2UF(hi) HIKEY2UF((hi)->hi_key)
218 #define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
219 #define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
221 #define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
222 #define VAR_SHORT_LEN 20 /* short variable name length */
223 #define FIXVAR_CNT 12 /* number of fixed variables */
225 /* structure to hold info for a function that is currently being executed. */
226 typedef struct funccall_S funccall_T
;
230 ufunc_T
*func
; /* function being called */
231 int linenr
; /* next line to be executed */
232 int returned
; /* ":return" used */
233 struct /* fixed variables for arguments */
235 dictitem_T var
; /* variable (without room for name) */
236 char_u room
[VAR_SHORT_LEN
]; /* room for the name */
237 } fixvar
[FIXVAR_CNT
];
238 dict_T l_vars
; /* l: local function variables */
239 dictitem_T l_vars_var
; /* variable for l: scope */
240 dict_T l_avars
; /* a: argument variables */
241 dictitem_T l_avars_var
; /* variable for a: scope */
242 list_T l_varlist
; /* list for a:000 */
243 listitem_T l_listitems
[MAX_FUNC_ARGS
]; /* listitems for a:000 */
244 typval_T
*rettv
; /* return value */
245 linenr_T breakpoint
; /* next line with breakpoint or zero */
246 int dbg_tick
; /* debug_tick when breakpoint was set */
247 int level
; /* top nesting level of executed function */
249 proftime_T prof_child
; /* time spent in a child */
251 funccall_T
*caller
; /* calling function or NULL */
255 * Info used by a ":for" loop.
259 int fi_semicolon
; /* TRUE if ending in '; var]' */
260 int fi_varcount
; /* nr of variables in the list */
261 listwatch_T fi_lw
; /* keep an eye on the item used. */
262 list_T
*fi_list
; /* list being used */
266 * Struct used by trans_function_name()
270 dict_T
*fd_dict
; /* Dictionary used */
271 char_u
*fd_newkey
; /* new key in "dict" in allocated memory */
272 dictitem_T
*fd_di
; /* Dictionary item used */
277 * Array to hold the value of v: variables.
278 * The value is in a dictitem, so that it can also be used in the v: scope.
279 * The reason to use this table anyway is for very quick access to the
280 * variables with the VV_ defines.
284 /* values for vv_flags: */
285 #define VV_COMPAT 1 /* compatible, also used without "v:" */
286 #define VV_RO 2 /* read-only */
287 #define VV_RO_SBX 4 /* read-only in the sandbox */
289 #define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
293 char *vv_name
; /* name of variable, without v: */
294 dictitem_T vv_di
; /* value and name for key */
295 char vv_filler
[16]; /* space for LONGEST name below!!! */
296 char vv_flags
; /* VV_COMPAT, VV_RO, VV_RO_SBX */
300 * The order here must match the VV_ defines in vim.h!
301 * Initializing a union does not work, leave tv.vval empty to get zero's.
303 {VV_NAME("count", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
304 {VV_NAME("count1", VAR_NUMBER
), VV_RO
},
305 {VV_NAME("prevcount", VAR_NUMBER
), VV_RO
},
306 {VV_NAME("errmsg", VAR_STRING
), VV_COMPAT
},
307 {VV_NAME("warningmsg", VAR_STRING
), 0},
308 {VV_NAME("statusmsg", VAR_STRING
), 0},
309 {VV_NAME("shell_error", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
310 {VV_NAME("this_session", VAR_STRING
), VV_COMPAT
},
311 {VV_NAME("version", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
312 {VV_NAME("lnum", VAR_NUMBER
), VV_RO_SBX
},
313 {VV_NAME("termresponse", VAR_STRING
), VV_RO
},
314 {VV_NAME("fname", VAR_STRING
), VV_RO
},
315 {VV_NAME("lang", VAR_STRING
), VV_RO
},
316 {VV_NAME("lc_time", VAR_STRING
), VV_RO
},
317 {VV_NAME("ctype", VAR_STRING
), VV_RO
},
318 {VV_NAME("charconvert_from", VAR_STRING
), VV_RO
},
319 {VV_NAME("charconvert_to", VAR_STRING
), VV_RO
},
320 {VV_NAME("fname_in", VAR_STRING
), VV_RO
},
321 {VV_NAME("fname_out", VAR_STRING
), VV_RO
},
322 {VV_NAME("fname_new", VAR_STRING
), VV_RO
},
323 {VV_NAME("fname_diff", VAR_STRING
), VV_RO
},
324 {VV_NAME("cmdarg", VAR_STRING
), VV_RO
},
325 {VV_NAME("foldstart", VAR_NUMBER
), VV_RO_SBX
},
326 {VV_NAME("foldend", VAR_NUMBER
), VV_RO_SBX
},
327 {VV_NAME("folddashes", VAR_STRING
), VV_RO_SBX
},
328 {VV_NAME("foldlevel", VAR_NUMBER
), VV_RO_SBX
},
329 {VV_NAME("progname", VAR_STRING
), VV_RO
},
330 {VV_NAME("servername", VAR_STRING
), VV_RO
},
331 {VV_NAME("dying", VAR_NUMBER
), VV_RO
},
332 {VV_NAME("exception", VAR_STRING
), VV_RO
},
333 {VV_NAME("throwpoint", VAR_STRING
), VV_RO
},
334 {VV_NAME("register", VAR_STRING
), VV_RO
},
335 {VV_NAME("cmdbang", VAR_NUMBER
), VV_RO
},
336 {VV_NAME("insertmode", VAR_STRING
), VV_RO
},
337 {VV_NAME("val", VAR_UNKNOWN
), VV_RO
},
338 {VV_NAME("key", VAR_UNKNOWN
), VV_RO
},
339 {VV_NAME("profiling", VAR_NUMBER
), VV_RO
},
340 {VV_NAME("fcs_reason", VAR_STRING
), VV_RO
},
341 {VV_NAME("fcs_choice", VAR_STRING
), 0},
342 {VV_NAME("beval_bufnr", VAR_NUMBER
), VV_RO
},
343 {VV_NAME("beval_winnr", VAR_NUMBER
), VV_RO
},
344 {VV_NAME("beval_lnum", VAR_NUMBER
), VV_RO
},
345 {VV_NAME("beval_col", VAR_NUMBER
), VV_RO
},
346 {VV_NAME("beval_text", VAR_STRING
), VV_RO
},
347 {VV_NAME("scrollstart", VAR_STRING
), 0},
348 {VV_NAME("swapname", VAR_STRING
), VV_RO
},
349 {VV_NAME("swapchoice", VAR_STRING
), 0},
350 {VV_NAME("swapcommand", VAR_STRING
), VV_RO
},
351 {VV_NAME("char", VAR_STRING
), VV_RO
},
352 {VV_NAME("mouse_win", VAR_NUMBER
), 0},
353 {VV_NAME("mouse_lnum", VAR_NUMBER
), 0},
354 {VV_NAME("mouse_col", VAR_NUMBER
), 0},
355 {VV_NAME("operator", VAR_STRING
), VV_RO
},
356 {VV_NAME("searchforward", VAR_NUMBER
), 0},
357 {VV_NAME("oldfiles", VAR_LIST
), 0},
361 #define vv_type vv_di.di_tv.v_type
362 #define vv_nr vv_di.di_tv.vval.v_number
363 #define vv_float vv_di.di_tv.vval.v_float
364 #define vv_str vv_di.di_tv.vval.v_string
365 #define vv_list vv_di.di_tv.vval.v_list
366 #define vv_tv vv_di.di_tv
369 * The v: variables are stored in dictionary "vimvardict".
370 * "vimvars_var" is the variable that is used for the "l:" scope.
372 static dict_T vimvardict
;
373 static dictitem_T vimvars_var
;
374 #define vimvarht vimvardict.dv_hashtab
376 static void prepare_vimvar
__ARGS((int idx
, typval_T
*save_tv
));
377 static void restore_vimvar
__ARGS((int idx
, typval_T
*save_tv
));
378 #if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
379 static int call_vim_function
__ARGS((char_u
*func
, int argc
, char_u
**argv
, int safe
, typval_T
*rettv
));
381 static int ex_let_vars
__ARGS((char_u
*arg
, typval_T
*tv
, int copy
, int semicolon
, int var_count
, char_u
*nextchars
));
382 static char_u
*skip_var_list
__ARGS((char_u
*arg
, int *var_count
, int *semicolon
));
383 static char_u
*skip_var_one
__ARGS((char_u
*arg
));
384 static void list_hashtable_vars
__ARGS((hashtab_T
*ht
, char_u
*prefix
, int empty
, int *first
));
385 static void list_glob_vars
__ARGS((int *first
));
386 static void list_buf_vars
__ARGS((int *first
));
387 static void list_win_vars
__ARGS((int *first
));
389 static void list_tab_vars
__ARGS((int *first
));
391 static void list_vim_vars
__ARGS((int *first
));
392 static void list_script_vars
__ARGS((int *first
));
393 static void list_func_vars
__ARGS((int *first
));
394 static char_u
*list_arg_vars
__ARGS((exarg_T
*eap
, char_u
*arg
, int *first
));
395 static char_u
*ex_let_one
__ARGS((char_u
*arg
, typval_T
*tv
, int copy
, char_u
*endchars
, char_u
*op
));
396 static int check_changedtick
__ARGS((char_u
*arg
));
397 static char_u
*get_lval
__ARGS((char_u
*name
, typval_T
*rettv
, lval_T
*lp
, int unlet
, int skip
, int quiet
, int fne_flags
));
398 static void clear_lval
__ARGS((lval_T
*lp
));
399 static void set_var_lval
__ARGS((lval_T
*lp
, char_u
*endp
, typval_T
*rettv
, int copy
, char_u
*op
));
400 static int tv_op
__ARGS((typval_T
*tv1
, typval_T
*tv2
, char_u
*op
));
401 static void list_add_watch
__ARGS((list_T
*l
, listwatch_T
*lw
));
402 static void list_rem_watch
__ARGS((list_T
*l
, listwatch_T
*lwrem
));
403 static void list_fix_watch
__ARGS((list_T
*l
, listitem_T
*item
));
404 static void ex_unletlock
__ARGS((exarg_T
*eap
, char_u
*argstart
, int deep
));
405 static int do_unlet_var
__ARGS((lval_T
*lp
, char_u
*name_end
, int forceit
));
406 static int do_lock_var
__ARGS((lval_T
*lp
, char_u
*name_end
, int deep
, int lock
));
407 static void item_lock
__ARGS((typval_T
*tv
, int deep
, int lock
));
408 static int tv_islocked
__ARGS((typval_T
*tv
));
410 static int eval0
__ARGS((char_u
*arg
, typval_T
*rettv
, char_u
**nextcmd
, int evaluate
));
411 static int eval1
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
412 static int eval2
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
413 static int eval3
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
414 static int eval4
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
415 static int eval5
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
416 static int eval6
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int want_string
));
417 static int eval7
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int want_string
));
419 static int eval_index
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int verbose
));
420 static int get_option_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
421 static int get_string_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
422 static int get_lit_string_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
423 static int get_list_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
424 static int rettv_list_alloc
__ARGS((typval_T
*rettv
));
425 static listitem_T
*listitem_alloc
__ARGS((void));
426 static void listitem_free
__ARGS((listitem_T
*item
));
427 static void listitem_remove
__ARGS((list_T
*l
, listitem_T
*item
));
428 static long list_len
__ARGS((list_T
*l
));
429 static int list_equal
__ARGS((list_T
*l1
, list_T
*l2
, int ic
));
430 static int dict_equal
__ARGS((dict_T
*d1
, dict_T
*d2
, int ic
));
431 static int tv_equal
__ARGS((typval_T
*tv1
, typval_T
*tv2
, int ic
));
432 static listitem_T
*list_find
__ARGS((list_T
*l
, long n
));
433 static long list_find_nr
__ARGS((list_T
*l
, long idx
, int *errorp
));
434 static long list_idx_of_item
__ARGS((list_T
*l
, listitem_T
*item
));
435 static void list_append
__ARGS((list_T
*l
, listitem_T
*item
));
436 static int list_append_number
__ARGS((list_T
*l
, varnumber_T n
));
437 static int list_insert_tv
__ARGS((list_T
*l
, typval_T
*tv
, listitem_T
*item
));
438 static int list_extend
__ARGS((list_T
*l1
, list_T
*l2
, listitem_T
*bef
));
439 static int list_concat
__ARGS((list_T
*l1
, list_T
*l2
, typval_T
*tv
));
440 static list_T
*list_copy
__ARGS((list_T
*orig
, int deep
, int copyID
));
441 static void list_remove
__ARGS((list_T
*l
, listitem_T
*item
, listitem_T
*item2
));
442 static char_u
*list2string
__ARGS((typval_T
*tv
, int copyID
));
443 static int list_join
__ARGS((garray_T
*gap
, list_T
*l
, char_u
*sep
, int echo
, int copyID
));
444 static int free_unref_items
__ARGS((int copyID
));
445 static void set_ref_in_ht
__ARGS((hashtab_T
*ht
, int copyID
));
446 static void set_ref_in_list
__ARGS((list_T
*l
, int copyID
));
447 static void set_ref_in_item
__ARGS((typval_T
*tv
, int copyID
));
448 static void dict_unref
__ARGS((dict_T
*d
));
449 static void dict_free
__ARGS((dict_T
*d
, int recurse
));
450 static dictitem_T
*dictitem_copy
__ARGS((dictitem_T
*org
));
451 static void dictitem_remove
__ARGS((dict_T
*dict
, dictitem_T
*item
));
452 static dict_T
*dict_copy
__ARGS((dict_T
*orig
, int deep
, int copyID
));
453 static long dict_len
__ARGS((dict_T
*d
));
454 static dictitem_T
*dict_find
__ARGS((dict_T
*d
, char_u
*key
, int len
));
455 static char_u
*dict2string
__ARGS((typval_T
*tv
, int copyID
));
456 static int get_dict_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
457 static char_u
*echo_string
__ARGS((typval_T
*tv
, char_u
**tofree
, char_u
*numbuf
, int copyID
));
458 static char_u
*tv2string
__ARGS((typval_T
*tv
, char_u
**tofree
, char_u
*numbuf
, int copyID
));
459 static char_u
*string_quote
__ARGS((char_u
*str
, int function
));
461 static int string2float
__ARGS((char_u
*text
, float_T
*value
));
463 static int get_env_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
464 static int find_internal_func
__ARGS((char_u
*name
));
465 static char_u
*deref_func_name
__ARGS((char_u
*name
, int *lenp
));
466 static int get_func_tv
__ARGS((char_u
*name
, int len
, typval_T
*rettv
, char_u
**arg
, linenr_T firstline
, linenr_T lastline
, int *doesrange
, int evaluate
, dict_T
*selfdict
));
467 static int call_func
__ARGS((char_u
*name
, int len
, typval_T
*rettv
, int argcount
, typval_T
*argvars
, linenr_T firstline
, linenr_T lastline
, int *doesrange
, int evaluate
, dict_T
*selfdict
));
468 static void emsg_funcname
__ARGS((char *ermsg
, char_u
*name
));
469 static int non_zero_arg
__ARGS((typval_T
*argvars
));
472 static void f_abs
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
474 static void f_add
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
475 static void f_append
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
476 static void f_argc
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
477 static void f_argidx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
478 static void f_argv
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
480 static void f_atan
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
482 static void f_browse
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
483 static void f_browsedir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
484 static void f_bufexists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
485 static void f_buflisted
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
486 static void f_bufloaded
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
487 static void f_bufname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
488 static void f_bufnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
489 static void f_bufwinnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
490 static void f_byte2line
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
491 static void f_byteidx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
492 static void f_call
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
494 static void f_ceil
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
496 static void f_changenr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
497 static void f_char2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
498 static void f_cindent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
499 static void f_clearmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
500 static void f_col
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
501 #if defined(FEAT_INS_EXPAND)
502 static void f_complete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
503 static void f_complete_add
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
504 static void f_complete_check
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
506 static void f_confirm
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
507 static void f_copy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
509 static void f_cos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
511 static void f_count
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
512 static void f_cscope_connection
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
513 static void f_cursor
__ARGS((typval_T
*argsvars
, typval_T
*rettv
));
514 static void f_deepcopy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
515 static void f_delete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
516 static void f_did_filetype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
517 static void f_diff_filler
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
518 static void f_diff_hlID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
519 static void f_empty
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
520 static void f_escape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
521 static void f_eval
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
522 static void f_eventhandler
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
523 static void f_executable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
524 static void f_exists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
525 static void f_expand
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
526 static void f_extend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
527 static void f_feedkeys
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
528 static void f_filereadable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
529 static void f_filewritable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
530 static void f_filter
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
531 static void f_finddir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
532 static void f_findfile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
534 static void f_float2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
535 static void f_floor
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
537 static void f_fnameescape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
538 static void f_fnamemodify
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
539 static void f_foldclosed
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
540 static void f_foldclosedend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
541 static void f_foldlevel
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
542 static void f_foldtext
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
543 static void f_foldtextresult
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
544 static void f_foreground
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
545 static void f_function
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
546 static void f_garbagecollect
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
547 static void f_get
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
548 static void f_getbufline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
549 static void f_getbufvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
550 static void f_getchar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
551 static void f_getcharmod
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
552 static void f_getcmdline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
553 static void f_getcmdpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
554 static void f_getcmdtype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
555 static void f_getcwd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
556 static void f_getfontname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
557 static void f_getfperm
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
558 static void f_getfsize
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
559 static void f_getftime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
560 static void f_getftype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
561 static void f_getline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
562 static void f_getmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
563 static void f_getpid
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
564 static void f_getpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
565 static void f_getqflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
566 static void f_getreg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
567 static void f_getregtype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
568 static void f_gettabwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
569 static void f_getwinposx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
570 static void f_getwinposy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
571 static void f_getwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
572 static void f_glob
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
573 static void f_globpath
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
574 static void f_has
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
575 static void f_has_key
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
576 static void f_haslocaldir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
577 static void f_hasmapto
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
578 static void f_histadd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
579 static void f_histdel
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
580 static void f_histget
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
581 static void f_histnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
582 static void f_hlID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
583 static void f_hlexists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
584 static void f_hostname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
585 static void f_iconv
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
586 static void f_indent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
587 static void f_index
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
588 static void f_input
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
589 static void f_inputdialog
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
590 static void f_inputlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
591 static void f_inputrestore
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
592 static void f_inputsave
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
593 static void f_inputsecret
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
594 static void f_insert
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
595 static void f_isdirectory
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
596 static void f_islocked
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
597 static void f_items
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
598 static void f_join
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
599 static void f_keys
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
600 static void f_last_buffer_nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
601 static void f_len
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
602 static void f_libcall
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
603 static void f_libcallnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
604 static void f_line
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
605 static void f_line2byte
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
606 static void f_lispindent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
607 static void f_localtime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
609 static void f_log10
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
611 static void f_map
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
612 static void f_maparg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
613 static void f_mapcheck
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
614 static void f_match
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
615 static void f_matchadd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
616 static void f_matcharg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
617 static void f_matchdelete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
618 static void f_matchend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
619 static void f_matchlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
620 static void f_matchstr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
621 static void f_max
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
622 static void f_migemo
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
623 static void f_min
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
625 static void f_mkdir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
627 static void f_mode
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
629 static void f_mzeval
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
631 static void f_nextnonblank
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
632 static void f_nr2char
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
633 static void f_pathshorten
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
635 static void f_pow
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
637 static void f_prevnonblank
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
638 static void f_printf
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
639 static void f_pumvisible
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
640 static void f_range
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
641 static void f_readfile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
642 static void f_reltime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
643 static void f_reltimestr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
644 static void f_remote_expr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
645 static void f_remote_foreground
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
646 static void f_remote_peek
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
647 static void f_remote_read
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
648 static void f_remote_send
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
649 static void f_remove
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
650 static void f_rename
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
651 static void f_repeat
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
652 static void f_resolve
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
653 static void f_reverse
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
655 static void f_round
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
657 static void f_search
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
658 static void f_searchdecl
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
659 static void f_searchpair
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
660 static void f_searchpairpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
661 static void f_searchpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
662 static void f_server2client
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
663 static void f_serverlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
664 static void f_setbufvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
665 static void f_setcmdpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
666 static void f_setline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
667 static void f_setloclist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
668 static void f_setmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
669 static void f_setpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
670 static void f_setqflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
671 static void f_setreg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
672 static void f_settabwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
673 static void f_setwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
674 static void f_shellescape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
675 static void f_simplify
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
677 static void f_sin
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
679 static void f_sort
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
680 static void f_soundfold
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
681 static void f_spellbadword
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
682 static void f_spellsuggest
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
683 static void f_split
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
685 static void f_sqrt
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
686 static void f_str2float
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
688 static void f_str2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
690 static void f_strftime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
692 static void f_stridx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
693 static void f_string
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
694 static void f_strlen
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
695 static void f_strpart
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
696 static void f_strridx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
697 static void f_strtrans
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
698 static void f_submatch
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
699 static void f_substitute
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
700 static void f_synID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
701 static void f_synIDattr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
702 static void f_synIDtrans
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
703 static void f_synstack
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
704 static void f_system
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
705 static void f_tabpagebuflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
706 static void f_tabpagenr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
707 static void f_tabpagewinnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
708 static void f_taglist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
709 static void f_tagfiles
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
710 static void f_tempname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
711 static void f_test
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
712 static void f_tolower
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
713 static void f_toupper
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
714 static void f_tr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
716 static void f_trunc
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
718 static void f_type
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
719 static void f_values
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
720 static void f_virtcol
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
721 static void f_visualmode
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
722 static void f_winbufnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
723 static void f_wincol
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
724 static void f_winheight
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
725 static void f_winline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
726 static void f_winnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
727 static void f_winrestcmd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
728 static void f_winrestview
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
729 static void f_winsaveview
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
730 static void f_winwidth
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
731 static void f_writefile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
733 static int list2fpos
__ARGS((typval_T
*arg
, pos_T
*posp
, int *fnump
));
734 static pos_T
*var2fpos
__ARGS((typval_T
*varp
, int dollar_lnum
, int *fnum
));
735 static int get_env_len
__ARGS((char_u
**arg
));
736 static int get_id_len
__ARGS((char_u
**arg
));
737 static int get_name_len
__ARGS((char_u
**arg
, char_u
**alias
, int evaluate
, int verbose
));
738 static char_u
*find_name_end
__ARGS((char_u
*arg
, char_u
**expr_start
, char_u
**expr_end
, int flags
));
739 #define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
740 #define FNE_CHECK_START 2 /* find_name_end(): check name starts with
742 static char_u
* make_expanded_name
__ARGS((char_u
*in_start
, char_u
*expr_start
, char_u
*expr_end
, char_u
*in_end
));
743 static int eval_isnamec
__ARGS((int c
));
744 static int eval_isnamec1
__ARGS((int c
));
745 static int get_var_tv
__ARGS((char_u
*name
, int len
, typval_T
*rettv
, int verbose
));
746 static int handle_subscript
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int verbose
));
747 static typval_T
*alloc_tv
__ARGS((void));
748 static typval_T
*alloc_string_tv
__ARGS((char_u
*string
));
749 static void init_tv
__ARGS((typval_T
*varp
));
750 static long get_tv_number
__ARGS((typval_T
*varp
));
751 static linenr_T get_tv_lnum
__ARGS((typval_T
*argvars
));
752 static linenr_T get_tv_lnum_buf
__ARGS((typval_T
*argvars
, buf_T
*buf
));
753 static char_u
*get_tv_string
__ARGS((typval_T
*varp
));
754 static char_u
*get_tv_string_buf
__ARGS((typval_T
*varp
, char_u
*buf
));
755 static char_u
*get_tv_string_buf_chk
__ARGS((typval_T
*varp
, char_u
*buf
));
756 static dictitem_T
*find_var
__ARGS((char_u
*name
, hashtab_T
**htp
));
757 static dictitem_T
*find_var_in_ht
__ARGS((hashtab_T
*ht
, char_u
*varname
, int writing
));
758 static hashtab_T
*find_var_ht
__ARGS((char_u
*name
, char_u
**varname
));
759 static void vars_clear_ext
__ARGS((hashtab_T
*ht
, int free_val
));
760 static void delete_var
__ARGS((hashtab_T
*ht
, hashitem_T
*hi
));
761 static void list_one_var
__ARGS((dictitem_T
*v
, char_u
*prefix
, int *first
));
762 static void list_one_var_a
__ARGS((char_u
*prefix
, char_u
*name
, int type
, char_u
*string
, int *first
));
763 static void set_var
__ARGS((char_u
*name
, typval_T
*varp
, int copy
));
764 static int var_check_ro
__ARGS((int flags
, char_u
*name
));
765 static int var_check_fixed
__ARGS((int flags
, char_u
*name
));
766 static int tv_check_lock
__ARGS((int lock
, char_u
*name
));
767 static int item_copy
__ARGS((typval_T
*from
, typval_T
*to
, int deep
, int copyID
));
768 static char_u
*find_option_end
__ARGS((char_u
**arg
, int *opt_flags
));
769 static char_u
*trans_function_name
__ARGS((char_u
**pp
, int skip
, int flags
, funcdict_T
*fd
));
770 static int eval_fname_script
__ARGS((char_u
*p
));
771 static int eval_fname_sid
__ARGS((char_u
*p
));
772 static void list_func_head
__ARGS((ufunc_T
*fp
, int indent
));
773 static ufunc_T
*find_func
__ARGS((char_u
*name
));
774 static int function_exists
__ARGS((char_u
*name
));
775 static int builtin_function
__ARGS((char_u
*name
));
777 static void func_do_profile
__ARGS((ufunc_T
*fp
));
778 static void prof_sort_list
__ARGS((FILE *fd
, ufunc_T
**sorttab
, int st_len
, char *title
, int prefer_self
));
779 static void prof_func_line
__ARGS((FILE *fd
, int count
, proftime_T
*total
, proftime_T
*self
, int prefer_self
));
784 prof_total_cmp
__ARGS((const void *s1
, const void *s2
));
789 prof_self_cmp
__ARGS((const void *s1
, const void *s2
));
791 static int script_autoload
__ARGS((char_u
*name
, int reload
));
792 static char_u
*autoload_name
__ARGS((char_u
*name
));
793 static void cat_func_name
__ARGS((char_u
*buf
, ufunc_T
*fp
));
794 static void func_free
__ARGS((ufunc_T
*fp
));
795 static void func_unref
__ARGS((char_u
*name
));
796 static void func_ref
__ARGS((char_u
*name
));
797 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
));
798 static int can_free_funccal
__ARGS((funccall_T
*fc
, int copyID
)) ;
799 static void free_funccal
__ARGS((funccall_T
*fc
, int free_val
));
800 static void add_nr_var
__ARGS((dict_T
*dp
, dictitem_T
*v
, char *name
, varnumber_T nr
));
801 static win_T
*find_win_by_nr
__ARGS((typval_T
*vp
, tabpage_T
*tp
));
802 static void getwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int off
));
803 static int searchpair_cmn
__ARGS((typval_T
*argvars
, pos_T
*match_pos
));
804 static int search_cmn
__ARGS((typval_T
*argvars
, pos_T
*match_pos
, int *flagsp
));
805 static void setwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int off
));
807 /* Character used as separated in autoload function/variable names. */
808 #define AUTOLOAD_CHAR '#'
811 * Initialize the global and v: variables.
819 init_var_dict(&globvardict
, &globvars_var
);
820 init_var_dict(&vimvardict
, &vimvars_var
);
821 hash_init(&compat_hashtab
);
822 hash_init(&func_hashtab
);
824 for (i
= 0; i
< VV_LEN
; ++i
)
827 STRCPY(p
->vv_di
.di_key
, p
->vv_name
);
828 if (p
->vv_flags
& VV_RO
)
829 p
->vv_di
.di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
830 else if (p
->vv_flags
& VV_RO_SBX
)
831 p
->vv_di
.di_flags
= DI_FLAGS_RO_SBX
| DI_FLAGS_FIX
;
833 p
->vv_di
.di_flags
= DI_FLAGS_FIX
;
835 /* add to v: scope dict, unless the value is not always available */
836 if (p
->vv_type
!= VAR_UNKNOWN
)
837 hash_add(&vimvarht
, p
->vv_di
.di_key
);
838 if (p
->vv_flags
& VV_COMPAT
)
839 /* add to compat scope dict */
840 hash_add(&compat_hashtab
, p
->vv_di
.di_key
);
842 set_vim_var_nr(VV_SEARCHFORWARD
, 1L);
845 #if defined(EXITFREE) || defined(PROTO)
852 for (i
= 0; i
< VV_LEN
; ++i
)
855 if (p
->vv_di
.di_tv
.v_type
== VAR_STRING
)
860 else if (p
->vv_di
.di_tv
.v_type
== VAR_LIST
)
862 list_unref(p
->vv_list
);
866 hash_clear(&vimvarht
);
867 hash_init(&vimvarht
); /* garbage_collect() will access it */
868 hash_clear(&compat_hashtab
);
870 /* script-local variables */
871 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
872 vars_clear(&SCRIPT_VARS(i
));
873 ga_clear(&ga_scripts
);
876 /* global variables */
877 vars_clear(&globvarht
);
879 /* autoloaded script names */
880 ga_clear_strings(&ga_loaded
);
882 /* unreferenced lists and dicts */
883 (void)garbage_collect();
886 free_all_functions();
887 hash_clear(&func_hashtab
);
892 * Return the name of the executed function.
898 return ((funccall_T
*)cookie
)->func
->uf_name
;
902 * Return the address holding the next breakpoint line for a funccall cookie.
905 func_breakpoint(cookie
)
908 return &((funccall_T
*)cookie
)->breakpoint
;
912 * Return the address holding the debug tick for a funccall cookie.
915 func_dbg_tick(cookie
)
918 return &((funccall_T
*)cookie
)->dbg_tick
;
922 * Return the nesting level for a funccall cookie.
928 return ((funccall_T
*)cookie
)->level
;
931 /* pointer to funccal for currently active function */
932 funccall_T
*current_funccal
= NULL
;
934 /* pointer to list of previously used funccal, still around because some
935 * item in it is still being used. */
936 funccall_T
*previous_funccal
= NULL
;
939 * Return TRUE when a function was ended by a ":return" command.
942 current_func_returned()
944 return current_funccal
->returned
;
949 * Set an internal variable to a string value. Creates the variable if it does
953 set_internal_string_var(name
, value
)
960 val
= vim_strsave(value
);
963 tvp
= alloc_string_tv(val
);
966 set_var(name
, tvp
, FALSE
);
972 static lval_T
*redir_lval
= NULL
;
973 static garray_T redir_ga
; /* only valid when redir_lval is not NULL */
974 static char_u
*redir_endp
= NULL
;
975 static char_u
*redir_varname
= NULL
;
978 * Start recording command output to a variable
979 * Returns OK if successfully completed the setup. FAIL otherwise.
982 var_redir_start(name
, append
)
984 int append
; /* append to an existing variable */
990 /* Catch a bad name early. */
991 if (!eval_isnamec1(*name
))
997 /* Make a copy of the name, it is used in redir_lval until redir ends. */
998 redir_varname
= vim_strsave(name
);
999 if (redir_varname
== NULL
)
1002 redir_lval
= (lval_T
*)alloc_clear((unsigned)sizeof(lval_T
));
1003 if (redir_lval
== NULL
)
1009 /* The output is stored in growarray "redir_ga" until redirection ends. */
1010 ga_init2(&redir_ga
, (int)sizeof(char), 500);
1012 /* Parse the variable name (can be a dict or list entry). */
1013 redir_endp
= get_lval(redir_varname
, NULL
, redir_lval
, FALSE
, FALSE
, FALSE
,
1015 if (redir_endp
== NULL
|| redir_lval
->ll_name
== NULL
|| *redir_endp
!= NUL
)
1017 if (redir_endp
!= NULL
&& *redir_endp
!= NUL
)
1018 /* Trailing characters are present after the variable name */
1019 EMSG(_(e_trailing
));
1022 redir_endp
= NULL
; /* don't store a value, only cleanup */
1027 /* check if we can write to the variable: set it to or append an empty
1029 save_emsg
= did_emsg
;
1031 tv
.v_type
= VAR_STRING
;
1032 tv
.vval
.v_string
= (char_u
*)"";
1034 set_var_lval(redir_lval
, redir_endp
, &tv
, TRUE
, (char_u
*)".");
1036 set_var_lval(redir_lval
, redir_endp
, &tv
, TRUE
, (char_u
*)"=");
1038 did_emsg
|= save_emsg
;
1041 redir_endp
= NULL
; /* don't store a value, only cleanup */
1045 if (redir_lval
->ll_newkey
!= NULL
)
1047 /* Dictionary item was created, don't do it again. */
1048 vim_free(redir_lval
->ll_newkey
);
1049 redir_lval
->ll_newkey
= NULL
;
1056 * Append "value[value_len]" to the variable set by var_redir_start().
1057 * The actual appending is postponed until redirection ends, because the value
1058 * appended may in fact be the string we write to, changing it may cause freed
1059 * memory to be used:
1065 var_redir_str(value
, value_len
)
1071 if (redir_lval
== NULL
)
1074 if (value_len
== -1)
1075 len
= (int)STRLEN(value
); /* Append the entire string */
1077 len
= value_len
; /* Append only "value_len" characters */
1079 if (ga_grow(&redir_ga
, len
) == OK
)
1081 mch_memmove((char *)redir_ga
.ga_data
+ redir_ga
.ga_len
, value
, len
);
1082 redir_ga
.ga_len
+= len
;
1089 * Stop redirecting command output to a variable.
1090 * Frees the allocated memory.
1097 if (redir_lval
!= NULL
)
1099 /* If there was no error: assign the text to the variable. */
1100 if (redir_endp
!= NULL
)
1102 ga_append(&redir_ga
, NUL
); /* Append the trailing NUL. */
1103 tv
.v_type
= VAR_STRING
;
1104 tv
.vval
.v_string
= redir_ga
.ga_data
;
1105 set_var_lval(redir_lval
, redir_endp
, &tv
, FALSE
, (char_u
*)".");
1108 /* free the collected output */
1109 vim_free(redir_ga
.ga_data
);
1110 redir_ga
.ga_data
= NULL
;
1112 clear_lval(redir_lval
);
1113 vim_free(redir_lval
);
1116 vim_free(redir_varname
);
1117 redir_varname
= NULL
;
1120 # if defined(FEAT_MBYTE) || defined(PROTO)
1122 eval_charconvert(enc_from
, enc_to
, fname_from
, fname_to
)
1130 set_vim_var_string(VV_CC_FROM
, enc_from
, -1);
1131 set_vim_var_string(VV_CC_TO
, enc_to
, -1);
1132 set_vim_var_string(VV_FNAME_IN
, fname_from
, -1);
1133 set_vim_var_string(VV_FNAME_OUT
, fname_to
, -1);
1134 if (eval_to_bool(p_ccv
, &err
, NULL
, FALSE
))
1136 set_vim_var_string(VV_CC_FROM
, NULL
, -1);
1137 set_vim_var_string(VV_CC_TO
, NULL
, -1);
1138 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1139 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1147 # if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1149 eval_printexpr(fname
, args
)
1155 set_vim_var_string(VV_FNAME_IN
, fname
, -1);
1156 set_vim_var_string(VV_CMDARG
, args
, -1);
1157 if (eval_to_bool(p_pexpr
, &err
, NULL
, FALSE
))
1159 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1160 set_vim_var_string(VV_CMDARG
, NULL
, -1);
1171 # if defined(FEAT_DIFF) || defined(PROTO)
1173 eval_diff(origfile
, newfile
, outfile
)
1180 set_vim_var_string(VV_FNAME_IN
, origfile
, -1);
1181 set_vim_var_string(VV_FNAME_NEW
, newfile
, -1);
1182 set_vim_var_string(VV_FNAME_OUT
, outfile
, -1);
1183 (void)eval_to_bool(p_dex
, &err
, NULL
, FALSE
);
1184 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1185 set_vim_var_string(VV_FNAME_NEW
, NULL
, -1);
1186 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1190 eval_patch(origfile
, difffile
, outfile
)
1197 set_vim_var_string(VV_FNAME_IN
, origfile
, -1);
1198 set_vim_var_string(VV_FNAME_DIFF
, difffile
, -1);
1199 set_vim_var_string(VV_FNAME_OUT
, outfile
, -1);
1200 (void)eval_to_bool(p_pex
, &err
, NULL
, FALSE
);
1201 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1202 set_vim_var_string(VV_FNAME_DIFF
, NULL
, -1);
1203 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1208 * Top level evaluation function, returning a boolean.
1209 * Sets "error" to TRUE if there was an error.
1210 * Return TRUE or FALSE.
1213 eval_to_bool(arg
, error
, nextcmd
, skip
)
1217 int skip
; /* only parse, don't execute */
1224 if (eval0(arg
, &tv
, nextcmd
, !skip
) == FAIL
)
1231 retval
= (get_tv_number_chk(&tv
, error
) != 0);
1242 * Top level evaluation function, returning a string. If "skip" is TRUE,
1243 * only parsing to "nextcmd" is done, without reporting errors. Return
1244 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1247 eval_to_string_skip(arg
, nextcmd
, skip
)
1250 int skip
; /* only parse, don't execute */
1257 if (eval0(arg
, &tv
, nextcmd
, !skip
) == FAIL
|| skip
)
1261 retval
= vim_strsave(get_tv_string(&tv
));
1271 * Skip over an expression at "*pp".
1272 * Return FAIL for an error, OK otherwise.
1280 *pp
= skipwhite(*pp
);
1281 return eval1(pp
, &rettv
, FALSE
);
1285 * Top level evaluation function, returning a string.
1286 * When "convert" is TRUE convert a List into a sequence of lines and convert
1287 * a Float to a String.
1288 * Return pointer to allocated memory, or NULL for failure.
1291 eval_to_string(arg
, nextcmd
, convert
)
1300 char_u numbuf
[NUMBUFLEN
];
1303 if (eval0(arg
, &tv
, nextcmd
, TRUE
) == FAIL
)
1307 if (convert
&& tv
.v_type
== VAR_LIST
)
1309 ga_init2(&ga
, (int)sizeof(char), 80);
1310 if (tv
.vval
.v_list
!= NULL
)
1311 list_join(&ga
, tv
.vval
.v_list
, (char_u
*)"\n", TRUE
, 0);
1312 ga_append(&ga
, NUL
);
1313 retval
= (char_u
*)ga
.ga_data
;
1316 else if (convert
&& tv
.v_type
== VAR_FLOAT
)
1318 vim_snprintf((char *)numbuf
, NUMBUFLEN
, "%g", tv
.vval
.v_float
);
1319 retval
= vim_strsave(numbuf
);
1323 retval
= vim_strsave(get_tv_string(&tv
));
1331 * Call eval_to_string() without using current local variables and using
1332 * textlock. When "use_sandbox" is TRUE use the sandbox.
1335 eval_to_string_safe(arg
, nextcmd
, use_sandbox
)
1341 void *save_funccalp
;
1343 save_funccalp
= save_funccal();
1347 retval
= eval_to_string(arg
, nextcmd
, FALSE
);
1351 restore_funccal(save_funccalp
);
1356 * Top level evaluation function, returning a number.
1357 * Evaluates "expr" silently.
1358 * Returns -1 for an error.
1361 eval_to_number(expr
)
1366 char_u
*p
= skipwhite(expr
);
1370 if (eval1(&p
, &rettv
, TRUE
) == FAIL
)
1374 retval
= get_tv_number_chk(&rettv
, NULL
);
1383 * Prepare v: variable "idx" to be used.
1384 * Save the current typeval in "save_tv".
1385 * When not used yet add the variable to the v: hashtable.
1388 prepare_vimvar(idx
, save_tv
)
1392 *save_tv
= vimvars
[idx
].vv_tv
;
1393 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1394 hash_add(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1398 * Restore v: variable "idx" to typeval "save_tv".
1399 * When no longer defined, remove the variable from the v: hashtable.
1402 restore_vimvar(idx
, save_tv
)
1408 vimvars
[idx
].vv_tv
= *save_tv
;
1409 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1411 hi
= hash_find(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1412 if (HASHITEM_EMPTY(hi
))
1413 EMSG2(_(e_intern2
), "restore_vimvar()");
1415 hash_remove(&vimvarht
, hi
);
1419 #if defined(FEAT_SPELL) || defined(PROTO)
1421 * Evaluate an expression to a list with suggestions.
1422 * For the "expr:" part of 'spellsuggest'.
1423 * Returns NULL when there is an error.
1426 eval_spell_expr(badword
, expr
)
1432 list_T
*list
= NULL
;
1433 char_u
*p
= skipwhite(expr
);
1435 /* Set "v:val" to the bad word. */
1436 prepare_vimvar(VV_VAL
, &save_val
);
1437 vimvars
[VV_VAL
].vv_type
= VAR_STRING
;
1438 vimvars
[VV_VAL
].vv_str
= badword
;
1442 if (eval1(&p
, &rettv
, TRUE
) == OK
)
1444 if (rettv
.v_type
!= VAR_LIST
)
1447 list
= rettv
.vval
.v_list
;
1452 restore_vimvar(VV_VAL
, &save_val
);
1458 * "list" is supposed to contain two items: a word and a number. Return the
1459 * word in "pp" and the number as the return value.
1460 * Return -1 if anything isn't right.
1461 * Used to get the good word and score from the eval_spell_expr() result.
1464 get_spellword(list
, pp
)
1470 li
= list
->lv_first
;
1473 *pp
= get_tv_string(&li
->li_tv
);
1478 return get_tv_number(&li
->li_tv
);
1483 * Top level evaluation function.
1484 * Returns an allocated typval_T with the result.
1485 * Returns NULL when there is an error.
1488 eval_expr(arg
, nextcmd
)
1494 tv
= (typval_T
*)alloc(sizeof(typval_T
));
1495 if (tv
!= NULL
&& eval0(arg
, tv
, nextcmd
, TRUE
) == FAIL
)
1505 #if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1506 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1508 * Call some vimL function and return the result in "*rettv".
1509 * Uses argv[argc] for the function arguments. Only Number and String
1510 * arguments are currently supported.
1511 * Returns OK or FAIL.
1514 call_vim_function(func
, argc
, argv
, safe
, rettv
)
1518 int safe
; /* use the sandbox */
1526 void *save_funccalp
= NULL
;
1529 argvars
= (typval_T
*)alloc((unsigned)((argc
+ 1) * sizeof(typval_T
)));
1530 if (argvars
== NULL
)
1533 for (i
= 0; i
< argc
; i
++)
1535 /* Pass a NULL or empty argument as an empty string */
1536 if (argv
[i
] == NULL
|| *argv
[i
] == NUL
)
1538 argvars
[i
].v_type
= VAR_STRING
;
1539 argvars
[i
].vval
.v_string
= (char_u
*)"";
1543 /* Recognize a number argument, the others must be strings. */
1544 vim_str2nr(argv
[i
], NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
1545 if (len
!= 0 && len
== (int)STRLEN(argv
[i
]))
1547 argvars
[i
].v_type
= VAR_NUMBER
;
1548 argvars
[i
].vval
.v_number
= n
;
1552 argvars
[i
].v_type
= VAR_STRING
;
1553 argvars
[i
].vval
.v_string
= argv
[i
];
1559 save_funccalp
= save_funccal();
1563 rettv
->v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
1564 ret
= call_func(func
, (int)STRLEN(func
), rettv
, argc
, argvars
,
1565 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
1566 &doesrange
, TRUE
, NULL
);
1570 restore_funccal(save_funccalp
);
1580 # if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1582 * Call vimL function "func" and return the result as a string.
1583 * Returns NULL when calling the function fails.
1584 * Uses argv[argc] for the function arguments.
1587 call_func_retstr(func
, argc
, argv
, safe
)
1591 int safe
; /* use the sandbox */
1596 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1599 retval
= vim_strsave(get_tv_string(&rettv
));
1605 # if defined(FEAT_COMPL_FUNC) || defined(PROTO)
1607 * Call vimL function "func" and return the result as a number.
1608 * Returns -1 when calling the function fails.
1609 * Uses argv[argc] for the function arguments.
1612 call_func_retnr(func
, argc
, argv
, safe
)
1616 int safe
; /* use the sandbox */
1621 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1624 retval
= get_tv_number_chk(&rettv
, NULL
);
1631 * Call vimL function "func" and return the result as a List.
1632 * Uses argv[argc] for the function arguments.
1633 * Returns NULL when there is something wrong.
1636 call_func_retlist(func
, argc
, argv
, safe
)
1640 int safe
; /* use the sandbox */
1644 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1647 if (rettv
.v_type
!= VAR_LIST
)
1653 return rettv
.vval
.v_list
;
1659 * Save the current function call pointer, and set it to NULL.
1660 * Used when executing autocommands and for ":source".
1665 funccall_T
*fc
= current_funccal
;
1667 current_funccal
= NULL
;
1672 restore_funccal(vfc
)
1675 funccall_T
*fc
= (funccall_T
*)vfc
;
1677 current_funccal
= fc
;
1680 #if defined(FEAT_PROFILE) || defined(PROTO)
1682 * Prepare profiling for entering a child or something else that is not
1683 * counted for the script/function itself.
1684 * Should always be called in pair with prof_child_exit().
1687 prof_child_enter(tm
)
1688 proftime_T
*tm
; /* place to store waittime */
1690 funccall_T
*fc
= current_funccal
;
1692 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1693 profile_start(&fc
->prof_child
);
1694 script_prof_save(tm
);
1698 * Take care of time spent in a child.
1699 * Should always be called after prof_child_enter().
1703 proftime_T
*tm
; /* where waittime was stored */
1705 funccall_T
*fc
= current_funccal
;
1707 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1709 profile_end(&fc
->prof_child
);
1710 profile_sub_wait(tm
, &fc
->prof_child
); /* don't count waiting time */
1711 profile_add(&fc
->func
->uf_tm_children
, &fc
->prof_child
);
1712 profile_add(&fc
->func
->uf_tml_children
, &fc
->prof_child
);
1714 script_prof_restore(tm
);
1721 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1722 * it in "*cp". Doesn't give error messages.
1725 eval_foldexpr(arg
, cp
)
1732 int use_sandbox
= was_set_insecurely((char_u
*)"foldexpr",
1740 if (eval0(arg
, &tv
, NULL
, TRUE
) == FAIL
)
1744 /* If the result is a number, just return the number. */
1745 if (tv
.v_type
== VAR_NUMBER
)
1746 retval
= tv
.vval
.v_number
;
1747 else if (tv
.v_type
!= VAR_STRING
|| tv
.vval
.v_string
== NULL
)
1751 /* If the result is a string, check if there is a non-digit before
1753 s
= tv
.vval
.v_string
;
1754 if (!VIM_ISDIGIT(*s
) && *s
!= '-')
1756 retval
= atol((char *)s
);
1770 * ":let" list all variable values
1771 * ":let var1 var2" list variable values
1772 * ":let var = expr" assignment command.
1773 * ":let var += expr" assignment command.
1774 * ":let var -= expr" assignment command.
1775 * ":let var .= expr" assignment command.
1776 * ":let [var1, var2] = expr" unpack list.
1782 char_u
*arg
= eap
->arg
;
1783 char_u
*expr
= NULL
;
1792 argend
= skip_var_list(arg
, &var_count
, &semicolon
);
1795 if (argend
> arg
&& argend
[-1] == '.') /* for var.='str' */
1797 expr
= vim_strchr(argend
, '=');
1801 * ":let" without "=": list variables
1805 else if (!ends_excmd(*arg
))
1806 /* ":let var1 var2" */
1807 arg
= list_arg_vars(eap
, arg
, &first
);
1808 else if (!eap
->skip
)
1811 list_glob_vars(&first
);
1812 list_buf_vars(&first
);
1813 list_win_vars(&first
);
1815 list_tab_vars(&first
);
1817 list_script_vars(&first
);
1818 list_func_vars(&first
);
1819 list_vim_vars(&first
);
1821 eap
->nextcmd
= check_nextcmd(arg
);
1829 if (vim_strchr((char_u
*)"+-.", expr
[-1]) != NULL
)
1830 op
[0] = expr
[-1]; /* +=, -= or .= */
1832 expr
= skipwhite(expr
+ 1);
1836 i
= eval0(expr
, &rettv
, &eap
->nextcmd
, !eap
->skip
);
1845 (void)ex_let_vars(eap
->arg
, &rettv
, FALSE
, semicolon
, var_count
,
1853 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1854 * Handles both "var" with any type and "[var, var; var]" with a list type.
1855 * When "nextchars" is not NULL it points to a string with characters that
1856 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1858 * Returns OK or FAIL;
1861 ex_let_vars(arg_start
, tv
, copy
, semicolon
, var_count
, nextchars
)
1864 int copy
; /* copy values from "tv", don't move */
1865 int semicolon
; /* from skip_var_list() */
1866 int var_count
; /* from skip_var_list() */
1869 char_u
*arg
= arg_start
;
1878 * ":let var = expr" or ":for var in list"
1880 if (ex_let_one(arg
, tv
, copy
, nextchars
, nextchars
) == NULL
)
1886 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1888 if (tv
->v_type
!= VAR_LIST
|| (l
= tv
->vval
.v_list
) == NULL
)
1895 if (semicolon
== 0 && var_count
< i
)
1897 EMSG(_("E687: Less targets than List items"));
1900 if (var_count
- semicolon
> i
)
1902 EMSG(_("E688: More targets than List items"));
1909 arg
= skipwhite(arg
+ 1);
1910 arg
= ex_let_one(arg
, &item
->li_tv
, TRUE
, (char_u
*)",;]", nextchars
);
1911 item
= item
->li_next
;
1915 arg
= skipwhite(arg
);
1918 /* Put the rest of the list (may be empty) in the var after ';'.
1919 * Create a new list for this. */
1923 while (item
!= NULL
)
1925 list_append_tv(l
, &item
->li_tv
);
1926 item
= item
->li_next
;
1929 ltv
.v_type
= VAR_LIST
;
1931 ltv
.vval
.v_list
= l
;
1934 arg
= ex_let_one(skipwhite(arg
+ 1), <v
, FALSE
,
1935 (char_u
*)"]", nextchars
);
1941 else if (*arg
!= ',' && *arg
!= ']')
1943 EMSG2(_(e_intern2
), "ex_let_vars()");
1952 * Skip over assignable variable "var" or list of variables "[var, var]".
1953 * Used for ":let varvar = expr" and ":for varvar in expr".
1954 * For "[var, var]" increment "*var_count" for each variable.
1955 * for "[var, var; var]" set "semicolon".
1956 * Return NULL for an error.
1959 skip_var_list(arg
, var_count
, semicolon
)
1968 /* "[var, var]": find the matching ']'. */
1972 p
= skipwhite(p
+ 1); /* skip whites after '[', ';' or ',' */
1973 s
= skip_var_one(p
);
1976 EMSG2(_(e_invarg2
), p
);
1986 if (*semicolon
== 1)
1988 EMSG(_("Double ; in list of variables"));
1995 EMSG2(_(e_invarg2
), p
);
2002 return skip_var_one(arg
);
2006 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
2013 if (*arg
== '@' && arg
[1] != NUL
)
2015 return find_name_end(*arg
== '$' || *arg
== '&' ? arg
+ 1 : arg
,
2016 NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
2020 * List variables for hashtab "ht" with prefix "prefix".
2021 * If "empty" is TRUE also list NULL strings as empty strings.
2024 list_hashtable_vars(ht
, prefix
, empty
, first
)
2034 todo
= (int)ht
->ht_used
;
2035 for (hi
= ht
->ht_array
; todo
> 0 && !got_int
; ++hi
)
2037 if (!HASHITEM_EMPTY(hi
))
2041 if (empty
|| di
->di_tv
.v_type
!= VAR_STRING
2042 || di
->di_tv
.vval
.v_string
!= NULL
)
2043 list_one_var(di
, prefix
, first
);
2049 * List global variables.
2052 list_glob_vars(first
)
2055 list_hashtable_vars(&globvarht
, (char_u
*)"", TRUE
, first
);
2059 * List buffer variables.
2062 list_buf_vars(first
)
2065 char_u numbuf
[NUMBUFLEN
];
2067 list_hashtable_vars(&curbuf
->b_vars
.dv_hashtab
, (char_u
*)"b:",
2070 sprintf((char *)numbuf
, "%ld", (long)curbuf
->b_changedtick
);
2071 list_one_var_a((char_u
*)"b:", (char_u
*)"changedtick", VAR_NUMBER
,
2076 * List window variables.
2079 list_win_vars(first
)
2082 list_hashtable_vars(&curwin
->w_vars
.dv_hashtab
,
2083 (char_u
*)"w:", TRUE
, first
);
2088 * List tab page variables.
2091 list_tab_vars(first
)
2094 list_hashtable_vars(&curtab
->tp_vars
.dv_hashtab
,
2095 (char_u
*)"t:", TRUE
, first
);
2100 * List Vim variables.
2103 list_vim_vars(first
)
2106 list_hashtable_vars(&vimvarht
, (char_u
*)"v:", FALSE
, first
);
2110 * List script-local variables, if there is a script.
2113 list_script_vars(first
)
2116 if (current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
2117 list_hashtable_vars(&SCRIPT_VARS(current_SID
),
2118 (char_u
*)"s:", FALSE
, first
);
2122 * List function variables, if there is a function.
2125 list_func_vars(first
)
2128 if (current_funccal
!= NULL
)
2129 list_hashtable_vars(¤t_funccal
->l_vars
.dv_hashtab
,
2130 (char_u
*)"l:", FALSE
, first
);
2134 * List variables in "arg".
2137 list_arg_vars(eap
, arg
, first
)
2150 while (!ends_excmd(*arg
) && !got_int
)
2152 if (error
|| eap
->skip
)
2154 arg
= find_name_end(arg
, NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
2155 if (!vim_iswhite(*arg
) && !ends_excmd(*arg
))
2158 EMSG(_(e_trailing
));
2164 /* get_name_len() takes care of expanding curly braces */
2165 name_start
= name
= arg
;
2166 len
= get_name_len(&arg
, &tofree
, TRUE
, TRUE
);
2169 /* This is mainly to keep test 49 working: when expanding
2170 * curly braces fails overrule the exception error message. */
2171 if (len
< 0 && !aborting())
2174 EMSG2(_(e_invarg2
), arg
);
2183 if (get_var_tv(name
, len
, &tv
, TRUE
) == FAIL
)
2187 /* handle d.key, l[idx], f(expr) */
2189 if (handle_subscript(&arg
, &tv
, TRUE
, TRUE
) == FAIL
)
2193 if (arg
== arg_subsc
&& len
== 2 && name
[1] == ':')
2197 case 'g': list_glob_vars(first
); break;
2198 case 'b': list_buf_vars(first
); break;
2199 case 'w': list_win_vars(first
); break;
2201 case 't': list_tab_vars(first
); break;
2203 case 'v': list_vim_vars(first
); break;
2204 case 's': list_script_vars(first
); break;
2205 case 'l': list_func_vars(first
); break;
2207 EMSG2(_("E738: Can't list variables for %s"), name
);
2212 char_u numbuf
[NUMBUFLEN
];
2217 s
= echo_string(&tv
, &tf
, numbuf
, 0);
2220 list_one_var_a((char_u
*)"",
2221 arg
== arg_subsc
? name
: name_start
,
2223 s
== NULL
? (char_u
*)"" : s
,
2236 arg
= skipwhite(arg
);
2243 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2244 * Returns a pointer to the char just after the var name.
2245 * Returns NULL if there is an error.
2248 ex_let_one(arg
, tv
, copy
, endchars
, op
)
2249 char_u
*arg
; /* points to variable name */
2250 typval_T
*tv
; /* value to assign to variable */
2251 int copy
; /* copy value from "tv" */
2252 char_u
*endchars
; /* valid chars after variable name or NULL */
2253 char_u
*op
; /* "+", "-", "." or NULL*/
2258 char_u
*arg_end
= NULL
;
2261 char_u
*tofree
= NULL
;
2264 * ":let $VAR = expr": Set environment variable.
2268 /* Find the end of the name. */
2271 len
= get_env_len(&arg
);
2273 EMSG2(_(e_invarg2
), name
- 1);
2276 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2277 EMSG2(_(e_letwrong
), op
);
2278 else if (endchars
!= NULL
2279 && vim_strchr(endchars
, *skipwhite(arg
)) == NULL
)
2280 EMSG(_(e_letunexp
));
2285 p
= get_tv_string_chk(tv
);
2286 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2288 int mustfree
= FALSE
;
2289 char_u
*s
= vim_getenv(name
, &mustfree
);
2293 p
= tofree
= concat_str(s
, p
);
2300 vim_setenv(name
, p
);
2301 if (STRICMP(name
, "HOME") == 0)
2303 else if (didset_vim
&& STRICMP(name
, "VIM") == 0)
2305 else if (didset_vimruntime
2306 && STRICMP(name
, "VIMRUNTIME") == 0)
2307 didset_vimruntime
= FALSE
;
2317 * ":let &option = expr": Set option value.
2318 * ":let &l:option = expr": Set local option value.
2319 * ":let &g:option = expr": Set global option value.
2321 else if (*arg
== '&')
2323 /* Find the end of the name. */
2324 p
= find_option_end(&arg
, &opt_flags
);
2325 if (p
== NULL
|| (endchars
!= NULL
2326 && vim_strchr(endchars
, *skipwhite(p
)) == NULL
))
2327 EMSG(_(e_letunexp
));
2333 char_u
*stringval
= NULL
;
2339 n
= get_tv_number(tv
);
2340 s
= get_tv_string_chk(tv
); /* != NULL if number or string */
2341 if (s
!= NULL
&& op
!= NULL
&& *op
!= '=')
2343 opt_type
= get_option_value(arg
, &numval
,
2344 &stringval
, opt_flags
);
2345 if ((opt_type
== 1 && *op
== '.')
2346 || (opt_type
== 0 && *op
!= '.'))
2347 EMSG2(_(e_letwrong
), op
);
2350 if (opt_type
== 1) /* number */
2357 else if (opt_type
== 0 && stringval
!= NULL
) /* string */
2359 s
= concat_str(stringval
, s
);
2360 vim_free(stringval
);
2367 set_option_value(arg
, n
, s
, opt_flags
);
2371 vim_free(stringval
);
2376 * ":let @r = expr": Set register contents.
2378 else if (*arg
== '@')
2381 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2382 EMSG2(_(e_letwrong
), op
);
2383 else if (endchars
!= NULL
2384 && vim_strchr(endchars
, *skipwhite(arg
+ 1)) == NULL
)
2385 EMSG(_(e_letunexp
));
2388 char_u
*ptofree
= NULL
;
2391 p
= get_tv_string_chk(tv
);
2392 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2394 s
= get_reg_contents(*arg
== '@' ? '"' : *arg
, TRUE
, TRUE
);
2397 p
= ptofree
= concat_str(s
, p
);
2403 write_reg_contents(*arg
== '@' ? '"' : *arg
, p
, -1, FALSE
);
2411 * ":let var = expr": Set internal variable.
2412 * ":let {expr} = expr": Idem, name made with curly braces
2414 else if (eval_isnamec1(*arg
) || *arg
== '{')
2418 p
= get_lval(arg
, tv
, &lv
, FALSE
, FALSE
, FALSE
, FNE_CHECK_START
);
2419 if (p
!= NULL
&& lv
.ll_name
!= NULL
)
2421 if (endchars
!= NULL
&& vim_strchr(endchars
, *skipwhite(p
)) == NULL
)
2422 EMSG(_(e_letunexp
));
2425 set_var_lval(&lv
, p
, tv
, copy
, op
);
2433 EMSG2(_(e_invarg2
), arg
);
2439 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2442 check_changedtick(arg
)
2445 if (STRNCMP(arg
, "b:changedtick", 13) == 0 && !eval_isnamec(arg
[13]))
2447 EMSG2(_(e_readonlyvar
), arg
);
2454 * Get an lval: variable, Dict item or List item that can be assigned a value
2455 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2456 * "name.key", "name.key[expr]" etc.
2457 * Indexing only works if "name" is an existing List or Dictionary.
2458 * "name" points to the start of the name.
2459 * If "rettv" is not NULL it points to the value to be assigned.
2460 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2461 * wrong; must end in space or cmd separator.
2463 * Returns a pointer to just after the name, including indexes.
2464 * When an evaluation error occurs "lp->ll_name" is NULL;
2465 * Returns NULL for a parsing error. Still need to free items in "lp"!
2468 get_lval(name
, rettv
, lp
, unlet
, skip
, quiet
, fne_flags
)
2474 int quiet
; /* don't give error messages */
2475 int fne_flags
; /* flags for find_name_end() */
2478 char_u
*expr_start
, *expr_end
;
2489 /* Clear everything in "lp". */
2490 vim_memset(lp
, 0, sizeof(lval_T
));
2494 /* When skipping just find the end of the name. */
2496 return find_name_end(name
, NULL
, NULL
, FNE_INCL_BR
| fne_flags
);
2499 /* Find the end of the name. */
2500 p
= find_name_end(name
, &expr_start
, &expr_end
, fne_flags
);
2501 if (expr_start
!= NULL
)
2503 /* Don't expand the name when we already know there is an error. */
2504 if (unlet
&& !vim_iswhite(*p
) && !ends_excmd(*p
)
2505 && *p
!= '[' && *p
!= '.')
2507 EMSG(_(e_trailing
));
2511 lp
->ll_exp_name
= make_expanded_name(name
, expr_start
, expr_end
, p
);
2512 if (lp
->ll_exp_name
== NULL
)
2514 /* Report an invalid expression in braces, unless the
2515 * expression evaluation has been cancelled due to an
2516 * aborting error, an interrupt, or an exception. */
2517 if (!aborting() && !quiet
)
2520 EMSG2(_(e_invarg2
), name
);
2524 lp
->ll_name
= lp
->ll_exp_name
;
2529 /* Without [idx] or .key we are done. */
2530 if ((*p
!= '[' && *p
!= '.') || lp
->ll_name
== NULL
)
2535 v
= find_var(lp
->ll_name
, &ht
);
2536 if (v
== NULL
&& !quiet
)
2537 EMSG2(_(e_undefvar
), lp
->ll_name
);
2543 * Loop until no more [idx] or .key is following.
2545 lp
->ll_tv
= &v
->di_tv
;
2546 while (*p
== '[' || (*p
== '.' && lp
->ll_tv
->v_type
== VAR_DICT
))
2548 if (!(lp
->ll_tv
->v_type
== VAR_LIST
&& lp
->ll_tv
->vval
.v_list
!= NULL
)
2549 && !(lp
->ll_tv
->v_type
== VAR_DICT
2550 && lp
->ll_tv
->vval
.v_dict
!= NULL
))
2553 EMSG(_("E689: Can only index a List or Dictionary"));
2559 EMSG(_("E708: [:] must come last"));
2567 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
2572 EMSG(_(e_emptykey
));
2579 /* Get the index [expr] or the first index [expr: ]. */
2580 p
= skipwhite(p
+ 1);
2586 if (eval1(&p
, &var1
, TRUE
) == FAIL
) /* recursive! */
2588 if (get_tv_string_chk(&var1
) == NULL
)
2590 /* not a number or string */
2596 /* Optionally get the second index [ :expr]. */
2599 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2602 EMSG(_(e_dictrange
));
2607 if (rettv
!= NULL
&& (rettv
->v_type
!= VAR_LIST
2608 || rettv
->vval
.v_list
== NULL
))
2611 EMSG(_("E709: [:] requires a List value"));
2616 p
= skipwhite(p
+ 1);
2618 lp
->ll_empty2
= TRUE
;
2621 lp
->ll_empty2
= FALSE
;
2622 if (eval1(&p
, &var2
, TRUE
) == FAIL
) /* recursive! */
2628 if (get_tv_string_chk(&var2
) == NULL
)
2630 /* not a number or string */
2637 lp
->ll_range
= TRUE
;
2640 lp
->ll_range
= FALSE
;
2645 EMSG(_(e_missbrac
));
2648 if (lp
->ll_range
&& !lp
->ll_empty2
)
2653 /* Skip to past ']'. */
2657 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2661 /* "[key]": get key from "var1" */
2662 key
= get_tv_string(&var1
); /* is number or string */
2666 EMSG(_(e_emptykey
));
2672 lp
->ll_dict
= lp
->ll_tv
->vval
.v_dict
;
2673 lp
->ll_di
= dict_find(lp
->ll_dict
, key
, len
);
2674 if (lp
->ll_di
== NULL
)
2676 /* Key does not exist in dict: may need to add it. */
2677 if (*p
== '[' || *p
== '.' || unlet
)
2680 EMSG2(_(e_dictkey
), key
);
2686 lp
->ll_newkey
= vim_strsave(key
);
2688 lp
->ll_newkey
= vim_strnsave(key
, len
);
2691 if (lp
->ll_newkey
== NULL
)
2697 lp
->ll_tv
= &lp
->ll_di
->di_tv
;
2702 * Get the number and item for the only or first index of the List.
2708 lp
->ll_n1
= get_tv_number(&var1
); /* is number or string */
2712 lp
->ll_list
= lp
->ll_tv
->vval
.v_list
;
2713 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2714 if (lp
->ll_li
== NULL
)
2719 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2722 if (lp
->ll_li
== NULL
)
2724 if (lp
->ll_range
&& !lp
->ll_empty2
)
2730 * May need to find the item or absolute index for the second
2732 * When no index given: "lp->ll_empty2" is TRUE.
2733 * Otherwise "lp->ll_n2" is set to the second index.
2735 if (lp
->ll_range
&& !lp
->ll_empty2
)
2737 lp
->ll_n2
= get_tv_number(&var2
); /* is number or string */
2741 ni
= list_find(lp
->ll_list
, lp
->ll_n2
);
2744 lp
->ll_n2
= list_idx_of_item(lp
->ll_list
, ni
);
2747 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2749 lp
->ll_n1
= list_idx_of_item(lp
->ll_list
, lp
->ll_li
);
2750 if (lp
->ll_n2
< lp
->ll_n1
)
2754 lp
->ll_tv
= &lp
->ll_li
->li_tv
;
2762 * Clear lval "lp" that was filled by get_lval().
2768 vim_free(lp
->ll_exp_name
);
2769 vim_free(lp
->ll_newkey
);
2773 * Set a variable that was parsed by get_lval() to "rettv".
2774 * "endp" points to just after the parsed name.
2775 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
2778 set_var_lval(lp
, endp
, rettv
, copy
, op
)
2789 if (lp
->ll_tv
== NULL
)
2791 if (!check_changedtick(lp
->ll_name
))
2795 if (op
!= NULL
&& *op
!= '=')
2799 /* handle +=, -= and .= */
2800 if (get_var_tv(lp
->ll_name
, (int)STRLEN(lp
->ll_name
),
2803 if (tv_op(&tv
, rettv
, op
) == OK
)
2804 set_var(lp
->ll_name
, &tv
, FALSE
);
2809 set_var(lp
->ll_name
, rettv
, copy
);
2813 else if (tv_check_lock(lp
->ll_newkey
== NULL
2815 : lp
->ll_tv
->vval
.v_dict
->dv_lock
, lp
->ll_name
))
2817 else if (lp
->ll_range
)
2820 * Assign the List values to the list items.
2822 for (ri
= rettv
->vval
.v_list
->lv_first
; ri
!= NULL
; )
2824 if (op
!= NULL
&& *op
!= '=')
2825 tv_op(&lp
->ll_li
->li_tv
, &ri
->li_tv
, op
);
2828 clear_tv(&lp
->ll_li
->li_tv
);
2829 copy_tv(&ri
->li_tv
, &lp
->ll_li
->li_tv
);
2832 if (ri
== NULL
|| (!lp
->ll_empty2
&& lp
->ll_n2
== lp
->ll_n1
))
2834 if (lp
->ll_li
->li_next
== NULL
)
2836 /* Need to add an empty item. */
2837 if (list_append_number(lp
->ll_list
, 0) == FAIL
)
2843 lp
->ll_li
= lp
->ll_li
->li_next
;
2847 EMSG(_("E710: List value has more items than target"));
2848 else if (lp
->ll_empty2
2849 ? (lp
->ll_li
!= NULL
&& lp
->ll_li
->li_next
!= NULL
)
2850 : lp
->ll_n1
!= lp
->ll_n2
)
2851 EMSG(_("E711: List value has not enough items"));
2856 * Assign to a List or Dictionary item.
2858 if (lp
->ll_newkey
!= NULL
)
2860 if (op
!= NULL
&& *op
!= '=')
2862 EMSG2(_(e_letwrong
), op
);
2866 /* Need to add an item to the Dictionary. */
2867 di
= dictitem_alloc(lp
->ll_newkey
);
2870 if (dict_add(lp
->ll_tv
->vval
.v_dict
, di
) == FAIL
)
2875 lp
->ll_tv
= &di
->di_tv
;
2877 else if (op
!= NULL
&& *op
!= '=')
2879 tv_op(lp
->ll_tv
, rettv
, op
);
2883 clear_tv(lp
->ll_tv
);
2886 * Assign the value to the variable or list item.
2889 copy_tv(rettv
, lp
->ll_tv
);
2892 *lp
->ll_tv
= *rettv
;
2893 lp
->ll_tv
->v_lock
= 0;
2900 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2901 * Returns OK or FAIL.
2910 char_u numbuf
[NUMBUFLEN
];
2913 /* Can't do anything with a Funcref or a Dict on the right. */
2914 if (tv2
->v_type
!= VAR_FUNC
&& tv2
->v_type
!= VAR_DICT
)
2916 switch (tv1
->v_type
)
2923 if (*op
!= '+' || tv2
->v_type
!= VAR_LIST
)
2926 if (tv1
->vval
.v_list
!= NULL
&& tv2
->vval
.v_list
!= NULL
)
2927 list_extend(tv1
->vval
.v_list
, tv2
->vval
.v_list
, NULL
);
2932 if (tv2
->v_type
== VAR_LIST
)
2934 if (*op
== '+' || *op
== '-')
2936 /* nr += nr or nr -= nr*/
2937 n
= get_tv_number(tv1
);
2939 if (tv2
->v_type
== VAR_FLOAT
)
2944 f
+= tv2
->vval
.v_float
;
2946 f
-= tv2
->vval
.v_float
;
2948 tv1
->v_type
= VAR_FLOAT
;
2949 tv1
->vval
.v_float
= f
;
2955 n
+= get_tv_number(tv2
);
2957 n
-= get_tv_number(tv2
);
2959 tv1
->v_type
= VAR_NUMBER
;
2960 tv1
->vval
.v_number
= n
;
2965 if (tv2
->v_type
== VAR_FLOAT
)
2969 s
= get_tv_string(tv1
);
2970 s
= concat_str(s
, get_tv_string_buf(tv2
, numbuf
));
2972 tv1
->v_type
= VAR_STRING
;
2973 tv1
->vval
.v_string
= s
;
2982 if (*op
== '.' || (tv2
->v_type
!= VAR_FLOAT
2983 && tv2
->v_type
!= VAR_NUMBER
2984 && tv2
->v_type
!= VAR_STRING
))
2986 if (tv2
->v_type
== VAR_FLOAT
)
2987 f
= tv2
->vval
.v_float
;
2989 f
= get_tv_number(tv2
);
2991 tv1
->vval
.v_float
+= f
;
2993 tv1
->vval
.v_float
-= f
;
3000 EMSG2(_(e_letwrong
), op
);
3005 * Add a watcher to a list.
3008 list_add_watch(l
, lw
)
3012 lw
->lw_next
= l
->lv_watch
;
3017 * Remove a watcher from a list.
3018 * No warning when it isn't found...
3021 list_rem_watch(l
, lwrem
)
3025 listwatch_T
*lw
, **lwp
;
3028 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
3040 * Just before removing an item from a list: advance watchers to the next
3044 list_fix_watch(l
, item
)
3050 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
3051 if (lw
->lw_item
== item
)
3052 lw
->lw_item
= item
->li_next
;
3056 * Evaluate the expression used in a ":for var in expr" command.
3057 * "arg" points to "var".
3058 * Set "*errp" to TRUE for an error, FALSE otherwise;
3059 * Return a pointer that holds the info. Null when there is an error.
3062 eval_for_line(arg
, errp
, nextcmdp
, skip
)
3073 *errp
= TRUE
; /* default: there is an error */
3075 fi
= (forinfo_T
*)alloc_clear(sizeof(forinfo_T
));
3079 expr
= skip_var_list(arg
, &fi
->fi_varcount
, &fi
->fi_semicolon
);
3083 expr
= skipwhite(expr
);
3084 if (expr
[0] != 'i' || expr
[1] != 'n' || !vim_iswhite(expr
[2]))
3086 EMSG(_("E690: Missing \"in\" after :for"));
3092 if (eval0(skipwhite(expr
+ 2), &tv
, nextcmdp
, !skip
) == OK
)
3098 if (tv
.v_type
!= VAR_LIST
|| l
== NULL
)
3105 /* No need to increment the refcount, it's already set for the
3106 * list being used in "tv". */
3108 list_add_watch(l
, &fi
->fi_lw
);
3109 fi
->fi_lw
.lw_item
= l
->lv_first
;
3120 * Use the first item in a ":for" list. Advance to the next.
3121 * Assign the values to the variable (list). "arg" points to the first one.
3122 * Return TRUE when a valid item was found, FALSE when at end of list or
3126 next_for_item(fi_void
, arg
)
3130 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3134 item
= fi
->fi_lw
.lw_item
;
3139 fi
->fi_lw
.lw_item
= item
->li_next
;
3140 result
= (ex_let_vars(arg
, &item
->li_tv
, TRUE
,
3141 fi
->fi_semicolon
, fi
->fi_varcount
, NULL
) == OK
);
3147 * Free the structure used to store info used by ":for".
3150 free_for_info(fi_void
)
3153 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3155 if (fi
!= NULL
&& fi
->fi_list
!= NULL
)
3157 list_rem_watch(fi
->fi_list
, &fi
->fi_lw
);
3158 list_unref(fi
->fi_list
);
3163 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3166 set_context_for_expression(xp
, arg
, cmdidx
)
3175 if (cmdidx
== CMD_let
)
3177 xp
->xp_context
= EXPAND_USER_VARS
;
3178 if (vim_strpbrk(arg
, (char_u
*)"\"'+-*/%.=!?~|&$([<>,#") == NULL
)
3180 /* ":let var1 var2 ...": find last space. */
3181 for (p
= arg
+ STRLEN(arg
); p
>= arg
; )
3184 mb_ptr_back(arg
, p
);
3185 if (vim_iswhite(*p
))
3192 xp
->xp_context
= cmdidx
== CMD_call
? EXPAND_FUNCTIONS
3193 : EXPAND_EXPRESSION
;
3194 while ((xp
->xp_pattern
= vim_strpbrk(arg
,
3195 (char_u
*)"\"'+-*/%.=!?~|&$([<>,#")) != NULL
)
3197 c
= *xp
->xp_pattern
;
3200 c
= xp
->xp_pattern
[1];
3204 xp
->xp_context
= cmdidx
!= CMD_let
|| got_eq
3205 ? EXPAND_EXPRESSION
: EXPAND_NOTHING
;
3209 xp
->xp_context
= EXPAND_SETTINGS
;
3210 if ((c
== 'l' || c
== 'g') && xp
->xp_pattern
[2] == ':')
3211 xp
->xp_pattern
+= 2;
3217 /* environment variable */
3218 xp
->xp_context
= EXPAND_ENV_VARS
;
3223 xp
->xp_context
= EXPAND_EXPRESSION
;
3226 && xp
->xp_context
== EXPAND_FUNCTIONS
3227 && vim_strchr(xp
->xp_pattern
, '(') == NULL
)
3229 /* Function name can start with "<SNR>" */
3232 else if (cmdidx
!= CMD_let
|| got_eq
)
3234 if (c
== '"') /* string */
3236 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '"')
3237 if (c
== '\\' && xp
->xp_pattern
[1] != NUL
)
3239 xp
->xp_context
= EXPAND_NOTHING
;
3241 else if (c
== '\'') /* literal string */
3243 /* Trick: '' is like stopping and starting a literal string. */
3244 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '\'')
3246 xp
->xp_context
= EXPAND_NOTHING
;
3250 if (xp
->xp_pattern
[1] == '|')
3253 xp
->xp_context
= EXPAND_EXPRESSION
;
3256 xp
->xp_context
= EXPAND_COMMANDS
;
3259 xp
->xp_context
= EXPAND_EXPRESSION
;
3262 /* Doesn't look like something valid, expand as an expression
3264 xp
->xp_context
= EXPAND_EXPRESSION
;
3265 arg
= xp
->xp_pattern
;
3267 while ((c
= *++arg
) != NUL
&& (c
== ' ' || c
== '\t'))
3270 xp
->xp_pattern
= arg
;
3273 #endif /* FEAT_CMDL_COMPL */
3276 * ":1,25call func(arg1, arg2)" function call.
3282 char_u
*arg
= eap
->arg
;
3293 tofree
= trans_function_name(&arg
, eap
->skip
, TFN_INT
, &fudi
);
3294 if (fudi
.fd_newkey
!= NULL
)
3296 /* Still need to give an error message for missing key. */
3297 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
3298 vim_free(fudi
.fd_newkey
);
3303 /* Increase refcount on dictionary, it could get deleted when evaluating
3305 if (fudi
.fd_dict
!= NULL
)
3306 ++fudi
.fd_dict
->dv_refcount
;
3308 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3309 len
= (int)STRLEN(tofree
);
3310 name
= deref_func_name(tofree
, &len
);
3312 /* Skip white space to allow ":call func ()". Not good, but required for
3313 * backward compatibility. */
3314 startarg
= skipwhite(arg
);
3315 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
3317 if (*startarg
!= '(')
3319 EMSG2(_("E107: Missing parentheses: %s"), eap
->arg
);
3324 * When skipping, evaluate the function once, to find the end of the
3326 * When the function takes a range, this is discovered after the first
3327 * call, and the loop is broken.
3332 lnum
= eap
->line2
; /* do it once, also with an invalid range */
3336 for ( ; lnum
<= eap
->line2
; ++lnum
)
3338 if (!eap
->skip
&& eap
->addr_count
> 0)
3340 curwin
->w_cursor
.lnum
= lnum
;
3341 curwin
->w_cursor
.col
= 0;
3344 if (get_func_tv(name
, (int)STRLEN(name
), &rettv
, &arg
,
3345 eap
->line1
, eap
->line2
, &doesrange
,
3346 !eap
->skip
, fudi
.fd_dict
) == FAIL
)
3352 /* Handle a function returning a Funcref, Dictionary or List. */
3353 if (handle_subscript(&arg
, &rettv
, !eap
->skip
, TRUE
) == FAIL
)
3360 if (doesrange
|| eap
->skip
)
3363 /* Stop when immediately aborting on error, or when an interrupt
3364 * occurred or an exception was thrown but not caught.
3365 * get_func_tv() returned OK, so that the check for trailing
3366 * characters below is executed. */
3375 /* Check for trailing illegal characters and a following command. */
3376 if (!ends_excmd(*arg
))
3379 EMSG(_(e_trailing
));
3382 eap
->nextcmd
= check_nextcmd(arg
);
3386 dict_unref(fudi
.fd_dict
);
3391 * ":unlet[!] var1 ... " command.
3397 ex_unletlock(eap
, eap
->arg
, 0);
3401 * ":lockvar" and ":unlockvar" commands
3407 char_u
*arg
= eap
->arg
;
3412 else if (vim_isdigit(*arg
))
3414 deep
= getdigits(&arg
);
3415 arg
= skipwhite(arg
);
3418 ex_unletlock(eap
, arg
, deep
);
3422 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3425 ex_unletlock(eap
, argstart
, deep
)
3430 char_u
*arg
= argstart
;
3437 /* Parse the name and find the end. */
3438 name_end
= get_lval(arg
, NULL
, &lv
, TRUE
, eap
->skip
|| error
, FALSE
,
3440 if (lv
.ll_name
== NULL
)
3441 error
= TRUE
; /* error but continue parsing */
3442 if (name_end
== NULL
|| (!vim_iswhite(*name_end
)
3443 && !ends_excmd(*name_end
)))
3445 if (name_end
!= NULL
)
3448 EMSG(_(e_trailing
));
3450 if (!(eap
->skip
|| error
))
3455 if (!error
&& !eap
->skip
)
3457 if (eap
->cmdidx
== CMD_unlet
)
3459 if (do_unlet_var(&lv
, name_end
, eap
->forceit
) == FAIL
)
3464 if (do_lock_var(&lv
, name_end
, deep
,
3465 eap
->cmdidx
== CMD_lockvar
) == FAIL
)
3473 arg
= skipwhite(name_end
);
3474 } while (!ends_excmd(*arg
));
3476 eap
->nextcmd
= check_nextcmd(arg
);
3480 do_unlet_var(lp
, name_end
, forceit
)
3488 if (lp
->ll_tv
== NULL
)
3493 /* Normal name or expanded name. */
3494 if (check_changedtick(lp
->ll_name
))
3496 else if (do_unlet(lp
->ll_name
, forceit
) == FAIL
)
3500 else if (tv_check_lock(lp
->ll_tv
->v_lock
, lp
->ll_name
))
3502 else if (lp
->ll_range
)
3506 /* Delete a range of List items. */
3507 while (lp
->ll_li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3509 li
= lp
->ll_li
->li_next
;
3510 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3517 if (lp
->ll_list
!= NULL
)
3518 /* unlet a List item. */
3519 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3521 /* unlet a Dictionary item. */
3522 dictitem_remove(lp
->ll_dict
, lp
->ll_di
);
3529 * "unlet" a variable. Return OK if it existed, FAIL if not.
3530 * When "forceit" is TRUE don't complain if the variable doesn't exist.
3533 do_unlet(name
, forceit
)
3542 ht
= find_var_ht(name
, &varname
);
3543 if (ht
!= NULL
&& *varname
!= NUL
)
3545 hi
= hash_find(ht
, varname
);
3546 if (!HASHITEM_EMPTY(hi
))
3549 if (var_check_fixed(di
->di_flags
, name
)
3550 || var_check_ro(di
->di_flags
, name
))
3558 EMSG2(_("E108: No such variable: \"%s\""), name
);
3563 * Lock or unlock variable indicated by "lp".
3564 * "deep" is the levels to go (-1 for unlimited);
3565 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3568 do_lock_var(lp
, name_end
, deep
, lock
)
3578 if (deep
== 0) /* nothing to do */
3581 if (lp
->ll_tv
== NULL
)
3586 /* Normal name or expanded name. */
3587 if (check_changedtick(lp
->ll_name
))
3591 di
= find_var(lp
->ll_name
, NULL
);
3597 di
->di_flags
|= DI_FLAGS_LOCK
;
3599 di
->di_flags
&= ~DI_FLAGS_LOCK
;
3600 item_lock(&di
->di_tv
, deep
, lock
);
3605 else if (lp
->ll_range
)
3607 listitem_T
*li
= lp
->ll_li
;
3609 /* (un)lock a range of List items. */
3610 while (li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3612 item_lock(&li
->li_tv
, deep
, lock
);
3617 else if (lp
->ll_list
!= NULL
)
3618 /* (un)lock a List item. */
3619 item_lock(&lp
->ll_li
->li_tv
, deep
, lock
);
3621 /* un(lock) a Dictionary item. */
3622 item_lock(&lp
->ll_di
->di_tv
, deep
, lock
);
3628 * Lock or unlock an item. "deep" is nr of levels to go.
3631 item_lock(tv
, deep
, lock
)
3636 static int recurse
= 0;
3643 if (recurse
>= DICT_MAXNEST
)
3645 EMSG(_("E743: variable nested too deep for (un)lock"));
3652 /* lock/unlock the item itself */
3654 tv
->v_lock
|= VAR_LOCKED
;
3656 tv
->v_lock
&= ~VAR_LOCKED
;
3661 if ((l
= tv
->vval
.v_list
) != NULL
)
3664 l
->lv_lock
|= VAR_LOCKED
;
3666 l
->lv_lock
&= ~VAR_LOCKED
;
3667 if (deep
< 0 || deep
> 1)
3668 /* recursive: lock/unlock the items the List contains */
3669 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
3670 item_lock(&li
->li_tv
, deep
- 1, lock
);
3674 if ((d
= tv
->vval
.v_dict
) != NULL
)
3677 d
->dv_lock
|= VAR_LOCKED
;
3679 d
->dv_lock
&= ~VAR_LOCKED
;
3680 if (deep
< 0 || deep
> 1)
3682 /* recursive: lock/unlock the items the List contains */
3683 todo
= (int)d
->dv_hashtab
.ht_used
;
3684 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
3686 if (!HASHITEM_EMPTY(hi
))
3689 item_lock(&HI2DI(hi
)->di_tv
, deep
- 1, lock
);
3699 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3700 * or it refers to a List or Dictionary that is locked.
3706 return (tv
->v_lock
& VAR_LOCKED
)
3707 || (tv
->v_type
== VAR_LIST
3708 && tv
->vval
.v_list
!= NULL
3709 && (tv
->vval
.v_list
->lv_lock
& VAR_LOCKED
))
3710 || (tv
->v_type
== VAR_DICT
3711 && tv
->vval
.v_dict
!= NULL
3712 && (tv
->vval
.v_dict
->dv_lock
& VAR_LOCKED
));
3715 #if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3717 * Delete all "menutrans_" variables.
3720 del_menutrans_vars()
3725 hash_lock(&globvarht
);
3726 todo
= (int)globvarht
.ht_used
;
3727 for (hi
= globvarht
.ht_array
; todo
> 0 && !got_int
; ++hi
)
3729 if (!HASHITEM_EMPTY(hi
))
3732 if (STRNCMP(HI2DI(hi
)->di_key
, "menutrans_", 10) == 0)
3733 delete_var(&globvarht
, hi
);
3736 hash_unlock(&globvarht
);
3740 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3743 * Local string buffer for the next two functions to store a variable name
3744 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3745 * get_user_var_name().
3748 static char_u
*cat_prefix_varname
__ARGS((int prefix
, char_u
*name
));
3750 static char_u
*varnamebuf
= NULL
;
3751 static int varnamebuflen
= 0;
3754 * Function to concatenate a prefix and a variable name.
3757 cat_prefix_varname(prefix
, name
)
3763 len
= (int)STRLEN(name
) + 3;
3764 if (len
> varnamebuflen
)
3766 vim_free(varnamebuf
);
3767 len
+= 10; /* some additional space */
3768 varnamebuf
= alloc(len
);
3769 if (varnamebuf
== NULL
)
3774 varnamebuflen
= len
;
3776 *varnamebuf
= prefix
;
3777 varnamebuf
[1] = ':';
3778 STRCPY(varnamebuf
+ 2, name
);
3783 * Function given to ExpandGeneric() to obtain the list of user defined
3784 * (global/buffer/window/built-in) variable names.
3787 get_user_var_name(xp
, idx
)
3791 static long_u gdone
;
3792 static long_u bdone
;
3793 static long_u wdone
;
3795 static long_u tdone
;
3798 static hashitem_T
*hi
;
3803 gdone
= bdone
= wdone
= vidx
= 0;
3809 /* Global variables */
3810 if (gdone
< globvarht
.ht_used
)
3813 hi
= globvarht
.ht_array
;
3816 while (HASHITEM_EMPTY(hi
))
3818 if (STRNCMP("g:", xp
->xp_pattern
, 2) == 0)
3819 return cat_prefix_varname('g', hi
->hi_key
);
3824 ht
= &curbuf
->b_vars
.dv_hashtab
;
3825 if (bdone
< ht
->ht_used
)
3831 while (HASHITEM_EMPTY(hi
))
3833 return cat_prefix_varname('b', hi
->hi_key
);
3835 if (bdone
== ht
->ht_used
)
3838 return (char_u
*)"b:changedtick";
3842 ht
= &curwin
->w_vars
.dv_hashtab
;
3843 if (wdone
< ht
->ht_used
)
3849 while (HASHITEM_EMPTY(hi
))
3851 return cat_prefix_varname('w', hi
->hi_key
);
3856 ht
= &curtab
->tp_vars
.dv_hashtab
;
3857 if (tdone
< ht
->ht_used
)
3863 while (HASHITEM_EMPTY(hi
))
3865 return cat_prefix_varname('t', hi
->hi_key
);
3871 return cat_prefix_varname('v', (char_u
*)vimvars
[vidx
++].vv_name
);
3873 vim_free(varnamebuf
);
3879 #endif /* FEAT_CMDL_COMPL */
3882 * types for expressions.
3887 , TYPE_EQUAL
/* == */
3888 , TYPE_NEQUAL
/* != */
3889 , TYPE_GREATER
/* > */
3890 , TYPE_GEQUAL
/* >= */
3891 , TYPE_SMALLER
/* < */
3892 , TYPE_SEQUAL
/* <= */
3893 , TYPE_MATCH
/* =~ */
3894 , TYPE_NOMATCH
/* !~ */
3898 * The "evaluate" argument: When FALSE, the argument is only parsed but not
3899 * executed. The function may return OK, but the rettv will be of type
3900 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3904 * Handle zero level expression.
3905 * This calls eval1() and handles error message and nextcmd.
3906 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
3907 * Note: "rettv.v_lock" is not set.
3908 * Return OK or FAIL.
3911 eval0(arg
, rettv
, nextcmd
, evaluate
)
3921 ret
= eval1(&p
, rettv
, evaluate
);
3922 if (ret
== FAIL
|| !ends_excmd(*p
))
3927 * Report the invalid expression unless the expression evaluation has
3928 * been cancelled due to an aborting error, an interrupt, or an
3932 EMSG2(_(e_invexpr2
), arg
);
3935 if (nextcmd
!= NULL
)
3936 *nextcmd
= check_nextcmd(p
);
3942 * Handle top level expression:
3943 * expr2 ? expr1 : expr1
3945 * "arg" must point to the first non-white of the expression.
3946 * "arg" is advanced to the next non-white after the recognized expression.
3948 * Note: "rettv.v_lock" is not set.
3950 * Return OK or FAIL.
3953 eval1(arg
, rettv
, evaluate
)
3962 * Get the first variable.
3964 if (eval2(arg
, rettv
, evaluate
) == FAIL
)
3967 if ((*arg
)[0] == '?')
3974 if (get_tv_number_chk(rettv
, &error
) != 0)
3982 * Get the second variable.
3984 *arg
= skipwhite(*arg
+ 1);
3985 if (eval1(arg
, rettv
, evaluate
&& result
) == FAIL
) /* recursive! */
3989 * Check for the ":".
3991 if ((*arg
)[0] != ':')
3993 EMSG(_("E109: Missing ':' after '?'"));
3994 if (evaluate
&& result
)
4000 * Get the third variable.
4002 *arg
= skipwhite(*arg
+ 1);
4003 if (eval1(arg
, &var2
, evaluate
&& !result
) == FAIL
) /* recursive! */
4005 if (evaluate
&& result
)
4009 if (evaluate
&& !result
)
4017 * Handle first level expression:
4018 * expr2 || expr2 || expr2 logical OR
4020 * "arg" must point to the first non-white of the expression.
4021 * "arg" is advanced to the next non-white after the recognized expression.
4023 * Return OK or FAIL.
4026 eval2(arg
, rettv
, evaluate
)
4037 * Get the first variable.
4039 if (eval3(arg
, rettv
, evaluate
) == FAIL
)
4043 * Repeat until there is no following "||".
4047 while ((*arg
)[0] == '|' && (*arg
)[1] == '|')
4049 if (evaluate
&& first
)
4051 if (get_tv_number_chk(rettv
, &error
) != 0)
4060 * Get the second variable.
4062 *arg
= skipwhite(*arg
+ 2);
4063 if (eval3(arg
, &var2
, evaluate
&& !result
) == FAIL
)
4067 * Compute the result.
4069 if (evaluate
&& !result
)
4071 if (get_tv_number_chk(&var2
, &error
) != 0)
4079 rettv
->v_type
= VAR_NUMBER
;
4080 rettv
->vval
.v_number
= result
;
4088 * Handle second level expression:
4089 * expr3 && expr3 && expr3 logical AND
4091 * "arg" must point to the first non-white of the expression.
4092 * "arg" is advanced to the next non-white after the recognized expression.
4094 * Return OK or FAIL.
4097 eval3(arg
, rettv
, evaluate
)
4108 * Get the first variable.
4110 if (eval4(arg
, rettv
, evaluate
) == FAIL
)
4114 * Repeat until there is no following "&&".
4118 while ((*arg
)[0] == '&' && (*arg
)[1] == '&')
4120 if (evaluate
&& first
)
4122 if (get_tv_number_chk(rettv
, &error
) == 0)
4131 * Get the second variable.
4133 *arg
= skipwhite(*arg
+ 2);
4134 if (eval4(arg
, &var2
, evaluate
&& result
) == FAIL
)
4138 * Compute the result.
4140 if (evaluate
&& result
)
4142 if (get_tv_number_chk(&var2
, &error
) == 0)
4150 rettv
->v_type
= VAR_NUMBER
;
4151 rettv
->vval
.v_number
= result
;
4159 * Handle third level expression:
4171 * "arg" must point to the first non-white of the expression.
4172 * "arg" is advanced to the next non-white after the recognized expression.
4174 * Return OK or FAIL.
4177 eval4(arg
, rettv
, evaluate
)
4185 exptype_T type
= TYPE_UNKNOWN
;
4186 int type_is
= FALSE
; /* TRUE for "is" and "isnot" */
4190 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4191 regmatch_T regmatch
;
4196 * Get the first variable.
4198 if (eval5(arg
, rettv
, evaluate
) == FAIL
)
4204 case '=': if (p
[1] == '=')
4206 else if (p
[1] == '~')
4209 case '!': if (p
[1] == '=')
4211 else if (p
[1] == '~')
4212 type
= TYPE_NOMATCH
;
4214 case '>': if (p
[1] != '=')
4216 type
= TYPE_GREATER
;
4222 case '<': if (p
[1] != '=')
4224 type
= TYPE_SMALLER
;
4230 case 'i': if (p
[1] == 's')
4232 if (p
[2] == 'n' && p
[3] == 'o' && p
[4] == 't')
4234 if (!vim_isIDc(p
[len
]))
4236 type
= len
== 2 ? TYPE_EQUAL
: TYPE_NEQUAL
;
4244 * If there is a comparative operator, use it.
4246 if (type
!= TYPE_UNKNOWN
)
4248 /* extra question mark appended: ignore case */
4254 /* extra '#' appended: match case */
4255 else if (p
[len
] == '#')
4260 /* nothing appended: use 'ignorecase' */
4265 * Get the second variable.
4267 *arg
= skipwhite(p
+ len
);
4268 if (eval5(arg
, &var2
, evaluate
) == FAIL
)
4276 if (type_is
&& rettv
->v_type
!= var2
.v_type
)
4278 /* For "is" a different type always means FALSE, for "notis"
4280 n1
= (type
== TYPE_NEQUAL
);
4282 else if (rettv
->v_type
== VAR_LIST
|| var2
.v_type
== VAR_LIST
)
4286 n1
= (rettv
->v_type
== var2
.v_type
4287 && rettv
->vval
.v_list
== var2
.vval
.v_list
);
4288 if (type
== TYPE_NEQUAL
)
4291 else if (rettv
->v_type
!= var2
.v_type
4292 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4294 if (rettv
->v_type
!= var2
.v_type
)
4295 EMSG(_("E691: Can only compare List with List"));
4297 EMSG(_("E692: Invalid operation for Lists"));
4304 /* Compare two Lists for being equal or unequal. */
4305 n1
= list_equal(rettv
->vval
.v_list
, var2
.vval
.v_list
, ic
);
4306 if (type
== TYPE_NEQUAL
)
4311 else if (rettv
->v_type
== VAR_DICT
|| var2
.v_type
== VAR_DICT
)
4315 n1
= (rettv
->v_type
== var2
.v_type
4316 && rettv
->vval
.v_dict
== var2
.vval
.v_dict
);
4317 if (type
== TYPE_NEQUAL
)
4320 else if (rettv
->v_type
!= var2
.v_type
4321 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4323 if (rettv
->v_type
!= var2
.v_type
)
4324 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4326 EMSG(_("E736: Invalid operation for Dictionary"));
4333 /* Compare two Dictionaries for being equal or unequal. */
4334 n1
= dict_equal(rettv
->vval
.v_dict
, var2
.vval
.v_dict
, ic
);
4335 if (type
== TYPE_NEQUAL
)
4340 else if (rettv
->v_type
== VAR_FUNC
|| var2
.v_type
== VAR_FUNC
)
4342 if (rettv
->v_type
!= var2
.v_type
4343 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4345 if (rettv
->v_type
!= var2
.v_type
)
4346 EMSG(_("E693: Can only compare Funcref with Funcref"));
4348 EMSG(_("E694: Invalid operation for Funcrefs"));
4355 /* Compare two Funcrefs for being equal or unequal. */
4356 if (rettv
->vval
.v_string
== NULL
4357 || var2
.vval
.v_string
== NULL
)
4360 n1
= STRCMP(rettv
->vval
.v_string
,
4361 var2
.vval
.v_string
) == 0;
4362 if (type
== TYPE_NEQUAL
)
4369 * If one of the two variables is a float, compare as a float.
4370 * When using "=~" or "!~", always compare as string.
4372 else if ((rettv
->v_type
== VAR_FLOAT
|| var2
.v_type
== VAR_FLOAT
)
4373 && type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4377 if (rettv
->v_type
== VAR_FLOAT
)
4378 f1
= rettv
->vval
.v_float
;
4380 f1
= get_tv_number(rettv
);
4381 if (var2
.v_type
== VAR_FLOAT
)
4382 f2
= var2
.vval
.v_float
;
4384 f2
= get_tv_number(&var2
);
4388 case TYPE_EQUAL
: n1
= (f1
== f2
); break;
4389 case TYPE_NEQUAL
: n1
= (f1
!= f2
); break;
4390 case TYPE_GREATER
: n1
= (f1
> f2
); break;
4391 case TYPE_GEQUAL
: n1
= (f1
>= f2
); break;
4392 case TYPE_SMALLER
: n1
= (f1
< f2
); break;
4393 case TYPE_SEQUAL
: n1
= (f1
<= f2
); break;
4396 case TYPE_NOMATCH
: break; /* avoid gcc warning */
4402 * If one of the two variables is a number, compare as a number.
4403 * When using "=~" or "!~", always compare as string.
4405 else if ((rettv
->v_type
== VAR_NUMBER
|| var2
.v_type
== VAR_NUMBER
)
4406 && type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4408 n1
= get_tv_number(rettv
);
4409 n2
= get_tv_number(&var2
);
4412 case TYPE_EQUAL
: n1
= (n1
== n2
); break;
4413 case TYPE_NEQUAL
: n1
= (n1
!= n2
); break;
4414 case TYPE_GREATER
: n1
= (n1
> n2
); break;
4415 case TYPE_GEQUAL
: n1
= (n1
>= n2
); break;
4416 case TYPE_SMALLER
: n1
= (n1
< n2
); break;
4417 case TYPE_SEQUAL
: n1
= (n1
<= n2
); break;
4420 case TYPE_NOMATCH
: break; /* avoid gcc warning */
4425 s1
= get_tv_string_buf(rettv
, buf1
);
4426 s2
= get_tv_string_buf(&var2
, buf2
);
4427 if (type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4428 i
= ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
);
4434 case TYPE_EQUAL
: n1
= (i
== 0); break;
4435 case TYPE_NEQUAL
: n1
= (i
!= 0); break;
4436 case TYPE_GREATER
: n1
= (i
> 0); break;
4437 case TYPE_GEQUAL
: n1
= (i
>= 0); break;
4438 case TYPE_SMALLER
: n1
= (i
< 0); break;
4439 case TYPE_SEQUAL
: n1
= (i
<= 0); break;
4443 /* avoid 'l' flag in 'cpoptions' */
4445 p_cpo
= (char_u
*)"";
4446 regmatch
.regprog
= vim_regcomp(s2
,
4447 RE_MAGIC
+ RE_STRING
);
4448 regmatch
.rm_ic
= ic
;
4449 if (regmatch
.regprog
!= NULL
)
4451 n1
= vim_regexec_nl(®match
, s1
, (colnr_T
)0);
4452 vim_free(regmatch
.regprog
);
4453 if (type
== TYPE_NOMATCH
)
4459 case TYPE_UNKNOWN
: break; /* avoid gcc warning */
4464 rettv
->v_type
= VAR_NUMBER
;
4465 rettv
->vval
.v_number
= n1
;
4473 * Handle fourth level expression:
4475 * - number subtraction
4476 * . string concatenation
4478 * "arg" must point to the first non-white of the expression.
4479 * "arg" is advanced to the next non-white after the recognized expression.
4481 * Return OK or FAIL.
4484 eval5(arg
, rettv
, evaluate
)
4494 float_T f1
= 0, f2
= 0;
4497 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4501 * Get the first variable.
4503 if (eval6(arg
, rettv
, evaluate
, FALSE
) == FAIL
)
4507 * Repeat computing, until no '+', '-' or '.' is following.
4512 if (op
!= '+' && op
!= '-' && op
!= '.')
4515 if ((op
!= '+' || rettv
->v_type
!= VAR_LIST
)
4517 && (op
== '.' || rettv
->v_type
!= VAR_FLOAT
)
4521 /* For "list + ...", an illegal use of the first operand as
4522 * a number cannot be determined before evaluating the 2nd
4523 * operand: if this is also a list, all is ok.
4524 * For "something . ...", "something - ..." or "non-list + ...",
4525 * we know that the first operand needs to be a string or number
4526 * without evaluating the 2nd operand. So check before to avoid
4527 * side effects after an error. */
4528 if (evaluate
&& get_tv_string_chk(rettv
) == NULL
)
4536 * Get the second variable.
4538 *arg
= skipwhite(*arg
+ 1);
4539 if (eval6(arg
, &var2
, evaluate
, op
== '.') == FAIL
)
4548 * Compute the result.
4552 s1
= get_tv_string_buf(rettv
, buf1
); /* already checked */
4553 s2
= get_tv_string_buf_chk(&var2
, buf2
);
4554 if (s2
== NULL
) /* type error ? */
4560 p
= concat_str(s1
, s2
);
4562 rettv
->v_type
= VAR_STRING
;
4563 rettv
->vval
.v_string
= p
;
4565 else if (op
== '+' && rettv
->v_type
== VAR_LIST
4566 && var2
.v_type
== VAR_LIST
)
4568 /* concatenate Lists */
4569 if (list_concat(rettv
->vval
.v_list
, var2
.vval
.v_list
,
4584 if (rettv
->v_type
== VAR_FLOAT
)
4586 f1
= rettv
->vval
.v_float
;
4592 n1
= get_tv_number_chk(rettv
, &error
);
4595 /* This can only happen for "list + non-list". For
4596 * "non-list + ..." or "something - ...", we returned
4597 * before evaluating the 2nd operand. */
4602 if (var2
.v_type
== VAR_FLOAT
)
4607 if (var2
.v_type
== VAR_FLOAT
)
4609 f2
= var2
.vval
.v_float
;
4615 n2
= get_tv_number_chk(&var2
, &error
);
4623 if (rettv
->v_type
== VAR_FLOAT
)
4630 /* If there is a float on either side the result is a float. */
4631 if (rettv
->v_type
== VAR_FLOAT
|| var2
.v_type
== VAR_FLOAT
)
4637 rettv
->v_type
= VAR_FLOAT
;
4638 rettv
->vval
.v_float
= f1
;
4647 rettv
->v_type
= VAR_NUMBER
;
4648 rettv
->vval
.v_number
= n1
;
4658 * Handle fifth level expression:
4659 * * number multiplication
4663 * "arg" must point to the first non-white of the expression.
4664 * "arg" is advanced to the next non-white after the recognized expression.
4666 * Return OK or FAIL.
4669 eval6(arg
, rettv
, evaluate
, want_string
)
4673 int want_string
; /* after "." operator */
4679 int use_float
= FALSE
;
4685 * Get the first variable.
4687 if (eval7(arg
, rettv
, evaluate
, want_string
) == FAIL
)
4691 * Repeat computing, until no '*', '/' or '%' is following.
4696 if (op
!= '*' && op
!= '/' && op
!= '%')
4702 if (rettv
->v_type
== VAR_FLOAT
)
4704 f1
= rettv
->vval
.v_float
;
4710 n1
= get_tv_number_chk(rettv
, &error
);
4719 * Get the second variable.
4721 *arg
= skipwhite(*arg
+ 1);
4722 if (eval7(arg
, &var2
, evaluate
, FALSE
) == FAIL
)
4728 if (var2
.v_type
== VAR_FLOAT
)
4735 f2
= var2
.vval
.v_float
;
4741 n2
= get_tv_number_chk(&var2
, &error
);
4752 * Compute the result.
4753 * When either side is a float the result is a float.
4762 /* We rely on the floating point library to handle divide
4763 * by zero to result in "inf" and not a crash. */
4768 EMSG(_("E804: Cannot use '%' with Float"));
4771 rettv
->v_type
= VAR_FLOAT
;
4772 rettv
->vval
.v_float
= f1
;
4781 if (n2
== 0) /* give an error message? */
4784 n1
= -0x7fffffffL
- 1L; /* similar to NaN */
4795 if (n2
== 0) /* give an error message? */
4800 rettv
->v_type
= VAR_NUMBER
;
4801 rettv
->vval
.v_number
= n1
;
4810 * Handle sixth level expression:
4811 * number number constant
4812 * "string" string constant
4813 * 'string' literal string constant
4814 * &option-name option value
4815 * @r register contents
4816 * identifier variable value
4817 * function() function call
4818 * $VAR environment variable
4819 * (expression) nested expression
4821 * {key: val, key: val} Dictionary
4824 * ! in front logical NOT
4825 * - in front unary minus
4826 * + in front unary plus (ignored)
4827 * trailing [] subscript in String or List
4828 * trailing .name entry in Dictionary
4830 * "arg" must point to the first non-white of the expression.
4831 * "arg" is advanced to the next non-white after the recognized expression.
4833 * Return OK or FAIL.
4836 eval7(arg
, rettv
, evaluate
, want_string
)
4840 int want_string
; /* after "." operator */
4845 char_u
*start_leader
, *end_leader
;
4850 * Initialise variable so that clear_tv() can't mistake this for a
4851 * string and free a string that isn't there.
4853 rettv
->v_type
= VAR_UNKNOWN
;
4856 * Skip '!' and '-' characters. They are handled later.
4858 start_leader
= *arg
;
4859 while (**arg
== '!' || **arg
== '-' || **arg
== '+')
4860 *arg
= skipwhite(*arg
+ 1);
4880 char_u
*p
= skipdigits(*arg
+ 1);
4881 int get_float
= FALSE
;
4883 /* We accept a float when the format matches
4884 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
4885 * strict to avoid backwards compatibility problems.
4886 * Don't look for a float after the "." operator, so that
4887 * ":let vers = 1.2.3" doesn't fail. */
4888 if (!want_string
&& p
[0] == '.' && vim_isdigit(p
[1]))
4891 p
= skipdigits(p
+ 2);
4892 if (*p
== 'e' || *p
== 'E')
4895 if (*p
== '-' || *p
== '+')
4897 if (!vim_isdigit(*p
))
4900 p
= skipdigits(p
+ 1);
4902 if (ASCII_ISALPHA(*p
) || *p
== '.')
4909 *arg
+= string2float(*arg
, &f
);
4912 rettv
->v_type
= VAR_FLOAT
;
4913 rettv
->vval
.v_float
= f
;
4919 vim_str2nr(*arg
, NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
4923 rettv
->v_type
= VAR_NUMBER
;
4924 rettv
->vval
.v_number
= n
;
4931 * String constant: "string".
4933 case '"': ret
= get_string_tv(arg
, rettv
, evaluate
);
4937 * Literal string constant: 'str''ing'.
4939 case '\'': ret
= get_lit_string_tv(arg
, rettv
, evaluate
);
4943 * List: [expr, expr]
4945 case '[': ret
= get_list_tv(arg
, rettv
, evaluate
);
4949 * Dictionary: {key: val, key: val}
4951 case '{': ret
= get_dict_tv(arg
, rettv
, evaluate
);
4955 * Option value: &name
4957 case '&': ret
= get_option_tv(arg
, rettv
, evaluate
);
4961 * Environment variable: $VAR.
4963 case '$': ret
= get_env_tv(arg
, rettv
, evaluate
);
4967 * Register contents: @r.
4972 rettv
->v_type
= VAR_STRING
;
4973 rettv
->vval
.v_string
= get_reg_contents(**arg
, TRUE
, TRUE
);
4980 * nested expression: (expression).
4982 case '(': *arg
= skipwhite(*arg
+ 1);
4983 ret
= eval1(arg
, rettv
, evaluate
); /* recursive! */
4988 EMSG(_("E110: Missing ')'"));
4994 default: ret
= NOTDONE
;
5001 * Must be a variable or function name.
5002 * Can also be a curly-braces kind of name: {expr}.
5005 len
= get_name_len(arg
, &alias
, evaluate
, TRUE
);
5013 if (**arg
== '(') /* recursive! */
5015 /* If "s" is the name of a variable of type VAR_FUNC
5016 * use its contents. */
5017 s
= deref_func_name(s
, &len
);
5019 /* Invoke the function. */
5020 ret
= get_func_tv(s
, len
, rettv
, arg
,
5021 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
5022 &len
, evaluate
, NULL
);
5023 /* Stop the expression evaluation when immediately
5024 * aborting on error, or when an interrupt occurred or
5025 * an exception was thrown but not caught. */
5034 ret
= get_var_tv(s
, len
, rettv
, TRUE
);
5043 *arg
= skipwhite(*arg
);
5045 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5048 ret
= handle_subscript(arg
, rettv
, evaluate
, TRUE
);
5051 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5053 if (ret
== OK
&& evaluate
&& end_leader
> start_leader
)
5060 if (rettv
->v_type
== VAR_FLOAT
)
5061 f
= rettv
->vval
.v_float
;
5064 val
= get_tv_number_chk(rettv
, &error
);
5072 while (end_leader
> start_leader
)
5075 if (*end_leader
== '!')
5078 if (rettv
->v_type
== VAR_FLOAT
)
5084 else if (*end_leader
== '-')
5087 if (rettv
->v_type
== VAR_FLOAT
)
5095 if (rettv
->v_type
== VAR_FLOAT
)
5098 rettv
->vval
.v_float
= f
;
5104 rettv
->v_type
= VAR_NUMBER
;
5105 rettv
->vval
.v_number
= val
;
5114 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5115 * "*arg" points to the '[' or '.'.
5116 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5119 eval_index(arg
, rettv
, evaluate
, verbose
)
5123 int verbose
; /* give error messages */
5125 int empty1
= FALSE
, empty2
= FALSE
;
5126 typval_T var1
, var2
;
5133 if (rettv
->v_type
== VAR_FUNC
5135 || rettv
->v_type
== VAR_FLOAT
5140 EMSG(_("E695: Cannot index a Funcref"));
5150 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
5154 *arg
= skipwhite(key
+ len
);
5161 * Get the (first) variable from inside the [].
5163 *arg
= skipwhite(*arg
+ 1);
5166 else if (eval1(arg
, &var1
, evaluate
) == FAIL
) /* recursive! */
5168 else if (evaluate
&& get_tv_string_chk(&var1
) == NULL
)
5170 /* not a number or string */
5176 * Get the second variable from inside the [:].
5181 *arg
= skipwhite(*arg
+ 1);
5184 else if (eval1(arg
, &var2
, evaluate
) == FAIL
) /* recursive! */
5190 else if (evaluate
&& get_tv_string_chk(&var2
) == NULL
)
5192 /* not a number or string */
5200 /* Check for the ']'. */
5204 EMSG(_(e_missbrac
));
5210 *arg
= skipwhite(*arg
+ 1); /* skip the ']' */
5216 if (!empty1
&& rettv
->v_type
!= VAR_DICT
)
5218 n1
= get_tv_number(&var1
);
5227 n2
= get_tv_number(&var2
);
5232 switch (rettv
->v_type
)
5236 s
= get_tv_string(rettv
);
5237 len
= (long)STRLEN(s
);
5240 /* The resulting variable is a substring. If the indexes
5241 * are out of range the result is empty. */
5252 if (n1
>= len
|| n2
< 0 || n1
> n2
)
5255 s
= vim_strnsave(s
+ n1
, (int)(n2
- n1
+ 1));
5259 /* The resulting variable is a string of a single
5260 * character. If the index is too big or negative the
5261 * result is empty. */
5262 if (n1
>= len
|| n1
< 0)
5265 s
= vim_strnsave(s
+ n1
, 1);
5268 rettv
->v_type
= VAR_STRING
;
5269 rettv
->vval
.v_string
= s
;
5273 len
= list_len(rettv
->vval
.v_list
);
5276 if (!empty1
&& (n1
< 0 || n1
>= len
))
5278 /* For a range we allow invalid values and return an empty
5279 * list. A list index out of range is an error. */
5283 EMSGN(_(e_listidx
), n1
);
5297 if (!empty2
&& (n2
< 0 || n2
+ 1 < n1
))
5302 for (item
= list_find(rettv
->vval
.v_list
, n1
);
5305 if (list_append_tv(l
, &item
->li_tv
) == FAIL
)
5310 item
= item
->li_next
;
5313 rettv
->v_type
= VAR_LIST
;
5314 rettv
->vval
.v_list
= l
;
5319 copy_tv(&list_find(rettv
->vval
.v_list
, n1
)->li_tv
, &var1
);
5329 EMSG(_(e_dictrange
));
5339 key
= get_tv_string(&var1
);
5343 EMSG(_(e_emptykey
));
5349 item
= dict_find(rettv
->vval
.v_dict
, key
, (int)len
);
5351 if (item
== NULL
&& verbose
)
5352 EMSG2(_(e_dictkey
), key
);
5358 copy_tv(&item
->di_tv
, &var1
);
5370 * Get an option value.
5371 * "arg" points to the '&' or '+' before the option name.
5372 * "arg" is advanced to character after the option name.
5373 * Return OK or FAIL.
5376 get_option_tv(arg
, rettv
, evaluate
)
5378 typval_T
*rettv
; /* when NULL, only check if option exists */
5386 int working
= (**arg
== '+'); /* has("+option") */
5391 * Isolate the option name and find its value.
5393 option_end
= find_option_end(arg
, &opt_flags
);
5394 if (option_end
== NULL
)
5397 EMSG2(_("E112: Option name missing: %s"), *arg
);
5409 opt_type
= get_option_value(*arg
, &numval
,
5410 rettv
== NULL
? NULL
: &stringval
, opt_flags
);
5412 if (opt_type
== -3) /* invalid name */
5415 EMSG2(_("E113: Unknown option: %s"), *arg
);
5418 else if (rettv
!= NULL
)
5420 if (opt_type
== -2) /* hidden string option */
5422 rettv
->v_type
= VAR_STRING
;
5423 rettv
->vval
.v_string
= NULL
;
5425 else if (opt_type
== -1) /* hidden number option */
5427 rettv
->v_type
= VAR_NUMBER
;
5428 rettv
->vval
.v_number
= 0;
5430 else if (opt_type
== 1) /* number option */
5432 rettv
->v_type
= VAR_NUMBER
;
5433 rettv
->vval
.v_number
= numval
;
5435 else /* string option */
5437 rettv
->v_type
= VAR_STRING
;
5438 rettv
->vval
.v_string
= stringval
;
5441 else if (working
&& (opt_type
== -2 || opt_type
== -1))
5444 *option_end
= c
; /* put back for error messages */
5451 * Allocate a variable for a string constant.
5452 * Return OK or FAIL.
5455 get_string_tv(arg
, rettv
, evaluate
)
5465 * Find the end of the string, skipping backslashed characters.
5467 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
5469 if (*p
== '\\' && p
[1] != NUL
)
5472 /* A "\<x>" form occupies at least 4 characters, and produces up
5473 * to 6 characters: reserve space for 2 extra */
5481 EMSG2(_("E114: Missing quote: %s"), *arg
);
5485 /* If only parsing, set *arg and return here */
5493 * Copy the string into allocated memory, handling backslashed
5496 name
= alloc((unsigned)(p
- *arg
+ extra
));
5499 rettv
->v_type
= VAR_STRING
;
5500 rettv
->vval
.v_string
= name
;
5502 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; )
5508 case 'b': *name
++ = BS
; ++p
; break;
5509 case 'e': *name
++ = ESC
; ++p
; break;
5510 case 'f': *name
++ = FF
; ++p
; break;
5511 case 'n': *name
++ = NL
; ++p
; break;
5512 case 'r': *name
++ = CAR
; ++p
; break;
5513 case 't': *name
++ = TAB
; ++p
; break;
5515 case 'X': /* hex: "\x1", "\x12" */
5517 case 'u': /* Unicode: "\u0023" */
5519 if (vim_isxdigit(p
[1]))
5522 int c
= toupper(*p
);
5529 while (--n
>= 0 && vim_isxdigit(p
[1]))
5532 nr
= (nr
<< 4) + hex2nr(*p
);
5536 /* For "\u" store the number according to
5539 name
+= (*mb_char2bytes
)(nr
, name
);
5546 /* octal: "\1", "\12", "\123" */
5554 case '7': *name
= *p
++ - '0';
5555 if (*p
>= '0' && *p
<= '7')
5557 *name
= (*name
<< 3) + *p
++ - '0';
5558 if (*p
>= '0' && *p
<= '7')
5559 *name
= (*name
<< 3) + *p
++ - '0';
5564 /* Special key, e.g.: "\<C-W>" */
5565 case '<': extra
= trans_special(&p
, name
, TRUE
);
5573 default: MB_COPY_CHAR(p
, name
);
5578 MB_COPY_CHAR(p
, name
);
5588 * Allocate a variable for a 'str''ing' constant.
5589 * Return OK or FAIL.
5592 get_lit_string_tv(arg
, rettv
, evaluate
)
5602 * Find the end of the string, skipping ''.
5604 for (p
= *arg
+ 1; *p
!= NUL
; mb_ptr_adv(p
))
5617 EMSG2(_("E115: Missing quote: %s"), *arg
);
5621 /* If only parsing return after setting "*arg" */
5629 * Copy the string into allocated memory, handling '' to ' reduction.
5631 str
= alloc((unsigned)((p
- *arg
) - reduce
));
5634 rettv
->v_type
= VAR_STRING
;
5635 rettv
->vval
.v_string
= str
;
5637 for (p
= *arg
+ 1; *p
!= NUL
; )
5645 MB_COPY_CHAR(p
, str
);
5654 * Allocate a variable for a List and fill it from "*arg".
5655 * Return OK or FAIL.
5658 get_list_tv(arg
, rettv
, evaluate
)
5674 *arg
= skipwhite(*arg
+ 1);
5675 while (**arg
!= ']' && **arg
!= NUL
)
5677 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
5681 item
= listitem_alloc();
5685 item
->li_tv
.v_lock
= 0;
5686 list_append(l
, item
);
5696 EMSG2(_("E696: Missing comma in List: %s"), *arg
);
5699 *arg
= skipwhite(*arg
+ 1);
5704 EMSG2(_("E697: Missing end of List ']': %s"), *arg
);
5711 *arg
= skipwhite(*arg
+ 1);
5714 rettv
->v_type
= VAR_LIST
;
5715 rettv
->vval
.v_list
= l
;
5723 * Allocate an empty header for a list.
5724 * Caller should take care of the reference count.
5731 l
= (list_T
*)alloc_clear(sizeof(list_T
));
5734 /* Prepend the list to the list of lists for garbage collection. */
5735 if (first_list
!= NULL
)
5736 first_list
->lv_used_prev
= l
;
5737 l
->lv_used_prev
= NULL
;
5738 l
->lv_used_next
= first_list
;
5745 * Allocate an empty list for a return value.
5746 * Returns OK or FAIL.
5749 rettv_list_alloc(rettv
)
5752 list_T
*l
= list_alloc();
5757 rettv
->vval
.v_list
= l
;
5758 rettv
->v_type
= VAR_LIST
;
5764 * Unreference a list: decrement the reference count and free it when it
5771 if (l
!= NULL
&& --l
->lv_refcount
<= 0)
5776 * Free a list, including all items it points to.
5777 * Ignores the reference count.
5780 list_free(l
, recurse
)
5782 int recurse
; /* Free Lists and Dictionaries recursively. */
5786 /* Remove the list from the list of lists for garbage collection. */
5787 if (l
->lv_used_prev
== NULL
)
5788 first_list
= l
->lv_used_next
;
5790 l
->lv_used_prev
->lv_used_next
= l
->lv_used_next
;
5791 if (l
->lv_used_next
!= NULL
)
5792 l
->lv_used_next
->lv_used_prev
= l
->lv_used_prev
;
5794 for (item
= l
->lv_first
; item
!= NULL
; item
= l
->lv_first
)
5796 /* Remove the item before deleting it. */
5797 l
->lv_first
= item
->li_next
;
5798 if (recurse
|| (item
->li_tv
.v_type
!= VAR_LIST
5799 && item
->li_tv
.v_type
!= VAR_DICT
))
5800 clear_tv(&item
->li_tv
);
5807 * Allocate a list item.
5812 return (listitem_T
*)alloc(sizeof(listitem_T
));
5816 * Free a list item. Also clears the value. Does not notify watchers.
5822 clear_tv(&item
->li_tv
);
5827 * Remove a list item from a List and free it. Also clears the value.
5830 listitem_remove(l
, item
)
5834 list_remove(l
, item
, item
);
5835 listitem_free(item
);
5839 * Get the number of items in a list.
5851 * Return TRUE when two lists have exactly the same values.
5854 list_equal(l1
, l2
, ic
)
5857 int ic
; /* ignore case for strings */
5859 listitem_T
*item1
, *item2
;
5861 if (l1
== NULL
|| l2
== NULL
)
5865 if (list_len(l1
) != list_len(l2
))
5868 for (item1
= l1
->lv_first
, item2
= l2
->lv_first
;
5869 item1
!= NULL
&& item2
!= NULL
;
5870 item1
= item1
->li_next
, item2
= item2
->li_next
)
5871 if (!tv_equal(&item1
->li_tv
, &item2
->li_tv
, ic
))
5873 return item1
== NULL
&& item2
== NULL
;
5876 #if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_MZSCHEME) \
5879 * Return the dictitem that an entry in a hashtable points to.
5890 * Return TRUE when two dictionaries have exactly the same key/values.
5893 dict_equal(d1
, d2
, ic
)
5896 int ic
; /* ignore case for strings */
5902 if (d1
== NULL
|| d2
== NULL
)
5906 if (dict_len(d1
) != dict_len(d2
))
5909 todo
= (int)d1
->dv_hashtab
.ht_used
;
5910 for (hi
= d1
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
5912 if (!HASHITEM_EMPTY(hi
))
5914 item2
= dict_find(d2
, hi
->hi_key
, -1);
5917 if (!tv_equal(&HI2DI(hi
)->di_tv
, &item2
->di_tv
, ic
))
5926 * Return TRUE if "tv1" and "tv2" have the same value.
5927 * Compares the items just like "==" would compare them, but strings and
5928 * numbers are different. Floats and numbers are also different.
5931 tv_equal(tv1
, tv2
, ic
)
5934 int ic
; /* ignore case */
5936 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
5938 static int recursive
= 0; /* cach recursive loops */
5941 if (tv1
->v_type
!= tv2
->v_type
)
5943 /* Catch lists and dicts that have an endless loop by limiting
5944 * recursiveness to 1000. We guess they are equal then. */
5945 if (recursive
>= 1000)
5948 switch (tv1
->v_type
)
5952 r
= list_equal(tv1
->vval
.v_list
, tv2
->vval
.v_list
, ic
);
5958 r
= dict_equal(tv1
->vval
.v_dict
, tv2
->vval
.v_dict
, ic
);
5963 return (tv1
->vval
.v_string
!= NULL
5964 && tv2
->vval
.v_string
!= NULL
5965 && STRCMP(tv1
->vval
.v_string
, tv2
->vval
.v_string
) == 0);
5968 return tv1
->vval
.v_number
== tv2
->vval
.v_number
;
5972 return tv1
->vval
.v_float
== tv2
->vval
.v_float
;
5976 s1
= get_tv_string_buf(tv1
, buf1
);
5977 s2
= get_tv_string_buf(tv2
, buf2
);
5978 return ((ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
)) == 0);
5981 EMSG2(_(e_intern2
), "tv_equal()");
5986 * Locate item with index "n" in list "l" and return it.
5987 * A negative index is counted from the end; -1 is the last item.
5988 * Returns NULL when "n" is out of range.
6001 /* Negative index is relative to the end. */
6005 /* Check for index out of range. */
6006 if (n
< 0 || n
>= l
->lv_len
)
6009 /* When there is a cached index may start search from there. */
6010 if (l
->lv_idx_item
!= NULL
)
6012 if (n
< l
->lv_idx
/ 2)
6014 /* closest to the start of the list */
6018 else if (n
> (l
->lv_idx
+ l
->lv_len
) / 2)
6020 /* closest to the end of the list */
6022 idx
= l
->lv_len
- 1;
6026 /* closest to the cached index */
6027 item
= l
->lv_idx_item
;
6033 if (n
< l
->lv_len
/ 2)
6035 /* closest to the start of the list */
6041 /* closest to the end of the list */
6043 idx
= l
->lv_len
- 1;
6049 /* search forward */
6050 item
= item
->li_next
;
6055 /* search backward */
6056 item
= item
->li_prev
;
6060 /* cache the used index */
6062 l
->lv_idx_item
= item
;
6068 * Get list item "l[idx]" as a number.
6071 list_find_nr(l
, idx
, errorp
)
6074 int *errorp
; /* set to TRUE when something wrong */
6078 li
= list_find(l
, idx
);
6085 return get_tv_number_chk(&li
->li_tv
, errorp
);
6089 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6092 list_find_str(l
, idx
)
6098 li
= list_find(l
, idx
- 1);
6101 EMSGN(_(e_listidx
), idx
);
6104 return get_tv_string(&li
->li_tv
);
6108 * Locate "item" list "l" and return its index.
6109 * Returns -1 when "item" is not in the list.
6112 list_idx_of_item(l
, item
)
6122 for (li
= l
->lv_first
; li
!= NULL
&& li
!= item
; li
= li
->li_next
)
6130 * Append item "item" to the end of list "l".
6133 list_append(l
, item
)
6137 if (l
->lv_last
== NULL
)
6142 item
->li_prev
= NULL
;
6146 l
->lv_last
->li_next
= item
;
6147 item
->li_prev
= l
->lv_last
;
6151 item
->li_next
= NULL
;
6155 * Append typval_T "tv" to the end of list "l".
6156 * Return FAIL when out of memory.
6159 list_append_tv(l
, tv
)
6163 listitem_T
*li
= listitem_alloc();
6167 copy_tv(tv
, &li
->li_tv
);
6173 * Add a dictionary to a list. Used by getqflist().
6174 * Return FAIL when out of memory.
6177 list_append_dict(list
, dict
)
6181 listitem_T
*li
= listitem_alloc();
6185 li
->li_tv
.v_type
= VAR_DICT
;
6186 li
->li_tv
.v_lock
= 0;
6187 li
->li_tv
.vval
.v_dict
= dict
;
6188 list_append(list
, li
);
6189 ++dict
->dv_refcount
;
6194 * Make a copy of "str" and append it as an item to list "l".
6195 * When "len" >= 0 use "str[len]".
6196 * Returns FAIL when out of memory.
6199 list_append_string(l
, str
, len
)
6204 listitem_T
*li
= listitem_alloc();
6209 li
->li_tv
.v_type
= VAR_STRING
;
6210 li
->li_tv
.v_lock
= 0;
6212 li
->li_tv
.vval
.v_string
= NULL
;
6213 else if ((li
->li_tv
.vval
.v_string
= (len
>= 0 ? vim_strnsave(str
, len
)
6214 : vim_strsave(str
))) == NULL
)
6220 * Append "n" to list "l".
6221 * Returns FAIL when out of memory.
6224 list_append_number(l
, n
)
6230 li
= listitem_alloc();
6233 li
->li_tv
.v_type
= VAR_NUMBER
;
6234 li
->li_tv
.v_lock
= 0;
6235 li
->li_tv
.vval
.v_number
= n
;
6241 * Insert typval_T "tv" in list "l" before "item".
6242 * If "item" is NULL append at the end.
6243 * Return FAIL when out of memory.
6246 list_insert_tv(l
, tv
, item
)
6251 listitem_T
*ni
= listitem_alloc();
6255 copy_tv(tv
, &ni
->li_tv
);
6257 /* Append new item at end of list. */
6261 /* Insert new item before existing item. */
6262 ni
->li_prev
= item
->li_prev
;
6264 if (item
->li_prev
== NULL
)
6271 item
->li_prev
->li_next
= ni
;
6272 l
->lv_idx_item
= NULL
;
6281 * Extend "l1" with "l2".
6282 * If "bef" is NULL append at the end, otherwise insert before this item.
6283 * Returns FAIL when out of memory.
6286 list_extend(l1
, l2
, bef
)
6292 int todo
= l2
->lv_len
;
6294 /* We also quit the loop when we have inserted the original item count of
6295 * the list, avoid a hang when we extend a list with itself. */
6296 for (item
= l2
->lv_first
; item
!= NULL
&& --todo
>= 0; item
= item
->li_next
)
6297 if (list_insert_tv(l1
, &item
->li_tv
, bef
) == FAIL
)
6303 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6304 * Return FAIL when out of memory.
6307 list_concat(l1
, l2
, tv
)
6314 if (l1
== NULL
|| l2
== NULL
)
6317 /* make a copy of the first list. */
6318 l
= list_copy(l1
, FALSE
, 0);
6321 tv
->v_type
= VAR_LIST
;
6322 tv
->vval
.v_list
= l
;
6324 /* append all items from the second list */
6325 return list_extend(l
, l2
, NULL
);
6329 * Make a copy of list "orig". Shallow if "deep" is FALSE.
6330 * The refcount of the new list is set to 1.
6331 * See item_copy() for "copyID".
6332 * Returns NULL when out of memory.
6335 list_copy(orig
, deep
, copyID
)
6347 copy
= list_alloc();
6352 /* Do this before adding the items, because one of the items may
6353 * refer back to this list. */
6354 orig
->lv_copyID
= copyID
;
6355 orig
->lv_copylist
= copy
;
6357 for (item
= orig
->lv_first
; item
!= NULL
&& !got_int
;
6358 item
= item
->li_next
)
6360 ni
= listitem_alloc();
6365 if (item_copy(&item
->li_tv
, &ni
->li_tv
, deep
, copyID
) == FAIL
)
6372 copy_tv(&item
->li_tv
, &ni
->li_tv
);
6373 list_append(copy
, ni
);
6375 ++copy
->lv_refcount
;
6387 * Remove items "item" to "item2" from list "l".
6388 * Does not free the listitem or the value!
6391 list_remove(l
, item
, item2
)
6398 /* notify watchers */
6399 for (ip
= item
; ip
!= NULL
; ip
= ip
->li_next
)
6402 list_fix_watch(l
, ip
);
6407 if (item2
->li_next
== NULL
)
6408 l
->lv_last
= item
->li_prev
;
6410 item2
->li_next
->li_prev
= item
->li_prev
;
6411 if (item
->li_prev
== NULL
)
6412 l
->lv_first
= item2
->li_next
;
6414 item
->li_prev
->li_next
= item2
->li_next
;
6415 l
->lv_idx_item
= NULL
;
6419 * Return an allocated string with the string representation of a list.
6423 list2string(tv
, copyID
)
6429 if (tv
->vval
.v_list
== NULL
)
6431 ga_init2(&ga
, (int)sizeof(char), 80);
6432 ga_append(&ga
, '[');
6433 if (list_join(&ga
, tv
->vval
.v_list
, (char_u
*)", ", FALSE
, copyID
) == FAIL
)
6435 vim_free(ga
.ga_data
);
6438 ga_append(&ga
, ']');
6439 ga_append(&ga
, NUL
);
6440 return (char_u
*)ga
.ga_data
;
6444 * Join list "l" into a string in "*gap", using separator "sep".
6445 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
6446 * Return FAIL or OK.
6449 list_join(gap
, l
, sep
, echo
, copyID
)
6458 char_u numbuf
[NUMBUFLEN
];
6462 for (item
= l
->lv_first
; item
!= NULL
&& !got_int
; item
= item
->li_next
)
6467 ga_concat(gap
, sep
);
6470 s
= echo_string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6472 s
= tv2string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6484 * Garbage collection for lists and dictionaries.
6486 * We use reference counts to be able to free most items right away when they
6487 * are no longer used. But for composite items it's possible that it becomes
6488 * unused while the reference count is > 0: When there is a recursive
6489 * reference. Example:
6490 * :let l = [1, 2, 3]
6494 * Since this is quite unusual we handle this with garbage collection: every
6495 * once in a while find out which lists and dicts are not referenced from any
6498 * Here is a good reference text about garbage collection (refers to Python
6499 * but it applies to all reference-counting mechanisms):
6500 * http://python.ca/nas/python/gc/
6504 * Do garbage collection for lists and dicts.
6505 * Return TRUE if some memory was freed.
6514 funccall_T
*fc
, **pfc
;
6516 int did_free_funccal
= FALSE
;
6521 /* Only do this once. */
6522 want_garbage_collect
= FALSE
;
6523 may_garbage_collect
= FALSE
;
6524 garbage_collect_at_exit
= FALSE
;
6526 /* We advance by two because we add one for items referenced through
6527 * previous_funccal. */
6528 current_copyID
+= COPYID_INC
;
6529 copyID
= current_copyID
;
6532 * 1. Go through all accessible variables and mark all lists and dicts
6536 /* Don't free variables in the previous_funccal list unless they are only
6537 * referenced through previous_funccal. This must be first, because if
6538 * the item is referenced elsewhere the funccal must not be freed. */
6539 for (fc
= previous_funccal
; fc
!= NULL
; fc
= fc
->caller
)
6541 set_ref_in_ht(&fc
->l_vars
.dv_hashtab
, copyID
+ 1);
6542 set_ref_in_ht(&fc
->l_avars
.dv_hashtab
, copyID
+ 1);
6545 /* script-local variables */
6546 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
6547 set_ref_in_ht(&SCRIPT_VARS(i
), copyID
);
6549 /* buffer-local variables */
6550 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
6551 set_ref_in_ht(&buf
->b_vars
.dv_hashtab
, copyID
);
6553 /* window-local variables */
6554 FOR_ALL_TAB_WINDOWS(tp
, wp
)
6555 set_ref_in_ht(&wp
->w_vars
.dv_hashtab
, copyID
);
6558 /* tabpage-local variables */
6559 for (tp
= first_tabpage
; tp
!= NULL
; tp
= tp
->tp_next
)
6560 set_ref_in_ht(&tp
->tp_vars
.dv_hashtab
, copyID
);
6563 /* global variables */
6564 set_ref_in_ht(&globvarht
, copyID
);
6566 /* function-local variables */
6567 for (fc
= current_funccal
; fc
!= NULL
; fc
= fc
->caller
)
6569 set_ref_in_ht(&fc
->l_vars
.dv_hashtab
, copyID
);
6570 set_ref_in_ht(&fc
->l_avars
.dv_hashtab
, copyID
);
6574 set_ref_in_ht(&vimvarht
, copyID
);
6577 * 2. Free lists and dictionaries that are not referenced.
6579 did_free
= free_unref_items(copyID
);
6582 * 3. Check if any funccal can be freed now.
6584 for (pfc
= &previous_funccal
; *pfc
!= NULL
; )
6586 if (can_free_funccal(*pfc
, copyID
))
6590 free_funccal(fc
, TRUE
);
6592 did_free_funccal
= TRUE
;
6595 pfc
= &(*pfc
)->caller
;
6597 if (did_free_funccal
)
6598 /* When a funccal was freed some more items might be garbage
6599 * collected, so run again. */
6600 (void)garbage_collect();
6606 * Free lists and dictionaries that are no longer referenced.
6609 free_unref_items(copyID
)
6614 int did_free
= FALSE
;
6617 * Go through the list of dicts and free items without the copyID.
6619 for (dd
= first_dict
; dd
!= NULL
; )
6620 if ((dd
->dv_copyID
& COPYID_MASK
) != (copyID
& COPYID_MASK
))
6622 /* Free the Dictionary and ordinary items it contains, but don't
6623 * recurse into Lists and Dictionaries, they will be in the list
6624 * of dicts or list of lists. */
6625 dict_free(dd
, FALSE
);
6628 /* restart, next dict may also have been freed */
6632 dd
= dd
->dv_used_next
;
6635 * Go through the list of lists and free items without the copyID.
6636 * But don't free a list that has a watcher (used in a for loop), these
6637 * are not referenced anywhere.
6639 for (ll
= first_list
; ll
!= NULL
; )
6640 if ((ll
->lv_copyID
& COPYID_MASK
) != (copyID
& COPYID_MASK
)
6641 && ll
->lv_watch
== NULL
)
6643 /* Free the List and ordinary items it contains, but don't recurse
6644 * into Lists and Dictionaries, they will be in the list of dicts
6645 * or list of lists. */
6646 list_free(ll
, FALSE
);
6649 /* restart, next list may also have been freed */
6653 ll
= ll
->lv_used_next
;
6659 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6662 set_ref_in_ht(ht
, copyID
)
6669 todo
= (int)ht
->ht_used
;
6670 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
6671 if (!HASHITEM_EMPTY(hi
))
6674 set_ref_in_item(&HI2DI(hi
)->di_tv
, copyID
);
6679 * Mark all lists and dicts referenced through list "l" with "copyID".
6682 set_ref_in_list(l
, copyID
)
6688 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
6689 set_ref_in_item(&li
->li_tv
, copyID
);
6693 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6696 set_ref_in_item(tv
, copyID
)
6706 dd
= tv
->vval
.v_dict
;
6707 if (dd
!= NULL
&& dd
->dv_copyID
!= copyID
)
6709 /* Didn't see this dict yet. */
6710 dd
->dv_copyID
= copyID
;
6711 set_ref_in_ht(&dd
->dv_hashtab
, copyID
);
6716 ll
= tv
->vval
.v_list
;
6717 if (ll
!= NULL
&& ll
->lv_copyID
!= copyID
)
6719 /* Didn't see this list yet. */
6720 ll
->lv_copyID
= copyID
;
6721 set_ref_in_list(ll
, copyID
);
6729 * Allocate an empty header for a dictionary.
6736 d
= (dict_T
*)alloc(sizeof(dict_T
));
6739 /* Add the list to the list of dicts for garbage collection. */
6740 if (first_dict
!= NULL
)
6741 first_dict
->dv_used_prev
= d
;
6742 d
->dv_used_next
= first_dict
;
6743 d
->dv_used_prev
= NULL
;
6746 hash_init(&d
->dv_hashtab
);
6755 * Unreference a Dictionary: decrement the reference count and free it when it
6762 if (d
!= NULL
&& --d
->dv_refcount
<= 0)
6767 * Free a Dictionary, including all items it contains.
6768 * Ignores the reference count.
6771 dict_free(d
, recurse
)
6773 int recurse
; /* Free Lists and Dictionaries recursively. */
6779 /* Remove the dict from the list of dicts for garbage collection. */
6780 if (d
->dv_used_prev
== NULL
)
6781 first_dict
= d
->dv_used_next
;
6783 d
->dv_used_prev
->dv_used_next
= d
->dv_used_next
;
6784 if (d
->dv_used_next
!= NULL
)
6785 d
->dv_used_next
->dv_used_prev
= d
->dv_used_prev
;
6787 /* Lock the hashtab, we don't want it to resize while freeing items. */
6788 hash_lock(&d
->dv_hashtab
);
6789 todo
= (int)d
->dv_hashtab
.ht_used
;
6790 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
6792 if (!HASHITEM_EMPTY(hi
))
6794 /* Remove the item before deleting it, just in case there is
6795 * something recursive causing trouble. */
6797 hash_remove(&d
->dv_hashtab
, hi
);
6798 if (recurse
|| (di
->di_tv
.v_type
!= VAR_LIST
6799 && di
->di_tv
.v_type
!= VAR_DICT
))
6800 clear_tv(&di
->di_tv
);
6805 hash_clear(&d
->dv_hashtab
);
6810 * Allocate a Dictionary item.
6811 * The "key" is copied to the new item.
6812 * Note that the value of the item "di_tv" still needs to be initialized!
6813 * Returns NULL when out of memory.
6821 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
) + STRLEN(key
)));
6824 STRCPY(di
->di_key
, key
);
6831 * Make a copy of a Dictionary item.
6839 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
6840 + STRLEN(org
->di_key
)));
6843 STRCPY(di
->di_key
, org
->di_key
);
6845 copy_tv(&org
->di_tv
, &di
->di_tv
);
6851 * Remove item "item" from Dictionary "dict" and free it.
6854 dictitem_remove(dict
, item
)
6860 hi
= hash_find(&dict
->dv_hashtab
, item
->di_key
);
6861 if (HASHITEM_EMPTY(hi
))
6862 EMSG2(_(e_intern2
), "dictitem_remove()");
6864 hash_remove(&dict
->dv_hashtab
, hi
);
6865 dictitem_free(item
);
6869 * Free a dict item. Also clears the value.
6875 clear_tv(&item
->di_tv
);
6880 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6881 * The refcount of the new dict is set to 1.
6882 * See item_copy() for "copyID".
6883 * Returns NULL when out of memory.
6886 dict_copy(orig
, deep
, copyID
)
6899 copy
= dict_alloc();
6904 orig
->dv_copyID
= copyID
;
6905 orig
->dv_copydict
= copy
;
6907 todo
= (int)orig
->dv_hashtab
.ht_used
;
6908 for (hi
= orig
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
6910 if (!HASHITEM_EMPTY(hi
))
6914 di
= dictitem_alloc(hi
->hi_key
);
6919 if (item_copy(&HI2DI(hi
)->di_tv
, &di
->di_tv
, deep
,
6927 copy_tv(&HI2DI(hi
)->di_tv
, &di
->di_tv
);
6928 if (dict_add(copy
, di
) == FAIL
)
6936 ++copy
->dv_refcount
;
6948 * Add item "item" to Dictionary "d".
6949 * Returns FAIL when out of memory and when key already existed.
6956 return hash_add(&d
->dv_hashtab
, item
->di_key
);
6960 * Add a number or string entry to dictionary "d".
6961 * When "str" is NULL use number "nr", otherwise use "str".
6962 * Returns FAIL when out of memory and when key already exists.
6965 dict_add_nr_str(d
, key
, nr
, str
)
6973 item
= dictitem_alloc((char_u
*)key
);
6976 item
->di_tv
.v_lock
= 0;
6979 item
->di_tv
.v_type
= VAR_NUMBER
;
6980 item
->di_tv
.vval
.v_number
= nr
;
6984 item
->di_tv
.v_type
= VAR_STRING
;
6985 item
->di_tv
.vval
.v_string
= vim_strsave(str
);
6987 if (dict_add(d
, item
) == FAIL
)
6989 dictitem_free(item
);
6996 * Get the number of items in a Dictionary.
7004 return (long)d
->dv_hashtab
.ht_used
;
7008 * Find item "key[len]" in Dictionary "d".
7009 * If "len" is negative use strlen(key).
7010 * Returns NULL when not found.
7013 dict_find(d
, key
, len
)
7019 char_u buf
[AKEYLEN
];
7021 char_u
*tofree
= NULL
;
7026 else if (len
>= AKEYLEN
)
7028 tofree
= akey
= vim_strnsave(key
, len
);
7034 /* Avoid a malloc/free by using buf[]. */
7035 vim_strncpy(buf
, key
, len
);
7039 hi
= hash_find(&d
->dv_hashtab
, akey
);
7041 if (HASHITEM_EMPTY(hi
))
7047 * Get a string item from a dictionary.
7048 * When "save" is TRUE allocate memory for it.
7049 * Returns NULL if the entry doesn't exist or out of memory.
7052 get_dict_string(d
, key
, save
)
7060 di
= dict_find(d
, key
, -1);
7063 s
= get_tv_string(&di
->di_tv
);
7064 if (save
&& s
!= NULL
)
7070 * Get a number item from a dictionary.
7071 * Returns 0 if the entry doesn't exist or out of memory.
7074 get_dict_number(d
, key
)
7080 di
= dict_find(d
, key
, -1);
7083 return get_tv_number(&di
->di_tv
);
7087 * Return an allocated string with the string representation of a Dictionary.
7091 dict2string(tv
, copyID
)
7098 char_u numbuf
[NUMBUFLEN
];
7104 if ((d
= tv
->vval
.v_dict
) == NULL
)
7106 ga_init2(&ga
, (int)sizeof(char), 80);
7107 ga_append(&ga
, '{');
7109 todo
= (int)d
->dv_hashtab
.ht_used
;
7110 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
7112 if (!HASHITEM_EMPTY(hi
))
7119 ga_concat(&ga
, (char_u
*)", ");
7121 tofree
= string_quote(hi
->hi_key
, FALSE
);
7124 ga_concat(&ga
, tofree
);
7127 ga_concat(&ga
, (char_u
*)": ");
7128 s
= tv2string(&HI2DI(hi
)->di_tv
, &tofree
, numbuf
, copyID
);
7138 vim_free(ga
.ga_data
);
7142 ga_append(&ga
, '}');
7143 ga_append(&ga
, NUL
);
7144 return (char_u
*)ga
.ga_data
;
7148 * Allocate a variable for a Dictionary and fill it from "*arg".
7149 * Return OK or FAIL. Returns NOTDONE for {expr}.
7152 get_dict_tv(arg
, rettv
, evaluate
)
7162 char_u
*start
= skipwhite(*arg
+ 1);
7163 char_u buf
[NUMBUFLEN
];
7166 * First check if it's not a curly-braces thing: {expr}.
7167 * Must do this without evaluating, otherwise a function may be called
7168 * twice. Unfortunately this means we need to call eval1() twice for the
7170 * But {} is an empty Dictionary.
7174 if (eval1(&start
, &tv
, FALSE
) == FAIL
) /* recursive! */
7186 tvkey
.v_type
= VAR_UNKNOWN
;
7187 tv
.v_type
= VAR_UNKNOWN
;
7189 *arg
= skipwhite(*arg
+ 1);
7190 while (**arg
!= '}' && **arg
!= NUL
)
7192 if (eval1(arg
, &tvkey
, evaluate
) == FAIL
) /* recursive! */
7196 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg
);
7202 key
= get_tv_string_buf_chk(&tvkey
, buf
);
7203 if (key
== NULL
|| *key
== NUL
)
7205 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7207 EMSG(_(e_emptykey
));
7213 *arg
= skipwhite(*arg
+ 1);
7214 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
7222 item
= dict_find(d
, key
, -1);
7225 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key
);
7230 item
= dictitem_alloc(key
);
7235 item
->di_tv
.v_lock
= 0;
7236 if (dict_add(d
, item
) == FAIL
)
7237 dictitem_free(item
);
7245 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg
);
7248 *arg
= skipwhite(*arg
+ 1);
7253 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg
);
7260 *arg
= skipwhite(*arg
+ 1);
7263 rettv
->v_type
= VAR_DICT
;
7264 rettv
->vval
.v_dict
= d
;
7272 * Return a string with the string representation of a variable.
7273 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7274 * "numbuf" is used for a number.
7275 * Does not put quotes around strings, as ":echo" displays values.
7276 * When "copyID" is not NULL replace recursive lists and dicts with "...".
7280 echo_string(tv
, tofree
, numbuf
, copyID
)
7286 static int recurse
= 0;
7289 if (recurse
>= DICT_MAXNEST
)
7291 EMSG(_("E724: variable nested too deep for displaying"));
7301 r
= tv
->vval
.v_string
;
7305 if (tv
->vval
.v_list
== NULL
)
7310 else if (copyID
!= 0 && tv
->vval
.v_list
->lv_copyID
== copyID
)
7313 r
= (char_u
*)"[...]";
7317 tv
->vval
.v_list
->lv_copyID
= copyID
;
7318 *tofree
= list2string(tv
, copyID
);
7324 if (tv
->vval
.v_dict
== NULL
)
7329 else if (copyID
!= 0 && tv
->vval
.v_dict
->dv_copyID
== copyID
)
7332 r
= (char_u
*)"{...}";
7336 tv
->vval
.v_dict
->dv_copyID
= copyID
;
7337 *tofree
= dict2string(tv
, copyID
);
7345 r
= get_tv_string_buf(tv
, numbuf
);
7351 vim_snprintf((char *)numbuf
, NUMBUFLEN
, "%g", tv
->vval
.v_float
);
7357 EMSG2(_(e_intern2
), "echo_string()");
7366 * Return a string with the string representation of a variable.
7367 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7368 * "numbuf" is used for a number.
7369 * Puts quotes around strings, so that they can be parsed back by eval().
7373 tv2string(tv
, tofree
, numbuf
, copyID
)
7382 *tofree
= string_quote(tv
->vval
.v_string
, TRUE
);
7385 *tofree
= string_quote(tv
->vval
.v_string
, FALSE
);
7390 vim_snprintf((char *)numbuf
, NUMBUFLEN
- 1, "%g", tv
->vval
.v_float
);
7398 EMSG2(_(e_intern2
), "tv2string()");
7400 return echo_string(tv
, tofree
, numbuf
, copyID
);
7404 * Return string "str" in ' quotes, doubling ' characters.
7405 * If "str" is NULL an empty string is assumed.
7406 * If "function" is TRUE make it function('string').
7409 string_quote(str
, function
)
7416 len
= (function
? 13 : 3);
7419 len
+= (unsigned)STRLEN(str
);
7420 for (p
= str
; *p
!= NUL
; mb_ptr_adv(p
))
7429 STRCPY(r
, "function('");
7435 for (p
= str
; *p
!= NUL
; )
7451 * Convert the string "text" to a floating point number.
7452 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7453 * this always uses a decimal point.
7454 * Returns the length of the text that was consumed.
7457 string2float(text
, value
)
7459 float_T
*value
; /* result stored here */
7461 char *s
= (char *)text
;
7466 return (int)((char_u
*)s
- text
);
7471 * Get the value of an environment variable.
7472 * "arg" is pointing to the '$'. It is advanced to after the name.
7473 * If the environment variable was not set, silently assume it is empty.
7477 get_env_tv(arg
, rettv
, evaluate
)
7482 char_u
*string
= NULL
;
7486 int mustfree
= FALSE
;
7490 len
= get_env_len(arg
);
7497 /* first try vim_getenv(), fast for normal environment vars */
7498 string
= vim_getenv(name
, &mustfree
);
7499 if (string
!= NULL
&& *string
!= NUL
)
7502 string
= vim_strsave(string
);
7509 /* next try expanding things like $VIM and ${HOME} */
7510 string
= expand_env_save(name
- 1);
7511 if (string
!= NULL
&& *string
== '$')
7519 rettv
->v_type
= VAR_STRING
;
7520 rettv
->vval
.v_string
= string
;
7527 * Array with names and number of arguments of all internal functions
7528 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7532 char *f_name
; /* function name */
7533 char f_min_argc
; /* minimal number of arguments */
7534 char f_max_argc
; /* maximal number of arguments */
7535 void (*f_func
) __ARGS((typval_T
*args
, typval_T
*rvar
));
7536 /* implementation of function */
7540 {"abs", 1, 1, f_abs
},
7542 {"add", 2, 2, f_add
},
7543 {"append", 2, 2, f_append
},
7544 {"argc", 0, 0, f_argc
},
7545 {"argidx", 0, 0, f_argidx
},
7546 {"argv", 0, 1, f_argv
},
7548 {"atan", 1, 1, f_atan
},
7550 {"browse", 4, 4, f_browse
},
7551 {"browsedir", 2, 2, f_browsedir
},
7552 {"bufexists", 1, 1, f_bufexists
},
7553 {"buffer_exists", 1, 1, f_bufexists
}, /* obsolete */
7554 {"buffer_name", 1, 1, f_bufname
}, /* obsolete */
7555 {"buffer_number", 1, 1, f_bufnr
}, /* obsolete */
7556 {"buflisted", 1, 1, f_buflisted
},
7557 {"bufloaded", 1, 1, f_bufloaded
},
7558 {"bufname", 1, 1, f_bufname
},
7559 {"bufnr", 1, 2, f_bufnr
},
7560 {"bufwinnr", 1, 1, f_bufwinnr
},
7561 {"byte2line", 1, 1, f_byte2line
},
7562 {"byteidx", 2, 2, f_byteidx
},
7563 {"call", 2, 3, f_call
},
7565 {"ceil", 1, 1, f_ceil
},
7567 {"changenr", 0, 0, f_changenr
},
7568 {"char2nr", 1, 1, f_char2nr
},
7569 {"cindent", 1, 1, f_cindent
},
7570 {"clearmatches", 0, 0, f_clearmatches
},
7571 {"col", 1, 1, f_col
},
7572 #if defined(FEAT_INS_EXPAND)
7573 {"complete", 2, 2, f_complete
},
7574 {"complete_add", 1, 1, f_complete_add
},
7575 {"complete_check", 0, 0, f_complete_check
},
7577 {"confirm", 1, 4, f_confirm
},
7578 {"copy", 1, 1, f_copy
},
7580 {"cos", 1, 1, f_cos
},
7582 {"count", 2, 4, f_count
},
7583 {"cscope_connection",0,3, f_cscope_connection
},
7584 {"cursor", 1, 3, f_cursor
},
7585 {"deepcopy", 1, 2, f_deepcopy
},
7586 {"delete", 1, 1, f_delete
},
7587 {"did_filetype", 0, 0, f_did_filetype
},
7588 {"diff_filler", 1, 1, f_diff_filler
},
7589 {"diff_hlID", 2, 2, f_diff_hlID
},
7590 {"empty", 1, 1, f_empty
},
7591 {"escape", 2, 2, f_escape
},
7592 {"eval", 1, 1, f_eval
},
7593 {"eventhandler", 0, 0, f_eventhandler
},
7594 {"executable", 1, 1, f_executable
},
7595 {"exists", 1, 1, f_exists
},
7596 {"expand", 1, 2, f_expand
},
7597 {"extend", 2, 3, f_extend
},
7598 {"feedkeys", 1, 2, f_feedkeys
},
7599 {"file_readable", 1, 1, f_filereadable
}, /* obsolete */
7600 {"filereadable", 1, 1, f_filereadable
},
7601 {"filewritable", 1, 1, f_filewritable
},
7602 {"filter", 2, 2, f_filter
},
7603 {"finddir", 1, 3, f_finddir
},
7604 {"findfile", 1, 3, f_findfile
},
7606 {"float2nr", 1, 1, f_float2nr
},
7607 {"floor", 1, 1, f_floor
},
7609 {"fnameescape", 1, 1, f_fnameescape
},
7610 {"fnamemodify", 2, 2, f_fnamemodify
},
7611 {"foldclosed", 1, 1, f_foldclosed
},
7612 {"foldclosedend", 1, 1, f_foldclosedend
},
7613 {"foldlevel", 1, 1, f_foldlevel
},
7614 {"foldtext", 0, 0, f_foldtext
},
7615 {"foldtextresult", 1, 1, f_foldtextresult
},
7616 {"foreground", 0, 0, f_foreground
},
7617 {"function", 1, 1, f_function
},
7618 {"garbagecollect", 0, 1, f_garbagecollect
},
7619 {"get", 2, 3, f_get
},
7620 {"getbufline", 2, 3, f_getbufline
},
7621 {"getbufvar", 2, 2, f_getbufvar
},
7622 {"getchar", 0, 1, f_getchar
},
7623 {"getcharmod", 0, 0, f_getcharmod
},
7624 {"getcmdline", 0, 0, f_getcmdline
},
7625 {"getcmdpos", 0, 0, f_getcmdpos
},
7626 {"getcmdtype", 0, 0, f_getcmdtype
},
7627 {"getcwd", 0, 0, f_getcwd
},
7628 {"getfontname", 0, 1, f_getfontname
},
7629 {"getfperm", 1, 1, f_getfperm
},
7630 {"getfsize", 1, 1, f_getfsize
},
7631 {"getftime", 1, 1, f_getftime
},
7632 {"getftype", 1, 1, f_getftype
},
7633 {"getline", 1, 2, f_getline
},
7634 {"getloclist", 1, 1, f_getqflist
},
7635 {"getmatches", 0, 0, f_getmatches
},
7636 {"getpid", 0, 0, f_getpid
},
7637 {"getpos", 1, 1, f_getpos
},
7638 {"getqflist", 0, 0, f_getqflist
},
7639 {"getreg", 0, 2, f_getreg
},
7640 {"getregtype", 0, 1, f_getregtype
},
7641 {"gettabwinvar", 3, 3, f_gettabwinvar
},
7642 {"getwinposx", 0, 0, f_getwinposx
},
7643 {"getwinposy", 0, 0, f_getwinposy
},
7644 {"getwinvar", 2, 2, f_getwinvar
},
7645 {"glob", 1, 2, f_glob
},
7646 {"globpath", 2, 3, f_globpath
},
7647 {"has", 1, 1, f_has
},
7648 {"has_key", 2, 2, f_has_key
},
7649 {"haslocaldir", 0, 0, f_haslocaldir
},
7650 {"hasmapto", 1, 3, f_hasmapto
},
7651 {"highlightID", 1, 1, f_hlID
}, /* obsolete */
7652 {"highlight_exists",1, 1, f_hlexists
}, /* obsolete */
7653 {"histadd", 2, 2, f_histadd
},
7654 {"histdel", 1, 2, f_histdel
},
7655 {"histget", 1, 2, f_histget
},
7656 {"histnr", 1, 1, f_histnr
},
7657 {"hlID", 1, 1, f_hlID
},
7658 {"hlexists", 1, 1, f_hlexists
},
7659 {"hostname", 0, 0, f_hostname
},
7660 {"iconv", 3, 3, f_iconv
},
7661 {"indent", 1, 1, f_indent
},
7662 {"index", 2, 4, f_index
},
7663 {"input", 1, 3, f_input
},
7664 {"inputdialog", 1, 3, f_inputdialog
},
7665 {"inputlist", 1, 1, f_inputlist
},
7666 {"inputrestore", 0, 0, f_inputrestore
},
7667 {"inputsave", 0, 0, f_inputsave
},
7668 {"inputsecret", 1, 2, f_inputsecret
},
7669 {"insert", 2, 3, f_insert
},
7670 {"isdirectory", 1, 1, f_isdirectory
},
7671 {"islocked", 1, 1, f_islocked
},
7672 {"items", 1, 1, f_items
},
7673 {"join", 1, 2, f_join
},
7674 {"keys", 1, 1, f_keys
},
7675 {"last_buffer_nr", 0, 0, f_last_buffer_nr
},/* obsolete */
7676 {"len", 1, 1, f_len
},
7677 {"libcall", 3, 3, f_libcall
},
7678 {"libcallnr", 3, 3, f_libcallnr
},
7679 {"line", 1, 1, f_line
},
7680 {"line2byte", 1, 1, f_line2byte
},
7681 {"lispindent", 1, 1, f_lispindent
},
7682 {"localtime", 0, 0, f_localtime
},
7684 {"log10", 1, 1, f_log10
},
7686 {"map", 2, 2, f_map
},
7687 {"maparg", 1, 3, f_maparg
},
7688 {"mapcheck", 1, 3, f_mapcheck
},
7689 {"match", 2, 4, f_match
},
7690 {"matchadd", 2, 4, f_matchadd
},
7691 {"matcharg", 1, 1, f_matcharg
},
7692 {"matchdelete", 1, 1, f_matchdelete
},
7693 {"matchend", 2, 4, f_matchend
},
7694 {"matchlist", 2, 4, f_matchlist
},
7695 {"matchstr", 2, 4, f_matchstr
},
7696 {"max", 1, 1, f_max
},
7697 {"migemo", 1, 1, f_migemo
},
7698 {"min", 1, 1, f_min
},
7700 {"mkdir", 1, 3, f_mkdir
},
7702 {"mode", 0, 1, f_mode
},
7703 #ifdef FEAT_MZSCHEME
7704 {"mzeval", 1, 1, f_mzeval
},
7706 {"nextnonblank", 1, 1, f_nextnonblank
},
7707 {"nr2char", 1, 1, f_nr2char
},
7708 {"pathshorten", 1, 1, f_pathshorten
},
7710 {"pow", 2, 2, f_pow
},
7712 {"prevnonblank", 1, 1, f_prevnonblank
},
7713 {"printf", 2, 19, f_printf
},
7714 {"pumvisible", 0, 0, f_pumvisible
},
7715 {"range", 1, 3, f_range
},
7716 {"readfile", 1, 3, f_readfile
},
7717 {"reltime", 0, 2, f_reltime
},
7718 {"reltimestr", 1, 1, f_reltimestr
},
7719 {"remote_expr", 2, 3, f_remote_expr
},
7720 {"remote_foreground", 1, 1, f_remote_foreground
},
7721 {"remote_peek", 1, 2, f_remote_peek
},
7722 {"remote_read", 1, 1, f_remote_read
},
7723 {"remote_send", 2, 3, f_remote_send
},
7724 {"remove", 2, 3, f_remove
},
7725 {"rename", 2, 2, f_rename
},
7726 {"repeat", 2, 2, f_repeat
},
7727 {"resolve", 1, 1, f_resolve
},
7728 {"reverse", 1, 1, f_reverse
},
7730 {"round", 1, 1, f_round
},
7732 {"search", 1, 4, f_search
},
7733 {"searchdecl", 1, 3, f_searchdecl
},
7734 {"searchpair", 3, 7, f_searchpair
},
7735 {"searchpairpos", 3, 7, f_searchpairpos
},
7736 {"searchpos", 1, 4, f_searchpos
},
7737 {"server2client", 2, 2, f_server2client
},
7738 {"serverlist", 0, 0, f_serverlist
},
7739 {"setbufvar", 3, 3, f_setbufvar
},
7740 {"setcmdpos", 1, 1, f_setcmdpos
},
7741 {"setline", 2, 2, f_setline
},
7742 {"setloclist", 2, 3, f_setloclist
},
7743 {"setmatches", 1, 1, f_setmatches
},
7744 {"setpos", 2, 2, f_setpos
},
7745 {"setqflist", 1, 2, f_setqflist
},
7746 {"setreg", 2, 3, f_setreg
},
7747 {"settabwinvar", 4, 4, f_settabwinvar
},
7748 {"setwinvar", 3, 3, f_setwinvar
},
7749 {"shellescape", 1, 2, f_shellescape
},
7750 {"simplify", 1, 1, f_simplify
},
7752 {"sin", 1, 1, f_sin
},
7754 {"sort", 1, 2, f_sort
},
7755 {"soundfold", 1, 1, f_soundfold
},
7756 {"spellbadword", 0, 1, f_spellbadword
},
7757 {"spellsuggest", 1, 3, f_spellsuggest
},
7758 {"split", 1, 3, f_split
},
7760 {"sqrt", 1, 1, f_sqrt
},
7761 {"str2float", 1, 1, f_str2float
},
7763 {"str2nr", 1, 2, f_str2nr
},
7764 #ifdef HAVE_STRFTIME
7765 {"strftime", 1, 2, f_strftime
},
7767 {"stridx", 2, 3, f_stridx
},
7768 {"string", 1, 1, f_string
},
7769 {"strlen", 1, 1, f_strlen
},
7770 {"strpart", 2, 3, f_strpart
},
7771 {"strridx", 2, 3, f_strridx
},
7772 {"strtrans", 1, 1, f_strtrans
},
7773 {"submatch", 1, 1, f_submatch
},
7774 {"substitute", 4, 4, f_substitute
},
7775 {"synID", 3, 3, f_synID
},
7776 {"synIDattr", 2, 3, f_synIDattr
},
7777 {"synIDtrans", 1, 1, f_synIDtrans
},
7778 {"synstack", 2, 2, f_synstack
},
7779 {"system", 1, 2, f_system
},
7780 {"tabpagebuflist", 0, 1, f_tabpagebuflist
},
7781 {"tabpagenr", 0, 1, f_tabpagenr
},
7782 {"tabpagewinnr", 1, 2, f_tabpagewinnr
},
7783 {"tagfiles", 0, 0, f_tagfiles
},
7784 {"taglist", 1, 1, f_taglist
},
7785 {"tempname", 0, 0, f_tempname
},
7786 {"test", 1, 1, f_test
},
7787 {"tolower", 1, 1, f_tolower
},
7788 {"toupper", 1, 1, f_toupper
},
7791 {"trunc", 1, 1, f_trunc
},
7793 {"type", 1, 1, f_type
},
7794 {"values", 1, 1, f_values
},
7795 {"virtcol", 1, 1, f_virtcol
},
7796 {"visualmode", 0, 1, f_visualmode
},
7797 {"winbufnr", 1, 1, f_winbufnr
},
7798 {"wincol", 0, 0, f_wincol
},
7799 {"winheight", 1, 1, f_winheight
},
7800 {"winline", 0, 0, f_winline
},
7801 {"winnr", 0, 1, f_winnr
},
7802 {"winrestcmd", 0, 0, f_winrestcmd
},
7803 {"winrestview", 1, 1, f_winrestview
},
7804 {"winsaveview", 0, 0, f_winsaveview
},
7805 {"winwidth", 1, 1, f_winwidth
},
7806 {"writefile", 2, 3, f_writefile
},
7809 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7812 * Function given to ExpandGeneric() to obtain the list of internal
7813 * or user defined function names.
7816 get_function_name(xp
, idx
)
7820 static int intidx
= -1;
7827 name
= get_user_func_name(xp
, idx
);
7831 if (++intidx
< (int)(sizeof(functions
) / sizeof(struct fst
)))
7833 STRCPY(IObuff
, functions
[intidx
].f_name
);
7834 STRCAT(IObuff
, "(");
7835 if (functions
[intidx
].f_max_argc
== 0)
7836 STRCAT(IObuff
, ")");
7844 * Function given to ExpandGeneric() to obtain the list of internal or
7845 * user defined variable or function names.
7848 get_expr_name(xp
, idx
)
7852 static int intidx
= -1;
7859 name
= get_function_name(xp
, idx
);
7863 return get_user_var_name(xp
, ++intidx
);
7866 #endif /* FEAT_CMDL_COMPL */
7869 * Find internal function in table above.
7870 * Return index, or -1 if not found
7873 find_internal_func(name
)
7874 char_u
*name
; /* name of the function */
7877 int last
= (int)(sizeof(functions
) / sizeof(struct fst
)) - 1;
7882 * Find the function name in the table. Binary search.
7884 while (first
<= last
)
7886 x
= first
+ ((unsigned)(last
- first
) >> 1);
7887 cmp
= STRCMP(name
, functions
[x
].f_name
);
7899 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7900 * name it contains, otherwise return "name".
7903 deref_func_name(name
, lenp
)
7912 v
= find_var(name
, NULL
);
7914 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
7916 if (v
->di_tv
.vval
.v_string
== NULL
)
7919 return (char_u
*)""; /* just in case */
7921 *lenp
= (int)STRLEN(v
->di_tv
.vval
.v_string
);
7922 return v
->di_tv
.vval
.v_string
;
7929 * Allocate a variable for the result of a function.
7930 * Return OK or FAIL.
7933 get_func_tv(name
, len
, rettv
, arg
, firstline
, lastline
, doesrange
,
7935 char_u
*name
; /* name of the function */
7936 int len
; /* length of "name" */
7938 char_u
**arg
; /* argument, pointing to the '(' */
7939 linenr_T firstline
; /* first line of range */
7940 linenr_T lastline
; /* last line of range */
7941 int *doesrange
; /* return: function handled range */
7943 dict_T
*selfdict
; /* Dictionary for "self" */
7947 typval_T argvars
[MAX_FUNC_ARGS
+ 1]; /* vars for arguments */
7948 int argcount
= 0; /* number of arguments found */
7951 * Get the arguments.
7954 while (argcount
< MAX_FUNC_ARGS
)
7956 argp
= skipwhite(argp
+ 1); /* skip the '(' or ',' */
7957 if (*argp
== ')' || *argp
== ',' || *argp
== NUL
)
7959 if (eval1(&argp
, &argvars
[argcount
], evaluate
) == FAIL
)
7974 ret
= call_func(name
, len
, rettv
, argcount
, argvars
,
7975 firstline
, lastline
, doesrange
, evaluate
, selfdict
);
7976 else if (!aborting())
7978 if (argcount
== MAX_FUNC_ARGS
)
7979 emsg_funcname(N_("E740: Too many arguments for function %s"), name
);
7981 emsg_funcname(N_("E116: Invalid arguments for function %s"), name
);
7984 while (--argcount
>= 0)
7985 clear_tv(&argvars
[argcount
]);
7987 *arg
= skipwhite(argp
);
7993 * Call a function with its resolved parameters
7994 * Return OK when the function can't be called, FAIL otherwise.
7995 * Also returns OK when an error was encountered while executing the function.
7998 call_func(name
, len
, rettv
, argcount
, argvars
, firstline
, lastline
,
7999 doesrange
, evaluate
, selfdict
)
8000 char_u
*name
; /* name of the function */
8001 int len
; /* length of "name" */
8002 typval_T
*rettv
; /* return value goes here */
8003 int argcount
; /* number of "argvars" */
8004 typval_T
*argvars
; /* vars for arguments, must have "argcount"
8005 PLUS ONE elements! */
8006 linenr_T firstline
; /* first line of range */
8007 linenr_T lastline
; /* last line of range */
8008 int *doesrange
; /* return: function handled range */
8010 dict_T
*selfdict
; /* Dictionary for "self" */
8013 #define ERROR_UNKNOWN 0
8014 #define ERROR_TOOMANY 1
8015 #define ERROR_TOOFEW 2
8016 #define ERROR_SCRIPT 3
8017 #define ERROR_DICT 4
8018 #define ERROR_NONE 5
8019 #define ERROR_OTHER 6
8020 int error
= ERROR_NONE
;
8025 #define FLEN_FIXED 40
8026 char_u fname_buf
[FLEN_FIXED
+ 1];
8030 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8031 * Change <SNR>123_name() to K_SNR 123_name().
8032 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8036 llen
= eval_fname_script(name
);
8039 fname_buf
[0] = K_SPECIAL
;
8040 fname_buf
[1] = KS_EXTRA
;
8041 fname_buf
[2] = (int)KE_SNR
;
8043 if (eval_fname_sid(name
)) /* "<SID>" or "s:" */
8045 if (current_SID
<= 0)
8046 error
= ERROR_SCRIPT
;
8049 sprintf((char *)fname_buf
+ 3, "%ld_", (long)current_SID
);
8050 i
= (int)STRLEN(fname_buf
);
8053 if (i
+ STRLEN(name
+ llen
) < FLEN_FIXED
)
8055 STRCPY(fname_buf
+ i
, name
+ llen
);
8060 fname
= alloc((unsigned)(i
+ STRLEN(name
+ llen
) + 1));
8062 error
= ERROR_OTHER
;
8065 mch_memmove(fname
, fname_buf
, (size_t)i
);
8066 STRCPY(fname
+ i
, name
+ llen
);
8076 /* execute the function if no errors detected and executing */
8077 if (evaluate
&& error
== ERROR_NONE
)
8079 rettv
->v_type
= VAR_NUMBER
; /* default rettv is number zero */
8080 rettv
->vval
.v_number
= 0;
8081 error
= ERROR_UNKNOWN
;
8083 if (!builtin_function(fname
))
8086 * User defined function.
8088 fp
= find_func(fname
);
8091 /* Trigger FuncUndefined event, may load the function. */
8093 && apply_autocmds(EVENT_FUNCUNDEFINED
,
8094 fname
, fname
, TRUE
, NULL
)
8097 /* executed an autocommand, search for the function again */
8098 fp
= find_func(fname
);
8101 /* Try loading a package. */
8102 if (fp
== NULL
&& script_autoload(fname
, TRUE
) && !aborting())
8104 /* loaded a package, search for the function again */
8105 fp
= find_func(fname
);
8110 if (fp
->uf_flags
& FC_RANGE
)
8112 if (argcount
< fp
->uf_args
.ga_len
)
8113 error
= ERROR_TOOFEW
;
8114 else if (!fp
->uf_varargs
&& argcount
> fp
->uf_args
.ga_len
)
8115 error
= ERROR_TOOMANY
;
8116 else if ((fp
->uf_flags
& FC_DICT
) && selfdict
== NULL
)
8121 * Call the user function.
8122 * Save and restore search patterns, script variables and
8125 save_search_patterns();
8128 call_user_func(fp
, argcount
, argvars
, rettv
,
8129 firstline
, lastline
,
8130 (fp
->uf_flags
& FC_DICT
) ? selfdict
: NULL
);
8131 if (--fp
->uf_calls
<= 0 && isdigit(*fp
->uf_name
)
8132 && fp
->uf_refcount
<= 0)
8133 /* Function was unreferenced while being used, free it
8137 restore_search_patterns();
8145 * Find the function name in the table, call its implementation.
8147 i
= find_internal_func(fname
);
8150 if (argcount
< functions
[i
].f_min_argc
)
8151 error
= ERROR_TOOFEW
;
8152 else if (argcount
> functions
[i
].f_max_argc
)
8153 error
= ERROR_TOOMANY
;
8156 argvars
[argcount
].v_type
= VAR_UNKNOWN
;
8157 functions
[i
].f_func(argvars
, rettv
);
8163 * The function call (or "FuncUndefined" autocommand sequence) might
8164 * have been aborted by an error, an interrupt, or an explicitly thrown
8165 * exception that has not been caught so far. This situation can be
8166 * tested for by calling aborting(). For an error in an internal
8167 * function or for the "E132" error in call_user_func(), however, the
8168 * throw point at which the "force_abort" flag (temporarily reset by
8169 * emsg()) is normally updated has not been reached yet. We need to
8170 * update that flag first to make aborting() reliable.
8172 update_force_abort();
8174 if (error
== ERROR_NONE
)
8178 * Report an error unless the argument evaluation or function call has been
8179 * cancelled due to an aborting error, an interrupt, or an exception.
8186 emsg_funcname(N_("E117: Unknown function: %s"), name
);
8189 emsg_funcname(e_toomanyarg
, name
);
8192 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
8196 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
8200 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
8207 if (fname
!= name
&& fname
!= fname_buf
)
8214 * Give an error message with a function name. Handle <SNR> things.
8215 * "ermsg" is to be passed without translation, use N_() instead of _().
8218 emsg_funcname(ermsg
, name
)
8224 if (*name
== K_SPECIAL
)
8225 p
= concat_str((char_u
*)"<SNR>", name
+ 3);
8234 * Return TRUE for a non-zero Number and a non-empty String.
8237 non_zero_arg(argvars
)
8240 return ((argvars
[0].v_type
== VAR_NUMBER
8241 && argvars
[0].vval
.v_number
!= 0)
8242 || (argvars
[0].v_type
== VAR_STRING
8243 && argvars
[0].vval
.v_string
!= NULL
8244 && *argvars
[0].vval
.v_string
!= NUL
));
8247 /*********************************************
8248 * Implementation of the built-in functions
8253 * "abs(expr)" function
8256 f_abs(argvars
, rettv
)
8260 if (argvars
[0].v_type
== VAR_FLOAT
)
8262 rettv
->v_type
= VAR_FLOAT
;
8263 rettv
->vval
.v_float
= fabs(argvars
[0].vval
.v_float
);
8270 n
= get_tv_number_chk(&argvars
[0], &error
);
8272 rettv
->vval
.v_number
= -1;
8274 rettv
->vval
.v_number
= n
;
8276 rettv
->vval
.v_number
= -n
;
8282 * "add(list, item)" function
8285 f_add(argvars
, rettv
)
8291 rettv
->vval
.v_number
= 1; /* Default: Failed */
8292 if (argvars
[0].v_type
== VAR_LIST
)
8294 if ((l
= argvars
[0].vval
.v_list
) != NULL
8295 && !tv_check_lock(l
->lv_lock
, (char_u
*)"add()")
8296 && list_append_tv(l
, &argvars
[1]) == OK
)
8297 copy_tv(&argvars
[0], rettv
);
8304 * "append(lnum, string/list)" function
8307 f_append(argvars
, rettv
)
8314 listitem_T
*li
= NULL
;
8318 lnum
= get_tv_lnum(argvars
);
8320 && lnum
<= curbuf
->b_ml
.ml_line_count
8321 && u_save(lnum
, lnum
+ 1) == OK
)
8323 if (argvars
[1].v_type
== VAR_LIST
)
8325 l
= argvars
[1].vval
.v_list
;
8333 tv
= &argvars
[1]; /* append a string */
8334 else if (li
== NULL
)
8335 break; /* end of list */
8337 tv
= &li
->li_tv
; /* append item from list */
8338 line
= get_tv_string_chk(tv
);
8339 if (line
== NULL
) /* type error */
8341 rettv
->vval
.v_number
= 1; /* Failed */
8344 ml_append(lnum
+ added
, line
, (colnr_T
)0, FALSE
);
8351 appended_lines_mark(lnum
, added
);
8352 if (curwin
->w_cursor
.lnum
> lnum
)
8353 curwin
->w_cursor
.lnum
+= added
;
8356 rettv
->vval
.v_number
= 1; /* Failed */
8363 f_argc(argvars
, rettv
)
8364 typval_T
*argvars UNUSED
;
8367 rettv
->vval
.v_number
= ARGCOUNT
;
8371 * "argidx()" function
8374 f_argidx(argvars
, rettv
)
8375 typval_T
*argvars UNUSED
;
8378 rettv
->vval
.v_number
= curwin
->w_arg_idx
;
8382 * "argv(nr)" function
8385 f_argv(argvars
, rettv
)
8391 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
8393 idx
= get_tv_number_chk(&argvars
[0], NULL
);
8394 if (idx
>= 0 && idx
< ARGCOUNT
)
8395 rettv
->vval
.v_string
= vim_strsave(alist_name(&ARGLIST
[idx
]));
8397 rettv
->vval
.v_string
= NULL
;
8398 rettv
->v_type
= VAR_STRING
;
8400 else if (rettv_list_alloc(rettv
) == OK
)
8401 for (idx
= 0; idx
< ARGCOUNT
; ++idx
)
8402 list_append_string(rettv
->vval
.v_list
,
8403 alist_name(&ARGLIST
[idx
]), -1);
8407 static int get_float_arg
__ARGS((typval_T
*argvars
, float_T
*f
));
8410 * Get the float value of "argvars[0]" into "f".
8411 * Returns FAIL when the argument is not a Number or Float.
8414 get_float_arg(argvars
, f
)
8418 if (argvars
[0].v_type
== VAR_FLOAT
)
8420 *f
= argvars
[0].vval
.v_float
;
8423 if (argvars
[0].v_type
== VAR_NUMBER
)
8425 *f
= (float_T
)argvars
[0].vval
.v_number
;
8428 EMSG(_("E808: Number or Float required"));
8436 f_atan(argvars
, rettv
)
8442 rettv
->v_type
= VAR_FLOAT
;
8443 if (get_float_arg(argvars
, &f
) == OK
)
8444 rettv
->vval
.v_float
= atan(f
);
8446 rettv
->vval
.v_float
= 0.0;
8451 * "browse(save, title, initdir, default)" function
8454 f_browse(argvars
, rettv
)
8455 typval_T
*argvars UNUSED
;
8463 char_u buf
[NUMBUFLEN
];
8464 char_u buf2
[NUMBUFLEN
];
8467 save
= get_tv_number_chk(&argvars
[0], &error
);
8468 title
= get_tv_string_chk(&argvars
[1]);
8469 initdir
= get_tv_string_buf_chk(&argvars
[2], buf
);
8470 defname
= get_tv_string_buf_chk(&argvars
[3], buf2
);
8472 if (error
|| title
== NULL
|| initdir
== NULL
|| defname
== NULL
)
8473 rettv
->vval
.v_string
= NULL
;
8475 rettv
->vval
.v_string
=
8476 do_browse(save
? BROWSE_SAVE
: 0,
8477 title
, defname
, NULL
, initdir
, NULL
, curbuf
);
8479 rettv
->vval
.v_string
= NULL
;
8481 rettv
->v_type
= VAR_STRING
;
8485 * "browsedir(title, initdir)" function
8488 f_browsedir(argvars
, rettv
)
8489 typval_T
*argvars UNUSED
;
8495 char_u buf
[NUMBUFLEN
];
8497 title
= get_tv_string_chk(&argvars
[0]);
8498 initdir
= get_tv_string_buf_chk(&argvars
[1], buf
);
8500 if (title
== NULL
|| initdir
== NULL
)
8501 rettv
->vval
.v_string
= NULL
;
8503 rettv
->vval
.v_string
= do_browse(BROWSE_DIR
,
8504 title
, NULL
, NULL
, initdir
, NULL
, curbuf
);
8506 rettv
->vval
.v_string
= NULL
;
8508 rettv
->v_type
= VAR_STRING
;
8511 static buf_T
*find_buffer
__ARGS((typval_T
*avar
));
8514 * Find a buffer by number or exact name.
8522 if (avar
->v_type
== VAR_NUMBER
)
8523 buf
= buflist_findnr((int)avar
->vval
.v_number
);
8524 else if (avar
->v_type
== VAR_STRING
&& avar
->vval
.v_string
!= NULL
)
8526 buf
= buflist_findname_exp(avar
->vval
.v_string
);
8529 /* No full path name match, try a match with a URL or a "nofile"
8530 * buffer, these don't use the full path. */
8531 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
8532 if (buf
->b_fname
!= NULL
8533 && (path_with_url(buf
->b_fname
)
8534 #ifdef FEAT_QUICKFIX
8538 && STRCMP(buf
->b_fname
, avar
->vval
.v_string
) == 0)
8546 * "bufexists(expr)" function
8549 f_bufexists(argvars
, rettv
)
8553 rettv
->vval
.v_number
= (find_buffer(&argvars
[0]) != NULL
);
8557 * "buflisted(expr)" function
8560 f_buflisted(argvars
, rettv
)
8566 buf
= find_buffer(&argvars
[0]);
8567 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_p_bl
);
8571 * "bufloaded(expr)" function
8574 f_bufloaded(argvars
, rettv
)
8580 buf
= find_buffer(&argvars
[0]);
8581 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_ml
.ml_mfp
!= NULL
);
8584 static buf_T
*get_buf_tv
__ARGS((typval_T
*tv
));
8587 * Get buffer by number or pattern.
8593 char_u
*name
= tv
->vval
.v_string
;
8598 if (tv
->v_type
== VAR_NUMBER
)
8599 return buflist_findnr((int)tv
->vval
.v_number
);
8600 if (tv
->v_type
!= VAR_STRING
)
8602 if (name
== NULL
|| *name
== NUL
)
8604 if (name
[0] == '$' && name
[1] == NUL
)
8607 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
8608 save_magic
= p_magic
;
8611 p_cpo
= (char_u
*)"";
8613 buf
= buflist_findnr(buflist_findpat(name
, name
+ STRLEN(name
),
8616 p_magic
= save_magic
;
8619 /* If not found, try expanding the name, like done for bufexists(). */
8621 buf
= find_buffer(tv
);
8627 * "bufname(expr)" function
8630 f_bufname(argvars
, rettv
)
8636 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8638 buf
= get_buf_tv(&argvars
[0]);
8639 rettv
->v_type
= VAR_STRING
;
8640 if (buf
!= NULL
&& buf
->b_fname
!= NULL
)
8641 rettv
->vval
.v_string
= vim_strsave(buf
->b_fname
);
8643 rettv
->vval
.v_string
= NULL
;
8648 * "bufnr(expr)" function
8651 f_bufnr(argvars
, rettv
)
8659 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8661 buf
= get_buf_tv(&argvars
[0]);
8664 /* If the buffer isn't found and the second argument is not zero create a
8667 && argvars
[1].v_type
!= VAR_UNKNOWN
8668 && get_tv_number_chk(&argvars
[1], &error
) != 0
8670 && (name
= get_tv_string_chk(&argvars
[0])) != NULL
8672 buf
= buflist_new(name
, NULL
, (linenr_T
)1, 0);
8675 rettv
->vval
.v_number
= buf
->b_fnum
;
8677 rettv
->vval
.v_number
= -1;
8681 * "bufwinnr(nr)" function
8684 f_bufwinnr(argvars
, rettv
)
8694 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8696 buf
= get_buf_tv(&argvars
[0]);
8698 for (wp
= firstwin
; wp
; wp
= wp
->w_next
)
8701 if (wp
->w_buffer
== buf
)
8704 rettv
->vval
.v_number
= (wp
!= NULL
? winnr
: -1);
8706 rettv
->vval
.v_number
= (curwin
->w_buffer
== buf
? 1 : -1);
8712 * "byte2line(byte)" function
8715 f_byte2line(argvars
, rettv
)
8716 typval_T
*argvars UNUSED
;
8719 #ifndef FEAT_BYTEOFF
8720 rettv
->vval
.v_number
= -1;
8724 boff
= get_tv_number(&argvars
[0]) - 1; /* boff gets -1 on type error */
8726 rettv
->vval
.v_number
= -1;
8728 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
,
8729 (linenr_T
)0, &boff
);
8734 * "byteidx()" function
8737 f_byteidx(argvars
, rettv
)
8747 str
= get_tv_string_chk(&argvars
[0]);
8748 idx
= get_tv_number_chk(&argvars
[1], NULL
);
8749 rettv
->vval
.v_number
= -1;
8750 if (str
== NULL
|| idx
< 0)
8755 for ( ; idx
> 0; idx
--)
8757 if (*t
== NUL
) /* EOL reached */
8759 t
+= (*mb_ptr2len
)(t
);
8761 rettv
->vval
.v_number
= (varnumber_T
)(t
- str
);
8763 if ((size_t)idx
<= STRLEN(str
))
8764 rettv
->vval
.v_number
= idx
;
8769 * "call(func, arglist)" function
8772 f_call(argvars
, rettv
)
8777 typval_T argv
[MAX_FUNC_ARGS
+ 1];
8781 dict_T
*selfdict
= NULL
;
8783 if (argvars
[1].v_type
!= VAR_LIST
)
8788 if (argvars
[1].vval
.v_list
== NULL
)
8791 if (argvars
[0].v_type
== VAR_FUNC
)
8792 func
= argvars
[0].vval
.v_string
;
8794 func
= get_tv_string(&argvars
[0]);
8796 return; /* type error or empty name */
8798 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8800 if (argvars
[2].v_type
!= VAR_DICT
)
8805 selfdict
= argvars
[2].vval
.v_dict
;
8808 for (item
= argvars
[1].vval
.v_list
->lv_first
; item
!= NULL
;
8809 item
= item
->li_next
)
8811 if (argc
== MAX_FUNC_ARGS
)
8813 EMSG(_("E699: Too many arguments"));
8816 /* Make a copy of each argument. This is needed to be able to set
8817 * v_lock to VAR_FIXED in the copy without changing the original list.
8819 copy_tv(&item
->li_tv
, &argv
[argc
++]);
8823 (void)call_func(func
, (int)STRLEN(func
), rettv
, argc
, argv
,
8824 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
8825 &dummy
, TRUE
, selfdict
);
8827 /* Free the arguments. */
8829 clear_tv(&argv
[--argc
]);
8834 * "ceil({float})" function
8837 f_ceil(argvars
, rettv
)
8843 rettv
->v_type
= VAR_FLOAT
;
8844 if (get_float_arg(argvars
, &f
) == OK
)
8845 rettv
->vval
.v_float
= ceil(f
);
8847 rettv
->vval
.v_float
= 0.0;
8852 * "changenr()" function
8855 f_changenr(argvars
, rettv
)
8856 typval_T
*argvars UNUSED
;
8859 rettv
->vval
.v_number
= curbuf
->b_u_seq_cur
;
8863 * "char2nr(string)" function
8866 f_char2nr(argvars
, rettv
)
8872 rettv
->vval
.v_number
= (*mb_ptr2char
)(get_tv_string(&argvars
[0]));
8875 rettv
->vval
.v_number
= get_tv_string(&argvars
[0])[0];
8879 * "cindent(lnum)" function
8882 f_cindent(argvars
, rettv
)
8890 pos
= curwin
->w_cursor
;
8891 lnum
= get_tv_lnum(argvars
);
8892 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
8894 curwin
->w_cursor
.lnum
= lnum
;
8895 rettv
->vval
.v_number
= get_c_indent();
8896 curwin
->w_cursor
= pos
;
8900 rettv
->vval
.v_number
= -1;
8904 * "clearmatches()" function
8907 f_clearmatches(argvars
, rettv
)
8908 typval_T
*argvars UNUSED
;
8909 typval_T
*rettv UNUSED
;
8911 #ifdef FEAT_SEARCH_EXTRA
8912 clear_matches(curwin
);
8917 * "col(string)" function
8920 f_col(argvars
, rettv
)
8926 int fnum
= curbuf
->b_fnum
;
8928 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
8929 if (fp
!= NULL
&& fnum
== curbuf
->b_fnum
)
8931 if (fp
->col
== MAXCOL
)
8933 /* '> can be MAXCOL, get the length of the line then */
8934 if (fp
->lnum
<= curbuf
->b_ml
.ml_line_count
)
8935 col
= (colnr_T
)STRLEN(ml_get(fp
->lnum
)) + 1;
8942 #ifdef FEAT_VIRTUALEDIT
8943 /* col(".") when the cursor is on the NUL at the end of the line
8944 * because of "coladd" can be seen as an extra column. */
8945 if (virtual_active() && fp
== &curwin
->w_cursor
)
8947 char_u
*p
= ml_get_cursor();
8949 if (curwin
->w_cursor
.coladd
>= (colnr_T
)chartabsize(p
,
8950 curwin
->w_virtcol
- curwin
->w_cursor
.coladd
))
8955 if (*p
!= NUL
&& p
[(l
= (*mb_ptr2len
)(p
))] == NUL
)
8958 if (*p
!= NUL
&& p
[1] == NUL
)
8966 rettv
->vval
.v_number
= col
;
8969 #if defined(FEAT_INS_EXPAND)
8971 * "complete()" function
8974 f_complete(argvars
, rettv
)
8976 typval_T
*rettv UNUSED
;
8980 if ((State
& INSERT
) == 0)
8982 EMSG(_("E785: complete() can only be used in Insert mode"));
8986 /* Check for undo allowed here, because if something was already inserted
8987 * the line was already saved for undo and this check isn't done. */
8988 if (!undo_allowed())
8991 if (argvars
[1].v_type
!= VAR_LIST
|| argvars
[1].vval
.v_list
== NULL
)
8997 startcol
= get_tv_number_chk(&argvars
[0], NULL
);
9001 set_completion(startcol
- 1, argvars
[1].vval
.v_list
);
9005 * "complete_add()" function
9008 f_complete_add(argvars
, rettv
)
9012 rettv
->vval
.v_number
= ins_compl_add_tv(&argvars
[0], 0);
9016 * "complete_check()" function
9019 f_complete_check(argvars
, rettv
)
9020 typval_T
*argvars UNUSED
;
9023 int saved
= RedrawingDisabled
;
9025 RedrawingDisabled
= 0;
9026 ins_compl_check_keys(0);
9027 rettv
->vval
.v_number
= compl_interrupted
;
9028 RedrawingDisabled
= saved
;
9033 * "confirm(message, buttons[, default [, type]])" function
9036 f_confirm(argvars
, rettv
)
9037 typval_T
*argvars UNUSED
;
9038 typval_T
*rettv UNUSED
;
9040 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9042 char_u
*buttons
= NULL
;
9043 char_u buf
[NUMBUFLEN
];
9044 char_u buf2
[NUMBUFLEN
];
9046 int type
= VIM_GENERIC
;
9050 message
= get_tv_string_chk(&argvars
[0]);
9051 if (message
== NULL
)
9053 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9055 buttons
= get_tv_string_buf_chk(&argvars
[1], buf
);
9056 if (buttons
== NULL
)
9058 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9060 def
= get_tv_number_chk(&argvars
[2], &error
);
9061 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9063 typestr
= get_tv_string_buf_chk(&argvars
[3], buf2
);
9064 if (typestr
== NULL
)
9068 switch (TOUPPER_ASC(*typestr
))
9070 case 'E': type
= VIM_ERROR
; break;
9071 case 'Q': type
= VIM_QUESTION
; break;
9072 case 'I': type
= VIM_INFO
; break;
9073 case 'W': type
= VIM_WARNING
; break;
9074 case 'G': type
= VIM_GENERIC
; break;
9081 if (buttons
== NULL
|| *buttons
== NUL
)
9082 buttons
= (char_u
*)_("&Ok");
9085 rettv
->vval
.v_number
= do_dialog(type
, NULL
, message
, buttons
,
9094 f_copy(argvars
, rettv
)
9098 item_copy(&argvars
[0], rettv
, FALSE
, 0);
9106 f_cos(argvars
, rettv
)
9112 rettv
->v_type
= VAR_FLOAT
;
9113 if (get_float_arg(argvars
, &f
) == OK
)
9114 rettv
->vval
.v_float
= cos(f
);
9116 rettv
->vval
.v_float
= 0.0;
9121 * "count()" function
9124 f_count(argvars
, rettv
)
9131 if (argvars
[0].v_type
== VAR_LIST
)
9137 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
9140 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9144 ic
= get_tv_number_chk(&argvars
[2], &error
);
9145 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9147 idx
= get_tv_number_chk(&argvars
[3], &error
);
9150 li
= list_find(l
, idx
);
9152 EMSGN(_(e_listidx
), idx
);
9159 for ( ; li
!= NULL
; li
= li
->li_next
)
9160 if (tv_equal(&li
->li_tv
, &argvars
[1], ic
))
9164 else if (argvars
[0].v_type
== VAR_DICT
)
9170 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
9174 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9176 ic
= get_tv_number_chk(&argvars
[2], &error
);
9177 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
9181 todo
= error
? 0 : (int)d
->dv_hashtab
.ht_used
;
9182 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
9184 if (!HASHITEM_EMPTY(hi
))
9187 if (tv_equal(&HI2DI(hi
)->di_tv
, &argvars
[1], ic
))
9194 EMSG2(_(e_listdictarg
), "count()");
9195 rettv
->vval
.v_number
= n
;
9199 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9201 * Checks the existence of a cscope connection.
9204 f_cscope_connection(argvars
, rettv
)
9205 typval_T
*argvars UNUSED
;
9206 typval_T
*rettv UNUSED
;
9210 char_u
*dbpath
= NULL
;
9211 char_u
*prepend
= NULL
;
9212 char_u buf
[NUMBUFLEN
];
9214 if (argvars
[0].v_type
!= VAR_UNKNOWN
9215 && argvars
[1].v_type
!= VAR_UNKNOWN
)
9217 num
= (int)get_tv_number(&argvars
[0]);
9218 dbpath
= get_tv_string(&argvars
[1]);
9219 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9220 prepend
= get_tv_string_buf(&argvars
[2], buf
);
9223 rettv
->vval
.v_number
= cs_connection(num
, dbpath
, prepend
);
9228 * "cursor(lnum, col)" function
9230 * Moves the cursor to the specified line and column.
9231 * Returns 0 when the position could be set, -1 otherwise.
9234 f_cursor(argvars
, rettv
)
9239 #ifdef FEAT_VIRTUALEDIT
9243 rettv
->vval
.v_number
= -1;
9244 if (argvars
[1].v_type
== VAR_UNKNOWN
)
9248 if (list2fpos(argvars
, &pos
, NULL
) == FAIL
)
9252 #ifdef FEAT_VIRTUALEDIT
9253 coladd
= pos
.coladd
;
9258 line
= get_tv_lnum(argvars
);
9259 col
= get_tv_number_chk(&argvars
[1], NULL
);
9260 #ifdef FEAT_VIRTUALEDIT
9261 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9262 coladd
= get_tv_number_chk(&argvars
[2], NULL
);
9265 if (line
< 0 || col
< 0
9266 #ifdef FEAT_VIRTUALEDIT
9270 return; /* type error; errmsg already given */
9272 curwin
->w_cursor
.lnum
= line
;
9274 curwin
->w_cursor
.col
= col
- 1;
9275 #ifdef FEAT_VIRTUALEDIT
9276 curwin
->w_cursor
.coladd
= coladd
;
9279 /* Make sure the cursor is in a valid position. */
9282 /* Correct cursor for multi-byte character. */
9287 curwin
->w_set_curswant
= TRUE
;
9288 rettv
->vval
.v_number
= 0;
9292 * "deepcopy()" function
9295 f_deepcopy(argvars
, rettv
)
9301 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9302 noref
= get_tv_number_chk(&argvars
[1], NULL
);
9303 if (noref
< 0 || noref
> 1)
9307 current_copyID
+= COPYID_INC
;
9308 item_copy(&argvars
[0], rettv
, TRUE
, noref
== 0 ? current_copyID
: 0);
9313 * "delete()" function
9316 f_delete(argvars
, rettv
)
9320 if (check_restricted() || check_secure())
9321 rettv
->vval
.v_number
= -1;
9323 rettv
->vval
.v_number
= mch_remove(get_tv_string(&argvars
[0]));
9327 * "did_filetype()" function
9330 f_did_filetype(argvars
, rettv
)
9331 typval_T
*argvars UNUSED
;
9332 typval_T
*rettv UNUSED
;
9335 rettv
->vval
.v_number
= did_filetype
;
9340 * "diff_filler()" function
9343 f_diff_filler(argvars
, rettv
)
9344 typval_T
*argvars UNUSED
;
9345 typval_T
*rettv UNUSED
;
9348 rettv
->vval
.v_number
= diff_check_fill(curwin
, get_tv_lnum(argvars
));
9353 * "diff_hlID()" function
9356 f_diff_hlID(argvars
, rettv
)
9357 typval_T
*argvars UNUSED
;
9358 typval_T
*rettv UNUSED
;
9361 linenr_T lnum
= get_tv_lnum(argvars
);
9362 static linenr_T prev_lnum
= 0;
9363 static int changedtick
= 0;
9364 static int fnum
= 0;
9365 static int change_start
= 0;
9366 static int change_end
= 0;
9367 static hlf_T hlID
= (hlf_T
)0;
9371 if (lnum
< 0) /* ignore type error in {lnum} arg */
9373 if (lnum
!= prev_lnum
9374 || changedtick
!= curbuf
->b_changedtick
9375 || fnum
!= curbuf
->b_fnum
)
9377 /* New line, buffer, change: need to get the values. */
9378 filler_lines
= diff_check(curwin
, lnum
);
9379 if (filler_lines
< 0)
9381 if (filler_lines
== -1)
9383 change_start
= MAXCOL
;
9385 if (diff_find_change(curwin
, lnum
, &change_start
, &change_end
))
9386 hlID
= HLF_ADD
; /* added line */
9388 hlID
= HLF_CHD
; /* changed line */
9391 hlID
= HLF_ADD
; /* added line */
9396 changedtick
= curbuf
->b_changedtick
;
9397 fnum
= curbuf
->b_fnum
;
9400 if (hlID
== HLF_CHD
|| hlID
== HLF_TXD
)
9402 col
= get_tv_number(&argvars
[1]) - 1; /* ignore type error in {col} */
9403 if (col
>= change_start
&& col
<= change_end
)
9404 hlID
= HLF_TXD
; /* changed text */
9406 hlID
= HLF_CHD
; /* changed line */
9408 rettv
->vval
.v_number
= hlID
== (hlf_T
)0 ? 0 : (int)hlID
;
9413 * "empty({expr})" function
9416 f_empty(argvars
, rettv
)
9422 switch (argvars
[0].v_type
)
9426 n
= argvars
[0].vval
.v_string
== NULL
9427 || *argvars
[0].vval
.v_string
== NUL
;
9430 n
= argvars
[0].vval
.v_number
== 0;
9434 n
= argvars
[0].vval
.v_float
== 0.0;
9438 n
= argvars
[0].vval
.v_list
== NULL
9439 || argvars
[0].vval
.v_list
->lv_first
== NULL
;
9442 n
= argvars
[0].vval
.v_dict
== NULL
9443 || argvars
[0].vval
.v_dict
->dv_hashtab
.ht_used
== 0;
9446 EMSG2(_(e_intern2
), "f_empty()");
9450 rettv
->vval
.v_number
= n
;
9454 * "escape({string}, {chars})" function
9457 f_escape(argvars
, rettv
)
9461 char_u buf
[NUMBUFLEN
];
9463 rettv
->vval
.v_string
= vim_strsave_escaped(get_tv_string(&argvars
[0]),
9464 get_tv_string_buf(&argvars
[1], buf
));
9465 rettv
->v_type
= VAR_STRING
;
9472 f_eval(argvars
, rettv
)
9478 s
= get_tv_string_chk(&argvars
[0]);
9482 if (s
== NULL
|| eval1(&s
, rettv
, TRUE
) == FAIL
)
9484 rettv
->v_type
= VAR_NUMBER
;
9485 rettv
->vval
.v_number
= 0;
9488 EMSG(_(e_trailing
));
9492 * "eventhandler()" function
9495 f_eventhandler(argvars
, rettv
)
9496 typval_T
*argvars UNUSED
;
9499 rettv
->vval
.v_number
= vgetc_busy
;
9503 * "executable()" function
9506 f_executable(argvars
, rettv
)
9510 rettv
->vval
.v_number
= mch_can_exe(get_tv_string(&argvars
[0]));
9514 * "exists()" function
9517 f_exists(argvars
, rettv
)
9526 p
= get_tv_string(&argvars
[0]);
9527 if (*p
== '$') /* environment variable */
9529 /* first try "normal" environment variables (fast) */
9530 if (mch_getenv(p
+ 1) != NULL
)
9534 /* try expanding things like $VIM and ${HOME} */
9535 p
= expand_env_save(p
);
9536 if (p
!= NULL
&& *p
!= '$')
9541 else if (*p
== '&' || *p
== '+') /* option */
9543 n
= (get_option_tv(&p
, NULL
, TRUE
) == OK
);
9544 if (*skipwhite(p
) != NUL
)
9545 n
= FALSE
; /* trailing garbage */
9547 else if (*p
== '*') /* internal or user defined function */
9549 n
= function_exists(p
+ 1);
9553 n
= cmd_exists(p
+ 1);
9559 n
= autocmd_supported(p
+ 2);
9561 n
= au_exists(p
+ 1);
9564 else /* internal variable */
9569 /* get_name_len() takes care of expanding curly braces */
9571 len
= get_name_len(&p
, &tofree
, TRUE
, FALSE
);
9576 n
= (get_var_tv(name
, len
, &tv
, FALSE
) == OK
);
9579 /* handle d.key, l[idx], f(expr) */
9580 n
= (handle_subscript(&p
, &tv
, TRUE
, FALSE
) == OK
);
9591 rettv
->vval
.v_number
= n
;
9595 * "expand()" function
9598 f_expand(argvars
, rettv
)
9605 int flags
= WILD_SILENT
|WILD_USE_NL
|WILD_LIST_NOTFOUND
;
9609 rettv
->v_type
= VAR_STRING
;
9610 s
= get_tv_string(&argvars
[0]);
9611 if (*s
== '%' || *s
== '#' || *s
== '<')
9614 rettv
->vval
.v_string
= eval_vars(s
, s
, &len
, NULL
, &errormsg
, NULL
);
9619 /* When the optional second argument is non-zero, don't remove matches
9620 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
9621 if (argvars
[1].v_type
!= VAR_UNKNOWN
9622 && get_tv_number_chk(&argvars
[1], &error
))
9623 flags
|= WILD_KEEP_ALL
;
9627 xpc
.xp_context
= EXPAND_FILES
;
9628 rettv
->vval
.v_string
= ExpandOne(&xpc
, s
, NULL
, flags
, WILD_ALL
);
9631 rettv
->vval
.v_string
= NULL
;
9636 * "extend(list, list [, idx])" function
9637 * "extend(dict, dict [, action])" function
9640 f_extend(argvars
, rettv
)
9644 if (argvars
[0].v_type
== VAR_LIST
&& argvars
[1].v_type
== VAR_LIST
)
9651 l1
= argvars
[0].vval
.v_list
;
9652 l2
= argvars
[1].vval
.v_list
;
9653 if (l1
!= NULL
&& !tv_check_lock(l1
->lv_lock
, (char_u
*)"extend()")
9656 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9658 before
= get_tv_number_chk(&argvars
[2], &error
);
9660 return; /* type error; errmsg already given */
9662 if (before
== l1
->lv_len
)
9666 item
= list_find(l1
, before
);
9669 EMSGN(_(e_listidx
), before
);
9676 list_extend(l1
, l2
, item
);
9678 copy_tv(&argvars
[0], rettv
);
9681 else if (argvars
[0].v_type
== VAR_DICT
&& argvars
[1].v_type
== VAR_DICT
)
9690 d1
= argvars
[0].vval
.v_dict
;
9691 d2
= argvars
[1].vval
.v_dict
;
9692 if (d1
!= NULL
&& !tv_check_lock(d1
->dv_lock
, (char_u
*)"extend()")
9695 /* Check the third argument. */
9696 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9698 static char *(av
[]) = {"keep", "force", "error"};
9700 action
= get_tv_string_chk(&argvars
[2]);
9702 return; /* type error; errmsg already given */
9703 for (i
= 0; i
< 3; ++i
)
9704 if (STRCMP(action
, av
[i
]) == 0)
9708 EMSG2(_(e_invarg2
), action
);
9713 action
= (char_u
*)"force";
9715 /* Go over all entries in the second dict and add them to the
9717 todo
= (int)d2
->dv_hashtab
.ht_used
;
9718 for (hi2
= d2
->dv_hashtab
.ht_array
; todo
> 0; ++hi2
)
9720 if (!HASHITEM_EMPTY(hi2
))
9723 di1
= dict_find(d1
, hi2
->hi_key
, -1);
9726 di1
= dictitem_copy(HI2DI(hi2
));
9727 if (di1
!= NULL
&& dict_add(d1
, di1
) == FAIL
)
9730 else if (*action
== 'e')
9732 EMSG2(_("E737: Key already exists: %s"), hi2
->hi_key
);
9735 else if (*action
== 'f')
9737 clear_tv(&di1
->di_tv
);
9738 copy_tv(&HI2DI(hi2
)->di_tv
, &di1
->di_tv
);
9743 copy_tv(&argvars
[0], rettv
);
9747 EMSG2(_(e_listdictarg
), "extend()");
9751 * "feedkeys()" function
9754 f_feedkeys(argvars
, rettv
)
9756 typval_T
*rettv UNUSED
;
9759 char_u
*keys
, *flags
;
9760 char_u nbuf
[NUMBUFLEN
];
9764 /* This is not allowed in the sandbox. If the commands would still be
9765 * executed in the sandbox it would be OK, but it probably happens later,
9766 * when "sandbox" is no longer set. */
9770 keys
= get_tv_string(&argvars
[0]);
9773 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9775 flags
= get_tv_string_buf(&argvars
[1], nbuf
);
9776 for ( ; *flags
!= NUL
; ++flags
)
9780 case 'n': remap
= FALSE
; break;
9781 case 'm': remap
= TRUE
; break;
9782 case 't': typed
= TRUE
; break;
9787 /* Need to escape K_SPECIAL and CSI before putting the string in the
9788 * typeahead buffer. */
9789 keys_esc
= vim_strsave_escape_csi(keys
);
9790 if (keys_esc
!= NULL
)
9792 ins_typebuf(keys_esc
, (remap
? REMAP_YES
: REMAP_NONE
),
9793 typebuf
.tb_len
, !typed
, FALSE
);
9796 typebuf_was_filled
= TRUE
;
9802 * "filereadable()" function
9805 f_filereadable(argvars
, rettv
)
9814 # define O_NONBLOCK 0
9816 p
= get_tv_string(&argvars
[0]);
9817 if (*p
&& !mch_isdir(p
) && (fd
= mch_open((char *)p
,
9818 O_RDONLY
| O_NONBLOCK
, 0)) >= 0)
9826 rettv
->vval
.v_number
= n
;
9830 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
9831 * rights to write into.
9834 f_filewritable(argvars
, rettv
)
9838 rettv
->vval
.v_number
= filewritable(get_tv_string(&argvars
[0]));
9841 static void findfilendir
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int find_what
));
9844 findfilendir(argvars
, rettv
, find_what
)
9849 #ifdef FEAT_SEARCHPATH
9851 char_u
*fresult
= NULL
;
9852 char_u
*path
= *curbuf
->b_p_path
== NUL
? p_path
: curbuf
->b_p_path
;
9854 char_u pathbuf
[NUMBUFLEN
];
9860 rettv
->vval
.v_string
= NULL
;
9861 rettv
->v_type
= VAR_STRING
;
9863 #ifdef FEAT_SEARCHPATH
9864 fname
= get_tv_string(&argvars
[0]);
9866 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9868 p
= get_tv_string_buf_chk(&argvars
[1], pathbuf
);
9876 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9877 count
= get_tv_number_chk(&argvars
[2], &error
);
9881 if (count
< 0 && rettv_list_alloc(rettv
) == FAIL
)
9884 if (*fname
!= NUL
&& !error
)
9888 if (rettv
->v_type
== VAR_STRING
)
9890 fresult
= find_file_in_path_option(first
? fname
: NULL
,
9891 first
? (int)STRLEN(fname
) : 0,
9895 find_what
== FINDFILE_DIR
9896 ? (char_u
*)"" : curbuf
->b_p_sua
);
9899 if (fresult
!= NULL
&& rettv
->v_type
== VAR_LIST
)
9900 list_append_string(rettv
->vval
.v_list
, fresult
, -1);
9902 } while ((rettv
->v_type
== VAR_LIST
|| --count
> 0) && fresult
!= NULL
);
9905 if (rettv
->v_type
== VAR_STRING
)
9906 rettv
->vval
.v_string
= fresult
;
9910 static void filter_map
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int map
));
9911 static int filter_map_one
__ARGS((typval_T
*tv
, char_u
*expr
, int map
, int *remp
));
9914 * Implementation of map() and filter().
9917 filter_map(argvars
, rettv
, map
)
9922 char_u buf
[NUMBUFLEN
];
9924 listitem_T
*li
, *nli
;
9934 char_u
*ermsg
= map
? (char_u
*)"map()" : (char_u
*)"filter()";
9938 if (argvars
[0].v_type
== VAR_LIST
)
9940 if ((l
= argvars
[0].vval
.v_list
) == NULL
9941 || (map
&& tv_check_lock(l
->lv_lock
, ermsg
)))
9944 else if (argvars
[0].v_type
== VAR_DICT
)
9946 if ((d
= argvars
[0].vval
.v_dict
) == NULL
9947 || (map
&& tv_check_lock(d
->dv_lock
, ermsg
)))
9952 EMSG2(_(e_listdictarg
), ermsg
);
9956 expr
= get_tv_string_buf_chk(&argvars
[1], buf
);
9957 /* On type errors, the preceding call has already displayed an error
9958 * message. Avoid a misleading error message for an empty string that
9959 * was not passed as argument. */
9962 prepare_vimvar(VV_VAL
, &save_val
);
9963 expr
= skipwhite(expr
);
9965 /* We reset "did_emsg" to be able to detect whether an error
9966 * occurred during evaluation of the expression. */
9967 save_did_emsg
= did_emsg
;
9970 prepare_vimvar(VV_KEY
, &save_key
);
9971 if (argvars
[0].v_type
== VAR_DICT
)
9973 vimvars
[VV_KEY
].vv_type
= VAR_STRING
;
9975 ht
= &d
->dv_hashtab
;
9977 todo
= (int)ht
->ht_used
;
9978 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
9980 if (!HASHITEM_EMPTY(hi
))
9984 if (tv_check_lock(di
->di_tv
.v_lock
, ermsg
))
9986 vimvars
[VV_KEY
].vv_str
= vim_strsave(di
->di_key
);
9987 if (filter_map_one(&di
->di_tv
, expr
, map
, &rem
) == FAIL
9991 dictitem_remove(d
, di
);
9992 clear_tv(&vimvars
[VV_KEY
].vv_tv
);
9999 vimvars
[VV_KEY
].vv_type
= VAR_NUMBER
;
10001 for (li
= l
->lv_first
; li
!= NULL
; li
= nli
)
10003 if (tv_check_lock(li
->li_tv
.v_lock
, ermsg
))
10006 vimvars
[VV_KEY
].vv_nr
= index
;
10007 if (filter_map_one(&li
->li_tv
, expr
, map
, &rem
) == FAIL
10011 listitem_remove(l
, li
);
10016 restore_vimvar(VV_KEY
, &save_key
);
10017 restore_vimvar(VV_VAL
, &save_val
);
10019 did_emsg
|= save_did_emsg
;
10022 copy_tv(&argvars
[0], rettv
);
10026 filter_map_one(tv
, expr
, map
, remp
)
10036 copy_tv(tv
, &vimvars
[VV_VAL
].vv_tv
);
10038 if (eval1(&s
, &rettv
, TRUE
) == FAIL
)
10040 if (*s
!= NUL
) /* check for trailing chars after expr */
10042 EMSG2(_(e_invexpr2
), s
);
10047 /* map(): replace the list item value */
10056 /* filter(): when expr is zero remove the item */
10057 *remp
= (get_tv_number_chk(&rettv
, &error
) == 0);
10059 /* On type error, nothing has been removed; return FAIL to stop the
10060 * loop. The error message was given by get_tv_number_chk(). */
10066 clear_tv(&vimvars
[VV_VAL
].vv_tv
);
10071 * "filter()" function
10074 f_filter(argvars
, rettv
)
10078 filter_map(argvars
, rettv
, FALSE
);
10082 * "finddir({fname}[, {path}[, {count}]])" function
10085 f_finddir(argvars
, rettv
)
10089 findfilendir(argvars
, rettv
, FINDFILE_DIR
);
10093 * "findfile({fname}[, {path}[, {count}]])" function
10096 f_findfile(argvars
, rettv
)
10100 findfilendir(argvars
, rettv
, FINDFILE_FILE
);
10105 * "float2nr({float})" function
10108 f_float2nr(argvars
, rettv
)
10114 if (get_float_arg(argvars
, &f
) == OK
)
10116 if (f
< -0x7fffffff)
10117 rettv
->vval
.v_number
= -0x7fffffff;
10118 else if (f
> 0x7fffffff)
10119 rettv
->vval
.v_number
= 0x7fffffff;
10121 rettv
->vval
.v_number
= (varnumber_T
)f
;
10126 * "floor({float})" function
10129 f_floor(argvars
, rettv
)
10135 rettv
->v_type
= VAR_FLOAT
;
10136 if (get_float_arg(argvars
, &f
) == OK
)
10137 rettv
->vval
.v_float
= floor(f
);
10139 rettv
->vval
.v_float
= 0.0;
10144 * "fnameescape({string})" function
10147 f_fnameescape(argvars
, rettv
)
10151 rettv
->vval
.v_string
= vim_strsave_fnameescape(
10152 get_tv_string(&argvars
[0]), FALSE
);
10153 rettv
->v_type
= VAR_STRING
;
10157 * "fnamemodify({fname}, {mods})" function
10160 f_fnamemodify(argvars
, rettv
)
10168 char_u
*fbuf
= NULL
;
10169 char_u buf
[NUMBUFLEN
];
10171 fname
= get_tv_string_chk(&argvars
[0]);
10172 mods
= get_tv_string_buf_chk(&argvars
[1], buf
);
10173 if (fname
== NULL
|| mods
== NULL
)
10177 len
= (int)STRLEN(fname
);
10178 (void)modify_fname(mods
, &usedlen
, &fname
, &fbuf
, &len
);
10181 rettv
->v_type
= VAR_STRING
;
10183 rettv
->vval
.v_string
= NULL
;
10185 rettv
->vval
.v_string
= vim_strnsave(fname
, len
);
10189 static void foldclosed_both
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int end
));
10192 * "foldclosed()" function
10195 foldclosed_both(argvars
, rettv
, end
)
10200 #ifdef FEAT_FOLDING
10202 linenr_T first
, last
;
10204 lnum
= get_tv_lnum(argvars
);
10205 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
10207 if (hasFoldingWin(curwin
, lnum
, &first
, &last
, FALSE
, NULL
))
10210 rettv
->vval
.v_number
= (varnumber_T
)last
;
10212 rettv
->vval
.v_number
= (varnumber_T
)first
;
10217 rettv
->vval
.v_number
= -1;
10221 * "foldclosed()" function
10224 f_foldclosed(argvars
, rettv
)
10228 foldclosed_both(argvars
, rettv
, FALSE
);
10232 * "foldclosedend()" function
10235 f_foldclosedend(argvars
, rettv
)
10239 foldclosed_both(argvars
, rettv
, TRUE
);
10243 * "foldlevel()" function
10246 f_foldlevel(argvars
, rettv
)
10250 #ifdef FEAT_FOLDING
10253 lnum
= get_tv_lnum(argvars
);
10254 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
10255 rettv
->vval
.v_number
= foldLevel(lnum
);
10260 * "foldtext()" function
10263 f_foldtext(argvars
, rettv
)
10264 typval_T
*argvars UNUSED
;
10267 #ifdef FEAT_FOLDING
10275 rettv
->v_type
= VAR_STRING
;
10276 rettv
->vval
.v_string
= NULL
;
10277 #ifdef FEAT_FOLDING
10278 if ((linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
> 0
10279 && (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
10280 <= curbuf
->b_ml
.ml_line_count
10281 && vimvars
[VV_FOLDDASHES
].vv_str
!= NULL
)
10283 /* Find first non-empty line in the fold. */
10284 lnum
= (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
;
10285 while (lnum
< (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
10287 if (!linewhite(lnum
))
10292 /* Find interesting text in this line. */
10293 s
= skipwhite(ml_get(lnum
));
10294 /* skip C comment-start */
10295 if (s
[0] == '/' && (s
[1] == '*' || s
[1] == '/'))
10297 s
= skipwhite(s
+ 2);
10298 if (*skipwhite(s
) == NUL
10299 && lnum
+ 1 < (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
10301 s
= skipwhite(ml_get(lnum
+ 1));
10303 s
= skipwhite(s
+ 1);
10306 txt
= _("+-%s%3ld lines: ");
10307 r
= alloc((unsigned)(STRLEN(txt
)
10308 + STRLEN(vimvars
[VV_FOLDDASHES
].vv_str
) /* for %s */
10309 + 20 /* for %3ld */
10310 + STRLEN(s
))); /* concatenated */
10313 sprintf((char *)r
, txt
, vimvars
[VV_FOLDDASHES
].vv_str
,
10314 (long)((linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
10315 - (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
+ 1));
10316 len
= (int)STRLEN(r
);
10318 /* remove 'foldmarker' and 'commentstring' */
10319 foldtext_cleanup(r
+ len
);
10320 rettv
->vval
.v_string
= r
;
10327 * "foldtextresult(lnum)" function
10330 f_foldtextresult(argvars
, rettv
)
10331 typval_T
*argvars UNUSED
;
10334 #ifdef FEAT_FOLDING
10338 foldinfo_T foldinfo
;
10342 rettv
->v_type
= VAR_STRING
;
10343 rettv
->vval
.v_string
= NULL
;
10344 #ifdef FEAT_FOLDING
10345 lnum
= get_tv_lnum(argvars
);
10346 /* treat illegal types and illegal string values for {lnum} the same */
10349 fold_count
= foldedCount(curwin
, lnum
, &foldinfo
);
10350 if (fold_count
> 0)
10352 text
= get_foldtext(curwin
, lnum
, lnum
+ fold_count
- 1,
10355 text
= vim_strsave(text
);
10356 rettv
->vval
.v_string
= text
;
10362 * "foreground()" function
10365 f_foreground(argvars
, rettv
)
10366 typval_T
*argvars UNUSED
;
10367 typval_T
*rettv UNUSED
;
10371 gui_mch_set_foreground();
10374 win32_set_foreground();
10380 * "function()" function
10383 f_function(argvars
, rettv
)
10389 s
= get_tv_string(&argvars
[0]);
10390 if (s
== NULL
|| *s
== NUL
|| VIM_ISDIGIT(*s
))
10391 EMSG2(_(e_invarg2
), s
);
10392 /* Don't check an autoload name for existence here. */
10393 else if (vim_strchr(s
, AUTOLOAD_CHAR
) == NULL
&& !function_exists(s
))
10394 EMSG2(_("E700: Unknown function: %s"), s
);
10397 rettv
->vval
.v_string
= vim_strsave(s
);
10398 rettv
->v_type
= VAR_FUNC
;
10403 * "garbagecollect()" function
10406 f_garbagecollect(argvars
, rettv
)
10408 typval_T
*rettv UNUSED
;
10410 /* This is postponed until we are back at the toplevel, because we may be
10411 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
10412 want_garbage_collect
= TRUE
;
10414 if (argvars
[0].v_type
!= VAR_UNKNOWN
&& get_tv_number(&argvars
[0]) == 1)
10415 garbage_collect_at_exit
= TRUE
;
10422 f_get(argvars
, rettv
)
10430 typval_T
*tv
= NULL
;
10432 if (argvars
[0].v_type
== VAR_LIST
)
10434 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
10438 li
= list_find(l
, get_tv_number_chk(&argvars
[1], &error
));
10439 if (!error
&& li
!= NULL
)
10443 else if (argvars
[0].v_type
== VAR_DICT
)
10445 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
10447 di
= dict_find(d
, get_tv_string(&argvars
[1]), -1);
10453 EMSG2(_(e_listdictarg
), "get()");
10457 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
10458 copy_tv(&argvars
[2], rettv
);
10461 copy_tv(tv
, rettv
);
10464 static void get_buffer_lines
__ARGS((buf_T
*buf
, linenr_T start
, linenr_T end
, int retlist
, typval_T
*rettv
));
10467 * Get line or list of lines from buffer "buf" into "rettv".
10468 * Return a range (from start to end) of lines in rettv from the specified
10470 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
10473 get_buffer_lines(buf
, start
, end
, retlist
, rettv
)
10482 if (retlist
&& rettv_list_alloc(rettv
) == FAIL
)
10485 if (buf
== NULL
|| buf
->b_ml
.ml_mfp
== NULL
|| start
< 0)
10490 if (start
>= 1 && start
<= buf
->b_ml
.ml_line_count
)
10491 p
= ml_get_buf(buf
, start
, FALSE
);
10495 rettv
->v_type
= VAR_STRING
;
10496 rettv
->vval
.v_string
= vim_strsave(p
);
10505 if (end
> buf
->b_ml
.ml_line_count
)
10506 end
= buf
->b_ml
.ml_line_count
;
10507 while (start
<= end
)
10508 if (list_append_string(rettv
->vval
.v_list
,
10509 ml_get_buf(buf
, start
++, FALSE
), -1) == FAIL
)
10515 * "getbufline()" function
10518 f_getbufline(argvars
, rettv
)
10526 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
10528 buf
= get_buf_tv(&argvars
[0]);
10531 lnum
= get_tv_lnum_buf(&argvars
[1], buf
);
10532 if (argvars
[2].v_type
== VAR_UNKNOWN
)
10535 end
= get_tv_lnum_buf(&argvars
[2], buf
);
10537 get_buffer_lines(buf
, lnum
, end
, TRUE
, rettv
);
10541 * "getbufvar()" function
10544 f_getbufvar(argvars
, rettv
)
10549 buf_T
*save_curbuf
;
10553 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
10554 varname
= get_tv_string_chk(&argvars
[1]);
10556 buf
= get_buf_tv(&argvars
[0]);
10558 rettv
->v_type
= VAR_STRING
;
10559 rettv
->vval
.v_string
= NULL
;
10561 if (buf
!= NULL
&& varname
!= NULL
)
10563 /* set curbuf to be our buf, temporarily */
10564 save_curbuf
= curbuf
;
10567 if (*varname
== '&') /* buffer-local-option */
10568 get_option_tv(&varname
, rettv
, TRUE
);
10571 if (*varname
== NUL
)
10572 /* let getbufvar({nr}, "") return the "b:" dictionary. The
10573 * scope prefix before the NUL byte is required by
10574 * find_var_in_ht(). */
10575 varname
= (char_u
*)"b:" + 2;
10576 /* look up the variable */
10577 v
= find_var_in_ht(&curbuf
->b_vars
.dv_hashtab
, varname
, FALSE
);
10579 copy_tv(&v
->di_tv
, rettv
);
10582 /* restore previous notion of curbuf */
10583 curbuf
= save_curbuf
;
10590 * "getchar()" function
10593 f_getchar(argvars
, rettv
)
10600 /* Position the cursor. Needed after a message that ends in a space. */
10601 windgoto(msg_row
, msg_col
);
10607 if (argvars
[0].v_type
== VAR_UNKNOWN
)
10608 /* getchar(): blocking wait. */
10610 else if (get_tv_number_chk(&argvars
[0], &error
) == 1)
10611 /* getchar(1): only check if char avail */
10613 else if (error
|| vpeekc() == NUL
)
10614 /* illegal argument or getchar(0) and no char avail: return zero */
10617 /* getchar(0) and char avail: return char */
10626 vimvars
[VV_MOUSE_WIN
].vv_nr
= 0;
10627 vimvars
[VV_MOUSE_LNUM
].vv_nr
= 0;
10628 vimvars
[VV_MOUSE_COL
].vv_nr
= 0;
10630 rettv
->vval
.v_number
= n
;
10631 if (IS_SPECIAL(n
) || mod_mask
!= 0)
10633 char_u temp
[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
10636 /* Turn a special key into three bytes, plus modifier. */
10639 temp
[i
++] = K_SPECIAL
;
10640 temp
[i
++] = KS_MODIFIER
;
10641 temp
[i
++] = mod_mask
;
10645 temp
[i
++] = K_SPECIAL
;
10646 temp
[i
++] = K_SECOND(n
);
10647 temp
[i
++] = K_THIRD(n
);
10650 else if (has_mbyte
)
10651 i
+= (*mb_char2bytes
)(n
, temp
+ i
);
10656 rettv
->v_type
= VAR_STRING
;
10657 rettv
->vval
.v_string
= vim_strsave(temp
);
10660 if (n
== K_LEFTMOUSE
10661 || n
== K_LEFTMOUSE_NM
10663 || n
== K_LEFTRELEASE
10664 || n
== K_LEFTRELEASE_NM
10665 || n
== K_MIDDLEMOUSE
10666 || n
== K_MIDDLEDRAG
10667 || n
== K_MIDDLERELEASE
10668 || n
== K_RIGHTMOUSE
10669 || n
== K_RIGHTDRAG
10670 || n
== K_RIGHTRELEASE
10673 || n
== K_X1RELEASE
10676 || n
== K_X2RELEASE
10677 || n
== K_MOUSEDOWN
10680 int row
= mouse_row
;
10681 int col
= mouse_col
;
10684 # ifdef FEAT_WINDOWS
10689 if (row
>= 0 && col
>= 0)
10691 /* Find the window at the mouse coordinates and compute the
10692 * text position. */
10693 win
= mouse_find_win(&row
, &col
);
10694 (void)mouse_comp_pos(win
, &row
, &col
, &lnum
);
10695 # ifdef FEAT_WINDOWS
10696 for (wp
= firstwin
; wp
!= win
; wp
= wp
->w_next
)
10699 vimvars
[VV_MOUSE_WIN
].vv_nr
= winnr
;
10700 vimvars
[VV_MOUSE_LNUM
].vv_nr
= lnum
;
10701 vimvars
[VV_MOUSE_COL
].vv_nr
= col
+ 1;
10709 * "getcharmod()" function
10712 f_getcharmod(argvars
, rettv
)
10713 typval_T
*argvars UNUSED
;
10716 rettv
->vval
.v_number
= mod_mask
;
10720 * "getcmdline()" function
10723 f_getcmdline(argvars
, rettv
)
10724 typval_T
*argvars UNUSED
;
10727 rettv
->v_type
= VAR_STRING
;
10728 rettv
->vval
.v_string
= get_cmdline_str();
10732 * "getcmdpos()" function
10735 f_getcmdpos(argvars
, rettv
)
10736 typval_T
*argvars UNUSED
;
10739 rettv
->vval
.v_number
= get_cmdline_pos() + 1;
10743 * "getcmdtype()" function
10746 f_getcmdtype(argvars
, rettv
)
10747 typval_T
*argvars UNUSED
;
10750 rettv
->v_type
= VAR_STRING
;
10751 rettv
->vval
.v_string
= alloc(2);
10752 if (rettv
->vval
.v_string
!= NULL
)
10754 rettv
->vval
.v_string
[0] = get_cmdline_type();
10755 rettv
->vval
.v_string
[1] = NUL
;
10760 * "getcwd()" function
10763 f_getcwd(argvars
, rettv
)
10764 typval_T
*argvars UNUSED
;
10767 char_u cwd
[MAXPATHL
];
10769 rettv
->v_type
= VAR_STRING
;
10770 if (mch_dirname(cwd
, MAXPATHL
) == FAIL
)
10771 rettv
->vval
.v_string
= NULL
;
10774 rettv
->vval
.v_string
= vim_strsave(cwd
);
10775 #ifdef BACKSLASH_IN_FILENAME
10776 if (rettv
->vval
.v_string
!= NULL
)
10777 slash_adjust(rettv
->vval
.v_string
);
10783 * "getfontname()" function
10786 f_getfontname(argvars
, rettv
)
10787 typval_T
*argvars UNUSED
;
10790 rettv
->v_type
= VAR_STRING
;
10791 rettv
->vval
.v_string
= NULL
;
10796 char_u
*name
= NULL
;
10798 if (argvars
[0].v_type
== VAR_UNKNOWN
)
10800 /* Get the "Normal" font. Either the name saved by
10801 * hl_set_font_name() or from the font ID. */
10802 font
= gui
.norm_font
;
10803 name
= hl_get_font_name();
10807 name
= get_tv_string(&argvars
[0]);
10808 if (STRCMP(name
, "*") == 0) /* don't use font dialog */
10810 font
= gui_mch_get_font(name
, FALSE
);
10811 if (font
== NOFONT
)
10812 return; /* Invalid font name, return empty string. */
10814 rettv
->vval
.v_string
= gui_mch_get_fontname(font
, name
);
10815 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
10816 gui_mch_free_font(font
);
10822 * "getfperm({fname})" function
10825 f_getfperm(argvars
, rettv
)
10831 char_u
*perm
= NULL
;
10832 char_u flags
[] = "rwx";
10835 fname
= get_tv_string(&argvars
[0]);
10837 rettv
->v_type
= VAR_STRING
;
10838 if (mch_stat((char *)fname
, &st
) >= 0)
10840 perm
= vim_strsave((char_u
*)"---------");
10843 for (i
= 0; i
< 9; i
++)
10845 if (st
.st_mode
& (1 << (8 - i
)))
10846 perm
[i
] = flags
[i
% 3];
10850 rettv
->vval
.v_string
= perm
;
10854 * "getfsize({fname})" function
10857 f_getfsize(argvars
, rettv
)
10864 fname
= get_tv_string(&argvars
[0]);
10866 rettv
->v_type
= VAR_NUMBER
;
10868 if (mch_stat((char *)fname
, &st
) >= 0)
10870 if (mch_isdir(fname
))
10871 rettv
->vval
.v_number
= 0;
10874 rettv
->vval
.v_number
= (varnumber_T
)st
.st_size
;
10876 /* non-perfect check for overflow */
10877 if ((off_t
)rettv
->vval
.v_number
!= (off_t
)st
.st_size
)
10878 rettv
->vval
.v_number
= -2;
10882 rettv
->vval
.v_number
= -1;
10886 * "getftime({fname})" function
10889 f_getftime(argvars
, rettv
)
10896 fname
= get_tv_string(&argvars
[0]);
10898 if (mch_stat((char *)fname
, &st
) >= 0)
10899 rettv
->vval
.v_number
= (varnumber_T
)st
.st_mtime
;
10901 rettv
->vval
.v_number
= -1;
10905 * "getftype({fname})" function
10908 f_getftype(argvars
, rettv
)
10914 char_u
*type
= NULL
;
10917 fname
= get_tv_string(&argvars
[0]);
10919 rettv
->v_type
= VAR_STRING
;
10920 if (mch_lstat((char *)fname
, &st
) >= 0)
10923 if (S_ISREG(st
.st_mode
))
10925 else if (S_ISDIR(st
.st_mode
))
10928 else if (S_ISLNK(st
.st_mode
))
10932 else if (S_ISBLK(st
.st_mode
))
10936 else if (S_ISCHR(st
.st_mode
))
10940 else if (S_ISFIFO(st
.st_mode
))
10944 else if (S_ISSOCK(st
.st_mode
))
10951 switch (st
.st_mode
& S_IFMT
)
10953 case S_IFREG
: t
= "file"; break;
10954 case S_IFDIR
: t
= "dir"; break;
10956 case S_IFLNK
: t
= "link"; break;
10959 case S_IFBLK
: t
= "bdev"; break;
10962 case S_IFCHR
: t
= "cdev"; break;
10965 case S_IFIFO
: t
= "fifo"; break;
10968 case S_IFSOCK
: t
= "socket"; break;
10970 default: t
= "other";
10973 if (mch_isdir(fname
))
10979 type
= vim_strsave((char_u
*)t
);
10981 rettv
->vval
.v_string
= type
;
10985 * "getline(lnum, [end])" function
10988 f_getline(argvars
, rettv
)
10996 lnum
= get_tv_lnum(argvars
);
10997 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11004 end
= get_tv_lnum(&argvars
[1]);
11008 get_buffer_lines(curbuf
, lnum
, end
, retlist
, rettv
);
11012 * "getmatches()" function
11015 f_getmatches(argvars
, rettv
)
11016 typval_T
*argvars UNUSED
;
11019 #ifdef FEAT_SEARCH_EXTRA
11021 matchitem_T
*cur
= curwin
->w_match_head
;
11023 if (rettv_list_alloc(rettv
) == OK
)
11025 while (cur
!= NULL
)
11027 dict
= dict_alloc();
11030 dict_add_nr_str(dict
, "group", 0L, syn_id2name(cur
->hlg_id
));
11031 dict_add_nr_str(dict
, "pattern", 0L, cur
->pattern
);
11032 dict_add_nr_str(dict
, "priority", (long)cur
->priority
, NULL
);
11033 dict_add_nr_str(dict
, "id", (long)cur
->id
, NULL
);
11034 list_append_dict(rettv
->vval
.v_list
, dict
);
11042 * "getpid()" function
11045 f_getpid(argvars
, rettv
)
11046 typval_T
*argvars UNUSED
;
11049 rettv
->vval
.v_number
= mch_get_pid();
11053 * "getpos(string)" function
11056 f_getpos(argvars
, rettv
)
11064 if (rettv_list_alloc(rettv
) == OK
)
11066 l
= rettv
->vval
.v_list
;
11067 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
11069 list_append_number(l
, (varnumber_T
)fnum
);
11071 list_append_number(l
, (varnumber_T
)0);
11072 list_append_number(l
, (fp
!= NULL
) ? (varnumber_T
)fp
->lnum
11074 list_append_number(l
, (fp
!= NULL
)
11075 ? (varnumber_T
)(fp
->col
== MAXCOL
? MAXCOL
: fp
->col
+ 1)
11077 list_append_number(l
,
11078 #ifdef FEAT_VIRTUALEDIT
11079 (fp
!= NULL
) ? (varnumber_T
)fp
->coladd
:
11084 rettv
->vval
.v_number
= FALSE
;
11088 * "getqflist()" and "getloclist()" functions
11091 f_getqflist(argvars
, rettv
)
11092 typval_T
*argvars UNUSED
;
11093 typval_T
*rettv UNUSED
;
11095 #ifdef FEAT_QUICKFIX
11099 #ifdef FEAT_QUICKFIX
11100 if (rettv_list_alloc(rettv
) == OK
)
11103 if (argvars
[0].v_type
!= VAR_UNKNOWN
) /* getloclist() */
11105 wp
= find_win_by_nr(&argvars
[0], NULL
);
11110 (void)get_errorlist(wp
, rettv
->vval
.v_list
);
11116 * "getreg()" function
11119 f_getreg(argvars
, rettv
)
11123 char_u
*strregname
;
11128 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
11130 strregname
= get_tv_string_chk(&argvars
[0]);
11131 error
= strregname
== NULL
;
11132 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
11133 arg2
= get_tv_number_chk(&argvars
[1], &error
);
11136 strregname
= vimvars
[VV_REG
].vv_str
;
11137 regname
= (strregname
== NULL
? '"' : *strregname
);
11141 rettv
->v_type
= VAR_STRING
;
11142 rettv
->vval
.v_string
= error
? NULL
:
11143 get_reg_contents(regname
, TRUE
, arg2
);
11147 * "getregtype()" function
11150 f_getregtype(argvars
, rettv
)
11154 char_u
*strregname
;
11156 char_u buf
[NUMBUFLEN
+ 2];
11159 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
11161 strregname
= get_tv_string_chk(&argvars
[0]);
11162 if (strregname
== NULL
) /* type error; errmsg already given */
11164 rettv
->v_type
= VAR_STRING
;
11165 rettv
->vval
.v_string
= NULL
;
11170 /* Default to v:register */
11171 strregname
= vimvars
[VV_REG
].vv_str
;
11173 regname
= (strregname
== NULL
? '"' : *strregname
);
11179 switch (get_reg_type(regname
, ®len
))
11181 case MLINE
: buf
[0] = 'V'; break;
11182 case MCHAR
: buf
[0] = 'v'; break;
11186 sprintf((char *)buf
+ 1, "%ld", reglen
+ 1);
11190 rettv
->v_type
= VAR_STRING
;
11191 rettv
->vval
.v_string
= vim_strsave(buf
);
11195 * "gettabwinvar()" function
11198 f_gettabwinvar(argvars
, rettv
)
11202 getwinvar(argvars
, rettv
, 1);
11206 * "getwinposx()" function
11209 f_getwinposx(argvars
, rettv
)
11210 typval_T
*argvars UNUSED
;
11213 rettv
->vval
.v_number
= -1;
11219 if (gui_mch_get_winpos(&x
, &y
) == OK
)
11220 rettv
->vval
.v_number
= x
;
11226 * "getwinposy()" function
11229 f_getwinposy(argvars
, rettv
)
11230 typval_T
*argvars UNUSED
;
11233 rettv
->vval
.v_number
= -1;
11239 if (gui_mch_get_winpos(&x
, &y
) == OK
)
11240 rettv
->vval
.v_number
= y
;
11246 * Find window specified by "vp" in tabpage "tp".
11249 find_win_by_nr(vp
, tp
)
11251 tabpage_T
*tp
; /* NULL for current tab page */
11253 #ifdef FEAT_WINDOWS
11258 nr
= get_tv_number_chk(vp
, NULL
);
11260 #ifdef FEAT_WINDOWS
11266 for (wp
= (tp
== NULL
|| tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
11267 wp
!= NULL
; wp
= wp
->w_next
)
11272 if (nr
== 0 || nr
== 1)
11279 * "getwinvar()" function
11282 f_getwinvar(argvars
, rettv
)
11286 getwinvar(argvars
, rettv
, 0);
11290 * getwinvar() and gettabwinvar()
11293 getwinvar(argvars
, rettv
, off
)
11296 int off
; /* 1 for gettabwinvar() */
11298 win_T
*win
, *oldcurwin
;
11303 #ifdef FEAT_WINDOWS
11305 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
11309 win
= find_win_by_nr(&argvars
[off
], tp
);
11310 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
11313 rettv
->v_type
= VAR_STRING
;
11314 rettv
->vval
.v_string
= NULL
;
11316 if (win
!= NULL
&& varname
!= NULL
)
11318 /* Set curwin to be our win, temporarily. Also set curbuf, so
11319 * that we can get buffer-local options. */
11320 oldcurwin
= curwin
;
11322 curbuf
= win
->w_buffer
;
11324 if (*varname
== '&') /* window-local-option */
11325 get_option_tv(&varname
, rettv
, 1);
11328 if (*varname
== NUL
)
11329 /* let getwinvar({nr}, "") return the "w:" dictionary. The
11330 * scope prefix before the NUL byte is required by
11331 * find_var_in_ht(). */
11332 varname
= (char_u
*)"w:" + 2;
11333 /* look up the variable */
11334 v
= find_var_in_ht(&win
->w_vars
.dv_hashtab
, varname
, FALSE
);
11336 copy_tv(&v
->di_tv
, rettv
);
11339 /* restore previous notion of curwin */
11340 curwin
= oldcurwin
;
11341 curbuf
= curwin
->w_buffer
;
11348 * "glob()" function
11351 f_glob(argvars
, rettv
)
11355 int flags
= WILD_SILENT
|WILD_USE_NL
;
11359 /* When the optional second argument is non-zero, don't remove matches
11360 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11361 if (argvars
[1].v_type
!= VAR_UNKNOWN
11362 && get_tv_number_chk(&argvars
[1], &error
))
11363 flags
|= WILD_KEEP_ALL
;
11364 rettv
->v_type
= VAR_STRING
;
11368 xpc
.xp_context
= EXPAND_FILES
;
11369 rettv
->vval
.v_string
= ExpandOne(&xpc
, get_tv_string(&argvars
[0]),
11370 NULL
, flags
, WILD_ALL
);
11373 rettv
->vval
.v_string
= NULL
;
11377 * "globpath()" function
11380 f_globpath(argvars
, rettv
)
11385 char_u buf1
[NUMBUFLEN
];
11386 char_u
*file
= get_tv_string_buf_chk(&argvars
[1], buf1
);
11389 /* When the optional second argument is non-zero, don't remove matches
11390 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
11391 if (argvars
[2].v_type
!= VAR_UNKNOWN
11392 && get_tv_number_chk(&argvars
[2], &error
))
11393 flags
|= WILD_KEEP_ALL
;
11394 rettv
->v_type
= VAR_STRING
;
11395 if (file
== NULL
|| error
)
11396 rettv
->vval
.v_string
= NULL
;
11398 rettv
->vval
.v_string
= globpath(get_tv_string(&argvars
[0]), file
,
11406 f_has(argvars
, rettv
)
11413 static char *(has_list
[]) =
11434 #if defined(MACOS_X_UNIX)
11458 #if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
11461 #if defined(WIN64) || defined(_WIN64)
11467 #ifndef CASE_INSENSITIVE_FILENAME
11473 #ifdef FEAT_AUTOCMD
11478 # ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
11479 "balloon_multiline",
11482 #if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
11484 # ifdef ALL_BUILTIN_TCAPS
11485 "all_builtin_terms",
11488 #ifdef FEAT_BYTEOFF
11491 #ifdef FEAT_CINDENT
11494 #ifdef FEAT_CLIENTSERVER
11497 #ifdef FEAT_CLIPBOARD
11500 #ifdef FEAT_CMDL_COMPL
11503 #ifdef FEAT_CMDHIST
11506 #ifdef FEAT_COMMENTS
11515 #ifdef CURSOR_SHAPE
11521 #ifdef FEAT_CON_DIALOG
11524 #ifdef FEAT_GUI_DIALOG
11530 #ifdef FEAT_DIGRAPHS
11536 #ifdef FEAT_EMACS_TAGS
11539 "eval", /* always present, of course! */
11540 #ifdef FEAT_EX_EXTRA
11543 #ifdef FEAT_SEARCH_EXTRA
11549 #ifdef FEAT_SEARCHPATH
11552 #if defined(UNIX) && !defined(USE_SYSTEM)
11555 #ifdef FEAT_FIND_ID
11561 #ifdef FEAT_FOLDING
11567 #if !defined(USE_SYSTEM) && defined(UNIX)
11570 #ifdef FEAT_GETTEXT
11577 #ifdef FEAT_GUI_ATHENA
11578 # ifdef FEAT_GUI_NEXTAW
11584 #ifdef FEAT_GUI_GTK
11590 #ifdef FEAT_GUI_GNOME
11593 #ifdef FEAT_GUI_MAC
11596 #ifdef FEAT_GUI_MOTIF
11599 #ifdef FEAT_GUI_PHOTON
11602 #ifdef FEAT_GUI_W16
11605 #ifdef FEAT_GUI_W32
11608 #ifdef FEAT_HANGULIN
11611 #if defined(HAVE_ICONV_H) && defined(USE_ICONV)
11614 #ifdef FEAT_INS_EXPAND
11617 #ifdef FEAT_JUMPLIST
11624 #ifdef FEAT_LANGMAP
11627 #ifdef FEAT_LIBCALL
11630 #ifdef FEAT_LINEBREAK
11636 #ifdef FEAT_LISTCMDS
11639 #ifdef FEAT_LOCALMAP
11646 # ifndef DYNAMIC_MIGEMO
11650 #ifdef FEAT_SESSION
11653 #ifdef FEAT_MODIFY_FNAME
11659 #ifdef FEAT_MOUSESHAPE
11662 #if defined(UNIX) || defined(VMS)
11663 # ifdef FEAT_MOUSE_DEC
11666 # ifdef FEAT_MOUSE_GPM
11669 # ifdef FEAT_MOUSE_JSB
11672 # ifdef FEAT_MOUSE_NET
11675 # ifdef FEAT_MOUSE_PTERM
11678 # ifdef FEAT_SYSMOUSE
11681 # ifdef FEAT_MOUSE_XTERM
11688 #ifdef FEAT_MBYTE_IME
11691 #ifdef FEAT_MULTI_LANG
11694 #ifdef FEAT_MZSCHEME
11695 #ifndef DYNAMIC_MZSCHEME
11702 #ifdef FEAT_OSFILETYPE
11705 #ifdef FEAT_PATH_EXTRA
11709 #ifndef DYNAMIC_PERL
11714 #ifndef DYNAMIC_PYTHON
11718 #ifdef FEAT_POSTSCRIPT
11721 #ifdef FEAT_PRINTER
11724 #ifdef FEAT_PROFILE
11727 #ifdef FEAT_RELTIME
11730 #ifdef FEAT_QUICKFIX
11733 #ifdef FEAT_RIGHTLEFT
11736 #if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11739 #ifdef FEAT_SCROLLBIND
11742 #ifdef FEAT_CMDL_INFO
11749 #ifdef FEAT_SMARTINDENT
11758 #ifdef FEAT_STL_OPT
11761 #ifdef FEAT_SUN_WORKSHOP
11764 #ifdef FEAT_NETBEANS_INTG
11773 #if defined(USE_SYSTEM) || !defined(UNIX)
11776 #ifdef FEAT_TAG_BINS
11779 #ifdef FEAT_TAG_OLDSTATIC
11782 #ifdef FEAT_TAG_ANYWHITE
11786 # ifndef DYNAMIC_TCL
11793 #ifdef FEAT_TERMRESPONSE
11796 #ifdef FEAT_TEXTOBJ
11799 #ifdef HAVE_TGETENT
11805 #ifdef FEAT_TOOLBAR
11808 #ifdef FEAT_USR_CMDS
11809 "user-commands", /* was accidentally included in 5.4 */
11812 #ifdef FEAT_VIMINFO
11815 #ifdef FEAT_VERTSPLIT
11818 #ifdef FEAT_VIRTUALEDIT
11824 #ifdef FEAT_VISUALEXTRA
11827 #ifdef FEAT_VREPLACE
11830 #ifdef FEAT_WILDIGN
11833 #ifdef FEAT_WILDMENU
11836 #ifdef FEAT_WINDOWS
11842 #ifdef FEAT_WRITEBACKUP
11848 #ifdef FEAT_XFONTSET
11854 #ifdef USE_XSMP_INTERACT
11857 #ifdef FEAT_XCLIPBOARD
11860 #ifdef FEAT_XTERM_SAVE
11863 #if defined(UNIX) && defined(FEAT_X11)
11869 name
= get_tv_string(&argvars
[0]);
11870 for (i
= 0; has_list
[i
] != NULL
; ++i
)
11871 if (STRICMP(name
, has_list
[i
]) == 0)
11879 if (STRNICMP(name
, "patch", 5) == 0)
11880 n
= has_patch(atoi((char *)name
+ 5));
11881 else if (STRICMP(name
, "vim_starting") == 0)
11882 n
= (starting
!= 0);
11884 else if (STRICMP(name
, "multi_byte_encoding") == 0)
11887 #if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11888 else if (STRICMP(name
, "balloon_multiline") == 0)
11889 n
= multiline_balloon_available();
11892 else if (STRICMP(name
, "tcl") == 0)
11893 n
= tcl_enabled(FALSE
);
11895 #if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11896 else if (STRICMP(name
, "iconv") == 0)
11897 n
= iconv_enabled(FALSE
);
11899 #ifdef DYNAMIC_MZSCHEME
11900 else if (STRICMP(name
, "mzscheme") == 0)
11901 n
= mzscheme_enabled(FALSE
);
11903 #ifdef DYNAMIC_RUBY
11904 else if (STRICMP(name
, "ruby") == 0)
11905 n
= ruby_enabled(FALSE
);
11907 #ifdef DYNAMIC_PYTHON
11908 else if (STRICMP(name
, "python") == 0)
11909 n
= python_enabled(FALSE
);
11911 #ifdef DYNAMIC_PERL
11912 else if (STRICMP(name
, "perl") == 0)
11913 n
= perl_enabled(FALSE
);
11916 else if (STRICMP(name
, "gui_running") == 0)
11917 n
= (gui
.in_use
|| gui
.starting
);
11918 # ifdef FEAT_GUI_W32
11919 else if (STRICMP(name
, "gui_win32s") == 0)
11920 n
= gui_is_win32s();
11922 # ifdef FEAT_BROWSE
11923 else if (STRICMP(name
, "browse") == 0)
11924 n
= gui
.in_use
; /* gui_mch_browse() works when GUI is running */
11928 else if (STRICMP(name
, "syntax_items") == 0)
11929 n
= syntax_present(curbuf
);
11931 #if defined(WIN3264)
11932 else if (STRICMP(name
, "win95") == 0)
11933 n
= mch_windows95();
11935 #if defined(USE_MIGEMO)
11936 else if (STRICMP(name
, "migemo") == 0)
11937 n
= migemo_enabled() ? TRUE
: FALSE
;
11939 #ifdef FEAT_NETBEANS_INTG
11940 else if (STRICMP(name
, "netbeans_enabled") == 0)
11945 rettv
->vval
.v_number
= n
;
11949 * "has_key()" function
11952 f_has_key(argvars
, rettv
)
11956 if (argvars
[0].v_type
!= VAR_DICT
)
11958 EMSG(_(e_dictreq
));
11961 if (argvars
[0].vval
.v_dict
== NULL
)
11964 rettv
->vval
.v_number
= dict_find(argvars
[0].vval
.v_dict
,
11965 get_tv_string(&argvars
[1]), -1) != NULL
;
11969 * "haslocaldir()" function
11972 f_haslocaldir(argvars
, rettv
)
11973 typval_T
*argvars UNUSED
;
11976 rettv
->vval
.v_number
= (curwin
->w_localdir
!= NULL
);
11980 * "hasmapto()" function
11983 f_hasmapto(argvars
, rettv
)
11989 char_u buf
[NUMBUFLEN
];
11992 name
= get_tv_string(&argvars
[0]);
11993 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11994 mode
= (char_u
*)"nvo";
11997 mode
= get_tv_string_buf(&argvars
[1], buf
);
11998 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
11999 abbr
= get_tv_number(&argvars
[2]);
12002 if (map_to_exists(name
, mode
, abbr
))
12003 rettv
->vval
.v_number
= TRUE
;
12005 rettv
->vval
.v_number
= FALSE
;
12009 * "histadd()" function
12012 f_histadd(argvars
, rettv
)
12013 typval_T
*argvars UNUSED
;
12016 #ifdef FEAT_CMDHIST
12019 char_u buf
[NUMBUFLEN
];
12022 rettv
->vval
.v_number
= FALSE
;
12023 if (check_restricted() || check_secure())
12025 #ifdef FEAT_CMDHIST
12026 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
12027 histype
= str
!= NULL
? get_histtype(str
) : -1;
12030 str
= get_tv_string_buf(&argvars
[1], buf
);
12034 add_to_history(histype
, str
, FALSE
, NUL
);
12035 rettv
->vval
.v_number
= TRUE
;
12043 * "histdel()" function
12046 f_histdel(argvars
, rettv
)
12047 typval_T
*argvars UNUSED
;
12048 typval_T
*rettv UNUSED
;
12050 #ifdef FEAT_CMDHIST
12052 char_u buf
[NUMBUFLEN
];
12055 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
12058 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
12059 /* only one argument: clear entire history */
12060 n
= clr_history(get_histtype(str
));
12061 else if (argvars
[1].v_type
== VAR_NUMBER
)
12062 /* index given: remove that entry */
12063 n
= del_history_idx(get_histtype(str
),
12064 (int)get_tv_number(&argvars
[1]));
12066 /* string given: remove all matching entries */
12067 n
= del_history_entry(get_histtype(str
),
12068 get_tv_string_buf(&argvars
[1], buf
));
12069 rettv
->vval
.v_number
= n
;
12074 * "histget()" function
12077 f_histget(argvars
, rettv
)
12078 typval_T
*argvars UNUSED
;
12081 #ifdef FEAT_CMDHIST
12086 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
12088 rettv
->vval
.v_string
= NULL
;
12091 type
= get_histtype(str
);
12092 if (argvars
[1].v_type
== VAR_UNKNOWN
)
12093 idx
= get_history_idx(type
);
12095 idx
= (int)get_tv_number_chk(&argvars
[1], NULL
);
12096 /* -1 on type error */
12097 rettv
->vval
.v_string
= vim_strsave(get_history_entry(type
, idx
));
12100 rettv
->vval
.v_string
= NULL
;
12102 rettv
->v_type
= VAR_STRING
;
12106 * "histnr()" function
12109 f_histnr(argvars
, rettv
)
12110 typval_T
*argvars UNUSED
;
12115 #ifdef FEAT_CMDHIST
12116 char_u
*history
= get_tv_string_chk(&argvars
[0]);
12118 i
= history
== NULL
? HIST_CMD
- 1 : get_histtype(history
);
12119 if (i
>= HIST_CMD
&& i
< HIST_COUNT
)
12120 i
= get_history_idx(i
);
12124 rettv
->vval
.v_number
= i
;
12128 * "highlightID(name)" function
12131 f_hlID(argvars
, rettv
)
12135 rettv
->vval
.v_number
= syn_name2id(get_tv_string(&argvars
[0]));
12139 * "highlight_exists()" function
12142 f_hlexists(argvars
, rettv
)
12146 rettv
->vval
.v_number
= highlight_exists(get_tv_string(&argvars
[0]));
12150 * "hostname()" function
12153 f_hostname(argvars
, rettv
)
12154 typval_T
*argvars UNUSED
;
12157 char_u hostname
[256];
12159 mch_get_host_name(hostname
, 256);
12160 rettv
->v_type
= VAR_STRING
;
12161 rettv
->vval
.v_string
= vim_strsave(hostname
);
12168 f_iconv(argvars
, rettv
)
12169 typval_T
*argvars UNUSED
;
12173 char_u buf1
[NUMBUFLEN
];
12174 char_u buf2
[NUMBUFLEN
];
12175 char_u
*from
, *to
, *str
;
12179 rettv
->v_type
= VAR_STRING
;
12180 rettv
->vval
.v_string
= NULL
;
12183 str
= get_tv_string(&argvars
[0]);
12184 from
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[1], buf1
)));
12185 to
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[2], buf2
)));
12186 vimconv
.vc_type
= CONV_NONE
;
12187 convert_setup(&vimconv
, from
, to
);
12189 /* If the encodings are equal, no conversion needed. */
12190 if (vimconv
.vc_type
== CONV_NONE
)
12191 rettv
->vval
.v_string
= vim_strsave(str
);
12193 rettv
->vval
.v_string
= string_convert(&vimconv
, str
, NULL
);
12195 convert_setup(&vimconv
, NULL
, NULL
);
12202 * "indent()" function
12205 f_indent(argvars
, rettv
)
12211 lnum
= get_tv_lnum(argvars
);
12212 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
12213 rettv
->vval
.v_number
= get_indent_lnum(lnum
);
12215 rettv
->vval
.v_number
= -1;
12219 * "index()" function
12222 f_index(argvars
, rettv
)
12231 rettv
->vval
.v_number
= -1;
12232 if (argvars
[0].v_type
!= VAR_LIST
)
12234 EMSG(_(e_listreq
));
12237 l
= argvars
[0].vval
.v_list
;
12240 item
= l
->lv_first
;
12241 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12245 /* Start at specified item. Use the cached index that list_find()
12246 * sets, so that a negative number also works. */
12247 item
= list_find(l
, get_tv_number_chk(&argvars
[2], &error
));
12249 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
12250 ic
= get_tv_number_chk(&argvars
[3], &error
);
12255 for ( ; item
!= NULL
; item
= item
->li_next
, ++idx
)
12256 if (tv_equal(&item
->li_tv
, &argvars
[1], ic
))
12258 rettv
->vval
.v_number
= idx
;
12264 static int inputsecret_flag
= 0;
12266 static void get_user_input
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int inputdialog
));
12269 * This function is used by f_input() and f_inputdialog() functions. The third
12270 * argument to f_input() specifies the type of completion to use at the
12271 * prompt. The third argument to f_inputdialog() specifies the value to return
12272 * when the user cancels the prompt.
12275 get_user_input(argvars
, rettv
, inputdialog
)
12280 char_u
*prompt
= get_tv_string_chk(&argvars
[0]);
12283 char_u buf
[NUMBUFLEN
];
12284 int cmd_silent_save
= cmd_silent
;
12285 char_u
*defstr
= (char_u
*)"";
12286 int xp_type
= EXPAND_NOTHING
;
12287 char_u
*xp_arg
= NULL
;
12289 rettv
->v_type
= VAR_STRING
;
12290 rettv
->vval
.v_string
= NULL
;
12292 #ifdef NO_CONSOLE_INPUT
12293 /* While starting up, there is no place to enter text. */
12294 if (no_console_input())
12298 cmd_silent
= FALSE
; /* Want to see the prompt. */
12299 if (prompt
!= NULL
)
12301 /* Only the part of the message after the last NL is considered as
12302 * prompt for the command line */
12303 p
= vim_strrchr(prompt
, '\n');
12313 msg_puts_attr(prompt
, echo_attr
);
12314 msg_didout
= FALSE
;
12318 cmdline_row
= msg_row
;
12320 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12322 defstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
12323 if (defstr
!= NULL
)
12324 stuffReadbuffSpec(defstr
);
12326 if (!inputdialog
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
12332 rettv
->vval
.v_string
= NULL
;
12334 xp_name
= get_tv_string_buf_chk(&argvars
[2], buf
);
12335 if (xp_name
== NULL
)
12338 xp_namelen
= (int)STRLEN(xp_name
);
12340 if (parse_compl_arg(xp_name
, xp_namelen
, &xp_type
, &argt
,
12346 if (defstr
!= NULL
)
12347 rettv
->vval
.v_string
=
12348 getcmdline_prompt(inputsecret_flag
? NUL
: '@', p
, echo_attr
,
12353 /* since the user typed this, no need to wait for return */
12354 need_wait_return
= FALSE
;
12355 msg_didout
= FALSE
;
12357 cmd_silent
= cmd_silent_save
;
12361 * "input()" function
12362 * Also handles inputsecret() when inputsecret is set.
12365 f_input(argvars
, rettv
)
12369 get_user_input(argvars
, rettv
, FALSE
);
12373 * "inputdialog()" function
12376 f_inputdialog(argvars
, rettv
)
12380 #if defined(FEAT_GUI_TEXTDIALOG)
12381 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
12382 if (gui
.in_use
&& vim_strchr(p_go
, GO_CONDIALOG
) == NULL
)
12385 char_u buf
[NUMBUFLEN
];
12386 char_u
*defstr
= (char_u
*)"";
12388 message
= get_tv_string_chk(&argvars
[0]);
12389 if (argvars
[1].v_type
!= VAR_UNKNOWN
12390 && (defstr
= get_tv_string_buf_chk(&argvars
[1], buf
)) != NULL
)
12391 vim_strncpy(IObuff
, defstr
, IOSIZE
- 1);
12394 if (message
!= NULL
&& defstr
!= NULL
12395 && do_dialog(VIM_QUESTION
, NULL
, message
,
12396 (char_u
*)_("&OK\n&Cancel"), 1, IObuff
) == 1)
12397 rettv
->vval
.v_string
= vim_strsave(IObuff
);
12400 if (message
!= NULL
&& defstr
!= NULL
12401 && argvars
[1].v_type
!= VAR_UNKNOWN
12402 && argvars
[2].v_type
!= VAR_UNKNOWN
)
12403 rettv
->vval
.v_string
= vim_strsave(
12404 get_tv_string_buf(&argvars
[2], buf
));
12406 rettv
->vval
.v_string
= NULL
;
12408 rettv
->v_type
= VAR_STRING
;
12412 get_user_input(argvars
, rettv
, TRUE
);
12416 * "inputlist()" function
12419 f_inputlist(argvars
, rettv
)
12427 #ifdef NO_CONSOLE_INPUT
12428 /* While starting up, there is no place to enter text. */
12429 if (no_console_input())
12432 if (argvars
[0].v_type
!= VAR_LIST
|| argvars
[0].vval
.v_list
== NULL
)
12434 EMSG2(_(e_listarg
), "inputlist()");
12439 msg_row
= Rows
- 1; /* for when 'cmdheight' > 1 */
12440 lines_left
= Rows
; /* avoid more prompt */
12444 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
; li
= li
->li_next
)
12446 msg_puts(get_tv_string(&li
->li_tv
));
12450 /* Ask for choice. */
12451 selected
= prompt_for_number(&mouse_used
);
12453 selected
-= lines_left
;
12455 rettv
->vval
.v_number
= selected
;
12459 static garray_T ga_userinput
= {0, 0, sizeof(tasave_T
), 4, NULL
};
12462 * "inputrestore()" function
12465 f_inputrestore(argvars
, rettv
)
12466 typval_T
*argvars UNUSED
;
12469 if (ga_userinput
.ga_len
> 0)
12471 --ga_userinput
.ga_len
;
12472 restore_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
12473 + ga_userinput
.ga_len
);
12474 /* default return is zero == OK */
12476 else if (p_verbose
> 1)
12478 verb_msg((char_u
*)_("called inputrestore() more often than inputsave()"));
12479 rettv
->vval
.v_number
= 1; /* Failed */
12484 * "inputsave()" function
12487 f_inputsave(argvars
, rettv
)
12488 typval_T
*argvars UNUSED
;
12491 /* Add an entry to the stack of typeahead storage. */
12492 if (ga_grow(&ga_userinput
, 1) == OK
)
12494 save_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
12495 + ga_userinput
.ga_len
);
12496 ++ga_userinput
.ga_len
;
12497 /* default return is zero == OK */
12500 rettv
->vval
.v_number
= 1; /* Failed */
12504 * "inputsecret()" function
12507 f_inputsecret(argvars
, rettv
)
12512 ++inputsecret_flag
;
12513 f_input(argvars
, rettv
);
12515 --inputsecret_flag
;
12519 * "insert()" function
12522 f_insert(argvars
, rettv
)
12531 if (argvars
[0].v_type
!= VAR_LIST
)
12532 EMSG2(_(e_listarg
), "insert()");
12533 else if ((l
= argvars
[0].vval
.v_list
) != NULL
12534 && !tv_check_lock(l
->lv_lock
, (char_u
*)"insert()"))
12536 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12537 before
= get_tv_number_chk(&argvars
[2], &error
);
12539 return; /* type error; errmsg already given */
12541 if (before
== l
->lv_len
)
12545 item
= list_find(l
, before
);
12548 EMSGN(_(e_listidx
), before
);
12554 list_insert_tv(l
, &argvars
[1], item
);
12555 copy_tv(&argvars
[0], rettv
);
12561 * "isdirectory()" function
12564 f_isdirectory(argvars
, rettv
)
12568 rettv
->vval
.v_number
= mch_isdir(get_tv_string(&argvars
[0]));
12572 * "islocked()" function
12575 f_islocked(argvars
, rettv
)
12583 rettv
->vval
.v_number
= -1;
12584 end
= get_lval(get_tv_string(&argvars
[0]), NULL
, &lv
, FALSE
, FALSE
, FALSE
,
12586 if (end
!= NULL
&& lv
.ll_name
!= NULL
)
12589 EMSG(_(e_trailing
));
12592 if (lv
.ll_tv
== NULL
)
12594 if (check_changedtick(lv
.ll_name
))
12595 rettv
->vval
.v_number
= 1; /* always locked */
12598 di
= find_var(lv
.ll_name
, NULL
);
12601 /* Consider a variable locked when:
12602 * 1. the variable itself is locked
12603 * 2. the value of the variable is locked.
12604 * 3. the List or Dict value is locked.
12606 rettv
->vval
.v_number
= ((di
->di_flags
& DI_FLAGS_LOCK
)
12607 || tv_islocked(&di
->di_tv
));
12611 else if (lv
.ll_range
)
12612 EMSG(_("E786: Range not allowed"));
12613 else if (lv
.ll_newkey
!= NULL
)
12614 EMSG2(_(e_dictkey
), lv
.ll_newkey
);
12615 else if (lv
.ll_list
!= NULL
)
12617 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_li
->li_tv
);
12619 /* Dictionary item. */
12620 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_di
->di_tv
);
12627 static void dict_list
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int what
));
12630 * Turn a dict into a list:
12631 * "what" == 0: list of keys
12632 * "what" == 1: list of values
12633 * "what" == 2: list of items
12636 dict_list(argvars
, rettv
, what
)
12649 if (argvars
[0].v_type
!= VAR_DICT
)
12651 EMSG(_(e_dictreq
));
12654 if ((d
= argvars
[0].vval
.v_dict
) == NULL
)
12657 if (rettv_list_alloc(rettv
) == FAIL
)
12660 todo
= (int)d
->dv_hashtab
.ht_used
;
12661 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
12663 if (!HASHITEM_EMPTY(hi
))
12668 li
= listitem_alloc();
12671 list_append(rettv
->vval
.v_list
, li
);
12676 li
->li_tv
.v_type
= VAR_STRING
;
12677 li
->li_tv
.v_lock
= 0;
12678 li
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
12680 else if (what
== 1)
12683 copy_tv(&di
->di_tv
, &li
->li_tv
);
12689 li
->li_tv
.v_type
= VAR_LIST
;
12690 li
->li_tv
.v_lock
= 0;
12691 li
->li_tv
.vval
.v_list
= l2
;
12696 li2
= listitem_alloc();
12699 list_append(l2
, li2
);
12700 li2
->li_tv
.v_type
= VAR_STRING
;
12701 li2
->li_tv
.v_lock
= 0;
12702 li2
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
12704 li2
= listitem_alloc();
12707 list_append(l2
, li2
);
12708 copy_tv(&di
->di_tv
, &li2
->li_tv
);
12715 * "items(dict)" function
12718 f_items(argvars
, rettv
)
12722 dict_list(argvars
, rettv
, 2);
12726 * "join()" function
12729 f_join(argvars
, rettv
)
12736 if (argvars
[0].v_type
!= VAR_LIST
)
12738 EMSG(_(e_listreq
));
12741 if (argvars
[0].vval
.v_list
== NULL
)
12743 if (argvars
[1].v_type
== VAR_UNKNOWN
)
12744 sep
= (char_u
*)" ";
12746 sep
= get_tv_string_chk(&argvars
[1]);
12748 rettv
->v_type
= VAR_STRING
;
12752 ga_init2(&ga
, (int)sizeof(char), 80);
12753 list_join(&ga
, argvars
[0].vval
.v_list
, sep
, TRUE
, 0);
12754 ga_append(&ga
, NUL
);
12755 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
12758 rettv
->vval
.v_string
= NULL
;
12762 * "keys()" function
12765 f_keys(argvars
, rettv
)
12769 dict_list(argvars
, rettv
, 0);
12773 * "last_buffer_nr()" function.
12776 f_last_buffer_nr(argvars
, rettv
)
12777 typval_T
*argvars UNUSED
;
12783 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
12784 if (n
< buf
->b_fnum
)
12787 rettv
->vval
.v_number
= n
;
12794 f_len(argvars
, rettv
)
12798 switch (argvars
[0].v_type
)
12802 rettv
->vval
.v_number
= (varnumber_T
)STRLEN(
12803 get_tv_string(&argvars
[0]));
12806 rettv
->vval
.v_number
= list_len(argvars
[0].vval
.v_list
);
12809 rettv
->vval
.v_number
= dict_len(argvars
[0].vval
.v_dict
);
12812 EMSG(_("E701: Invalid type for len()"));
12817 static void libcall_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int type
));
12820 libcall_common(argvars
, rettv
, type
)
12825 #ifdef FEAT_LIBCALL
12827 char_u
**string_result
;
12831 rettv
->v_type
= type
;
12832 if (type
!= VAR_NUMBER
)
12833 rettv
->vval
.v_string
= NULL
;
12835 if (check_restricted() || check_secure())
12838 #ifdef FEAT_LIBCALL
12839 /* The first two args must be strings, otherwise its meaningless */
12840 if (argvars
[0].v_type
== VAR_STRING
&& argvars
[1].v_type
== VAR_STRING
)
12843 if (argvars
[2].v_type
== VAR_STRING
)
12844 string_in
= argvars
[2].vval
.v_string
;
12845 if (type
== VAR_NUMBER
)
12846 string_result
= NULL
;
12848 string_result
= &rettv
->vval
.v_string
;
12849 if (mch_libcall(argvars
[0].vval
.v_string
,
12850 argvars
[1].vval
.v_string
,
12852 argvars
[2].vval
.v_number
,
12855 && type
== VAR_NUMBER
)
12856 rettv
->vval
.v_number
= nr_result
;
12862 * "libcall()" function
12865 f_libcall(argvars
, rettv
)
12869 libcall_common(argvars
, rettv
, VAR_STRING
);
12873 * "libcallnr()" function
12876 f_libcallnr(argvars
, rettv
)
12880 libcall_common(argvars
, rettv
, VAR_NUMBER
);
12884 * "line(string)" function
12887 f_line(argvars
, rettv
)
12895 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
12898 rettv
->vval
.v_number
= lnum
;
12902 * "line2byte(lnum)" function
12905 f_line2byte(argvars
, rettv
)
12906 typval_T
*argvars UNUSED
;
12909 #ifndef FEAT_BYTEOFF
12910 rettv
->vval
.v_number
= -1;
12914 lnum
= get_tv_lnum(argvars
);
12915 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
12916 rettv
->vval
.v_number
= -1;
12918 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
, lnum
, NULL
);
12919 if (rettv
->vval
.v_number
>= 0)
12920 ++rettv
->vval
.v_number
;
12925 * "lispindent(lnum)" function
12928 f_lispindent(argvars
, rettv
)
12936 pos
= curwin
->w_cursor
;
12937 lnum
= get_tv_lnum(argvars
);
12938 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
12940 curwin
->w_cursor
.lnum
= lnum
;
12941 rettv
->vval
.v_number
= get_lisp_indent();
12942 curwin
->w_cursor
= pos
;
12946 rettv
->vval
.v_number
= -1;
12950 * "localtime()" function
12953 f_localtime(argvars
, rettv
)
12954 typval_T
*argvars UNUSED
;
12957 rettv
->vval
.v_number
= (varnumber_T
)time(NULL
);
12960 static void get_maparg
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int exact
));
12963 get_maparg(argvars
, rettv
, exact
)
12970 char_u buf
[NUMBUFLEN
];
12971 char_u
*keys_buf
= NULL
;
12977 /* return empty string for failure */
12978 rettv
->v_type
= VAR_STRING
;
12979 rettv
->vval
.v_string
= NULL
;
12981 keys
= get_tv_string(&argvars
[0]);
12985 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12987 which
= get_tv_string_buf_chk(&argvars
[1], buf
);
12988 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12989 abbr
= get_tv_number(&argvars
[2]);
12992 which
= (char_u
*)"";
12996 mode
= get_map_mode(&which
, 0);
12998 keys
= replace_termcodes(keys
, &keys_buf
, TRUE
, TRUE
, FALSE
);
12999 rhs
= check_map(keys
, mode
, exact
, FALSE
, abbr
);
13000 vim_free(keys_buf
);
13004 ga
.ga_itemsize
= 1;
13005 ga
.ga_growsize
= 40;
13007 while (*rhs
!= NUL
)
13008 ga_concat(&ga
, str2special(&rhs
, FALSE
));
13010 ga_append(&ga
, NUL
);
13011 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
13017 * "log10()" function
13020 f_log10(argvars
, rettv
)
13026 rettv
->v_type
= VAR_FLOAT
;
13027 if (get_float_arg(argvars
, &f
) == OK
)
13028 rettv
->vval
.v_float
= log10(f
);
13030 rettv
->vval
.v_float
= 0.0;
13038 f_map(argvars
, rettv
)
13042 filter_map(argvars
, rettv
, TRUE
);
13046 * "maparg()" function
13049 f_maparg(argvars
, rettv
)
13053 get_maparg(argvars
, rettv
, TRUE
);
13057 * "mapcheck()" function
13060 f_mapcheck(argvars
, rettv
)
13064 get_maparg(argvars
, rettv
, FALSE
);
13067 static void find_some_match
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int start
));
13070 find_some_match(argvars
, rettv
, type
)
13075 char_u
*str
= NULL
;
13076 char_u
*expr
= NULL
;
13078 regmatch_T regmatch
;
13079 char_u patbuf
[NUMBUFLEN
];
13080 char_u strbuf
[NUMBUFLEN
];
13084 colnr_T startcol
= 0;
13087 listitem_T
*li
= NULL
;
13089 char_u
*tofree
= NULL
;
13091 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13093 p_cpo
= (char_u
*)"";
13095 rettv
->vval
.v_number
= -1;
13098 /* return empty list when there are no matches */
13099 if (rettv_list_alloc(rettv
) == FAIL
)
13102 else if (type
== 2)
13104 rettv
->v_type
= VAR_STRING
;
13105 rettv
->vval
.v_string
= NULL
;
13108 if (argvars
[0].v_type
== VAR_LIST
)
13110 if ((l
= argvars
[0].vval
.v_list
) == NULL
)
13115 expr
= str
= get_tv_string(&argvars
[0]);
13117 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
13121 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13125 start
= get_tv_number_chk(&argvars
[2], &error
);
13130 li
= list_find(l
, start
);
13133 idx
= l
->lv_idx
; /* use the cached index */
13139 if (start
> (long)STRLEN(str
))
13141 /* When "count" argument is there ignore matches before "start",
13142 * otherwise skip part of the string. Differs when pattern is "^"
13144 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13150 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13151 nth
= get_tv_number_chk(&argvars
[3], &error
);
13156 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
13157 if (regmatch
.regprog
!= NULL
)
13159 regmatch
.rm_ic
= p_ic
;
13171 str
= echo_string(&li
->li_tv
, &tofree
, strbuf
, 0);
13176 match
= vim_regexec_nl(®match
, str
, (colnr_T
)startcol
);
13178 if (match
&& --nth
<= 0)
13180 if (l
== NULL
&& !match
)
13183 /* Advance to just after the match. */
13192 startcol
= (colnr_T
)(regmatch
.startp
[0]
13193 + (*mb_ptr2len
)(regmatch
.startp
[0]) - str
);
13195 startcol
= regmatch
.startp
[0] + 1 - str
;
13206 /* return list with matched string and submatches */
13207 for (i
= 0; i
< NSUBEXP
; ++i
)
13209 if (regmatch
.endp
[i
] == NULL
)
13211 if (list_append_string(rettv
->vval
.v_list
,
13212 (char_u
*)"", 0) == FAIL
)
13215 else if (list_append_string(rettv
->vval
.v_list
,
13216 regmatch
.startp
[i
],
13217 (int)(regmatch
.endp
[i
] - regmatch
.startp
[i
]))
13222 else if (type
== 2)
13224 /* return matched string */
13226 copy_tv(&li
->li_tv
, rettv
);
13228 rettv
->vval
.v_string
= vim_strnsave(regmatch
.startp
[0],
13229 (int)(regmatch
.endp
[0] - regmatch
.startp
[0]));
13231 else if (l
!= NULL
)
13232 rettv
->vval
.v_number
= idx
;
13236 rettv
->vval
.v_number
=
13237 (varnumber_T
)(regmatch
.startp
[0] - str
);
13239 rettv
->vval
.v_number
=
13240 (varnumber_T
)(regmatch
.endp
[0] - str
);
13241 rettv
->vval
.v_number
+= (varnumber_T
)(str
- expr
);
13244 vim_free(regmatch
.regprog
);
13253 * "match()" function
13256 f_match(argvars
, rettv
)
13260 find_some_match(argvars
, rettv
, 1);
13264 * "matchadd()" function
13267 f_matchadd(argvars
, rettv
)
13271 #ifdef FEAT_SEARCH_EXTRA
13272 char_u buf
[NUMBUFLEN
];
13273 char_u
*grp
= get_tv_string_buf_chk(&argvars
[0], buf
); /* group */
13274 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], buf
); /* pattern */
13275 int prio
= 10; /* default priority */
13279 rettv
->vval
.v_number
= -1;
13281 if (grp
== NULL
|| pat
== NULL
)
13283 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13285 prio
= get_tv_number_chk(&argvars
[2], &error
);
13286 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
13287 id
= get_tv_number_chk(&argvars
[3], &error
);
13291 if (id
>= 1 && id
<= 3)
13293 EMSGN("E798: ID is reserved for \":match\": %ld", id
);
13297 rettv
->vval
.v_number
= match_add(curwin
, grp
, pat
, prio
, id
);
13302 * "matcharg()" function
13305 f_matcharg(argvars
, rettv
)
13309 if (rettv_list_alloc(rettv
) == OK
)
13311 #ifdef FEAT_SEARCH_EXTRA
13312 int id
= get_tv_number(&argvars
[0]);
13315 if (id
>= 1 && id
<= 3)
13317 if ((m
= (matchitem_T
*)get_match(curwin
, id
)) != NULL
)
13319 list_append_string(rettv
->vval
.v_list
,
13320 syn_id2name(m
->hlg_id
), -1);
13321 list_append_string(rettv
->vval
.v_list
, m
->pattern
, -1);
13325 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
13326 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
13334 * "matchdelete()" function
13337 f_matchdelete(argvars
, rettv
)
13341 #ifdef FEAT_SEARCH_EXTRA
13342 rettv
->vval
.v_number
= match_delete(curwin
,
13343 (int)get_tv_number(&argvars
[0]), TRUE
);
13348 * "matchend()" function
13351 f_matchend(argvars
, rettv
)
13355 find_some_match(argvars
, rettv
, 0);
13359 * "matchlist()" function
13362 f_matchlist(argvars
, rettv
)
13366 find_some_match(argvars
, rettv
, 3);
13370 * "matchstr()" function
13373 f_matchstr(argvars
, rettv
)
13377 find_some_match(argvars
, rettv
, 2);
13380 static void max_min
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int domax
));
13383 max_min(argvars
, rettv
, domax
)
13392 if (argvars
[0].v_type
== VAR_LIST
)
13397 l
= argvars
[0].vval
.v_list
;
13403 n
= get_tv_number_chk(&li
->li_tv
, &error
);
13409 i
= get_tv_number_chk(&li
->li_tv
, &error
);
13410 if (domax
? i
> n
: i
< n
)
13416 else if (argvars
[0].v_type
== VAR_DICT
)
13423 d
= argvars
[0].vval
.v_dict
;
13426 todo
= (int)d
->dv_hashtab
.ht_used
;
13427 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
13429 if (!HASHITEM_EMPTY(hi
))
13432 i
= get_tv_number_chk(&HI2DI(hi
)->di_tv
, &error
);
13438 else if (domax
? i
> n
: i
< n
)
13445 EMSG(_(e_listdictarg
));
13446 rettv
->vval
.v_number
= error
? 0 : n
;
13453 f_max(argvars
, rettv
)
13457 max_min(argvars
, rettv
, TRUE
);
13461 * "migemo()" function
13464 f_migemo(argvars
, rettv
)
13468 char_u
* arg
= get_tv_string(&argvars
[0]);
13470 rettv
->v_type
= VAR_STRING
;
13472 rettv
->vval
.v_string
= query_migemo(arg
);
13474 rettv
->vval
.v_string
= vim_strsave(arg
);
13482 f_min(argvars
, rettv
)
13486 max_min(argvars
, rettv
, FALSE
);
13489 static int mkdir_recurse
__ARGS((char_u
*dir
, int prot
));
13492 * Create the directory in which "dir" is located, and higher levels when
13496 mkdir_recurse(dir
, prot
)
13504 /* Get end of directory name in "dir".
13505 * We're done when it's "/" or "c:/". */
13506 p
= gettail_sep(dir
);
13507 if (p
<= get_past_head(dir
))
13510 /* If the directory exists we're done. Otherwise: create it.*/
13511 updir
= vim_strnsave(dir
, (int)(p
- dir
));
13514 if (mch_isdir(updir
))
13516 else if (mkdir_recurse(updir
, prot
) == OK
)
13517 r
= vim_mkdir_emsg(updir
, prot
);
13524 * "mkdir()" function
13527 f_mkdir(argvars
, rettv
)
13532 char_u buf
[NUMBUFLEN
];
13535 rettv
->vval
.v_number
= FAIL
;
13536 if (check_restricted() || check_secure())
13539 dir
= get_tv_string_buf(&argvars
[0], buf
);
13540 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
13542 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13543 prot
= get_tv_number_chk(&argvars
[2], NULL
);
13544 if (prot
!= -1 && STRCMP(get_tv_string(&argvars
[1]), "p") == 0)
13545 mkdir_recurse(dir
, prot
);
13547 rettv
->vval
.v_number
= prot
!= -1 ? vim_mkdir_emsg(dir
, prot
) : 0;
13552 * "mode()" function
13555 f_mode(argvars
, rettv
)
13568 buf
[0] = VIsual_mode
+ 's' - 'v';
13570 buf
[0] = VIsual_mode
;
13574 if (State
== HITRETURN
|| State
== ASKMORE
|| State
== SETWSIZE
13575 || State
== CONFIRM
)
13578 if (State
== ASKMORE
)
13580 else if (State
== CONFIRM
)
13583 else if (State
== EXTERNCMD
)
13585 else if (State
& INSERT
)
13587 #ifdef FEAT_VREPLACE
13588 if (State
& VREPLACE_FLAG
)
13595 if (State
& REPLACE_FLAG
)
13600 else if (State
& CMDLINE
)
13606 else if (exmode_active
)
13618 /* Clear out the minor mode when the argument is not a non-zero number or
13619 * non-empty string. */
13620 if (!non_zero_arg(&argvars
[0]))
13623 rettv
->vval
.v_string
= vim_strsave(buf
);
13624 rettv
->v_type
= VAR_STRING
;
13627 #ifdef FEAT_MZSCHEME
13629 * "mzeval()" function
13632 f_mzeval(argvars
, rettv
)
13637 char_u buf
[NUMBUFLEN
];
13639 str
= get_tv_string_buf(&argvars
[0], buf
);
13640 do_mzeval(str
, rettv
);
13645 * "nextnonblank()" function
13648 f_nextnonblank(argvars
, rettv
)
13654 for (lnum
= get_tv_lnum(argvars
); ; ++lnum
)
13656 if (lnum
< 0 || lnum
> curbuf
->b_ml
.ml_line_count
)
13661 if (*skipwhite(ml_get(lnum
)) != NUL
)
13664 rettv
->vval
.v_number
= lnum
;
13668 * "nr2char()" function
13671 f_nr2char(argvars
, rettv
)
13675 char_u buf
[NUMBUFLEN
];
13679 buf
[(*mb_char2bytes
)((int)get_tv_number(&argvars
[0]), buf
)] = NUL
;
13683 buf
[0] = (char_u
)get_tv_number(&argvars
[0]);
13686 rettv
->v_type
= VAR_STRING
;
13687 rettv
->vval
.v_string
= vim_strsave(buf
);
13691 * "pathshorten()" function
13694 f_pathshorten(argvars
, rettv
)
13700 rettv
->v_type
= VAR_STRING
;
13701 p
= get_tv_string_chk(&argvars
[0]);
13703 rettv
->vval
.v_string
= NULL
;
13706 p
= vim_strsave(p
);
13707 rettv
->vval
.v_string
= p
;
13718 f_pow(argvars
, rettv
)
13724 rettv
->v_type
= VAR_FLOAT
;
13725 if (get_float_arg(argvars
, &fx
) == OK
13726 && get_float_arg(&argvars
[1], &fy
) == OK
)
13727 rettv
->vval
.v_float
= pow(fx
, fy
);
13729 rettv
->vval
.v_float
= 0.0;
13734 * "prevnonblank()" function
13737 f_prevnonblank(argvars
, rettv
)
13743 lnum
= get_tv_lnum(argvars
);
13744 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
)
13747 while (lnum
>= 1 && *skipwhite(ml_get(lnum
)) == NUL
)
13749 rettv
->vval
.v_number
= lnum
;
13752 #ifdef HAVE_STDARG_H
13753 /* This dummy va_list is here because:
13754 * - passing a NULL pointer doesn't work when va_list isn't a pointer
13755 * - locally in the function results in a "used before set" warning
13756 * - using va_start() to initialize it gives "function with fixed args" error */
13761 * "printf()" function
13764 f_printf(argvars
, rettv
)
13768 rettv
->v_type
= VAR_STRING
;
13769 rettv
->vval
.v_string
= NULL
;
13770 #ifdef HAVE_STDARG_H /* only very old compilers can't do this */
13772 char_u buf
[NUMBUFLEN
];
13775 int saved_did_emsg
= did_emsg
;
13778 /* Get the required length, allocate the buffer and do it for real. */
13780 fmt
= (char *)get_tv_string_buf(&argvars
[0], buf
);
13781 len
= vim_vsnprintf(NULL
, 0, fmt
, ap
, argvars
+ 1);
13784 s
= alloc(len
+ 1);
13787 rettv
->vval
.v_string
= s
;
13788 (void)vim_vsnprintf((char *)s
, len
+ 1, fmt
, ap
, argvars
+ 1);
13791 did_emsg
|= saved_did_emsg
;
13797 * "pumvisible()" function
13800 f_pumvisible(argvars
, rettv
)
13801 typval_T
*argvars UNUSED
;
13802 typval_T
*rettv UNUSED
;
13804 #ifdef FEAT_INS_EXPAND
13806 rettv
->vval
.v_number
= 1;
13811 * "range()" function
13814 f_range(argvars
, rettv
)
13824 start
= get_tv_number_chk(&argvars
[0], &error
);
13825 if (argvars
[1].v_type
== VAR_UNKNOWN
)
13832 end
= get_tv_number_chk(&argvars
[1], &error
);
13833 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13834 stride
= get_tv_number_chk(&argvars
[2], &error
);
13838 return; /* type error; errmsg already given */
13840 EMSG(_("E726: Stride is zero"));
13841 else if (stride
> 0 ? end
+ 1 < start
: end
- 1 > start
)
13842 EMSG(_("E727: Start past end"));
13845 if (rettv_list_alloc(rettv
) == OK
)
13846 for (i
= start
; stride
> 0 ? i
<= end
: i
>= end
; i
+= stride
)
13847 if (list_append_number(rettv
->vval
.v_list
,
13848 (varnumber_T
)i
) == FAIL
)
13854 * "readfile()" function
13857 f_readfile(argvars
, rettv
)
13861 int binary
= FALSE
;
13865 #define FREAD_SIZE 200 /* optimized for text lines */
13866 char_u buf
[FREAD_SIZE
];
13867 int readlen
; /* size of last fread() */
13868 int buflen
; /* nr of valid chars in buf[] */
13869 int filtd
; /* how much in buf[] was NUL -> '\n' filtered */
13870 int tolist
; /* first byte in buf[] still to be put in list */
13871 int chop
; /* how many CR to chop off */
13872 char_u
*prev
= NULL
; /* previously read bytes, if any */
13873 int prevlen
= 0; /* length of "prev" if not NULL */
13876 long maxline
= MAXLNUM
;
13879 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
13881 if (STRCMP(get_tv_string(&argvars
[1]), "b") == 0)
13883 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13884 maxline
= get_tv_number(&argvars
[2]);
13887 if (rettv_list_alloc(rettv
) == FAIL
)
13890 /* Always open the file in binary mode, library functions have a mind of
13891 * their own about CR-LF conversion. */
13892 fname
= get_tv_string(&argvars
[0]);
13893 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, READBIN
)) == NULL
)
13895 EMSG2(_(e_notopen
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
13900 while (cnt
< maxline
|| maxline
< 0)
13902 readlen
= (int)fread(buf
+ filtd
, 1, FREAD_SIZE
- filtd
, fd
);
13903 buflen
= filtd
+ readlen
;
13905 for ( ; filtd
< buflen
|| readlen
<= 0; ++filtd
)
13907 if (buf
[filtd
] == '\n' || readlen
<= 0)
13909 /* Only when in binary mode add an empty list item when the
13910 * last line ends in a '\n'. */
13911 if (!binary
&& readlen
== 0 && filtd
== 0)
13914 /* Found end-of-line or end-of-file: add a text line to the
13918 while (filtd
- chop
- 1 >= tolist
13919 && buf
[filtd
- chop
- 1] == '\r')
13921 len
= filtd
- tolist
- chop
;
13923 s
= vim_strnsave(buf
+ tolist
, len
);
13926 s
= alloc((unsigned)(prevlen
+ len
+ 1));
13929 mch_memmove(s
, prev
, prevlen
);
13932 mch_memmove(s
+ prevlen
, buf
+ tolist
, len
);
13933 s
[prevlen
+ len
] = NUL
;
13936 tolist
= filtd
+ 1;
13938 li
= listitem_alloc();
13944 li
->li_tv
.v_type
= VAR_STRING
;
13945 li
->li_tv
.v_lock
= 0;
13946 li
->li_tv
.vval
.v_string
= s
;
13947 list_append(rettv
->vval
.v_list
, li
);
13949 if (++cnt
>= maxline
&& maxline
>= 0)
13954 else if (buf
[filtd
] == NUL
)
13962 /* "buf" is full, need to move text to an allocated buffer */
13965 prev
= vim_strnsave(buf
, buflen
);
13970 s
= alloc((unsigned)(prevlen
+ buflen
));
13973 mch_memmove(s
, prev
, prevlen
);
13974 mch_memmove(s
+ prevlen
, buf
, buflen
);
13984 mch_memmove(buf
, buf
+ tolist
, buflen
- tolist
);
13990 * For a negative line count use only the lines at the end of the file,
13994 while (cnt
> -maxline
)
13996 listitem_remove(rettv
->vval
.v_list
, rettv
->vval
.v_list
->lv_first
);
14004 #if defined(FEAT_RELTIME)
14005 static int list2proftime
__ARGS((typval_T
*arg
, proftime_T
*tm
));
14008 * Convert a List to proftime_T.
14009 * Return FAIL when there is something wrong.
14012 list2proftime(arg
, tm
)
14019 if (arg
->v_type
!= VAR_LIST
|| arg
->vval
.v_list
== NULL
14020 || arg
->vval
.v_list
->lv_len
!= 2)
14022 n1
= list_find_nr(arg
->vval
.v_list
, 0L, &error
);
14023 n2
= list_find_nr(arg
->vval
.v_list
, 1L, &error
);
14031 return error
? FAIL
: OK
;
14033 #endif /* FEAT_RELTIME */
14036 * "reltime()" function
14039 f_reltime(argvars
, rettv
)
14043 #ifdef FEAT_RELTIME
14047 if (argvars
[0].v_type
== VAR_UNKNOWN
)
14049 /* No arguments: get current time. */
14050 profile_start(&res
);
14052 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
14054 if (list2proftime(&argvars
[0], &res
) == FAIL
)
14060 /* Two arguments: compute the difference. */
14061 if (list2proftime(&argvars
[0], &start
) == FAIL
14062 || list2proftime(&argvars
[1], &res
) == FAIL
)
14064 profile_sub(&res
, &start
);
14067 if (rettv_list_alloc(rettv
) == OK
)
14078 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n1
);
14079 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n2
);
14085 * "reltimestr()" function
14088 f_reltimestr(argvars
, rettv
)
14092 #ifdef FEAT_RELTIME
14096 rettv
->v_type
= VAR_STRING
;
14097 rettv
->vval
.v_string
= NULL
;
14098 #ifdef FEAT_RELTIME
14099 if (list2proftime(&argvars
[0], &tm
) == OK
)
14100 rettv
->vval
.v_string
= vim_strsave((char_u
*)profile_msg(&tm
));
14104 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14105 static void make_connection
__ARGS((void));
14106 static int check_connection
__ARGS((void));
14111 if (X_DISPLAY
== NULL
14117 x_force_connect
= TRUE
;
14119 x_force_connect
= FALSE
;
14127 if (X_DISPLAY
== NULL
)
14129 EMSG(_("E240: No connection to Vim server"));
14136 #ifdef FEAT_CLIENTSERVER
14137 static void remote_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int expr
));
14140 remote_common(argvars
, rettv
, expr
)
14145 char_u
*server_name
;
14148 char_u buf
[NUMBUFLEN
];
14155 if (check_restricted() || check_secure())
14159 if (check_connection() == FAIL
)
14163 server_name
= get_tv_string_chk(&argvars
[0]);
14164 if (server_name
== NULL
)
14165 return; /* type error; errmsg already given */
14166 keys
= get_tv_string_buf(&argvars
[1], buf
);
14168 if (serverSendToVim(server_name
, keys
, &r
, &w
, expr
, TRUE
) < 0)
14170 if (serverSendToVim(X_DISPLAY
, server_name
, keys
, &r
, &w
, expr
, 0, TRUE
)
14175 EMSG(r
); /* sending worked but evaluation failed */
14177 EMSG2(_("E241: Unable to send to %s"), server_name
);
14181 rettv
->vval
.v_string
= r
;
14183 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14189 sprintf((char *)str
, PRINTF_HEX_LONG_U
, (long_u
)w
);
14190 v
.di_tv
.v_type
= VAR_STRING
;
14191 v
.di_tv
.vval
.v_string
= vim_strsave(str
);
14192 idvar
= get_tv_string_chk(&argvars
[2]);
14194 set_var(idvar
, &v
.di_tv
, FALSE
);
14195 vim_free(v
.di_tv
.vval
.v_string
);
14201 * "remote_expr()" function
14204 f_remote_expr(argvars
, rettv
)
14205 typval_T
*argvars UNUSED
;
14208 rettv
->v_type
= VAR_STRING
;
14209 rettv
->vval
.v_string
= NULL
;
14210 #ifdef FEAT_CLIENTSERVER
14211 remote_common(argvars
, rettv
, TRUE
);
14216 * "remote_foreground()" function
14219 f_remote_foreground(argvars
, rettv
)
14220 typval_T
*argvars UNUSED
;
14221 typval_T
*rettv UNUSED
;
14223 #ifdef FEAT_CLIENTSERVER
14225 /* On Win32 it's done in this application. */
14227 char_u
*server_name
= get_tv_string_chk(&argvars
[0]);
14229 if (server_name
!= NULL
)
14230 serverForeground(server_name
);
14233 /* Send a foreground() expression to the server. */
14234 argvars
[1].v_type
= VAR_STRING
;
14235 argvars
[1].vval
.v_string
= vim_strsave((char_u
*)"foreground()");
14236 argvars
[2].v_type
= VAR_UNKNOWN
;
14237 remote_common(argvars
, rettv
, TRUE
);
14238 vim_free(argvars
[1].vval
.v_string
);
14244 f_remote_peek(argvars
, rettv
)
14245 typval_T
*argvars UNUSED
;
14248 #ifdef FEAT_CLIENTSERVER
14256 if (check_restricted() || check_secure())
14258 rettv
->vval
.v_number
= -1;
14261 serverid
= get_tv_string_chk(&argvars
[0]);
14262 if (serverid
== NULL
)
14264 rettv
->vval
.v_number
= -1;
14265 return; /* type error; errmsg already given */
14268 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
14270 rettv
->vval
.v_number
= -1;
14273 s
= serverGetReply((HWND
)n
, FALSE
, FALSE
, FALSE
);
14274 rettv
->vval
.v_number
= (s
!= NULL
);
14277 if (check_connection() == FAIL
)
14280 rettv
->vval
.v_number
= serverPeekReply(X_DISPLAY
,
14281 serverStrToWin(serverid
), &s
);
14284 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& rettv
->vval
.v_number
> 0)
14288 v
.di_tv
.v_type
= VAR_STRING
;
14289 v
.di_tv
.vval
.v_string
= vim_strsave(s
);
14290 retvar
= get_tv_string_chk(&argvars
[1]);
14291 if (retvar
!= NULL
)
14292 set_var(retvar
, &v
.di_tv
, FALSE
);
14293 vim_free(v
.di_tv
.vval
.v_string
);
14296 rettv
->vval
.v_number
= -1;
14301 f_remote_read(argvars
, rettv
)
14302 typval_T
*argvars UNUSED
;
14307 #ifdef FEAT_CLIENTSERVER
14308 char_u
*serverid
= get_tv_string_chk(&argvars
[0]);
14310 if (serverid
!= NULL
&& !check_restricted() && !check_secure())
14313 /* The server's HWND is encoded in the 'id' parameter */
14316 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
14318 r
= serverGetReply((HWND
)n
, FALSE
, TRUE
, TRUE
);
14321 if (check_connection() == FAIL
|| serverReadReply(X_DISPLAY
,
14322 serverStrToWin(serverid
), &r
, FALSE
) < 0)
14324 EMSG(_("E277: Unable to read a server reply"));
14327 rettv
->v_type
= VAR_STRING
;
14328 rettv
->vval
.v_string
= r
;
14332 * "remote_send()" function
14335 f_remote_send(argvars
, rettv
)
14336 typval_T
*argvars UNUSED
;
14339 rettv
->v_type
= VAR_STRING
;
14340 rettv
->vval
.v_string
= NULL
;
14341 #ifdef FEAT_CLIENTSERVER
14342 remote_common(argvars
, rettv
, FALSE
);
14347 * "remove()" function
14350 f_remove(argvars
, rettv
)
14355 listitem_T
*item
, *item2
;
14363 if (argvars
[0].v_type
== VAR_DICT
)
14365 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14366 EMSG2(_(e_toomanyarg
), "remove()");
14367 else if ((d
= argvars
[0].vval
.v_dict
) != NULL
14368 && !tv_check_lock(d
->dv_lock
, (char_u
*)"remove() argument"))
14370 key
= get_tv_string_chk(&argvars
[1]);
14373 di
= dict_find(d
, key
, -1);
14375 EMSG2(_(e_dictkey
), key
);
14378 *rettv
= di
->di_tv
;
14379 init_tv(&di
->di_tv
);
14380 dictitem_remove(d
, di
);
14385 else if (argvars
[0].v_type
!= VAR_LIST
)
14386 EMSG2(_(e_listdictarg
), "remove()");
14387 else if ((l
= argvars
[0].vval
.v_list
) != NULL
14388 && !tv_check_lock(l
->lv_lock
, (char_u
*)"remove() argument"))
14392 idx
= get_tv_number_chk(&argvars
[1], &error
);
14394 ; /* type error: do nothing, errmsg already given */
14395 else if ((item
= list_find(l
, idx
)) == NULL
)
14396 EMSGN(_(e_listidx
), idx
);
14399 if (argvars
[2].v_type
== VAR_UNKNOWN
)
14401 /* Remove one item, return its value. */
14402 list_remove(l
, item
, item
);
14403 *rettv
= item
->li_tv
;
14408 /* Remove range of items, return list with values. */
14409 end
= get_tv_number_chk(&argvars
[2], &error
);
14411 ; /* type error: do nothing */
14412 else if ((item2
= list_find(l
, end
)) == NULL
)
14413 EMSGN(_(e_listidx
), end
);
14418 for (li
= item
; li
!= NULL
; li
= li
->li_next
)
14424 if (li
== NULL
) /* didn't find "item2" after "item" */
14425 EMSG(_(e_invrange
));
14428 list_remove(l
, item
, item2
);
14429 if (rettv_list_alloc(rettv
) == OK
)
14431 l
= rettv
->vval
.v_list
;
14432 l
->lv_first
= item
;
14433 l
->lv_last
= item2
;
14434 item
->li_prev
= NULL
;
14435 item2
->li_next
= NULL
;
14446 * "rename({from}, {to})" function
14449 f_rename(argvars
, rettv
)
14453 char_u buf
[NUMBUFLEN
];
14455 if (check_restricted() || check_secure())
14456 rettv
->vval
.v_number
= -1;
14458 rettv
->vval
.v_number
= vim_rename(get_tv_string(&argvars
[0]),
14459 get_tv_string_buf(&argvars
[1], buf
));
14463 * "repeat()" function
14466 f_repeat(argvars
, rettv
)
14477 n
= get_tv_number(&argvars
[1]);
14478 if (argvars
[0].v_type
== VAR_LIST
)
14480 if (rettv_list_alloc(rettv
) == OK
&& argvars
[0].vval
.v_list
!= NULL
)
14482 if (list_extend(rettv
->vval
.v_list
,
14483 argvars
[0].vval
.v_list
, NULL
) == FAIL
)
14488 p
= get_tv_string(&argvars
[0]);
14489 rettv
->v_type
= VAR_STRING
;
14490 rettv
->vval
.v_string
= NULL
;
14492 slen
= (int)STRLEN(p
);
14497 r
= alloc(len
+ 1);
14500 for (i
= 0; i
< n
; i
++)
14501 mch_memmove(r
+ i
* slen
, p
, (size_t)slen
);
14505 rettv
->vval
.v_string
= r
;
14510 * "resolve()" function
14513 f_resolve(argvars
, rettv
)
14519 p
= get_tv_string(&argvars
[0]);
14520 #ifdef FEAT_SHORTCUT
14524 v
= mch_resolve_shortcut(p
);
14526 rettv
->vval
.v_string
= v
;
14528 rettv
->vval
.v_string
= vim_strsave(p
);
14531 # ifdef HAVE_READLINK
14533 char_u buf
[MAXPATHL
+ 1];
14536 char_u
*remain
= NULL
;
14538 int is_relative_to_current
= FALSE
;
14539 int has_trailing_pathsep
= FALSE
;
14542 p
= vim_strsave(p
);
14544 if (p
[0] == '.' && (vim_ispathsep(p
[1])
14545 || (p
[1] == '.' && (vim_ispathsep(p
[2])))))
14546 is_relative_to_current
= TRUE
;
14549 if (len
> 0 && after_pathsep(p
, p
+ len
))
14550 has_trailing_pathsep
= TRUE
;
14552 q
= getnextcomp(p
);
14555 /* Separate the first path component in "p", and keep the
14556 * remainder (beginning with the path separator). */
14557 remain
= vim_strsave(q
- 1);
14565 len
= readlink((char *)p
, (char *)buf
, MAXPATHL
);
14574 EMSG(_("E655: Too many symbolic links (cycle?)"));
14575 rettv
->vval
.v_string
= NULL
;
14579 /* Ensure that the result will have a trailing path separator
14580 * if the argument has one. */
14581 if (remain
== NULL
&& has_trailing_pathsep
)
14584 /* Separate the first path component in the link value and
14585 * concatenate the remainders. */
14586 q
= getnextcomp(vim_ispathsep(*buf
) ? buf
+ 1 : buf
);
14589 if (remain
== NULL
)
14590 remain
= vim_strsave(q
- 1);
14593 cpy
= concat_str(q
- 1, remain
);
14604 if (q
> p
&& *q
== NUL
)
14606 /* Ignore trailing path separator. */
14610 if (q
> p
&& !mch_isFullName(buf
))
14612 /* symlink is relative to directory of argument */
14613 cpy
= alloc((unsigned)(STRLEN(p
) + STRLEN(buf
) + 1));
14617 STRCPY(gettail(cpy
), buf
);
14625 p
= vim_strsave(buf
);
14629 if (remain
== NULL
)
14632 /* Append the first path component of "remain" to "p". */
14633 q
= getnextcomp(remain
+ 1);
14634 len
= q
- remain
- (*q
!= NUL
);
14635 cpy
= vim_strnsave(p
, STRLEN(p
) + len
);
14638 STRNCAT(cpy
, remain
, len
);
14642 /* Shorten "remain". */
14644 STRMOVE(remain
, q
- 1);
14652 /* If the result is a relative path name, make it explicitly relative to
14653 * the current directory if and only if the argument had this form. */
14654 if (!vim_ispathsep(*p
))
14656 if (is_relative_to_current
14660 || vim_ispathsep(p
[1])
14663 || vim_ispathsep(p
[2]))))))
14665 /* Prepend "./". */
14666 cpy
= concat_str((char_u
*)"./", p
);
14673 else if (!is_relative_to_current
)
14675 /* Strip leading "./". */
14677 while (q
[0] == '.' && vim_ispathsep(q
[1]))
14684 /* Ensure that the result will have no trailing path separator
14685 * if the argument had none. But keep "/" or "//". */
14686 if (!has_trailing_pathsep
)
14689 if (after_pathsep(p
, q
))
14690 *gettail_sep(p
) = NUL
;
14693 rettv
->vval
.v_string
= p
;
14696 rettv
->vval
.v_string
= vim_strsave(p
);
14700 simplify_filename(rettv
->vval
.v_string
);
14702 #ifdef HAVE_READLINK
14705 rettv
->v_type
= VAR_STRING
;
14709 * "reverse({list})" function
14712 f_reverse(argvars
, rettv
)
14717 listitem_T
*li
, *ni
;
14719 if (argvars
[0].v_type
!= VAR_LIST
)
14720 EMSG2(_(e_listarg
), "reverse()");
14721 else if ((l
= argvars
[0].vval
.v_list
) != NULL
14722 && !tv_check_lock(l
->lv_lock
, (char_u
*)"reverse()"))
14725 l
->lv_first
= l
->lv_last
= NULL
;
14730 list_append(l
, li
);
14733 rettv
->vval
.v_list
= l
;
14734 rettv
->v_type
= VAR_LIST
;
14736 l
->lv_idx
= l
->lv_len
- l
->lv_idx
- 1;
14740 #define SP_NOMOVE 0x01 /* don't move cursor */
14741 #define SP_REPEAT 0x02 /* repeat to find outer pair */
14742 #define SP_RETCOUNT 0x04 /* return matchcount */
14743 #define SP_SETPCMARK 0x08 /* set previous context mark */
14744 #define SP_START 0x10 /* accept match at start position */
14745 #define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
14746 #define SP_END 0x40 /* leave cursor at end of match */
14748 static int get_search_arg
__ARGS((typval_T
*varp
, int *flagsp
));
14751 * Get flags for a search function.
14752 * Possibly sets "p_ws".
14753 * Returns BACKWARD, FORWARD or zero (for an error).
14756 get_search_arg(varp
, flagsp
)
14762 char_u nbuf
[NUMBUFLEN
];
14765 if (varp
->v_type
!= VAR_UNKNOWN
)
14767 flags
= get_tv_string_buf_chk(varp
, nbuf
);
14769 return 0; /* type error; errmsg already given */
14770 while (*flags
!= NUL
)
14774 case 'b': dir
= BACKWARD
; break;
14775 case 'w': p_ws
= TRUE
; break;
14776 case 'W': p_ws
= FALSE
; break;
14778 if (flagsp
!= NULL
)
14781 case 'c': mask
= SP_START
; break;
14782 case 'e': mask
= SP_END
; break;
14783 case 'm': mask
= SP_RETCOUNT
; break;
14784 case 'n': mask
= SP_NOMOVE
; break;
14785 case 'p': mask
= SP_SUBPAT
; break;
14786 case 'r': mask
= SP_REPEAT
; break;
14787 case 's': mask
= SP_SETPCMARK
; break;
14791 EMSG2(_(e_invarg2
), flags
);
14806 * Shared by search() and searchpos() functions
14809 search_cmn(argvars
, match_pos
, flagsp
)
14818 int save_p_ws
= p_ws
;
14820 int retval
= 0; /* default: FAIL */
14821 long lnum_stop
= 0;
14823 #ifdef FEAT_RELTIME
14824 long time_limit
= 0;
14826 int options
= SEARCH_KEEP
;
14829 pat
= get_tv_string(&argvars
[0]);
14830 dir
= get_search_arg(&argvars
[1], flagsp
); /* may set p_ws */
14834 if (flags
& SP_START
)
14835 options
|= SEARCH_START
;
14836 if (flags
& SP_END
)
14837 options
|= SEARCH_END
;
14839 /* Optional arguments: line number to stop searching and timeout. */
14840 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
14842 lnum_stop
= get_tv_number_chk(&argvars
[2], NULL
);
14845 #ifdef FEAT_RELTIME
14846 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
14848 time_limit
= get_tv_number_chk(&argvars
[3], NULL
);
14849 if (time_limit
< 0)
14855 #ifdef FEAT_RELTIME
14856 /* Set the time limit, if there is one. */
14857 profile_setlimit(time_limit
, &tm
);
14861 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
14862 * Check to make sure only those flags are set.
14863 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14864 * flags cannot be set. Check for that condition also.
14866 if (((flags
& (SP_REPEAT
| SP_RETCOUNT
)) != 0)
14867 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
14869 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[1]));
14873 pos
= save_cursor
= curwin
->w_cursor
;
14874 subpatnum
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
14875 options
, RE_SEARCH
, (linenr_T
)lnum_stop
, &tm
);
14876 if (subpatnum
!= FAIL
)
14878 if (flags
& SP_SUBPAT
)
14879 retval
= subpatnum
;
14882 if (flags
& SP_SETPCMARK
)
14884 curwin
->w_cursor
= pos
;
14885 if (match_pos
!= NULL
)
14887 /* Store the match cursor position */
14888 match_pos
->lnum
= pos
.lnum
;
14889 match_pos
->col
= pos
.col
+ 1;
14891 /* "/$" will put the cursor after the end of the line, may need to
14892 * correct that here */
14896 /* If 'n' flag is used: restore cursor position. */
14897 if (flags
& SP_NOMOVE
)
14898 curwin
->w_cursor
= save_cursor
;
14900 curwin
->w_set_curswant
= TRUE
;
14909 * "round({float})" function
14912 f_round(argvars
, rettv
)
14918 rettv
->v_type
= VAR_FLOAT
;
14919 if (get_float_arg(argvars
, &f
) == OK
)
14920 /* round() is not in C90, use ceil() or floor() instead. */
14921 rettv
->vval
.v_float
= f
> 0 ? floor(f
+ 0.5) : ceil(f
- 0.5);
14923 rettv
->vval
.v_float
= 0.0;
14928 * "search()" function
14931 f_search(argvars
, rettv
)
14937 rettv
->vval
.v_number
= search_cmn(argvars
, NULL
, &flags
);
14941 * "searchdecl()" function
14944 f_searchdecl(argvars
, rettv
)
14953 rettv
->vval
.v_number
= 1; /* default: FAIL */
14955 name
= get_tv_string_chk(&argvars
[0]);
14956 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
14958 locally
= get_tv_number_chk(&argvars
[1], &error
) == 0;
14959 if (!error
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
14960 thisblock
= get_tv_number_chk(&argvars
[2], &error
) != 0;
14962 if (!error
&& name
!= NULL
)
14963 rettv
->vval
.v_number
= find_decl(name
, (int)STRLEN(name
),
14964 locally
, thisblock
, SEARCH_KEEP
) == FAIL
;
14968 * Used by searchpair() and searchpairpos()
14971 searchpair_cmn(argvars
, match_pos
)
14975 char_u
*spat
, *mpat
, *epat
;
14977 int save_p_ws
= p_ws
;
14980 char_u nbuf1
[NUMBUFLEN
];
14981 char_u nbuf2
[NUMBUFLEN
];
14982 char_u nbuf3
[NUMBUFLEN
];
14983 int retval
= 0; /* default: FAIL */
14984 long lnum_stop
= 0;
14985 long time_limit
= 0;
14987 /* Get the three pattern arguments: start, middle, end. */
14988 spat
= get_tv_string_chk(&argvars
[0]);
14989 mpat
= get_tv_string_buf_chk(&argvars
[1], nbuf1
);
14990 epat
= get_tv_string_buf_chk(&argvars
[2], nbuf2
);
14991 if (spat
== NULL
|| mpat
== NULL
|| epat
== NULL
)
14992 goto theend
; /* type error */
14994 /* Handle the optional fourth argument: flags */
14995 dir
= get_search_arg(&argvars
[3], &flags
); /* may set p_ws */
14999 /* Don't accept SP_END or SP_SUBPAT.
15000 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15002 if ((flags
& (SP_END
| SP_SUBPAT
)) != 0
15003 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
15005 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[3]));
15009 /* Using 'r' implies 'W', otherwise it doesn't work. */
15010 if (flags
& SP_REPEAT
)
15013 /* Optional fifth argument: skip expression */
15014 if (argvars
[3].v_type
== VAR_UNKNOWN
15015 || argvars
[4].v_type
== VAR_UNKNOWN
)
15016 skip
= (char_u
*)"";
15019 skip
= get_tv_string_buf_chk(&argvars
[4], nbuf3
);
15020 if (argvars
[5].v_type
!= VAR_UNKNOWN
)
15022 lnum_stop
= get_tv_number_chk(&argvars
[5], NULL
);
15025 #ifdef FEAT_RELTIME
15026 if (argvars
[6].v_type
!= VAR_UNKNOWN
)
15028 time_limit
= get_tv_number_chk(&argvars
[6], NULL
);
15029 if (time_limit
< 0)
15036 goto theend
; /* type error */
15038 retval
= do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
,
15039 match_pos
, lnum_stop
, time_limit
);
15048 * "searchpair()" function
15051 f_searchpair(argvars
, rettv
)
15055 rettv
->vval
.v_number
= searchpair_cmn(argvars
, NULL
);
15059 * "searchpairpos()" function
15062 f_searchpairpos(argvars
, rettv
)
15070 if (rettv_list_alloc(rettv
) == FAIL
)
15073 if (searchpair_cmn(argvars
, &match_pos
) > 0)
15075 lnum
= match_pos
.lnum
;
15076 col
= match_pos
.col
;
15079 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
15080 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
15084 * Search for a start/middle/end thing.
15085 * Used by searchpair(), see its documentation for the details.
15086 * Returns 0 or -1 for no match,
15089 do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
, match_pos
,
15090 lnum_stop
, time_limit
)
15091 char_u
*spat
; /* start pattern */
15092 char_u
*mpat
; /* middle pattern */
15093 char_u
*epat
; /* end pattern */
15094 int dir
; /* BACKWARD or FORWARD */
15095 char_u
*skip
; /* skip expression */
15096 int flags
; /* SP_SETPCMARK and other SP_ values */
15098 linenr_T lnum_stop
; /* stop at this line if not zero */
15099 long time_limit
; /* stop after this many msec */
15102 char_u
*pat
, *pat2
= NULL
, *pat3
= NULL
;
15113 int options
= SEARCH_KEEP
;
15116 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15118 p_cpo
= empty_option
;
15120 #ifdef FEAT_RELTIME
15121 /* Set the time limit, if there is one. */
15122 profile_setlimit(time_limit
, &tm
);
15125 /* Make two search patterns: start/end (pat2, for in nested pairs) and
15126 * start/middle/end (pat3, for the top pair). */
15127 pat2
= alloc((unsigned)(STRLEN(spat
) + STRLEN(epat
) + 15));
15128 pat3
= alloc((unsigned)(STRLEN(spat
) + STRLEN(mpat
) + STRLEN(epat
) + 23));
15129 if (pat2
== NULL
|| pat3
== NULL
)
15131 sprintf((char *)pat2
, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat
, epat
);
15133 STRCPY(pat3
, pat2
);
15135 sprintf((char *)pat3
, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
15137 if (flags
& SP_START
)
15138 options
|= SEARCH_START
;
15140 save_cursor
= curwin
->w_cursor
;
15141 pos
= curwin
->w_cursor
;
15142 clearpos(&firstpos
);
15143 clearpos(&foundpos
);
15147 n
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
15148 options
, RE_SEARCH
, lnum_stop
, &tm
);
15149 if (n
== FAIL
|| (firstpos
.lnum
!= 0 && equalpos(pos
, firstpos
)))
15150 /* didn't find it or found the first match again: FAIL */
15153 if (firstpos
.lnum
== 0)
15155 if (equalpos(pos
, foundpos
))
15157 /* Found the same position again. Can happen with a pattern that
15158 * has "\zs" at the end and searching backwards. Advance one
15159 * character and try again. */
15160 if (dir
== BACKWARD
)
15167 /* clear the start flag to avoid getting stuck here */
15168 options
&= ~SEARCH_START
;
15170 /* If the skip pattern matches, ignore this match. */
15173 save_pos
= curwin
->w_cursor
;
15174 curwin
->w_cursor
= pos
;
15175 r
= eval_to_bool(skip
, &err
, NULL
, FALSE
);
15176 curwin
->w_cursor
= save_pos
;
15179 /* Evaluating {skip} caused an error, break here. */
15180 curwin
->w_cursor
= save_cursor
;
15188 if ((dir
== BACKWARD
&& n
== 3) || (dir
== FORWARD
&& n
== 2))
15190 /* Found end when searching backwards or start when searching
15191 * forward: nested pair. */
15193 pat
= pat2
; /* nested, don't search for middle */
15197 /* Found end when searching forward or start when searching
15198 * backward: end of (nested) pair; or found middle in outer pair. */
15200 pat
= pat3
; /* outer level, search for middle */
15205 /* Found the match: return matchcount or line number. */
15206 if (flags
& SP_RETCOUNT
)
15210 if (flags
& SP_SETPCMARK
)
15212 curwin
->w_cursor
= pos
;
15213 if (!(flags
& SP_REPEAT
))
15215 nest
= 1; /* search for next unmatched */
15219 if (match_pos
!= NULL
)
15221 /* Store the match cursor position */
15222 match_pos
->lnum
= curwin
->w_cursor
.lnum
;
15223 match_pos
->col
= curwin
->w_cursor
.col
+ 1;
15226 /* If 'n' flag is used or search failed: restore cursor position. */
15227 if ((flags
& SP_NOMOVE
) || retval
== 0)
15228 curwin
->w_cursor
= save_cursor
;
15233 if (p_cpo
== empty_option
)
15236 /* Darn, evaluating the {skip} expression changed the value. */
15237 free_string_option(save_cpo
);
15243 * "searchpos()" function
15246 f_searchpos(argvars
, rettv
)
15256 if (rettv_list_alloc(rettv
) == FAIL
)
15259 n
= search_cmn(argvars
, &match_pos
, &flags
);
15262 lnum
= match_pos
.lnum
;
15263 col
= match_pos
.col
;
15266 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
15267 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
15268 if (flags
& SP_SUBPAT
)
15269 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n
);
15274 f_server2client(argvars
, rettv
)
15275 typval_T
*argvars UNUSED
;
15278 #ifdef FEAT_CLIENTSERVER
15279 char_u buf
[NUMBUFLEN
];
15280 char_u
*server
= get_tv_string_chk(&argvars
[0]);
15281 char_u
*reply
= get_tv_string_buf_chk(&argvars
[1], buf
);
15283 rettv
->vval
.v_number
= -1;
15284 if (server
== NULL
|| reply
== NULL
)
15286 if (check_restricted() || check_secure())
15289 if (check_connection() == FAIL
)
15293 if (serverSendReply(server
, reply
) < 0)
15295 EMSG(_("E258: Unable to send to client"));
15298 rettv
->vval
.v_number
= 0;
15300 rettv
->vval
.v_number
= -1;
15305 f_serverlist(argvars
, rettv
)
15306 typval_T
*argvars UNUSED
;
15311 #ifdef FEAT_CLIENTSERVER
15313 r
= serverGetVimNames();
15316 if (X_DISPLAY
!= NULL
)
15317 r
= serverGetVimNames(X_DISPLAY
);
15320 rettv
->v_type
= VAR_STRING
;
15321 rettv
->vval
.v_string
= r
;
15325 * "setbufvar()" function
15328 f_setbufvar(argvars
, rettv
)
15330 typval_T
*rettv UNUSED
;
15334 char_u
*varname
, *bufvarname
;
15336 char_u nbuf
[NUMBUFLEN
];
15338 if (check_restricted() || check_secure())
15340 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
15341 varname
= get_tv_string_chk(&argvars
[1]);
15342 buf
= get_buf_tv(&argvars
[0]);
15343 varp
= &argvars
[2];
15345 if (buf
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
15347 /* set curbuf to be our buf, temporarily */
15348 aucmd_prepbuf(&aco
, buf
);
15350 if (*varname
== '&')
15357 numval
= get_tv_number_chk(varp
, &error
);
15358 strval
= get_tv_string_buf_chk(varp
, nbuf
);
15359 if (!error
&& strval
!= NULL
)
15360 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
15364 bufvarname
= alloc((unsigned)STRLEN(varname
) + 3);
15365 if (bufvarname
!= NULL
)
15367 STRCPY(bufvarname
, "b:");
15368 STRCPY(bufvarname
+ 2, varname
);
15369 set_var(bufvarname
, varp
, TRUE
);
15370 vim_free(bufvarname
);
15374 /* reset notion of buffer */
15375 aucmd_restbuf(&aco
);
15380 * "setcmdpos()" function
15383 f_setcmdpos(argvars
, rettv
)
15387 int pos
= (int)get_tv_number(&argvars
[0]) - 1;
15390 rettv
->vval
.v_number
= set_cmdline_pos(pos
);
15394 * "setline()" function
15397 f_setline(argvars
, rettv
)
15402 char_u
*line
= NULL
;
15404 listitem_T
*li
= NULL
;
15406 linenr_T lcount
= curbuf
->b_ml
.ml_line_count
;
15408 lnum
= get_tv_lnum(&argvars
[0]);
15409 if (argvars
[1].v_type
== VAR_LIST
)
15411 l
= argvars
[1].vval
.v_list
;
15415 line
= get_tv_string_chk(&argvars
[1]);
15417 /* default result is zero == OK */
15422 /* list argument, get next string */
15425 line
= get_tv_string_chk(&li
->li_tv
);
15429 rettv
->vval
.v_number
= 1; /* FAIL */
15430 if (line
== NULL
|| lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
15432 if (lnum
<= curbuf
->b_ml
.ml_line_count
)
15434 /* existing line, replace it */
15435 if (u_savesub(lnum
) == OK
&& ml_replace(lnum
, line
, TRUE
) == OK
)
15437 changed_bytes(lnum
, 0);
15438 if (lnum
== curwin
->w_cursor
.lnum
)
15439 check_cursor_col();
15440 rettv
->vval
.v_number
= 0; /* OK */
15443 else if (added
> 0 || u_save(lnum
- 1, lnum
) == OK
)
15445 /* lnum is one past the last line, append the line */
15447 if (ml_append(lnum
- 1, line
, (colnr_T
)0, FALSE
) == OK
)
15448 rettv
->vval
.v_number
= 0; /* OK */
15451 if (l
== NULL
) /* only one string argument */
15457 appended_lines_mark(lcount
, added
);
15460 static void set_qf_ll_list
__ARGS((win_T
*wp
, typval_T
*list_arg
, typval_T
*action_arg
, typval_T
*rettv
));
15463 * Used by "setqflist()" and "setloclist()" functions
15466 set_qf_ll_list(wp
, list_arg
, action_arg
, rettv
)
15468 typval_T
*list_arg UNUSED
;
15469 typval_T
*action_arg UNUSED
;
15472 #ifdef FEAT_QUICKFIX
15477 rettv
->vval
.v_number
= -1;
15479 #ifdef FEAT_QUICKFIX
15480 if (list_arg
->v_type
!= VAR_LIST
)
15481 EMSG(_(e_listreq
));
15484 list_T
*l
= list_arg
->vval
.v_list
;
15486 if (action_arg
->v_type
== VAR_STRING
)
15488 act
= get_tv_string_chk(action_arg
);
15490 return; /* type error; errmsg already given */
15491 if (*act
== 'a' || *act
== 'r')
15495 if (l
!= NULL
&& set_errorlist(wp
, l
, action
) == OK
)
15496 rettv
->vval
.v_number
= 0;
15502 * "setloclist()" function
15505 f_setloclist(argvars
, rettv
)
15511 rettv
->vval
.v_number
= -1;
15513 win
= find_win_by_nr(&argvars
[0], NULL
);
15515 set_qf_ll_list(win
, &argvars
[1], &argvars
[2], rettv
);
15519 * "setmatches()" function
15522 f_setmatches(argvars
, rettv
)
15526 #ifdef FEAT_SEARCH_EXTRA
15531 rettv
->vval
.v_number
= -1;
15532 if (argvars
[0].v_type
!= VAR_LIST
)
15534 EMSG(_(e_listreq
));
15537 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
15540 /* To some extent make sure that we are dealing with a list from
15541 * "getmatches()". */
15545 if (li
->li_tv
.v_type
!= VAR_DICT
15546 || (d
= li
->li_tv
.vval
.v_dict
) == NULL
)
15551 if (!(dict_find(d
, (char_u
*)"group", -1) != NULL
15552 && dict_find(d
, (char_u
*)"pattern", -1) != NULL
15553 && dict_find(d
, (char_u
*)"priority", -1) != NULL
15554 && dict_find(d
, (char_u
*)"id", -1) != NULL
))
15562 clear_matches(curwin
);
15566 d
= li
->li_tv
.vval
.v_dict
;
15567 match_add(curwin
, get_dict_string(d
, (char_u
*)"group", FALSE
),
15568 get_dict_string(d
, (char_u
*)"pattern", FALSE
),
15569 (int)get_dict_number(d
, (char_u
*)"priority"),
15570 (int)get_dict_number(d
, (char_u
*)"id"));
15573 rettv
->vval
.v_number
= 0;
15579 * "setpos()" function
15582 f_setpos(argvars
, rettv
)
15590 rettv
->vval
.v_number
= -1;
15591 name
= get_tv_string_chk(argvars
);
15594 if (list2fpos(&argvars
[1], &pos
, &fnum
) == OK
)
15598 if (name
[0] == '.' && name
[1] == NUL
)
15601 if (fnum
== curbuf
->b_fnum
)
15603 curwin
->w_cursor
= pos
;
15605 rettv
->vval
.v_number
= 0;
15610 else if (name
[0] == '\'' && name
[1] != NUL
&& name
[2] == NUL
)
15613 if (setmark_pos(name
[1], &pos
, fnum
) == OK
)
15614 rettv
->vval
.v_number
= 0;
15623 * "setqflist()" function
15626 f_setqflist(argvars
, rettv
)
15630 set_qf_ll_list(NULL
, &argvars
[0], &argvars
[1], rettv
);
15634 * "setreg()" function
15637 f_setreg(argvars
, rettv
)
15642 char_u
*strregname
;
15653 strregname
= get_tv_string_chk(argvars
);
15654 rettv
->vval
.v_number
= 1; /* FAIL is default */
15656 if (strregname
== NULL
)
15657 return; /* type error; errmsg already given */
15658 regname
= *strregname
;
15659 if (regname
== 0 || regname
== '@')
15661 else if (regname
== '=')
15664 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15666 stropt
= get_tv_string_chk(&argvars
[2]);
15667 if (stropt
== NULL
)
15668 return; /* type error */
15669 for (; *stropt
!= NUL
; ++stropt
)
15672 case 'a': case 'A': /* append */
15675 case 'v': case 'c': /* character-wise selection */
15678 case 'V': case 'l': /* line-wise selection */
15682 case 'b': case Ctrl_V
: /* block-wise selection */
15683 yank_type
= MBLOCK
;
15684 if (VIM_ISDIGIT(stropt
[1]))
15687 block_len
= getdigits(&stropt
) - 1;
15695 strval
= get_tv_string_chk(&argvars
[1]);
15696 if (strval
!= NULL
)
15697 write_reg_contents_ex(regname
, strval
, -1,
15698 append
, yank_type
, block_len
);
15699 rettv
->vval
.v_number
= 0;
15703 * "settabwinvar()" function
15706 f_settabwinvar(argvars
, rettv
)
15710 setwinvar(argvars
, rettv
, 1);
15714 * "setwinvar()" function
15717 f_setwinvar(argvars
, rettv
)
15721 setwinvar(argvars
, rettv
, 0);
15725 * "setwinvar()" and "settabwinvar()" functions
15728 setwinvar(argvars
, rettv
, off
)
15730 typval_T
*rettv UNUSED
;
15734 #ifdef FEAT_WINDOWS
15735 win_T
*save_curwin
;
15736 tabpage_T
*save_curtab
;
15738 char_u
*varname
, *winvarname
;
15740 char_u nbuf
[NUMBUFLEN
];
15743 if (check_restricted() || check_secure())
15746 #ifdef FEAT_WINDOWS
15748 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
15752 win
= find_win_by_nr(&argvars
[off
], tp
);
15753 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
15754 varp
= &argvars
[off
+ 2];
15756 if (win
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
15758 #ifdef FEAT_WINDOWS
15759 /* set curwin to be our win, temporarily */
15760 save_curwin
= curwin
;
15761 save_curtab
= curtab
;
15762 goto_tabpage_tp(tp
);
15763 if (!win_valid(win
))
15766 curbuf
= curwin
->w_buffer
;
15769 if (*varname
== '&')
15776 numval
= get_tv_number_chk(varp
, &error
);
15777 strval
= get_tv_string_buf_chk(varp
, nbuf
);
15778 if (!error
&& strval
!= NULL
)
15779 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
15783 winvarname
= alloc((unsigned)STRLEN(varname
) + 3);
15784 if (winvarname
!= NULL
)
15786 STRCPY(winvarname
, "w:");
15787 STRCPY(winvarname
+ 2, varname
);
15788 set_var(winvarname
, varp
, TRUE
);
15789 vim_free(winvarname
);
15793 #ifdef FEAT_WINDOWS
15794 /* Restore current tabpage and window, if still valid (autocomands can
15795 * make them invalid). */
15796 if (valid_tabpage(save_curtab
))
15797 goto_tabpage_tp(save_curtab
);
15798 if (win_valid(save_curwin
))
15800 curwin
= save_curwin
;
15801 curbuf
= curwin
->w_buffer
;
15808 * "shellescape({string})" function
15811 f_shellescape(argvars
, rettv
)
15815 rettv
->vval
.v_string
= vim_strsave_shellescape(
15816 get_tv_string(&argvars
[0]), non_zero_arg(&argvars
[1]));
15817 rettv
->v_type
= VAR_STRING
;
15821 * "simplify()" function
15824 f_simplify(argvars
, rettv
)
15830 p
= get_tv_string(&argvars
[0]);
15831 rettv
->vval
.v_string
= vim_strsave(p
);
15832 simplify_filename(rettv
->vval
.v_string
); /* simplify in place */
15833 rettv
->v_type
= VAR_STRING
;
15841 f_sin(argvars
, rettv
)
15847 rettv
->v_type
= VAR_FLOAT
;
15848 if (get_float_arg(argvars
, &f
) == OK
)
15849 rettv
->vval
.v_float
= sin(f
);
15851 rettv
->vval
.v_float
= 0.0;
15856 #ifdef __BORLANDC__
15859 item_compare
__ARGS((const void *s1
, const void *s2
));
15861 #ifdef __BORLANDC__
15864 item_compare2
__ARGS((const void *s1
, const void *s2
));
15866 static int item_compare_ic
;
15867 static char_u
*item_compare_func
;
15868 static int item_compare_func_err
;
15869 #define ITEM_COMPARE_FAIL 999
15872 * Compare functions for f_sort() below.
15875 #ifdef __BORLANDC__
15878 item_compare(s1
, s2
)
15883 char_u
*tofree1
, *tofree2
;
15885 char_u numbuf1
[NUMBUFLEN
];
15886 char_u numbuf2
[NUMBUFLEN
];
15888 p1
= tv2string(&(*(listitem_T
**)s1
)->li_tv
, &tofree1
, numbuf1
, 0);
15889 p2
= tv2string(&(*(listitem_T
**)s2
)->li_tv
, &tofree2
, numbuf2
, 0);
15894 if (item_compare_ic
)
15895 res
= STRICMP(p1
, p2
);
15897 res
= STRCMP(p1
, p2
);
15904 #ifdef __BORLANDC__
15907 item_compare2(s1
, s2
)
15916 /* shortcut after failure in previous call; compare all items equal */
15917 if (item_compare_func_err
)
15920 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15921 * in the copy without changing the original list items. */
15922 copy_tv(&(*(listitem_T
**)s1
)->li_tv
, &argv
[0]);
15923 copy_tv(&(*(listitem_T
**)s2
)->li_tv
, &argv
[1]);
15925 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
15926 res
= call_func(item_compare_func
, (int)STRLEN(item_compare_func
),
15927 &rettv
, 2, argv
, 0L, 0L, &dummy
, TRUE
, NULL
);
15928 clear_tv(&argv
[0]);
15929 clear_tv(&argv
[1]);
15932 res
= ITEM_COMPARE_FAIL
;
15934 res
= get_tv_number_chk(&rettv
, &item_compare_func_err
);
15935 if (item_compare_func_err
)
15936 res
= ITEM_COMPARE_FAIL
; /* return value has wrong type */
15942 * "sort({list})" function
15945 f_sort(argvars
, rettv
)
15955 if (argvars
[0].v_type
!= VAR_LIST
)
15956 EMSG2(_(e_listarg
), "sort()");
15959 l
= argvars
[0].vval
.v_list
;
15960 if (l
== NULL
|| tv_check_lock(l
->lv_lock
, (char_u
*)"sort()"))
15962 rettv
->vval
.v_list
= l
;
15963 rettv
->v_type
= VAR_LIST
;
15968 return; /* short list sorts pretty quickly */
15970 item_compare_ic
= FALSE
;
15971 item_compare_func
= NULL
;
15972 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15974 if (argvars
[1].v_type
== VAR_FUNC
)
15975 item_compare_func
= argvars
[1].vval
.v_string
;
15980 i
= get_tv_number_chk(&argvars
[1], &error
);
15982 return; /* type error; errmsg already given */
15984 item_compare_ic
= TRUE
;
15986 item_compare_func
= get_tv_string(&argvars
[1]);
15990 /* Make an array with each entry pointing to an item in the List. */
15991 ptrs
= (listitem_T
**)alloc((int)(len
* sizeof(listitem_T
*)));
15995 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
15998 item_compare_func_err
= FALSE
;
15999 /* test the compare function */
16000 if (item_compare_func
!= NULL
16001 && item_compare2((void *)&ptrs
[0], (void *)&ptrs
[1])
16002 == ITEM_COMPARE_FAIL
)
16003 EMSG(_("E702: Sort compare function failed"));
16006 /* Sort the array with item pointers. */
16007 qsort((void *)ptrs
, (size_t)len
, sizeof(listitem_T
*),
16008 item_compare_func
== NULL
? item_compare
: item_compare2
);
16010 if (!item_compare_func_err
)
16012 /* Clear the List and append the items in the sorted order. */
16013 l
->lv_first
= l
->lv_last
= l
->lv_idx_item
= NULL
;
16015 for (i
= 0; i
< len
; ++i
)
16016 list_append(l
, ptrs
[i
]);
16025 * "soundfold({word})" function
16028 f_soundfold(argvars
, rettv
)
16034 rettv
->v_type
= VAR_STRING
;
16035 s
= get_tv_string(&argvars
[0]);
16037 rettv
->vval
.v_string
= eval_soundfold(s
);
16039 rettv
->vval
.v_string
= vim_strsave(s
);
16044 * "spellbadword()" function
16047 f_spellbadword(argvars
, rettv
)
16048 typval_T
*argvars UNUSED
;
16051 char_u
*word
= (char_u
*)"";
16052 hlf_T attr
= HLF_COUNT
;
16055 if (rettv_list_alloc(rettv
) == FAIL
)
16059 if (argvars
[0].v_type
== VAR_UNKNOWN
)
16061 /* Find the start and length of the badly spelled word. */
16062 len
= spell_move_to(curwin
, FORWARD
, TRUE
, TRUE
, &attr
);
16064 word
= ml_get_cursor();
16066 else if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
16068 char_u
*str
= get_tv_string_chk(&argvars
[0]);
16073 /* Check the argument for spelling. */
16074 while (*str
!= NUL
)
16076 len
= spell_check(curwin
, str
, &attr
, &capcol
, FALSE
);
16077 if (attr
!= HLF_COUNT
)
16088 list_append_string(rettv
->vval
.v_list
, word
, len
);
16089 list_append_string(rettv
->vval
.v_list
, (char_u
*)(
16090 attr
== HLF_SPB
? "bad" :
16091 attr
== HLF_SPR
? "rare" :
16092 attr
== HLF_SPL
? "local" :
16093 attr
== HLF_SPC
? "caps" :
16098 * "spellsuggest()" function
16101 f_spellsuggest(argvars
, rettv
)
16102 typval_T
*argvars UNUSED
;
16107 int typeerr
= FALSE
;
16112 int need_capital
= FALSE
;
16115 if (rettv_list_alloc(rettv
) == FAIL
)
16119 if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
16121 str
= get_tv_string(&argvars
[0]);
16122 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16124 maxcount
= get_tv_number_chk(&argvars
[1], &typeerr
);
16127 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16129 need_capital
= get_tv_number_chk(&argvars
[2], &typeerr
);
16137 spell_suggest_list(&ga
, str
, maxcount
, need_capital
, FALSE
);
16139 for (i
= 0; i
< ga
.ga_len
; ++i
)
16141 str
= ((char_u
**)ga
.ga_data
)[i
];
16143 li
= listitem_alloc();
16148 li
->li_tv
.v_type
= VAR_STRING
;
16149 li
->li_tv
.v_lock
= 0;
16150 li
->li_tv
.vval
.v_string
= str
;
16151 list_append(rettv
->vval
.v_list
, li
);
16160 f_split(argvars
, rettv
)
16166 char_u
*pat
= NULL
;
16167 regmatch_T regmatch
;
16168 char_u patbuf
[NUMBUFLEN
];
16172 int keepempty
= FALSE
;
16173 int typeerr
= FALSE
;
16175 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16177 p_cpo
= (char_u
*)"";
16179 str
= get_tv_string(&argvars
[0]);
16180 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16182 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
16185 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16186 keepempty
= get_tv_number_chk(&argvars
[2], &typeerr
);
16188 if (pat
== NULL
|| *pat
== NUL
)
16189 pat
= (char_u
*)"[\\x01- ]\\+";
16191 if (rettv_list_alloc(rettv
) == FAIL
)
16196 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
16197 if (regmatch
.regprog
!= NULL
)
16199 regmatch
.rm_ic
= FALSE
;
16200 while (*str
!= NUL
|| keepempty
)
16203 match
= FALSE
; /* empty item at the end */
16205 match
= vim_regexec_nl(®match
, str
, col
);
16207 end
= regmatch
.startp
[0];
16209 end
= str
+ STRLEN(str
);
16210 if (keepempty
|| end
> str
|| (rettv
->vval
.v_list
->lv_len
> 0
16211 && *str
!= NUL
&& match
&& end
< regmatch
.endp
[0]))
16213 if (list_append_string(rettv
->vval
.v_list
, str
,
16214 (int)(end
- str
)) == FAIL
)
16219 /* Advance to just after the match. */
16220 if (regmatch
.endp
[0] > str
)
16224 /* Don't get stuck at the same match. */
16226 col
= (*mb_ptr2len
)(regmatch
.endp
[0]);
16231 str
= regmatch
.endp
[0];
16234 vim_free(regmatch
.regprog
);
16242 * "sqrt()" function
16245 f_sqrt(argvars
, rettv
)
16251 rettv
->v_type
= VAR_FLOAT
;
16252 if (get_float_arg(argvars
, &f
) == OK
)
16253 rettv
->vval
.v_float
= sqrt(f
);
16255 rettv
->vval
.v_float
= 0.0;
16259 * "str2float()" function
16262 f_str2float(argvars
, rettv
)
16266 char_u
*p
= skipwhite(get_tv_string(&argvars
[0]));
16269 p
= skipwhite(p
+ 1);
16270 (void)string2float(p
, &rettv
->vval
.v_float
);
16271 rettv
->v_type
= VAR_FLOAT
;
16276 * "str2nr()" function
16279 f_str2nr(argvars
, rettv
)
16287 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16289 base
= get_tv_number(&argvars
[1]);
16290 if (base
!= 8 && base
!= 10 && base
!= 16)
16297 p
= skipwhite(get_tv_string(&argvars
[0]));
16299 p
= skipwhite(p
+ 1);
16300 vim_str2nr(p
, NULL
, NULL
, base
== 8 ? 2 : 0, base
== 16 ? 2 : 0, &n
, NULL
);
16301 rettv
->vval
.v_number
= n
;
16304 #ifdef HAVE_STRFTIME
16306 * "strftime({format}[, {time}])" function
16309 f_strftime(argvars
, rettv
)
16313 char_u result_buf
[256];
16314 struct tm
*curtime
;
16318 rettv
->v_type
= VAR_STRING
;
16320 p
= get_tv_string(&argvars
[0]);
16321 if (argvars
[1].v_type
== VAR_UNKNOWN
)
16322 seconds
= time(NULL
);
16324 seconds
= (time_t)get_tv_number(&argvars
[1]);
16325 curtime
= localtime(&seconds
);
16326 /* MSVC returns NULL for an invalid value of seconds. */
16327 if (curtime
== NULL
)
16328 rettv
->vval
.v_string
= vim_strsave((char_u
*)_("(Invalid)"));
16335 conv
.vc_type
= CONV_NONE
;
16336 enc
= enc_locale();
16337 convert_setup(&conv
, p_enc
, enc
);
16338 if (conv
.vc_type
!= CONV_NONE
)
16339 p
= string_convert(&conv
, p
, NULL
);
16342 (void)strftime((char *)result_buf
, sizeof(result_buf
),
16343 (char *)p
, curtime
);
16345 result_buf
[0] = NUL
;
16348 if (conv
.vc_type
!= CONV_NONE
)
16350 convert_setup(&conv
, enc
, p_enc
);
16351 if (conv
.vc_type
!= CONV_NONE
)
16352 rettv
->vval
.v_string
= string_convert(&conv
, result_buf
, NULL
);
16355 rettv
->vval
.v_string
= vim_strsave(result_buf
);
16358 /* Release conversion descriptors */
16359 convert_setup(&conv
, NULL
, NULL
);
16367 * "stridx()" function
16370 f_stridx(argvars
, rettv
)
16374 char_u buf
[NUMBUFLEN
];
16377 char_u
*save_haystack
;
16381 needle
= get_tv_string_chk(&argvars
[1]);
16382 save_haystack
= haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
16383 rettv
->vval
.v_number
= -1;
16384 if (needle
== NULL
|| haystack
== NULL
)
16385 return; /* type error; errmsg already given */
16387 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16391 start_idx
= get_tv_number_chk(&argvars
[2], &error
);
16392 if (error
|| start_idx
>= (int)STRLEN(haystack
))
16394 if (start_idx
>= 0)
16395 haystack
+= start_idx
;
16398 pos
= (char_u
*)strstr((char *)haystack
, (char *)needle
);
16400 rettv
->vval
.v_number
= (varnumber_T
)(pos
- save_haystack
);
16404 * "string()" function
16407 f_string(argvars
, rettv
)
16412 char_u numbuf
[NUMBUFLEN
];
16414 rettv
->v_type
= VAR_STRING
;
16415 rettv
->vval
.v_string
= tv2string(&argvars
[0], &tofree
, numbuf
, 0);
16416 /* Make a copy if we have a value but it's not in allocated memory. */
16417 if (rettv
->vval
.v_string
!= NULL
&& tofree
== NULL
)
16418 rettv
->vval
.v_string
= vim_strsave(rettv
->vval
.v_string
);
16422 * "strlen()" function
16425 f_strlen(argvars
, rettv
)
16429 rettv
->vval
.v_number
= (varnumber_T
)(STRLEN(
16430 get_tv_string(&argvars
[0])));
16434 * "strpart()" function
16437 f_strpart(argvars
, rettv
)
16447 p
= get_tv_string(&argvars
[0]);
16448 slen
= (int)STRLEN(p
);
16450 n
= get_tv_number_chk(&argvars
[1], &error
);
16453 else if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16454 len
= get_tv_number(&argvars
[2]);
16456 len
= slen
- n
; /* default len: all bytes that are available. */
16459 * Only return the overlap between the specified part and the actual
16471 else if (n
+ len
> slen
)
16474 rettv
->v_type
= VAR_STRING
;
16475 rettv
->vval
.v_string
= vim_strnsave(p
+ n
, len
);
16479 * "strridx()" function
16482 f_strridx(argvars
, rettv
)
16486 char_u buf
[NUMBUFLEN
];
16490 char_u
*lastmatch
= NULL
;
16491 int haystack_len
, end_idx
;
16493 needle
= get_tv_string_chk(&argvars
[1]);
16494 haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
16496 rettv
->vval
.v_number
= -1;
16497 if (needle
== NULL
|| haystack
== NULL
)
16498 return; /* type error; errmsg already given */
16500 haystack_len
= (int)STRLEN(haystack
);
16501 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16503 /* Third argument: upper limit for index */
16504 end_idx
= get_tv_number_chk(&argvars
[2], NULL
);
16506 return; /* can never find a match */
16509 end_idx
= haystack_len
;
16511 if (*needle
== NUL
)
16513 /* Empty string matches past the end. */
16514 lastmatch
= haystack
+ end_idx
;
16518 for (rest
= haystack
; *rest
!= '\0'; ++rest
)
16520 rest
= (char_u
*)strstr((char *)rest
, (char *)needle
);
16521 if (rest
== NULL
|| rest
> haystack
+ end_idx
)
16527 if (lastmatch
== NULL
)
16528 rettv
->vval
.v_number
= -1;
16530 rettv
->vval
.v_number
= (varnumber_T
)(lastmatch
- haystack
);
16534 * "strtrans()" function
16537 f_strtrans(argvars
, rettv
)
16541 rettv
->v_type
= VAR_STRING
;
16542 rettv
->vval
.v_string
= transstr(get_tv_string(&argvars
[0]));
16546 * "submatch()" function
16549 f_submatch(argvars
, rettv
)
16553 rettv
->v_type
= VAR_STRING
;
16554 rettv
->vval
.v_string
=
16555 reg_submatch((int)get_tv_number_chk(&argvars
[0], NULL
));
16559 * "substitute()" function
16562 f_substitute(argvars
, rettv
)
16566 char_u patbuf
[NUMBUFLEN
];
16567 char_u subbuf
[NUMBUFLEN
];
16568 char_u flagsbuf
[NUMBUFLEN
];
16570 char_u
*str
= get_tv_string_chk(&argvars
[0]);
16571 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
16572 char_u
*sub
= get_tv_string_buf_chk(&argvars
[2], subbuf
);
16573 char_u
*flg
= get_tv_string_buf_chk(&argvars
[3], flagsbuf
);
16575 rettv
->v_type
= VAR_STRING
;
16576 if (str
== NULL
|| pat
== NULL
|| sub
== NULL
|| flg
== NULL
)
16577 rettv
->vval
.v_string
= NULL
;
16579 rettv
->vval
.v_string
= do_string_sub(str
, pat
, sub
, flg
);
16583 * "synID(lnum, col, trans)" function
16586 f_synID(argvars
, rettv
)
16587 typval_T
*argvars UNUSED
;
16595 int transerr
= FALSE
;
16597 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
16598 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
16599 trans
= get_tv_number_chk(&argvars
[2], &transerr
);
16601 if (!transerr
&& lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
16602 && col
>= 0 && col
< (long)STRLEN(ml_get(lnum
)))
16603 id
= syn_get_id(curwin
, lnum
, (colnr_T
)col
, trans
, NULL
, FALSE
);
16606 rettv
->vval
.v_number
= id
;
16610 * "synIDattr(id, what [, mode])" function
16613 f_synIDattr(argvars
, rettv
)
16614 typval_T
*argvars UNUSED
;
16622 char_u modebuf
[NUMBUFLEN
];
16625 id
= get_tv_number(&argvars
[0]);
16626 what
= get_tv_string(&argvars
[1]);
16627 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
16629 mode
= get_tv_string_buf(&argvars
[2], modebuf
);
16630 modec
= TOLOWER_ASC(mode
[0]);
16631 if (modec
!= 't' && modec
!= 'c'
16636 modec
= 0; /* replace invalid with current */
16652 switch (TOLOWER_ASC(what
[0]))
16655 if (TOLOWER_ASC(what
[1]) == 'g') /* bg[#] */
16656 p
= highlight_color(id
, what
, modec
);
16658 p
= highlight_has_attr(id
, HL_BOLD
, modec
);
16661 case 'f': /* fg[#] or font */
16662 p
= highlight_color(id
, what
, modec
);
16666 if (TOLOWER_ASC(what
[1]) == 'n') /* inverse */
16667 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
16669 p
= highlight_has_attr(id
, HL_ITALIC
, modec
);
16672 case 'n': /* name */
16673 p
= get_highlight_name(NULL
, id
- 1);
16676 case 'r': /* reverse */
16677 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
16681 if (TOLOWER_ASC(what
[1]) == 'p') /* sp[#] */
16682 p
= highlight_color(id
, what
, modec
);
16683 else /* standout */
16684 p
= highlight_has_attr(id
, HL_STANDOUT
, modec
);
16688 if (STRLEN(what
) <= 5 || TOLOWER_ASC(what
[5]) != 'c')
16690 p
= highlight_has_attr(id
, HL_UNDERLINE
, modec
);
16693 p
= highlight_has_attr(id
, HL_UNDERCURL
, modec
);
16698 p
= vim_strsave(p
);
16700 rettv
->v_type
= VAR_STRING
;
16701 rettv
->vval
.v_string
= p
;
16705 * "synIDtrans(id)" function
16708 f_synIDtrans(argvars
, rettv
)
16709 typval_T
*argvars UNUSED
;
16715 id
= get_tv_number(&argvars
[0]);
16718 id
= syn_get_final_id(id
);
16723 rettv
->vval
.v_number
= id
;
16727 * "synstack(lnum, col)" function
16730 f_synstack(argvars
, rettv
)
16731 typval_T
*argvars UNUSED
;
16741 rettv
->v_type
= VAR_LIST
;
16742 rettv
->vval
.v_list
= NULL
;
16745 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
16746 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
16748 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
16749 && col
>= 0 && (col
== 0 || col
< (long)STRLEN(ml_get(lnum
)))
16750 && rettv_list_alloc(rettv
) != FAIL
)
16752 (void)syn_get_id(curwin
, lnum
, (colnr_T
)col
, FALSE
, NULL
, TRUE
);
16755 id
= syn_get_stack_item(i
);
16758 if (list_append_number(rettv
->vval
.v_list
, id
) == FAIL
)
16766 * "system()" function
16769 f_system(argvars
, rettv
)
16773 char_u
*res
= NULL
;
16775 char_u
*infile
= NULL
;
16776 char_u buf
[NUMBUFLEN
];
16780 if (check_restricted() || check_secure())
16783 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
16786 * Write the string to a temp file, to be used for input of the shell
16789 if ((infile
= vim_tempname('i')) == NULL
)
16795 fd
= mch_fopen((char *)infile
, WRITEBIN
);
16798 EMSG2(_(e_notopen
), infile
);
16801 p
= get_tv_string_buf_chk(&argvars
[1], buf
);
16805 goto done
; /* type error; errmsg already given */
16807 if (fwrite(p
, STRLEN(p
), 1, fd
) != 1)
16809 if (fclose(fd
) != 0)
16813 EMSG(_("E677: Error writing temp file"));
16818 res
= get_cmd_output(get_tv_string(&argvars
[0]), infile
,
16819 SHELL_SILENT
| SHELL_COOKED
);
16822 /* translate <CR> into <NL> */
16827 for (s
= res
; *s
; ++s
)
16835 /* translate <CR><NL> into <NL> */
16841 for (s
= res
; *s
; ++s
)
16843 if (s
[0] == CAR
&& s
[1] == NL
)
16853 if (infile
!= NULL
)
16855 mch_remove(infile
);
16858 rettv
->v_type
= VAR_STRING
;
16859 rettv
->vval
.v_string
= res
;
16863 * "tabpagebuflist()" function
16866 f_tabpagebuflist(argvars
, rettv
)
16867 typval_T
*argvars UNUSED
;
16868 typval_T
*rettv UNUSED
;
16870 #ifdef FEAT_WINDOWS
16874 if (argvars
[0].v_type
== VAR_UNKNOWN
)
16878 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
16880 wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
16882 if (wp
!= NULL
&& rettv_list_alloc(rettv
) != FAIL
)
16884 for (; wp
!= NULL
; wp
= wp
->w_next
)
16885 if (list_append_number(rettv
->vval
.v_list
,
16886 wp
->w_buffer
->b_fnum
) == FAIL
)
16894 * "tabpagenr()" function
16897 f_tabpagenr(argvars
, rettv
)
16898 typval_T
*argvars UNUSED
;
16902 #ifdef FEAT_WINDOWS
16905 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
16907 arg
= get_tv_string_chk(&argvars
[0]);
16911 if (STRCMP(arg
, "$") == 0)
16912 nr
= tabpage_index(NULL
) - 1;
16914 EMSG2(_(e_invexpr2
), arg
);
16918 nr
= tabpage_index(curtab
);
16920 rettv
->vval
.v_number
= nr
;
16924 #ifdef FEAT_WINDOWS
16925 static int get_winnr
__ARGS((tabpage_T
*tp
, typval_T
*argvar
));
16928 * Common code for tabpagewinnr() and winnr().
16931 get_winnr(tp
, argvar
)
16940 twin
= (tp
== curtab
) ? curwin
: tp
->tp_curwin
;
16941 if (argvar
->v_type
!= VAR_UNKNOWN
)
16943 arg
= get_tv_string_chk(argvar
);
16945 nr
= 0; /* type error; errmsg already given */
16946 else if (STRCMP(arg
, "$") == 0)
16947 twin
= (tp
== curtab
) ? lastwin
: tp
->tp_lastwin
;
16948 else if (STRCMP(arg
, "#") == 0)
16950 twin
= (tp
== curtab
) ? prevwin
: tp
->tp_prevwin
;
16956 EMSG2(_(e_invexpr2
), arg
);
16962 for (wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
16963 wp
!= twin
; wp
= wp
->w_next
)
16967 /* didn't find it in this tabpage */
16978 * "tabpagewinnr()" function
16981 f_tabpagewinnr(argvars
, rettv
)
16982 typval_T
*argvars UNUSED
;
16986 #ifdef FEAT_WINDOWS
16989 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
16993 nr
= get_winnr(tp
, &argvars
[1]);
16995 rettv
->vval
.v_number
= nr
;
17000 * "tagfiles()" function
17003 f_tagfiles(argvars
, rettv
)
17004 typval_T
*argvars UNUSED
;
17007 char_u fname
[MAXPATHL
+ 1];
17011 if (rettv_list_alloc(rettv
) == FAIL
)
17014 for (first
= TRUE
; ; first
= FALSE
)
17015 if (get_tagfname(&tn
, first
, fname
) == FAIL
17016 || list_append_string(rettv
->vval
.v_list
, fname
, -1) == FAIL
)
17022 * "taglist()" function
17025 f_taglist(argvars
, rettv
)
17029 char_u
*tag_pattern
;
17031 tag_pattern
= get_tv_string(&argvars
[0]);
17033 rettv
->vval
.v_number
= FALSE
;
17034 if (*tag_pattern
== NUL
)
17037 if (rettv_list_alloc(rettv
) == OK
)
17038 (void)get_tags(rettv
->vval
.v_list
, tag_pattern
);
17042 * "tempname()" function
17045 f_tempname(argvars
, rettv
)
17046 typval_T
*argvars UNUSED
;
17049 static int x
= 'A';
17051 rettv
->v_type
= VAR_STRING
;
17052 rettv
->vval
.v_string
= vim_tempname(x
);
17054 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
17055 * names. Skip 'I' and 'O', they are used for shell redirection. */
17073 } while (x
== 'I' || x
== 'O');
17077 * "test(list)" function: Just checking the walls...
17080 f_test(argvars
, rettv
)
17081 typval_T
*argvars UNUSED
;
17082 typval_T
*rettv UNUSED
;
17084 /* Used for unit testing. Change the code below to your liking. */
17088 char_u
*bad
, *good
;
17090 if (argvars
[0].v_type
!= VAR_LIST
)
17092 l
= argvars
[0].vval
.v_list
;
17098 bad
= get_tv_string(&li
->li_tv
);
17102 good
= get_tv_string(&li
->li_tv
);
17103 rettv
->vval
.v_number
= test_edit_score(bad
, good
);
17108 * "tolower(string)" function
17111 f_tolower(argvars
, rettv
)
17117 p
= vim_strsave(get_tv_string(&argvars
[0]));
17118 rettv
->v_type
= VAR_STRING
;
17119 rettv
->vval
.v_string
= p
;
17131 c
= utf_ptr2char(p
);
17132 lc
= utf_tolower(c
);
17133 l
= utf_ptr2len(p
);
17134 /* TODO: reallocate string when byte count changes. */
17135 if (utf_char2len(lc
) == l
)
17136 utf_char2bytes(lc
, p
);
17139 else if (has_mbyte
&& (l
= (*mb_ptr2len
)(p
)) > 1)
17140 p
+= l
; /* skip multi-byte character */
17144 *p
= TOLOWER_LOC(*p
); /* note that tolower() can be a macro */
17151 * "toupper(string)" function
17154 f_toupper(argvars
, rettv
)
17158 rettv
->v_type
= VAR_STRING
;
17159 rettv
->vval
.v_string
= strup_save(get_tv_string(&argvars
[0]));
17163 * "tr(string, fromstr, tostr)" function
17166 f_tr(argvars
, rettv
)
17183 char_u buf
[NUMBUFLEN
];
17184 char_u buf2
[NUMBUFLEN
];
17187 instr
= get_tv_string(&argvars
[0]);
17188 fromstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
17189 tostr
= get_tv_string_buf_chk(&argvars
[2], buf2
);
17191 /* Default return value: empty string. */
17192 rettv
->v_type
= VAR_STRING
;
17193 rettv
->vval
.v_string
= NULL
;
17194 if (fromstr
== NULL
|| tostr
== NULL
)
17195 return; /* type error; errmsg already given */
17196 ga_init2(&ga
, (int)sizeof(char), 80);
17201 /* not multi-byte: fromstr and tostr must be the same length */
17202 if (STRLEN(fromstr
) != STRLEN(tostr
))
17207 EMSG2(_(e_invarg2
), fromstr
);
17212 /* fromstr and tostr have to contain the same number of chars */
17213 while (*instr
!= NUL
)
17218 inlen
= (*mb_ptr2len
)(instr
);
17222 for (p
= fromstr
; *p
!= NUL
; p
+= fromlen
)
17224 fromlen
= (*mb_ptr2len
)(p
);
17225 if (fromlen
== inlen
&& STRNCMP(instr
, p
, inlen
) == 0)
17227 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
17229 tolen
= (*mb_ptr2len
)(p
);
17237 if (*p
== NUL
) /* tostr is shorter than fromstr */
17244 if (first
&& cpstr
== instr
)
17246 /* Check that fromstr and tostr have the same number of
17247 * (multi-byte) characters. Done only once when a character
17248 * of instr doesn't appear in fromstr. */
17250 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
17252 tolen
= (*mb_ptr2len
)(p
);
17259 ga_grow(&ga
, cplen
);
17260 mch_memmove((char *)ga
.ga_data
+ ga
.ga_len
, cpstr
, (size_t)cplen
);
17261 ga
.ga_len
+= cplen
;
17268 /* When not using multi-byte chars we can do it faster. */
17269 p
= vim_strchr(fromstr
, *instr
);
17271 ga_append(&ga
, tostr
[p
- fromstr
]);
17273 ga_append(&ga
, *instr
);
17278 /* add a terminating NUL */
17280 ga_append(&ga
, NUL
);
17282 rettv
->vval
.v_string
= ga
.ga_data
;
17287 * "trunc({float})" function
17290 f_trunc(argvars
, rettv
)
17296 rettv
->v_type
= VAR_FLOAT
;
17297 if (get_float_arg(argvars
, &f
) == OK
)
17298 /* trunc() is not in C90, use floor() or ceil() instead. */
17299 rettv
->vval
.v_float
= f
> 0 ? floor(f
) : ceil(f
);
17301 rettv
->vval
.v_float
= 0.0;
17306 * "type(expr)" function
17309 f_type(argvars
, rettv
)
17315 switch (argvars
[0].v_type
)
17317 case VAR_NUMBER
: n
= 0; break;
17318 case VAR_STRING
: n
= 1; break;
17319 case VAR_FUNC
: n
= 2; break;
17320 case VAR_LIST
: n
= 3; break;
17321 case VAR_DICT
: n
= 4; break;
17323 case VAR_FLOAT
: n
= 5; break;
17325 default: EMSG2(_(e_intern2
), "f_type()"); n
= 0; break;
17327 rettv
->vval
.v_number
= n
;
17331 * "values(dict)" function
17334 f_values(argvars
, rettv
)
17338 dict_list(argvars
, rettv
, 1);
17342 * "virtcol(string)" function
17345 f_virtcol(argvars
, rettv
)
17351 int fnum
= curbuf
->b_fnum
;
17353 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
17354 if (fp
!= NULL
&& fp
->lnum
<= curbuf
->b_ml
.ml_line_count
17355 && fnum
== curbuf
->b_fnum
)
17357 getvvcol(curwin
, fp
, NULL
, NULL
, &vcol
);
17361 rettv
->vval
.v_number
= vcol
;
17365 * "visualmode()" function
17368 f_visualmode(argvars
, rettv
)
17369 typval_T
*argvars UNUSED
;
17370 typval_T
*rettv UNUSED
;
17375 rettv
->v_type
= VAR_STRING
;
17376 str
[0] = curbuf
->b_visual_mode_eval
;
17378 rettv
->vval
.v_string
= vim_strsave(str
);
17380 /* A non-zero number or non-empty string argument: reset mode. */
17381 if (non_zero_arg(&argvars
[0]))
17382 curbuf
->b_visual_mode_eval
= NUL
;
17387 * "winbufnr(nr)" function
17390 f_winbufnr(argvars
, rettv
)
17396 wp
= find_win_by_nr(&argvars
[0], NULL
);
17398 rettv
->vval
.v_number
= -1;
17400 rettv
->vval
.v_number
= wp
->w_buffer
->b_fnum
;
17404 * "wincol()" function
17407 f_wincol(argvars
, rettv
)
17408 typval_T
*argvars UNUSED
;
17412 rettv
->vval
.v_number
= curwin
->w_wcol
+ 1;
17416 * "winheight(nr)" function
17419 f_winheight(argvars
, rettv
)
17425 wp
= find_win_by_nr(&argvars
[0], NULL
);
17427 rettv
->vval
.v_number
= -1;
17429 rettv
->vval
.v_number
= wp
->w_height
;
17433 * "winline()" function
17436 f_winline(argvars
, rettv
)
17437 typval_T
*argvars UNUSED
;
17441 rettv
->vval
.v_number
= curwin
->w_wrow
+ 1;
17445 * "winnr()" function
17448 f_winnr(argvars
, rettv
)
17449 typval_T
*argvars UNUSED
;
17454 #ifdef FEAT_WINDOWS
17455 nr
= get_winnr(curtab
, &argvars
[0]);
17457 rettv
->vval
.v_number
= nr
;
17461 * "winrestcmd()" function
17464 f_winrestcmd(argvars
, rettv
)
17465 typval_T
*argvars UNUSED
;
17468 #ifdef FEAT_WINDOWS
17474 ga_init2(&ga
, (int)sizeof(char), 70);
17475 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
17477 sprintf((char *)buf
, "%dresize %d|", winnr
, wp
->w_height
);
17478 ga_concat(&ga
, buf
);
17479 # ifdef FEAT_VERTSPLIT
17480 sprintf((char *)buf
, "vert %dresize %d|", winnr
, wp
->w_width
);
17481 ga_concat(&ga
, buf
);
17485 ga_append(&ga
, NUL
);
17487 rettv
->vval
.v_string
= ga
.ga_data
;
17489 rettv
->vval
.v_string
= NULL
;
17491 rettv
->v_type
= VAR_STRING
;
17495 * "winrestview()" function
17498 f_winrestview(argvars
, rettv
)
17500 typval_T
*rettv UNUSED
;
17504 if (argvars
[0].v_type
!= VAR_DICT
17505 || (dict
= argvars
[0].vval
.v_dict
) == NULL
)
17509 curwin
->w_cursor
.lnum
= get_dict_number(dict
, (char_u
*)"lnum");
17510 curwin
->w_cursor
.col
= get_dict_number(dict
, (char_u
*)"col");
17511 #ifdef FEAT_VIRTUALEDIT
17512 curwin
->w_cursor
.coladd
= get_dict_number(dict
, (char_u
*)"coladd");
17514 curwin
->w_curswant
= get_dict_number(dict
, (char_u
*)"curswant");
17515 curwin
->w_set_curswant
= FALSE
;
17517 set_topline(curwin
, get_dict_number(dict
, (char_u
*)"topline"));
17519 curwin
->w_topfill
= get_dict_number(dict
, (char_u
*)"topfill");
17521 curwin
->w_leftcol
= get_dict_number(dict
, (char_u
*)"leftcol");
17522 curwin
->w_skipcol
= get_dict_number(dict
, (char_u
*)"skipcol");
17525 changed_cline_bef_curs();
17526 invalidate_botline();
17527 redraw_later(VALID
);
17529 if (curwin
->w_topline
== 0)
17530 curwin
->w_topline
= 1;
17531 if (curwin
->w_topline
> curbuf
->b_ml
.ml_line_count
)
17532 curwin
->w_topline
= curbuf
->b_ml
.ml_line_count
;
17534 check_topfill(curwin
, TRUE
);
17540 * "winsaveview()" function
17543 f_winsaveview(argvars
, rettv
)
17544 typval_T
*argvars UNUSED
;
17549 dict
= dict_alloc();
17552 rettv
->v_type
= VAR_DICT
;
17553 rettv
->vval
.v_dict
= dict
;
17554 ++dict
->dv_refcount
;
17556 dict_add_nr_str(dict
, "lnum", (long)curwin
->w_cursor
.lnum
, NULL
);
17557 dict_add_nr_str(dict
, "col", (long)curwin
->w_cursor
.col
, NULL
);
17558 #ifdef FEAT_VIRTUALEDIT
17559 dict_add_nr_str(dict
, "coladd", (long)curwin
->w_cursor
.coladd
, NULL
);
17562 dict_add_nr_str(dict
, "curswant", (long)curwin
->w_curswant
, NULL
);
17564 dict_add_nr_str(dict
, "topline", (long)curwin
->w_topline
, NULL
);
17566 dict_add_nr_str(dict
, "topfill", (long)curwin
->w_topfill
, NULL
);
17568 dict_add_nr_str(dict
, "leftcol", (long)curwin
->w_leftcol
, NULL
);
17569 dict_add_nr_str(dict
, "skipcol", (long)curwin
->w_skipcol
, NULL
);
17573 * "winwidth(nr)" function
17576 f_winwidth(argvars
, rettv
)
17582 wp
= find_win_by_nr(&argvars
[0], NULL
);
17584 rettv
->vval
.v_number
= -1;
17586 #ifdef FEAT_VERTSPLIT
17587 rettv
->vval
.v_number
= wp
->w_width
;
17589 rettv
->vval
.v_number
= Columns
;
17594 * "writefile()" function
17597 f_writefile(argvars
, rettv
)
17601 int binary
= FALSE
;
17609 if (check_restricted() || check_secure())
17612 if (argvars
[0].v_type
!= VAR_LIST
)
17614 EMSG2(_(e_listarg
), "writefile()");
17617 if (argvars
[0].vval
.v_list
== NULL
)
17620 if (argvars
[2].v_type
!= VAR_UNKNOWN
17621 && STRCMP(get_tv_string(&argvars
[2]), "b") == 0)
17624 /* Always open the file in binary mode, library functions have a mind of
17625 * their own about CR-LF conversion. */
17626 fname
= get_tv_string(&argvars
[1]);
17627 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, WRITEBIN
)) == NULL
)
17629 EMSG2(_(e_notcreate
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
17634 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
;
17637 for (s
= get_tv_string(&li
->li_tv
); *s
!= NUL
; ++s
)
17649 if (!binary
|| li
->li_next
!= NULL
)
17650 if (putc('\n', fd
) == EOF
)
17664 rettv
->vval
.v_number
= ret
;
17668 * Translate a String variable into a position.
17669 * Returns NULL when there is an error.
17672 var2fpos(varp
, dollar_lnum
, fnum
)
17674 int dollar_lnum
; /* TRUE when $ is last line */
17675 int *fnum
; /* set to fnum for '0, 'A, etc. */
17681 /* Argument can be [lnum, col, coladd]. */
17682 if (varp
->v_type
== VAR_LIST
)
17689 l
= varp
->vval
.v_list
;
17693 /* Get the line number */
17694 pos
.lnum
= list_find_nr(l
, 0L, &error
);
17695 if (error
|| pos
.lnum
<= 0 || pos
.lnum
> curbuf
->b_ml
.ml_line_count
)
17696 return NULL
; /* invalid line number */
17698 /* Get the column number */
17699 pos
.col
= list_find_nr(l
, 1L, &error
);
17702 len
= (long)STRLEN(ml_get(pos
.lnum
));
17704 /* We accept "$" for the column number: last column. */
17705 li
= list_find(l
, 1L);
17706 if (li
!= NULL
&& li
->li_tv
.v_type
== VAR_STRING
17707 && li
->li_tv
.vval
.v_string
!= NULL
17708 && STRCMP(li
->li_tv
.vval
.v_string
, "$") == 0)
17711 /* Accept a position up to the NUL after the line. */
17712 if (pos
.col
== 0 || (int)pos
.col
> len
+ 1)
17713 return NULL
; /* invalid column number */
17716 #ifdef FEAT_VIRTUALEDIT
17717 /* Get the virtual offset. Defaults to zero. */
17718 pos
.coladd
= list_find_nr(l
, 2L, &error
);
17726 name
= get_tv_string_chk(varp
);
17729 if (name
[0] == '.') /* cursor */
17730 return &curwin
->w_cursor
;
17732 if (name
[0] == 'v' && name
[1] == NUL
) /* Visual start */
17736 return &curwin
->w_cursor
;
17739 if (name
[0] == '\'') /* mark */
17741 pp
= getmark_fnum(name
[1], FALSE
, fnum
);
17742 if (pp
== NULL
|| pp
== (pos_T
*)-1 || pp
->lnum
<= 0)
17747 #ifdef FEAT_VIRTUALEDIT
17751 if (name
[0] == 'w' && dollar_lnum
)
17754 if (name
[1] == '0') /* "w0": first visible line */
17757 pos
.lnum
= curwin
->w_topline
;
17760 else if (name
[1] == '$') /* "w$": last visible line */
17762 validate_botline();
17763 pos
.lnum
= curwin
->w_botline
- 1;
17767 else if (name
[0] == '$') /* last column or line */
17771 pos
.lnum
= curbuf
->b_ml
.ml_line_count
;
17776 pos
.lnum
= curwin
->w_cursor
.lnum
;
17777 pos
.col
= (colnr_T
)STRLEN(ml_get_curline());
17785 * Convert list in "arg" into a position and optional file number.
17786 * When "fnump" is NULL there is no file number, only 3 items.
17787 * Note that the column is passed on as-is, the caller may want to decrement
17788 * it to use 1 for the first column.
17789 * Return FAIL when conversion is not possible, doesn't check the position for
17793 list2fpos(arg
, posp
, fnump
)
17798 list_T
*l
= arg
->vval
.v_list
;
17802 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
17803 * when "fnump" isn't NULL and "coladd" is optional. */
17804 if (arg
->v_type
!= VAR_LIST
17806 || l
->lv_len
< (fnump
== NULL
? 2 : 3)
17807 || l
->lv_len
> (fnump
== NULL
? 3 : 4))
17812 n
= list_find_nr(l
, i
++, NULL
); /* fnum */
17816 n
= curbuf
->b_fnum
; /* current buffer */
17820 n
= list_find_nr(l
, i
++, NULL
); /* lnum */
17825 n
= list_find_nr(l
, i
++, NULL
); /* col */
17830 #ifdef FEAT_VIRTUALEDIT
17831 n
= list_find_nr(l
, i
, NULL
);
17842 * Get the length of an environment variable name.
17843 * Advance "arg" to the first character after the name.
17844 * Return 0 for error.
17853 for (p
= *arg
; vim_isIDc(*p
); ++p
)
17855 if (p
== *arg
) /* no name found */
17858 len
= (int)(p
- *arg
);
17864 * Get the length of the name of a function or internal variable.
17865 * "arg" is advanced to the first non-white character after the name.
17866 * Return 0 if something is wrong.
17875 /* Find the end of the name. */
17876 for (p
= *arg
; eval_isnamec(*p
); ++p
)
17878 if (p
== *arg
) /* no name found */
17881 len
= (int)(p
- *arg
);
17882 *arg
= skipwhite(p
);
17888 * Get the length of the name of a variable or function.
17889 * Only the name is recognized, does not handle ".key" or "[idx]".
17890 * "arg" is advanced to the first non-white character after the name.
17891 * Return -1 if curly braces expansion failed.
17892 * Return 0 if something else is wrong.
17893 * If the name contains 'magic' {}'s, expand them and return the
17894 * expanded name in an allocated string via 'alias' - caller must free.
17897 get_name_len(arg
, alias
, evaluate
, verbose
)
17905 char_u
*expr_start
;
17908 *alias
= NULL
; /* default to no alias */
17910 if ((*arg
)[0] == K_SPECIAL
&& (*arg
)[1] == KS_EXTRA
17911 && (*arg
)[2] == (int)KE_SNR
)
17913 /* hard coded <SNR>, already translated */
17915 return get_id_len(arg
) + 3;
17917 len
= eval_fname_script(*arg
);
17920 /* literal "<SID>", "s:" or "<SNR>" */
17925 * Find the end of the name; check for {} construction.
17927 p
= find_name_end(*arg
, &expr_start
, &expr_end
,
17928 len
> 0 ? 0 : FNE_CHECK_START
);
17929 if (expr_start
!= NULL
)
17931 char_u
*temp_string
;
17935 len
+= (int)(p
- *arg
);
17936 *arg
= skipwhite(p
);
17941 * Include any <SID> etc in the expanded string:
17942 * Thus the -len here.
17944 temp_string
= make_expanded_name(*arg
- len
, expr_start
, expr_end
, p
);
17945 if (temp_string
== NULL
)
17947 *alias
= temp_string
;
17948 *arg
= skipwhite(p
);
17949 return (int)STRLEN(temp_string
);
17952 len
+= get_id_len(arg
);
17953 if (len
== 0 && verbose
)
17954 EMSG2(_(e_invexpr2
), *arg
);
17960 * Find the end of a variable or function name, taking care of magic braces.
17961 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17962 * start and end of the first magic braces item.
17963 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
17964 * Return a pointer to just after the name. Equal to "arg" if there is no
17968 find_name_end(arg
, expr_start
, expr_end
, flags
)
17970 char_u
**expr_start
;
17978 if (expr_start
!= NULL
)
17980 *expr_start
= NULL
;
17984 /* Quick check for valid starting character. */
17985 if ((flags
& FNE_CHECK_START
) && !eval_isnamec1(*arg
) && *arg
!= '{')
17988 for (p
= arg
; *p
!= NUL
17989 && (eval_isnamec(*p
)
17991 || ((flags
& FNE_INCL_BR
) && (*p
== '[' || *p
== '.'))
17993 || br_nest
!= 0); mb_ptr_adv(p
))
17997 /* skip over 'string' to avoid counting [ and ] inside it. */
17998 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '\''; mb_ptr_adv(p
))
18003 else if (*p
== '"')
18005 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
18006 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
18007 if (*p
== '\\' && p
[1] != NUL
)
18017 else if (*p
== ']')
18026 if (expr_start
!= NULL
&& *expr_start
== NULL
)
18029 else if (*p
== '}')
18032 if (expr_start
!= NULL
&& mb_nest
== 0 && *expr_end
== NULL
)
18042 * Expands out the 'magic' {}'s in a variable/function name.
18043 * Note that this can call itself recursively, to deal with
18044 * constructs like foo{bar}{baz}{bam}
18045 * The four pointer arguments point to "foo{expre}ss{ion}bar"
18051 * Returns a new allocated string, which the caller must free.
18052 * Returns NULL for failure.
18055 make_expanded_name(in_start
, expr_start
, expr_end
, in_end
)
18057 char_u
*expr_start
;
18062 char_u
*retval
= NULL
;
18063 char_u
*temp_result
;
18064 char_u
*nextcmd
= NULL
;
18066 if (expr_end
== NULL
|| in_end
== NULL
)
18073 temp_result
= eval_to_string(expr_start
+ 1, &nextcmd
, FALSE
);
18074 if (temp_result
!= NULL
&& nextcmd
== NULL
)
18076 retval
= alloc((unsigned)(STRLEN(temp_result
) + (expr_start
- in_start
)
18077 + (in_end
- expr_end
) + 1));
18078 if (retval
!= NULL
)
18080 STRCPY(retval
, in_start
);
18081 STRCAT(retval
, temp_result
);
18082 STRCAT(retval
, expr_end
+ 1);
18085 vim_free(temp_result
);
18087 *in_end
= c1
; /* put char back for error messages */
18091 if (retval
!= NULL
)
18093 temp_result
= find_name_end(retval
, &expr_start
, &expr_end
, 0);
18094 if (expr_start
!= NULL
)
18096 /* Further expansion! */
18097 temp_result
= make_expanded_name(retval
, expr_start
,
18098 expr_end
, temp_result
);
18100 retval
= temp_result
;
18108 * Return TRUE if character "c" can be used in a variable or function name.
18109 * Does not include '{' or '}' for magic braces.
18115 return (ASCII_ISALNUM(c
) || c
== '_' || c
== ':' || c
== AUTOLOAD_CHAR
);
18119 * Return TRUE if character "c" can be used as the first character in a
18120 * variable or function name (excluding '{' and '}').
18126 return (ASCII_ISALPHA(c
) || c
== '_');
18130 * Set number v: variable to "val".
18133 set_vim_var_nr(idx
, val
)
18137 vimvars
[idx
].vv_nr
= val
;
18141 * Get number v: variable value.
18144 get_vim_var_nr(idx
)
18147 return vimvars
[idx
].vv_nr
;
18151 * Get string v: variable value. Uses a static buffer, can only be used once.
18154 get_vim_var_str(idx
)
18157 return get_tv_string(&vimvars
[idx
].vv_tv
);
18161 * Get List v: variable value. Caller must take care of reference count when
18165 get_vim_var_list(idx
)
18168 return vimvars
[idx
].vv_list
;
18172 * Set v:char to character "c".
18175 set_vim_var_char(c
)
18179 char_u buf
[MB_MAXBYTES
];
18186 buf
[(*mb_char2bytes
)(c
, buf
)] = NUL
;
18193 set_vim_var_string(VV_CHAR
, buf
, -1);
18197 * Set v:count to "count" and v:count1 to "count1".
18198 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
18201 set_vcount(count
, count1
, set_prevcount
)
18207 vimvars
[VV_PREVCOUNT
].vv_nr
= vimvars
[VV_COUNT
].vv_nr
;
18208 vimvars
[VV_COUNT
].vv_nr
= count
;
18209 vimvars
[VV_COUNT1
].vv_nr
= count1
;
18213 * Set string v: variable to a copy of "val".
18216 set_vim_var_string(idx
, val
, len
)
18219 int len
; /* length of "val" to use or -1 (whole string) */
18221 /* Need to do this (at least) once, since we can't initialize a union.
18222 * Will always be invoked when "v:progname" is set. */
18223 vimvars
[VV_VERSION
].vv_nr
= VIM_VERSION_100
;
18225 vim_free(vimvars
[idx
].vv_str
);
18227 vimvars
[idx
].vv_str
= NULL
;
18228 else if (len
== -1)
18229 vimvars
[idx
].vv_str
= vim_strsave(val
);
18231 vimvars
[idx
].vv_str
= vim_strnsave(val
, len
);
18235 * Set List v: variable to "val".
18238 set_vim_var_list(idx
, val
)
18242 list_unref(vimvars
[idx
].vv_list
);
18243 vimvars
[idx
].vv_list
= val
;
18245 ++val
->lv_refcount
;
18249 * Set v:register if needed.
18257 if (c
== 0 || c
== ' ')
18261 /* Avoid free/alloc when the value is already right. */
18262 if (vimvars
[VV_REG
].vv_str
== NULL
|| vimvars
[VV_REG
].vv_str
[0] != c
)
18263 set_vim_var_string(VV_REG
, ®name
, 1);
18267 * Get or set v:exception. If "oldval" == NULL, return the current value.
18268 * Otherwise, restore the value to "oldval" and return NULL.
18269 * Must always be called in pairs to save and restore v:exception! Does not
18270 * take care of memory allocations.
18273 v_exception(oldval
)
18276 if (oldval
== NULL
)
18277 return vimvars
[VV_EXCEPTION
].vv_str
;
18279 vimvars
[VV_EXCEPTION
].vv_str
= oldval
;
18284 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
18285 * Otherwise, restore the value to "oldval" and return NULL.
18286 * Must always be called in pairs to save and restore v:throwpoint! Does not
18287 * take care of memory allocations.
18290 v_throwpoint(oldval
)
18293 if (oldval
== NULL
)
18294 return vimvars
[VV_THROWPOINT
].vv_str
;
18296 vimvars
[VV_THROWPOINT
].vv_str
= oldval
;
18300 #if defined(FEAT_AUTOCMD) || defined(PROTO)
18303 * If "eap" != NULL, use "eap" to generate the value and return the old value.
18304 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
18305 * Must always be called in pairs!
18308 set_cmdarg(eap
, oldarg
)
18316 oldval
= vimvars
[VV_CMDARG
].vv_str
;
18320 vimvars
[VV_CMDARG
].vv_str
= oldarg
;
18324 if (eap
->force_bin
== FORCE_BIN
)
18326 else if (eap
->force_bin
== FORCE_NOBIN
)
18331 if (eap
->read_edit
)
18334 if (eap
->force_ff
!= 0)
18335 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_ff
) + 6;
18337 if (eap
->force_enc
!= 0)
18338 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_enc
) + 7;
18339 if (eap
->bad_char
!= 0)
18340 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->bad_char
) + 7;
18343 newval
= alloc(len
+ 1);
18344 if (newval
== NULL
)
18347 if (eap
->force_bin
== FORCE_BIN
)
18348 sprintf((char *)newval
, " ++bin");
18349 else if (eap
->force_bin
== FORCE_NOBIN
)
18350 sprintf((char *)newval
, " ++nobin");
18354 if (eap
->read_edit
)
18355 STRCAT(newval
, " ++edit");
18357 if (eap
->force_ff
!= 0)
18358 sprintf((char *)newval
+ STRLEN(newval
), " ++ff=%s",
18359 eap
->cmd
+ eap
->force_ff
);
18361 if (eap
->force_enc
!= 0)
18362 sprintf((char *)newval
+ STRLEN(newval
), " ++enc=%s",
18363 eap
->cmd
+ eap
->force_enc
);
18364 if (eap
->bad_char
!= 0)
18365 sprintf((char *)newval
+ STRLEN(newval
), " ++bad=%s",
18366 eap
->cmd
+ eap
->bad_char
);
18368 vimvars
[VV_CMDARG
].vv_str
= newval
;
18374 * Get the value of internal variable "name".
18375 * Return OK or FAIL.
18378 get_var_tv(name
, len
, rettv
, verbose
)
18380 int len
; /* length of "name" */
18381 typval_T
*rettv
; /* NULL when only checking existence */
18382 int verbose
; /* may give error message */
18385 typval_T
*tv
= NULL
;
18390 /* truncate the name, so that we can use strcmp() */
18395 * Check for "b:changedtick".
18397 if (STRCMP(name
, "b:changedtick") == 0)
18399 atv
.v_type
= VAR_NUMBER
;
18400 atv
.vval
.v_number
= curbuf
->b_changedtick
;
18405 * Check for user-defined variables.
18409 v
= find_var(name
, NULL
);
18416 if (rettv
!= NULL
&& verbose
)
18417 EMSG2(_(e_undefvar
), name
);
18420 else if (rettv
!= NULL
)
18421 copy_tv(tv
, rettv
);
18429 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
18430 * Also handle function call with Funcref variable: func(expr)
18431 * Can all be combined: dict.func(expr)[idx]['func'](expr)
18434 handle_subscript(arg
, rettv
, evaluate
, verbose
)
18437 int evaluate
; /* do more than finding the end */
18438 int verbose
; /* give error messages */
18441 dict_T
*selfdict
= NULL
;
18448 || (**arg
== '.' && rettv
->v_type
== VAR_DICT
)
18449 || (**arg
== '(' && rettv
->v_type
== VAR_FUNC
))
18450 && !vim_iswhite(*(*arg
- 1)))
18454 /* need to copy the funcref so that we can clear rettv */
18456 rettv
->v_type
= VAR_UNKNOWN
;
18458 /* Invoke the function. Recursive! */
18459 s
= functv
.vval
.v_string
;
18460 ret
= get_func_tv(s
, (int)STRLEN(s
), rettv
, arg
,
18461 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
18462 &len
, evaluate
, selfdict
);
18464 /* Clear the funcref afterwards, so that deleting it while
18465 * evaluating the arguments is possible (see test55). */
18468 /* Stop the expression evaluation when immediately aborting on
18469 * error, or when an interrupt occurred or an exception was thrown
18470 * but not caught. */
18477 dict_unref(selfdict
);
18480 else /* **arg == '[' || **arg == '.' */
18482 dict_unref(selfdict
);
18483 if (rettv
->v_type
== VAR_DICT
)
18485 selfdict
= rettv
->vval
.v_dict
;
18486 if (selfdict
!= NULL
)
18487 ++selfdict
->dv_refcount
;
18491 if (eval_index(arg
, rettv
, evaluate
, verbose
) == FAIL
)
18498 dict_unref(selfdict
);
18503 * Allocate memory for a variable type-value, and make it empty (0 or NULL
18509 return (typval_T
*)alloc_clear((unsigned)sizeof(typval_T
));
18513 * Allocate memory for a variable type-value, and assign a string to it.
18514 * The string "s" must have been allocated, it is consumed.
18515 * Return NULL for out of memory, the variable otherwise.
18523 rettv
= alloc_tv();
18526 rettv
->v_type
= VAR_STRING
;
18527 rettv
->vval
.v_string
= s
;
18535 * Free the memory for a variable type-value.
18543 switch (varp
->v_type
)
18546 func_unref(varp
->vval
.v_string
);
18549 vim_free(varp
->vval
.v_string
);
18552 list_unref(varp
->vval
.v_list
);
18555 dict_unref(varp
->vval
.v_dict
);
18564 EMSG2(_(e_intern2
), "free_tv()");
18572 * Free the memory for a variable value and set the value to NULL or 0.
18580 switch (varp
->v_type
)
18583 func_unref(varp
->vval
.v_string
);
18586 vim_free(varp
->vval
.v_string
);
18587 varp
->vval
.v_string
= NULL
;
18590 list_unref(varp
->vval
.v_list
);
18591 varp
->vval
.v_list
= NULL
;
18594 dict_unref(varp
->vval
.v_dict
);
18595 varp
->vval
.v_dict
= NULL
;
18598 varp
->vval
.v_number
= 0;
18602 varp
->vval
.v_float
= 0.0;
18608 EMSG2(_(e_intern2
), "clear_tv()");
18615 * Set the value of a variable to NULL without freeing items.
18622 vim_memset(varp
, 0, sizeof(typval_T
));
18626 * Get the number value of a variable.
18627 * If it is a String variable, uses vim_str2nr().
18628 * For incompatible types, return 0.
18629 * get_tv_number_chk() is similar to get_tv_number(), but informs the
18630 * caller of incompatible types: it sets *denote to TRUE if "denote"
18631 * is not NULL or returns -1 otherwise.
18634 get_tv_number(varp
)
18639 return get_tv_number_chk(varp
, &error
); /* return 0L on error */
18643 get_tv_number_chk(varp
, denote
)
18649 switch (varp
->v_type
)
18652 return (long)(varp
->vval
.v_number
);
18655 EMSG(_("E805: Using a Float as a Number"));
18659 EMSG(_("E703: Using a Funcref as a Number"));
18662 if (varp
->vval
.v_string
!= NULL
)
18663 vim_str2nr(varp
->vval
.v_string
, NULL
, NULL
,
18664 TRUE
, TRUE
, &n
, NULL
);
18667 EMSG(_("E745: Using a List as a Number"));
18670 EMSG(_("E728: Using a Dictionary as a Number"));
18673 EMSG2(_(e_intern2
), "get_tv_number()");
18676 if (denote
== NULL
) /* useful for values that must be unsigned */
18684 * Get the lnum from the first argument.
18685 * Also accepts ".", "$", etc., but that only works for the current buffer.
18686 * Returns -1 on error.
18689 get_tv_lnum(argvars
)
18695 lnum
= get_tv_number_chk(&argvars
[0], NULL
);
18696 if (lnum
== 0) /* no valid number, try using line() */
18698 rettv
.v_type
= VAR_NUMBER
;
18699 f_line(argvars
, &rettv
);
18700 lnum
= rettv
.vval
.v_number
;
18707 * Get the lnum from the first argument.
18708 * Also accepts "$", then "buf" is used.
18709 * Returns 0 on error.
18712 get_tv_lnum_buf(argvars
, buf
)
18716 if (argvars
[0].v_type
== VAR_STRING
18717 && argvars
[0].vval
.v_string
!= NULL
18718 && argvars
[0].vval
.v_string
[0] == '$'
18720 return buf
->b_ml
.ml_line_count
;
18721 return get_tv_number_chk(&argvars
[0], NULL
);
18725 * Get the string value of a variable.
18726 * If it is a Number variable, the number is converted into a string.
18727 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
18728 * get_tv_string_buf() uses a given buffer.
18729 * If the String variable has never been set, return an empty string.
18730 * Never returns NULL;
18731 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
18735 get_tv_string(varp
)
18738 static char_u mybuf
[NUMBUFLEN
];
18740 return get_tv_string_buf(varp
, mybuf
);
18744 get_tv_string_buf(varp
, buf
)
18748 char_u
*res
= get_tv_string_buf_chk(varp
, buf
);
18750 return res
!= NULL
? res
: (char_u
*)"";
18754 get_tv_string_chk(varp
)
18757 static char_u mybuf
[NUMBUFLEN
];
18759 return get_tv_string_buf_chk(varp
, mybuf
);
18763 get_tv_string_buf_chk(varp
, buf
)
18767 switch (varp
->v_type
)
18770 sprintf((char *)buf
, "%ld", (long)varp
->vval
.v_number
);
18773 EMSG(_("E729: using Funcref as a String"));
18776 EMSG(_("E730: using List as a String"));
18779 EMSG(_("E731: using Dictionary as a String"));
18783 EMSG(_("E806: using Float as a String"));
18787 if (varp
->vval
.v_string
!= NULL
)
18788 return varp
->vval
.v_string
;
18789 return (char_u
*)"";
18791 EMSG2(_(e_intern2
), "get_tv_string_buf()");
18798 * Find variable "name" in the list of variables.
18799 * Return a pointer to it if found, NULL if not found.
18800 * Careful: "a:0" variables don't have a name.
18801 * When "htp" is not NULL we are writing to the variable, set "htp" to the
18804 static dictitem_T
*
18805 find_var(name
, htp
)
18812 ht
= find_var_ht(name
, &varname
);
18817 return find_var_in_ht(ht
, varname
, htp
!= NULL
);
18821 * Find variable "varname" in hashtab "ht".
18822 * Returns NULL if not found.
18824 static dictitem_T
*
18825 find_var_in_ht(ht
, varname
, writing
)
18832 if (*varname
== NUL
)
18834 /* Must be something like "s:", otherwise "ht" would be NULL. */
18835 switch (varname
[-2])
18837 case 's': return &SCRIPT_SV(current_SID
).sv_var
;
18838 case 'g': return &globvars_var
;
18839 case 'v': return &vimvars_var
;
18840 case 'b': return &curbuf
->b_bufvar
;
18841 case 'w': return &curwin
->w_winvar
;
18842 #ifdef FEAT_WINDOWS
18843 case 't': return &curtab
->tp_winvar
;
18845 case 'l': return current_funccal
== NULL
18846 ? NULL
: ¤t_funccal
->l_vars_var
;
18847 case 'a': return current_funccal
== NULL
18848 ? NULL
: ¤t_funccal
->l_avars_var
;
18853 hi
= hash_find(ht
, varname
);
18854 if (HASHITEM_EMPTY(hi
))
18856 /* For global variables we may try auto-loading the script. If it
18857 * worked find the variable again. Don't auto-load a script if it was
18858 * loaded already, otherwise it would be loaded every time when
18859 * checking if a function name is a Funcref variable. */
18860 if (ht
== &globvarht
&& !writing
18861 && script_autoload(varname
, FALSE
) && !aborting())
18862 hi
= hash_find(ht
, varname
);
18863 if (HASHITEM_EMPTY(hi
))
18870 * Find the hashtab used for a variable name.
18871 * Set "varname" to the start of name without ':'.
18874 find_var_ht(name
, varname
)
18880 if (name
[1] != ':')
18882 /* The name must not start with a colon or #. */
18883 if (name
[0] == ':' || name
[0] == AUTOLOAD_CHAR
)
18887 /* "version" is "v:version" in all scopes */
18888 hi
= hash_find(&compat_hashtab
, name
);
18889 if (!HASHITEM_EMPTY(hi
))
18890 return &compat_hashtab
;
18892 if (current_funccal
== NULL
)
18893 return &globvarht
; /* global variable */
18894 return ¤t_funccal
->l_vars
.dv_hashtab
; /* l: variable */
18896 *varname
= name
+ 2;
18897 if (*name
== 'g') /* global variable */
18899 /* There must be no ':' or '#' in the rest of the name, unless g: is used
18901 if (vim_strchr(name
+ 2, ':') != NULL
18902 || vim_strchr(name
+ 2, AUTOLOAD_CHAR
) != NULL
)
18904 if (*name
== 'b') /* buffer variable */
18905 return &curbuf
->b_vars
.dv_hashtab
;
18906 if (*name
== 'w') /* window variable */
18907 return &curwin
->w_vars
.dv_hashtab
;
18908 #ifdef FEAT_WINDOWS
18909 if (*name
== 't') /* tab page variable */
18910 return &curtab
->tp_vars
.dv_hashtab
;
18912 if (*name
== 'v') /* v: variable */
18914 if (*name
== 'a' && current_funccal
!= NULL
) /* function argument */
18915 return ¤t_funccal
->l_avars
.dv_hashtab
;
18916 if (*name
== 'l' && current_funccal
!= NULL
) /* local function variable */
18917 return ¤t_funccal
->l_vars
.dv_hashtab
;
18918 if (*name
== 's' /* script variable */
18919 && current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
18920 return &SCRIPT_VARS(current_SID
);
18925 * Get the string value of a (global/local) variable.
18926 * Returns NULL when it doesn't exist.
18929 get_var_value(name
)
18934 v
= find_var(name
, NULL
);
18937 return get_tv_string(&v
->di_tv
);
18941 * Allocate a new hashtab for a sourced script. It will be used while
18942 * sourcing this script and when executing functions defined in the script.
18945 new_script_vars(id
)
18952 if (ga_grow(&ga_scripts
, (int)(id
- ga_scripts
.ga_len
)) == OK
)
18954 /* Re-allocating ga_data means that an ht_array pointing to
18955 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
18956 * at its init value. Also reset "v_dict", it's always the same. */
18957 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
18959 ht
= &SCRIPT_VARS(i
);
18960 if (ht
->ht_mask
== HT_INIT_SIZE
- 1)
18961 ht
->ht_array
= ht
->ht_smallarray
;
18962 sv
= &SCRIPT_SV(i
);
18963 sv
->sv_var
.di_tv
.vval
.v_dict
= &sv
->sv_dict
;
18966 while (ga_scripts
.ga_len
< id
)
18968 sv
= &SCRIPT_SV(ga_scripts
.ga_len
+ 1);
18969 init_var_dict(&sv
->sv_dict
, &sv
->sv_var
);
18970 ++ga_scripts
.ga_len
;
18976 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18980 init_var_dict(dict
, dict_var
)
18982 dictitem_T
*dict_var
;
18984 hash_init(&dict
->dv_hashtab
);
18985 dict
->dv_refcount
= DO_NOT_FREE_CNT
;
18986 dict
->dv_copyID
= 0;
18987 dict_var
->di_tv
.vval
.v_dict
= dict
;
18988 dict_var
->di_tv
.v_type
= VAR_DICT
;
18989 dict_var
->di_tv
.v_lock
= VAR_FIXED
;
18990 dict_var
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
18991 dict_var
->di_key
[0] = NUL
;
18995 * Clean up a list of internal variables.
18996 * Frees all allocated variables and the value they contain.
18997 * Clears hashtab "ht", does not free it.
19003 vars_clear_ext(ht
, TRUE
);
19007 * Like vars_clear(), but only free the value if "free_val" is TRUE.
19010 vars_clear_ext(ht
, free_val
)
19019 todo
= (int)ht
->ht_used
;
19020 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
19022 if (!HASHITEM_EMPTY(hi
))
19026 /* Free the variable. Don't remove it from the hashtab,
19027 * ht_array might change then. hash_clear() takes care of it
19031 clear_tv(&v
->di_tv
);
19032 if ((v
->di_flags
& DI_FLAGS_FIX
) == 0)
19041 * Delete a variable from hashtab "ht" at item "hi".
19042 * Clear the variable value and free the dictitem.
19049 dictitem_T
*di
= HI2DI(hi
);
19051 hash_remove(ht
, hi
);
19052 clear_tv(&di
->di_tv
);
19057 * List the value of one internal variable.
19060 list_one_var(v
, prefix
, first
)
19067 char_u numbuf
[NUMBUFLEN
];
19069 current_copyID
+= COPYID_INC
;
19070 s
= echo_string(&v
->di_tv
, &tofree
, numbuf
, current_copyID
);
19071 list_one_var_a(prefix
, v
->di_key
, v
->di_tv
.v_type
,
19072 s
== NULL
? (char_u
*)"" : s
, first
);
19077 list_one_var_a(prefix
, name
, type
, string
, first
)
19082 int *first
; /* when TRUE clear rest of screen and set to FALSE */
19084 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
19087 if (name
!= NULL
) /* "a:" vars don't have a name stored */
19091 if (type
== VAR_NUMBER
)
19093 else if (type
== VAR_FUNC
)
19095 else if (type
== VAR_LIST
)
19098 if (*string
== '[')
19101 else if (type
== VAR_DICT
)
19104 if (*string
== '{')
19110 msg_outtrans(string
);
19112 if (type
== VAR_FUNC
)
19113 msg_puts((char_u
*)"()");
19122 * Set variable "name" to value in "tv".
19123 * If the variable already exists, the value is updated.
19124 * Otherwise the variable is created.
19127 set_var(name
, tv
, copy
)
19130 int copy
; /* make copy of value in "tv" */
19137 ht
= find_var_ht(name
, &varname
);
19138 if (ht
== NULL
|| *varname
== NUL
)
19140 EMSG2(_(e_illvar
), name
);
19143 v
= find_var_in_ht(ht
, varname
, TRUE
);
19145 if (tv
->v_type
== VAR_FUNC
)
19147 if (!(vim_strchr((char_u
*)"wbs", name
[0]) != NULL
&& name
[1] == ':')
19148 && !ASCII_ISUPPER((name
[0] != NUL
&& name
[1] == ':')
19149 ? name
[2] : name
[0]))
19151 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name
);
19154 /* Don't allow hiding a function. When "v" is not NULL we migth be
19155 * assigning another function to the same var, the type is checked
19157 if (v
== NULL
&& function_exists(name
))
19159 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
19167 /* existing variable, need to clear the value */
19168 if (var_check_ro(v
->di_flags
, name
)
19169 || tv_check_lock(v
->di_tv
.v_lock
, name
))
19171 if (v
->di_tv
.v_type
!= tv
->v_type
19172 && !((v
->di_tv
.v_type
== VAR_STRING
19173 || v
->di_tv
.v_type
== VAR_NUMBER
)
19174 && (tv
->v_type
== VAR_STRING
19175 || tv
->v_type
== VAR_NUMBER
))
19177 && !((v
->di_tv
.v_type
== VAR_NUMBER
19178 || v
->di_tv
.v_type
== VAR_FLOAT
)
19179 && (tv
->v_type
== VAR_NUMBER
19180 || tv
->v_type
== VAR_FLOAT
))
19184 EMSG2(_("E706: Variable type mismatch for: %s"), name
);
19189 * Handle setting internal v: variables separately: we don't change
19192 if (ht
== &vimvarht
)
19194 if (v
->di_tv
.v_type
== VAR_STRING
)
19196 vim_free(v
->di_tv
.vval
.v_string
);
19197 if (copy
|| tv
->v_type
!= VAR_STRING
)
19198 v
->di_tv
.vval
.v_string
= vim_strsave(get_tv_string(tv
));
19201 /* Take over the string to avoid an extra alloc/free. */
19202 v
->di_tv
.vval
.v_string
= tv
->vval
.v_string
;
19203 tv
->vval
.v_string
= NULL
;
19206 else if (v
->di_tv
.v_type
!= VAR_NUMBER
)
19207 EMSG2(_(e_intern2
), "set_var()");
19210 v
->di_tv
.vval
.v_number
= get_tv_number(tv
);
19211 if (STRCMP(varname
, "searchforward") == 0)
19212 set_search_direction(v
->di_tv
.vval
.v_number
? '/' : '?');
19217 clear_tv(&v
->di_tv
);
19219 else /* add a new variable */
19221 /* Can't add "v:" variable. */
19222 if (ht
== &vimvarht
)
19224 EMSG2(_(e_illvar
), name
);
19228 /* Make sure the variable name is valid. */
19229 for (p
= varname
; *p
!= NUL
; ++p
)
19230 if (!eval_isnamec1(*p
) && (p
== varname
|| !VIM_ISDIGIT(*p
))
19231 && *p
!= AUTOLOAD_CHAR
)
19233 EMSG2(_(e_illvar
), varname
);
19237 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
19238 + STRLEN(varname
)));
19241 STRCPY(v
->di_key
, varname
);
19242 if (hash_add(ht
, DI2HIKEY(v
)) == FAIL
)
19250 if (copy
|| tv
->v_type
== VAR_NUMBER
|| tv
->v_type
== VAR_FLOAT
)
19251 copy_tv(tv
, &v
->di_tv
);
19255 v
->di_tv
.v_lock
= 0;
19261 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
19262 * Also give an error message.
19265 var_check_ro(flags
, name
)
19269 if (flags
& DI_FLAGS_RO
)
19271 EMSG2(_(e_readonlyvar
), name
);
19274 if ((flags
& DI_FLAGS_RO_SBX
) && sandbox
)
19276 EMSG2(_(e_readonlysbx
), name
);
19283 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
19284 * Also give an error message.
19287 var_check_fixed(flags
, name
)
19291 if (flags
& DI_FLAGS_FIX
)
19293 EMSG2(_("E795: Cannot delete variable %s"), name
);
19300 * Return TRUE if typeval "tv" is set to be locked (immutable).
19301 * Also give an error message, using "name".
19304 tv_check_lock(lock
, name
)
19308 if (lock
& VAR_LOCKED
)
19310 EMSG2(_("E741: Value is locked: %s"),
19311 name
== NULL
? (char_u
*)_("Unknown") : name
);
19314 if (lock
& VAR_FIXED
)
19316 EMSG2(_("E742: Cannot change value of %s"),
19317 name
== NULL
? (char_u
*)_("Unknown") : name
);
19324 * Copy the values from typval_T "from" to typval_T "to".
19325 * When needed allocates string or increases reference count.
19326 * Does not make a copy of a list or dict but copies the reference!
19327 * It is OK for "from" and "to" to point to the same item. This is used to
19328 * make a copy later.
19335 to
->v_type
= from
->v_type
;
19337 switch (from
->v_type
)
19340 to
->vval
.v_number
= from
->vval
.v_number
;
19344 to
->vval
.v_float
= from
->vval
.v_float
;
19349 if (from
->vval
.v_string
== NULL
)
19350 to
->vval
.v_string
= NULL
;
19353 to
->vval
.v_string
= vim_strsave(from
->vval
.v_string
);
19354 if (from
->v_type
== VAR_FUNC
)
19355 func_ref(to
->vval
.v_string
);
19359 if (from
->vval
.v_list
== NULL
)
19360 to
->vval
.v_list
= NULL
;
19363 to
->vval
.v_list
= from
->vval
.v_list
;
19364 ++to
->vval
.v_list
->lv_refcount
;
19368 if (from
->vval
.v_dict
== NULL
)
19369 to
->vval
.v_dict
= NULL
;
19372 to
->vval
.v_dict
= from
->vval
.v_dict
;
19373 ++to
->vval
.v_dict
->dv_refcount
;
19377 EMSG2(_(e_intern2
), "copy_tv()");
19383 * Make a copy of an item.
19384 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
19385 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
19386 * reference to an already copied list/dict can be used.
19387 * Returns FAIL or OK.
19390 item_copy(from
, to
, deep
, copyID
)
19396 static int recurse
= 0;
19399 if (recurse
>= DICT_MAXNEST
)
19401 EMSG(_("E698: variable nested too deep for making a copy"));
19406 switch (from
->v_type
)
19417 to
->v_type
= VAR_LIST
;
19419 if (from
->vval
.v_list
== NULL
)
19420 to
->vval
.v_list
= NULL
;
19421 else if (copyID
!= 0 && from
->vval
.v_list
->lv_copyID
== copyID
)
19423 /* use the copy made earlier */
19424 to
->vval
.v_list
= from
->vval
.v_list
->lv_copylist
;
19425 ++to
->vval
.v_list
->lv_refcount
;
19428 to
->vval
.v_list
= list_copy(from
->vval
.v_list
, deep
, copyID
);
19429 if (to
->vval
.v_list
== NULL
)
19433 to
->v_type
= VAR_DICT
;
19435 if (from
->vval
.v_dict
== NULL
)
19436 to
->vval
.v_dict
= NULL
;
19437 else if (copyID
!= 0 && from
->vval
.v_dict
->dv_copyID
== copyID
)
19439 /* use the copy made earlier */
19440 to
->vval
.v_dict
= from
->vval
.v_dict
->dv_copydict
;
19441 ++to
->vval
.v_dict
->dv_refcount
;
19444 to
->vval
.v_dict
= dict_copy(from
->vval
.v_dict
, deep
, copyID
);
19445 if (to
->vval
.v_dict
== NULL
)
19449 EMSG2(_(e_intern2
), "item_copy()");
19457 * ":echo expr1 ..." print each argument separated with a space, add a
19458 * newline at the end.
19459 * ":echon expr1 ..." print each argument plain.
19465 char_u
*arg
= eap
->arg
;
19469 int needclr
= TRUE
;
19470 int atstart
= TRUE
;
19471 char_u numbuf
[NUMBUFLEN
];
19475 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n' && !got_int
)
19477 /* If eval1() causes an error message the text from the command may
19478 * still need to be cleared. E.g., "echo 22,44". */
19479 need_clr_eos
= needclr
;
19482 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
19485 * Report the invalid expression unless the expression evaluation
19486 * has been cancelled due to an aborting error, an interrupt, or an
19490 EMSG2(_(e_invexpr2
), p
);
19491 need_clr_eos
= FALSE
;
19494 need_clr_eos
= FALSE
;
19501 /* Call msg_start() after eval1(), evaluating the expression
19502 * may cause a message to appear. */
19503 if (eap
->cmdidx
== CMD_echo
)
19506 else if (eap
->cmdidx
== CMD_echo
)
19507 msg_puts_attr((char_u
*)" ", echo_attr
);
19508 current_copyID
+= COPYID_INC
;
19509 p
= echo_string(&rettv
, &tofree
, numbuf
, current_copyID
);
19511 for ( ; *p
!= NUL
&& !got_int
; ++p
)
19513 if (*p
== '\n' || *p
== '\r' || *p
== TAB
)
19515 if (*p
!= TAB
&& needclr
)
19517 /* remove any text still there from the command */
19521 msg_putchar_attr(*p
, echo_attr
);
19528 int i
= (*mb_ptr2len
)(p
);
19530 (void)msg_outtrans_len_attr(p
, i
, echo_attr
);
19535 (void)msg_outtrans_len_attr(p
, 1, echo_attr
);
19541 arg
= skipwhite(arg
);
19543 eap
->nextcmd
= check_nextcmd(arg
);
19549 /* remove text that may still be there from the command */
19552 if (eap
->cmdidx
== CMD_echo
)
19558 * ":echohl {name}".
19566 id
= syn_name2id(eap
->arg
);
19570 echo_attr
= syn_id2attr(id
);
19574 * ":execute expr1 ..." execute the result of an expression.
19575 * ":echomsg expr1 ..." Print a message
19576 * ":echoerr expr1 ..." Print an error
19577 * Each gets spaces around each argument and a newline at the end for
19584 char_u
*arg
= eap
->arg
;
19592 ga_init2(&ga
, 1, 80);
19596 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
19599 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
19602 * Report the invalid expression unless the expression evaluation
19603 * has been cancelled due to an aborting error, an interrupt, or an
19607 EMSG2(_(e_invexpr2
), p
);
19614 p
= get_tv_string(&rettv
);
19615 len
= (int)STRLEN(p
);
19616 if (ga_grow(&ga
, len
+ 2) == FAIL
)
19623 ((char_u
*)(ga
.ga_data
))[ga
.ga_len
++] = ' ';
19624 STRCPY((char_u
*)(ga
.ga_data
) + ga
.ga_len
, p
);
19629 arg
= skipwhite(arg
);
19632 if (ret
!= FAIL
&& ga
.ga_data
!= NULL
)
19634 if (eap
->cmdidx
== CMD_echomsg
)
19636 MSG_ATTR(ga
.ga_data
, echo_attr
);
19639 else if (eap
->cmdidx
== CMD_echoerr
)
19641 /* We don't want to abort following commands, restore did_emsg. */
19642 save_did_emsg
= did_emsg
;
19643 EMSG((char_u
*)ga
.ga_data
);
19645 did_emsg
= save_did_emsg
;
19647 else if (eap
->cmdidx
== CMD_execute
)
19648 do_cmdline((char_u
*)ga
.ga_data
,
19649 eap
->getline
, eap
->cookie
, DOCMD_NOWAIT
|DOCMD_VERBOSE
);
19657 eap
->nextcmd
= check_nextcmd(arg
);
19661 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
19662 * "arg" points to the "&" or '+' when called, to "option" when returning.
19663 * Returns NULL when no option name found. Otherwise pointer to the char
19664 * after the option name.
19667 find_option_end(arg
, opt_flags
)
19674 if (*p
== 'g' && p
[1] == ':')
19676 *opt_flags
= OPT_GLOBAL
;
19679 else if (*p
== 'l' && p
[1] == ':')
19681 *opt_flags
= OPT_LOCAL
;
19687 if (!ASCII_ISALPHA(*p
))
19691 if (p
[0] == 't' && p
[1] == '_' && p
[2] != NUL
&& p
[3] != NUL
)
19692 p
+= 4; /* termcap option */
19694 while (ASCII_ISALPHA(*p
))
19709 int saved_did_emsg
;
19710 char_u
*name
= NULL
;
19713 char_u
*line_arg
= NULL
;
19716 int varargs
= FALSE
;
19717 int mustend
= FALSE
;
19722 char_u
*skip_until
= NULL
;
19725 static int func_nr
= 0; /* number for nameless function */
19730 int sourcing_lnum_off
;
19733 * ":function" without argument: list functions.
19735 if (ends_excmd(*eap
->arg
))
19739 todo
= (int)func_hashtab
.ht_used
;
19740 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
19742 if (!HASHITEM_EMPTY(hi
))
19746 if (!isdigit(*fp
->uf_name
))
19747 list_func_head(fp
, FALSE
);
19751 eap
->nextcmd
= check_nextcmd(eap
->arg
);
19756 * ":function /pat": list functions matching pattern.
19758 if (*eap
->arg
== '/')
19760 p
= skip_regexp(eap
->arg
+ 1, '/', TRUE
, NULL
);
19763 regmatch_T regmatch
;
19767 regmatch
.regprog
= vim_regcomp(eap
->arg
+ 1, RE_MAGIC
);
19769 if (regmatch
.regprog
!= NULL
)
19771 regmatch
.rm_ic
= p_ic
;
19773 todo
= (int)func_hashtab
.ht_used
;
19774 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
19776 if (!HASHITEM_EMPTY(hi
))
19780 if (!isdigit(*fp
->uf_name
)
19781 && vim_regexec(®match
, fp
->uf_name
, 0))
19782 list_func_head(fp
, FALSE
);
19785 vim_free(regmatch
.regprog
);
19790 eap
->nextcmd
= check_nextcmd(p
);
19795 * Get the function name. There are these situations:
19796 * func normal function name
19797 * "name" == func, "fudi.fd_dict" == NULL
19798 * dict.func new dictionary entry
19799 * "name" == NULL, "fudi.fd_dict" set,
19800 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
19801 * dict.func existing dict entry with a Funcref
19802 * "name" == func, "fudi.fd_dict" set,
19803 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19804 * dict.func existing dict entry that's not a Funcref
19805 * "name" == NULL, "fudi.fd_dict" set,
19806 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
19809 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
19810 paren
= (vim_strchr(p
, '(') != NULL
);
19811 if (name
== NULL
&& (fudi
.fd_dict
== NULL
|| !paren
) && !eap
->skip
)
19814 * Return on an invalid expression in braces, unless the expression
19815 * evaluation has been cancelled due to an aborting error, an
19816 * interrupt, or an exception.
19820 if (!eap
->skip
&& fudi
.fd_newkey
!= NULL
)
19821 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
19822 vim_free(fudi
.fd_newkey
);
19829 /* An error in a function call during evaluation of an expression in magic
19830 * braces should not cause the function not to be defined. */
19831 saved_did_emsg
= did_emsg
;
19835 * ":function func" with only function name: list function.
19839 if (!ends_excmd(*skipwhite(p
)))
19841 EMSG(_(e_trailing
));
19844 eap
->nextcmd
= check_nextcmd(p
);
19845 if (eap
->nextcmd
!= NULL
)
19847 if (!eap
->skip
&& !got_int
)
19849 fp
= find_func(name
);
19852 list_func_head(fp
, TRUE
);
19853 for (j
= 0; j
< fp
->uf_lines
.ga_len
&& !got_int
; ++j
)
19855 if (FUNCLINE(fp
, j
) == NULL
)
19858 msg_outnum((long)(j
+ 1));
19863 msg_prt_line(FUNCLINE(fp
, j
), FALSE
);
19864 out_flush(); /* show a line at a time */
19870 msg_puts((char_u
*)" endfunction");
19874 emsg_funcname(N_("E123: Undefined function: %s"), name
);
19880 * ":function name(arg1, arg2)" Define function.
19887 EMSG2(_("E124: Missing '(': %s"), eap
->arg
);
19890 /* attempt to continue by skipping some text */
19891 if (vim_strchr(p
, '(') != NULL
)
19892 p
= vim_strchr(p
, '(');
19894 p
= skipwhite(p
+ 1);
19896 ga_init2(&newargs
, (int)sizeof(char_u
*), 3);
19897 ga_init2(&newlines
, (int)sizeof(char_u
*), 3);
19901 /* Check the name of the function. Unless it's a dictionary function
19902 * (that we are overwriting). */
19906 arg
= fudi
.fd_newkey
;
19907 if (arg
!= NULL
&& (fudi
.fd_di
== NULL
19908 || fudi
.fd_di
->di_tv
.v_type
!= VAR_FUNC
))
19910 if (*arg
== K_SPECIAL
)
19914 while (arg
[j
] != NUL
&& (j
== 0 ? eval_isnamec1(arg
[j
])
19915 : eval_isnamec(arg
[j
])))
19918 emsg_funcname((char *)e_invarg2
, arg
);
19923 * Isolate the arguments: "arg1, arg2, ...)"
19927 if (p
[0] == '.' && p
[1] == '.' && p
[2] == '.')
19936 while (ASCII_ISALNUM(*p
) || *p
== '_')
19938 if (arg
== p
|| isdigit(*arg
)
19939 || (p
- arg
== 9 && STRNCMP(arg
, "firstline", 9) == 0)
19940 || (p
- arg
== 8 && STRNCMP(arg
, "lastline", 8) == 0))
19943 EMSG2(_("E125: Illegal argument: %s"), arg
);
19946 if (ga_grow(&newargs
, 1) == FAIL
)
19950 arg
= vim_strsave(arg
);
19953 ((char_u
**)(newargs
.ga_data
))[newargs
.ga_len
] = arg
;
19962 if (mustend
&& *p
!= ')')
19965 EMSG2(_(e_invarg2
), eap
->arg
);
19969 ++p
; /* skip the ')' */
19971 /* find extra arguments "range", "dict" and "abort" */
19975 if (STRNCMP(p
, "range", 5) == 0)
19980 else if (STRNCMP(p
, "dict", 4) == 0)
19985 else if (STRNCMP(p
, "abort", 5) == 0)
19994 /* When there is a line break use what follows for the function body.
19995 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19998 else if (*p
!= NUL
&& *p
!= '"' && !eap
->skip
&& !did_emsg
)
19999 EMSG(_(e_trailing
));
20002 * Read the body of the function, until ":endfunction" is found.
20006 /* Check if the function already exists, don't let the user type the
20007 * whole function before telling him it doesn't work! For a script we
20008 * need to skip the body to be able to find what follows. */
20009 if (!eap
->skip
&& !eap
->forceit
)
20011 if (fudi
.fd_dict
!= NULL
&& fudi
.fd_newkey
== NULL
)
20012 EMSG(_(e_funcdict
));
20013 else if (name
!= NULL
&& find_func(name
) != NULL
)
20014 emsg_funcname(e_funcexts
, name
);
20017 if (!eap
->skip
&& did_emsg
)
20020 msg_putchar('\n'); /* don't overwrite the function name */
20021 cmdline_row
= msg_row
;
20029 need_wait_return
= FALSE
;
20030 sourcing_lnum_off
= sourcing_lnum
;
20032 if (line_arg
!= NULL
)
20034 /* Use eap->arg, split up in parts by line breaks. */
20035 theline
= line_arg
;
20036 p
= vim_strchr(theline
, '\n');
20038 line_arg
+= STRLEN(line_arg
);
20045 else if (eap
->getline
== NULL
)
20046 theline
= getcmdline(':', 0L, indent
);
20048 theline
= eap
->getline(':', eap
->cookie
, indent
);
20050 lines_left
= Rows
- 1;
20051 if (theline
== NULL
)
20053 EMSG(_("E126: Missing :endfunction"));
20057 /* Detect line continuation: sourcing_lnum increased more than one. */
20058 if (sourcing_lnum
> sourcing_lnum_off
+ 1)
20059 sourcing_lnum_off
= sourcing_lnum
- sourcing_lnum_off
- 1;
20061 sourcing_lnum_off
= 0;
20063 if (skip_until
!= NULL
)
20065 /* between ":append" and "." and between ":python <<EOF" and "EOF"
20066 * don't check for ":endfunc". */
20067 if (STRCMP(theline
, skip_until
) == 0)
20069 vim_free(skip_until
);
20075 /* skip ':' and blanks*/
20076 for (p
= theline
; vim_iswhite(*p
) || *p
== ':'; ++p
)
20079 /* Check for "endfunction". */
20080 if (checkforcmd(&p
, "endfunction", 4) && nesting
-- == 0)
20082 if (line_arg
== NULL
)
20087 /* Increase indent inside "if", "while", "for" and "try", decrease
20089 if (indent
> 2 && STRNCMP(p
, "end", 3) == 0)
20091 else if (STRNCMP(p
, "if", 2) == 0
20092 || STRNCMP(p
, "wh", 2) == 0
20093 || STRNCMP(p
, "for", 3) == 0
20094 || STRNCMP(p
, "try", 3) == 0)
20097 /* Check for defining a function inside this function. */
20098 if (checkforcmd(&p
, "function", 2))
20101 p
= skipwhite(p
+ 1);
20102 p
+= eval_fname_script(p
);
20103 if (ASCII_ISALPHA(*p
))
20105 vim_free(trans_function_name(&p
, TRUE
, 0, NULL
));
20106 if (*skipwhite(p
) == '(')
20114 /* Check for ":append" or ":insert". */
20115 p
= skip_range(p
, NULL
);
20116 if ((p
[0] == 'a' && (!ASCII_ISALPHA(p
[1]) || p
[1] == 'p'))
20118 && (!ASCII_ISALPHA(p
[1]) || (p
[1] == 'n'
20119 && (!ASCII_ISALPHA(p
[2]) || (p
[2] == 's'))))))
20120 skip_until
= vim_strsave((char_u
*)".");
20122 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
20123 arg
= skipwhite(skiptowhite(p
));
20124 if (arg
[0] == '<' && arg
[1] =='<'
20125 && ((p
[0] == 'p' && p
[1] == 'y'
20126 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 't'))
20127 || (p
[0] == 'p' && p
[1] == 'e'
20128 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'r'))
20129 || (p
[0] == 't' && p
[1] == 'c'
20130 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'l'))
20131 || (p
[0] == 'r' && p
[1] == 'u' && p
[2] == 'b'
20132 && (!ASCII_ISALPHA(p
[3]) || p
[3] == 'y'))
20133 || (p
[0] == 'm' && p
[1] == 'z'
20134 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 's'))
20137 /* ":python <<" continues until a dot, like ":append" */
20138 p
= skipwhite(arg
+ 2);
20140 skip_until
= vim_strsave((char_u
*)".");
20142 skip_until
= vim_strsave(p
);
20146 /* Add the line to the function. */
20147 if (ga_grow(&newlines
, 1 + sourcing_lnum_off
) == FAIL
)
20149 if (line_arg
== NULL
)
20154 /* Copy the line to newly allocated memory. get_one_sourceline()
20155 * allocates 250 bytes per line, this saves 80% on average. The cost
20156 * is an extra alloc/free. */
20157 p
= vim_strsave(theline
);
20160 if (line_arg
== NULL
)
20165 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = theline
;
20167 /* Add NULL lines for continuation lines, so that the line count is
20168 * equal to the index in the growarray. */
20169 while (sourcing_lnum_off
-- > 0)
20170 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = NULL
;
20172 /* Check for end of eap->arg. */
20173 if (line_arg
!= NULL
&& *line_arg
== NUL
)
20177 /* Don't define the function when skipping commands or when an error was
20179 if (eap
->skip
|| did_emsg
)
20183 * If there are no errors, add the function
20185 if (fudi
.fd_dict
== NULL
)
20187 v
= find_var(name
, &ht
);
20188 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
20190 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
20195 fp
= find_func(name
);
20200 emsg_funcname(e_funcexts
, name
);
20203 if (fp
->uf_calls
> 0)
20205 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
20209 /* redefine existing function */
20210 ga_clear_strings(&(fp
->uf_args
));
20211 ga_clear_strings(&(fp
->uf_lines
));
20221 if (fudi
.fd_newkey
== NULL
&& !eap
->forceit
)
20223 EMSG(_(e_funcdict
));
20226 if (fudi
.fd_di
== NULL
)
20228 /* Can't add a function to a locked dictionary */
20229 if (tv_check_lock(fudi
.fd_dict
->dv_lock
, eap
->arg
))
20232 /* Can't change an existing function if it is locked */
20233 else if (tv_check_lock(fudi
.fd_di
->di_tv
.v_lock
, eap
->arg
))
20236 /* Give the function a sequential number. Can only be used with a
20239 sprintf(numbuf
, "%d", ++func_nr
);
20240 name
= vim_strsave((char_u
*)numbuf
);
20247 if (fudi
.fd_dict
== NULL
&& vim_strchr(name
, AUTOLOAD_CHAR
) != NULL
)
20250 char_u
*scriptname
;
20252 /* Check that the autoload name matches the script name. */
20254 if (sourcing_name
!= NULL
)
20256 scriptname
= autoload_name(name
);
20257 if (scriptname
!= NULL
)
20259 p
= vim_strchr(scriptname
, '/');
20260 plen
= (int)STRLEN(p
);
20261 slen
= (int)STRLEN(sourcing_name
);
20262 if (slen
> plen
&& fnamecmp(p
,
20263 sourcing_name
+ slen
- plen
) == 0)
20265 vim_free(scriptname
);
20270 EMSG2(_("E746: Function name does not match script file name: %s"), name
);
20275 fp
= (ufunc_T
*)alloc((unsigned)(sizeof(ufunc_T
) + STRLEN(name
)));
20279 if (fudi
.fd_dict
!= NULL
)
20281 if (fudi
.fd_di
== NULL
)
20283 /* add new dict entry */
20284 fudi
.fd_di
= dictitem_alloc(fudi
.fd_newkey
);
20285 if (fudi
.fd_di
== NULL
)
20290 if (dict_add(fudi
.fd_dict
, fudi
.fd_di
) == FAIL
)
20292 vim_free(fudi
.fd_di
);
20298 /* overwrite existing dict entry */
20299 clear_tv(&fudi
.fd_di
->di_tv
);
20300 fudi
.fd_di
->di_tv
.v_type
= VAR_FUNC
;
20301 fudi
.fd_di
->di_tv
.v_lock
= 0;
20302 fudi
.fd_di
->di_tv
.vval
.v_string
= vim_strsave(name
);
20303 fp
->uf_refcount
= 1;
20305 /* behave like "dict" was used */
20309 /* insert the new function in the function list */
20310 STRCPY(fp
->uf_name
, name
);
20311 hash_add(&func_hashtab
, UF2HIKEY(fp
));
20313 fp
->uf_args
= newargs
;
20314 fp
->uf_lines
= newlines
;
20315 #ifdef FEAT_PROFILE
20316 fp
->uf_tml_count
= NULL
;
20317 fp
->uf_tml_total
= NULL
;
20318 fp
->uf_tml_self
= NULL
;
20319 fp
->uf_profiling
= FALSE
;
20320 if (prof_def_func())
20321 func_do_profile(fp
);
20323 fp
->uf_varargs
= varargs
;
20324 fp
->uf_flags
= flags
;
20326 fp
->uf_script_ID
= current_SID
;
20330 ga_clear_strings(&newargs
);
20331 ga_clear_strings(&newlines
);
20333 vim_free(skip_until
);
20334 vim_free(fudi
.fd_newkey
);
20336 did_emsg
|= saved_did_emsg
;
20340 * Get a function name, translating "<SID>" and "<SNR>".
20341 * Also handles a Funcref in a List or Dictionary.
20342 * Returns the function name in allocated memory, or NULL for failure.
20344 * TFN_INT: internal function name OK
20345 * TFN_QUIET: be quiet
20346 * Advances "pp" to just after the function name (if no error).
20349 trans_function_name(pp
, skip
, flags
, fdp
)
20351 int skip
; /* only find the end, don't evaluate */
20353 funcdict_T
*fdp
; /* return: info about dictionary used */
20355 char_u
*name
= NULL
;
20359 char_u sid_buf
[20];
20364 vim_memset(fdp
, 0, sizeof(funcdict_T
));
20367 /* Check for hard coded <SNR>: already translated function ID (from a user
20369 if ((*pp
)[0] == K_SPECIAL
&& (*pp
)[1] == KS_EXTRA
20370 && (*pp
)[2] == (int)KE_SNR
)
20373 len
= get_id_len(pp
) + 3;
20374 return vim_strnsave(start
, len
);
20377 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
20378 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
20379 lead
= eval_fname_script(start
);
20383 end
= get_lval(start
, NULL
, &lv
, FALSE
, skip
, flags
& TFN_QUIET
,
20384 lead
> 2 ? 0 : FNE_CHECK_START
);
20388 EMSG(_("E129: Function name required"));
20391 if (end
== NULL
|| (lv
.ll_tv
!= NULL
&& (lead
> 2 || lv
.ll_range
)))
20394 * Report an invalid expression in braces, unless the expression
20395 * evaluation has been cancelled due to an aborting error, an
20396 * interrupt, or an exception.
20401 EMSG2(_(e_invarg2
), start
);
20404 *pp
= find_name_end(start
, NULL
, NULL
, FNE_INCL_BR
);
20408 if (lv
.ll_tv
!= NULL
)
20412 fdp
->fd_dict
= lv
.ll_dict
;
20413 fdp
->fd_newkey
= lv
.ll_newkey
;
20414 lv
.ll_newkey
= NULL
;
20415 fdp
->fd_di
= lv
.ll_di
;
20417 if (lv
.ll_tv
->v_type
== VAR_FUNC
&& lv
.ll_tv
->vval
.v_string
!= NULL
)
20419 name
= vim_strsave(lv
.ll_tv
->vval
.v_string
);
20424 if (!skip
&& !(flags
& TFN_QUIET
) && (fdp
== NULL
20425 || lv
.ll_dict
== NULL
|| fdp
->fd_newkey
== NULL
))
20426 EMSG(_(e_funcref
));
20434 if (lv
.ll_name
== NULL
)
20436 /* Error found, but continue after the function name. */
20441 /* Check if the name is a Funcref. If so, use the value. */
20442 if (lv
.ll_exp_name
!= NULL
)
20444 len
= (int)STRLEN(lv
.ll_exp_name
);
20445 name
= deref_func_name(lv
.ll_exp_name
, &len
);
20446 if (name
== lv
.ll_exp_name
)
20451 len
= (int)(end
- *pp
);
20452 name
= deref_func_name(*pp
, &len
);
20458 name
= vim_strsave(name
);
20463 if (lv
.ll_exp_name
!= NULL
)
20465 len
= (int)STRLEN(lv
.ll_exp_name
);
20466 if (lead
<= 2 && lv
.ll_name
== lv
.ll_exp_name
20467 && STRNCMP(lv
.ll_name
, "s:", 2) == 0)
20469 /* When there was "s:" already or the name expanded to get a
20470 * leading "s:" then remove it. */
20478 if (lead
== 2) /* skip over "s:" */
20480 len
= (int)(end
- lv
.ll_name
);
20484 * Copy the function name to allocated memory.
20485 * Accept <SID>name() inside a script, translate into <SNR>123_name().
20486 * Accept <SNR>123_name() outside a script.
20489 lead
= 0; /* do nothing */
20493 if ((lv
.ll_exp_name
!= NULL
&& eval_fname_sid(lv
.ll_exp_name
))
20494 || eval_fname_sid(*pp
))
20496 /* It's "s:" or "<SID>" */
20497 if (current_SID
<= 0)
20499 EMSG(_(e_usingsid
));
20502 sprintf((char *)sid_buf
, "%ld_", (long)current_SID
);
20503 lead
+= (int)STRLEN(sid_buf
);
20506 else if (!(flags
& TFN_INT
) && builtin_function(lv
.ll_name
))
20508 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv
.ll_name
);
20511 name
= alloc((unsigned)(len
+ lead
+ 1));
20516 name
[0] = K_SPECIAL
;
20517 name
[1] = KS_EXTRA
;
20518 name
[2] = (int)KE_SNR
;
20519 if (lead
> 3) /* If it's "<SID>" */
20520 STRCPY(name
+ 3, sid_buf
);
20522 mch_memmove(name
+ lead
, lv
.ll_name
, (size_t)len
);
20523 name
[len
+ lead
] = NUL
;
20533 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
20534 * Return 2 if "p" starts with "s:".
20535 * Return 0 otherwise.
20538 eval_fname_script(p
)
20541 if (p
[0] == '<' && (STRNICMP(p
+ 1, "SID>", 4) == 0
20542 || STRNICMP(p
+ 1, "SNR>", 4) == 0))
20544 if (p
[0] == 's' && p
[1] == ':')
20550 * Return TRUE if "p" starts with "<SID>" or "s:".
20551 * Only works if eval_fname_script() returned non-zero for "p"!
20557 return (*p
== 's' || TOUPPER_ASC(p
[2]) == 'I');
20561 * List the head of the function: "name(arg1, arg2)".
20564 list_func_head(fp
, indent
)
20573 MSG_PUTS("function ");
20574 if (fp
->uf_name
[0] == K_SPECIAL
)
20576 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8
));
20577 msg_puts(fp
->uf_name
+ 3);
20580 msg_puts(fp
->uf_name
);
20582 for (j
= 0; j
< fp
->uf_args
.ga_len
; ++j
)
20586 msg_puts(FUNCARG(fp
, j
));
20588 if (fp
->uf_varargs
)
20597 last_set_msg(fp
->uf_script_ID
);
20601 * Find a function by name, return pointer to it in ufuncs.
20602 * Return NULL for unknown function.
20610 hi
= hash_find(&func_hashtab
, name
);
20611 if (!HASHITEM_EMPTY(hi
))
20616 #if defined(EXITFREE) || defined(PROTO)
20618 free_all_functions()
20622 /* Need to start all over every time, because func_free() may change the
20624 while (func_hashtab
.ht_used
> 0)
20625 for (hi
= func_hashtab
.ht_array
; ; ++hi
)
20626 if (!HASHITEM_EMPTY(hi
))
20628 func_free(HI2UF(hi
));
20635 * Return TRUE if a function "name" exists.
20638 function_exists(name
)
20645 p
= trans_function_name(&nm
, FALSE
, TFN_INT
|TFN_QUIET
, NULL
);
20646 nm
= skipwhite(nm
);
20648 /* Only accept "funcname", "funcname ", "funcname (..." and
20649 * "funcname(...", not "funcname!...". */
20650 if (p
!= NULL
&& (*nm
== NUL
|| *nm
== '('))
20652 if (builtin_function(p
))
20653 n
= (find_internal_func(p
) >= 0);
20655 n
= (find_func(p
) != NULL
);
20662 * Return TRUE if "name" looks like a builtin function name: starts with a
20663 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
20666 builtin_function(name
)
20669 return ASCII_ISLOWER(name
[0]) && vim_strchr(name
, ':') == NULL
20670 && vim_strchr(name
, AUTOLOAD_CHAR
) == NULL
;
20673 #if defined(FEAT_PROFILE) || defined(PROTO)
20675 * Start profiling function "fp".
20678 func_do_profile(fp
)
20681 fp
->uf_tm_count
= 0;
20682 profile_zero(&fp
->uf_tm_self
);
20683 profile_zero(&fp
->uf_tm_total
);
20684 if (fp
->uf_tml_count
== NULL
)
20685 fp
->uf_tml_count
= (int *)alloc_clear((unsigned)
20686 (sizeof(int) * fp
->uf_lines
.ga_len
));
20687 if (fp
->uf_tml_total
== NULL
)
20688 fp
->uf_tml_total
= (proftime_T
*)alloc_clear((unsigned)
20689 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
20690 if (fp
->uf_tml_self
== NULL
)
20691 fp
->uf_tml_self
= (proftime_T
*)alloc_clear((unsigned)
20692 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
20693 fp
->uf_tml_idx
= -1;
20694 if (fp
->uf_tml_count
== NULL
|| fp
->uf_tml_total
== NULL
20695 || fp
->uf_tml_self
== NULL
)
20696 return; /* out of memory */
20698 fp
->uf_profiling
= TRUE
;
20702 * Dump the profiling results for all functions in file "fd".
20705 func_dump_profile(fd
)
20715 todo
= (int)func_hashtab
.ht_used
;
20717 return; /* nothing to dump */
20719 sorttab
= (ufunc_T
**)alloc((unsigned)(sizeof(ufunc_T
) * todo
));
20721 for (hi
= func_hashtab
.ht_array
; todo
> 0; ++hi
)
20723 if (!HASHITEM_EMPTY(hi
))
20727 if (fp
->uf_profiling
)
20729 if (sorttab
!= NULL
)
20730 sorttab
[st_len
++] = fp
;
20732 if (fp
->uf_name
[0] == K_SPECIAL
)
20733 fprintf(fd
, "FUNCTION <SNR>%s()\n", fp
->uf_name
+ 3);
20735 fprintf(fd
, "FUNCTION %s()\n", fp
->uf_name
);
20736 if (fp
->uf_tm_count
== 1)
20737 fprintf(fd
, "Called 1 time\n");
20739 fprintf(fd
, "Called %d times\n", fp
->uf_tm_count
);
20740 fprintf(fd
, "Total time: %s\n", profile_msg(&fp
->uf_tm_total
));
20741 fprintf(fd
, " Self time: %s\n", profile_msg(&fp
->uf_tm_self
));
20743 fprintf(fd
, "count total (s) self (s)\n");
20745 for (i
= 0; i
< fp
->uf_lines
.ga_len
; ++i
)
20747 if (FUNCLINE(fp
, i
) == NULL
)
20749 prof_func_line(fd
, fp
->uf_tml_count
[i
],
20750 &fp
->uf_tml_total
[i
], &fp
->uf_tml_self
[i
], TRUE
);
20751 fprintf(fd
, "%s\n", FUNCLINE(fp
, i
));
20758 if (sorttab
!= NULL
&& st_len
> 0)
20760 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
20762 prof_sort_list(fd
, sorttab
, st_len
, "TOTAL", FALSE
);
20763 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
20765 prof_sort_list(fd
, sorttab
, st_len
, "SELF", TRUE
);
20772 prof_sort_list(fd
, sorttab
, st_len
, title
, prefer_self
)
20777 int prefer_self
; /* when equal print only self time */
20782 fprintf(fd
, "FUNCTIONS SORTED ON %s TIME\n", title
);
20783 fprintf(fd
, "count total (s) self (s) function\n");
20784 for (i
= 0; i
< 20 && i
< st_len
; ++i
)
20787 prof_func_line(fd
, fp
->uf_tm_count
, &fp
->uf_tm_total
, &fp
->uf_tm_self
,
20789 if (fp
->uf_name
[0] == K_SPECIAL
)
20790 fprintf(fd
, " <SNR>%s()\n", fp
->uf_name
+ 3);
20792 fprintf(fd
, " %s()\n", fp
->uf_name
);
20798 * Print the count and times for one function or function line.
20801 prof_func_line(fd
, count
, total
, self
, prefer_self
)
20806 int prefer_self
; /* when equal print only self time */
20810 fprintf(fd
, "%5d ", count
);
20811 if (prefer_self
&& profile_equal(total
, self
))
20814 fprintf(fd
, "%s ", profile_msg(total
));
20815 if (!prefer_self
&& profile_equal(total
, self
))
20818 fprintf(fd
, "%s ", profile_msg(self
));
20825 * Compare function for total time sorting.
20828 #ifdef __BORLANDC__
20831 prof_total_cmp(s1
, s2
)
20837 p1
= *(ufunc_T
**)s1
;
20838 p2
= *(ufunc_T
**)s2
;
20839 return profile_cmp(&p1
->uf_tm_total
, &p2
->uf_tm_total
);
20843 * Compare function for self time sorting.
20846 #ifdef __BORLANDC__
20849 prof_self_cmp(s1
, s2
)
20855 p1
= *(ufunc_T
**)s1
;
20856 p2
= *(ufunc_T
**)s2
;
20857 return profile_cmp(&p1
->uf_tm_self
, &p2
->uf_tm_self
);
20863 * If "name" has a package name try autoloading the script for it.
20864 * Return TRUE if a package was loaded.
20867 script_autoload(name
, reload
)
20869 int reload
; /* load script again when already loaded */
20872 char_u
*scriptname
, *tofree
;
20876 /* If there is no '#' after name[0] there is no package name. */
20877 p
= vim_strchr(name
, AUTOLOAD_CHAR
);
20878 if (p
== NULL
|| p
== name
)
20881 tofree
= scriptname
= autoload_name(name
);
20883 /* Find the name in the list of previously loaded package names. Skip
20884 * "autoload/", it's always the same. */
20885 for (i
= 0; i
< ga_loaded
.ga_len
; ++i
)
20886 if (STRCMP(((char_u
**)ga_loaded
.ga_data
)[i
] + 9, scriptname
+ 9) == 0)
20888 if (!reload
&& i
< ga_loaded
.ga_len
)
20889 ret
= FALSE
; /* was loaded already */
20892 /* Remember the name if it wasn't loaded already. */
20893 if (i
== ga_loaded
.ga_len
&& ga_grow(&ga_loaded
, 1) == OK
)
20895 ((char_u
**)ga_loaded
.ga_data
)[ga_loaded
.ga_len
++] = scriptname
;
20899 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
20900 if (source_runtime(scriptname
, FALSE
) == OK
)
20909 * Return the autoload script name for a function or variable name.
20910 * Returns NULL when out of memory.
20913 autoload_name(name
)
20917 char_u
*scriptname
;
20919 /* Get the script file name: replace '#' with '/', append ".vim". */
20920 scriptname
= alloc((unsigned)(STRLEN(name
) + 14));
20921 if (scriptname
== NULL
)
20923 STRCPY(scriptname
, "autoload/");
20924 STRCAT(scriptname
, name
);
20925 *vim_strrchr(scriptname
, AUTOLOAD_CHAR
) = NUL
;
20926 STRCAT(scriptname
, ".vim");
20927 while ((p
= vim_strchr(scriptname
, AUTOLOAD_CHAR
)) != NULL
)
20932 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
20935 * Function given to ExpandGeneric() to obtain the list of user defined
20939 get_user_func_name(xp
, idx
)
20943 static long_u done
;
20944 static hashitem_T
*hi
;
20950 hi
= func_hashtab
.ht_array
;
20952 if (done
< func_hashtab
.ht_used
)
20956 while (HASHITEM_EMPTY(hi
))
20960 if (STRLEN(fp
->uf_name
) + 4 >= IOSIZE
)
20961 return fp
->uf_name
; /* prevents overflow */
20963 cat_func_name(IObuff
, fp
);
20964 if (xp
->xp_context
!= EXPAND_USER_FUNC
)
20966 STRCAT(IObuff
, "(");
20967 if (!fp
->uf_varargs
&& fp
->uf_args
.ga_len
== 0)
20968 STRCAT(IObuff
, ")");
20975 #endif /* FEAT_CMDL_COMPL */
20978 * Copy the function name of "fp" to buffer "buf".
20979 * "buf" must be able to hold the function name plus three bytes.
20980 * Takes care of script-local function names.
20983 cat_func_name(buf
, fp
)
20987 if (fp
->uf_name
[0] == K_SPECIAL
)
20989 STRCPY(buf
, "<SNR>");
20990 STRCAT(buf
, fp
->uf_name
+ 3);
20993 STRCPY(buf
, fp
->uf_name
);
20997 * ":delfunction {name}"
21000 ex_delfunction(eap
)
21003 ufunc_T
*fp
= NULL
;
21009 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
21010 vim_free(fudi
.fd_newkey
);
21013 if (fudi
.fd_dict
!= NULL
&& !eap
->skip
)
21014 EMSG(_(e_funcref
));
21017 if (!ends_excmd(*skipwhite(p
)))
21020 EMSG(_(e_trailing
));
21023 eap
->nextcmd
= check_nextcmd(p
);
21024 if (eap
->nextcmd
!= NULL
)
21028 fp
= find_func(name
);
21035 EMSG2(_(e_nofunc
), eap
->arg
);
21038 if (fp
->uf_calls
> 0)
21040 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap
->arg
);
21044 if (fudi
.fd_dict
!= NULL
)
21046 /* Delete the dict item that refers to the function, it will
21047 * invoke func_unref() and possibly delete the function. */
21048 dictitem_remove(fudi
.fd_dict
, fudi
.fd_di
);
21056 * Free a function and remove it from the list of functions.
21064 /* clear this function */
21065 ga_clear_strings(&(fp
->uf_args
));
21066 ga_clear_strings(&(fp
->uf_lines
));
21067 #ifdef FEAT_PROFILE
21068 vim_free(fp
->uf_tml_count
);
21069 vim_free(fp
->uf_tml_total
);
21070 vim_free(fp
->uf_tml_self
);
21073 /* remove the function from the function hashtable */
21074 hi
= hash_find(&func_hashtab
, UF2HIKEY(fp
));
21075 if (HASHITEM_EMPTY(hi
))
21076 EMSG2(_(e_intern2
), "func_free()");
21078 hash_remove(&func_hashtab
, hi
);
21084 * Unreference a Function: decrement the reference count and free it when it
21085 * becomes zero. Only for numbered functions.
21093 if (name
!= NULL
&& isdigit(*name
))
21095 fp
= find_func(name
);
21097 EMSG2(_(e_intern2
), "func_unref()");
21098 else if (--fp
->uf_refcount
<= 0)
21100 /* Only delete it when it's not being used. Otherwise it's done
21101 * when "uf_calls" becomes zero. */
21102 if (fp
->uf_calls
== 0)
21109 * Count a reference to a Function.
21117 if (name
!= NULL
&& isdigit(*name
))
21119 fp
= find_func(name
);
21121 EMSG2(_(e_intern2
), "func_ref()");
21128 * Call a user function.
21131 call_user_func(fp
, argcount
, argvars
, rettv
, firstline
, lastline
, selfdict
)
21132 ufunc_T
*fp
; /* pointer to function */
21133 int argcount
; /* nr of args */
21134 typval_T
*argvars
; /* arguments */
21135 typval_T
*rettv
; /* return value */
21136 linenr_T firstline
; /* first line of range */
21137 linenr_T lastline
; /* last line of range */
21138 dict_T
*selfdict
; /* Dictionary for "self" */
21140 char_u
*save_sourcing_name
;
21141 linenr_T save_sourcing_lnum
;
21142 scid_T save_current_SID
;
21145 static int depth
= 0;
21147 int fixvar_idx
= 0; /* index in fixvar[] */
21150 char_u numbuf
[NUMBUFLEN
];
21152 #ifdef FEAT_PROFILE
21153 proftime_T wait_start
;
21154 proftime_T call_start
;
21157 /* If depth of calling is getting too high, don't execute the function */
21158 if (depth
>= p_mfd
)
21160 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
21161 rettv
->v_type
= VAR_NUMBER
;
21162 rettv
->vval
.v_number
= -1;
21167 line_breakcheck(); /* check for CTRL-C hit */
21169 fc
= (funccall_T
*)alloc(sizeof(funccall_T
));
21170 fc
->caller
= current_funccal
;
21171 current_funccal
= fc
;
21174 rettv
->vval
.v_number
= 0;
21176 fc
->returned
= FALSE
;
21177 fc
->level
= ex_nesting_level
;
21178 /* Check if this function has a breakpoint. */
21179 fc
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
, (linenr_T
)0);
21180 fc
->dbg_tick
= debug_tick
;
21183 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
21184 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
21185 * each argument variable and saves a lot of time.
21188 * Init l: variables.
21190 init_var_dict(&fc
->l_vars
, &fc
->l_vars_var
);
21191 if (selfdict
!= NULL
)
21193 /* Set l:self to "selfdict". Use "name" to avoid a warning from
21194 * some compiler that checks the destination size. */
21195 v
= &fc
->fixvar
[fixvar_idx
++].var
;
21197 STRCPY(name
, "self");
21198 v
->di_flags
= DI_FLAGS_RO
+ DI_FLAGS_FIX
;
21199 hash_add(&fc
->l_vars
.dv_hashtab
, DI2HIKEY(v
));
21200 v
->di_tv
.v_type
= VAR_DICT
;
21201 v
->di_tv
.v_lock
= 0;
21202 v
->di_tv
.vval
.v_dict
= selfdict
;
21203 ++selfdict
->dv_refcount
;
21207 * Init a: variables.
21208 * Set a:0 to "argcount".
21209 * Set a:000 to a list with room for the "..." arguments.
21211 init_var_dict(&fc
->l_avars
, &fc
->l_avars_var
);
21212 add_nr_var(&fc
->l_avars
, &fc
->fixvar
[fixvar_idx
++].var
, "0",
21213 (varnumber_T
)(argcount
- fp
->uf_args
.ga_len
));
21214 /* Use "name" to avoid a warning from some compiler that checks the
21215 * destination size. */
21216 v
= &fc
->fixvar
[fixvar_idx
++].var
;
21218 STRCPY(name
, "000");
21219 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21220 hash_add(&fc
->l_avars
.dv_hashtab
, DI2HIKEY(v
));
21221 v
->di_tv
.v_type
= VAR_LIST
;
21222 v
->di_tv
.v_lock
= VAR_FIXED
;
21223 v
->di_tv
.vval
.v_list
= &fc
->l_varlist
;
21224 vim_memset(&fc
->l_varlist
, 0, sizeof(list_T
));
21225 fc
->l_varlist
.lv_refcount
= DO_NOT_FREE_CNT
;
21226 fc
->l_varlist
.lv_lock
= VAR_FIXED
;
21229 * Set a:firstline to "firstline" and a:lastline to "lastline".
21230 * Set a:name to named arguments.
21231 * Set a:N to the "..." arguments.
21233 add_nr_var(&fc
->l_avars
, &fc
->fixvar
[fixvar_idx
++].var
, "firstline",
21234 (varnumber_T
)firstline
);
21235 add_nr_var(&fc
->l_avars
, &fc
->fixvar
[fixvar_idx
++].var
, "lastline",
21236 (varnumber_T
)lastline
);
21237 for (i
= 0; i
< argcount
; ++i
)
21239 ai
= i
- fp
->uf_args
.ga_len
;
21241 /* named argument a:name */
21242 name
= FUNCARG(fp
, i
);
21245 /* "..." argument a:1, a:2, etc. */
21246 sprintf((char *)numbuf
, "%d", ai
+ 1);
21249 if (fixvar_idx
< FIXVAR_CNT
&& STRLEN(name
) <= VAR_SHORT_LEN
)
21251 v
= &fc
->fixvar
[fixvar_idx
++].var
;
21252 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21256 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
21260 v
->di_flags
= DI_FLAGS_RO
;
21262 STRCPY(v
->di_key
, name
);
21263 hash_add(&fc
->l_avars
.dv_hashtab
, DI2HIKEY(v
));
21265 /* Note: the values are copied directly to avoid alloc/free.
21266 * "argvars" must have VAR_FIXED for v_lock. */
21267 v
->di_tv
= argvars
[i
];
21268 v
->di_tv
.v_lock
= VAR_FIXED
;
21270 if (ai
>= 0 && ai
< MAX_FUNC_ARGS
)
21272 list_append(&fc
->l_varlist
, &fc
->l_listitems
[ai
]);
21273 fc
->l_listitems
[ai
].li_tv
= argvars
[i
];
21274 fc
->l_listitems
[ai
].li_tv
.v_lock
= VAR_FIXED
;
21278 /* Don't redraw while executing the function. */
21279 ++RedrawingDisabled
;
21280 save_sourcing_name
= sourcing_name
;
21281 save_sourcing_lnum
= sourcing_lnum
;
21283 sourcing_name
= alloc((unsigned)((save_sourcing_name
== NULL
? 0
21284 : STRLEN(save_sourcing_name
)) + STRLEN(fp
->uf_name
) + 13));
21285 if (sourcing_name
!= NULL
)
21287 if (save_sourcing_name
!= NULL
21288 && STRNCMP(save_sourcing_name
, "function ", 9) == 0)
21289 sprintf((char *)sourcing_name
, "%s..", save_sourcing_name
);
21291 STRCPY(sourcing_name
, "function ");
21292 cat_func_name(sourcing_name
+ STRLEN(sourcing_name
), fp
);
21294 if (p_verbose
>= 12)
21297 verbose_enter_scroll();
21299 smsg((char_u
*)_("calling %s"), sourcing_name
);
21300 if (p_verbose
>= 14)
21302 char_u buf
[MSG_BUF_LEN
];
21303 char_u numbuf2
[NUMBUFLEN
];
21307 msg_puts((char_u
*)"(");
21308 for (i
= 0; i
< argcount
; ++i
)
21311 msg_puts((char_u
*)", ");
21312 if (argvars
[i
].v_type
== VAR_NUMBER
)
21313 msg_outnum((long)argvars
[i
].vval
.v_number
);
21316 s
= tv2string(&argvars
[i
], &tofree
, numbuf2
, 0);
21319 trunc_string(s
, buf
, MSG_BUF_CLEN
);
21325 msg_puts((char_u
*)")");
21327 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21329 verbose_leave_scroll();
21333 #ifdef FEAT_PROFILE
21334 if (do_profiling
== PROF_YES
)
21336 if (!fp
->uf_profiling
&& has_profiling(FALSE
, fp
->uf_name
, NULL
))
21337 func_do_profile(fp
);
21338 if (fp
->uf_profiling
21339 || (fc
->caller
!= NULL
&& fc
->caller
->func
->uf_profiling
))
21342 profile_start(&call_start
);
21343 profile_zero(&fp
->uf_tm_children
);
21345 script_prof_save(&wait_start
);
21349 save_current_SID
= current_SID
;
21350 current_SID
= fp
->uf_script_ID
;
21351 save_did_emsg
= did_emsg
;
21354 /* call do_cmdline() to execute the lines */
21355 do_cmdline(NULL
, get_func_line
, (void *)fc
,
21356 DOCMD_NOWAIT
|DOCMD_VERBOSE
|DOCMD_REPEAT
);
21358 --RedrawingDisabled
;
21360 /* when the function was aborted because of an error, return -1 */
21361 if ((did_emsg
&& (fp
->uf_flags
& FC_ABORT
)) || rettv
->v_type
== VAR_UNKNOWN
)
21364 rettv
->v_type
= VAR_NUMBER
;
21365 rettv
->vval
.v_number
= -1;
21368 #ifdef FEAT_PROFILE
21369 if (do_profiling
== PROF_YES
&& (fp
->uf_profiling
21370 || (fc
->caller
!= NULL
&& fc
->caller
->func
->uf_profiling
)))
21372 profile_end(&call_start
);
21373 profile_sub_wait(&wait_start
, &call_start
);
21374 profile_add(&fp
->uf_tm_total
, &call_start
);
21375 profile_self(&fp
->uf_tm_self
, &call_start
, &fp
->uf_tm_children
);
21376 if (fc
->caller
!= NULL
&& fc
->caller
->func
->uf_profiling
)
21378 profile_add(&fc
->caller
->func
->uf_tm_children
, &call_start
);
21379 profile_add(&fc
->caller
->func
->uf_tml_children
, &call_start
);
21384 /* when being verbose, mention the return value */
21385 if (p_verbose
>= 12)
21388 verbose_enter_scroll();
21391 smsg((char_u
*)_("%s aborted"), sourcing_name
);
21392 else if (fc
->rettv
->v_type
== VAR_NUMBER
)
21393 smsg((char_u
*)_("%s returning #%ld"), sourcing_name
,
21394 (long)fc
->rettv
->vval
.v_number
);
21397 char_u buf
[MSG_BUF_LEN
];
21398 char_u numbuf2
[NUMBUFLEN
];
21402 /* The value may be very long. Skip the middle part, so that we
21403 * have some idea how it starts and ends. smsg() would always
21404 * truncate it at the end. */
21405 s
= tv2string(fc
->rettv
, &tofree
, numbuf2
, 0);
21408 trunc_string(s
, buf
, MSG_BUF_CLEN
);
21409 smsg((char_u
*)_("%s returning %s"), sourcing_name
, buf
);
21413 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21415 verbose_leave_scroll();
21419 vim_free(sourcing_name
);
21420 sourcing_name
= save_sourcing_name
;
21421 sourcing_lnum
= save_sourcing_lnum
;
21422 current_SID
= save_current_SID
;
21423 #ifdef FEAT_PROFILE
21424 if (do_profiling
== PROF_YES
)
21425 script_prof_restore(&wait_start
);
21428 if (p_verbose
>= 12 && sourcing_name
!= NULL
)
21431 verbose_enter_scroll();
21433 smsg((char_u
*)_("continuing in %s"), sourcing_name
);
21434 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
21436 verbose_leave_scroll();
21440 did_emsg
|= save_did_emsg
;
21441 current_funccal
= fc
->caller
;
21444 /* If the a:000 list and the l: and a: dicts are not referenced we can
21445 * free the funccall_T and what's in it. */
21446 if (fc
->l_varlist
.lv_refcount
== DO_NOT_FREE_CNT
21447 && fc
->l_vars
.dv_refcount
== DO_NOT_FREE_CNT
21448 && fc
->l_avars
.dv_refcount
== DO_NOT_FREE_CNT
)
21450 free_funccal(fc
, FALSE
);
21458 /* "fc" is still in use. This can happen when returning "a:000" or
21459 * assigning "l:" to a global variable.
21460 * Link "fc" in the list for garbage collection later. */
21461 fc
->caller
= previous_funccal
;
21462 previous_funccal
= fc
;
21464 /* Make a copy of the a: variables, since we didn't do that above. */
21465 todo
= (int)fc
->l_avars
.dv_hashtab
.ht_used
;
21466 for (hi
= fc
->l_avars
.dv_hashtab
.ht_array
; todo
> 0; ++hi
)
21468 if (!HASHITEM_EMPTY(hi
))
21472 copy_tv(&v
->di_tv
, &v
->di_tv
);
21476 /* Make a copy of the a:000 items, since we didn't do that above. */
21477 for (li
= fc
->l_varlist
.lv_first
; li
!= NULL
; li
= li
->li_next
)
21478 copy_tv(&li
->li_tv
, &li
->li_tv
);
21483 * Return TRUE if items in "fc" do not have "copyID". That means they are not
21484 * referenced from anywhere that is in use.
21487 can_free_funccal(fc
, copyID
)
21491 return (fc
->l_varlist
.lv_copyID
!= copyID
21492 && fc
->l_vars
.dv_copyID
!= copyID
21493 && fc
->l_avars
.dv_copyID
!= copyID
);
21497 * Free "fc" and what it contains.
21500 free_funccal(fc
, free_val
)
21502 int free_val
; /* a: vars were allocated */
21506 /* The a: variables typevals may not have been allocated, only free the
21507 * allocated variables. */
21508 vars_clear_ext(&fc
->l_avars
.dv_hashtab
, free_val
);
21510 /* free all l: variables */
21511 vars_clear(&fc
->l_vars
.dv_hashtab
);
21513 /* Free the a:000 variables if they were allocated. */
21515 for (li
= fc
->l_varlist
.lv_first
; li
!= NULL
; li
= li
->li_next
)
21516 clear_tv(&li
->li_tv
);
21522 * Add a number variable "name" to dict "dp" with value "nr".
21525 add_nr_var(dp
, v
, name
, nr
)
21531 STRCPY(v
->di_key
, name
);
21532 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
21533 hash_add(&dp
->dv_hashtab
, DI2HIKEY(v
));
21534 v
->di_tv
.v_type
= VAR_NUMBER
;
21535 v
->di_tv
.v_lock
= VAR_FIXED
;
21536 v
->di_tv
.vval
.v_number
= nr
;
21546 char_u
*arg
= eap
->arg
;
21548 int returning
= FALSE
;
21550 if (current_funccal
== NULL
)
21552 EMSG(_("E133: :return not inside a function"));
21559 eap
->nextcmd
= NULL
;
21560 if ((*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
21561 && eval0(arg
, &rettv
, &eap
->nextcmd
, !eap
->skip
) != FAIL
)
21564 returning
= do_return(eap
, FALSE
, TRUE
, &rettv
);
21568 /* It's safer to return also on error. */
21569 else if (!eap
->skip
)
21572 * Return unless the expression evaluation has been cancelled due to an
21573 * aborting error, an interrupt, or an exception.
21576 returning
= do_return(eap
, FALSE
, TRUE
, NULL
);
21579 /* When skipping or the return gets pending, advance to the next command
21580 * in this line (!returning). Otherwise, ignore the rest of the line.
21581 * Following lines will be ignored by get_func_line(). */
21583 eap
->nextcmd
= NULL
;
21584 else if (eap
->nextcmd
== NULL
) /* no argument */
21585 eap
->nextcmd
= check_nextcmd(arg
);
21592 * Return from a function. Possibly makes the return pending. Also called
21593 * for a pending return at the ":endtry" or after returning from an extra
21594 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
21595 * when called due to a ":return" command. "rettv" may point to a typval_T
21596 * with the return rettv. Returns TRUE when the return can be carried out,
21597 * FALSE when the return gets pending.
21600 do_return(eap
, reanimate
, is_cmd
, rettv
)
21607 struct condstack
*cstack
= eap
->cstack
;
21610 /* Undo the return. */
21611 current_funccal
->returned
= FALSE
;
21614 * Cleanup (and inactivate) conditionals, but stop when a try conditional
21615 * not in its finally clause (which then is to be executed next) is found.
21616 * In this case, make the ":return" pending for execution at the ":endtry".
21617 * Otherwise, return normally.
21619 idx
= cleanup_conditionals(eap
->cstack
, 0, TRUE
);
21622 cstack
->cs_pending
[idx
] = CSTP_RETURN
;
21624 if (!is_cmd
&& !reanimate
)
21625 /* A pending return again gets pending. "rettv" points to an
21626 * allocated variable with the rettv of the original ":return"'s
21627 * argument if present or is NULL else. */
21628 cstack
->cs_rettv
[idx
] = rettv
;
21631 /* When undoing a return in order to make it pending, get the stored
21634 rettv
= current_funccal
->rettv
;
21638 /* Store the value of the pending return. */
21639 if ((cstack
->cs_rettv
[idx
] = alloc_tv()) != NULL
)
21640 *(typval_T
*)cstack
->cs_rettv
[idx
] = *(typval_T
*)rettv
;
21642 EMSG(_(e_outofmem
));
21645 cstack
->cs_rettv
[idx
] = NULL
;
21649 /* The pending return value could be overwritten by a ":return"
21650 * without argument in a finally clause; reset the default
21652 current_funccal
->rettv
->v_type
= VAR_NUMBER
;
21653 current_funccal
->rettv
->vval
.v_number
= 0;
21656 report_make_pending(CSTP_RETURN
, rettv
);
21660 current_funccal
->returned
= TRUE
;
21662 /* If the return is carried out now, store the return value. For
21663 * a return immediately after reanimation, the value is already
21665 if (!reanimate
&& rettv
!= NULL
)
21667 clear_tv(current_funccal
->rettv
);
21668 *current_funccal
->rettv
= *(typval_T
*)rettv
;
21678 * Free the variable with a pending return value.
21681 discard_pending_return(rettv
)
21684 free_tv((typval_T
*)rettv
);
21688 * Generate a return command for producing the value of "rettv". The result
21689 * is an allocated string. Used by report_pending() for verbose messages.
21692 get_return_cmd(rettv
)
21696 char_u
*tofree
= NULL
;
21697 char_u numbuf
[NUMBUFLEN
];
21700 s
= echo_string((typval_T
*)rettv
, &tofree
, numbuf
, 0);
21704 STRCPY(IObuff
, ":return ");
21705 STRNCPY(IObuff
+ 8, s
, IOSIZE
- 8);
21706 if (STRLEN(s
) + 8 >= IOSIZE
)
21707 STRCPY(IObuff
+ IOSIZE
- 4, "...");
21709 return vim_strsave(IObuff
);
21713 * Get next function line.
21714 * Called by do_cmdline() to get the next line.
21715 * Returns allocated string, or NULL for end of function.
21718 get_func_line(c
, cookie
, indent
)
21723 funccall_T
*fcp
= (funccall_T
*)cookie
;
21724 ufunc_T
*fp
= fcp
->func
;
21726 garray_T
*gap
; /* growarray with function lines */
21728 /* If breakpoints have been added/deleted need to check for it. */
21729 if (fcp
->dbg_tick
!= debug_tick
)
21731 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
21733 fcp
->dbg_tick
= debug_tick
;
21735 #ifdef FEAT_PROFILE
21736 if (do_profiling
== PROF_YES
)
21737 func_line_end(cookie
);
21740 gap
= &fp
->uf_lines
;
21741 if (((fp
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
21746 /* Skip NULL lines (continuation lines). */
21747 while (fcp
->linenr
< gap
->ga_len
21748 && ((char_u
**)(gap
->ga_data
))[fcp
->linenr
] == NULL
)
21750 if (fcp
->linenr
>= gap
->ga_len
)
21754 retval
= vim_strsave(((char_u
**)(gap
->ga_data
))[fcp
->linenr
++]);
21755 sourcing_lnum
= fcp
->linenr
;
21756 #ifdef FEAT_PROFILE
21757 if (do_profiling
== PROF_YES
)
21758 func_line_start(cookie
);
21763 /* Did we encounter a breakpoint? */
21764 if (fcp
->breakpoint
!= 0 && fcp
->breakpoint
<= sourcing_lnum
)
21766 dbg_breakpoint(fp
->uf_name
, sourcing_lnum
);
21767 /* Find next breakpoint. */
21768 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
21770 fcp
->dbg_tick
= debug_tick
;
21776 #if defined(FEAT_PROFILE) || defined(PROTO)
21778 * Called when starting to read a function line.
21779 * "sourcing_lnum" must be correct!
21780 * When skipping lines it may not actually be executed, but we won't find out
21781 * until later and we need to store the time now.
21784 func_line_start(cookie
)
21787 funccall_T
*fcp
= (funccall_T
*)cookie
;
21788 ufunc_T
*fp
= fcp
->func
;
21790 if (fp
->uf_profiling
&& sourcing_lnum
>= 1
21791 && sourcing_lnum
<= fp
->uf_lines
.ga_len
)
21793 fp
->uf_tml_idx
= sourcing_lnum
- 1;
21794 /* Skip continuation lines. */
21795 while (fp
->uf_tml_idx
> 0 && FUNCLINE(fp
, fp
->uf_tml_idx
) == NULL
)
21797 fp
->uf_tml_execed
= FALSE
;
21798 profile_start(&fp
->uf_tml_start
);
21799 profile_zero(&fp
->uf_tml_children
);
21800 profile_get_wait(&fp
->uf_tml_wait
);
21805 * Called when actually executing a function line.
21808 func_line_exec(cookie
)
21811 funccall_T
*fcp
= (funccall_T
*)cookie
;
21812 ufunc_T
*fp
= fcp
->func
;
21814 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
21815 fp
->uf_tml_execed
= TRUE
;
21819 * Called when done with a function line.
21822 func_line_end(cookie
)
21825 funccall_T
*fcp
= (funccall_T
*)cookie
;
21826 ufunc_T
*fp
= fcp
->func
;
21828 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
21830 if (fp
->uf_tml_execed
)
21832 ++fp
->uf_tml_count
[fp
->uf_tml_idx
];
21833 profile_end(&fp
->uf_tml_start
);
21834 profile_sub_wait(&fp
->uf_tml_wait
, &fp
->uf_tml_start
);
21835 profile_add(&fp
->uf_tml_total
[fp
->uf_tml_idx
], &fp
->uf_tml_start
);
21836 profile_self(&fp
->uf_tml_self
[fp
->uf_tml_idx
], &fp
->uf_tml_start
,
21837 &fp
->uf_tml_children
);
21839 fp
->uf_tml_idx
= -1;
21845 * Return TRUE if the currently active function should be ended, because a
21846 * return was encountered or an error occurred. Used inside a ":while".
21849 func_has_ended(cookie
)
21852 funccall_T
*fcp
= (funccall_T
*)cookie
;
21854 /* Ignore the "abort" flag if the abortion behavior has been changed due to
21855 * an error inside a try conditional. */
21856 return (((fcp
->func
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
21861 * return TRUE if cookie indicates a function which "abort"s on errors.
21864 func_has_abort(cookie
)
21867 return ((funccall_T
*)cookie
)->func
->uf_flags
& FC_ABORT
;
21870 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
21873 VAR_FLAVOUR_DEFAULT
, /* doesn't start with uppercase */
21874 VAR_FLAVOUR_SESSION
, /* starts with uppercase, some lower */
21875 VAR_FLAVOUR_VIMINFO
/* all uppercase */
21878 static var_flavour_T var_flavour
__ARGS((char_u
*varname
));
21880 static var_flavour_T
21881 var_flavour(varname
)
21884 char_u
*p
= varname
;
21886 if (ASCII_ISUPPER(*p
))
21889 if (ASCII_ISLOWER(*p
))
21890 return VAR_FLAVOUR_SESSION
;
21891 return VAR_FLAVOUR_VIMINFO
;
21894 return VAR_FLAVOUR_DEFAULT
;
21898 #if defined(FEAT_VIMINFO) || defined(PROTO)
21900 * Restore global vars that start with a capital from the viminfo file
21903 read_viminfo_varlist(virp
, writing
)
21908 int type
= VAR_NUMBER
;
21911 if (!writing
&& (find_viminfo_parameter('!') != NULL
))
21913 tab
= vim_strchr(virp
->vir_line
+ 1, '\t');
21916 *tab
++ = '\0'; /* isolate the variable name */
21917 if (*tab
== 'S') /* string var */
21920 else if (*tab
== 'F')
21924 tab
= vim_strchr(tab
, '\t');
21928 if (type
== VAR_STRING
)
21929 tv
.vval
.v_string
= viminfo_readstring(virp
,
21930 (int)(tab
- virp
->vir_line
+ 1), TRUE
);
21932 else if (type
== VAR_FLOAT
)
21933 (void)string2float(tab
+ 1, &tv
.vval
.v_float
);
21936 tv
.vval
.v_number
= atol((char *)tab
+ 1);
21937 set_var(virp
->vir_line
+ 1, &tv
, FALSE
);
21938 if (type
== VAR_STRING
)
21939 vim_free(tv
.vval
.v_string
);
21944 return viminfo_readline(virp
);
21948 * Write global vars that start with a capital to the viminfo file
21951 write_viminfo_varlist(fp
)
21955 dictitem_T
*this_var
;
21960 char_u numbuf
[NUMBUFLEN
];
21962 if (find_viminfo_parameter('!') == NULL
)
21965 fprintf(fp
, _("\n# global variables:\n"));
21967 todo
= (int)globvarht
.ht_used
;
21968 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
21970 if (!HASHITEM_EMPTY(hi
))
21973 this_var
= HI2DI(hi
);
21974 if (var_flavour(this_var
->di_key
) == VAR_FLAVOUR_VIMINFO
)
21976 switch (this_var
->di_tv
.v_type
)
21978 case VAR_STRING
: s
= "STR"; break;
21979 case VAR_NUMBER
: s
= "NUM"; break;
21981 case VAR_FLOAT
: s
= "FLO"; break;
21985 fprintf(fp
, "!%s\t%s\t", this_var
->di_key
, s
);
21986 p
= echo_string(&this_var
->di_tv
, &tofree
, numbuf
, 0);
21988 viminfo_writestring(fp
, p
);
21996 #if defined(FEAT_SESSION) || defined(PROTO)
21998 store_session_globals(fd
)
22002 dictitem_T
*this_var
;
22006 todo
= (int)globvarht
.ht_used
;
22007 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
22009 if (!HASHITEM_EMPTY(hi
))
22012 this_var
= HI2DI(hi
);
22013 if ((this_var
->di_tv
.v_type
== VAR_NUMBER
22014 || this_var
->di_tv
.v_type
== VAR_STRING
)
22015 && var_flavour(this_var
->di_key
) == VAR_FLAVOUR_SESSION
)
22017 /* Escape special characters with a backslash. Turn a LF and
22018 * CR into \n and \r. */
22019 p
= vim_strsave_escaped(get_tv_string(&this_var
->di_tv
),
22020 (char_u
*)"\\\"\n\r");
22021 if (p
== NULL
) /* out of memory */
22023 for (t
= p
; *t
!= NUL
; ++t
)
22026 else if (*t
== '\r')
22028 if ((fprintf(fd
, "let %s = %c%s%c",
22030 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
22033 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
22035 || put_eol(fd
) == FAIL
)
22043 else if (this_var
->di_tv
.v_type
== VAR_FLOAT
22044 && var_flavour(this_var
->di_key
) == VAR_FLAVOUR_SESSION
)
22046 float_T f
= this_var
->di_tv
.vval
.v_float
;
22054 if ((fprintf(fd
, "let %s = %c&%f",
22055 this_var
->di_key
, sign
, f
) < 0)
22056 || put_eol(fd
) == FAIL
)
22067 * Display script name where an item was last set.
22068 * Should only be invoked when 'verbose' is non-zero.
22071 last_set_msg(scriptID
)
22078 p
= home_replace_save(NULL
, get_scriptname(scriptID
));
22082 MSG_PUTS(_("\n\tLast set from "));
22091 * List v:oldfiles in a nice way.
22095 exarg_T
*eap UNUSED
;
22097 list_T
*l
= vimvars
[VV_OLDFILES
].vv_list
;
22102 msg((char_u
*)_("No old files"));
22107 for (li
= l
->lv_first
; li
!= NULL
&& !got_int
; li
= li
->li_next
)
22109 msg_outnum((long)++nr
);
22111 msg_outtrans(get_tv_string(&li
->li_tv
));
22113 out_flush(); /* output one line at a time */
22116 /* Assume "got_int" was set to truncate the listing. */
22119 #ifdef FEAT_BROWSE_CMD
22123 nr
= prompt_for_number(FALSE
);
22127 char_u
*p
= list_find_str(get_vim_var_list(VV_OLDFILES
),
22132 p
= expand_env_save(p
);
22134 eap
->cmdidx
= CMD_edit
;
22135 cmdmod
.browse
= FALSE
;
22136 do_exedit(eap
, NULL
);
22145 #endif /* FEAT_EVAL */
22148 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
22152 * Functions for ":8" filename modifier: get 8.3 version of a filename.
22154 static int get_short_pathname
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
22155 static int shortpath_for_invalid_fname
__ARGS((char_u
**fname
, char_u
**bufp
, int *fnamelen
));
22156 static int shortpath_for_partial
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
22159 * Get the short path (8.3) for the filename in "fnamep".
22160 * Only works for a valid file name.
22161 * When the path gets longer "fnamep" is changed and the allocated buffer
22162 * is put in "bufp".
22163 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
22164 * Returns OK on success, FAIL on failure.
22167 get_short_pathname(fnamep
, bufp
, fnamelen
)
22176 l
= GetShortPathName(*fnamep
, *fnamep
, len
);
22179 /* If that doesn't work (not enough space), then save the string
22180 * and try again with a new buffer big enough. */
22181 newbuf
= vim_strnsave(*fnamep
, l
);
22182 if (newbuf
== NULL
)
22186 *fnamep
= *bufp
= newbuf
;
22188 /* Really should always succeed, as the buffer is big enough. */
22189 l
= GetShortPathName(*fnamep
, *fnamep
, l
+1);
22197 * Get the short path (8.3) for the filename in "fname". The converted
22198 * path is returned in "bufp".
22200 * Some of the directories specified in "fname" may not exist. This function
22201 * will shorten the existing directories at the beginning of the path and then
22202 * append the remaining non-existing path.
22204 * fname - Pointer to the filename to shorten. On return, contains the
22205 * pointer to the shortened pathname
22206 * bufp - Pointer to an allocated buffer for the filename.
22207 * fnamelen - Length of the filename pointed to by fname
22209 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
22212 shortpath_for_invalid_fname(fname
, bufp
, fnamelen
)
22217 char_u
*short_fname
, *save_fname
, *pbuf_unused
;
22218 char_u
*endp
, *save_endp
;
22221 int new_len
, sfx_len
;
22225 old_len
= *fnamelen
;
22226 save_fname
= vim_strnsave(*fname
, old_len
);
22227 pbuf_unused
= NULL
;
22228 short_fname
= NULL
;
22230 endp
= save_fname
+ old_len
- 1; /* Find the end of the copy */
22234 * Try shortening the supplied path till it succeeds by removing one
22235 * directory at a time from the tail of the path.
22240 /* go back one path-separator */
22241 while (endp
> save_fname
&& !after_pathsep(save_fname
, endp
+ 1))
22243 if (endp
<= save_fname
)
22244 break; /* processed the complete path */
22247 * Replace the path separator with a NUL and try to shorten the
22252 short_fname
= save_fname
;
22253 len
= (int)STRLEN(short_fname
) + 1;
22254 if (get_short_pathname(&short_fname
, &pbuf_unused
, &len
) == FAIL
)
22259 *endp
= ch
; /* preserve the string */
22262 break; /* successfully shortened the path */
22264 /* failed to shorten the path. Skip the path separator */
22271 * Succeeded in shortening the path. Now concatenate the shortened
22272 * path with the remaining path at the tail.
22275 /* Compute the length of the new path. */
22276 sfx_len
= (int)(save_endp
- endp
) + 1;
22277 new_len
= len
+ sfx_len
;
22279 *fnamelen
= new_len
;
22281 if (new_len
> old_len
)
22283 /* There is not enough space in the currently allocated string,
22284 * copy it to a buffer big enough. */
22285 *fname
= *bufp
= vim_strnsave(short_fname
, new_len
);
22286 if (*fname
== NULL
)
22294 /* Transfer short_fname to the main buffer (it's big enough),
22295 * unless get_short_pathname() did its work in-place. */
22296 *fname
= *bufp
= save_fname
;
22297 if (short_fname
!= save_fname
)
22298 vim_strncpy(save_fname
, short_fname
, len
);
22302 /* concat the not-shortened part of the path */
22303 vim_strncpy(*fname
+ len
, endp
, sfx_len
);
22304 (*fname
)[new_len
] = NUL
;
22308 vim_free(pbuf_unused
);
22309 vim_free(save_fname
);
22315 * Get a pathname for a partial path.
22316 * Returns OK for success, FAIL for failure.
22319 shortpath_for_partial(fnamep
, bufp
, fnamelen
)
22324 int sepcount
, len
, tflen
;
22326 char_u
*pbuf
, *tfname
;
22329 /* Count up the path separators from the RHS.. so we know which part
22330 * of the path to return. */
22332 for (p
= *fnamep
; p
< *fnamep
+ *fnamelen
; mb_ptr_adv(p
))
22333 if (vim_ispathsep(*p
))
22336 /* Need full path first (use expand_env() to remove a "~/") */
22337 hasTilde
= (**fnamep
== '~');
22339 pbuf
= tfname
= expand_env_save(*fnamep
);
22341 pbuf
= tfname
= FullName_save(*fnamep
, FALSE
);
22343 len
= tflen
= (int)STRLEN(tfname
);
22345 if (get_short_pathname(&tfname
, &pbuf
, &len
) == FAIL
)
22350 /* Don't have a valid filename, so shorten the rest of the
22351 * path if we can. This CAN give us invalid 8.3 filenames, but
22352 * there's not a lot of point in guessing what it might be.
22355 if (shortpath_for_invalid_fname(&tfname
, &pbuf
, &len
) == FAIL
)
22359 /* Count the paths backward to find the beginning of the desired string. */
22360 for (p
= tfname
+ len
- 1; p
>= tfname
; --p
)
22364 p
-= mb_head_off(tfname
, p
);
22366 if (vim_ispathsep(*p
))
22368 if (sepcount
== 0 || (hasTilde
&& sepcount
== 1))
22385 /* Copy in the string - p indexes into tfname - allocated at pbuf */
22387 *fnamelen
= (int)STRLEN(p
);
22393 #endif /* WIN3264 */
22396 * Adjust a filename, according to a string of modifiers.
22397 * *fnamep must be NUL terminated when called. When returning, the length is
22398 * determined by *fnamelen.
22399 * Returns VALID_ flags or -1 for failure.
22400 * When there is an error, *fnamep is set to NULL.
22403 modify_fname(src
, usedlen
, fnamep
, bufp
, fnamelen
)
22404 char_u
*src
; /* string with modifiers */
22405 int *usedlen
; /* characters after src that are used */
22406 char_u
**fnamep
; /* file name so far */
22407 char_u
**bufp
; /* buffer for allocated file name or NULL */
22408 int *fnamelen
; /* length of fnamep */
22412 char_u
*s
, *p
, *pbuf
;
22413 char_u dirname
[MAXPATHL
];
22415 int has_fullname
= 0;
22417 int has_shortname
= 0;
22421 /* ":p" - full path/file_name */
22422 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'p')
22426 valid
|= VALID_PATH
;
22429 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
22430 if ((*fnamep
)[0] == '~'
22431 #if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
22432 && ((*fnamep
)[1] == '/'
22433 # ifdef BACKSLASH_IN_FILENAME
22434 || (*fnamep
)[1] == '\\'
22436 || (*fnamep
)[1] == NUL
)
22441 *fnamep
= expand_env_save(*fnamep
);
22442 vim_free(*bufp
); /* free any allocated file name */
22444 if (*fnamep
== NULL
)
22448 /* When "/." or "/.." is used: force expansion to get rid of it. */
22449 for (p
= *fnamep
; *p
!= NUL
; mb_ptr_adv(p
))
22451 if (vim_ispathsep(*p
)
22454 || vim_ispathsep(p
[2])
22456 && (p
[3] == NUL
|| vim_ispathsep(p
[3])))))
22460 /* FullName_save() is slow, don't use it when not needed. */
22461 if (*p
!= NUL
|| !vim_isAbsName(*fnamep
))
22463 *fnamep
= FullName_save(*fnamep
, *p
!= NUL
);
22464 vim_free(*bufp
); /* free any allocated file name */
22466 if (*fnamep
== NULL
)
22470 /* Append a path separator to a directory. */
22471 if (mch_isdir(*fnamep
))
22473 /* Make room for one or two extra characters. */
22474 *fnamep
= vim_strnsave(*fnamep
, (int)STRLEN(*fnamep
) + 2);
22475 vim_free(*bufp
); /* free any allocated file name */
22477 if (*fnamep
== NULL
)
22479 add_pathsep(*fnamep
);
22483 /* ":." - path relative to the current directory */
22484 /* ":~" - path relative to the home directory */
22485 /* ":8" - shortname path - postponed till after */
22486 while (src
[*usedlen
] == ':'
22487 && ((c
= src
[*usedlen
+ 1]) == '.' || c
== '~' || c
== '8'))
22493 has_shortname
= 1; /* Postpone this. */
22498 /* Need full path first (use expand_env() to remove a "~/") */
22501 if (c
== '.' && **fnamep
== '~')
22502 p
= pbuf
= expand_env_save(*fnamep
);
22504 p
= pbuf
= FullName_save(*fnamep
, FALSE
);
22515 mch_dirname(dirname
, MAXPATHL
);
22516 s
= shorten_fname(p
, dirname
);
22522 vim_free(*bufp
); /* free any allocated file name */
22530 home_replace(NULL
, p
, dirname
, MAXPATHL
, TRUE
);
22531 /* Only replace it when it starts with '~' */
22532 if (*dirname
== '~')
22534 s
= vim_strsave(dirname
);
22547 tail
= gettail(*fnamep
);
22548 *fnamelen
= (int)STRLEN(*fnamep
);
22550 /* ":h" - head, remove "/file_name", can be repeated */
22551 /* Don't remove the first "/" or "c:\" */
22552 while (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'h')
22554 valid
|= VALID_HEAD
;
22556 s
= get_past_head(*fnamep
);
22557 while (tail
> s
&& after_pathsep(s
, tail
))
22558 mb_ptr_back(*fnamep
, tail
);
22559 *fnamelen
= (int)(tail
- *fnamep
);
22562 *fnamelen
+= 1; /* the path separator is part of the path */
22564 if (*fnamelen
== 0)
22566 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
22567 p
= vim_strsave((char_u
*)".");
22571 *bufp
= *fnamep
= tail
= p
;
22576 while (tail
> s
&& !after_pathsep(s
, tail
))
22577 mb_ptr_back(*fnamep
, tail
);
22581 /* ":8" - shortname */
22582 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == '8')
22591 /* Check shortname after we have done 'heads' and before we do 'tails'
22596 /* Copy the string if it is shortened by :h */
22597 if (*fnamelen
< (int)STRLEN(*fnamep
))
22599 p
= vim_strnsave(*fnamep
, *fnamelen
);
22603 *bufp
= *fnamep
= p
;
22606 /* Split into two implementations - makes it easier. First is where
22607 * there isn't a full name already, second is where there is.
22609 if (!has_fullname
&& !vim_isAbsName(*fnamep
))
22611 if (shortpath_for_partial(fnamep
, bufp
, fnamelen
) == FAIL
)
22618 /* Simple case, already have the full-name
22619 * Nearly always shorter, so try first time. */
22621 if (get_short_pathname(fnamep
, bufp
, &l
) == FAIL
)
22626 /* Couldn't find the filename.. search the paths.
22629 if (shortpath_for_invalid_fname(fnamep
, bufp
, &l
) == FAIL
)
22635 #endif /* WIN3264 */
22637 /* ":t" - tail, just the basename */
22638 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 't')
22641 *fnamelen
-= (int)(tail
- *fnamep
);
22645 /* ":e" - extension, can be repeated */
22646 /* ":r" - root, without extension, can be repeated */
22647 while (src
[*usedlen
] == ':'
22648 && (src
[*usedlen
+ 1] == 'e' || src
[*usedlen
+ 1] == 'r'))
22650 /* find a '.' in the tail:
22651 * - for second :e: before the current fname
22652 * - otherwise: The last '.'
22654 if (src
[*usedlen
+ 1] == 'e' && *fnamep
> tail
)
22657 s
= *fnamep
+ *fnamelen
- 1;
22658 for ( ; s
> tail
; --s
)
22661 if (src
[*usedlen
+ 1] == 'e') /* :e */
22665 *fnamelen
+= (int)(*fnamep
- (s
+ 1));
22668 /* cut version from the extension */
22669 s
= *fnamep
+ *fnamelen
- 1;
22670 for ( ; s
> *fnamep
; --s
)
22674 *fnamelen
= s
- *fnamep
;
22677 else if (*fnamep
<= tail
)
22682 if (s
> tail
) /* remove one extension */
22683 *fnamelen
= (int)(s
- *fnamep
);
22688 /* ":s?pat?foo?" - substitute */
22689 /* ":gs?pat?foo?" - global substitute */
22690 if (src
[*usedlen
] == ':'
22691 && (src
[*usedlen
+ 1] == 's'
22692 || (src
[*usedlen
+ 1] == 'g' && src
[*usedlen
+ 2] == 's')))
22701 flags
= (char_u
*)"";
22702 s
= src
+ *usedlen
+ 2;
22703 if (src
[*usedlen
+ 1] == 'g')
22705 flags
= (char_u
*)"g";
22712 /* find end of pattern */
22713 p
= vim_strchr(s
, sep
);
22716 pat
= vim_strnsave(s
, (int)(p
- s
));
22720 /* find end of substitution */
22721 p
= vim_strchr(s
, sep
);
22724 sub
= vim_strnsave(s
, (int)(p
- s
));
22725 str
= vim_strnsave(*fnamep
, *fnamelen
);
22726 if (sub
!= NULL
&& str
!= NULL
)
22728 *usedlen
= (int)(p
+ 1 - src
);
22729 s
= do_string_sub(str
, pat
, sub
, flags
);
22733 *fnamelen
= (int)STRLEN(s
);
22745 /* after using ":s", repeat all the modifiers */
22755 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
22756 * "flags" can be "g" to do a global substitute.
22757 * Returns an allocated string, NULL for error.
22760 do_string_sub(str
, pat
, sub
, flags
)
22767 regmatch_T regmatch
;
22775 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
22777 p_cpo
= empty_option
;
22779 ga_init2(&ga
, 1, 200);
22781 do_all
= (flags
[0] == 'g');
22783 regmatch
.rm_ic
= p_ic
;
22784 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
22785 if (regmatch
.regprog
!= NULL
)
22788 while (vim_regexec_nl(®match
, str
, (colnr_T
)(tail
- str
)))
22791 * Get some space for a temporary buffer to do the substitution
22792 * into. It will contain:
22793 * - The text up to where the match is.
22794 * - The substituted text.
22795 * - The text after the match.
22797 sublen
= vim_regsub(®match
, sub
, tail
, FALSE
, TRUE
, FALSE
);
22798 if (ga_grow(&ga
, (int)(STRLEN(tail
) + sublen
-
22799 (regmatch
.endp
[0] - regmatch
.startp
[0]))) == FAIL
)
22805 /* copy the text up to where the match is */
22806 i
= (int)(regmatch
.startp
[0] - tail
);
22807 mch_memmove((char_u
*)ga
.ga_data
+ ga
.ga_len
, tail
, (size_t)i
);
22808 /* add the substituted text */
22809 (void)vim_regsub(®match
, sub
, (char_u
*)ga
.ga_data
22810 + ga
.ga_len
+ i
, TRUE
, TRUE
, FALSE
);
22811 ga
.ga_len
+= i
+ sublen
- 1;
22812 /* avoid getting stuck on a match with an empty string */
22813 if (tail
== regmatch
.endp
[0])
22817 *((char_u
*)ga
.ga_data
+ ga
.ga_len
) = *tail
++;
22822 tail
= regmatch
.endp
[0];
22830 if (ga
.ga_data
!= NULL
)
22831 STRCPY((char *)ga
.ga_data
+ ga
.ga_len
, tail
);
22833 vim_free(regmatch
.regprog
);
22836 ret
= vim_strsave(ga
.ga_data
== NULL
? str
: (char_u
*)ga
.ga_data
);
22838 if (p_cpo
== empty_option
)
22841 /* Darn, evaluating {sub} expression changed the value. */
22842 free_string_option(save_cpo
);
22847 #endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */