1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
11 * eval.c: Expression evaluation.
13 #if defined(MSDOS) || defined(MSWIN)
14 # include "vimio.h" /* for mch_open(), must be before vim.h */
20 # include <time.h> /* for strftime() */
24 # include <time.h> /* for time_t */
31 #if defined(FEAT_EVAL) || defined(PROTO)
33 #define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
36 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
38 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 static dictitem_T dumdi
;
43 #define DI2HIKEY(di) ((di)->di_key)
44 #define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
45 #define HI2DI(hi) HIKEY2DI((hi)->hi_key)
48 * Structure returned by get_lval() and used by set_var_lval().
50 * "name" points to the variable name.
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
71 * "tv" points to the Dictionary typval_T
72 * "newkey" is the key for the new item.
76 char_u
*ll_name
; /* start of variable name (can be NULL) */
77 char_u
*ll_exp_name
; /* NULL or expanded name in allocated memory. */
78 typval_T
*ll_tv
; /* Typeval of item being used. If "newkey"
79 isn't NULL it's the Dict to which to add
81 listitem_T
*ll_li
; /* The list item or NULL. */
82 list_T
*ll_list
; /* The list or NULL. */
83 int ll_range
; /* TRUE when a [i:j] range was used */
84 long ll_n1
; /* First index for list */
85 long ll_n2
; /* Second index for list range */
86 int ll_empty2
; /* Second index is empty: [i:] */
87 dict_T
*ll_dict
; /* The Dictionary or NULL */
88 dictitem_T
*ll_di
; /* The dictitem or NULL */
89 char_u
*ll_newkey
; /* New key for Dict in alloc. mem or NULL. */
93 static char *e_letunexp
= N_("E18: Unexpected characters in :let");
94 static char *e_listidx
= N_("E684: list index out of range: %ld");
95 static char *e_undefvar
= N_("E121: Undefined variable: %s");
96 static char *e_missbrac
= N_("E111: Missing ']'");
97 static char *e_listarg
= N_("E686: Argument of %s must be a List");
98 static char *e_listdictarg
= N_("E712: Argument of %s must be a List or Dictionary");
99 static char *e_emptykey
= N_("E713: Cannot use empty key for Dictionary");
100 static char *e_listreq
= N_("E714: List required");
101 static char *e_dictreq
= N_("E715: Dictionary required");
102 static char *e_toomanyarg
= N_("E118: Too many arguments for function: %s");
103 static char *e_dictkey
= N_("E716: Key not present in Dictionary: %s");
104 static char *e_funcexts
= N_("E122: Function %s already exists, add ! to replace it");
105 static char *e_funcdict
= N_("E717: Dictionary entry already exists");
106 static char *e_funcref
= N_("E718: Funcref required");
107 static char *e_dictrange
= N_("E719: Cannot use [:] with a Dictionary");
108 static char *e_letwrong
= N_("E734: Wrong variable type for %s=");
109 static char *e_nofunc
= N_("E130: Unknown function: %s");
110 static char *e_illvar
= N_("E461: Illegal variable name: %s");
112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
115 static dict_T globvardict
;
116 static dictitem_T globvars_var
;
117 #define globvarht globvardict.dv_hashtab
120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
123 static hashtab_T compat_hashtab
;
126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
129 static int current_copyID
= 0;
132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
141 static garray_T ga_scripts
= {0, 0, sizeof(scriptvar_T
), 4, NULL
};
142 #define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143 #define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
145 static int echo_attr
= 0; /* attributes used for ":echo" */
147 /* Values for trans_function_name() argument: */
148 #define TFN_INT 1 /* internal function name OK */
149 #define TFN_QUIET 2 /* no error messages */
152 * Structure to hold info for a user function.
154 typedef struct ufunc ufunc_T
;
158 int uf_varargs
; /* variable nr of arguments */
160 int uf_calls
; /* nr of active calls */
161 garray_T uf_args
; /* arguments */
162 garray_T uf_lines
; /* function lines */
164 int uf_profiling
; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count
; /* nr of calls */
167 proftime_T uf_tm_total
; /* time spend in function + children */
168 proftime_T uf_tm_self
; /* time spend in function itself */
169 proftime_T uf_tm_children
; /* time spent in children this call */
170 /* profiling the function per line */
171 int *uf_tml_count
; /* nr of times line was executed */
172 proftime_T
*uf_tml_total
; /* time spend in a line + children */
173 proftime_T
*uf_tml_self
; /* time spend in a line itself */
174 proftime_T uf_tml_start
; /* start time for current line */
175 proftime_T uf_tml_children
; /* time spent in children for this line */
176 proftime_T uf_tml_wait
; /* start wait time for current line */
177 int uf_tml_idx
; /* index of line being timed; -1 if none */
178 int uf_tml_execed
; /* line being timed was executed */
180 scid_T uf_script_ID
; /* ID of script where function was defined,
181 used for s: variables */
182 int uf_refcount
; /* for numbered function: reference count */
183 char_u uf_name
[1]; /* name of function (actually longer); can
184 start with <SNR>123_ (<SNR> is K_SPECIAL
189 #define FC_ABORT 1 /* abort function on error */
190 #define FC_RANGE 2 /* function accepts range */
191 #define FC_DICT 4 /* Dict function, uses "self" */
194 * All user-defined functions are found in this hashtable.
196 static hashtab_T func_hashtab
;
198 /* The names of packages that once were loaded are remembered. */
199 static garray_T ga_loaded
= {0, 0, sizeof(char_u
*), 4, NULL
};
201 /* list heads for garbage collection */
202 static dict_T
*first_dict
= NULL
; /* list of all dicts */
203 static list_T
*first_list
= NULL
; /* list of all lists */
205 /* From user function to hashitem and back. */
206 static ufunc_T dumuf
;
207 #define UF2HIKEY(fp) ((fp)->uf_name)
208 #define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209 #define HI2UF(hi) HIKEY2UF((hi)->hi_key)
211 #define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212 #define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
214 #define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215 #define VAR_SHORT_LEN 20 /* short variable name length */
216 #define FIXVAR_CNT 12 /* number of fixed variables */
218 /* structure to hold info for a function that is currently being executed. */
219 typedef struct funccall_S funccall_T
;
223 ufunc_T
*func
; /* function being called */
224 int linenr
; /* next line to be executed */
225 int returned
; /* ":return" used */
226 struct /* fixed variables for arguments */
228 dictitem_T var
; /* variable (without room for name) */
229 char_u room
[VAR_SHORT_LEN
]; /* room for the name */
230 } fixvar
[FIXVAR_CNT
];
231 dict_T l_vars
; /* l: local function variables */
232 dictitem_T l_vars_var
; /* variable for l: scope */
233 dict_T l_avars
; /* a: argument variables */
234 dictitem_T l_avars_var
; /* variable for a: scope */
235 list_T l_varlist
; /* list for a:000 */
236 listitem_T l_listitems
[MAX_FUNC_ARGS
]; /* listitems for a:000 */
237 typval_T
*rettv
; /* return value */
238 linenr_T breakpoint
; /* next line with breakpoint or zero */
239 int dbg_tick
; /* debug_tick when breakpoint was set */
240 int level
; /* top nesting level of executed function */
242 proftime_T prof_child
; /* time spent in a child */
244 funccall_T
*caller
; /* calling function or NULL */
248 * Info used by a ":for" loop.
252 int fi_semicolon
; /* TRUE if ending in '; var]' */
253 int fi_varcount
; /* nr of variables in the list */
254 listwatch_T fi_lw
; /* keep an eye on the item used. */
255 list_T
*fi_list
; /* list being used */
259 * Struct used by trans_function_name()
263 dict_T
*fd_dict
; /* Dictionary used */
264 char_u
*fd_newkey
; /* new key in "dict" in allocated memory */
265 dictitem_T
*fd_di
; /* Dictionary item used */
270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
277 /* values for vv_flags: */
278 #define VV_COMPAT 1 /* compatible, also used without "v:" */
279 #define VV_RO 2 /* read-only */
280 #define VV_RO_SBX 4 /* read-only in the sandbox */
282 #define VV_NAME(s, t) s, {{t}}, {0}
286 char *vv_name
; /* name of variable, without v: */
287 dictitem_T vv_di
; /* value and name for key */
288 char vv_filler
[16]; /* space for LONGEST name below!!! */
289 char vv_flags
; /* VV_COMPAT, VV_RO, VV_RO_SBX */
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
296 {VV_NAME("count", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
297 {VV_NAME("count1", VAR_NUMBER
), VV_RO
},
298 {VV_NAME("prevcount", VAR_NUMBER
), VV_RO
},
299 {VV_NAME("errmsg", VAR_STRING
), VV_COMPAT
},
300 {VV_NAME("warningmsg", VAR_STRING
), 0},
301 {VV_NAME("statusmsg", VAR_STRING
), 0},
302 {VV_NAME("shell_error", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
303 {VV_NAME("this_session", VAR_STRING
), VV_COMPAT
},
304 {VV_NAME("version", VAR_NUMBER
), VV_COMPAT
+VV_RO
},
305 {VV_NAME("lnum", VAR_NUMBER
), VV_RO_SBX
},
306 {VV_NAME("termresponse", VAR_STRING
), VV_RO
},
307 {VV_NAME("fname", VAR_STRING
), VV_RO
},
308 {VV_NAME("lang", VAR_STRING
), VV_RO
},
309 {VV_NAME("lc_time", VAR_STRING
), VV_RO
},
310 {VV_NAME("ctype", VAR_STRING
), VV_RO
},
311 {VV_NAME("charconvert_from", VAR_STRING
), VV_RO
},
312 {VV_NAME("charconvert_to", VAR_STRING
), VV_RO
},
313 {VV_NAME("fname_in", VAR_STRING
), VV_RO
},
314 {VV_NAME("fname_out", VAR_STRING
), VV_RO
},
315 {VV_NAME("fname_new", VAR_STRING
), VV_RO
},
316 {VV_NAME("fname_diff", VAR_STRING
), VV_RO
},
317 {VV_NAME("cmdarg", VAR_STRING
), VV_RO
},
318 {VV_NAME("foldstart", VAR_NUMBER
), VV_RO_SBX
},
319 {VV_NAME("foldend", VAR_NUMBER
), VV_RO_SBX
},
320 {VV_NAME("folddashes", VAR_STRING
), VV_RO_SBX
},
321 {VV_NAME("foldlevel", VAR_NUMBER
), VV_RO_SBX
},
322 {VV_NAME("progname", VAR_STRING
), VV_RO
},
323 {VV_NAME("servername", VAR_STRING
), VV_RO
},
324 {VV_NAME("dying", VAR_NUMBER
), VV_RO
},
325 {VV_NAME("exception", VAR_STRING
), VV_RO
},
326 {VV_NAME("throwpoint", VAR_STRING
), VV_RO
},
327 {VV_NAME("register", VAR_STRING
), VV_RO
},
328 {VV_NAME("cmdbang", VAR_NUMBER
), VV_RO
},
329 {VV_NAME("insertmode", VAR_STRING
), VV_RO
},
330 {VV_NAME("val", VAR_UNKNOWN
), VV_RO
},
331 {VV_NAME("key", VAR_UNKNOWN
), VV_RO
},
332 {VV_NAME("profiling", VAR_NUMBER
), VV_RO
},
333 {VV_NAME("fcs_reason", VAR_STRING
), VV_RO
},
334 {VV_NAME("fcs_choice", VAR_STRING
), 0},
335 {VV_NAME("beval_bufnr", VAR_NUMBER
), VV_RO
},
336 {VV_NAME("beval_winnr", VAR_NUMBER
), VV_RO
},
337 {VV_NAME("beval_lnum", VAR_NUMBER
), VV_RO
},
338 {VV_NAME("beval_col", VAR_NUMBER
), VV_RO
},
339 {VV_NAME("beval_text", VAR_STRING
), VV_RO
},
340 {VV_NAME("scrollstart", VAR_STRING
), 0},
341 {VV_NAME("swapname", VAR_STRING
), VV_RO
},
342 {VV_NAME("swapchoice", VAR_STRING
), 0},
343 {VV_NAME("swapcommand", VAR_STRING
), VV_RO
},
344 {VV_NAME("char", VAR_STRING
), VV_RO
},
345 {VV_NAME("mouse_win", VAR_NUMBER
), 0},
346 {VV_NAME("mouse_lnum", VAR_NUMBER
), 0},
347 {VV_NAME("mouse_col", VAR_NUMBER
), 0},
348 {VV_NAME("operator", VAR_STRING
), VV_RO
},
352 #define vv_type vv_di.di_tv.v_type
353 #define vv_nr vv_di.di_tv.vval.v_number
354 #define vv_str vv_di.di_tv.vval.v_string
355 #define vv_tv vv_di.di_tv
358 * The v: variables are stored in dictionary "vimvardict".
359 * "vimvars_var" is the variable that is used for the "l:" scope.
361 static dict_T vimvardict
;
362 static dictitem_T vimvars_var
;
363 #define vimvarht vimvardict.dv_hashtab
365 static void prepare_vimvar
__ARGS((int idx
, typval_T
*save_tv
));
366 static void restore_vimvar
__ARGS((int idx
, typval_T
*save_tv
));
367 #if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
368 static int call_vim_function
__ARGS((char_u
*func
, int argc
, char_u
**argv
, int safe
, typval_T
*rettv
));
370 static int ex_let_vars
__ARGS((char_u
*arg
, typval_T
*tv
, int copy
, int semicolon
, int var_count
, char_u
*nextchars
));
371 static char_u
*skip_var_list
__ARGS((char_u
*arg
, int *var_count
, int *semicolon
));
372 static char_u
*skip_var_one
__ARGS((char_u
*arg
));
373 static void list_hashtable_vars
__ARGS((hashtab_T
*ht
, char_u
*prefix
, int empty
, int *first
));
374 static void list_glob_vars
__ARGS((int *first
));
375 static void list_buf_vars
__ARGS((int *first
));
376 static void list_win_vars
__ARGS((int *first
));
378 static void list_tab_vars
__ARGS((int *first
));
380 static void list_vim_vars
__ARGS((int *first
));
381 static void list_script_vars
__ARGS((int *first
));
382 static void list_func_vars
__ARGS((int *first
));
383 static char_u
*list_arg_vars
__ARGS((exarg_T
*eap
, char_u
*arg
, int *first
));
384 static char_u
*ex_let_one
__ARGS((char_u
*arg
, typval_T
*tv
, int copy
, char_u
*endchars
, char_u
*op
));
385 static int check_changedtick
__ARGS((char_u
*arg
));
386 static char_u
*get_lval
__ARGS((char_u
*name
, typval_T
*rettv
, lval_T
*lp
, int unlet
, int skip
, int quiet
, int fne_flags
));
387 static void clear_lval
__ARGS((lval_T
*lp
));
388 static void set_var_lval
__ARGS((lval_T
*lp
, char_u
*endp
, typval_T
*rettv
, int copy
, char_u
*op
));
389 static int tv_op
__ARGS((typval_T
*tv1
, typval_T
*tv2
, char_u
*op
));
390 static void list_add_watch
__ARGS((list_T
*l
, listwatch_T
*lw
));
391 static void list_rem_watch
__ARGS((list_T
*l
, listwatch_T
*lwrem
));
392 static void list_fix_watch
__ARGS((list_T
*l
, listitem_T
*item
));
393 static void ex_unletlock
__ARGS((exarg_T
*eap
, char_u
*argstart
, int deep
));
394 static int do_unlet_var
__ARGS((lval_T
*lp
, char_u
*name_end
, int forceit
));
395 static int do_lock_var
__ARGS((lval_T
*lp
, char_u
*name_end
, int deep
, int lock
));
396 static void item_lock
__ARGS((typval_T
*tv
, int deep
, int lock
));
397 static int tv_islocked
__ARGS((typval_T
*tv
));
399 static int eval0
__ARGS((char_u
*arg
, typval_T
*rettv
, char_u
**nextcmd
, int evaluate
));
400 static int eval1
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
401 static int eval2
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
402 static int eval3
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
403 static int eval4
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
404 static int eval5
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
405 static int eval6
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
406 static int eval7
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
408 static int eval_index
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int verbose
));
409 static int get_option_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
410 static int get_string_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
411 static int get_lit_string_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
412 static int get_list_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
413 static int rettv_list_alloc
__ARGS((typval_T
*rettv
));
414 static listitem_T
*listitem_alloc
__ARGS((void));
415 static void listitem_free
__ARGS((listitem_T
*item
));
416 static void listitem_remove
__ARGS((list_T
*l
, listitem_T
*item
));
417 static long list_len
__ARGS((list_T
*l
));
418 static int list_equal
__ARGS((list_T
*l1
, list_T
*l2
, int ic
));
419 static int dict_equal
__ARGS((dict_T
*d1
, dict_T
*d2
, int ic
));
420 static int tv_equal
__ARGS((typval_T
*tv1
, typval_T
*tv2
, int ic
));
421 static listitem_T
*list_find
__ARGS((list_T
*l
, long n
));
422 static long list_find_nr
__ARGS((list_T
*l
, long idx
, int *errorp
));
423 static long list_idx_of_item
__ARGS((list_T
*l
, listitem_T
*item
));
424 static void list_append
__ARGS((list_T
*l
, listitem_T
*item
));
425 static int list_append_tv
__ARGS((list_T
*l
, typval_T
*tv
));
426 static int list_append_string
__ARGS((list_T
*l
, char_u
*str
, int len
));
427 static int list_append_number
__ARGS((list_T
*l
, varnumber_T n
));
428 static int list_insert_tv
__ARGS((list_T
*l
, typval_T
*tv
, listitem_T
*item
));
429 static int list_extend
__ARGS((list_T
*l1
, list_T
*l2
, listitem_T
*bef
));
430 static int list_concat
__ARGS((list_T
*l1
, list_T
*l2
, typval_T
*tv
));
431 static list_T
*list_copy
__ARGS((list_T
*orig
, int deep
, int copyID
));
432 static void list_remove
__ARGS((list_T
*l
, listitem_T
*item
, listitem_T
*item2
));
433 static char_u
*list2string
__ARGS((typval_T
*tv
, int copyID
));
434 static int list_join
__ARGS((garray_T
*gap
, list_T
*l
, char_u
*sep
, int echo
, int copyID
));
435 static void set_ref_in_ht
__ARGS((hashtab_T
*ht
, int copyID
));
436 static void set_ref_in_list
__ARGS((list_T
*l
, int copyID
));
437 static void set_ref_in_item
__ARGS((typval_T
*tv
, int copyID
));
438 static void dict_unref
__ARGS((dict_T
*d
));
439 static void dict_free
__ARGS((dict_T
*d
, int recurse
));
440 static dictitem_T
*dictitem_alloc
__ARGS((char_u
*key
));
441 static dictitem_T
*dictitem_copy
__ARGS((dictitem_T
*org
));
442 static void dictitem_remove
__ARGS((dict_T
*dict
, dictitem_T
*item
));
443 static void dictitem_free
__ARGS((dictitem_T
*item
));
444 static dict_T
*dict_copy
__ARGS((dict_T
*orig
, int deep
, int copyID
));
445 static int dict_add
__ARGS((dict_T
*d
, dictitem_T
*item
));
446 static long dict_len
__ARGS((dict_T
*d
));
447 static dictitem_T
*dict_find
__ARGS((dict_T
*d
, char_u
*key
, int len
));
448 static char_u
*dict2string
__ARGS((typval_T
*tv
, int copyID
));
449 static int get_dict_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
450 static char_u
*echo_string
__ARGS((typval_T
*tv
, char_u
**tofree
, char_u
*numbuf
, int copyID
));
451 static char_u
*tv2string
__ARGS((typval_T
*tv
, char_u
**tofree
, char_u
*numbuf
, int copyID
));
452 static char_u
*string_quote
__ARGS((char_u
*str
, int function
));
453 static int get_env_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
454 static int find_internal_func
__ARGS((char_u
*name
));
455 static char_u
*deref_func_name
__ARGS((char_u
*name
, int *lenp
));
456 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
));
457 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
));
458 static void emsg_funcname
__ARGS((char *ermsg
, char_u
*name
));
460 static void f_add
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
461 static void f_append
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
462 static void f_argc
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
463 static void f_argidx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
464 static void f_argv
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
465 static void f_browse
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
466 static void f_browsedir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
467 static void f_bufexists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
468 static void f_buflisted
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
469 static void f_bufloaded
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
470 static void f_bufname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
471 static void f_bufnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
472 static void f_bufwinnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
473 static void f_byte2line
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
474 static void f_byteidx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
475 static void f_call
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
476 static void f_changenr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
477 static void f_char2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
478 static void f_cindent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
479 static void f_clearmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
480 static void f_col
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
481 #if defined(FEAT_INS_EXPAND)
482 static void f_complete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
483 static void f_complete_add
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
484 static void f_complete_check
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
486 static void f_confirm
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
487 static void f_copy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
488 static void f_count
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
489 static void f_cscope_connection
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
490 static void f_cursor
__ARGS((typval_T
*argsvars
, typval_T
*rettv
));
491 static void f_deepcopy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
492 static void f_delete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
493 static void f_did_filetype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
494 static void f_diff_filler
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
495 static void f_diff_hlID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
496 static void f_empty
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
497 static void f_escape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
498 static void f_eval
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
499 static void f_eventhandler
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
500 static void f_executable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
501 static void f_exists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
502 static void f_expand
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
503 static void f_extend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
504 static void f_feedkeys
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
505 static void f_filereadable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
506 static void f_filewritable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
507 static void f_filter
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
508 static void f_finddir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
509 static void f_findfile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
510 static void f_fnamemodify
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
511 static void f_foldclosed
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
512 static void f_foldclosedend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
513 static void f_foldlevel
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
514 static void f_foldtext
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
515 static void f_foldtextresult
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
516 static void f_foreground
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
517 static void f_function
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
518 static void f_garbagecollect
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
519 static void f_get
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
520 static void f_getbufline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
521 static void f_getbufvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
522 static void f_getchar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
523 static void f_getcharmod
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
524 static void f_getcmdline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
525 static void f_getcmdpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
526 static void f_getcmdtype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
527 static void f_getcwd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
528 static void f_getfontname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
529 static void f_getfperm
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
530 static void f_getfsize
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
531 static void f_getftime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
532 static void f_getftype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
533 static void f_getline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
534 static void f_getmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
535 static void f_getpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
536 static void f_getqflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
537 static void f_getreg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
538 static void f_getregtype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
539 static void f_gettabwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
540 static void f_getwinposx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
541 static void f_getwinposy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
542 static void f_getwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
543 static void f_glob
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
544 static void f_globpath
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
545 static void f_has
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
546 static void f_has_key
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
547 static void f_haslocaldir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
548 static void f_hasmapto
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
549 static void f_histadd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
550 static void f_histdel
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
551 static void f_histget
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
552 static void f_histnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
553 static void f_hlID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
554 static void f_hlexists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
555 static void f_hostname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
556 static void f_iconv
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
557 static void f_indent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
558 static void f_index
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
559 static void f_input
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
560 static void f_inputdialog
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
561 static void f_inputlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
562 static void f_inputrestore
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
563 static void f_inputsave
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
564 static void f_inputsecret
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
565 static void f_insert
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
566 static void f_isdirectory
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
567 static void f_islocked
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
568 static void f_items
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
569 static void f_join
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
570 static void f_keys
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
571 static void f_last_buffer_nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
572 static void f_len
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
573 static void f_libcall
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
574 static void f_libcallnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
575 static void f_line
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
576 static void f_line2byte
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
577 static void f_lispindent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
578 static void f_localtime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
579 static void f_map
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
580 static void f_maparg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
581 static void f_mapcheck
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
582 static void f_match
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
583 static void f_matchadd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
584 static void f_matcharg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
585 static void f_matchdelete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
586 static void f_matchend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
587 static void f_matchlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
588 static void f_matchstr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
589 static void f_max
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
590 static void f_min
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
592 static void f_mkdir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
594 static void f_mode
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
595 static void f_nextnonblank
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
596 static void f_nr2char
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
597 static void f_pathshorten
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
598 static void f_prevnonblank
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
599 static void f_printf
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
600 static void f_pumvisible
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
601 static void f_range
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
602 static void f_readfile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
603 static void f_reltime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
604 static void f_reltimestr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
605 static void f_remote_expr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
606 static void f_remote_foreground
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
607 static void f_remote_peek
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
608 static void f_remote_read
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
609 static void f_remote_send
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
610 static void f_remove
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
611 static void f_rename
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
612 static void f_repeat
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
613 static void f_resolve
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
614 static void f_reverse
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
615 static void f_search
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
616 static void f_searchdecl
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
617 static void f_searchpair
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
618 static void f_searchpairpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
619 static void f_searchpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
620 static void f_server2client
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
621 static void f_serverlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
622 static void f_setbufvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
623 static void f_setcmdpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
624 static void f_setline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
625 static void f_setloclist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
626 static void f_setmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
627 static void f_setpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
628 static void f_setqflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
629 static void f_setreg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
630 static void f_settabwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
631 static void f_setwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
632 static void f_shellescape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
633 static void f_simplify
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
634 static void f_sort
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
635 static void f_soundfold
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
636 static void f_spellbadword
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
637 static void f_spellsuggest
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
638 static void f_split
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
639 static void f_str2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
641 static void f_strftime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
643 static void f_stridx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
644 static void f_string
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
645 static void f_strlen
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
646 static void f_strpart
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
647 static void f_strridx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
648 static void f_strtrans
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
649 static void f_submatch
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
650 static void f_substitute
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
651 static void f_synID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
652 static void f_synIDattr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
653 static void f_synIDtrans
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
654 static void f_synstack
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
655 static void f_system
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
656 static void f_tabpagebuflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
657 static void f_tabpagenr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
658 static void f_tabpagewinnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
659 static void f_taglist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
660 static void f_tagfiles
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
661 static void f_tempname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
662 static void f_test
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
663 static void f_tolower
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
664 static void f_toupper
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
665 static void f_tr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
666 static void f_type
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
667 static void f_values
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
668 static void f_virtcol
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
669 static void f_visualmode
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
670 static void f_winbufnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
671 static void f_wincol
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
672 static void f_winheight
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
673 static void f_winline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
674 static void f_winnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
675 static void f_winrestcmd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
676 static void f_winrestview
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
677 static void f_winsaveview
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
678 static void f_winwidth
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
679 static void f_writefile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
681 static int list2fpos
__ARGS((typval_T
*arg
, pos_T
*posp
, int *fnump
));
682 static pos_T
*var2fpos
__ARGS((typval_T
*varp
, int dollar_lnum
, int *fnum
));
683 static int get_env_len
__ARGS((char_u
**arg
));
684 static int get_id_len
__ARGS((char_u
**arg
));
685 static int get_name_len
__ARGS((char_u
**arg
, char_u
**alias
, int evaluate
, int verbose
));
686 static char_u
*find_name_end
__ARGS((char_u
*arg
, char_u
**expr_start
, char_u
**expr_end
, int flags
));
687 #define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
688 #define FNE_CHECK_START 2 /* find_name_end(): check name starts with
690 static char_u
* make_expanded_name
__ARGS((char_u
*in_start
, char_u
*expr_start
, char_u
*expr_end
, char_u
*in_end
));
691 static int eval_isnamec
__ARGS((int c
));
692 static int eval_isnamec1
__ARGS((int c
));
693 static int get_var_tv
__ARGS((char_u
*name
, int len
, typval_T
*rettv
, int verbose
));
694 static int handle_subscript
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int verbose
));
695 static typval_T
*alloc_tv
__ARGS((void));
696 static typval_T
*alloc_string_tv
__ARGS((char_u
*string
));
697 static void init_tv
__ARGS((typval_T
*varp
));
698 static long get_tv_number
__ARGS((typval_T
*varp
));
699 static linenr_T get_tv_lnum
__ARGS((typval_T
*argvars
));
700 static linenr_T get_tv_lnum_buf
__ARGS((typval_T
*argvars
, buf_T
*buf
));
701 static char_u
*get_tv_string
__ARGS((typval_T
*varp
));
702 static char_u
*get_tv_string_buf
__ARGS((typval_T
*varp
, char_u
*buf
));
703 static char_u
*get_tv_string_buf_chk
__ARGS((typval_T
*varp
, char_u
*buf
));
704 static dictitem_T
*find_var
__ARGS((char_u
*name
, hashtab_T
**htp
));
705 static dictitem_T
*find_var_in_ht
__ARGS((hashtab_T
*ht
, char_u
*varname
, int writing
));
706 static hashtab_T
*find_var_ht
__ARGS((char_u
*name
, char_u
**varname
));
707 static void vars_clear_ext
__ARGS((hashtab_T
*ht
, int free_val
));
708 static void delete_var
__ARGS((hashtab_T
*ht
, hashitem_T
*hi
));
709 static void list_one_var
__ARGS((dictitem_T
*v
, char_u
*prefix
, int *first
));
710 static void list_one_var_a
__ARGS((char_u
*prefix
, char_u
*name
, int type
, char_u
*string
, int *first
));
711 static void set_var
__ARGS((char_u
*name
, typval_T
*varp
, int copy
));
712 static int var_check_ro
__ARGS((int flags
, char_u
*name
));
713 static int var_check_fixed
__ARGS((int flags
, char_u
*name
));
714 static int tv_check_lock
__ARGS((int lock
, char_u
*name
));
715 static void copy_tv
__ARGS((typval_T
*from
, typval_T
*to
));
716 static int item_copy
__ARGS((typval_T
*from
, typval_T
*to
, int deep
, int copyID
));
717 static char_u
*find_option_end
__ARGS((char_u
**arg
, int *opt_flags
));
718 static char_u
*trans_function_name
__ARGS((char_u
**pp
, int skip
, int flags
, funcdict_T
*fd
));
719 static int eval_fname_script
__ARGS((char_u
*p
));
720 static int eval_fname_sid
__ARGS((char_u
*p
));
721 static void list_func_head
__ARGS((ufunc_T
*fp
, int indent
));
722 static ufunc_T
*find_func
__ARGS((char_u
*name
));
723 static int function_exists
__ARGS((char_u
*name
));
724 static int builtin_function
__ARGS((char_u
*name
));
726 static void func_do_profile
__ARGS((ufunc_T
*fp
));
727 static void prof_sort_list
__ARGS((FILE *fd
, ufunc_T
**sorttab
, int st_len
, char *title
, int prefer_self
));
728 static void prof_func_line
__ARGS((FILE *fd
, int count
, proftime_T
*total
, proftime_T
*self
, int prefer_self
));
733 prof_total_cmp
__ARGS((const void *s1
, const void *s2
));
738 prof_self_cmp
__ARGS((const void *s1
, const void *s2
));
740 static int script_autoload
__ARGS((char_u
*name
, int reload
));
741 static char_u
*autoload_name
__ARGS((char_u
*name
));
742 static void cat_func_name
__ARGS((char_u
*buf
, ufunc_T
*fp
));
743 static void func_free
__ARGS((ufunc_T
*fp
));
744 static void func_unref
__ARGS((char_u
*name
));
745 static void func_ref
__ARGS((char_u
*name
));
746 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
));
747 static void add_nr_var
__ARGS((dict_T
*dp
, dictitem_T
*v
, char *name
, varnumber_T nr
));
748 static win_T
*find_win_by_nr
__ARGS((typval_T
*vp
, tabpage_T
*tp
));
749 static void getwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int off
));
750 static int searchpair_cmn
__ARGS((typval_T
*argvars
, pos_T
*match_pos
));
751 static int search_cmn
__ARGS((typval_T
*argvars
, pos_T
*match_pos
, int *flagsp
));
752 static void setwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int off
));
754 /* Character used as separated in autoload function/variable names. */
755 #define AUTOLOAD_CHAR '#'
758 * Initialize the global and v: variables.
766 init_var_dict(&globvardict
, &globvars_var
);
767 init_var_dict(&vimvardict
, &vimvars_var
);
768 hash_init(&compat_hashtab
);
769 hash_init(&func_hashtab
);
771 for (i
= 0; i
< VV_LEN
; ++i
)
774 STRCPY(p
->vv_di
.di_key
, p
->vv_name
);
775 if (p
->vv_flags
& VV_RO
)
776 p
->vv_di
.di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
777 else if (p
->vv_flags
& VV_RO_SBX
)
778 p
->vv_di
.di_flags
= DI_FLAGS_RO_SBX
| DI_FLAGS_FIX
;
780 p
->vv_di
.di_flags
= DI_FLAGS_FIX
;
782 /* add to v: scope dict, unless the value is not always available */
783 if (p
->vv_type
!= VAR_UNKNOWN
)
784 hash_add(&vimvarht
, p
->vv_di
.di_key
);
785 if (p
->vv_flags
& VV_COMPAT
)
786 /* add to compat scope dict */
787 hash_add(&compat_hashtab
, p
->vv_di
.di_key
);
791 #if defined(EXITFREE) || defined(PROTO)
798 for (i
= 0; i
< VV_LEN
; ++i
)
801 if (p
->vv_di
.di_tv
.v_type
== VAR_STRING
)
803 vim_free(p
->vv_di
.di_tv
.vval
.v_string
);
804 p
->vv_di
.di_tv
.vval
.v_string
= NULL
;
807 hash_clear(&vimvarht
);
808 hash_clear(&compat_hashtab
);
810 /* script-local variables */
811 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
812 vars_clear(&SCRIPT_VARS(i
));
813 ga_clear(&ga_scripts
);
816 /* global variables */
817 vars_clear(&globvarht
);
820 free_all_functions();
821 hash_clear(&func_hashtab
);
823 /* autoloaded script names */
824 ga_clear_strings(&ga_loaded
);
826 /* unreferenced lists and dicts */
827 (void)garbage_collect();
832 * Return the name of the executed function.
838 return ((funccall_T
*)cookie
)->func
->uf_name
;
842 * Return the address holding the next breakpoint line for a funccall cookie.
845 func_breakpoint(cookie
)
848 return &((funccall_T
*)cookie
)->breakpoint
;
852 * Return the address holding the debug tick for a funccall cookie.
855 func_dbg_tick(cookie
)
858 return &((funccall_T
*)cookie
)->dbg_tick
;
862 * Return the nesting level for a funccall cookie.
868 return ((funccall_T
*)cookie
)->level
;
871 /* pointer to funccal for currently active function */
872 funccall_T
*current_funccal
= NULL
;
875 * Return TRUE when a function was ended by a ":return" command.
878 current_func_returned()
880 return current_funccal
->returned
;
885 * Set an internal variable to a string value. Creates the variable if it does
889 set_internal_string_var(name
, value
)
896 val
= vim_strsave(value
);
899 tvp
= alloc_string_tv(val
);
902 set_var(name
, tvp
, FALSE
);
908 static lval_T
*redir_lval
= NULL
;
909 static garray_T redir_ga
; /* only valid when redir_lval is not NULL */
910 static char_u
*redir_endp
= NULL
;
911 static char_u
*redir_varname
= NULL
;
914 * Start recording command output to a variable
915 * Returns OK if successfully completed the setup. FAIL otherwise.
918 var_redir_start(name
, append
)
920 int append
; /* append to an existing variable */
926 /* Make sure a valid variable name is specified */
927 if (!eval_isnamec1(*name
))
933 redir_varname
= vim_strsave(name
);
934 if (redir_varname
== NULL
)
937 redir_lval
= (lval_T
*)alloc_clear((unsigned)sizeof(lval_T
));
938 if (redir_lval
== NULL
)
944 /* The output is stored in growarray "redir_ga" until redirection ends. */
945 ga_init2(&redir_ga
, (int)sizeof(char), 500);
947 /* Parse the variable name (can be a dict or list entry). */
948 redir_endp
= get_lval(redir_varname
, NULL
, redir_lval
, FALSE
, FALSE
, FALSE
,
950 if (redir_endp
== NULL
|| redir_lval
->ll_name
== NULL
|| *redir_endp
!= NUL
)
952 if (redir_endp
!= NULL
&& *redir_endp
!= NUL
)
953 /* Trailing characters are present after the variable name */
961 /* check if we can write to the variable: set it to or append an empty
963 save_emsg
= did_emsg
;
965 tv
.v_type
= VAR_STRING
;
966 tv
.vval
.v_string
= (char_u
*)"";
968 set_var_lval(redir_lval
, redir_endp
, &tv
, TRUE
, (char_u
*)".");
970 set_var_lval(redir_lval
, redir_endp
, &tv
, TRUE
, (char_u
*)"=");
972 did_emsg
|= save_emsg
;
978 if (redir_lval
->ll_newkey
!= NULL
)
980 /* Dictionary item was created, don't do it again. */
981 vim_free(redir_lval
->ll_newkey
);
982 redir_lval
->ll_newkey
= NULL
;
989 * Append "value[value_len]" to the variable set by var_redir_start().
990 * The actual appending is postponed until redirection ends, because the value
991 * appended may in fact be the string we write to, changing it may cause freed
998 var_redir_str(value
, value_len
)
1004 if (redir_lval
== NULL
)
1007 if (value_len
== -1)
1008 len
= (int)STRLEN(value
); /* Append the entire string */
1010 len
= value_len
; /* Append only "value_len" characters */
1012 if (ga_grow(&redir_ga
, len
) == OK
)
1014 mch_memmove((char *)redir_ga
.ga_data
+ redir_ga
.ga_len
, value
, len
);
1015 redir_ga
.ga_len
+= len
;
1022 * Stop redirecting command output to a variable.
1029 if (redir_lval
!= NULL
)
1031 /* Append the trailing NUL. */
1032 ga_append(&redir_ga
, NUL
);
1034 /* Assign the text to the variable. */
1035 tv
.v_type
= VAR_STRING
;
1036 tv
.vval
.v_string
= redir_ga
.ga_data
;
1037 set_var_lval(redir_lval
, redir_endp
, &tv
, FALSE
, (char_u
*)".");
1038 vim_free(tv
.vval
.v_string
);
1040 clear_lval(redir_lval
);
1041 vim_free(redir_lval
);
1044 vim_free(redir_varname
);
1045 redir_varname
= NULL
;
1048 # if defined(FEAT_MBYTE) || defined(PROTO)
1050 eval_charconvert(enc_from
, enc_to
, fname_from
, fname_to
)
1058 set_vim_var_string(VV_CC_FROM
, enc_from
, -1);
1059 set_vim_var_string(VV_CC_TO
, enc_to
, -1);
1060 set_vim_var_string(VV_FNAME_IN
, fname_from
, -1);
1061 set_vim_var_string(VV_FNAME_OUT
, fname_to
, -1);
1062 if (eval_to_bool(p_ccv
, &err
, NULL
, FALSE
))
1064 set_vim_var_string(VV_CC_FROM
, NULL
, -1);
1065 set_vim_var_string(VV_CC_TO
, NULL
, -1);
1066 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1067 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1075 # if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1077 eval_printexpr(fname
, args
)
1083 set_vim_var_string(VV_FNAME_IN
, fname
, -1);
1084 set_vim_var_string(VV_CMDARG
, args
, -1);
1085 if (eval_to_bool(p_pexpr
, &err
, NULL
, FALSE
))
1087 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1088 set_vim_var_string(VV_CMDARG
, NULL
, -1);
1099 # if defined(FEAT_DIFF) || defined(PROTO)
1101 eval_diff(origfile
, newfile
, outfile
)
1108 set_vim_var_string(VV_FNAME_IN
, origfile
, -1);
1109 set_vim_var_string(VV_FNAME_NEW
, newfile
, -1);
1110 set_vim_var_string(VV_FNAME_OUT
, outfile
, -1);
1111 (void)eval_to_bool(p_dex
, &err
, NULL
, FALSE
);
1112 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1113 set_vim_var_string(VV_FNAME_NEW
, NULL
, -1);
1114 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1118 eval_patch(origfile
, difffile
, outfile
)
1125 set_vim_var_string(VV_FNAME_IN
, origfile
, -1);
1126 set_vim_var_string(VV_FNAME_DIFF
, difffile
, -1);
1127 set_vim_var_string(VV_FNAME_OUT
, outfile
, -1);
1128 (void)eval_to_bool(p_pex
, &err
, NULL
, FALSE
);
1129 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1130 set_vim_var_string(VV_FNAME_DIFF
, NULL
, -1);
1131 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1136 * Top level evaluation function, returning a boolean.
1137 * Sets "error" to TRUE if there was an error.
1138 * Return TRUE or FALSE.
1141 eval_to_bool(arg
, error
, nextcmd
, skip
)
1145 int skip
; /* only parse, don't execute */
1152 if (eval0(arg
, &tv
, nextcmd
, !skip
) == FAIL
)
1159 retval
= (get_tv_number_chk(&tv
, error
) != 0);
1170 * Top level evaluation function, returning a string. If "skip" is TRUE,
1171 * only parsing to "nextcmd" is done, without reporting errors. Return
1172 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1175 eval_to_string_skip(arg
, nextcmd
, skip
)
1178 int skip
; /* only parse, don't execute */
1185 if (eval0(arg
, &tv
, nextcmd
, !skip
) == FAIL
|| skip
)
1189 retval
= vim_strsave(get_tv_string(&tv
));
1199 * Skip over an expression at "*pp".
1200 * Return FAIL for an error, OK otherwise.
1208 *pp
= skipwhite(*pp
);
1209 return eval1(pp
, &rettv
, FALSE
);
1213 * Top level evaluation function, returning a string.
1214 * Return pointer to allocated memory, or NULL for failure.
1217 eval_to_string(arg
, nextcmd
, dolist
)
1220 int dolist
; /* turn List into sequence of lines */
1226 if (eval0(arg
, &tv
, nextcmd
, TRUE
) == FAIL
)
1230 if (dolist
&& tv
.v_type
== VAR_LIST
)
1232 ga_init2(&ga
, (int)sizeof(char), 80);
1233 list_join(&ga
, tv
.vval
.v_list
, (char_u
*)"\n", TRUE
, 0);
1234 ga_append(&ga
, NUL
);
1235 retval
= (char_u
*)ga
.ga_data
;
1238 retval
= vim_strsave(get_tv_string(&tv
));
1246 * Call eval_to_string() without using current local variables and using
1247 * textlock. When "use_sandbox" is TRUE use the sandbox.
1250 eval_to_string_safe(arg
, nextcmd
, use_sandbox
)
1256 void *save_funccalp
;
1258 save_funccalp
= save_funccal();
1262 retval
= eval_to_string(arg
, nextcmd
, FALSE
);
1266 restore_funccal(save_funccalp
);
1271 * Top level evaluation function, returning a number.
1272 * Evaluates "expr" silently.
1273 * Returns -1 for an error.
1276 eval_to_number(expr
)
1281 char_u
*p
= skipwhite(expr
);
1285 if (eval1(&p
, &rettv
, TRUE
) == FAIL
)
1289 retval
= get_tv_number_chk(&rettv
, NULL
);
1298 * Prepare v: variable "idx" to be used.
1299 * Save the current typeval in "save_tv".
1300 * When not used yet add the variable to the v: hashtable.
1303 prepare_vimvar(idx
, save_tv
)
1307 *save_tv
= vimvars
[idx
].vv_tv
;
1308 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1309 hash_add(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1313 * Restore v: variable "idx" to typeval "save_tv".
1314 * When no longer defined, remove the variable from the v: hashtable.
1317 restore_vimvar(idx
, save_tv
)
1323 vimvars
[idx
].vv_tv
= *save_tv
;
1324 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1326 hi
= hash_find(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1327 if (HASHITEM_EMPTY(hi
))
1328 EMSG2(_(e_intern2
), "restore_vimvar()");
1330 hash_remove(&vimvarht
, hi
);
1334 #if defined(FEAT_SPELL) || defined(PROTO)
1336 * Evaluate an expression to a list with suggestions.
1337 * For the "expr:" part of 'spellsuggest'.
1340 eval_spell_expr(badword
, expr
)
1346 list_T
*list
= NULL
;
1347 char_u
*p
= skipwhite(expr
);
1349 /* Set "v:val" to the bad word. */
1350 prepare_vimvar(VV_VAL
, &save_val
);
1351 vimvars
[VV_VAL
].vv_type
= VAR_STRING
;
1352 vimvars
[VV_VAL
].vv_str
= badword
;
1356 if (eval1(&p
, &rettv
, TRUE
) == OK
)
1358 if (rettv
.v_type
!= VAR_LIST
)
1361 list
= rettv
.vval
.v_list
;
1366 restore_vimvar(VV_VAL
, &save_val
);
1372 * "list" is supposed to contain two items: a word and a number. Return the
1373 * word in "pp" and the number as the return value.
1374 * Return -1 if anything isn't right.
1375 * Used to get the good word and score from the eval_spell_expr() result.
1378 get_spellword(list
, pp
)
1384 li
= list
->lv_first
;
1387 *pp
= get_tv_string(&li
->li_tv
);
1392 return get_tv_number(&li
->li_tv
);
1397 * Top level evaluation function.
1398 * Returns an allocated typval_T with the result.
1399 * Returns NULL when there is an error.
1402 eval_expr(arg
, nextcmd
)
1408 tv
= (typval_T
*)alloc(sizeof(typval_T
));
1409 if (tv
!= NULL
&& eval0(arg
, tv
, nextcmd
, TRUE
) == FAIL
)
1419 #if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1420 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1422 * Call some vimL function and return the result in "*rettv".
1423 * Uses argv[argc] for the function arguments.
1424 * Returns OK or FAIL.
1427 call_vim_function(func
, argc
, argv
, safe
, rettv
)
1431 int safe
; /* use the sandbox */
1439 void *save_funccalp
= NULL
;
1442 argvars
= (typval_T
*)alloc((unsigned)((argc
+ 1) * sizeof(typval_T
)));
1443 if (argvars
== NULL
)
1446 for (i
= 0; i
< argc
; i
++)
1448 /* Pass a NULL or empty argument as an empty string */
1449 if (argv
[i
] == NULL
|| *argv
[i
] == NUL
)
1451 argvars
[i
].v_type
= VAR_STRING
;
1452 argvars
[i
].vval
.v_string
= (char_u
*)"";
1456 /* Recognize a number argument, the others must be strings. */
1457 vim_str2nr(argv
[i
], NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
1458 if (len
!= 0 && len
== (int)STRLEN(argv
[i
]))
1460 argvars
[i
].v_type
= VAR_NUMBER
;
1461 argvars
[i
].vval
.v_number
= n
;
1465 argvars
[i
].v_type
= VAR_STRING
;
1466 argvars
[i
].vval
.v_string
= argv
[i
];
1472 save_funccalp
= save_funccal();
1476 rettv
->v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
1477 ret
= call_func(func
, (int)STRLEN(func
), rettv
, argc
, argvars
,
1478 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
1479 &doesrange
, TRUE
, NULL
);
1483 restore_funccal(save_funccalp
);
1493 # if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1495 * Call vimL function "func" and return the result as a string.
1496 * Returns NULL when calling the function fails.
1497 * Uses argv[argc] for the function arguments.
1500 call_func_retstr(func
, argc
, argv
, safe
)
1504 int safe
; /* use the sandbox */
1509 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1512 retval
= vim_strsave(get_tv_string(&rettv
));
1518 # if defined(FEAT_COMPL_FUNC) || defined(PROTO)
1520 * Call vimL function "func" and return the result as a number.
1521 * Returns -1 when calling the function fails.
1522 * Uses argv[argc] for the function arguments.
1525 call_func_retnr(func
, argc
, argv
, safe
)
1529 int safe
; /* use the sandbox */
1534 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1537 retval
= get_tv_number_chk(&rettv
, NULL
);
1544 * Call vimL function "func" and return the result as a list
1545 * Uses argv[argc] for the function arguments.
1548 call_func_retlist(func
, argc
, argv
, safe
)
1552 int safe
; /* use the sandbox */
1556 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1559 if (rettv
.v_type
!= VAR_LIST
)
1565 return rettv
.vval
.v_list
;
1571 * Save the current function call pointer, and set it to NULL.
1572 * Used when executing autocommands and for ":source".
1577 funccall_T
*fc
= current_funccal
;
1579 current_funccal
= NULL
;
1584 restore_funccal(vfc
)
1587 funccall_T
*fc
= (funccall_T
*)vfc
;
1589 current_funccal
= fc
;
1592 #if defined(FEAT_PROFILE) || defined(PROTO)
1594 * Prepare profiling for entering a child or something else that is not
1595 * counted for the script/function itself.
1596 * Should always be called in pair with prof_child_exit().
1599 prof_child_enter(tm
)
1600 proftime_T
*tm
; /* place to store waittime */
1602 funccall_T
*fc
= current_funccal
;
1604 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1605 profile_start(&fc
->prof_child
);
1606 script_prof_save(tm
);
1610 * Take care of time spent in a child.
1611 * Should always be called after prof_child_enter().
1615 proftime_T
*tm
; /* where waittime was stored */
1617 funccall_T
*fc
= current_funccal
;
1619 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1621 profile_end(&fc
->prof_child
);
1622 profile_sub_wait(tm
, &fc
->prof_child
); /* don't count waiting time */
1623 profile_add(&fc
->func
->uf_tm_children
, &fc
->prof_child
);
1624 profile_add(&fc
->func
->uf_tml_children
, &fc
->prof_child
);
1626 script_prof_restore(tm
);
1633 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1634 * it in "*cp". Doesn't give error messages.
1637 eval_foldexpr(arg
, cp
)
1644 int use_sandbox
= was_set_insecurely((char_u
*)"foldexpr",
1652 if (eval0(arg
, &tv
, NULL
, TRUE
) == FAIL
)
1656 /* If the result is a number, just return the number. */
1657 if (tv
.v_type
== VAR_NUMBER
)
1658 retval
= tv
.vval
.v_number
;
1659 else if (tv
.v_type
!= VAR_STRING
|| tv
.vval
.v_string
== NULL
)
1663 /* If the result is a string, check if there is a non-digit before
1665 s
= tv
.vval
.v_string
;
1666 if (!VIM_ISDIGIT(*s
) && *s
!= '-')
1668 retval
= atol((char *)s
);
1682 * ":let" list all variable values
1683 * ":let var1 var2" list variable values
1684 * ":let var = expr" assignment command.
1685 * ":let var += expr" assignment command.
1686 * ":let var -= expr" assignment command.
1687 * ":let var .= expr" assignment command.
1688 * ":let [var1, var2] = expr" unpack list.
1694 char_u
*arg
= eap
->arg
;
1695 char_u
*expr
= NULL
;
1704 argend
= skip_var_list(arg
, &var_count
, &semicolon
);
1707 if (argend
> arg
&& argend
[-1] == '.') /* for var.='str' */
1709 expr
= vim_strchr(argend
, '=');
1713 * ":let" without "=": list variables
1717 else if (!ends_excmd(*arg
))
1718 /* ":let var1 var2" */
1719 arg
= list_arg_vars(eap
, arg
, &first
);
1720 else if (!eap
->skip
)
1723 list_glob_vars(&first
);
1724 list_buf_vars(&first
);
1725 list_win_vars(&first
);
1727 list_tab_vars(&first
);
1729 list_script_vars(&first
);
1730 list_func_vars(&first
);
1731 list_vim_vars(&first
);
1733 eap
->nextcmd
= check_nextcmd(arg
);
1741 if (vim_strchr((char_u
*)"+-.", expr
[-1]) != NULL
)
1742 op
[0] = expr
[-1]; /* +=, -= or .= */
1744 expr
= skipwhite(expr
+ 1);
1748 i
= eval0(expr
, &rettv
, &eap
->nextcmd
, !eap
->skip
);
1757 (void)ex_let_vars(eap
->arg
, &rettv
, FALSE
, semicolon
, var_count
,
1765 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1766 * Handles both "var" with any type and "[var, var; var]" with a list type.
1767 * When "nextchars" is not NULL it points to a string with characters that
1768 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1770 * Returns OK or FAIL;
1773 ex_let_vars(arg_start
, tv
, copy
, semicolon
, var_count
, nextchars
)
1776 int copy
; /* copy values from "tv", don't move */
1777 int semicolon
; /* from skip_var_list() */
1778 int var_count
; /* from skip_var_list() */
1781 char_u
*arg
= arg_start
;
1790 * ":let var = expr" or ":for var in list"
1792 if (ex_let_one(arg
, tv
, copy
, nextchars
, nextchars
) == NULL
)
1798 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1800 if (tv
->v_type
!= VAR_LIST
|| (l
= tv
->vval
.v_list
) == NULL
)
1807 if (semicolon
== 0 && var_count
< i
)
1809 EMSG(_("E687: Less targets than List items"));
1812 if (var_count
- semicolon
> i
)
1814 EMSG(_("E688: More targets than List items"));
1821 arg
= skipwhite(arg
+ 1);
1822 arg
= ex_let_one(arg
, &item
->li_tv
, TRUE
, (char_u
*)",;]", nextchars
);
1823 item
= item
->li_next
;
1827 arg
= skipwhite(arg
);
1830 /* Put the rest of the list (may be empty) in the var after ';'.
1831 * Create a new list for this. */
1835 while (item
!= NULL
)
1837 list_append_tv(l
, &item
->li_tv
);
1838 item
= item
->li_next
;
1841 ltv
.v_type
= VAR_LIST
;
1843 ltv
.vval
.v_list
= l
;
1846 arg
= ex_let_one(skipwhite(arg
+ 1), <v
, FALSE
,
1847 (char_u
*)"]", nextchars
);
1853 else if (*arg
!= ',' && *arg
!= ']')
1855 EMSG2(_(e_intern2
), "ex_let_vars()");
1864 * Skip over assignable variable "var" or list of variables "[var, var]".
1865 * Used for ":let varvar = expr" and ":for varvar in expr".
1866 * For "[var, var]" increment "*var_count" for each variable.
1867 * for "[var, var; var]" set "semicolon".
1868 * Return NULL for an error.
1871 skip_var_list(arg
, var_count
, semicolon
)
1880 /* "[var, var]": find the matching ']'. */
1884 p
= skipwhite(p
+ 1); /* skip whites after '[', ';' or ',' */
1885 s
= skip_var_one(p
);
1888 EMSG2(_(e_invarg2
), p
);
1898 if (*semicolon
== 1)
1900 EMSG(_("Double ; in list of variables"));
1907 EMSG2(_(e_invarg2
), p
);
1914 return skip_var_one(arg
);
1918 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1925 if (*arg
== '@' && arg
[1] != NUL
)
1927 return find_name_end(*arg
== '$' || *arg
== '&' ? arg
+ 1 : arg
,
1928 NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
1932 * List variables for hashtab "ht" with prefix "prefix".
1933 * If "empty" is TRUE also list NULL strings as empty strings.
1936 list_hashtable_vars(ht
, prefix
, empty
, first
)
1946 todo
= (int)ht
->ht_used
;
1947 for (hi
= ht
->ht_array
; todo
> 0 && !got_int
; ++hi
)
1949 if (!HASHITEM_EMPTY(hi
))
1953 if (empty
|| di
->di_tv
.v_type
!= VAR_STRING
1954 || di
->di_tv
.vval
.v_string
!= NULL
)
1955 list_one_var(di
, prefix
, first
);
1961 * List global variables.
1964 list_glob_vars(first
)
1967 list_hashtable_vars(&globvarht
, (char_u
*)"", TRUE
, first
);
1971 * List buffer variables.
1974 list_buf_vars(first
)
1977 char_u numbuf
[NUMBUFLEN
];
1979 list_hashtable_vars(&curbuf
->b_vars
.dv_hashtab
, (char_u
*)"b:",
1982 sprintf((char *)numbuf
, "%ld", (long)curbuf
->b_changedtick
);
1983 list_one_var_a((char_u
*)"b:", (char_u
*)"changedtick", VAR_NUMBER
,
1988 * List window variables.
1991 list_win_vars(first
)
1994 list_hashtable_vars(&curwin
->w_vars
.dv_hashtab
,
1995 (char_u
*)"w:", TRUE
, first
);
2000 * List tab page variables.
2003 list_tab_vars(first
)
2006 list_hashtable_vars(&curtab
->tp_vars
.dv_hashtab
,
2007 (char_u
*)"t:", TRUE
, first
);
2012 * List Vim variables.
2015 list_vim_vars(first
)
2018 list_hashtable_vars(&vimvarht
, (char_u
*)"v:", FALSE
, first
);
2022 * List script-local variables, if there is a script.
2025 list_script_vars(first
)
2028 if (current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
2029 list_hashtable_vars(&SCRIPT_VARS(current_SID
),
2030 (char_u
*)"s:", FALSE
, first
);
2034 * List function variables, if there is a function.
2037 list_func_vars(first
)
2040 if (current_funccal
!= NULL
)
2041 list_hashtable_vars(¤t_funccal
->l_vars
.dv_hashtab
,
2042 (char_u
*)"l:", FALSE
, first
);
2046 * List variables in "arg".
2049 list_arg_vars(eap
, arg
, first
)
2062 while (!ends_excmd(*arg
) && !got_int
)
2064 if (error
|| eap
->skip
)
2066 arg
= find_name_end(arg
, NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
2067 if (!vim_iswhite(*arg
) && !ends_excmd(*arg
))
2070 EMSG(_(e_trailing
));
2076 /* get_name_len() takes care of expanding curly braces */
2077 name_start
= name
= arg
;
2078 len
= get_name_len(&arg
, &tofree
, TRUE
, TRUE
);
2081 /* This is mainly to keep test 49 working: when expanding
2082 * curly braces fails overrule the exception error message. */
2083 if (len
< 0 && !aborting())
2086 EMSG2(_(e_invarg2
), arg
);
2095 if (get_var_tv(name
, len
, &tv
, TRUE
) == FAIL
)
2099 /* handle d.key, l[idx], f(expr) */
2101 if (handle_subscript(&arg
, &tv
, TRUE
, TRUE
) == FAIL
)
2105 if (arg
== arg_subsc
&& len
== 2 && name
[1] == ':')
2109 case 'g': list_glob_vars(first
); break;
2110 case 'b': list_buf_vars(first
); break;
2111 case 'w': list_win_vars(first
); break;
2113 case 't': list_tab_vars(first
); break;
2115 case 'v': list_vim_vars(first
); break;
2116 case 's': list_script_vars(first
); break;
2117 case 'l': list_func_vars(first
); break;
2119 EMSG2(_("E738: Can't list variables for %s"), name
);
2124 char_u numbuf
[NUMBUFLEN
];
2129 s
= echo_string(&tv
, &tf
, numbuf
, 0);
2132 list_one_var_a((char_u
*)"",
2133 arg
== arg_subsc
? name
: name_start
,
2135 s
== NULL
? (char_u
*)"" : s
,
2148 arg
= skipwhite(arg
);
2155 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2156 * Returns a pointer to the char just after the var name.
2157 * Returns NULL if there is an error.
2160 ex_let_one(arg
, tv
, copy
, endchars
, op
)
2161 char_u
*arg
; /* points to variable name */
2162 typval_T
*tv
; /* value to assign to variable */
2163 int copy
; /* copy value from "tv" */
2164 char_u
*endchars
; /* valid chars after variable name or NULL */
2165 char_u
*op
; /* "+", "-", "." or NULL*/
2170 char_u
*arg_end
= NULL
;
2173 char_u
*tofree
= NULL
;
2176 * ":let $VAR = expr": Set environment variable.
2180 /* Find the end of the name. */
2183 len
= get_env_len(&arg
);
2185 EMSG2(_(e_invarg2
), name
- 1);
2188 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2189 EMSG2(_(e_letwrong
), op
);
2190 else if (endchars
!= NULL
2191 && vim_strchr(endchars
, *skipwhite(arg
)) == NULL
)
2192 EMSG(_(e_letunexp
));
2197 p
= get_tv_string_chk(tv
);
2198 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2200 int mustfree
= FALSE
;
2201 char_u
*s
= vim_getenv(name
, &mustfree
);
2205 p
= tofree
= concat_str(s
, p
);
2212 vim_setenv(name
, p
);
2213 if (STRICMP(name
, "HOME") == 0)
2215 else if (didset_vim
&& STRICMP(name
, "VIM") == 0)
2217 else if (didset_vimruntime
2218 && STRICMP(name
, "VIMRUNTIME") == 0)
2219 didset_vimruntime
= FALSE
;
2229 * ":let &option = expr": Set option value.
2230 * ":let &l:option = expr": Set local option value.
2231 * ":let &g:option = expr": Set global option value.
2233 else if (*arg
== '&')
2235 /* Find the end of the name. */
2236 p
= find_option_end(&arg
, &opt_flags
);
2237 if (p
== NULL
|| (endchars
!= NULL
2238 && vim_strchr(endchars
, *skipwhite(p
)) == NULL
))
2239 EMSG(_(e_letunexp
));
2245 char_u
*stringval
= NULL
;
2251 n
= get_tv_number(tv
);
2252 s
= get_tv_string_chk(tv
); /* != NULL if number or string */
2253 if (s
!= NULL
&& op
!= NULL
&& *op
!= '=')
2255 opt_type
= get_option_value(arg
, &numval
,
2256 &stringval
, opt_flags
);
2257 if ((opt_type
== 1 && *op
== '.')
2258 || (opt_type
== 0 && *op
!= '.'))
2259 EMSG2(_(e_letwrong
), op
);
2262 if (opt_type
== 1) /* number */
2269 else if (opt_type
== 0 && stringval
!= NULL
) /* string */
2271 s
= concat_str(stringval
, s
);
2272 vim_free(stringval
);
2279 set_option_value(arg
, n
, s
, opt_flags
);
2283 vim_free(stringval
);
2288 * ":let @r = expr": Set register contents.
2290 else if (*arg
== '@')
2293 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2294 EMSG2(_(e_letwrong
), op
);
2295 else if (endchars
!= NULL
2296 && vim_strchr(endchars
, *skipwhite(arg
+ 1)) == NULL
)
2297 EMSG(_(e_letunexp
));
2300 char_u
*ptofree
= NULL
;
2303 p
= get_tv_string_chk(tv
);
2304 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2306 s
= get_reg_contents(*arg
== '@' ? '"' : *arg
, TRUE
, TRUE
);
2309 p
= ptofree
= concat_str(s
, p
);
2315 write_reg_contents(*arg
== '@' ? '"' : *arg
, p
, -1, FALSE
);
2323 * ":let var = expr": Set internal variable.
2324 * ":let {expr} = expr": Idem, name made with curly braces
2326 else if (eval_isnamec1(*arg
) || *arg
== '{')
2330 p
= get_lval(arg
, tv
, &lv
, FALSE
, FALSE
, FALSE
, FNE_CHECK_START
);
2331 if (p
!= NULL
&& lv
.ll_name
!= NULL
)
2333 if (endchars
!= NULL
&& vim_strchr(endchars
, *skipwhite(p
)) == NULL
)
2334 EMSG(_(e_letunexp
));
2337 set_var_lval(&lv
, p
, tv
, copy
, op
);
2345 EMSG2(_(e_invarg2
), arg
);
2351 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2354 check_changedtick(arg
)
2357 if (STRNCMP(arg
, "b:changedtick", 13) == 0 && !eval_isnamec(arg
[13]))
2359 EMSG2(_(e_readonlyvar
), arg
);
2366 * Get an lval: variable, Dict item or List item that can be assigned a value
2367 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2368 * "name.key", "name.key[expr]" etc.
2369 * Indexing only works if "name" is an existing List or Dictionary.
2370 * "name" points to the start of the name.
2371 * If "rettv" is not NULL it points to the value to be assigned.
2372 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2373 * wrong; must end in space or cmd separator.
2375 * Returns a pointer to just after the name, including indexes.
2376 * When an evaluation error occurs "lp->ll_name" is NULL;
2377 * Returns NULL for a parsing error. Still need to free items in "lp"!
2380 get_lval(name
, rettv
, lp
, unlet
, skip
, quiet
, fne_flags
)
2386 int quiet
; /* don't give error messages */
2387 int fne_flags
; /* flags for find_name_end() */
2390 char_u
*expr_start
, *expr_end
;
2401 /* Clear everything in "lp". */
2402 vim_memset(lp
, 0, sizeof(lval_T
));
2406 /* When skipping just find the end of the name. */
2408 return find_name_end(name
, NULL
, NULL
, FNE_INCL_BR
| fne_flags
);
2411 /* Find the end of the name. */
2412 p
= find_name_end(name
, &expr_start
, &expr_end
, fne_flags
);
2413 if (expr_start
!= NULL
)
2415 /* Don't expand the name when we already know there is an error. */
2416 if (unlet
&& !vim_iswhite(*p
) && !ends_excmd(*p
)
2417 && *p
!= '[' && *p
!= '.')
2419 EMSG(_(e_trailing
));
2423 lp
->ll_exp_name
= make_expanded_name(name
, expr_start
, expr_end
, p
);
2424 if (lp
->ll_exp_name
== NULL
)
2426 /* Report an invalid expression in braces, unless the
2427 * expression evaluation has been cancelled due to an
2428 * aborting error, an interrupt, or an exception. */
2429 if (!aborting() && !quiet
)
2432 EMSG2(_(e_invarg2
), name
);
2436 lp
->ll_name
= lp
->ll_exp_name
;
2441 /* Without [idx] or .key we are done. */
2442 if ((*p
!= '[' && *p
!= '.') || lp
->ll_name
== NULL
)
2447 v
= find_var(lp
->ll_name
, &ht
);
2448 if (v
== NULL
&& !quiet
)
2449 EMSG2(_(e_undefvar
), lp
->ll_name
);
2455 * Loop until no more [idx] or .key is following.
2457 lp
->ll_tv
= &v
->di_tv
;
2458 while (*p
== '[' || (*p
== '.' && lp
->ll_tv
->v_type
== VAR_DICT
))
2460 if (!(lp
->ll_tv
->v_type
== VAR_LIST
&& lp
->ll_tv
->vval
.v_list
!= NULL
)
2461 && !(lp
->ll_tv
->v_type
== VAR_DICT
2462 && lp
->ll_tv
->vval
.v_dict
!= NULL
))
2465 EMSG(_("E689: Can only index a List or Dictionary"));
2471 EMSG(_("E708: [:] must come last"));
2479 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
2484 EMSG(_(e_emptykey
));
2491 /* Get the index [expr] or the first index [expr: ]. */
2492 p
= skipwhite(p
+ 1);
2498 if (eval1(&p
, &var1
, TRUE
) == FAIL
) /* recursive! */
2500 if (get_tv_string_chk(&var1
) == NULL
)
2502 /* not a number or string */
2508 /* Optionally get the second index [ :expr]. */
2511 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2514 EMSG(_(e_dictrange
));
2519 if (rettv
!= NULL
&& (rettv
->v_type
!= VAR_LIST
2520 || rettv
->vval
.v_list
== NULL
))
2523 EMSG(_("E709: [:] requires a List value"));
2528 p
= skipwhite(p
+ 1);
2530 lp
->ll_empty2
= TRUE
;
2533 lp
->ll_empty2
= FALSE
;
2534 if (eval1(&p
, &var2
, TRUE
) == FAIL
) /* recursive! */
2540 if (get_tv_string_chk(&var2
) == NULL
)
2542 /* not a number or string */
2549 lp
->ll_range
= TRUE
;
2552 lp
->ll_range
= FALSE
;
2557 EMSG(_(e_missbrac
));
2560 if (lp
->ll_range
&& !lp
->ll_empty2
)
2565 /* Skip to past ']'. */
2569 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2573 /* "[key]": get key from "var1" */
2574 key
= get_tv_string(&var1
); /* is number or string */
2578 EMSG(_(e_emptykey
));
2584 lp
->ll_dict
= lp
->ll_tv
->vval
.v_dict
;
2585 lp
->ll_di
= dict_find(lp
->ll_dict
, key
, len
);
2586 if (lp
->ll_di
== NULL
)
2588 /* Key does not exist in dict: may need to add it. */
2589 if (*p
== '[' || *p
== '.' || unlet
)
2592 EMSG2(_(e_dictkey
), key
);
2598 lp
->ll_newkey
= vim_strsave(key
);
2600 lp
->ll_newkey
= vim_strnsave(key
, len
);
2603 if (lp
->ll_newkey
== NULL
)
2609 lp
->ll_tv
= &lp
->ll_di
->di_tv
;
2614 * Get the number and item for the only or first index of the List.
2620 lp
->ll_n1
= get_tv_number(&var1
); /* is number or string */
2624 lp
->ll_list
= lp
->ll_tv
->vval
.v_list
;
2625 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2626 if (lp
->ll_li
== NULL
)
2631 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2634 if (lp
->ll_li
== NULL
)
2636 if (lp
->ll_range
&& !lp
->ll_empty2
)
2642 * May need to find the item or absolute index for the second
2644 * When no index given: "lp->ll_empty2" is TRUE.
2645 * Otherwise "lp->ll_n2" is set to the second index.
2647 if (lp
->ll_range
&& !lp
->ll_empty2
)
2649 lp
->ll_n2
= get_tv_number(&var2
); /* is number or string */
2653 ni
= list_find(lp
->ll_list
, lp
->ll_n2
);
2656 lp
->ll_n2
= list_idx_of_item(lp
->ll_list
, ni
);
2659 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2661 lp
->ll_n1
= list_idx_of_item(lp
->ll_list
, lp
->ll_li
);
2662 if (lp
->ll_n2
< lp
->ll_n1
)
2666 lp
->ll_tv
= &lp
->ll_li
->li_tv
;
2674 * Clear lval "lp" that was filled by get_lval().
2680 vim_free(lp
->ll_exp_name
);
2681 vim_free(lp
->ll_newkey
);
2685 * Set a variable that was parsed by get_lval() to "rettv".
2686 * "endp" points to just after the parsed name.
2687 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
2690 set_var_lval(lp
, endp
, rettv
, copy
, op
)
2701 if (lp
->ll_tv
== NULL
)
2703 if (!check_changedtick(lp
->ll_name
))
2707 if (op
!= NULL
&& *op
!= '=')
2711 /* handle +=, -= and .= */
2712 if (get_var_tv(lp
->ll_name
, (int)STRLEN(lp
->ll_name
),
2715 if (tv_op(&tv
, rettv
, op
) == OK
)
2716 set_var(lp
->ll_name
, &tv
, FALSE
);
2721 set_var(lp
->ll_name
, rettv
, copy
);
2725 else if (tv_check_lock(lp
->ll_newkey
== NULL
2727 : lp
->ll_tv
->vval
.v_dict
->dv_lock
, lp
->ll_name
))
2729 else if (lp
->ll_range
)
2732 * Assign the List values to the list items.
2734 for (ri
= rettv
->vval
.v_list
->lv_first
; ri
!= NULL
; )
2736 if (op
!= NULL
&& *op
!= '=')
2737 tv_op(&lp
->ll_li
->li_tv
, &ri
->li_tv
, op
);
2740 clear_tv(&lp
->ll_li
->li_tv
);
2741 copy_tv(&ri
->li_tv
, &lp
->ll_li
->li_tv
);
2744 if (ri
== NULL
|| (!lp
->ll_empty2
&& lp
->ll_n2
== lp
->ll_n1
))
2746 if (lp
->ll_li
->li_next
== NULL
)
2748 /* Need to add an empty item. */
2749 if (list_append_number(lp
->ll_list
, 0) == FAIL
)
2755 lp
->ll_li
= lp
->ll_li
->li_next
;
2759 EMSG(_("E710: List value has more items than target"));
2760 else if (lp
->ll_empty2
2761 ? (lp
->ll_li
!= NULL
&& lp
->ll_li
->li_next
!= NULL
)
2762 : lp
->ll_n1
!= lp
->ll_n2
)
2763 EMSG(_("E711: List value has not enough items"));
2768 * Assign to a List or Dictionary item.
2770 if (lp
->ll_newkey
!= NULL
)
2772 if (op
!= NULL
&& *op
!= '=')
2774 EMSG2(_(e_letwrong
), op
);
2778 /* Need to add an item to the Dictionary. */
2779 di
= dictitem_alloc(lp
->ll_newkey
);
2782 if (dict_add(lp
->ll_tv
->vval
.v_dict
, di
) == FAIL
)
2787 lp
->ll_tv
= &di
->di_tv
;
2789 else if (op
!= NULL
&& *op
!= '=')
2791 tv_op(lp
->ll_tv
, rettv
, op
);
2795 clear_tv(lp
->ll_tv
);
2798 * Assign the value to the variable or list item.
2801 copy_tv(rettv
, lp
->ll_tv
);
2804 *lp
->ll_tv
= *rettv
;
2805 lp
->ll_tv
->v_lock
= 0;
2812 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2813 * Returns OK or FAIL.
2822 char_u numbuf
[NUMBUFLEN
];
2825 /* Can't do anything with a Funcref or a Dict on the right. */
2826 if (tv2
->v_type
!= VAR_FUNC
&& tv2
->v_type
!= VAR_DICT
)
2828 switch (tv1
->v_type
)
2835 if (*op
!= '+' || tv2
->v_type
!= VAR_LIST
)
2838 if (tv1
->vval
.v_list
!= NULL
&& tv2
->vval
.v_list
!= NULL
)
2839 list_extend(tv1
->vval
.v_list
, tv2
->vval
.v_list
, NULL
);
2844 if (tv2
->v_type
== VAR_LIST
)
2846 if (*op
== '+' || *op
== '-')
2848 /* nr += nr or nr -= nr*/
2849 n
= get_tv_number(tv1
);
2851 n
+= get_tv_number(tv2
);
2853 n
-= get_tv_number(tv2
);
2855 tv1
->v_type
= VAR_NUMBER
;
2856 tv1
->vval
.v_number
= n
;
2861 s
= get_tv_string(tv1
);
2862 s
= concat_str(s
, get_tv_string_buf(tv2
, numbuf
));
2864 tv1
->v_type
= VAR_STRING
;
2865 tv1
->vval
.v_string
= s
;
2871 EMSG2(_(e_letwrong
), op
);
2876 * Add a watcher to a list.
2879 list_add_watch(l
, lw
)
2883 lw
->lw_next
= l
->lv_watch
;
2888 * Remove a watcher from a list.
2889 * No warning when it isn't found...
2892 list_rem_watch(l
, lwrem
)
2896 listwatch_T
*lw
, **lwp
;
2899 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
2911 * Just before removing an item from a list: advance watchers to the next
2915 list_fix_watch(l
, item
)
2921 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
2922 if (lw
->lw_item
== item
)
2923 lw
->lw_item
= item
->li_next
;
2927 * Evaluate the expression used in a ":for var in expr" command.
2928 * "arg" points to "var".
2929 * Set "*errp" to TRUE for an error, FALSE otherwise;
2930 * Return a pointer that holds the info. Null when there is an error.
2933 eval_for_line(arg
, errp
, nextcmdp
, skip
)
2944 *errp
= TRUE
; /* default: there is an error */
2946 fi
= (forinfo_T
*)alloc_clear(sizeof(forinfo_T
));
2950 expr
= skip_var_list(arg
, &fi
->fi_varcount
, &fi
->fi_semicolon
);
2954 expr
= skipwhite(expr
);
2955 if (expr
[0] != 'i' || expr
[1] != 'n' || !vim_iswhite(expr
[2]))
2957 EMSG(_("E690: Missing \"in\" after :for"));
2963 if (eval0(skipwhite(expr
+ 2), &tv
, nextcmdp
, !skip
) == OK
)
2969 if (tv
.v_type
!= VAR_LIST
|| l
== NULL
)
2976 /* No need to increment the refcount, it's already set for the
2977 * list being used in "tv". */
2979 list_add_watch(l
, &fi
->fi_lw
);
2980 fi
->fi_lw
.lw_item
= l
->lv_first
;
2991 * Use the first item in a ":for" list. Advance to the next.
2992 * Assign the values to the variable (list). "arg" points to the first one.
2993 * Return TRUE when a valid item was found, FALSE when at end of list or
2997 next_for_item(fi_void
, arg
)
3001 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3005 item
= fi
->fi_lw
.lw_item
;
3010 fi
->fi_lw
.lw_item
= item
->li_next
;
3011 result
= (ex_let_vars(arg
, &item
->li_tv
, TRUE
,
3012 fi
->fi_semicolon
, fi
->fi_varcount
, NULL
) == OK
);
3018 * Free the structure used to store info used by ":for".
3021 free_for_info(fi_void
)
3024 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3026 if (fi
!= NULL
&& fi
->fi_list
!= NULL
)
3028 list_rem_watch(fi
->fi_list
, &fi
->fi_lw
);
3029 list_unref(fi
->fi_list
);
3034 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3037 set_context_for_expression(xp
, arg
, cmdidx
)
3046 if (cmdidx
== CMD_let
)
3048 xp
->xp_context
= EXPAND_USER_VARS
;
3049 if (vim_strpbrk(arg
, (char_u
*)"\"'+-*/%.=!?~|&$([<>,#") == NULL
)
3051 /* ":let var1 var2 ...": find last space. */
3052 for (p
= arg
+ STRLEN(arg
); p
>= arg
; )
3055 mb_ptr_back(arg
, p
);
3056 if (vim_iswhite(*p
))
3063 xp
->xp_context
= cmdidx
== CMD_call
? EXPAND_FUNCTIONS
3064 : EXPAND_EXPRESSION
;
3065 while ((xp
->xp_pattern
= vim_strpbrk(arg
,
3066 (char_u
*)"\"'+-*/%.=!?~|&$([<>,#")) != NULL
)
3068 c
= *xp
->xp_pattern
;
3071 c
= xp
->xp_pattern
[1];
3075 xp
->xp_context
= cmdidx
!= CMD_let
|| got_eq
3076 ? EXPAND_EXPRESSION
: EXPAND_NOTHING
;
3080 xp
->xp_context
= EXPAND_SETTINGS
;
3081 if ((c
== 'l' || c
== 'g') && xp
->xp_pattern
[2] == ':')
3082 xp
->xp_pattern
+= 2;
3088 /* environment variable */
3089 xp
->xp_context
= EXPAND_ENV_VARS
;
3094 xp
->xp_context
= EXPAND_EXPRESSION
;
3097 && xp
->xp_context
== EXPAND_FUNCTIONS
3098 && vim_strchr(xp
->xp_pattern
, '(') == NULL
)
3100 /* Function name can start with "<SNR>" */
3103 else if (cmdidx
!= CMD_let
|| got_eq
)
3105 if (c
== '"') /* string */
3107 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '"')
3108 if (c
== '\\' && xp
->xp_pattern
[1] != NUL
)
3110 xp
->xp_context
= EXPAND_NOTHING
;
3112 else if (c
== '\'') /* literal string */
3114 /* Trick: '' is like stopping and starting a literal string. */
3115 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '\'')
3117 xp
->xp_context
= EXPAND_NOTHING
;
3121 if (xp
->xp_pattern
[1] == '|')
3124 xp
->xp_context
= EXPAND_EXPRESSION
;
3127 xp
->xp_context
= EXPAND_COMMANDS
;
3130 xp
->xp_context
= EXPAND_EXPRESSION
;
3133 /* Doesn't look like something valid, expand as an expression
3135 xp
->xp_context
= EXPAND_EXPRESSION
;
3136 arg
= xp
->xp_pattern
;
3138 while ((c
= *++arg
) != NUL
&& (c
== ' ' || c
== '\t'))
3141 xp
->xp_pattern
= arg
;
3144 #endif /* FEAT_CMDL_COMPL */
3147 * ":1,25call func(arg1, arg2)" function call.
3153 char_u
*arg
= eap
->arg
;
3164 tofree
= trans_function_name(&arg
, eap
->skip
, TFN_INT
, &fudi
);
3165 if (fudi
.fd_newkey
!= NULL
)
3167 /* Still need to give an error message for missing key. */
3168 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
3169 vim_free(fudi
.fd_newkey
);
3174 /* Increase refcount on dictionary, it could get deleted when evaluating
3176 if (fudi
.fd_dict
!= NULL
)
3177 ++fudi
.fd_dict
->dv_refcount
;
3179 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3180 len
= (int)STRLEN(tofree
);
3181 name
= deref_func_name(tofree
, &len
);
3183 /* Skip white space to allow ":call func ()". Not good, but required for
3184 * backward compatibility. */
3185 startarg
= skipwhite(arg
);
3186 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
3188 if (*startarg
!= '(')
3190 EMSG2(_("E107: Missing braces: %s"), eap
->arg
);
3195 * When skipping, evaluate the function once, to find the end of the
3197 * When the function takes a range, this is discovered after the first
3198 * call, and the loop is broken.
3203 lnum
= eap
->line2
; /* do it once, also with an invalid range */
3207 for ( ; lnum
<= eap
->line2
; ++lnum
)
3209 if (!eap
->skip
&& eap
->addr_count
> 0)
3211 curwin
->w_cursor
.lnum
= lnum
;
3212 curwin
->w_cursor
.col
= 0;
3215 if (get_func_tv(name
, (int)STRLEN(name
), &rettv
, &arg
,
3216 eap
->line1
, eap
->line2
, &doesrange
,
3217 !eap
->skip
, fudi
.fd_dict
) == FAIL
)
3223 /* Handle a function returning a Funcref, Dictionary or List. */
3224 if (handle_subscript(&arg
, &rettv
, !eap
->skip
, TRUE
) == FAIL
)
3231 if (doesrange
|| eap
->skip
)
3234 /* Stop when immediately aborting on error, or when an interrupt
3235 * occurred or an exception was thrown but not caught.
3236 * get_func_tv() returned OK, so that the check for trailing
3237 * characters below is executed. */
3246 /* Check for trailing illegal characters and a following command. */
3247 if (!ends_excmd(*arg
))
3250 EMSG(_(e_trailing
));
3253 eap
->nextcmd
= check_nextcmd(arg
);
3257 dict_unref(fudi
.fd_dict
);
3262 * ":unlet[!] var1 ... " command.
3268 ex_unletlock(eap
, eap
->arg
, 0);
3272 * ":lockvar" and ":unlockvar" commands
3278 char_u
*arg
= eap
->arg
;
3283 else if (vim_isdigit(*arg
))
3285 deep
= getdigits(&arg
);
3286 arg
= skipwhite(arg
);
3289 ex_unletlock(eap
, arg
, deep
);
3293 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3296 ex_unletlock(eap
, argstart
, deep
)
3301 char_u
*arg
= argstart
;
3308 /* Parse the name and find the end. */
3309 name_end
= get_lval(arg
, NULL
, &lv
, TRUE
, eap
->skip
|| error
, FALSE
,
3311 if (lv
.ll_name
== NULL
)
3312 error
= TRUE
; /* error but continue parsing */
3313 if (name_end
== NULL
|| (!vim_iswhite(*name_end
)
3314 && !ends_excmd(*name_end
)))
3316 if (name_end
!= NULL
)
3319 EMSG(_(e_trailing
));
3321 if (!(eap
->skip
|| error
))
3326 if (!error
&& !eap
->skip
)
3328 if (eap
->cmdidx
== CMD_unlet
)
3330 if (do_unlet_var(&lv
, name_end
, eap
->forceit
) == FAIL
)
3335 if (do_lock_var(&lv
, name_end
, deep
,
3336 eap
->cmdidx
== CMD_lockvar
) == FAIL
)
3344 arg
= skipwhite(name_end
);
3345 } while (!ends_excmd(*arg
));
3347 eap
->nextcmd
= check_nextcmd(arg
);
3351 do_unlet_var(lp
, name_end
, forceit
)
3359 if (lp
->ll_tv
== NULL
)
3364 /* Normal name or expanded name. */
3365 if (check_changedtick(lp
->ll_name
))
3367 else if (do_unlet(lp
->ll_name
, forceit
) == FAIL
)
3371 else if (tv_check_lock(lp
->ll_tv
->v_lock
, lp
->ll_name
))
3373 else if (lp
->ll_range
)
3377 /* Delete a range of List items. */
3378 while (lp
->ll_li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3380 li
= lp
->ll_li
->li_next
;
3381 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3388 if (lp
->ll_list
!= NULL
)
3389 /* unlet a List item. */
3390 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3392 /* unlet a Dictionary item. */
3393 dictitem_remove(lp
->ll_dict
, lp
->ll_di
);
3400 * "unlet" a variable. Return OK if it existed, FAIL if not.
3401 * When "forceit" is TRUE don't complain if the variable doesn't exist.
3404 do_unlet(name
, forceit
)
3413 ht
= find_var_ht(name
, &varname
);
3414 if (ht
!= NULL
&& *varname
!= NUL
)
3416 hi
= hash_find(ht
, varname
);
3417 if (!HASHITEM_EMPTY(hi
))
3420 if (var_check_fixed(di
->di_flags
, name
)
3421 || var_check_ro(di
->di_flags
, name
))
3429 EMSG2(_("E108: No such variable: \"%s\""), name
);
3434 * Lock or unlock variable indicated by "lp".
3435 * "deep" is the levels to go (-1 for unlimited);
3436 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3439 do_lock_var(lp
, name_end
, deep
, lock
)
3449 if (deep
== 0) /* nothing to do */
3452 if (lp
->ll_tv
== NULL
)
3457 /* Normal name or expanded name. */
3458 if (check_changedtick(lp
->ll_name
))
3462 di
= find_var(lp
->ll_name
, NULL
);
3468 di
->di_flags
|= DI_FLAGS_LOCK
;
3470 di
->di_flags
&= ~DI_FLAGS_LOCK
;
3471 item_lock(&di
->di_tv
, deep
, lock
);
3476 else if (lp
->ll_range
)
3478 listitem_T
*li
= lp
->ll_li
;
3480 /* (un)lock a range of List items. */
3481 while (li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3483 item_lock(&li
->li_tv
, deep
, lock
);
3488 else if (lp
->ll_list
!= NULL
)
3489 /* (un)lock a List item. */
3490 item_lock(&lp
->ll_li
->li_tv
, deep
, lock
);
3492 /* un(lock) a Dictionary item. */
3493 item_lock(&lp
->ll_di
->di_tv
, deep
, lock
);
3499 * Lock or unlock an item. "deep" is nr of levels to go.
3502 item_lock(tv
, deep
, lock
)
3507 static int recurse
= 0;
3514 if (recurse
>= DICT_MAXNEST
)
3516 EMSG(_("E743: variable nested too deep for (un)lock"));
3523 /* lock/unlock the item itself */
3525 tv
->v_lock
|= VAR_LOCKED
;
3527 tv
->v_lock
&= ~VAR_LOCKED
;
3532 if ((l
= tv
->vval
.v_list
) != NULL
)
3535 l
->lv_lock
|= VAR_LOCKED
;
3537 l
->lv_lock
&= ~VAR_LOCKED
;
3538 if (deep
< 0 || deep
> 1)
3539 /* recursive: lock/unlock the items the List contains */
3540 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
3541 item_lock(&li
->li_tv
, deep
- 1, lock
);
3545 if ((d
= tv
->vval
.v_dict
) != NULL
)
3548 d
->dv_lock
|= VAR_LOCKED
;
3550 d
->dv_lock
&= ~VAR_LOCKED
;
3551 if (deep
< 0 || deep
> 1)
3553 /* recursive: lock/unlock the items the List contains */
3554 todo
= (int)d
->dv_hashtab
.ht_used
;
3555 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
3557 if (!HASHITEM_EMPTY(hi
))
3560 item_lock(&HI2DI(hi
)->di_tv
, deep
- 1, lock
);
3570 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3571 * it refers to a List or Dictionary that is locked.
3577 return (tv
->v_lock
& VAR_LOCKED
)
3578 || (tv
->v_type
== VAR_LIST
3579 && tv
->vval
.v_list
!= NULL
3580 && (tv
->vval
.v_list
->lv_lock
& VAR_LOCKED
))
3581 || (tv
->v_type
== VAR_DICT
3582 && tv
->vval
.v_dict
!= NULL
3583 && (tv
->vval
.v_dict
->dv_lock
& VAR_LOCKED
));
3586 #if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3588 * Delete all "menutrans_" variables.
3591 del_menutrans_vars()
3596 hash_lock(&globvarht
);
3597 todo
= (int)globvarht
.ht_used
;
3598 for (hi
= globvarht
.ht_array
; todo
> 0 && !got_int
; ++hi
)
3600 if (!HASHITEM_EMPTY(hi
))
3603 if (STRNCMP(HI2DI(hi
)->di_key
, "menutrans_", 10) == 0)
3604 delete_var(&globvarht
, hi
);
3607 hash_unlock(&globvarht
);
3611 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3614 * Local string buffer for the next two functions to store a variable name
3615 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3616 * get_user_var_name().
3619 static char_u
*cat_prefix_varname
__ARGS((int prefix
, char_u
*name
));
3621 static char_u
*varnamebuf
= NULL
;
3622 static int varnamebuflen
= 0;
3625 * Function to concatenate a prefix and a variable name.
3628 cat_prefix_varname(prefix
, name
)
3634 len
= (int)STRLEN(name
) + 3;
3635 if (len
> varnamebuflen
)
3637 vim_free(varnamebuf
);
3638 len
+= 10; /* some additional space */
3639 varnamebuf
= alloc(len
);
3640 if (varnamebuf
== NULL
)
3645 varnamebuflen
= len
;
3647 *varnamebuf
= prefix
;
3648 varnamebuf
[1] = ':';
3649 STRCPY(varnamebuf
+ 2, name
);
3654 * Function given to ExpandGeneric() to obtain the list of user defined
3655 * (global/buffer/window/built-in) variable names.
3659 get_user_var_name(xp
, idx
)
3663 static long_u gdone
;
3664 static long_u bdone
;
3665 static long_u wdone
;
3667 static long_u tdone
;
3670 static hashitem_T
*hi
;
3675 gdone
= bdone
= wdone
= vidx
= 0;
3681 /* Global variables */
3682 if (gdone
< globvarht
.ht_used
)
3685 hi
= globvarht
.ht_array
;
3688 while (HASHITEM_EMPTY(hi
))
3690 if (STRNCMP("g:", xp
->xp_pattern
, 2) == 0)
3691 return cat_prefix_varname('g', hi
->hi_key
);
3696 ht
= &curbuf
->b_vars
.dv_hashtab
;
3697 if (bdone
< ht
->ht_used
)
3703 while (HASHITEM_EMPTY(hi
))
3705 return cat_prefix_varname('b', hi
->hi_key
);
3707 if (bdone
== ht
->ht_used
)
3710 return (char_u
*)"b:changedtick";
3714 ht
= &curwin
->w_vars
.dv_hashtab
;
3715 if (wdone
< ht
->ht_used
)
3721 while (HASHITEM_EMPTY(hi
))
3723 return cat_prefix_varname('w', hi
->hi_key
);
3728 ht
= &curtab
->tp_vars
.dv_hashtab
;
3729 if (tdone
< ht
->ht_used
)
3735 while (HASHITEM_EMPTY(hi
))
3737 return cat_prefix_varname('t', hi
->hi_key
);
3743 return cat_prefix_varname('v', (char_u
*)vimvars
[vidx
++].vv_name
);
3745 vim_free(varnamebuf
);
3751 #endif /* FEAT_CMDL_COMPL */
3754 * types for expressions.
3759 , TYPE_EQUAL
/* == */
3760 , TYPE_NEQUAL
/* != */
3761 , TYPE_GREATER
/* > */
3762 , TYPE_GEQUAL
/* >= */
3763 , TYPE_SMALLER
/* < */
3764 , TYPE_SEQUAL
/* <= */
3765 , TYPE_MATCH
/* =~ */
3766 , TYPE_NOMATCH
/* !~ */
3770 * The "evaluate" argument: When FALSE, the argument is only parsed but not
3771 * executed. The function may return OK, but the rettv will be of type
3772 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3776 * Handle zero level expression.
3777 * This calls eval1() and handles error message and nextcmd.
3778 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
3779 * Note: "rettv.v_lock" is not set.
3780 * Return OK or FAIL.
3783 eval0(arg
, rettv
, nextcmd
, evaluate
)
3793 ret
= eval1(&p
, rettv
, evaluate
);
3794 if (ret
== FAIL
|| !ends_excmd(*p
))
3799 * Report the invalid expression unless the expression evaluation has
3800 * been cancelled due to an aborting error, an interrupt, or an
3804 EMSG2(_(e_invexpr2
), arg
);
3807 if (nextcmd
!= NULL
)
3808 *nextcmd
= check_nextcmd(p
);
3814 * Handle top level expression:
3815 * expr1 ? expr0 : expr0
3817 * "arg" must point to the first non-white of the expression.
3818 * "arg" is advanced to the next non-white after the recognized expression.
3820 * Note: "rettv.v_lock" is not set.
3822 * Return OK or FAIL.
3825 eval1(arg
, rettv
, evaluate
)
3834 * Get the first variable.
3836 if (eval2(arg
, rettv
, evaluate
) == FAIL
)
3839 if ((*arg
)[0] == '?')
3846 if (get_tv_number_chk(rettv
, &error
) != 0)
3854 * Get the second variable.
3856 *arg
= skipwhite(*arg
+ 1);
3857 if (eval1(arg
, rettv
, evaluate
&& result
) == FAIL
) /* recursive! */
3861 * Check for the ":".
3863 if ((*arg
)[0] != ':')
3865 EMSG(_("E109: Missing ':' after '?'"));
3866 if (evaluate
&& result
)
3872 * Get the third variable.
3874 *arg
= skipwhite(*arg
+ 1);
3875 if (eval1(arg
, &var2
, evaluate
&& !result
) == FAIL
) /* recursive! */
3877 if (evaluate
&& result
)
3881 if (evaluate
&& !result
)
3889 * Handle first level expression:
3890 * expr2 || expr2 || expr2 logical OR
3892 * "arg" must point to the first non-white of the expression.
3893 * "arg" is advanced to the next non-white after the recognized expression.
3895 * Return OK or FAIL.
3898 eval2(arg
, rettv
, evaluate
)
3909 * Get the first variable.
3911 if (eval3(arg
, rettv
, evaluate
) == FAIL
)
3915 * Repeat until there is no following "||".
3919 while ((*arg
)[0] == '|' && (*arg
)[1] == '|')
3921 if (evaluate
&& first
)
3923 if (get_tv_number_chk(rettv
, &error
) != 0)
3932 * Get the second variable.
3934 *arg
= skipwhite(*arg
+ 2);
3935 if (eval3(arg
, &var2
, evaluate
&& !result
) == FAIL
)
3939 * Compute the result.
3941 if (evaluate
&& !result
)
3943 if (get_tv_number_chk(&var2
, &error
) != 0)
3951 rettv
->v_type
= VAR_NUMBER
;
3952 rettv
->vval
.v_number
= result
;
3960 * Handle second level expression:
3961 * expr3 && expr3 && expr3 logical AND
3963 * "arg" must point to the first non-white of the expression.
3964 * "arg" is advanced to the next non-white after the recognized expression.
3966 * Return OK or FAIL.
3969 eval3(arg
, rettv
, evaluate
)
3980 * Get the first variable.
3982 if (eval4(arg
, rettv
, evaluate
) == FAIL
)
3986 * Repeat until there is no following "&&".
3990 while ((*arg
)[0] == '&' && (*arg
)[1] == '&')
3992 if (evaluate
&& first
)
3994 if (get_tv_number_chk(rettv
, &error
) == 0)
4003 * Get the second variable.
4005 *arg
= skipwhite(*arg
+ 2);
4006 if (eval4(arg
, &var2
, evaluate
&& result
) == FAIL
)
4010 * Compute the result.
4012 if (evaluate
&& result
)
4014 if (get_tv_number_chk(&var2
, &error
) == 0)
4022 rettv
->v_type
= VAR_NUMBER
;
4023 rettv
->vval
.v_number
= result
;
4031 * Handle third level expression:
4043 * "arg" must point to the first non-white of the expression.
4044 * "arg" is advanced to the next non-white after the recognized expression.
4046 * Return OK or FAIL.
4049 eval4(arg
, rettv
, evaluate
)
4057 exptype_T type
= TYPE_UNKNOWN
;
4058 int type_is
= FALSE
; /* TRUE for "is" and "isnot" */
4062 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4063 regmatch_T regmatch
;
4068 * Get the first variable.
4070 if (eval5(arg
, rettv
, evaluate
) == FAIL
)
4076 case '=': if (p
[1] == '=')
4078 else if (p
[1] == '~')
4081 case '!': if (p
[1] == '=')
4083 else if (p
[1] == '~')
4084 type
= TYPE_NOMATCH
;
4086 case '>': if (p
[1] != '=')
4088 type
= TYPE_GREATER
;
4094 case '<': if (p
[1] != '=')
4096 type
= TYPE_SMALLER
;
4102 case 'i': if (p
[1] == 's')
4104 if (p
[2] == 'n' && p
[3] == 'o' && p
[4] == 't')
4106 if (!vim_isIDc(p
[len
]))
4108 type
= len
== 2 ? TYPE_EQUAL
: TYPE_NEQUAL
;
4116 * If there is a comparitive operator, use it.
4118 if (type
!= TYPE_UNKNOWN
)
4120 /* extra question mark appended: ignore case */
4126 /* extra '#' appended: match case */
4127 else if (p
[len
] == '#')
4132 /* nothing appened: use 'ignorecase' */
4137 * Get the second variable.
4139 *arg
= skipwhite(p
+ len
);
4140 if (eval5(arg
, &var2
, evaluate
) == FAIL
)
4148 if (type_is
&& rettv
->v_type
!= var2
.v_type
)
4150 /* For "is" a different type always means FALSE, for "notis"
4152 n1
= (type
== TYPE_NEQUAL
);
4154 else if (rettv
->v_type
== VAR_LIST
|| var2
.v_type
== VAR_LIST
)
4158 n1
= (rettv
->v_type
== var2
.v_type
4159 && rettv
->vval
.v_list
== var2
.vval
.v_list
);
4160 if (type
== TYPE_NEQUAL
)
4163 else if (rettv
->v_type
!= var2
.v_type
4164 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4166 if (rettv
->v_type
!= var2
.v_type
)
4167 EMSG(_("E691: Can only compare List with List"));
4169 EMSG(_("E692: Invalid operation for Lists"));
4176 /* Compare two Lists for being equal or unequal. */
4177 n1
= list_equal(rettv
->vval
.v_list
, var2
.vval
.v_list
, ic
);
4178 if (type
== TYPE_NEQUAL
)
4183 else if (rettv
->v_type
== VAR_DICT
|| var2
.v_type
== VAR_DICT
)
4187 n1
= (rettv
->v_type
== var2
.v_type
4188 && rettv
->vval
.v_dict
== var2
.vval
.v_dict
);
4189 if (type
== TYPE_NEQUAL
)
4192 else if (rettv
->v_type
!= var2
.v_type
4193 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4195 if (rettv
->v_type
!= var2
.v_type
)
4196 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4198 EMSG(_("E736: Invalid operation for Dictionary"));
4205 /* Compare two Dictionaries for being equal or unequal. */
4206 n1
= dict_equal(rettv
->vval
.v_dict
, var2
.vval
.v_dict
, ic
);
4207 if (type
== TYPE_NEQUAL
)
4212 else if (rettv
->v_type
== VAR_FUNC
|| var2
.v_type
== VAR_FUNC
)
4214 if (rettv
->v_type
!= var2
.v_type
4215 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4217 if (rettv
->v_type
!= var2
.v_type
)
4218 EMSG(_("E693: Can only compare Funcref with Funcref"));
4220 EMSG(_("E694: Invalid operation for Funcrefs"));
4227 /* Compare two Funcrefs for being equal or unequal. */
4228 if (rettv
->vval
.v_string
== NULL
4229 || var2
.vval
.v_string
== NULL
)
4232 n1
= STRCMP(rettv
->vval
.v_string
,
4233 var2
.vval
.v_string
) == 0;
4234 if (type
== TYPE_NEQUAL
)
4240 * If one of the two variables is a number, compare as a number.
4241 * When using "=~" or "!~", always compare as string.
4243 else if ((rettv
->v_type
== VAR_NUMBER
|| var2
.v_type
== VAR_NUMBER
)
4244 && type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4246 n1
= get_tv_number(rettv
);
4247 n2
= get_tv_number(&var2
);
4250 case TYPE_EQUAL
: n1
= (n1
== n2
); break;
4251 case TYPE_NEQUAL
: n1
= (n1
!= n2
); break;
4252 case TYPE_GREATER
: n1
= (n1
> n2
); break;
4253 case TYPE_GEQUAL
: n1
= (n1
>= n2
); break;
4254 case TYPE_SMALLER
: n1
= (n1
< n2
); break;
4255 case TYPE_SEQUAL
: n1
= (n1
<= n2
); break;
4258 case TYPE_NOMATCH
: break; /* avoid gcc warning */
4263 s1
= get_tv_string_buf(rettv
, buf1
);
4264 s2
= get_tv_string_buf(&var2
, buf2
);
4265 if (type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4266 i
= ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
);
4272 case TYPE_EQUAL
: n1
= (i
== 0); break;
4273 case TYPE_NEQUAL
: n1
= (i
!= 0); break;
4274 case TYPE_GREATER
: n1
= (i
> 0); break;
4275 case TYPE_GEQUAL
: n1
= (i
>= 0); break;
4276 case TYPE_SMALLER
: n1
= (i
< 0); break;
4277 case TYPE_SEQUAL
: n1
= (i
<= 0); break;
4281 /* avoid 'l' flag in 'cpoptions' */
4283 p_cpo
= (char_u
*)"";
4284 regmatch
.regprog
= vim_regcomp(s2
,
4285 RE_MAGIC
+ RE_STRING
);
4286 regmatch
.rm_ic
= ic
;
4287 if (regmatch
.regprog
!= NULL
)
4289 n1
= vim_regexec_nl(®match
, s1
, (colnr_T
)0);
4290 vim_free(regmatch
.regprog
);
4291 if (type
== TYPE_NOMATCH
)
4297 case TYPE_UNKNOWN
: break; /* avoid gcc warning */
4302 rettv
->v_type
= VAR_NUMBER
;
4303 rettv
->vval
.v_number
= n1
;
4311 * Handle fourth level expression:
4313 * - number subtraction
4314 * . string concatenation
4316 * "arg" must point to the first non-white of the expression.
4317 * "arg" is advanced to the next non-white after the recognized expression.
4319 * Return OK or FAIL.
4322 eval5(arg
, rettv
, evaluate
)
4332 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4336 * Get the first variable.
4338 if (eval6(arg
, rettv
, evaluate
) == FAIL
)
4342 * Repeat computing, until no '+', '-' or '.' is following.
4347 if (op
!= '+' && op
!= '-' && op
!= '.')
4350 if (op
!= '+' || rettv
->v_type
!= VAR_LIST
)
4352 /* For "list + ...", an illegal use of the first operand as
4353 * a number cannot be determined before evaluating the 2nd
4354 * operand: if this is also a list, all is ok.
4355 * For "something . ...", "something - ..." or "non-list + ...",
4356 * we know that the first operand needs to be a string or number
4357 * without evaluating the 2nd operand. So check before to avoid
4358 * side effects after an error. */
4359 if (evaluate
&& get_tv_string_chk(rettv
) == NULL
)
4367 * Get the second variable.
4369 *arg
= skipwhite(*arg
+ 1);
4370 if (eval6(arg
, &var2
, evaluate
) == FAIL
)
4379 * Compute the result.
4383 s1
= get_tv_string_buf(rettv
, buf1
); /* already checked */
4384 s2
= get_tv_string_buf_chk(&var2
, buf2
);
4385 if (s2
== NULL
) /* type error ? */
4391 p
= concat_str(s1
, s2
);
4393 rettv
->v_type
= VAR_STRING
;
4394 rettv
->vval
.v_string
= p
;
4396 else if (op
== '+' && rettv
->v_type
== VAR_LIST
4397 && var2
.v_type
== VAR_LIST
)
4399 /* concatenate Lists */
4400 if (list_concat(rettv
->vval
.v_list
, var2
.vval
.v_list
,
4414 n1
= get_tv_number_chk(rettv
, &error
);
4417 /* This can only happen for "list + non-list".
4418 * For "non-list + ..." or "something - ...", we returned
4419 * before evaluating the 2nd operand. */
4423 n2
= get_tv_number_chk(&var2
, &error
);
4435 rettv
->v_type
= VAR_NUMBER
;
4436 rettv
->vval
.v_number
= n1
;
4445 * Handle fifth level expression:
4446 * * number multiplication
4450 * "arg" must point to the first non-white of the expression.
4451 * "arg" is advanced to the next non-white after the recognized expression.
4453 * Return OK or FAIL.
4456 eval6(arg
, rettv
, evaluate
)
4467 * Get the first variable.
4469 if (eval7(arg
, rettv
, evaluate
) == FAIL
)
4473 * Repeat computing, until no '*', '/' or '%' is following.
4478 if (op
!= '*' && op
!= '/' && op
!= '%')
4483 n1
= get_tv_number_chk(rettv
, &error
);
4492 * Get the second variable.
4494 *arg
= skipwhite(*arg
+ 1);
4495 if (eval7(arg
, &var2
, evaluate
) == FAIL
)
4500 n2
= get_tv_number_chk(&var2
, &error
);
4506 * Compute the result.
4512 if (n2
== 0) /* give an error message? */
4519 if (n2
== 0) /* give an error message? */
4524 rettv
->v_type
= VAR_NUMBER
;
4525 rettv
->vval
.v_number
= n1
;
4533 * Handle sixth level expression:
4534 * number number constant
4535 * "string" string constant
4536 * 'string' literal string constant
4537 * &option-name option value
4538 * @r register contents
4539 * identifier variable value
4540 * function() function call
4541 * $VAR environment variable
4542 * (expression) nested expression
4544 * {key: val, key: val} Dictionary
4547 * ! in front logical NOT
4548 * - in front unary minus
4549 * + in front unary plus (ignored)
4550 * trailing [] subscript in String or List
4551 * trailing .name entry in Dictionary
4553 * "arg" must point to the first non-white of the expression.
4554 * "arg" is advanced to the next non-white after the recognized expression.
4556 * Return OK or FAIL.
4559 eval7(arg
, rettv
, evaluate
)
4568 char_u
*start_leader
, *end_leader
;
4573 * Initialise variable so that clear_tv() can't mistake this for a
4574 * string and free a string that isn't there.
4576 rettv
->v_type
= VAR_UNKNOWN
;
4579 * Skip '!' and '-' characters. They are handled later.
4581 start_leader
= *arg
;
4582 while (**arg
== '!' || **arg
== '-' || **arg
== '+')
4583 *arg
= skipwhite(*arg
+ 1);
4601 vim_str2nr(*arg
, NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
4605 rettv
->v_type
= VAR_NUMBER
;
4606 rettv
->vval
.v_number
= n
;
4611 * String constant: "string".
4613 case '"': ret
= get_string_tv(arg
, rettv
, evaluate
);
4617 * Literal string constant: 'str''ing'.
4619 case '\'': ret
= get_lit_string_tv(arg
, rettv
, evaluate
);
4623 * List: [expr, expr]
4625 case '[': ret
= get_list_tv(arg
, rettv
, evaluate
);
4629 * Dictionary: {key: val, key: val}
4631 case '{': ret
= get_dict_tv(arg
, rettv
, evaluate
);
4635 * Option value: &name
4637 case '&': ret
= get_option_tv(arg
, rettv
, evaluate
);
4641 * Environment variable: $VAR.
4643 case '$': ret
= get_env_tv(arg
, rettv
, evaluate
);
4647 * Register contents: @r.
4652 rettv
->v_type
= VAR_STRING
;
4653 rettv
->vval
.v_string
= get_reg_contents(**arg
, TRUE
, TRUE
);
4660 * nested expression: (expression).
4662 case '(': *arg
= skipwhite(*arg
+ 1);
4663 ret
= eval1(arg
, rettv
, evaluate
); /* recursive! */
4668 EMSG(_("E110: Missing ')'"));
4674 default: ret
= NOTDONE
;
4681 * Must be a variable or function name.
4682 * Can also be a curly-braces kind of name: {expr}.
4685 len
= get_name_len(arg
, &alias
, evaluate
, TRUE
);
4693 if (**arg
== '(') /* recursive! */
4695 /* If "s" is the name of a variable of type VAR_FUNC
4696 * use its contents. */
4697 s
= deref_func_name(s
, &len
);
4699 /* Invoke the function. */
4700 ret
= get_func_tv(s
, len
, rettv
, arg
,
4701 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
4702 &len
, evaluate
, NULL
);
4703 /* Stop the expression evaluation when immediately
4704 * aborting on error, or when an interrupt occurred or
4705 * an exception was thrown but not caught. */
4714 ret
= get_var_tv(s
, len
, rettv
, TRUE
);
4723 *arg
= skipwhite(*arg
);
4725 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4728 ret
= handle_subscript(arg
, rettv
, evaluate
, TRUE
);
4731 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4733 if (ret
== OK
&& evaluate
&& end_leader
> start_leader
)
4737 val
= get_tv_number_chk(rettv
, &error
);
4745 while (end_leader
> start_leader
)
4748 if (*end_leader
== '!')
4750 else if (*end_leader
== '-')
4754 rettv
->v_type
= VAR_NUMBER
;
4755 rettv
->vval
.v_number
= val
;
4763 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4764 * "*arg" points to the '[' or '.'.
4765 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4768 eval_index(arg
, rettv
, evaluate
, verbose
)
4772 int verbose
; /* give error messages */
4774 int empty1
= FALSE
, empty2
= FALSE
;
4775 typval_T var1
, var2
;
4782 if (rettv
->v_type
== VAR_FUNC
)
4785 EMSG(_("E695: Cannot index a Funcref"));
4795 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
4799 *arg
= skipwhite(key
+ len
);
4806 * Get the (first) variable from inside the [].
4808 *arg
= skipwhite(*arg
+ 1);
4811 else if (eval1(arg
, &var1
, evaluate
) == FAIL
) /* recursive! */
4813 else if (evaluate
&& get_tv_string_chk(&var1
) == NULL
)
4815 /* not a number or string */
4821 * Get the second variable from inside the [:].
4826 *arg
= skipwhite(*arg
+ 1);
4829 else if (eval1(arg
, &var2
, evaluate
) == FAIL
) /* recursive! */
4835 else if (evaluate
&& get_tv_string_chk(&var2
) == NULL
)
4837 /* not a number or string */
4845 /* Check for the ']'. */
4849 EMSG(_(e_missbrac
));
4855 *arg
= skipwhite(*arg
+ 1); /* skip the ']' */
4861 if (!empty1
&& rettv
->v_type
!= VAR_DICT
)
4863 n1
= get_tv_number(&var1
);
4872 n2
= get_tv_number(&var2
);
4877 switch (rettv
->v_type
)
4881 s
= get_tv_string(rettv
);
4882 len
= (long)STRLEN(s
);
4885 /* The resulting variable is a substring. If the indexes
4886 * are out of range the result is empty. */
4897 if (n1
>= len
|| n2
< 0 || n1
> n2
)
4900 s
= vim_strnsave(s
+ n1
, (int)(n2
- n1
+ 1));
4904 /* The resulting variable is a string of a single
4905 * character. If the index is too big or negative the
4906 * result is empty. */
4907 if (n1
>= len
|| n1
< 0)
4910 s
= vim_strnsave(s
+ n1
, 1);
4913 rettv
->v_type
= VAR_STRING
;
4914 rettv
->vval
.v_string
= s
;
4918 len
= list_len(rettv
->vval
.v_list
);
4921 if (!empty1
&& (n1
< 0 || n1
>= len
))
4923 /* For a range we allow invalid values and return an empty
4924 * list. A list index out of range is an error. */
4928 EMSGN(_(e_listidx
), n1
);
4942 if (!empty2
&& (n2
< 0 || n2
+ 1 < n1
))
4947 for (item
= list_find(rettv
->vval
.v_list
, n1
);
4950 if (list_append_tv(l
, &item
->li_tv
) == FAIL
)
4955 item
= item
->li_next
;
4958 rettv
->v_type
= VAR_LIST
;
4959 rettv
->vval
.v_list
= l
;
4964 copy_tv(&list_find(rettv
->vval
.v_list
, n1
)->li_tv
, &var1
);
4974 EMSG(_(e_dictrange
));
4984 key
= get_tv_string(&var1
);
4988 EMSG(_(e_emptykey
));
4994 item
= dict_find(rettv
->vval
.v_dict
, key
, (int)len
);
4996 if (item
== NULL
&& verbose
)
4997 EMSG2(_(e_dictkey
), key
);
5003 copy_tv(&item
->di_tv
, &var1
);
5015 * Get an option value.
5016 * "arg" points to the '&' or '+' before the option name.
5017 * "arg" is advanced to character after the option name.
5018 * Return OK or FAIL.
5021 get_option_tv(arg
, rettv
, evaluate
)
5023 typval_T
*rettv
; /* when NULL, only check if option exists */
5031 int working
= (**arg
== '+'); /* has("+option") */
5036 * Isolate the option name and find its value.
5038 option_end
= find_option_end(arg
, &opt_flags
);
5039 if (option_end
== NULL
)
5042 EMSG2(_("E112: Option name missing: %s"), *arg
);
5054 opt_type
= get_option_value(*arg
, &numval
,
5055 rettv
== NULL
? NULL
: &stringval
, opt_flags
);
5057 if (opt_type
== -3) /* invalid name */
5060 EMSG2(_("E113: Unknown option: %s"), *arg
);
5063 else if (rettv
!= NULL
)
5065 if (opt_type
== -2) /* hidden string option */
5067 rettv
->v_type
= VAR_STRING
;
5068 rettv
->vval
.v_string
= NULL
;
5070 else if (opt_type
== -1) /* hidden number option */
5072 rettv
->v_type
= VAR_NUMBER
;
5073 rettv
->vval
.v_number
= 0;
5075 else if (opt_type
== 1) /* number option */
5077 rettv
->v_type
= VAR_NUMBER
;
5078 rettv
->vval
.v_number
= numval
;
5080 else /* string option */
5082 rettv
->v_type
= VAR_STRING
;
5083 rettv
->vval
.v_string
= stringval
;
5086 else if (working
&& (opt_type
== -2 || opt_type
== -1))
5089 *option_end
= c
; /* put back for error messages */
5096 * Allocate a variable for a string constant.
5097 * Return OK or FAIL.
5100 get_string_tv(arg
, rettv
, evaluate
)
5110 * Find the end of the string, skipping backslashed characters.
5112 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
5114 if (*p
== '\\' && p
[1] != NUL
)
5117 /* A "\<x>" form occupies at least 4 characters, and produces up
5118 * to 6 characters: reserve space for 2 extra */
5126 EMSG2(_("E114: Missing quote: %s"), *arg
);
5130 /* If only parsing, set *arg and return here */
5138 * Copy the string into allocated memory, handling backslashed
5141 name
= alloc((unsigned)(p
- *arg
+ extra
));
5144 rettv
->v_type
= VAR_STRING
;
5145 rettv
->vval
.v_string
= name
;
5147 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; )
5153 case 'b': *name
++ = BS
; ++p
; break;
5154 case 'e': *name
++ = ESC
; ++p
; break;
5155 case 'f': *name
++ = FF
; ++p
; break;
5156 case 'n': *name
++ = NL
; ++p
; break;
5157 case 'r': *name
++ = CAR
; ++p
; break;
5158 case 't': *name
++ = TAB
; ++p
; break;
5160 case 'X': /* hex: "\x1", "\x12" */
5162 case 'u': /* Unicode: "\u0023" */
5164 if (vim_isxdigit(p
[1]))
5167 int c
= toupper(*p
);
5174 while (--n
>= 0 && vim_isxdigit(p
[1]))
5177 nr
= (nr
<< 4) + hex2nr(*p
);
5181 /* For "\u" store the number according to
5184 name
+= (*mb_char2bytes
)(nr
, name
);
5191 /* octal: "\1", "\12", "\123" */
5199 case '7': *name
= *p
++ - '0';
5200 if (*p
>= '0' && *p
<= '7')
5202 *name
= (*name
<< 3) + *p
++ - '0';
5203 if (*p
>= '0' && *p
<= '7')
5204 *name
= (*name
<< 3) + *p
++ - '0';
5209 /* Special key, e.g.: "\<C-W>" */
5210 case '<': extra
= trans_special(&p
, name
, TRUE
);
5218 default: MB_COPY_CHAR(p
, name
);
5223 MB_COPY_CHAR(p
, name
);
5233 * Allocate a variable for a 'str''ing' constant.
5234 * Return OK or FAIL.
5237 get_lit_string_tv(arg
, rettv
, evaluate
)
5247 * Find the end of the string, skipping ''.
5249 for (p
= *arg
+ 1; *p
!= NUL
; mb_ptr_adv(p
))
5262 EMSG2(_("E115: Missing quote: %s"), *arg
);
5266 /* If only parsing return after setting "*arg" */
5274 * Copy the string into allocated memory, handling '' to ' reduction.
5276 str
= alloc((unsigned)((p
- *arg
) - reduce
));
5279 rettv
->v_type
= VAR_STRING
;
5280 rettv
->vval
.v_string
= str
;
5282 for (p
= *arg
+ 1; *p
!= NUL
; )
5290 MB_COPY_CHAR(p
, str
);
5299 * Allocate a variable for a List and fill it from "*arg".
5300 * Return OK or FAIL.
5303 get_list_tv(arg
, rettv
, evaluate
)
5319 *arg
= skipwhite(*arg
+ 1);
5320 while (**arg
!= ']' && **arg
!= NUL
)
5322 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
5326 item
= listitem_alloc();
5330 item
->li_tv
.v_lock
= 0;
5331 list_append(l
, item
);
5341 EMSG2(_("E696: Missing comma in List: %s"), *arg
);
5344 *arg
= skipwhite(*arg
+ 1);
5349 EMSG2(_("E697: Missing end of List ']': %s"), *arg
);
5356 *arg
= skipwhite(*arg
+ 1);
5359 rettv
->v_type
= VAR_LIST
;
5360 rettv
->vval
.v_list
= l
;
5368 * Allocate an empty header for a list.
5369 * Caller should take care of the reference count.
5376 l
= (list_T
*)alloc_clear(sizeof(list_T
));
5379 /* Prepend the list to the list of lists for garbage collection. */
5380 if (first_list
!= NULL
)
5381 first_list
->lv_used_prev
= l
;
5382 l
->lv_used_prev
= NULL
;
5383 l
->lv_used_next
= first_list
;
5390 * Allocate an empty list for a return value.
5391 * Returns OK or FAIL.
5394 rettv_list_alloc(rettv
)
5397 list_T
*l
= list_alloc();
5402 rettv
->vval
.v_list
= l
;
5403 rettv
->v_type
= VAR_LIST
;
5409 * Unreference a list: decrement the reference count and free it when it
5416 if (l
!= NULL
&& --l
->lv_refcount
<= 0)
5421 * Free a list, including all items it points to.
5422 * Ignores the reference count.
5425 list_free(l
, recurse
)
5427 int recurse
; /* Free Lists and Dictionaries recursively. */
5431 /* Remove the list from the list of lists for garbage collection. */
5432 if (l
->lv_used_prev
== NULL
)
5433 first_list
= l
->lv_used_next
;
5435 l
->lv_used_prev
->lv_used_next
= l
->lv_used_next
;
5436 if (l
->lv_used_next
!= NULL
)
5437 l
->lv_used_next
->lv_used_prev
= l
->lv_used_prev
;
5439 for (item
= l
->lv_first
; item
!= NULL
; item
= l
->lv_first
)
5441 /* Remove the item before deleting it. */
5442 l
->lv_first
= item
->li_next
;
5443 if (recurse
|| (item
->li_tv
.v_type
!= VAR_LIST
5444 && item
->li_tv
.v_type
!= VAR_DICT
))
5445 clear_tv(&item
->li_tv
);
5452 * Allocate a list item.
5457 return (listitem_T
*)alloc(sizeof(listitem_T
));
5461 * Free a list item. Also clears the value. Does not notify watchers.
5467 clear_tv(&item
->li_tv
);
5472 * Remove a list item from a List and free it. Also clears the value.
5475 listitem_remove(l
, item
)
5479 list_remove(l
, item
, item
);
5480 listitem_free(item
);
5484 * Get the number of items in a list.
5496 * Return TRUE when two lists have exactly the same values.
5499 list_equal(l1
, l2
, ic
)
5502 int ic
; /* ignore case for strings */
5504 listitem_T
*item1
, *item2
;
5508 if (list_len(l1
) != list_len(l2
))
5511 for (item1
= l1
->lv_first
, item2
= l2
->lv_first
;
5512 item1
!= NULL
&& item2
!= NULL
;
5513 item1
= item1
->li_next
, item2
= item2
->li_next
)
5514 if (!tv_equal(&item1
->li_tv
, &item2
->li_tv
, ic
))
5516 return item1
== NULL
&& item2
== NULL
;
5519 #if defined(FEAT_PYTHON) || defined(PROTO)
5521 * Return the dictitem that an entry in a hashtable points to.
5532 * Return TRUE when two dictionaries have exactly the same key/values.
5535 dict_equal(d1
, d2
, ic
)
5538 int ic
; /* ignore case for strings */
5546 if (dict_len(d1
) != dict_len(d2
))
5549 todo
= (int)d1
->dv_hashtab
.ht_used
;
5550 for (hi
= d1
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
5552 if (!HASHITEM_EMPTY(hi
))
5554 item2
= dict_find(d2
, hi
->hi_key
, -1);
5557 if (!tv_equal(&HI2DI(hi
)->di_tv
, &item2
->di_tv
, ic
))
5566 * Return TRUE if "tv1" and "tv2" have the same value.
5567 * Compares the items just like "==" would compare them, but strings and
5568 * numbers are different.
5571 tv_equal(tv1
, tv2
, ic
)
5574 int ic
; /* ignore case */
5576 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
5578 static int recursive
= 0; /* cach recursive loops */
5581 if (tv1
->v_type
!= tv2
->v_type
)
5583 /* Catch lists and dicts that have an endless loop by limiting
5584 * recursiveness to 1000. We guess they are equal then. */
5585 if (recursive
>= 1000)
5588 switch (tv1
->v_type
)
5592 r
= list_equal(tv1
->vval
.v_list
, tv2
->vval
.v_list
, ic
);
5598 r
= dict_equal(tv1
->vval
.v_dict
, tv2
->vval
.v_dict
, ic
);
5603 return (tv1
->vval
.v_string
!= NULL
5604 && tv2
->vval
.v_string
!= NULL
5605 && STRCMP(tv1
->vval
.v_string
, tv2
->vval
.v_string
) == 0);
5608 return tv1
->vval
.v_number
== tv2
->vval
.v_number
;
5611 s1
= get_tv_string_buf(tv1
, buf1
);
5612 s2
= get_tv_string_buf(tv2
, buf2
);
5613 return ((ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
)) == 0);
5616 EMSG2(_(e_intern2
), "tv_equal()");
5621 * Locate item with index "n" in list "l" and return it.
5622 * A negative index is counted from the end; -1 is the last item.
5623 * Returns NULL when "n" is out of range.
5636 /* Negative index is relative to the end. */
5640 /* Check for index out of range. */
5641 if (n
< 0 || n
>= l
->lv_len
)
5644 /* When there is a cached index may start search from there. */
5645 if (l
->lv_idx_item
!= NULL
)
5647 if (n
< l
->lv_idx
/ 2)
5649 /* closest to the start of the list */
5653 else if (n
> (l
->lv_idx
+ l
->lv_len
) / 2)
5655 /* closest to the end of the list */
5657 idx
= l
->lv_len
- 1;
5661 /* closest to the cached index */
5662 item
= l
->lv_idx_item
;
5668 if (n
< l
->lv_len
/ 2)
5670 /* closest to the start of the list */
5676 /* closest to the end of the list */
5678 idx
= l
->lv_len
- 1;
5684 /* search forward */
5685 item
= item
->li_next
;
5690 /* search backward */
5691 item
= item
->li_prev
;
5695 /* cache the used index */
5697 l
->lv_idx_item
= item
;
5703 * Get list item "l[idx]" as a number.
5706 list_find_nr(l
, idx
, errorp
)
5709 int *errorp
; /* set to TRUE when something wrong */
5713 li
= list_find(l
, idx
);
5720 return get_tv_number_chk(&li
->li_tv
, errorp
);
5724 * Locate "item" list "l" and return its index.
5725 * Returns -1 when "item" is not in the list.
5728 list_idx_of_item(l
, item
)
5738 for (li
= l
->lv_first
; li
!= NULL
&& li
!= item
; li
= li
->li_next
)
5746 * Append item "item" to the end of list "l".
5749 list_append(l
, item
)
5753 if (l
->lv_last
== NULL
)
5758 item
->li_prev
= NULL
;
5762 l
->lv_last
->li_next
= item
;
5763 item
->li_prev
= l
->lv_last
;
5767 item
->li_next
= NULL
;
5771 * Append typval_T "tv" to the end of list "l".
5772 * Return FAIL when out of memory.
5775 list_append_tv(l
, tv
)
5779 listitem_T
*li
= listitem_alloc();
5783 copy_tv(tv
, &li
->li_tv
);
5789 * Add a dictionary to a list. Used by getqflist().
5790 * Return FAIL when out of memory.
5793 list_append_dict(list
, dict
)
5797 listitem_T
*li
= listitem_alloc();
5801 li
->li_tv
.v_type
= VAR_DICT
;
5802 li
->li_tv
.v_lock
= 0;
5803 li
->li_tv
.vval
.v_dict
= dict
;
5804 list_append(list
, li
);
5805 ++dict
->dv_refcount
;
5810 * Make a copy of "str" and append it as an item to list "l".
5811 * When "len" >= 0 use "str[len]".
5812 * Returns FAIL when out of memory.
5815 list_append_string(l
, str
, len
)
5820 listitem_T
*li
= listitem_alloc();
5825 li
->li_tv
.v_type
= VAR_STRING
;
5826 li
->li_tv
.v_lock
= 0;
5828 li
->li_tv
.vval
.v_string
= NULL
;
5829 else if ((li
->li_tv
.vval
.v_string
= (len
>= 0 ? vim_strnsave(str
, len
)
5830 : vim_strsave(str
))) == NULL
)
5836 * Append "n" to list "l".
5837 * Returns FAIL when out of memory.
5840 list_append_number(l
, n
)
5846 li
= listitem_alloc();
5849 li
->li_tv
.v_type
= VAR_NUMBER
;
5850 li
->li_tv
.v_lock
= 0;
5851 li
->li_tv
.vval
.v_number
= n
;
5857 * Insert typval_T "tv" in list "l" before "item".
5858 * If "item" is NULL append at the end.
5859 * Return FAIL when out of memory.
5862 list_insert_tv(l
, tv
, item
)
5867 listitem_T
*ni
= listitem_alloc();
5871 copy_tv(tv
, &ni
->li_tv
);
5873 /* Append new item at end of list. */
5877 /* Insert new item before existing item. */
5878 ni
->li_prev
= item
->li_prev
;
5880 if (item
->li_prev
== NULL
)
5887 item
->li_prev
->li_next
= ni
;
5888 l
->lv_idx_item
= NULL
;
5897 * Extend "l1" with "l2".
5898 * If "bef" is NULL append at the end, otherwise insert before this item.
5899 * Returns FAIL when out of memory.
5902 list_extend(l1
, l2
, bef
)
5909 for (item
= l2
->lv_first
; item
!= NULL
; item
= item
->li_next
)
5910 if (list_insert_tv(l1
, &item
->li_tv
, bef
) == FAIL
)
5916 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5917 * Return FAIL when out of memory.
5920 list_concat(l1
, l2
, tv
)
5927 /* make a copy of the first list. */
5928 l
= list_copy(l1
, FALSE
, 0);
5931 tv
->v_type
= VAR_LIST
;
5932 tv
->vval
.v_list
= l
;
5934 /* append all items from the second list */
5935 return list_extend(l
, l2
, NULL
);
5939 * Make a copy of list "orig". Shallow if "deep" is FALSE.
5940 * The refcount of the new list is set to 1.
5941 * See item_copy() for "copyID".
5942 * Returns NULL when out of memory.
5945 list_copy(orig
, deep
, copyID
)
5957 copy
= list_alloc();
5962 /* Do this before adding the items, because one of the items may
5963 * refer back to this list. */
5964 orig
->lv_copyID
= copyID
;
5965 orig
->lv_copylist
= copy
;
5967 for (item
= orig
->lv_first
; item
!= NULL
&& !got_int
;
5968 item
= item
->li_next
)
5970 ni
= listitem_alloc();
5975 if (item_copy(&item
->li_tv
, &ni
->li_tv
, deep
, copyID
) == FAIL
)
5982 copy_tv(&item
->li_tv
, &ni
->li_tv
);
5983 list_append(copy
, ni
);
5985 ++copy
->lv_refcount
;
5997 * Remove items "item" to "item2" from list "l".
5998 * Does not free the listitem or the value!
6001 list_remove(l
, item
, item2
)
6008 /* notify watchers */
6009 for (ip
= item
; ip
!= NULL
; ip
= ip
->li_next
)
6012 list_fix_watch(l
, ip
);
6017 if (item2
->li_next
== NULL
)
6018 l
->lv_last
= item
->li_prev
;
6020 item2
->li_next
->li_prev
= item
->li_prev
;
6021 if (item
->li_prev
== NULL
)
6022 l
->lv_first
= item2
->li_next
;
6024 item
->li_prev
->li_next
= item2
->li_next
;
6025 l
->lv_idx_item
= NULL
;
6029 * Return an allocated string with the string representation of a list.
6033 list2string(tv
, copyID
)
6039 if (tv
->vval
.v_list
== NULL
)
6041 ga_init2(&ga
, (int)sizeof(char), 80);
6042 ga_append(&ga
, '[');
6043 if (list_join(&ga
, tv
->vval
.v_list
, (char_u
*)", ", FALSE
, copyID
) == FAIL
)
6045 vim_free(ga
.ga_data
);
6048 ga_append(&ga
, ']');
6049 ga_append(&ga
, NUL
);
6050 return (char_u
*)ga
.ga_data
;
6054 * Join list "l" into a string in "*gap", using separator "sep".
6055 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
6056 * Return FAIL or OK.
6059 list_join(gap
, l
, sep
, echo
, copyID
)
6068 char_u numbuf
[NUMBUFLEN
];
6072 for (item
= l
->lv_first
; item
!= NULL
&& !got_int
; item
= item
->li_next
)
6077 ga_concat(gap
, sep
);
6080 s
= echo_string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6082 s
= tv2string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6093 * Garbage collection for lists and dictionaries.
6095 * We use reference counts to be able to free most items right away when they
6096 * are no longer used. But for composite items it's possible that it becomes
6097 * unused while the reference count is > 0: When there is a recursive
6098 * reference. Example:
6099 * :let l = [1, 2, 3]
6103 * Since this is quite unusual we handle this with garbage collection: every
6104 * once in a while find out which lists and dicts are not referenced from any
6107 * Here is a good reference text about garbage collection (refers to Python
6108 * but it applies to all reference-counting mechanisms):
6109 * http://python.ca/nas/python/gc/
6113 * Do garbage collection for lists and dicts.
6114 * Return TRUE if some memory was freed.
6121 int copyID
= ++current_copyID
;
6126 int did_free
= FALSE
;
6131 /* Only do this once. */
6132 want_garbage_collect
= FALSE
;
6133 may_garbage_collect
= FALSE
;
6134 garbage_collect_at_exit
= FALSE
;
6137 * 1. Go through all accessible variables and mark all lists and dicts
6140 /* script-local variables */
6141 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
6142 set_ref_in_ht(&SCRIPT_VARS(i
), copyID
);
6144 /* buffer-local variables */
6145 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
6146 set_ref_in_ht(&buf
->b_vars
.dv_hashtab
, copyID
);
6148 /* window-local variables */
6149 FOR_ALL_TAB_WINDOWS(tp
, wp
)
6150 set_ref_in_ht(&wp
->w_vars
.dv_hashtab
, copyID
);
6153 /* tabpage-local variables */
6154 for (tp
= first_tabpage
; tp
!= NULL
; tp
= tp
->tp_next
)
6155 set_ref_in_ht(&tp
->tp_vars
.dv_hashtab
, copyID
);
6158 /* global variables */
6159 set_ref_in_ht(&globvarht
, copyID
);
6161 /* function-local variables */
6162 for (fc
= current_funccal
; fc
!= NULL
; fc
= fc
->caller
)
6164 set_ref_in_ht(&fc
->l_vars
.dv_hashtab
, copyID
);
6165 set_ref_in_ht(&fc
->l_avars
.dv_hashtab
, copyID
);
6169 * 2. Go through the list of dicts and free items without the copyID.
6171 for (dd
= first_dict
; dd
!= NULL
; )
6172 if (dd
->dv_copyID
!= copyID
)
6174 /* Free the Dictionary and ordinary items it contains, but don't
6175 * recurse into Lists and Dictionaries, they will be in the list
6176 * of dicts or list of lists. */
6177 dict_free(dd
, FALSE
);
6180 /* restart, next dict may also have been freed */
6184 dd
= dd
->dv_used_next
;
6187 * 3. Go through the list of lists and free items without the copyID.
6188 * But don't free a list that has a watcher (used in a for loop), these
6189 * are not referenced anywhere.
6191 for (ll
= first_list
; ll
!= NULL
; )
6192 if (ll
->lv_copyID
!= copyID
&& ll
->lv_watch
== NULL
)
6194 /* Free the List and ordinary items it contains, but don't recurse
6195 * into Lists and Dictionaries, they will be in the list of dicts
6196 * or list of lists. */
6197 list_free(ll
, FALSE
);
6200 /* restart, next list may also have been freed */
6204 ll
= ll
->lv_used_next
;
6210 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6213 set_ref_in_ht(ht
, copyID
)
6220 todo
= (int)ht
->ht_used
;
6221 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
6222 if (!HASHITEM_EMPTY(hi
))
6225 set_ref_in_item(&HI2DI(hi
)->di_tv
, copyID
);
6230 * Mark all lists and dicts referenced through list "l" with "copyID".
6233 set_ref_in_list(l
, copyID
)
6239 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
6240 set_ref_in_item(&li
->li_tv
, copyID
);
6244 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6247 set_ref_in_item(tv
, copyID
)
6257 dd
= tv
->vval
.v_dict
;
6258 if (dd
->dv_copyID
!= copyID
)
6260 /* Didn't see this dict yet. */
6261 dd
->dv_copyID
= copyID
;
6262 set_ref_in_ht(&dd
->dv_hashtab
, copyID
);
6267 ll
= tv
->vval
.v_list
;
6268 if (ll
->lv_copyID
!= copyID
)
6270 /* Didn't see this list yet. */
6271 ll
->lv_copyID
= copyID
;
6272 set_ref_in_list(ll
, copyID
);
6280 * Allocate an empty header for a dictionary.
6287 d
= (dict_T
*)alloc(sizeof(dict_T
));
6290 /* Add the list to the list of dicts for garbage collection. */
6291 if (first_dict
!= NULL
)
6292 first_dict
->dv_used_prev
= d
;
6293 d
->dv_used_next
= first_dict
;
6294 d
->dv_used_prev
= NULL
;
6297 hash_init(&d
->dv_hashtab
);
6306 * Unreference a Dictionary: decrement the reference count and free it when it
6313 if (d
!= NULL
&& --d
->dv_refcount
<= 0)
6318 * Free a Dictionary, including all items it contains.
6319 * Ignores the reference count.
6322 dict_free(d
, recurse
)
6324 int recurse
; /* Free Lists and Dictionaries recursively. */
6330 /* Remove the dict from the list of dicts for garbage collection. */
6331 if (d
->dv_used_prev
== NULL
)
6332 first_dict
= d
->dv_used_next
;
6334 d
->dv_used_prev
->dv_used_next
= d
->dv_used_next
;
6335 if (d
->dv_used_next
!= NULL
)
6336 d
->dv_used_next
->dv_used_prev
= d
->dv_used_prev
;
6338 /* Lock the hashtab, we don't want it to resize while freeing items. */
6339 hash_lock(&d
->dv_hashtab
);
6340 todo
= (int)d
->dv_hashtab
.ht_used
;
6341 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
6343 if (!HASHITEM_EMPTY(hi
))
6345 /* Remove the item before deleting it, just in case there is
6346 * something recursive causing trouble. */
6348 hash_remove(&d
->dv_hashtab
, hi
);
6349 if (recurse
|| (di
->di_tv
.v_type
!= VAR_LIST
6350 && di
->di_tv
.v_type
!= VAR_DICT
))
6351 clear_tv(&di
->di_tv
);
6356 hash_clear(&d
->dv_hashtab
);
6361 * Allocate a Dictionary item.
6362 * The "key" is copied to the new item.
6363 * Note that the value of the item "di_tv" still needs to be initialized!
6364 * Returns NULL when out of memory.
6372 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
) + STRLEN(key
)));
6375 STRCPY(di
->di_key
, key
);
6382 * Make a copy of a Dictionary item.
6390 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
6391 + STRLEN(org
->di_key
)));
6394 STRCPY(di
->di_key
, org
->di_key
);
6396 copy_tv(&org
->di_tv
, &di
->di_tv
);
6402 * Remove item "item" from Dictionary "dict" and free it.
6405 dictitem_remove(dict
, item
)
6411 hi
= hash_find(&dict
->dv_hashtab
, item
->di_key
);
6412 if (HASHITEM_EMPTY(hi
))
6413 EMSG2(_(e_intern2
), "dictitem_remove()");
6415 hash_remove(&dict
->dv_hashtab
, hi
);
6416 dictitem_free(item
);
6420 * Free a dict item. Also clears the value.
6426 clear_tv(&item
->di_tv
);
6431 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6432 * The refcount of the new dict is set to 1.
6433 * See item_copy() for "copyID".
6434 * Returns NULL when out of memory.
6437 dict_copy(orig
, deep
, copyID
)
6450 copy
= dict_alloc();
6455 orig
->dv_copyID
= copyID
;
6456 orig
->dv_copydict
= copy
;
6458 todo
= (int)orig
->dv_hashtab
.ht_used
;
6459 for (hi
= orig
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
6461 if (!HASHITEM_EMPTY(hi
))
6465 di
= dictitem_alloc(hi
->hi_key
);
6470 if (item_copy(&HI2DI(hi
)->di_tv
, &di
->di_tv
, deep
,
6478 copy_tv(&HI2DI(hi
)->di_tv
, &di
->di_tv
);
6479 if (dict_add(copy
, di
) == FAIL
)
6487 ++copy
->dv_refcount
;
6499 * Add item "item" to Dictionary "d".
6500 * Returns FAIL when out of memory and when key already existed.
6507 return hash_add(&d
->dv_hashtab
, item
->di_key
);
6511 * Add a number or string entry to dictionary "d".
6512 * When "str" is NULL use number "nr", otherwise use "str".
6513 * Returns FAIL when out of memory and when key already exists.
6516 dict_add_nr_str(d
, key
, nr
, str
)
6524 item
= dictitem_alloc((char_u
*)key
);
6527 item
->di_tv
.v_lock
= 0;
6530 item
->di_tv
.v_type
= VAR_NUMBER
;
6531 item
->di_tv
.vval
.v_number
= nr
;
6535 item
->di_tv
.v_type
= VAR_STRING
;
6536 item
->di_tv
.vval
.v_string
= vim_strsave(str
);
6538 if (dict_add(d
, item
) == FAIL
)
6540 dictitem_free(item
);
6547 * Get the number of items in a Dictionary.
6555 return (long)d
->dv_hashtab
.ht_used
;
6559 * Find item "key[len]" in Dictionary "d".
6560 * If "len" is negative use strlen(key).
6561 * Returns NULL when not found.
6564 dict_find(d
, key
, len
)
6570 char_u buf
[AKEYLEN
];
6572 char_u
*tofree
= NULL
;
6577 else if (len
>= AKEYLEN
)
6579 tofree
= akey
= vim_strnsave(key
, len
);
6585 /* Avoid a malloc/free by using buf[]. */
6586 vim_strncpy(buf
, key
, len
);
6590 hi
= hash_find(&d
->dv_hashtab
, akey
);
6592 if (HASHITEM_EMPTY(hi
))
6598 * Get a string item from a dictionary.
6599 * When "save" is TRUE allocate memory for it.
6600 * Returns NULL if the entry doesn't exist or out of memory.
6603 get_dict_string(d
, key
, save
)
6611 di
= dict_find(d
, key
, -1);
6614 s
= get_tv_string(&di
->di_tv
);
6615 if (save
&& s
!= NULL
)
6621 * Get a number item from a dictionary.
6622 * Returns 0 if the entry doesn't exist or out of memory.
6625 get_dict_number(d
, key
)
6631 di
= dict_find(d
, key
, -1);
6634 return get_tv_number(&di
->di_tv
);
6638 * Return an allocated string with the string representation of a Dictionary.
6642 dict2string(tv
, copyID
)
6649 char_u numbuf
[NUMBUFLEN
];
6655 if ((d
= tv
->vval
.v_dict
) == NULL
)
6657 ga_init2(&ga
, (int)sizeof(char), 80);
6658 ga_append(&ga
, '{');
6660 todo
= (int)d
->dv_hashtab
.ht_used
;
6661 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
6663 if (!HASHITEM_EMPTY(hi
))
6670 ga_concat(&ga
, (char_u
*)", ");
6672 tofree
= string_quote(hi
->hi_key
, FALSE
);
6675 ga_concat(&ga
, tofree
);
6678 ga_concat(&ga
, (char_u
*)": ");
6679 s
= tv2string(&HI2DI(hi
)->di_tv
, &tofree
, numbuf
, copyID
);
6689 vim_free(ga
.ga_data
);
6693 ga_append(&ga
, '}');
6694 ga_append(&ga
, NUL
);
6695 return (char_u
*)ga
.ga_data
;
6699 * Allocate a variable for a Dictionary and fill it from "*arg".
6700 * Return OK or FAIL. Returns NOTDONE for {expr}.
6703 get_dict_tv(arg
, rettv
, evaluate
)
6713 char_u
*start
= skipwhite(*arg
+ 1);
6714 char_u buf
[NUMBUFLEN
];
6717 * First check if it's not a curly-braces thing: {expr}.
6718 * Must do this without evaluating, otherwise a function may be called
6719 * twice. Unfortunately this means we need to call eval1() twice for the
6721 * But {} is an empty Dictionary.
6725 if (eval1(&start
, &tv
, FALSE
) == FAIL
) /* recursive! */
6737 tvkey
.v_type
= VAR_UNKNOWN
;
6738 tv
.v_type
= VAR_UNKNOWN
;
6740 *arg
= skipwhite(*arg
+ 1);
6741 while (**arg
!= '}' && **arg
!= NUL
)
6743 if (eval1(arg
, &tvkey
, evaluate
) == FAIL
) /* recursive! */
6747 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg
);
6753 key
= get_tv_string_buf_chk(&tvkey
, buf
);
6754 if (key
== NULL
|| *key
== NUL
)
6756 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6758 EMSG(_(e_emptykey
));
6764 *arg
= skipwhite(*arg
+ 1);
6765 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
6773 item
= dict_find(d
, key
, -1);
6776 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key
);
6781 item
= dictitem_alloc(key
);
6786 item
->di_tv
.v_lock
= 0;
6787 if (dict_add(d
, item
) == FAIL
)
6788 dictitem_free(item
);
6796 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg
);
6799 *arg
= skipwhite(*arg
+ 1);
6804 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg
);
6811 *arg
= skipwhite(*arg
+ 1);
6814 rettv
->v_type
= VAR_DICT
;
6815 rettv
->vval
.v_dict
= d
;
6823 * Return a string with the string representation of a variable.
6824 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6825 * "numbuf" is used for a number.
6826 * Does not put quotes around strings, as ":echo" displays values.
6827 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6831 echo_string(tv
, tofree
, numbuf
, copyID
)
6837 static int recurse
= 0;
6840 if (recurse
>= DICT_MAXNEST
)
6842 EMSG(_("E724: variable nested too deep for displaying"));
6852 r
= tv
->vval
.v_string
;
6856 if (tv
->vval
.v_list
== NULL
)
6861 else if (copyID
!= 0 && tv
->vval
.v_list
->lv_copyID
== copyID
)
6864 r
= (char_u
*)"[...]";
6868 tv
->vval
.v_list
->lv_copyID
= copyID
;
6869 *tofree
= list2string(tv
, copyID
);
6875 if (tv
->vval
.v_dict
== NULL
)
6880 else if (copyID
!= 0 && tv
->vval
.v_dict
->dv_copyID
== copyID
)
6883 r
= (char_u
*)"{...}";
6887 tv
->vval
.v_dict
->dv_copyID
= copyID
;
6888 *tofree
= dict2string(tv
, copyID
);
6896 r
= get_tv_string_buf(tv
, numbuf
);
6900 EMSG2(_(e_intern2
), "echo_string()");
6909 * Return a string with the string representation of a variable.
6910 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6911 * "numbuf" is used for a number.
6912 * Puts quotes around strings, so that they can be parsed back by eval().
6916 tv2string(tv
, tofree
, numbuf
, copyID
)
6925 *tofree
= string_quote(tv
->vval
.v_string
, TRUE
);
6928 *tofree
= string_quote(tv
->vval
.v_string
, FALSE
);
6935 EMSG2(_(e_intern2
), "tv2string()");
6937 return echo_string(tv
, tofree
, numbuf
, copyID
);
6941 * Return string "str" in ' quotes, doubling ' characters.
6942 * If "str" is NULL an empty string is assumed.
6943 * If "function" is TRUE make it function('string').
6946 string_quote(str
, function
)
6953 len
= (function
? 13 : 3);
6956 len
+= (unsigned)STRLEN(str
);
6957 for (p
= str
; *p
!= NUL
; mb_ptr_adv(p
))
6966 STRCPY(r
, "function('");
6972 for (p
= str
; *p
!= NUL
; )
6987 * Get the value of an environment variable.
6988 * "arg" is pointing to the '$'. It is advanced to after the name.
6989 * If the environment variable was not set, silently assume it is empty.
6993 get_env_tv(arg
, rettv
, evaluate
)
6998 char_u
*string
= NULL
;
7002 int mustfree
= FALSE
;
7006 len
= get_env_len(arg
);
7013 /* first try vim_getenv(), fast for normal environment vars */
7014 string
= vim_getenv(name
, &mustfree
);
7015 if (string
!= NULL
&& *string
!= NUL
)
7018 string
= vim_strsave(string
);
7025 /* next try expanding things like $VIM and ${HOME} */
7026 string
= expand_env_save(name
- 1);
7027 if (string
!= NULL
&& *string
== '$')
7035 rettv
->v_type
= VAR_STRING
;
7036 rettv
->vval
.v_string
= string
;
7043 * Array with names and number of arguments of all internal functions
7044 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7048 char *f_name
; /* function name */
7049 char f_min_argc
; /* minimal number of arguments */
7050 char f_max_argc
; /* maximal number of arguments */
7051 void (*f_func
) __ARGS((typval_T
*args
, typval_T
*rvar
));
7052 /* implementation of function */
7055 {"add", 2, 2, f_add
},
7056 {"append", 2, 2, f_append
},
7057 {"argc", 0, 0, f_argc
},
7058 {"argidx", 0, 0, f_argidx
},
7059 {"argv", 0, 1, f_argv
},
7060 {"browse", 4, 4, f_browse
},
7061 {"browsedir", 2, 2, f_browsedir
},
7062 {"bufexists", 1, 1, f_bufexists
},
7063 {"buffer_exists", 1, 1, f_bufexists
}, /* obsolete */
7064 {"buffer_name", 1, 1, f_bufname
}, /* obsolete */
7065 {"buffer_number", 1, 1, f_bufnr
}, /* obsolete */
7066 {"buflisted", 1, 1, f_buflisted
},
7067 {"bufloaded", 1, 1, f_bufloaded
},
7068 {"bufname", 1, 1, f_bufname
},
7069 {"bufnr", 1, 2, f_bufnr
},
7070 {"bufwinnr", 1, 1, f_bufwinnr
},
7071 {"byte2line", 1, 1, f_byte2line
},
7072 {"byteidx", 2, 2, f_byteidx
},
7073 {"call", 2, 3, f_call
},
7074 {"changenr", 0, 0, f_changenr
},
7075 {"char2nr", 1, 1, f_char2nr
},
7076 {"cindent", 1, 1, f_cindent
},
7077 {"clearmatches", 0, 0, f_clearmatches
},
7078 {"col", 1, 1, f_col
},
7079 #if defined(FEAT_INS_EXPAND)
7080 {"complete", 2, 2, f_complete
},
7081 {"complete_add", 1, 1, f_complete_add
},
7082 {"complete_check", 0, 0, f_complete_check
},
7084 {"confirm", 1, 4, f_confirm
},
7085 {"copy", 1, 1, f_copy
},
7086 {"count", 2, 4, f_count
},
7087 {"cscope_connection",0,3, f_cscope_connection
},
7088 {"cursor", 1, 3, f_cursor
},
7089 {"deepcopy", 1, 2, f_deepcopy
},
7090 {"delete", 1, 1, f_delete
},
7091 {"did_filetype", 0, 0, f_did_filetype
},
7092 {"diff_filler", 1, 1, f_diff_filler
},
7093 {"diff_hlID", 2, 2, f_diff_hlID
},
7094 {"empty", 1, 1, f_empty
},
7095 {"escape", 2, 2, f_escape
},
7096 {"eval", 1, 1, f_eval
},
7097 {"eventhandler", 0, 0, f_eventhandler
},
7098 {"executable", 1, 1, f_executable
},
7099 {"exists", 1, 1, f_exists
},
7100 {"expand", 1, 2, f_expand
},
7101 {"extend", 2, 3, f_extend
},
7102 {"feedkeys", 1, 2, f_feedkeys
},
7103 {"file_readable", 1, 1, f_filereadable
}, /* obsolete */
7104 {"filereadable", 1, 1, f_filereadable
},
7105 {"filewritable", 1, 1, f_filewritable
},
7106 {"filter", 2, 2, f_filter
},
7107 {"finddir", 1, 3, f_finddir
},
7108 {"findfile", 1, 3, f_findfile
},
7109 {"fnamemodify", 2, 2, f_fnamemodify
},
7110 {"foldclosed", 1, 1, f_foldclosed
},
7111 {"foldclosedend", 1, 1, f_foldclosedend
},
7112 {"foldlevel", 1, 1, f_foldlevel
},
7113 {"foldtext", 0, 0, f_foldtext
},
7114 {"foldtextresult", 1, 1, f_foldtextresult
},
7115 {"foreground", 0, 0, f_foreground
},
7116 {"function", 1, 1, f_function
},
7117 {"garbagecollect", 0, 1, f_garbagecollect
},
7118 {"get", 2, 3, f_get
},
7119 {"getbufline", 2, 3, f_getbufline
},
7120 {"getbufvar", 2, 2, f_getbufvar
},
7121 {"getchar", 0, 1, f_getchar
},
7122 {"getcharmod", 0, 0, f_getcharmod
},
7123 {"getcmdline", 0, 0, f_getcmdline
},
7124 {"getcmdpos", 0, 0, f_getcmdpos
},
7125 {"getcmdtype", 0, 0, f_getcmdtype
},
7126 {"getcwd", 0, 0, f_getcwd
},
7127 {"getfontname", 0, 1, f_getfontname
},
7128 {"getfperm", 1, 1, f_getfperm
},
7129 {"getfsize", 1, 1, f_getfsize
},
7130 {"getftime", 1, 1, f_getftime
},
7131 {"getftype", 1, 1, f_getftype
},
7132 {"getline", 1, 2, f_getline
},
7133 {"getloclist", 1, 1, f_getqflist
},
7134 {"getmatches", 0, 0, f_getmatches
},
7135 {"getpos", 1, 1, f_getpos
},
7136 {"getqflist", 0, 0, f_getqflist
},
7137 {"getreg", 0, 2, f_getreg
},
7138 {"getregtype", 0, 1, f_getregtype
},
7139 {"gettabwinvar", 3, 3, f_gettabwinvar
},
7140 {"getwinposx", 0, 0, f_getwinposx
},
7141 {"getwinposy", 0, 0, f_getwinposy
},
7142 {"getwinvar", 2, 2, f_getwinvar
},
7143 {"glob", 1, 1, f_glob
},
7144 {"globpath", 2, 2, f_globpath
},
7145 {"has", 1, 1, f_has
},
7146 {"has_key", 2, 2, f_has_key
},
7147 {"haslocaldir", 0, 0, f_haslocaldir
},
7148 {"hasmapto", 1, 3, f_hasmapto
},
7149 {"highlightID", 1, 1, f_hlID
}, /* obsolete */
7150 {"highlight_exists",1, 1, f_hlexists
}, /* obsolete */
7151 {"histadd", 2, 2, f_histadd
},
7152 {"histdel", 1, 2, f_histdel
},
7153 {"histget", 1, 2, f_histget
},
7154 {"histnr", 1, 1, f_histnr
},
7155 {"hlID", 1, 1, f_hlID
},
7156 {"hlexists", 1, 1, f_hlexists
},
7157 {"hostname", 0, 0, f_hostname
},
7158 {"iconv", 3, 3, f_iconv
},
7159 {"indent", 1, 1, f_indent
},
7160 {"index", 2, 4, f_index
},
7161 {"input", 1, 3, f_input
},
7162 {"inputdialog", 1, 3, f_inputdialog
},
7163 {"inputlist", 1, 1, f_inputlist
},
7164 {"inputrestore", 0, 0, f_inputrestore
},
7165 {"inputsave", 0, 0, f_inputsave
},
7166 {"inputsecret", 1, 2, f_inputsecret
},
7167 {"insert", 2, 3, f_insert
},
7168 {"isdirectory", 1, 1, f_isdirectory
},
7169 {"islocked", 1, 1, f_islocked
},
7170 {"items", 1, 1, f_items
},
7171 {"join", 1, 2, f_join
},
7172 {"keys", 1, 1, f_keys
},
7173 {"last_buffer_nr", 0, 0, f_last_buffer_nr
},/* obsolete */
7174 {"len", 1, 1, f_len
},
7175 {"libcall", 3, 3, f_libcall
},
7176 {"libcallnr", 3, 3, f_libcallnr
},
7177 {"line", 1, 1, f_line
},
7178 {"line2byte", 1, 1, f_line2byte
},
7179 {"lispindent", 1, 1, f_lispindent
},
7180 {"localtime", 0, 0, f_localtime
},
7181 {"map", 2, 2, f_map
},
7182 {"maparg", 1, 3, f_maparg
},
7183 {"mapcheck", 1, 3, f_mapcheck
},
7184 {"match", 2, 4, f_match
},
7185 {"matchadd", 2, 4, f_matchadd
},
7186 {"matcharg", 1, 1, f_matcharg
},
7187 {"matchdelete", 1, 1, f_matchdelete
},
7188 {"matchend", 2, 4, f_matchend
},
7189 {"matchlist", 2, 4, f_matchlist
},
7190 {"matchstr", 2, 4, f_matchstr
},
7191 {"max", 1, 1, f_max
},
7192 {"min", 1, 1, f_min
},
7194 {"mkdir", 1, 3, f_mkdir
},
7196 {"mode", 0, 0, f_mode
},
7197 {"nextnonblank", 1, 1, f_nextnonblank
},
7198 {"nr2char", 1, 1, f_nr2char
},
7199 {"pathshorten", 1, 1, f_pathshorten
},
7200 {"prevnonblank", 1, 1, f_prevnonblank
},
7201 {"printf", 2, 19, f_printf
},
7202 {"pumvisible", 0, 0, f_pumvisible
},
7203 {"range", 1, 3, f_range
},
7204 {"readfile", 1, 3, f_readfile
},
7205 {"reltime", 0, 2, f_reltime
},
7206 {"reltimestr", 1, 1, f_reltimestr
},
7207 {"remote_expr", 2, 3, f_remote_expr
},
7208 {"remote_foreground", 1, 1, f_remote_foreground
},
7209 {"remote_peek", 1, 2, f_remote_peek
},
7210 {"remote_read", 1, 1, f_remote_read
},
7211 {"remote_send", 2, 3, f_remote_send
},
7212 {"remove", 2, 3, f_remove
},
7213 {"rename", 2, 2, f_rename
},
7214 {"repeat", 2, 2, f_repeat
},
7215 {"resolve", 1, 1, f_resolve
},
7216 {"reverse", 1, 1, f_reverse
},
7217 {"search", 1, 4, f_search
},
7218 {"searchdecl", 1, 3, f_searchdecl
},
7219 {"searchpair", 3, 7, f_searchpair
},
7220 {"searchpairpos", 3, 7, f_searchpairpos
},
7221 {"searchpos", 1, 4, f_searchpos
},
7222 {"server2client", 2, 2, f_server2client
},
7223 {"serverlist", 0, 0, f_serverlist
},
7224 {"setbufvar", 3, 3, f_setbufvar
},
7225 {"setcmdpos", 1, 1, f_setcmdpos
},
7226 {"setline", 2, 2, f_setline
},
7227 {"setloclist", 2, 3, f_setloclist
},
7228 {"setmatches", 1, 1, f_setmatches
},
7229 {"setpos", 2, 2, f_setpos
},
7230 {"setqflist", 1, 2, f_setqflist
},
7231 {"setreg", 2, 3, f_setreg
},
7232 {"settabwinvar", 4, 4, f_settabwinvar
},
7233 {"setwinvar", 3, 3, f_setwinvar
},
7234 {"shellescape", 1, 1, f_shellescape
},
7235 {"simplify", 1, 1, f_simplify
},
7236 {"sort", 1, 2, f_sort
},
7237 {"soundfold", 1, 1, f_soundfold
},
7238 {"spellbadword", 0, 1, f_spellbadword
},
7239 {"spellsuggest", 1, 3, f_spellsuggest
},
7240 {"split", 1, 3, f_split
},
7241 {"str2nr", 1, 2, f_str2nr
},
7242 #ifdef HAVE_STRFTIME
7243 {"strftime", 1, 2, f_strftime
},
7245 {"stridx", 2, 3, f_stridx
},
7246 {"string", 1, 1, f_string
},
7247 {"strlen", 1, 1, f_strlen
},
7248 {"strpart", 2, 3, f_strpart
},
7249 {"strridx", 2, 3, f_strridx
},
7250 {"strtrans", 1, 1, f_strtrans
},
7251 {"submatch", 1, 1, f_submatch
},
7252 {"substitute", 4, 4, f_substitute
},
7253 {"synID", 3, 3, f_synID
},
7254 {"synIDattr", 2, 3, f_synIDattr
},
7255 {"synIDtrans", 1, 1, f_synIDtrans
},
7256 {"synstack", 2, 2, f_synstack
},
7257 {"system", 1, 2, f_system
},
7258 {"tabpagebuflist", 0, 1, f_tabpagebuflist
},
7259 {"tabpagenr", 0, 1, f_tabpagenr
},
7260 {"tabpagewinnr", 1, 2, f_tabpagewinnr
},
7261 {"tagfiles", 0, 0, f_tagfiles
},
7262 {"taglist", 1, 1, f_taglist
},
7263 {"tempname", 0, 0, f_tempname
},
7264 {"test", 1, 1, f_test
},
7265 {"tolower", 1, 1, f_tolower
},
7266 {"toupper", 1, 1, f_toupper
},
7268 {"type", 1, 1, f_type
},
7269 {"values", 1, 1, f_values
},
7270 {"virtcol", 1, 1, f_virtcol
},
7271 {"visualmode", 0, 1, f_visualmode
},
7272 {"winbufnr", 1, 1, f_winbufnr
},
7273 {"wincol", 0, 0, f_wincol
},
7274 {"winheight", 1, 1, f_winheight
},
7275 {"winline", 0, 0, f_winline
},
7276 {"winnr", 0, 1, f_winnr
},
7277 {"winrestcmd", 0, 0, f_winrestcmd
},
7278 {"winrestview", 1, 1, f_winrestview
},
7279 {"winsaveview", 0, 0, f_winsaveview
},
7280 {"winwidth", 1, 1, f_winwidth
},
7281 {"writefile", 2, 3, f_writefile
},
7284 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7287 * Function given to ExpandGeneric() to obtain the list of internal
7288 * or user defined function names.
7291 get_function_name(xp
, idx
)
7295 static int intidx
= -1;
7302 name
= get_user_func_name(xp
, idx
);
7306 if (++intidx
< (int)(sizeof(functions
) / sizeof(struct fst
)))
7308 STRCPY(IObuff
, functions
[intidx
].f_name
);
7309 STRCAT(IObuff
, "(");
7310 if (functions
[intidx
].f_max_argc
== 0)
7311 STRCAT(IObuff
, ")");
7319 * Function given to ExpandGeneric() to obtain the list of internal or
7320 * user defined variable or function names.
7324 get_expr_name(xp
, idx
)
7328 static int intidx
= -1;
7335 name
= get_function_name(xp
, idx
);
7339 return get_user_var_name(xp
, ++intidx
);
7342 #endif /* FEAT_CMDL_COMPL */
7345 * Find internal function in table above.
7346 * Return index, or -1 if not found
7349 find_internal_func(name
)
7350 char_u
*name
; /* name of the function */
7353 int last
= (int)(sizeof(functions
) / sizeof(struct fst
)) - 1;
7358 * Find the function name in the table. Binary search.
7360 while (first
<= last
)
7362 x
= first
+ ((unsigned)(last
- first
) >> 1);
7363 cmp
= STRCMP(name
, functions
[x
].f_name
);
7375 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7376 * name it contains, otherwise return "name".
7379 deref_func_name(name
, lenp
)
7388 v
= find_var(name
, NULL
);
7390 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
7392 if (v
->di_tv
.vval
.v_string
== NULL
)
7395 return (char_u
*)""; /* just in case */
7397 *lenp
= (int)STRLEN(v
->di_tv
.vval
.v_string
);
7398 return v
->di_tv
.vval
.v_string
;
7405 * Allocate a variable for the result of a function.
7406 * Return OK or FAIL.
7409 get_func_tv(name
, len
, rettv
, arg
, firstline
, lastline
, doesrange
,
7411 char_u
*name
; /* name of the function */
7412 int len
; /* length of "name" */
7414 char_u
**arg
; /* argument, pointing to the '(' */
7415 linenr_T firstline
; /* first line of range */
7416 linenr_T lastline
; /* last line of range */
7417 int *doesrange
; /* return: function handled range */
7419 dict_T
*selfdict
; /* Dictionary for "self" */
7423 typval_T argvars
[MAX_FUNC_ARGS
+ 1]; /* vars for arguments */
7424 int argcount
= 0; /* number of arguments found */
7427 * Get the arguments.
7430 while (argcount
< MAX_FUNC_ARGS
)
7432 argp
= skipwhite(argp
+ 1); /* skip the '(' or ',' */
7433 if (*argp
== ')' || *argp
== ',' || *argp
== NUL
)
7435 if (eval1(&argp
, &argvars
[argcount
], evaluate
) == FAIL
)
7450 ret
= call_func(name
, len
, rettv
, argcount
, argvars
,
7451 firstline
, lastline
, doesrange
, evaluate
, selfdict
);
7452 else if (!aborting())
7454 if (argcount
== MAX_FUNC_ARGS
)
7455 emsg_funcname("E740: Too many arguments for function %s", name
);
7457 emsg_funcname("E116: Invalid arguments for function %s", name
);
7460 while (--argcount
>= 0)
7461 clear_tv(&argvars
[argcount
]);
7463 *arg
= skipwhite(argp
);
7469 * Call a function with its resolved parameters
7470 * Return OK when the function can't be called, FAIL otherwise.
7471 * Also returns OK when an error was encountered while executing the function.
7474 call_func(name
, len
, rettv
, argcount
, argvars
, firstline
, lastline
,
7475 doesrange
, evaluate
, selfdict
)
7476 char_u
*name
; /* name of the function */
7477 int len
; /* length of "name" */
7478 typval_T
*rettv
; /* return value goes here */
7479 int argcount
; /* number of "argvars" */
7480 typval_T
*argvars
; /* vars for arguments, must have "argcount"
7481 PLUS ONE elements! */
7482 linenr_T firstline
; /* first line of range */
7483 linenr_T lastline
; /* last line of range */
7484 int *doesrange
; /* return: function handled range */
7486 dict_T
*selfdict
; /* Dictionary for "self" */
7489 #define ERROR_UNKNOWN 0
7490 #define ERROR_TOOMANY 1
7491 #define ERROR_TOOFEW 2
7492 #define ERROR_SCRIPT 3
7493 #define ERROR_DICT 4
7494 #define ERROR_NONE 5
7495 #define ERROR_OTHER 6
7496 int error
= ERROR_NONE
;
7501 #define FLEN_FIXED 40
7502 char_u fname_buf
[FLEN_FIXED
+ 1];
7506 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7507 * Change <SNR>123_name() to K_SNR 123_name().
7508 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7512 llen
= eval_fname_script(name
);
7515 fname_buf
[0] = K_SPECIAL
;
7516 fname_buf
[1] = KS_EXTRA
;
7517 fname_buf
[2] = (int)KE_SNR
;
7519 if (eval_fname_sid(name
)) /* "<SID>" or "s:" */
7521 if (current_SID
<= 0)
7522 error
= ERROR_SCRIPT
;
7525 sprintf((char *)fname_buf
+ 3, "%ld_", (long)current_SID
);
7526 i
= (int)STRLEN(fname_buf
);
7529 if (i
+ STRLEN(name
+ llen
) < FLEN_FIXED
)
7531 STRCPY(fname_buf
+ i
, name
+ llen
);
7536 fname
= alloc((unsigned)(i
+ STRLEN(name
+ llen
) + 1));
7538 error
= ERROR_OTHER
;
7541 mch_memmove(fname
, fname_buf
, (size_t)i
);
7542 STRCPY(fname
+ i
, name
+ llen
);
7552 /* execute the function if no errors detected and executing */
7553 if (evaluate
&& error
== ERROR_NONE
)
7555 rettv
->v_type
= VAR_NUMBER
; /* default is number rettv */
7556 error
= ERROR_UNKNOWN
;
7558 if (!builtin_function(fname
))
7561 * User defined function.
7563 fp
= find_func(fname
);
7566 /* Trigger FuncUndefined event, may load the function. */
7568 && apply_autocmds(EVENT_FUNCUNDEFINED
,
7569 fname
, fname
, TRUE
, NULL
)
7572 /* executed an autocommand, search for the function again */
7573 fp
= find_func(fname
);
7576 /* Try loading a package. */
7577 if (fp
== NULL
&& script_autoload(fname
, TRUE
) && !aborting())
7579 /* loaded a package, search for the function again */
7580 fp
= find_func(fname
);
7585 if (fp
->uf_flags
& FC_RANGE
)
7587 if (argcount
< fp
->uf_args
.ga_len
)
7588 error
= ERROR_TOOFEW
;
7589 else if (!fp
->uf_varargs
&& argcount
> fp
->uf_args
.ga_len
)
7590 error
= ERROR_TOOMANY
;
7591 else if ((fp
->uf_flags
& FC_DICT
) && selfdict
== NULL
)
7596 * Call the user function.
7597 * Save and restore search patterns, script variables and
7600 save_search_patterns();
7603 call_user_func(fp
, argcount
, argvars
, rettv
,
7604 firstline
, lastline
,
7605 (fp
->uf_flags
& FC_DICT
) ? selfdict
: NULL
);
7606 if (--fp
->uf_calls
<= 0 && isdigit(*fp
->uf_name
)
7607 && fp
->uf_refcount
<= 0)
7608 /* Function was unreferenced while being used, free it
7612 restore_search_patterns();
7620 * Find the function name in the table, call its implementation.
7622 i
= find_internal_func(fname
);
7625 if (argcount
< functions
[i
].f_min_argc
)
7626 error
= ERROR_TOOFEW
;
7627 else if (argcount
> functions
[i
].f_max_argc
)
7628 error
= ERROR_TOOMANY
;
7631 argvars
[argcount
].v_type
= VAR_UNKNOWN
;
7632 functions
[i
].f_func(argvars
, rettv
);
7638 * The function call (or "FuncUndefined" autocommand sequence) might
7639 * have been aborted by an error, an interrupt, or an explicitly thrown
7640 * exception that has not been caught so far. This situation can be
7641 * tested for by calling aborting(). For an error in an internal
7642 * function or for the "E132" error in call_user_func(), however, the
7643 * throw point at which the "force_abort" flag (temporarily reset by
7644 * emsg()) is normally updated has not been reached yet. We need to
7645 * update that flag first to make aborting() reliable.
7647 update_force_abort();
7649 if (error
== ERROR_NONE
)
7653 * Report an error unless the argument evaluation or function call has been
7654 * cancelled due to an aborting error, an interrupt, or an exception.
7661 emsg_funcname(N_("E117: Unknown function: %s"), name
);
7664 emsg_funcname(e_toomanyarg
, name
);
7667 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
7671 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
7675 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
7682 if (fname
!= name
&& fname
!= fname_buf
)
7689 * Give an error message with a function name. Handle <SNR> things.
7692 emsg_funcname(ermsg
, name
)
7698 if (*name
== K_SPECIAL
)
7699 p
= concat_str((char_u
*)"<SNR>", name
+ 3);
7707 /*********************************************
7708 * Implementation of the built-in functions
7712 * "add(list, item)" function
7715 f_add(argvars
, rettv
)
7721 rettv
->vval
.v_number
= 1; /* Default: Failed */
7722 if (argvars
[0].v_type
== VAR_LIST
)
7724 if ((l
= argvars
[0].vval
.v_list
) != NULL
7725 && !tv_check_lock(l
->lv_lock
, (char_u
*)"add()")
7726 && list_append_tv(l
, &argvars
[1]) == OK
)
7727 copy_tv(&argvars
[0], rettv
);
7734 * "append(lnum, string/list)" function
7737 f_append(argvars
, rettv
)
7744 listitem_T
*li
= NULL
;
7748 lnum
= get_tv_lnum(argvars
);
7750 && lnum
<= curbuf
->b_ml
.ml_line_count
7751 && u_save(lnum
, lnum
+ 1) == OK
)
7753 if (argvars
[1].v_type
== VAR_LIST
)
7755 l
= argvars
[1].vval
.v_list
;
7760 rettv
->vval
.v_number
= 0; /* Default: Success */
7764 tv
= &argvars
[1]; /* append a string */
7765 else if (li
== NULL
)
7766 break; /* end of list */
7768 tv
= &li
->li_tv
; /* append item from list */
7769 line
= get_tv_string_chk(tv
);
7770 if (line
== NULL
) /* type error */
7772 rettv
->vval
.v_number
= 1; /* Failed */
7775 ml_append(lnum
+ added
, line
, (colnr_T
)0, FALSE
);
7782 appended_lines_mark(lnum
, added
);
7783 if (curwin
->w_cursor
.lnum
> lnum
)
7784 curwin
->w_cursor
.lnum
+= added
;
7787 rettv
->vval
.v_number
= 1; /* Failed */
7795 f_argc(argvars
, rettv
)
7799 rettv
->vval
.v_number
= ARGCOUNT
;
7803 * "argidx()" function
7807 f_argidx(argvars
, rettv
)
7811 rettv
->vval
.v_number
= curwin
->w_arg_idx
;
7815 * "argv(nr)" function
7818 f_argv(argvars
, rettv
)
7824 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
7826 idx
= get_tv_number_chk(&argvars
[0], NULL
);
7827 if (idx
>= 0 && idx
< ARGCOUNT
)
7828 rettv
->vval
.v_string
= vim_strsave(alist_name(&ARGLIST
[idx
]));
7830 rettv
->vval
.v_string
= NULL
;
7831 rettv
->v_type
= VAR_STRING
;
7833 else if (rettv_list_alloc(rettv
) == OK
)
7834 for (idx
= 0; idx
< ARGCOUNT
; ++idx
)
7835 list_append_string(rettv
->vval
.v_list
,
7836 alist_name(&ARGLIST
[idx
]), -1);
7840 * "browse(save, title, initdir, default)" function
7844 f_browse(argvars
, rettv
)
7853 char_u buf
[NUMBUFLEN
];
7854 char_u buf2
[NUMBUFLEN
];
7857 save
= get_tv_number_chk(&argvars
[0], &error
);
7858 title
= get_tv_string_chk(&argvars
[1]);
7859 initdir
= get_tv_string_buf_chk(&argvars
[2], buf
);
7860 defname
= get_tv_string_buf_chk(&argvars
[3], buf2
);
7862 if (error
|| title
== NULL
|| initdir
== NULL
|| defname
== NULL
)
7863 rettv
->vval
.v_string
= NULL
;
7865 rettv
->vval
.v_string
=
7866 do_browse(save
? BROWSE_SAVE
: 0,
7867 title
, defname
, NULL
, initdir
, NULL
, curbuf
);
7869 rettv
->vval
.v_string
= NULL
;
7871 rettv
->v_type
= VAR_STRING
;
7875 * "browsedir(title, initdir)" function
7879 f_browsedir(argvars
, rettv
)
7886 char_u buf
[NUMBUFLEN
];
7888 title
= get_tv_string_chk(&argvars
[0]);
7889 initdir
= get_tv_string_buf_chk(&argvars
[1], buf
);
7891 if (title
== NULL
|| initdir
== NULL
)
7892 rettv
->vval
.v_string
= NULL
;
7894 rettv
->vval
.v_string
= do_browse(BROWSE_DIR
,
7895 title
, NULL
, NULL
, initdir
, NULL
, curbuf
);
7897 rettv
->vval
.v_string
= NULL
;
7899 rettv
->v_type
= VAR_STRING
;
7902 static buf_T
*find_buffer
__ARGS((typval_T
*avar
));
7905 * Find a buffer by number or exact name.
7913 if (avar
->v_type
== VAR_NUMBER
)
7914 buf
= buflist_findnr((int)avar
->vval
.v_number
);
7915 else if (avar
->v_type
== VAR_STRING
&& avar
->vval
.v_string
!= NULL
)
7917 buf
= buflist_findname_exp(avar
->vval
.v_string
);
7920 /* No full path name match, try a match with a URL or a "nofile"
7921 * buffer, these don't use the full path. */
7922 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
7923 if (buf
->b_fname
!= NULL
7924 && (path_with_url(buf
->b_fname
)
7925 #ifdef FEAT_QUICKFIX
7929 && STRCMP(buf
->b_fname
, avar
->vval
.v_string
) == 0)
7937 * "bufexists(expr)" function
7940 f_bufexists(argvars
, rettv
)
7944 rettv
->vval
.v_number
= (find_buffer(&argvars
[0]) != NULL
);
7948 * "buflisted(expr)" function
7951 f_buflisted(argvars
, rettv
)
7957 buf
= find_buffer(&argvars
[0]);
7958 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_p_bl
);
7962 * "bufloaded(expr)" function
7965 f_bufloaded(argvars
, rettv
)
7971 buf
= find_buffer(&argvars
[0]);
7972 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_ml
.ml_mfp
!= NULL
);
7975 static buf_T
*get_buf_tv
__ARGS((typval_T
*tv
));
7978 * Get buffer by number or pattern.
7984 char_u
*name
= tv
->vval
.v_string
;
7989 if (tv
->v_type
== VAR_NUMBER
)
7990 return buflist_findnr((int)tv
->vval
.v_number
);
7991 if (tv
->v_type
!= VAR_STRING
)
7993 if (name
== NULL
|| *name
== NUL
)
7995 if (name
[0] == '$' && name
[1] == NUL
)
7998 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7999 save_magic
= p_magic
;
8002 p_cpo
= (char_u
*)"";
8004 buf
= buflist_findnr(buflist_findpat(name
, name
+ STRLEN(name
),
8007 p_magic
= save_magic
;
8010 /* If not found, try expanding the name, like done for bufexists(). */
8012 buf
= find_buffer(tv
);
8018 * "bufname(expr)" function
8021 f_bufname(argvars
, rettv
)
8027 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8029 buf
= get_buf_tv(&argvars
[0]);
8030 rettv
->v_type
= VAR_STRING
;
8031 if (buf
!= NULL
&& buf
->b_fname
!= NULL
)
8032 rettv
->vval
.v_string
= vim_strsave(buf
->b_fname
);
8034 rettv
->vval
.v_string
= NULL
;
8039 * "bufnr(expr)" function
8042 f_bufnr(argvars
, rettv
)
8050 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8052 buf
= get_buf_tv(&argvars
[0]);
8055 /* If the buffer isn't found and the second argument is not zero create a
8058 && argvars
[1].v_type
!= VAR_UNKNOWN
8059 && get_tv_number_chk(&argvars
[1], &error
) != 0
8061 && (name
= get_tv_string_chk(&argvars
[0])) != NULL
8063 buf
= buflist_new(name
, NULL
, (linenr_T
)1, 0);
8066 rettv
->vval
.v_number
= buf
->b_fnum
;
8068 rettv
->vval
.v_number
= -1;
8072 * "bufwinnr(nr)" function
8075 f_bufwinnr(argvars
, rettv
)
8085 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8087 buf
= get_buf_tv(&argvars
[0]);
8089 for (wp
= firstwin
; wp
; wp
= wp
->w_next
)
8092 if (wp
->w_buffer
== buf
)
8095 rettv
->vval
.v_number
= (wp
!= NULL
? winnr
: -1);
8097 rettv
->vval
.v_number
= (curwin
->w_buffer
== buf
? 1 : -1);
8103 * "byte2line(byte)" function
8107 f_byte2line(argvars
, rettv
)
8111 #ifndef FEAT_BYTEOFF
8112 rettv
->vval
.v_number
= -1;
8116 boff
= get_tv_number(&argvars
[0]) - 1; /* boff gets -1 on type error */
8118 rettv
->vval
.v_number
= -1;
8120 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
,
8121 (linenr_T
)0, &boff
);
8126 * "byteidx()" function
8130 f_byteidx(argvars
, rettv
)
8140 str
= get_tv_string_chk(&argvars
[0]);
8141 idx
= get_tv_number_chk(&argvars
[1], NULL
);
8142 rettv
->vval
.v_number
= -1;
8143 if (str
== NULL
|| idx
< 0)
8148 for ( ; idx
> 0; idx
--)
8150 if (*t
== NUL
) /* EOL reached */
8152 t
+= (*mb_ptr2len
)(t
);
8154 rettv
->vval
.v_number
= (varnumber_T
)(t
- str
);
8156 if (idx
<= STRLEN(str
))
8157 rettv
->vval
.v_number
= idx
;
8162 * "call(func, arglist)" function
8165 f_call(argvars
, rettv
)
8170 typval_T argv
[MAX_FUNC_ARGS
+ 1];
8174 dict_T
*selfdict
= NULL
;
8176 rettv
->vval
.v_number
= 0;
8177 if (argvars
[1].v_type
!= VAR_LIST
)
8182 if (argvars
[1].vval
.v_list
== NULL
)
8185 if (argvars
[0].v_type
== VAR_FUNC
)
8186 func
= argvars
[0].vval
.v_string
;
8188 func
= get_tv_string(&argvars
[0]);
8190 return; /* type error or empty name */
8192 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8194 if (argvars
[2].v_type
!= VAR_DICT
)
8199 selfdict
= argvars
[2].vval
.v_dict
;
8202 for (item
= argvars
[1].vval
.v_list
->lv_first
; item
!= NULL
;
8203 item
= item
->li_next
)
8205 if (argc
== MAX_FUNC_ARGS
)
8207 EMSG(_("E699: Too many arguments"));
8210 /* Make a copy of each argument. This is needed to be able to set
8211 * v_lock to VAR_FIXED in the copy without changing the original list.
8213 copy_tv(&item
->li_tv
, &argv
[argc
++]);
8217 (void)call_func(func
, (int)STRLEN(func
), rettv
, argc
, argv
,
8218 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
8219 &dummy
, TRUE
, selfdict
);
8221 /* Free the arguments. */
8223 clear_tv(&argv
[--argc
]);
8227 * "changenr()" function
8231 f_changenr(argvars
, rettv
)
8235 rettv
->vval
.v_number
= curbuf
->b_u_seq_cur
;
8239 * "char2nr(string)" function
8242 f_char2nr(argvars
, rettv
)
8248 rettv
->vval
.v_number
= (*mb_ptr2char
)(get_tv_string(&argvars
[0]));
8251 rettv
->vval
.v_number
= get_tv_string(&argvars
[0])[0];
8255 * "cindent(lnum)" function
8258 f_cindent(argvars
, rettv
)
8266 pos
= curwin
->w_cursor
;
8267 lnum
= get_tv_lnum(argvars
);
8268 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
8270 curwin
->w_cursor
.lnum
= lnum
;
8271 rettv
->vval
.v_number
= get_c_indent();
8272 curwin
->w_cursor
= pos
;
8276 rettv
->vval
.v_number
= -1;
8280 * "clearmatches()" function
8284 f_clearmatches(argvars
, rettv
)
8288 #ifdef FEAT_SEARCH_EXTRA
8289 clear_matches(curwin
);
8294 * "col(string)" function
8297 f_col(argvars
, rettv
)
8303 int fnum
= curbuf
->b_fnum
;
8305 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
8306 if (fp
!= NULL
&& fnum
== curbuf
->b_fnum
)
8308 if (fp
->col
== MAXCOL
)
8310 /* '> can be MAXCOL, get the length of the line then */
8311 if (fp
->lnum
<= curbuf
->b_ml
.ml_line_count
)
8312 col
= (colnr_T
)STRLEN(ml_get(fp
->lnum
)) + 1;
8319 #ifdef FEAT_VIRTUALEDIT
8320 /* col(".") when the cursor is on the NUL at the end of the line
8321 * because of "coladd" can be seen as an extra column. */
8322 if (virtual_active() && fp
== &curwin
->w_cursor
)
8324 char_u
*p
= ml_get_cursor();
8326 if (curwin
->w_cursor
.coladd
>= (colnr_T
)chartabsize(p
,
8327 curwin
->w_virtcol
- curwin
->w_cursor
.coladd
))
8332 if (*p
!= NUL
&& p
[(l
= (*mb_ptr2len
)(p
))] == NUL
)
8335 if (*p
!= NUL
&& p
[1] == NUL
)
8343 rettv
->vval
.v_number
= col
;
8346 #if defined(FEAT_INS_EXPAND)
8348 * "complete()" function
8352 f_complete(argvars
, rettv
)
8358 if ((State
& INSERT
) == 0)
8360 EMSG(_("E785: complete() can only be used in Insert mode"));
8364 /* Check for undo allowed here, because if something was already inserted
8365 * the line was already saved for undo and this check isn't done. */
8366 if (!undo_allowed())
8369 if (argvars
[1].v_type
!= VAR_LIST
|| argvars
[1].vval
.v_list
== NULL
)
8375 startcol
= get_tv_number_chk(&argvars
[0], NULL
);
8379 set_completion(startcol
- 1, argvars
[1].vval
.v_list
);
8383 * "complete_add()" function
8387 f_complete_add(argvars
, rettv
)
8391 rettv
->vval
.v_number
= ins_compl_add_tv(&argvars
[0], 0);
8395 * "complete_check()" function
8399 f_complete_check(argvars
, rettv
)
8403 int saved
= RedrawingDisabled
;
8405 RedrawingDisabled
= 0;
8406 ins_compl_check_keys(0);
8407 rettv
->vval
.v_number
= compl_interrupted
;
8408 RedrawingDisabled
= saved
;
8413 * "confirm(message, buttons[, default [, type]])" function
8417 f_confirm(argvars
, rettv
)
8421 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8423 char_u
*buttons
= NULL
;
8424 char_u buf
[NUMBUFLEN
];
8425 char_u buf2
[NUMBUFLEN
];
8427 int type
= VIM_GENERIC
;
8431 message
= get_tv_string_chk(&argvars
[0]);
8432 if (message
== NULL
)
8434 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
8436 buttons
= get_tv_string_buf_chk(&argvars
[1], buf
);
8437 if (buttons
== NULL
)
8439 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8441 def
= get_tv_number_chk(&argvars
[2], &error
);
8442 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
8444 typestr
= get_tv_string_buf_chk(&argvars
[3], buf2
);
8445 if (typestr
== NULL
)
8449 switch (TOUPPER_ASC(*typestr
))
8451 case 'E': type
= VIM_ERROR
; break;
8452 case 'Q': type
= VIM_QUESTION
; break;
8453 case 'I': type
= VIM_INFO
; break;
8454 case 'W': type
= VIM_WARNING
; break;
8455 case 'G': type
= VIM_GENERIC
; break;
8462 if (buttons
== NULL
|| *buttons
== NUL
)
8463 buttons
= (char_u
*)_("&Ok");
8466 rettv
->vval
.v_number
= 0;
8468 rettv
->vval
.v_number
= do_dialog(type
, NULL
, message
, buttons
,
8471 rettv
->vval
.v_number
= 0;
8479 f_copy(argvars
, rettv
)
8483 item_copy(&argvars
[0], rettv
, FALSE
, 0);
8487 * "count()" function
8490 f_count(argvars
, rettv
)
8497 if (argvars
[0].v_type
== VAR_LIST
)
8503 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
8506 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8510 ic
= get_tv_number_chk(&argvars
[2], &error
);
8511 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
8513 idx
= get_tv_number_chk(&argvars
[3], &error
);
8516 li
= list_find(l
, idx
);
8518 EMSGN(_(e_listidx
), idx
);
8525 for ( ; li
!= NULL
; li
= li
->li_next
)
8526 if (tv_equal(&li
->li_tv
, &argvars
[1], ic
))
8530 else if (argvars
[0].v_type
== VAR_DICT
)
8536 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
8540 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8542 ic
= get_tv_number_chk(&argvars
[2], &error
);
8543 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
8547 todo
= error
? 0 : (int)d
->dv_hashtab
.ht_used
;
8548 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
8550 if (!HASHITEM_EMPTY(hi
))
8553 if (tv_equal(&HI2DI(hi
)->di_tv
, &argvars
[1], ic
))
8560 EMSG2(_(e_listdictarg
), "count()");
8561 rettv
->vval
.v_number
= n
;
8565 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8567 * Checks the existence of a cscope connection.
8571 f_cscope_connection(argvars
, rettv
)
8577 char_u
*dbpath
= NULL
;
8578 char_u
*prepend
= NULL
;
8579 char_u buf
[NUMBUFLEN
];
8581 if (argvars
[0].v_type
!= VAR_UNKNOWN
8582 && argvars
[1].v_type
!= VAR_UNKNOWN
)
8584 num
= (int)get_tv_number(&argvars
[0]);
8585 dbpath
= get_tv_string(&argvars
[1]);
8586 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8587 prepend
= get_tv_string_buf(&argvars
[2], buf
);
8590 rettv
->vval
.v_number
= cs_connection(num
, dbpath
, prepend
);
8592 rettv
->vval
.v_number
= 0;
8597 * "cursor(lnum, col)" function
8599 * Moves the cursor to the specified line and column
8603 f_cursor(argvars
, rettv
)
8608 #ifdef FEAT_VIRTUALEDIT
8612 if (argvars
[1].v_type
== VAR_UNKNOWN
)
8616 if (list2fpos(argvars
, &pos
, NULL
) == FAIL
)
8620 #ifdef FEAT_VIRTUALEDIT
8621 coladd
= pos
.coladd
;
8626 line
= get_tv_lnum(argvars
);
8627 col
= get_tv_number_chk(&argvars
[1], NULL
);
8628 #ifdef FEAT_VIRTUALEDIT
8629 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8630 coladd
= get_tv_number_chk(&argvars
[2], NULL
);
8633 if (line
< 0 || col
< 0
8634 #ifdef FEAT_VIRTUALEDIT
8638 return; /* type error; errmsg already given */
8640 curwin
->w_cursor
.lnum
= line
;
8642 curwin
->w_cursor
.col
= col
- 1;
8643 #ifdef FEAT_VIRTUALEDIT
8644 curwin
->w_cursor
.coladd
= coladd
;
8647 /* Make sure the cursor is in a valid position. */
8650 /* Correct cursor for multi-byte character. */
8655 curwin
->w_set_curswant
= TRUE
;
8659 * "deepcopy()" function
8662 f_deepcopy(argvars
, rettv
)
8668 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
8669 noref
= get_tv_number_chk(&argvars
[1], NULL
);
8670 if (noref
< 0 || noref
> 1)
8673 item_copy(&argvars
[0], rettv
, TRUE
, noref
== 0 ? ++current_copyID
: 0);
8677 * "delete()" function
8680 f_delete(argvars
, rettv
)
8684 if (check_restricted() || check_secure())
8685 rettv
->vval
.v_number
= -1;
8687 rettv
->vval
.v_number
= mch_remove(get_tv_string(&argvars
[0]));
8691 * "did_filetype()" function
8695 f_did_filetype(argvars
, rettv
)
8700 rettv
->vval
.v_number
= did_filetype
;
8702 rettv
->vval
.v_number
= 0;
8707 * "diff_filler()" function
8711 f_diff_filler(argvars
, rettv
)
8716 rettv
->vval
.v_number
= diff_check_fill(curwin
, get_tv_lnum(argvars
));
8721 * "diff_hlID()" function
8725 f_diff_hlID(argvars
, rettv
)
8730 linenr_T lnum
= get_tv_lnum(argvars
);
8731 static linenr_T prev_lnum
= 0;
8732 static int changedtick
= 0;
8733 static int fnum
= 0;
8734 static int change_start
= 0;
8735 static int change_end
= 0;
8736 static hlf_T hlID
= (hlf_T
)0;
8740 if (lnum
< 0) /* ignore type error in {lnum} arg */
8742 if (lnum
!= prev_lnum
8743 || changedtick
!= curbuf
->b_changedtick
8744 || fnum
!= curbuf
->b_fnum
)
8746 /* New line, buffer, change: need to get the values. */
8747 filler_lines
= diff_check(curwin
, lnum
);
8748 if (filler_lines
< 0)
8750 if (filler_lines
== -1)
8752 change_start
= MAXCOL
;
8754 if (diff_find_change(curwin
, lnum
, &change_start
, &change_end
))
8755 hlID
= HLF_ADD
; /* added line */
8757 hlID
= HLF_CHD
; /* changed line */
8760 hlID
= HLF_ADD
; /* added line */
8765 changedtick
= curbuf
->b_changedtick
;
8766 fnum
= curbuf
->b_fnum
;
8769 if (hlID
== HLF_CHD
|| hlID
== HLF_TXD
)
8771 col
= get_tv_number(&argvars
[1]) - 1; /* ignore type error in {col} */
8772 if (col
>= change_start
&& col
<= change_end
)
8773 hlID
= HLF_TXD
; /* changed text */
8775 hlID
= HLF_CHD
; /* changed line */
8777 rettv
->vval
.v_number
= hlID
== (hlf_T
)0 ? 0 : (int)hlID
;
8782 * "empty({expr})" function
8785 f_empty(argvars
, rettv
)
8791 switch (argvars
[0].v_type
)
8795 n
= argvars
[0].vval
.v_string
== NULL
8796 || *argvars
[0].vval
.v_string
== NUL
;
8799 n
= argvars
[0].vval
.v_number
== 0;
8802 n
= argvars
[0].vval
.v_list
== NULL
8803 || argvars
[0].vval
.v_list
->lv_first
== NULL
;
8806 n
= argvars
[0].vval
.v_dict
== NULL
8807 || argvars
[0].vval
.v_dict
->dv_hashtab
.ht_used
== 0;
8810 EMSG2(_(e_intern2
), "f_empty()");
8814 rettv
->vval
.v_number
= n
;
8818 * "escape({string}, {chars})" function
8821 f_escape(argvars
, rettv
)
8825 char_u buf
[NUMBUFLEN
];
8827 rettv
->vval
.v_string
= vim_strsave_escaped(get_tv_string(&argvars
[0]),
8828 get_tv_string_buf(&argvars
[1], buf
));
8829 rettv
->v_type
= VAR_STRING
;
8837 f_eval(argvars
, rettv
)
8843 s
= get_tv_string_chk(&argvars
[0]);
8847 if (s
== NULL
|| eval1(&s
, rettv
, TRUE
) == FAIL
)
8849 rettv
->v_type
= VAR_NUMBER
;
8850 rettv
->vval
.v_number
= 0;
8853 EMSG(_(e_trailing
));
8857 * "eventhandler()" function
8861 f_eventhandler(argvars
, rettv
)
8865 rettv
->vval
.v_number
= vgetc_busy
;
8869 * "executable()" function
8872 f_executable(argvars
, rettv
)
8876 rettv
->vval
.v_number
= mch_can_exe(get_tv_string(&argvars
[0]));
8880 * "exists()" function
8883 f_exists(argvars
, rettv
)
8892 p
= get_tv_string(&argvars
[0]);
8893 if (*p
== '$') /* environment variable */
8895 /* first try "normal" environment variables (fast) */
8896 if (mch_getenv(p
+ 1) != NULL
)
8900 /* try expanding things like $VIM and ${HOME} */
8901 p
= expand_env_save(p
);
8902 if (p
!= NULL
&& *p
!= '$')
8907 else if (*p
== '&' || *p
== '+') /* option */
8909 n
= (get_option_tv(&p
, NULL
, TRUE
) == OK
);
8910 if (*skipwhite(p
) != NUL
)
8911 n
= FALSE
; /* trailing garbage */
8913 else if (*p
== '*') /* internal or user defined function */
8915 n
= function_exists(p
+ 1);
8919 n
= cmd_exists(p
+ 1);
8925 n
= autocmd_supported(p
+ 2);
8927 n
= au_exists(p
+ 1);
8930 else /* internal variable */
8935 /* get_name_len() takes care of expanding curly braces */
8937 len
= get_name_len(&p
, &tofree
, TRUE
, FALSE
);
8942 n
= (get_var_tv(name
, len
, &tv
, FALSE
) == OK
);
8945 /* handle d.key, l[idx], f(expr) */
8946 n
= (handle_subscript(&p
, &tv
, TRUE
, FALSE
) == OK
);
8957 rettv
->vval
.v_number
= n
;
8961 * "expand()" function
8964 f_expand(argvars
, rettv
)
8971 int flags
= WILD_SILENT
|WILD_USE_NL
|WILD_LIST_NOTFOUND
;
8975 rettv
->v_type
= VAR_STRING
;
8976 s
= get_tv_string(&argvars
[0]);
8977 if (*s
== '%' || *s
== '#' || *s
== '<')
8980 rettv
->vval
.v_string
= eval_vars(s
, s
, &len
, NULL
, &errormsg
, NULL
);
8985 /* When the optional second argument is non-zero, don't remove matches
8986 * for 'suffixes' and 'wildignore' */
8987 if (argvars
[1].v_type
!= VAR_UNKNOWN
8988 && get_tv_number_chk(&argvars
[1], &error
))
8989 flags
|= WILD_KEEP_ALL
;
8993 xpc
.xp_context
= EXPAND_FILES
;
8994 rettv
->vval
.v_string
= ExpandOne(&xpc
, s
, NULL
, flags
, WILD_ALL
);
8997 rettv
->vval
.v_string
= NULL
;
9002 * "extend(list, list [, idx])" function
9003 * "extend(dict, dict [, action])" function
9006 f_extend(argvars
, rettv
)
9010 rettv
->vval
.v_number
= 0;
9011 if (argvars
[0].v_type
== VAR_LIST
&& argvars
[1].v_type
== VAR_LIST
)
9018 l1
= argvars
[0].vval
.v_list
;
9019 l2
= argvars
[1].vval
.v_list
;
9020 if (l1
!= NULL
&& !tv_check_lock(l1
->lv_lock
, (char_u
*)"extend()")
9023 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9025 before
= get_tv_number_chk(&argvars
[2], &error
);
9027 return; /* type error; errmsg already given */
9029 if (before
== l1
->lv_len
)
9033 item
= list_find(l1
, before
);
9036 EMSGN(_(e_listidx
), before
);
9043 list_extend(l1
, l2
, item
);
9045 copy_tv(&argvars
[0], rettv
);
9048 else if (argvars
[0].v_type
== VAR_DICT
&& argvars
[1].v_type
== VAR_DICT
)
9057 d1
= argvars
[0].vval
.v_dict
;
9058 d2
= argvars
[1].vval
.v_dict
;
9059 if (d1
!= NULL
&& !tv_check_lock(d1
->dv_lock
, (char_u
*)"extend()")
9062 /* Check the third argument. */
9063 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9065 static char *(av
[]) = {"keep", "force", "error"};
9067 action
= get_tv_string_chk(&argvars
[2]);
9069 return; /* type error; errmsg already given */
9070 for (i
= 0; i
< 3; ++i
)
9071 if (STRCMP(action
, av
[i
]) == 0)
9075 EMSG2(_(e_invarg2
), action
);
9080 action
= (char_u
*)"force";
9082 /* Go over all entries in the second dict and add them to the
9084 todo
= (int)d2
->dv_hashtab
.ht_used
;
9085 for (hi2
= d2
->dv_hashtab
.ht_array
; todo
> 0; ++hi2
)
9087 if (!HASHITEM_EMPTY(hi2
))
9090 di1
= dict_find(d1
, hi2
->hi_key
, -1);
9093 di1
= dictitem_copy(HI2DI(hi2
));
9094 if (di1
!= NULL
&& dict_add(d1
, di1
) == FAIL
)
9097 else if (*action
== 'e')
9099 EMSG2(_("E737: Key already exists: %s"), hi2
->hi_key
);
9102 else if (*action
== 'f')
9104 clear_tv(&di1
->di_tv
);
9105 copy_tv(&HI2DI(hi2
)->di_tv
, &di1
->di_tv
);
9110 copy_tv(&argvars
[0], rettv
);
9114 EMSG2(_(e_listdictarg
), "extend()");
9118 * "feedkeys()" function
9122 f_feedkeys(argvars
, rettv
)
9127 char_u
*keys
, *flags
;
9128 char_u nbuf
[NUMBUFLEN
];
9132 /* This is not allowed in the sandbox. If the commands would still be
9133 * executed in the sandbox it would be OK, but it probably happens later,
9134 * when "sandbox" is no longer set. */
9138 rettv
->vval
.v_number
= 0;
9139 keys
= get_tv_string(&argvars
[0]);
9142 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9144 flags
= get_tv_string_buf(&argvars
[1], nbuf
);
9145 for ( ; *flags
!= NUL
; ++flags
)
9149 case 'n': remap
= FALSE
; break;
9150 case 'm': remap
= TRUE
; break;
9151 case 't': typed
= TRUE
; break;
9156 /* Need to escape K_SPECIAL and CSI before putting the string in the
9157 * typeahead buffer. */
9158 keys_esc
= vim_strsave_escape_csi(keys
);
9159 if (keys_esc
!= NULL
)
9161 ins_typebuf(keys_esc
, (remap
? REMAP_YES
: REMAP_NONE
),
9162 typebuf
.tb_len
, !typed
, FALSE
);
9165 typebuf_was_filled
= TRUE
;
9171 * "filereadable()" function
9174 f_filereadable(argvars
, rettv
)
9182 p
= get_tv_string(&argvars
[0]);
9183 if (*p
&& !mch_isdir(p
) && (fd
= mch_fopen((char *)p
, "r")) != NULL
)
9191 rettv
->vval
.v_number
= n
;
9195 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
9196 * rights to write into.
9199 f_filewritable(argvars
, rettv
)
9203 rettv
->vval
.v_number
= filewritable(get_tv_string(&argvars
[0]));
9206 static void findfilendir
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int dir
));
9209 findfilendir(argvars
, rettv
, dir
)
9214 #ifdef FEAT_SEARCHPATH
9216 char_u
*fresult
= NULL
;
9217 char_u
*path
= *curbuf
->b_p_path
== NUL
? p_path
: curbuf
->b_p_path
;
9219 char_u pathbuf
[NUMBUFLEN
];
9225 rettv
->vval
.v_string
= NULL
;
9226 rettv
->v_type
= VAR_STRING
;
9228 #ifdef FEAT_SEARCHPATH
9229 fname
= get_tv_string(&argvars
[0]);
9231 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9233 p
= get_tv_string_buf_chk(&argvars
[1], pathbuf
);
9241 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9242 count
= get_tv_number_chk(&argvars
[2], &error
);
9246 if (count
< 0 && rettv_list_alloc(rettv
) == FAIL
)
9249 if (*fname
!= NUL
&& !error
)
9253 if (rettv
->v_type
== VAR_STRING
)
9255 fresult
= find_file_in_path_option(first
? fname
: NULL
,
9256 first
? (int)STRLEN(fname
) : 0,
9257 0, first
, path
, dir
, curbuf
->b_ffname
,
9258 dir
? (char_u
*)"" : curbuf
->b_p_sua
);
9261 if (fresult
!= NULL
&& rettv
->v_type
== VAR_LIST
)
9262 list_append_string(rettv
->vval
.v_list
, fresult
, -1);
9264 } while ((rettv
->v_type
== VAR_LIST
|| --count
> 0) && fresult
!= NULL
);
9267 if (rettv
->v_type
== VAR_STRING
)
9268 rettv
->vval
.v_string
= fresult
;
9272 static void filter_map
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int map
));
9273 static int filter_map_one
__ARGS((typval_T
*tv
, char_u
*expr
, int map
, int *remp
));
9276 * Implementation of map() and filter().
9279 filter_map(argvars
, rettv
, map
)
9284 char_u buf
[NUMBUFLEN
];
9286 listitem_T
*li
, *nli
;
9296 char_u
*ermsg
= map
? (char_u
*)"map()" : (char_u
*)"filter()";
9299 rettv
->vval
.v_number
= 0;
9300 if (argvars
[0].v_type
== VAR_LIST
)
9302 if ((l
= argvars
[0].vval
.v_list
) == NULL
9303 || (map
&& tv_check_lock(l
->lv_lock
, ermsg
)))
9306 else if (argvars
[0].v_type
== VAR_DICT
)
9308 if ((d
= argvars
[0].vval
.v_dict
) == NULL
9309 || (map
&& tv_check_lock(d
->dv_lock
, ermsg
)))
9314 EMSG2(_(e_listdictarg
), ermsg
);
9318 expr
= get_tv_string_buf_chk(&argvars
[1], buf
);
9319 /* On type errors, the preceding call has already displayed an error
9320 * message. Avoid a misleading error message for an empty string that
9321 * was not passed as argument. */
9324 prepare_vimvar(VV_VAL
, &save_val
);
9325 expr
= skipwhite(expr
);
9327 /* We reset "did_emsg" to be able to detect whether an error
9328 * occurred during evaluation of the expression. */
9329 save_did_emsg
= did_emsg
;
9332 if (argvars
[0].v_type
== VAR_DICT
)
9334 prepare_vimvar(VV_KEY
, &save_key
);
9335 vimvars
[VV_KEY
].vv_type
= VAR_STRING
;
9337 ht
= &d
->dv_hashtab
;
9339 todo
= (int)ht
->ht_used
;
9340 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
9342 if (!HASHITEM_EMPTY(hi
))
9346 if (tv_check_lock(di
->di_tv
.v_lock
, ermsg
))
9348 vimvars
[VV_KEY
].vv_str
= vim_strsave(di
->di_key
);
9349 if (filter_map_one(&di
->di_tv
, expr
, map
, &rem
) == FAIL
9353 dictitem_remove(d
, di
);
9354 clear_tv(&vimvars
[VV_KEY
].vv_tv
);
9359 restore_vimvar(VV_KEY
, &save_key
);
9363 for (li
= l
->lv_first
; li
!= NULL
; li
= nli
)
9365 if (tv_check_lock(li
->li_tv
.v_lock
, ermsg
))
9368 if (filter_map_one(&li
->li_tv
, expr
, map
, &rem
) == FAIL
9372 listitem_remove(l
, li
);
9376 restore_vimvar(VV_VAL
, &save_val
);
9378 did_emsg
|= save_did_emsg
;
9381 copy_tv(&argvars
[0], rettv
);
9385 filter_map_one(tv
, expr
, map
, remp
)
9395 copy_tv(tv
, &vimvars
[VV_VAL
].vv_tv
);
9397 if (eval1(&s
, &rettv
, TRUE
) == FAIL
)
9399 if (*s
!= NUL
) /* check for trailing chars after expr */
9401 EMSG2(_(e_invexpr2
), s
);
9406 /* map(): replace the list item value */
9415 /* filter(): when expr is zero remove the item */
9416 *remp
= (get_tv_number_chk(&rettv
, &error
) == 0);
9418 /* On type error, nothing has been removed; return FAIL to stop the
9419 * loop. The error message was given by get_tv_number_chk(). */
9425 clear_tv(&vimvars
[VV_VAL
].vv_tv
);
9430 * "filter()" function
9433 f_filter(argvars
, rettv
)
9437 filter_map(argvars
, rettv
, FALSE
);
9441 * "finddir({fname}[, {path}[, {count}]])" function
9444 f_finddir(argvars
, rettv
)
9448 findfilendir(argvars
, rettv
, TRUE
);
9452 * "findfile({fname}[, {path}[, {count}]])" function
9455 f_findfile(argvars
, rettv
)
9459 findfilendir(argvars
, rettv
, FALSE
);
9463 * "fnamemodify({fname}, {mods})" function
9466 f_fnamemodify(argvars
, rettv
)
9474 char_u
*fbuf
= NULL
;
9475 char_u buf
[NUMBUFLEN
];
9477 fname
= get_tv_string_chk(&argvars
[0]);
9478 mods
= get_tv_string_buf_chk(&argvars
[1], buf
);
9479 if (fname
== NULL
|| mods
== NULL
)
9483 len
= (int)STRLEN(fname
);
9484 (void)modify_fname(mods
, &usedlen
, &fname
, &fbuf
, &len
);
9487 rettv
->v_type
= VAR_STRING
;
9489 rettv
->vval
.v_string
= NULL
;
9491 rettv
->vval
.v_string
= vim_strnsave(fname
, len
);
9495 static void foldclosed_both
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int end
));
9498 * "foldclosed()" function
9501 foldclosed_both(argvars
, rettv
, end
)
9508 linenr_T first
, last
;
9510 lnum
= get_tv_lnum(argvars
);
9511 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
9513 if (hasFoldingWin(curwin
, lnum
, &first
, &last
, FALSE
, NULL
))
9516 rettv
->vval
.v_number
= (varnumber_T
)last
;
9518 rettv
->vval
.v_number
= (varnumber_T
)first
;
9523 rettv
->vval
.v_number
= -1;
9527 * "foldclosed()" function
9530 f_foldclosed(argvars
, rettv
)
9534 foldclosed_both(argvars
, rettv
, FALSE
);
9538 * "foldclosedend()" function
9541 f_foldclosedend(argvars
, rettv
)
9545 foldclosed_both(argvars
, rettv
, TRUE
);
9549 * "foldlevel()" function
9552 f_foldlevel(argvars
, rettv
)
9559 lnum
= get_tv_lnum(argvars
);
9560 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
9561 rettv
->vval
.v_number
= foldLevel(lnum
);
9564 rettv
->vval
.v_number
= 0;
9568 * "foldtext()" function
9572 f_foldtext(argvars
, rettv
)
9584 rettv
->v_type
= VAR_STRING
;
9585 rettv
->vval
.v_string
= NULL
;
9587 if ((linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
> 0
9588 && (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
9589 <= curbuf
->b_ml
.ml_line_count
9590 && vimvars
[VV_FOLDDASHES
].vv_str
!= NULL
)
9592 /* Find first non-empty line in the fold. */
9593 lnum
= (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
;
9594 while (lnum
< (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
9596 if (!linewhite(lnum
))
9601 /* Find interesting text in this line. */
9602 s
= skipwhite(ml_get(lnum
));
9603 /* skip C comment-start */
9604 if (s
[0] == '/' && (s
[1] == '*' || s
[1] == '/'))
9606 s
= skipwhite(s
+ 2);
9607 if (*skipwhite(s
) == NUL
9608 && lnum
+ 1 < (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
9610 s
= skipwhite(ml_get(lnum
+ 1));
9612 s
= skipwhite(s
+ 1);
9615 txt
= _("+-%s%3ld lines: ");
9616 r
= alloc((unsigned)(STRLEN(txt
)
9617 + STRLEN(vimvars
[VV_FOLDDASHES
].vv_str
) /* for %s */
9619 + STRLEN(s
))); /* concatenated */
9622 sprintf((char *)r
, txt
, vimvars
[VV_FOLDDASHES
].vv_str
,
9623 (long)((linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
9624 - (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
+ 1));
9625 len
= (int)STRLEN(r
);
9627 /* remove 'foldmarker' and 'commentstring' */
9628 foldtext_cleanup(r
+ len
);
9629 rettv
->vval
.v_string
= r
;
9636 * "foldtextresult(lnum)" function
9640 f_foldtextresult(argvars
, rettv
)
9648 foldinfo_T foldinfo
;
9652 rettv
->v_type
= VAR_STRING
;
9653 rettv
->vval
.v_string
= NULL
;
9655 lnum
= get_tv_lnum(argvars
);
9656 /* treat illegal types and illegal string values for {lnum} the same */
9659 fold_count
= foldedCount(curwin
, lnum
, &foldinfo
);
9662 text
= get_foldtext(curwin
, lnum
, lnum
+ fold_count
- 1,
9665 text
= vim_strsave(text
);
9666 rettv
->vval
.v_string
= text
;
9672 * "foreground()" function
9676 f_foreground(argvars
, rettv
)
9680 rettv
->vval
.v_number
= 0;
9683 gui_mch_set_foreground();
9686 win32_set_foreground();
9692 * "function()" function
9696 f_function(argvars
, rettv
)
9702 rettv
->vval
.v_number
= 0;
9703 s
= get_tv_string(&argvars
[0]);
9704 if (s
== NULL
|| *s
== NUL
|| VIM_ISDIGIT(*s
))
9705 EMSG2(_(e_invarg2
), s
);
9706 else if (!function_exists(s
))
9707 EMSG2(_("E700: Unknown function: %s"), s
);
9710 rettv
->vval
.v_string
= vim_strsave(s
);
9711 rettv
->v_type
= VAR_FUNC
;
9716 * "garbagecollect()" function
9720 f_garbagecollect(argvars
, rettv
)
9724 /* This is postponed until we are back at the toplevel, because we may be
9725 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9726 want_garbage_collect
= TRUE
;
9728 if (argvars
[0].v_type
!= VAR_UNKNOWN
&& get_tv_number(&argvars
[0]) == 1)
9729 garbage_collect_at_exit
= TRUE
;
9736 f_get(argvars
, rettv
)
9744 typval_T
*tv
= NULL
;
9746 if (argvars
[0].v_type
== VAR_LIST
)
9748 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
9752 li
= list_find(l
, get_tv_number_chk(&argvars
[1], &error
));
9753 if (!error
&& li
!= NULL
)
9757 else if (argvars
[0].v_type
== VAR_DICT
)
9759 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
9761 di
= dict_find(d
, get_tv_string(&argvars
[1]), -1);
9767 EMSG2(_(e_listdictarg
), "get()");
9771 if (argvars
[2].v_type
== VAR_UNKNOWN
)
9772 rettv
->vval
.v_number
= 0;
9774 copy_tv(&argvars
[2], rettv
);
9780 static void get_buffer_lines
__ARGS((buf_T
*buf
, linenr_T start
, linenr_T end
, int retlist
, typval_T
*rettv
));
9783 * Get line or list of lines from buffer "buf" into "rettv".
9784 * Return a range (from start to end) of lines in rettv from the specified
9786 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
9789 get_buffer_lines(buf
, start
, end
, retlist
, rettv
)
9800 if (rettv_list_alloc(rettv
) == FAIL
)
9804 rettv
->vval
.v_number
= 0;
9806 if (buf
== NULL
|| buf
->b_ml
.ml_mfp
== NULL
|| start
< 0)
9811 if (start
>= 1 && start
<= buf
->b_ml
.ml_line_count
)
9812 p
= ml_get_buf(buf
, start
, FALSE
);
9816 rettv
->v_type
= VAR_STRING
;
9817 rettv
->vval
.v_string
= vim_strsave(p
);
9826 if (end
> buf
->b_ml
.ml_line_count
)
9827 end
= buf
->b_ml
.ml_line_count
;
9828 while (start
<= end
)
9829 if (list_append_string(rettv
->vval
.v_list
,
9830 ml_get_buf(buf
, start
++, FALSE
), -1) == FAIL
)
9836 * "getbufline()" function
9839 f_getbufline(argvars
, rettv
)
9847 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
9849 buf
= get_buf_tv(&argvars
[0]);
9852 lnum
= get_tv_lnum_buf(&argvars
[1], buf
);
9853 if (argvars
[2].v_type
== VAR_UNKNOWN
)
9856 end
= get_tv_lnum_buf(&argvars
[2], buf
);
9858 get_buffer_lines(buf
, lnum
, end
, TRUE
, rettv
);
9862 * "getbufvar()" function
9865 f_getbufvar(argvars
, rettv
)
9874 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
9875 varname
= get_tv_string_chk(&argvars
[1]);
9877 buf
= get_buf_tv(&argvars
[0]);
9879 rettv
->v_type
= VAR_STRING
;
9880 rettv
->vval
.v_string
= NULL
;
9882 if (buf
!= NULL
&& varname
!= NULL
)
9884 if (*varname
== '&') /* buffer-local-option */
9886 /* set curbuf to be our buf, temporarily */
9887 save_curbuf
= curbuf
;
9890 get_option_tv(&varname
, rettv
, TRUE
);
9892 /* restore previous notion of curbuf */
9893 curbuf
= save_curbuf
;
9897 if (*varname
== NUL
)
9898 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9899 * scope prefix before the NUL byte is required by
9900 * find_var_in_ht(). */
9901 varname
= (char_u
*)"b:" + 2;
9902 /* look up the variable */
9903 v
= find_var_in_ht(&buf
->b_vars
.dv_hashtab
, varname
, FALSE
);
9905 copy_tv(&v
->di_tv
, rettv
);
9913 * "getchar()" function
9916 f_getchar(argvars
, rettv
)
9923 /* Position the cursor. Needed after a message that ends in a space. */
9924 windgoto(msg_row
, msg_col
);
9930 if (argvars
[0].v_type
== VAR_UNKNOWN
)
9931 /* getchar(): blocking wait. */
9933 else if (get_tv_number_chk(&argvars
[0], &error
) == 1)
9934 /* getchar(1): only check if char avail */
9936 else if (error
|| vpeekc() == NUL
)
9937 /* illegal argument or getchar(0) and no char avail: return zero */
9940 /* getchar(0) and char avail: return char */
9949 vimvars
[VV_MOUSE_WIN
].vv_nr
= 0;
9950 vimvars
[VV_MOUSE_LNUM
].vv_nr
= 0;
9951 vimvars
[VV_MOUSE_COL
].vv_nr
= 0;
9953 rettv
->vval
.v_number
= n
;
9954 if (IS_SPECIAL(n
) || mod_mask
!= 0)
9956 char_u temp
[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9959 /* Turn a special key into three bytes, plus modifier. */
9962 temp
[i
++] = K_SPECIAL
;
9963 temp
[i
++] = KS_MODIFIER
;
9964 temp
[i
++] = mod_mask
;
9968 temp
[i
++] = K_SPECIAL
;
9969 temp
[i
++] = K_SECOND(n
);
9970 temp
[i
++] = K_THIRD(n
);
9974 i
+= (*mb_char2bytes
)(n
, temp
+ i
);
9979 rettv
->v_type
= VAR_STRING
;
9980 rettv
->vval
.v_string
= vim_strsave(temp
);
9983 if (n
== K_LEFTMOUSE
9984 || n
== K_LEFTMOUSE_NM
9986 || n
== K_LEFTRELEASE
9987 || n
== K_LEFTRELEASE_NM
9988 || n
== K_MIDDLEMOUSE
9989 || n
== K_MIDDLEDRAG
9990 || n
== K_MIDDLERELEASE
9991 || n
== K_RIGHTMOUSE
9993 || n
== K_RIGHTRELEASE
10000 || n
== K_MOUSEDOWN
10003 int row
= mouse_row
;
10004 int col
= mouse_col
;
10007 # ifdef FEAT_WINDOWS
10012 if (row
>= 0 && col
>= 0)
10014 /* Find the window at the mouse coordinates and compute the
10015 * text position. */
10016 win
= mouse_find_win(&row
, &col
);
10017 (void)mouse_comp_pos(win
, &row
, &col
, &lnum
);
10018 # ifdef FEAT_WINDOWS
10019 for (wp
= firstwin
; wp
!= win
; wp
= wp
->w_next
)
10022 vimvars
[VV_MOUSE_WIN
].vv_nr
= n
;
10023 vimvars
[VV_MOUSE_LNUM
].vv_nr
= lnum
;
10024 vimvars
[VV_MOUSE_COL
].vv_nr
= col
+ 1;
10032 * "getcharmod()" function
10036 f_getcharmod(argvars
, rettv
)
10040 rettv
->vval
.v_number
= mod_mask
;
10044 * "getcmdline()" function
10048 f_getcmdline(argvars
, rettv
)
10052 rettv
->v_type
= VAR_STRING
;
10053 rettv
->vval
.v_string
= get_cmdline_str();
10057 * "getcmdpos()" function
10061 f_getcmdpos(argvars
, rettv
)
10065 rettv
->vval
.v_number
= get_cmdline_pos() + 1;
10069 * "getcmdtype()" function
10073 f_getcmdtype(argvars
, rettv
)
10077 rettv
->v_type
= VAR_STRING
;
10078 rettv
->vval
.v_string
= alloc(2);
10079 if (rettv
->vval
.v_string
!= NULL
)
10081 rettv
->vval
.v_string
[0] = get_cmdline_type();
10082 rettv
->vval
.v_string
[1] = NUL
;
10087 * "getcwd()" function
10091 f_getcwd(argvars
, rettv
)
10095 char_u cwd
[MAXPATHL
];
10097 rettv
->v_type
= VAR_STRING
;
10098 if (mch_dirname(cwd
, MAXPATHL
) == FAIL
)
10099 rettv
->vval
.v_string
= NULL
;
10102 rettv
->vval
.v_string
= vim_strsave(cwd
);
10103 #ifdef BACKSLASH_IN_FILENAME
10104 if (rettv
->vval
.v_string
!= NULL
)
10105 slash_adjust(rettv
->vval
.v_string
);
10111 * "getfontname()" function
10115 f_getfontname(argvars
, rettv
)
10119 rettv
->v_type
= VAR_STRING
;
10120 rettv
->vval
.v_string
= NULL
;
10125 char_u
*name
= NULL
;
10127 if (argvars
[0].v_type
== VAR_UNKNOWN
)
10129 /* Get the "Normal" font. Either the name saved by
10130 * hl_set_font_name() or from the font ID. */
10131 font
= gui
.norm_font
;
10132 name
= hl_get_font_name();
10136 name
= get_tv_string(&argvars
[0]);
10137 if (STRCMP(name
, "*") == 0) /* don't use font dialog */
10139 font
= gui_mch_get_font(name
, FALSE
);
10140 if (font
== NOFONT
)
10141 return; /* Invalid font name, return empty string. */
10143 rettv
->vval
.v_string
= gui_mch_get_fontname(font
, name
);
10144 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
10145 gui_mch_free_font(font
);
10151 * "getfperm({fname})" function
10154 f_getfperm(argvars
, rettv
)
10160 char_u
*perm
= NULL
;
10161 char_u flags
[] = "rwx";
10164 fname
= get_tv_string(&argvars
[0]);
10166 rettv
->v_type
= VAR_STRING
;
10167 if (mch_stat((char *)fname
, &st
) >= 0)
10169 perm
= vim_strsave((char_u
*)"---------");
10172 for (i
= 0; i
< 9; i
++)
10174 if (st
.st_mode
& (1 << (8 - i
)))
10175 perm
[i
] = flags
[i
% 3];
10179 rettv
->vval
.v_string
= perm
;
10183 * "getfsize({fname})" function
10186 f_getfsize(argvars
, rettv
)
10193 fname
= get_tv_string(&argvars
[0]);
10195 rettv
->v_type
= VAR_NUMBER
;
10197 if (mch_stat((char *)fname
, &st
) >= 0)
10199 if (mch_isdir(fname
))
10200 rettv
->vval
.v_number
= 0;
10203 rettv
->vval
.v_number
= (varnumber_T
)st
.st_size
;
10205 /* non-perfect check for overflow */
10206 if ((off_t
)rettv
->vval
.v_number
!= (off_t
)st
.st_size
)
10207 rettv
->vval
.v_number
= -2;
10211 rettv
->vval
.v_number
= -1;
10215 * "getftime({fname})" function
10218 f_getftime(argvars
, rettv
)
10225 fname
= get_tv_string(&argvars
[0]);
10227 if (mch_stat((char *)fname
, &st
) >= 0)
10228 rettv
->vval
.v_number
= (varnumber_T
)st
.st_mtime
;
10230 rettv
->vval
.v_number
= -1;
10234 * "getftype({fname})" function
10237 f_getftype(argvars
, rettv
)
10243 char_u
*type
= NULL
;
10246 fname
= get_tv_string(&argvars
[0]);
10248 rettv
->v_type
= VAR_STRING
;
10249 if (mch_lstat((char *)fname
, &st
) >= 0)
10252 if (S_ISREG(st
.st_mode
))
10254 else if (S_ISDIR(st
.st_mode
))
10257 else if (S_ISLNK(st
.st_mode
))
10261 else if (S_ISBLK(st
.st_mode
))
10265 else if (S_ISCHR(st
.st_mode
))
10269 else if (S_ISFIFO(st
.st_mode
))
10273 else if (S_ISSOCK(st
.st_mode
))
10280 switch (st
.st_mode
& S_IFMT
)
10282 case S_IFREG
: t
= "file"; break;
10283 case S_IFDIR
: t
= "dir"; break;
10285 case S_IFLNK
: t
= "link"; break;
10288 case S_IFBLK
: t
= "bdev"; break;
10291 case S_IFCHR
: t
= "cdev"; break;
10294 case S_IFIFO
: t
= "fifo"; break;
10297 case S_IFSOCK
: t
= "socket"; break;
10299 default: t
= "other";
10302 if (mch_isdir(fname
))
10308 type
= vim_strsave((char_u
*)t
);
10310 rettv
->vval
.v_string
= type
;
10314 * "getline(lnum, [end])" function
10317 f_getline(argvars
, rettv
)
10325 lnum
= get_tv_lnum(argvars
);
10326 if (argvars
[1].v_type
== VAR_UNKNOWN
)
10333 end
= get_tv_lnum(&argvars
[1]);
10337 get_buffer_lines(curbuf
, lnum
, end
, retlist
, rettv
);
10341 * "getmatches()" function
10345 f_getmatches(argvars
, rettv
)
10349 #ifdef FEAT_SEARCH_EXTRA
10351 matchitem_T
*cur
= curwin
->w_match_head
;
10353 rettv
->vval
.v_number
= 0;
10355 if (rettv_list_alloc(rettv
) == OK
)
10357 while (cur
!= NULL
)
10359 dict
= dict_alloc();
10362 dict_add_nr_str(dict
, "group", 0L, syn_id2name(cur
->hlg_id
));
10363 dict_add_nr_str(dict
, "pattern", 0L, cur
->pattern
);
10364 dict_add_nr_str(dict
, "priority", (long)cur
->priority
, NULL
);
10365 dict_add_nr_str(dict
, "id", (long)cur
->id
, NULL
);
10366 list_append_dict(rettv
->vval
.v_list
, dict
);
10374 * "getpos(string)" function
10377 f_getpos(argvars
, rettv
)
10385 if (rettv_list_alloc(rettv
) == OK
)
10387 l
= rettv
->vval
.v_list
;
10388 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
10390 list_append_number(l
, (varnumber_T
)fnum
);
10392 list_append_number(l
, (varnumber_T
)0);
10393 list_append_number(l
, (fp
!= NULL
) ? (varnumber_T
)fp
->lnum
10395 list_append_number(l
, (fp
!= NULL
)
10396 ? (varnumber_T
)(fp
->col
== MAXCOL
? MAXCOL
: fp
->col
+ 1)
10398 list_append_number(l
,
10399 #ifdef FEAT_VIRTUALEDIT
10400 (fp
!= NULL
) ? (varnumber_T
)fp
->coladd
:
10405 rettv
->vval
.v_number
= FALSE
;
10409 * "getqflist()" and "getloclist()" functions
10413 f_getqflist(argvars
, rettv
)
10417 #ifdef FEAT_QUICKFIX
10421 rettv
->vval
.v_number
= 0;
10422 #ifdef FEAT_QUICKFIX
10423 if (rettv_list_alloc(rettv
) == OK
)
10426 if (argvars
[0].v_type
!= VAR_UNKNOWN
) /* getloclist() */
10428 wp
= find_win_by_nr(&argvars
[0], NULL
);
10433 (void)get_errorlist(wp
, rettv
->vval
.v_list
);
10439 * "getreg()" function
10442 f_getreg(argvars
, rettv
)
10446 char_u
*strregname
;
10451 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
10453 strregname
= get_tv_string_chk(&argvars
[0]);
10454 error
= strregname
== NULL
;
10455 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
10456 arg2
= get_tv_number_chk(&argvars
[1], &error
);
10459 strregname
= vimvars
[VV_REG
].vv_str
;
10460 regname
= (strregname
== NULL
? '"' : *strregname
);
10464 rettv
->v_type
= VAR_STRING
;
10465 rettv
->vval
.v_string
= error
? NULL
:
10466 get_reg_contents(regname
, TRUE
, arg2
);
10470 * "getregtype()" function
10473 f_getregtype(argvars
, rettv
)
10477 char_u
*strregname
;
10479 char_u buf
[NUMBUFLEN
+ 2];
10482 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
10484 strregname
= get_tv_string_chk(&argvars
[0]);
10485 if (strregname
== NULL
) /* type error; errmsg already given */
10487 rettv
->v_type
= VAR_STRING
;
10488 rettv
->vval
.v_string
= NULL
;
10493 /* Default to v:register */
10494 strregname
= vimvars
[VV_REG
].vv_str
;
10496 regname
= (strregname
== NULL
? '"' : *strregname
);
10502 switch (get_reg_type(regname
, ®len
))
10504 case MLINE
: buf
[0] = 'V'; break;
10505 case MCHAR
: buf
[0] = 'v'; break;
10509 sprintf((char *)buf
+ 1, "%ld", reglen
+ 1);
10513 rettv
->v_type
= VAR_STRING
;
10514 rettv
->vval
.v_string
= vim_strsave(buf
);
10518 * "gettabwinvar()" function
10521 f_gettabwinvar(argvars
, rettv
)
10525 getwinvar(argvars
, rettv
, 1);
10529 * "getwinposx()" function
10533 f_getwinposx(argvars
, rettv
)
10537 rettv
->vval
.v_number
= -1;
10543 if (gui_mch_get_winpos(&x
, &y
) == OK
)
10544 rettv
->vval
.v_number
= x
;
10550 * "getwinposy()" function
10554 f_getwinposy(argvars
, rettv
)
10558 rettv
->vval
.v_number
= -1;
10564 if (gui_mch_get_winpos(&x
, &y
) == OK
)
10565 rettv
->vval
.v_number
= y
;
10571 * Find window specifed by "vp" in tabpage "tp".
10574 find_win_by_nr(vp
, tp
)
10576 tabpage_T
*tp
; /* NULL for current tab page */
10578 #ifdef FEAT_WINDOWS
10583 nr
= get_tv_number_chk(vp
, NULL
);
10585 #ifdef FEAT_WINDOWS
10591 for (wp
= (tp
== NULL
|| tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
10592 wp
!= NULL
; wp
= wp
->w_next
)
10597 if (nr
== 0 || nr
== 1)
10604 * "getwinvar()" function
10607 f_getwinvar(argvars
, rettv
)
10611 getwinvar(argvars
, rettv
, 0);
10615 * getwinvar() and gettabwinvar()
10618 getwinvar(argvars
, rettv
, off
)
10621 int off
; /* 1 for gettabwinvar() */
10623 win_T
*win
, *oldcurwin
;
10628 #ifdef FEAT_WINDOWS
10630 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
10634 win
= find_win_by_nr(&argvars
[off
], tp
);
10635 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
10638 rettv
->v_type
= VAR_STRING
;
10639 rettv
->vval
.v_string
= NULL
;
10641 if (win
!= NULL
&& varname
!= NULL
)
10643 /* Set curwin to be our win, temporarily. Also set curbuf, so
10644 * that we can get buffer-local options. */
10645 oldcurwin
= curwin
;
10647 curbuf
= win
->w_buffer
;
10649 if (*varname
== '&') /* window-local-option */
10650 get_option_tv(&varname
, rettv
, 1);
10653 if (*varname
== NUL
)
10654 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10655 * scope prefix before the NUL byte is required by
10656 * find_var_in_ht(). */
10657 varname
= (char_u
*)"w:" + 2;
10658 /* look up the variable */
10659 v
= find_var_in_ht(&win
->w_vars
.dv_hashtab
, varname
, FALSE
);
10661 copy_tv(&v
->di_tv
, rettv
);
10664 /* restore previous notion of curwin */
10665 curwin
= oldcurwin
;
10666 curbuf
= curwin
->w_buffer
;
10673 * "glob()" function
10676 f_glob(argvars
, rettv
)
10683 xpc
.xp_context
= EXPAND_FILES
;
10684 rettv
->v_type
= VAR_STRING
;
10685 rettv
->vval
.v_string
= ExpandOne(&xpc
, get_tv_string(&argvars
[0]),
10686 NULL
, WILD_USE_NL
|WILD_SILENT
, WILD_ALL
);
10690 * "globpath()" function
10693 f_globpath(argvars
, rettv
)
10697 char_u buf1
[NUMBUFLEN
];
10698 char_u
*file
= get_tv_string_buf_chk(&argvars
[1], buf1
);
10700 rettv
->v_type
= VAR_STRING
;
10702 rettv
->vval
.v_string
= NULL
;
10704 rettv
->vval
.v_string
= globpath(get_tv_string(&argvars
[0]), file
);
10711 f_has(argvars
, rettv
)
10718 static char *(has_list
[]) =
10739 #if defined(MACOS_X_UNIX)
10763 #if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10772 #ifndef CASE_INSENSITIVE_FILENAME
10778 #ifdef FEAT_AUTOCMD
10783 # ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10784 "balloon_multiline",
10787 #if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10789 # ifdef ALL_BUILTIN_TCAPS
10790 "all_builtin_terms",
10793 #ifdef FEAT_BYTEOFF
10796 #ifdef FEAT_CINDENT
10799 #ifdef FEAT_CLIENTSERVER
10802 #ifdef FEAT_CLIPBOARD
10805 #ifdef FEAT_CMDL_COMPL
10808 #ifdef FEAT_CMDHIST
10811 #ifdef FEAT_COMMENTS
10820 #ifdef CURSOR_SHAPE
10826 #ifdef FEAT_CON_DIALOG
10829 #ifdef FEAT_GUI_DIALOG
10835 #ifdef FEAT_DIGRAPHS
10841 #ifdef FEAT_EMACS_TAGS
10844 "eval", /* always present, of course! */
10845 #ifdef FEAT_EX_EXTRA
10848 #ifdef FEAT_SEARCH_EXTRA
10854 #ifdef FEAT_SEARCHPATH
10857 #if defined(UNIX) && !defined(USE_SYSTEM)
10860 #ifdef FEAT_FIND_ID
10863 #ifdef FEAT_FOLDING
10869 #if !defined(USE_SYSTEM) && defined(UNIX)
10872 #ifdef FEAT_GETTEXT
10878 #ifdef FEAT_GUI_ATHENA
10879 # ifdef FEAT_GUI_NEXTAW
10885 #ifdef FEAT_GUI_GTK
10891 #ifdef FEAT_GUI_GNOME
10894 #ifdef FEAT_GUI_MAC
10897 #ifdef FEAT_GUI_MOTIF
10900 #ifdef FEAT_GUI_PHOTON
10903 #ifdef FEAT_GUI_W16
10906 #ifdef FEAT_GUI_W32
10909 #ifdef FEAT_HANGULIN
10912 #if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10915 #ifdef FEAT_INS_EXPAND
10918 #ifdef FEAT_JUMPLIST
10924 #ifdef FEAT_LANGMAP
10927 #ifdef FEAT_LIBCALL
10930 #ifdef FEAT_LINEBREAK
10936 #ifdef FEAT_LISTCMDS
10939 #ifdef FEAT_LOCALMAP
10945 #ifdef FEAT_SESSION
10948 #ifdef FEAT_MODIFY_FNAME
10954 #ifdef FEAT_MOUSESHAPE
10957 #if defined(UNIX) || defined(VMS)
10958 # ifdef FEAT_MOUSE_DEC
10961 # ifdef FEAT_MOUSE_GPM
10964 # ifdef FEAT_MOUSE_JSB
10967 # ifdef FEAT_MOUSE_NET
10970 # ifdef FEAT_MOUSE_PTERM
10973 # ifdef FEAT_MOUSE_XTERM
10980 #ifdef FEAT_MBYTE_IME
10983 #ifdef FEAT_MULTI_LANG
10986 #ifdef FEAT_MZSCHEME
10987 #ifndef DYNAMIC_MZSCHEME
10994 #ifdef FEAT_OSFILETYPE
10997 #ifdef FEAT_PATH_EXTRA
11001 #ifndef DYNAMIC_PERL
11006 #ifndef DYNAMIC_PYTHON
11010 #ifdef FEAT_POSTSCRIPT
11013 #ifdef FEAT_PRINTER
11016 #ifdef FEAT_PROFILE
11019 #ifdef FEAT_RELTIME
11022 #ifdef FEAT_QUICKFIX
11025 #ifdef FEAT_RIGHTLEFT
11028 #if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11031 #ifdef FEAT_SCROLLBIND
11034 #ifdef FEAT_CMDL_INFO
11041 #ifdef FEAT_SMARTINDENT
11047 #ifdef FEAT_STL_OPT
11050 #ifdef FEAT_SUN_WORKSHOP
11053 #ifdef FEAT_NETBEANS_INTG
11062 #if defined(USE_SYSTEM) || !defined(UNIX)
11065 #ifdef FEAT_TAG_BINS
11068 #ifdef FEAT_TAG_OLDSTATIC
11071 #ifdef FEAT_TAG_ANYWHITE
11075 # ifndef DYNAMIC_TCL
11082 #ifdef FEAT_TERMRESPONSE
11085 #ifdef FEAT_TEXTOBJ
11088 #ifdef HAVE_TGETENT
11094 #ifdef FEAT_TOOLBAR
11097 #ifdef FEAT_USR_CMDS
11098 "user-commands", /* was accidentally included in 5.4 */
11101 #ifdef FEAT_VIMINFO
11104 #ifdef FEAT_VERTSPLIT
11107 #ifdef FEAT_VIRTUALEDIT
11113 #ifdef FEAT_VISUALEXTRA
11116 #ifdef FEAT_VREPLACE
11119 #ifdef FEAT_WILDIGN
11122 #ifdef FEAT_WILDMENU
11125 #ifdef FEAT_WINDOWS
11131 #ifdef FEAT_WRITEBACKUP
11137 #ifdef FEAT_XFONTSET
11143 #ifdef USE_XSMP_INTERACT
11146 #ifdef FEAT_XCLIPBOARD
11149 #ifdef FEAT_XTERM_SAVE
11152 #if defined(UNIX) && defined(FEAT_X11)
11158 name
= get_tv_string(&argvars
[0]);
11159 for (i
= 0; has_list
[i
] != NULL
; ++i
)
11160 if (STRICMP(name
, has_list
[i
]) == 0)
11168 if (STRNICMP(name
, "patch", 5) == 0)
11169 n
= has_patch(atoi((char *)name
+ 5));
11170 else if (STRICMP(name
, "vim_starting") == 0)
11171 n
= (starting
!= 0);
11172 #if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11173 else if (STRICMP(name
, "balloon_multiline") == 0)
11174 n
= multiline_balloon_available();
11177 else if (STRICMP(name
, "tcl") == 0)
11178 n
= tcl_enabled(FALSE
);
11180 #if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11181 else if (STRICMP(name
, "iconv") == 0)
11182 n
= iconv_enabled(FALSE
);
11184 #ifdef DYNAMIC_MZSCHEME
11185 else if (STRICMP(name
, "mzscheme") == 0)
11186 n
= mzscheme_enabled(FALSE
);
11188 #ifdef DYNAMIC_RUBY
11189 else if (STRICMP(name
, "ruby") == 0)
11190 n
= ruby_enabled(FALSE
);
11192 #ifdef DYNAMIC_PYTHON
11193 else if (STRICMP(name
, "python") == 0)
11194 n
= python_enabled(FALSE
);
11196 #ifdef DYNAMIC_PERL
11197 else if (STRICMP(name
, "perl") == 0)
11198 n
= perl_enabled(FALSE
);
11201 else if (STRICMP(name
, "gui_running") == 0)
11202 n
= (gui
.in_use
|| gui
.starting
);
11203 # ifdef FEAT_GUI_W32
11204 else if (STRICMP(name
, "gui_win32s") == 0)
11205 n
= gui_is_win32s();
11207 # ifdef FEAT_BROWSE
11208 else if (STRICMP(name
, "browse") == 0)
11209 n
= gui
.in_use
; /* gui_mch_browse() works when GUI is running */
11213 else if (STRICMP(name
, "syntax_items") == 0)
11214 n
= syntax_present(curbuf
);
11216 #if defined(WIN3264)
11217 else if (STRICMP(name
, "win95") == 0)
11218 n
= mch_windows95();
11220 #ifdef FEAT_NETBEANS_INTG
11221 else if (STRICMP(name
, "netbeans_enabled") == 0)
11226 rettv
->vval
.v_number
= n
;
11230 * "has_key()" function
11233 f_has_key(argvars
, rettv
)
11237 rettv
->vval
.v_number
= 0;
11238 if (argvars
[0].v_type
!= VAR_DICT
)
11240 EMSG(_(e_dictreq
));
11243 if (argvars
[0].vval
.v_dict
== NULL
)
11246 rettv
->vval
.v_number
= dict_find(argvars
[0].vval
.v_dict
,
11247 get_tv_string(&argvars
[1]), -1) != NULL
;
11251 * "haslocaldir()" function
11255 f_haslocaldir(argvars
, rettv
)
11259 rettv
->vval
.v_number
= (curwin
->w_localdir
!= NULL
);
11263 * "hasmapto()" function
11266 f_hasmapto(argvars
, rettv
)
11272 char_u buf
[NUMBUFLEN
];
11275 name
= get_tv_string(&argvars
[0]);
11276 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11277 mode
= (char_u
*)"nvo";
11280 mode
= get_tv_string_buf(&argvars
[1], buf
);
11281 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
11282 abbr
= get_tv_number(&argvars
[2]);
11285 if (map_to_exists(name
, mode
, abbr
))
11286 rettv
->vval
.v_number
= TRUE
;
11288 rettv
->vval
.v_number
= FALSE
;
11292 * "histadd()" function
11296 f_histadd(argvars
, rettv
)
11300 #ifdef FEAT_CMDHIST
11303 char_u buf
[NUMBUFLEN
];
11306 rettv
->vval
.v_number
= FALSE
;
11307 if (check_restricted() || check_secure())
11309 #ifdef FEAT_CMDHIST
11310 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11311 histype
= str
!= NULL
? get_histtype(str
) : -1;
11314 str
= get_tv_string_buf(&argvars
[1], buf
);
11317 add_to_history(histype
, str
, FALSE
, NUL
);
11318 rettv
->vval
.v_number
= TRUE
;
11326 * "histdel()" function
11330 f_histdel(argvars
, rettv
)
11334 #ifdef FEAT_CMDHIST
11336 char_u buf
[NUMBUFLEN
];
11339 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11342 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
11343 /* only one argument: clear entire history */
11344 n
= clr_history(get_histtype(str
));
11345 else if (argvars
[1].v_type
== VAR_NUMBER
)
11346 /* index given: remove that entry */
11347 n
= del_history_idx(get_histtype(str
),
11348 (int)get_tv_number(&argvars
[1]));
11350 /* string given: remove all matching entries */
11351 n
= del_history_entry(get_histtype(str
),
11352 get_tv_string_buf(&argvars
[1], buf
));
11353 rettv
->vval
.v_number
= n
;
11355 rettv
->vval
.v_number
= 0;
11360 * "histget()" function
11364 f_histget(argvars
, rettv
)
11368 #ifdef FEAT_CMDHIST
11373 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11375 rettv
->vval
.v_string
= NULL
;
11378 type
= get_histtype(str
);
11379 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11380 idx
= get_history_idx(type
);
11382 idx
= (int)get_tv_number_chk(&argvars
[1], NULL
);
11383 /* -1 on type error */
11384 rettv
->vval
.v_string
= vim_strsave(get_history_entry(type
, idx
));
11387 rettv
->vval
.v_string
= NULL
;
11389 rettv
->v_type
= VAR_STRING
;
11393 * "histnr()" function
11397 f_histnr(argvars
, rettv
)
11403 #ifdef FEAT_CMDHIST
11404 char_u
*history
= get_tv_string_chk(&argvars
[0]);
11406 i
= history
== NULL
? HIST_CMD
- 1 : get_histtype(history
);
11407 if (i
>= HIST_CMD
&& i
< HIST_COUNT
)
11408 i
= get_history_idx(i
);
11412 rettv
->vval
.v_number
= i
;
11416 * "highlightID(name)" function
11419 f_hlID(argvars
, rettv
)
11423 rettv
->vval
.v_number
= syn_name2id(get_tv_string(&argvars
[0]));
11427 * "highlight_exists()" function
11430 f_hlexists(argvars
, rettv
)
11434 rettv
->vval
.v_number
= highlight_exists(get_tv_string(&argvars
[0]));
11438 * "hostname()" function
11442 f_hostname(argvars
, rettv
)
11446 char_u hostname
[256];
11448 mch_get_host_name(hostname
, 256);
11449 rettv
->v_type
= VAR_STRING
;
11450 rettv
->vval
.v_string
= vim_strsave(hostname
);
11458 f_iconv(argvars
, rettv
)
11463 char_u buf1
[NUMBUFLEN
];
11464 char_u buf2
[NUMBUFLEN
];
11465 char_u
*from
, *to
, *str
;
11469 rettv
->v_type
= VAR_STRING
;
11470 rettv
->vval
.v_string
= NULL
;
11473 str
= get_tv_string(&argvars
[0]);
11474 from
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[1], buf1
)));
11475 to
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[2], buf2
)));
11476 vimconv
.vc_type
= CONV_NONE
;
11477 convert_setup(&vimconv
, from
, to
);
11479 /* If the encodings are equal, no conversion needed. */
11480 if (vimconv
.vc_type
== CONV_NONE
)
11481 rettv
->vval
.v_string
= vim_strsave(str
);
11483 rettv
->vval
.v_string
= string_convert(&vimconv
, str
, NULL
);
11485 convert_setup(&vimconv
, NULL
, NULL
);
11492 * "indent()" function
11495 f_indent(argvars
, rettv
)
11501 lnum
= get_tv_lnum(argvars
);
11502 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
11503 rettv
->vval
.v_number
= get_indent_lnum(lnum
);
11505 rettv
->vval
.v_number
= -1;
11509 * "index()" function
11512 f_index(argvars
, rettv
)
11521 rettv
->vval
.v_number
= -1;
11522 if (argvars
[0].v_type
!= VAR_LIST
)
11524 EMSG(_(e_listreq
));
11527 l
= argvars
[0].vval
.v_list
;
11530 item
= l
->lv_first
;
11531 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
11535 /* Start at specified item. Use the cached index that list_find()
11536 * sets, so that a negative number also works. */
11537 item
= list_find(l
, get_tv_number_chk(&argvars
[2], &error
));
11539 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
11540 ic
= get_tv_number_chk(&argvars
[3], &error
);
11545 for ( ; item
!= NULL
; item
= item
->li_next
, ++idx
)
11546 if (tv_equal(&item
->li_tv
, &argvars
[1], ic
))
11548 rettv
->vval
.v_number
= idx
;
11554 static int inputsecret_flag
= 0;
11556 static void get_user_input
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int inputdialog
));
11559 * This function is used by f_input() and f_inputdialog() functions. The third
11560 * argument to f_input() specifies the type of completion to use at the
11561 * prompt. The third argument to f_inputdialog() specifies the value to return
11562 * when the user cancels the prompt.
11565 get_user_input(argvars
, rettv
, inputdialog
)
11570 char_u
*prompt
= get_tv_string_chk(&argvars
[0]);
11573 char_u buf
[NUMBUFLEN
];
11574 int cmd_silent_save
= cmd_silent
;
11575 char_u
*defstr
= (char_u
*)"";
11576 int xp_type
= EXPAND_NOTHING
;
11577 char_u
*xp_arg
= NULL
;
11579 rettv
->v_type
= VAR_STRING
;
11580 rettv
->vval
.v_string
= NULL
;
11582 #ifdef NO_CONSOLE_INPUT
11583 /* While starting up, there is no place to enter text. */
11584 if (no_console_input())
11588 cmd_silent
= FALSE
; /* Want to see the prompt. */
11589 if (prompt
!= NULL
)
11591 /* Only the part of the message after the last NL is considered as
11592 * prompt for the command line */
11593 p
= vim_strrchr(prompt
, '\n');
11603 msg_puts_attr(prompt
, echo_attr
);
11604 msg_didout
= FALSE
;
11608 cmdline_row
= msg_row
;
11610 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
11612 defstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
11613 if (defstr
!= NULL
)
11614 stuffReadbuffSpec(defstr
);
11616 if (!inputdialog
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
11622 rettv
->vval
.v_string
= NULL
;
11624 xp_name
= get_tv_string_buf_chk(&argvars
[2], buf
);
11625 if (xp_name
== NULL
)
11628 xp_namelen
= (int)STRLEN(xp_name
);
11630 if (parse_compl_arg(xp_name
, xp_namelen
, &xp_type
, &argt
,
11636 if (defstr
!= NULL
)
11637 rettv
->vval
.v_string
=
11638 getcmdline_prompt(inputsecret_flag
? NUL
: '@', p
, echo_attr
,
11643 /* since the user typed this, no need to wait for return */
11644 need_wait_return
= FALSE
;
11645 msg_didout
= FALSE
;
11647 cmd_silent
= cmd_silent_save
;
11651 * "input()" function
11652 * Also handles inputsecret() when inputsecret is set.
11655 f_input(argvars
, rettv
)
11659 get_user_input(argvars
, rettv
, FALSE
);
11663 * "inputdialog()" function
11666 f_inputdialog(argvars
, rettv
)
11670 #if defined(FEAT_GUI_TEXTDIALOG)
11671 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11672 if (gui
.in_use
&& vim_strchr(p_go
, GO_CONDIALOG
) == NULL
)
11675 char_u buf
[NUMBUFLEN
];
11676 char_u
*defstr
= (char_u
*)"";
11678 message
= get_tv_string_chk(&argvars
[0]);
11679 if (argvars
[1].v_type
!= VAR_UNKNOWN
11680 && (defstr
= get_tv_string_buf_chk(&argvars
[1], buf
)) != NULL
)
11681 vim_strncpy(IObuff
, defstr
, IOSIZE
- 1);
11684 if (message
!= NULL
&& defstr
!= NULL
11685 && do_dialog(VIM_QUESTION
, NULL
, message
,
11686 (char_u
*)_("&OK\n&Cancel"), 1, IObuff
) == 1)
11687 rettv
->vval
.v_string
= vim_strsave(IObuff
);
11690 if (message
!= NULL
&& defstr
!= NULL
11691 && argvars
[1].v_type
!= VAR_UNKNOWN
11692 && argvars
[2].v_type
!= VAR_UNKNOWN
)
11693 rettv
->vval
.v_string
= vim_strsave(
11694 get_tv_string_buf(&argvars
[2], buf
));
11696 rettv
->vval
.v_string
= NULL
;
11698 rettv
->v_type
= VAR_STRING
;
11702 get_user_input(argvars
, rettv
, TRUE
);
11706 * "inputlist()" function
11709 f_inputlist(argvars
, rettv
)
11717 rettv
->vval
.v_number
= 0;
11718 #ifdef NO_CONSOLE_INPUT
11719 /* While starting up, there is no place to enter text. */
11720 if (no_console_input())
11723 if (argvars
[0].v_type
!= VAR_LIST
|| argvars
[0].vval
.v_list
== NULL
)
11725 EMSG2(_(e_listarg
), "inputlist()");
11730 msg_row
= Rows
- 1; /* for when 'cmdheight' > 1 */
11731 lines_left
= Rows
; /* avoid more prompt */
11735 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
; li
= li
->li_next
)
11737 msg_puts(get_tv_string(&li
->li_tv
));
11741 /* Ask for choice. */
11742 selected
= prompt_for_number(&mouse_used
);
11744 selected
-= lines_left
;
11746 rettv
->vval
.v_number
= selected
;
11750 static garray_T ga_userinput
= {0, 0, sizeof(tasave_T
), 4, NULL
};
11753 * "inputrestore()" function
11757 f_inputrestore(argvars
, rettv
)
11761 if (ga_userinput
.ga_len
> 0)
11763 --ga_userinput
.ga_len
;
11764 restore_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
11765 + ga_userinput
.ga_len
);
11766 rettv
->vval
.v_number
= 0; /* OK */
11768 else if (p_verbose
> 1)
11770 verb_msg((char_u
*)_("called inputrestore() more often than inputsave()"));
11771 rettv
->vval
.v_number
= 1; /* Failed */
11776 * "inputsave()" function
11780 f_inputsave(argvars
, rettv
)
11784 /* Add an entry to the stack of typehead storage. */
11785 if (ga_grow(&ga_userinput
, 1) == OK
)
11787 save_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
11788 + ga_userinput
.ga_len
);
11789 ++ga_userinput
.ga_len
;
11790 rettv
->vval
.v_number
= 0; /* OK */
11793 rettv
->vval
.v_number
= 1; /* Failed */
11797 * "inputsecret()" function
11800 f_inputsecret(argvars
, rettv
)
11805 ++inputsecret_flag
;
11806 f_input(argvars
, rettv
);
11808 --inputsecret_flag
;
11812 * "insert()" function
11815 f_insert(argvars
, rettv
)
11824 rettv
->vval
.v_number
= 0;
11825 if (argvars
[0].v_type
!= VAR_LIST
)
11826 EMSG2(_(e_listarg
), "insert()");
11827 else if ((l
= argvars
[0].vval
.v_list
) != NULL
11828 && !tv_check_lock(l
->lv_lock
, (char_u
*)"insert()"))
11830 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
11831 before
= get_tv_number_chk(&argvars
[2], &error
);
11833 return; /* type error; errmsg already given */
11835 if (before
== l
->lv_len
)
11839 item
= list_find(l
, before
);
11842 EMSGN(_(e_listidx
), before
);
11848 list_insert_tv(l
, &argvars
[1], item
);
11849 copy_tv(&argvars
[0], rettv
);
11855 * "isdirectory()" function
11858 f_isdirectory(argvars
, rettv
)
11862 rettv
->vval
.v_number
= mch_isdir(get_tv_string(&argvars
[0]));
11866 * "islocked()" function
11869 f_islocked(argvars
, rettv
)
11877 rettv
->vval
.v_number
= -1;
11878 end
= get_lval(get_tv_string(&argvars
[0]), NULL
, &lv
, FALSE
, FALSE
, FALSE
,
11880 if (end
!= NULL
&& lv
.ll_name
!= NULL
)
11883 EMSG(_(e_trailing
));
11886 if (lv
.ll_tv
== NULL
)
11888 if (check_changedtick(lv
.ll_name
))
11889 rettv
->vval
.v_number
= 1; /* always locked */
11892 di
= find_var(lv
.ll_name
, NULL
);
11895 /* Consider a variable locked when:
11896 * 1. the variable itself is locked
11897 * 2. the value of the variable is locked.
11898 * 3. the List or Dict value is locked.
11900 rettv
->vval
.v_number
= ((di
->di_flags
& DI_FLAGS_LOCK
)
11901 || tv_islocked(&di
->di_tv
));
11905 else if (lv
.ll_range
)
11906 EMSG(_("E786: Range not allowed"));
11907 else if (lv
.ll_newkey
!= NULL
)
11908 EMSG2(_(e_dictkey
), lv
.ll_newkey
);
11909 else if (lv
.ll_list
!= NULL
)
11911 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_li
->li_tv
);
11913 /* Dictionary item. */
11914 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_di
->di_tv
);
11921 static void dict_list
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int what
));
11924 * Turn a dict into a list:
11925 * "what" == 0: list of keys
11926 * "what" == 1: list of values
11927 * "what" == 2: list of items
11930 dict_list(argvars
, rettv
, what
)
11943 rettv
->vval
.v_number
= 0;
11944 if (argvars
[0].v_type
!= VAR_DICT
)
11946 EMSG(_(e_dictreq
));
11949 if ((d
= argvars
[0].vval
.v_dict
) == NULL
)
11952 if (rettv_list_alloc(rettv
) == FAIL
)
11955 todo
= (int)d
->dv_hashtab
.ht_used
;
11956 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
11958 if (!HASHITEM_EMPTY(hi
))
11963 li
= listitem_alloc();
11966 list_append(rettv
->vval
.v_list
, li
);
11971 li
->li_tv
.v_type
= VAR_STRING
;
11972 li
->li_tv
.v_lock
= 0;
11973 li
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
11975 else if (what
== 1)
11978 copy_tv(&di
->di_tv
, &li
->li_tv
);
11984 li
->li_tv
.v_type
= VAR_LIST
;
11985 li
->li_tv
.v_lock
= 0;
11986 li
->li_tv
.vval
.v_list
= l2
;
11991 li2
= listitem_alloc();
11994 list_append(l2
, li2
);
11995 li2
->li_tv
.v_type
= VAR_STRING
;
11996 li2
->li_tv
.v_lock
= 0;
11997 li2
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
11999 li2
= listitem_alloc();
12002 list_append(l2
, li2
);
12003 copy_tv(&di
->di_tv
, &li2
->li_tv
);
12010 * "items(dict)" function
12013 f_items(argvars
, rettv
)
12017 dict_list(argvars
, rettv
, 2);
12021 * "join()" function
12024 f_join(argvars
, rettv
)
12031 rettv
->vval
.v_number
= 0;
12032 if (argvars
[0].v_type
!= VAR_LIST
)
12034 EMSG(_(e_listreq
));
12037 if (argvars
[0].vval
.v_list
== NULL
)
12039 if (argvars
[1].v_type
== VAR_UNKNOWN
)
12040 sep
= (char_u
*)" ";
12042 sep
= get_tv_string_chk(&argvars
[1]);
12044 rettv
->v_type
= VAR_STRING
;
12048 ga_init2(&ga
, (int)sizeof(char), 80);
12049 list_join(&ga
, argvars
[0].vval
.v_list
, sep
, TRUE
, 0);
12050 ga_append(&ga
, NUL
);
12051 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
12054 rettv
->vval
.v_string
= NULL
;
12058 * "keys()" function
12061 f_keys(argvars
, rettv
)
12065 dict_list(argvars
, rettv
, 0);
12069 * "last_buffer_nr()" function.
12073 f_last_buffer_nr(argvars
, rettv
)
12080 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
12081 if (n
< buf
->b_fnum
)
12084 rettv
->vval
.v_number
= n
;
12091 f_len(argvars
, rettv
)
12095 switch (argvars
[0].v_type
)
12099 rettv
->vval
.v_number
= (varnumber_T
)STRLEN(
12100 get_tv_string(&argvars
[0]));
12103 rettv
->vval
.v_number
= list_len(argvars
[0].vval
.v_list
);
12106 rettv
->vval
.v_number
= dict_len(argvars
[0].vval
.v_dict
);
12109 EMSG(_("E701: Invalid type for len()"));
12114 static void libcall_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int type
));
12117 libcall_common(argvars
, rettv
, type
)
12122 #ifdef FEAT_LIBCALL
12124 char_u
**string_result
;
12128 rettv
->v_type
= type
;
12129 if (type
== VAR_NUMBER
)
12130 rettv
->vval
.v_number
= 0;
12132 rettv
->vval
.v_string
= NULL
;
12134 if (check_restricted() || check_secure())
12137 #ifdef FEAT_LIBCALL
12138 /* The first two args must be strings, otherwise its meaningless */
12139 if (argvars
[0].v_type
== VAR_STRING
&& argvars
[1].v_type
== VAR_STRING
)
12142 if (argvars
[2].v_type
== VAR_STRING
)
12143 string_in
= argvars
[2].vval
.v_string
;
12144 if (type
== VAR_NUMBER
)
12145 string_result
= NULL
;
12147 string_result
= &rettv
->vval
.v_string
;
12148 if (mch_libcall(argvars
[0].vval
.v_string
,
12149 argvars
[1].vval
.v_string
,
12151 argvars
[2].vval
.v_number
,
12154 && type
== VAR_NUMBER
)
12155 rettv
->vval
.v_number
= nr_result
;
12161 * "libcall()" function
12164 f_libcall(argvars
, rettv
)
12168 libcall_common(argvars
, rettv
, VAR_STRING
);
12172 * "libcallnr()" function
12175 f_libcallnr(argvars
, rettv
)
12179 libcall_common(argvars
, rettv
, VAR_NUMBER
);
12183 * "line(string)" function
12186 f_line(argvars
, rettv
)
12194 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
12197 rettv
->vval
.v_number
= lnum
;
12201 * "line2byte(lnum)" function
12205 f_line2byte(argvars
, rettv
)
12209 #ifndef FEAT_BYTEOFF
12210 rettv
->vval
.v_number
= -1;
12214 lnum
= get_tv_lnum(argvars
);
12215 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
12216 rettv
->vval
.v_number
= -1;
12218 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
, lnum
, NULL
);
12219 if (rettv
->vval
.v_number
>= 0)
12220 ++rettv
->vval
.v_number
;
12225 * "lispindent(lnum)" function
12228 f_lispindent(argvars
, rettv
)
12236 pos
= curwin
->w_cursor
;
12237 lnum
= get_tv_lnum(argvars
);
12238 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
12240 curwin
->w_cursor
.lnum
= lnum
;
12241 rettv
->vval
.v_number
= get_lisp_indent();
12242 curwin
->w_cursor
= pos
;
12246 rettv
->vval
.v_number
= -1;
12250 * "localtime()" function
12254 f_localtime(argvars
, rettv
)
12258 rettv
->vval
.v_number
= (varnumber_T
)time(NULL
);
12261 static void get_maparg
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int exact
));
12264 get_maparg(argvars
, rettv
, exact
)
12271 char_u buf
[NUMBUFLEN
];
12272 char_u
*keys_buf
= NULL
;
12278 /* return empty string for failure */
12279 rettv
->v_type
= VAR_STRING
;
12280 rettv
->vval
.v_string
= NULL
;
12282 keys
= get_tv_string(&argvars
[0]);
12286 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12288 which
= get_tv_string_buf_chk(&argvars
[1], buf
);
12289 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12290 abbr
= get_tv_number(&argvars
[2]);
12293 which
= (char_u
*)"";
12297 mode
= get_map_mode(&which
, 0);
12299 keys
= replace_termcodes(keys
, &keys_buf
, TRUE
, TRUE
, FALSE
);
12300 rhs
= check_map(keys
, mode
, exact
, FALSE
, abbr
);
12301 vim_free(keys_buf
);
12305 ga
.ga_itemsize
= 1;
12306 ga
.ga_growsize
= 40;
12308 while (*rhs
!= NUL
)
12309 ga_concat(&ga
, str2special(&rhs
, FALSE
));
12311 ga_append(&ga
, NUL
);
12312 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
12320 f_map(argvars
, rettv
)
12324 filter_map(argvars
, rettv
, TRUE
);
12328 * "maparg()" function
12331 f_maparg(argvars
, rettv
)
12335 get_maparg(argvars
, rettv
, TRUE
);
12339 * "mapcheck()" function
12342 f_mapcheck(argvars
, rettv
)
12346 get_maparg(argvars
, rettv
, FALSE
);
12349 static void find_some_match
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int start
));
12352 find_some_match(argvars
, rettv
, type
)
12357 char_u
*str
= NULL
;
12358 char_u
*expr
= NULL
;
12360 regmatch_T regmatch
;
12361 char_u patbuf
[NUMBUFLEN
];
12362 char_u strbuf
[NUMBUFLEN
];
12366 colnr_T startcol
= 0;
12369 listitem_T
*li
= NULL
;
12371 char_u
*tofree
= NULL
;
12373 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12375 p_cpo
= (char_u
*)"";
12377 rettv
->vval
.v_number
= -1;
12380 /* return empty list when there are no matches */
12381 if (rettv_list_alloc(rettv
) == FAIL
)
12384 else if (type
== 2)
12386 rettv
->v_type
= VAR_STRING
;
12387 rettv
->vval
.v_string
= NULL
;
12390 if (argvars
[0].v_type
== VAR_LIST
)
12392 if ((l
= argvars
[0].vval
.v_list
) == NULL
)
12397 expr
= str
= get_tv_string(&argvars
[0]);
12399 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
12403 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12407 start
= get_tv_number_chk(&argvars
[2], &error
);
12412 li
= list_find(l
, start
);
12415 idx
= l
->lv_idx
; /* use the cached index */
12421 if (start
> (long)STRLEN(str
))
12423 /* When "count" argument is there ignore matches before "start",
12424 * otherwise skip part of the string. Differs when pattern is "^"
12426 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
12432 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
12433 nth
= get_tv_number_chk(&argvars
[3], &error
);
12438 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
12439 if (regmatch
.regprog
!= NULL
)
12441 regmatch
.rm_ic
= p_ic
;
12453 str
= echo_string(&li
->li_tv
, &tofree
, strbuf
, 0);
12458 match
= vim_regexec_nl(®match
, str
, (colnr_T
)startcol
);
12460 if (match
&& --nth
<= 0)
12462 if (l
== NULL
&& !match
)
12465 /* Advance to just after the match. */
12474 startcol
= (colnr_T
)(regmatch
.startp
[0]
12475 + (*mb_ptr2len
)(regmatch
.startp
[0]) - str
);
12477 startcol
= regmatch
.startp
[0] + 1 - str
;
12488 /* return list with matched string and submatches */
12489 for (i
= 0; i
< NSUBEXP
; ++i
)
12491 if (regmatch
.endp
[i
] == NULL
)
12493 if (list_append_string(rettv
->vval
.v_list
,
12494 (char_u
*)"", 0) == FAIL
)
12497 else if (list_append_string(rettv
->vval
.v_list
,
12498 regmatch
.startp
[i
],
12499 (int)(regmatch
.endp
[i
] - regmatch
.startp
[i
]))
12504 else if (type
== 2)
12506 /* return matched string */
12508 copy_tv(&li
->li_tv
, rettv
);
12510 rettv
->vval
.v_string
= vim_strnsave(regmatch
.startp
[0],
12511 (int)(regmatch
.endp
[0] - regmatch
.startp
[0]));
12513 else if (l
!= NULL
)
12514 rettv
->vval
.v_number
= idx
;
12518 rettv
->vval
.v_number
=
12519 (varnumber_T
)(regmatch
.startp
[0] - str
);
12521 rettv
->vval
.v_number
=
12522 (varnumber_T
)(regmatch
.endp
[0] - str
);
12523 rettv
->vval
.v_number
+= (varnumber_T
)(str
- expr
);
12526 vim_free(regmatch
.regprog
);
12535 * "match()" function
12538 f_match(argvars
, rettv
)
12542 find_some_match(argvars
, rettv
, 1);
12546 * "matchadd()" function
12549 f_matchadd(argvars
, rettv
)
12553 #ifdef FEAT_SEARCH_EXTRA
12554 char_u buf
[NUMBUFLEN
];
12555 char_u
*grp
= get_tv_string_buf_chk(&argvars
[0], buf
); /* group */
12556 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], buf
); /* pattern */
12557 int prio
= 10; /* default priority */
12561 rettv
->vval
.v_number
= -1;
12563 if (grp
== NULL
|| pat
== NULL
)
12565 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12567 prio
= get_tv_number_chk(&argvars
[2], &error
);
12568 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
12569 id
= get_tv_number_chk(&argvars
[3], &error
);
12573 if (id
>= 1 && id
<= 3)
12575 EMSGN("E798: ID is reserved for \":match\": %ld", id
);
12579 rettv
->vval
.v_number
= match_add(curwin
, grp
, pat
, prio
, id
);
12584 * "matcharg()" function
12587 f_matcharg(argvars
, rettv
)
12591 if (rettv_list_alloc(rettv
) == OK
)
12593 #ifdef FEAT_SEARCH_EXTRA
12594 int id
= get_tv_number(&argvars
[0]);
12597 if (id
>= 1 && id
<= 3)
12599 if ((m
= (matchitem_T
*)get_match(curwin
, id
)) != NULL
)
12601 list_append_string(rettv
->vval
.v_list
,
12602 syn_id2name(m
->hlg_id
), -1);
12603 list_append_string(rettv
->vval
.v_list
, m
->pattern
, -1);
12607 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
12608 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
12616 * "matchdelete()" function
12619 f_matchdelete(argvars
, rettv
)
12623 #ifdef FEAT_SEARCH_EXTRA
12624 rettv
->vval
.v_number
= match_delete(curwin
,
12625 (int)get_tv_number(&argvars
[0]), TRUE
);
12630 * "matchend()" function
12633 f_matchend(argvars
, rettv
)
12637 find_some_match(argvars
, rettv
, 0);
12641 * "matchlist()" function
12644 f_matchlist(argvars
, rettv
)
12648 find_some_match(argvars
, rettv
, 3);
12652 * "matchstr()" function
12655 f_matchstr(argvars
, rettv
)
12659 find_some_match(argvars
, rettv
, 2);
12662 static void max_min
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int domax
));
12665 max_min(argvars
, rettv
, domax
)
12674 if (argvars
[0].v_type
== VAR_LIST
)
12679 l
= argvars
[0].vval
.v_list
;
12685 n
= get_tv_number_chk(&li
->li_tv
, &error
);
12691 i
= get_tv_number_chk(&li
->li_tv
, &error
);
12692 if (domax
? i
> n
: i
< n
)
12698 else if (argvars
[0].v_type
== VAR_DICT
)
12705 d
= argvars
[0].vval
.v_dict
;
12708 todo
= (int)d
->dv_hashtab
.ht_used
;
12709 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
12711 if (!HASHITEM_EMPTY(hi
))
12714 i
= get_tv_number_chk(&HI2DI(hi
)->di_tv
, &error
);
12720 else if (domax
? i
> n
: i
< n
)
12727 EMSG(_(e_listdictarg
));
12728 rettv
->vval
.v_number
= error
? 0 : n
;
12735 f_max(argvars
, rettv
)
12739 max_min(argvars
, rettv
, TRUE
);
12746 f_min(argvars
, rettv
)
12750 max_min(argvars
, rettv
, FALSE
);
12753 static int mkdir_recurse
__ARGS((char_u
*dir
, int prot
));
12756 * Create the directory in which "dir" is located, and higher levels when
12760 mkdir_recurse(dir
, prot
)
12768 /* Get end of directory name in "dir".
12769 * We're done when it's "/" or "c:/". */
12770 p
= gettail_sep(dir
);
12771 if (p
<= get_past_head(dir
))
12774 /* If the directory exists we're done. Otherwise: create it.*/
12775 updir
= vim_strnsave(dir
, (int)(p
- dir
));
12778 if (mch_isdir(updir
))
12780 else if (mkdir_recurse(updir
, prot
) == OK
)
12781 r
= vim_mkdir_emsg(updir
, prot
);
12788 * "mkdir()" function
12791 f_mkdir(argvars
, rettv
)
12796 char_u buf
[NUMBUFLEN
];
12799 rettv
->vval
.v_number
= FAIL
;
12800 if (check_restricted() || check_secure())
12803 dir
= get_tv_string_buf(&argvars
[0], buf
);
12804 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12806 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12807 prot
= get_tv_number_chk(&argvars
[2], NULL
);
12808 if (prot
!= -1 && STRCMP(get_tv_string(&argvars
[1]), "p") == 0)
12809 mkdir_recurse(dir
, prot
);
12811 rettv
->vval
.v_number
= prot
!= -1 ? vim_mkdir_emsg(dir
, prot
) : 0;
12816 * "mode()" function
12820 f_mode(argvars
, rettv
)
12830 buf
[0] = VIsual_mode
+ 's' - 'v';
12832 buf
[0] = VIsual_mode
;
12836 if (State
== HITRETURN
|| State
== ASKMORE
|| State
== SETWSIZE
)
12838 else if (State
& INSERT
)
12840 if (State
& REPLACE_FLAG
)
12845 else if (State
& CMDLINE
)
12851 rettv
->vval
.v_string
= vim_strsave(buf
);
12852 rettv
->v_type
= VAR_STRING
;
12856 * "nextnonblank()" function
12859 f_nextnonblank(argvars
, rettv
)
12865 for (lnum
= get_tv_lnum(argvars
); ; ++lnum
)
12867 if (lnum
< 0 || lnum
> curbuf
->b_ml
.ml_line_count
)
12872 if (*skipwhite(ml_get(lnum
)) != NUL
)
12875 rettv
->vval
.v_number
= lnum
;
12879 * "nr2char()" function
12882 f_nr2char(argvars
, rettv
)
12886 char_u buf
[NUMBUFLEN
];
12890 buf
[(*mb_char2bytes
)((int)get_tv_number(&argvars
[0]), buf
)] = NUL
;
12894 buf
[0] = (char_u
)get_tv_number(&argvars
[0]);
12897 rettv
->v_type
= VAR_STRING
;
12898 rettv
->vval
.v_string
= vim_strsave(buf
);
12902 * "pathshorten()" function
12905 f_pathshorten(argvars
, rettv
)
12911 rettv
->v_type
= VAR_STRING
;
12912 p
= get_tv_string_chk(&argvars
[0]);
12914 rettv
->vval
.v_string
= NULL
;
12917 p
= vim_strsave(p
);
12918 rettv
->vval
.v_string
= p
;
12925 * "prevnonblank()" function
12928 f_prevnonblank(argvars
, rettv
)
12934 lnum
= get_tv_lnum(argvars
);
12935 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
)
12938 while (lnum
>= 1 && *skipwhite(ml_get(lnum
)) == NUL
)
12940 rettv
->vval
.v_number
= lnum
;
12943 #ifdef HAVE_STDARG_H
12944 /* This dummy va_list is here because:
12945 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12946 * - locally in the function results in a "used before set" warning
12947 * - using va_start() to initialize it gives "function with fixed args" error */
12952 * "printf()" function
12955 f_printf(argvars
, rettv
)
12959 rettv
->v_type
= VAR_STRING
;
12960 rettv
->vval
.v_string
= NULL
;
12961 #ifdef HAVE_STDARG_H /* only very old compilers can't do this */
12963 char_u buf
[NUMBUFLEN
];
12966 int saved_did_emsg
= did_emsg
;
12969 /* Get the required length, allocate the buffer and do it for real. */
12971 fmt
= (char *)get_tv_string_buf(&argvars
[0], buf
);
12972 len
= vim_vsnprintf(NULL
, 0, fmt
, ap
, argvars
+ 1);
12975 s
= alloc(len
+ 1);
12978 rettv
->vval
.v_string
= s
;
12979 (void)vim_vsnprintf((char *)s
, len
+ 1, fmt
, ap
, argvars
+ 1);
12982 did_emsg
|= saved_did_emsg
;
12988 * "pumvisible()" function
12992 f_pumvisible(argvars
, rettv
)
12996 rettv
->vval
.v_number
= 0;
12997 #ifdef FEAT_INS_EXPAND
12999 rettv
->vval
.v_number
= 1;
13004 * "range()" function
13007 f_range(argvars
, rettv
)
13017 start
= get_tv_number_chk(&argvars
[0], &error
);
13018 if (argvars
[1].v_type
== VAR_UNKNOWN
)
13025 end
= get_tv_number_chk(&argvars
[1], &error
);
13026 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13027 stride
= get_tv_number_chk(&argvars
[2], &error
);
13030 rettv
->vval
.v_number
= 0;
13032 return; /* type error; errmsg already given */
13034 EMSG(_("E726: Stride is zero"));
13035 else if (stride
> 0 ? end
+ 1 < start
: end
- 1 > start
)
13036 EMSG(_("E727: Start past end"));
13039 if (rettv_list_alloc(rettv
) == OK
)
13040 for (i
= start
; stride
> 0 ? i
<= end
: i
>= end
; i
+= stride
)
13041 if (list_append_number(rettv
->vval
.v_list
,
13042 (varnumber_T
)i
) == FAIL
)
13048 * "readfile()" function
13051 f_readfile(argvars
, rettv
)
13055 int binary
= FALSE
;
13059 #define FREAD_SIZE 200 /* optimized for text lines */
13060 char_u buf
[FREAD_SIZE
];
13061 int readlen
; /* size of last fread() */
13062 int buflen
; /* nr of valid chars in buf[] */
13063 int filtd
; /* how much in buf[] was NUL -> '\n' filtered */
13064 int tolist
; /* first byte in buf[] still to be put in list */
13065 int chop
; /* how many CR to chop off */
13066 char_u
*prev
= NULL
; /* previously read bytes, if any */
13067 int prevlen
= 0; /* length of "prev" if not NULL */
13070 long maxline
= MAXLNUM
;
13073 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
13075 if (STRCMP(get_tv_string(&argvars
[1]), "b") == 0)
13077 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13078 maxline
= get_tv_number(&argvars
[2]);
13081 if (rettv_list_alloc(rettv
) == FAIL
)
13084 /* Always open the file in binary mode, library functions have a mind of
13085 * their own about CR-LF conversion. */
13086 fname
= get_tv_string(&argvars
[0]);
13087 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, READBIN
)) == NULL
)
13089 EMSG2(_(e_notopen
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
13094 while (cnt
< maxline
|| maxline
< 0)
13096 readlen
= (int)fread(buf
+ filtd
, 1, FREAD_SIZE
- filtd
, fd
);
13097 buflen
= filtd
+ readlen
;
13099 for ( ; filtd
< buflen
|| readlen
<= 0; ++filtd
)
13101 if (buf
[filtd
] == '\n' || readlen
<= 0)
13103 /* Only when in binary mode add an empty list item when the
13104 * last line ends in a '\n'. */
13105 if (!binary
&& readlen
== 0 && filtd
== 0)
13108 /* Found end-of-line or end-of-file: add a text line to the
13112 while (filtd
- chop
- 1 >= tolist
13113 && buf
[filtd
- chop
- 1] == '\r')
13115 len
= filtd
- tolist
- chop
;
13117 s
= vim_strnsave(buf
+ tolist
, len
);
13120 s
= alloc((unsigned)(prevlen
+ len
+ 1));
13123 mch_memmove(s
, prev
, prevlen
);
13126 mch_memmove(s
+ prevlen
, buf
+ tolist
, len
);
13127 s
[prevlen
+ len
] = NUL
;
13130 tolist
= filtd
+ 1;
13132 li
= listitem_alloc();
13138 li
->li_tv
.v_type
= VAR_STRING
;
13139 li
->li_tv
.v_lock
= 0;
13140 li
->li_tv
.vval
.v_string
= s
;
13141 list_append(rettv
->vval
.v_list
, li
);
13143 if (++cnt
>= maxline
&& maxline
>= 0)
13148 else if (buf
[filtd
] == NUL
)
13156 /* "buf" is full, need to move text to an allocated buffer */
13159 prev
= vim_strnsave(buf
, buflen
);
13164 s
= alloc((unsigned)(prevlen
+ buflen
));
13167 mch_memmove(s
, prev
, prevlen
);
13168 mch_memmove(s
+ prevlen
, buf
, buflen
);
13178 mch_memmove(buf
, buf
+ tolist
, buflen
- tolist
);
13184 * For a negative line count use only the lines at the end of the file,
13188 while (cnt
> -maxline
)
13190 listitem_remove(rettv
->vval
.v_list
, rettv
->vval
.v_list
->lv_first
);
13198 #if defined(FEAT_RELTIME)
13199 static int list2proftime
__ARGS((typval_T
*arg
, proftime_T
*tm
));
13202 * Convert a List to proftime_T.
13203 * Return FAIL when there is something wrong.
13206 list2proftime(arg
, tm
)
13213 if (arg
->v_type
!= VAR_LIST
|| arg
->vval
.v_list
== NULL
13214 || arg
->vval
.v_list
->lv_len
!= 2)
13216 n1
= list_find_nr(arg
->vval
.v_list
, 0L, &error
);
13217 n2
= list_find_nr(arg
->vval
.v_list
, 1L, &error
);
13225 return error
? FAIL
: OK
;
13227 #endif /* FEAT_RELTIME */
13230 * "reltime()" function
13233 f_reltime(argvars
, rettv
)
13237 #ifdef FEAT_RELTIME
13241 if (argvars
[0].v_type
== VAR_UNKNOWN
)
13243 /* No arguments: get current time. */
13244 profile_start(&res
);
13246 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
13248 if (list2proftime(&argvars
[0], &res
) == FAIL
)
13254 /* Two arguments: compute the difference. */
13255 if (list2proftime(&argvars
[0], &start
) == FAIL
13256 || list2proftime(&argvars
[1], &res
) == FAIL
)
13258 profile_sub(&res
, &start
);
13261 if (rettv_list_alloc(rettv
) == OK
)
13272 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n1
);
13273 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n2
);
13279 * "reltimestr()" function
13282 f_reltimestr(argvars
, rettv
)
13286 #ifdef FEAT_RELTIME
13290 rettv
->v_type
= VAR_STRING
;
13291 rettv
->vval
.v_string
= NULL
;
13292 #ifdef FEAT_RELTIME
13293 if (list2proftime(&argvars
[0], &tm
) == OK
)
13294 rettv
->vval
.v_string
= vim_strsave((char_u
*)profile_msg(&tm
));
13298 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13299 static void make_connection
__ARGS((void));
13300 static int check_connection
__ARGS((void));
13305 if (X_DISPLAY
== NULL
13311 x_force_connect
= TRUE
;
13313 x_force_connect
= FALSE
;
13321 if (X_DISPLAY
== NULL
)
13323 EMSG(_("E240: No connection to Vim server"));
13330 #ifdef FEAT_CLIENTSERVER
13331 static void remote_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int expr
));
13334 remote_common(argvars
, rettv
, expr
)
13339 char_u
*server_name
;
13342 char_u buf
[NUMBUFLEN
];
13349 if (check_restricted() || check_secure())
13353 if (check_connection() == FAIL
)
13357 server_name
= get_tv_string_chk(&argvars
[0]);
13358 if (server_name
== NULL
)
13359 return; /* type error; errmsg already given */
13360 keys
= get_tv_string_buf(&argvars
[1], buf
);
13362 if (serverSendToVim(server_name
, keys
, &r
, &w
, expr
, TRUE
) < 0)
13364 if (serverSendToVim(X_DISPLAY
, server_name
, keys
, &r
, &w
, expr
, 0, TRUE
)
13369 EMSG(r
); /* sending worked but evaluation failed */
13371 EMSG2(_("E241: Unable to send to %s"), server_name
);
13375 rettv
->vval
.v_string
= r
;
13377 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13383 sprintf((char *)str
, PRINTF_HEX_LONG_U
, (long_u
)w
);
13384 v
.di_tv
.v_type
= VAR_STRING
;
13385 v
.di_tv
.vval
.v_string
= vim_strsave(str
);
13386 idvar
= get_tv_string_chk(&argvars
[2]);
13388 set_var(idvar
, &v
.di_tv
, FALSE
);
13389 vim_free(v
.di_tv
.vval
.v_string
);
13395 * "remote_expr()" function
13399 f_remote_expr(argvars
, rettv
)
13403 rettv
->v_type
= VAR_STRING
;
13404 rettv
->vval
.v_string
= NULL
;
13405 #ifdef FEAT_CLIENTSERVER
13406 remote_common(argvars
, rettv
, TRUE
);
13411 * "remote_foreground()" function
13415 f_remote_foreground(argvars
, rettv
)
13419 rettv
->vval
.v_number
= 0;
13420 #ifdef FEAT_CLIENTSERVER
13422 /* On Win32 it's done in this application. */
13424 char_u
*server_name
= get_tv_string_chk(&argvars
[0]);
13426 if (server_name
!= NULL
)
13427 serverForeground(server_name
);
13430 /* Send a foreground() expression to the server. */
13431 argvars
[1].v_type
= VAR_STRING
;
13432 argvars
[1].vval
.v_string
= vim_strsave((char_u
*)"foreground()");
13433 argvars
[2].v_type
= VAR_UNKNOWN
;
13434 remote_common(argvars
, rettv
, TRUE
);
13435 vim_free(argvars
[1].vval
.v_string
);
13442 f_remote_peek(argvars
, rettv
)
13446 #ifdef FEAT_CLIENTSERVER
13454 if (check_restricted() || check_secure())
13456 rettv
->vval
.v_number
= -1;
13459 serverid
= get_tv_string_chk(&argvars
[0]);
13460 if (serverid
== NULL
)
13462 rettv
->vval
.v_number
= -1;
13463 return; /* type error; errmsg already given */
13466 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
13468 rettv
->vval
.v_number
= -1;
13471 s
= serverGetReply((HWND
)n
, FALSE
, FALSE
, FALSE
);
13472 rettv
->vval
.v_number
= (s
!= NULL
);
13475 rettv
->vval
.v_number
= 0;
13476 if (check_connection() == FAIL
)
13479 rettv
->vval
.v_number
= serverPeekReply(X_DISPLAY
,
13480 serverStrToWin(serverid
), &s
);
13483 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& rettv
->vval
.v_number
> 0)
13487 v
.di_tv
.v_type
= VAR_STRING
;
13488 v
.di_tv
.vval
.v_string
= vim_strsave(s
);
13489 retvar
= get_tv_string_chk(&argvars
[1]);
13490 if (retvar
!= NULL
)
13491 set_var(retvar
, &v
.di_tv
, FALSE
);
13492 vim_free(v
.di_tv
.vval
.v_string
);
13495 rettv
->vval
.v_number
= -1;
13501 f_remote_read(argvars
, rettv
)
13507 #ifdef FEAT_CLIENTSERVER
13508 char_u
*serverid
= get_tv_string_chk(&argvars
[0]);
13510 if (serverid
!= NULL
&& !check_restricted() && !check_secure())
13513 /* The server's HWND is encoded in the 'id' parameter */
13516 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
13518 r
= serverGetReply((HWND
)n
, FALSE
, TRUE
, TRUE
);
13521 if (check_connection() == FAIL
|| serverReadReply(X_DISPLAY
,
13522 serverStrToWin(serverid
), &r
, FALSE
) < 0)
13524 EMSG(_("E277: Unable to read a server reply"));
13527 rettv
->v_type
= VAR_STRING
;
13528 rettv
->vval
.v_string
= r
;
13532 * "remote_send()" function
13536 f_remote_send(argvars
, rettv
)
13540 rettv
->v_type
= VAR_STRING
;
13541 rettv
->vval
.v_string
= NULL
;
13542 #ifdef FEAT_CLIENTSERVER
13543 remote_common(argvars
, rettv
, FALSE
);
13548 * "remove()" function
13551 f_remove(argvars
, rettv
)
13556 listitem_T
*item
, *item2
;
13564 rettv
->vval
.v_number
= 0;
13565 if (argvars
[0].v_type
== VAR_DICT
)
13567 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13568 EMSG2(_(e_toomanyarg
), "remove()");
13569 else if ((d
= argvars
[0].vval
.v_dict
) != NULL
13570 && !tv_check_lock(d
->dv_lock
, (char_u
*)"remove() argument"))
13572 key
= get_tv_string_chk(&argvars
[1]);
13575 di
= dict_find(d
, key
, -1);
13577 EMSG2(_(e_dictkey
), key
);
13580 *rettv
= di
->di_tv
;
13581 init_tv(&di
->di_tv
);
13582 dictitem_remove(d
, di
);
13587 else if (argvars
[0].v_type
!= VAR_LIST
)
13588 EMSG2(_(e_listdictarg
), "remove()");
13589 else if ((l
= argvars
[0].vval
.v_list
) != NULL
13590 && !tv_check_lock(l
->lv_lock
, (char_u
*)"remove() argument"))
13594 idx
= get_tv_number_chk(&argvars
[1], &error
);
13596 ; /* type error: do nothing, errmsg already given */
13597 else if ((item
= list_find(l
, idx
)) == NULL
)
13598 EMSGN(_(e_listidx
), idx
);
13601 if (argvars
[2].v_type
== VAR_UNKNOWN
)
13603 /* Remove one item, return its value. */
13604 list_remove(l
, item
, item
);
13605 *rettv
= item
->li_tv
;
13610 /* Remove range of items, return list with values. */
13611 end
= get_tv_number_chk(&argvars
[2], &error
);
13613 ; /* type error: do nothing */
13614 else if ((item2
= list_find(l
, end
)) == NULL
)
13615 EMSGN(_(e_listidx
), end
);
13620 for (li
= item
; li
!= NULL
; li
= li
->li_next
)
13626 if (li
== NULL
) /* didn't find "item2" after "item" */
13627 EMSG(_(e_invrange
));
13630 list_remove(l
, item
, item2
);
13631 if (rettv_list_alloc(rettv
) == OK
)
13633 l
= rettv
->vval
.v_list
;
13634 l
->lv_first
= item
;
13635 l
->lv_last
= item2
;
13636 item
->li_prev
= NULL
;
13637 item2
->li_next
= NULL
;
13648 * "rename({from}, {to})" function
13651 f_rename(argvars
, rettv
)
13655 char_u buf
[NUMBUFLEN
];
13657 if (check_restricted() || check_secure())
13658 rettv
->vval
.v_number
= -1;
13660 rettv
->vval
.v_number
= vim_rename(get_tv_string(&argvars
[0]),
13661 get_tv_string_buf(&argvars
[1], buf
));
13665 * "repeat()" function
13669 f_repeat(argvars
, rettv
)
13680 n
= get_tv_number(&argvars
[1]);
13681 if (argvars
[0].v_type
== VAR_LIST
)
13683 if (rettv_list_alloc(rettv
) == OK
&& argvars
[0].vval
.v_list
!= NULL
)
13685 if (list_extend(rettv
->vval
.v_list
,
13686 argvars
[0].vval
.v_list
, NULL
) == FAIL
)
13691 p
= get_tv_string(&argvars
[0]);
13692 rettv
->v_type
= VAR_STRING
;
13693 rettv
->vval
.v_string
= NULL
;
13695 slen
= (int)STRLEN(p
);
13700 r
= alloc(len
+ 1);
13703 for (i
= 0; i
< n
; i
++)
13704 mch_memmove(r
+ i
* slen
, p
, (size_t)slen
);
13708 rettv
->vval
.v_string
= r
;
13713 * "resolve()" function
13716 f_resolve(argvars
, rettv
)
13722 p
= get_tv_string(&argvars
[0]);
13723 #ifdef FEAT_SHORTCUT
13727 v
= mch_resolve_shortcut(p
);
13729 rettv
->vval
.v_string
= v
;
13731 rettv
->vval
.v_string
= vim_strsave(p
);
13734 # ifdef HAVE_READLINK
13736 char_u buf
[MAXPATHL
+ 1];
13739 char_u
*remain
= NULL
;
13741 int is_relative_to_current
= FALSE
;
13742 int has_trailing_pathsep
= FALSE
;
13745 p
= vim_strsave(p
);
13747 if (p
[0] == '.' && (vim_ispathsep(p
[1])
13748 || (p
[1] == '.' && (vim_ispathsep(p
[2])))))
13749 is_relative_to_current
= TRUE
;
13752 if (len
> 0 && after_pathsep(p
, p
+ len
))
13753 has_trailing_pathsep
= TRUE
;
13755 q
= getnextcomp(p
);
13758 /* Separate the first path component in "p", and keep the
13759 * remainder (beginning with the path separator). */
13760 remain
= vim_strsave(q
- 1);
13768 len
= readlink((char *)p
, (char *)buf
, MAXPATHL
);
13777 EMSG(_("E655: Too many symbolic links (cycle?)"));
13778 rettv
->vval
.v_string
= NULL
;
13782 /* Ensure that the result will have a trailing path separator
13783 * if the argument has one. */
13784 if (remain
== NULL
&& has_trailing_pathsep
)
13787 /* Separate the first path component in the link value and
13788 * concatenate the remainders. */
13789 q
= getnextcomp(vim_ispathsep(*buf
) ? buf
+ 1 : buf
);
13792 if (remain
== NULL
)
13793 remain
= vim_strsave(q
- 1);
13796 cpy
= concat_str(q
- 1, remain
);
13807 if (q
> p
&& *q
== NUL
)
13809 /* Ignore trailing path separator. */
13813 if (q
> p
&& !mch_isFullName(buf
))
13815 /* symlink is relative to directory of argument */
13816 cpy
= alloc((unsigned)(STRLEN(p
) + STRLEN(buf
) + 1));
13820 STRCPY(gettail(cpy
), buf
);
13828 p
= vim_strsave(buf
);
13832 if (remain
== NULL
)
13835 /* Append the first path component of "remain" to "p". */
13836 q
= getnextcomp(remain
+ 1);
13837 len
= q
- remain
- (*q
!= NUL
);
13838 cpy
= vim_strnsave(p
, STRLEN(p
) + len
);
13841 STRNCAT(cpy
, remain
, len
);
13845 /* Shorten "remain". */
13847 mch_memmove(remain
, q
- 1, STRLEN(q
- 1) + 1);
13855 /* If the result is a relative path name, make it explicitly relative to
13856 * the current directory if and only if the argument had this form. */
13857 if (!vim_ispathsep(*p
))
13859 if (is_relative_to_current
13863 || vim_ispathsep(p
[1])
13866 || vim_ispathsep(p
[2]))))))
13868 /* Prepend "./". */
13869 cpy
= concat_str((char_u
*)"./", p
);
13876 else if (!is_relative_to_current
)
13878 /* Strip leading "./". */
13880 while (q
[0] == '.' && vim_ispathsep(q
[1]))
13883 mch_memmove(p
, p
+ 2, STRLEN(p
+ 2) + (size_t)1);
13887 /* Ensure that the result will have no trailing path separator
13888 * if the argument had none. But keep "/" or "//". */
13889 if (!has_trailing_pathsep
)
13892 if (after_pathsep(p
, q
))
13893 *gettail_sep(p
) = NUL
;
13896 rettv
->vval
.v_string
= p
;
13899 rettv
->vval
.v_string
= vim_strsave(p
);
13903 simplify_filename(rettv
->vval
.v_string
);
13905 #ifdef HAVE_READLINK
13908 rettv
->v_type
= VAR_STRING
;
13912 * "reverse({list})" function
13915 f_reverse(argvars
, rettv
)
13920 listitem_T
*li
, *ni
;
13922 rettv
->vval
.v_number
= 0;
13923 if (argvars
[0].v_type
!= VAR_LIST
)
13924 EMSG2(_(e_listarg
), "reverse()");
13925 else if ((l
= argvars
[0].vval
.v_list
) != NULL
13926 && !tv_check_lock(l
->lv_lock
, (char_u
*)"reverse()"))
13929 l
->lv_first
= l
->lv_last
= NULL
;
13934 list_append(l
, li
);
13937 rettv
->vval
.v_list
= l
;
13938 rettv
->v_type
= VAR_LIST
;
13943 #define SP_NOMOVE 0x01 /* don't move cursor */
13944 #define SP_REPEAT 0x02 /* repeat to find outer pair */
13945 #define SP_RETCOUNT 0x04 /* return matchcount */
13946 #define SP_SETPCMARK 0x08 /* set previous context mark */
13947 #define SP_START 0x10 /* accept match at start position */
13948 #define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13949 #define SP_END 0x40 /* leave cursor at end of match */
13951 static int get_search_arg
__ARGS((typval_T
*varp
, int *flagsp
));
13954 * Get flags for a search function.
13955 * Possibly sets "p_ws".
13956 * Returns BACKWARD, FORWARD or zero (for an error).
13959 get_search_arg(varp
, flagsp
)
13965 char_u nbuf
[NUMBUFLEN
];
13968 if (varp
->v_type
!= VAR_UNKNOWN
)
13970 flags
= get_tv_string_buf_chk(varp
, nbuf
);
13972 return 0; /* type error; errmsg already given */
13973 while (*flags
!= NUL
)
13977 case 'b': dir
= BACKWARD
; break;
13978 case 'w': p_ws
= TRUE
; break;
13979 case 'W': p_ws
= FALSE
; break;
13981 if (flagsp
!= NULL
)
13984 case 'c': mask
= SP_START
; break;
13985 case 'e': mask
= SP_END
; break;
13986 case 'm': mask
= SP_RETCOUNT
; break;
13987 case 'n': mask
= SP_NOMOVE
; break;
13988 case 'p': mask
= SP_SUBPAT
; break;
13989 case 'r': mask
= SP_REPEAT
; break;
13990 case 's': mask
= SP_SETPCMARK
; break;
13994 EMSG2(_(e_invarg2
), flags
);
14009 * Shared by search() and searchpos() functions
14012 search_cmn(argvars
, match_pos
, flagsp
)
14021 int save_p_ws
= p_ws
;
14023 int retval
= 0; /* default: FAIL */
14024 long lnum_stop
= 0;
14026 #ifdef FEAT_RELTIME
14027 long time_limit
= 0;
14029 int options
= SEARCH_KEEP
;
14032 pat
= get_tv_string(&argvars
[0]);
14033 dir
= get_search_arg(&argvars
[1], flagsp
); /* may set p_ws */
14037 if (flags
& SP_START
)
14038 options
|= SEARCH_START
;
14039 if (flags
& SP_END
)
14040 options
|= SEARCH_END
;
14042 /* Optional arguments: line number to stop searching and timeout. */
14043 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
14045 lnum_stop
= get_tv_number_chk(&argvars
[2], NULL
);
14048 #ifdef FEAT_RELTIME
14049 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
14051 time_limit
= get_tv_number_chk(&argvars
[3], NULL
);
14052 if (time_limit
< 0)
14058 #ifdef FEAT_RELTIME
14059 /* Set the time limit, if there is one. */
14060 profile_setlimit(time_limit
, &tm
);
14064 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
14065 * Check to make sure only those flags are set.
14066 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14067 * flags cannot be set. Check for that condition also.
14069 if (((flags
& (SP_REPEAT
| SP_RETCOUNT
)) != 0)
14070 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
14072 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[1]));
14076 pos
= save_cursor
= curwin
->w_cursor
;
14077 subpatnum
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
14078 options
, RE_SEARCH
, (linenr_T
)lnum_stop
, &tm
);
14079 if (subpatnum
!= FAIL
)
14081 if (flags
& SP_SUBPAT
)
14082 retval
= subpatnum
;
14085 if (flags
& SP_SETPCMARK
)
14087 curwin
->w_cursor
= pos
;
14088 if (match_pos
!= NULL
)
14090 /* Store the match cursor position */
14091 match_pos
->lnum
= pos
.lnum
;
14092 match_pos
->col
= pos
.col
+ 1;
14094 /* "/$" will put the cursor after the end of the line, may need to
14095 * correct that here */
14099 /* If 'n' flag is used: restore cursor position. */
14100 if (flags
& SP_NOMOVE
)
14101 curwin
->w_cursor
= save_cursor
;
14103 curwin
->w_set_curswant
= TRUE
;
14111 * "search()" function
14114 f_search(argvars
, rettv
)
14120 rettv
->vval
.v_number
= search_cmn(argvars
, NULL
, &flags
);
14124 * "searchdecl()" function
14127 f_searchdecl(argvars
, rettv
)
14136 rettv
->vval
.v_number
= 1; /* default: FAIL */
14138 name
= get_tv_string_chk(&argvars
[0]);
14139 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
14141 locally
= get_tv_number_chk(&argvars
[1], &error
) == 0;
14142 if (!error
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
14143 thisblock
= get_tv_number_chk(&argvars
[2], &error
) != 0;
14145 if (!error
&& name
!= NULL
)
14146 rettv
->vval
.v_number
= find_decl(name
, (int)STRLEN(name
),
14147 locally
, thisblock
, SEARCH_KEEP
) == FAIL
;
14151 * Used by searchpair() and searchpairpos()
14154 searchpair_cmn(argvars
, match_pos
)
14158 char_u
*spat
, *mpat
, *epat
;
14160 int save_p_ws
= p_ws
;
14163 char_u nbuf1
[NUMBUFLEN
];
14164 char_u nbuf2
[NUMBUFLEN
];
14165 char_u nbuf3
[NUMBUFLEN
];
14166 int retval
= 0; /* default: FAIL */
14167 long lnum_stop
= 0;
14168 long time_limit
= 0;
14170 /* Get the three pattern arguments: start, middle, end. */
14171 spat
= get_tv_string_chk(&argvars
[0]);
14172 mpat
= get_tv_string_buf_chk(&argvars
[1], nbuf1
);
14173 epat
= get_tv_string_buf_chk(&argvars
[2], nbuf2
);
14174 if (spat
== NULL
|| mpat
== NULL
|| epat
== NULL
)
14175 goto theend
; /* type error */
14177 /* Handle the optional fourth argument: flags */
14178 dir
= get_search_arg(&argvars
[3], &flags
); /* may set p_ws */
14182 /* Don't accept SP_END or SP_SUBPAT.
14183 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14185 if ((flags
& (SP_END
| SP_SUBPAT
)) != 0
14186 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
14188 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[3]));
14192 /* Using 'r' implies 'W', otherwise it doesn't work. */
14193 if (flags
& SP_REPEAT
)
14196 /* Optional fifth argument: skip expression */
14197 if (argvars
[3].v_type
== VAR_UNKNOWN
14198 || argvars
[4].v_type
== VAR_UNKNOWN
)
14199 skip
= (char_u
*)"";
14202 skip
= get_tv_string_buf_chk(&argvars
[4], nbuf3
);
14203 if (argvars
[5].v_type
!= VAR_UNKNOWN
)
14205 lnum_stop
= get_tv_number_chk(&argvars
[5], NULL
);
14208 #ifdef FEAT_RELTIME
14209 if (argvars
[6].v_type
!= VAR_UNKNOWN
)
14211 time_limit
= get_tv_number_chk(&argvars
[6], NULL
);
14212 if (time_limit
< 0)
14219 goto theend
; /* type error */
14221 retval
= do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
,
14222 match_pos
, lnum_stop
, time_limit
);
14231 * "searchpair()" function
14234 f_searchpair(argvars
, rettv
)
14238 rettv
->vval
.v_number
= searchpair_cmn(argvars
, NULL
);
14242 * "searchpairpos()" function
14245 f_searchpairpos(argvars
, rettv
)
14253 rettv
->vval
.v_number
= 0;
14255 if (rettv_list_alloc(rettv
) == FAIL
)
14258 if (searchpair_cmn(argvars
, &match_pos
) > 0)
14260 lnum
= match_pos
.lnum
;
14261 col
= match_pos
.col
;
14264 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
14265 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
14269 * Search for a start/middle/end thing.
14270 * Used by searchpair(), see its documentation for the details.
14271 * Returns 0 or -1 for no match,
14274 do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
, match_pos
,
14275 lnum_stop
, time_limit
)
14276 char_u
*spat
; /* start pattern */
14277 char_u
*mpat
; /* middle pattern */
14278 char_u
*epat
; /* end pattern */
14279 int dir
; /* BACKWARD or FORWARD */
14280 char_u
*skip
; /* skip expression */
14281 int flags
; /* SP_SETPCMARK and other SP_ values */
14283 linenr_T lnum_stop
; /* stop at this line if not zero */
14284 long time_limit
; /* stop after this many msec */
14287 char_u
*pat
, *pat2
= NULL
, *pat3
= NULL
;
14298 int options
= SEARCH_KEEP
;
14301 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14303 p_cpo
= (char_u
*)"";
14305 #ifdef FEAT_RELTIME
14306 /* Set the time limit, if there is one. */
14307 profile_setlimit(time_limit
, &tm
);
14310 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14311 * start/middle/end (pat3, for the top pair). */
14312 pat2
= alloc((unsigned)(STRLEN(spat
) + STRLEN(epat
) + 15));
14313 pat3
= alloc((unsigned)(STRLEN(spat
) + STRLEN(mpat
) + STRLEN(epat
) + 23));
14314 if (pat2
== NULL
|| pat3
== NULL
)
14316 sprintf((char *)pat2
, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat
, epat
);
14318 STRCPY(pat3
, pat2
);
14320 sprintf((char *)pat3
, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14322 if (flags
& SP_START
)
14323 options
|= SEARCH_START
;
14325 save_cursor
= curwin
->w_cursor
;
14326 pos
= curwin
->w_cursor
;
14327 clearpos(&firstpos
);
14328 clearpos(&foundpos
);
14332 n
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
14333 options
, RE_SEARCH
, lnum_stop
, &tm
);
14334 if (n
== FAIL
|| (firstpos
.lnum
!= 0 && equalpos(pos
, firstpos
)))
14335 /* didn't find it or found the first match again: FAIL */
14338 if (firstpos
.lnum
== 0)
14340 if (equalpos(pos
, foundpos
))
14342 /* Found the same position again. Can happen with a pattern that
14343 * has "\zs" at the end and searching backwards. Advance one
14344 * character and try again. */
14345 if (dir
== BACKWARD
)
14352 /* clear the start flag to avoid getting stuck here */
14353 options
&= ~SEARCH_START
;
14355 /* If the skip pattern matches, ignore this match. */
14358 save_pos
= curwin
->w_cursor
;
14359 curwin
->w_cursor
= pos
;
14360 r
= eval_to_bool(skip
, &err
, NULL
, FALSE
);
14361 curwin
->w_cursor
= save_pos
;
14364 /* Evaluating {skip} caused an error, break here. */
14365 curwin
->w_cursor
= save_cursor
;
14373 if ((dir
== BACKWARD
&& n
== 3) || (dir
== FORWARD
&& n
== 2))
14375 /* Found end when searching backwards or start when searching
14376 * forward: nested pair. */
14378 pat
= pat2
; /* nested, don't search for middle */
14382 /* Found end when searching forward or start when searching
14383 * backward: end of (nested) pair; or found middle in outer pair. */
14385 pat
= pat3
; /* outer level, search for middle */
14390 /* Found the match: return matchcount or line number. */
14391 if (flags
& SP_RETCOUNT
)
14395 if (flags
& SP_SETPCMARK
)
14397 curwin
->w_cursor
= pos
;
14398 if (!(flags
& SP_REPEAT
))
14400 nest
= 1; /* search for next unmatched */
14404 if (match_pos
!= NULL
)
14406 /* Store the match cursor position */
14407 match_pos
->lnum
= curwin
->w_cursor
.lnum
;
14408 match_pos
->col
= curwin
->w_cursor
.col
+ 1;
14411 /* If 'n' flag is used or search failed: restore cursor position. */
14412 if ((flags
& SP_NOMOVE
) || retval
== 0)
14413 curwin
->w_cursor
= save_cursor
;
14424 * "searchpos()" function
14427 f_searchpos(argvars
, rettv
)
14437 rettv
->vval
.v_number
= 0;
14439 if (rettv_list_alloc(rettv
) == FAIL
)
14442 n
= search_cmn(argvars
, &match_pos
, &flags
);
14445 lnum
= match_pos
.lnum
;
14446 col
= match_pos
.col
;
14449 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
14450 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
14451 if (flags
& SP_SUBPAT
)
14452 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n
);
14458 f_server2client(argvars
, rettv
)
14462 #ifdef FEAT_CLIENTSERVER
14463 char_u buf
[NUMBUFLEN
];
14464 char_u
*server
= get_tv_string_chk(&argvars
[0]);
14465 char_u
*reply
= get_tv_string_buf_chk(&argvars
[1], buf
);
14467 rettv
->vval
.v_number
= -1;
14468 if (server
== NULL
|| reply
== NULL
)
14470 if (check_restricted() || check_secure())
14473 if (check_connection() == FAIL
)
14477 if (serverSendReply(server
, reply
) < 0)
14479 EMSG(_("E258: Unable to send to client"));
14482 rettv
->vval
.v_number
= 0;
14484 rettv
->vval
.v_number
= -1;
14490 f_serverlist(argvars
, rettv
)
14496 #ifdef FEAT_CLIENTSERVER
14498 r
= serverGetVimNames();
14501 if (X_DISPLAY
!= NULL
)
14502 r
= serverGetVimNames(X_DISPLAY
);
14505 rettv
->v_type
= VAR_STRING
;
14506 rettv
->vval
.v_string
= r
;
14510 * "setbufvar()" function
14514 f_setbufvar(argvars
, rettv
)
14520 char_u
*varname
, *bufvarname
;
14522 char_u nbuf
[NUMBUFLEN
];
14524 rettv
->vval
.v_number
= 0;
14526 if (check_restricted() || check_secure())
14528 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
14529 varname
= get_tv_string_chk(&argvars
[1]);
14530 buf
= get_buf_tv(&argvars
[0]);
14531 varp
= &argvars
[2];
14533 if (buf
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
14535 /* set curbuf to be our buf, temporarily */
14536 aucmd_prepbuf(&aco
, buf
);
14538 if (*varname
== '&')
14545 numval
= get_tv_number_chk(varp
, &error
);
14546 strval
= get_tv_string_buf_chk(varp
, nbuf
);
14547 if (!error
&& strval
!= NULL
)
14548 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
14552 bufvarname
= alloc((unsigned)STRLEN(varname
) + 3);
14553 if (bufvarname
!= NULL
)
14555 STRCPY(bufvarname
, "b:");
14556 STRCPY(bufvarname
+ 2, varname
);
14557 set_var(bufvarname
, varp
, TRUE
);
14558 vim_free(bufvarname
);
14562 /* reset notion of buffer */
14563 aucmd_restbuf(&aco
);
14568 * "setcmdpos()" function
14571 f_setcmdpos(argvars
, rettv
)
14575 int pos
= (int)get_tv_number(&argvars
[0]) - 1;
14578 rettv
->vval
.v_number
= set_cmdline_pos(pos
);
14582 * "setline()" function
14585 f_setline(argvars
, rettv
)
14590 char_u
*line
= NULL
;
14592 listitem_T
*li
= NULL
;
14594 linenr_T lcount
= curbuf
->b_ml
.ml_line_count
;
14596 lnum
= get_tv_lnum(&argvars
[0]);
14597 if (argvars
[1].v_type
== VAR_LIST
)
14599 l
= argvars
[1].vval
.v_list
;
14603 line
= get_tv_string_chk(&argvars
[1]);
14605 rettv
->vval
.v_number
= 0; /* OK */
14610 /* list argument, get next string */
14613 line
= get_tv_string_chk(&li
->li_tv
);
14617 rettv
->vval
.v_number
= 1; /* FAIL */
14618 if (line
== NULL
|| lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
14620 if (lnum
<= curbuf
->b_ml
.ml_line_count
)
14622 /* existing line, replace it */
14623 if (u_savesub(lnum
) == OK
&& ml_replace(lnum
, line
, TRUE
) == OK
)
14625 changed_bytes(lnum
, 0);
14626 if (lnum
== curwin
->w_cursor
.lnum
)
14627 check_cursor_col();
14628 rettv
->vval
.v_number
= 0; /* OK */
14631 else if (added
> 0 || u_save(lnum
- 1, lnum
) == OK
)
14633 /* lnum is one past the last line, append the line */
14635 if (ml_append(lnum
- 1, line
, (colnr_T
)0, FALSE
) == OK
)
14636 rettv
->vval
.v_number
= 0; /* OK */
14639 if (l
== NULL
) /* only one string argument */
14645 appended_lines_mark(lcount
, added
);
14649 * Used by "setqflist()" and "setloclist()" functions
14653 set_qf_ll_list(wp
, list_arg
, action_arg
, rettv
)
14655 typval_T
*list_arg
;
14656 typval_T
*action_arg
;
14659 #ifdef FEAT_QUICKFIX
14664 rettv
->vval
.v_number
= -1;
14666 #ifdef FEAT_QUICKFIX
14667 if (list_arg
->v_type
!= VAR_LIST
)
14668 EMSG(_(e_listreq
));
14671 list_T
*l
= list_arg
->vval
.v_list
;
14673 if (action_arg
->v_type
== VAR_STRING
)
14675 act
= get_tv_string_chk(action_arg
);
14677 return; /* type error; errmsg already given */
14678 if (*act
== 'a' || *act
== 'r')
14682 if (l
!= NULL
&& set_errorlist(wp
, l
, action
) == OK
)
14683 rettv
->vval
.v_number
= 0;
14689 * "setloclist()" function
14693 f_setloclist(argvars
, rettv
)
14699 rettv
->vval
.v_number
= -1;
14701 win
= find_win_by_nr(&argvars
[0], NULL
);
14703 set_qf_ll_list(win
, &argvars
[1], &argvars
[2], rettv
);
14707 * "setmatches()" function
14710 f_setmatches(argvars
, rettv
)
14714 #ifdef FEAT_SEARCH_EXTRA
14719 rettv
->vval
.v_number
= -1;
14720 if (argvars
[0].v_type
!= VAR_LIST
)
14722 EMSG(_(e_listreq
));
14725 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
14728 /* To some extent make sure that we are dealing with a list from
14729 * "getmatches()". */
14733 if (li
->li_tv
.v_type
!= VAR_DICT
14734 || (d
= li
->li_tv
.vval
.v_dict
) == NULL
)
14739 if (!(dict_find(d
, (char_u
*)"group", -1) != NULL
14740 && dict_find(d
, (char_u
*)"pattern", -1) != NULL
14741 && dict_find(d
, (char_u
*)"priority", -1) != NULL
14742 && dict_find(d
, (char_u
*)"id", -1) != NULL
))
14750 clear_matches(curwin
);
14754 d
= li
->li_tv
.vval
.v_dict
;
14755 match_add(curwin
, get_dict_string(d
, (char_u
*)"group", FALSE
),
14756 get_dict_string(d
, (char_u
*)"pattern", FALSE
),
14757 (int)get_dict_number(d
, (char_u
*)"priority"),
14758 (int)get_dict_number(d
, (char_u
*)"id"));
14761 rettv
->vval
.v_number
= 0;
14767 * "setpos()" function
14771 f_setpos(argvars
, rettv
)
14779 rettv
->vval
.v_number
= -1;
14780 name
= get_tv_string_chk(argvars
);
14783 if (list2fpos(&argvars
[1], &pos
, &fnum
) == OK
)
14786 if (name
[0] == '.' && name
[1] == NUL
)
14789 if (fnum
== curbuf
->b_fnum
)
14791 curwin
->w_cursor
= pos
;
14793 rettv
->vval
.v_number
= 0;
14798 else if (name
[0] == '\'' && name
[1] != NUL
&& name
[2] == NUL
)
14801 if (setmark_pos(name
[1], &pos
, fnum
) == OK
)
14802 rettv
->vval
.v_number
= 0;
14811 * "setqflist()" function
14815 f_setqflist(argvars
, rettv
)
14819 set_qf_ll_list(NULL
, &argvars
[0], &argvars
[1], rettv
);
14823 * "setreg()" function
14826 f_setreg(argvars
, rettv
)
14831 char_u
*strregname
;
14842 strregname
= get_tv_string_chk(argvars
);
14843 rettv
->vval
.v_number
= 1; /* FAIL is default */
14845 if (strregname
== NULL
)
14846 return; /* type error; errmsg already given */
14847 regname
= *strregname
;
14848 if (regname
== 0 || regname
== '@')
14850 else if (regname
== '=')
14853 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14855 stropt
= get_tv_string_chk(&argvars
[2]);
14856 if (stropt
== NULL
)
14857 return; /* type error */
14858 for (; *stropt
!= NUL
; ++stropt
)
14861 case 'a': case 'A': /* append */
14864 case 'v': case 'c': /* character-wise selection */
14867 case 'V': case 'l': /* line-wise selection */
14871 case 'b': case Ctrl_V
: /* block-wise selection */
14872 yank_type
= MBLOCK
;
14873 if (VIM_ISDIGIT(stropt
[1]))
14876 block_len
= getdigits(&stropt
) - 1;
14884 strval
= get_tv_string_chk(&argvars
[1]);
14885 if (strval
!= NULL
)
14886 write_reg_contents_ex(regname
, strval
, -1,
14887 append
, yank_type
, block_len
);
14888 rettv
->vval
.v_number
= 0;
14892 * "settabwinvar()" function
14895 f_settabwinvar(argvars
, rettv
)
14899 setwinvar(argvars
, rettv
, 1);
14903 * "setwinvar()" function
14906 f_setwinvar(argvars
, rettv
)
14910 setwinvar(argvars
, rettv
, 0);
14914 * "setwinvar()" and "settabwinvar()" functions
14917 setwinvar(argvars
, rettv
, off
)
14923 #ifdef FEAT_WINDOWS
14924 win_T
*save_curwin
;
14925 tabpage_T
*save_curtab
;
14927 char_u
*varname
, *winvarname
;
14929 char_u nbuf
[NUMBUFLEN
];
14932 rettv
->vval
.v_number
= 0;
14934 if (check_restricted() || check_secure())
14937 #ifdef FEAT_WINDOWS
14939 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
14943 win
= find_win_by_nr(&argvars
[off
], tp
);
14944 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
14945 varp
= &argvars
[off
+ 2];
14947 if (win
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
14949 #ifdef FEAT_WINDOWS
14950 /* set curwin to be our win, temporarily */
14951 save_curwin
= curwin
;
14952 save_curtab
= curtab
;
14953 goto_tabpage_tp(tp
);
14954 if (!win_valid(win
))
14957 curbuf
= curwin
->w_buffer
;
14960 if (*varname
== '&')
14967 numval
= get_tv_number_chk(varp
, &error
);
14968 strval
= get_tv_string_buf_chk(varp
, nbuf
);
14969 if (!error
&& strval
!= NULL
)
14970 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
14974 winvarname
= alloc((unsigned)STRLEN(varname
) + 3);
14975 if (winvarname
!= NULL
)
14977 STRCPY(winvarname
, "w:");
14978 STRCPY(winvarname
+ 2, varname
);
14979 set_var(winvarname
, varp
, TRUE
);
14980 vim_free(winvarname
);
14984 #ifdef FEAT_WINDOWS
14985 /* Restore current tabpage and window, if still valid (autocomands can
14986 * make them invalid). */
14987 if (valid_tabpage(save_curtab
))
14988 goto_tabpage_tp(save_curtab
);
14989 if (win_valid(save_curwin
))
14991 curwin
= save_curwin
;
14992 curbuf
= curwin
->w_buffer
;
14999 * "shellescape({string})" function
15002 f_shellescape(argvars
, rettv
)
15006 rettv
->vval
.v_string
= vim_strsave_shellescape(get_tv_string(&argvars
[0]));
15007 rettv
->v_type
= VAR_STRING
;
15011 * "simplify()" function
15014 f_simplify(argvars
, rettv
)
15020 p
= get_tv_string(&argvars
[0]);
15021 rettv
->vval
.v_string
= vim_strsave(p
);
15022 simplify_filename(rettv
->vval
.v_string
); /* simplify in place */
15023 rettv
->v_type
= VAR_STRING
;
15027 #ifdef __BORLANDC__
15030 item_compare
__ARGS((const void *s1
, const void *s2
));
15032 #ifdef __BORLANDC__
15035 item_compare2
__ARGS((const void *s1
, const void *s2
));
15037 static int item_compare_ic
;
15038 static char_u
*item_compare_func
;
15039 static int item_compare_func_err
;
15040 #define ITEM_COMPARE_FAIL 999
15043 * Compare functions for f_sort() below.
15046 #ifdef __BORLANDC__
15049 item_compare(s1
, s2
)
15054 char_u
*tofree1
, *tofree2
;
15056 char_u numbuf1
[NUMBUFLEN
];
15057 char_u numbuf2
[NUMBUFLEN
];
15059 p1
= tv2string(&(*(listitem_T
**)s1
)->li_tv
, &tofree1
, numbuf1
, 0);
15060 p2
= tv2string(&(*(listitem_T
**)s2
)->li_tv
, &tofree2
, numbuf2
, 0);
15065 if (item_compare_ic
)
15066 res
= STRICMP(p1
, p2
);
15068 res
= STRCMP(p1
, p2
);
15075 #ifdef __BORLANDC__
15078 item_compare2(s1
, s2
)
15087 /* shortcut after failure in previous call; compare all items equal */
15088 if (item_compare_func_err
)
15091 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15092 * in the copy without changing the original list items. */
15093 copy_tv(&(*(listitem_T
**)s1
)->li_tv
, &argv
[0]);
15094 copy_tv(&(*(listitem_T
**)s2
)->li_tv
, &argv
[1]);
15096 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
15097 res
= call_func(item_compare_func
, (int)STRLEN(item_compare_func
),
15098 &rettv
, 2, argv
, 0L, 0L, &dummy
, TRUE
, NULL
);
15099 clear_tv(&argv
[0]);
15100 clear_tv(&argv
[1]);
15103 res
= ITEM_COMPARE_FAIL
;
15105 /* return value has wrong type */
15106 res
= get_tv_number_chk(&rettv
, &item_compare_func_err
);
15107 if (item_compare_func_err
)
15108 res
= ITEM_COMPARE_FAIL
;
15114 * "sort({list})" function
15117 f_sort(argvars
, rettv
)
15127 rettv
->vval
.v_number
= 0;
15128 if (argvars
[0].v_type
!= VAR_LIST
)
15129 EMSG2(_(e_listarg
), "sort()");
15132 l
= argvars
[0].vval
.v_list
;
15133 if (l
== NULL
|| tv_check_lock(l
->lv_lock
, (char_u
*)"sort()"))
15135 rettv
->vval
.v_list
= l
;
15136 rettv
->v_type
= VAR_LIST
;
15141 return; /* short list sorts pretty quickly */
15143 item_compare_ic
= FALSE
;
15144 item_compare_func
= NULL
;
15145 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15147 if (argvars
[1].v_type
== VAR_FUNC
)
15148 item_compare_func
= argvars
[1].vval
.v_string
;
15153 i
= get_tv_number_chk(&argvars
[1], &error
);
15155 return; /* type error; errmsg already given */
15157 item_compare_ic
= TRUE
;
15159 item_compare_func
= get_tv_string(&argvars
[1]);
15163 /* Make an array with each entry pointing to an item in the List. */
15164 ptrs
= (listitem_T
**)alloc((int)(len
* sizeof(listitem_T
*)));
15168 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
15171 item_compare_func_err
= FALSE
;
15172 /* test the compare function */
15173 if (item_compare_func
!= NULL
15174 && item_compare2((void *)&ptrs
[0], (void *)&ptrs
[1])
15175 == ITEM_COMPARE_FAIL
)
15176 EMSG(_("E702: Sort compare function failed"));
15179 /* Sort the array with item pointers. */
15180 qsort((void *)ptrs
, (size_t)len
, sizeof(listitem_T
*),
15181 item_compare_func
== NULL
? item_compare
: item_compare2
);
15183 if (!item_compare_func_err
)
15185 /* Clear the List and append the items in the sorted order. */
15186 l
->lv_first
= l
->lv_last
= NULL
;
15188 for (i
= 0; i
< len
; ++i
)
15189 list_append(l
, ptrs
[i
]);
15198 * "soundfold({word})" function
15201 f_soundfold(argvars
, rettv
)
15207 rettv
->v_type
= VAR_STRING
;
15208 s
= get_tv_string(&argvars
[0]);
15210 rettv
->vval
.v_string
= eval_soundfold(s
);
15212 rettv
->vval
.v_string
= vim_strsave(s
);
15217 * "spellbadword()" function
15221 f_spellbadword(argvars
, rettv
)
15225 char_u
*word
= (char_u
*)"";
15226 hlf_T attr
= HLF_COUNT
;
15229 if (rettv_list_alloc(rettv
) == FAIL
)
15233 if (argvars
[0].v_type
== VAR_UNKNOWN
)
15235 /* Find the start and length of the badly spelled word. */
15236 len
= spell_move_to(curwin
, FORWARD
, TRUE
, TRUE
, &attr
);
15238 word
= ml_get_cursor();
15240 else if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
15242 char_u
*str
= get_tv_string_chk(&argvars
[0]);
15247 /* Check the argument for spelling. */
15248 while (*str
!= NUL
)
15250 len
= spell_check(curwin
, str
, &attr
, &capcol
, FALSE
);
15251 if (attr
!= HLF_COUNT
)
15262 list_append_string(rettv
->vval
.v_list
, word
, len
);
15263 list_append_string(rettv
->vval
.v_list
, (char_u
*)(
15264 attr
== HLF_SPB
? "bad" :
15265 attr
== HLF_SPR
? "rare" :
15266 attr
== HLF_SPL
? "local" :
15267 attr
== HLF_SPC
? "caps" :
15272 * "spellsuggest()" function
15276 f_spellsuggest(argvars
, rettv
)
15282 int typeerr
= FALSE
;
15287 int need_capital
= FALSE
;
15290 if (rettv_list_alloc(rettv
) == FAIL
)
15294 if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
15296 str
= get_tv_string(&argvars
[0]);
15297 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15299 maxcount
= get_tv_number_chk(&argvars
[1], &typeerr
);
15302 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15304 need_capital
= get_tv_number_chk(&argvars
[2], &typeerr
);
15312 spell_suggest_list(&ga
, str
, maxcount
, need_capital
, FALSE
);
15314 for (i
= 0; i
< ga
.ga_len
; ++i
)
15316 str
= ((char_u
**)ga
.ga_data
)[i
];
15318 li
= listitem_alloc();
15323 li
->li_tv
.v_type
= VAR_STRING
;
15324 li
->li_tv
.v_lock
= 0;
15325 li
->li_tv
.vval
.v_string
= str
;
15326 list_append(rettv
->vval
.v_list
, li
);
15335 f_split(argvars
, rettv
)
15341 char_u
*pat
= NULL
;
15342 regmatch_T regmatch
;
15343 char_u patbuf
[NUMBUFLEN
];
15347 int keepempty
= FALSE
;
15348 int typeerr
= FALSE
;
15350 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15352 p_cpo
= (char_u
*)"";
15354 str
= get_tv_string(&argvars
[0]);
15355 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15357 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
15360 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15361 keepempty
= get_tv_number_chk(&argvars
[2], &typeerr
);
15363 if (pat
== NULL
|| *pat
== NUL
)
15364 pat
= (char_u
*)"[\\x01- ]\\+";
15366 if (rettv_list_alloc(rettv
) == FAIL
)
15371 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
15372 if (regmatch
.regprog
!= NULL
)
15374 regmatch
.rm_ic
= FALSE
;
15375 while (*str
!= NUL
|| keepempty
)
15378 match
= FALSE
; /* empty item at the end */
15380 match
= vim_regexec_nl(®match
, str
, col
);
15382 end
= regmatch
.startp
[0];
15384 end
= str
+ STRLEN(str
);
15385 if (keepempty
|| end
> str
|| (rettv
->vval
.v_list
->lv_len
> 0
15386 && *str
!= NUL
&& match
&& end
< regmatch
.endp
[0]))
15388 if (list_append_string(rettv
->vval
.v_list
, str
,
15389 (int)(end
- str
)) == FAIL
)
15394 /* Advance to just after the match. */
15395 if (regmatch
.endp
[0] > str
)
15399 /* Don't get stuck at the same match. */
15401 col
= (*mb_ptr2len
)(regmatch
.endp
[0]);
15406 str
= regmatch
.endp
[0];
15409 vim_free(regmatch
.regprog
);
15416 * "str2nr()" function
15419 f_str2nr(argvars
, rettv
)
15427 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15429 base
= get_tv_number(&argvars
[1]);
15430 if (base
!= 8 && base
!= 10 && base
!= 16)
15437 p
= skipwhite(get_tv_string(&argvars
[0]));
15438 vim_str2nr(p
, NULL
, NULL
, base
== 8 ? 2 : 0, base
== 16 ? 2 : 0, &n
, NULL
);
15439 rettv
->vval
.v_number
= n
;
15442 #ifdef HAVE_STRFTIME
15444 * "strftime({format}[, {time}])" function
15447 f_strftime(argvars
, rettv
)
15451 char_u result_buf
[256];
15452 struct tm
*curtime
;
15456 rettv
->v_type
= VAR_STRING
;
15458 p
= get_tv_string(&argvars
[0]);
15459 if (argvars
[1].v_type
== VAR_UNKNOWN
)
15460 seconds
= time(NULL
);
15462 seconds
= (time_t)get_tv_number(&argvars
[1]);
15463 curtime
= localtime(&seconds
);
15464 /* MSVC returns NULL for an invalid value of seconds. */
15465 if (curtime
== NULL
)
15466 rettv
->vval
.v_string
= vim_strsave((char_u
*)_("(Invalid)"));
15473 conv
.vc_type
= CONV_NONE
;
15474 enc
= enc_locale();
15475 convert_setup(&conv
, p_enc
, enc
);
15476 if (conv
.vc_type
!= CONV_NONE
)
15477 p
= string_convert(&conv
, p
, NULL
);
15480 (void)strftime((char *)result_buf
, sizeof(result_buf
),
15481 (char *)p
, curtime
);
15483 result_buf
[0] = NUL
;
15486 if (conv
.vc_type
!= CONV_NONE
)
15488 convert_setup(&conv
, enc
, p_enc
);
15489 if (conv
.vc_type
!= CONV_NONE
)
15490 rettv
->vval
.v_string
= string_convert(&conv
, result_buf
, NULL
);
15493 rettv
->vval
.v_string
= vim_strsave(result_buf
);
15496 /* Release conversion descriptors */
15497 convert_setup(&conv
, NULL
, NULL
);
15505 * "stridx()" function
15508 f_stridx(argvars
, rettv
)
15512 char_u buf
[NUMBUFLEN
];
15515 char_u
*save_haystack
;
15519 needle
= get_tv_string_chk(&argvars
[1]);
15520 save_haystack
= haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
15521 rettv
->vval
.v_number
= -1;
15522 if (needle
== NULL
|| haystack
== NULL
)
15523 return; /* type error; errmsg already given */
15525 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15529 start_idx
= get_tv_number_chk(&argvars
[2], &error
);
15530 if (error
|| start_idx
>= (int)STRLEN(haystack
))
15532 if (start_idx
>= 0)
15533 haystack
+= start_idx
;
15536 pos
= (char_u
*)strstr((char *)haystack
, (char *)needle
);
15538 rettv
->vval
.v_number
= (varnumber_T
)(pos
- save_haystack
);
15542 * "string()" function
15545 f_string(argvars
, rettv
)
15550 char_u numbuf
[NUMBUFLEN
];
15552 rettv
->v_type
= VAR_STRING
;
15553 rettv
->vval
.v_string
= tv2string(&argvars
[0], &tofree
, numbuf
, 0);
15554 /* Make a copy if we have a value but it's not in allocate memory. */
15555 if (rettv
->vval
.v_string
!= NULL
&& tofree
== NULL
)
15556 rettv
->vval
.v_string
= vim_strsave(rettv
->vval
.v_string
);
15560 * "strlen()" function
15563 f_strlen(argvars
, rettv
)
15567 rettv
->vval
.v_number
= (varnumber_T
)(STRLEN(
15568 get_tv_string(&argvars
[0])));
15572 * "strpart()" function
15575 f_strpart(argvars
, rettv
)
15585 p
= get_tv_string(&argvars
[0]);
15586 slen
= (int)STRLEN(p
);
15588 n
= get_tv_number_chk(&argvars
[1], &error
);
15591 else if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15592 len
= get_tv_number(&argvars
[2]);
15594 len
= slen
- n
; /* default len: all bytes that are available. */
15597 * Only return the overlap between the specified part and the actual
15609 else if (n
+ len
> slen
)
15612 rettv
->v_type
= VAR_STRING
;
15613 rettv
->vval
.v_string
= vim_strnsave(p
+ n
, len
);
15617 * "strridx()" function
15620 f_strridx(argvars
, rettv
)
15624 char_u buf
[NUMBUFLEN
];
15628 char_u
*lastmatch
= NULL
;
15629 int haystack_len
, end_idx
;
15631 needle
= get_tv_string_chk(&argvars
[1]);
15632 haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
15634 rettv
->vval
.v_number
= -1;
15635 if (needle
== NULL
|| haystack
== NULL
)
15636 return; /* type error; errmsg already given */
15638 haystack_len
= (int)STRLEN(haystack
);
15639 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15641 /* Third argument: upper limit for index */
15642 end_idx
= get_tv_number_chk(&argvars
[2], NULL
);
15644 return; /* can never find a match */
15647 end_idx
= haystack_len
;
15649 if (*needle
== NUL
)
15651 /* Empty string matches past the end. */
15652 lastmatch
= haystack
+ end_idx
;
15656 for (rest
= haystack
; *rest
!= '\0'; ++rest
)
15658 rest
= (char_u
*)strstr((char *)rest
, (char *)needle
);
15659 if (rest
== NULL
|| rest
> haystack
+ end_idx
)
15665 if (lastmatch
== NULL
)
15666 rettv
->vval
.v_number
= -1;
15668 rettv
->vval
.v_number
= (varnumber_T
)(lastmatch
- haystack
);
15672 * "strtrans()" function
15675 f_strtrans(argvars
, rettv
)
15679 rettv
->v_type
= VAR_STRING
;
15680 rettv
->vval
.v_string
= transstr(get_tv_string(&argvars
[0]));
15684 * "submatch()" function
15687 f_submatch(argvars
, rettv
)
15691 rettv
->v_type
= VAR_STRING
;
15692 rettv
->vval
.v_string
=
15693 reg_submatch((int)get_tv_number_chk(&argvars
[0], NULL
));
15697 * "substitute()" function
15700 f_substitute(argvars
, rettv
)
15704 char_u patbuf
[NUMBUFLEN
];
15705 char_u subbuf
[NUMBUFLEN
];
15706 char_u flagsbuf
[NUMBUFLEN
];
15708 char_u
*str
= get_tv_string_chk(&argvars
[0]);
15709 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
15710 char_u
*sub
= get_tv_string_buf_chk(&argvars
[2], subbuf
);
15711 char_u
*flg
= get_tv_string_buf_chk(&argvars
[3], flagsbuf
);
15713 rettv
->v_type
= VAR_STRING
;
15714 if (str
== NULL
|| pat
== NULL
|| sub
== NULL
|| flg
== NULL
)
15715 rettv
->vval
.v_string
= NULL
;
15717 rettv
->vval
.v_string
= do_string_sub(str
, pat
, sub
, flg
);
15721 * "synID(lnum, col, trans)" function
15725 f_synID(argvars
, rettv
)
15734 int transerr
= FALSE
;
15736 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
15737 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
15738 trans
= get_tv_number_chk(&argvars
[2], &transerr
);
15740 if (!transerr
&& lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
15741 && col
>= 0 && col
< (long)STRLEN(ml_get(lnum
)))
15742 id
= syn_get_id(curwin
, lnum
, (colnr_T
)col
, trans
, NULL
, FALSE
);
15745 rettv
->vval
.v_number
= id
;
15749 * "synIDattr(id, what [, mode])" function
15753 f_synIDattr(argvars
, rettv
)
15762 char_u modebuf
[NUMBUFLEN
];
15765 id
= get_tv_number(&argvars
[0]);
15766 what
= get_tv_string(&argvars
[1]);
15767 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15769 mode
= get_tv_string_buf(&argvars
[2], modebuf
);
15770 modec
= TOLOWER_ASC(mode
[0]);
15771 if (modec
!= 't' && modec
!= 'c'
15776 modec
= 0; /* replace invalid with current */
15792 switch (TOLOWER_ASC(what
[0]))
15795 if (TOLOWER_ASC(what
[1]) == 'g') /* bg[#] */
15796 p
= highlight_color(id
, what
, modec
);
15798 p
= highlight_has_attr(id
, HL_BOLD
, modec
);
15801 case 'f': /* fg[#] */
15802 p
= highlight_color(id
, what
, modec
);
15806 if (TOLOWER_ASC(what
[1]) == 'n') /* inverse */
15807 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
15809 p
= highlight_has_attr(id
, HL_ITALIC
, modec
);
15812 case 'n': /* name */
15813 p
= get_highlight_name(NULL
, id
- 1);
15816 case 'r': /* reverse */
15817 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
15820 case 's': /* standout */
15821 p
= highlight_has_attr(id
, HL_STANDOUT
, modec
);
15825 if (STRLEN(what
) <= 5 || TOLOWER_ASC(what
[5]) != 'c')
15827 p
= highlight_has_attr(id
, HL_UNDERLINE
, modec
);
15830 p
= highlight_has_attr(id
, HL_UNDERCURL
, modec
);
15835 p
= vim_strsave(p
);
15837 rettv
->v_type
= VAR_STRING
;
15838 rettv
->vval
.v_string
= p
;
15842 * "synIDtrans(id)" function
15846 f_synIDtrans(argvars
, rettv
)
15853 id
= get_tv_number(&argvars
[0]);
15856 id
= syn_get_final_id(id
);
15861 rettv
->vval
.v_number
= id
;
15865 * "synstack(lnum, col)" function
15869 f_synstack(argvars
, rettv
)
15880 rettv
->v_type
= VAR_LIST
;
15881 rettv
->vval
.v_list
= NULL
;
15884 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
15885 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
15887 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
15888 && col
>= 0 && col
< (long)STRLEN(ml_get(lnum
))
15889 && rettv_list_alloc(rettv
) != FAIL
)
15891 (void)syn_get_id(curwin
, lnum
, (colnr_T
)col
, FALSE
, NULL
, TRUE
);
15894 id
= syn_get_stack_item(i
);
15897 if (list_append_number(rettv
->vval
.v_list
, id
) == FAIL
)
15905 * "system()" function
15908 f_system(argvars
, rettv
)
15912 char_u
*res
= NULL
;
15914 char_u
*infile
= NULL
;
15915 char_u buf
[NUMBUFLEN
];
15919 if (check_restricted() || check_secure())
15922 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15925 * Write the string to a temp file, to be used for input of the shell
15928 if ((infile
= vim_tempname('i')) == NULL
)
15934 fd
= mch_fopen((char *)infile
, WRITEBIN
);
15937 EMSG2(_(e_notopen
), infile
);
15940 p
= get_tv_string_buf_chk(&argvars
[1], buf
);
15944 goto done
; /* type error; errmsg already given */
15946 if (fwrite(p
, STRLEN(p
), 1, fd
) != 1)
15948 if (fclose(fd
) != 0)
15952 EMSG(_("E677: Error writing temp file"));
15957 res
= get_cmd_output(get_tv_string(&argvars
[0]), infile
,
15958 SHELL_SILENT
| SHELL_COOKED
);
15961 /* translate <CR> into <NL> */
15966 for (s
= res
; *s
; ++s
)
15974 /* translate <CR><NL> into <NL> */
15980 for (s
= res
; *s
; ++s
)
15982 if (s
[0] == CAR
&& s
[1] == NL
)
15992 if (infile
!= NULL
)
15994 mch_remove(infile
);
15997 rettv
->v_type
= VAR_STRING
;
15998 rettv
->vval
.v_string
= res
;
16002 * "tabpagebuflist()" function
16006 f_tabpagebuflist(argvars
, rettv
)
16010 #ifndef FEAT_WINDOWS
16011 rettv
->vval
.v_number
= 0;
16016 if (argvars
[0].v_type
== VAR_UNKNOWN
)
16020 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
16022 wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
16025 rettv
->vval
.v_number
= 0;
16028 if (rettv_list_alloc(rettv
) == FAIL
)
16029 rettv
->vval
.v_number
= 0;
16032 for (; wp
!= NULL
; wp
= wp
->w_next
)
16033 if (list_append_number(rettv
->vval
.v_list
,
16034 wp
->w_buffer
->b_fnum
) == FAIL
)
16043 * "tabpagenr()" function
16047 f_tabpagenr(argvars
, rettv
)
16052 #ifdef FEAT_WINDOWS
16055 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
16057 arg
= get_tv_string_chk(&argvars
[0]);
16061 if (STRCMP(arg
, "$") == 0)
16062 nr
= tabpage_index(NULL
) - 1;
16064 EMSG2(_(e_invexpr2
), arg
);
16068 nr
= tabpage_index(curtab
);
16070 rettv
->vval
.v_number
= nr
;
16074 #ifdef FEAT_WINDOWS
16075 static int get_winnr
__ARGS((tabpage_T
*tp
, typval_T
*argvar
));
16078 * Common code for tabpagewinnr() and winnr().
16081 get_winnr(tp
, argvar
)
16090 twin
= (tp
== curtab
) ? curwin
: tp
->tp_curwin
;
16091 if (argvar
->v_type
!= VAR_UNKNOWN
)
16093 arg
= get_tv_string_chk(argvar
);
16095 nr
= 0; /* type error; errmsg already given */
16096 else if (STRCMP(arg
, "$") == 0)
16097 twin
= (tp
== curtab
) ? lastwin
: tp
->tp_lastwin
;
16098 else if (STRCMP(arg
, "#") == 0)
16100 twin
= (tp
== curtab
) ? prevwin
: tp
->tp_prevwin
;
16106 EMSG2(_(e_invexpr2
), arg
);
16112 for (wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
16113 wp
!= twin
; wp
= wp
->w_next
)
16117 /* didn't find it in this tabpage */
16128 * "tabpagewinnr()" function
16132 f_tabpagewinnr(argvars
, rettv
)
16137 #ifdef FEAT_WINDOWS
16140 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
16144 nr
= get_winnr(tp
, &argvars
[1]);
16146 rettv
->vval
.v_number
= nr
;
16151 * "tagfiles()" function
16155 f_tagfiles(argvars
, rettv
)
16159 char_u fname
[MAXPATHL
+ 1];
16163 if (rettv_list_alloc(rettv
) == FAIL
)
16165 rettv
->vval
.v_number
= 0;
16169 for (first
= TRUE
; ; first
= FALSE
)
16170 if (get_tagfname(&tn
, first
, fname
) == FAIL
16171 || list_append_string(rettv
->vval
.v_list
, fname
, -1) == FAIL
)
16177 * "taglist()" function
16180 f_taglist(argvars
, rettv
)
16184 char_u
*tag_pattern
;
16186 tag_pattern
= get_tv_string(&argvars
[0]);
16188 rettv
->vval
.v_number
= FALSE
;
16189 if (*tag_pattern
== NUL
)
16192 if (rettv_list_alloc(rettv
) == OK
)
16193 (void)get_tags(rettv
->vval
.v_list
, tag_pattern
);
16197 * "tempname()" function
16201 f_tempname(argvars
, rettv
)
16205 static int x
= 'A';
16207 rettv
->v_type
= VAR_STRING
;
16208 rettv
->vval
.v_string
= vim_tempname(x
);
16210 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16211 * names. Skip 'I' and 'O', they are used for shell redirection. */
16229 } while (x
== 'I' || x
== 'O');
16233 * "test(list)" function: Just checking the walls...
16237 f_test(argvars
, rettv
)
16241 /* Used for unit testing. Change the code below to your liking. */
16245 char_u
*bad
, *good
;
16247 if (argvars
[0].v_type
!= VAR_LIST
)
16249 l
= argvars
[0].vval
.v_list
;
16255 bad
= get_tv_string(&li
->li_tv
);
16259 good
= get_tv_string(&li
->li_tv
);
16260 rettv
->vval
.v_number
= test_edit_score(bad
, good
);
16265 * "tolower(string)" function
16268 f_tolower(argvars
, rettv
)
16274 p
= vim_strsave(get_tv_string(&argvars
[0]));
16275 rettv
->v_type
= VAR_STRING
;
16276 rettv
->vval
.v_string
= p
;
16288 c
= utf_ptr2char(p
);
16289 lc
= utf_tolower(c
);
16290 l
= utf_ptr2len(p
);
16291 /* TODO: reallocate string when byte count changes. */
16292 if (utf_char2len(lc
) == l
)
16293 utf_char2bytes(lc
, p
);
16296 else if (has_mbyte
&& (l
= (*mb_ptr2len
)(p
)) > 1)
16297 p
+= l
; /* skip multi-byte character */
16301 *p
= TOLOWER_LOC(*p
); /* note that tolower() can be a macro */
16308 * "toupper(string)" function
16311 f_toupper(argvars
, rettv
)
16315 rettv
->v_type
= VAR_STRING
;
16316 rettv
->vval
.v_string
= strup_save(get_tv_string(&argvars
[0]));
16320 * "tr(string, fromstr, tostr)" function
16323 f_tr(argvars
, rettv
)
16340 char_u buf
[NUMBUFLEN
];
16341 char_u buf2
[NUMBUFLEN
];
16344 instr
= get_tv_string(&argvars
[0]);
16345 fromstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
16346 tostr
= get_tv_string_buf_chk(&argvars
[2], buf2
);
16348 /* Default return value: empty string. */
16349 rettv
->v_type
= VAR_STRING
;
16350 rettv
->vval
.v_string
= NULL
;
16351 if (fromstr
== NULL
|| tostr
== NULL
)
16352 return; /* type error; errmsg already given */
16353 ga_init2(&ga
, (int)sizeof(char), 80);
16358 /* not multi-byte: fromstr and tostr must be the same length */
16359 if (STRLEN(fromstr
) != STRLEN(tostr
))
16364 EMSG2(_(e_invarg2
), fromstr
);
16369 /* fromstr and tostr have to contain the same number of chars */
16370 while (*instr
!= NUL
)
16375 inlen
= (*mb_ptr2len
)(instr
);
16379 for (p
= fromstr
; *p
!= NUL
; p
+= fromlen
)
16381 fromlen
= (*mb_ptr2len
)(p
);
16382 if (fromlen
== inlen
&& STRNCMP(instr
, p
, inlen
) == 0)
16384 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
16386 tolen
= (*mb_ptr2len
)(p
);
16394 if (*p
== NUL
) /* tostr is shorter than fromstr */
16401 if (first
&& cpstr
== instr
)
16403 /* Check that fromstr and tostr have the same number of
16404 * (multi-byte) characters. Done only once when a character
16405 * of instr doesn't appear in fromstr. */
16407 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
16409 tolen
= (*mb_ptr2len
)(p
);
16416 ga_grow(&ga
, cplen
);
16417 mch_memmove((char *)ga
.ga_data
+ ga
.ga_len
, cpstr
, (size_t)cplen
);
16418 ga
.ga_len
+= cplen
;
16425 /* When not using multi-byte chars we can do it faster. */
16426 p
= vim_strchr(fromstr
, *instr
);
16428 ga_append(&ga
, tostr
[p
- fromstr
]);
16430 ga_append(&ga
, *instr
);
16435 /* add a terminating NUL */
16437 ga_append(&ga
, NUL
);
16439 rettv
->vval
.v_string
= ga
.ga_data
;
16443 * "type(expr)" function
16446 f_type(argvars
, rettv
)
16452 switch (argvars
[0].v_type
)
16454 case VAR_NUMBER
: n
= 0; break;
16455 case VAR_STRING
: n
= 1; break;
16456 case VAR_FUNC
: n
= 2; break;
16457 case VAR_LIST
: n
= 3; break;
16458 case VAR_DICT
: n
= 4; break;
16459 default: EMSG2(_(e_intern2
), "f_type()"); n
= 0; break;
16461 rettv
->vval
.v_number
= n
;
16465 * "values(dict)" function
16468 f_values(argvars
, rettv
)
16472 dict_list(argvars
, rettv
, 1);
16476 * "virtcol(string)" function
16479 f_virtcol(argvars
, rettv
)
16485 int fnum
= curbuf
->b_fnum
;
16487 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
16488 if (fp
!= NULL
&& fp
->lnum
<= curbuf
->b_ml
.ml_line_count
16489 && fnum
== curbuf
->b_fnum
)
16491 getvvcol(curwin
, fp
, NULL
, NULL
, &vcol
);
16495 rettv
->vval
.v_number
= vcol
;
16499 * "visualmode()" function
16503 f_visualmode(argvars
, rettv
)
16510 rettv
->v_type
= VAR_STRING
;
16511 str
[0] = curbuf
->b_visual_mode_eval
;
16513 rettv
->vval
.v_string
= vim_strsave(str
);
16515 /* A non-zero number or non-empty string argument: reset mode. */
16516 if ((argvars
[0].v_type
== VAR_NUMBER
16517 && argvars
[0].vval
.v_number
!= 0)
16518 || (argvars
[0].v_type
== VAR_STRING
16519 && *get_tv_string(&argvars
[0]) != NUL
))
16520 curbuf
->b_visual_mode_eval
= NUL
;
16522 rettv
->vval
.v_number
= 0; /* return anything, it won't work anyway */
16527 * "winbufnr(nr)" function
16530 f_winbufnr(argvars
, rettv
)
16536 wp
= find_win_by_nr(&argvars
[0], NULL
);
16538 rettv
->vval
.v_number
= -1;
16540 rettv
->vval
.v_number
= wp
->w_buffer
->b_fnum
;
16544 * "wincol()" function
16548 f_wincol(argvars
, rettv
)
16553 rettv
->vval
.v_number
= curwin
->w_wcol
+ 1;
16557 * "winheight(nr)" function
16560 f_winheight(argvars
, rettv
)
16566 wp
= find_win_by_nr(&argvars
[0], NULL
);
16568 rettv
->vval
.v_number
= -1;
16570 rettv
->vval
.v_number
= wp
->w_height
;
16574 * "winline()" function
16578 f_winline(argvars
, rettv
)
16583 rettv
->vval
.v_number
= curwin
->w_wrow
+ 1;
16587 * "winnr()" function
16591 f_winnr(argvars
, rettv
)
16597 #ifdef FEAT_WINDOWS
16598 nr
= get_winnr(curtab
, &argvars
[0]);
16600 rettv
->vval
.v_number
= nr
;
16604 * "winrestcmd()" function
16608 f_winrestcmd(argvars
, rettv
)
16612 #ifdef FEAT_WINDOWS
16618 ga_init2(&ga
, (int)sizeof(char), 70);
16619 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
16621 sprintf((char *)buf
, "%dresize %d|", winnr
, wp
->w_height
);
16622 ga_concat(&ga
, buf
);
16623 # ifdef FEAT_VERTSPLIT
16624 sprintf((char *)buf
, "vert %dresize %d|", winnr
, wp
->w_width
);
16625 ga_concat(&ga
, buf
);
16629 ga_append(&ga
, NUL
);
16631 rettv
->vval
.v_string
= ga
.ga_data
;
16633 rettv
->vval
.v_string
= NULL
;
16635 rettv
->v_type
= VAR_STRING
;
16639 * "winrestview()" function
16643 f_winrestview(argvars
, rettv
)
16649 if (argvars
[0].v_type
!= VAR_DICT
16650 || (dict
= argvars
[0].vval
.v_dict
) == NULL
)
16654 curwin
->w_cursor
.lnum
= get_dict_number(dict
, (char_u
*)"lnum");
16655 curwin
->w_cursor
.col
= get_dict_number(dict
, (char_u
*)"col");
16656 #ifdef FEAT_VIRTUALEDIT
16657 curwin
->w_cursor
.coladd
= get_dict_number(dict
, (char_u
*)"coladd");
16659 curwin
->w_curswant
= get_dict_number(dict
, (char_u
*)"curswant");
16660 curwin
->w_set_curswant
= FALSE
;
16662 set_topline(curwin
, get_dict_number(dict
, (char_u
*)"topline"));
16664 curwin
->w_topfill
= get_dict_number(dict
, (char_u
*)"topfill");
16666 curwin
->w_leftcol
= get_dict_number(dict
, (char_u
*)"leftcol");
16667 curwin
->w_skipcol
= get_dict_number(dict
, (char_u
*)"skipcol");
16670 changed_cline_bef_curs();
16671 invalidate_botline();
16672 redraw_later(VALID
);
16674 if (curwin
->w_topline
== 0)
16675 curwin
->w_topline
= 1;
16676 if (curwin
->w_topline
> curbuf
->b_ml
.ml_line_count
)
16677 curwin
->w_topline
= curbuf
->b_ml
.ml_line_count
;
16679 check_topfill(curwin
, TRUE
);
16685 * "winsaveview()" function
16689 f_winsaveview(argvars
, rettv
)
16695 dict
= dict_alloc();
16698 rettv
->v_type
= VAR_DICT
;
16699 rettv
->vval
.v_dict
= dict
;
16700 ++dict
->dv_refcount
;
16702 dict_add_nr_str(dict
, "lnum", (long)curwin
->w_cursor
.lnum
, NULL
);
16703 dict_add_nr_str(dict
, "col", (long)curwin
->w_cursor
.col
, NULL
);
16704 #ifdef FEAT_VIRTUALEDIT
16705 dict_add_nr_str(dict
, "coladd", (long)curwin
->w_cursor
.coladd
, NULL
);
16708 dict_add_nr_str(dict
, "curswant", (long)curwin
->w_curswant
, NULL
);
16710 dict_add_nr_str(dict
, "topline", (long)curwin
->w_topline
, NULL
);
16712 dict_add_nr_str(dict
, "topfill", (long)curwin
->w_topfill
, NULL
);
16714 dict_add_nr_str(dict
, "leftcol", (long)curwin
->w_leftcol
, NULL
);
16715 dict_add_nr_str(dict
, "skipcol", (long)curwin
->w_skipcol
, NULL
);
16719 * "winwidth(nr)" function
16722 f_winwidth(argvars
, rettv
)
16728 wp
= find_win_by_nr(&argvars
[0], NULL
);
16730 rettv
->vval
.v_number
= -1;
16732 #ifdef FEAT_VERTSPLIT
16733 rettv
->vval
.v_number
= wp
->w_width
;
16735 rettv
->vval
.v_number
= Columns
;
16740 * "writefile()" function
16743 f_writefile(argvars
, rettv
)
16747 int binary
= FALSE
;
16755 if (check_restricted() || check_secure())
16758 if (argvars
[0].v_type
!= VAR_LIST
)
16760 EMSG2(_(e_listarg
), "writefile()");
16763 if (argvars
[0].vval
.v_list
== NULL
)
16766 if (argvars
[2].v_type
!= VAR_UNKNOWN
16767 && STRCMP(get_tv_string(&argvars
[2]), "b") == 0)
16770 /* Always open the file in binary mode, library functions have a mind of
16771 * their own about CR-LF conversion. */
16772 fname
= get_tv_string(&argvars
[1]);
16773 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, WRITEBIN
)) == NULL
)
16775 EMSG2(_(e_notcreate
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
16780 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
;
16783 for (s
= get_tv_string(&li
->li_tv
); *s
!= NUL
; ++s
)
16795 if (!binary
|| li
->li_next
!= NULL
)
16796 if (putc('\n', fd
) == EOF
)
16810 rettv
->vval
.v_number
= ret
;
16814 * Translate a String variable into a position.
16815 * Returns NULL when there is an error.
16818 var2fpos(varp
, dollar_lnum
, fnum
)
16820 int dollar_lnum
; /* TRUE when $ is last line */
16821 int *fnum
; /* set to fnum for '0, 'A, etc. */
16827 /* Argument can be [lnum, col, coladd]. */
16828 if (varp
->v_type
== VAR_LIST
)
16835 l
= varp
->vval
.v_list
;
16839 /* Get the line number */
16840 pos
.lnum
= list_find_nr(l
, 0L, &error
);
16841 if (error
|| pos
.lnum
<= 0 || pos
.lnum
> curbuf
->b_ml
.ml_line_count
)
16842 return NULL
; /* invalid line number */
16844 /* Get the column number */
16845 pos
.col
= list_find_nr(l
, 1L, &error
);
16848 len
= (long)STRLEN(ml_get(pos
.lnum
));
16850 /* We accept "$" for the column number: last column. */
16851 li
= list_find(l
, 1L);
16852 if (li
!= NULL
&& li
->li_tv
.v_type
== VAR_STRING
16853 && li
->li_tv
.vval
.v_string
!= NULL
16854 && STRCMP(li
->li_tv
.vval
.v_string
, "$") == 0)
16857 /* Accept a position up to the NUL after the line. */
16858 if (pos
.col
== 0 || (int)pos
.col
> len
+ 1)
16859 return NULL
; /* invalid column number */
16862 #ifdef FEAT_VIRTUALEDIT
16863 /* Get the virtual offset. Defaults to zero. */
16864 pos
.coladd
= list_find_nr(l
, 2L, &error
);
16872 name
= get_tv_string_chk(varp
);
16875 if (name
[0] == '.') /* cursor */
16876 return &curwin
->w_cursor
;
16877 if (name
[0] == '\'') /* mark */
16879 pp
= getmark_fnum(name
[1], FALSE
, fnum
);
16880 if (pp
== NULL
|| pp
== (pos_T
*)-1 || pp
->lnum
<= 0)
16885 #ifdef FEAT_VIRTUALEDIT
16889 if (name
[0] == 'w' && dollar_lnum
)
16892 if (name
[1] == '0') /* "w0": first visible line */
16895 pos
.lnum
= curwin
->w_topline
;
16898 else if (name
[1] == '$') /* "w$": last visible line */
16900 validate_botline();
16901 pos
.lnum
= curwin
->w_botline
- 1;
16905 else if (name
[0] == '$') /* last column or line */
16909 pos
.lnum
= curbuf
->b_ml
.ml_line_count
;
16914 pos
.lnum
= curwin
->w_cursor
.lnum
;
16915 pos
.col
= (colnr_T
)STRLEN(ml_get_curline());
16923 * Convert list in "arg" into a position and optional file number.
16924 * When "fnump" is NULL there is no file number, only 3 items.
16925 * Note that the column is passed on as-is, the caller may want to decrement
16926 * it to use 1 for the first column.
16927 * Return FAIL when conversion is not possible, doesn't check the position for
16931 list2fpos(arg
, posp
, fnump
)
16936 list_T
*l
= arg
->vval
.v_list
;
16940 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16941 * when "fnump" isn't NULL and "coladd" is optional. */
16942 if (arg
->v_type
!= VAR_LIST
16944 || l
->lv_len
< (fnump
== NULL
? 2 : 3)
16945 || l
->lv_len
> (fnump
== NULL
? 3 : 4))
16950 n
= list_find_nr(l
, i
++, NULL
); /* fnum */
16954 n
= curbuf
->b_fnum
; /* current buffer */
16958 n
= list_find_nr(l
, i
++, NULL
); /* lnum */
16963 n
= list_find_nr(l
, i
++, NULL
); /* col */
16968 #ifdef FEAT_VIRTUALEDIT
16969 n
= list_find_nr(l
, i
, NULL
);
16980 * Get the length of an environment variable name.
16981 * Advance "arg" to the first character after the name.
16982 * Return 0 for error.
16991 for (p
= *arg
; vim_isIDc(*p
); ++p
)
16993 if (p
== *arg
) /* no name found */
16996 len
= (int)(p
- *arg
);
17002 * Get the length of the name of a function or internal variable.
17003 * "arg" is advanced to the first non-white character after the name.
17004 * Return 0 if something is wrong.
17013 /* Find the end of the name. */
17014 for (p
= *arg
; eval_isnamec(*p
); ++p
)
17016 if (p
== *arg
) /* no name found */
17019 len
= (int)(p
- *arg
);
17020 *arg
= skipwhite(p
);
17026 * Get the length of the name of a variable or function.
17027 * Only the name is recognized, does not handle ".key" or "[idx]".
17028 * "arg" is advanced to the first non-white character after the name.
17029 * Return -1 if curly braces expansion failed.
17030 * Return 0 if something else is wrong.
17031 * If the name contains 'magic' {}'s, expand them and return the
17032 * expanded name in an allocated string via 'alias' - caller must free.
17035 get_name_len(arg
, alias
, evaluate
, verbose
)
17043 char_u
*expr_start
;
17046 *alias
= NULL
; /* default to no alias */
17048 if ((*arg
)[0] == K_SPECIAL
&& (*arg
)[1] == KS_EXTRA
17049 && (*arg
)[2] == (int)KE_SNR
)
17051 /* hard coded <SNR>, already translated */
17053 return get_id_len(arg
) + 3;
17055 len
= eval_fname_script(*arg
);
17058 /* literal "<SID>", "s:" or "<SNR>" */
17063 * Find the end of the name; check for {} construction.
17065 p
= find_name_end(*arg
, &expr_start
, &expr_end
,
17066 len
> 0 ? 0 : FNE_CHECK_START
);
17067 if (expr_start
!= NULL
)
17069 char_u
*temp_string
;
17073 len
+= (int)(p
- *arg
);
17074 *arg
= skipwhite(p
);
17079 * Include any <SID> etc in the expanded string:
17080 * Thus the -len here.
17082 temp_string
= make_expanded_name(*arg
- len
, expr_start
, expr_end
, p
);
17083 if (temp_string
== NULL
)
17085 *alias
= temp_string
;
17086 *arg
= skipwhite(p
);
17087 return (int)STRLEN(temp_string
);
17090 len
+= get_id_len(arg
);
17091 if (len
== 0 && verbose
)
17092 EMSG2(_(e_invexpr2
), *arg
);
17098 * Find the end of a variable or function name, taking care of magic braces.
17099 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17100 * start and end of the first magic braces item.
17101 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
17102 * Return a pointer to just after the name. Equal to "arg" if there is no
17106 find_name_end(arg
, expr_start
, expr_end
, flags
)
17108 char_u
**expr_start
;
17116 if (expr_start
!= NULL
)
17118 *expr_start
= NULL
;
17122 /* Quick check for valid starting character. */
17123 if ((flags
& FNE_CHECK_START
) && !eval_isnamec1(*arg
) && *arg
!= '{')
17126 for (p
= arg
; *p
!= NUL
17127 && (eval_isnamec(*p
)
17129 || ((flags
& FNE_INCL_BR
) && (*p
== '[' || *p
== '.'))
17131 || br_nest
!= 0); mb_ptr_adv(p
))
17135 /* skip over 'string' to avoid counting [ and ] inside it. */
17136 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '\''; mb_ptr_adv(p
))
17141 else if (*p
== '"')
17143 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17144 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
17145 if (*p
== '\\' && p
[1] != NUL
)
17155 else if (*p
== ']')
17164 if (expr_start
!= NULL
&& *expr_start
== NULL
)
17167 else if (*p
== '}')
17170 if (expr_start
!= NULL
&& mb_nest
== 0 && *expr_end
== NULL
)
17180 * Expands out the 'magic' {}'s in a variable/function name.
17181 * Note that this can call itself recursively, to deal with
17182 * constructs like foo{bar}{baz}{bam}
17183 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17189 * Returns a new allocated string, which the caller must free.
17190 * Returns NULL for failure.
17193 make_expanded_name(in_start
, expr_start
, expr_end
, in_end
)
17195 char_u
*expr_start
;
17200 char_u
*retval
= NULL
;
17201 char_u
*temp_result
;
17202 char_u
*nextcmd
= NULL
;
17204 if (expr_end
== NULL
|| in_end
== NULL
)
17211 temp_result
= eval_to_string(expr_start
+ 1, &nextcmd
, FALSE
);
17212 if (temp_result
!= NULL
&& nextcmd
== NULL
)
17214 retval
= alloc((unsigned)(STRLEN(temp_result
) + (expr_start
- in_start
)
17215 + (in_end
- expr_end
) + 1));
17216 if (retval
!= NULL
)
17218 STRCPY(retval
, in_start
);
17219 STRCAT(retval
, temp_result
);
17220 STRCAT(retval
, expr_end
+ 1);
17223 vim_free(temp_result
);
17225 *in_end
= c1
; /* put char back for error messages */
17229 if (retval
!= NULL
)
17231 temp_result
= find_name_end(retval
, &expr_start
, &expr_end
, 0);
17232 if (expr_start
!= NULL
)
17234 /* Further expansion! */
17235 temp_result
= make_expanded_name(retval
, expr_start
,
17236 expr_end
, temp_result
);
17238 retval
= temp_result
;
17246 * Return TRUE if character "c" can be used in a variable or function name.
17247 * Does not include '{' or '}' for magic braces.
17253 return (ASCII_ISALNUM(c
) || c
== '_' || c
== ':' || c
== AUTOLOAD_CHAR
);
17257 * Return TRUE if character "c" can be used as the first character in a
17258 * variable or function name (excluding '{' and '}').
17264 return (ASCII_ISALPHA(c
) || c
== '_');
17268 * Set number v: variable to "val".
17271 set_vim_var_nr(idx
, val
)
17275 vimvars
[idx
].vv_nr
= val
;
17279 * Get number v: variable value.
17282 get_vim_var_nr(idx
)
17285 return vimvars
[idx
].vv_nr
;
17288 #if defined(FEAT_AUTOCMD) || defined(PROTO)
17290 * Get string v: variable value. Uses a static buffer, can only be used once.
17293 get_vim_var_str(idx
)
17296 return get_tv_string(&vimvars
[idx
].vv_tv
);
17301 * Set v:count, v:count1 and v:prevcount.
17304 set_vcount(count
, count1
)
17308 vimvars
[VV_PREVCOUNT
].vv_nr
= vimvars
[VV_COUNT
].vv_nr
;
17309 vimvars
[VV_COUNT
].vv_nr
= count
;
17310 vimvars
[VV_COUNT1
].vv_nr
= count1
;
17314 * Set string v: variable to a copy of "val".
17317 set_vim_var_string(idx
, val
, len
)
17320 int len
; /* length of "val" to use or -1 (whole string) */
17322 /* Need to do this (at least) once, since we can't initialize a union.
17323 * Will always be invoked when "v:progname" is set. */
17324 vimvars
[VV_VERSION
].vv_nr
= VIM_VERSION_100
;
17326 vim_free(vimvars
[idx
].vv_str
);
17328 vimvars
[idx
].vv_str
= NULL
;
17329 else if (len
== -1)
17330 vimvars
[idx
].vv_str
= vim_strsave(val
);
17332 vimvars
[idx
].vv_str
= vim_strnsave(val
, len
);
17336 * Set v:register if needed.
17344 if (c
== 0 || c
== ' ')
17348 /* Avoid free/alloc when the value is already right. */
17349 if (vimvars
[VV_REG
].vv_str
== NULL
|| vimvars
[VV_REG
].vv_str
[0] != c
)
17350 set_vim_var_string(VV_REG
, ®name
, 1);
17354 * Get or set v:exception. If "oldval" == NULL, return the current value.
17355 * Otherwise, restore the value to "oldval" and return NULL.
17356 * Must always be called in pairs to save and restore v:exception! Does not
17357 * take care of memory allocations.
17360 v_exception(oldval
)
17363 if (oldval
== NULL
)
17364 return vimvars
[VV_EXCEPTION
].vv_str
;
17366 vimvars
[VV_EXCEPTION
].vv_str
= oldval
;
17371 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17372 * Otherwise, restore the value to "oldval" and return NULL.
17373 * Must always be called in pairs to save and restore v:throwpoint! Does not
17374 * take care of memory allocations.
17377 v_throwpoint(oldval
)
17380 if (oldval
== NULL
)
17381 return vimvars
[VV_THROWPOINT
].vv_str
;
17383 vimvars
[VV_THROWPOINT
].vv_str
= oldval
;
17387 #if defined(FEAT_AUTOCMD) || defined(PROTO)
17390 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17391 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17392 * Must always be called in pairs!
17395 set_cmdarg(eap
, oldarg
)
17403 oldval
= vimvars
[VV_CMDARG
].vv_str
;
17407 vimvars
[VV_CMDARG
].vv_str
= oldarg
;
17411 if (eap
->force_bin
== FORCE_BIN
)
17413 else if (eap
->force_bin
== FORCE_NOBIN
)
17418 if (eap
->read_edit
)
17421 if (eap
->force_ff
!= 0)
17422 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_ff
) + 6;
17424 if (eap
->force_enc
!= 0)
17425 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_enc
) + 7;
17426 if (eap
->bad_char
!= 0)
17427 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->bad_char
) + 7;
17430 newval
= alloc(len
+ 1);
17431 if (newval
== NULL
)
17434 if (eap
->force_bin
== FORCE_BIN
)
17435 sprintf((char *)newval
, " ++bin");
17436 else if (eap
->force_bin
== FORCE_NOBIN
)
17437 sprintf((char *)newval
, " ++nobin");
17441 if (eap
->read_edit
)
17442 STRCAT(newval
, " ++edit");
17444 if (eap
->force_ff
!= 0)
17445 sprintf((char *)newval
+ STRLEN(newval
), " ++ff=%s",
17446 eap
->cmd
+ eap
->force_ff
);
17448 if (eap
->force_enc
!= 0)
17449 sprintf((char *)newval
+ STRLEN(newval
), " ++enc=%s",
17450 eap
->cmd
+ eap
->force_enc
);
17451 if (eap
->bad_char
!= 0)
17452 sprintf((char *)newval
+ STRLEN(newval
), " ++bad=%s",
17453 eap
->cmd
+ eap
->bad_char
);
17455 vimvars
[VV_CMDARG
].vv_str
= newval
;
17461 * Get the value of internal variable "name".
17462 * Return OK or FAIL.
17465 get_var_tv(name
, len
, rettv
, verbose
)
17467 int len
; /* length of "name" */
17468 typval_T
*rettv
; /* NULL when only checking existence */
17469 int verbose
; /* may give error message */
17472 typval_T
*tv
= NULL
;
17477 /* truncate the name, so that we can use strcmp() */
17482 * Check for "b:changedtick".
17484 if (STRCMP(name
, "b:changedtick") == 0)
17486 atv
.v_type
= VAR_NUMBER
;
17487 atv
.vval
.v_number
= curbuf
->b_changedtick
;
17492 * Check for user-defined variables.
17496 v
= find_var(name
, NULL
);
17503 if (rettv
!= NULL
&& verbose
)
17504 EMSG2(_(e_undefvar
), name
);
17507 else if (rettv
!= NULL
)
17508 copy_tv(tv
, rettv
);
17516 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17517 * Also handle function call with Funcref variable: func(expr)
17518 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17521 handle_subscript(arg
, rettv
, evaluate
, verbose
)
17524 int evaluate
; /* do more than finding the end */
17525 int verbose
; /* give error messages */
17528 dict_T
*selfdict
= NULL
;
17535 || (**arg
== '.' && rettv
->v_type
== VAR_DICT
)
17536 || (**arg
== '(' && rettv
->v_type
== VAR_FUNC
))
17537 && !vim_iswhite(*(*arg
- 1)))
17541 /* need to copy the funcref so that we can clear rettv */
17543 rettv
->v_type
= VAR_UNKNOWN
;
17545 /* Invoke the function. Recursive! */
17546 s
= functv
.vval
.v_string
;
17547 ret
= get_func_tv(s
, (int)STRLEN(s
), rettv
, arg
,
17548 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
17549 &len
, evaluate
, selfdict
);
17551 /* Clear the funcref afterwards, so that deleting it while
17552 * evaluating the arguments is possible (see test55). */
17555 /* Stop the expression evaluation when immediately aborting on
17556 * error, or when an interrupt occurred or an exception was thrown
17557 * but not caught. */
17564 dict_unref(selfdict
);
17567 else /* **arg == '[' || **arg == '.' */
17569 dict_unref(selfdict
);
17570 if (rettv
->v_type
== VAR_DICT
)
17572 selfdict
= rettv
->vval
.v_dict
;
17573 if (selfdict
!= NULL
)
17574 ++selfdict
->dv_refcount
;
17578 if (eval_index(arg
, rettv
, evaluate
, verbose
) == FAIL
)
17585 dict_unref(selfdict
);
17590 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17596 return (typval_T
*)alloc_clear((unsigned)sizeof(typval_T
));
17600 * Allocate memory for a variable type-value, and assign a string to it.
17601 * The string "s" must have been allocated, it is consumed.
17602 * Return NULL for out of memory, the variable otherwise.
17610 rettv
= alloc_tv();
17613 rettv
->v_type
= VAR_STRING
;
17614 rettv
->vval
.v_string
= s
;
17622 * Free the memory for a variable type-value.
17630 switch (varp
->v_type
)
17633 func_unref(varp
->vval
.v_string
);
17636 vim_free(varp
->vval
.v_string
);
17639 list_unref(varp
->vval
.v_list
);
17642 dict_unref(varp
->vval
.v_dict
);
17648 EMSG2(_(e_intern2
), "free_tv()");
17656 * Free the memory for a variable value and set the value to NULL or 0.
17664 switch (varp
->v_type
)
17667 func_unref(varp
->vval
.v_string
);
17670 vim_free(varp
->vval
.v_string
);
17671 varp
->vval
.v_string
= NULL
;
17674 list_unref(varp
->vval
.v_list
);
17675 varp
->vval
.v_list
= NULL
;
17678 dict_unref(varp
->vval
.v_dict
);
17679 varp
->vval
.v_dict
= NULL
;
17682 varp
->vval
.v_number
= 0;
17687 EMSG2(_(e_intern2
), "clear_tv()");
17694 * Set the value of a variable to NULL without freeing items.
17701 vim_memset(varp
, 0, sizeof(typval_T
));
17705 * Get the number value of a variable.
17706 * If it is a String variable, uses vim_str2nr().
17707 * For incompatible types, return 0.
17708 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17709 * caller of incompatible types: it sets *denote to TRUE if "denote"
17710 * is not NULL or returns -1 otherwise.
17713 get_tv_number(varp
)
17718 return get_tv_number_chk(varp
, &error
); /* return 0L on error */
17722 get_tv_number_chk(varp
, denote
)
17728 switch (varp
->v_type
)
17731 return (long)(varp
->vval
.v_number
);
17733 EMSG(_("E703: Using a Funcref as a number"));
17736 if (varp
->vval
.v_string
!= NULL
)
17737 vim_str2nr(varp
->vval
.v_string
, NULL
, NULL
,
17738 TRUE
, TRUE
, &n
, NULL
);
17741 EMSG(_("E745: Using a List as a number"));
17744 EMSG(_("E728: Using a Dictionary as a number"));
17747 EMSG2(_(e_intern2
), "get_tv_number()");
17750 if (denote
== NULL
) /* useful for values that must be unsigned */
17758 * Get the lnum from the first argument.
17759 * Also accepts ".", "$", etc., but that only works for the current buffer.
17760 * Returns -1 on error.
17763 get_tv_lnum(argvars
)
17769 lnum
= get_tv_number_chk(&argvars
[0], NULL
);
17770 if (lnum
== 0) /* no valid number, try using line() */
17772 rettv
.v_type
= VAR_NUMBER
;
17773 f_line(argvars
, &rettv
);
17774 lnum
= rettv
.vval
.v_number
;
17781 * Get the lnum from the first argument.
17782 * Also accepts "$", then "buf" is used.
17783 * Returns 0 on error.
17786 get_tv_lnum_buf(argvars
, buf
)
17790 if (argvars
[0].v_type
== VAR_STRING
17791 && argvars
[0].vval
.v_string
!= NULL
17792 && argvars
[0].vval
.v_string
[0] == '$'
17794 return buf
->b_ml
.ml_line_count
;
17795 return get_tv_number_chk(&argvars
[0], NULL
);
17799 * Get the string value of a variable.
17800 * If it is a Number variable, the number is converted into a string.
17801 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17802 * get_tv_string_buf() uses a given buffer.
17803 * If the String variable has never been set, return an empty string.
17804 * Never returns NULL;
17805 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17809 get_tv_string(varp
)
17812 static char_u mybuf
[NUMBUFLEN
];
17814 return get_tv_string_buf(varp
, mybuf
);
17818 get_tv_string_buf(varp
, buf
)
17822 char_u
*res
= get_tv_string_buf_chk(varp
, buf
);
17824 return res
!= NULL
? res
: (char_u
*)"";
17828 get_tv_string_chk(varp
)
17831 static char_u mybuf
[NUMBUFLEN
];
17833 return get_tv_string_buf_chk(varp
, mybuf
);
17837 get_tv_string_buf_chk(varp
, buf
)
17841 switch (varp
->v_type
)
17844 sprintf((char *)buf
, "%ld", (long)varp
->vval
.v_number
);
17847 EMSG(_("E729: using Funcref as a String"));
17850 EMSG(_("E730: using List as a String"));
17853 EMSG(_("E731: using Dictionary as a String"));
17856 if (varp
->vval
.v_string
!= NULL
)
17857 return varp
->vval
.v_string
;
17858 return (char_u
*)"";
17860 EMSG2(_(e_intern2
), "get_tv_string_buf()");
17867 * Find variable "name" in the list of variables.
17868 * Return a pointer to it if found, NULL if not found.
17869 * Careful: "a:0" variables don't have a name.
17870 * When "htp" is not NULL we are writing to the variable, set "htp" to the
17873 static dictitem_T
*
17874 find_var(name
, htp
)
17881 ht
= find_var_ht(name
, &varname
);
17886 return find_var_in_ht(ht
, varname
, htp
!= NULL
);
17890 * Find variable "varname" in hashtab "ht".
17891 * Returns NULL if not found.
17893 static dictitem_T
*
17894 find_var_in_ht(ht
, varname
, writing
)
17901 if (*varname
== NUL
)
17903 /* Must be something like "s:", otherwise "ht" would be NULL. */
17904 switch (varname
[-2])
17906 case 's': return &SCRIPT_SV(current_SID
).sv_var
;
17907 case 'g': return &globvars_var
;
17908 case 'v': return &vimvars_var
;
17909 case 'b': return &curbuf
->b_bufvar
;
17910 case 'w': return &curwin
->w_winvar
;
17911 #ifdef FEAT_WINDOWS
17912 case 't': return &curtab
->tp_winvar
;
17914 case 'l': return current_funccal
== NULL
17915 ? NULL
: ¤t_funccal
->l_vars_var
;
17916 case 'a': return current_funccal
== NULL
17917 ? NULL
: ¤t_funccal
->l_avars_var
;
17922 hi
= hash_find(ht
, varname
);
17923 if (HASHITEM_EMPTY(hi
))
17925 /* For global variables we may try auto-loading the script. If it
17926 * worked find the variable again. Don't auto-load a script if it was
17927 * loaded already, otherwise it would be loaded every time when
17928 * checking if a function name is a Funcref variable. */
17929 if (ht
== &globvarht
&& !writing
17930 && script_autoload(varname
, FALSE
) && !aborting())
17931 hi
= hash_find(ht
, varname
);
17932 if (HASHITEM_EMPTY(hi
))
17939 * Find the hashtab used for a variable name.
17940 * Set "varname" to the start of name without ':'.
17943 find_var_ht(name
, varname
)
17949 if (name
[1] != ':')
17951 /* The name must not start with a colon or #. */
17952 if (name
[0] == ':' || name
[0] == AUTOLOAD_CHAR
)
17956 /* "version" is "v:version" in all scopes */
17957 hi
= hash_find(&compat_hashtab
, name
);
17958 if (!HASHITEM_EMPTY(hi
))
17959 return &compat_hashtab
;
17961 if (current_funccal
== NULL
)
17962 return &globvarht
; /* global variable */
17963 return ¤t_funccal
->l_vars
.dv_hashtab
; /* l: variable */
17965 *varname
= name
+ 2;
17966 if (*name
== 'g') /* global variable */
17968 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17970 if (vim_strchr(name
+ 2, ':') != NULL
17971 || vim_strchr(name
+ 2, AUTOLOAD_CHAR
) != NULL
)
17973 if (*name
== 'b') /* buffer variable */
17974 return &curbuf
->b_vars
.dv_hashtab
;
17975 if (*name
== 'w') /* window variable */
17976 return &curwin
->w_vars
.dv_hashtab
;
17977 #ifdef FEAT_WINDOWS
17978 if (*name
== 't') /* tab page variable */
17979 return &curtab
->tp_vars
.dv_hashtab
;
17981 if (*name
== 'v') /* v: variable */
17983 if (*name
== 'a' && current_funccal
!= NULL
) /* function argument */
17984 return ¤t_funccal
->l_avars
.dv_hashtab
;
17985 if (*name
== 'l' && current_funccal
!= NULL
) /* local function variable */
17986 return ¤t_funccal
->l_vars
.dv_hashtab
;
17987 if (*name
== 's' /* script variable */
17988 && current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
17989 return &SCRIPT_VARS(current_SID
);
17994 * Get the string value of a (global/local) variable.
17995 * Returns NULL when it doesn't exist.
17998 get_var_value(name
)
18003 v
= find_var(name
, NULL
);
18006 return get_tv_string(&v
->di_tv
);
18010 * Allocate a new hashtab for a sourced script. It will be used while
18011 * sourcing this script and when executing functions defined in the script.
18014 new_script_vars(id
)
18021 if (ga_grow(&ga_scripts
, (int)(id
- ga_scripts
.ga_len
)) == OK
)
18023 /* Re-allocating ga_data means that an ht_array pointing to
18024 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
18025 * at its init value. Also reset "v_dict", it's always the same. */
18026 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
18028 ht
= &SCRIPT_VARS(i
);
18029 if (ht
->ht_mask
== HT_INIT_SIZE
- 1)
18030 ht
->ht_array
= ht
->ht_smallarray
;
18031 sv
= &SCRIPT_SV(i
);
18032 sv
->sv_var
.di_tv
.vval
.v_dict
= &sv
->sv_dict
;
18035 while (ga_scripts
.ga_len
< id
)
18037 sv
= &SCRIPT_SV(ga_scripts
.ga_len
+ 1);
18038 init_var_dict(&sv
->sv_dict
, &sv
->sv_var
);
18039 ++ga_scripts
.ga_len
;
18045 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
18049 init_var_dict(dict
, dict_var
)
18051 dictitem_T
*dict_var
;
18053 hash_init(&dict
->dv_hashtab
);
18054 dict
->dv_refcount
= 99999;
18055 dict_var
->di_tv
.vval
.v_dict
= dict
;
18056 dict_var
->di_tv
.v_type
= VAR_DICT
;
18057 dict_var
->di_tv
.v_lock
= VAR_FIXED
;
18058 dict_var
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
18059 dict_var
->di_key
[0] = NUL
;
18063 * Clean up a list of internal variables.
18064 * Frees all allocated variables and the value they contain.
18065 * Clears hashtab "ht", does not free it.
18071 vars_clear_ext(ht
, TRUE
);
18075 * Like vars_clear(), but only free the value if "free_val" is TRUE.
18078 vars_clear_ext(ht
, free_val
)
18087 todo
= (int)ht
->ht_used
;
18088 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
18090 if (!HASHITEM_EMPTY(hi
))
18094 /* Free the variable. Don't remove it from the hashtab,
18095 * ht_array might change then. hash_clear() takes care of it
18099 clear_tv(&v
->di_tv
);
18100 if ((v
->di_flags
& DI_FLAGS_FIX
) == 0)
18109 * Delete a variable from hashtab "ht" at item "hi".
18110 * Clear the variable value and free the dictitem.
18117 dictitem_T
*di
= HI2DI(hi
);
18119 hash_remove(ht
, hi
);
18120 clear_tv(&di
->di_tv
);
18125 * List the value of one internal variable.
18128 list_one_var(v
, prefix
, first
)
18135 char_u numbuf
[NUMBUFLEN
];
18137 s
= echo_string(&v
->di_tv
, &tofree
, numbuf
, ++current_copyID
);
18138 list_one_var_a(prefix
, v
->di_key
, v
->di_tv
.v_type
,
18139 s
== NULL
? (char_u
*)"" : s
, first
);
18144 list_one_var_a(prefix
, name
, type
, string
, first
)
18149 int *first
; /* when TRUE clear rest of screen and set to FALSE */
18151 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18154 if (name
!= NULL
) /* "a:" vars don't have a name stored */
18158 if (type
== VAR_NUMBER
)
18160 else if (type
== VAR_FUNC
)
18162 else if (type
== VAR_LIST
)
18165 if (*string
== '[')
18168 else if (type
== VAR_DICT
)
18171 if (*string
== '{')
18177 msg_outtrans(string
);
18179 if (type
== VAR_FUNC
)
18180 msg_puts((char_u
*)"()");
18189 * Set variable "name" to value in "tv".
18190 * If the variable already exists, the value is updated.
18191 * Otherwise the variable is created.
18194 set_var(name
, tv
, copy
)
18197 int copy
; /* make copy of value in "tv" */
18204 if (tv
->v_type
== VAR_FUNC
)
18206 if (!(vim_strchr((char_u
*)"wbs", name
[0]) != NULL
&& name
[1] == ':')
18207 && !ASCII_ISUPPER((name
[0] != NUL
&& name
[1] == ':')
18208 ? name
[2] : name
[0]))
18210 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name
);
18213 if (function_exists(name
))
18215 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
18221 ht
= find_var_ht(name
, &varname
);
18222 if (ht
== NULL
|| *varname
== NUL
)
18224 EMSG2(_(e_illvar
), name
);
18228 v
= find_var_in_ht(ht
, varname
, TRUE
);
18231 /* existing variable, need to clear the value */
18232 if (var_check_ro(v
->di_flags
, name
)
18233 || tv_check_lock(v
->di_tv
.v_lock
, name
))
18235 if (v
->di_tv
.v_type
!= tv
->v_type
18236 && !((v
->di_tv
.v_type
== VAR_STRING
18237 || v
->di_tv
.v_type
== VAR_NUMBER
)
18238 && (tv
->v_type
== VAR_STRING
18239 || tv
->v_type
== VAR_NUMBER
)))
18241 EMSG2(_("E706: Variable type mismatch for: %s"), name
);
18246 * Handle setting internal v: variables separately: we don't change
18249 if (ht
== &vimvarht
)
18251 if (v
->di_tv
.v_type
== VAR_STRING
)
18253 vim_free(v
->di_tv
.vval
.v_string
);
18254 if (copy
|| tv
->v_type
!= VAR_STRING
)
18255 v
->di_tv
.vval
.v_string
= vim_strsave(get_tv_string(tv
));
18258 /* Take over the string to avoid an extra alloc/free. */
18259 v
->di_tv
.vval
.v_string
= tv
->vval
.v_string
;
18260 tv
->vval
.v_string
= NULL
;
18263 else if (v
->di_tv
.v_type
!= VAR_NUMBER
)
18264 EMSG2(_(e_intern2
), "set_var()");
18266 v
->di_tv
.vval
.v_number
= get_tv_number(tv
);
18270 clear_tv(&v
->di_tv
);
18272 else /* add a new variable */
18274 /* Can't add "v:" variable. */
18275 if (ht
== &vimvarht
)
18277 EMSG2(_(e_illvar
), name
);
18281 /* Make sure the variable name is valid. */
18282 for (p
= varname
; *p
!= NUL
; ++p
)
18283 if (!eval_isnamec1(*p
) && (p
== varname
|| !VIM_ISDIGIT(*p
))
18284 && *p
!= AUTOLOAD_CHAR
)
18286 EMSG2(_(e_illvar
), varname
);
18290 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
18291 + STRLEN(varname
)));
18294 STRCPY(v
->di_key
, varname
);
18295 if (hash_add(ht
, DI2HIKEY(v
)) == FAIL
)
18303 if (copy
|| tv
->v_type
== VAR_NUMBER
)
18304 copy_tv(tv
, &v
->di_tv
);
18308 v
->di_tv
.v_lock
= 0;
18314 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
18315 * Also give an error message.
18318 var_check_ro(flags
, name
)
18322 if (flags
& DI_FLAGS_RO
)
18324 EMSG2(_(e_readonlyvar
), name
);
18327 if ((flags
& DI_FLAGS_RO_SBX
) && sandbox
)
18329 EMSG2(_(e_readonlysbx
), name
);
18336 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
18337 * Also give an error message.
18340 var_check_fixed(flags
, name
)
18344 if (flags
& DI_FLAGS_FIX
)
18346 EMSG2(_("E795: Cannot delete variable %s"), name
);
18353 * Return TRUE if typeval "tv" is set to be locked (immutable).
18354 * Also give an error message, using "name".
18357 tv_check_lock(lock
, name
)
18361 if (lock
& VAR_LOCKED
)
18363 EMSG2(_("E741: Value is locked: %s"),
18364 name
== NULL
? (char_u
*)_("Unknown") : name
);
18367 if (lock
& VAR_FIXED
)
18369 EMSG2(_("E742: Cannot change value of %s"),
18370 name
== NULL
? (char_u
*)_("Unknown") : name
);
18377 * Copy the values from typval_T "from" to typval_T "to".
18378 * When needed allocates string or increases reference count.
18379 * Does not make a copy of a list or dict but copies the reference!
18386 to
->v_type
= from
->v_type
;
18388 switch (from
->v_type
)
18391 to
->vval
.v_number
= from
->vval
.v_number
;
18395 if (from
->vval
.v_string
== NULL
)
18396 to
->vval
.v_string
= NULL
;
18399 to
->vval
.v_string
= vim_strsave(from
->vval
.v_string
);
18400 if (from
->v_type
== VAR_FUNC
)
18401 func_ref(to
->vval
.v_string
);
18405 if (from
->vval
.v_list
== NULL
)
18406 to
->vval
.v_list
= NULL
;
18409 to
->vval
.v_list
= from
->vval
.v_list
;
18410 ++to
->vval
.v_list
->lv_refcount
;
18414 if (from
->vval
.v_dict
== NULL
)
18415 to
->vval
.v_dict
= NULL
;
18418 to
->vval
.v_dict
= from
->vval
.v_dict
;
18419 ++to
->vval
.v_dict
->dv_refcount
;
18423 EMSG2(_(e_intern2
), "copy_tv()");
18429 * Make a copy of an item.
18430 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
18431 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18432 * reference to an already copied list/dict can be used.
18433 * Returns FAIL or OK.
18436 item_copy(from
, to
, deep
, copyID
)
18442 static int recurse
= 0;
18445 if (recurse
>= DICT_MAXNEST
)
18447 EMSG(_("E698: variable nested too deep for making a copy"));
18452 switch (from
->v_type
)
18460 to
->v_type
= VAR_LIST
;
18462 if (from
->vval
.v_list
== NULL
)
18463 to
->vval
.v_list
= NULL
;
18464 else if (copyID
!= 0 && from
->vval
.v_list
->lv_copyID
== copyID
)
18466 /* use the copy made earlier */
18467 to
->vval
.v_list
= from
->vval
.v_list
->lv_copylist
;
18468 ++to
->vval
.v_list
->lv_refcount
;
18471 to
->vval
.v_list
= list_copy(from
->vval
.v_list
, deep
, copyID
);
18472 if (to
->vval
.v_list
== NULL
)
18476 to
->v_type
= VAR_DICT
;
18478 if (from
->vval
.v_dict
== NULL
)
18479 to
->vval
.v_dict
= NULL
;
18480 else if (copyID
!= 0 && from
->vval
.v_dict
->dv_copyID
== copyID
)
18482 /* use the copy made earlier */
18483 to
->vval
.v_dict
= from
->vval
.v_dict
->dv_copydict
;
18484 ++to
->vval
.v_dict
->dv_refcount
;
18487 to
->vval
.v_dict
= dict_copy(from
->vval
.v_dict
, deep
, copyID
);
18488 if (to
->vval
.v_dict
== NULL
)
18492 EMSG2(_(e_intern2
), "item_copy()");
18500 * ":echo expr1 ..." print each argument separated with a space, add a
18501 * newline at the end.
18502 * ":echon expr1 ..." print each argument plain.
18508 char_u
*arg
= eap
->arg
;
18512 int needclr
= TRUE
;
18513 int atstart
= TRUE
;
18514 char_u numbuf
[NUMBUFLEN
];
18518 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n' && !got_int
)
18521 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
18524 * Report the invalid expression unless the expression evaluation
18525 * has been cancelled due to an aborting error, an interrupt, or an
18529 EMSG2(_(e_invexpr2
), p
);
18537 /* Call msg_start() after eval1(), evaluating the expression
18538 * may cause a message to appear. */
18539 if (eap
->cmdidx
== CMD_echo
)
18542 else if (eap
->cmdidx
== CMD_echo
)
18543 msg_puts_attr((char_u
*)" ", echo_attr
);
18544 p
= echo_string(&rettv
, &tofree
, numbuf
, ++current_copyID
);
18546 for ( ; *p
!= NUL
&& !got_int
; ++p
)
18548 if (*p
== '\n' || *p
== '\r' || *p
== TAB
)
18550 if (*p
!= TAB
&& needclr
)
18552 /* remove any text still there from the command */
18556 msg_putchar_attr(*p
, echo_attr
);
18563 int i
= (*mb_ptr2len
)(p
);
18565 (void)msg_outtrans_len_attr(p
, i
, echo_attr
);
18570 (void)msg_outtrans_len_attr(p
, 1, echo_attr
);
18576 arg
= skipwhite(arg
);
18578 eap
->nextcmd
= check_nextcmd(arg
);
18584 /* remove text that may still be there from the command */
18587 if (eap
->cmdidx
== CMD_echo
)
18593 * ":echohl {name}".
18601 id
= syn_name2id(eap
->arg
);
18605 echo_attr
= syn_id2attr(id
);
18609 * ":execute expr1 ..." execute the result of an expression.
18610 * ":echomsg expr1 ..." Print a message
18611 * ":echoerr expr1 ..." Print an error
18612 * Each gets spaces around each argument and a newline at the end for
18619 char_u
*arg
= eap
->arg
;
18627 ga_init2(&ga
, 1, 80);
18631 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
18634 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
18637 * Report the invalid expression unless the expression evaluation
18638 * has been cancelled due to an aborting error, an interrupt, or an
18642 EMSG2(_(e_invexpr2
), p
);
18649 p
= get_tv_string(&rettv
);
18650 len
= (int)STRLEN(p
);
18651 if (ga_grow(&ga
, len
+ 2) == FAIL
)
18658 ((char_u
*)(ga
.ga_data
))[ga
.ga_len
++] = ' ';
18659 STRCPY((char_u
*)(ga
.ga_data
) + ga
.ga_len
, p
);
18664 arg
= skipwhite(arg
);
18667 if (ret
!= FAIL
&& ga
.ga_data
!= NULL
)
18669 if (eap
->cmdidx
== CMD_echomsg
)
18671 MSG_ATTR(ga
.ga_data
, echo_attr
);
18674 else if (eap
->cmdidx
== CMD_echoerr
)
18676 /* We don't want to abort following commands, restore did_emsg. */
18677 save_did_emsg
= did_emsg
;
18678 EMSG((char_u
*)ga
.ga_data
);
18680 did_emsg
= save_did_emsg
;
18682 else if (eap
->cmdidx
== CMD_execute
)
18683 do_cmdline((char_u
*)ga
.ga_data
,
18684 eap
->getline
, eap
->cookie
, DOCMD_NOWAIT
|DOCMD_VERBOSE
);
18692 eap
->nextcmd
= check_nextcmd(arg
);
18696 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18697 * "arg" points to the "&" or '+' when called, to "option" when returning.
18698 * Returns NULL when no option name found. Otherwise pointer to the char
18699 * after the option name.
18702 find_option_end(arg
, opt_flags
)
18709 if (*p
== 'g' && p
[1] == ':')
18711 *opt_flags
= OPT_GLOBAL
;
18714 else if (*p
== 'l' && p
[1] == ':')
18716 *opt_flags
= OPT_LOCAL
;
18722 if (!ASCII_ISALPHA(*p
))
18726 if (p
[0] == 't' && p
[1] == '_' && p
[2] != NUL
&& p
[3] != NUL
)
18727 p
+= 4; /* termcap option */
18729 while (ASCII_ISALPHA(*p
))
18744 int saved_did_emsg
;
18745 char_u
*name
= NULL
;
18748 char_u
*line_arg
= NULL
;
18751 int varargs
= FALSE
;
18752 int mustend
= FALSE
;
18757 char_u
*skip_until
= NULL
;
18760 static int func_nr
= 0; /* number for nameless function */
18765 int sourcing_lnum_off
;
18768 * ":function" without argument: list functions.
18770 if (ends_excmd(*eap
->arg
))
18774 todo
= (int)func_hashtab
.ht_used
;
18775 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
18777 if (!HASHITEM_EMPTY(hi
))
18781 if (!isdigit(*fp
->uf_name
))
18782 list_func_head(fp
, FALSE
);
18786 eap
->nextcmd
= check_nextcmd(eap
->arg
);
18791 * ":function /pat": list functions matching pattern.
18793 if (*eap
->arg
== '/')
18795 p
= skip_regexp(eap
->arg
+ 1, '/', TRUE
, NULL
);
18798 regmatch_T regmatch
;
18802 regmatch
.regprog
= vim_regcomp(eap
->arg
+ 1, RE_MAGIC
);
18804 if (regmatch
.regprog
!= NULL
)
18806 regmatch
.rm_ic
= p_ic
;
18808 todo
= (int)func_hashtab
.ht_used
;
18809 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
18811 if (!HASHITEM_EMPTY(hi
))
18815 if (!isdigit(*fp
->uf_name
)
18816 && vim_regexec(®match
, fp
->uf_name
, 0))
18817 list_func_head(fp
, FALSE
);
18824 eap
->nextcmd
= check_nextcmd(p
);
18829 * Get the function name. There are these situations:
18830 * func normal function name
18831 * "name" == func, "fudi.fd_dict" == NULL
18832 * dict.func new dictionary entry
18833 * "name" == NULL, "fudi.fd_dict" set,
18834 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18835 * dict.func existing dict entry with a Funcref
18836 * "name" == func, "fudi.fd_dict" set,
18837 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18838 * dict.func existing dict entry that's not a Funcref
18839 * "name" == NULL, "fudi.fd_dict" set,
18840 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18843 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
18844 paren
= (vim_strchr(p
, '(') != NULL
);
18845 if (name
== NULL
&& (fudi
.fd_dict
== NULL
|| !paren
) && !eap
->skip
)
18848 * Return on an invalid expression in braces, unless the expression
18849 * evaluation has been cancelled due to an aborting error, an
18850 * interrupt, or an exception.
18854 if (!eap
->skip
&& fudi
.fd_newkey
!= NULL
)
18855 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
18856 vim_free(fudi
.fd_newkey
);
18863 /* An error in a function call during evaluation of an expression in magic
18864 * braces should not cause the function not to be defined. */
18865 saved_did_emsg
= did_emsg
;
18869 * ":function func" with only function name: list function.
18873 if (!ends_excmd(*skipwhite(p
)))
18875 EMSG(_(e_trailing
));
18878 eap
->nextcmd
= check_nextcmd(p
);
18879 if (eap
->nextcmd
!= NULL
)
18881 if (!eap
->skip
&& !got_int
)
18883 fp
= find_func(name
);
18886 list_func_head(fp
, TRUE
);
18887 for (j
= 0; j
< fp
->uf_lines
.ga_len
&& !got_int
; ++j
)
18889 if (FUNCLINE(fp
, j
) == NULL
)
18892 msg_outnum((long)(j
+ 1));
18897 msg_prt_line(FUNCLINE(fp
, j
), FALSE
);
18898 out_flush(); /* show a line at a time */
18904 msg_puts((char_u
*)" endfunction");
18908 emsg_funcname("E123: Undefined function: %s", name
);
18914 * ":function name(arg1, arg2)" Define function.
18921 EMSG2(_("E124: Missing '(': %s"), eap
->arg
);
18924 /* attempt to continue by skipping some text */
18925 if (vim_strchr(p
, '(') != NULL
)
18926 p
= vim_strchr(p
, '(');
18928 p
= skipwhite(p
+ 1);
18930 ga_init2(&newargs
, (int)sizeof(char_u
*), 3);
18931 ga_init2(&newlines
, (int)sizeof(char_u
*), 3);
18935 /* Check the name of the function. Unless it's a dictionary function
18936 * (that we are overwriting). */
18940 arg
= fudi
.fd_newkey
;
18941 if (arg
!= NULL
&& (fudi
.fd_di
== NULL
18942 || fudi
.fd_di
->di_tv
.v_type
!= VAR_FUNC
))
18944 if (*arg
== K_SPECIAL
)
18948 while (arg
[j
] != NUL
&& (j
== 0 ? eval_isnamec1(arg
[j
])
18949 : eval_isnamec(arg
[j
])))
18952 emsg_funcname(_(e_invarg2
), arg
);
18957 * Isolate the arguments: "arg1, arg2, ...)"
18961 if (p
[0] == '.' && p
[1] == '.' && p
[2] == '.')
18970 while (ASCII_ISALNUM(*p
) || *p
== '_')
18972 if (arg
== p
|| isdigit(*arg
)
18973 || (p
- arg
== 9 && STRNCMP(arg
, "firstline", 9) == 0)
18974 || (p
- arg
== 8 && STRNCMP(arg
, "lastline", 8) == 0))
18977 EMSG2(_("E125: Illegal argument: %s"), arg
);
18980 if (ga_grow(&newargs
, 1) == FAIL
)
18984 arg
= vim_strsave(arg
);
18987 ((char_u
**)(newargs
.ga_data
))[newargs
.ga_len
] = arg
;
18996 if (mustend
&& *p
!= ')')
18999 EMSG2(_(e_invarg2
), eap
->arg
);
19003 ++p
; /* skip the ')' */
19005 /* find extra arguments "range", "dict" and "abort" */
19009 if (STRNCMP(p
, "range", 5) == 0)
19014 else if (STRNCMP(p
, "dict", 4) == 0)
19019 else if (STRNCMP(p
, "abort", 5) == 0)
19028 /* When there is a line break use what follows for the function body.
19029 * Makes 'exe "func Test()\n...\nendfunc"' work. */
19032 else if (*p
!= NUL
&& *p
!= '"' && !eap
->skip
&& !did_emsg
)
19033 EMSG(_(e_trailing
));
19036 * Read the body of the function, until ":endfunction" is found.
19040 /* Check if the function already exists, don't let the user type the
19041 * whole function before telling him it doesn't work! For a script we
19042 * need to skip the body to be able to find what follows. */
19043 if (!eap
->skip
&& !eap
->forceit
)
19045 if (fudi
.fd_dict
!= NULL
&& fudi
.fd_newkey
== NULL
)
19046 EMSG(_(e_funcdict
));
19047 else if (name
!= NULL
&& find_func(name
) != NULL
)
19048 emsg_funcname(e_funcexts
, name
);
19051 if (!eap
->skip
&& did_emsg
)
19054 msg_putchar('\n'); /* don't overwrite the function name */
19055 cmdline_row
= msg_row
;
19063 need_wait_return
= FALSE
;
19064 sourcing_lnum_off
= sourcing_lnum
;
19066 if (line_arg
!= NULL
)
19068 /* Use eap->arg, split up in parts by line breaks. */
19069 theline
= line_arg
;
19070 p
= vim_strchr(theline
, '\n');
19072 line_arg
+= STRLEN(line_arg
);
19079 else if (eap
->getline
== NULL
)
19080 theline
= getcmdline(':', 0L, indent
);
19082 theline
= eap
->getline(':', eap
->cookie
, indent
);
19084 lines_left
= Rows
- 1;
19085 if (theline
== NULL
)
19087 EMSG(_("E126: Missing :endfunction"));
19091 /* Detect line continuation: sourcing_lnum increased more than one. */
19092 if (sourcing_lnum
> sourcing_lnum_off
+ 1)
19093 sourcing_lnum_off
= sourcing_lnum
- sourcing_lnum_off
- 1;
19095 sourcing_lnum_off
= 0;
19097 if (skip_until
!= NULL
)
19099 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19100 * don't check for ":endfunc". */
19101 if (STRCMP(theline
, skip_until
) == 0)
19103 vim_free(skip_until
);
19109 /* skip ':' and blanks*/
19110 for (p
= theline
; vim_iswhite(*p
) || *p
== ':'; ++p
)
19113 /* Check for "endfunction". */
19114 if (checkforcmd(&p
, "endfunction", 4) && nesting
-- == 0)
19116 if (line_arg
== NULL
)
19121 /* Increase indent inside "if", "while", "for" and "try", decrease
19123 if (indent
> 2 && STRNCMP(p
, "end", 3) == 0)
19125 else if (STRNCMP(p
, "if", 2) == 0
19126 || STRNCMP(p
, "wh", 2) == 0
19127 || STRNCMP(p
, "for", 3) == 0
19128 || STRNCMP(p
, "try", 3) == 0)
19131 /* Check for defining a function inside this function. */
19132 if (checkforcmd(&p
, "function", 2))
19135 p
= skipwhite(p
+ 1);
19136 p
+= eval_fname_script(p
);
19137 if (ASCII_ISALPHA(*p
))
19139 vim_free(trans_function_name(&p
, TRUE
, 0, NULL
));
19140 if (*skipwhite(p
) == '(')
19148 /* Check for ":append" or ":insert". */
19149 p
= skip_range(p
, NULL
);
19150 if ((p
[0] == 'a' && (!ASCII_ISALPHA(p
[1]) || p
[1] == 'p'))
19152 && (!ASCII_ISALPHA(p
[1]) || (p
[1] == 'n'
19153 && (!ASCII_ISALPHA(p
[2]) || (p
[2] == 's'))))))
19154 skip_until
= vim_strsave((char_u
*)".");
19156 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19157 arg
= skipwhite(skiptowhite(p
));
19158 if (arg
[0] == '<' && arg
[1] =='<'
19159 && ((p
[0] == 'p' && p
[1] == 'y'
19160 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 't'))
19161 || (p
[0] == 'p' && p
[1] == 'e'
19162 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'r'))
19163 || (p
[0] == 't' && p
[1] == 'c'
19164 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'l'))
19165 || (p
[0] == 'r' && p
[1] == 'u' && p
[2] == 'b'
19166 && (!ASCII_ISALPHA(p
[3]) || p
[3] == 'y'))
19167 || (p
[0] == 'm' && p
[1] == 'z'
19168 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 's'))
19171 /* ":python <<" continues until a dot, like ":append" */
19172 p
= skipwhite(arg
+ 2);
19174 skip_until
= vim_strsave((char_u
*)".");
19176 skip_until
= vim_strsave(p
);
19180 /* Add the line to the function. */
19181 if (ga_grow(&newlines
, 1 + sourcing_lnum_off
) == FAIL
)
19183 if (line_arg
== NULL
)
19188 /* Copy the line to newly allocated memory. get_one_sourceline()
19189 * allocates 250 bytes per line, this saves 80% on average. The cost
19190 * is an extra alloc/free. */
19191 p
= vim_strsave(theline
);
19194 if (line_arg
== NULL
)
19199 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = theline
;
19201 /* Add NULL lines for continuation lines, so that the line count is
19202 * equal to the index in the growarray. */
19203 while (sourcing_lnum_off
-- > 0)
19204 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = NULL
;
19206 /* Check for end of eap->arg. */
19207 if (line_arg
!= NULL
&& *line_arg
== NUL
)
19211 /* Don't define the function when skipping commands or when an error was
19213 if (eap
->skip
|| did_emsg
)
19217 * If there are no errors, add the function
19219 if (fudi
.fd_dict
== NULL
)
19221 v
= find_var(name
, &ht
);
19222 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
19224 emsg_funcname("E707: Function name conflicts with variable: %s",
19229 fp
= find_func(name
);
19234 emsg_funcname(e_funcexts
, name
);
19237 if (fp
->uf_calls
> 0)
19239 emsg_funcname("E127: Cannot redefine function %s: It is in use",
19243 /* redefine existing function */
19244 ga_clear_strings(&(fp
->uf_args
));
19245 ga_clear_strings(&(fp
->uf_lines
));
19255 if (fudi
.fd_newkey
== NULL
&& !eap
->forceit
)
19257 EMSG(_(e_funcdict
));
19260 if (fudi
.fd_di
== NULL
)
19262 /* Can't add a function to a locked dictionary */
19263 if (tv_check_lock(fudi
.fd_dict
->dv_lock
, eap
->arg
))
19266 /* Can't change an existing function if it is locked */
19267 else if (tv_check_lock(fudi
.fd_di
->di_tv
.v_lock
, eap
->arg
))
19270 /* Give the function a sequential number. Can only be used with a
19273 sprintf(numbuf
, "%d", ++func_nr
);
19274 name
= vim_strsave((char_u
*)numbuf
);
19281 if (fudi
.fd_dict
== NULL
&& vim_strchr(name
, AUTOLOAD_CHAR
) != NULL
)
19284 char_u
*scriptname
;
19286 /* Check that the autoload name matches the script name. */
19288 if (sourcing_name
!= NULL
)
19290 scriptname
= autoload_name(name
);
19291 if (scriptname
!= NULL
)
19293 p
= vim_strchr(scriptname
, '/');
19294 plen
= (int)STRLEN(p
);
19295 slen
= (int)STRLEN(sourcing_name
);
19296 if (slen
> plen
&& fnamecmp(p
,
19297 sourcing_name
+ slen
- plen
) == 0)
19299 vim_free(scriptname
);
19304 EMSG2(_("E746: Function name does not match script file name: %s"), name
);
19309 fp
= (ufunc_T
*)alloc((unsigned)(sizeof(ufunc_T
) + STRLEN(name
)));
19313 if (fudi
.fd_dict
!= NULL
)
19315 if (fudi
.fd_di
== NULL
)
19317 /* add new dict entry */
19318 fudi
.fd_di
= dictitem_alloc(fudi
.fd_newkey
);
19319 if (fudi
.fd_di
== NULL
)
19324 if (dict_add(fudi
.fd_dict
, fudi
.fd_di
) == FAIL
)
19326 vim_free(fudi
.fd_di
);
19332 /* overwrite existing dict entry */
19333 clear_tv(&fudi
.fd_di
->di_tv
);
19334 fudi
.fd_di
->di_tv
.v_type
= VAR_FUNC
;
19335 fudi
.fd_di
->di_tv
.v_lock
= 0;
19336 fudi
.fd_di
->di_tv
.vval
.v_string
= vim_strsave(name
);
19337 fp
->uf_refcount
= 1;
19339 /* behave like "dict" was used */
19343 /* insert the new function in the function list */
19344 STRCPY(fp
->uf_name
, name
);
19345 hash_add(&func_hashtab
, UF2HIKEY(fp
));
19347 fp
->uf_args
= newargs
;
19348 fp
->uf_lines
= newlines
;
19349 #ifdef FEAT_PROFILE
19350 fp
->uf_tml_count
= NULL
;
19351 fp
->uf_tml_total
= NULL
;
19352 fp
->uf_tml_self
= NULL
;
19353 fp
->uf_profiling
= FALSE
;
19354 if (prof_def_func())
19355 func_do_profile(fp
);
19357 fp
->uf_varargs
= varargs
;
19358 fp
->uf_flags
= flags
;
19360 fp
->uf_script_ID
= current_SID
;
19364 ga_clear_strings(&newargs
);
19365 ga_clear_strings(&newlines
);
19367 vim_free(skip_until
);
19368 vim_free(fudi
.fd_newkey
);
19370 did_emsg
|= saved_did_emsg
;
19374 * Get a function name, translating "<SID>" and "<SNR>".
19375 * Also handles a Funcref in a List or Dictionary.
19376 * Returns the function name in allocated memory, or NULL for failure.
19378 * TFN_INT: internal function name OK
19379 * TFN_QUIET: be quiet
19380 * Advances "pp" to just after the function name (if no error).
19383 trans_function_name(pp
, skip
, flags
, fdp
)
19385 int skip
; /* only find the end, don't evaluate */
19387 funcdict_T
*fdp
; /* return: info about dictionary used */
19389 char_u
*name
= NULL
;
19393 char_u sid_buf
[20];
19398 vim_memset(fdp
, 0, sizeof(funcdict_T
));
19401 /* Check for hard coded <SNR>: already translated function ID (from a user
19403 if ((*pp
)[0] == K_SPECIAL
&& (*pp
)[1] == KS_EXTRA
19404 && (*pp
)[2] == (int)KE_SNR
)
19407 len
= get_id_len(pp
) + 3;
19408 return vim_strnsave(start
, len
);
19411 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19412 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
19413 lead
= eval_fname_script(start
);
19417 end
= get_lval(start
, NULL
, &lv
, FALSE
, skip
, flags
& TFN_QUIET
,
19418 lead
> 2 ? 0 : FNE_CHECK_START
);
19422 EMSG(_("E129: Function name required"));
19425 if (end
== NULL
|| (lv
.ll_tv
!= NULL
&& (lead
> 2 || lv
.ll_range
)))
19428 * Report an invalid expression in braces, unless the expression
19429 * evaluation has been cancelled due to an aborting error, an
19430 * interrupt, or an exception.
19435 EMSG2(_(e_invarg2
), start
);
19438 *pp
= find_name_end(start
, NULL
, NULL
, FNE_INCL_BR
);
19442 if (lv
.ll_tv
!= NULL
)
19446 fdp
->fd_dict
= lv
.ll_dict
;
19447 fdp
->fd_newkey
= lv
.ll_newkey
;
19448 lv
.ll_newkey
= NULL
;
19449 fdp
->fd_di
= lv
.ll_di
;
19451 if (lv
.ll_tv
->v_type
== VAR_FUNC
&& lv
.ll_tv
->vval
.v_string
!= NULL
)
19453 name
= vim_strsave(lv
.ll_tv
->vval
.v_string
);
19458 if (!skip
&& !(flags
& TFN_QUIET
) && (fdp
== NULL
19459 || lv
.ll_dict
== NULL
|| fdp
->fd_newkey
== NULL
))
19460 EMSG(_(e_funcref
));
19468 if (lv
.ll_name
== NULL
)
19470 /* Error found, but continue after the function name. */
19475 /* Check if the name is a Funcref. If so, use the value. */
19476 if (lv
.ll_exp_name
!= NULL
)
19478 len
= (int)STRLEN(lv
.ll_exp_name
);
19479 name
= deref_func_name(lv
.ll_exp_name
, &len
);
19480 if (name
== lv
.ll_exp_name
)
19485 len
= (int)(end
- *pp
);
19486 name
= deref_func_name(*pp
, &len
);
19492 name
= vim_strsave(name
);
19497 if (lv
.ll_exp_name
!= NULL
)
19499 len
= (int)STRLEN(lv
.ll_exp_name
);
19500 if (lead
<= 2 && lv
.ll_name
== lv
.ll_exp_name
19501 && STRNCMP(lv
.ll_name
, "s:", 2) == 0)
19503 /* When there was "s:" already or the name expanded to get a
19504 * leading "s:" then remove it. */
19512 if (lead
== 2) /* skip over "s:" */
19514 len
= (int)(end
- lv
.ll_name
);
19518 * Copy the function name to allocated memory.
19519 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19520 * Accept <SNR>123_name() outside a script.
19523 lead
= 0; /* do nothing */
19527 if ((lv
.ll_exp_name
!= NULL
&& eval_fname_sid(lv
.ll_exp_name
))
19528 || eval_fname_sid(*pp
))
19530 /* It's "s:" or "<SID>" */
19531 if (current_SID
<= 0)
19533 EMSG(_(e_usingsid
));
19536 sprintf((char *)sid_buf
, "%ld_", (long)current_SID
);
19537 lead
+= (int)STRLEN(sid_buf
);
19540 else if (!(flags
& TFN_INT
) && builtin_function(lv
.ll_name
))
19542 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv
.ll_name
);
19545 name
= alloc((unsigned)(len
+ lead
+ 1));
19550 name
[0] = K_SPECIAL
;
19551 name
[1] = KS_EXTRA
;
19552 name
[2] = (int)KE_SNR
;
19553 if (lead
> 3) /* If it's "<SID>" */
19554 STRCPY(name
+ 3, sid_buf
);
19556 mch_memmove(name
+ lead
, lv
.ll_name
, (size_t)len
);
19557 name
[len
+ lead
] = NUL
;
19567 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19568 * Return 2 if "p" starts with "s:".
19569 * Return 0 otherwise.
19572 eval_fname_script(p
)
19575 if (p
[0] == '<' && (STRNICMP(p
+ 1, "SID>", 4) == 0
19576 || STRNICMP(p
+ 1, "SNR>", 4) == 0))
19578 if (p
[0] == 's' && p
[1] == ':')
19584 * Return TRUE if "p" starts with "<SID>" or "s:".
19585 * Only works if eval_fname_script() returned non-zero for "p"!
19591 return (*p
== 's' || TOUPPER_ASC(p
[2]) == 'I');
19595 * List the head of the function: "name(arg1, arg2)".
19598 list_func_head(fp
, indent
)
19607 MSG_PUTS("function ");
19608 if (fp
->uf_name
[0] == K_SPECIAL
)
19610 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8
));
19611 msg_puts(fp
->uf_name
+ 3);
19614 msg_puts(fp
->uf_name
);
19616 for (j
= 0; j
< fp
->uf_args
.ga_len
; ++j
)
19620 msg_puts(FUNCARG(fp
, j
));
19622 if (fp
->uf_varargs
)
19631 last_set_msg(fp
->uf_script_ID
);
19635 * Find a function by name, return pointer to it in ufuncs.
19636 * Return NULL for unknown function.
19644 hi
= hash_find(&func_hashtab
, name
);
19645 if (!HASHITEM_EMPTY(hi
))
19650 #if defined(EXITFREE) || defined(PROTO)
19652 free_all_functions()
19656 /* Need to start all over every time, because func_free() may change the
19658 while (func_hashtab
.ht_used
> 0)
19659 for (hi
= func_hashtab
.ht_array
; ; ++hi
)
19660 if (!HASHITEM_EMPTY(hi
))
19662 func_free(HI2UF(hi
));
19669 * Return TRUE if a function "name" exists.
19672 function_exists(name
)
19679 p
= trans_function_name(&nm
, FALSE
, TFN_INT
|TFN_QUIET
, NULL
);
19680 nm
= skipwhite(nm
);
19682 /* Only accept "funcname", "funcname ", "funcname (..." and
19683 * "funcname(...", not "funcname!...". */
19684 if (p
!= NULL
&& (*nm
== NUL
|| *nm
== '('))
19686 if (builtin_function(p
))
19687 n
= (find_internal_func(p
) >= 0);
19689 n
= (find_func(p
) != NULL
);
19696 * Return TRUE if "name" looks like a builtin function name: starts with a
19697 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
19700 builtin_function(name
)
19703 return ASCII_ISLOWER(name
[0]) && vim_strchr(name
, ':') == NULL
19704 && vim_strchr(name
, AUTOLOAD_CHAR
) == NULL
;
19707 #if defined(FEAT_PROFILE) || defined(PROTO)
19709 * Start profiling function "fp".
19712 func_do_profile(fp
)
19715 fp
->uf_tm_count
= 0;
19716 profile_zero(&fp
->uf_tm_self
);
19717 profile_zero(&fp
->uf_tm_total
);
19718 if (fp
->uf_tml_count
== NULL
)
19719 fp
->uf_tml_count
= (int *)alloc_clear((unsigned)
19720 (sizeof(int) * fp
->uf_lines
.ga_len
));
19721 if (fp
->uf_tml_total
== NULL
)
19722 fp
->uf_tml_total
= (proftime_T
*)alloc_clear((unsigned)
19723 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
19724 if (fp
->uf_tml_self
== NULL
)
19725 fp
->uf_tml_self
= (proftime_T
*)alloc_clear((unsigned)
19726 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
19727 fp
->uf_tml_idx
= -1;
19728 if (fp
->uf_tml_count
== NULL
|| fp
->uf_tml_total
== NULL
19729 || fp
->uf_tml_self
== NULL
)
19730 return; /* out of memory */
19732 fp
->uf_profiling
= TRUE
;
19736 * Dump the profiling results for all functions in file "fd".
19739 func_dump_profile(fd
)
19749 todo
= (int)func_hashtab
.ht_used
;
19750 sorttab
= (ufunc_T
**)alloc((unsigned)(sizeof(ufunc_T
) * todo
));
19752 for (hi
= func_hashtab
.ht_array
; todo
> 0; ++hi
)
19754 if (!HASHITEM_EMPTY(hi
))
19758 if (fp
->uf_profiling
)
19760 if (sorttab
!= NULL
)
19761 sorttab
[st_len
++] = fp
;
19763 if (fp
->uf_name
[0] == K_SPECIAL
)
19764 fprintf(fd
, "FUNCTION <SNR>%s()\n", fp
->uf_name
+ 3);
19766 fprintf(fd
, "FUNCTION %s()\n", fp
->uf_name
);
19767 if (fp
->uf_tm_count
== 1)
19768 fprintf(fd
, "Called 1 time\n");
19770 fprintf(fd
, "Called %d times\n", fp
->uf_tm_count
);
19771 fprintf(fd
, "Total time: %s\n", profile_msg(&fp
->uf_tm_total
));
19772 fprintf(fd
, " Self time: %s\n", profile_msg(&fp
->uf_tm_self
));
19774 fprintf(fd
, "count total (s) self (s)\n");
19776 for (i
= 0; i
< fp
->uf_lines
.ga_len
; ++i
)
19778 if (FUNCLINE(fp
, i
) == NULL
)
19780 prof_func_line(fd
, fp
->uf_tml_count
[i
],
19781 &fp
->uf_tml_total
[i
], &fp
->uf_tml_self
[i
], TRUE
);
19782 fprintf(fd
, "%s\n", FUNCLINE(fp
, i
));
19789 if (sorttab
!= NULL
&& st_len
> 0)
19791 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
19793 prof_sort_list(fd
, sorttab
, st_len
, "TOTAL", FALSE
);
19794 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
19796 prof_sort_list(fd
, sorttab
, st_len
, "SELF", TRUE
);
19801 prof_sort_list(fd
, sorttab
, st_len
, title
, prefer_self
)
19806 int prefer_self
; /* when equal print only self time */
19811 fprintf(fd
, "FUNCTIONS SORTED ON %s TIME\n", title
);
19812 fprintf(fd
, "count total (s) self (s) function\n");
19813 for (i
= 0; i
< 20 && i
< st_len
; ++i
)
19816 prof_func_line(fd
, fp
->uf_tm_count
, &fp
->uf_tm_total
, &fp
->uf_tm_self
,
19818 if (fp
->uf_name
[0] == K_SPECIAL
)
19819 fprintf(fd
, " <SNR>%s()\n", fp
->uf_name
+ 3);
19821 fprintf(fd
, " %s()\n", fp
->uf_name
);
19827 * Print the count and times for one function or function line.
19830 prof_func_line(fd
, count
, total
, self
, prefer_self
)
19835 int prefer_self
; /* when equal print only self time */
19839 fprintf(fd
, "%5d ", count
);
19840 if (prefer_self
&& profile_equal(total
, self
))
19843 fprintf(fd
, "%s ", profile_msg(total
));
19844 if (!prefer_self
&& profile_equal(total
, self
))
19847 fprintf(fd
, "%s ", profile_msg(self
));
19854 * Compare function for total time sorting.
19857 #ifdef __BORLANDC__
19860 prof_total_cmp(s1
, s2
)
19866 p1
= *(ufunc_T
**)s1
;
19867 p2
= *(ufunc_T
**)s2
;
19868 return profile_cmp(&p1
->uf_tm_total
, &p2
->uf_tm_total
);
19872 * Compare function for self time sorting.
19875 #ifdef __BORLANDC__
19878 prof_self_cmp(s1
, s2
)
19884 p1
= *(ufunc_T
**)s1
;
19885 p2
= *(ufunc_T
**)s2
;
19886 return profile_cmp(&p1
->uf_tm_self
, &p2
->uf_tm_self
);
19892 * If "name" has a package name try autoloading the script for it.
19893 * Return TRUE if a package was loaded.
19896 script_autoload(name
, reload
)
19898 int reload
; /* load script again when already loaded */
19901 char_u
*scriptname
, *tofree
;
19905 /* If there is no '#' after name[0] there is no package name. */
19906 p
= vim_strchr(name
, AUTOLOAD_CHAR
);
19907 if (p
== NULL
|| p
== name
)
19910 tofree
= scriptname
= autoload_name(name
);
19912 /* Find the name in the list of previously loaded package names. Skip
19913 * "autoload/", it's always the same. */
19914 for (i
= 0; i
< ga_loaded
.ga_len
; ++i
)
19915 if (STRCMP(((char_u
**)ga_loaded
.ga_data
)[i
] + 9, scriptname
+ 9) == 0)
19917 if (!reload
&& i
< ga_loaded
.ga_len
)
19918 ret
= FALSE
; /* was loaded already */
19921 /* Remember the name if it wasn't loaded already. */
19922 if (i
== ga_loaded
.ga_len
&& ga_grow(&ga_loaded
, 1) == OK
)
19924 ((char_u
**)ga_loaded
.ga_data
)[ga_loaded
.ga_len
++] = scriptname
;
19928 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
19929 if (source_runtime(scriptname
, FALSE
) == OK
)
19938 * Return the autoload script name for a function or variable name.
19939 * Returns NULL when out of memory.
19942 autoload_name(name
)
19946 char_u
*scriptname
;
19948 /* Get the script file name: replace '#' with '/', append ".vim". */
19949 scriptname
= alloc((unsigned)(STRLEN(name
) + 14));
19950 if (scriptname
== NULL
)
19952 STRCPY(scriptname
, "autoload/");
19953 STRCAT(scriptname
, name
);
19954 *vim_strrchr(scriptname
, AUTOLOAD_CHAR
) = NUL
;
19955 STRCAT(scriptname
, ".vim");
19956 while ((p
= vim_strchr(scriptname
, AUTOLOAD_CHAR
)) != NULL
)
19961 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19964 * Function given to ExpandGeneric() to obtain the list of user defined
19968 get_user_func_name(xp
, idx
)
19972 static long_u done
;
19973 static hashitem_T
*hi
;
19979 hi
= func_hashtab
.ht_array
;
19981 if (done
< func_hashtab
.ht_used
)
19985 while (HASHITEM_EMPTY(hi
))
19989 if (STRLEN(fp
->uf_name
) + 4 >= IOSIZE
)
19990 return fp
->uf_name
; /* prevents overflow */
19992 cat_func_name(IObuff
, fp
);
19993 if (xp
->xp_context
!= EXPAND_USER_FUNC
)
19995 STRCAT(IObuff
, "(");
19996 if (!fp
->uf_varargs
&& fp
->uf_args
.ga_len
== 0)
19997 STRCAT(IObuff
, ")");
20004 #endif /* FEAT_CMDL_COMPL */
20007 * Copy the function name of "fp" to buffer "buf".
20008 * "buf" must be able to hold the function name plus three bytes.
20009 * Takes care of script-local function names.
20012 cat_func_name(buf
, fp
)
20016 if (fp
->uf_name
[0] == K_SPECIAL
)
20018 STRCPY(buf
, "<SNR>");
20019 STRCAT(buf
, fp
->uf_name
+ 3);
20022 STRCPY(buf
, fp
->uf_name
);
20026 * ":delfunction {name}"
20029 ex_delfunction(eap
)
20032 ufunc_T
*fp
= NULL
;
20038 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
20039 vim_free(fudi
.fd_newkey
);
20042 if (fudi
.fd_dict
!= NULL
&& !eap
->skip
)
20043 EMSG(_(e_funcref
));
20046 if (!ends_excmd(*skipwhite(p
)))
20049 EMSG(_(e_trailing
));
20052 eap
->nextcmd
= check_nextcmd(p
);
20053 if (eap
->nextcmd
!= NULL
)
20057 fp
= find_func(name
);
20064 EMSG2(_(e_nofunc
), eap
->arg
);
20067 if (fp
->uf_calls
> 0)
20069 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap
->arg
);
20073 if (fudi
.fd_dict
!= NULL
)
20075 /* Delete the dict item that refers to the function, it will
20076 * invoke func_unref() and possibly delete the function. */
20077 dictitem_remove(fudi
.fd_dict
, fudi
.fd_di
);
20085 * Free a function and remove it from the list of functions.
20093 /* clear this function */
20094 ga_clear_strings(&(fp
->uf_args
));
20095 ga_clear_strings(&(fp
->uf_lines
));
20096 #ifdef FEAT_PROFILE
20097 vim_free(fp
->uf_tml_count
);
20098 vim_free(fp
->uf_tml_total
);
20099 vim_free(fp
->uf_tml_self
);
20102 /* remove the function from the function hashtable */
20103 hi
= hash_find(&func_hashtab
, UF2HIKEY(fp
));
20104 if (HASHITEM_EMPTY(hi
))
20105 EMSG2(_(e_intern2
), "func_free()");
20107 hash_remove(&func_hashtab
, hi
);
20113 * Unreference a Function: decrement the reference count and free it when it
20114 * becomes zero. Only for numbered functions.
20122 if (name
!= NULL
&& isdigit(*name
))
20124 fp
= find_func(name
);
20126 EMSG2(_(e_intern2
), "func_unref()");
20127 else if (--fp
->uf_refcount
<= 0)
20129 /* Only delete it when it's not being used. Otherwise it's done
20130 * when "uf_calls" becomes zero. */
20131 if (fp
->uf_calls
== 0)
20138 * Count a reference to a Function.
20146 if (name
!= NULL
&& isdigit(*name
))
20148 fp
= find_func(name
);
20150 EMSG2(_(e_intern2
), "func_ref()");
20157 * Call a user function.
20160 call_user_func(fp
, argcount
, argvars
, rettv
, firstline
, lastline
, selfdict
)
20161 ufunc_T
*fp
; /* pointer to function */
20162 int argcount
; /* nr of args */
20163 typval_T
*argvars
; /* arguments */
20164 typval_T
*rettv
; /* return value */
20165 linenr_T firstline
; /* first line of range */
20166 linenr_T lastline
; /* last line of range */
20167 dict_T
*selfdict
; /* Dictionary for "self" */
20169 char_u
*save_sourcing_name
;
20170 linenr_T save_sourcing_lnum
;
20171 scid_T save_current_SID
;
20174 static int depth
= 0;
20176 int fixvar_idx
= 0; /* index in fixvar[] */
20179 char_u numbuf
[NUMBUFLEN
];
20181 #ifdef FEAT_PROFILE
20182 proftime_T wait_start
;
20183 proftime_T call_start
;
20186 /* If depth of calling is getting too high, don't execute the function */
20187 if (depth
>= p_mfd
)
20189 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
20190 rettv
->v_type
= VAR_NUMBER
;
20191 rettv
->vval
.v_number
= -1;
20196 line_breakcheck(); /* check for CTRL-C hit */
20198 fc
.caller
= current_funccal
;
20199 current_funccal
= &fc
;
20202 rettv
->vval
.v_number
= 0;
20204 fc
.returned
= FALSE
;
20205 fc
.level
= ex_nesting_level
;
20206 /* Check if this function has a breakpoint. */
20207 fc
.breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
, (linenr_T
)0);
20208 fc
.dbg_tick
= debug_tick
;
20211 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
20212 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
20213 * each argument variable and saves a lot of time.
20216 * Init l: variables.
20218 init_var_dict(&fc
.l_vars
, &fc
.l_vars_var
);
20219 if (selfdict
!= NULL
)
20221 /* Set l:self to "selfdict". Use "name" to avoid a warning from
20222 * some compiler that checks the destination size. */
20223 v
= &fc
.fixvar
[fixvar_idx
++].var
;
20225 STRCPY(name
, "self");
20226 v
->di_flags
= DI_FLAGS_RO
+ DI_FLAGS_FIX
;
20227 hash_add(&fc
.l_vars
.dv_hashtab
, DI2HIKEY(v
));
20228 v
->di_tv
.v_type
= VAR_DICT
;
20229 v
->di_tv
.v_lock
= 0;
20230 v
->di_tv
.vval
.v_dict
= selfdict
;
20231 ++selfdict
->dv_refcount
;
20235 * Init a: variables.
20236 * Set a:0 to "argcount".
20237 * Set a:000 to a list with room for the "..." arguments.
20239 init_var_dict(&fc
.l_avars
, &fc
.l_avars_var
);
20240 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "0",
20241 (varnumber_T
)(argcount
- fp
->uf_args
.ga_len
));
20242 v
= &fc
.fixvar
[fixvar_idx
++].var
;
20243 STRCPY(v
->di_key
, "000");
20244 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
20245 hash_add(&fc
.l_avars
.dv_hashtab
, DI2HIKEY(v
));
20246 v
->di_tv
.v_type
= VAR_LIST
;
20247 v
->di_tv
.v_lock
= VAR_FIXED
;
20248 v
->di_tv
.vval
.v_list
= &fc
.l_varlist
;
20249 vim_memset(&fc
.l_varlist
, 0, sizeof(list_T
));
20250 fc
.l_varlist
.lv_refcount
= 99999;
20251 fc
.l_varlist
.lv_lock
= VAR_FIXED
;
20254 * Set a:firstline to "firstline" and a:lastline to "lastline".
20255 * Set a:name to named arguments.
20256 * Set a:N to the "..." arguments.
20258 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "firstline",
20259 (varnumber_T
)firstline
);
20260 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "lastline",
20261 (varnumber_T
)lastline
);
20262 for (i
= 0; i
< argcount
; ++i
)
20264 ai
= i
- fp
->uf_args
.ga_len
;
20266 /* named argument a:name */
20267 name
= FUNCARG(fp
, i
);
20270 /* "..." argument a:1, a:2, etc. */
20271 sprintf((char *)numbuf
, "%d", ai
+ 1);
20274 if (fixvar_idx
< FIXVAR_CNT
&& STRLEN(name
) <= VAR_SHORT_LEN
)
20276 v
= &fc
.fixvar
[fixvar_idx
++].var
;
20277 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
20281 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
20285 v
->di_flags
= DI_FLAGS_RO
;
20287 STRCPY(v
->di_key
, name
);
20288 hash_add(&fc
.l_avars
.dv_hashtab
, DI2HIKEY(v
));
20290 /* Note: the values are copied directly to avoid alloc/free.
20291 * "argvars" must have VAR_FIXED for v_lock. */
20292 v
->di_tv
= argvars
[i
];
20293 v
->di_tv
.v_lock
= VAR_FIXED
;
20295 if (ai
>= 0 && ai
< MAX_FUNC_ARGS
)
20297 list_append(&fc
.l_varlist
, &fc
.l_listitems
[ai
]);
20298 fc
.l_listitems
[ai
].li_tv
= argvars
[i
];
20299 fc
.l_listitems
[ai
].li_tv
.v_lock
= VAR_FIXED
;
20303 /* Don't redraw while executing the function. */
20304 ++RedrawingDisabled
;
20305 save_sourcing_name
= sourcing_name
;
20306 save_sourcing_lnum
= sourcing_lnum
;
20308 sourcing_name
= alloc((unsigned)((save_sourcing_name
== NULL
? 0
20309 : STRLEN(save_sourcing_name
)) + STRLEN(fp
->uf_name
) + 13));
20310 if (sourcing_name
!= NULL
)
20312 if (save_sourcing_name
!= NULL
20313 && STRNCMP(save_sourcing_name
, "function ", 9) == 0)
20314 sprintf((char *)sourcing_name
, "%s..", save_sourcing_name
);
20316 STRCPY(sourcing_name
, "function ");
20317 cat_func_name(sourcing_name
+ STRLEN(sourcing_name
), fp
);
20319 if (p_verbose
>= 12)
20322 verbose_enter_scroll();
20324 smsg((char_u
*)_("calling %s"), sourcing_name
);
20325 if (p_verbose
>= 14)
20327 char_u buf
[MSG_BUF_LEN
];
20328 char_u numbuf2
[NUMBUFLEN
];
20332 msg_puts((char_u
*)"(");
20333 for (i
= 0; i
< argcount
; ++i
)
20336 msg_puts((char_u
*)", ");
20337 if (argvars
[i
].v_type
== VAR_NUMBER
)
20338 msg_outnum((long)argvars
[i
].vval
.v_number
);
20341 s
= tv2string(&argvars
[i
], &tofree
, numbuf2
, 0);
20344 trunc_string(s
, buf
, MSG_BUF_CLEN
);
20350 msg_puts((char_u
*)")");
20352 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
20354 verbose_leave_scroll();
20358 #ifdef FEAT_PROFILE
20359 if (do_profiling
== PROF_YES
)
20361 if (!fp
->uf_profiling
&& has_profiling(FALSE
, fp
->uf_name
, NULL
))
20362 func_do_profile(fp
);
20363 if (fp
->uf_profiling
20364 || (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
))
20367 profile_start(&call_start
);
20368 profile_zero(&fp
->uf_tm_children
);
20370 script_prof_save(&wait_start
);
20374 save_current_SID
= current_SID
;
20375 current_SID
= fp
->uf_script_ID
;
20376 save_did_emsg
= did_emsg
;
20379 /* call do_cmdline() to execute the lines */
20380 do_cmdline(NULL
, get_func_line
, (void *)&fc
,
20381 DOCMD_NOWAIT
|DOCMD_VERBOSE
|DOCMD_REPEAT
);
20383 --RedrawingDisabled
;
20385 /* when the function was aborted because of an error, return -1 */
20386 if ((did_emsg
&& (fp
->uf_flags
& FC_ABORT
)) || rettv
->v_type
== VAR_UNKNOWN
)
20389 rettv
->v_type
= VAR_NUMBER
;
20390 rettv
->vval
.v_number
= -1;
20393 #ifdef FEAT_PROFILE
20394 if (do_profiling
== PROF_YES
&& (fp
->uf_profiling
20395 || (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
)))
20397 profile_end(&call_start
);
20398 profile_sub_wait(&wait_start
, &call_start
);
20399 profile_add(&fp
->uf_tm_total
, &call_start
);
20400 profile_self(&fp
->uf_tm_self
, &call_start
, &fp
->uf_tm_children
);
20401 if (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
)
20403 profile_add(&fc
.caller
->func
->uf_tm_children
, &call_start
);
20404 profile_add(&fc
.caller
->func
->uf_tml_children
, &call_start
);
20409 /* when being verbose, mention the return value */
20410 if (p_verbose
>= 12)
20413 verbose_enter_scroll();
20416 smsg((char_u
*)_("%s aborted"), sourcing_name
);
20417 else if (fc
.rettv
->v_type
== VAR_NUMBER
)
20418 smsg((char_u
*)_("%s returning #%ld"), sourcing_name
,
20419 (long)fc
.rettv
->vval
.v_number
);
20422 char_u buf
[MSG_BUF_LEN
];
20423 char_u numbuf2
[NUMBUFLEN
];
20427 /* The value may be very long. Skip the middle part, so that we
20428 * have some idea how it starts and ends. smsg() would always
20429 * truncate it at the end. */
20430 s
= tv2string(fc
.rettv
, &tofree
, numbuf2
, 0);
20433 trunc_string(s
, buf
, MSG_BUF_CLEN
);
20434 smsg((char_u
*)_("%s returning %s"), sourcing_name
, buf
);
20438 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
20440 verbose_leave_scroll();
20444 vim_free(sourcing_name
);
20445 sourcing_name
= save_sourcing_name
;
20446 sourcing_lnum
= save_sourcing_lnum
;
20447 current_SID
= save_current_SID
;
20448 #ifdef FEAT_PROFILE
20449 if (do_profiling
== PROF_YES
)
20450 script_prof_restore(&wait_start
);
20453 if (p_verbose
>= 12 && sourcing_name
!= NULL
)
20456 verbose_enter_scroll();
20458 smsg((char_u
*)_("continuing in %s"), sourcing_name
);
20459 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
20461 verbose_leave_scroll();
20465 did_emsg
|= save_did_emsg
;
20466 current_funccal
= fc
.caller
;
20468 /* The a: variables typevals were not alloced, only free the allocated
20470 vars_clear_ext(&fc
.l_avars
.dv_hashtab
, FALSE
);
20472 vars_clear(&fc
.l_vars
.dv_hashtab
); /* free all l: variables */
20477 * Add a number variable "name" to dict "dp" with value "nr".
20480 add_nr_var(dp
, v
, name
, nr
)
20486 STRCPY(v
->di_key
, name
);
20487 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
20488 hash_add(&dp
->dv_hashtab
, DI2HIKEY(v
));
20489 v
->di_tv
.v_type
= VAR_NUMBER
;
20490 v
->di_tv
.v_lock
= VAR_FIXED
;
20491 v
->di_tv
.vval
.v_number
= nr
;
20501 char_u
*arg
= eap
->arg
;
20503 int returning
= FALSE
;
20505 if (current_funccal
== NULL
)
20507 EMSG(_("E133: :return not inside a function"));
20514 eap
->nextcmd
= NULL
;
20515 if ((*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
20516 && eval0(arg
, &rettv
, &eap
->nextcmd
, !eap
->skip
) != FAIL
)
20519 returning
= do_return(eap
, FALSE
, TRUE
, &rettv
);
20523 /* It's safer to return also on error. */
20524 else if (!eap
->skip
)
20527 * Return unless the expression evaluation has been cancelled due to an
20528 * aborting error, an interrupt, or an exception.
20531 returning
= do_return(eap
, FALSE
, TRUE
, NULL
);
20534 /* When skipping or the return gets pending, advance to the next command
20535 * in this line (!returning). Otherwise, ignore the rest of the line.
20536 * Following lines will be ignored by get_func_line(). */
20538 eap
->nextcmd
= NULL
;
20539 else if (eap
->nextcmd
== NULL
) /* no argument */
20540 eap
->nextcmd
= check_nextcmd(arg
);
20547 * Return from a function. Possibly makes the return pending. Also called
20548 * for a pending return at the ":endtry" or after returning from an extra
20549 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
20550 * when called due to a ":return" command. "rettv" may point to a typval_T
20551 * with the return rettv. Returns TRUE when the return can be carried out,
20552 * FALSE when the return gets pending.
20555 do_return(eap
, reanimate
, is_cmd
, rettv
)
20562 struct condstack
*cstack
= eap
->cstack
;
20565 /* Undo the return. */
20566 current_funccal
->returned
= FALSE
;
20569 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20570 * not in its finally clause (which then is to be executed next) is found.
20571 * In this case, make the ":return" pending for execution at the ":endtry".
20572 * Otherwise, return normally.
20574 idx
= cleanup_conditionals(eap
->cstack
, 0, TRUE
);
20577 cstack
->cs_pending
[idx
] = CSTP_RETURN
;
20579 if (!is_cmd
&& !reanimate
)
20580 /* A pending return again gets pending. "rettv" points to an
20581 * allocated variable with the rettv of the original ":return"'s
20582 * argument if present or is NULL else. */
20583 cstack
->cs_rettv
[idx
] = rettv
;
20586 /* When undoing a return in order to make it pending, get the stored
20589 rettv
= current_funccal
->rettv
;
20593 /* Store the value of the pending return. */
20594 if ((cstack
->cs_rettv
[idx
] = alloc_tv()) != NULL
)
20595 *(typval_T
*)cstack
->cs_rettv
[idx
] = *(typval_T
*)rettv
;
20597 EMSG(_(e_outofmem
));
20600 cstack
->cs_rettv
[idx
] = NULL
;
20604 /* The pending return value could be overwritten by a ":return"
20605 * without argument in a finally clause; reset the default
20607 current_funccal
->rettv
->v_type
= VAR_NUMBER
;
20608 current_funccal
->rettv
->vval
.v_number
= 0;
20611 report_make_pending(CSTP_RETURN
, rettv
);
20615 current_funccal
->returned
= TRUE
;
20617 /* If the return is carried out now, store the return value. For
20618 * a return immediately after reanimation, the value is already
20620 if (!reanimate
&& rettv
!= NULL
)
20622 clear_tv(current_funccal
->rettv
);
20623 *current_funccal
->rettv
= *(typval_T
*)rettv
;
20633 * Free the variable with a pending return value.
20636 discard_pending_return(rettv
)
20639 free_tv((typval_T
*)rettv
);
20643 * Generate a return command for producing the value of "rettv". The result
20644 * is an allocated string. Used by report_pending() for verbose messages.
20647 get_return_cmd(rettv
)
20651 char_u
*tofree
= NULL
;
20652 char_u numbuf
[NUMBUFLEN
];
20655 s
= echo_string((typval_T
*)rettv
, &tofree
, numbuf
, 0);
20659 STRCPY(IObuff
, ":return ");
20660 STRNCPY(IObuff
+ 8, s
, IOSIZE
- 8);
20661 if (STRLEN(s
) + 8 >= IOSIZE
)
20662 STRCPY(IObuff
+ IOSIZE
- 4, "...");
20664 return vim_strsave(IObuff
);
20668 * Get next function line.
20669 * Called by do_cmdline() to get the next line.
20670 * Returns allocated string, or NULL for end of function.
20674 get_func_line(c
, cookie
, indent
)
20675 int c
; /* not used */
20677 int indent
; /* not used */
20679 funccall_T
*fcp
= (funccall_T
*)cookie
;
20680 ufunc_T
*fp
= fcp
->func
;
20682 garray_T
*gap
; /* growarray with function lines */
20684 /* If breakpoints have been added/deleted need to check for it. */
20685 if (fcp
->dbg_tick
!= debug_tick
)
20687 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
20689 fcp
->dbg_tick
= debug_tick
;
20691 #ifdef FEAT_PROFILE
20692 if (do_profiling
== PROF_YES
)
20693 func_line_end(cookie
);
20696 gap
= &fp
->uf_lines
;
20697 if (((fp
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
20702 /* Skip NULL lines (continuation lines). */
20703 while (fcp
->linenr
< gap
->ga_len
20704 && ((char_u
**)(gap
->ga_data
))[fcp
->linenr
] == NULL
)
20706 if (fcp
->linenr
>= gap
->ga_len
)
20710 retval
= vim_strsave(((char_u
**)(gap
->ga_data
))[fcp
->linenr
++]);
20711 sourcing_lnum
= fcp
->linenr
;
20712 #ifdef FEAT_PROFILE
20713 if (do_profiling
== PROF_YES
)
20714 func_line_start(cookie
);
20719 /* Did we encounter a breakpoint? */
20720 if (fcp
->breakpoint
!= 0 && fcp
->breakpoint
<= sourcing_lnum
)
20722 dbg_breakpoint(fp
->uf_name
, sourcing_lnum
);
20723 /* Find next breakpoint. */
20724 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
20726 fcp
->dbg_tick
= debug_tick
;
20732 #if defined(FEAT_PROFILE) || defined(PROTO)
20734 * Called when starting to read a function line.
20735 * "sourcing_lnum" must be correct!
20736 * When skipping lines it may not actually be executed, but we won't find out
20737 * until later and we need to store the time now.
20740 func_line_start(cookie
)
20743 funccall_T
*fcp
= (funccall_T
*)cookie
;
20744 ufunc_T
*fp
= fcp
->func
;
20746 if (fp
->uf_profiling
&& sourcing_lnum
>= 1
20747 && sourcing_lnum
<= fp
->uf_lines
.ga_len
)
20749 fp
->uf_tml_idx
= sourcing_lnum
- 1;
20750 /* Skip continuation lines. */
20751 while (fp
->uf_tml_idx
> 0 && FUNCLINE(fp
, fp
->uf_tml_idx
) == NULL
)
20753 fp
->uf_tml_execed
= FALSE
;
20754 profile_start(&fp
->uf_tml_start
);
20755 profile_zero(&fp
->uf_tml_children
);
20756 profile_get_wait(&fp
->uf_tml_wait
);
20761 * Called when actually executing a function line.
20764 func_line_exec(cookie
)
20767 funccall_T
*fcp
= (funccall_T
*)cookie
;
20768 ufunc_T
*fp
= fcp
->func
;
20770 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
20771 fp
->uf_tml_execed
= TRUE
;
20775 * Called when done with a function line.
20778 func_line_end(cookie
)
20781 funccall_T
*fcp
= (funccall_T
*)cookie
;
20782 ufunc_T
*fp
= fcp
->func
;
20784 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
20786 if (fp
->uf_tml_execed
)
20788 ++fp
->uf_tml_count
[fp
->uf_tml_idx
];
20789 profile_end(&fp
->uf_tml_start
);
20790 profile_sub_wait(&fp
->uf_tml_wait
, &fp
->uf_tml_start
);
20791 profile_add(&fp
->uf_tml_total
[fp
->uf_tml_idx
], &fp
->uf_tml_start
);
20792 profile_self(&fp
->uf_tml_self
[fp
->uf_tml_idx
], &fp
->uf_tml_start
,
20793 &fp
->uf_tml_children
);
20795 fp
->uf_tml_idx
= -1;
20801 * Return TRUE if the currently active function should be ended, because a
20802 * return was encountered or an error occured. Used inside a ":while".
20805 func_has_ended(cookie
)
20808 funccall_T
*fcp
= (funccall_T
*)cookie
;
20810 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20811 * an error inside a try conditional. */
20812 return (((fcp
->func
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
20817 * return TRUE if cookie indicates a function which "abort"s on errors.
20820 func_has_abort(cookie
)
20823 return ((funccall_T
*)cookie
)->func
->uf_flags
& FC_ABORT
;
20826 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20829 VAR_FLAVOUR_DEFAULT
,
20830 VAR_FLAVOUR_SESSION
,
20831 VAR_FLAVOUR_VIMINFO
20834 static var_flavour_T var_flavour
__ARGS((char_u
*varname
));
20836 static var_flavour_T
20837 var_flavour(varname
)
20840 char_u
*p
= varname
;
20842 if (ASCII_ISUPPER(*p
))
20845 if (ASCII_ISLOWER(*p
))
20846 return VAR_FLAVOUR_SESSION
;
20847 return VAR_FLAVOUR_VIMINFO
;
20850 return VAR_FLAVOUR_DEFAULT
;
20854 #if defined(FEAT_VIMINFO) || defined(PROTO)
20856 * Restore global vars that start with a capital from the viminfo file
20859 read_viminfo_varlist(virp
, writing
)
20864 int is_string
= FALSE
;
20867 if (!writing
&& (find_viminfo_parameter('!') != NULL
))
20869 tab
= vim_strchr(virp
->vir_line
+ 1, '\t');
20872 *tab
++ = '\0'; /* isolate the variable name */
20873 if (*tab
== 'S') /* string var */
20876 tab
= vim_strchr(tab
, '\t');
20881 tv
.v_type
= VAR_STRING
;
20882 tv
.vval
.v_string
= viminfo_readstring(virp
,
20883 (int)(tab
- virp
->vir_line
+ 1), TRUE
);
20887 tv
.v_type
= VAR_NUMBER
;
20888 tv
.vval
.v_number
= atol((char *)tab
+ 1);
20890 set_var(virp
->vir_line
+ 1, &tv
, FALSE
);
20892 vim_free(tv
.vval
.v_string
);
20897 return viminfo_readline(virp
);
20901 * Write global vars that start with a capital to the viminfo file
20904 write_viminfo_varlist(fp
)
20908 dictitem_T
*this_var
;
20913 char_u numbuf
[NUMBUFLEN
];
20915 if (find_viminfo_parameter('!') == NULL
)
20918 fprintf(fp
, _("\n# global variables:\n"));
20920 todo
= (int)globvarht
.ht_used
;
20921 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
20923 if (!HASHITEM_EMPTY(hi
))
20926 this_var
= HI2DI(hi
);
20927 if (var_flavour(this_var
->di_key
) == VAR_FLAVOUR_VIMINFO
)
20929 switch (this_var
->di_tv
.v_type
)
20931 case VAR_STRING
: s
= "STR"; break;
20932 case VAR_NUMBER
: s
= "NUM"; break;
20935 fprintf(fp
, "!%s\t%s\t", this_var
->di_key
, s
);
20936 p
= echo_string(&this_var
->di_tv
, &tofree
, numbuf
, 0);
20938 viminfo_writestring(fp
, p
);
20946 #if defined(FEAT_SESSION) || defined(PROTO)
20948 store_session_globals(fd
)
20952 dictitem_T
*this_var
;
20956 todo
= (int)globvarht
.ht_used
;
20957 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
20959 if (!HASHITEM_EMPTY(hi
))
20962 this_var
= HI2DI(hi
);
20963 if ((this_var
->di_tv
.v_type
== VAR_NUMBER
20964 || this_var
->di_tv
.v_type
== VAR_STRING
)
20965 && var_flavour(this_var
->di_key
) == VAR_FLAVOUR_SESSION
)
20967 /* Escape special characters with a backslash. Turn a LF and
20968 * CR into \n and \r. */
20969 p
= vim_strsave_escaped(get_tv_string(&this_var
->di_tv
),
20970 (char_u
*)"\\\"\n\r");
20971 if (p
== NULL
) /* out of memory */
20973 for (t
= p
; *t
!= NUL
; ++t
)
20976 else if (*t
== '\r')
20978 if ((fprintf(fd
, "let %s = %c%s%c",
20980 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
20983 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
20985 || put_eol(fd
) == FAIL
)
20999 * Display script name where an item was last set.
21000 * Should only be invoked when 'verbose' is non-zero.
21003 last_set_msg(scriptID
)
21010 p
= home_replace_save(NULL
, get_scriptname(scriptID
));
21014 MSG_PUTS(_("\n\tLast set from "));
21022 #endif /* FEAT_EVAL */
21024 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
21029 * Functions for ":8" filename modifier: get 8.3 version of a filename.
21031 static int get_short_pathname
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
21032 static int shortpath_for_invalid_fname
__ARGS((char_u
**fname
, char_u
**bufp
, int *fnamelen
));
21033 static int shortpath_for_partial
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
21036 * Get the short pathname of a file.
21037 * Returns 1 on success. *fnamelen is 0 for nonexistent path.
21040 get_short_pathname(fnamep
, bufp
, fnamelen
)
21050 l
= GetShortPathName(*fnamep
, *fnamep
, len
);
21053 /* If that doesn't work (not enough space), then save the string
21054 * and try again with a new buffer big enough
21056 newbuf
= vim_strnsave(*fnamep
, l
);
21057 if (newbuf
== NULL
)
21061 *fnamep
= *bufp
= newbuf
;
21063 l
= GetShortPathName(*fnamep
,*fnamep
,l
+1);
21065 /* Really should always succeed, as the buffer is big enough */
21073 * Create a short path name. Returns the length of the buffer it needs.
21074 * Doesn't copy over the end of the buffer passed in.
21077 shortpath_for_invalid_fname(fname
, bufp
, fnamelen
)
21082 char_u
*s
, *p
, *pbuf2
, *pbuf3
;
21084 int len
, len2
, plen
, slen
;
21088 pbuf2
= vim_strnsave(*fname
, len2
);
21091 s
= pbuf2
+ len2
- 1; /* Find the end */
21095 if (after_pathsep(pbuf2
, s
+ 1))
21104 /* Go back one path-separator */
21105 while (s
> pbuf2
&& !after_pathsep(pbuf2
, s
+ 1))
21114 /* Remember the character that is about to be splatted */
21116 *s
= 0; /* get_short_pathname requires a null-terminated string */
21118 /* Try it in situ */
21120 if (!get_short_pathname(&p
, &pbuf3
, &plen
))
21125 *s
= ch
; /* Preserve the string */
21126 } while (plen
== 0);
21130 /* Remember the length of the new string. */
21131 *fnamelen
= len
= plen
+ slen
;
21135 /* If there's not enough space in the currently allocated string,
21136 * then copy it to a buffer big enough.
21138 *fname
= *bufp
= vim_strnsave(p
, len
);
21139 if (*fname
== NULL
)
21144 /* Transfer pbuf2 to being the main buffer (it's big enough) */
21145 *fname
= *bufp
= pbuf2
;
21147 strncpy(*fname
, p
, plen
);
21150 /* Concat the next bit */
21151 strncpy(*fname
+ plen
, s
, slen
);
21152 (*fname
)[len
] = '\0';
21160 * Get a pathname for a partial path.
21163 shortpath_for_partial(fnamep
, bufp
, fnamelen
)
21168 int sepcount
, len
, tflen
;
21170 char_u
*pbuf
, *tfname
;
21173 /* Count up the path seperators from the RHS.. so we know which part
21174 * of the path to return.
21177 for (p
= *fnamep
; p
< *fnamep
+ *fnamelen
; mb_ptr_adv(p
))
21178 if (vim_ispathsep(*p
))
21181 /* Need full path first (use expand_env() to remove a "~/") */
21182 hasTilde
= (**fnamep
== '~');
21184 pbuf
= tfname
= expand_env_save(*fnamep
);
21186 pbuf
= tfname
= FullName_save(*fnamep
, FALSE
);
21188 len
= tflen
= (int)STRLEN(tfname
);
21190 if (!get_short_pathname(&tfname
, &pbuf
, &len
))
21195 /* Don't have a valid filename, so shorten the rest of the
21196 * path if we can. This CAN give us invalid 8.3 filenames, but
21197 * there's not a lot of point in guessing what it might be.
21200 if (shortpath_for_invalid_fname(&tfname
, &pbuf
, &len
) == -1)
21204 /* Count the paths backward to find the beginning of the desired string. */
21205 for (p
= tfname
+ len
- 1; p
>= tfname
; --p
)
21209 p
-= mb_head_off(tfname
, p
);
21211 if (vim_ispathsep(*p
))
21213 if (sepcount
== 0 || (hasTilde
&& sepcount
== 1))
21230 /* Copy in the string - p indexes into tfname - allocated at pbuf */
21232 *fnamelen
= (int)STRLEN(p
);
21238 #endif /* WIN3264 */
21241 * Adjust a filename, according to a string of modifiers.
21242 * *fnamep must be NUL terminated when called. When returning, the length is
21243 * determined by *fnamelen.
21244 * Returns valid flags.
21245 * When there is an error, *fnamep is set to NULL.
21248 modify_fname(src
, usedlen
, fnamep
, bufp
, fnamelen
)
21249 char_u
*src
; /* string with modifiers */
21250 int *usedlen
; /* characters after src that are used */
21251 char_u
**fnamep
; /* file name so far */
21252 char_u
**bufp
; /* buffer for allocated file name or NULL */
21253 int *fnamelen
; /* length of fnamep */
21257 char_u
*s
, *p
, *pbuf
;
21258 char_u dirname
[MAXPATHL
];
21260 int has_fullname
= 0;
21262 int has_shortname
= 0;
21266 /* ":p" - full path/file_name */
21267 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'p')
21271 valid
|= VALID_PATH
;
21274 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
21275 if ((*fnamep
)[0] == '~'
21276 #if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
21277 && ((*fnamep
)[1] == '/'
21278 # ifdef BACKSLASH_IN_FILENAME
21279 || (*fnamep
)[1] == '\\'
21281 || (*fnamep
)[1] == NUL
)
21286 *fnamep
= expand_env_save(*fnamep
);
21287 vim_free(*bufp
); /* free any allocated file name */
21289 if (*fnamep
== NULL
)
21293 /* When "/." or "/.." is used: force expansion to get rid of it. */
21294 for (p
= *fnamep
; *p
!= NUL
; mb_ptr_adv(p
))
21296 if (vim_ispathsep(*p
)
21299 || vim_ispathsep(p
[2])
21301 && (p
[3] == NUL
|| vim_ispathsep(p
[3])))))
21305 /* FullName_save() is slow, don't use it when not needed. */
21306 if (*p
!= NUL
|| !vim_isAbsName(*fnamep
))
21308 *fnamep
= FullName_save(*fnamep
, *p
!= NUL
);
21309 vim_free(*bufp
); /* free any allocated file name */
21311 if (*fnamep
== NULL
)
21315 /* Append a path separator to a directory. */
21316 if (mch_isdir(*fnamep
))
21318 /* Make room for one or two extra characters. */
21319 *fnamep
= vim_strnsave(*fnamep
, (int)STRLEN(*fnamep
) + 2);
21320 vim_free(*bufp
); /* free any allocated file name */
21322 if (*fnamep
== NULL
)
21324 add_pathsep(*fnamep
);
21328 /* ":." - path relative to the current directory */
21329 /* ":~" - path relative to the home directory */
21330 /* ":8" - shortname path - postponed till after */
21331 while (src
[*usedlen
] == ':'
21332 && ((c
= src
[*usedlen
+ 1]) == '.' || c
== '~' || c
== '8'))
21338 has_shortname
= 1; /* Postpone this. */
21343 /* Need full path first (use expand_env() to remove a "~/") */
21346 if (c
== '.' && **fnamep
== '~')
21347 p
= pbuf
= expand_env_save(*fnamep
);
21349 p
= pbuf
= FullName_save(*fnamep
, FALSE
);
21360 mch_dirname(dirname
, MAXPATHL
);
21361 s
= shorten_fname(p
, dirname
);
21367 vim_free(*bufp
); /* free any allocated file name */
21375 home_replace(NULL
, p
, dirname
, MAXPATHL
, TRUE
);
21376 /* Only replace it when it starts with '~' */
21377 if (*dirname
== '~')
21379 s
= vim_strsave(dirname
);
21392 tail
= gettail(*fnamep
);
21393 *fnamelen
= (int)STRLEN(*fnamep
);
21395 /* ":h" - head, remove "/file_name", can be repeated */
21396 /* Don't remove the first "/" or "c:\" */
21397 while (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'h')
21399 valid
|= VALID_HEAD
;
21401 s
= get_past_head(*fnamep
);
21402 while (tail
> s
&& after_pathsep(s
, tail
))
21403 mb_ptr_back(*fnamep
, tail
);
21404 *fnamelen
= (int)(tail
- *fnamep
);
21407 *fnamelen
+= 1; /* the path separator is part of the path */
21409 if (*fnamelen
== 0)
21411 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
21412 p
= vim_strsave((char_u
*)".");
21416 *bufp
= *fnamep
= tail
= p
;
21421 while (tail
> s
&& !after_pathsep(s
, tail
))
21422 mb_ptr_back(*fnamep
, tail
);
21426 /* ":8" - shortname */
21427 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == '8')
21436 /* Check shortname after we have done 'heads' and before we do 'tails'
21441 /* Copy the string if it is shortened by :h */
21442 if (*fnamelen
< (int)STRLEN(*fnamep
))
21444 p
= vim_strnsave(*fnamep
, *fnamelen
);
21448 *bufp
= *fnamep
= p
;
21451 /* Split into two implementations - makes it easier. First is where
21452 * there isn't a full name already, second is where there is.
21454 if (!has_fullname
&& !vim_isAbsName(*fnamep
))
21456 if (shortpath_for_partial(fnamep
, bufp
, fnamelen
) == -1)
21463 /* Simple case, already have the full-name
21464 * Nearly always shorter, so try first time. */
21466 if (!get_short_pathname(fnamep
, bufp
, &l
))
21471 /* Couldn't find the filename.. search the paths.
21474 if (shortpath_for_invalid_fname(fnamep
, bufp
, &l
) == -1)
21480 #endif /* WIN3264 */
21482 /* ":t" - tail, just the basename */
21483 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 't')
21486 *fnamelen
-= (int)(tail
- *fnamep
);
21490 /* ":e" - extension, can be repeated */
21491 /* ":r" - root, without extension, can be repeated */
21492 while (src
[*usedlen
] == ':'
21493 && (src
[*usedlen
+ 1] == 'e' || src
[*usedlen
+ 1] == 'r'))
21495 /* find a '.' in the tail:
21496 * - for second :e: before the current fname
21497 * - otherwise: The last '.'
21499 if (src
[*usedlen
+ 1] == 'e' && *fnamep
> tail
)
21502 s
= *fnamep
+ *fnamelen
- 1;
21503 for ( ; s
> tail
; --s
)
21506 if (src
[*usedlen
+ 1] == 'e') /* :e */
21510 *fnamelen
+= (int)(*fnamep
- (s
+ 1));
21513 /* cut version from the extension */
21514 s
= *fnamep
+ *fnamelen
- 1;
21515 for ( ; s
> *fnamep
; --s
)
21519 *fnamelen
= s
- *fnamep
;
21522 else if (*fnamep
<= tail
)
21527 if (s
> tail
) /* remove one extension */
21528 *fnamelen
= (int)(s
- *fnamep
);
21533 /* ":s?pat?foo?" - substitute */
21534 /* ":gs?pat?foo?" - global substitute */
21535 if (src
[*usedlen
] == ':'
21536 && (src
[*usedlen
+ 1] == 's'
21537 || (src
[*usedlen
+ 1] == 'g' && src
[*usedlen
+ 2] == 's')))
21546 flags
= (char_u
*)"";
21547 s
= src
+ *usedlen
+ 2;
21548 if (src
[*usedlen
+ 1] == 'g')
21550 flags
= (char_u
*)"g";
21557 /* find end of pattern */
21558 p
= vim_strchr(s
, sep
);
21561 pat
= vim_strnsave(s
, (int)(p
- s
));
21565 /* find end of substitution */
21566 p
= vim_strchr(s
, sep
);
21569 sub
= vim_strnsave(s
, (int)(p
- s
));
21570 str
= vim_strnsave(*fnamep
, *fnamelen
);
21571 if (sub
!= NULL
&& str
!= NULL
)
21573 *usedlen
= (int)(p
+ 1 - src
);
21574 s
= do_string_sub(str
, pat
, sub
, flags
);
21578 *fnamelen
= (int)STRLEN(s
);
21590 /* after using ":s", repeat all the modifiers */
21600 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21601 * "flags" can be "g" to do a global substitute.
21602 * Returns an allocated string, NULL for error.
21605 do_string_sub(str
, pat
, sub
, flags
)
21612 regmatch_T regmatch
;
21620 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21622 p_cpo
= (char_u
*)"";
21624 ga_init2(&ga
, 1, 200);
21626 do_all
= (flags
[0] == 'g');
21628 regmatch
.rm_ic
= p_ic
;
21629 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
21630 if (regmatch
.regprog
!= NULL
)
21633 while (vim_regexec_nl(®match
, str
, (colnr_T
)(tail
- str
)))
21636 * Get some space for a temporary buffer to do the substitution
21637 * into. It will contain:
21638 * - The text up to where the match is.
21639 * - The substituted text.
21640 * - The text after the match.
21642 sublen
= vim_regsub(®match
, sub
, tail
, FALSE
, TRUE
, FALSE
);
21643 if (ga_grow(&ga
, (int)(STRLEN(tail
) + sublen
-
21644 (regmatch
.endp
[0] - regmatch
.startp
[0]))) == FAIL
)
21650 /* copy the text up to where the match is */
21651 i
= (int)(regmatch
.startp
[0] - tail
);
21652 mch_memmove((char_u
*)ga
.ga_data
+ ga
.ga_len
, tail
, (size_t)i
);
21653 /* add the substituted text */
21654 (void)vim_regsub(®match
, sub
, (char_u
*)ga
.ga_data
21655 + ga
.ga_len
+ i
, TRUE
, TRUE
, FALSE
);
21656 ga
.ga_len
+= i
+ sublen
- 1;
21657 /* avoid getting stuck on a match with an empty string */
21658 if (tail
== regmatch
.endp
[0])
21662 *((char_u
*)ga
.ga_data
+ ga
.ga_len
) = *tail
++;
21667 tail
= regmatch
.endp
[0];
21675 if (ga
.ga_data
!= NULL
)
21676 STRCPY((char *)ga
.ga_data
+ ga
.ga_len
, tail
);
21678 vim_free(regmatch
.regprog
);
21681 ret
= vim_strsave(ga
.ga_data
== NULL
? str
: (char_u
*)ga
.ga_data
);
21688 #endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */