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},
351 #define vv_type vv_di.di_tv.v_type
352 #define vv_nr vv_di.di_tv.vval.v_number
353 #define vv_str vv_di.di_tv.vval.v_string
354 #define vv_tv vv_di.di_tv
357 * The v: variables are stored in dictionary "vimvardict".
358 * "vimvars_var" is the variable that is used for the "l:" scope.
360 static dict_T vimvardict
;
361 static dictitem_T vimvars_var
;
362 #define vimvarht vimvardict.dv_hashtab
364 static void prepare_vimvar
__ARGS((int idx
, typval_T
*save_tv
));
365 static void restore_vimvar
__ARGS((int idx
, typval_T
*save_tv
));
366 #if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
367 static int call_vim_function
__ARGS((char_u
*func
, int argc
, char_u
**argv
, int safe
, typval_T
*rettv
));
369 static int ex_let_vars
__ARGS((char_u
*arg
, typval_T
*tv
, int copy
, int semicolon
, int var_count
, char_u
*nextchars
));
370 static char_u
*skip_var_list
__ARGS((char_u
*arg
, int *var_count
, int *semicolon
));
371 static char_u
*skip_var_one
__ARGS((char_u
*arg
));
372 static void list_hashtable_vars
__ARGS((hashtab_T
*ht
, char_u
*prefix
, int empty
, int *first
));
373 static void list_glob_vars
__ARGS((int *first
));
374 static void list_buf_vars
__ARGS((int *first
));
375 static void list_win_vars
__ARGS((int *first
));
377 static void list_tab_vars
__ARGS((int *first
));
379 static void list_vim_vars
__ARGS((int *first
));
380 static void list_script_vars
__ARGS((int *first
));
381 static void list_func_vars
__ARGS((int *first
));
382 static char_u
*list_arg_vars
__ARGS((exarg_T
*eap
, char_u
*arg
, int *first
));
383 static char_u
*ex_let_one
__ARGS((char_u
*arg
, typval_T
*tv
, int copy
, char_u
*endchars
, char_u
*op
));
384 static int check_changedtick
__ARGS((char_u
*arg
));
385 static char_u
*get_lval
__ARGS((char_u
*name
, typval_T
*rettv
, lval_T
*lp
, int unlet
, int skip
, int quiet
, int fne_flags
));
386 static void clear_lval
__ARGS((lval_T
*lp
));
387 static void set_var_lval
__ARGS((lval_T
*lp
, char_u
*endp
, typval_T
*rettv
, int copy
, char_u
*op
));
388 static int tv_op
__ARGS((typval_T
*tv1
, typval_T
*tv2
, char_u
*op
));
389 static void list_add_watch
__ARGS((list_T
*l
, listwatch_T
*lw
));
390 static void list_rem_watch
__ARGS((list_T
*l
, listwatch_T
*lwrem
));
391 static void list_fix_watch
__ARGS((list_T
*l
, listitem_T
*item
));
392 static void ex_unletlock
__ARGS((exarg_T
*eap
, char_u
*argstart
, int deep
));
393 static int do_unlet_var
__ARGS((lval_T
*lp
, char_u
*name_end
, int forceit
));
394 static int do_lock_var
__ARGS((lval_T
*lp
, char_u
*name_end
, int deep
, int lock
));
395 static void item_lock
__ARGS((typval_T
*tv
, int deep
, int lock
));
396 static int tv_islocked
__ARGS((typval_T
*tv
));
398 static int eval0
__ARGS((char_u
*arg
, typval_T
*rettv
, char_u
**nextcmd
, int evaluate
));
399 static int eval1
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
400 static int eval2
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
401 static int eval3
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
402 static int eval4
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
403 static int eval5
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
404 static int eval6
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
405 static int eval7
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
407 static int eval_index
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int verbose
));
408 static int get_option_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
409 static int get_string_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
410 static int get_lit_string_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
411 static int get_list_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
412 static int rettv_list_alloc
__ARGS((typval_T
*rettv
));
413 static listitem_T
*listitem_alloc
__ARGS((void));
414 static void listitem_free
__ARGS((listitem_T
*item
));
415 static void listitem_remove
__ARGS((list_T
*l
, listitem_T
*item
));
416 static long list_len
__ARGS((list_T
*l
));
417 static int list_equal
__ARGS((list_T
*l1
, list_T
*l2
, int ic
));
418 static int dict_equal
__ARGS((dict_T
*d1
, dict_T
*d2
, int ic
));
419 static int tv_equal
__ARGS((typval_T
*tv1
, typval_T
*tv2
, int ic
));
420 static listitem_T
*list_find
__ARGS((list_T
*l
, long n
));
421 static long list_find_nr
__ARGS((list_T
*l
, long idx
, int *errorp
));
422 static long list_idx_of_item
__ARGS((list_T
*l
, listitem_T
*item
));
423 static void list_append
__ARGS((list_T
*l
, listitem_T
*item
));
424 static int list_append_tv
__ARGS((list_T
*l
, typval_T
*tv
));
425 static int list_append_string
__ARGS((list_T
*l
, char_u
*str
, int len
));
426 static int list_append_number
__ARGS((list_T
*l
, varnumber_T n
));
427 static int list_insert_tv
__ARGS((list_T
*l
, typval_T
*tv
, listitem_T
*item
));
428 static int list_extend
__ARGS((list_T
*l1
, list_T
*l2
, listitem_T
*bef
));
429 static int list_concat
__ARGS((list_T
*l1
, list_T
*l2
, typval_T
*tv
));
430 static list_T
*list_copy
__ARGS((list_T
*orig
, int deep
, int copyID
));
431 static void list_remove
__ARGS((list_T
*l
, listitem_T
*item
, listitem_T
*item2
));
432 static char_u
*list2string
__ARGS((typval_T
*tv
, int copyID
));
433 static int list_join
__ARGS((garray_T
*gap
, list_T
*l
, char_u
*sep
, int echo
, int copyID
));
434 static void set_ref_in_ht
__ARGS((hashtab_T
*ht
, int copyID
));
435 static void set_ref_in_list
__ARGS((list_T
*l
, int copyID
));
436 static void set_ref_in_item
__ARGS((typval_T
*tv
, int copyID
));
437 static void dict_unref
__ARGS((dict_T
*d
));
438 static void dict_free
__ARGS((dict_T
*d
, int recurse
));
439 static dictitem_T
*dictitem_alloc
__ARGS((char_u
*key
));
440 static dictitem_T
*dictitem_copy
__ARGS((dictitem_T
*org
));
441 static void dictitem_remove
__ARGS((dict_T
*dict
, dictitem_T
*item
));
442 static void dictitem_free
__ARGS((dictitem_T
*item
));
443 static dict_T
*dict_copy
__ARGS((dict_T
*orig
, int deep
, int copyID
));
444 static int dict_add
__ARGS((dict_T
*d
, dictitem_T
*item
));
445 static long dict_len
__ARGS((dict_T
*d
));
446 static dictitem_T
*dict_find
__ARGS((dict_T
*d
, char_u
*key
, int len
));
447 static char_u
*dict2string
__ARGS((typval_T
*tv
, int copyID
));
448 static int get_dict_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
449 static char_u
*echo_string
__ARGS((typval_T
*tv
, char_u
**tofree
, char_u
*numbuf
, int copyID
));
450 static char_u
*tv2string
__ARGS((typval_T
*tv
, char_u
**tofree
, char_u
*numbuf
, int copyID
));
451 static char_u
*string_quote
__ARGS((char_u
*str
, int function
));
452 static int get_env_tv
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
));
453 static int find_internal_func
__ARGS((char_u
*name
));
454 static char_u
*deref_func_name
__ARGS((char_u
*name
, int *lenp
));
455 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
));
456 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
));
457 static void emsg_funcname
__ARGS((char *ermsg
, char_u
*name
));
459 static void f_add
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
460 static void f_append
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
461 static void f_argc
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
462 static void f_argidx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
463 static void f_argv
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
464 static void f_browse
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
465 static void f_browsedir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
466 static void f_bufexists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
467 static void f_buflisted
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
468 static void f_bufloaded
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
469 static void f_bufname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
470 static void f_bufnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
471 static void f_bufwinnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
472 static void f_byte2line
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
473 static void f_byteidx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
474 static void f_call
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
475 static void f_changenr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
476 static void f_char2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
477 static void f_cindent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
478 static void f_clearmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
479 static void f_col
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
480 #if defined(FEAT_INS_EXPAND)
481 static void f_complete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
482 static void f_complete_add
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
483 static void f_complete_check
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
485 static void f_confirm
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
486 static void f_copy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
487 static void f_count
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
488 static void f_cscope_connection
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
489 static void f_cursor
__ARGS((typval_T
*argsvars
, typval_T
*rettv
));
490 static void f_deepcopy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
491 static void f_delete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
492 static void f_did_filetype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
493 static void f_diff_filler
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
494 static void f_diff_hlID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
495 static void f_empty
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
496 static void f_escape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
497 static void f_eval
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
498 static void f_eventhandler
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
499 static void f_executable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
500 static void f_exists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
501 static void f_expand
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
502 static void f_extend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
503 static void f_feedkeys
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
504 static void f_filereadable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
505 static void f_filewritable
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
506 static void f_filter
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
507 static void f_finddir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
508 static void f_findfile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
509 static void f_fnamemodify
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
510 static void f_foldclosed
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
511 static void f_foldclosedend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
512 static void f_foldlevel
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
513 static void f_foldtext
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
514 static void f_foldtextresult
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
515 static void f_foreground
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
516 static void f_function
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
517 static void f_garbagecollect
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
518 static void f_get
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
519 static void f_getbufline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
520 static void f_getbufvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
521 static void f_getchar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
522 static void f_getcharmod
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
523 static void f_getcmdline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
524 static void f_getcmdpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
525 static void f_getcmdtype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
526 static void f_getcwd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
527 static void f_getfontname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
528 static void f_getfperm
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
529 static void f_getfsize
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
530 static void f_getftime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
531 static void f_getftype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
532 static void f_getline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
533 static void f_getmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
534 static void f_getpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
535 static void f_getqflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
536 static void f_getreg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
537 static void f_getregtype
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
538 static void f_gettabwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
539 static void f_getwinposx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
540 static void f_getwinposy
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
541 static void f_getwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
542 static void f_glob
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
543 static void f_globpath
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
544 static void f_has
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
545 static void f_has_key
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
546 static void f_haslocaldir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
547 static void f_hasmapto
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
548 static void f_histadd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
549 static void f_histdel
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
550 static void f_histget
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
551 static void f_histnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
552 static void f_hlID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
553 static void f_hlexists
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
554 static void f_hostname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
555 static void f_iconv
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
556 static void f_indent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
557 static void f_index
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
558 static void f_input
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
559 static void f_inputdialog
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
560 static void f_inputlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
561 static void f_inputrestore
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
562 static void f_inputsave
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
563 static void f_inputsecret
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
564 static void f_insert
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
565 static void f_isdirectory
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
566 static void f_islocked
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
567 static void f_items
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
568 static void f_join
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
569 static void f_keys
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
570 static void f_last_buffer_nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
571 static void f_len
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
572 static void f_libcall
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
573 static void f_libcallnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
574 static void f_line
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
575 static void f_line2byte
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
576 static void f_lispindent
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
577 static void f_localtime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
578 static void f_map
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
579 static void f_maparg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
580 static void f_mapcheck
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
581 static void f_match
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
582 static void f_matchadd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
583 static void f_matcharg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
584 static void f_matchdelete
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
585 static void f_matchend
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
586 static void f_matchlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
587 static void f_matchstr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
588 static void f_max
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
589 static void f_min
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
591 static void f_mkdir
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
593 static void f_mode
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
594 static void f_nextnonblank
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
595 static void f_nr2char
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
596 static void f_pathshorten
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
597 static void f_prevnonblank
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
598 static void f_printf
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
599 static void f_pumvisible
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
600 static void f_range
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
601 static void f_readfile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
602 static void f_reltime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
603 static void f_reltimestr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
604 static void f_remote_expr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
605 static void f_remote_foreground
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
606 static void f_remote_peek
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
607 static void f_remote_read
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
608 static void f_remote_send
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
609 static void f_remove
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
610 static void f_rename
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
611 static void f_repeat
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
612 static void f_resolve
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
613 static void f_reverse
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
614 static void f_search
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
615 static void f_searchdecl
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
616 static void f_searchpair
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
617 static void f_searchpairpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
618 static void f_searchpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
619 static void f_server2client
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
620 static void f_serverlist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
621 static void f_setbufvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
622 static void f_setcmdpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
623 static void f_setline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
624 static void f_setloclist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
625 static void f_setmatches
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
626 static void f_setpos
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
627 static void f_setqflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
628 static void f_setreg
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
629 static void f_settabwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
630 static void f_setwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
631 static void f_shellescape
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
632 static void f_simplify
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
633 static void f_sort
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
634 static void f_soundfold
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
635 static void f_spellbadword
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
636 static void f_spellsuggest
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
637 static void f_split
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
638 static void f_str2nr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
640 static void f_strftime
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
642 static void f_stridx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
643 static void f_string
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
644 static void f_strlen
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
645 static void f_strpart
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
646 static void f_strridx
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
647 static void f_strtrans
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
648 static void f_submatch
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
649 static void f_substitute
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
650 static void f_synID
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
651 static void f_synIDattr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
652 static void f_synIDtrans
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
653 static void f_system
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
654 static void f_tabpagebuflist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
655 static void f_tabpagenr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
656 static void f_tabpagewinnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
657 static void f_taglist
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
658 static void f_tagfiles
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
659 static void f_tempname
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
660 static void f_test
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
661 static void f_tolower
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
662 static void f_toupper
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
663 static void f_tr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
664 static void f_type
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
665 static void f_values
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
666 static void f_virtcol
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
667 static void f_visualmode
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
668 static void f_winbufnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
669 static void f_wincol
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
670 static void f_winheight
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
671 static void f_winline
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
672 static void f_winnr
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
673 static void f_winrestcmd
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
674 static void f_winrestview
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
675 static void f_winsaveview
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
676 static void f_winwidth
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
677 static void f_writefile
__ARGS((typval_T
*argvars
, typval_T
*rettv
));
679 static int list2fpos
__ARGS((typval_T
*arg
, pos_T
*posp
, int *fnump
));
680 static pos_T
*var2fpos
__ARGS((typval_T
*varp
, int dollar_lnum
, int *fnum
));
681 static int get_env_len
__ARGS((char_u
**arg
));
682 static int get_id_len
__ARGS((char_u
**arg
));
683 static int get_name_len
__ARGS((char_u
**arg
, char_u
**alias
, int evaluate
, int verbose
));
684 static char_u
*find_name_end
__ARGS((char_u
*arg
, char_u
**expr_start
, char_u
**expr_end
, int flags
));
685 #define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
686 #define FNE_CHECK_START 2 /* find_name_end(): check name starts with
688 static char_u
* make_expanded_name
__ARGS((char_u
*in_start
, char_u
*expr_start
, char_u
*expr_end
, char_u
*in_end
));
689 static int eval_isnamec
__ARGS((int c
));
690 static int eval_isnamec1
__ARGS((int c
));
691 static int get_var_tv
__ARGS((char_u
*name
, int len
, typval_T
*rettv
, int verbose
));
692 static int handle_subscript
__ARGS((char_u
**arg
, typval_T
*rettv
, int evaluate
, int verbose
));
693 static typval_T
*alloc_tv
__ARGS((void));
694 static typval_T
*alloc_string_tv
__ARGS((char_u
*string
));
695 static void init_tv
__ARGS((typval_T
*varp
));
696 static long get_tv_number
__ARGS((typval_T
*varp
));
697 static linenr_T get_tv_lnum
__ARGS((typval_T
*argvars
));
698 static linenr_T get_tv_lnum_buf
__ARGS((typval_T
*argvars
, buf_T
*buf
));
699 static char_u
*get_tv_string
__ARGS((typval_T
*varp
));
700 static char_u
*get_tv_string_buf
__ARGS((typval_T
*varp
, char_u
*buf
));
701 static char_u
*get_tv_string_buf_chk
__ARGS((typval_T
*varp
, char_u
*buf
));
702 static dictitem_T
*find_var
__ARGS((char_u
*name
, hashtab_T
**htp
));
703 static dictitem_T
*find_var_in_ht
__ARGS((hashtab_T
*ht
, char_u
*varname
, int writing
));
704 static hashtab_T
*find_var_ht
__ARGS((char_u
*name
, char_u
**varname
));
705 static void vars_clear_ext
__ARGS((hashtab_T
*ht
, int free_val
));
706 static void delete_var
__ARGS((hashtab_T
*ht
, hashitem_T
*hi
));
707 static void list_one_var
__ARGS((dictitem_T
*v
, char_u
*prefix
, int *first
));
708 static void list_one_var_a
__ARGS((char_u
*prefix
, char_u
*name
, int type
, char_u
*string
, int *first
));
709 static void set_var
__ARGS((char_u
*name
, typval_T
*varp
, int copy
));
710 static int var_check_ro
__ARGS((int flags
, char_u
*name
));
711 static int var_check_fixed
__ARGS((int flags
, char_u
*name
));
712 static int tv_check_lock
__ARGS((int lock
, char_u
*name
));
713 static void copy_tv
__ARGS((typval_T
*from
, typval_T
*to
));
714 static int item_copy
__ARGS((typval_T
*from
, typval_T
*to
, int deep
, int copyID
));
715 static char_u
*find_option_end
__ARGS((char_u
**arg
, int *opt_flags
));
716 static char_u
*trans_function_name
__ARGS((char_u
**pp
, int skip
, int flags
, funcdict_T
*fd
));
717 static int eval_fname_script
__ARGS((char_u
*p
));
718 static int eval_fname_sid
__ARGS((char_u
*p
));
719 static void list_func_head
__ARGS((ufunc_T
*fp
, int indent
));
720 static ufunc_T
*find_func
__ARGS((char_u
*name
));
721 static int function_exists
__ARGS((char_u
*name
));
722 static int builtin_function
__ARGS((char_u
*name
));
724 static void func_do_profile
__ARGS((ufunc_T
*fp
));
725 static void prof_sort_list
__ARGS((FILE *fd
, ufunc_T
**sorttab
, int st_len
, char *title
, int prefer_self
));
726 static void prof_func_line
__ARGS((FILE *fd
, int count
, proftime_T
*total
, proftime_T
*self
, int prefer_self
));
731 prof_total_cmp
__ARGS((const void *s1
, const void *s2
));
736 prof_self_cmp
__ARGS((const void *s1
, const void *s2
));
738 static int script_autoload
__ARGS((char_u
*name
, int reload
));
739 static char_u
*autoload_name
__ARGS((char_u
*name
));
740 static void cat_func_name
__ARGS((char_u
*buf
, ufunc_T
*fp
));
741 static void func_free
__ARGS((ufunc_T
*fp
));
742 static void func_unref
__ARGS((char_u
*name
));
743 static void func_ref
__ARGS((char_u
*name
));
744 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
));
745 static void add_nr_var
__ARGS((dict_T
*dp
, dictitem_T
*v
, char *name
, varnumber_T nr
));
746 static win_T
*find_win_by_nr
__ARGS((typval_T
*vp
, tabpage_T
*tp
));
747 static void getwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int off
));
748 static int searchpair_cmn
__ARGS((typval_T
*argvars
, pos_T
*match_pos
));
749 static int search_cmn
__ARGS((typval_T
*argvars
, pos_T
*match_pos
, int *flagsp
));
750 static void setwinvar
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int off
));
752 /* Character used as separated in autoload function/variable names. */
753 #define AUTOLOAD_CHAR '#'
756 * Initialize the global and v: variables.
764 init_var_dict(&globvardict
, &globvars_var
);
765 init_var_dict(&vimvardict
, &vimvars_var
);
766 hash_init(&compat_hashtab
);
767 hash_init(&func_hashtab
);
769 for (i
= 0; i
< VV_LEN
; ++i
)
772 STRCPY(p
->vv_di
.di_key
, p
->vv_name
);
773 if (p
->vv_flags
& VV_RO
)
774 p
->vv_di
.di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
775 else if (p
->vv_flags
& VV_RO_SBX
)
776 p
->vv_di
.di_flags
= DI_FLAGS_RO_SBX
| DI_FLAGS_FIX
;
778 p
->vv_di
.di_flags
= DI_FLAGS_FIX
;
780 /* add to v: scope dict, unless the value is not always available */
781 if (p
->vv_type
!= VAR_UNKNOWN
)
782 hash_add(&vimvarht
, p
->vv_di
.di_key
);
783 if (p
->vv_flags
& VV_COMPAT
)
784 /* add to compat scope dict */
785 hash_add(&compat_hashtab
, p
->vv_di
.di_key
);
789 #if defined(EXITFREE) || defined(PROTO)
796 for (i
= 0; i
< VV_LEN
; ++i
)
799 if (p
->vv_di
.di_tv
.v_type
== VAR_STRING
)
801 vim_free(p
->vv_di
.di_tv
.vval
.v_string
);
802 p
->vv_di
.di_tv
.vval
.v_string
= NULL
;
805 hash_clear(&vimvarht
);
806 hash_clear(&compat_hashtab
);
808 /* script-local variables */
809 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
810 vars_clear(&SCRIPT_VARS(i
));
811 ga_clear(&ga_scripts
);
814 /* global variables */
815 vars_clear(&globvarht
);
818 free_all_functions();
819 hash_clear(&func_hashtab
);
821 /* autoloaded script names */
822 ga_clear_strings(&ga_loaded
);
824 /* unreferenced lists and dicts */
825 (void)garbage_collect();
830 * Return the name of the executed function.
836 return ((funccall_T
*)cookie
)->func
->uf_name
;
840 * Return the address holding the next breakpoint line for a funccall cookie.
843 func_breakpoint(cookie
)
846 return &((funccall_T
*)cookie
)->breakpoint
;
850 * Return the address holding the debug tick for a funccall cookie.
853 func_dbg_tick(cookie
)
856 return &((funccall_T
*)cookie
)->dbg_tick
;
860 * Return the nesting level for a funccall cookie.
866 return ((funccall_T
*)cookie
)->level
;
869 /* pointer to funccal for currently active function */
870 funccall_T
*current_funccal
= NULL
;
873 * Return TRUE when a function was ended by a ":return" command.
876 current_func_returned()
878 return current_funccal
->returned
;
883 * Set an internal variable to a string value. Creates the variable if it does
887 set_internal_string_var(name
, value
)
894 val
= vim_strsave(value
);
897 tvp
= alloc_string_tv(val
);
900 set_var(name
, tvp
, FALSE
);
906 static lval_T
*redir_lval
= NULL
;
907 static garray_T redir_ga
; /* only valid when redir_lval is not NULL */
908 static char_u
*redir_endp
= NULL
;
909 static char_u
*redir_varname
= NULL
;
912 * Start recording command output to a variable
913 * Returns OK if successfully completed the setup. FAIL otherwise.
916 var_redir_start(name
, append
)
918 int append
; /* append to an existing variable */
924 /* Make sure a valid variable name is specified */
925 if (!eval_isnamec1(*name
))
931 redir_varname
= vim_strsave(name
);
932 if (redir_varname
== NULL
)
935 redir_lval
= (lval_T
*)alloc_clear((unsigned)sizeof(lval_T
));
936 if (redir_lval
== NULL
)
942 /* The output is stored in growarray "redir_ga" until redirection ends. */
943 ga_init2(&redir_ga
, (int)sizeof(char), 500);
945 /* Parse the variable name (can be a dict or list entry). */
946 redir_endp
= get_lval(redir_varname
, NULL
, redir_lval
, FALSE
, FALSE
, FALSE
,
948 if (redir_endp
== NULL
|| redir_lval
->ll_name
== NULL
|| *redir_endp
!= NUL
)
950 if (redir_endp
!= NULL
&& *redir_endp
!= NUL
)
951 /* Trailing characters are present after the variable name */
959 /* check if we can write to the variable: set it to or append an empty
961 save_emsg
= did_emsg
;
963 tv
.v_type
= VAR_STRING
;
964 tv
.vval
.v_string
= (char_u
*)"";
966 set_var_lval(redir_lval
, redir_endp
, &tv
, TRUE
, (char_u
*)".");
968 set_var_lval(redir_lval
, redir_endp
, &tv
, TRUE
, (char_u
*)"=");
970 did_emsg
|= save_emsg
;
976 if (redir_lval
->ll_newkey
!= NULL
)
978 /* Dictionary item was created, don't do it again. */
979 vim_free(redir_lval
->ll_newkey
);
980 redir_lval
->ll_newkey
= NULL
;
987 * Append "value[value_len]" to the variable set by var_redir_start().
988 * The actual appending is postponed until redirection ends, because the value
989 * appended may in fact be the string we write to, changing it may cause freed
996 var_redir_str(value
, value_len
)
1002 if (redir_lval
== NULL
)
1005 if (value_len
== -1)
1006 len
= (int)STRLEN(value
); /* Append the entire string */
1008 len
= value_len
; /* Append only "value_len" characters */
1010 if (ga_grow(&redir_ga
, len
) == OK
)
1012 mch_memmove((char *)redir_ga
.ga_data
+ redir_ga
.ga_len
, value
, len
);
1013 redir_ga
.ga_len
+= len
;
1020 * Stop redirecting command output to a variable.
1027 if (redir_lval
!= NULL
)
1029 /* Append the trailing NUL. */
1030 ga_append(&redir_ga
, NUL
);
1032 /* Assign the text to the variable. */
1033 tv
.v_type
= VAR_STRING
;
1034 tv
.vval
.v_string
= redir_ga
.ga_data
;
1035 set_var_lval(redir_lval
, redir_endp
, &tv
, FALSE
, (char_u
*)".");
1036 vim_free(tv
.vval
.v_string
);
1038 clear_lval(redir_lval
);
1039 vim_free(redir_lval
);
1042 vim_free(redir_varname
);
1043 redir_varname
= NULL
;
1046 # if defined(FEAT_MBYTE) || defined(PROTO)
1048 eval_charconvert(enc_from
, enc_to
, fname_from
, fname_to
)
1056 set_vim_var_string(VV_CC_FROM
, enc_from
, -1);
1057 set_vim_var_string(VV_CC_TO
, enc_to
, -1);
1058 set_vim_var_string(VV_FNAME_IN
, fname_from
, -1);
1059 set_vim_var_string(VV_FNAME_OUT
, fname_to
, -1);
1060 if (eval_to_bool(p_ccv
, &err
, NULL
, FALSE
))
1062 set_vim_var_string(VV_CC_FROM
, NULL
, -1);
1063 set_vim_var_string(VV_CC_TO
, NULL
, -1);
1064 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1065 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1073 # if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1075 eval_printexpr(fname
, args
)
1081 set_vim_var_string(VV_FNAME_IN
, fname
, -1);
1082 set_vim_var_string(VV_CMDARG
, args
, -1);
1083 if (eval_to_bool(p_pexpr
, &err
, NULL
, FALSE
))
1085 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1086 set_vim_var_string(VV_CMDARG
, NULL
, -1);
1097 # if defined(FEAT_DIFF) || defined(PROTO)
1099 eval_diff(origfile
, newfile
, outfile
)
1106 set_vim_var_string(VV_FNAME_IN
, origfile
, -1);
1107 set_vim_var_string(VV_FNAME_NEW
, newfile
, -1);
1108 set_vim_var_string(VV_FNAME_OUT
, outfile
, -1);
1109 (void)eval_to_bool(p_dex
, &err
, NULL
, FALSE
);
1110 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1111 set_vim_var_string(VV_FNAME_NEW
, NULL
, -1);
1112 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1116 eval_patch(origfile
, difffile
, outfile
)
1123 set_vim_var_string(VV_FNAME_IN
, origfile
, -1);
1124 set_vim_var_string(VV_FNAME_DIFF
, difffile
, -1);
1125 set_vim_var_string(VV_FNAME_OUT
, outfile
, -1);
1126 (void)eval_to_bool(p_pex
, &err
, NULL
, FALSE
);
1127 set_vim_var_string(VV_FNAME_IN
, NULL
, -1);
1128 set_vim_var_string(VV_FNAME_DIFF
, NULL
, -1);
1129 set_vim_var_string(VV_FNAME_OUT
, NULL
, -1);
1134 * Top level evaluation function, returning a boolean.
1135 * Sets "error" to TRUE if there was an error.
1136 * Return TRUE or FALSE.
1139 eval_to_bool(arg
, error
, nextcmd
, skip
)
1143 int skip
; /* only parse, don't execute */
1150 if (eval0(arg
, &tv
, nextcmd
, !skip
) == FAIL
)
1157 retval
= (get_tv_number_chk(&tv
, error
) != 0);
1168 * Top level evaluation function, returning a string. If "skip" is TRUE,
1169 * only parsing to "nextcmd" is done, without reporting errors. Return
1170 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1173 eval_to_string_skip(arg
, nextcmd
, skip
)
1176 int skip
; /* only parse, don't execute */
1183 if (eval0(arg
, &tv
, nextcmd
, !skip
) == FAIL
|| skip
)
1187 retval
= vim_strsave(get_tv_string(&tv
));
1197 * Skip over an expression at "*pp".
1198 * Return FAIL for an error, OK otherwise.
1206 *pp
= skipwhite(*pp
);
1207 return eval1(pp
, &rettv
, FALSE
);
1211 * Top level evaluation function, returning a string.
1212 * Return pointer to allocated memory, or NULL for failure.
1215 eval_to_string(arg
, nextcmd
, dolist
)
1218 int dolist
; /* turn List into sequence of lines */
1224 if (eval0(arg
, &tv
, nextcmd
, TRUE
) == FAIL
)
1228 if (dolist
&& tv
.v_type
== VAR_LIST
)
1230 ga_init2(&ga
, (int)sizeof(char), 80);
1231 list_join(&ga
, tv
.vval
.v_list
, (char_u
*)"\n", TRUE
, 0);
1232 ga_append(&ga
, NUL
);
1233 retval
= (char_u
*)ga
.ga_data
;
1236 retval
= vim_strsave(get_tv_string(&tv
));
1244 * Call eval_to_string() without using current local variables and using
1245 * textlock. When "use_sandbox" is TRUE use the sandbox.
1248 eval_to_string_safe(arg
, nextcmd
, use_sandbox
)
1254 void *save_funccalp
;
1256 save_funccalp
= save_funccal();
1260 retval
= eval_to_string(arg
, nextcmd
, FALSE
);
1264 restore_funccal(save_funccalp
);
1269 * Top level evaluation function, returning a number.
1270 * Evaluates "expr" silently.
1271 * Returns -1 for an error.
1274 eval_to_number(expr
)
1279 char_u
*p
= skipwhite(expr
);
1283 if (eval1(&p
, &rettv
, TRUE
) == FAIL
)
1287 retval
= get_tv_number_chk(&rettv
, NULL
);
1296 * Prepare v: variable "idx" to be used.
1297 * Save the current typeval in "save_tv".
1298 * When not used yet add the variable to the v: hashtable.
1301 prepare_vimvar(idx
, save_tv
)
1305 *save_tv
= vimvars
[idx
].vv_tv
;
1306 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1307 hash_add(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1311 * Restore v: variable "idx" to typeval "save_tv".
1312 * When no longer defined, remove the variable from the v: hashtable.
1315 restore_vimvar(idx
, save_tv
)
1321 vimvars
[idx
].vv_tv
= *save_tv
;
1322 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1324 hi
= hash_find(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1325 if (HASHITEM_EMPTY(hi
))
1326 EMSG2(_(e_intern2
), "restore_vimvar()");
1328 hash_remove(&vimvarht
, hi
);
1332 #if defined(FEAT_SPELL) || defined(PROTO)
1334 * Evaluate an expression to a list with suggestions.
1335 * For the "expr:" part of 'spellsuggest'.
1338 eval_spell_expr(badword
, expr
)
1344 list_T
*list
= NULL
;
1345 char_u
*p
= skipwhite(expr
);
1347 /* Set "v:val" to the bad word. */
1348 prepare_vimvar(VV_VAL
, &save_val
);
1349 vimvars
[VV_VAL
].vv_type
= VAR_STRING
;
1350 vimvars
[VV_VAL
].vv_str
= badword
;
1354 if (eval1(&p
, &rettv
, TRUE
) == OK
)
1356 if (rettv
.v_type
!= VAR_LIST
)
1359 list
= rettv
.vval
.v_list
;
1364 restore_vimvar(VV_VAL
, &save_val
);
1370 * "list" is supposed to contain two items: a word and a number. Return the
1371 * word in "pp" and the number as the return value.
1372 * Return -1 if anything isn't right.
1373 * Used to get the good word and score from the eval_spell_expr() result.
1376 get_spellword(list
, pp
)
1382 li
= list
->lv_first
;
1385 *pp
= get_tv_string(&li
->li_tv
);
1390 return get_tv_number(&li
->li_tv
);
1395 * Top level evaluation function.
1396 * Returns an allocated typval_T with the result.
1397 * Returns NULL when there is an error.
1400 eval_expr(arg
, nextcmd
)
1406 tv
= (typval_T
*)alloc(sizeof(typval_T
));
1407 if (tv
!= NULL
&& eval0(arg
, tv
, nextcmd
, TRUE
) == FAIL
)
1417 #if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1418 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1420 * Call some vimL function and return the result in "*rettv".
1421 * Uses argv[argc] for the function arguments.
1422 * Returns OK or FAIL.
1425 call_vim_function(func
, argc
, argv
, safe
, rettv
)
1429 int safe
; /* use the sandbox */
1437 void *save_funccalp
= NULL
;
1440 argvars
= (typval_T
*)alloc((unsigned)((argc
+ 1) * sizeof(typval_T
)));
1441 if (argvars
== NULL
)
1444 for (i
= 0; i
< argc
; i
++)
1446 /* Pass a NULL or empty argument as an empty string */
1447 if (argv
[i
] == NULL
|| *argv
[i
] == NUL
)
1449 argvars
[i
].v_type
= VAR_STRING
;
1450 argvars
[i
].vval
.v_string
= (char_u
*)"";
1454 /* Recognize a number argument, the others must be strings. */
1455 vim_str2nr(argv
[i
], NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
1456 if (len
!= 0 && len
== (int)STRLEN(argv
[i
]))
1458 argvars
[i
].v_type
= VAR_NUMBER
;
1459 argvars
[i
].vval
.v_number
= n
;
1463 argvars
[i
].v_type
= VAR_STRING
;
1464 argvars
[i
].vval
.v_string
= argv
[i
];
1470 save_funccalp
= save_funccal();
1474 rettv
->v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
1475 ret
= call_func(func
, (int)STRLEN(func
), rettv
, argc
, argvars
,
1476 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
1477 &doesrange
, TRUE
, NULL
);
1481 restore_funccal(save_funccalp
);
1491 # if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1493 * Call vimL function "func" and return the result as a string.
1494 * Returns NULL when calling the function fails.
1495 * Uses argv[argc] for the function arguments.
1498 call_func_retstr(func
, argc
, argv
, safe
)
1502 int safe
; /* use the sandbox */
1507 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1510 retval
= vim_strsave(get_tv_string(&rettv
));
1516 # if defined(FEAT_COMPL_FUNC) || defined(PROTO)
1518 * Call vimL function "func" and return the result as a number.
1519 * Returns -1 when calling the function fails.
1520 * Uses argv[argc] for the function arguments.
1523 call_func_retnr(func
, argc
, argv
, safe
)
1527 int safe
; /* use the sandbox */
1532 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1535 retval
= get_tv_number_chk(&rettv
, NULL
);
1542 * Call vimL function "func" and return the result as a list
1543 * Uses argv[argc] for the function arguments.
1546 call_func_retlist(func
, argc
, argv
, safe
)
1550 int safe
; /* use the sandbox */
1554 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1557 if (rettv
.v_type
!= VAR_LIST
)
1563 return rettv
.vval
.v_list
;
1569 * Save the current function call pointer, and set it to NULL.
1570 * Used when executing autocommands and for ":source".
1575 funccall_T
*fc
= current_funccal
;
1577 current_funccal
= NULL
;
1582 restore_funccal(vfc
)
1585 funccall_T
*fc
= (funccall_T
*)vfc
;
1587 current_funccal
= fc
;
1590 #if defined(FEAT_PROFILE) || defined(PROTO)
1592 * Prepare profiling for entering a child or something else that is not
1593 * counted for the script/function itself.
1594 * Should always be called in pair with prof_child_exit().
1597 prof_child_enter(tm
)
1598 proftime_T
*tm
; /* place to store waittime */
1600 funccall_T
*fc
= current_funccal
;
1602 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1603 profile_start(&fc
->prof_child
);
1604 script_prof_save(tm
);
1608 * Take care of time spent in a child.
1609 * Should always be called after prof_child_enter().
1613 proftime_T
*tm
; /* where waittime was stored */
1615 funccall_T
*fc
= current_funccal
;
1617 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1619 profile_end(&fc
->prof_child
);
1620 profile_sub_wait(tm
, &fc
->prof_child
); /* don't count waiting time */
1621 profile_add(&fc
->func
->uf_tm_children
, &fc
->prof_child
);
1622 profile_add(&fc
->func
->uf_tml_children
, &fc
->prof_child
);
1624 script_prof_restore(tm
);
1631 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1632 * it in "*cp". Doesn't give error messages.
1635 eval_foldexpr(arg
, cp
)
1642 int use_sandbox
= was_set_insecurely((char_u
*)"foldexpr",
1650 if (eval0(arg
, &tv
, NULL
, TRUE
) == FAIL
)
1654 /* If the result is a number, just return the number. */
1655 if (tv
.v_type
== VAR_NUMBER
)
1656 retval
= tv
.vval
.v_number
;
1657 else if (tv
.v_type
!= VAR_STRING
|| tv
.vval
.v_string
== NULL
)
1661 /* If the result is a string, check if there is a non-digit before
1663 s
= tv
.vval
.v_string
;
1664 if (!VIM_ISDIGIT(*s
) && *s
!= '-')
1666 retval
= atol((char *)s
);
1680 * ":let" list all variable values
1681 * ":let var1 var2" list variable values
1682 * ":let var = expr" assignment command.
1683 * ":let var += expr" assignment command.
1684 * ":let var -= expr" assignment command.
1685 * ":let var .= expr" assignment command.
1686 * ":let [var1, var2] = expr" unpack list.
1692 char_u
*arg
= eap
->arg
;
1693 char_u
*expr
= NULL
;
1702 argend
= skip_var_list(arg
, &var_count
, &semicolon
);
1705 if (argend
> arg
&& argend
[-1] == '.') /* for var.='str' */
1707 expr
= vim_strchr(argend
, '=');
1711 * ":let" without "=": list variables
1715 else if (!ends_excmd(*arg
))
1716 /* ":let var1 var2" */
1717 arg
= list_arg_vars(eap
, arg
, &first
);
1718 else if (!eap
->skip
)
1721 list_glob_vars(&first
);
1722 list_buf_vars(&first
);
1723 list_win_vars(&first
);
1725 list_tab_vars(&first
);
1727 list_script_vars(&first
);
1728 list_func_vars(&first
);
1729 list_vim_vars(&first
);
1731 eap
->nextcmd
= check_nextcmd(arg
);
1739 if (vim_strchr((char_u
*)"+-.", expr
[-1]) != NULL
)
1740 op
[0] = expr
[-1]; /* +=, -= or .= */
1742 expr
= skipwhite(expr
+ 1);
1746 i
= eval0(expr
, &rettv
, &eap
->nextcmd
, !eap
->skip
);
1755 (void)ex_let_vars(eap
->arg
, &rettv
, FALSE
, semicolon
, var_count
,
1763 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1764 * Handles both "var" with any type and "[var, var; var]" with a list type.
1765 * When "nextchars" is not NULL it points to a string with characters that
1766 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1768 * Returns OK or FAIL;
1771 ex_let_vars(arg_start
, tv
, copy
, semicolon
, var_count
, nextchars
)
1774 int copy
; /* copy values from "tv", don't move */
1775 int semicolon
; /* from skip_var_list() */
1776 int var_count
; /* from skip_var_list() */
1779 char_u
*arg
= arg_start
;
1788 * ":let var = expr" or ":for var in list"
1790 if (ex_let_one(arg
, tv
, copy
, nextchars
, nextchars
) == NULL
)
1796 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1798 if (tv
->v_type
!= VAR_LIST
|| (l
= tv
->vval
.v_list
) == NULL
)
1805 if (semicolon
== 0 && var_count
< i
)
1807 EMSG(_("E687: Less targets than List items"));
1810 if (var_count
- semicolon
> i
)
1812 EMSG(_("E688: More targets than List items"));
1819 arg
= skipwhite(arg
+ 1);
1820 arg
= ex_let_one(arg
, &item
->li_tv
, TRUE
, (char_u
*)",;]", nextchars
);
1821 item
= item
->li_next
;
1825 arg
= skipwhite(arg
);
1828 /* Put the rest of the list (may be empty) in the var after ';'.
1829 * Create a new list for this. */
1833 while (item
!= NULL
)
1835 list_append_tv(l
, &item
->li_tv
);
1836 item
= item
->li_next
;
1839 ltv
.v_type
= VAR_LIST
;
1841 ltv
.vval
.v_list
= l
;
1844 arg
= ex_let_one(skipwhite(arg
+ 1), <v
, FALSE
,
1845 (char_u
*)"]", nextchars
);
1851 else if (*arg
!= ',' && *arg
!= ']')
1853 EMSG2(_(e_intern2
), "ex_let_vars()");
1862 * Skip over assignable variable "var" or list of variables "[var, var]".
1863 * Used for ":let varvar = expr" and ":for varvar in expr".
1864 * For "[var, var]" increment "*var_count" for each variable.
1865 * for "[var, var; var]" set "semicolon".
1866 * Return NULL for an error.
1869 skip_var_list(arg
, var_count
, semicolon
)
1878 /* "[var, var]": find the matching ']'. */
1882 p
= skipwhite(p
+ 1); /* skip whites after '[', ';' or ',' */
1883 s
= skip_var_one(p
);
1886 EMSG2(_(e_invarg2
), p
);
1896 if (*semicolon
== 1)
1898 EMSG(_("Double ; in list of variables"));
1905 EMSG2(_(e_invarg2
), p
);
1912 return skip_var_one(arg
);
1916 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1923 if (*arg
== '@' && arg
[1] != NUL
)
1925 return find_name_end(*arg
== '$' || *arg
== '&' ? arg
+ 1 : arg
,
1926 NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
1930 * List variables for hashtab "ht" with prefix "prefix".
1931 * If "empty" is TRUE also list NULL strings as empty strings.
1934 list_hashtable_vars(ht
, prefix
, empty
, first
)
1944 todo
= (int)ht
->ht_used
;
1945 for (hi
= ht
->ht_array
; todo
> 0 && !got_int
; ++hi
)
1947 if (!HASHITEM_EMPTY(hi
))
1951 if (empty
|| di
->di_tv
.v_type
!= VAR_STRING
1952 || di
->di_tv
.vval
.v_string
!= NULL
)
1953 list_one_var(di
, prefix
, first
);
1959 * List global variables.
1962 list_glob_vars(first
)
1965 list_hashtable_vars(&globvarht
, (char_u
*)"", TRUE
, first
);
1969 * List buffer variables.
1972 list_buf_vars(first
)
1975 char_u numbuf
[NUMBUFLEN
];
1977 list_hashtable_vars(&curbuf
->b_vars
.dv_hashtab
, (char_u
*)"b:",
1980 sprintf((char *)numbuf
, "%ld", (long)curbuf
->b_changedtick
);
1981 list_one_var_a((char_u
*)"b:", (char_u
*)"changedtick", VAR_NUMBER
,
1986 * List window variables.
1989 list_win_vars(first
)
1992 list_hashtable_vars(&curwin
->w_vars
.dv_hashtab
,
1993 (char_u
*)"w:", TRUE
, first
);
1998 * List tab page variables.
2001 list_tab_vars(first
)
2004 list_hashtable_vars(&curtab
->tp_vars
.dv_hashtab
,
2005 (char_u
*)"t:", TRUE
, first
);
2010 * List Vim variables.
2013 list_vim_vars(first
)
2016 list_hashtable_vars(&vimvarht
, (char_u
*)"v:", FALSE
, first
);
2020 * List script-local variables, if there is a script.
2023 list_script_vars(first
)
2026 if (current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
2027 list_hashtable_vars(&SCRIPT_VARS(current_SID
),
2028 (char_u
*)"s:", FALSE
, first
);
2032 * List function variables, if there is a function.
2035 list_func_vars(first
)
2038 if (current_funccal
!= NULL
)
2039 list_hashtable_vars(¤t_funccal
->l_vars
.dv_hashtab
,
2040 (char_u
*)"l:", FALSE
, first
);
2044 * List variables in "arg".
2047 list_arg_vars(eap
, arg
, first
)
2060 while (!ends_excmd(*arg
) && !got_int
)
2062 if (error
|| eap
->skip
)
2064 arg
= find_name_end(arg
, NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
2065 if (!vim_iswhite(*arg
) && !ends_excmd(*arg
))
2068 EMSG(_(e_trailing
));
2074 /* get_name_len() takes care of expanding curly braces */
2075 name_start
= name
= arg
;
2076 len
= get_name_len(&arg
, &tofree
, TRUE
, TRUE
);
2079 /* This is mainly to keep test 49 working: when expanding
2080 * curly braces fails overrule the exception error message. */
2081 if (len
< 0 && !aborting())
2084 EMSG2(_(e_invarg2
), arg
);
2093 if (get_var_tv(name
, len
, &tv
, TRUE
) == FAIL
)
2097 /* handle d.key, l[idx], f(expr) */
2099 if (handle_subscript(&arg
, &tv
, TRUE
, TRUE
) == FAIL
)
2103 if (arg
== arg_subsc
&& len
== 2 && name
[1] == ':')
2107 case 'g': list_glob_vars(first
); break;
2108 case 'b': list_buf_vars(first
); break;
2109 case 'w': list_win_vars(first
); break;
2111 case 't': list_tab_vars(first
); break;
2113 case 'v': list_vim_vars(first
); break;
2114 case 's': list_script_vars(first
); break;
2115 case 'l': list_func_vars(first
); break;
2117 EMSG2(_("E738: Can't list variables for %s"), name
);
2122 char_u numbuf
[NUMBUFLEN
];
2127 s
= echo_string(&tv
, &tf
, numbuf
, 0);
2130 list_one_var_a((char_u
*)"",
2131 arg
== arg_subsc
? name
: name_start
,
2133 s
== NULL
? (char_u
*)"" : s
,
2146 arg
= skipwhite(arg
);
2153 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2154 * Returns a pointer to the char just after the var name.
2155 * Returns NULL if there is an error.
2158 ex_let_one(arg
, tv
, copy
, endchars
, op
)
2159 char_u
*arg
; /* points to variable name */
2160 typval_T
*tv
; /* value to assign to variable */
2161 int copy
; /* copy value from "tv" */
2162 char_u
*endchars
; /* valid chars after variable name or NULL */
2163 char_u
*op
; /* "+", "-", "." or NULL*/
2168 char_u
*arg_end
= NULL
;
2171 char_u
*tofree
= NULL
;
2174 * ":let $VAR = expr": Set environment variable.
2178 /* Find the end of the name. */
2181 len
= get_env_len(&arg
);
2183 EMSG2(_(e_invarg2
), name
- 1);
2186 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2187 EMSG2(_(e_letwrong
), op
);
2188 else if (endchars
!= NULL
2189 && vim_strchr(endchars
, *skipwhite(arg
)) == NULL
)
2190 EMSG(_(e_letunexp
));
2195 p
= get_tv_string_chk(tv
);
2196 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2198 int mustfree
= FALSE
;
2199 char_u
*s
= vim_getenv(name
, &mustfree
);
2203 p
= tofree
= concat_str(s
, p
);
2210 vim_setenv(name
, p
);
2211 if (STRICMP(name
, "HOME") == 0)
2213 else if (didset_vim
&& STRICMP(name
, "VIM") == 0)
2215 else if (didset_vimruntime
2216 && STRICMP(name
, "VIMRUNTIME") == 0)
2217 didset_vimruntime
= FALSE
;
2227 * ":let &option = expr": Set option value.
2228 * ":let &l:option = expr": Set local option value.
2229 * ":let &g:option = expr": Set global option value.
2231 else if (*arg
== '&')
2233 /* Find the end of the name. */
2234 p
= find_option_end(&arg
, &opt_flags
);
2235 if (p
== NULL
|| (endchars
!= NULL
2236 && vim_strchr(endchars
, *skipwhite(p
)) == NULL
))
2237 EMSG(_(e_letunexp
));
2243 char_u
*stringval
= NULL
;
2249 n
= get_tv_number(tv
);
2250 s
= get_tv_string_chk(tv
); /* != NULL if number or string */
2251 if (s
!= NULL
&& op
!= NULL
&& *op
!= '=')
2253 opt_type
= get_option_value(arg
, &numval
,
2254 &stringval
, opt_flags
);
2255 if ((opt_type
== 1 && *op
== '.')
2256 || (opt_type
== 0 && *op
!= '.'))
2257 EMSG2(_(e_letwrong
), op
);
2260 if (opt_type
== 1) /* number */
2267 else if (opt_type
== 0 && stringval
!= NULL
) /* string */
2269 s
= concat_str(stringval
, s
);
2270 vim_free(stringval
);
2277 set_option_value(arg
, n
, s
, opt_flags
);
2281 vim_free(stringval
);
2286 * ":let @r = expr": Set register contents.
2288 else if (*arg
== '@')
2291 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2292 EMSG2(_(e_letwrong
), op
);
2293 else if (endchars
!= NULL
2294 && vim_strchr(endchars
, *skipwhite(arg
+ 1)) == NULL
)
2295 EMSG(_(e_letunexp
));
2298 char_u
*ptofree
= NULL
;
2301 p
= get_tv_string_chk(tv
);
2302 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2304 s
= get_reg_contents(*arg
== '@' ? '"' : *arg
, TRUE
, TRUE
);
2307 p
= ptofree
= concat_str(s
, p
);
2313 write_reg_contents(*arg
== '@' ? '"' : *arg
, p
, -1, FALSE
);
2321 * ":let var = expr": Set internal variable.
2322 * ":let {expr} = expr": Idem, name made with curly braces
2324 else if (eval_isnamec1(*arg
) || *arg
== '{')
2328 p
= get_lval(arg
, tv
, &lv
, FALSE
, FALSE
, FALSE
, FNE_CHECK_START
);
2329 if (p
!= NULL
&& lv
.ll_name
!= NULL
)
2331 if (endchars
!= NULL
&& vim_strchr(endchars
, *skipwhite(p
)) == NULL
)
2332 EMSG(_(e_letunexp
));
2335 set_var_lval(&lv
, p
, tv
, copy
, op
);
2343 EMSG2(_(e_invarg2
), arg
);
2349 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2352 check_changedtick(arg
)
2355 if (STRNCMP(arg
, "b:changedtick", 13) == 0 && !eval_isnamec(arg
[13]))
2357 EMSG2(_(e_readonlyvar
), arg
);
2364 * Get an lval: variable, Dict item or List item that can be assigned a value
2365 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2366 * "name.key", "name.key[expr]" etc.
2367 * Indexing only works if "name" is an existing List or Dictionary.
2368 * "name" points to the start of the name.
2369 * If "rettv" is not NULL it points to the value to be assigned.
2370 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2371 * wrong; must end in space or cmd separator.
2373 * Returns a pointer to just after the name, including indexes.
2374 * When an evaluation error occurs "lp->ll_name" is NULL;
2375 * Returns NULL for a parsing error. Still need to free items in "lp"!
2378 get_lval(name
, rettv
, lp
, unlet
, skip
, quiet
, fne_flags
)
2384 int quiet
; /* don't give error messages */
2385 int fne_flags
; /* flags for find_name_end() */
2388 char_u
*expr_start
, *expr_end
;
2399 /* Clear everything in "lp". */
2400 vim_memset(lp
, 0, sizeof(lval_T
));
2404 /* When skipping just find the end of the name. */
2406 return find_name_end(name
, NULL
, NULL
, FNE_INCL_BR
| fne_flags
);
2409 /* Find the end of the name. */
2410 p
= find_name_end(name
, &expr_start
, &expr_end
, fne_flags
);
2411 if (expr_start
!= NULL
)
2413 /* Don't expand the name when we already know there is an error. */
2414 if (unlet
&& !vim_iswhite(*p
) && !ends_excmd(*p
)
2415 && *p
!= '[' && *p
!= '.')
2417 EMSG(_(e_trailing
));
2421 lp
->ll_exp_name
= make_expanded_name(name
, expr_start
, expr_end
, p
);
2422 if (lp
->ll_exp_name
== NULL
)
2424 /* Report an invalid expression in braces, unless the
2425 * expression evaluation has been cancelled due to an
2426 * aborting error, an interrupt, or an exception. */
2427 if (!aborting() && !quiet
)
2430 EMSG2(_(e_invarg2
), name
);
2434 lp
->ll_name
= lp
->ll_exp_name
;
2439 /* Without [idx] or .key we are done. */
2440 if ((*p
!= '[' && *p
!= '.') || lp
->ll_name
== NULL
)
2445 v
= find_var(lp
->ll_name
, &ht
);
2446 if (v
== NULL
&& !quiet
)
2447 EMSG2(_(e_undefvar
), lp
->ll_name
);
2453 * Loop until no more [idx] or .key is following.
2455 lp
->ll_tv
= &v
->di_tv
;
2456 while (*p
== '[' || (*p
== '.' && lp
->ll_tv
->v_type
== VAR_DICT
))
2458 if (!(lp
->ll_tv
->v_type
== VAR_LIST
&& lp
->ll_tv
->vval
.v_list
!= NULL
)
2459 && !(lp
->ll_tv
->v_type
== VAR_DICT
2460 && lp
->ll_tv
->vval
.v_dict
!= NULL
))
2463 EMSG(_("E689: Can only index a List or Dictionary"));
2469 EMSG(_("E708: [:] must come last"));
2477 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
2482 EMSG(_(e_emptykey
));
2489 /* Get the index [expr] or the first index [expr: ]. */
2490 p
= skipwhite(p
+ 1);
2496 if (eval1(&p
, &var1
, TRUE
) == FAIL
) /* recursive! */
2498 if (get_tv_string_chk(&var1
) == NULL
)
2500 /* not a number or string */
2506 /* Optionally get the second index [ :expr]. */
2509 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2512 EMSG(_(e_dictrange
));
2517 if (rettv
!= NULL
&& (rettv
->v_type
!= VAR_LIST
2518 || rettv
->vval
.v_list
== NULL
))
2521 EMSG(_("E709: [:] requires a List value"));
2526 p
= skipwhite(p
+ 1);
2528 lp
->ll_empty2
= TRUE
;
2531 lp
->ll_empty2
= FALSE
;
2532 if (eval1(&p
, &var2
, TRUE
) == FAIL
) /* recursive! */
2538 if (get_tv_string_chk(&var2
) == NULL
)
2540 /* not a number or string */
2547 lp
->ll_range
= TRUE
;
2550 lp
->ll_range
= FALSE
;
2555 EMSG(_(e_missbrac
));
2558 if (lp
->ll_range
&& !lp
->ll_empty2
)
2563 /* Skip to past ']'. */
2567 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2571 /* "[key]": get key from "var1" */
2572 key
= get_tv_string(&var1
); /* is number or string */
2576 EMSG(_(e_emptykey
));
2582 lp
->ll_dict
= lp
->ll_tv
->vval
.v_dict
;
2583 lp
->ll_di
= dict_find(lp
->ll_dict
, key
, len
);
2584 if (lp
->ll_di
== NULL
)
2586 /* Key does not exist in dict: may need to add it. */
2587 if (*p
== '[' || *p
== '.' || unlet
)
2590 EMSG2(_(e_dictkey
), key
);
2596 lp
->ll_newkey
= vim_strsave(key
);
2598 lp
->ll_newkey
= vim_strnsave(key
, len
);
2601 if (lp
->ll_newkey
== NULL
)
2607 lp
->ll_tv
= &lp
->ll_di
->di_tv
;
2612 * Get the number and item for the only or first index of the List.
2618 lp
->ll_n1
= get_tv_number(&var1
); /* is number or string */
2622 lp
->ll_list
= lp
->ll_tv
->vval
.v_list
;
2623 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2624 if (lp
->ll_li
== NULL
)
2629 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2632 if (lp
->ll_li
== NULL
)
2634 if (lp
->ll_range
&& !lp
->ll_empty2
)
2640 * May need to find the item or absolute index for the second
2642 * When no index given: "lp->ll_empty2" is TRUE.
2643 * Otherwise "lp->ll_n2" is set to the second index.
2645 if (lp
->ll_range
&& !lp
->ll_empty2
)
2647 lp
->ll_n2
= get_tv_number(&var2
); /* is number or string */
2651 ni
= list_find(lp
->ll_list
, lp
->ll_n2
);
2654 lp
->ll_n2
= list_idx_of_item(lp
->ll_list
, ni
);
2657 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2659 lp
->ll_n1
= list_idx_of_item(lp
->ll_list
, lp
->ll_li
);
2660 if (lp
->ll_n2
< lp
->ll_n1
)
2664 lp
->ll_tv
= &lp
->ll_li
->li_tv
;
2672 * Clear lval "lp" that was filled by get_lval().
2678 vim_free(lp
->ll_exp_name
);
2679 vim_free(lp
->ll_newkey
);
2683 * Set a variable that was parsed by get_lval() to "rettv".
2684 * "endp" points to just after the parsed name.
2685 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
2688 set_var_lval(lp
, endp
, rettv
, copy
, op
)
2699 if (lp
->ll_tv
== NULL
)
2701 if (!check_changedtick(lp
->ll_name
))
2705 if (op
!= NULL
&& *op
!= '=')
2709 /* handle +=, -= and .= */
2710 if (get_var_tv(lp
->ll_name
, (int)STRLEN(lp
->ll_name
),
2713 if (tv_op(&tv
, rettv
, op
) == OK
)
2714 set_var(lp
->ll_name
, &tv
, FALSE
);
2719 set_var(lp
->ll_name
, rettv
, copy
);
2723 else if (tv_check_lock(lp
->ll_newkey
== NULL
2725 : lp
->ll_tv
->vval
.v_dict
->dv_lock
, lp
->ll_name
))
2727 else if (lp
->ll_range
)
2730 * Assign the List values to the list items.
2732 for (ri
= rettv
->vval
.v_list
->lv_first
; ri
!= NULL
; )
2734 if (op
!= NULL
&& *op
!= '=')
2735 tv_op(&lp
->ll_li
->li_tv
, &ri
->li_tv
, op
);
2738 clear_tv(&lp
->ll_li
->li_tv
);
2739 copy_tv(&ri
->li_tv
, &lp
->ll_li
->li_tv
);
2742 if (ri
== NULL
|| (!lp
->ll_empty2
&& lp
->ll_n2
== lp
->ll_n1
))
2744 if (lp
->ll_li
->li_next
== NULL
)
2746 /* Need to add an empty item. */
2747 if (list_append_number(lp
->ll_list
, 0) == FAIL
)
2753 lp
->ll_li
= lp
->ll_li
->li_next
;
2757 EMSG(_("E710: List value has more items than target"));
2758 else if (lp
->ll_empty2
2759 ? (lp
->ll_li
!= NULL
&& lp
->ll_li
->li_next
!= NULL
)
2760 : lp
->ll_n1
!= lp
->ll_n2
)
2761 EMSG(_("E711: List value has not enough items"));
2766 * Assign to a List or Dictionary item.
2768 if (lp
->ll_newkey
!= NULL
)
2770 if (op
!= NULL
&& *op
!= '=')
2772 EMSG2(_(e_letwrong
), op
);
2776 /* Need to add an item to the Dictionary. */
2777 di
= dictitem_alloc(lp
->ll_newkey
);
2780 if (dict_add(lp
->ll_tv
->vval
.v_dict
, di
) == FAIL
)
2785 lp
->ll_tv
= &di
->di_tv
;
2787 else if (op
!= NULL
&& *op
!= '=')
2789 tv_op(lp
->ll_tv
, rettv
, op
);
2793 clear_tv(lp
->ll_tv
);
2796 * Assign the value to the variable or list item.
2799 copy_tv(rettv
, lp
->ll_tv
);
2802 *lp
->ll_tv
= *rettv
;
2803 lp
->ll_tv
->v_lock
= 0;
2810 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2811 * Returns OK or FAIL.
2820 char_u numbuf
[NUMBUFLEN
];
2823 /* Can't do anything with a Funcref or a Dict on the right. */
2824 if (tv2
->v_type
!= VAR_FUNC
&& tv2
->v_type
!= VAR_DICT
)
2826 switch (tv1
->v_type
)
2833 if (*op
!= '+' || tv2
->v_type
!= VAR_LIST
)
2836 if (tv1
->vval
.v_list
!= NULL
&& tv2
->vval
.v_list
!= NULL
)
2837 list_extend(tv1
->vval
.v_list
, tv2
->vval
.v_list
, NULL
);
2842 if (tv2
->v_type
== VAR_LIST
)
2844 if (*op
== '+' || *op
== '-')
2846 /* nr += nr or nr -= nr*/
2847 n
= get_tv_number(tv1
);
2849 n
+= get_tv_number(tv2
);
2851 n
-= get_tv_number(tv2
);
2853 tv1
->v_type
= VAR_NUMBER
;
2854 tv1
->vval
.v_number
= n
;
2859 s
= get_tv_string(tv1
);
2860 s
= concat_str(s
, get_tv_string_buf(tv2
, numbuf
));
2862 tv1
->v_type
= VAR_STRING
;
2863 tv1
->vval
.v_string
= s
;
2869 EMSG2(_(e_letwrong
), op
);
2874 * Add a watcher to a list.
2877 list_add_watch(l
, lw
)
2881 lw
->lw_next
= l
->lv_watch
;
2886 * Remove a watcher from a list.
2887 * No warning when it isn't found...
2890 list_rem_watch(l
, lwrem
)
2894 listwatch_T
*lw
, **lwp
;
2897 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
2909 * Just before removing an item from a list: advance watchers to the next
2913 list_fix_watch(l
, item
)
2919 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
2920 if (lw
->lw_item
== item
)
2921 lw
->lw_item
= item
->li_next
;
2925 * Evaluate the expression used in a ":for var in expr" command.
2926 * "arg" points to "var".
2927 * Set "*errp" to TRUE for an error, FALSE otherwise;
2928 * Return a pointer that holds the info. Null when there is an error.
2931 eval_for_line(arg
, errp
, nextcmdp
, skip
)
2942 *errp
= TRUE
; /* default: there is an error */
2944 fi
= (forinfo_T
*)alloc_clear(sizeof(forinfo_T
));
2948 expr
= skip_var_list(arg
, &fi
->fi_varcount
, &fi
->fi_semicolon
);
2952 expr
= skipwhite(expr
);
2953 if (expr
[0] != 'i' || expr
[1] != 'n' || !vim_iswhite(expr
[2]))
2955 EMSG(_("E690: Missing \"in\" after :for"));
2961 if (eval0(skipwhite(expr
+ 2), &tv
, nextcmdp
, !skip
) == OK
)
2967 if (tv
.v_type
!= VAR_LIST
|| l
== NULL
)
2974 /* No need to increment the refcount, it's already set for the
2975 * list being used in "tv". */
2977 list_add_watch(l
, &fi
->fi_lw
);
2978 fi
->fi_lw
.lw_item
= l
->lv_first
;
2989 * Use the first item in a ":for" list. Advance to the next.
2990 * Assign the values to the variable (list). "arg" points to the first one.
2991 * Return TRUE when a valid item was found, FALSE when at end of list or
2995 next_for_item(fi_void
, arg
)
2999 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3003 item
= fi
->fi_lw
.lw_item
;
3008 fi
->fi_lw
.lw_item
= item
->li_next
;
3009 result
= (ex_let_vars(arg
, &item
->li_tv
, TRUE
,
3010 fi
->fi_semicolon
, fi
->fi_varcount
, NULL
) == OK
);
3016 * Free the structure used to store info used by ":for".
3019 free_for_info(fi_void
)
3022 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3024 if (fi
!= NULL
&& fi
->fi_list
!= NULL
)
3026 list_rem_watch(fi
->fi_list
, &fi
->fi_lw
);
3027 list_unref(fi
->fi_list
);
3032 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3035 set_context_for_expression(xp
, arg
, cmdidx
)
3044 if (cmdidx
== CMD_let
)
3046 xp
->xp_context
= EXPAND_USER_VARS
;
3047 if (vim_strpbrk(arg
, (char_u
*)"\"'+-*/%.=!?~|&$([<>,#") == NULL
)
3049 /* ":let var1 var2 ...": find last space. */
3050 for (p
= arg
+ STRLEN(arg
); p
>= arg
; )
3053 mb_ptr_back(arg
, p
);
3054 if (vim_iswhite(*p
))
3061 xp
->xp_context
= cmdidx
== CMD_call
? EXPAND_FUNCTIONS
3062 : EXPAND_EXPRESSION
;
3063 while ((xp
->xp_pattern
= vim_strpbrk(arg
,
3064 (char_u
*)"\"'+-*/%.=!?~|&$([<>,#")) != NULL
)
3066 c
= *xp
->xp_pattern
;
3069 c
= xp
->xp_pattern
[1];
3073 xp
->xp_context
= cmdidx
!= CMD_let
|| got_eq
3074 ? EXPAND_EXPRESSION
: EXPAND_NOTHING
;
3078 xp
->xp_context
= EXPAND_SETTINGS
;
3079 if ((c
== 'l' || c
== 'g') && xp
->xp_pattern
[2] == ':')
3080 xp
->xp_pattern
+= 2;
3086 /* environment variable */
3087 xp
->xp_context
= EXPAND_ENV_VARS
;
3092 xp
->xp_context
= EXPAND_EXPRESSION
;
3095 && xp
->xp_context
== EXPAND_FUNCTIONS
3096 && vim_strchr(xp
->xp_pattern
, '(') == NULL
)
3098 /* Function name can start with "<SNR>" */
3101 else if (cmdidx
!= CMD_let
|| got_eq
)
3103 if (c
== '"') /* string */
3105 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '"')
3106 if (c
== '\\' && xp
->xp_pattern
[1] != NUL
)
3108 xp
->xp_context
= EXPAND_NOTHING
;
3110 else if (c
== '\'') /* literal string */
3112 /* Trick: '' is like stopping and starting a literal string. */
3113 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '\'')
3115 xp
->xp_context
= EXPAND_NOTHING
;
3119 if (xp
->xp_pattern
[1] == '|')
3122 xp
->xp_context
= EXPAND_EXPRESSION
;
3125 xp
->xp_context
= EXPAND_COMMANDS
;
3128 xp
->xp_context
= EXPAND_EXPRESSION
;
3131 /* Doesn't look like something valid, expand as an expression
3133 xp
->xp_context
= EXPAND_EXPRESSION
;
3134 arg
= xp
->xp_pattern
;
3136 while ((c
= *++arg
) != NUL
&& (c
== ' ' || c
== '\t'))
3139 xp
->xp_pattern
= arg
;
3142 #endif /* FEAT_CMDL_COMPL */
3145 * ":1,25call func(arg1, arg2)" function call.
3151 char_u
*arg
= eap
->arg
;
3162 tofree
= trans_function_name(&arg
, eap
->skip
, TFN_INT
, &fudi
);
3163 if (fudi
.fd_newkey
!= NULL
)
3165 /* Still need to give an error message for missing key. */
3166 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
3167 vim_free(fudi
.fd_newkey
);
3172 /* Increase refcount on dictionary, it could get deleted when evaluating
3174 if (fudi
.fd_dict
!= NULL
)
3175 ++fudi
.fd_dict
->dv_refcount
;
3177 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3178 len
= (int)STRLEN(tofree
);
3179 name
= deref_func_name(tofree
, &len
);
3181 /* Skip white space to allow ":call func ()". Not good, but required for
3182 * backward compatibility. */
3183 startarg
= skipwhite(arg
);
3184 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
3186 if (*startarg
!= '(')
3188 EMSG2(_("E107: Missing braces: %s"), eap
->arg
);
3193 * When skipping, evaluate the function once, to find the end of the
3195 * When the function takes a range, this is discovered after the first
3196 * call, and the loop is broken.
3201 lnum
= eap
->line2
; /* do it once, also with an invalid range */
3205 for ( ; lnum
<= eap
->line2
; ++lnum
)
3207 if (!eap
->skip
&& eap
->addr_count
> 0)
3209 curwin
->w_cursor
.lnum
= lnum
;
3210 curwin
->w_cursor
.col
= 0;
3213 if (get_func_tv(name
, (int)STRLEN(name
), &rettv
, &arg
,
3214 eap
->line1
, eap
->line2
, &doesrange
,
3215 !eap
->skip
, fudi
.fd_dict
) == FAIL
)
3221 /* Handle a function returning a Funcref, Dictionary or List. */
3222 if (handle_subscript(&arg
, &rettv
, !eap
->skip
, TRUE
) == FAIL
)
3229 if (doesrange
|| eap
->skip
)
3232 /* Stop when immediately aborting on error, or when an interrupt
3233 * occurred or an exception was thrown but not caught.
3234 * get_func_tv() returned OK, so that the check for trailing
3235 * characters below is executed. */
3244 /* Check for trailing illegal characters and a following command. */
3245 if (!ends_excmd(*arg
))
3248 EMSG(_(e_trailing
));
3251 eap
->nextcmd
= check_nextcmd(arg
);
3255 dict_unref(fudi
.fd_dict
);
3260 * ":unlet[!] var1 ... " command.
3266 ex_unletlock(eap
, eap
->arg
, 0);
3270 * ":lockvar" and ":unlockvar" commands
3276 char_u
*arg
= eap
->arg
;
3281 else if (vim_isdigit(*arg
))
3283 deep
= getdigits(&arg
);
3284 arg
= skipwhite(arg
);
3287 ex_unletlock(eap
, arg
, deep
);
3291 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3294 ex_unletlock(eap
, argstart
, deep
)
3299 char_u
*arg
= argstart
;
3306 /* Parse the name and find the end. */
3307 name_end
= get_lval(arg
, NULL
, &lv
, TRUE
, eap
->skip
|| error
, FALSE
,
3309 if (lv
.ll_name
== NULL
)
3310 error
= TRUE
; /* error but continue parsing */
3311 if (name_end
== NULL
|| (!vim_iswhite(*name_end
)
3312 && !ends_excmd(*name_end
)))
3314 if (name_end
!= NULL
)
3317 EMSG(_(e_trailing
));
3319 if (!(eap
->skip
|| error
))
3324 if (!error
&& !eap
->skip
)
3326 if (eap
->cmdidx
== CMD_unlet
)
3328 if (do_unlet_var(&lv
, name_end
, eap
->forceit
) == FAIL
)
3333 if (do_lock_var(&lv
, name_end
, deep
,
3334 eap
->cmdidx
== CMD_lockvar
) == FAIL
)
3342 arg
= skipwhite(name_end
);
3343 } while (!ends_excmd(*arg
));
3345 eap
->nextcmd
= check_nextcmd(arg
);
3349 do_unlet_var(lp
, name_end
, forceit
)
3357 if (lp
->ll_tv
== NULL
)
3362 /* Normal name or expanded name. */
3363 if (check_changedtick(lp
->ll_name
))
3365 else if (do_unlet(lp
->ll_name
, forceit
) == FAIL
)
3369 else if (tv_check_lock(lp
->ll_tv
->v_lock
, lp
->ll_name
))
3371 else if (lp
->ll_range
)
3375 /* Delete a range of List items. */
3376 while (lp
->ll_li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3378 li
= lp
->ll_li
->li_next
;
3379 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3386 if (lp
->ll_list
!= NULL
)
3387 /* unlet a List item. */
3388 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3390 /* unlet a Dictionary item. */
3391 dictitem_remove(lp
->ll_dict
, lp
->ll_di
);
3398 * "unlet" a variable. Return OK if it existed, FAIL if not.
3399 * When "forceit" is TRUE don't complain if the variable doesn't exist.
3402 do_unlet(name
, forceit
)
3410 ht
= find_var_ht(name
, &varname
);
3411 if (ht
!= NULL
&& *varname
!= NUL
)
3413 hi
= hash_find(ht
, varname
);
3414 if (!HASHITEM_EMPTY(hi
))
3416 if (var_check_fixed(HI2DI(hi
)->di_flags
, name
))
3418 if (var_check_ro(HI2DI(hi
)->di_flags
, name
))
3426 EMSG2(_("E108: No such variable: \"%s\""), name
);
3431 * Lock or unlock variable indicated by "lp".
3432 * "deep" is the levels to go (-1 for unlimited);
3433 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3436 do_lock_var(lp
, name_end
, deep
, lock
)
3446 if (deep
== 0) /* nothing to do */
3449 if (lp
->ll_tv
== NULL
)
3454 /* Normal name or expanded name. */
3455 if (check_changedtick(lp
->ll_name
))
3459 di
= find_var(lp
->ll_name
, NULL
);
3465 di
->di_flags
|= DI_FLAGS_LOCK
;
3467 di
->di_flags
&= ~DI_FLAGS_LOCK
;
3468 item_lock(&di
->di_tv
, deep
, lock
);
3473 else if (lp
->ll_range
)
3475 listitem_T
*li
= lp
->ll_li
;
3477 /* (un)lock a range of List items. */
3478 while (li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3480 item_lock(&li
->li_tv
, deep
, lock
);
3485 else if (lp
->ll_list
!= NULL
)
3486 /* (un)lock a List item. */
3487 item_lock(&lp
->ll_li
->li_tv
, deep
, lock
);
3489 /* un(lock) a Dictionary item. */
3490 item_lock(&lp
->ll_di
->di_tv
, deep
, lock
);
3496 * Lock or unlock an item. "deep" is nr of levels to go.
3499 item_lock(tv
, deep
, lock
)
3504 static int recurse
= 0;
3511 if (recurse
>= DICT_MAXNEST
)
3513 EMSG(_("E743: variable nested too deep for (un)lock"));
3520 /* lock/unlock the item itself */
3522 tv
->v_lock
|= VAR_LOCKED
;
3524 tv
->v_lock
&= ~VAR_LOCKED
;
3529 if ((l
= tv
->vval
.v_list
) != NULL
)
3532 l
->lv_lock
|= VAR_LOCKED
;
3534 l
->lv_lock
&= ~VAR_LOCKED
;
3535 if (deep
< 0 || deep
> 1)
3536 /* recursive: lock/unlock the items the List contains */
3537 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
3538 item_lock(&li
->li_tv
, deep
- 1, lock
);
3542 if ((d
= tv
->vval
.v_dict
) != NULL
)
3545 d
->dv_lock
|= VAR_LOCKED
;
3547 d
->dv_lock
&= ~VAR_LOCKED
;
3548 if (deep
< 0 || deep
> 1)
3550 /* recursive: lock/unlock the items the List contains */
3551 todo
= (int)d
->dv_hashtab
.ht_used
;
3552 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
3554 if (!HASHITEM_EMPTY(hi
))
3557 item_lock(&HI2DI(hi
)->di_tv
, deep
- 1, lock
);
3567 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3568 * it refers to a List or Dictionary that is locked.
3574 return (tv
->v_lock
& VAR_LOCKED
)
3575 || (tv
->v_type
== VAR_LIST
3576 && tv
->vval
.v_list
!= NULL
3577 && (tv
->vval
.v_list
->lv_lock
& VAR_LOCKED
))
3578 || (tv
->v_type
== VAR_DICT
3579 && tv
->vval
.v_dict
!= NULL
3580 && (tv
->vval
.v_dict
->dv_lock
& VAR_LOCKED
));
3583 #if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3585 * Delete all "menutrans_" variables.
3588 del_menutrans_vars()
3593 hash_lock(&globvarht
);
3594 todo
= (int)globvarht
.ht_used
;
3595 for (hi
= globvarht
.ht_array
; todo
> 0 && !got_int
; ++hi
)
3597 if (!HASHITEM_EMPTY(hi
))
3600 if (STRNCMP(HI2DI(hi
)->di_key
, "menutrans_", 10) == 0)
3601 delete_var(&globvarht
, hi
);
3604 hash_unlock(&globvarht
);
3608 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3611 * Local string buffer for the next two functions to store a variable name
3612 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3613 * get_user_var_name().
3616 static char_u
*cat_prefix_varname
__ARGS((int prefix
, char_u
*name
));
3618 static char_u
*varnamebuf
= NULL
;
3619 static int varnamebuflen
= 0;
3622 * Function to concatenate a prefix and a variable name.
3625 cat_prefix_varname(prefix
, name
)
3631 len
= (int)STRLEN(name
) + 3;
3632 if (len
> varnamebuflen
)
3634 vim_free(varnamebuf
);
3635 len
+= 10; /* some additional space */
3636 varnamebuf
= alloc(len
);
3637 if (varnamebuf
== NULL
)
3642 varnamebuflen
= len
;
3644 *varnamebuf
= prefix
;
3645 varnamebuf
[1] = ':';
3646 STRCPY(varnamebuf
+ 2, name
);
3651 * Function given to ExpandGeneric() to obtain the list of user defined
3652 * (global/buffer/window/built-in) variable names.
3656 get_user_var_name(xp
, idx
)
3660 static long_u gdone
;
3661 static long_u bdone
;
3662 static long_u wdone
;
3664 static long_u tdone
;
3667 static hashitem_T
*hi
;
3672 gdone
= bdone
= wdone
= vidx
= 0;
3678 /* Global variables */
3679 if (gdone
< globvarht
.ht_used
)
3682 hi
= globvarht
.ht_array
;
3685 while (HASHITEM_EMPTY(hi
))
3687 if (STRNCMP("g:", xp
->xp_pattern
, 2) == 0)
3688 return cat_prefix_varname('g', hi
->hi_key
);
3693 ht
= &curbuf
->b_vars
.dv_hashtab
;
3694 if (bdone
< ht
->ht_used
)
3700 while (HASHITEM_EMPTY(hi
))
3702 return cat_prefix_varname('b', hi
->hi_key
);
3704 if (bdone
== ht
->ht_used
)
3707 return (char_u
*)"b:changedtick";
3711 ht
= &curwin
->w_vars
.dv_hashtab
;
3712 if (wdone
< ht
->ht_used
)
3718 while (HASHITEM_EMPTY(hi
))
3720 return cat_prefix_varname('w', hi
->hi_key
);
3725 ht
= &curtab
->tp_vars
.dv_hashtab
;
3726 if (tdone
< ht
->ht_used
)
3732 while (HASHITEM_EMPTY(hi
))
3734 return cat_prefix_varname('t', hi
->hi_key
);
3740 return cat_prefix_varname('v', (char_u
*)vimvars
[vidx
++].vv_name
);
3742 vim_free(varnamebuf
);
3748 #endif /* FEAT_CMDL_COMPL */
3751 * types for expressions.
3756 , TYPE_EQUAL
/* == */
3757 , TYPE_NEQUAL
/* != */
3758 , TYPE_GREATER
/* > */
3759 , TYPE_GEQUAL
/* >= */
3760 , TYPE_SMALLER
/* < */
3761 , TYPE_SEQUAL
/* <= */
3762 , TYPE_MATCH
/* =~ */
3763 , TYPE_NOMATCH
/* !~ */
3767 * The "evaluate" argument: When FALSE, the argument is only parsed but not
3768 * executed. The function may return OK, but the rettv will be of type
3769 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3773 * Handle zero level expression.
3774 * This calls eval1() and handles error message and nextcmd.
3775 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
3776 * Note: "rettv.v_lock" is not set.
3777 * Return OK or FAIL.
3780 eval0(arg
, rettv
, nextcmd
, evaluate
)
3790 ret
= eval1(&p
, rettv
, evaluate
);
3791 if (ret
== FAIL
|| !ends_excmd(*p
))
3796 * Report the invalid expression unless the expression evaluation has
3797 * been cancelled due to an aborting error, an interrupt, or an
3801 EMSG2(_(e_invexpr2
), arg
);
3804 if (nextcmd
!= NULL
)
3805 *nextcmd
= check_nextcmd(p
);
3811 * Handle top level expression:
3812 * expr1 ? expr0 : expr0
3814 * "arg" must point to the first non-white of the expression.
3815 * "arg" is advanced to the next non-white after the recognized expression.
3817 * Note: "rettv.v_lock" is not set.
3819 * Return OK or FAIL.
3822 eval1(arg
, rettv
, evaluate
)
3831 * Get the first variable.
3833 if (eval2(arg
, rettv
, evaluate
) == FAIL
)
3836 if ((*arg
)[0] == '?')
3843 if (get_tv_number_chk(rettv
, &error
) != 0)
3851 * Get the second variable.
3853 *arg
= skipwhite(*arg
+ 1);
3854 if (eval1(arg
, rettv
, evaluate
&& result
) == FAIL
) /* recursive! */
3858 * Check for the ":".
3860 if ((*arg
)[0] != ':')
3862 EMSG(_("E109: Missing ':' after '?'"));
3863 if (evaluate
&& result
)
3869 * Get the third variable.
3871 *arg
= skipwhite(*arg
+ 1);
3872 if (eval1(arg
, &var2
, evaluate
&& !result
) == FAIL
) /* recursive! */
3874 if (evaluate
&& result
)
3878 if (evaluate
&& !result
)
3886 * Handle first level expression:
3887 * expr2 || expr2 || expr2 logical OR
3889 * "arg" must point to the first non-white of the expression.
3890 * "arg" is advanced to the next non-white after the recognized expression.
3892 * Return OK or FAIL.
3895 eval2(arg
, rettv
, evaluate
)
3906 * Get the first variable.
3908 if (eval3(arg
, rettv
, evaluate
) == FAIL
)
3912 * Repeat until there is no following "||".
3916 while ((*arg
)[0] == '|' && (*arg
)[1] == '|')
3918 if (evaluate
&& first
)
3920 if (get_tv_number_chk(rettv
, &error
) != 0)
3929 * Get the second variable.
3931 *arg
= skipwhite(*arg
+ 2);
3932 if (eval3(arg
, &var2
, evaluate
&& !result
) == FAIL
)
3936 * Compute the result.
3938 if (evaluate
&& !result
)
3940 if (get_tv_number_chk(&var2
, &error
) != 0)
3948 rettv
->v_type
= VAR_NUMBER
;
3949 rettv
->vval
.v_number
= result
;
3957 * Handle second level expression:
3958 * expr3 && expr3 && expr3 logical AND
3960 * "arg" must point to the first non-white of the expression.
3961 * "arg" is advanced to the next non-white after the recognized expression.
3963 * Return OK or FAIL.
3966 eval3(arg
, rettv
, evaluate
)
3977 * Get the first variable.
3979 if (eval4(arg
, rettv
, evaluate
) == FAIL
)
3983 * Repeat until there is no following "&&".
3987 while ((*arg
)[0] == '&' && (*arg
)[1] == '&')
3989 if (evaluate
&& first
)
3991 if (get_tv_number_chk(rettv
, &error
) == 0)
4000 * Get the second variable.
4002 *arg
= skipwhite(*arg
+ 2);
4003 if (eval4(arg
, &var2
, evaluate
&& result
) == FAIL
)
4007 * Compute the result.
4009 if (evaluate
&& result
)
4011 if (get_tv_number_chk(&var2
, &error
) == 0)
4019 rettv
->v_type
= VAR_NUMBER
;
4020 rettv
->vval
.v_number
= result
;
4028 * Handle third level expression:
4040 * "arg" must point to the first non-white of the expression.
4041 * "arg" is advanced to the next non-white after the recognized expression.
4043 * Return OK or FAIL.
4046 eval4(arg
, rettv
, evaluate
)
4054 exptype_T type
= TYPE_UNKNOWN
;
4055 int type_is
= FALSE
; /* TRUE for "is" and "isnot" */
4059 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4060 regmatch_T regmatch
;
4065 * Get the first variable.
4067 if (eval5(arg
, rettv
, evaluate
) == FAIL
)
4073 case '=': if (p
[1] == '=')
4075 else if (p
[1] == '~')
4078 case '!': if (p
[1] == '=')
4080 else if (p
[1] == '~')
4081 type
= TYPE_NOMATCH
;
4083 case '>': if (p
[1] != '=')
4085 type
= TYPE_GREATER
;
4091 case '<': if (p
[1] != '=')
4093 type
= TYPE_SMALLER
;
4099 case 'i': if (p
[1] == 's')
4101 if (p
[2] == 'n' && p
[3] == 'o' && p
[4] == 't')
4103 if (!vim_isIDc(p
[len
]))
4105 type
= len
== 2 ? TYPE_EQUAL
: TYPE_NEQUAL
;
4113 * If there is a comparitive operator, use it.
4115 if (type
!= TYPE_UNKNOWN
)
4117 /* extra question mark appended: ignore case */
4123 /* extra '#' appended: match case */
4124 else if (p
[len
] == '#')
4129 /* nothing appened: use 'ignorecase' */
4134 * Get the second variable.
4136 *arg
= skipwhite(p
+ len
);
4137 if (eval5(arg
, &var2
, evaluate
) == FAIL
)
4145 if (type_is
&& rettv
->v_type
!= var2
.v_type
)
4147 /* For "is" a different type always means FALSE, for "notis"
4149 n1
= (type
== TYPE_NEQUAL
);
4151 else if (rettv
->v_type
== VAR_LIST
|| var2
.v_type
== VAR_LIST
)
4155 n1
= (rettv
->v_type
== var2
.v_type
4156 && rettv
->vval
.v_list
== var2
.vval
.v_list
);
4157 if (type
== TYPE_NEQUAL
)
4160 else if (rettv
->v_type
!= var2
.v_type
4161 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4163 if (rettv
->v_type
!= var2
.v_type
)
4164 EMSG(_("E691: Can only compare List with List"));
4166 EMSG(_("E692: Invalid operation for Lists"));
4173 /* Compare two Lists for being equal or unequal. */
4174 n1
= list_equal(rettv
->vval
.v_list
, var2
.vval
.v_list
, ic
);
4175 if (type
== TYPE_NEQUAL
)
4180 else if (rettv
->v_type
== VAR_DICT
|| var2
.v_type
== VAR_DICT
)
4184 n1
= (rettv
->v_type
== var2
.v_type
4185 && rettv
->vval
.v_dict
== var2
.vval
.v_dict
);
4186 if (type
== TYPE_NEQUAL
)
4189 else if (rettv
->v_type
!= var2
.v_type
4190 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4192 if (rettv
->v_type
!= var2
.v_type
)
4193 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4195 EMSG(_("E736: Invalid operation for Dictionary"));
4202 /* Compare two Dictionaries for being equal or unequal. */
4203 n1
= dict_equal(rettv
->vval
.v_dict
, var2
.vval
.v_dict
, ic
);
4204 if (type
== TYPE_NEQUAL
)
4209 else if (rettv
->v_type
== VAR_FUNC
|| var2
.v_type
== VAR_FUNC
)
4211 if (rettv
->v_type
!= var2
.v_type
4212 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4214 if (rettv
->v_type
!= var2
.v_type
)
4215 EMSG(_("E693: Can only compare Funcref with Funcref"));
4217 EMSG(_("E694: Invalid operation for Funcrefs"));
4224 /* Compare two Funcrefs for being equal or unequal. */
4225 if (rettv
->vval
.v_string
== NULL
4226 || var2
.vval
.v_string
== NULL
)
4229 n1
= STRCMP(rettv
->vval
.v_string
,
4230 var2
.vval
.v_string
) == 0;
4231 if (type
== TYPE_NEQUAL
)
4237 * If one of the two variables is a number, compare as a number.
4238 * When using "=~" or "!~", always compare as string.
4240 else if ((rettv
->v_type
== VAR_NUMBER
|| var2
.v_type
== VAR_NUMBER
)
4241 && type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4243 n1
= get_tv_number(rettv
);
4244 n2
= get_tv_number(&var2
);
4247 case TYPE_EQUAL
: n1
= (n1
== n2
); break;
4248 case TYPE_NEQUAL
: n1
= (n1
!= n2
); break;
4249 case TYPE_GREATER
: n1
= (n1
> n2
); break;
4250 case TYPE_GEQUAL
: n1
= (n1
>= n2
); break;
4251 case TYPE_SMALLER
: n1
= (n1
< n2
); break;
4252 case TYPE_SEQUAL
: n1
= (n1
<= n2
); break;
4255 case TYPE_NOMATCH
: break; /* avoid gcc warning */
4260 s1
= get_tv_string_buf(rettv
, buf1
);
4261 s2
= get_tv_string_buf(&var2
, buf2
);
4262 if (type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4263 i
= ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
);
4269 case TYPE_EQUAL
: n1
= (i
== 0); break;
4270 case TYPE_NEQUAL
: n1
= (i
!= 0); break;
4271 case TYPE_GREATER
: n1
= (i
> 0); break;
4272 case TYPE_GEQUAL
: n1
= (i
>= 0); break;
4273 case TYPE_SMALLER
: n1
= (i
< 0); break;
4274 case TYPE_SEQUAL
: n1
= (i
<= 0); break;
4278 /* avoid 'l' flag in 'cpoptions' */
4280 p_cpo
= (char_u
*)"";
4281 regmatch
.regprog
= vim_regcomp(s2
,
4282 RE_MAGIC
+ RE_STRING
);
4283 regmatch
.rm_ic
= ic
;
4284 if (regmatch
.regprog
!= NULL
)
4286 n1
= vim_regexec_nl(®match
, s1
, (colnr_T
)0);
4287 vim_free(regmatch
.regprog
);
4288 if (type
== TYPE_NOMATCH
)
4294 case TYPE_UNKNOWN
: break; /* avoid gcc warning */
4299 rettv
->v_type
= VAR_NUMBER
;
4300 rettv
->vval
.v_number
= n1
;
4308 * Handle fourth level expression:
4310 * - number subtraction
4311 * . string concatenation
4313 * "arg" must point to the first non-white of the expression.
4314 * "arg" is advanced to the next non-white after the recognized expression.
4316 * Return OK or FAIL.
4319 eval5(arg
, rettv
, evaluate
)
4329 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4333 * Get the first variable.
4335 if (eval6(arg
, rettv
, evaluate
) == FAIL
)
4339 * Repeat computing, until no '+', '-' or '.' is following.
4344 if (op
!= '+' && op
!= '-' && op
!= '.')
4347 if (op
!= '+' || rettv
->v_type
!= VAR_LIST
)
4349 /* For "list + ...", an illegal use of the first operand as
4350 * a number cannot be determined before evaluating the 2nd
4351 * operand: if this is also a list, all is ok.
4352 * For "something . ...", "something - ..." or "non-list + ...",
4353 * we know that the first operand needs to be a string or number
4354 * without evaluating the 2nd operand. So check before to avoid
4355 * side effects after an error. */
4356 if (evaluate
&& get_tv_string_chk(rettv
) == NULL
)
4364 * Get the second variable.
4366 *arg
= skipwhite(*arg
+ 1);
4367 if (eval6(arg
, &var2
, evaluate
) == FAIL
)
4376 * Compute the result.
4380 s1
= get_tv_string_buf(rettv
, buf1
); /* already checked */
4381 s2
= get_tv_string_buf_chk(&var2
, buf2
);
4382 if (s2
== NULL
) /* type error ? */
4388 p
= concat_str(s1
, s2
);
4390 rettv
->v_type
= VAR_STRING
;
4391 rettv
->vval
.v_string
= p
;
4393 else if (op
== '+' && rettv
->v_type
== VAR_LIST
4394 && var2
.v_type
== VAR_LIST
)
4396 /* concatenate Lists */
4397 if (list_concat(rettv
->vval
.v_list
, var2
.vval
.v_list
,
4411 n1
= get_tv_number_chk(rettv
, &error
);
4414 /* This can only happen for "list + non-list".
4415 * For "non-list + ..." or "something - ...", we returned
4416 * before evaluating the 2nd operand. */
4420 n2
= get_tv_number_chk(&var2
, &error
);
4432 rettv
->v_type
= VAR_NUMBER
;
4433 rettv
->vval
.v_number
= n1
;
4442 * Handle fifth level expression:
4443 * * number multiplication
4447 * "arg" must point to the first non-white of the expression.
4448 * "arg" is advanced to the next non-white after the recognized expression.
4450 * Return OK or FAIL.
4453 eval6(arg
, rettv
, evaluate
)
4464 * Get the first variable.
4466 if (eval7(arg
, rettv
, evaluate
) == FAIL
)
4470 * Repeat computing, until no '*', '/' or '%' is following.
4475 if (op
!= '*' && op
!= '/' && op
!= '%')
4480 n1
= get_tv_number_chk(rettv
, &error
);
4489 * Get the second variable.
4491 *arg
= skipwhite(*arg
+ 1);
4492 if (eval7(arg
, &var2
, evaluate
) == FAIL
)
4497 n2
= get_tv_number_chk(&var2
, &error
);
4503 * Compute the result.
4509 if (n2
== 0) /* give an error message? */
4516 if (n2
== 0) /* give an error message? */
4521 rettv
->v_type
= VAR_NUMBER
;
4522 rettv
->vval
.v_number
= n1
;
4530 * Handle sixth level expression:
4531 * number number constant
4532 * "string" string constant
4533 * 'string' literal string constant
4534 * &option-name option value
4535 * @r register contents
4536 * identifier variable value
4537 * function() function call
4538 * $VAR environment variable
4539 * (expression) nested expression
4541 * {key: val, key: val} Dictionary
4544 * ! in front logical NOT
4545 * - in front unary minus
4546 * + in front unary plus (ignored)
4547 * trailing [] subscript in String or List
4548 * trailing .name entry in Dictionary
4550 * "arg" must point to the first non-white of the expression.
4551 * "arg" is advanced to the next non-white after the recognized expression.
4553 * Return OK or FAIL.
4556 eval7(arg
, rettv
, evaluate
)
4565 char_u
*start_leader
, *end_leader
;
4570 * Initialise variable so that clear_tv() can't mistake this for a
4571 * string and free a string that isn't there.
4573 rettv
->v_type
= VAR_UNKNOWN
;
4576 * Skip '!' and '-' characters. They are handled later.
4578 start_leader
= *arg
;
4579 while (**arg
== '!' || **arg
== '-' || **arg
== '+')
4580 *arg
= skipwhite(*arg
+ 1);
4598 vim_str2nr(*arg
, NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
4602 rettv
->v_type
= VAR_NUMBER
;
4603 rettv
->vval
.v_number
= n
;
4608 * String constant: "string".
4610 case '"': ret
= get_string_tv(arg
, rettv
, evaluate
);
4614 * Literal string constant: 'str''ing'.
4616 case '\'': ret
= get_lit_string_tv(arg
, rettv
, evaluate
);
4620 * List: [expr, expr]
4622 case '[': ret
= get_list_tv(arg
, rettv
, evaluate
);
4626 * Dictionary: {key: val, key: val}
4628 case '{': ret
= get_dict_tv(arg
, rettv
, evaluate
);
4632 * Option value: &name
4634 case '&': ret
= get_option_tv(arg
, rettv
, evaluate
);
4638 * Environment variable: $VAR.
4640 case '$': ret
= get_env_tv(arg
, rettv
, evaluate
);
4644 * Register contents: @r.
4649 rettv
->v_type
= VAR_STRING
;
4650 rettv
->vval
.v_string
= get_reg_contents(**arg
, TRUE
, TRUE
);
4657 * nested expression: (expression).
4659 case '(': *arg
= skipwhite(*arg
+ 1);
4660 ret
= eval1(arg
, rettv
, evaluate
); /* recursive! */
4665 EMSG(_("E110: Missing ')'"));
4671 default: ret
= NOTDONE
;
4678 * Must be a variable or function name.
4679 * Can also be a curly-braces kind of name: {expr}.
4682 len
= get_name_len(arg
, &alias
, evaluate
, TRUE
);
4690 if (**arg
== '(') /* recursive! */
4692 /* If "s" is the name of a variable of type VAR_FUNC
4693 * use its contents. */
4694 s
= deref_func_name(s
, &len
);
4696 /* Invoke the function. */
4697 ret
= get_func_tv(s
, len
, rettv
, arg
,
4698 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
4699 &len
, evaluate
, NULL
);
4700 /* Stop the expression evaluation when immediately
4701 * aborting on error, or when an interrupt occurred or
4702 * an exception was thrown but not caught. */
4711 ret
= get_var_tv(s
, len
, rettv
, TRUE
);
4720 *arg
= skipwhite(*arg
);
4722 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4725 ret
= handle_subscript(arg
, rettv
, evaluate
, TRUE
);
4728 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4730 if (ret
== OK
&& evaluate
&& end_leader
> start_leader
)
4734 val
= get_tv_number_chk(rettv
, &error
);
4742 while (end_leader
> start_leader
)
4745 if (*end_leader
== '!')
4747 else if (*end_leader
== '-')
4751 rettv
->v_type
= VAR_NUMBER
;
4752 rettv
->vval
.v_number
= val
;
4760 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4761 * "*arg" points to the '[' or '.'.
4762 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4765 eval_index(arg
, rettv
, evaluate
, verbose
)
4769 int verbose
; /* give error messages */
4771 int empty1
= FALSE
, empty2
= FALSE
;
4772 typval_T var1
, var2
;
4779 if (rettv
->v_type
== VAR_FUNC
)
4782 EMSG(_("E695: Cannot index a Funcref"));
4792 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
4796 *arg
= skipwhite(key
+ len
);
4803 * Get the (first) variable from inside the [].
4805 *arg
= skipwhite(*arg
+ 1);
4808 else if (eval1(arg
, &var1
, evaluate
) == FAIL
) /* recursive! */
4810 else if (evaluate
&& get_tv_string_chk(&var1
) == NULL
)
4812 /* not a number or string */
4818 * Get the second variable from inside the [:].
4823 *arg
= skipwhite(*arg
+ 1);
4826 else if (eval1(arg
, &var2
, evaluate
) == FAIL
) /* recursive! */
4832 else if (evaluate
&& get_tv_string_chk(&var2
) == NULL
)
4834 /* not a number or string */
4842 /* Check for the ']'. */
4846 EMSG(_(e_missbrac
));
4852 *arg
= skipwhite(*arg
+ 1); /* skip the ']' */
4858 if (!empty1
&& rettv
->v_type
!= VAR_DICT
)
4860 n1
= get_tv_number(&var1
);
4869 n2
= get_tv_number(&var2
);
4874 switch (rettv
->v_type
)
4878 s
= get_tv_string(rettv
);
4879 len
= (long)STRLEN(s
);
4882 /* The resulting variable is a substring. If the indexes
4883 * are out of range the result is empty. */
4894 if (n1
>= len
|| n2
< 0 || n1
> n2
)
4897 s
= vim_strnsave(s
+ n1
, (int)(n2
- n1
+ 1));
4901 /* The resulting variable is a string of a single
4902 * character. If the index is too big or negative the
4903 * result is empty. */
4904 if (n1
>= len
|| n1
< 0)
4907 s
= vim_strnsave(s
+ n1
, 1);
4910 rettv
->v_type
= VAR_STRING
;
4911 rettv
->vval
.v_string
= s
;
4915 len
= list_len(rettv
->vval
.v_list
);
4918 if (!empty1
&& (n1
< 0 || n1
>= len
))
4920 /* For a range we allow invalid values and return an empty
4921 * list. A list index out of range is an error. */
4925 EMSGN(_(e_listidx
), n1
);
4939 if (!empty2
&& (n2
< 0 || n2
+ 1 < n1
))
4944 for (item
= list_find(rettv
->vval
.v_list
, n1
);
4947 if (list_append_tv(l
, &item
->li_tv
) == FAIL
)
4952 item
= item
->li_next
;
4955 rettv
->v_type
= VAR_LIST
;
4956 rettv
->vval
.v_list
= l
;
4961 copy_tv(&list_find(rettv
->vval
.v_list
, n1
)->li_tv
, &var1
);
4971 EMSG(_(e_dictrange
));
4981 key
= get_tv_string(&var1
);
4985 EMSG(_(e_emptykey
));
4991 item
= dict_find(rettv
->vval
.v_dict
, key
, (int)len
);
4993 if (item
== NULL
&& verbose
)
4994 EMSG2(_(e_dictkey
), key
);
5000 copy_tv(&item
->di_tv
, &var1
);
5012 * Get an option value.
5013 * "arg" points to the '&' or '+' before the option name.
5014 * "arg" is advanced to character after the option name.
5015 * Return OK or FAIL.
5018 get_option_tv(arg
, rettv
, evaluate
)
5020 typval_T
*rettv
; /* when NULL, only check if option exists */
5028 int working
= (**arg
== '+'); /* has("+option") */
5033 * Isolate the option name and find its value.
5035 option_end
= find_option_end(arg
, &opt_flags
);
5036 if (option_end
== NULL
)
5039 EMSG2(_("E112: Option name missing: %s"), *arg
);
5051 opt_type
= get_option_value(*arg
, &numval
,
5052 rettv
== NULL
? NULL
: &stringval
, opt_flags
);
5054 if (opt_type
== -3) /* invalid name */
5057 EMSG2(_("E113: Unknown option: %s"), *arg
);
5060 else if (rettv
!= NULL
)
5062 if (opt_type
== -2) /* hidden string option */
5064 rettv
->v_type
= VAR_STRING
;
5065 rettv
->vval
.v_string
= NULL
;
5067 else if (opt_type
== -1) /* hidden number option */
5069 rettv
->v_type
= VAR_NUMBER
;
5070 rettv
->vval
.v_number
= 0;
5072 else if (opt_type
== 1) /* number option */
5074 rettv
->v_type
= VAR_NUMBER
;
5075 rettv
->vval
.v_number
= numval
;
5077 else /* string option */
5079 rettv
->v_type
= VAR_STRING
;
5080 rettv
->vval
.v_string
= stringval
;
5083 else if (working
&& (opt_type
== -2 || opt_type
== -1))
5086 *option_end
= c
; /* put back for error messages */
5093 * Allocate a variable for a string constant.
5094 * Return OK or FAIL.
5097 get_string_tv(arg
, rettv
, evaluate
)
5107 * Find the end of the string, skipping backslashed characters.
5109 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
5111 if (*p
== '\\' && p
[1] != NUL
)
5114 /* A "\<x>" form occupies at least 4 characters, and produces up
5115 * to 6 characters: reserve space for 2 extra */
5123 EMSG2(_("E114: Missing quote: %s"), *arg
);
5127 /* If only parsing, set *arg and return here */
5135 * Copy the string into allocated memory, handling backslashed
5138 name
= alloc((unsigned)(p
- *arg
+ extra
));
5141 rettv
->v_type
= VAR_STRING
;
5142 rettv
->vval
.v_string
= name
;
5144 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; )
5150 case 'b': *name
++ = BS
; ++p
; break;
5151 case 'e': *name
++ = ESC
; ++p
; break;
5152 case 'f': *name
++ = FF
; ++p
; break;
5153 case 'n': *name
++ = NL
; ++p
; break;
5154 case 'r': *name
++ = CAR
; ++p
; break;
5155 case 't': *name
++ = TAB
; ++p
; break;
5157 case 'X': /* hex: "\x1", "\x12" */
5159 case 'u': /* Unicode: "\u0023" */
5161 if (vim_isxdigit(p
[1]))
5164 int c
= toupper(*p
);
5171 while (--n
>= 0 && vim_isxdigit(p
[1]))
5174 nr
= (nr
<< 4) + hex2nr(*p
);
5178 /* For "\u" store the number according to
5181 name
+= (*mb_char2bytes
)(nr
, name
);
5188 /* octal: "\1", "\12", "\123" */
5196 case '7': *name
= *p
++ - '0';
5197 if (*p
>= '0' && *p
<= '7')
5199 *name
= (*name
<< 3) + *p
++ - '0';
5200 if (*p
>= '0' && *p
<= '7')
5201 *name
= (*name
<< 3) + *p
++ - '0';
5206 /* Special key, e.g.: "\<C-W>" */
5207 case '<': extra
= trans_special(&p
, name
, TRUE
);
5215 default: MB_COPY_CHAR(p
, name
);
5220 MB_COPY_CHAR(p
, name
);
5230 * Allocate a variable for a 'str''ing' constant.
5231 * Return OK or FAIL.
5234 get_lit_string_tv(arg
, rettv
, evaluate
)
5244 * Find the end of the string, skipping ''.
5246 for (p
= *arg
+ 1; *p
!= NUL
; mb_ptr_adv(p
))
5259 EMSG2(_("E115: Missing quote: %s"), *arg
);
5263 /* If only parsing return after setting "*arg" */
5271 * Copy the string into allocated memory, handling '' to ' reduction.
5273 str
= alloc((unsigned)((p
- *arg
) - reduce
));
5276 rettv
->v_type
= VAR_STRING
;
5277 rettv
->vval
.v_string
= str
;
5279 for (p
= *arg
+ 1; *p
!= NUL
; )
5287 MB_COPY_CHAR(p
, str
);
5296 * Allocate a variable for a List and fill it from "*arg".
5297 * Return OK or FAIL.
5300 get_list_tv(arg
, rettv
, evaluate
)
5316 *arg
= skipwhite(*arg
+ 1);
5317 while (**arg
!= ']' && **arg
!= NUL
)
5319 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
5323 item
= listitem_alloc();
5327 item
->li_tv
.v_lock
= 0;
5328 list_append(l
, item
);
5338 EMSG2(_("E696: Missing comma in List: %s"), *arg
);
5341 *arg
= skipwhite(*arg
+ 1);
5346 EMSG2(_("E697: Missing end of List ']': %s"), *arg
);
5353 *arg
= skipwhite(*arg
+ 1);
5356 rettv
->v_type
= VAR_LIST
;
5357 rettv
->vval
.v_list
= l
;
5365 * Allocate an empty header for a list.
5366 * Caller should take care of the reference count.
5373 l
= (list_T
*)alloc_clear(sizeof(list_T
));
5376 /* Prepend the list to the list of lists for garbage collection. */
5377 if (first_list
!= NULL
)
5378 first_list
->lv_used_prev
= l
;
5379 l
->lv_used_prev
= NULL
;
5380 l
->lv_used_next
= first_list
;
5387 * Allocate an empty list for a return value.
5388 * Returns OK or FAIL.
5391 rettv_list_alloc(rettv
)
5394 list_T
*l
= list_alloc();
5399 rettv
->vval
.v_list
= l
;
5400 rettv
->v_type
= VAR_LIST
;
5406 * Unreference a list: decrement the reference count and free it when it
5413 if (l
!= NULL
&& --l
->lv_refcount
<= 0)
5418 * Free a list, including all items it points to.
5419 * Ignores the reference count.
5422 list_free(l
, recurse
)
5424 int recurse
; /* Free Lists and Dictionaries recursively. */
5428 /* Remove the list from the list of lists for garbage collection. */
5429 if (l
->lv_used_prev
== NULL
)
5430 first_list
= l
->lv_used_next
;
5432 l
->lv_used_prev
->lv_used_next
= l
->lv_used_next
;
5433 if (l
->lv_used_next
!= NULL
)
5434 l
->lv_used_next
->lv_used_prev
= l
->lv_used_prev
;
5436 for (item
= l
->lv_first
; item
!= NULL
; item
= l
->lv_first
)
5438 /* Remove the item before deleting it. */
5439 l
->lv_first
= item
->li_next
;
5440 if (recurse
|| (item
->li_tv
.v_type
!= VAR_LIST
5441 && item
->li_tv
.v_type
!= VAR_DICT
))
5442 clear_tv(&item
->li_tv
);
5449 * Allocate a list item.
5454 return (listitem_T
*)alloc(sizeof(listitem_T
));
5458 * Free a list item. Also clears the value. Does not notify watchers.
5464 clear_tv(&item
->li_tv
);
5469 * Remove a list item from a List and free it. Also clears the value.
5472 listitem_remove(l
, item
)
5476 list_remove(l
, item
, item
);
5477 listitem_free(item
);
5481 * Get the number of items in a list.
5493 * Return TRUE when two lists have exactly the same values.
5496 list_equal(l1
, l2
, ic
)
5499 int ic
; /* ignore case for strings */
5501 listitem_T
*item1
, *item2
;
5505 if (list_len(l1
) != list_len(l2
))
5508 for (item1
= l1
->lv_first
, item2
= l2
->lv_first
;
5509 item1
!= NULL
&& item2
!= NULL
;
5510 item1
= item1
->li_next
, item2
= item2
->li_next
)
5511 if (!tv_equal(&item1
->li_tv
, &item2
->li_tv
, ic
))
5513 return item1
== NULL
&& item2
== NULL
;
5516 #if defined(FEAT_PYTHON) || defined(PROTO)
5518 * Return the dictitem that an entry in a hashtable points to.
5529 * Return TRUE when two dictionaries have exactly the same key/values.
5532 dict_equal(d1
, d2
, ic
)
5535 int ic
; /* ignore case for strings */
5543 if (dict_len(d1
) != dict_len(d2
))
5546 todo
= (int)d1
->dv_hashtab
.ht_used
;
5547 for (hi
= d1
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
5549 if (!HASHITEM_EMPTY(hi
))
5551 item2
= dict_find(d2
, hi
->hi_key
, -1);
5554 if (!tv_equal(&HI2DI(hi
)->di_tv
, &item2
->di_tv
, ic
))
5563 * Return TRUE if "tv1" and "tv2" have the same value.
5564 * Compares the items just like "==" would compare them, but strings and
5565 * numbers are different.
5568 tv_equal(tv1
, tv2
, ic
)
5571 int ic
; /* ignore case */
5573 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
5575 static int recursive
= 0; /* cach recursive loops */
5578 if (tv1
->v_type
!= tv2
->v_type
)
5580 /* Catch lists and dicts that have an endless loop by limiting
5581 * recursiveness to 1000. We guess they are equal then. */
5582 if (recursive
>= 1000)
5585 switch (tv1
->v_type
)
5589 r
= list_equal(tv1
->vval
.v_list
, tv2
->vval
.v_list
, ic
);
5595 r
= dict_equal(tv1
->vval
.v_dict
, tv2
->vval
.v_dict
, ic
);
5600 return (tv1
->vval
.v_string
!= NULL
5601 && tv2
->vval
.v_string
!= NULL
5602 && STRCMP(tv1
->vval
.v_string
, tv2
->vval
.v_string
) == 0);
5605 return tv1
->vval
.v_number
== tv2
->vval
.v_number
;
5608 s1
= get_tv_string_buf(tv1
, buf1
);
5609 s2
= get_tv_string_buf(tv2
, buf2
);
5610 return ((ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
)) == 0);
5613 EMSG2(_(e_intern2
), "tv_equal()");
5618 * Locate item with index "n" in list "l" and return it.
5619 * A negative index is counted from the end; -1 is the last item.
5620 * Returns NULL when "n" is out of range.
5633 /* Negative index is relative to the end. */
5637 /* Check for index out of range. */
5638 if (n
< 0 || n
>= l
->lv_len
)
5641 /* When there is a cached index may start search from there. */
5642 if (l
->lv_idx_item
!= NULL
)
5644 if (n
< l
->lv_idx
/ 2)
5646 /* closest to the start of the list */
5650 else if (n
> (l
->lv_idx
+ l
->lv_len
) / 2)
5652 /* closest to the end of the list */
5654 idx
= l
->lv_len
- 1;
5658 /* closest to the cached index */
5659 item
= l
->lv_idx_item
;
5665 if (n
< l
->lv_len
/ 2)
5667 /* closest to the start of the list */
5673 /* closest to the end of the list */
5675 idx
= l
->lv_len
- 1;
5681 /* search forward */
5682 item
= item
->li_next
;
5687 /* search backward */
5688 item
= item
->li_prev
;
5692 /* cache the used index */
5694 l
->lv_idx_item
= item
;
5700 * Get list item "l[idx]" as a number.
5703 list_find_nr(l
, idx
, errorp
)
5706 int *errorp
; /* set to TRUE when something wrong */
5710 li
= list_find(l
, idx
);
5717 return get_tv_number_chk(&li
->li_tv
, errorp
);
5721 * Locate "item" list "l" and return its index.
5722 * Returns -1 when "item" is not in the list.
5725 list_idx_of_item(l
, item
)
5735 for (li
= l
->lv_first
; li
!= NULL
&& li
!= item
; li
= li
->li_next
)
5743 * Append item "item" to the end of list "l".
5746 list_append(l
, item
)
5750 if (l
->lv_last
== NULL
)
5755 item
->li_prev
= NULL
;
5759 l
->lv_last
->li_next
= item
;
5760 item
->li_prev
= l
->lv_last
;
5764 item
->li_next
= NULL
;
5768 * Append typval_T "tv" to the end of list "l".
5769 * Return FAIL when out of memory.
5772 list_append_tv(l
, tv
)
5776 listitem_T
*li
= listitem_alloc();
5780 copy_tv(tv
, &li
->li_tv
);
5786 * Add a dictionary to a list. Used by getqflist().
5787 * Return FAIL when out of memory.
5790 list_append_dict(list
, dict
)
5794 listitem_T
*li
= listitem_alloc();
5798 li
->li_tv
.v_type
= VAR_DICT
;
5799 li
->li_tv
.v_lock
= 0;
5800 li
->li_tv
.vval
.v_dict
= dict
;
5801 list_append(list
, li
);
5802 ++dict
->dv_refcount
;
5807 * Make a copy of "str" and append it as an item to list "l".
5808 * When "len" >= 0 use "str[len]".
5809 * Returns FAIL when out of memory.
5812 list_append_string(l
, str
, len
)
5817 listitem_T
*li
= listitem_alloc();
5822 li
->li_tv
.v_type
= VAR_STRING
;
5823 li
->li_tv
.v_lock
= 0;
5825 li
->li_tv
.vval
.v_string
= NULL
;
5826 else if ((li
->li_tv
.vval
.v_string
= (len
>= 0 ? vim_strnsave(str
, len
)
5827 : vim_strsave(str
))) == NULL
)
5833 * Append "n" to list "l".
5834 * Returns FAIL when out of memory.
5837 list_append_number(l
, n
)
5843 li
= listitem_alloc();
5846 li
->li_tv
.v_type
= VAR_NUMBER
;
5847 li
->li_tv
.v_lock
= 0;
5848 li
->li_tv
.vval
.v_number
= n
;
5854 * Insert typval_T "tv" in list "l" before "item".
5855 * If "item" is NULL append at the end.
5856 * Return FAIL when out of memory.
5859 list_insert_tv(l
, tv
, item
)
5864 listitem_T
*ni
= listitem_alloc();
5868 copy_tv(tv
, &ni
->li_tv
);
5870 /* Append new item at end of list. */
5874 /* Insert new item before existing item. */
5875 ni
->li_prev
= item
->li_prev
;
5877 if (item
->li_prev
== NULL
)
5884 item
->li_prev
->li_next
= ni
;
5885 l
->lv_idx_item
= NULL
;
5894 * Extend "l1" with "l2".
5895 * If "bef" is NULL append at the end, otherwise insert before this item.
5896 * Returns FAIL when out of memory.
5899 list_extend(l1
, l2
, bef
)
5906 for (item
= l2
->lv_first
; item
!= NULL
; item
= item
->li_next
)
5907 if (list_insert_tv(l1
, &item
->li_tv
, bef
) == FAIL
)
5913 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5914 * Return FAIL when out of memory.
5917 list_concat(l1
, l2
, tv
)
5924 /* make a copy of the first list. */
5925 l
= list_copy(l1
, FALSE
, 0);
5928 tv
->v_type
= VAR_LIST
;
5929 tv
->vval
.v_list
= l
;
5931 /* append all items from the second list */
5932 return list_extend(l
, l2
, NULL
);
5936 * Make a copy of list "orig". Shallow if "deep" is FALSE.
5937 * The refcount of the new list is set to 1.
5938 * See item_copy() for "copyID".
5939 * Returns NULL when out of memory.
5942 list_copy(orig
, deep
, copyID
)
5954 copy
= list_alloc();
5959 /* Do this before adding the items, because one of the items may
5960 * refer back to this list. */
5961 orig
->lv_copyID
= copyID
;
5962 orig
->lv_copylist
= copy
;
5964 for (item
= orig
->lv_first
; item
!= NULL
&& !got_int
;
5965 item
= item
->li_next
)
5967 ni
= listitem_alloc();
5972 if (item_copy(&item
->li_tv
, &ni
->li_tv
, deep
, copyID
) == FAIL
)
5979 copy_tv(&item
->li_tv
, &ni
->li_tv
);
5980 list_append(copy
, ni
);
5982 ++copy
->lv_refcount
;
5994 * Remove items "item" to "item2" from list "l".
5995 * Does not free the listitem or the value!
5998 list_remove(l
, item
, item2
)
6005 /* notify watchers */
6006 for (ip
= item
; ip
!= NULL
; ip
= ip
->li_next
)
6009 list_fix_watch(l
, ip
);
6014 if (item2
->li_next
== NULL
)
6015 l
->lv_last
= item
->li_prev
;
6017 item2
->li_next
->li_prev
= item
->li_prev
;
6018 if (item
->li_prev
== NULL
)
6019 l
->lv_first
= item2
->li_next
;
6021 item
->li_prev
->li_next
= item2
->li_next
;
6022 l
->lv_idx_item
= NULL
;
6026 * Return an allocated string with the string representation of a list.
6030 list2string(tv
, copyID
)
6036 if (tv
->vval
.v_list
== NULL
)
6038 ga_init2(&ga
, (int)sizeof(char), 80);
6039 ga_append(&ga
, '[');
6040 if (list_join(&ga
, tv
->vval
.v_list
, (char_u
*)", ", FALSE
, copyID
) == FAIL
)
6042 vim_free(ga
.ga_data
);
6045 ga_append(&ga
, ']');
6046 ga_append(&ga
, NUL
);
6047 return (char_u
*)ga
.ga_data
;
6051 * Join list "l" into a string in "*gap", using separator "sep".
6052 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
6053 * Return FAIL or OK.
6056 list_join(gap
, l
, sep
, echo
, copyID
)
6065 char_u numbuf
[NUMBUFLEN
];
6069 for (item
= l
->lv_first
; item
!= NULL
&& !got_int
; item
= item
->li_next
)
6074 ga_concat(gap
, sep
);
6077 s
= echo_string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6079 s
= tv2string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6090 * Garbage collection for lists and dictionaries.
6092 * We use reference counts to be able to free most items right away when they
6093 * are no longer used. But for composite items it's possible that it becomes
6094 * unused while the reference count is > 0: When there is a recursive
6095 * reference. Example:
6096 * :let l = [1, 2, 3]
6100 * Since this is quite unusual we handle this with garbage collection: every
6101 * once in a while find out which lists and dicts are not referenced from any
6104 * Here is a good reference text about garbage collection (refers to Python
6105 * but it applies to all reference-counting mechanisms):
6106 * http://python.ca/nas/python/gc/
6110 * Do garbage collection for lists and dicts.
6111 * Return TRUE if some memory was freed.
6118 int copyID
= ++current_copyID
;
6123 int did_free
= FALSE
;
6128 /* Only do this once. */
6129 want_garbage_collect
= FALSE
;
6130 may_garbage_collect
= FALSE
;
6131 garbage_collect_at_exit
= FALSE
;
6134 * 1. Go through all accessible variables and mark all lists and dicts
6137 /* script-local variables */
6138 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
6139 set_ref_in_ht(&SCRIPT_VARS(i
), copyID
);
6141 /* buffer-local variables */
6142 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
6143 set_ref_in_ht(&buf
->b_vars
.dv_hashtab
, copyID
);
6145 /* window-local variables */
6146 FOR_ALL_TAB_WINDOWS(tp
, wp
)
6147 set_ref_in_ht(&wp
->w_vars
.dv_hashtab
, copyID
);
6150 /* tabpage-local variables */
6151 for (tp
= first_tabpage
; tp
!= NULL
; tp
= tp
->tp_next
)
6152 set_ref_in_ht(&tp
->tp_vars
.dv_hashtab
, copyID
);
6155 /* global variables */
6156 set_ref_in_ht(&globvarht
, copyID
);
6158 /* function-local variables */
6159 for (fc
= current_funccal
; fc
!= NULL
; fc
= fc
->caller
)
6161 set_ref_in_ht(&fc
->l_vars
.dv_hashtab
, copyID
);
6162 set_ref_in_ht(&fc
->l_avars
.dv_hashtab
, copyID
);
6166 * 2. Go through the list of dicts and free items without the copyID.
6168 for (dd
= first_dict
; dd
!= NULL
; )
6169 if (dd
->dv_copyID
!= copyID
)
6171 /* Free the Dictionary and ordinary items it contains, but don't
6172 * recurse into Lists and Dictionaries, they will be in the list
6173 * of dicts or list of lists. */
6174 dict_free(dd
, FALSE
);
6177 /* restart, next dict may also have been freed */
6181 dd
= dd
->dv_used_next
;
6184 * 3. Go through the list of lists and free items without the copyID.
6185 * But don't free a list that has a watcher (used in a for loop), these
6186 * are not referenced anywhere.
6188 for (ll
= first_list
; ll
!= NULL
; )
6189 if (ll
->lv_copyID
!= copyID
&& ll
->lv_watch
== NULL
)
6191 /* Free the List and ordinary items it contains, but don't recurse
6192 * into Lists and Dictionaries, they will be in the list of dicts
6193 * or list of lists. */
6194 list_free(ll
, FALSE
);
6197 /* restart, next list may also have been freed */
6201 ll
= ll
->lv_used_next
;
6207 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6210 set_ref_in_ht(ht
, copyID
)
6217 todo
= (int)ht
->ht_used
;
6218 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
6219 if (!HASHITEM_EMPTY(hi
))
6222 set_ref_in_item(&HI2DI(hi
)->di_tv
, copyID
);
6227 * Mark all lists and dicts referenced through list "l" with "copyID".
6230 set_ref_in_list(l
, copyID
)
6236 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
6237 set_ref_in_item(&li
->li_tv
, copyID
);
6241 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6244 set_ref_in_item(tv
, copyID
)
6254 dd
= tv
->vval
.v_dict
;
6255 if (dd
->dv_copyID
!= copyID
)
6257 /* Didn't see this dict yet. */
6258 dd
->dv_copyID
= copyID
;
6259 set_ref_in_ht(&dd
->dv_hashtab
, copyID
);
6264 ll
= tv
->vval
.v_list
;
6265 if (ll
->lv_copyID
!= copyID
)
6267 /* Didn't see this list yet. */
6268 ll
->lv_copyID
= copyID
;
6269 set_ref_in_list(ll
, copyID
);
6277 * Allocate an empty header for a dictionary.
6284 d
= (dict_T
*)alloc(sizeof(dict_T
));
6287 /* Add the list to the list of dicts for garbage collection. */
6288 if (first_dict
!= NULL
)
6289 first_dict
->dv_used_prev
= d
;
6290 d
->dv_used_next
= first_dict
;
6291 d
->dv_used_prev
= NULL
;
6294 hash_init(&d
->dv_hashtab
);
6303 * Unreference a Dictionary: decrement the reference count and free it when it
6310 if (d
!= NULL
&& --d
->dv_refcount
<= 0)
6315 * Free a Dictionary, including all items it contains.
6316 * Ignores the reference count.
6319 dict_free(d
, recurse
)
6321 int recurse
; /* Free Lists and Dictionaries recursively. */
6327 /* Remove the dict from the list of dicts for garbage collection. */
6328 if (d
->dv_used_prev
== NULL
)
6329 first_dict
= d
->dv_used_next
;
6331 d
->dv_used_prev
->dv_used_next
= d
->dv_used_next
;
6332 if (d
->dv_used_next
!= NULL
)
6333 d
->dv_used_next
->dv_used_prev
= d
->dv_used_prev
;
6335 /* Lock the hashtab, we don't want it to resize while freeing items. */
6336 hash_lock(&d
->dv_hashtab
);
6337 todo
= (int)d
->dv_hashtab
.ht_used
;
6338 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
6340 if (!HASHITEM_EMPTY(hi
))
6342 /* Remove the item before deleting it, just in case there is
6343 * something recursive causing trouble. */
6345 hash_remove(&d
->dv_hashtab
, hi
);
6346 if (recurse
|| (di
->di_tv
.v_type
!= VAR_LIST
6347 && di
->di_tv
.v_type
!= VAR_DICT
))
6348 clear_tv(&di
->di_tv
);
6353 hash_clear(&d
->dv_hashtab
);
6358 * Allocate a Dictionary item.
6359 * The "key" is copied to the new item.
6360 * Note that the value of the item "di_tv" still needs to be initialized!
6361 * Returns NULL when out of memory.
6369 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
) + STRLEN(key
)));
6372 STRCPY(di
->di_key
, key
);
6379 * Make a copy of a Dictionary item.
6387 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
6388 + STRLEN(org
->di_key
)));
6391 STRCPY(di
->di_key
, org
->di_key
);
6393 copy_tv(&org
->di_tv
, &di
->di_tv
);
6399 * Remove item "item" from Dictionary "dict" and free it.
6402 dictitem_remove(dict
, item
)
6408 hi
= hash_find(&dict
->dv_hashtab
, item
->di_key
);
6409 if (HASHITEM_EMPTY(hi
))
6410 EMSG2(_(e_intern2
), "dictitem_remove()");
6412 hash_remove(&dict
->dv_hashtab
, hi
);
6413 dictitem_free(item
);
6417 * Free a dict item. Also clears the value.
6423 clear_tv(&item
->di_tv
);
6428 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6429 * The refcount of the new dict is set to 1.
6430 * See item_copy() for "copyID".
6431 * Returns NULL when out of memory.
6434 dict_copy(orig
, deep
, copyID
)
6447 copy
= dict_alloc();
6452 orig
->dv_copyID
= copyID
;
6453 orig
->dv_copydict
= copy
;
6455 todo
= (int)orig
->dv_hashtab
.ht_used
;
6456 for (hi
= orig
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
6458 if (!HASHITEM_EMPTY(hi
))
6462 di
= dictitem_alloc(hi
->hi_key
);
6467 if (item_copy(&HI2DI(hi
)->di_tv
, &di
->di_tv
, deep
,
6475 copy_tv(&HI2DI(hi
)->di_tv
, &di
->di_tv
);
6476 if (dict_add(copy
, di
) == FAIL
)
6484 ++copy
->dv_refcount
;
6496 * Add item "item" to Dictionary "d".
6497 * Returns FAIL when out of memory and when key already existed.
6504 return hash_add(&d
->dv_hashtab
, item
->di_key
);
6508 * Add a number or string entry to dictionary "d".
6509 * When "str" is NULL use number "nr", otherwise use "str".
6510 * Returns FAIL when out of memory and when key already exists.
6513 dict_add_nr_str(d
, key
, nr
, str
)
6521 item
= dictitem_alloc((char_u
*)key
);
6524 item
->di_tv
.v_lock
= 0;
6527 item
->di_tv
.v_type
= VAR_NUMBER
;
6528 item
->di_tv
.vval
.v_number
= nr
;
6532 item
->di_tv
.v_type
= VAR_STRING
;
6533 item
->di_tv
.vval
.v_string
= vim_strsave(str
);
6535 if (dict_add(d
, item
) == FAIL
)
6537 dictitem_free(item
);
6544 * Get the number of items in a Dictionary.
6552 return (long)d
->dv_hashtab
.ht_used
;
6556 * Find item "key[len]" in Dictionary "d".
6557 * If "len" is negative use strlen(key).
6558 * Returns NULL when not found.
6561 dict_find(d
, key
, len
)
6567 char_u buf
[AKEYLEN
];
6569 char_u
*tofree
= NULL
;
6574 else if (len
>= AKEYLEN
)
6576 tofree
= akey
= vim_strnsave(key
, len
);
6582 /* Avoid a malloc/free by using buf[]. */
6583 vim_strncpy(buf
, key
, len
);
6587 hi
= hash_find(&d
->dv_hashtab
, akey
);
6589 if (HASHITEM_EMPTY(hi
))
6595 * Get a string item from a dictionary.
6596 * When "save" is TRUE allocate memory for it.
6597 * Returns NULL if the entry doesn't exist or out of memory.
6600 get_dict_string(d
, key
, save
)
6608 di
= dict_find(d
, key
, -1);
6611 s
= get_tv_string(&di
->di_tv
);
6612 if (save
&& s
!= NULL
)
6618 * Get a number item from a dictionary.
6619 * Returns 0 if the entry doesn't exist or out of memory.
6622 get_dict_number(d
, key
)
6628 di
= dict_find(d
, key
, -1);
6631 return get_tv_number(&di
->di_tv
);
6635 * Return an allocated string with the string representation of a Dictionary.
6639 dict2string(tv
, copyID
)
6646 char_u numbuf
[NUMBUFLEN
];
6652 if ((d
= tv
->vval
.v_dict
) == NULL
)
6654 ga_init2(&ga
, (int)sizeof(char), 80);
6655 ga_append(&ga
, '{');
6657 todo
= (int)d
->dv_hashtab
.ht_used
;
6658 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
6660 if (!HASHITEM_EMPTY(hi
))
6667 ga_concat(&ga
, (char_u
*)", ");
6669 tofree
= string_quote(hi
->hi_key
, FALSE
);
6672 ga_concat(&ga
, tofree
);
6675 ga_concat(&ga
, (char_u
*)": ");
6676 s
= tv2string(&HI2DI(hi
)->di_tv
, &tofree
, numbuf
, copyID
);
6686 vim_free(ga
.ga_data
);
6690 ga_append(&ga
, '}');
6691 ga_append(&ga
, NUL
);
6692 return (char_u
*)ga
.ga_data
;
6696 * Allocate a variable for a Dictionary and fill it from "*arg".
6697 * Return OK or FAIL. Returns NOTDONE for {expr}.
6700 get_dict_tv(arg
, rettv
, evaluate
)
6710 char_u
*start
= skipwhite(*arg
+ 1);
6711 char_u buf
[NUMBUFLEN
];
6714 * First check if it's not a curly-braces thing: {expr}.
6715 * Must do this without evaluating, otherwise a function may be called
6716 * twice. Unfortunately this means we need to call eval1() twice for the
6718 * But {} is an empty Dictionary.
6722 if (eval1(&start
, &tv
, FALSE
) == FAIL
) /* recursive! */
6734 tvkey
.v_type
= VAR_UNKNOWN
;
6735 tv
.v_type
= VAR_UNKNOWN
;
6737 *arg
= skipwhite(*arg
+ 1);
6738 while (**arg
!= '}' && **arg
!= NUL
)
6740 if (eval1(arg
, &tvkey
, evaluate
) == FAIL
) /* recursive! */
6744 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg
);
6750 key
= get_tv_string_buf_chk(&tvkey
, buf
);
6751 if (key
== NULL
|| *key
== NUL
)
6753 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6755 EMSG(_(e_emptykey
));
6761 *arg
= skipwhite(*arg
+ 1);
6762 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
6770 item
= dict_find(d
, key
, -1);
6773 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key
);
6778 item
= dictitem_alloc(key
);
6783 item
->di_tv
.v_lock
= 0;
6784 if (dict_add(d
, item
) == FAIL
)
6785 dictitem_free(item
);
6793 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg
);
6796 *arg
= skipwhite(*arg
+ 1);
6801 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg
);
6808 *arg
= skipwhite(*arg
+ 1);
6811 rettv
->v_type
= VAR_DICT
;
6812 rettv
->vval
.v_dict
= d
;
6820 * Return a string with the string representation of a variable.
6821 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6822 * "numbuf" is used for a number.
6823 * Does not put quotes around strings, as ":echo" displays values.
6824 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6828 echo_string(tv
, tofree
, numbuf
, copyID
)
6834 static int recurse
= 0;
6837 if (recurse
>= DICT_MAXNEST
)
6839 EMSG(_("E724: variable nested too deep for displaying"));
6849 r
= tv
->vval
.v_string
;
6853 if (tv
->vval
.v_list
== NULL
)
6858 else if (copyID
!= 0 && tv
->vval
.v_list
->lv_copyID
== copyID
)
6861 r
= (char_u
*)"[...]";
6865 tv
->vval
.v_list
->lv_copyID
= copyID
;
6866 *tofree
= list2string(tv
, copyID
);
6872 if (tv
->vval
.v_dict
== NULL
)
6877 else if (copyID
!= 0 && tv
->vval
.v_dict
->dv_copyID
== copyID
)
6880 r
= (char_u
*)"{...}";
6884 tv
->vval
.v_dict
->dv_copyID
= copyID
;
6885 *tofree
= dict2string(tv
, copyID
);
6893 r
= get_tv_string_buf(tv
, numbuf
);
6897 EMSG2(_(e_intern2
), "echo_string()");
6906 * Return a string with the string representation of a variable.
6907 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6908 * "numbuf" is used for a number.
6909 * Puts quotes around strings, so that they can be parsed back by eval().
6913 tv2string(tv
, tofree
, numbuf
, copyID
)
6922 *tofree
= string_quote(tv
->vval
.v_string
, TRUE
);
6925 *tofree
= string_quote(tv
->vval
.v_string
, FALSE
);
6932 EMSG2(_(e_intern2
), "tv2string()");
6934 return echo_string(tv
, tofree
, numbuf
, copyID
);
6938 * Return string "str" in ' quotes, doubling ' characters.
6939 * If "str" is NULL an empty string is assumed.
6940 * If "function" is TRUE make it function('string').
6943 string_quote(str
, function
)
6950 len
= (function
? 13 : 3);
6953 len
+= (unsigned)STRLEN(str
);
6954 for (p
= str
; *p
!= NUL
; mb_ptr_adv(p
))
6963 STRCPY(r
, "function('");
6969 for (p
= str
; *p
!= NUL
; )
6984 * Get the value of an environment variable.
6985 * "arg" is pointing to the '$'. It is advanced to after the name.
6986 * If the environment variable was not set, silently assume it is empty.
6990 get_env_tv(arg
, rettv
, evaluate
)
6995 char_u
*string
= NULL
;
6999 int mustfree
= FALSE
;
7003 len
= get_env_len(arg
);
7010 /* first try vim_getenv(), fast for normal environment vars */
7011 string
= vim_getenv(name
, &mustfree
);
7012 if (string
!= NULL
&& *string
!= NUL
)
7015 string
= vim_strsave(string
);
7022 /* next try expanding things like $VIM and ${HOME} */
7023 string
= expand_env_save(name
- 1);
7024 if (string
!= NULL
&& *string
== '$')
7032 rettv
->v_type
= VAR_STRING
;
7033 rettv
->vval
.v_string
= string
;
7040 * Array with names and number of arguments of all internal functions
7041 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7045 char *f_name
; /* function name */
7046 char f_min_argc
; /* minimal number of arguments */
7047 char f_max_argc
; /* maximal number of arguments */
7048 void (*f_func
) __ARGS((typval_T
*args
, typval_T
*rvar
));
7049 /* implementation of function */
7052 {"add", 2, 2, f_add
},
7053 {"append", 2, 2, f_append
},
7054 {"argc", 0, 0, f_argc
},
7055 {"argidx", 0, 0, f_argidx
},
7056 {"argv", 0, 1, f_argv
},
7057 {"browse", 4, 4, f_browse
},
7058 {"browsedir", 2, 2, f_browsedir
},
7059 {"bufexists", 1, 1, f_bufexists
},
7060 {"buffer_exists", 1, 1, f_bufexists
}, /* obsolete */
7061 {"buffer_name", 1, 1, f_bufname
}, /* obsolete */
7062 {"buffer_number", 1, 1, f_bufnr
}, /* obsolete */
7063 {"buflisted", 1, 1, f_buflisted
},
7064 {"bufloaded", 1, 1, f_bufloaded
},
7065 {"bufname", 1, 1, f_bufname
},
7066 {"bufnr", 1, 2, f_bufnr
},
7067 {"bufwinnr", 1, 1, f_bufwinnr
},
7068 {"byte2line", 1, 1, f_byte2line
},
7069 {"byteidx", 2, 2, f_byteidx
},
7070 {"call", 2, 3, f_call
},
7071 {"changenr", 0, 0, f_changenr
},
7072 {"char2nr", 1, 1, f_char2nr
},
7073 {"cindent", 1, 1, f_cindent
},
7074 {"clearmatches", 0, 0, f_clearmatches
},
7075 {"col", 1, 1, f_col
},
7076 #if defined(FEAT_INS_EXPAND)
7077 {"complete", 2, 2, f_complete
},
7078 {"complete_add", 1, 1, f_complete_add
},
7079 {"complete_check", 0, 0, f_complete_check
},
7081 {"confirm", 1, 4, f_confirm
},
7082 {"copy", 1, 1, f_copy
},
7083 {"count", 2, 4, f_count
},
7084 {"cscope_connection",0,3, f_cscope_connection
},
7085 {"cursor", 1, 3, f_cursor
},
7086 {"deepcopy", 1, 2, f_deepcopy
},
7087 {"delete", 1, 1, f_delete
},
7088 {"did_filetype", 0, 0, f_did_filetype
},
7089 {"diff_filler", 1, 1, f_diff_filler
},
7090 {"diff_hlID", 2, 2, f_diff_hlID
},
7091 {"empty", 1, 1, f_empty
},
7092 {"escape", 2, 2, f_escape
},
7093 {"eval", 1, 1, f_eval
},
7094 {"eventhandler", 0, 0, f_eventhandler
},
7095 {"executable", 1, 1, f_executable
},
7096 {"exists", 1, 1, f_exists
},
7097 {"expand", 1, 2, f_expand
},
7098 {"extend", 2, 3, f_extend
},
7099 {"feedkeys", 1, 2, f_feedkeys
},
7100 {"file_readable", 1, 1, f_filereadable
}, /* obsolete */
7101 {"filereadable", 1, 1, f_filereadable
},
7102 {"filewritable", 1, 1, f_filewritable
},
7103 {"filter", 2, 2, f_filter
},
7104 {"finddir", 1, 3, f_finddir
},
7105 {"findfile", 1, 3, f_findfile
},
7106 {"fnamemodify", 2, 2, f_fnamemodify
},
7107 {"foldclosed", 1, 1, f_foldclosed
},
7108 {"foldclosedend", 1, 1, f_foldclosedend
},
7109 {"foldlevel", 1, 1, f_foldlevel
},
7110 {"foldtext", 0, 0, f_foldtext
},
7111 {"foldtextresult", 1, 1, f_foldtextresult
},
7112 {"foreground", 0, 0, f_foreground
},
7113 {"function", 1, 1, f_function
},
7114 {"garbagecollect", 0, 1, f_garbagecollect
},
7115 {"get", 2, 3, f_get
},
7116 {"getbufline", 2, 3, f_getbufline
},
7117 {"getbufvar", 2, 2, f_getbufvar
},
7118 {"getchar", 0, 1, f_getchar
},
7119 {"getcharmod", 0, 0, f_getcharmod
},
7120 {"getcmdline", 0, 0, f_getcmdline
},
7121 {"getcmdpos", 0, 0, f_getcmdpos
},
7122 {"getcmdtype", 0, 0, f_getcmdtype
},
7123 {"getcwd", 0, 0, f_getcwd
},
7124 {"getfontname", 0, 1, f_getfontname
},
7125 {"getfperm", 1, 1, f_getfperm
},
7126 {"getfsize", 1, 1, f_getfsize
},
7127 {"getftime", 1, 1, f_getftime
},
7128 {"getftype", 1, 1, f_getftype
},
7129 {"getline", 1, 2, f_getline
},
7130 {"getloclist", 1, 1, f_getqflist
},
7131 {"getmatches", 0, 0, f_getmatches
},
7132 {"getpos", 1, 1, f_getpos
},
7133 {"getqflist", 0, 0, f_getqflist
},
7134 {"getreg", 0, 2, f_getreg
},
7135 {"getregtype", 0, 1, f_getregtype
},
7136 {"gettabwinvar", 3, 3, f_gettabwinvar
},
7137 {"getwinposx", 0, 0, f_getwinposx
},
7138 {"getwinposy", 0, 0, f_getwinposy
},
7139 {"getwinvar", 2, 2, f_getwinvar
},
7140 {"glob", 1, 1, f_glob
},
7141 {"globpath", 2, 2, f_globpath
},
7142 {"has", 1, 1, f_has
},
7143 {"has_key", 2, 2, f_has_key
},
7144 {"haslocaldir", 0, 0, f_haslocaldir
},
7145 {"hasmapto", 1, 3, f_hasmapto
},
7146 {"highlightID", 1, 1, f_hlID
}, /* obsolete */
7147 {"highlight_exists",1, 1, f_hlexists
}, /* obsolete */
7148 {"histadd", 2, 2, f_histadd
},
7149 {"histdel", 1, 2, f_histdel
},
7150 {"histget", 1, 2, f_histget
},
7151 {"histnr", 1, 1, f_histnr
},
7152 {"hlID", 1, 1, f_hlID
},
7153 {"hlexists", 1, 1, f_hlexists
},
7154 {"hostname", 0, 0, f_hostname
},
7155 {"iconv", 3, 3, f_iconv
},
7156 {"indent", 1, 1, f_indent
},
7157 {"index", 2, 4, f_index
},
7158 {"input", 1, 3, f_input
},
7159 {"inputdialog", 1, 3, f_inputdialog
},
7160 {"inputlist", 1, 1, f_inputlist
},
7161 {"inputrestore", 0, 0, f_inputrestore
},
7162 {"inputsave", 0, 0, f_inputsave
},
7163 {"inputsecret", 1, 2, f_inputsecret
},
7164 {"insert", 2, 3, f_insert
},
7165 {"isdirectory", 1, 1, f_isdirectory
},
7166 {"islocked", 1, 1, f_islocked
},
7167 {"items", 1, 1, f_items
},
7168 {"join", 1, 2, f_join
},
7169 {"keys", 1, 1, f_keys
},
7170 {"last_buffer_nr", 0, 0, f_last_buffer_nr
},/* obsolete */
7171 {"len", 1, 1, f_len
},
7172 {"libcall", 3, 3, f_libcall
},
7173 {"libcallnr", 3, 3, f_libcallnr
},
7174 {"line", 1, 1, f_line
},
7175 {"line2byte", 1, 1, f_line2byte
},
7176 {"lispindent", 1, 1, f_lispindent
},
7177 {"localtime", 0, 0, f_localtime
},
7178 {"map", 2, 2, f_map
},
7179 {"maparg", 1, 3, f_maparg
},
7180 {"mapcheck", 1, 3, f_mapcheck
},
7181 {"match", 2, 4, f_match
},
7182 {"matchadd", 2, 4, f_matchadd
},
7183 {"matcharg", 1, 1, f_matcharg
},
7184 {"matchdelete", 1, 1, f_matchdelete
},
7185 {"matchend", 2, 4, f_matchend
},
7186 {"matchlist", 2, 4, f_matchlist
},
7187 {"matchstr", 2, 4, f_matchstr
},
7188 {"max", 1, 1, f_max
},
7189 {"min", 1, 1, f_min
},
7191 {"mkdir", 1, 3, f_mkdir
},
7193 {"mode", 0, 0, f_mode
},
7194 {"nextnonblank", 1, 1, f_nextnonblank
},
7195 {"nr2char", 1, 1, f_nr2char
},
7196 {"pathshorten", 1, 1, f_pathshorten
},
7197 {"prevnonblank", 1, 1, f_prevnonblank
},
7198 {"printf", 2, 19, f_printf
},
7199 {"pumvisible", 0, 0, f_pumvisible
},
7200 {"range", 1, 3, f_range
},
7201 {"readfile", 1, 3, f_readfile
},
7202 {"reltime", 0, 2, f_reltime
},
7203 {"reltimestr", 1, 1, f_reltimestr
},
7204 {"remote_expr", 2, 3, f_remote_expr
},
7205 {"remote_foreground", 1, 1, f_remote_foreground
},
7206 {"remote_peek", 1, 2, f_remote_peek
},
7207 {"remote_read", 1, 1, f_remote_read
},
7208 {"remote_send", 2, 3, f_remote_send
},
7209 {"remove", 2, 3, f_remove
},
7210 {"rename", 2, 2, f_rename
},
7211 {"repeat", 2, 2, f_repeat
},
7212 {"resolve", 1, 1, f_resolve
},
7213 {"reverse", 1, 1, f_reverse
},
7214 {"search", 1, 3, f_search
},
7215 {"searchdecl", 1, 3, f_searchdecl
},
7216 {"searchpair", 3, 6, f_searchpair
},
7217 {"searchpairpos", 3, 6, f_searchpairpos
},
7218 {"searchpos", 1, 3, f_searchpos
},
7219 {"server2client", 2, 2, f_server2client
},
7220 {"serverlist", 0, 0, f_serverlist
},
7221 {"setbufvar", 3, 3, f_setbufvar
},
7222 {"setcmdpos", 1, 1, f_setcmdpos
},
7223 {"setline", 2, 2, f_setline
},
7224 {"setloclist", 2, 3, f_setloclist
},
7225 {"setmatches", 1, 1, f_setmatches
},
7226 {"setpos", 2, 2, f_setpos
},
7227 {"setqflist", 1, 2, f_setqflist
},
7228 {"setreg", 2, 3, f_setreg
},
7229 {"settabwinvar", 4, 4, f_settabwinvar
},
7230 {"setwinvar", 3, 3, f_setwinvar
},
7231 {"shellescape", 1, 1, f_shellescape
},
7232 {"simplify", 1, 1, f_simplify
},
7233 {"sort", 1, 2, f_sort
},
7234 {"soundfold", 1, 1, f_soundfold
},
7235 {"spellbadword", 0, 1, f_spellbadword
},
7236 {"spellsuggest", 1, 3, f_spellsuggest
},
7237 {"split", 1, 3, f_split
},
7238 {"str2nr", 1, 2, f_str2nr
},
7239 #ifdef HAVE_STRFTIME
7240 {"strftime", 1, 2, f_strftime
},
7242 {"stridx", 2, 3, f_stridx
},
7243 {"string", 1, 1, f_string
},
7244 {"strlen", 1, 1, f_strlen
},
7245 {"strpart", 2, 3, f_strpart
},
7246 {"strridx", 2, 3, f_strridx
},
7247 {"strtrans", 1, 1, f_strtrans
},
7248 {"submatch", 1, 1, f_submatch
},
7249 {"substitute", 4, 4, f_substitute
},
7250 {"synID", 3, 3, f_synID
},
7251 {"synIDattr", 2, 3, f_synIDattr
},
7252 {"synIDtrans", 1, 1, f_synIDtrans
},
7253 {"system", 1, 2, f_system
},
7254 {"tabpagebuflist", 0, 1, f_tabpagebuflist
},
7255 {"tabpagenr", 0, 1, f_tabpagenr
},
7256 {"tabpagewinnr", 1, 2, f_tabpagewinnr
},
7257 {"tagfiles", 0, 0, f_tagfiles
},
7258 {"taglist", 1, 1, f_taglist
},
7259 {"tempname", 0, 0, f_tempname
},
7260 {"test", 1, 1, f_test
},
7261 {"tolower", 1, 1, f_tolower
},
7262 {"toupper", 1, 1, f_toupper
},
7264 {"type", 1, 1, f_type
},
7265 {"values", 1, 1, f_values
},
7266 {"virtcol", 1, 1, f_virtcol
},
7267 {"visualmode", 0, 1, f_visualmode
},
7268 {"winbufnr", 1, 1, f_winbufnr
},
7269 {"wincol", 0, 0, f_wincol
},
7270 {"winheight", 1, 1, f_winheight
},
7271 {"winline", 0, 0, f_winline
},
7272 {"winnr", 0, 1, f_winnr
},
7273 {"winrestcmd", 0, 0, f_winrestcmd
},
7274 {"winrestview", 1, 1, f_winrestview
},
7275 {"winsaveview", 0, 0, f_winsaveview
},
7276 {"winwidth", 1, 1, f_winwidth
},
7277 {"writefile", 2, 3, f_writefile
},
7280 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7283 * Function given to ExpandGeneric() to obtain the list of internal
7284 * or user defined function names.
7287 get_function_name(xp
, idx
)
7291 static int intidx
= -1;
7298 name
= get_user_func_name(xp
, idx
);
7302 if (++intidx
< (int)(sizeof(functions
) / sizeof(struct fst
)))
7304 STRCPY(IObuff
, functions
[intidx
].f_name
);
7305 STRCAT(IObuff
, "(");
7306 if (functions
[intidx
].f_max_argc
== 0)
7307 STRCAT(IObuff
, ")");
7315 * Function given to ExpandGeneric() to obtain the list of internal or
7316 * user defined variable or function names.
7320 get_expr_name(xp
, idx
)
7324 static int intidx
= -1;
7331 name
= get_function_name(xp
, idx
);
7335 return get_user_var_name(xp
, ++intidx
);
7338 #endif /* FEAT_CMDL_COMPL */
7341 * Find internal function in table above.
7342 * Return index, or -1 if not found
7345 find_internal_func(name
)
7346 char_u
*name
; /* name of the function */
7349 int last
= (int)(sizeof(functions
) / sizeof(struct fst
)) - 1;
7354 * Find the function name in the table. Binary search.
7356 while (first
<= last
)
7358 x
= first
+ ((unsigned)(last
- first
) >> 1);
7359 cmp
= STRCMP(name
, functions
[x
].f_name
);
7371 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7372 * name it contains, otherwise return "name".
7375 deref_func_name(name
, lenp
)
7384 v
= find_var(name
, NULL
);
7386 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
7388 if (v
->di_tv
.vval
.v_string
== NULL
)
7391 return (char_u
*)""; /* just in case */
7393 *lenp
= (int)STRLEN(v
->di_tv
.vval
.v_string
);
7394 return v
->di_tv
.vval
.v_string
;
7401 * Allocate a variable for the result of a function.
7402 * Return OK or FAIL.
7405 get_func_tv(name
, len
, rettv
, arg
, firstline
, lastline
, doesrange
,
7407 char_u
*name
; /* name of the function */
7408 int len
; /* length of "name" */
7410 char_u
**arg
; /* argument, pointing to the '(' */
7411 linenr_T firstline
; /* first line of range */
7412 linenr_T lastline
; /* last line of range */
7413 int *doesrange
; /* return: function handled range */
7415 dict_T
*selfdict
; /* Dictionary for "self" */
7419 typval_T argvars
[MAX_FUNC_ARGS
+ 1]; /* vars for arguments */
7420 int argcount
= 0; /* number of arguments found */
7423 * Get the arguments.
7426 while (argcount
< MAX_FUNC_ARGS
)
7428 argp
= skipwhite(argp
+ 1); /* skip the '(' or ',' */
7429 if (*argp
== ')' || *argp
== ',' || *argp
== NUL
)
7431 if (eval1(&argp
, &argvars
[argcount
], evaluate
) == FAIL
)
7446 ret
= call_func(name
, len
, rettv
, argcount
, argvars
,
7447 firstline
, lastline
, doesrange
, evaluate
, selfdict
);
7448 else if (!aborting())
7450 if (argcount
== MAX_FUNC_ARGS
)
7451 emsg_funcname("E740: Too many arguments for function %s", name
);
7453 emsg_funcname("E116: Invalid arguments for function %s", name
);
7456 while (--argcount
>= 0)
7457 clear_tv(&argvars
[argcount
]);
7459 *arg
= skipwhite(argp
);
7465 * Call a function with its resolved parameters
7466 * Return OK when the function can't be called, FAIL otherwise.
7467 * Also returns OK when an error was encountered while executing the function.
7470 call_func(name
, len
, rettv
, argcount
, argvars
, firstline
, lastline
,
7471 doesrange
, evaluate
, selfdict
)
7472 char_u
*name
; /* name of the function */
7473 int len
; /* length of "name" */
7474 typval_T
*rettv
; /* return value goes here */
7475 int argcount
; /* number of "argvars" */
7476 typval_T
*argvars
; /* vars for arguments, must have "argcount"
7477 PLUS ONE elements! */
7478 linenr_T firstline
; /* first line of range */
7479 linenr_T lastline
; /* last line of range */
7480 int *doesrange
; /* return: function handled range */
7482 dict_T
*selfdict
; /* Dictionary for "self" */
7485 #define ERROR_UNKNOWN 0
7486 #define ERROR_TOOMANY 1
7487 #define ERROR_TOOFEW 2
7488 #define ERROR_SCRIPT 3
7489 #define ERROR_DICT 4
7490 #define ERROR_NONE 5
7491 #define ERROR_OTHER 6
7492 int error
= ERROR_NONE
;
7497 #define FLEN_FIXED 40
7498 char_u fname_buf
[FLEN_FIXED
+ 1];
7502 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7503 * Change <SNR>123_name() to K_SNR 123_name().
7504 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7508 llen
= eval_fname_script(name
);
7511 fname_buf
[0] = K_SPECIAL
;
7512 fname_buf
[1] = KS_EXTRA
;
7513 fname_buf
[2] = (int)KE_SNR
;
7515 if (eval_fname_sid(name
)) /* "<SID>" or "s:" */
7517 if (current_SID
<= 0)
7518 error
= ERROR_SCRIPT
;
7521 sprintf((char *)fname_buf
+ 3, "%ld_", (long)current_SID
);
7522 i
= (int)STRLEN(fname_buf
);
7525 if (i
+ STRLEN(name
+ llen
) < FLEN_FIXED
)
7527 STRCPY(fname_buf
+ i
, name
+ llen
);
7532 fname
= alloc((unsigned)(i
+ STRLEN(name
+ llen
) + 1));
7534 error
= ERROR_OTHER
;
7537 mch_memmove(fname
, fname_buf
, (size_t)i
);
7538 STRCPY(fname
+ i
, name
+ llen
);
7548 /* execute the function if no errors detected and executing */
7549 if (evaluate
&& error
== ERROR_NONE
)
7551 rettv
->v_type
= VAR_NUMBER
; /* default is number rettv */
7552 error
= ERROR_UNKNOWN
;
7554 if (!builtin_function(fname
))
7557 * User defined function.
7559 fp
= find_func(fname
);
7562 /* Trigger FuncUndefined event, may load the function. */
7564 && apply_autocmds(EVENT_FUNCUNDEFINED
,
7565 fname
, fname
, TRUE
, NULL
)
7568 /* executed an autocommand, search for the function again */
7569 fp
= find_func(fname
);
7572 /* Try loading a package. */
7573 if (fp
== NULL
&& script_autoload(fname
, TRUE
) && !aborting())
7575 /* loaded a package, search for the function again */
7576 fp
= find_func(fname
);
7581 if (fp
->uf_flags
& FC_RANGE
)
7583 if (argcount
< fp
->uf_args
.ga_len
)
7584 error
= ERROR_TOOFEW
;
7585 else if (!fp
->uf_varargs
&& argcount
> fp
->uf_args
.ga_len
)
7586 error
= ERROR_TOOMANY
;
7587 else if ((fp
->uf_flags
& FC_DICT
) && selfdict
== NULL
)
7592 * Call the user function.
7593 * Save and restore search patterns, script variables and
7596 save_search_patterns();
7599 call_user_func(fp
, argcount
, argvars
, rettv
,
7600 firstline
, lastline
,
7601 (fp
->uf_flags
& FC_DICT
) ? selfdict
: NULL
);
7602 if (--fp
->uf_calls
<= 0 && isdigit(*fp
->uf_name
)
7603 && fp
->uf_refcount
<= 0)
7604 /* Function was unreferenced while being used, free it
7608 restore_search_patterns();
7616 * Find the function name in the table, call its implementation.
7618 i
= find_internal_func(fname
);
7621 if (argcount
< functions
[i
].f_min_argc
)
7622 error
= ERROR_TOOFEW
;
7623 else if (argcount
> functions
[i
].f_max_argc
)
7624 error
= ERROR_TOOMANY
;
7627 argvars
[argcount
].v_type
= VAR_UNKNOWN
;
7628 functions
[i
].f_func(argvars
, rettv
);
7634 * The function call (or "FuncUndefined" autocommand sequence) might
7635 * have been aborted by an error, an interrupt, or an explicitly thrown
7636 * exception that has not been caught so far. This situation can be
7637 * tested for by calling aborting(). For an error in an internal
7638 * function or for the "E132" error in call_user_func(), however, the
7639 * throw point at which the "force_abort" flag (temporarily reset by
7640 * emsg()) is normally updated has not been reached yet. We need to
7641 * update that flag first to make aborting() reliable.
7643 update_force_abort();
7645 if (error
== ERROR_NONE
)
7649 * Report an error unless the argument evaluation or function call has been
7650 * cancelled due to an aborting error, an interrupt, or an exception.
7657 emsg_funcname(N_("E117: Unknown function: %s"), name
);
7660 emsg_funcname(e_toomanyarg
, name
);
7663 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
7667 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
7671 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
7678 if (fname
!= name
&& fname
!= fname_buf
)
7685 * Give an error message with a function name. Handle <SNR> things.
7688 emsg_funcname(ermsg
, name
)
7694 if (*name
== K_SPECIAL
)
7695 p
= concat_str((char_u
*)"<SNR>", name
+ 3);
7703 /*********************************************
7704 * Implementation of the built-in functions
7708 * "add(list, item)" function
7711 f_add(argvars
, rettv
)
7717 rettv
->vval
.v_number
= 1; /* Default: Failed */
7718 if (argvars
[0].v_type
== VAR_LIST
)
7720 if ((l
= argvars
[0].vval
.v_list
) != NULL
7721 && !tv_check_lock(l
->lv_lock
, (char_u
*)"add()")
7722 && list_append_tv(l
, &argvars
[1]) == OK
)
7723 copy_tv(&argvars
[0], rettv
);
7730 * "append(lnum, string/list)" function
7733 f_append(argvars
, rettv
)
7740 listitem_T
*li
= NULL
;
7744 lnum
= get_tv_lnum(argvars
);
7746 && lnum
<= curbuf
->b_ml
.ml_line_count
7747 && u_save(lnum
, lnum
+ 1) == OK
)
7749 if (argvars
[1].v_type
== VAR_LIST
)
7751 l
= argvars
[1].vval
.v_list
;
7756 rettv
->vval
.v_number
= 0; /* Default: Success */
7760 tv
= &argvars
[1]; /* append a string */
7761 else if (li
== NULL
)
7762 break; /* end of list */
7764 tv
= &li
->li_tv
; /* append item from list */
7765 line
= get_tv_string_chk(tv
);
7766 if (line
== NULL
) /* type error */
7768 rettv
->vval
.v_number
= 1; /* Failed */
7771 ml_append(lnum
+ added
, line
, (colnr_T
)0, FALSE
);
7778 appended_lines_mark(lnum
, added
);
7779 if (curwin
->w_cursor
.lnum
> lnum
)
7780 curwin
->w_cursor
.lnum
+= added
;
7783 rettv
->vval
.v_number
= 1; /* Failed */
7791 f_argc(argvars
, rettv
)
7795 rettv
->vval
.v_number
= ARGCOUNT
;
7799 * "argidx()" function
7803 f_argidx(argvars
, rettv
)
7807 rettv
->vval
.v_number
= curwin
->w_arg_idx
;
7811 * "argv(nr)" function
7814 f_argv(argvars
, rettv
)
7820 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
7822 idx
= get_tv_number_chk(&argvars
[0], NULL
);
7823 if (idx
>= 0 && idx
< ARGCOUNT
)
7824 rettv
->vval
.v_string
= vim_strsave(alist_name(&ARGLIST
[idx
]));
7826 rettv
->vval
.v_string
= NULL
;
7827 rettv
->v_type
= VAR_STRING
;
7829 else if (rettv_list_alloc(rettv
) == OK
)
7830 for (idx
= 0; idx
< ARGCOUNT
; ++idx
)
7831 list_append_string(rettv
->vval
.v_list
,
7832 alist_name(&ARGLIST
[idx
]), -1);
7836 * "browse(save, title, initdir, default)" function
7840 f_browse(argvars
, rettv
)
7849 char_u buf
[NUMBUFLEN
];
7850 char_u buf2
[NUMBUFLEN
];
7853 save
= get_tv_number_chk(&argvars
[0], &error
);
7854 title
= get_tv_string_chk(&argvars
[1]);
7855 initdir
= get_tv_string_buf_chk(&argvars
[2], buf
);
7856 defname
= get_tv_string_buf_chk(&argvars
[3], buf2
);
7858 if (error
|| title
== NULL
|| initdir
== NULL
|| defname
== NULL
)
7859 rettv
->vval
.v_string
= NULL
;
7861 rettv
->vval
.v_string
=
7862 do_browse(save
? BROWSE_SAVE
: 0,
7863 title
, defname
, NULL
, initdir
, NULL
, curbuf
);
7865 rettv
->vval
.v_string
= NULL
;
7867 rettv
->v_type
= VAR_STRING
;
7871 * "browsedir(title, initdir)" function
7875 f_browsedir(argvars
, rettv
)
7882 char_u buf
[NUMBUFLEN
];
7884 title
= get_tv_string_chk(&argvars
[0]);
7885 initdir
= get_tv_string_buf_chk(&argvars
[1], buf
);
7887 if (title
== NULL
|| initdir
== NULL
)
7888 rettv
->vval
.v_string
= NULL
;
7890 rettv
->vval
.v_string
= do_browse(BROWSE_DIR
,
7891 title
, NULL
, NULL
, initdir
, NULL
, curbuf
);
7893 rettv
->vval
.v_string
= NULL
;
7895 rettv
->v_type
= VAR_STRING
;
7898 static buf_T
*find_buffer
__ARGS((typval_T
*avar
));
7901 * Find a buffer by number or exact name.
7909 if (avar
->v_type
== VAR_NUMBER
)
7910 buf
= buflist_findnr((int)avar
->vval
.v_number
);
7911 else if (avar
->v_type
== VAR_STRING
&& avar
->vval
.v_string
!= NULL
)
7913 buf
= buflist_findname_exp(avar
->vval
.v_string
);
7916 /* No full path name match, try a match with a URL or a "nofile"
7917 * buffer, these don't use the full path. */
7918 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
7919 if (buf
->b_fname
!= NULL
7920 && (path_with_url(buf
->b_fname
)
7921 #ifdef FEAT_QUICKFIX
7925 && STRCMP(buf
->b_fname
, avar
->vval
.v_string
) == 0)
7933 * "bufexists(expr)" function
7936 f_bufexists(argvars
, rettv
)
7940 rettv
->vval
.v_number
= (find_buffer(&argvars
[0]) != NULL
);
7944 * "buflisted(expr)" function
7947 f_buflisted(argvars
, rettv
)
7953 buf
= find_buffer(&argvars
[0]);
7954 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_p_bl
);
7958 * "bufloaded(expr)" function
7961 f_bufloaded(argvars
, rettv
)
7967 buf
= find_buffer(&argvars
[0]);
7968 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_ml
.ml_mfp
!= NULL
);
7971 static buf_T
*get_buf_tv
__ARGS((typval_T
*tv
));
7974 * Get buffer by number or pattern.
7980 char_u
*name
= tv
->vval
.v_string
;
7985 if (tv
->v_type
== VAR_NUMBER
)
7986 return buflist_findnr((int)tv
->vval
.v_number
);
7987 if (tv
->v_type
!= VAR_STRING
)
7989 if (name
== NULL
|| *name
== NUL
)
7991 if (name
[0] == '$' && name
[1] == NUL
)
7994 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7995 save_magic
= p_magic
;
7998 p_cpo
= (char_u
*)"";
8000 buf
= buflist_findnr(buflist_findpat(name
, name
+ STRLEN(name
),
8003 p_magic
= save_magic
;
8006 /* If not found, try expanding the name, like done for bufexists(). */
8008 buf
= find_buffer(tv
);
8014 * "bufname(expr)" function
8017 f_bufname(argvars
, rettv
)
8023 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8025 buf
= get_buf_tv(&argvars
[0]);
8026 rettv
->v_type
= VAR_STRING
;
8027 if (buf
!= NULL
&& buf
->b_fname
!= NULL
)
8028 rettv
->vval
.v_string
= vim_strsave(buf
->b_fname
);
8030 rettv
->vval
.v_string
= NULL
;
8035 * "bufnr(expr)" function
8038 f_bufnr(argvars
, rettv
)
8046 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8048 buf
= get_buf_tv(&argvars
[0]);
8051 /* If the buffer isn't found and the second argument is not zero create a
8054 && argvars
[1].v_type
!= VAR_UNKNOWN
8055 && get_tv_number_chk(&argvars
[1], &error
) != 0
8057 && (name
= get_tv_string_chk(&argvars
[0])) != NULL
8059 buf
= buflist_new(name
, NULL
, (linenr_T
)1, 0);
8062 rettv
->vval
.v_number
= buf
->b_fnum
;
8064 rettv
->vval
.v_number
= -1;
8068 * "bufwinnr(nr)" function
8071 f_bufwinnr(argvars
, rettv
)
8081 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8083 buf
= get_buf_tv(&argvars
[0]);
8085 for (wp
= firstwin
; wp
; wp
= wp
->w_next
)
8088 if (wp
->w_buffer
== buf
)
8091 rettv
->vval
.v_number
= (wp
!= NULL
? winnr
: -1);
8093 rettv
->vval
.v_number
= (curwin
->w_buffer
== buf
? 1 : -1);
8099 * "byte2line(byte)" function
8103 f_byte2line(argvars
, rettv
)
8107 #ifndef FEAT_BYTEOFF
8108 rettv
->vval
.v_number
= -1;
8112 boff
= get_tv_number(&argvars
[0]) - 1; /* boff gets -1 on type error */
8114 rettv
->vval
.v_number
= -1;
8116 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
,
8117 (linenr_T
)0, &boff
);
8122 * "byteidx()" function
8126 f_byteidx(argvars
, rettv
)
8136 str
= get_tv_string_chk(&argvars
[0]);
8137 idx
= get_tv_number_chk(&argvars
[1], NULL
);
8138 rettv
->vval
.v_number
= -1;
8139 if (str
== NULL
|| idx
< 0)
8144 for ( ; idx
> 0; idx
--)
8146 if (*t
== NUL
) /* EOL reached */
8148 t
+= (*mb_ptr2len
)(t
);
8150 rettv
->vval
.v_number
= (varnumber_T
)(t
- str
);
8152 if (idx
<= STRLEN(str
))
8153 rettv
->vval
.v_number
= idx
;
8158 * "call(func, arglist)" function
8161 f_call(argvars
, rettv
)
8166 typval_T argv
[MAX_FUNC_ARGS
+ 1];
8170 dict_T
*selfdict
= NULL
;
8172 rettv
->vval
.v_number
= 0;
8173 if (argvars
[1].v_type
!= VAR_LIST
)
8178 if (argvars
[1].vval
.v_list
== NULL
)
8181 if (argvars
[0].v_type
== VAR_FUNC
)
8182 func
= argvars
[0].vval
.v_string
;
8184 func
= get_tv_string(&argvars
[0]);
8186 return; /* type error or empty name */
8188 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8190 if (argvars
[2].v_type
!= VAR_DICT
)
8195 selfdict
= argvars
[2].vval
.v_dict
;
8198 for (item
= argvars
[1].vval
.v_list
->lv_first
; item
!= NULL
;
8199 item
= item
->li_next
)
8201 if (argc
== MAX_FUNC_ARGS
)
8203 EMSG(_("E699: Too many arguments"));
8206 /* Make a copy of each argument. This is needed to be able to set
8207 * v_lock to VAR_FIXED in the copy without changing the original list.
8209 copy_tv(&item
->li_tv
, &argv
[argc
++]);
8213 (void)call_func(func
, (int)STRLEN(func
), rettv
, argc
, argv
,
8214 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
8215 &dummy
, TRUE
, selfdict
);
8217 /* Free the arguments. */
8219 clear_tv(&argv
[--argc
]);
8223 * "changenr()" function
8227 f_changenr(argvars
, rettv
)
8231 rettv
->vval
.v_number
= curbuf
->b_u_seq_cur
;
8235 * "char2nr(string)" function
8238 f_char2nr(argvars
, rettv
)
8244 rettv
->vval
.v_number
= (*mb_ptr2char
)(get_tv_string(&argvars
[0]));
8247 rettv
->vval
.v_number
= get_tv_string(&argvars
[0])[0];
8251 * "cindent(lnum)" function
8254 f_cindent(argvars
, rettv
)
8262 pos
= curwin
->w_cursor
;
8263 lnum
= get_tv_lnum(argvars
);
8264 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
8266 curwin
->w_cursor
.lnum
= lnum
;
8267 rettv
->vval
.v_number
= get_c_indent();
8268 curwin
->w_cursor
= pos
;
8272 rettv
->vval
.v_number
= -1;
8276 * "clearmatches()" function
8280 f_clearmatches(argvars
, rettv
)
8284 #ifdef FEAT_SEARCH_EXTRA
8285 clear_matches(curwin
);
8290 * "col(string)" function
8293 f_col(argvars
, rettv
)
8299 int fnum
= curbuf
->b_fnum
;
8301 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
8302 if (fp
!= NULL
&& fnum
== curbuf
->b_fnum
)
8304 if (fp
->col
== MAXCOL
)
8306 /* '> can be MAXCOL, get the length of the line then */
8307 if (fp
->lnum
<= curbuf
->b_ml
.ml_line_count
)
8308 col
= (colnr_T
)STRLEN(ml_get(fp
->lnum
)) + 1;
8315 #ifdef FEAT_VIRTUALEDIT
8316 /* col(".") when the cursor is on the NUL at the end of the line
8317 * because of "coladd" can be seen as an extra column. */
8318 if (virtual_active() && fp
== &curwin
->w_cursor
)
8320 char_u
*p
= ml_get_cursor();
8322 if (curwin
->w_cursor
.coladd
>= (colnr_T
)chartabsize(p
,
8323 curwin
->w_virtcol
- curwin
->w_cursor
.coladd
))
8328 if (*p
!= NUL
&& p
[(l
= (*mb_ptr2len
)(p
))] == NUL
)
8331 if (*p
!= NUL
&& p
[1] == NUL
)
8339 rettv
->vval
.v_number
= col
;
8342 #if defined(FEAT_INS_EXPAND)
8344 * "complete()" function
8348 f_complete(argvars
, rettv
)
8354 if ((State
& INSERT
) == 0)
8356 EMSG(_("E785: complete() can only be used in Insert mode"));
8360 /* Check for undo allowed here, because if something was already inserted
8361 * the line was already saved for undo and this check isn't done. */
8362 if (!undo_allowed())
8365 if (argvars
[1].v_type
!= VAR_LIST
|| argvars
[1].vval
.v_list
== NULL
)
8371 startcol
= get_tv_number_chk(&argvars
[0], NULL
);
8375 set_completion(startcol
- 1, argvars
[1].vval
.v_list
);
8379 * "complete_add()" function
8383 f_complete_add(argvars
, rettv
)
8387 rettv
->vval
.v_number
= ins_compl_add_tv(&argvars
[0], 0);
8391 * "complete_check()" function
8395 f_complete_check(argvars
, rettv
)
8399 int saved
= RedrawingDisabled
;
8401 RedrawingDisabled
= 0;
8402 ins_compl_check_keys(0);
8403 rettv
->vval
.v_number
= compl_interrupted
;
8404 RedrawingDisabled
= saved
;
8409 * "confirm(message, buttons[, default [, type]])" function
8413 f_confirm(argvars
, rettv
)
8417 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8419 char_u
*buttons
= NULL
;
8420 char_u buf
[NUMBUFLEN
];
8421 char_u buf2
[NUMBUFLEN
];
8423 int type
= VIM_GENERIC
;
8427 message
= get_tv_string_chk(&argvars
[0]);
8428 if (message
== NULL
)
8430 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
8432 buttons
= get_tv_string_buf_chk(&argvars
[1], buf
);
8433 if (buttons
== NULL
)
8435 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8437 def
= get_tv_number_chk(&argvars
[2], &error
);
8438 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
8440 typestr
= get_tv_string_buf_chk(&argvars
[3], buf2
);
8441 if (typestr
== NULL
)
8445 switch (TOUPPER_ASC(*typestr
))
8447 case 'E': type
= VIM_ERROR
; break;
8448 case 'Q': type
= VIM_QUESTION
; break;
8449 case 'I': type
= VIM_INFO
; break;
8450 case 'W': type
= VIM_WARNING
; break;
8451 case 'G': type
= VIM_GENERIC
; break;
8458 if (buttons
== NULL
|| *buttons
== NUL
)
8459 buttons
= (char_u
*)_("&Ok");
8462 rettv
->vval
.v_number
= 0;
8464 rettv
->vval
.v_number
= do_dialog(type
, NULL
, message
, buttons
,
8467 rettv
->vval
.v_number
= 0;
8475 f_copy(argvars
, rettv
)
8479 item_copy(&argvars
[0], rettv
, FALSE
, 0);
8483 * "count()" function
8486 f_count(argvars
, rettv
)
8493 if (argvars
[0].v_type
== VAR_LIST
)
8499 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
8502 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8506 ic
= get_tv_number_chk(&argvars
[2], &error
);
8507 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
8509 idx
= get_tv_number_chk(&argvars
[3], &error
);
8512 li
= list_find(l
, idx
);
8514 EMSGN(_(e_listidx
), idx
);
8521 for ( ; li
!= NULL
; li
= li
->li_next
)
8522 if (tv_equal(&li
->li_tv
, &argvars
[1], ic
))
8526 else if (argvars
[0].v_type
== VAR_DICT
)
8532 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
8536 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8538 ic
= get_tv_number_chk(&argvars
[2], &error
);
8539 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
8543 todo
= error
? 0 : (int)d
->dv_hashtab
.ht_used
;
8544 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
8546 if (!HASHITEM_EMPTY(hi
))
8549 if (tv_equal(&HI2DI(hi
)->di_tv
, &argvars
[1], ic
))
8556 EMSG2(_(e_listdictarg
), "count()");
8557 rettv
->vval
.v_number
= n
;
8561 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8563 * Checks the existence of a cscope connection.
8567 f_cscope_connection(argvars
, rettv
)
8573 char_u
*dbpath
= NULL
;
8574 char_u
*prepend
= NULL
;
8575 char_u buf
[NUMBUFLEN
];
8577 if (argvars
[0].v_type
!= VAR_UNKNOWN
8578 && argvars
[1].v_type
!= VAR_UNKNOWN
)
8580 num
= (int)get_tv_number(&argvars
[0]);
8581 dbpath
= get_tv_string(&argvars
[1]);
8582 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8583 prepend
= get_tv_string_buf(&argvars
[2], buf
);
8586 rettv
->vval
.v_number
= cs_connection(num
, dbpath
, prepend
);
8588 rettv
->vval
.v_number
= 0;
8593 * "cursor(lnum, col)" function
8595 * Moves the cursor to the specified line and column
8599 f_cursor(argvars
, rettv
)
8604 #ifdef FEAT_VIRTUALEDIT
8608 if (argvars
[1].v_type
== VAR_UNKNOWN
)
8612 if (list2fpos(argvars
, &pos
, NULL
) == FAIL
)
8616 #ifdef FEAT_VIRTUALEDIT
8617 coladd
= pos
.coladd
;
8622 line
= get_tv_lnum(argvars
);
8623 col
= get_tv_number_chk(&argvars
[1], NULL
);
8624 #ifdef FEAT_VIRTUALEDIT
8625 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8626 coladd
= get_tv_number_chk(&argvars
[2], NULL
);
8629 if (line
< 0 || col
< 0
8630 #ifdef FEAT_VIRTUALEDIT
8634 return; /* type error; errmsg already given */
8636 curwin
->w_cursor
.lnum
= line
;
8638 curwin
->w_cursor
.col
= col
- 1;
8639 #ifdef FEAT_VIRTUALEDIT
8640 curwin
->w_cursor
.coladd
= coladd
;
8643 /* Make sure the cursor is in a valid position. */
8646 /* Correct cursor for multi-byte character. */
8651 curwin
->w_set_curswant
= TRUE
;
8655 * "deepcopy()" function
8658 f_deepcopy(argvars
, rettv
)
8664 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
8665 noref
= get_tv_number_chk(&argvars
[1], NULL
);
8666 if (noref
< 0 || noref
> 1)
8669 item_copy(&argvars
[0], rettv
, TRUE
, noref
== 0 ? ++current_copyID
: 0);
8673 * "delete()" function
8676 f_delete(argvars
, rettv
)
8680 if (check_restricted() || check_secure())
8681 rettv
->vval
.v_number
= -1;
8683 rettv
->vval
.v_number
= mch_remove(get_tv_string(&argvars
[0]));
8687 * "did_filetype()" function
8691 f_did_filetype(argvars
, rettv
)
8696 rettv
->vval
.v_number
= did_filetype
;
8698 rettv
->vval
.v_number
= 0;
8703 * "diff_filler()" function
8707 f_diff_filler(argvars
, rettv
)
8712 rettv
->vval
.v_number
= diff_check_fill(curwin
, get_tv_lnum(argvars
));
8717 * "diff_hlID()" function
8721 f_diff_hlID(argvars
, rettv
)
8726 linenr_T lnum
= get_tv_lnum(argvars
);
8727 static linenr_T prev_lnum
= 0;
8728 static int changedtick
= 0;
8729 static int fnum
= 0;
8730 static int change_start
= 0;
8731 static int change_end
= 0;
8732 static hlf_T hlID
= 0;
8736 if (lnum
< 0) /* ignore type error in {lnum} arg */
8738 if (lnum
!= prev_lnum
8739 || changedtick
!= curbuf
->b_changedtick
8740 || fnum
!= curbuf
->b_fnum
)
8742 /* New line, buffer, change: need to get the values. */
8743 filler_lines
= diff_check(curwin
, lnum
);
8744 if (filler_lines
< 0)
8746 if (filler_lines
== -1)
8748 change_start
= MAXCOL
;
8750 if (diff_find_change(curwin
, lnum
, &change_start
, &change_end
))
8751 hlID
= HLF_ADD
; /* added line */
8753 hlID
= HLF_CHD
; /* changed line */
8756 hlID
= HLF_ADD
; /* added line */
8761 changedtick
= curbuf
->b_changedtick
;
8762 fnum
= curbuf
->b_fnum
;
8765 if (hlID
== HLF_CHD
|| hlID
== HLF_TXD
)
8767 col
= get_tv_number(&argvars
[1]) - 1; /* ignore type error in {col} */
8768 if (col
>= change_start
&& col
<= change_end
)
8769 hlID
= HLF_TXD
; /* changed text */
8771 hlID
= HLF_CHD
; /* changed line */
8773 rettv
->vval
.v_number
= hlID
== (hlf_T
)0 ? 0 : (int)hlID
;
8778 * "empty({expr})" function
8781 f_empty(argvars
, rettv
)
8787 switch (argvars
[0].v_type
)
8791 n
= argvars
[0].vval
.v_string
== NULL
8792 || *argvars
[0].vval
.v_string
== NUL
;
8795 n
= argvars
[0].vval
.v_number
== 0;
8798 n
= argvars
[0].vval
.v_list
== NULL
8799 || argvars
[0].vval
.v_list
->lv_first
== NULL
;
8802 n
= argvars
[0].vval
.v_dict
== NULL
8803 || argvars
[0].vval
.v_dict
->dv_hashtab
.ht_used
== 0;
8806 EMSG2(_(e_intern2
), "f_empty()");
8810 rettv
->vval
.v_number
= n
;
8814 * "escape({string}, {chars})" function
8817 f_escape(argvars
, rettv
)
8821 char_u buf
[NUMBUFLEN
];
8823 rettv
->vval
.v_string
= vim_strsave_escaped(get_tv_string(&argvars
[0]),
8824 get_tv_string_buf(&argvars
[1], buf
));
8825 rettv
->v_type
= VAR_STRING
;
8833 f_eval(argvars
, rettv
)
8839 s
= get_tv_string_chk(&argvars
[0]);
8843 if (s
== NULL
|| eval1(&s
, rettv
, TRUE
) == FAIL
)
8845 rettv
->v_type
= VAR_NUMBER
;
8846 rettv
->vval
.v_number
= 0;
8849 EMSG(_(e_trailing
));
8853 * "eventhandler()" function
8857 f_eventhandler(argvars
, rettv
)
8861 rettv
->vval
.v_number
= vgetc_busy
;
8865 * "executable()" function
8868 f_executable(argvars
, rettv
)
8872 rettv
->vval
.v_number
= mch_can_exe(get_tv_string(&argvars
[0]));
8876 * "exists()" function
8879 f_exists(argvars
, rettv
)
8888 p
= get_tv_string(&argvars
[0]);
8889 if (*p
== '$') /* environment variable */
8891 /* first try "normal" environment variables (fast) */
8892 if (mch_getenv(p
+ 1) != NULL
)
8896 /* try expanding things like $VIM and ${HOME} */
8897 p
= expand_env_save(p
);
8898 if (p
!= NULL
&& *p
!= '$')
8903 else if (*p
== '&' || *p
== '+') /* option */
8905 n
= (get_option_tv(&p
, NULL
, TRUE
) == OK
);
8906 if (*skipwhite(p
) != NUL
)
8907 n
= FALSE
; /* trailing garbage */
8909 else if (*p
== '*') /* internal or user defined function */
8911 n
= function_exists(p
+ 1);
8915 n
= cmd_exists(p
+ 1);
8921 n
= autocmd_supported(p
+ 2);
8923 n
= au_exists(p
+ 1);
8926 else /* internal variable */
8931 /* get_name_len() takes care of expanding curly braces */
8933 len
= get_name_len(&p
, &tofree
, TRUE
, FALSE
);
8938 n
= (get_var_tv(name
, len
, &tv
, FALSE
) == OK
);
8941 /* handle d.key, l[idx], f(expr) */
8942 n
= (handle_subscript(&p
, &tv
, TRUE
, FALSE
) == OK
);
8953 rettv
->vval
.v_number
= n
;
8957 * "expand()" function
8960 f_expand(argvars
, rettv
)
8967 int flags
= WILD_SILENT
|WILD_USE_NL
|WILD_LIST_NOTFOUND
;
8971 rettv
->v_type
= VAR_STRING
;
8972 s
= get_tv_string(&argvars
[0]);
8973 if (*s
== '%' || *s
== '#' || *s
== '<')
8976 rettv
->vval
.v_string
= eval_vars(s
, s
, &len
, NULL
, &errormsg
, NULL
);
8981 /* When the optional second argument is non-zero, don't remove matches
8982 * for 'suffixes' and 'wildignore' */
8983 if (argvars
[1].v_type
!= VAR_UNKNOWN
8984 && get_tv_number_chk(&argvars
[1], &error
))
8985 flags
|= WILD_KEEP_ALL
;
8989 xpc
.xp_context
= EXPAND_FILES
;
8990 rettv
->vval
.v_string
= ExpandOne(&xpc
, s
, NULL
, flags
, WILD_ALL
);
8993 rettv
->vval
.v_string
= NULL
;
8998 * "extend(list, list [, idx])" function
8999 * "extend(dict, dict [, action])" function
9002 f_extend(argvars
, rettv
)
9006 rettv
->vval
.v_number
= 0;
9007 if (argvars
[0].v_type
== VAR_LIST
&& argvars
[1].v_type
== VAR_LIST
)
9014 l1
= argvars
[0].vval
.v_list
;
9015 l2
= argvars
[1].vval
.v_list
;
9016 if (l1
!= NULL
&& !tv_check_lock(l1
->lv_lock
, (char_u
*)"extend()")
9019 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9021 before
= get_tv_number_chk(&argvars
[2], &error
);
9023 return; /* type error; errmsg already given */
9025 if (before
== l1
->lv_len
)
9029 item
= list_find(l1
, before
);
9032 EMSGN(_(e_listidx
), before
);
9039 list_extend(l1
, l2
, item
);
9041 copy_tv(&argvars
[0], rettv
);
9044 else if (argvars
[0].v_type
== VAR_DICT
&& argvars
[1].v_type
== VAR_DICT
)
9053 d1
= argvars
[0].vval
.v_dict
;
9054 d2
= argvars
[1].vval
.v_dict
;
9055 if (d1
!= NULL
&& !tv_check_lock(d1
->dv_lock
, (char_u
*)"extend()")
9058 /* Check the third argument. */
9059 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9061 static char *(av
[]) = {"keep", "force", "error"};
9063 action
= get_tv_string_chk(&argvars
[2]);
9065 return; /* type error; errmsg already given */
9066 for (i
= 0; i
< 3; ++i
)
9067 if (STRCMP(action
, av
[i
]) == 0)
9071 EMSG2(_(e_invarg2
), action
);
9076 action
= (char_u
*)"force";
9078 /* Go over all entries in the second dict and add them to the
9080 todo
= (int)d2
->dv_hashtab
.ht_used
;
9081 for (hi2
= d2
->dv_hashtab
.ht_array
; todo
> 0; ++hi2
)
9083 if (!HASHITEM_EMPTY(hi2
))
9086 di1
= dict_find(d1
, hi2
->hi_key
, -1);
9089 di1
= dictitem_copy(HI2DI(hi2
));
9090 if (di1
!= NULL
&& dict_add(d1
, di1
) == FAIL
)
9093 else if (*action
== 'e')
9095 EMSG2(_("E737: Key already exists: %s"), hi2
->hi_key
);
9098 else if (*action
== 'f')
9100 clear_tv(&di1
->di_tv
);
9101 copy_tv(&HI2DI(hi2
)->di_tv
, &di1
->di_tv
);
9106 copy_tv(&argvars
[0], rettv
);
9110 EMSG2(_(e_listdictarg
), "extend()");
9114 * "feedkeys()" function
9118 f_feedkeys(argvars
, rettv
)
9123 char_u
*keys
, *flags
;
9124 char_u nbuf
[NUMBUFLEN
];
9128 /* This is not allowed in the sandbox. If the commands would still be
9129 * executed in the sandbox it would be OK, but it probably happens later,
9130 * when "sandbox" is no longer set. */
9134 rettv
->vval
.v_number
= 0;
9135 keys
= get_tv_string(&argvars
[0]);
9138 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9140 flags
= get_tv_string_buf(&argvars
[1], nbuf
);
9141 for ( ; *flags
!= NUL
; ++flags
)
9145 case 'n': remap
= FALSE
; break;
9146 case 'm': remap
= TRUE
; break;
9147 case 't': typed
= TRUE
; break;
9152 /* Need to escape K_SPECIAL and CSI before putting the string in the
9153 * typeahead buffer. */
9154 keys_esc
= vim_strsave_escape_csi(keys
);
9155 if (keys_esc
!= NULL
)
9157 ins_typebuf(keys_esc
, (remap
? REMAP_YES
: REMAP_NONE
),
9158 typebuf
.tb_len
, !typed
, FALSE
);
9161 typebuf_was_filled
= TRUE
;
9167 * "filereadable()" function
9170 f_filereadable(argvars
, rettv
)
9178 p
= get_tv_string(&argvars
[0]);
9179 if (*p
&& !mch_isdir(p
) && (fd
= mch_fopen((char *)p
, "r")) != NULL
)
9187 rettv
->vval
.v_number
= n
;
9191 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
9192 * rights to write into.
9195 f_filewritable(argvars
, rettv
)
9199 rettv
->vval
.v_number
= filewritable(get_tv_string(&argvars
[0]));
9202 static void findfilendir
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int dir
));
9205 findfilendir(argvars
, rettv
, dir
)
9210 #ifdef FEAT_SEARCHPATH
9212 char_u
*fresult
= NULL
;
9213 char_u
*path
= *curbuf
->b_p_path
== NUL
? p_path
: curbuf
->b_p_path
;
9215 char_u pathbuf
[NUMBUFLEN
];
9221 rettv
->vval
.v_string
= NULL
;
9222 rettv
->v_type
= VAR_STRING
;
9224 #ifdef FEAT_SEARCHPATH
9225 fname
= get_tv_string(&argvars
[0]);
9227 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9229 p
= get_tv_string_buf_chk(&argvars
[1], pathbuf
);
9237 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9238 count
= get_tv_number_chk(&argvars
[2], &error
);
9242 if (count
< 0 && rettv_list_alloc(rettv
) == FAIL
)
9245 if (*fname
!= NUL
&& !error
)
9249 if (rettv
->v_type
== VAR_STRING
)
9251 fresult
= find_file_in_path_option(first
? fname
: NULL
,
9252 first
? (int)STRLEN(fname
) : 0,
9253 0, first
, path
, dir
, curbuf
->b_ffname
,
9254 dir
? (char_u
*)"" : curbuf
->b_p_sua
);
9257 if (fresult
!= NULL
&& rettv
->v_type
== VAR_LIST
)
9258 list_append_string(rettv
->vval
.v_list
, fresult
, -1);
9260 } while ((rettv
->v_type
== VAR_LIST
|| --count
> 0) && fresult
!= NULL
);
9263 if (rettv
->v_type
== VAR_STRING
)
9264 rettv
->vval
.v_string
= fresult
;
9268 static void filter_map
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int map
));
9269 static int filter_map_one
__ARGS((typval_T
*tv
, char_u
*expr
, int map
, int *remp
));
9272 * Implementation of map() and filter().
9275 filter_map(argvars
, rettv
, map
)
9280 char_u buf
[NUMBUFLEN
];
9282 listitem_T
*li
, *nli
;
9292 char_u
*ermsg
= map
? (char_u
*)"map()" : (char_u
*)"filter()";
9295 rettv
->vval
.v_number
= 0;
9296 if (argvars
[0].v_type
== VAR_LIST
)
9298 if ((l
= argvars
[0].vval
.v_list
) == NULL
9299 || (map
&& tv_check_lock(l
->lv_lock
, ermsg
)))
9302 else if (argvars
[0].v_type
== VAR_DICT
)
9304 if ((d
= argvars
[0].vval
.v_dict
) == NULL
9305 || (map
&& tv_check_lock(d
->dv_lock
, ermsg
)))
9310 EMSG2(_(e_listdictarg
), ermsg
);
9314 expr
= get_tv_string_buf_chk(&argvars
[1], buf
);
9315 /* On type errors, the preceding call has already displayed an error
9316 * message. Avoid a misleading error message for an empty string that
9317 * was not passed as argument. */
9320 prepare_vimvar(VV_VAL
, &save_val
);
9321 expr
= skipwhite(expr
);
9323 /* We reset "did_emsg" to be able to detect whether an error
9324 * occurred during evaluation of the expression. */
9325 save_did_emsg
= did_emsg
;
9328 if (argvars
[0].v_type
== VAR_DICT
)
9330 prepare_vimvar(VV_KEY
, &save_key
);
9331 vimvars
[VV_KEY
].vv_type
= VAR_STRING
;
9333 ht
= &d
->dv_hashtab
;
9335 todo
= (int)ht
->ht_used
;
9336 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
9338 if (!HASHITEM_EMPTY(hi
))
9342 if (tv_check_lock(di
->di_tv
.v_lock
, ermsg
))
9344 vimvars
[VV_KEY
].vv_str
= vim_strsave(di
->di_key
);
9345 if (filter_map_one(&di
->di_tv
, expr
, map
, &rem
) == FAIL
9349 dictitem_remove(d
, di
);
9350 clear_tv(&vimvars
[VV_KEY
].vv_tv
);
9355 restore_vimvar(VV_KEY
, &save_key
);
9359 for (li
= l
->lv_first
; li
!= NULL
; li
= nli
)
9361 if (tv_check_lock(li
->li_tv
.v_lock
, ermsg
))
9364 if (filter_map_one(&li
->li_tv
, expr
, map
, &rem
) == FAIL
9368 listitem_remove(l
, li
);
9372 restore_vimvar(VV_VAL
, &save_val
);
9374 did_emsg
|= save_did_emsg
;
9377 copy_tv(&argvars
[0], rettv
);
9381 filter_map_one(tv
, expr
, map
, remp
)
9391 copy_tv(tv
, &vimvars
[VV_VAL
].vv_tv
);
9393 if (eval1(&s
, &rettv
, TRUE
) == FAIL
)
9395 if (*s
!= NUL
) /* check for trailing chars after expr */
9397 EMSG2(_(e_invexpr2
), s
);
9402 /* map(): replace the list item value */
9411 /* filter(): when expr is zero remove the item */
9412 *remp
= (get_tv_number_chk(&rettv
, &error
) == 0);
9414 /* On type error, nothing has been removed; return FAIL to stop the
9415 * loop. The error message was given by get_tv_number_chk(). */
9421 clear_tv(&vimvars
[VV_VAL
].vv_tv
);
9426 * "filter()" function
9429 f_filter(argvars
, rettv
)
9433 filter_map(argvars
, rettv
, FALSE
);
9437 * "finddir({fname}[, {path}[, {count}]])" function
9440 f_finddir(argvars
, rettv
)
9444 findfilendir(argvars
, rettv
, TRUE
);
9448 * "findfile({fname}[, {path}[, {count}]])" function
9451 f_findfile(argvars
, rettv
)
9455 findfilendir(argvars
, rettv
, FALSE
);
9459 * "fnamemodify({fname}, {mods})" function
9462 f_fnamemodify(argvars
, rettv
)
9470 char_u
*fbuf
= NULL
;
9471 char_u buf
[NUMBUFLEN
];
9473 fname
= get_tv_string_chk(&argvars
[0]);
9474 mods
= get_tv_string_buf_chk(&argvars
[1], buf
);
9475 if (fname
== NULL
|| mods
== NULL
)
9479 len
= (int)STRLEN(fname
);
9480 (void)modify_fname(mods
, &usedlen
, &fname
, &fbuf
, &len
);
9483 rettv
->v_type
= VAR_STRING
;
9485 rettv
->vval
.v_string
= NULL
;
9487 rettv
->vval
.v_string
= vim_strnsave(fname
, len
);
9491 static void foldclosed_both
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int end
));
9494 * "foldclosed()" function
9497 foldclosed_both(argvars
, rettv
, end
)
9504 linenr_T first
, last
;
9506 lnum
= get_tv_lnum(argvars
);
9507 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
9509 if (hasFoldingWin(curwin
, lnum
, &first
, &last
, FALSE
, NULL
))
9512 rettv
->vval
.v_number
= (varnumber_T
)last
;
9514 rettv
->vval
.v_number
= (varnumber_T
)first
;
9519 rettv
->vval
.v_number
= -1;
9523 * "foldclosed()" function
9526 f_foldclosed(argvars
, rettv
)
9530 foldclosed_both(argvars
, rettv
, FALSE
);
9534 * "foldclosedend()" function
9537 f_foldclosedend(argvars
, rettv
)
9541 foldclosed_both(argvars
, rettv
, TRUE
);
9545 * "foldlevel()" function
9548 f_foldlevel(argvars
, rettv
)
9555 lnum
= get_tv_lnum(argvars
);
9556 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
9557 rettv
->vval
.v_number
= foldLevel(lnum
);
9560 rettv
->vval
.v_number
= 0;
9564 * "foldtext()" function
9568 f_foldtext(argvars
, rettv
)
9580 rettv
->v_type
= VAR_STRING
;
9581 rettv
->vval
.v_string
= NULL
;
9583 if ((linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
> 0
9584 && (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
9585 <= curbuf
->b_ml
.ml_line_count
9586 && vimvars
[VV_FOLDDASHES
].vv_str
!= NULL
)
9588 /* Find first non-empty line in the fold. */
9589 lnum
= (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
;
9590 while (lnum
< (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
9592 if (!linewhite(lnum
))
9597 /* Find interesting text in this line. */
9598 s
= skipwhite(ml_get(lnum
));
9599 /* skip C comment-start */
9600 if (s
[0] == '/' && (s
[1] == '*' || s
[1] == '/'))
9602 s
= skipwhite(s
+ 2);
9603 if (*skipwhite(s
) == NUL
9604 && lnum
+ 1 < (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
9606 s
= skipwhite(ml_get(lnum
+ 1));
9608 s
= skipwhite(s
+ 1);
9611 txt
= _("+-%s%3ld lines: ");
9612 r
= alloc((unsigned)(STRLEN(txt
)
9613 + STRLEN(vimvars
[VV_FOLDDASHES
].vv_str
) /* for %s */
9615 + STRLEN(s
))); /* concatenated */
9618 sprintf((char *)r
, txt
, vimvars
[VV_FOLDDASHES
].vv_str
,
9619 (long)((linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
9620 - (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
+ 1));
9621 len
= (int)STRLEN(r
);
9623 /* remove 'foldmarker' and 'commentstring' */
9624 foldtext_cleanup(r
+ len
);
9625 rettv
->vval
.v_string
= r
;
9632 * "foldtextresult(lnum)" function
9636 f_foldtextresult(argvars
, rettv
)
9644 foldinfo_T foldinfo
;
9648 rettv
->v_type
= VAR_STRING
;
9649 rettv
->vval
.v_string
= NULL
;
9651 lnum
= get_tv_lnum(argvars
);
9652 /* treat illegal types and illegal string values for {lnum} the same */
9655 fold_count
= foldedCount(curwin
, lnum
, &foldinfo
);
9658 text
= get_foldtext(curwin
, lnum
, lnum
+ fold_count
- 1,
9661 text
= vim_strsave(text
);
9662 rettv
->vval
.v_string
= text
;
9668 * "foreground()" function
9672 f_foreground(argvars
, rettv
)
9676 rettv
->vval
.v_number
= 0;
9679 gui_mch_set_foreground();
9682 win32_set_foreground();
9688 * "function()" function
9692 f_function(argvars
, rettv
)
9698 rettv
->vval
.v_number
= 0;
9699 s
= get_tv_string(&argvars
[0]);
9700 if (s
== NULL
|| *s
== NUL
|| VIM_ISDIGIT(*s
))
9701 EMSG2(_(e_invarg2
), s
);
9702 else if (!function_exists(s
))
9703 EMSG2(_("E700: Unknown function: %s"), s
);
9706 rettv
->vval
.v_string
= vim_strsave(s
);
9707 rettv
->v_type
= VAR_FUNC
;
9712 * "garbagecollect()" function
9716 f_garbagecollect(argvars
, rettv
)
9720 /* This is postponed until we are back at the toplevel, because we may be
9721 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9722 want_garbage_collect
= TRUE
;
9724 if (argvars
[0].v_type
!= VAR_UNKNOWN
&& get_tv_number(&argvars
[0]) == 1)
9725 garbage_collect_at_exit
= TRUE
;
9732 f_get(argvars
, rettv
)
9740 typval_T
*tv
= NULL
;
9742 if (argvars
[0].v_type
== VAR_LIST
)
9744 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
9748 li
= list_find(l
, get_tv_number_chk(&argvars
[1], &error
));
9749 if (!error
&& li
!= NULL
)
9753 else if (argvars
[0].v_type
== VAR_DICT
)
9755 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
9757 di
= dict_find(d
, get_tv_string(&argvars
[1]), -1);
9763 EMSG2(_(e_listdictarg
), "get()");
9767 if (argvars
[2].v_type
== VAR_UNKNOWN
)
9768 rettv
->vval
.v_number
= 0;
9770 copy_tv(&argvars
[2], rettv
);
9776 static void get_buffer_lines
__ARGS((buf_T
*buf
, linenr_T start
, linenr_T end
, int retlist
, typval_T
*rettv
));
9779 * Get line or list of lines from buffer "buf" into "rettv".
9780 * Return a range (from start to end) of lines in rettv from the specified
9782 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
9785 get_buffer_lines(buf
, start
, end
, retlist
, rettv
)
9796 if (rettv_list_alloc(rettv
) == FAIL
)
9800 rettv
->vval
.v_number
= 0;
9802 if (buf
== NULL
|| buf
->b_ml
.ml_mfp
== NULL
|| start
< 0)
9807 if (start
>= 1 && start
<= buf
->b_ml
.ml_line_count
)
9808 p
= ml_get_buf(buf
, start
, FALSE
);
9812 rettv
->v_type
= VAR_STRING
;
9813 rettv
->vval
.v_string
= vim_strsave(p
);
9822 if (end
> buf
->b_ml
.ml_line_count
)
9823 end
= buf
->b_ml
.ml_line_count
;
9824 while (start
<= end
)
9825 if (list_append_string(rettv
->vval
.v_list
,
9826 ml_get_buf(buf
, start
++, FALSE
), -1) == FAIL
)
9832 * "getbufline()" function
9835 f_getbufline(argvars
, rettv
)
9843 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
9845 buf
= get_buf_tv(&argvars
[0]);
9848 lnum
= get_tv_lnum_buf(&argvars
[1], buf
);
9849 if (argvars
[2].v_type
== VAR_UNKNOWN
)
9852 end
= get_tv_lnum_buf(&argvars
[2], buf
);
9854 get_buffer_lines(buf
, lnum
, end
, TRUE
, rettv
);
9858 * "getbufvar()" function
9861 f_getbufvar(argvars
, rettv
)
9870 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
9871 varname
= get_tv_string_chk(&argvars
[1]);
9873 buf
= get_buf_tv(&argvars
[0]);
9875 rettv
->v_type
= VAR_STRING
;
9876 rettv
->vval
.v_string
= NULL
;
9878 if (buf
!= NULL
&& varname
!= NULL
)
9880 if (*varname
== '&') /* buffer-local-option */
9882 /* set curbuf to be our buf, temporarily */
9883 save_curbuf
= curbuf
;
9886 get_option_tv(&varname
, rettv
, TRUE
);
9888 /* restore previous notion of curbuf */
9889 curbuf
= save_curbuf
;
9893 if (*varname
== NUL
)
9894 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9895 * scope prefix before the NUL byte is required by
9896 * find_var_in_ht(). */
9897 varname
= (char_u
*)"b:" + 2;
9898 /* look up the variable */
9899 v
= find_var_in_ht(&buf
->b_vars
.dv_hashtab
, varname
, FALSE
);
9901 copy_tv(&v
->di_tv
, rettv
);
9909 * "getchar()" function
9912 f_getchar(argvars
, rettv
)
9919 /* Position the cursor. Needed after a message that ends in a space. */
9920 windgoto(msg_row
, msg_col
);
9926 if (argvars
[0].v_type
== VAR_UNKNOWN
)
9927 /* getchar(): blocking wait. */
9929 else if (get_tv_number_chk(&argvars
[0], &error
) == 1)
9930 /* getchar(1): only check if char avail */
9932 else if (error
|| vpeekc() == NUL
)
9933 /* illegal argument or getchar(0) and no char avail: return zero */
9936 /* getchar(0) and char avail: return char */
9945 vimvars
[VV_MOUSE_WIN
].vv_nr
= 0;
9946 vimvars
[VV_MOUSE_LNUM
].vv_nr
= 0;
9947 vimvars
[VV_MOUSE_COL
].vv_nr
= 0;
9949 rettv
->vval
.v_number
= n
;
9950 if (IS_SPECIAL(n
) || mod_mask
!= 0)
9952 char_u temp
[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9955 /* Turn a special key into three bytes, plus modifier. */
9958 temp
[i
++] = K_SPECIAL
;
9959 temp
[i
++] = KS_MODIFIER
;
9960 temp
[i
++] = mod_mask
;
9964 temp
[i
++] = K_SPECIAL
;
9965 temp
[i
++] = K_SECOND(n
);
9966 temp
[i
++] = K_THIRD(n
);
9970 i
+= (*mb_char2bytes
)(n
, temp
+ i
);
9975 rettv
->v_type
= VAR_STRING
;
9976 rettv
->vval
.v_string
= vim_strsave(temp
);
9979 if (n
== K_LEFTMOUSE
9980 || n
== K_LEFTMOUSE_NM
9982 || n
== K_LEFTRELEASE
9983 || n
== K_LEFTRELEASE_NM
9984 || n
== K_MIDDLEMOUSE
9985 || n
== K_MIDDLEDRAG
9986 || n
== K_MIDDLERELEASE
9987 || n
== K_RIGHTMOUSE
9989 || n
== K_RIGHTRELEASE
9999 int row
= mouse_row
;
10000 int col
= mouse_col
;
10003 # ifdef FEAT_WINDOWS
10008 if (row
>= 0 && col
>= 0)
10010 /* Find the window at the mouse coordinates and compute the
10011 * text position. */
10012 win
= mouse_find_win(&row
, &col
);
10013 (void)mouse_comp_pos(win
, &row
, &col
, &lnum
);
10014 # ifdef FEAT_WINDOWS
10015 for (wp
= firstwin
; wp
!= win
; wp
= wp
->w_next
)
10018 vimvars
[VV_MOUSE_WIN
].vv_nr
= n
;
10019 vimvars
[VV_MOUSE_LNUM
].vv_nr
= lnum
;
10020 vimvars
[VV_MOUSE_COL
].vv_nr
= col
+ 1;
10028 * "getcharmod()" function
10032 f_getcharmod(argvars
, rettv
)
10036 rettv
->vval
.v_number
= mod_mask
;
10040 * "getcmdline()" function
10044 f_getcmdline(argvars
, rettv
)
10048 rettv
->v_type
= VAR_STRING
;
10049 rettv
->vval
.v_string
= get_cmdline_str();
10053 * "getcmdpos()" function
10057 f_getcmdpos(argvars
, rettv
)
10061 rettv
->vval
.v_number
= get_cmdline_pos() + 1;
10065 * "getcmdtype()" function
10069 f_getcmdtype(argvars
, rettv
)
10073 rettv
->v_type
= VAR_STRING
;
10074 rettv
->vval
.v_string
= alloc(2);
10075 if (rettv
->vval
.v_string
!= NULL
)
10077 rettv
->vval
.v_string
[0] = get_cmdline_type();
10078 rettv
->vval
.v_string
[1] = NUL
;
10083 * "getcwd()" function
10087 f_getcwd(argvars
, rettv
)
10091 char_u cwd
[MAXPATHL
];
10093 rettv
->v_type
= VAR_STRING
;
10094 if (mch_dirname(cwd
, MAXPATHL
) == FAIL
)
10095 rettv
->vval
.v_string
= NULL
;
10098 rettv
->vval
.v_string
= vim_strsave(cwd
);
10099 #ifdef BACKSLASH_IN_FILENAME
10100 if (rettv
->vval
.v_string
!= NULL
)
10101 slash_adjust(rettv
->vval
.v_string
);
10107 * "getfontname()" function
10111 f_getfontname(argvars
, rettv
)
10115 rettv
->v_type
= VAR_STRING
;
10116 rettv
->vval
.v_string
= NULL
;
10121 char_u
*name
= NULL
;
10123 if (argvars
[0].v_type
== VAR_UNKNOWN
)
10125 /* Get the "Normal" font. Either the name saved by
10126 * hl_set_font_name() or from the font ID. */
10127 font
= gui
.norm_font
;
10128 name
= hl_get_font_name();
10132 name
= get_tv_string(&argvars
[0]);
10133 if (STRCMP(name
, "*") == 0) /* don't use font dialog */
10135 font
= gui_mch_get_font(name
, FALSE
);
10136 if (font
== NOFONT
)
10137 return; /* Invalid font name, return empty string. */
10139 rettv
->vval
.v_string
= gui_mch_get_fontname(font
, name
);
10140 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
10141 gui_mch_free_font(font
);
10147 * "getfperm({fname})" function
10150 f_getfperm(argvars
, rettv
)
10156 char_u
*perm
= NULL
;
10157 char_u flags
[] = "rwx";
10160 fname
= get_tv_string(&argvars
[0]);
10162 rettv
->v_type
= VAR_STRING
;
10163 if (mch_stat((char *)fname
, &st
) >= 0)
10165 perm
= vim_strsave((char_u
*)"---------");
10168 for (i
= 0; i
< 9; i
++)
10170 if (st
.st_mode
& (1 << (8 - i
)))
10171 perm
[i
] = flags
[i
% 3];
10175 rettv
->vval
.v_string
= perm
;
10179 * "getfsize({fname})" function
10182 f_getfsize(argvars
, rettv
)
10189 fname
= get_tv_string(&argvars
[0]);
10191 rettv
->v_type
= VAR_NUMBER
;
10193 if (mch_stat((char *)fname
, &st
) >= 0)
10195 if (mch_isdir(fname
))
10196 rettv
->vval
.v_number
= 0;
10199 rettv
->vval
.v_number
= (varnumber_T
)st
.st_size
;
10201 /* non-perfect check for overflow */
10202 if ((off_t
)rettv
->vval
.v_number
!= (off_t
)st
.st_size
)
10203 rettv
->vval
.v_number
= -2;
10207 rettv
->vval
.v_number
= -1;
10211 * "getftime({fname})" function
10214 f_getftime(argvars
, rettv
)
10221 fname
= get_tv_string(&argvars
[0]);
10223 if (mch_stat((char *)fname
, &st
) >= 0)
10224 rettv
->vval
.v_number
= (varnumber_T
)st
.st_mtime
;
10226 rettv
->vval
.v_number
= -1;
10230 * "getftype({fname})" function
10233 f_getftype(argvars
, rettv
)
10239 char_u
*type
= NULL
;
10242 fname
= get_tv_string(&argvars
[0]);
10244 rettv
->v_type
= VAR_STRING
;
10245 if (mch_lstat((char *)fname
, &st
) >= 0)
10248 if (S_ISREG(st
.st_mode
))
10250 else if (S_ISDIR(st
.st_mode
))
10253 else if (S_ISLNK(st
.st_mode
))
10257 else if (S_ISBLK(st
.st_mode
))
10261 else if (S_ISCHR(st
.st_mode
))
10265 else if (S_ISFIFO(st
.st_mode
))
10269 else if (S_ISSOCK(st
.st_mode
))
10276 switch (st
.st_mode
& S_IFMT
)
10278 case S_IFREG
: t
= "file"; break;
10279 case S_IFDIR
: t
= "dir"; break;
10281 case S_IFLNK
: t
= "link"; break;
10284 case S_IFBLK
: t
= "bdev"; break;
10287 case S_IFCHR
: t
= "cdev"; break;
10290 case S_IFIFO
: t
= "fifo"; break;
10293 case S_IFSOCK
: t
= "socket"; break;
10295 default: t
= "other";
10298 if (mch_isdir(fname
))
10304 type
= vim_strsave((char_u
*)t
);
10306 rettv
->vval
.v_string
= type
;
10310 * "getline(lnum, [end])" function
10313 f_getline(argvars
, rettv
)
10321 lnum
= get_tv_lnum(argvars
);
10322 if (argvars
[1].v_type
== VAR_UNKNOWN
)
10329 end
= get_tv_lnum(&argvars
[1]);
10333 get_buffer_lines(curbuf
, lnum
, end
, retlist
, rettv
);
10337 * "getmatches()" function
10341 f_getmatches(argvars
, rettv
)
10345 #ifdef FEAT_SEARCH_EXTRA
10347 matchitem_T
*cur
= curwin
->w_match_head
;
10349 rettv
->vval
.v_number
= 0;
10351 if (rettv_list_alloc(rettv
) == OK
)
10353 while (cur
!= NULL
)
10355 dict
= dict_alloc();
10358 dict_add_nr_str(dict
, "group", 0L, syn_id2name(cur
->hlg_id
));
10359 dict_add_nr_str(dict
, "pattern", 0L, cur
->pattern
);
10360 dict_add_nr_str(dict
, "priority", (long)cur
->priority
, NULL
);
10361 dict_add_nr_str(dict
, "id", (long)cur
->id
, NULL
);
10362 list_append_dict(rettv
->vval
.v_list
, dict
);
10370 * "getpos(string)" function
10373 f_getpos(argvars
, rettv
)
10381 if (rettv_list_alloc(rettv
) == OK
)
10383 l
= rettv
->vval
.v_list
;
10384 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
10386 list_append_number(l
, (varnumber_T
)fnum
);
10388 list_append_number(l
, (varnumber_T
)0);
10389 list_append_number(l
, (fp
!= NULL
) ? (varnumber_T
)fp
->lnum
10391 list_append_number(l
, (fp
!= NULL
)
10392 ? (varnumber_T
)(fp
->col
== MAXCOL
? MAXCOL
: fp
->col
+ 1)
10394 list_append_number(l
,
10395 #ifdef FEAT_VIRTUALEDIT
10396 (fp
!= NULL
) ? (varnumber_T
)fp
->coladd
:
10401 rettv
->vval
.v_number
= FALSE
;
10405 * "getqflist()" and "getloclist()" functions
10409 f_getqflist(argvars
, rettv
)
10413 #ifdef FEAT_QUICKFIX
10417 rettv
->vval
.v_number
= 0;
10418 #ifdef FEAT_QUICKFIX
10419 if (rettv_list_alloc(rettv
) == OK
)
10422 if (argvars
[0].v_type
!= VAR_UNKNOWN
) /* getloclist() */
10424 wp
= find_win_by_nr(&argvars
[0], NULL
);
10429 (void)get_errorlist(wp
, rettv
->vval
.v_list
);
10435 * "getreg()" function
10438 f_getreg(argvars
, rettv
)
10442 char_u
*strregname
;
10447 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
10449 strregname
= get_tv_string_chk(&argvars
[0]);
10450 error
= strregname
== NULL
;
10451 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
10452 arg2
= get_tv_number_chk(&argvars
[1], &error
);
10455 strregname
= vimvars
[VV_REG
].vv_str
;
10456 regname
= (strregname
== NULL
? '"' : *strregname
);
10460 rettv
->v_type
= VAR_STRING
;
10461 rettv
->vval
.v_string
= error
? NULL
:
10462 get_reg_contents(regname
, TRUE
, arg2
);
10466 * "getregtype()" function
10469 f_getregtype(argvars
, rettv
)
10473 char_u
*strregname
;
10475 char_u buf
[NUMBUFLEN
+ 2];
10478 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
10480 strregname
= get_tv_string_chk(&argvars
[0]);
10481 if (strregname
== NULL
) /* type error; errmsg already given */
10483 rettv
->v_type
= VAR_STRING
;
10484 rettv
->vval
.v_string
= NULL
;
10489 /* Default to v:register */
10490 strregname
= vimvars
[VV_REG
].vv_str
;
10492 regname
= (strregname
== NULL
? '"' : *strregname
);
10498 switch (get_reg_type(regname
, ®len
))
10500 case MLINE
: buf
[0] = 'V'; break;
10501 case MCHAR
: buf
[0] = 'v'; break;
10505 sprintf((char *)buf
+ 1, "%ld", reglen
+ 1);
10509 rettv
->v_type
= VAR_STRING
;
10510 rettv
->vval
.v_string
= vim_strsave(buf
);
10514 * "gettabwinvar()" function
10517 f_gettabwinvar(argvars
, rettv
)
10521 getwinvar(argvars
, rettv
, 1);
10525 * "getwinposx()" function
10529 f_getwinposx(argvars
, rettv
)
10533 rettv
->vval
.v_number
= -1;
10539 if (gui_mch_get_winpos(&x
, &y
) == OK
)
10540 rettv
->vval
.v_number
= x
;
10546 * "getwinposy()" function
10550 f_getwinposy(argvars
, rettv
)
10554 rettv
->vval
.v_number
= -1;
10560 if (gui_mch_get_winpos(&x
, &y
) == OK
)
10561 rettv
->vval
.v_number
= y
;
10567 * Find window specifed by "vp" in tabpage "tp".
10570 find_win_by_nr(vp
, tp
)
10572 tabpage_T
*tp
; /* NULL for current tab page */
10574 #ifdef FEAT_WINDOWS
10579 nr
= get_tv_number_chk(vp
, NULL
);
10581 #ifdef FEAT_WINDOWS
10587 for (wp
= (tp
== NULL
|| tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
10588 wp
!= NULL
; wp
= wp
->w_next
)
10593 if (nr
== 0 || nr
== 1)
10600 * "getwinvar()" function
10603 f_getwinvar(argvars
, rettv
)
10607 getwinvar(argvars
, rettv
, 0);
10611 * getwinvar() and gettabwinvar()
10614 getwinvar(argvars
, rettv
, off
)
10617 int off
; /* 1 for gettabwinvar() */
10619 win_T
*win
, *oldcurwin
;
10624 #ifdef FEAT_WINDOWS
10626 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
10630 win
= find_win_by_nr(&argvars
[off
], tp
);
10631 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
10634 rettv
->v_type
= VAR_STRING
;
10635 rettv
->vval
.v_string
= NULL
;
10637 if (win
!= NULL
&& varname
!= NULL
)
10639 /* Set curwin to be our win, temporarily. Also set curbuf, so
10640 * that we can get buffer-local options. */
10641 oldcurwin
= curwin
;
10643 curbuf
= win
->w_buffer
;
10645 if (*varname
== '&') /* window-local-option */
10646 get_option_tv(&varname
, rettv
, 1);
10649 if (*varname
== NUL
)
10650 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10651 * scope prefix before the NUL byte is required by
10652 * find_var_in_ht(). */
10653 varname
= (char_u
*)"w:" + 2;
10654 /* look up the variable */
10655 v
= find_var_in_ht(&win
->w_vars
.dv_hashtab
, varname
, FALSE
);
10657 copy_tv(&v
->di_tv
, rettv
);
10660 /* restore previous notion of curwin */
10661 curwin
= oldcurwin
;
10662 curbuf
= curwin
->w_buffer
;
10669 * "glob()" function
10672 f_glob(argvars
, rettv
)
10679 xpc
.xp_context
= EXPAND_FILES
;
10680 rettv
->v_type
= VAR_STRING
;
10681 rettv
->vval
.v_string
= ExpandOne(&xpc
, get_tv_string(&argvars
[0]),
10682 NULL
, WILD_USE_NL
|WILD_SILENT
, WILD_ALL
);
10686 * "globpath()" function
10689 f_globpath(argvars
, rettv
)
10693 char_u buf1
[NUMBUFLEN
];
10694 char_u
*file
= get_tv_string_buf_chk(&argvars
[1], buf1
);
10696 rettv
->v_type
= VAR_STRING
;
10698 rettv
->vval
.v_string
= NULL
;
10700 rettv
->vval
.v_string
= globpath(get_tv_string(&argvars
[0]), file
);
10707 f_has(argvars
, rettv
)
10714 static char *(has_list
[]) =
10735 #if defined(MACOS_X_UNIX)
10759 #if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10768 #ifndef CASE_INSENSITIVE_FILENAME
10774 #ifdef FEAT_AUTOCMD
10779 # ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10780 "balloon_multiline",
10783 #if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10785 # ifdef ALL_BUILTIN_TCAPS
10786 "all_builtin_terms",
10789 #ifdef FEAT_BYTEOFF
10792 #ifdef FEAT_CINDENT
10795 #ifdef FEAT_CLIENTSERVER
10798 #ifdef FEAT_CLIPBOARD
10801 #ifdef FEAT_CMDL_COMPL
10804 #ifdef FEAT_CMDHIST
10807 #ifdef FEAT_COMMENTS
10816 #ifdef CURSOR_SHAPE
10822 #ifdef FEAT_CON_DIALOG
10825 #ifdef FEAT_GUI_DIALOG
10831 #ifdef FEAT_DIGRAPHS
10837 #ifdef FEAT_EMACS_TAGS
10840 "eval", /* always present, of course! */
10841 #ifdef FEAT_EX_EXTRA
10844 #ifdef FEAT_SEARCH_EXTRA
10850 #ifdef FEAT_SEARCHPATH
10853 #if defined(UNIX) && !defined(USE_SYSTEM)
10856 #ifdef FEAT_FIND_ID
10859 #ifdef FEAT_FOLDING
10865 #if !defined(USE_SYSTEM) && defined(UNIX)
10868 #ifdef FEAT_GETTEXT
10874 #ifdef FEAT_GUI_ATHENA
10875 # ifdef FEAT_GUI_NEXTAW
10881 #ifdef FEAT_GUI_GTK
10887 #ifdef FEAT_GUI_GNOME
10890 #ifdef FEAT_GUI_MAC
10893 #ifdef FEAT_GUI_MOTIF
10896 #ifdef FEAT_GUI_PHOTON
10899 #ifdef FEAT_GUI_W16
10902 #ifdef FEAT_GUI_W32
10905 #ifdef FEAT_HANGULIN
10908 #if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10911 #ifdef FEAT_INS_EXPAND
10914 #ifdef FEAT_JUMPLIST
10920 #ifdef FEAT_LANGMAP
10923 #ifdef FEAT_LIBCALL
10926 #ifdef FEAT_LINEBREAK
10932 #ifdef FEAT_LISTCMDS
10935 #ifdef FEAT_LOCALMAP
10941 #ifdef FEAT_SESSION
10944 #ifdef FEAT_MODIFY_FNAME
10950 #ifdef FEAT_MOUSESHAPE
10953 #if defined(UNIX) || defined(VMS)
10954 # ifdef FEAT_MOUSE_DEC
10957 # ifdef FEAT_MOUSE_GPM
10960 # ifdef FEAT_MOUSE_JSB
10963 # ifdef FEAT_MOUSE_NET
10966 # ifdef FEAT_MOUSE_PTERM
10969 # ifdef FEAT_MOUSE_XTERM
10976 #ifdef FEAT_MBYTE_IME
10979 #ifdef FEAT_MULTI_LANG
10982 #ifdef FEAT_MZSCHEME
10983 #ifndef DYNAMIC_MZSCHEME
10990 #ifdef FEAT_OSFILETYPE
10993 #ifdef FEAT_PATH_EXTRA
10997 #ifndef DYNAMIC_PERL
11002 #ifndef DYNAMIC_PYTHON
11006 #ifdef FEAT_POSTSCRIPT
11009 #ifdef FEAT_PRINTER
11012 #ifdef FEAT_PROFILE
11015 #ifdef FEAT_RELTIME
11018 #ifdef FEAT_QUICKFIX
11021 #ifdef FEAT_RIGHTLEFT
11024 #if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11027 #ifdef FEAT_SCROLLBIND
11030 #ifdef FEAT_CMDL_INFO
11037 #ifdef FEAT_SMARTINDENT
11043 #ifdef FEAT_STL_OPT
11046 #ifdef FEAT_SUN_WORKSHOP
11049 #ifdef FEAT_NETBEANS_INTG
11058 #if defined(USE_SYSTEM) || !defined(UNIX)
11061 #ifdef FEAT_TAG_BINS
11064 #ifdef FEAT_TAG_OLDSTATIC
11067 #ifdef FEAT_TAG_ANYWHITE
11071 # ifndef DYNAMIC_TCL
11078 #ifdef FEAT_TERMRESPONSE
11081 #ifdef FEAT_TEXTOBJ
11084 #ifdef HAVE_TGETENT
11090 #ifdef FEAT_TOOLBAR
11093 #ifdef FEAT_USR_CMDS
11094 "user-commands", /* was accidentally included in 5.4 */
11097 #ifdef FEAT_VIMINFO
11100 #ifdef FEAT_VERTSPLIT
11103 #ifdef FEAT_VIRTUALEDIT
11109 #ifdef FEAT_VISUALEXTRA
11112 #ifdef FEAT_VREPLACE
11115 #ifdef FEAT_WILDIGN
11118 #ifdef FEAT_WILDMENU
11121 #ifdef FEAT_WINDOWS
11127 #ifdef FEAT_WRITEBACKUP
11133 #ifdef FEAT_XFONTSET
11139 #ifdef USE_XSMP_INTERACT
11142 #ifdef FEAT_XCLIPBOARD
11145 #ifdef FEAT_XTERM_SAVE
11148 #if defined(UNIX) && defined(FEAT_X11)
11154 name
= get_tv_string(&argvars
[0]);
11155 for (i
= 0; has_list
[i
] != NULL
; ++i
)
11156 if (STRICMP(name
, has_list
[i
]) == 0)
11164 if (STRNICMP(name
, "patch", 5) == 0)
11165 n
= has_patch(atoi((char *)name
+ 5));
11166 else if (STRICMP(name
, "vim_starting") == 0)
11167 n
= (starting
!= 0);
11168 #if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11169 else if (STRICMP(name
, "balloon_multiline") == 0)
11170 n
= multiline_balloon_available();
11173 else if (STRICMP(name
, "tcl") == 0)
11174 n
= tcl_enabled(FALSE
);
11176 #if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11177 else if (STRICMP(name
, "iconv") == 0)
11178 n
= iconv_enabled(FALSE
);
11180 #ifdef DYNAMIC_MZSCHEME
11181 else if (STRICMP(name
, "mzscheme") == 0)
11182 n
= mzscheme_enabled(FALSE
);
11184 #ifdef DYNAMIC_RUBY
11185 else if (STRICMP(name
, "ruby") == 0)
11186 n
= ruby_enabled(FALSE
);
11188 #ifdef DYNAMIC_PYTHON
11189 else if (STRICMP(name
, "python") == 0)
11190 n
= python_enabled(FALSE
);
11192 #ifdef DYNAMIC_PERL
11193 else if (STRICMP(name
, "perl") == 0)
11194 n
= perl_enabled(FALSE
);
11197 else if (STRICMP(name
, "gui_running") == 0)
11198 n
= (gui
.in_use
|| gui
.starting
);
11199 # ifdef FEAT_GUI_W32
11200 else if (STRICMP(name
, "gui_win32s") == 0)
11201 n
= gui_is_win32s();
11203 # ifdef FEAT_BROWSE
11204 else if (STRICMP(name
, "browse") == 0)
11205 n
= gui
.in_use
; /* gui_mch_browse() works when GUI is running */
11209 else if (STRICMP(name
, "syntax_items") == 0)
11210 n
= syntax_present(curbuf
);
11212 #if defined(WIN3264)
11213 else if (STRICMP(name
, "win95") == 0)
11214 n
= mch_windows95();
11216 #ifdef FEAT_NETBEANS_INTG
11217 else if (STRICMP(name
, "netbeans_enabled") == 0)
11222 rettv
->vval
.v_number
= n
;
11226 * "has_key()" function
11229 f_has_key(argvars
, rettv
)
11233 rettv
->vval
.v_number
= 0;
11234 if (argvars
[0].v_type
!= VAR_DICT
)
11236 EMSG(_(e_dictreq
));
11239 if (argvars
[0].vval
.v_dict
== NULL
)
11242 rettv
->vval
.v_number
= dict_find(argvars
[0].vval
.v_dict
,
11243 get_tv_string(&argvars
[1]), -1) != NULL
;
11247 * "haslocaldir()" function
11251 f_haslocaldir(argvars
, rettv
)
11255 rettv
->vval
.v_number
= (curwin
->w_localdir
!= NULL
);
11259 * "hasmapto()" function
11262 f_hasmapto(argvars
, rettv
)
11268 char_u buf
[NUMBUFLEN
];
11271 name
= get_tv_string(&argvars
[0]);
11272 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11273 mode
= (char_u
*)"nvo";
11276 mode
= get_tv_string_buf(&argvars
[1], buf
);
11277 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
11278 abbr
= get_tv_number(&argvars
[2]);
11281 if (map_to_exists(name
, mode
, abbr
))
11282 rettv
->vval
.v_number
= TRUE
;
11284 rettv
->vval
.v_number
= FALSE
;
11288 * "histadd()" function
11292 f_histadd(argvars
, rettv
)
11296 #ifdef FEAT_CMDHIST
11299 char_u buf
[NUMBUFLEN
];
11302 rettv
->vval
.v_number
= FALSE
;
11303 if (check_restricted() || check_secure())
11305 #ifdef FEAT_CMDHIST
11306 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11307 histype
= str
!= NULL
? get_histtype(str
) : -1;
11310 str
= get_tv_string_buf(&argvars
[1], buf
);
11313 add_to_history(histype
, str
, FALSE
, NUL
);
11314 rettv
->vval
.v_number
= TRUE
;
11322 * "histdel()" function
11326 f_histdel(argvars
, rettv
)
11330 #ifdef FEAT_CMDHIST
11332 char_u buf
[NUMBUFLEN
];
11335 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11338 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
11339 /* only one argument: clear entire history */
11340 n
= clr_history(get_histtype(str
));
11341 else if (argvars
[1].v_type
== VAR_NUMBER
)
11342 /* index given: remove that entry */
11343 n
= del_history_idx(get_histtype(str
),
11344 (int)get_tv_number(&argvars
[1]));
11346 /* string given: remove all matching entries */
11347 n
= del_history_entry(get_histtype(str
),
11348 get_tv_string_buf(&argvars
[1], buf
));
11349 rettv
->vval
.v_number
= n
;
11351 rettv
->vval
.v_number
= 0;
11356 * "histget()" function
11360 f_histget(argvars
, rettv
)
11364 #ifdef FEAT_CMDHIST
11369 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11371 rettv
->vval
.v_string
= NULL
;
11374 type
= get_histtype(str
);
11375 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11376 idx
= get_history_idx(type
);
11378 idx
= (int)get_tv_number_chk(&argvars
[1], NULL
);
11379 /* -1 on type error */
11380 rettv
->vval
.v_string
= vim_strsave(get_history_entry(type
, idx
));
11383 rettv
->vval
.v_string
= NULL
;
11385 rettv
->v_type
= VAR_STRING
;
11389 * "histnr()" function
11393 f_histnr(argvars
, rettv
)
11399 #ifdef FEAT_CMDHIST
11400 char_u
*history
= get_tv_string_chk(&argvars
[0]);
11402 i
= history
== NULL
? HIST_CMD
- 1 : get_histtype(history
);
11403 if (i
>= HIST_CMD
&& i
< HIST_COUNT
)
11404 i
= get_history_idx(i
);
11408 rettv
->vval
.v_number
= i
;
11412 * "highlightID(name)" function
11415 f_hlID(argvars
, rettv
)
11419 rettv
->vval
.v_number
= syn_name2id(get_tv_string(&argvars
[0]));
11423 * "highlight_exists()" function
11426 f_hlexists(argvars
, rettv
)
11430 rettv
->vval
.v_number
= highlight_exists(get_tv_string(&argvars
[0]));
11434 * "hostname()" function
11438 f_hostname(argvars
, rettv
)
11442 char_u hostname
[256];
11444 mch_get_host_name(hostname
, 256);
11445 rettv
->v_type
= VAR_STRING
;
11446 rettv
->vval
.v_string
= vim_strsave(hostname
);
11454 f_iconv(argvars
, rettv
)
11459 char_u buf1
[NUMBUFLEN
];
11460 char_u buf2
[NUMBUFLEN
];
11461 char_u
*from
, *to
, *str
;
11465 rettv
->v_type
= VAR_STRING
;
11466 rettv
->vval
.v_string
= NULL
;
11469 str
= get_tv_string(&argvars
[0]);
11470 from
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[1], buf1
)));
11471 to
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[2], buf2
)));
11472 vimconv
.vc_type
= CONV_NONE
;
11473 convert_setup(&vimconv
, from
, to
);
11475 /* If the encodings are equal, no conversion needed. */
11476 if (vimconv
.vc_type
== CONV_NONE
)
11477 rettv
->vval
.v_string
= vim_strsave(str
);
11479 rettv
->vval
.v_string
= string_convert(&vimconv
, str
, NULL
);
11481 convert_setup(&vimconv
, NULL
, NULL
);
11488 * "indent()" function
11491 f_indent(argvars
, rettv
)
11497 lnum
= get_tv_lnum(argvars
);
11498 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
11499 rettv
->vval
.v_number
= get_indent_lnum(lnum
);
11501 rettv
->vval
.v_number
= -1;
11505 * "index()" function
11508 f_index(argvars
, rettv
)
11517 rettv
->vval
.v_number
= -1;
11518 if (argvars
[0].v_type
!= VAR_LIST
)
11520 EMSG(_(e_listreq
));
11523 l
= argvars
[0].vval
.v_list
;
11526 item
= l
->lv_first
;
11527 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
11531 /* Start at specified item. Use the cached index that list_find()
11532 * sets, so that a negative number also works. */
11533 item
= list_find(l
, get_tv_number_chk(&argvars
[2], &error
));
11535 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
11536 ic
= get_tv_number_chk(&argvars
[3], &error
);
11541 for ( ; item
!= NULL
; item
= item
->li_next
, ++idx
)
11542 if (tv_equal(&item
->li_tv
, &argvars
[1], ic
))
11544 rettv
->vval
.v_number
= idx
;
11550 static int inputsecret_flag
= 0;
11552 static void get_user_input
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int inputdialog
));
11555 * This function is used by f_input() and f_inputdialog() functions. The third
11556 * argument to f_input() specifies the type of completion to use at the
11557 * prompt. The third argument to f_inputdialog() specifies the value to return
11558 * when the user cancels the prompt.
11561 get_user_input(argvars
, rettv
, inputdialog
)
11566 char_u
*prompt
= get_tv_string_chk(&argvars
[0]);
11569 char_u buf
[NUMBUFLEN
];
11570 int cmd_silent_save
= cmd_silent
;
11571 char_u
*defstr
= (char_u
*)"";
11572 int xp_type
= EXPAND_NOTHING
;
11573 char_u
*xp_arg
= NULL
;
11575 rettv
->v_type
= VAR_STRING
;
11576 rettv
->vval
.v_string
= NULL
;
11578 #ifdef NO_CONSOLE_INPUT
11579 /* While starting up, there is no place to enter text. */
11580 if (no_console_input())
11584 cmd_silent
= FALSE
; /* Want to see the prompt. */
11585 if (prompt
!= NULL
)
11587 /* Only the part of the message after the last NL is considered as
11588 * prompt for the command line */
11589 p
= vim_strrchr(prompt
, '\n');
11599 msg_puts_attr(prompt
, echo_attr
);
11600 msg_didout
= FALSE
;
11604 cmdline_row
= msg_row
;
11606 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
11608 defstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
11609 if (defstr
!= NULL
)
11610 stuffReadbuffSpec(defstr
);
11612 if (!inputdialog
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
11618 rettv
->vval
.v_string
= NULL
;
11620 xp_name
= get_tv_string_buf_chk(&argvars
[2], buf
);
11621 if (xp_name
== NULL
)
11624 xp_namelen
= (int)STRLEN(xp_name
);
11626 if (parse_compl_arg(xp_name
, xp_namelen
, &xp_type
, &argt
,
11632 if (defstr
!= NULL
)
11633 rettv
->vval
.v_string
=
11634 getcmdline_prompt(inputsecret_flag
? NUL
: '@', p
, echo_attr
,
11639 /* since the user typed this, no need to wait for return */
11640 need_wait_return
= FALSE
;
11641 msg_didout
= FALSE
;
11643 cmd_silent
= cmd_silent_save
;
11647 * "input()" function
11648 * Also handles inputsecret() when inputsecret is set.
11651 f_input(argvars
, rettv
)
11655 get_user_input(argvars
, rettv
, FALSE
);
11659 * "inputdialog()" function
11662 f_inputdialog(argvars
, rettv
)
11666 #if defined(FEAT_GUI_TEXTDIALOG)
11667 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11668 if (gui
.in_use
&& vim_strchr(p_go
, GO_CONDIALOG
) == NULL
)
11671 char_u buf
[NUMBUFLEN
];
11672 char_u
*defstr
= (char_u
*)"";
11674 message
= get_tv_string_chk(&argvars
[0]);
11675 if (argvars
[1].v_type
!= VAR_UNKNOWN
11676 && (defstr
= get_tv_string_buf_chk(&argvars
[1], buf
)) != NULL
)
11677 vim_strncpy(IObuff
, defstr
, IOSIZE
- 1);
11680 if (message
!= NULL
&& defstr
!= NULL
11681 && do_dialog(VIM_QUESTION
, NULL
, message
,
11682 (char_u
*)_("&OK\n&Cancel"), 1, IObuff
) == 1)
11683 rettv
->vval
.v_string
= vim_strsave(IObuff
);
11686 if (message
!= NULL
&& defstr
!= NULL
11687 && argvars
[1].v_type
!= VAR_UNKNOWN
11688 && argvars
[2].v_type
!= VAR_UNKNOWN
)
11689 rettv
->vval
.v_string
= vim_strsave(
11690 get_tv_string_buf(&argvars
[2], buf
));
11692 rettv
->vval
.v_string
= NULL
;
11694 rettv
->v_type
= VAR_STRING
;
11698 get_user_input(argvars
, rettv
, TRUE
);
11702 * "inputlist()" function
11705 f_inputlist(argvars
, rettv
)
11713 rettv
->vval
.v_number
= 0;
11714 #ifdef NO_CONSOLE_INPUT
11715 /* While starting up, there is no place to enter text. */
11716 if (no_console_input())
11719 if (argvars
[0].v_type
!= VAR_LIST
|| argvars
[0].vval
.v_list
== NULL
)
11721 EMSG2(_(e_listarg
), "inputlist()");
11726 msg_row
= Rows
- 1; /* for when 'cmdheight' > 1 */
11727 lines_left
= Rows
; /* avoid more prompt */
11731 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
; li
= li
->li_next
)
11733 msg_puts(get_tv_string(&li
->li_tv
));
11737 /* Ask for choice. */
11738 selected
= prompt_for_number(&mouse_used
);
11740 selected
-= lines_left
;
11742 rettv
->vval
.v_number
= selected
;
11746 static garray_T ga_userinput
= {0, 0, sizeof(tasave_T
), 4, NULL
};
11749 * "inputrestore()" function
11753 f_inputrestore(argvars
, rettv
)
11757 if (ga_userinput
.ga_len
> 0)
11759 --ga_userinput
.ga_len
;
11760 restore_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
11761 + ga_userinput
.ga_len
);
11762 rettv
->vval
.v_number
= 0; /* OK */
11764 else if (p_verbose
> 1)
11766 verb_msg((char_u
*)_("called inputrestore() more often than inputsave()"));
11767 rettv
->vval
.v_number
= 1; /* Failed */
11772 * "inputsave()" function
11776 f_inputsave(argvars
, rettv
)
11780 /* Add an entry to the stack of typehead storage. */
11781 if (ga_grow(&ga_userinput
, 1) == OK
)
11783 save_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
11784 + ga_userinput
.ga_len
);
11785 ++ga_userinput
.ga_len
;
11786 rettv
->vval
.v_number
= 0; /* OK */
11789 rettv
->vval
.v_number
= 1; /* Failed */
11793 * "inputsecret()" function
11796 f_inputsecret(argvars
, rettv
)
11801 ++inputsecret_flag
;
11802 f_input(argvars
, rettv
);
11804 --inputsecret_flag
;
11808 * "insert()" function
11811 f_insert(argvars
, rettv
)
11820 rettv
->vval
.v_number
= 0;
11821 if (argvars
[0].v_type
!= VAR_LIST
)
11822 EMSG2(_(e_listarg
), "insert()");
11823 else if ((l
= argvars
[0].vval
.v_list
) != NULL
11824 && !tv_check_lock(l
->lv_lock
, (char_u
*)"insert()"))
11826 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
11827 before
= get_tv_number_chk(&argvars
[2], &error
);
11829 return; /* type error; errmsg already given */
11831 if (before
== l
->lv_len
)
11835 item
= list_find(l
, before
);
11838 EMSGN(_(e_listidx
), before
);
11844 list_insert_tv(l
, &argvars
[1], item
);
11845 copy_tv(&argvars
[0], rettv
);
11851 * "isdirectory()" function
11854 f_isdirectory(argvars
, rettv
)
11858 rettv
->vval
.v_number
= mch_isdir(get_tv_string(&argvars
[0]));
11862 * "islocked()" function
11865 f_islocked(argvars
, rettv
)
11873 rettv
->vval
.v_number
= -1;
11874 end
= get_lval(get_tv_string(&argvars
[0]), NULL
, &lv
, FALSE
, FALSE
, FALSE
,
11876 if (end
!= NULL
&& lv
.ll_name
!= NULL
)
11879 EMSG(_(e_trailing
));
11882 if (lv
.ll_tv
== NULL
)
11884 if (check_changedtick(lv
.ll_name
))
11885 rettv
->vval
.v_number
= 1; /* always locked */
11888 di
= find_var(lv
.ll_name
, NULL
);
11891 /* Consider a variable locked when:
11892 * 1. the variable itself is locked
11893 * 2. the value of the variable is locked.
11894 * 3. the List or Dict value is locked.
11896 rettv
->vval
.v_number
= ((di
->di_flags
& DI_FLAGS_LOCK
)
11897 || tv_islocked(&di
->di_tv
));
11901 else if (lv
.ll_range
)
11902 EMSG(_("E786: Range not allowed"));
11903 else if (lv
.ll_newkey
!= NULL
)
11904 EMSG2(_(e_dictkey
), lv
.ll_newkey
);
11905 else if (lv
.ll_list
!= NULL
)
11907 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_li
->li_tv
);
11909 /* Dictionary item. */
11910 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_di
->di_tv
);
11917 static void dict_list
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int what
));
11920 * Turn a dict into a list:
11921 * "what" == 0: list of keys
11922 * "what" == 1: list of values
11923 * "what" == 2: list of items
11926 dict_list(argvars
, rettv
, what
)
11939 rettv
->vval
.v_number
= 0;
11940 if (argvars
[0].v_type
!= VAR_DICT
)
11942 EMSG(_(e_dictreq
));
11945 if ((d
= argvars
[0].vval
.v_dict
) == NULL
)
11948 if (rettv_list_alloc(rettv
) == FAIL
)
11951 todo
= (int)d
->dv_hashtab
.ht_used
;
11952 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
11954 if (!HASHITEM_EMPTY(hi
))
11959 li
= listitem_alloc();
11962 list_append(rettv
->vval
.v_list
, li
);
11967 li
->li_tv
.v_type
= VAR_STRING
;
11968 li
->li_tv
.v_lock
= 0;
11969 li
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
11971 else if (what
== 1)
11974 copy_tv(&di
->di_tv
, &li
->li_tv
);
11980 li
->li_tv
.v_type
= VAR_LIST
;
11981 li
->li_tv
.v_lock
= 0;
11982 li
->li_tv
.vval
.v_list
= l2
;
11987 li2
= listitem_alloc();
11990 list_append(l2
, li2
);
11991 li2
->li_tv
.v_type
= VAR_STRING
;
11992 li2
->li_tv
.v_lock
= 0;
11993 li2
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
11995 li2
= listitem_alloc();
11998 list_append(l2
, li2
);
11999 copy_tv(&di
->di_tv
, &li2
->li_tv
);
12006 * "items(dict)" function
12009 f_items(argvars
, rettv
)
12013 dict_list(argvars
, rettv
, 2);
12017 * "join()" function
12020 f_join(argvars
, rettv
)
12027 rettv
->vval
.v_number
= 0;
12028 if (argvars
[0].v_type
!= VAR_LIST
)
12030 EMSG(_(e_listreq
));
12033 if (argvars
[0].vval
.v_list
== NULL
)
12035 if (argvars
[1].v_type
== VAR_UNKNOWN
)
12036 sep
= (char_u
*)" ";
12038 sep
= get_tv_string_chk(&argvars
[1]);
12040 rettv
->v_type
= VAR_STRING
;
12044 ga_init2(&ga
, (int)sizeof(char), 80);
12045 list_join(&ga
, argvars
[0].vval
.v_list
, sep
, TRUE
, 0);
12046 ga_append(&ga
, NUL
);
12047 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
12050 rettv
->vval
.v_string
= NULL
;
12054 * "keys()" function
12057 f_keys(argvars
, rettv
)
12061 dict_list(argvars
, rettv
, 0);
12065 * "last_buffer_nr()" function.
12069 f_last_buffer_nr(argvars
, rettv
)
12076 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
12077 if (n
< buf
->b_fnum
)
12080 rettv
->vval
.v_number
= n
;
12087 f_len(argvars
, rettv
)
12091 switch (argvars
[0].v_type
)
12095 rettv
->vval
.v_number
= (varnumber_T
)STRLEN(
12096 get_tv_string(&argvars
[0]));
12099 rettv
->vval
.v_number
= list_len(argvars
[0].vval
.v_list
);
12102 rettv
->vval
.v_number
= dict_len(argvars
[0].vval
.v_dict
);
12105 EMSG(_("E701: Invalid type for len()"));
12110 static void libcall_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int type
));
12113 libcall_common(argvars
, rettv
, type
)
12118 #ifdef FEAT_LIBCALL
12120 char_u
**string_result
;
12124 rettv
->v_type
= type
;
12125 if (type
== VAR_NUMBER
)
12126 rettv
->vval
.v_number
= 0;
12128 rettv
->vval
.v_string
= NULL
;
12130 if (check_restricted() || check_secure())
12133 #ifdef FEAT_LIBCALL
12134 /* The first two args must be strings, otherwise its meaningless */
12135 if (argvars
[0].v_type
== VAR_STRING
&& argvars
[1].v_type
== VAR_STRING
)
12138 if (argvars
[2].v_type
== VAR_STRING
)
12139 string_in
= argvars
[2].vval
.v_string
;
12140 if (type
== VAR_NUMBER
)
12141 string_result
= NULL
;
12143 string_result
= &rettv
->vval
.v_string
;
12144 if (mch_libcall(argvars
[0].vval
.v_string
,
12145 argvars
[1].vval
.v_string
,
12147 argvars
[2].vval
.v_number
,
12150 && type
== VAR_NUMBER
)
12151 rettv
->vval
.v_number
= nr_result
;
12157 * "libcall()" function
12160 f_libcall(argvars
, rettv
)
12164 libcall_common(argvars
, rettv
, VAR_STRING
);
12168 * "libcallnr()" function
12171 f_libcallnr(argvars
, rettv
)
12175 libcall_common(argvars
, rettv
, VAR_NUMBER
);
12179 * "line(string)" function
12182 f_line(argvars
, rettv
)
12190 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
12193 rettv
->vval
.v_number
= lnum
;
12197 * "line2byte(lnum)" function
12201 f_line2byte(argvars
, rettv
)
12205 #ifndef FEAT_BYTEOFF
12206 rettv
->vval
.v_number
= -1;
12210 lnum
= get_tv_lnum(argvars
);
12211 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
12212 rettv
->vval
.v_number
= -1;
12214 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
, lnum
, NULL
);
12215 if (rettv
->vval
.v_number
>= 0)
12216 ++rettv
->vval
.v_number
;
12221 * "lispindent(lnum)" function
12224 f_lispindent(argvars
, rettv
)
12232 pos
= curwin
->w_cursor
;
12233 lnum
= get_tv_lnum(argvars
);
12234 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
12236 curwin
->w_cursor
.lnum
= lnum
;
12237 rettv
->vval
.v_number
= get_lisp_indent();
12238 curwin
->w_cursor
= pos
;
12242 rettv
->vval
.v_number
= -1;
12246 * "localtime()" function
12250 f_localtime(argvars
, rettv
)
12254 rettv
->vval
.v_number
= (varnumber_T
)time(NULL
);
12257 static void get_maparg
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int exact
));
12260 get_maparg(argvars
, rettv
, exact
)
12267 char_u buf
[NUMBUFLEN
];
12268 char_u
*keys_buf
= NULL
;
12274 /* return empty string for failure */
12275 rettv
->v_type
= VAR_STRING
;
12276 rettv
->vval
.v_string
= NULL
;
12278 keys
= get_tv_string(&argvars
[0]);
12282 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12284 which
= get_tv_string_buf_chk(&argvars
[1], buf
);
12285 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12286 abbr
= get_tv_number(&argvars
[2]);
12289 which
= (char_u
*)"";
12293 mode
= get_map_mode(&which
, 0);
12295 keys
= replace_termcodes(keys
, &keys_buf
, TRUE
, TRUE
, FALSE
);
12296 rhs
= check_map(keys
, mode
, exact
, FALSE
, abbr
);
12297 vim_free(keys_buf
);
12301 ga
.ga_itemsize
= 1;
12302 ga
.ga_growsize
= 40;
12304 while (*rhs
!= NUL
)
12305 ga_concat(&ga
, str2special(&rhs
, FALSE
));
12307 ga_append(&ga
, NUL
);
12308 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
12316 f_map(argvars
, rettv
)
12320 filter_map(argvars
, rettv
, TRUE
);
12324 * "maparg()" function
12327 f_maparg(argvars
, rettv
)
12331 get_maparg(argvars
, rettv
, TRUE
);
12335 * "mapcheck()" function
12338 f_mapcheck(argvars
, rettv
)
12342 get_maparg(argvars
, rettv
, FALSE
);
12345 static void find_some_match
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int start
));
12348 find_some_match(argvars
, rettv
, type
)
12353 char_u
*str
= NULL
;
12354 char_u
*expr
= NULL
;
12356 regmatch_T regmatch
;
12357 char_u patbuf
[NUMBUFLEN
];
12358 char_u strbuf
[NUMBUFLEN
];
12362 colnr_T startcol
= 0;
12365 listitem_T
*li
= NULL
;
12367 char_u
*tofree
= NULL
;
12369 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12371 p_cpo
= (char_u
*)"";
12373 rettv
->vval
.v_number
= -1;
12376 /* return empty list when there are no matches */
12377 if (rettv_list_alloc(rettv
) == FAIL
)
12380 else if (type
== 2)
12382 rettv
->v_type
= VAR_STRING
;
12383 rettv
->vval
.v_string
= NULL
;
12386 if (argvars
[0].v_type
== VAR_LIST
)
12388 if ((l
= argvars
[0].vval
.v_list
) == NULL
)
12393 expr
= str
= get_tv_string(&argvars
[0]);
12395 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
12399 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12403 start
= get_tv_number_chk(&argvars
[2], &error
);
12408 li
= list_find(l
, start
);
12411 idx
= l
->lv_idx
; /* use the cached index */
12417 if (start
> (long)STRLEN(str
))
12419 /* When "count" argument is there ignore matches before "start",
12420 * otherwise skip part of the string. Differs when pattern is "^"
12422 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
12428 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
12429 nth
= get_tv_number_chk(&argvars
[3], &error
);
12434 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
12435 if (regmatch
.regprog
!= NULL
)
12437 regmatch
.rm_ic
= p_ic
;
12449 str
= echo_string(&li
->li_tv
, &tofree
, strbuf
, 0);
12454 match
= vim_regexec_nl(®match
, str
, (colnr_T
)startcol
);
12456 if (match
&& --nth
<= 0)
12458 if (l
== NULL
&& !match
)
12461 /* Advance to just after the match. */
12470 startcol
= (colnr_T
)(regmatch
.startp
[0]
12471 + (*mb_ptr2len
)(regmatch
.startp
[0]) - str
);
12473 startcol
= regmatch
.startp
[0] + 1 - str
;
12484 /* return list with matched string and submatches */
12485 for (i
= 0; i
< NSUBEXP
; ++i
)
12487 if (regmatch
.endp
[i
] == NULL
)
12489 if (list_append_string(rettv
->vval
.v_list
,
12490 (char_u
*)"", 0) == FAIL
)
12493 else if (list_append_string(rettv
->vval
.v_list
,
12494 regmatch
.startp
[i
],
12495 (int)(regmatch
.endp
[i
] - regmatch
.startp
[i
]))
12500 else if (type
== 2)
12502 /* return matched string */
12504 copy_tv(&li
->li_tv
, rettv
);
12506 rettv
->vval
.v_string
= vim_strnsave(regmatch
.startp
[0],
12507 (int)(regmatch
.endp
[0] - regmatch
.startp
[0]));
12509 else if (l
!= NULL
)
12510 rettv
->vval
.v_number
= idx
;
12514 rettv
->vval
.v_number
=
12515 (varnumber_T
)(regmatch
.startp
[0] - str
);
12517 rettv
->vval
.v_number
=
12518 (varnumber_T
)(regmatch
.endp
[0] - str
);
12519 rettv
->vval
.v_number
+= (varnumber_T
)(str
- expr
);
12522 vim_free(regmatch
.regprog
);
12531 * "match()" function
12534 f_match(argvars
, rettv
)
12538 find_some_match(argvars
, rettv
, 1);
12542 * "matchadd()" function
12545 f_matchadd(argvars
, rettv
)
12549 #ifdef FEAT_SEARCH_EXTRA
12550 char_u buf
[NUMBUFLEN
];
12551 char_u
*grp
= get_tv_string_buf_chk(&argvars
[0], buf
); /* group */
12552 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], buf
); /* pattern */
12553 int prio
= 10; /* default priority */
12557 rettv
->vval
.v_number
= -1;
12559 if (grp
== NULL
|| pat
== NULL
)
12561 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12563 prio
= get_tv_number_chk(&argvars
[2], &error
);
12564 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
12565 id
= get_tv_number_chk(&argvars
[3], &error
);
12569 if (id
>= 1 && id
<= 3)
12571 EMSGN("E798: ID is reserved for \":match\": %ld", id
);
12575 rettv
->vval
.v_number
= match_add(curwin
, grp
, pat
, prio
, id
);
12580 * "matcharg()" function
12583 f_matcharg(argvars
, rettv
)
12587 if (rettv_list_alloc(rettv
) == OK
)
12589 #ifdef FEAT_SEARCH_EXTRA
12590 int id
= get_tv_number(&argvars
[0]);
12593 if (id
>= 1 && id
<= 3)
12595 if ((m
= (matchitem_T
*)get_match(curwin
, id
)) != NULL
)
12597 list_append_string(rettv
->vval
.v_list
,
12598 syn_id2name(m
->hlg_id
), -1);
12599 list_append_string(rettv
->vval
.v_list
, m
->pattern
, -1);
12603 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
12604 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
12612 * "matchdelete()" function
12615 f_matchdelete(argvars
, rettv
)
12619 #ifdef FEAT_SEARCH_EXTRA
12620 rettv
->vval
.v_number
= match_delete(curwin
,
12621 (int)get_tv_number(&argvars
[0]), TRUE
);
12626 * "matchend()" function
12629 f_matchend(argvars
, rettv
)
12633 find_some_match(argvars
, rettv
, 0);
12637 * "matchlist()" function
12640 f_matchlist(argvars
, rettv
)
12644 find_some_match(argvars
, rettv
, 3);
12648 * "matchstr()" function
12651 f_matchstr(argvars
, rettv
)
12655 find_some_match(argvars
, rettv
, 2);
12658 static void max_min
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int domax
));
12661 max_min(argvars
, rettv
, domax
)
12670 if (argvars
[0].v_type
== VAR_LIST
)
12675 l
= argvars
[0].vval
.v_list
;
12681 n
= get_tv_number_chk(&li
->li_tv
, &error
);
12687 i
= get_tv_number_chk(&li
->li_tv
, &error
);
12688 if (domax
? i
> n
: i
< n
)
12694 else if (argvars
[0].v_type
== VAR_DICT
)
12701 d
= argvars
[0].vval
.v_dict
;
12704 todo
= (int)d
->dv_hashtab
.ht_used
;
12705 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
12707 if (!HASHITEM_EMPTY(hi
))
12710 i
= get_tv_number_chk(&HI2DI(hi
)->di_tv
, &error
);
12716 else if (domax
? i
> n
: i
< n
)
12723 EMSG(_(e_listdictarg
));
12724 rettv
->vval
.v_number
= error
? 0 : n
;
12731 f_max(argvars
, rettv
)
12735 max_min(argvars
, rettv
, TRUE
);
12742 f_min(argvars
, rettv
)
12746 max_min(argvars
, rettv
, FALSE
);
12749 static int mkdir_recurse
__ARGS((char_u
*dir
, int prot
));
12752 * Create the directory in which "dir" is located, and higher levels when
12756 mkdir_recurse(dir
, prot
)
12764 /* Get end of directory name in "dir".
12765 * We're done when it's "/" or "c:/". */
12766 p
= gettail_sep(dir
);
12767 if (p
<= get_past_head(dir
))
12770 /* If the directory exists we're done. Otherwise: create it.*/
12771 updir
= vim_strnsave(dir
, (int)(p
- dir
));
12774 if (mch_isdir(updir
))
12776 else if (mkdir_recurse(updir
, prot
) == OK
)
12777 r
= vim_mkdir_emsg(updir
, prot
);
12784 * "mkdir()" function
12787 f_mkdir(argvars
, rettv
)
12792 char_u buf
[NUMBUFLEN
];
12795 rettv
->vval
.v_number
= FAIL
;
12796 if (check_restricted() || check_secure())
12799 dir
= get_tv_string_buf(&argvars
[0], buf
);
12800 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12802 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12803 prot
= get_tv_number_chk(&argvars
[2], NULL
);
12804 if (prot
!= -1 && STRCMP(get_tv_string(&argvars
[1]), "p") == 0)
12805 mkdir_recurse(dir
, prot
);
12807 rettv
->vval
.v_number
= prot
!= -1 ? vim_mkdir_emsg(dir
, prot
) : 0;
12812 * "mode()" function
12816 f_mode(argvars
, rettv
)
12826 buf
[0] = VIsual_mode
+ 's' - 'v';
12828 buf
[0] = VIsual_mode
;
12832 if (State
== HITRETURN
|| State
== ASKMORE
|| State
== SETWSIZE
)
12834 else if (State
& INSERT
)
12836 if (State
& REPLACE_FLAG
)
12841 else if (State
& CMDLINE
)
12847 rettv
->vval
.v_string
= vim_strsave(buf
);
12848 rettv
->v_type
= VAR_STRING
;
12852 * "nextnonblank()" function
12855 f_nextnonblank(argvars
, rettv
)
12861 for (lnum
= get_tv_lnum(argvars
); ; ++lnum
)
12863 if (lnum
< 0 || lnum
> curbuf
->b_ml
.ml_line_count
)
12868 if (*skipwhite(ml_get(lnum
)) != NUL
)
12871 rettv
->vval
.v_number
= lnum
;
12875 * "nr2char()" function
12878 f_nr2char(argvars
, rettv
)
12882 char_u buf
[NUMBUFLEN
];
12886 buf
[(*mb_char2bytes
)((int)get_tv_number(&argvars
[0]), buf
)] = NUL
;
12890 buf
[0] = (char_u
)get_tv_number(&argvars
[0]);
12893 rettv
->v_type
= VAR_STRING
;
12894 rettv
->vval
.v_string
= vim_strsave(buf
);
12898 * "pathshorten()" function
12901 f_pathshorten(argvars
, rettv
)
12907 rettv
->v_type
= VAR_STRING
;
12908 p
= get_tv_string_chk(&argvars
[0]);
12910 rettv
->vval
.v_string
= NULL
;
12913 p
= vim_strsave(p
);
12914 rettv
->vval
.v_string
= p
;
12921 * "prevnonblank()" function
12924 f_prevnonblank(argvars
, rettv
)
12930 lnum
= get_tv_lnum(argvars
);
12931 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
)
12934 while (lnum
>= 1 && *skipwhite(ml_get(lnum
)) == NUL
)
12936 rettv
->vval
.v_number
= lnum
;
12939 #ifdef HAVE_STDARG_H
12940 /* This dummy va_list is here because:
12941 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12942 * - locally in the function results in a "used before set" warning
12943 * - using va_start() to initialize it gives "function with fixed args" error */
12948 * "printf()" function
12951 f_printf(argvars
, rettv
)
12955 rettv
->v_type
= VAR_STRING
;
12956 rettv
->vval
.v_string
= NULL
;
12957 #ifdef HAVE_STDARG_H /* only very old compilers can't do this */
12959 char_u buf
[NUMBUFLEN
];
12962 int saved_did_emsg
= did_emsg
;
12965 /* Get the required length, allocate the buffer and do it for real. */
12967 fmt
= (char *)get_tv_string_buf(&argvars
[0], buf
);
12968 len
= vim_vsnprintf(NULL
, 0, fmt
, ap
, argvars
+ 1);
12971 s
= alloc(len
+ 1);
12974 rettv
->vval
.v_string
= s
;
12975 (void)vim_vsnprintf((char *)s
, len
+ 1, fmt
, ap
, argvars
+ 1);
12978 did_emsg
|= saved_did_emsg
;
12984 * "pumvisible()" function
12988 f_pumvisible(argvars
, rettv
)
12992 rettv
->vval
.v_number
= 0;
12993 #ifdef FEAT_INS_EXPAND
12995 rettv
->vval
.v_number
= 1;
13000 * "range()" function
13003 f_range(argvars
, rettv
)
13013 start
= get_tv_number_chk(&argvars
[0], &error
);
13014 if (argvars
[1].v_type
== VAR_UNKNOWN
)
13021 end
= get_tv_number_chk(&argvars
[1], &error
);
13022 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13023 stride
= get_tv_number_chk(&argvars
[2], &error
);
13026 rettv
->vval
.v_number
= 0;
13028 return; /* type error; errmsg already given */
13030 EMSG(_("E726: Stride is zero"));
13031 else if (stride
> 0 ? end
+ 1 < start
: end
- 1 > start
)
13032 EMSG(_("E727: Start past end"));
13035 if (rettv_list_alloc(rettv
) == OK
)
13036 for (i
= start
; stride
> 0 ? i
<= end
: i
>= end
; i
+= stride
)
13037 if (list_append_number(rettv
->vval
.v_list
,
13038 (varnumber_T
)i
) == FAIL
)
13044 * "readfile()" function
13047 f_readfile(argvars
, rettv
)
13051 int binary
= FALSE
;
13055 #define FREAD_SIZE 200 /* optimized for text lines */
13056 char_u buf
[FREAD_SIZE
];
13057 int readlen
; /* size of last fread() */
13058 int buflen
; /* nr of valid chars in buf[] */
13059 int filtd
; /* how much in buf[] was NUL -> '\n' filtered */
13060 int tolist
; /* first byte in buf[] still to be put in list */
13061 int chop
; /* how many CR to chop off */
13062 char_u
*prev
= NULL
; /* previously read bytes, if any */
13063 int prevlen
= 0; /* length of "prev" if not NULL */
13066 long maxline
= MAXLNUM
;
13069 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
13071 if (STRCMP(get_tv_string(&argvars
[1]), "b") == 0)
13073 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13074 maxline
= get_tv_number(&argvars
[2]);
13077 if (rettv_list_alloc(rettv
) == FAIL
)
13080 /* Always open the file in binary mode, library functions have a mind of
13081 * their own about CR-LF conversion. */
13082 fname
= get_tv_string(&argvars
[0]);
13083 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, READBIN
)) == NULL
)
13085 EMSG2(_(e_notopen
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
13090 while (cnt
< maxline
|| maxline
< 0)
13092 readlen
= (int)fread(buf
+ filtd
, 1, FREAD_SIZE
- filtd
, fd
);
13093 buflen
= filtd
+ readlen
;
13095 for ( ; filtd
< buflen
|| readlen
<= 0; ++filtd
)
13097 if (buf
[filtd
] == '\n' || readlen
<= 0)
13099 /* Only when in binary mode add an empty list item when the
13100 * last line ends in a '\n'. */
13101 if (!binary
&& readlen
== 0 && filtd
== 0)
13104 /* Found end-of-line or end-of-file: add a text line to the
13108 while (filtd
- chop
- 1 >= tolist
13109 && buf
[filtd
- chop
- 1] == '\r')
13111 len
= filtd
- tolist
- chop
;
13113 s
= vim_strnsave(buf
+ tolist
, len
);
13116 s
= alloc((unsigned)(prevlen
+ len
+ 1));
13119 mch_memmove(s
, prev
, prevlen
);
13122 mch_memmove(s
+ prevlen
, buf
+ tolist
, len
);
13123 s
[prevlen
+ len
] = NUL
;
13126 tolist
= filtd
+ 1;
13128 li
= listitem_alloc();
13134 li
->li_tv
.v_type
= VAR_STRING
;
13135 li
->li_tv
.v_lock
= 0;
13136 li
->li_tv
.vval
.v_string
= s
;
13137 list_append(rettv
->vval
.v_list
, li
);
13139 if (++cnt
>= maxline
&& maxline
>= 0)
13144 else if (buf
[filtd
] == NUL
)
13152 /* "buf" is full, need to move text to an allocated buffer */
13155 prev
= vim_strnsave(buf
, buflen
);
13160 s
= alloc((unsigned)(prevlen
+ buflen
));
13163 mch_memmove(s
, prev
, prevlen
);
13164 mch_memmove(s
+ prevlen
, buf
, buflen
);
13174 mch_memmove(buf
, buf
+ tolist
, buflen
- tolist
);
13180 * For a negative line count use only the lines at the end of the file,
13184 while (cnt
> -maxline
)
13186 listitem_remove(rettv
->vval
.v_list
, rettv
->vval
.v_list
->lv_first
);
13194 #if defined(FEAT_RELTIME)
13195 static int list2proftime
__ARGS((typval_T
*arg
, proftime_T
*tm
));
13198 * Convert a List to proftime_T.
13199 * Return FAIL when there is something wrong.
13202 list2proftime(arg
, tm
)
13209 if (arg
->v_type
!= VAR_LIST
|| arg
->vval
.v_list
== NULL
13210 || arg
->vval
.v_list
->lv_len
!= 2)
13212 n1
= list_find_nr(arg
->vval
.v_list
, 0L, &error
);
13213 n2
= list_find_nr(arg
->vval
.v_list
, 1L, &error
);
13221 return error
? FAIL
: OK
;
13223 #endif /* FEAT_RELTIME */
13226 * "reltime()" function
13229 f_reltime(argvars
, rettv
)
13233 #ifdef FEAT_RELTIME
13237 if (argvars
[0].v_type
== VAR_UNKNOWN
)
13239 /* No arguments: get current time. */
13240 profile_start(&res
);
13242 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
13244 if (list2proftime(&argvars
[0], &res
) == FAIL
)
13250 /* Two arguments: compute the difference. */
13251 if (list2proftime(&argvars
[0], &start
) == FAIL
13252 || list2proftime(&argvars
[1], &res
) == FAIL
)
13254 profile_sub(&res
, &start
);
13257 if (rettv_list_alloc(rettv
) == OK
)
13268 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n1
);
13269 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n2
);
13275 * "reltimestr()" function
13278 f_reltimestr(argvars
, rettv
)
13282 #ifdef FEAT_RELTIME
13286 rettv
->v_type
= VAR_STRING
;
13287 rettv
->vval
.v_string
= NULL
;
13288 #ifdef FEAT_RELTIME
13289 if (list2proftime(&argvars
[0], &tm
) == OK
)
13290 rettv
->vval
.v_string
= vim_strsave((char_u
*)profile_msg(&tm
));
13294 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13295 static void make_connection
__ARGS((void));
13296 static int check_connection
__ARGS((void));
13301 if (X_DISPLAY
== NULL
13307 x_force_connect
= TRUE
;
13309 x_force_connect
= FALSE
;
13317 if (X_DISPLAY
== NULL
)
13319 EMSG(_("E240: No connection to Vim server"));
13326 #ifdef FEAT_CLIENTSERVER
13327 static void remote_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int expr
));
13330 remote_common(argvars
, rettv
, expr
)
13335 char_u
*server_name
;
13338 char_u buf
[NUMBUFLEN
];
13345 if (check_restricted() || check_secure())
13349 if (check_connection() == FAIL
)
13353 server_name
= get_tv_string_chk(&argvars
[0]);
13354 if (server_name
== NULL
)
13355 return; /* type error; errmsg already given */
13356 keys
= get_tv_string_buf(&argvars
[1], buf
);
13358 if (serverSendToVim(server_name
, keys
, &r
, &w
, expr
, TRUE
) < 0)
13360 if (serverSendToVim(X_DISPLAY
, server_name
, keys
, &r
, &w
, expr
, 0, TRUE
)
13365 EMSG(r
); /* sending worked but evaluation failed */
13367 EMSG2(_("E241: Unable to send to %s"), server_name
);
13371 rettv
->vval
.v_string
= r
;
13373 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13379 sprintf((char *)str
, PRINTF_HEX_LONG_U
, (long_u
)w
);
13380 v
.di_tv
.v_type
= VAR_STRING
;
13381 v
.di_tv
.vval
.v_string
= vim_strsave(str
);
13382 idvar
= get_tv_string_chk(&argvars
[2]);
13384 set_var(idvar
, &v
.di_tv
, FALSE
);
13385 vim_free(v
.di_tv
.vval
.v_string
);
13391 * "remote_expr()" function
13395 f_remote_expr(argvars
, rettv
)
13399 rettv
->v_type
= VAR_STRING
;
13400 rettv
->vval
.v_string
= NULL
;
13401 #ifdef FEAT_CLIENTSERVER
13402 remote_common(argvars
, rettv
, TRUE
);
13407 * "remote_foreground()" function
13411 f_remote_foreground(argvars
, rettv
)
13415 rettv
->vval
.v_number
= 0;
13416 #ifdef FEAT_CLIENTSERVER
13418 /* On Win32 it's done in this application. */
13420 char_u
*server_name
= get_tv_string_chk(&argvars
[0]);
13422 if (server_name
!= NULL
)
13423 serverForeground(server_name
);
13426 /* Send a foreground() expression to the server. */
13427 argvars
[1].v_type
= VAR_STRING
;
13428 argvars
[1].vval
.v_string
= vim_strsave((char_u
*)"foreground()");
13429 argvars
[2].v_type
= VAR_UNKNOWN
;
13430 remote_common(argvars
, rettv
, TRUE
);
13431 vim_free(argvars
[1].vval
.v_string
);
13438 f_remote_peek(argvars
, rettv
)
13442 #ifdef FEAT_CLIENTSERVER
13450 if (check_restricted() || check_secure())
13452 rettv
->vval
.v_number
= -1;
13455 serverid
= get_tv_string_chk(&argvars
[0]);
13456 if (serverid
== NULL
)
13458 rettv
->vval
.v_number
= -1;
13459 return; /* type error; errmsg already given */
13462 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
13464 rettv
->vval
.v_number
= -1;
13467 s
= serverGetReply((HWND
)n
, FALSE
, FALSE
, FALSE
);
13468 rettv
->vval
.v_number
= (s
!= NULL
);
13471 rettv
->vval
.v_number
= 0;
13472 if (check_connection() == FAIL
)
13475 rettv
->vval
.v_number
= serverPeekReply(X_DISPLAY
,
13476 serverStrToWin(serverid
), &s
);
13479 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& rettv
->vval
.v_number
> 0)
13483 v
.di_tv
.v_type
= VAR_STRING
;
13484 v
.di_tv
.vval
.v_string
= vim_strsave(s
);
13485 retvar
= get_tv_string_chk(&argvars
[1]);
13486 if (retvar
!= NULL
)
13487 set_var(retvar
, &v
.di_tv
, FALSE
);
13488 vim_free(v
.di_tv
.vval
.v_string
);
13491 rettv
->vval
.v_number
= -1;
13497 f_remote_read(argvars
, rettv
)
13503 #ifdef FEAT_CLIENTSERVER
13504 char_u
*serverid
= get_tv_string_chk(&argvars
[0]);
13506 if (serverid
!= NULL
&& !check_restricted() && !check_secure())
13509 /* The server's HWND is encoded in the 'id' parameter */
13512 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
13514 r
= serverGetReply((HWND
)n
, FALSE
, TRUE
, TRUE
);
13517 if (check_connection() == FAIL
|| serverReadReply(X_DISPLAY
,
13518 serverStrToWin(serverid
), &r
, FALSE
) < 0)
13520 EMSG(_("E277: Unable to read a server reply"));
13523 rettv
->v_type
= VAR_STRING
;
13524 rettv
->vval
.v_string
= r
;
13528 * "remote_send()" function
13532 f_remote_send(argvars
, rettv
)
13536 rettv
->v_type
= VAR_STRING
;
13537 rettv
->vval
.v_string
= NULL
;
13538 #ifdef FEAT_CLIENTSERVER
13539 remote_common(argvars
, rettv
, FALSE
);
13544 * "remove()" function
13547 f_remove(argvars
, rettv
)
13552 listitem_T
*item
, *item2
;
13560 rettv
->vval
.v_number
= 0;
13561 if (argvars
[0].v_type
== VAR_DICT
)
13563 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13564 EMSG2(_(e_toomanyarg
), "remove()");
13565 else if ((d
= argvars
[0].vval
.v_dict
) != NULL
13566 && !tv_check_lock(d
->dv_lock
, (char_u
*)"remove() argument"))
13568 key
= get_tv_string_chk(&argvars
[1]);
13571 di
= dict_find(d
, key
, -1);
13573 EMSG2(_(e_dictkey
), key
);
13576 *rettv
= di
->di_tv
;
13577 init_tv(&di
->di_tv
);
13578 dictitem_remove(d
, di
);
13583 else if (argvars
[0].v_type
!= VAR_LIST
)
13584 EMSG2(_(e_listdictarg
), "remove()");
13585 else if ((l
= argvars
[0].vval
.v_list
) != NULL
13586 && !tv_check_lock(l
->lv_lock
, (char_u
*)"remove() argument"))
13590 idx
= get_tv_number_chk(&argvars
[1], &error
);
13592 ; /* type error: do nothing, errmsg already given */
13593 else if ((item
= list_find(l
, idx
)) == NULL
)
13594 EMSGN(_(e_listidx
), idx
);
13597 if (argvars
[2].v_type
== VAR_UNKNOWN
)
13599 /* Remove one item, return its value. */
13600 list_remove(l
, item
, item
);
13601 *rettv
= item
->li_tv
;
13606 /* Remove range of items, return list with values. */
13607 end
= get_tv_number_chk(&argvars
[2], &error
);
13609 ; /* type error: do nothing */
13610 else if ((item2
= list_find(l
, end
)) == NULL
)
13611 EMSGN(_(e_listidx
), end
);
13616 for (li
= item
; li
!= NULL
; li
= li
->li_next
)
13622 if (li
== NULL
) /* didn't find "item2" after "item" */
13623 EMSG(_(e_invrange
));
13626 list_remove(l
, item
, item2
);
13627 if (rettv_list_alloc(rettv
) == OK
)
13629 l
= rettv
->vval
.v_list
;
13630 l
->lv_first
= item
;
13631 l
->lv_last
= item2
;
13632 item
->li_prev
= NULL
;
13633 item2
->li_next
= NULL
;
13644 * "rename({from}, {to})" function
13647 f_rename(argvars
, rettv
)
13651 char_u buf
[NUMBUFLEN
];
13653 if (check_restricted() || check_secure())
13654 rettv
->vval
.v_number
= -1;
13656 rettv
->vval
.v_number
= vim_rename(get_tv_string(&argvars
[0]),
13657 get_tv_string_buf(&argvars
[1], buf
));
13661 * "repeat()" function
13665 f_repeat(argvars
, rettv
)
13676 n
= get_tv_number(&argvars
[1]);
13677 if (argvars
[0].v_type
== VAR_LIST
)
13679 if (rettv_list_alloc(rettv
) == OK
&& argvars
[0].vval
.v_list
!= NULL
)
13681 if (list_extend(rettv
->vval
.v_list
,
13682 argvars
[0].vval
.v_list
, NULL
) == FAIL
)
13687 p
= get_tv_string(&argvars
[0]);
13688 rettv
->v_type
= VAR_STRING
;
13689 rettv
->vval
.v_string
= NULL
;
13691 slen
= (int)STRLEN(p
);
13696 r
= alloc(len
+ 1);
13699 for (i
= 0; i
< n
; i
++)
13700 mch_memmove(r
+ i
* slen
, p
, (size_t)slen
);
13704 rettv
->vval
.v_string
= r
;
13709 * "resolve()" function
13712 f_resolve(argvars
, rettv
)
13718 p
= get_tv_string(&argvars
[0]);
13719 #ifdef FEAT_SHORTCUT
13723 v
= mch_resolve_shortcut(p
);
13725 rettv
->vval
.v_string
= v
;
13727 rettv
->vval
.v_string
= vim_strsave(p
);
13730 # ifdef HAVE_READLINK
13732 char_u buf
[MAXPATHL
+ 1];
13735 char_u
*remain
= NULL
;
13737 int is_relative_to_current
= FALSE
;
13738 int has_trailing_pathsep
= FALSE
;
13741 p
= vim_strsave(p
);
13743 if (p
[0] == '.' && (vim_ispathsep(p
[1])
13744 || (p
[1] == '.' && (vim_ispathsep(p
[2])))))
13745 is_relative_to_current
= TRUE
;
13748 if (len
> 0 && after_pathsep(p
, p
+ len
))
13749 has_trailing_pathsep
= TRUE
;
13751 q
= getnextcomp(p
);
13754 /* Separate the first path component in "p", and keep the
13755 * remainder (beginning with the path separator). */
13756 remain
= vim_strsave(q
- 1);
13764 len
= readlink((char *)p
, (char *)buf
, MAXPATHL
);
13773 EMSG(_("E655: Too many symbolic links (cycle?)"));
13774 rettv
->vval
.v_string
= NULL
;
13778 /* Ensure that the result will have a trailing path separator
13779 * if the argument has one. */
13780 if (remain
== NULL
&& has_trailing_pathsep
)
13783 /* Separate the first path component in the link value and
13784 * concatenate the remainders. */
13785 q
= getnextcomp(vim_ispathsep(*buf
) ? buf
+ 1 : buf
);
13788 if (remain
== NULL
)
13789 remain
= vim_strsave(q
- 1);
13792 cpy
= concat_str(q
- 1, remain
);
13803 if (q
> p
&& *q
== NUL
)
13805 /* Ignore trailing path separator. */
13809 if (q
> p
&& !mch_isFullName(buf
))
13811 /* symlink is relative to directory of argument */
13812 cpy
= alloc((unsigned)(STRLEN(p
) + STRLEN(buf
) + 1));
13816 STRCPY(gettail(cpy
), buf
);
13824 p
= vim_strsave(buf
);
13828 if (remain
== NULL
)
13831 /* Append the first path component of "remain" to "p". */
13832 q
= getnextcomp(remain
+ 1);
13833 len
= q
- remain
- (*q
!= NUL
);
13834 cpy
= vim_strnsave(p
, STRLEN(p
) + len
);
13837 STRNCAT(cpy
, remain
, len
);
13841 /* Shorten "remain". */
13843 mch_memmove(remain
, q
- 1, STRLEN(q
- 1) + 1);
13851 /* If the result is a relative path name, make it explicitly relative to
13852 * the current directory if and only if the argument had this form. */
13853 if (!vim_ispathsep(*p
))
13855 if (is_relative_to_current
13859 || vim_ispathsep(p
[1])
13862 || vim_ispathsep(p
[2]))))))
13864 /* Prepend "./". */
13865 cpy
= concat_str((char_u
*)"./", p
);
13872 else if (!is_relative_to_current
)
13874 /* Strip leading "./". */
13876 while (q
[0] == '.' && vim_ispathsep(q
[1]))
13879 mch_memmove(p
, p
+ 2, STRLEN(p
+ 2) + (size_t)1);
13883 /* Ensure that the result will have no trailing path separator
13884 * if the argument had none. But keep "/" or "//". */
13885 if (!has_trailing_pathsep
)
13888 if (after_pathsep(p
, q
))
13889 *gettail_sep(p
) = NUL
;
13892 rettv
->vval
.v_string
= p
;
13895 rettv
->vval
.v_string
= vim_strsave(p
);
13899 simplify_filename(rettv
->vval
.v_string
);
13901 #ifdef HAVE_READLINK
13904 rettv
->v_type
= VAR_STRING
;
13908 * "reverse({list})" function
13911 f_reverse(argvars
, rettv
)
13916 listitem_T
*li
, *ni
;
13918 rettv
->vval
.v_number
= 0;
13919 if (argvars
[0].v_type
!= VAR_LIST
)
13920 EMSG2(_(e_listarg
), "reverse()");
13921 else if ((l
= argvars
[0].vval
.v_list
) != NULL
13922 && !tv_check_lock(l
->lv_lock
, (char_u
*)"reverse()"))
13925 l
->lv_first
= l
->lv_last
= NULL
;
13930 list_append(l
, li
);
13933 rettv
->vval
.v_list
= l
;
13934 rettv
->v_type
= VAR_LIST
;
13939 #define SP_NOMOVE 0x01 /* don't move cursor */
13940 #define SP_REPEAT 0x02 /* repeat to find outer pair */
13941 #define SP_RETCOUNT 0x04 /* return matchcount */
13942 #define SP_SETPCMARK 0x08 /* set previous context mark */
13943 #define SP_START 0x10 /* accept match at start position */
13944 #define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13945 #define SP_END 0x40 /* leave cursor at end of match */
13947 static int get_search_arg
__ARGS((typval_T
*varp
, int *flagsp
));
13950 * Get flags for a search function.
13951 * Possibly sets "p_ws".
13952 * Returns BACKWARD, FORWARD or zero (for an error).
13955 get_search_arg(varp
, flagsp
)
13961 char_u nbuf
[NUMBUFLEN
];
13964 if (varp
->v_type
!= VAR_UNKNOWN
)
13966 flags
= get_tv_string_buf_chk(varp
, nbuf
);
13968 return 0; /* type error; errmsg already given */
13969 while (*flags
!= NUL
)
13973 case 'b': dir
= BACKWARD
; break;
13974 case 'w': p_ws
= TRUE
; break;
13975 case 'W': p_ws
= FALSE
; break;
13977 if (flagsp
!= NULL
)
13980 case 'c': mask
= SP_START
; break;
13981 case 'e': mask
= SP_END
; break;
13982 case 'm': mask
= SP_RETCOUNT
; break;
13983 case 'n': mask
= SP_NOMOVE
; break;
13984 case 'p': mask
= SP_SUBPAT
; break;
13985 case 'r': mask
= SP_REPEAT
; break;
13986 case 's': mask
= SP_SETPCMARK
; break;
13990 EMSG2(_(e_invarg2
), flags
);
14005 * Shared by search() and searchpos() functions
14008 search_cmn(argvars
, match_pos
, flagsp
)
14017 int save_p_ws
= p_ws
;
14019 int retval
= 0; /* default: FAIL */
14020 long lnum_stop
= 0;
14021 int options
= SEARCH_KEEP
;
14024 pat
= get_tv_string(&argvars
[0]);
14025 dir
= get_search_arg(&argvars
[1], flagsp
); /* may set p_ws */
14029 if (flags
& SP_START
)
14030 options
|= SEARCH_START
;
14031 if (flags
& SP_END
)
14032 options
|= SEARCH_END
;
14034 /* Optional extra argument: line number to stop searching. */
14035 if (argvars
[1].v_type
!= VAR_UNKNOWN
14036 && argvars
[2].v_type
!= VAR_UNKNOWN
)
14038 lnum_stop
= get_tv_number_chk(&argvars
[2], NULL
);
14044 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
14045 * Check to make sure only those flags are set.
14046 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14047 * flags cannot be set. Check for that condition also.
14049 if (((flags
& (SP_REPEAT
| SP_RETCOUNT
)) != 0)
14050 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
14052 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[1]));
14056 pos
= save_cursor
= curwin
->w_cursor
;
14057 subpatnum
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
14058 options
, RE_SEARCH
, (linenr_T
)lnum_stop
);
14059 if (subpatnum
!= FAIL
)
14061 if (flags
& SP_SUBPAT
)
14062 retval
= subpatnum
;
14065 if (flags
& SP_SETPCMARK
)
14067 curwin
->w_cursor
= pos
;
14068 if (match_pos
!= NULL
)
14070 /* Store the match cursor position */
14071 match_pos
->lnum
= pos
.lnum
;
14072 match_pos
->col
= pos
.col
+ 1;
14074 /* "/$" will put the cursor after the end of the line, may need to
14075 * correct that here */
14079 /* If 'n' flag is used: restore cursor position. */
14080 if (flags
& SP_NOMOVE
)
14081 curwin
->w_cursor
= save_cursor
;
14083 curwin
->w_set_curswant
= TRUE
;
14091 * "search()" function
14094 f_search(argvars
, rettv
)
14100 rettv
->vval
.v_number
= search_cmn(argvars
, NULL
, &flags
);
14104 * "searchdecl()" function
14107 f_searchdecl(argvars
, rettv
)
14116 rettv
->vval
.v_number
= 1; /* default: FAIL */
14118 name
= get_tv_string_chk(&argvars
[0]);
14119 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
14121 locally
= get_tv_number_chk(&argvars
[1], &error
) == 0;
14122 if (!error
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
14123 thisblock
= get_tv_number_chk(&argvars
[2], &error
) != 0;
14125 if (!error
&& name
!= NULL
)
14126 rettv
->vval
.v_number
= find_decl(name
, (int)STRLEN(name
),
14127 locally
, thisblock
, SEARCH_KEEP
) == FAIL
;
14131 * Used by searchpair() and searchpairpos()
14134 searchpair_cmn(argvars
, match_pos
)
14138 char_u
*spat
, *mpat
, *epat
;
14140 int save_p_ws
= p_ws
;
14143 char_u nbuf1
[NUMBUFLEN
];
14144 char_u nbuf2
[NUMBUFLEN
];
14145 char_u nbuf3
[NUMBUFLEN
];
14146 int retval
= 0; /* default: FAIL */
14147 long lnum_stop
= 0;
14149 /* Get the three pattern arguments: start, middle, end. */
14150 spat
= get_tv_string_chk(&argvars
[0]);
14151 mpat
= get_tv_string_buf_chk(&argvars
[1], nbuf1
);
14152 epat
= get_tv_string_buf_chk(&argvars
[2], nbuf2
);
14153 if (spat
== NULL
|| mpat
== NULL
|| epat
== NULL
)
14154 goto theend
; /* type error */
14156 /* Handle the optional fourth argument: flags */
14157 dir
= get_search_arg(&argvars
[3], &flags
); /* may set p_ws */
14161 /* Don't accept SP_END or SP_SUBPAT.
14162 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14164 if ((flags
& (SP_END
| SP_SUBPAT
)) != 0
14165 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
14167 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[3]));
14171 /* Optional fifth argument: skip expression */
14172 if (argvars
[3].v_type
== VAR_UNKNOWN
14173 || argvars
[4].v_type
== VAR_UNKNOWN
)
14174 skip
= (char_u
*)"";
14177 skip
= get_tv_string_buf_chk(&argvars
[4], nbuf3
);
14178 if (argvars
[5].v_type
!= VAR_UNKNOWN
)
14180 lnum_stop
= get_tv_number_chk(&argvars
[5], NULL
);
14186 goto theend
; /* type error */
14188 retval
= do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
,
14189 match_pos
, lnum_stop
);
14198 * "searchpair()" function
14201 f_searchpair(argvars
, rettv
)
14205 rettv
->vval
.v_number
= searchpair_cmn(argvars
, NULL
);
14209 * "searchpairpos()" function
14212 f_searchpairpos(argvars
, rettv
)
14220 rettv
->vval
.v_number
= 0;
14222 if (rettv_list_alloc(rettv
) == FAIL
)
14225 if (searchpair_cmn(argvars
, &match_pos
) > 0)
14227 lnum
= match_pos
.lnum
;
14228 col
= match_pos
.col
;
14231 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
14232 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
14236 * Search for a start/middle/end thing.
14237 * Used by searchpair(), see its documentation for the details.
14238 * Returns 0 or -1 for no match,
14241 do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
, match_pos
, lnum_stop
)
14242 char_u
*spat
; /* start pattern */
14243 char_u
*mpat
; /* middle pattern */
14244 char_u
*epat
; /* end pattern */
14245 int dir
; /* BACKWARD or FORWARD */
14246 char_u
*skip
; /* skip expression */
14247 int flags
; /* SP_SETPCMARK and other SP_ values */
14249 linenr_T lnum_stop
; /* stop at this line if not zero */
14252 char_u
*pat
, *pat2
= NULL
, *pat3
= NULL
;
14263 int options
= SEARCH_KEEP
;
14265 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14267 p_cpo
= (char_u
*)"";
14269 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14270 * start/middle/end (pat3, for the top pair). */
14271 pat2
= alloc((unsigned)(STRLEN(spat
) + STRLEN(epat
) + 15));
14272 pat3
= alloc((unsigned)(STRLEN(spat
) + STRLEN(mpat
) + STRLEN(epat
) + 23));
14273 if (pat2
== NULL
|| pat3
== NULL
)
14275 sprintf((char *)pat2
, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat
, epat
);
14277 STRCPY(pat3
, pat2
);
14279 sprintf((char *)pat3
, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14281 if (flags
& SP_START
)
14282 options
|= SEARCH_START
;
14284 save_cursor
= curwin
->w_cursor
;
14285 pos
= curwin
->w_cursor
;
14286 clearpos(&firstpos
);
14287 clearpos(&foundpos
);
14291 n
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
14292 options
, RE_SEARCH
, lnum_stop
);
14293 if (n
== FAIL
|| (firstpos
.lnum
!= 0 && equalpos(pos
, firstpos
)))
14294 /* didn't find it or found the first match again: FAIL */
14297 if (firstpos
.lnum
== 0)
14299 if (equalpos(pos
, foundpos
))
14301 /* Found the same position again. Can happen with a pattern that
14302 * has "\zs" at the end and searching backwards. Advance one
14303 * character and try again. */
14304 if (dir
== BACKWARD
)
14311 /* If the skip pattern matches, ignore this match. */
14314 save_pos
= curwin
->w_cursor
;
14315 curwin
->w_cursor
= pos
;
14316 r
= eval_to_bool(skip
, &err
, NULL
, FALSE
);
14317 curwin
->w_cursor
= save_pos
;
14320 /* Evaluating {skip} caused an error, break here. */
14321 curwin
->w_cursor
= save_cursor
;
14329 if ((dir
== BACKWARD
&& n
== 3) || (dir
== FORWARD
&& n
== 2))
14331 /* Found end when searching backwards or start when searching
14332 * forward: nested pair. */
14334 pat
= pat2
; /* nested, don't search for middle */
14338 /* Found end when searching forward or start when searching
14339 * backward: end of (nested) pair; or found middle in outer pair. */
14341 pat
= pat3
; /* outer level, search for middle */
14346 /* Found the match: return matchcount or line number. */
14347 if (flags
& SP_RETCOUNT
)
14351 if (flags
& SP_SETPCMARK
)
14353 curwin
->w_cursor
= pos
;
14354 if (!(flags
& SP_REPEAT
))
14356 nest
= 1; /* search for next unmatched */
14360 if (match_pos
!= NULL
)
14362 /* Store the match cursor position */
14363 match_pos
->lnum
= curwin
->w_cursor
.lnum
;
14364 match_pos
->col
= curwin
->w_cursor
.col
+ 1;
14367 /* If 'n' flag is used or search failed: restore cursor position. */
14368 if ((flags
& SP_NOMOVE
) || retval
== 0)
14369 curwin
->w_cursor
= save_cursor
;
14380 * "searchpos()" function
14383 f_searchpos(argvars
, rettv
)
14393 rettv
->vval
.v_number
= 0;
14395 if (rettv_list_alloc(rettv
) == FAIL
)
14398 n
= search_cmn(argvars
, &match_pos
, &flags
);
14401 lnum
= match_pos
.lnum
;
14402 col
= match_pos
.col
;
14405 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
14406 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
14407 if (flags
& SP_SUBPAT
)
14408 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n
);
14414 f_server2client(argvars
, rettv
)
14418 #ifdef FEAT_CLIENTSERVER
14419 char_u buf
[NUMBUFLEN
];
14420 char_u
*server
= get_tv_string_chk(&argvars
[0]);
14421 char_u
*reply
= get_tv_string_buf_chk(&argvars
[1], buf
);
14423 rettv
->vval
.v_number
= -1;
14424 if (server
== NULL
|| reply
== NULL
)
14426 if (check_restricted() || check_secure())
14429 if (check_connection() == FAIL
)
14433 if (serverSendReply(server
, reply
) < 0)
14435 EMSG(_("E258: Unable to send to client"));
14438 rettv
->vval
.v_number
= 0;
14440 rettv
->vval
.v_number
= -1;
14446 f_serverlist(argvars
, rettv
)
14452 #ifdef FEAT_CLIENTSERVER
14454 r
= serverGetVimNames();
14457 if (X_DISPLAY
!= NULL
)
14458 r
= serverGetVimNames(X_DISPLAY
);
14461 rettv
->v_type
= VAR_STRING
;
14462 rettv
->vval
.v_string
= r
;
14466 * "setbufvar()" function
14470 f_setbufvar(argvars
, rettv
)
14476 char_u
*varname
, *bufvarname
;
14478 char_u nbuf
[NUMBUFLEN
];
14480 rettv
->vval
.v_number
= 0;
14482 if (check_restricted() || check_secure())
14484 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
14485 varname
= get_tv_string_chk(&argvars
[1]);
14486 buf
= get_buf_tv(&argvars
[0]);
14487 varp
= &argvars
[2];
14489 if (buf
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
14491 /* set curbuf to be our buf, temporarily */
14492 aucmd_prepbuf(&aco
, buf
);
14494 if (*varname
== '&')
14501 numval
= get_tv_number_chk(varp
, &error
);
14502 strval
= get_tv_string_buf_chk(varp
, nbuf
);
14503 if (!error
&& strval
!= NULL
)
14504 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
14508 bufvarname
= alloc((unsigned)STRLEN(varname
) + 3);
14509 if (bufvarname
!= NULL
)
14511 STRCPY(bufvarname
, "b:");
14512 STRCPY(bufvarname
+ 2, varname
);
14513 set_var(bufvarname
, varp
, TRUE
);
14514 vim_free(bufvarname
);
14518 /* reset notion of buffer */
14519 aucmd_restbuf(&aco
);
14524 * "setcmdpos()" function
14527 f_setcmdpos(argvars
, rettv
)
14531 int pos
= (int)get_tv_number(&argvars
[0]) - 1;
14534 rettv
->vval
.v_number
= set_cmdline_pos(pos
);
14538 * "setline()" function
14541 f_setline(argvars
, rettv
)
14546 char_u
*line
= NULL
;
14548 listitem_T
*li
= NULL
;
14550 linenr_T lcount
= curbuf
->b_ml
.ml_line_count
;
14552 lnum
= get_tv_lnum(&argvars
[0]);
14553 if (argvars
[1].v_type
== VAR_LIST
)
14555 l
= argvars
[1].vval
.v_list
;
14559 line
= get_tv_string_chk(&argvars
[1]);
14561 rettv
->vval
.v_number
= 0; /* OK */
14566 /* list argument, get next string */
14569 line
= get_tv_string_chk(&li
->li_tv
);
14573 rettv
->vval
.v_number
= 1; /* FAIL */
14574 if (line
== NULL
|| lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
14576 if (lnum
<= curbuf
->b_ml
.ml_line_count
)
14578 /* existing line, replace it */
14579 if (u_savesub(lnum
) == OK
&& ml_replace(lnum
, line
, TRUE
) == OK
)
14581 changed_bytes(lnum
, 0);
14582 if (lnum
== curwin
->w_cursor
.lnum
)
14583 check_cursor_col();
14584 rettv
->vval
.v_number
= 0; /* OK */
14587 else if (added
> 0 || u_save(lnum
- 1, lnum
) == OK
)
14589 /* lnum is one past the last line, append the line */
14591 if (ml_append(lnum
- 1, line
, (colnr_T
)0, FALSE
) == OK
)
14592 rettv
->vval
.v_number
= 0; /* OK */
14595 if (l
== NULL
) /* only one string argument */
14601 appended_lines_mark(lcount
, added
);
14605 * Used by "setqflist()" and "setloclist()" functions
14609 set_qf_ll_list(wp
, list_arg
, action_arg
, rettv
)
14611 typval_T
*list_arg
;
14612 typval_T
*action_arg
;
14615 #ifdef FEAT_QUICKFIX
14620 rettv
->vval
.v_number
= -1;
14622 #ifdef FEAT_QUICKFIX
14623 if (list_arg
->v_type
!= VAR_LIST
)
14624 EMSG(_(e_listreq
));
14627 list_T
*l
= list_arg
->vval
.v_list
;
14629 if (action_arg
->v_type
== VAR_STRING
)
14631 act
= get_tv_string_chk(action_arg
);
14633 return; /* type error; errmsg already given */
14634 if (*act
== 'a' || *act
== 'r')
14638 if (l
!= NULL
&& set_errorlist(wp
, l
, action
) == OK
)
14639 rettv
->vval
.v_number
= 0;
14645 * "setloclist()" function
14649 f_setloclist(argvars
, rettv
)
14655 rettv
->vval
.v_number
= -1;
14657 win
= find_win_by_nr(&argvars
[0], NULL
);
14659 set_qf_ll_list(win
, &argvars
[1], &argvars
[2], rettv
);
14663 * "setmatches()" function
14666 f_setmatches(argvars
, rettv
)
14670 #ifdef FEAT_SEARCH_EXTRA
14675 rettv
->vval
.v_number
= -1;
14676 if (argvars
[0].v_type
!= VAR_LIST
)
14678 EMSG(_(e_listreq
));
14681 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
14684 /* To some extent make sure that we are dealing with a list from
14685 * "getmatches()". */
14689 if (li
->li_tv
.v_type
!= VAR_DICT
14690 || (d
= li
->li_tv
.vval
.v_dict
) == NULL
)
14695 if (!(dict_find(d
, (char_u
*)"group", -1) != NULL
14696 && dict_find(d
, (char_u
*)"pattern", -1) != NULL
14697 && dict_find(d
, (char_u
*)"priority", -1) != NULL
14698 && dict_find(d
, (char_u
*)"id", -1) != NULL
))
14706 clear_matches(curwin
);
14710 d
= li
->li_tv
.vval
.v_dict
;
14711 match_add(curwin
, get_dict_string(d
, (char_u
*)"group", FALSE
),
14712 get_dict_string(d
, (char_u
*)"pattern", FALSE
),
14713 (int)get_dict_number(d
, (char_u
*)"priority"),
14714 (int)get_dict_number(d
, (char_u
*)"id"));
14717 rettv
->vval
.v_number
= 0;
14723 * "setpos()" function
14727 f_setpos(argvars
, rettv
)
14735 name
= get_tv_string_chk(argvars
);
14738 if (list2fpos(&argvars
[1], &pos
, &fnum
) == OK
)
14741 if (name
[0] == '.') /* cursor */
14743 if (fnum
== curbuf
->b_fnum
)
14745 curwin
->w_cursor
= pos
;
14751 else if (name
[0] == '\'') /* mark */
14752 (void)setmark_pos(name
[1], &pos
, fnum
);
14760 * "setqflist()" function
14764 f_setqflist(argvars
, rettv
)
14768 set_qf_ll_list(NULL
, &argvars
[0], &argvars
[1], rettv
);
14772 * "setreg()" function
14775 f_setreg(argvars
, rettv
)
14780 char_u
*strregname
;
14791 strregname
= get_tv_string_chk(argvars
);
14792 rettv
->vval
.v_number
= 1; /* FAIL is default */
14794 if (strregname
== NULL
)
14795 return; /* type error; errmsg already given */
14796 regname
= *strregname
;
14797 if (regname
== 0 || regname
== '@')
14799 else if (regname
== '=')
14802 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14804 stropt
= get_tv_string_chk(&argvars
[2]);
14805 if (stropt
== NULL
)
14806 return; /* type error */
14807 for (; *stropt
!= NUL
; ++stropt
)
14810 case 'a': case 'A': /* append */
14813 case 'v': case 'c': /* character-wise selection */
14816 case 'V': case 'l': /* line-wise selection */
14820 case 'b': case Ctrl_V
: /* block-wise selection */
14821 yank_type
= MBLOCK
;
14822 if (VIM_ISDIGIT(stropt
[1]))
14825 block_len
= getdigits(&stropt
) - 1;
14833 strval
= get_tv_string_chk(&argvars
[1]);
14834 if (strval
!= NULL
)
14835 write_reg_contents_ex(regname
, strval
, -1,
14836 append
, yank_type
, block_len
);
14837 rettv
->vval
.v_number
= 0;
14841 * "settabwinvar()" function
14844 f_settabwinvar(argvars
, rettv
)
14848 setwinvar(argvars
, rettv
, 1);
14852 * "setwinvar()" function
14855 f_setwinvar(argvars
, rettv
)
14859 setwinvar(argvars
, rettv
, 0);
14863 * "setwinvar()" and "settabwinvar()" functions
14866 setwinvar(argvars
, rettv
, off
)
14872 #ifdef FEAT_WINDOWS
14873 win_T
*save_curwin
;
14874 tabpage_T
*save_curtab
;
14876 char_u
*varname
, *winvarname
;
14878 char_u nbuf
[NUMBUFLEN
];
14881 rettv
->vval
.v_number
= 0;
14883 if (check_restricted() || check_secure())
14886 #ifdef FEAT_WINDOWS
14888 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
14892 win
= find_win_by_nr(&argvars
[off
], tp
);
14893 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
14894 varp
= &argvars
[off
+ 2];
14896 if (win
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
14898 #ifdef FEAT_WINDOWS
14899 /* set curwin to be our win, temporarily */
14900 save_curwin
= curwin
;
14901 save_curtab
= curtab
;
14902 goto_tabpage_tp(tp
);
14903 if (!win_valid(win
))
14906 curbuf
= curwin
->w_buffer
;
14909 if (*varname
== '&')
14916 numval
= get_tv_number_chk(varp
, &error
);
14917 strval
= get_tv_string_buf_chk(varp
, nbuf
);
14918 if (!error
&& strval
!= NULL
)
14919 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
14923 winvarname
= alloc((unsigned)STRLEN(varname
) + 3);
14924 if (winvarname
!= NULL
)
14926 STRCPY(winvarname
, "w:");
14927 STRCPY(winvarname
+ 2, varname
);
14928 set_var(winvarname
, varp
, TRUE
);
14929 vim_free(winvarname
);
14933 #ifdef FEAT_WINDOWS
14934 /* Restore current tabpage and window, if still valid (autocomands can
14935 * make them invalid). */
14936 if (valid_tabpage(save_curtab
))
14937 goto_tabpage_tp(save_curtab
);
14938 if (win_valid(save_curwin
))
14940 curwin
= save_curwin
;
14941 curbuf
= curwin
->w_buffer
;
14948 * "shellescape({string})" function
14951 f_shellescape(argvars
, rettv
)
14955 rettv
->vval
.v_string
= vim_strsave_shellescape(get_tv_string(&argvars
[0]));
14956 rettv
->v_type
= VAR_STRING
;
14960 * "simplify()" function
14963 f_simplify(argvars
, rettv
)
14969 p
= get_tv_string(&argvars
[0]);
14970 rettv
->vval
.v_string
= vim_strsave(p
);
14971 simplify_filename(rettv
->vval
.v_string
); /* simplify in place */
14972 rettv
->v_type
= VAR_STRING
;
14976 #ifdef __BORLANDC__
14979 item_compare
__ARGS((const void *s1
, const void *s2
));
14981 #ifdef __BORLANDC__
14984 item_compare2
__ARGS((const void *s1
, const void *s2
));
14986 static int item_compare_ic
;
14987 static char_u
*item_compare_func
;
14988 static int item_compare_func_err
;
14989 #define ITEM_COMPARE_FAIL 999
14992 * Compare functions for f_sort() below.
14995 #ifdef __BORLANDC__
14998 item_compare(s1
, s2
)
15003 char_u
*tofree1
, *tofree2
;
15005 char_u numbuf1
[NUMBUFLEN
];
15006 char_u numbuf2
[NUMBUFLEN
];
15008 p1
= tv2string(&(*(listitem_T
**)s1
)->li_tv
, &tofree1
, numbuf1
, 0);
15009 p2
= tv2string(&(*(listitem_T
**)s2
)->li_tv
, &tofree2
, numbuf2
, 0);
15014 if (item_compare_ic
)
15015 res
= STRICMP(p1
, p2
);
15017 res
= STRCMP(p1
, p2
);
15024 #ifdef __BORLANDC__
15027 item_compare2(s1
, s2
)
15036 /* shortcut after failure in previous call; compare all items equal */
15037 if (item_compare_func_err
)
15040 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15041 * in the copy without changing the original list items. */
15042 copy_tv(&(*(listitem_T
**)s1
)->li_tv
, &argv
[0]);
15043 copy_tv(&(*(listitem_T
**)s2
)->li_tv
, &argv
[1]);
15045 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
15046 res
= call_func(item_compare_func
, (int)STRLEN(item_compare_func
),
15047 &rettv
, 2, argv
, 0L, 0L, &dummy
, TRUE
, NULL
);
15048 clear_tv(&argv
[0]);
15049 clear_tv(&argv
[1]);
15052 res
= ITEM_COMPARE_FAIL
;
15054 /* return value has wrong type */
15055 res
= get_tv_number_chk(&rettv
, &item_compare_func_err
);
15056 if (item_compare_func_err
)
15057 res
= ITEM_COMPARE_FAIL
;
15063 * "sort({list})" function
15066 f_sort(argvars
, rettv
)
15076 rettv
->vval
.v_number
= 0;
15077 if (argvars
[0].v_type
!= VAR_LIST
)
15078 EMSG2(_(e_listarg
), "sort()");
15081 l
= argvars
[0].vval
.v_list
;
15082 if (l
== NULL
|| tv_check_lock(l
->lv_lock
, (char_u
*)"sort()"))
15084 rettv
->vval
.v_list
= l
;
15085 rettv
->v_type
= VAR_LIST
;
15090 return; /* short list sorts pretty quickly */
15092 item_compare_ic
= FALSE
;
15093 item_compare_func
= NULL
;
15094 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15096 if (argvars
[1].v_type
== VAR_FUNC
)
15097 item_compare_func
= argvars
[1].vval
.v_string
;
15102 i
= get_tv_number_chk(&argvars
[1], &error
);
15104 return; /* type error; errmsg already given */
15106 item_compare_ic
= TRUE
;
15108 item_compare_func
= get_tv_string(&argvars
[1]);
15112 /* Make an array with each entry pointing to an item in the List. */
15113 ptrs
= (listitem_T
**)alloc((int)(len
* sizeof(listitem_T
*)));
15117 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
15120 item_compare_func_err
= FALSE
;
15121 /* test the compare function */
15122 if (item_compare_func
!= NULL
15123 && item_compare2((void *)&ptrs
[0], (void *)&ptrs
[1])
15124 == ITEM_COMPARE_FAIL
)
15125 EMSG(_("E702: Sort compare function failed"));
15128 /* Sort the array with item pointers. */
15129 qsort((void *)ptrs
, (size_t)len
, sizeof(listitem_T
*),
15130 item_compare_func
== NULL
? item_compare
: item_compare2
);
15132 if (!item_compare_func_err
)
15134 /* Clear the List and append the items in the sorted order. */
15135 l
->lv_first
= l
->lv_last
= NULL
;
15137 for (i
= 0; i
< len
; ++i
)
15138 list_append(l
, ptrs
[i
]);
15147 * "soundfold({word})" function
15150 f_soundfold(argvars
, rettv
)
15156 rettv
->v_type
= VAR_STRING
;
15157 s
= get_tv_string(&argvars
[0]);
15159 rettv
->vval
.v_string
= eval_soundfold(s
);
15161 rettv
->vval
.v_string
= vim_strsave(s
);
15166 * "spellbadword()" function
15170 f_spellbadword(argvars
, rettv
)
15174 char_u
*word
= (char_u
*)"";
15175 hlf_T attr
= HLF_COUNT
;
15178 if (rettv_list_alloc(rettv
) == FAIL
)
15182 if (argvars
[0].v_type
== VAR_UNKNOWN
)
15184 /* Find the start and length of the badly spelled word. */
15185 len
= spell_move_to(curwin
, FORWARD
, TRUE
, TRUE
, &attr
);
15187 word
= ml_get_cursor();
15189 else if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
15191 char_u
*str
= get_tv_string_chk(&argvars
[0]);
15196 /* Check the argument for spelling. */
15197 while (*str
!= NUL
)
15199 len
= spell_check(curwin
, str
, &attr
, &capcol
, FALSE
);
15200 if (attr
!= HLF_COUNT
)
15211 list_append_string(rettv
->vval
.v_list
, word
, len
);
15212 list_append_string(rettv
->vval
.v_list
, (char_u
*)(
15213 attr
== HLF_SPB
? "bad" :
15214 attr
== HLF_SPR
? "rare" :
15215 attr
== HLF_SPL
? "local" :
15216 attr
== HLF_SPC
? "caps" :
15221 * "spellsuggest()" function
15225 f_spellsuggest(argvars
, rettv
)
15231 int typeerr
= FALSE
;
15236 int need_capital
= FALSE
;
15239 if (rettv_list_alloc(rettv
) == FAIL
)
15243 if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
15245 str
= get_tv_string(&argvars
[0]);
15246 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15248 maxcount
= get_tv_number_chk(&argvars
[1], &typeerr
);
15251 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15253 need_capital
= get_tv_number_chk(&argvars
[2], &typeerr
);
15261 spell_suggest_list(&ga
, str
, maxcount
, need_capital
, FALSE
);
15263 for (i
= 0; i
< ga
.ga_len
; ++i
)
15265 str
= ((char_u
**)ga
.ga_data
)[i
];
15267 li
= listitem_alloc();
15272 li
->li_tv
.v_type
= VAR_STRING
;
15273 li
->li_tv
.v_lock
= 0;
15274 li
->li_tv
.vval
.v_string
= str
;
15275 list_append(rettv
->vval
.v_list
, li
);
15284 f_split(argvars
, rettv
)
15290 char_u
*pat
= NULL
;
15291 regmatch_T regmatch
;
15292 char_u patbuf
[NUMBUFLEN
];
15296 int keepempty
= FALSE
;
15297 int typeerr
= FALSE
;
15299 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15301 p_cpo
= (char_u
*)"";
15303 str
= get_tv_string(&argvars
[0]);
15304 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15306 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
15309 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15310 keepempty
= get_tv_number_chk(&argvars
[2], &typeerr
);
15312 if (pat
== NULL
|| *pat
== NUL
)
15313 pat
= (char_u
*)"[\\x01- ]\\+";
15315 if (rettv_list_alloc(rettv
) == FAIL
)
15320 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
15321 if (regmatch
.regprog
!= NULL
)
15323 regmatch
.rm_ic
= FALSE
;
15324 while (*str
!= NUL
|| keepempty
)
15327 match
= FALSE
; /* empty item at the end */
15329 match
= vim_regexec_nl(®match
, str
, col
);
15331 end
= regmatch
.startp
[0];
15333 end
= str
+ STRLEN(str
);
15334 if (keepempty
|| end
> str
|| (rettv
->vval
.v_list
->lv_len
> 0
15335 && *str
!= NUL
&& match
&& end
< regmatch
.endp
[0]))
15337 if (list_append_string(rettv
->vval
.v_list
, str
,
15338 (int)(end
- str
)) == FAIL
)
15343 /* Advance to just after the match. */
15344 if (regmatch
.endp
[0] > str
)
15348 /* Don't get stuck at the same match. */
15350 col
= (*mb_ptr2len
)(regmatch
.endp
[0]);
15355 str
= regmatch
.endp
[0];
15358 vim_free(regmatch
.regprog
);
15365 * "str2nr()" function
15368 f_str2nr(argvars
, rettv
)
15376 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15378 base
= get_tv_number(&argvars
[1]);
15379 if (base
!= 8 && base
!= 10 && base
!= 16)
15386 p
= skipwhite(get_tv_string(&argvars
[0]));
15387 vim_str2nr(p
, NULL
, NULL
, base
== 8 ? 2 : 0, base
== 16 ? 2 : 0, &n
, NULL
);
15388 rettv
->vval
.v_number
= n
;
15391 #ifdef HAVE_STRFTIME
15393 * "strftime({format}[, {time}])" function
15396 f_strftime(argvars
, rettv
)
15400 char_u result_buf
[256];
15401 struct tm
*curtime
;
15405 rettv
->v_type
= VAR_STRING
;
15407 p
= get_tv_string(&argvars
[0]);
15408 if (argvars
[1].v_type
== VAR_UNKNOWN
)
15409 seconds
= time(NULL
);
15411 seconds
= (time_t)get_tv_number(&argvars
[1]);
15412 curtime
= localtime(&seconds
);
15413 /* MSVC returns NULL for an invalid value of seconds. */
15414 if (curtime
== NULL
)
15415 rettv
->vval
.v_string
= vim_strsave((char_u
*)_("(Invalid)"));
15422 conv
.vc_type
= CONV_NONE
;
15423 enc
= enc_locale();
15424 convert_setup(&conv
, p_enc
, enc
);
15425 if (conv
.vc_type
!= CONV_NONE
)
15426 p
= string_convert(&conv
, p
, NULL
);
15429 (void)strftime((char *)result_buf
, sizeof(result_buf
),
15430 (char *)p
, curtime
);
15432 result_buf
[0] = NUL
;
15435 if (conv
.vc_type
!= CONV_NONE
)
15437 convert_setup(&conv
, enc
, p_enc
);
15438 if (conv
.vc_type
!= CONV_NONE
)
15439 rettv
->vval
.v_string
= string_convert(&conv
, result_buf
, NULL
);
15442 rettv
->vval
.v_string
= vim_strsave(result_buf
);
15445 /* Release conversion descriptors */
15446 convert_setup(&conv
, NULL
, NULL
);
15454 * "stridx()" function
15457 f_stridx(argvars
, rettv
)
15461 char_u buf
[NUMBUFLEN
];
15464 char_u
*save_haystack
;
15468 needle
= get_tv_string_chk(&argvars
[1]);
15469 save_haystack
= haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
15470 rettv
->vval
.v_number
= -1;
15471 if (needle
== NULL
|| haystack
== NULL
)
15472 return; /* type error; errmsg already given */
15474 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15478 start_idx
= get_tv_number_chk(&argvars
[2], &error
);
15479 if (error
|| start_idx
>= (int)STRLEN(haystack
))
15481 if (start_idx
>= 0)
15482 haystack
+= start_idx
;
15485 pos
= (char_u
*)strstr((char *)haystack
, (char *)needle
);
15487 rettv
->vval
.v_number
= (varnumber_T
)(pos
- save_haystack
);
15491 * "string()" function
15494 f_string(argvars
, rettv
)
15499 char_u numbuf
[NUMBUFLEN
];
15501 rettv
->v_type
= VAR_STRING
;
15502 rettv
->vval
.v_string
= tv2string(&argvars
[0], &tofree
, numbuf
, 0);
15503 /* Make a copy if we have a value but it's not in allocate memory. */
15504 if (rettv
->vval
.v_string
!= NULL
&& tofree
== NULL
)
15505 rettv
->vval
.v_string
= vim_strsave(rettv
->vval
.v_string
);
15509 * "strlen()" function
15512 f_strlen(argvars
, rettv
)
15516 rettv
->vval
.v_number
= (varnumber_T
)(STRLEN(
15517 get_tv_string(&argvars
[0])));
15521 * "strpart()" function
15524 f_strpart(argvars
, rettv
)
15534 p
= get_tv_string(&argvars
[0]);
15535 slen
= (int)STRLEN(p
);
15537 n
= get_tv_number_chk(&argvars
[1], &error
);
15540 else if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15541 len
= get_tv_number(&argvars
[2]);
15543 len
= slen
- n
; /* default len: all bytes that are available. */
15546 * Only return the overlap between the specified part and the actual
15558 else if (n
+ len
> slen
)
15561 rettv
->v_type
= VAR_STRING
;
15562 rettv
->vval
.v_string
= vim_strnsave(p
+ n
, len
);
15566 * "strridx()" function
15569 f_strridx(argvars
, rettv
)
15573 char_u buf
[NUMBUFLEN
];
15577 char_u
*lastmatch
= NULL
;
15578 int haystack_len
, end_idx
;
15580 needle
= get_tv_string_chk(&argvars
[1]);
15581 haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
15583 rettv
->vval
.v_number
= -1;
15584 if (needle
== NULL
|| haystack
== NULL
)
15585 return; /* type error; errmsg already given */
15587 haystack_len
= (int)STRLEN(haystack
);
15588 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15590 /* Third argument: upper limit for index */
15591 end_idx
= get_tv_number_chk(&argvars
[2], NULL
);
15593 return; /* can never find a match */
15596 end_idx
= haystack_len
;
15598 if (*needle
== NUL
)
15600 /* Empty string matches past the end. */
15601 lastmatch
= haystack
+ end_idx
;
15605 for (rest
= haystack
; *rest
!= '\0'; ++rest
)
15607 rest
= (char_u
*)strstr((char *)rest
, (char *)needle
);
15608 if (rest
== NULL
|| rest
> haystack
+ end_idx
)
15614 if (lastmatch
== NULL
)
15615 rettv
->vval
.v_number
= -1;
15617 rettv
->vval
.v_number
= (varnumber_T
)(lastmatch
- haystack
);
15621 * "strtrans()" function
15624 f_strtrans(argvars
, rettv
)
15628 rettv
->v_type
= VAR_STRING
;
15629 rettv
->vval
.v_string
= transstr(get_tv_string(&argvars
[0]));
15633 * "submatch()" function
15636 f_submatch(argvars
, rettv
)
15640 rettv
->v_type
= VAR_STRING
;
15641 rettv
->vval
.v_string
=
15642 reg_submatch((int)get_tv_number_chk(&argvars
[0], NULL
));
15646 * "substitute()" function
15649 f_substitute(argvars
, rettv
)
15653 char_u patbuf
[NUMBUFLEN
];
15654 char_u subbuf
[NUMBUFLEN
];
15655 char_u flagsbuf
[NUMBUFLEN
];
15657 char_u
*str
= get_tv_string_chk(&argvars
[0]);
15658 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
15659 char_u
*sub
= get_tv_string_buf_chk(&argvars
[2], subbuf
);
15660 char_u
*flg
= get_tv_string_buf_chk(&argvars
[3], flagsbuf
);
15662 rettv
->v_type
= VAR_STRING
;
15663 if (str
== NULL
|| pat
== NULL
|| sub
== NULL
|| flg
== NULL
)
15664 rettv
->vval
.v_string
= NULL
;
15666 rettv
->vval
.v_string
= do_string_sub(str
, pat
, sub
, flg
);
15670 * "synID(lnum, col, trans)" function
15674 f_synID(argvars
, rettv
)
15683 int transerr
= FALSE
;
15685 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
15686 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
15687 trans
= get_tv_number_chk(&argvars
[2], &transerr
);
15689 if (!transerr
&& lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
15690 && col
>= 0 && col
< (long)STRLEN(ml_get(lnum
)))
15691 id
= syn_get_id(curwin
, lnum
, (colnr_T
)col
, trans
, NULL
);
15694 rettv
->vval
.v_number
= id
;
15698 * "synIDattr(id, what [, mode])" function
15702 f_synIDattr(argvars
, rettv
)
15711 char_u modebuf
[NUMBUFLEN
];
15714 id
= get_tv_number(&argvars
[0]);
15715 what
= get_tv_string(&argvars
[1]);
15716 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15718 mode
= get_tv_string_buf(&argvars
[2], modebuf
);
15719 modec
= TOLOWER_ASC(mode
[0]);
15720 if (modec
!= 't' && modec
!= 'c'
15725 modec
= 0; /* replace invalid with current */
15741 switch (TOLOWER_ASC(what
[0]))
15744 if (TOLOWER_ASC(what
[1]) == 'g') /* bg[#] */
15745 p
= highlight_color(id
, what
, modec
);
15747 p
= highlight_has_attr(id
, HL_BOLD
, modec
);
15750 case 'f': /* fg[#] */
15751 p
= highlight_color(id
, what
, modec
);
15755 if (TOLOWER_ASC(what
[1]) == 'n') /* inverse */
15756 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
15758 p
= highlight_has_attr(id
, HL_ITALIC
, modec
);
15761 case 'n': /* name */
15762 p
= get_highlight_name(NULL
, id
- 1);
15765 case 'r': /* reverse */
15766 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
15769 case 's': /* standout */
15770 p
= highlight_has_attr(id
, HL_STANDOUT
, modec
);
15774 if (STRLEN(what
) <= 5 || TOLOWER_ASC(what
[5]) != 'c')
15776 p
= highlight_has_attr(id
, HL_UNDERLINE
, modec
);
15779 p
= highlight_has_attr(id
, HL_UNDERCURL
, modec
);
15784 p
= vim_strsave(p
);
15786 rettv
->v_type
= VAR_STRING
;
15787 rettv
->vval
.v_string
= p
;
15791 * "synIDtrans(id)" function
15795 f_synIDtrans(argvars
, rettv
)
15802 id
= get_tv_number(&argvars
[0]);
15805 id
= syn_get_final_id(id
);
15810 rettv
->vval
.v_number
= id
;
15814 * "system()" function
15817 f_system(argvars
, rettv
)
15821 char_u
*res
= NULL
;
15823 char_u
*infile
= NULL
;
15824 char_u buf
[NUMBUFLEN
];
15828 if (check_restricted() || check_secure())
15831 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15834 * Write the string to a temp file, to be used for input of the shell
15837 if ((infile
= vim_tempname('i')) == NULL
)
15843 fd
= mch_fopen((char *)infile
, WRITEBIN
);
15846 EMSG2(_(e_notopen
), infile
);
15849 p
= get_tv_string_buf_chk(&argvars
[1], buf
);
15853 goto done
; /* type error; errmsg already given */
15855 if (fwrite(p
, STRLEN(p
), 1, fd
) != 1)
15857 if (fclose(fd
) != 0)
15861 EMSG(_("E677: Error writing temp file"));
15866 res
= get_cmd_output(get_tv_string(&argvars
[0]), infile
,
15867 SHELL_SILENT
| SHELL_COOKED
);
15870 /* translate <CR> into <NL> */
15875 for (s
= res
; *s
; ++s
)
15883 /* translate <CR><NL> into <NL> */
15889 for (s
= res
; *s
; ++s
)
15891 if (s
[0] == CAR
&& s
[1] == NL
)
15901 if (infile
!= NULL
)
15903 mch_remove(infile
);
15906 rettv
->v_type
= VAR_STRING
;
15907 rettv
->vval
.v_string
= res
;
15911 * "tabpagebuflist()" function
15915 f_tabpagebuflist(argvars
, rettv
)
15919 #ifndef FEAT_WINDOWS
15920 rettv
->vval
.v_number
= 0;
15925 if (argvars
[0].v_type
== VAR_UNKNOWN
)
15929 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
15931 wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
15934 rettv
->vval
.v_number
= 0;
15937 if (rettv_list_alloc(rettv
) == FAIL
)
15938 rettv
->vval
.v_number
= 0;
15941 for (; wp
!= NULL
; wp
= wp
->w_next
)
15942 if (list_append_number(rettv
->vval
.v_list
,
15943 wp
->w_buffer
->b_fnum
) == FAIL
)
15952 * "tabpagenr()" function
15956 f_tabpagenr(argvars
, rettv
)
15961 #ifdef FEAT_WINDOWS
15964 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
15966 arg
= get_tv_string_chk(&argvars
[0]);
15970 if (STRCMP(arg
, "$") == 0)
15971 nr
= tabpage_index(NULL
) - 1;
15973 EMSG2(_(e_invexpr2
), arg
);
15977 nr
= tabpage_index(curtab
);
15979 rettv
->vval
.v_number
= nr
;
15983 #ifdef FEAT_WINDOWS
15984 static int get_winnr
__ARGS((tabpage_T
*tp
, typval_T
*argvar
));
15987 * Common code for tabpagewinnr() and winnr().
15990 get_winnr(tp
, argvar
)
15999 twin
= (tp
== curtab
) ? curwin
: tp
->tp_curwin
;
16000 if (argvar
->v_type
!= VAR_UNKNOWN
)
16002 arg
= get_tv_string_chk(argvar
);
16004 nr
= 0; /* type error; errmsg already given */
16005 else if (STRCMP(arg
, "$") == 0)
16006 twin
= (tp
== curtab
) ? lastwin
: tp
->tp_lastwin
;
16007 else if (STRCMP(arg
, "#") == 0)
16009 twin
= (tp
== curtab
) ? prevwin
: tp
->tp_prevwin
;
16015 EMSG2(_(e_invexpr2
), arg
);
16021 for (wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
16022 wp
!= twin
; wp
= wp
->w_next
)
16026 /* didn't find it in this tabpage */
16037 * "tabpagewinnr()" function
16041 f_tabpagewinnr(argvars
, rettv
)
16046 #ifdef FEAT_WINDOWS
16049 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
16053 nr
= get_winnr(tp
, &argvars
[1]);
16055 rettv
->vval
.v_number
= nr
;
16060 * "tagfiles()" function
16064 f_tagfiles(argvars
, rettv
)
16068 char_u fname
[MAXPATHL
+ 1];
16072 if (rettv_list_alloc(rettv
) == FAIL
)
16074 rettv
->vval
.v_number
= 0;
16078 for (first
= TRUE
; ; first
= FALSE
)
16079 if (get_tagfname(&tn
, first
, fname
) == FAIL
16080 || list_append_string(rettv
->vval
.v_list
, fname
, -1) == FAIL
)
16086 * "taglist()" function
16089 f_taglist(argvars
, rettv
)
16093 char_u
*tag_pattern
;
16095 tag_pattern
= get_tv_string(&argvars
[0]);
16097 rettv
->vval
.v_number
= FALSE
;
16098 if (*tag_pattern
== NUL
)
16101 if (rettv_list_alloc(rettv
) == OK
)
16102 (void)get_tags(rettv
->vval
.v_list
, tag_pattern
);
16106 * "tempname()" function
16110 f_tempname(argvars
, rettv
)
16114 static int x
= 'A';
16116 rettv
->v_type
= VAR_STRING
;
16117 rettv
->vval
.v_string
= vim_tempname(x
);
16119 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16120 * names. Skip 'I' and 'O', they are used for shell redirection. */
16138 } while (x
== 'I' || x
== 'O');
16142 * "test(list)" function: Just checking the walls...
16146 f_test(argvars
, rettv
)
16150 /* Used for unit testing. Change the code below to your liking. */
16154 char_u
*bad
, *good
;
16156 if (argvars
[0].v_type
!= VAR_LIST
)
16158 l
= argvars
[0].vval
.v_list
;
16164 bad
= get_tv_string(&li
->li_tv
);
16168 good
= get_tv_string(&li
->li_tv
);
16169 rettv
->vval
.v_number
= test_edit_score(bad
, good
);
16174 * "tolower(string)" function
16177 f_tolower(argvars
, rettv
)
16183 p
= vim_strsave(get_tv_string(&argvars
[0]));
16184 rettv
->v_type
= VAR_STRING
;
16185 rettv
->vval
.v_string
= p
;
16197 c
= utf_ptr2char(p
);
16198 lc
= utf_tolower(c
);
16199 l
= utf_ptr2len(p
);
16200 /* TODO: reallocate string when byte count changes. */
16201 if (utf_char2len(lc
) == l
)
16202 utf_char2bytes(lc
, p
);
16205 else if (has_mbyte
&& (l
= (*mb_ptr2len
)(p
)) > 1)
16206 p
+= l
; /* skip multi-byte character */
16210 *p
= TOLOWER_LOC(*p
); /* note that tolower() can be a macro */
16217 * "toupper(string)" function
16220 f_toupper(argvars
, rettv
)
16224 rettv
->v_type
= VAR_STRING
;
16225 rettv
->vval
.v_string
= strup_save(get_tv_string(&argvars
[0]));
16229 * "tr(string, fromstr, tostr)" function
16232 f_tr(argvars
, rettv
)
16249 char_u buf
[NUMBUFLEN
];
16250 char_u buf2
[NUMBUFLEN
];
16253 instr
= get_tv_string(&argvars
[0]);
16254 fromstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
16255 tostr
= get_tv_string_buf_chk(&argvars
[2], buf2
);
16257 /* Default return value: empty string. */
16258 rettv
->v_type
= VAR_STRING
;
16259 rettv
->vval
.v_string
= NULL
;
16260 if (fromstr
== NULL
|| tostr
== NULL
)
16261 return; /* type error; errmsg already given */
16262 ga_init2(&ga
, (int)sizeof(char), 80);
16267 /* not multi-byte: fromstr and tostr must be the same length */
16268 if (STRLEN(fromstr
) != STRLEN(tostr
))
16273 EMSG2(_(e_invarg2
), fromstr
);
16278 /* fromstr and tostr have to contain the same number of chars */
16279 while (*instr
!= NUL
)
16284 inlen
= (*mb_ptr2len
)(instr
);
16288 for (p
= fromstr
; *p
!= NUL
; p
+= fromlen
)
16290 fromlen
= (*mb_ptr2len
)(p
);
16291 if (fromlen
== inlen
&& STRNCMP(instr
, p
, inlen
) == 0)
16293 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
16295 tolen
= (*mb_ptr2len
)(p
);
16303 if (*p
== NUL
) /* tostr is shorter than fromstr */
16310 if (first
&& cpstr
== instr
)
16312 /* Check that fromstr and tostr have the same number of
16313 * (multi-byte) characters. Done only once when a character
16314 * of instr doesn't appear in fromstr. */
16316 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
16318 tolen
= (*mb_ptr2len
)(p
);
16325 ga_grow(&ga
, cplen
);
16326 mch_memmove((char *)ga
.ga_data
+ ga
.ga_len
, cpstr
, (size_t)cplen
);
16327 ga
.ga_len
+= cplen
;
16334 /* When not using multi-byte chars we can do it faster. */
16335 p
= vim_strchr(fromstr
, *instr
);
16337 ga_append(&ga
, tostr
[p
- fromstr
]);
16339 ga_append(&ga
, *instr
);
16344 /* add a terminating NUL */
16346 ga_append(&ga
, NUL
);
16348 rettv
->vval
.v_string
= ga
.ga_data
;
16352 * "type(expr)" function
16355 f_type(argvars
, rettv
)
16361 switch (argvars
[0].v_type
)
16363 case VAR_NUMBER
: n
= 0; break;
16364 case VAR_STRING
: n
= 1; break;
16365 case VAR_FUNC
: n
= 2; break;
16366 case VAR_LIST
: n
= 3; break;
16367 case VAR_DICT
: n
= 4; break;
16368 default: EMSG2(_(e_intern2
), "f_type()"); n
= 0; break;
16370 rettv
->vval
.v_number
= n
;
16374 * "values(dict)" function
16377 f_values(argvars
, rettv
)
16381 dict_list(argvars
, rettv
, 1);
16385 * "virtcol(string)" function
16388 f_virtcol(argvars
, rettv
)
16394 int fnum
= curbuf
->b_fnum
;
16396 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
16397 if (fp
!= NULL
&& fp
->lnum
<= curbuf
->b_ml
.ml_line_count
16398 && fnum
== curbuf
->b_fnum
)
16400 getvvcol(curwin
, fp
, NULL
, NULL
, &vcol
);
16404 rettv
->vval
.v_number
= vcol
;
16408 * "visualmode()" function
16412 f_visualmode(argvars
, rettv
)
16419 rettv
->v_type
= VAR_STRING
;
16420 str
[0] = curbuf
->b_visual_mode_eval
;
16422 rettv
->vval
.v_string
= vim_strsave(str
);
16424 /* A non-zero number or non-empty string argument: reset mode. */
16425 if ((argvars
[0].v_type
== VAR_NUMBER
16426 && argvars
[0].vval
.v_number
!= 0)
16427 || (argvars
[0].v_type
== VAR_STRING
16428 && *get_tv_string(&argvars
[0]) != NUL
))
16429 curbuf
->b_visual_mode_eval
= NUL
;
16431 rettv
->vval
.v_number
= 0; /* return anything, it won't work anyway */
16436 * "winbufnr(nr)" function
16439 f_winbufnr(argvars
, rettv
)
16445 wp
= find_win_by_nr(&argvars
[0], NULL
);
16447 rettv
->vval
.v_number
= -1;
16449 rettv
->vval
.v_number
= wp
->w_buffer
->b_fnum
;
16453 * "wincol()" function
16457 f_wincol(argvars
, rettv
)
16462 rettv
->vval
.v_number
= curwin
->w_wcol
+ 1;
16466 * "winheight(nr)" function
16469 f_winheight(argvars
, rettv
)
16475 wp
= find_win_by_nr(&argvars
[0], NULL
);
16477 rettv
->vval
.v_number
= -1;
16479 rettv
->vval
.v_number
= wp
->w_height
;
16483 * "winline()" function
16487 f_winline(argvars
, rettv
)
16492 rettv
->vval
.v_number
= curwin
->w_wrow
+ 1;
16496 * "winnr()" function
16500 f_winnr(argvars
, rettv
)
16506 #ifdef FEAT_WINDOWS
16507 nr
= get_winnr(curtab
, &argvars
[0]);
16509 rettv
->vval
.v_number
= nr
;
16513 * "winrestcmd()" function
16517 f_winrestcmd(argvars
, rettv
)
16521 #ifdef FEAT_WINDOWS
16527 ga_init2(&ga
, (int)sizeof(char), 70);
16528 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
16530 sprintf((char *)buf
, "%dresize %d|", winnr
, wp
->w_height
);
16531 ga_concat(&ga
, buf
);
16532 # ifdef FEAT_VERTSPLIT
16533 sprintf((char *)buf
, "vert %dresize %d|", winnr
, wp
->w_width
);
16534 ga_concat(&ga
, buf
);
16538 ga_append(&ga
, NUL
);
16540 rettv
->vval
.v_string
= ga
.ga_data
;
16542 rettv
->vval
.v_string
= NULL
;
16544 rettv
->v_type
= VAR_STRING
;
16548 * "winrestview()" function
16552 f_winrestview(argvars
, rettv
)
16558 if (argvars
[0].v_type
!= VAR_DICT
16559 || (dict
= argvars
[0].vval
.v_dict
) == NULL
)
16563 curwin
->w_cursor
.lnum
= get_dict_number(dict
, (char_u
*)"lnum");
16564 curwin
->w_cursor
.col
= get_dict_number(dict
, (char_u
*)"col");
16565 #ifdef FEAT_VIRTUALEDIT
16566 curwin
->w_cursor
.coladd
= get_dict_number(dict
, (char_u
*)"coladd");
16568 curwin
->w_curswant
= get_dict_number(dict
, (char_u
*)"curswant");
16569 curwin
->w_set_curswant
= FALSE
;
16571 set_topline(curwin
, get_dict_number(dict
, (char_u
*)"topline"));
16573 curwin
->w_topfill
= get_dict_number(dict
, (char_u
*)"topfill");
16575 curwin
->w_leftcol
= get_dict_number(dict
, (char_u
*)"leftcol");
16576 curwin
->w_skipcol
= get_dict_number(dict
, (char_u
*)"skipcol");
16579 changed_cline_bef_curs();
16580 invalidate_botline();
16581 redraw_later(VALID
);
16583 if (curwin
->w_topline
== 0)
16584 curwin
->w_topline
= 1;
16585 if (curwin
->w_topline
> curbuf
->b_ml
.ml_line_count
)
16586 curwin
->w_topline
= curbuf
->b_ml
.ml_line_count
;
16588 check_topfill(curwin
, TRUE
);
16594 * "winsaveview()" function
16598 f_winsaveview(argvars
, rettv
)
16604 dict
= dict_alloc();
16607 rettv
->v_type
= VAR_DICT
;
16608 rettv
->vval
.v_dict
= dict
;
16609 ++dict
->dv_refcount
;
16611 dict_add_nr_str(dict
, "lnum", (long)curwin
->w_cursor
.lnum
, NULL
);
16612 dict_add_nr_str(dict
, "col", (long)curwin
->w_cursor
.col
, NULL
);
16613 #ifdef FEAT_VIRTUALEDIT
16614 dict_add_nr_str(dict
, "coladd", (long)curwin
->w_cursor
.coladd
, NULL
);
16617 dict_add_nr_str(dict
, "curswant", (long)curwin
->w_curswant
, NULL
);
16619 dict_add_nr_str(dict
, "topline", (long)curwin
->w_topline
, NULL
);
16621 dict_add_nr_str(dict
, "topfill", (long)curwin
->w_topfill
, NULL
);
16623 dict_add_nr_str(dict
, "leftcol", (long)curwin
->w_leftcol
, NULL
);
16624 dict_add_nr_str(dict
, "skipcol", (long)curwin
->w_skipcol
, NULL
);
16628 * "winwidth(nr)" function
16631 f_winwidth(argvars
, rettv
)
16637 wp
= find_win_by_nr(&argvars
[0], NULL
);
16639 rettv
->vval
.v_number
= -1;
16641 #ifdef FEAT_VERTSPLIT
16642 rettv
->vval
.v_number
= wp
->w_width
;
16644 rettv
->vval
.v_number
= Columns
;
16649 * "writefile()" function
16652 f_writefile(argvars
, rettv
)
16656 int binary
= FALSE
;
16664 if (check_restricted() || check_secure())
16667 if (argvars
[0].v_type
!= VAR_LIST
)
16669 EMSG2(_(e_listarg
), "writefile()");
16672 if (argvars
[0].vval
.v_list
== NULL
)
16675 if (argvars
[2].v_type
!= VAR_UNKNOWN
16676 && STRCMP(get_tv_string(&argvars
[2]), "b") == 0)
16679 /* Always open the file in binary mode, library functions have a mind of
16680 * their own about CR-LF conversion. */
16681 fname
= get_tv_string(&argvars
[1]);
16682 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, WRITEBIN
)) == NULL
)
16684 EMSG2(_(e_notcreate
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
16689 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
;
16692 for (s
= get_tv_string(&li
->li_tv
); *s
!= NUL
; ++s
)
16704 if (!binary
|| li
->li_next
!= NULL
)
16705 if (putc('\n', fd
) == EOF
)
16719 rettv
->vval
.v_number
= ret
;
16723 * Translate a String variable into a position.
16724 * Returns NULL when there is an error.
16727 var2fpos(varp
, dollar_lnum
, fnum
)
16729 int dollar_lnum
; /* TRUE when $ is last line */
16730 int *fnum
; /* set to fnum for '0, 'A, etc. */
16736 /* Argument can be [lnum, col, coladd]. */
16737 if (varp
->v_type
== VAR_LIST
)
16744 l
= varp
->vval
.v_list
;
16748 /* Get the line number */
16749 pos
.lnum
= list_find_nr(l
, 0L, &error
);
16750 if (error
|| pos
.lnum
<= 0 || pos
.lnum
> curbuf
->b_ml
.ml_line_count
)
16751 return NULL
; /* invalid line number */
16753 /* Get the column number */
16754 pos
.col
= list_find_nr(l
, 1L, &error
);
16757 len
= (long)STRLEN(ml_get(pos
.lnum
));
16759 /* We accept "$" for the column number: last column. */
16760 li
= list_find(l
, 1L);
16761 if (li
!= NULL
&& li
->li_tv
.v_type
== VAR_STRING
16762 && li
->li_tv
.vval
.v_string
!= NULL
16763 && STRCMP(li
->li_tv
.vval
.v_string
, "$") == 0)
16766 /* Accept a position up to the NUL after the line. */
16767 if (pos
.col
== 0 || (int)pos
.col
> len
+ 1)
16768 return NULL
; /* invalid column number */
16771 #ifdef FEAT_VIRTUALEDIT
16772 /* Get the virtual offset. Defaults to zero. */
16773 pos
.coladd
= list_find_nr(l
, 2L, &error
);
16781 name
= get_tv_string_chk(varp
);
16784 if (name
[0] == '.') /* cursor */
16785 return &curwin
->w_cursor
;
16786 if (name
[0] == '\'') /* mark */
16788 pp
= getmark_fnum(name
[1], FALSE
, fnum
);
16789 if (pp
== NULL
|| pp
== (pos_T
*)-1 || pp
->lnum
<= 0)
16794 #ifdef FEAT_VIRTUALEDIT
16798 if (name
[0] == 'w' && dollar_lnum
)
16801 if (name
[1] == '0') /* "w0": first visible line */
16804 pos
.lnum
= curwin
->w_topline
;
16807 else if (name
[1] == '$') /* "w$": last visible line */
16809 validate_botline();
16810 pos
.lnum
= curwin
->w_botline
- 1;
16814 else if (name
[0] == '$') /* last column or line */
16818 pos
.lnum
= curbuf
->b_ml
.ml_line_count
;
16823 pos
.lnum
= curwin
->w_cursor
.lnum
;
16824 pos
.col
= (colnr_T
)STRLEN(ml_get_curline());
16832 * Convert list in "arg" into a position and optional file number.
16833 * When "fnump" is NULL there is no file number, only 3 items.
16834 * Note that the column is passed on as-is, the caller may want to decrement
16835 * it to use 1 for the first column.
16836 * Return FAIL when conversion is not possible, doesn't check the position for
16840 list2fpos(arg
, posp
, fnump
)
16845 list_T
*l
= arg
->vval
.v_list
;
16849 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16850 * when "fnump" isn't NULL and "coladd" is optional. */
16851 if (arg
->v_type
!= VAR_LIST
16853 || l
->lv_len
< (fnump
== NULL
? 2 : 3)
16854 || l
->lv_len
> (fnump
== NULL
? 3 : 4))
16859 n
= list_find_nr(l
, i
++, NULL
); /* fnum */
16863 n
= curbuf
->b_fnum
; /* current buffer */
16867 n
= list_find_nr(l
, i
++, NULL
); /* lnum */
16872 n
= list_find_nr(l
, i
++, NULL
); /* col */
16877 #ifdef FEAT_VIRTUALEDIT
16878 n
= list_find_nr(l
, i
, NULL
);
16889 * Get the length of an environment variable name.
16890 * Advance "arg" to the first character after the name.
16891 * Return 0 for error.
16900 for (p
= *arg
; vim_isIDc(*p
); ++p
)
16902 if (p
== *arg
) /* no name found */
16905 len
= (int)(p
- *arg
);
16911 * Get the length of the name of a function or internal variable.
16912 * "arg" is advanced to the first non-white character after the name.
16913 * Return 0 if something is wrong.
16922 /* Find the end of the name. */
16923 for (p
= *arg
; eval_isnamec(*p
); ++p
)
16925 if (p
== *arg
) /* no name found */
16928 len
= (int)(p
- *arg
);
16929 *arg
= skipwhite(p
);
16935 * Get the length of the name of a variable or function.
16936 * Only the name is recognized, does not handle ".key" or "[idx]".
16937 * "arg" is advanced to the first non-white character after the name.
16938 * Return -1 if curly braces expansion failed.
16939 * Return 0 if something else is wrong.
16940 * If the name contains 'magic' {}'s, expand them and return the
16941 * expanded name in an allocated string via 'alias' - caller must free.
16944 get_name_len(arg
, alias
, evaluate
, verbose
)
16952 char_u
*expr_start
;
16955 *alias
= NULL
; /* default to no alias */
16957 if ((*arg
)[0] == K_SPECIAL
&& (*arg
)[1] == KS_EXTRA
16958 && (*arg
)[2] == (int)KE_SNR
)
16960 /* hard coded <SNR>, already translated */
16962 return get_id_len(arg
) + 3;
16964 len
= eval_fname_script(*arg
);
16967 /* literal "<SID>", "s:" or "<SNR>" */
16972 * Find the end of the name; check for {} construction.
16974 p
= find_name_end(*arg
, &expr_start
, &expr_end
,
16975 len
> 0 ? 0 : FNE_CHECK_START
);
16976 if (expr_start
!= NULL
)
16978 char_u
*temp_string
;
16982 len
+= (int)(p
- *arg
);
16983 *arg
= skipwhite(p
);
16988 * Include any <SID> etc in the expanded string:
16989 * Thus the -len here.
16991 temp_string
= make_expanded_name(*arg
- len
, expr_start
, expr_end
, p
);
16992 if (temp_string
== NULL
)
16994 *alias
= temp_string
;
16995 *arg
= skipwhite(p
);
16996 return (int)STRLEN(temp_string
);
16999 len
+= get_id_len(arg
);
17000 if (len
== 0 && verbose
)
17001 EMSG2(_(e_invexpr2
), *arg
);
17007 * Find the end of a variable or function name, taking care of magic braces.
17008 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
17009 * start and end of the first magic braces item.
17010 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
17011 * Return a pointer to just after the name. Equal to "arg" if there is no
17015 find_name_end(arg
, expr_start
, expr_end
, flags
)
17017 char_u
**expr_start
;
17025 if (expr_start
!= NULL
)
17027 *expr_start
= NULL
;
17031 /* Quick check for valid starting character. */
17032 if ((flags
& FNE_CHECK_START
) && !eval_isnamec1(*arg
) && *arg
!= '{')
17035 for (p
= arg
; *p
!= NUL
17036 && (eval_isnamec(*p
)
17038 || ((flags
& FNE_INCL_BR
) && (*p
== '[' || *p
== '.'))
17040 || br_nest
!= 0); mb_ptr_adv(p
))
17044 /* skip over 'string' to avoid counting [ and ] inside it. */
17045 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '\''; mb_ptr_adv(p
))
17050 else if (*p
== '"')
17052 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17053 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
17054 if (*p
== '\\' && p
[1] != NUL
)
17064 else if (*p
== ']')
17073 if (expr_start
!= NULL
&& *expr_start
== NULL
)
17076 else if (*p
== '}')
17079 if (expr_start
!= NULL
&& mb_nest
== 0 && *expr_end
== NULL
)
17089 * Expands out the 'magic' {}'s in a variable/function name.
17090 * Note that this can call itself recursively, to deal with
17091 * constructs like foo{bar}{baz}{bam}
17092 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17098 * Returns a new allocated string, which the caller must free.
17099 * Returns NULL for failure.
17102 make_expanded_name(in_start
, expr_start
, expr_end
, in_end
)
17104 char_u
*expr_start
;
17109 char_u
*retval
= NULL
;
17110 char_u
*temp_result
;
17111 char_u
*nextcmd
= NULL
;
17113 if (expr_end
== NULL
|| in_end
== NULL
)
17120 temp_result
= eval_to_string(expr_start
+ 1, &nextcmd
, FALSE
);
17121 if (temp_result
!= NULL
&& nextcmd
== NULL
)
17123 retval
= alloc((unsigned)(STRLEN(temp_result
) + (expr_start
- in_start
)
17124 + (in_end
- expr_end
) + 1));
17125 if (retval
!= NULL
)
17127 STRCPY(retval
, in_start
);
17128 STRCAT(retval
, temp_result
);
17129 STRCAT(retval
, expr_end
+ 1);
17132 vim_free(temp_result
);
17134 *in_end
= c1
; /* put char back for error messages */
17138 if (retval
!= NULL
)
17140 temp_result
= find_name_end(retval
, &expr_start
, &expr_end
, 0);
17141 if (expr_start
!= NULL
)
17143 /* Further expansion! */
17144 temp_result
= make_expanded_name(retval
, expr_start
,
17145 expr_end
, temp_result
);
17147 retval
= temp_result
;
17155 * Return TRUE if character "c" can be used in a variable or function name.
17156 * Does not include '{' or '}' for magic braces.
17162 return (ASCII_ISALNUM(c
) || c
== '_' || c
== ':' || c
== AUTOLOAD_CHAR
);
17166 * Return TRUE if character "c" can be used as the first character in a
17167 * variable or function name (excluding '{' and '}').
17173 return (ASCII_ISALPHA(c
) || c
== '_');
17177 * Set number v: variable to "val".
17180 set_vim_var_nr(idx
, val
)
17184 vimvars
[idx
].vv_nr
= val
;
17188 * Get number v: variable value.
17191 get_vim_var_nr(idx
)
17194 return vimvars
[idx
].vv_nr
;
17197 #if defined(FEAT_AUTOCMD) || defined(PROTO)
17199 * Get string v: variable value. Uses a static buffer, can only be used once.
17202 get_vim_var_str(idx
)
17205 return get_tv_string(&vimvars
[idx
].vv_tv
);
17210 * Set v:count, v:count1 and v:prevcount.
17213 set_vcount(count
, count1
)
17217 vimvars
[VV_PREVCOUNT
].vv_nr
= vimvars
[VV_COUNT
].vv_nr
;
17218 vimvars
[VV_COUNT
].vv_nr
= count
;
17219 vimvars
[VV_COUNT1
].vv_nr
= count1
;
17223 * Set string v: variable to a copy of "val".
17226 set_vim_var_string(idx
, val
, len
)
17229 int len
; /* length of "val" to use or -1 (whole string) */
17231 /* Need to do this (at least) once, since we can't initialize a union.
17232 * Will always be invoked when "v:progname" is set. */
17233 vimvars
[VV_VERSION
].vv_nr
= VIM_VERSION_100
;
17235 vim_free(vimvars
[idx
].vv_str
);
17237 vimvars
[idx
].vv_str
= NULL
;
17238 else if (len
== -1)
17239 vimvars
[idx
].vv_str
= vim_strsave(val
);
17241 vimvars
[idx
].vv_str
= vim_strnsave(val
, len
);
17245 * Set v:register if needed.
17253 if (c
== 0 || c
== ' ')
17257 /* Avoid free/alloc when the value is already right. */
17258 if (vimvars
[VV_REG
].vv_str
== NULL
|| vimvars
[VV_REG
].vv_str
[0] != c
)
17259 set_vim_var_string(VV_REG
, ®name
, 1);
17263 * Get or set v:exception. If "oldval" == NULL, return the current value.
17264 * Otherwise, restore the value to "oldval" and return NULL.
17265 * Must always be called in pairs to save and restore v:exception! Does not
17266 * take care of memory allocations.
17269 v_exception(oldval
)
17272 if (oldval
== NULL
)
17273 return vimvars
[VV_EXCEPTION
].vv_str
;
17275 vimvars
[VV_EXCEPTION
].vv_str
= oldval
;
17280 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17281 * Otherwise, restore the value to "oldval" and return NULL.
17282 * Must always be called in pairs to save and restore v:throwpoint! Does not
17283 * take care of memory allocations.
17286 v_throwpoint(oldval
)
17289 if (oldval
== NULL
)
17290 return vimvars
[VV_THROWPOINT
].vv_str
;
17292 vimvars
[VV_THROWPOINT
].vv_str
= oldval
;
17296 #if defined(FEAT_AUTOCMD) || defined(PROTO)
17299 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17300 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17301 * Must always be called in pairs!
17304 set_cmdarg(eap
, oldarg
)
17312 oldval
= vimvars
[VV_CMDARG
].vv_str
;
17316 vimvars
[VV_CMDARG
].vv_str
= oldarg
;
17320 if (eap
->force_bin
== FORCE_BIN
)
17322 else if (eap
->force_bin
== FORCE_NOBIN
)
17327 if (eap
->read_edit
)
17330 if (eap
->force_ff
!= 0)
17331 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_ff
) + 6;
17333 if (eap
->force_enc
!= 0)
17334 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_enc
) + 7;
17335 if (eap
->bad_char
!= 0)
17336 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->bad_char
) + 7;
17339 newval
= alloc(len
+ 1);
17340 if (newval
== NULL
)
17343 if (eap
->force_bin
== FORCE_BIN
)
17344 sprintf((char *)newval
, " ++bin");
17345 else if (eap
->force_bin
== FORCE_NOBIN
)
17346 sprintf((char *)newval
, " ++nobin");
17350 if (eap
->read_edit
)
17351 STRCAT(newval
, " ++edit");
17353 if (eap
->force_ff
!= 0)
17354 sprintf((char *)newval
+ STRLEN(newval
), " ++ff=%s",
17355 eap
->cmd
+ eap
->force_ff
);
17357 if (eap
->force_enc
!= 0)
17358 sprintf((char *)newval
+ STRLEN(newval
), " ++enc=%s",
17359 eap
->cmd
+ eap
->force_enc
);
17360 if (eap
->bad_char
!= 0)
17361 sprintf((char *)newval
+ STRLEN(newval
), " ++bad=%s",
17362 eap
->cmd
+ eap
->bad_char
);
17364 vimvars
[VV_CMDARG
].vv_str
= newval
;
17370 * Get the value of internal variable "name".
17371 * Return OK or FAIL.
17374 get_var_tv(name
, len
, rettv
, verbose
)
17376 int len
; /* length of "name" */
17377 typval_T
*rettv
; /* NULL when only checking existence */
17378 int verbose
; /* may give error message */
17381 typval_T
*tv
= NULL
;
17386 /* truncate the name, so that we can use strcmp() */
17391 * Check for "b:changedtick".
17393 if (STRCMP(name
, "b:changedtick") == 0)
17395 atv
.v_type
= VAR_NUMBER
;
17396 atv
.vval
.v_number
= curbuf
->b_changedtick
;
17401 * Check for user-defined variables.
17405 v
= find_var(name
, NULL
);
17412 if (rettv
!= NULL
&& verbose
)
17413 EMSG2(_(e_undefvar
), name
);
17416 else if (rettv
!= NULL
)
17417 copy_tv(tv
, rettv
);
17425 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17426 * Also handle function call with Funcref variable: func(expr)
17427 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17430 handle_subscript(arg
, rettv
, evaluate
, verbose
)
17433 int evaluate
; /* do more than finding the end */
17434 int verbose
; /* give error messages */
17437 dict_T
*selfdict
= NULL
;
17444 || (**arg
== '.' && rettv
->v_type
== VAR_DICT
)
17445 || (**arg
== '(' && rettv
->v_type
== VAR_FUNC
))
17446 && !vim_iswhite(*(*arg
- 1)))
17450 /* need to copy the funcref so that we can clear rettv */
17452 rettv
->v_type
= VAR_UNKNOWN
;
17454 /* Invoke the function. Recursive! */
17455 s
= functv
.vval
.v_string
;
17456 ret
= get_func_tv(s
, (int)STRLEN(s
), rettv
, arg
,
17457 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
17458 &len
, evaluate
, selfdict
);
17460 /* Clear the funcref afterwards, so that deleting it while
17461 * evaluating the arguments is possible (see test55). */
17464 /* Stop the expression evaluation when immediately aborting on
17465 * error, or when an interrupt occurred or an exception was thrown
17466 * but not caught. */
17473 dict_unref(selfdict
);
17476 else /* **arg == '[' || **arg == '.' */
17478 dict_unref(selfdict
);
17479 if (rettv
->v_type
== VAR_DICT
)
17481 selfdict
= rettv
->vval
.v_dict
;
17482 if (selfdict
!= NULL
)
17483 ++selfdict
->dv_refcount
;
17487 if (eval_index(arg
, rettv
, evaluate
, verbose
) == FAIL
)
17494 dict_unref(selfdict
);
17499 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17505 return (typval_T
*)alloc_clear((unsigned)sizeof(typval_T
));
17509 * Allocate memory for a variable type-value, and assign a string to it.
17510 * The string "s" must have been allocated, it is consumed.
17511 * Return NULL for out of memory, the variable otherwise.
17519 rettv
= alloc_tv();
17522 rettv
->v_type
= VAR_STRING
;
17523 rettv
->vval
.v_string
= s
;
17531 * Free the memory for a variable type-value.
17539 switch (varp
->v_type
)
17542 func_unref(varp
->vval
.v_string
);
17545 vim_free(varp
->vval
.v_string
);
17548 list_unref(varp
->vval
.v_list
);
17551 dict_unref(varp
->vval
.v_dict
);
17557 EMSG2(_(e_intern2
), "free_tv()");
17565 * Free the memory for a variable value and set the value to NULL or 0.
17573 switch (varp
->v_type
)
17576 func_unref(varp
->vval
.v_string
);
17579 vim_free(varp
->vval
.v_string
);
17580 varp
->vval
.v_string
= NULL
;
17583 list_unref(varp
->vval
.v_list
);
17584 varp
->vval
.v_list
= NULL
;
17587 dict_unref(varp
->vval
.v_dict
);
17588 varp
->vval
.v_dict
= NULL
;
17591 varp
->vval
.v_number
= 0;
17596 EMSG2(_(e_intern2
), "clear_tv()");
17603 * Set the value of a variable to NULL without freeing items.
17610 vim_memset(varp
, 0, sizeof(typval_T
));
17614 * Get the number value of a variable.
17615 * If it is a String variable, uses vim_str2nr().
17616 * For incompatible types, return 0.
17617 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17618 * caller of incompatible types: it sets *denote to TRUE if "denote"
17619 * is not NULL or returns -1 otherwise.
17622 get_tv_number(varp
)
17627 return get_tv_number_chk(varp
, &error
); /* return 0L on error */
17631 get_tv_number_chk(varp
, denote
)
17637 switch (varp
->v_type
)
17640 return (long)(varp
->vval
.v_number
);
17642 EMSG(_("E703: Using a Funcref as a number"));
17645 if (varp
->vval
.v_string
!= NULL
)
17646 vim_str2nr(varp
->vval
.v_string
, NULL
, NULL
,
17647 TRUE
, TRUE
, &n
, NULL
);
17650 EMSG(_("E745: Using a List as a number"));
17653 EMSG(_("E728: Using a Dictionary as a number"));
17656 EMSG2(_(e_intern2
), "get_tv_number()");
17659 if (denote
== NULL
) /* useful for values that must be unsigned */
17667 * Get the lnum from the first argument.
17668 * Also accepts ".", "$", etc., but that only works for the current buffer.
17669 * Returns -1 on error.
17672 get_tv_lnum(argvars
)
17678 lnum
= get_tv_number_chk(&argvars
[0], NULL
);
17679 if (lnum
== 0) /* no valid number, try using line() */
17681 rettv
.v_type
= VAR_NUMBER
;
17682 f_line(argvars
, &rettv
);
17683 lnum
= rettv
.vval
.v_number
;
17690 * Get the lnum from the first argument.
17691 * Also accepts "$", then "buf" is used.
17692 * Returns 0 on error.
17695 get_tv_lnum_buf(argvars
, buf
)
17699 if (argvars
[0].v_type
== VAR_STRING
17700 && argvars
[0].vval
.v_string
!= NULL
17701 && argvars
[0].vval
.v_string
[0] == '$'
17703 return buf
->b_ml
.ml_line_count
;
17704 return get_tv_number_chk(&argvars
[0], NULL
);
17708 * Get the string value of a variable.
17709 * If it is a Number variable, the number is converted into a string.
17710 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17711 * get_tv_string_buf() uses a given buffer.
17712 * If the String variable has never been set, return an empty string.
17713 * Never returns NULL;
17714 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17718 get_tv_string(varp
)
17721 static char_u mybuf
[NUMBUFLEN
];
17723 return get_tv_string_buf(varp
, mybuf
);
17727 get_tv_string_buf(varp
, buf
)
17731 char_u
*res
= get_tv_string_buf_chk(varp
, buf
);
17733 return res
!= NULL
? res
: (char_u
*)"";
17737 get_tv_string_chk(varp
)
17740 static char_u mybuf
[NUMBUFLEN
];
17742 return get_tv_string_buf_chk(varp
, mybuf
);
17746 get_tv_string_buf_chk(varp
, buf
)
17750 switch (varp
->v_type
)
17753 sprintf((char *)buf
, "%ld", (long)varp
->vval
.v_number
);
17756 EMSG(_("E729: using Funcref as a String"));
17759 EMSG(_("E730: using List as a String"));
17762 EMSG(_("E731: using Dictionary as a String"));
17765 if (varp
->vval
.v_string
!= NULL
)
17766 return varp
->vval
.v_string
;
17767 return (char_u
*)"";
17769 EMSG2(_(e_intern2
), "get_tv_string_buf()");
17776 * Find variable "name" in the list of variables.
17777 * Return a pointer to it if found, NULL if not found.
17778 * Careful: "a:0" variables don't have a name.
17779 * When "htp" is not NULL we are writing to the variable, set "htp" to the
17782 static dictitem_T
*
17783 find_var(name
, htp
)
17790 ht
= find_var_ht(name
, &varname
);
17795 return find_var_in_ht(ht
, varname
, htp
!= NULL
);
17799 * Find variable "varname" in hashtab "ht".
17800 * Returns NULL if not found.
17802 static dictitem_T
*
17803 find_var_in_ht(ht
, varname
, writing
)
17810 if (*varname
== NUL
)
17812 /* Must be something like "s:", otherwise "ht" would be NULL. */
17813 switch (varname
[-2])
17815 case 's': return &SCRIPT_SV(current_SID
).sv_var
;
17816 case 'g': return &globvars_var
;
17817 case 'v': return &vimvars_var
;
17818 case 'b': return &curbuf
->b_bufvar
;
17819 case 'w': return &curwin
->w_winvar
;
17820 #ifdef FEAT_WINDOWS
17821 case 't': return &curtab
->tp_winvar
;
17823 case 'l': return current_funccal
== NULL
17824 ? NULL
: ¤t_funccal
->l_vars_var
;
17825 case 'a': return current_funccal
== NULL
17826 ? NULL
: ¤t_funccal
->l_avars_var
;
17831 hi
= hash_find(ht
, varname
);
17832 if (HASHITEM_EMPTY(hi
))
17834 /* For global variables we may try auto-loading the script. If it
17835 * worked find the variable again. Don't auto-load a script if it was
17836 * loaded already, otherwise it would be loaded every time when
17837 * checking if a function name is a Funcref variable. */
17838 if (ht
== &globvarht
&& !writing
17839 && script_autoload(varname
, FALSE
) && !aborting())
17840 hi
= hash_find(ht
, varname
);
17841 if (HASHITEM_EMPTY(hi
))
17848 * Find the hashtab used for a variable name.
17849 * Set "varname" to the start of name without ':'.
17852 find_var_ht(name
, varname
)
17858 if (name
[1] != ':')
17860 /* The name must not start with a colon or #. */
17861 if (name
[0] == ':' || name
[0] == AUTOLOAD_CHAR
)
17865 /* "version" is "v:version" in all scopes */
17866 hi
= hash_find(&compat_hashtab
, name
);
17867 if (!HASHITEM_EMPTY(hi
))
17868 return &compat_hashtab
;
17870 if (current_funccal
== NULL
)
17871 return &globvarht
; /* global variable */
17872 return ¤t_funccal
->l_vars
.dv_hashtab
; /* l: variable */
17874 *varname
= name
+ 2;
17875 if (*name
== 'g') /* global variable */
17877 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17879 if (vim_strchr(name
+ 2, ':') != NULL
17880 || vim_strchr(name
+ 2, AUTOLOAD_CHAR
) != NULL
)
17882 if (*name
== 'b') /* buffer variable */
17883 return &curbuf
->b_vars
.dv_hashtab
;
17884 if (*name
== 'w') /* window variable */
17885 return &curwin
->w_vars
.dv_hashtab
;
17886 #ifdef FEAT_WINDOWS
17887 if (*name
== 't') /* tab page variable */
17888 return &curtab
->tp_vars
.dv_hashtab
;
17890 if (*name
== 'v') /* v: variable */
17892 if (*name
== 'a' && current_funccal
!= NULL
) /* function argument */
17893 return ¤t_funccal
->l_avars
.dv_hashtab
;
17894 if (*name
== 'l' && current_funccal
!= NULL
) /* local function variable */
17895 return ¤t_funccal
->l_vars
.dv_hashtab
;
17896 if (*name
== 's' /* script variable */
17897 && current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
17898 return &SCRIPT_VARS(current_SID
);
17903 * Get the string value of a (global/local) variable.
17904 * Returns NULL when it doesn't exist.
17907 get_var_value(name
)
17912 v
= find_var(name
, NULL
);
17915 return get_tv_string(&v
->di_tv
);
17919 * Allocate a new hashtab for a sourced script. It will be used while
17920 * sourcing this script and when executing functions defined in the script.
17923 new_script_vars(id
)
17930 if (ga_grow(&ga_scripts
, (int)(id
- ga_scripts
.ga_len
)) == OK
)
17932 /* Re-allocating ga_data means that an ht_array pointing to
17933 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
17934 * at its init value. Also reset "v_dict", it's always the same. */
17935 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
17937 ht
= &SCRIPT_VARS(i
);
17938 if (ht
->ht_mask
== HT_INIT_SIZE
- 1)
17939 ht
->ht_array
= ht
->ht_smallarray
;
17940 sv
= &SCRIPT_SV(i
);
17941 sv
->sv_var
.di_tv
.vval
.v_dict
= &sv
->sv_dict
;
17944 while (ga_scripts
.ga_len
< id
)
17946 sv
= &SCRIPT_SV(ga_scripts
.ga_len
+ 1);
17947 init_var_dict(&sv
->sv_dict
, &sv
->sv_var
);
17948 ++ga_scripts
.ga_len
;
17954 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17958 init_var_dict(dict
, dict_var
)
17960 dictitem_T
*dict_var
;
17962 hash_init(&dict
->dv_hashtab
);
17963 dict
->dv_refcount
= 99999;
17964 dict_var
->di_tv
.vval
.v_dict
= dict
;
17965 dict_var
->di_tv
.v_type
= VAR_DICT
;
17966 dict_var
->di_tv
.v_lock
= VAR_FIXED
;
17967 dict_var
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
17968 dict_var
->di_key
[0] = NUL
;
17972 * Clean up a list of internal variables.
17973 * Frees all allocated variables and the value they contain.
17974 * Clears hashtab "ht", does not free it.
17980 vars_clear_ext(ht
, TRUE
);
17984 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17987 vars_clear_ext(ht
, free_val
)
17996 todo
= (int)ht
->ht_used
;
17997 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
17999 if (!HASHITEM_EMPTY(hi
))
18003 /* Free the variable. Don't remove it from the hashtab,
18004 * ht_array might change then. hash_clear() takes care of it
18008 clear_tv(&v
->di_tv
);
18009 if ((v
->di_flags
& DI_FLAGS_FIX
) == 0)
18018 * Delete a variable from hashtab "ht" at item "hi".
18019 * Clear the variable value and free the dictitem.
18026 dictitem_T
*di
= HI2DI(hi
);
18028 hash_remove(ht
, hi
);
18029 clear_tv(&di
->di_tv
);
18034 * List the value of one internal variable.
18037 list_one_var(v
, prefix
, first
)
18044 char_u numbuf
[NUMBUFLEN
];
18046 s
= echo_string(&v
->di_tv
, &tofree
, numbuf
, ++current_copyID
);
18047 list_one_var_a(prefix
, v
->di_key
, v
->di_tv
.v_type
,
18048 s
== NULL
? (char_u
*)"" : s
, first
);
18053 list_one_var_a(prefix
, name
, type
, string
, first
)
18058 int *first
; /* when TRUE clear rest of screen and set to FALSE */
18060 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18063 if (name
!= NULL
) /* "a:" vars don't have a name stored */
18067 if (type
== VAR_NUMBER
)
18069 else if (type
== VAR_FUNC
)
18071 else if (type
== VAR_LIST
)
18074 if (*string
== '[')
18077 else if (type
== VAR_DICT
)
18080 if (*string
== '{')
18086 msg_outtrans(string
);
18088 if (type
== VAR_FUNC
)
18089 msg_puts((char_u
*)"()");
18098 * Set variable "name" to value in "tv".
18099 * If the variable already exists, the value is updated.
18100 * Otherwise the variable is created.
18103 set_var(name
, tv
, copy
)
18106 int copy
; /* make copy of value in "tv" */
18113 if (tv
->v_type
== VAR_FUNC
)
18115 if (!(vim_strchr((char_u
*)"wbs", name
[0]) != NULL
&& name
[1] == ':')
18116 && !ASCII_ISUPPER((name
[0] != NUL
&& name
[1] == ':')
18117 ? name
[2] : name
[0]))
18119 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name
);
18122 if (function_exists(name
))
18124 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
18130 ht
= find_var_ht(name
, &varname
);
18131 if (ht
== NULL
|| *varname
== NUL
)
18133 EMSG2(_(e_illvar
), name
);
18137 v
= find_var_in_ht(ht
, varname
, TRUE
);
18140 /* existing variable, need to clear the value */
18141 if (var_check_ro(v
->di_flags
, name
)
18142 || tv_check_lock(v
->di_tv
.v_lock
, name
))
18144 if (v
->di_tv
.v_type
!= tv
->v_type
18145 && !((v
->di_tv
.v_type
== VAR_STRING
18146 || v
->di_tv
.v_type
== VAR_NUMBER
)
18147 && (tv
->v_type
== VAR_STRING
18148 || tv
->v_type
== VAR_NUMBER
)))
18150 EMSG2(_("E706: Variable type mismatch for: %s"), name
);
18155 * Handle setting internal v: variables separately: we don't change
18158 if (ht
== &vimvarht
)
18160 if (v
->di_tv
.v_type
== VAR_STRING
)
18162 vim_free(v
->di_tv
.vval
.v_string
);
18163 if (copy
|| tv
->v_type
!= VAR_STRING
)
18164 v
->di_tv
.vval
.v_string
= vim_strsave(get_tv_string(tv
));
18167 /* Take over the string to avoid an extra alloc/free. */
18168 v
->di_tv
.vval
.v_string
= tv
->vval
.v_string
;
18169 tv
->vval
.v_string
= NULL
;
18172 else if (v
->di_tv
.v_type
!= VAR_NUMBER
)
18173 EMSG2(_(e_intern2
), "set_var()");
18175 v
->di_tv
.vval
.v_number
= get_tv_number(tv
);
18179 clear_tv(&v
->di_tv
);
18181 else /* add a new variable */
18183 /* Can't add "v:" variable. */
18184 if (ht
== &vimvarht
)
18186 EMSG2(_(e_illvar
), name
);
18190 /* Make sure the variable name is valid. */
18191 for (p
= varname
; *p
!= NUL
; ++p
)
18192 if (!eval_isnamec1(*p
) && (p
== varname
|| !VIM_ISDIGIT(*p
))
18193 && *p
!= AUTOLOAD_CHAR
)
18195 EMSG2(_(e_illvar
), varname
);
18199 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
18200 + STRLEN(varname
)));
18203 STRCPY(v
->di_key
, varname
);
18204 if (hash_add(ht
, DI2HIKEY(v
)) == FAIL
)
18212 if (copy
|| tv
->v_type
== VAR_NUMBER
)
18213 copy_tv(tv
, &v
->di_tv
);
18217 v
->di_tv
.v_lock
= 0;
18223 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
18224 * Also give an error message.
18227 var_check_ro(flags
, name
)
18231 if (flags
& DI_FLAGS_RO
)
18233 EMSG2(_(e_readonlyvar
), name
);
18236 if ((flags
& DI_FLAGS_RO_SBX
) && sandbox
)
18238 EMSG2(_(e_readonlysbx
), name
);
18245 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
18246 * Also give an error message.
18249 var_check_fixed(flags
, name
)
18253 if (flags
& DI_FLAGS_FIX
)
18255 EMSG2(_("E795: Cannot delete variable %s"), name
);
18262 * Return TRUE if typeval "tv" is set to be locked (immutable).
18263 * Also give an error message, using "name".
18266 tv_check_lock(lock
, name
)
18270 if (lock
& VAR_LOCKED
)
18272 EMSG2(_("E741: Value is locked: %s"),
18273 name
== NULL
? (char_u
*)_("Unknown") : name
);
18276 if (lock
& VAR_FIXED
)
18278 EMSG2(_("E742: Cannot change value of %s"),
18279 name
== NULL
? (char_u
*)_("Unknown") : name
);
18286 * Copy the values from typval_T "from" to typval_T "to".
18287 * When needed allocates string or increases reference count.
18288 * Does not make a copy of a list or dict but copies the reference!
18295 to
->v_type
= from
->v_type
;
18297 switch (from
->v_type
)
18300 to
->vval
.v_number
= from
->vval
.v_number
;
18304 if (from
->vval
.v_string
== NULL
)
18305 to
->vval
.v_string
= NULL
;
18308 to
->vval
.v_string
= vim_strsave(from
->vval
.v_string
);
18309 if (from
->v_type
== VAR_FUNC
)
18310 func_ref(to
->vval
.v_string
);
18314 if (from
->vval
.v_list
== NULL
)
18315 to
->vval
.v_list
= NULL
;
18318 to
->vval
.v_list
= from
->vval
.v_list
;
18319 ++to
->vval
.v_list
->lv_refcount
;
18323 if (from
->vval
.v_dict
== NULL
)
18324 to
->vval
.v_dict
= NULL
;
18327 to
->vval
.v_dict
= from
->vval
.v_dict
;
18328 ++to
->vval
.v_dict
->dv_refcount
;
18332 EMSG2(_(e_intern2
), "copy_tv()");
18338 * Make a copy of an item.
18339 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
18340 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18341 * reference to an already copied list/dict can be used.
18342 * Returns FAIL or OK.
18345 item_copy(from
, to
, deep
, copyID
)
18351 static int recurse
= 0;
18354 if (recurse
>= DICT_MAXNEST
)
18356 EMSG(_("E698: variable nested too deep for making a copy"));
18361 switch (from
->v_type
)
18369 to
->v_type
= VAR_LIST
;
18371 if (from
->vval
.v_list
== NULL
)
18372 to
->vval
.v_list
= NULL
;
18373 else if (copyID
!= 0 && from
->vval
.v_list
->lv_copyID
== copyID
)
18375 /* use the copy made earlier */
18376 to
->vval
.v_list
= from
->vval
.v_list
->lv_copylist
;
18377 ++to
->vval
.v_list
->lv_refcount
;
18380 to
->vval
.v_list
= list_copy(from
->vval
.v_list
, deep
, copyID
);
18381 if (to
->vval
.v_list
== NULL
)
18385 to
->v_type
= VAR_DICT
;
18387 if (from
->vval
.v_dict
== NULL
)
18388 to
->vval
.v_dict
= NULL
;
18389 else if (copyID
!= 0 && from
->vval
.v_dict
->dv_copyID
== copyID
)
18391 /* use the copy made earlier */
18392 to
->vval
.v_dict
= from
->vval
.v_dict
->dv_copydict
;
18393 ++to
->vval
.v_dict
->dv_refcount
;
18396 to
->vval
.v_dict
= dict_copy(from
->vval
.v_dict
, deep
, copyID
);
18397 if (to
->vval
.v_dict
== NULL
)
18401 EMSG2(_(e_intern2
), "item_copy()");
18409 * ":echo expr1 ..." print each argument separated with a space, add a
18410 * newline at the end.
18411 * ":echon expr1 ..." print each argument plain.
18417 char_u
*arg
= eap
->arg
;
18421 int needclr
= TRUE
;
18422 int atstart
= TRUE
;
18423 char_u numbuf
[NUMBUFLEN
];
18427 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n' && !got_int
)
18430 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
18433 * Report the invalid expression unless the expression evaluation
18434 * has been cancelled due to an aborting error, an interrupt, or an
18438 EMSG2(_(e_invexpr2
), p
);
18446 /* Call msg_start() after eval1(), evaluating the expression
18447 * may cause a message to appear. */
18448 if (eap
->cmdidx
== CMD_echo
)
18451 else if (eap
->cmdidx
== CMD_echo
)
18452 msg_puts_attr((char_u
*)" ", echo_attr
);
18453 p
= echo_string(&rettv
, &tofree
, numbuf
, ++current_copyID
);
18455 for ( ; *p
!= NUL
&& !got_int
; ++p
)
18457 if (*p
== '\n' || *p
== '\r' || *p
== TAB
)
18459 if (*p
!= TAB
&& needclr
)
18461 /* remove any text still there from the command */
18465 msg_putchar_attr(*p
, echo_attr
);
18472 int i
= (*mb_ptr2len
)(p
);
18474 (void)msg_outtrans_len_attr(p
, i
, echo_attr
);
18479 (void)msg_outtrans_len_attr(p
, 1, echo_attr
);
18485 arg
= skipwhite(arg
);
18487 eap
->nextcmd
= check_nextcmd(arg
);
18493 /* remove text that may still be there from the command */
18496 if (eap
->cmdidx
== CMD_echo
)
18502 * ":echohl {name}".
18510 id
= syn_name2id(eap
->arg
);
18514 echo_attr
= syn_id2attr(id
);
18518 * ":execute expr1 ..." execute the result of an expression.
18519 * ":echomsg expr1 ..." Print a message
18520 * ":echoerr expr1 ..." Print an error
18521 * Each gets spaces around each argument and a newline at the end for
18528 char_u
*arg
= eap
->arg
;
18536 ga_init2(&ga
, 1, 80);
18540 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
18543 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
18546 * Report the invalid expression unless the expression evaluation
18547 * has been cancelled due to an aborting error, an interrupt, or an
18551 EMSG2(_(e_invexpr2
), p
);
18558 p
= get_tv_string(&rettv
);
18559 len
= (int)STRLEN(p
);
18560 if (ga_grow(&ga
, len
+ 2) == FAIL
)
18567 ((char_u
*)(ga
.ga_data
))[ga
.ga_len
++] = ' ';
18568 STRCPY((char_u
*)(ga
.ga_data
) + ga
.ga_len
, p
);
18573 arg
= skipwhite(arg
);
18576 if (ret
!= FAIL
&& ga
.ga_data
!= NULL
)
18578 if (eap
->cmdidx
== CMD_echomsg
)
18580 MSG_ATTR(ga
.ga_data
, echo_attr
);
18583 else if (eap
->cmdidx
== CMD_echoerr
)
18585 /* We don't want to abort following commands, restore did_emsg. */
18586 save_did_emsg
= did_emsg
;
18587 EMSG((char_u
*)ga
.ga_data
);
18589 did_emsg
= save_did_emsg
;
18591 else if (eap
->cmdidx
== CMD_execute
)
18592 do_cmdline((char_u
*)ga
.ga_data
,
18593 eap
->getline
, eap
->cookie
, DOCMD_NOWAIT
|DOCMD_VERBOSE
);
18601 eap
->nextcmd
= check_nextcmd(arg
);
18605 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18606 * "arg" points to the "&" or '+' when called, to "option" when returning.
18607 * Returns NULL when no option name found. Otherwise pointer to the char
18608 * after the option name.
18611 find_option_end(arg
, opt_flags
)
18618 if (*p
== 'g' && p
[1] == ':')
18620 *opt_flags
= OPT_GLOBAL
;
18623 else if (*p
== 'l' && p
[1] == ':')
18625 *opt_flags
= OPT_LOCAL
;
18631 if (!ASCII_ISALPHA(*p
))
18635 if (p
[0] == 't' && p
[1] == '_' && p
[2] != NUL
&& p
[3] != NUL
)
18636 p
+= 4; /* termcap option */
18638 while (ASCII_ISALPHA(*p
))
18653 int saved_did_emsg
;
18654 char_u
*name
= NULL
;
18657 char_u
*line_arg
= NULL
;
18660 int varargs
= FALSE
;
18661 int mustend
= FALSE
;
18666 char_u
*skip_until
= NULL
;
18669 static int func_nr
= 0; /* number for nameless function */
18674 int sourcing_lnum_off
;
18677 * ":function" without argument: list functions.
18679 if (ends_excmd(*eap
->arg
))
18683 todo
= (int)func_hashtab
.ht_used
;
18684 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
18686 if (!HASHITEM_EMPTY(hi
))
18690 if (!isdigit(*fp
->uf_name
))
18691 list_func_head(fp
, FALSE
);
18695 eap
->nextcmd
= check_nextcmd(eap
->arg
);
18700 * ":function /pat": list functions matching pattern.
18702 if (*eap
->arg
== '/')
18704 p
= skip_regexp(eap
->arg
+ 1, '/', TRUE
, NULL
);
18707 regmatch_T regmatch
;
18711 regmatch
.regprog
= vim_regcomp(eap
->arg
+ 1, RE_MAGIC
);
18713 if (regmatch
.regprog
!= NULL
)
18715 regmatch
.rm_ic
= p_ic
;
18717 todo
= (int)func_hashtab
.ht_used
;
18718 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
18720 if (!HASHITEM_EMPTY(hi
))
18724 if (!isdigit(*fp
->uf_name
)
18725 && vim_regexec(®match
, fp
->uf_name
, 0))
18726 list_func_head(fp
, FALSE
);
18733 eap
->nextcmd
= check_nextcmd(p
);
18738 * Get the function name. There are these situations:
18739 * func normal function name
18740 * "name" == func, "fudi.fd_dict" == NULL
18741 * dict.func new dictionary entry
18742 * "name" == NULL, "fudi.fd_dict" set,
18743 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18744 * dict.func existing dict entry with a Funcref
18745 * "name" == func, "fudi.fd_dict" set,
18746 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18747 * dict.func existing dict entry that's not a Funcref
18748 * "name" == NULL, "fudi.fd_dict" set,
18749 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18752 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
18753 paren
= (vim_strchr(p
, '(') != NULL
);
18754 if (name
== NULL
&& (fudi
.fd_dict
== NULL
|| !paren
) && !eap
->skip
)
18757 * Return on an invalid expression in braces, unless the expression
18758 * evaluation has been cancelled due to an aborting error, an
18759 * interrupt, or an exception.
18763 if (!eap
->skip
&& fudi
.fd_newkey
!= NULL
)
18764 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
18765 vim_free(fudi
.fd_newkey
);
18772 /* An error in a function call during evaluation of an expression in magic
18773 * braces should not cause the function not to be defined. */
18774 saved_did_emsg
= did_emsg
;
18778 * ":function func" with only function name: list function.
18782 if (!ends_excmd(*skipwhite(p
)))
18784 EMSG(_(e_trailing
));
18787 eap
->nextcmd
= check_nextcmd(p
);
18788 if (eap
->nextcmd
!= NULL
)
18790 if (!eap
->skip
&& !got_int
)
18792 fp
= find_func(name
);
18795 list_func_head(fp
, TRUE
);
18796 for (j
= 0; j
< fp
->uf_lines
.ga_len
&& !got_int
; ++j
)
18798 if (FUNCLINE(fp
, j
) == NULL
)
18801 msg_outnum((long)(j
+ 1));
18806 msg_prt_line(FUNCLINE(fp
, j
), FALSE
);
18807 out_flush(); /* show a line at a time */
18813 msg_puts((char_u
*)" endfunction");
18817 emsg_funcname("E123: Undefined function: %s", name
);
18823 * ":function name(arg1, arg2)" Define function.
18830 EMSG2(_("E124: Missing '(': %s"), eap
->arg
);
18833 /* attempt to continue by skipping some text */
18834 if (vim_strchr(p
, '(') != NULL
)
18835 p
= vim_strchr(p
, '(');
18837 p
= skipwhite(p
+ 1);
18839 ga_init2(&newargs
, (int)sizeof(char_u
*), 3);
18840 ga_init2(&newlines
, (int)sizeof(char_u
*), 3);
18844 /* Check the name of the function. Unless it's a dictionary function
18845 * (that we are overwriting). */
18849 arg
= fudi
.fd_newkey
;
18850 if (arg
!= NULL
&& (fudi
.fd_di
== NULL
18851 || fudi
.fd_di
->di_tv
.v_type
!= VAR_FUNC
))
18853 if (*arg
== K_SPECIAL
)
18857 while (arg
[j
] != NUL
&& (j
== 0 ? eval_isnamec1(arg
[j
])
18858 : eval_isnamec(arg
[j
])))
18861 emsg_funcname(_(e_invarg2
), arg
);
18866 * Isolate the arguments: "arg1, arg2, ...)"
18870 if (p
[0] == '.' && p
[1] == '.' && p
[2] == '.')
18879 while (ASCII_ISALNUM(*p
) || *p
== '_')
18881 if (arg
== p
|| isdigit(*arg
)
18882 || (p
- arg
== 9 && STRNCMP(arg
, "firstline", 9) == 0)
18883 || (p
- arg
== 8 && STRNCMP(arg
, "lastline", 8) == 0))
18886 EMSG2(_("E125: Illegal argument: %s"), arg
);
18889 if (ga_grow(&newargs
, 1) == FAIL
)
18893 arg
= vim_strsave(arg
);
18896 ((char_u
**)(newargs
.ga_data
))[newargs
.ga_len
] = arg
;
18905 if (mustend
&& *p
!= ')')
18908 EMSG2(_(e_invarg2
), eap
->arg
);
18912 ++p
; /* skip the ')' */
18914 /* find extra arguments "range", "dict" and "abort" */
18918 if (STRNCMP(p
, "range", 5) == 0)
18923 else if (STRNCMP(p
, "dict", 4) == 0)
18928 else if (STRNCMP(p
, "abort", 5) == 0)
18937 /* When there is a line break use what follows for the function body.
18938 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18941 else if (*p
!= NUL
&& *p
!= '"' && !eap
->skip
&& !did_emsg
)
18942 EMSG(_(e_trailing
));
18945 * Read the body of the function, until ":endfunction" is found.
18949 /* Check if the function already exists, don't let the user type the
18950 * whole function before telling him it doesn't work! For a script we
18951 * need to skip the body to be able to find what follows. */
18952 if (!eap
->skip
&& !eap
->forceit
)
18954 if (fudi
.fd_dict
!= NULL
&& fudi
.fd_newkey
== NULL
)
18955 EMSG(_(e_funcdict
));
18956 else if (name
!= NULL
&& find_func(name
) != NULL
)
18957 emsg_funcname(e_funcexts
, name
);
18960 if (!eap
->skip
&& did_emsg
)
18963 msg_putchar('\n'); /* don't overwrite the function name */
18964 cmdline_row
= msg_row
;
18972 need_wait_return
= FALSE
;
18973 sourcing_lnum_off
= sourcing_lnum
;
18975 if (line_arg
!= NULL
)
18977 /* Use eap->arg, split up in parts by line breaks. */
18978 theline
= line_arg
;
18979 p
= vim_strchr(theline
, '\n');
18981 line_arg
+= STRLEN(line_arg
);
18988 else if (eap
->getline
== NULL
)
18989 theline
= getcmdline(':', 0L, indent
);
18991 theline
= eap
->getline(':', eap
->cookie
, indent
);
18993 lines_left
= Rows
- 1;
18994 if (theline
== NULL
)
18996 EMSG(_("E126: Missing :endfunction"));
19000 /* Detect line continuation: sourcing_lnum increased more than one. */
19001 if (sourcing_lnum
> sourcing_lnum_off
+ 1)
19002 sourcing_lnum_off
= sourcing_lnum
- sourcing_lnum_off
- 1;
19004 sourcing_lnum_off
= 0;
19006 if (skip_until
!= NULL
)
19008 /* between ":append" and "." and between ":python <<EOF" and "EOF"
19009 * don't check for ":endfunc". */
19010 if (STRCMP(theline
, skip_until
) == 0)
19012 vim_free(skip_until
);
19018 /* skip ':' and blanks*/
19019 for (p
= theline
; vim_iswhite(*p
) || *p
== ':'; ++p
)
19022 /* Check for "endfunction". */
19023 if (checkforcmd(&p
, "endfunction", 4) && nesting
-- == 0)
19025 if (line_arg
== NULL
)
19030 /* Increase indent inside "if", "while", "for" and "try", decrease
19032 if (indent
> 2 && STRNCMP(p
, "end", 3) == 0)
19034 else if (STRNCMP(p
, "if", 2) == 0
19035 || STRNCMP(p
, "wh", 2) == 0
19036 || STRNCMP(p
, "for", 3) == 0
19037 || STRNCMP(p
, "try", 3) == 0)
19040 /* Check for defining a function inside this function. */
19041 if (checkforcmd(&p
, "function", 2))
19044 p
= skipwhite(p
+ 1);
19045 p
+= eval_fname_script(p
);
19046 if (ASCII_ISALPHA(*p
))
19048 vim_free(trans_function_name(&p
, TRUE
, 0, NULL
));
19049 if (*skipwhite(p
) == '(')
19057 /* Check for ":append" or ":insert". */
19058 p
= skip_range(p
, NULL
);
19059 if ((p
[0] == 'a' && (!ASCII_ISALPHA(p
[1]) || p
[1] == 'p'))
19061 && (!ASCII_ISALPHA(p
[1]) || (p
[1] == 'n'
19062 && (!ASCII_ISALPHA(p
[2]) || (p
[2] == 's'))))))
19063 skip_until
= vim_strsave((char_u
*)".");
19065 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19066 arg
= skipwhite(skiptowhite(p
));
19067 if (arg
[0] == '<' && arg
[1] =='<'
19068 && ((p
[0] == 'p' && p
[1] == 'y'
19069 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 't'))
19070 || (p
[0] == 'p' && p
[1] == 'e'
19071 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'r'))
19072 || (p
[0] == 't' && p
[1] == 'c'
19073 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'l'))
19074 || (p
[0] == 'r' && p
[1] == 'u' && p
[2] == 'b'
19075 && (!ASCII_ISALPHA(p
[3]) || p
[3] == 'y'))
19076 || (p
[0] == 'm' && p
[1] == 'z'
19077 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 's'))
19080 /* ":python <<" continues until a dot, like ":append" */
19081 p
= skipwhite(arg
+ 2);
19083 skip_until
= vim_strsave((char_u
*)".");
19085 skip_until
= vim_strsave(p
);
19089 /* Add the line to the function. */
19090 if (ga_grow(&newlines
, 1 + sourcing_lnum_off
) == FAIL
)
19092 if (line_arg
== NULL
)
19097 /* Copy the line to newly allocated memory. get_one_sourceline()
19098 * allocates 250 bytes per line, this saves 80% on average. The cost
19099 * is an extra alloc/free. */
19100 p
= vim_strsave(theline
);
19103 if (line_arg
== NULL
)
19108 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = theline
;
19110 /* Add NULL lines for continuation lines, so that the line count is
19111 * equal to the index in the growarray. */
19112 while (sourcing_lnum_off
-- > 0)
19113 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = NULL
;
19115 /* Check for end of eap->arg. */
19116 if (line_arg
!= NULL
&& *line_arg
== NUL
)
19120 /* Don't define the function when skipping commands or when an error was
19122 if (eap
->skip
|| did_emsg
)
19126 * If there are no errors, add the function
19128 if (fudi
.fd_dict
== NULL
)
19130 v
= find_var(name
, &ht
);
19131 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
19133 emsg_funcname("E707: Function name conflicts with variable: %s",
19138 fp
= find_func(name
);
19143 emsg_funcname(e_funcexts
, name
);
19146 if (fp
->uf_calls
> 0)
19148 emsg_funcname("E127: Cannot redefine function %s: It is in use",
19152 /* redefine existing function */
19153 ga_clear_strings(&(fp
->uf_args
));
19154 ga_clear_strings(&(fp
->uf_lines
));
19164 if (fudi
.fd_newkey
== NULL
&& !eap
->forceit
)
19166 EMSG(_(e_funcdict
));
19169 if (fudi
.fd_di
== NULL
)
19171 /* Can't add a function to a locked dictionary */
19172 if (tv_check_lock(fudi
.fd_dict
->dv_lock
, eap
->arg
))
19175 /* Can't change an existing function if it is locked */
19176 else if (tv_check_lock(fudi
.fd_di
->di_tv
.v_lock
, eap
->arg
))
19179 /* Give the function a sequential number. Can only be used with a
19182 sprintf(numbuf
, "%d", ++func_nr
);
19183 name
= vim_strsave((char_u
*)numbuf
);
19190 if (fudi
.fd_dict
== NULL
&& vim_strchr(name
, AUTOLOAD_CHAR
) != NULL
)
19193 char_u
*scriptname
;
19195 /* Check that the autoload name matches the script name. */
19197 if (sourcing_name
!= NULL
)
19199 scriptname
= autoload_name(name
);
19200 if (scriptname
!= NULL
)
19202 p
= vim_strchr(scriptname
, '/');
19203 plen
= (int)STRLEN(p
);
19204 slen
= (int)STRLEN(sourcing_name
);
19205 if (slen
> plen
&& fnamecmp(p
,
19206 sourcing_name
+ slen
- plen
) == 0)
19208 vim_free(scriptname
);
19213 EMSG2(_("E746: Function name does not match script file name: %s"), name
);
19218 fp
= (ufunc_T
*)alloc((unsigned)(sizeof(ufunc_T
) + STRLEN(name
)));
19222 if (fudi
.fd_dict
!= NULL
)
19224 if (fudi
.fd_di
== NULL
)
19226 /* add new dict entry */
19227 fudi
.fd_di
= dictitem_alloc(fudi
.fd_newkey
);
19228 if (fudi
.fd_di
== NULL
)
19233 if (dict_add(fudi
.fd_dict
, fudi
.fd_di
) == FAIL
)
19235 vim_free(fudi
.fd_di
);
19241 /* overwrite existing dict entry */
19242 clear_tv(&fudi
.fd_di
->di_tv
);
19243 fudi
.fd_di
->di_tv
.v_type
= VAR_FUNC
;
19244 fudi
.fd_di
->di_tv
.v_lock
= 0;
19245 fudi
.fd_di
->di_tv
.vval
.v_string
= vim_strsave(name
);
19246 fp
->uf_refcount
= 1;
19248 /* behave like "dict" was used */
19252 /* insert the new function in the function list */
19253 STRCPY(fp
->uf_name
, name
);
19254 hash_add(&func_hashtab
, UF2HIKEY(fp
));
19256 fp
->uf_args
= newargs
;
19257 fp
->uf_lines
= newlines
;
19258 #ifdef FEAT_PROFILE
19259 fp
->uf_tml_count
= NULL
;
19260 fp
->uf_tml_total
= NULL
;
19261 fp
->uf_tml_self
= NULL
;
19262 fp
->uf_profiling
= FALSE
;
19263 if (prof_def_func())
19264 func_do_profile(fp
);
19266 fp
->uf_varargs
= varargs
;
19267 fp
->uf_flags
= flags
;
19269 fp
->uf_script_ID
= current_SID
;
19273 ga_clear_strings(&newargs
);
19274 ga_clear_strings(&newlines
);
19276 vim_free(skip_until
);
19277 vim_free(fudi
.fd_newkey
);
19279 did_emsg
|= saved_did_emsg
;
19283 * Get a function name, translating "<SID>" and "<SNR>".
19284 * Also handles a Funcref in a List or Dictionary.
19285 * Returns the function name in allocated memory, or NULL for failure.
19287 * TFN_INT: internal function name OK
19288 * TFN_QUIET: be quiet
19289 * Advances "pp" to just after the function name (if no error).
19292 trans_function_name(pp
, skip
, flags
, fdp
)
19294 int skip
; /* only find the end, don't evaluate */
19296 funcdict_T
*fdp
; /* return: info about dictionary used */
19298 char_u
*name
= NULL
;
19302 char_u sid_buf
[20];
19307 vim_memset(fdp
, 0, sizeof(funcdict_T
));
19310 /* Check for hard coded <SNR>: already translated function ID (from a user
19312 if ((*pp
)[0] == K_SPECIAL
&& (*pp
)[1] == KS_EXTRA
19313 && (*pp
)[2] == (int)KE_SNR
)
19316 len
= get_id_len(pp
) + 3;
19317 return vim_strnsave(start
, len
);
19320 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19321 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
19322 lead
= eval_fname_script(start
);
19326 end
= get_lval(start
, NULL
, &lv
, FALSE
, skip
, flags
& TFN_QUIET
,
19327 lead
> 2 ? 0 : FNE_CHECK_START
);
19331 EMSG(_("E129: Function name required"));
19334 if (end
== NULL
|| (lv
.ll_tv
!= NULL
&& (lead
> 2 || lv
.ll_range
)))
19337 * Report an invalid expression in braces, unless the expression
19338 * evaluation has been cancelled due to an aborting error, an
19339 * interrupt, or an exception.
19344 EMSG2(_(e_invarg2
), start
);
19347 *pp
= find_name_end(start
, NULL
, NULL
, FNE_INCL_BR
);
19351 if (lv
.ll_tv
!= NULL
)
19355 fdp
->fd_dict
= lv
.ll_dict
;
19356 fdp
->fd_newkey
= lv
.ll_newkey
;
19357 lv
.ll_newkey
= NULL
;
19358 fdp
->fd_di
= lv
.ll_di
;
19360 if (lv
.ll_tv
->v_type
== VAR_FUNC
&& lv
.ll_tv
->vval
.v_string
!= NULL
)
19362 name
= vim_strsave(lv
.ll_tv
->vval
.v_string
);
19367 if (!skip
&& !(flags
& TFN_QUIET
) && (fdp
== NULL
19368 || lv
.ll_dict
== NULL
|| fdp
->fd_newkey
== NULL
))
19369 EMSG(_(e_funcref
));
19377 if (lv
.ll_name
== NULL
)
19379 /* Error found, but continue after the function name. */
19384 /* Check if the name is a Funcref. If so, use the value. */
19385 if (lv
.ll_exp_name
!= NULL
)
19387 len
= (int)STRLEN(lv
.ll_exp_name
);
19388 name
= deref_func_name(lv
.ll_exp_name
, &len
);
19389 if (name
== lv
.ll_exp_name
)
19394 len
= (int)(end
- *pp
);
19395 name
= deref_func_name(*pp
, &len
);
19401 name
= vim_strsave(name
);
19406 if (lv
.ll_exp_name
!= NULL
)
19408 len
= (int)STRLEN(lv
.ll_exp_name
);
19409 if (lead
<= 2 && lv
.ll_name
== lv
.ll_exp_name
19410 && STRNCMP(lv
.ll_name
, "s:", 2) == 0)
19412 /* When there was "s:" already or the name expanded to get a
19413 * leading "s:" then remove it. */
19421 if (lead
== 2) /* skip over "s:" */
19423 len
= (int)(end
- lv
.ll_name
);
19427 * Copy the function name to allocated memory.
19428 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19429 * Accept <SNR>123_name() outside a script.
19432 lead
= 0; /* do nothing */
19436 if ((lv
.ll_exp_name
!= NULL
&& eval_fname_sid(lv
.ll_exp_name
))
19437 || eval_fname_sid(*pp
))
19439 /* It's "s:" or "<SID>" */
19440 if (current_SID
<= 0)
19442 EMSG(_(e_usingsid
));
19445 sprintf((char *)sid_buf
, "%ld_", (long)current_SID
);
19446 lead
+= (int)STRLEN(sid_buf
);
19449 else if (!(flags
& TFN_INT
) && builtin_function(lv
.ll_name
))
19451 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv
.ll_name
);
19454 name
= alloc((unsigned)(len
+ lead
+ 1));
19459 name
[0] = K_SPECIAL
;
19460 name
[1] = KS_EXTRA
;
19461 name
[2] = (int)KE_SNR
;
19462 if (lead
> 3) /* If it's "<SID>" */
19463 STRCPY(name
+ 3, sid_buf
);
19465 mch_memmove(name
+ lead
, lv
.ll_name
, (size_t)len
);
19466 name
[len
+ lead
] = NUL
;
19476 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19477 * Return 2 if "p" starts with "s:".
19478 * Return 0 otherwise.
19481 eval_fname_script(p
)
19484 if (p
[0] == '<' && (STRNICMP(p
+ 1, "SID>", 4) == 0
19485 || STRNICMP(p
+ 1, "SNR>", 4) == 0))
19487 if (p
[0] == 's' && p
[1] == ':')
19493 * Return TRUE if "p" starts with "<SID>" or "s:".
19494 * Only works if eval_fname_script() returned non-zero for "p"!
19500 return (*p
== 's' || TOUPPER_ASC(p
[2]) == 'I');
19504 * List the head of the function: "name(arg1, arg2)".
19507 list_func_head(fp
, indent
)
19516 MSG_PUTS("function ");
19517 if (fp
->uf_name
[0] == K_SPECIAL
)
19519 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8
));
19520 msg_puts(fp
->uf_name
+ 3);
19523 msg_puts(fp
->uf_name
);
19525 for (j
= 0; j
< fp
->uf_args
.ga_len
; ++j
)
19529 msg_puts(FUNCARG(fp
, j
));
19531 if (fp
->uf_varargs
)
19540 last_set_msg(fp
->uf_script_ID
);
19544 * Find a function by name, return pointer to it in ufuncs.
19545 * Return NULL for unknown function.
19553 hi
= hash_find(&func_hashtab
, name
);
19554 if (!HASHITEM_EMPTY(hi
))
19559 #if defined(EXITFREE) || defined(PROTO)
19561 free_all_functions()
19565 /* Need to start all over every time, because func_free() may change the
19567 while (func_hashtab
.ht_used
> 0)
19568 for (hi
= func_hashtab
.ht_array
; ; ++hi
)
19569 if (!HASHITEM_EMPTY(hi
))
19571 func_free(HI2UF(hi
));
19578 * Return TRUE if a function "name" exists.
19581 function_exists(name
)
19588 p
= trans_function_name(&nm
, FALSE
, TFN_INT
|TFN_QUIET
, NULL
);
19589 nm
= skipwhite(nm
);
19591 /* Only accept "funcname", "funcname ", "funcname (..." and
19592 * "funcname(...", not "funcname!...". */
19593 if (p
!= NULL
&& (*nm
== NUL
|| *nm
== '('))
19595 if (builtin_function(p
))
19596 n
= (find_internal_func(p
) >= 0);
19598 n
= (find_func(p
) != NULL
);
19605 * Return TRUE if "name" looks like a builtin function name: starts with a
19606 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
19609 builtin_function(name
)
19612 return ASCII_ISLOWER(name
[0]) && vim_strchr(name
, ':') == NULL
19613 && vim_strchr(name
, AUTOLOAD_CHAR
) == NULL
;
19616 #if defined(FEAT_PROFILE) || defined(PROTO)
19618 * Start profiling function "fp".
19621 func_do_profile(fp
)
19624 fp
->uf_tm_count
= 0;
19625 profile_zero(&fp
->uf_tm_self
);
19626 profile_zero(&fp
->uf_tm_total
);
19627 if (fp
->uf_tml_count
== NULL
)
19628 fp
->uf_tml_count
= (int *)alloc_clear((unsigned)
19629 (sizeof(int) * fp
->uf_lines
.ga_len
));
19630 if (fp
->uf_tml_total
== NULL
)
19631 fp
->uf_tml_total
= (proftime_T
*)alloc_clear((unsigned)
19632 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
19633 if (fp
->uf_tml_self
== NULL
)
19634 fp
->uf_tml_self
= (proftime_T
*)alloc_clear((unsigned)
19635 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
19636 fp
->uf_tml_idx
= -1;
19637 if (fp
->uf_tml_count
== NULL
|| fp
->uf_tml_total
== NULL
19638 || fp
->uf_tml_self
== NULL
)
19639 return; /* out of memory */
19641 fp
->uf_profiling
= TRUE
;
19645 * Dump the profiling results for all functions in file "fd".
19648 func_dump_profile(fd
)
19658 todo
= (int)func_hashtab
.ht_used
;
19659 sorttab
= (ufunc_T
**)alloc((unsigned)(sizeof(ufunc_T
) * todo
));
19661 for (hi
= func_hashtab
.ht_array
; todo
> 0; ++hi
)
19663 if (!HASHITEM_EMPTY(hi
))
19667 if (fp
->uf_profiling
)
19669 if (sorttab
!= NULL
)
19670 sorttab
[st_len
++] = fp
;
19672 if (fp
->uf_name
[0] == K_SPECIAL
)
19673 fprintf(fd
, "FUNCTION <SNR>%s()\n", fp
->uf_name
+ 3);
19675 fprintf(fd
, "FUNCTION %s()\n", fp
->uf_name
);
19676 if (fp
->uf_tm_count
== 1)
19677 fprintf(fd
, "Called 1 time\n");
19679 fprintf(fd
, "Called %d times\n", fp
->uf_tm_count
);
19680 fprintf(fd
, "Total time: %s\n", profile_msg(&fp
->uf_tm_total
));
19681 fprintf(fd
, " Self time: %s\n", profile_msg(&fp
->uf_tm_self
));
19683 fprintf(fd
, "count total (s) self (s)\n");
19685 for (i
= 0; i
< fp
->uf_lines
.ga_len
; ++i
)
19687 if (FUNCLINE(fp
, i
) == NULL
)
19689 prof_func_line(fd
, fp
->uf_tml_count
[i
],
19690 &fp
->uf_tml_total
[i
], &fp
->uf_tml_self
[i
], TRUE
);
19691 fprintf(fd
, "%s\n", FUNCLINE(fp
, i
));
19698 if (sorttab
!= NULL
&& st_len
> 0)
19700 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
19702 prof_sort_list(fd
, sorttab
, st_len
, "TOTAL", FALSE
);
19703 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
19705 prof_sort_list(fd
, sorttab
, st_len
, "SELF", TRUE
);
19710 prof_sort_list(fd
, sorttab
, st_len
, title
, prefer_self
)
19715 int prefer_self
; /* when equal print only self time */
19720 fprintf(fd
, "FUNCTIONS SORTED ON %s TIME\n", title
);
19721 fprintf(fd
, "count total (s) self (s) function\n");
19722 for (i
= 0; i
< 20 && i
< st_len
; ++i
)
19725 prof_func_line(fd
, fp
->uf_tm_count
, &fp
->uf_tm_total
, &fp
->uf_tm_self
,
19727 if (fp
->uf_name
[0] == K_SPECIAL
)
19728 fprintf(fd
, " <SNR>%s()\n", fp
->uf_name
+ 3);
19730 fprintf(fd
, " %s()\n", fp
->uf_name
);
19736 * Print the count and times for one function or function line.
19739 prof_func_line(fd
, count
, total
, self
, prefer_self
)
19744 int prefer_self
; /* when equal print only self time */
19748 fprintf(fd
, "%5d ", count
);
19749 if (prefer_self
&& profile_equal(total
, self
))
19752 fprintf(fd
, "%s ", profile_msg(total
));
19753 if (!prefer_self
&& profile_equal(total
, self
))
19756 fprintf(fd
, "%s ", profile_msg(self
));
19763 * Compare function for total time sorting.
19766 #ifdef __BORLANDC__
19769 prof_total_cmp(s1
, s2
)
19775 p1
= *(ufunc_T
**)s1
;
19776 p2
= *(ufunc_T
**)s2
;
19777 return profile_cmp(&p1
->uf_tm_total
, &p2
->uf_tm_total
);
19781 * Compare function for self time sorting.
19784 #ifdef __BORLANDC__
19787 prof_self_cmp(s1
, s2
)
19793 p1
= *(ufunc_T
**)s1
;
19794 p2
= *(ufunc_T
**)s2
;
19795 return profile_cmp(&p1
->uf_tm_self
, &p2
->uf_tm_self
);
19801 * If "name" has a package name try autoloading the script for it.
19802 * Return TRUE if a package was loaded.
19805 script_autoload(name
, reload
)
19807 int reload
; /* load script again when already loaded */
19810 char_u
*scriptname
, *tofree
;
19814 /* If there is no '#' after name[0] there is no package name. */
19815 p
= vim_strchr(name
, AUTOLOAD_CHAR
);
19816 if (p
== NULL
|| p
== name
)
19819 tofree
= scriptname
= autoload_name(name
);
19821 /* Find the name in the list of previously loaded package names. Skip
19822 * "autoload/", it's always the same. */
19823 for (i
= 0; i
< ga_loaded
.ga_len
; ++i
)
19824 if (STRCMP(((char_u
**)ga_loaded
.ga_data
)[i
] + 9, scriptname
+ 9) == 0)
19826 if (!reload
&& i
< ga_loaded
.ga_len
)
19827 ret
= FALSE
; /* was loaded already */
19830 /* Remember the name if it wasn't loaded already. */
19831 if (i
== ga_loaded
.ga_len
&& ga_grow(&ga_loaded
, 1) == OK
)
19833 ((char_u
**)ga_loaded
.ga_data
)[ga_loaded
.ga_len
++] = scriptname
;
19837 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
19838 if (source_runtime(scriptname
, FALSE
) == OK
)
19847 * Return the autoload script name for a function or variable name.
19848 * Returns NULL when out of memory.
19851 autoload_name(name
)
19855 char_u
*scriptname
;
19857 /* Get the script file name: replace '#' with '/', append ".vim". */
19858 scriptname
= alloc((unsigned)(STRLEN(name
) + 14));
19859 if (scriptname
== NULL
)
19861 STRCPY(scriptname
, "autoload/");
19862 STRCAT(scriptname
, name
);
19863 *vim_strrchr(scriptname
, AUTOLOAD_CHAR
) = NUL
;
19864 STRCAT(scriptname
, ".vim");
19865 while ((p
= vim_strchr(scriptname
, AUTOLOAD_CHAR
)) != NULL
)
19870 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19873 * Function given to ExpandGeneric() to obtain the list of user defined
19877 get_user_func_name(xp
, idx
)
19881 static long_u done
;
19882 static hashitem_T
*hi
;
19888 hi
= func_hashtab
.ht_array
;
19890 if (done
< func_hashtab
.ht_used
)
19894 while (HASHITEM_EMPTY(hi
))
19898 if (STRLEN(fp
->uf_name
) + 4 >= IOSIZE
)
19899 return fp
->uf_name
; /* prevents overflow */
19901 cat_func_name(IObuff
, fp
);
19902 if (xp
->xp_context
!= EXPAND_USER_FUNC
)
19904 STRCAT(IObuff
, "(");
19905 if (!fp
->uf_varargs
&& fp
->uf_args
.ga_len
== 0)
19906 STRCAT(IObuff
, ")");
19913 #endif /* FEAT_CMDL_COMPL */
19916 * Copy the function name of "fp" to buffer "buf".
19917 * "buf" must be able to hold the function name plus three bytes.
19918 * Takes care of script-local function names.
19921 cat_func_name(buf
, fp
)
19925 if (fp
->uf_name
[0] == K_SPECIAL
)
19927 STRCPY(buf
, "<SNR>");
19928 STRCAT(buf
, fp
->uf_name
+ 3);
19931 STRCPY(buf
, fp
->uf_name
);
19935 * ":delfunction {name}"
19938 ex_delfunction(eap
)
19941 ufunc_T
*fp
= NULL
;
19947 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
19948 vim_free(fudi
.fd_newkey
);
19951 if (fudi
.fd_dict
!= NULL
&& !eap
->skip
)
19952 EMSG(_(e_funcref
));
19955 if (!ends_excmd(*skipwhite(p
)))
19958 EMSG(_(e_trailing
));
19961 eap
->nextcmd
= check_nextcmd(p
);
19962 if (eap
->nextcmd
!= NULL
)
19966 fp
= find_func(name
);
19973 EMSG2(_(e_nofunc
), eap
->arg
);
19976 if (fp
->uf_calls
> 0)
19978 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap
->arg
);
19982 if (fudi
.fd_dict
!= NULL
)
19984 /* Delete the dict item that refers to the function, it will
19985 * invoke func_unref() and possibly delete the function. */
19986 dictitem_remove(fudi
.fd_dict
, fudi
.fd_di
);
19994 * Free a function and remove it from the list of functions.
20002 /* clear this function */
20003 ga_clear_strings(&(fp
->uf_args
));
20004 ga_clear_strings(&(fp
->uf_lines
));
20005 #ifdef FEAT_PROFILE
20006 vim_free(fp
->uf_tml_count
);
20007 vim_free(fp
->uf_tml_total
);
20008 vim_free(fp
->uf_tml_self
);
20011 /* remove the function from the function hashtable */
20012 hi
= hash_find(&func_hashtab
, UF2HIKEY(fp
));
20013 if (HASHITEM_EMPTY(hi
))
20014 EMSG2(_(e_intern2
), "func_free()");
20016 hash_remove(&func_hashtab
, hi
);
20022 * Unreference a Function: decrement the reference count and free it when it
20023 * becomes zero. Only for numbered functions.
20031 if (name
!= NULL
&& isdigit(*name
))
20033 fp
= find_func(name
);
20035 EMSG2(_(e_intern2
), "func_unref()");
20036 else if (--fp
->uf_refcount
<= 0)
20038 /* Only delete it when it's not being used. Otherwise it's done
20039 * when "uf_calls" becomes zero. */
20040 if (fp
->uf_calls
== 0)
20047 * Count a reference to a Function.
20055 if (name
!= NULL
&& isdigit(*name
))
20057 fp
= find_func(name
);
20059 EMSG2(_(e_intern2
), "func_ref()");
20066 * Call a user function.
20069 call_user_func(fp
, argcount
, argvars
, rettv
, firstline
, lastline
, selfdict
)
20070 ufunc_T
*fp
; /* pointer to function */
20071 int argcount
; /* nr of args */
20072 typval_T
*argvars
; /* arguments */
20073 typval_T
*rettv
; /* return value */
20074 linenr_T firstline
; /* first line of range */
20075 linenr_T lastline
; /* last line of range */
20076 dict_T
*selfdict
; /* Dictionary for "self" */
20078 char_u
*save_sourcing_name
;
20079 linenr_T save_sourcing_lnum
;
20080 scid_T save_current_SID
;
20083 static int depth
= 0;
20085 int fixvar_idx
= 0; /* index in fixvar[] */
20088 char_u numbuf
[NUMBUFLEN
];
20090 #ifdef FEAT_PROFILE
20091 proftime_T wait_start
;
20092 proftime_T call_start
;
20095 /* If depth of calling is getting too high, don't execute the function */
20096 if (depth
>= p_mfd
)
20098 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
20099 rettv
->v_type
= VAR_NUMBER
;
20100 rettv
->vval
.v_number
= -1;
20105 line_breakcheck(); /* check for CTRL-C hit */
20107 fc
.caller
= current_funccal
;
20108 current_funccal
= &fc
;
20111 rettv
->vval
.v_number
= 0;
20113 fc
.returned
= FALSE
;
20114 fc
.level
= ex_nesting_level
;
20115 /* Check if this function has a breakpoint. */
20116 fc
.breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
, (linenr_T
)0);
20117 fc
.dbg_tick
= debug_tick
;
20120 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
20121 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
20122 * each argument variable and saves a lot of time.
20125 * Init l: variables.
20127 init_var_dict(&fc
.l_vars
, &fc
.l_vars_var
);
20128 if (selfdict
!= NULL
)
20130 /* Set l:self to "selfdict". Use "name" to avoid a warning from
20131 * some compiler that checks the destination size. */
20132 v
= &fc
.fixvar
[fixvar_idx
++].var
;
20134 STRCPY(name
, "self");
20135 v
->di_flags
= DI_FLAGS_RO
+ DI_FLAGS_FIX
;
20136 hash_add(&fc
.l_vars
.dv_hashtab
, DI2HIKEY(v
));
20137 v
->di_tv
.v_type
= VAR_DICT
;
20138 v
->di_tv
.v_lock
= 0;
20139 v
->di_tv
.vval
.v_dict
= selfdict
;
20140 ++selfdict
->dv_refcount
;
20144 * Init a: variables.
20145 * Set a:0 to "argcount".
20146 * Set a:000 to a list with room for the "..." arguments.
20148 init_var_dict(&fc
.l_avars
, &fc
.l_avars_var
);
20149 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "0",
20150 (varnumber_T
)(argcount
- fp
->uf_args
.ga_len
));
20151 v
= &fc
.fixvar
[fixvar_idx
++].var
;
20152 STRCPY(v
->di_key
, "000");
20153 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
20154 hash_add(&fc
.l_avars
.dv_hashtab
, DI2HIKEY(v
));
20155 v
->di_tv
.v_type
= VAR_LIST
;
20156 v
->di_tv
.v_lock
= VAR_FIXED
;
20157 v
->di_tv
.vval
.v_list
= &fc
.l_varlist
;
20158 vim_memset(&fc
.l_varlist
, 0, sizeof(list_T
));
20159 fc
.l_varlist
.lv_refcount
= 99999;
20160 fc
.l_varlist
.lv_lock
= VAR_FIXED
;
20163 * Set a:firstline to "firstline" and a:lastline to "lastline".
20164 * Set a:name to named arguments.
20165 * Set a:N to the "..." arguments.
20167 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "firstline",
20168 (varnumber_T
)firstline
);
20169 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "lastline",
20170 (varnumber_T
)lastline
);
20171 for (i
= 0; i
< argcount
; ++i
)
20173 ai
= i
- fp
->uf_args
.ga_len
;
20175 /* named argument a:name */
20176 name
= FUNCARG(fp
, i
);
20179 /* "..." argument a:1, a:2, etc. */
20180 sprintf((char *)numbuf
, "%d", ai
+ 1);
20183 if (fixvar_idx
< FIXVAR_CNT
&& STRLEN(name
) <= VAR_SHORT_LEN
)
20185 v
= &fc
.fixvar
[fixvar_idx
++].var
;
20186 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
20190 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
20194 v
->di_flags
= DI_FLAGS_RO
;
20196 STRCPY(v
->di_key
, name
);
20197 hash_add(&fc
.l_avars
.dv_hashtab
, DI2HIKEY(v
));
20199 /* Note: the values are copied directly to avoid alloc/free.
20200 * "argvars" must have VAR_FIXED for v_lock. */
20201 v
->di_tv
= argvars
[i
];
20202 v
->di_tv
.v_lock
= VAR_FIXED
;
20204 if (ai
>= 0 && ai
< MAX_FUNC_ARGS
)
20206 list_append(&fc
.l_varlist
, &fc
.l_listitems
[ai
]);
20207 fc
.l_listitems
[ai
].li_tv
= argvars
[i
];
20208 fc
.l_listitems
[ai
].li_tv
.v_lock
= VAR_FIXED
;
20212 /* Don't redraw while executing the function. */
20213 ++RedrawingDisabled
;
20214 save_sourcing_name
= sourcing_name
;
20215 save_sourcing_lnum
= sourcing_lnum
;
20217 sourcing_name
= alloc((unsigned)((save_sourcing_name
== NULL
? 0
20218 : STRLEN(save_sourcing_name
)) + STRLEN(fp
->uf_name
) + 13));
20219 if (sourcing_name
!= NULL
)
20221 if (save_sourcing_name
!= NULL
20222 && STRNCMP(save_sourcing_name
, "function ", 9) == 0)
20223 sprintf((char *)sourcing_name
, "%s..", save_sourcing_name
);
20225 STRCPY(sourcing_name
, "function ");
20226 cat_func_name(sourcing_name
+ STRLEN(sourcing_name
), fp
);
20228 if (p_verbose
>= 12)
20231 verbose_enter_scroll();
20233 smsg((char_u
*)_("calling %s"), sourcing_name
);
20234 if (p_verbose
>= 14)
20236 char_u buf
[MSG_BUF_LEN
];
20237 char_u numbuf2
[NUMBUFLEN
];
20241 msg_puts((char_u
*)"(");
20242 for (i
= 0; i
< argcount
; ++i
)
20245 msg_puts((char_u
*)", ");
20246 if (argvars
[i
].v_type
== VAR_NUMBER
)
20247 msg_outnum((long)argvars
[i
].vval
.v_number
);
20250 s
= tv2string(&argvars
[i
], &tofree
, numbuf2
, 0);
20253 trunc_string(s
, buf
, MSG_BUF_CLEN
);
20259 msg_puts((char_u
*)")");
20261 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
20263 verbose_leave_scroll();
20267 #ifdef FEAT_PROFILE
20268 if (do_profiling
== PROF_YES
)
20270 if (!fp
->uf_profiling
&& has_profiling(FALSE
, fp
->uf_name
, NULL
))
20271 func_do_profile(fp
);
20272 if (fp
->uf_profiling
20273 || (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
))
20276 profile_start(&call_start
);
20277 profile_zero(&fp
->uf_tm_children
);
20279 script_prof_save(&wait_start
);
20283 save_current_SID
= current_SID
;
20284 current_SID
= fp
->uf_script_ID
;
20285 save_did_emsg
= did_emsg
;
20288 /* call do_cmdline() to execute the lines */
20289 do_cmdline(NULL
, get_func_line
, (void *)&fc
,
20290 DOCMD_NOWAIT
|DOCMD_VERBOSE
|DOCMD_REPEAT
);
20292 --RedrawingDisabled
;
20294 /* when the function was aborted because of an error, return -1 */
20295 if ((did_emsg
&& (fp
->uf_flags
& FC_ABORT
)) || rettv
->v_type
== VAR_UNKNOWN
)
20298 rettv
->v_type
= VAR_NUMBER
;
20299 rettv
->vval
.v_number
= -1;
20302 #ifdef FEAT_PROFILE
20303 if (do_profiling
== PROF_YES
&& (fp
->uf_profiling
20304 || (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
)))
20306 profile_end(&call_start
);
20307 profile_sub_wait(&wait_start
, &call_start
);
20308 profile_add(&fp
->uf_tm_total
, &call_start
);
20309 profile_self(&fp
->uf_tm_self
, &call_start
, &fp
->uf_tm_children
);
20310 if (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
)
20312 profile_add(&fc
.caller
->func
->uf_tm_children
, &call_start
);
20313 profile_add(&fc
.caller
->func
->uf_tml_children
, &call_start
);
20318 /* when being verbose, mention the return value */
20319 if (p_verbose
>= 12)
20322 verbose_enter_scroll();
20325 smsg((char_u
*)_("%s aborted"), sourcing_name
);
20326 else if (fc
.rettv
->v_type
== VAR_NUMBER
)
20327 smsg((char_u
*)_("%s returning #%ld"), sourcing_name
,
20328 (long)fc
.rettv
->vval
.v_number
);
20331 char_u buf
[MSG_BUF_LEN
];
20332 char_u numbuf2
[NUMBUFLEN
];
20336 /* The value may be very long. Skip the middle part, so that we
20337 * have some idea how it starts and ends. smsg() would always
20338 * truncate it at the end. */
20339 s
= tv2string(fc
.rettv
, &tofree
, numbuf2
, 0);
20342 trunc_string(s
, buf
, MSG_BUF_CLEN
);
20343 smsg((char_u
*)_("%s returning %s"), sourcing_name
, buf
);
20347 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
20349 verbose_leave_scroll();
20353 vim_free(sourcing_name
);
20354 sourcing_name
= save_sourcing_name
;
20355 sourcing_lnum
= save_sourcing_lnum
;
20356 current_SID
= save_current_SID
;
20357 #ifdef FEAT_PROFILE
20358 if (do_profiling
== PROF_YES
)
20359 script_prof_restore(&wait_start
);
20362 if (p_verbose
>= 12 && sourcing_name
!= NULL
)
20365 verbose_enter_scroll();
20367 smsg((char_u
*)_("continuing in %s"), sourcing_name
);
20368 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
20370 verbose_leave_scroll();
20374 did_emsg
|= save_did_emsg
;
20375 current_funccal
= fc
.caller
;
20377 /* The a: variables typevals were not alloced, only free the allocated
20379 vars_clear_ext(&fc
.l_avars
.dv_hashtab
, FALSE
);
20381 vars_clear(&fc
.l_vars
.dv_hashtab
); /* free all l: variables */
20386 * Add a number variable "name" to dict "dp" with value "nr".
20389 add_nr_var(dp
, v
, name
, nr
)
20395 STRCPY(v
->di_key
, name
);
20396 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
20397 hash_add(&dp
->dv_hashtab
, DI2HIKEY(v
));
20398 v
->di_tv
.v_type
= VAR_NUMBER
;
20399 v
->di_tv
.v_lock
= VAR_FIXED
;
20400 v
->di_tv
.vval
.v_number
= nr
;
20410 char_u
*arg
= eap
->arg
;
20412 int returning
= FALSE
;
20414 if (current_funccal
== NULL
)
20416 EMSG(_("E133: :return not inside a function"));
20423 eap
->nextcmd
= NULL
;
20424 if ((*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
20425 && eval0(arg
, &rettv
, &eap
->nextcmd
, !eap
->skip
) != FAIL
)
20428 returning
= do_return(eap
, FALSE
, TRUE
, &rettv
);
20432 /* It's safer to return also on error. */
20433 else if (!eap
->skip
)
20436 * Return unless the expression evaluation has been cancelled due to an
20437 * aborting error, an interrupt, or an exception.
20440 returning
= do_return(eap
, FALSE
, TRUE
, NULL
);
20443 /* When skipping or the return gets pending, advance to the next command
20444 * in this line (!returning). Otherwise, ignore the rest of the line.
20445 * Following lines will be ignored by get_func_line(). */
20447 eap
->nextcmd
= NULL
;
20448 else if (eap
->nextcmd
== NULL
) /* no argument */
20449 eap
->nextcmd
= check_nextcmd(arg
);
20456 * Return from a function. Possibly makes the return pending. Also called
20457 * for a pending return at the ":endtry" or after returning from an extra
20458 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
20459 * when called due to a ":return" command. "rettv" may point to a typval_T
20460 * with the return rettv. Returns TRUE when the return can be carried out,
20461 * FALSE when the return gets pending.
20464 do_return(eap
, reanimate
, is_cmd
, rettv
)
20471 struct condstack
*cstack
= eap
->cstack
;
20474 /* Undo the return. */
20475 current_funccal
->returned
= FALSE
;
20478 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20479 * not in its finally clause (which then is to be executed next) is found.
20480 * In this case, make the ":return" pending for execution at the ":endtry".
20481 * Otherwise, return normally.
20483 idx
= cleanup_conditionals(eap
->cstack
, 0, TRUE
);
20486 cstack
->cs_pending
[idx
] = CSTP_RETURN
;
20488 if (!is_cmd
&& !reanimate
)
20489 /* A pending return again gets pending. "rettv" points to an
20490 * allocated variable with the rettv of the original ":return"'s
20491 * argument if present or is NULL else. */
20492 cstack
->cs_rettv
[idx
] = rettv
;
20495 /* When undoing a return in order to make it pending, get the stored
20498 rettv
= current_funccal
->rettv
;
20502 /* Store the value of the pending return. */
20503 if ((cstack
->cs_rettv
[idx
] = alloc_tv()) != NULL
)
20504 *(typval_T
*)cstack
->cs_rettv
[idx
] = *(typval_T
*)rettv
;
20506 EMSG(_(e_outofmem
));
20509 cstack
->cs_rettv
[idx
] = NULL
;
20513 /* The pending return value could be overwritten by a ":return"
20514 * without argument in a finally clause; reset the default
20516 current_funccal
->rettv
->v_type
= VAR_NUMBER
;
20517 current_funccal
->rettv
->vval
.v_number
= 0;
20520 report_make_pending(CSTP_RETURN
, rettv
);
20524 current_funccal
->returned
= TRUE
;
20526 /* If the return is carried out now, store the return value. For
20527 * a return immediately after reanimation, the value is already
20529 if (!reanimate
&& rettv
!= NULL
)
20531 clear_tv(current_funccal
->rettv
);
20532 *current_funccal
->rettv
= *(typval_T
*)rettv
;
20542 * Free the variable with a pending return value.
20545 discard_pending_return(rettv
)
20548 free_tv((typval_T
*)rettv
);
20552 * Generate a return command for producing the value of "rettv". The result
20553 * is an allocated string. Used by report_pending() for verbose messages.
20556 get_return_cmd(rettv
)
20560 char_u
*tofree
= NULL
;
20561 char_u numbuf
[NUMBUFLEN
];
20564 s
= echo_string((typval_T
*)rettv
, &tofree
, numbuf
, 0);
20568 STRCPY(IObuff
, ":return ");
20569 STRNCPY(IObuff
+ 8, s
, IOSIZE
- 8);
20570 if (STRLEN(s
) + 8 >= IOSIZE
)
20571 STRCPY(IObuff
+ IOSIZE
- 4, "...");
20573 return vim_strsave(IObuff
);
20577 * Get next function line.
20578 * Called by do_cmdline() to get the next line.
20579 * Returns allocated string, or NULL for end of function.
20583 get_func_line(c
, cookie
, indent
)
20584 int c
; /* not used */
20586 int indent
; /* not used */
20588 funccall_T
*fcp
= (funccall_T
*)cookie
;
20589 ufunc_T
*fp
= fcp
->func
;
20591 garray_T
*gap
; /* growarray with function lines */
20593 /* If breakpoints have been added/deleted need to check for it. */
20594 if (fcp
->dbg_tick
!= debug_tick
)
20596 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
20598 fcp
->dbg_tick
= debug_tick
;
20600 #ifdef FEAT_PROFILE
20601 if (do_profiling
== PROF_YES
)
20602 func_line_end(cookie
);
20605 gap
= &fp
->uf_lines
;
20606 if (((fp
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
20611 /* Skip NULL lines (continuation lines). */
20612 while (fcp
->linenr
< gap
->ga_len
20613 && ((char_u
**)(gap
->ga_data
))[fcp
->linenr
] == NULL
)
20615 if (fcp
->linenr
>= gap
->ga_len
)
20619 retval
= vim_strsave(((char_u
**)(gap
->ga_data
))[fcp
->linenr
++]);
20620 sourcing_lnum
= fcp
->linenr
;
20621 #ifdef FEAT_PROFILE
20622 if (do_profiling
== PROF_YES
)
20623 func_line_start(cookie
);
20628 /* Did we encounter a breakpoint? */
20629 if (fcp
->breakpoint
!= 0 && fcp
->breakpoint
<= sourcing_lnum
)
20631 dbg_breakpoint(fp
->uf_name
, sourcing_lnum
);
20632 /* Find next breakpoint. */
20633 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
20635 fcp
->dbg_tick
= debug_tick
;
20641 #if defined(FEAT_PROFILE) || defined(PROTO)
20643 * Called when starting to read a function line.
20644 * "sourcing_lnum" must be correct!
20645 * When skipping lines it may not actually be executed, but we won't find out
20646 * until later and we need to store the time now.
20649 func_line_start(cookie
)
20652 funccall_T
*fcp
= (funccall_T
*)cookie
;
20653 ufunc_T
*fp
= fcp
->func
;
20655 if (fp
->uf_profiling
&& sourcing_lnum
>= 1
20656 && sourcing_lnum
<= fp
->uf_lines
.ga_len
)
20658 fp
->uf_tml_idx
= sourcing_lnum
- 1;
20659 /* Skip continuation lines. */
20660 while (fp
->uf_tml_idx
> 0 && FUNCLINE(fp
, fp
->uf_tml_idx
) == NULL
)
20662 fp
->uf_tml_execed
= FALSE
;
20663 profile_start(&fp
->uf_tml_start
);
20664 profile_zero(&fp
->uf_tml_children
);
20665 profile_get_wait(&fp
->uf_tml_wait
);
20670 * Called when actually executing a function line.
20673 func_line_exec(cookie
)
20676 funccall_T
*fcp
= (funccall_T
*)cookie
;
20677 ufunc_T
*fp
= fcp
->func
;
20679 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
20680 fp
->uf_tml_execed
= TRUE
;
20684 * Called when done with a function line.
20687 func_line_end(cookie
)
20690 funccall_T
*fcp
= (funccall_T
*)cookie
;
20691 ufunc_T
*fp
= fcp
->func
;
20693 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
20695 if (fp
->uf_tml_execed
)
20697 ++fp
->uf_tml_count
[fp
->uf_tml_idx
];
20698 profile_end(&fp
->uf_tml_start
);
20699 profile_sub_wait(&fp
->uf_tml_wait
, &fp
->uf_tml_start
);
20700 profile_add(&fp
->uf_tml_total
[fp
->uf_tml_idx
], &fp
->uf_tml_start
);
20701 profile_self(&fp
->uf_tml_self
[fp
->uf_tml_idx
], &fp
->uf_tml_start
,
20702 &fp
->uf_tml_children
);
20704 fp
->uf_tml_idx
= -1;
20710 * Return TRUE if the currently active function should be ended, because a
20711 * return was encountered or an error occured. Used inside a ":while".
20714 func_has_ended(cookie
)
20717 funccall_T
*fcp
= (funccall_T
*)cookie
;
20719 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20720 * an error inside a try conditional. */
20721 return (((fcp
->func
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
20726 * return TRUE if cookie indicates a function which "abort"s on errors.
20729 func_has_abort(cookie
)
20732 return ((funccall_T
*)cookie
)->func
->uf_flags
& FC_ABORT
;
20735 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20738 VAR_FLAVOUR_DEFAULT
,
20739 VAR_FLAVOUR_SESSION
,
20740 VAR_FLAVOUR_VIMINFO
20743 static var_flavour_T var_flavour
__ARGS((char_u
*varname
));
20745 static var_flavour_T
20746 var_flavour(varname
)
20749 char_u
*p
= varname
;
20751 if (ASCII_ISUPPER(*p
))
20754 if (ASCII_ISLOWER(*p
))
20755 return VAR_FLAVOUR_SESSION
;
20756 return VAR_FLAVOUR_VIMINFO
;
20759 return VAR_FLAVOUR_DEFAULT
;
20763 #if defined(FEAT_VIMINFO) || defined(PROTO)
20765 * Restore global vars that start with a capital from the viminfo file
20768 read_viminfo_varlist(virp
, writing
)
20773 int is_string
= FALSE
;
20776 if (!writing
&& (find_viminfo_parameter('!') != NULL
))
20778 tab
= vim_strchr(virp
->vir_line
+ 1, '\t');
20781 *tab
++ = '\0'; /* isolate the variable name */
20782 if (*tab
== 'S') /* string var */
20785 tab
= vim_strchr(tab
, '\t');
20790 tv
.v_type
= VAR_STRING
;
20791 tv
.vval
.v_string
= viminfo_readstring(virp
,
20792 (int)(tab
- virp
->vir_line
+ 1), TRUE
);
20796 tv
.v_type
= VAR_NUMBER
;
20797 tv
.vval
.v_number
= atol((char *)tab
+ 1);
20799 set_var(virp
->vir_line
+ 1, &tv
, FALSE
);
20801 vim_free(tv
.vval
.v_string
);
20806 return viminfo_readline(virp
);
20810 * Write global vars that start with a capital to the viminfo file
20813 write_viminfo_varlist(fp
)
20817 dictitem_T
*this_var
;
20822 char_u numbuf
[NUMBUFLEN
];
20824 if (find_viminfo_parameter('!') == NULL
)
20827 fprintf(fp
, _("\n# global variables:\n"));
20829 todo
= (int)globvarht
.ht_used
;
20830 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
20832 if (!HASHITEM_EMPTY(hi
))
20835 this_var
= HI2DI(hi
);
20836 if (var_flavour(this_var
->di_key
) == VAR_FLAVOUR_VIMINFO
)
20838 switch (this_var
->di_tv
.v_type
)
20840 case VAR_STRING
: s
= "STR"; break;
20841 case VAR_NUMBER
: s
= "NUM"; break;
20844 fprintf(fp
, "!%s\t%s\t", this_var
->di_key
, s
);
20845 p
= echo_string(&this_var
->di_tv
, &tofree
, numbuf
, 0);
20847 viminfo_writestring(fp
, p
);
20855 #if defined(FEAT_SESSION) || defined(PROTO)
20857 store_session_globals(fd
)
20861 dictitem_T
*this_var
;
20865 todo
= (int)globvarht
.ht_used
;
20866 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
20868 if (!HASHITEM_EMPTY(hi
))
20871 this_var
= HI2DI(hi
);
20872 if ((this_var
->di_tv
.v_type
== VAR_NUMBER
20873 || this_var
->di_tv
.v_type
== VAR_STRING
)
20874 && var_flavour(this_var
->di_key
) == VAR_FLAVOUR_SESSION
)
20876 /* Escape special characters with a backslash. Turn a LF and
20877 * CR into \n and \r. */
20878 p
= vim_strsave_escaped(get_tv_string(&this_var
->di_tv
),
20879 (char_u
*)"\\\"\n\r");
20880 if (p
== NULL
) /* out of memory */
20882 for (t
= p
; *t
!= NUL
; ++t
)
20885 else if (*t
== '\r')
20887 if ((fprintf(fd
, "let %s = %c%s%c",
20889 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
20892 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
20894 || put_eol(fd
) == FAIL
)
20908 * Display script name where an item was last set.
20909 * Should only be invoked when 'verbose' is non-zero.
20912 last_set_msg(scriptID
)
20919 p
= home_replace_save(NULL
, get_scriptname(scriptID
));
20923 MSG_PUTS(_("\n\tLast set from "));
20931 #endif /* FEAT_EVAL */
20933 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20938 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20940 static int get_short_pathname
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
20941 static int shortpath_for_invalid_fname
__ARGS((char_u
**fname
, char_u
**bufp
, int *fnamelen
));
20942 static int shortpath_for_partial
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
20945 * Get the short pathname of a file.
20946 * Returns 1 on success. *fnamelen is 0 for nonexistent path.
20949 get_short_pathname(fnamep
, bufp
, fnamelen
)
20959 l
= GetShortPathName(*fnamep
, *fnamep
, len
);
20962 /* If that doesn't work (not enough space), then save the string
20963 * and try again with a new buffer big enough
20965 newbuf
= vim_strnsave(*fnamep
, l
);
20966 if (newbuf
== NULL
)
20970 *fnamep
= *bufp
= newbuf
;
20972 l
= GetShortPathName(*fnamep
,*fnamep
,l
+1);
20974 /* Really should always succeed, as the buffer is big enough */
20982 * Create a short path name. Returns the length of the buffer it needs.
20983 * Doesn't copy over the end of the buffer passed in.
20986 shortpath_for_invalid_fname(fname
, bufp
, fnamelen
)
20991 char_u
*s
, *p
, *pbuf2
, *pbuf3
;
20993 int len
, len2
, plen
, slen
;
20997 pbuf2
= vim_strnsave(*fname
, len2
);
21000 s
= pbuf2
+ len2
- 1; /* Find the end */
21004 if (after_pathsep(pbuf2
, s
+ 1))
21013 /* Go back one path-separator */
21014 while (s
> pbuf2
&& !after_pathsep(pbuf2
, s
+ 1))
21023 /* Remember the character that is about to be splatted */
21025 *s
= 0; /* get_short_pathname requires a null-terminated string */
21027 /* Try it in situ */
21029 if (!get_short_pathname(&p
, &pbuf3
, &plen
))
21034 *s
= ch
; /* Preserve the string */
21035 } while (plen
== 0);
21039 /* Remember the length of the new string. */
21040 *fnamelen
= len
= plen
+ slen
;
21044 /* If there's not enough space in the currently allocated string,
21045 * then copy it to a buffer big enough.
21047 *fname
= *bufp
= vim_strnsave(p
, len
);
21048 if (*fname
== NULL
)
21053 /* Transfer pbuf2 to being the main buffer (it's big enough) */
21054 *fname
= *bufp
= pbuf2
;
21056 strncpy(*fname
, p
, plen
);
21059 /* Concat the next bit */
21060 strncpy(*fname
+ plen
, s
, slen
);
21061 (*fname
)[len
] = '\0';
21069 * Get a pathname for a partial path.
21072 shortpath_for_partial(fnamep
, bufp
, fnamelen
)
21077 int sepcount
, len
, tflen
;
21079 char_u
*pbuf
, *tfname
;
21082 /* Count up the path seperators from the RHS.. so we know which part
21083 * of the path to return.
21086 for (p
= *fnamep
; p
< *fnamep
+ *fnamelen
; mb_ptr_adv(p
))
21087 if (vim_ispathsep(*p
))
21090 /* Need full path first (use expand_env() to remove a "~/") */
21091 hasTilde
= (**fnamep
== '~');
21093 pbuf
= tfname
= expand_env_save(*fnamep
);
21095 pbuf
= tfname
= FullName_save(*fnamep
, FALSE
);
21097 len
= tflen
= (int)STRLEN(tfname
);
21099 if (!get_short_pathname(&tfname
, &pbuf
, &len
))
21104 /* Don't have a valid filename, so shorten the rest of the
21105 * path if we can. This CAN give us invalid 8.3 filenames, but
21106 * there's not a lot of point in guessing what it might be.
21109 if (shortpath_for_invalid_fname(&tfname
, &pbuf
, &len
) == -1)
21113 /* Count the paths backward to find the beginning of the desired string. */
21114 for (p
= tfname
+ len
- 1; p
>= tfname
; --p
)
21118 p
-= mb_head_off(tfname
, p
);
21120 if (vim_ispathsep(*p
))
21122 if (sepcount
== 0 || (hasTilde
&& sepcount
== 1))
21139 /* Copy in the string - p indexes into tfname - allocated at pbuf */
21141 *fnamelen
= (int)STRLEN(p
);
21147 #endif /* WIN3264 */
21150 * Adjust a filename, according to a string of modifiers.
21151 * *fnamep must be NUL terminated when called. When returning, the length is
21152 * determined by *fnamelen.
21153 * Returns valid flags.
21154 * When there is an error, *fnamep is set to NULL.
21157 modify_fname(src
, usedlen
, fnamep
, bufp
, fnamelen
)
21158 char_u
*src
; /* string with modifiers */
21159 int *usedlen
; /* characters after src that are used */
21160 char_u
**fnamep
; /* file name so far */
21161 char_u
**bufp
; /* buffer for allocated file name or NULL */
21162 int *fnamelen
; /* length of fnamep */
21166 char_u
*s
, *p
, *pbuf
;
21167 char_u dirname
[MAXPATHL
];
21169 int has_fullname
= 0;
21171 int has_shortname
= 0;
21175 /* ":p" - full path/file_name */
21176 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'p')
21180 valid
|= VALID_PATH
;
21183 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
21184 if ((*fnamep
)[0] == '~'
21185 #if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
21186 && ((*fnamep
)[1] == '/'
21187 # ifdef BACKSLASH_IN_FILENAME
21188 || (*fnamep
)[1] == '\\'
21190 || (*fnamep
)[1] == NUL
)
21195 *fnamep
= expand_env_save(*fnamep
);
21196 vim_free(*bufp
); /* free any allocated file name */
21198 if (*fnamep
== NULL
)
21202 /* When "/." or "/.." is used: force expansion to get rid of it. */
21203 for (p
= *fnamep
; *p
!= NUL
; mb_ptr_adv(p
))
21205 if (vim_ispathsep(*p
)
21208 || vim_ispathsep(p
[2])
21210 && (p
[3] == NUL
|| vim_ispathsep(p
[3])))))
21214 /* FullName_save() is slow, don't use it when not needed. */
21215 if (*p
!= NUL
|| !vim_isAbsName(*fnamep
))
21217 *fnamep
= FullName_save(*fnamep
, *p
!= NUL
);
21218 vim_free(*bufp
); /* free any allocated file name */
21220 if (*fnamep
== NULL
)
21224 /* Append a path separator to a directory. */
21225 if (mch_isdir(*fnamep
))
21227 /* Make room for one or two extra characters. */
21228 *fnamep
= vim_strnsave(*fnamep
, (int)STRLEN(*fnamep
) + 2);
21229 vim_free(*bufp
); /* free any allocated file name */
21231 if (*fnamep
== NULL
)
21233 add_pathsep(*fnamep
);
21237 /* ":." - path relative to the current directory */
21238 /* ":~" - path relative to the home directory */
21239 /* ":8" - shortname path - postponed till after */
21240 while (src
[*usedlen
] == ':'
21241 && ((c
= src
[*usedlen
+ 1]) == '.' || c
== '~' || c
== '8'))
21247 has_shortname
= 1; /* Postpone this. */
21252 /* Need full path first (use expand_env() to remove a "~/") */
21255 if (c
== '.' && **fnamep
== '~')
21256 p
= pbuf
= expand_env_save(*fnamep
);
21258 p
= pbuf
= FullName_save(*fnamep
, FALSE
);
21269 mch_dirname(dirname
, MAXPATHL
);
21270 s
= shorten_fname(p
, dirname
);
21276 vim_free(*bufp
); /* free any allocated file name */
21284 home_replace(NULL
, p
, dirname
, MAXPATHL
, TRUE
);
21285 /* Only replace it when it starts with '~' */
21286 if (*dirname
== '~')
21288 s
= vim_strsave(dirname
);
21301 tail
= gettail(*fnamep
);
21302 *fnamelen
= (int)STRLEN(*fnamep
);
21304 /* ":h" - head, remove "/file_name", can be repeated */
21305 /* Don't remove the first "/" or "c:\" */
21306 while (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'h')
21308 valid
|= VALID_HEAD
;
21310 s
= get_past_head(*fnamep
);
21311 while (tail
> s
&& after_pathsep(s
, tail
))
21312 mb_ptr_back(*fnamep
, tail
);
21313 *fnamelen
= (int)(tail
- *fnamep
);
21316 *fnamelen
+= 1; /* the path separator is part of the path */
21318 if (*fnamelen
== 0)
21320 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
21321 p
= vim_strsave((char_u
*)".");
21325 *bufp
= *fnamep
= tail
= p
;
21330 while (tail
> s
&& !after_pathsep(s
, tail
))
21331 mb_ptr_back(*fnamep
, tail
);
21335 /* ":8" - shortname */
21336 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == '8')
21345 /* Check shortname after we have done 'heads' and before we do 'tails'
21350 /* Copy the string if it is shortened by :h */
21351 if (*fnamelen
< (int)STRLEN(*fnamep
))
21353 p
= vim_strnsave(*fnamep
, *fnamelen
);
21357 *bufp
= *fnamep
= p
;
21360 /* Split into two implementations - makes it easier. First is where
21361 * there isn't a full name already, second is where there is.
21363 if (!has_fullname
&& !vim_isAbsName(*fnamep
))
21365 if (shortpath_for_partial(fnamep
, bufp
, fnamelen
) == -1)
21372 /* Simple case, already have the full-name
21373 * Nearly always shorter, so try first time. */
21375 if (!get_short_pathname(fnamep
, bufp
, &l
))
21380 /* Couldn't find the filename.. search the paths.
21383 if (shortpath_for_invalid_fname(fnamep
, bufp
, &l
) == -1)
21389 #endif /* WIN3264 */
21391 /* ":t" - tail, just the basename */
21392 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 't')
21395 *fnamelen
-= (int)(tail
- *fnamep
);
21399 /* ":e" - extension, can be repeated */
21400 /* ":r" - root, without extension, can be repeated */
21401 while (src
[*usedlen
] == ':'
21402 && (src
[*usedlen
+ 1] == 'e' || src
[*usedlen
+ 1] == 'r'))
21404 /* find a '.' in the tail:
21405 * - for second :e: before the current fname
21406 * - otherwise: The last '.'
21408 if (src
[*usedlen
+ 1] == 'e' && *fnamep
> tail
)
21411 s
= *fnamep
+ *fnamelen
- 1;
21412 for ( ; s
> tail
; --s
)
21415 if (src
[*usedlen
+ 1] == 'e') /* :e */
21419 *fnamelen
+= (int)(*fnamep
- (s
+ 1));
21422 /* cut version from the extension */
21423 s
= *fnamep
+ *fnamelen
- 1;
21424 for ( ; s
> *fnamep
; --s
)
21428 *fnamelen
= s
- *fnamep
;
21431 else if (*fnamep
<= tail
)
21436 if (s
> tail
) /* remove one extension */
21437 *fnamelen
= (int)(s
- *fnamep
);
21442 /* ":s?pat?foo?" - substitute */
21443 /* ":gs?pat?foo?" - global substitute */
21444 if (src
[*usedlen
] == ':'
21445 && (src
[*usedlen
+ 1] == 's'
21446 || (src
[*usedlen
+ 1] == 'g' && src
[*usedlen
+ 2] == 's')))
21455 flags
= (char_u
*)"";
21456 s
= src
+ *usedlen
+ 2;
21457 if (src
[*usedlen
+ 1] == 'g')
21459 flags
= (char_u
*)"g";
21466 /* find end of pattern */
21467 p
= vim_strchr(s
, sep
);
21470 pat
= vim_strnsave(s
, (int)(p
- s
));
21474 /* find end of substitution */
21475 p
= vim_strchr(s
, sep
);
21478 sub
= vim_strnsave(s
, (int)(p
- s
));
21479 str
= vim_strnsave(*fnamep
, *fnamelen
);
21480 if (sub
!= NULL
&& str
!= NULL
)
21482 *usedlen
= (int)(p
+ 1 - src
);
21483 s
= do_string_sub(str
, pat
, sub
, flags
);
21487 *fnamelen
= (int)STRLEN(s
);
21499 /* after using ":s", repeat all the modifiers */
21509 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21510 * "flags" can be "g" to do a global substitute.
21511 * Returns an allocated string, NULL for error.
21514 do_string_sub(str
, pat
, sub
, flags
)
21521 regmatch_T regmatch
;
21529 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21531 p_cpo
= (char_u
*)"";
21533 ga_init2(&ga
, 1, 200);
21535 do_all
= (flags
[0] == 'g');
21537 regmatch
.rm_ic
= p_ic
;
21538 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
21539 if (regmatch
.regprog
!= NULL
)
21542 while (vim_regexec_nl(®match
, str
, (colnr_T
)(tail
- str
)))
21545 * Get some space for a temporary buffer to do the substitution
21546 * into. It will contain:
21547 * - The text up to where the match is.
21548 * - The substituted text.
21549 * - The text after the match.
21551 sublen
= vim_regsub(®match
, sub
, tail
, FALSE
, TRUE
, FALSE
);
21552 if (ga_grow(&ga
, (int)(STRLEN(tail
) + sublen
-
21553 (regmatch
.endp
[0] - regmatch
.startp
[0]))) == FAIL
)
21559 /* copy the text up to where the match is */
21560 i
= (int)(regmatch
.startp
[0] - tail
);
21561 mch_memmove((char_u
*)ga
.ga_data
+ ga
.ga_len
, tail
, (size_t)i
);
21562 /* add the substituted text */
21563 (void)vim_regsub(®match
, sub
, (char_u
*)ga
.ga_data
21564 + ga
.ga_len
+ i
, TRUE
, TRUE
, FALSE
);
21565 ga
.ga_len
+= i
+ sublen
- 1;
21566 /* avoid getting stuck on a match with an empty string */
21567 if (tail
== regmatch
.endp
[0])
21571 *((char_u
*)ga
.ga_data
+ ga
.ga_len
) = *tail
++;
21576 tail
= regmatch
.endp
[0];
21584 if (ga
.ga_data
!= NULL
)
21585 STRCPY((char *)ga
.ga_data
+ ga
.ga_len
, tail
);
21587 vim_free(regmatch
.regprog
);
21590 ret
= vim_strsave(ga
.ga_data
== NULL
? str
: (char_u
*)ga
.ga_data
);
21597 #endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */