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 clear_tv(&vimvars
[idx
].vv_tv
);
1322 vimvars
[idx
].vv_tv
= *save_tv
;
1323 if (vimvars
[idx
].vv_type
== VAR_UNKNOWN
)
1325 hi
= hash_find(&vimvarht
, vimvars
[idx
].vv_di
.di_key
);
1326 if (HASHITEM_EMPTY(hi
))
1327 EMSG2(_(e_intern2
), "restore_vimvar()");
1329 hash_remove(&vimvarht
, hi
);
1333 #if defined(FEAT_SPELL) || defined(PROTO)
1335 * Evaluate an expression to a list with suggestions.
1336 * For the "expr:" part of 'spellsuggest'.
1339 eval_spell_expr(badword
, expr
)
1345 list_T
*list
= NULL
;
1346 char_u
*p
= skipwhite(expr
);
1348 /* Set "v:val" to the bad word. */
1349 prepare_vimvar(VV_VAL
, &save_val
);
1350 vimvars
[VV_VAL
].vv_type
= VAR_STRING
;
1351 vimvars
[VV_VAL
].vv_str
= badword
;
1355 if (eval1(&p
, &rettv
, TRUE
) == OK
)
1357 if (rettv
.v_type
!= VAR_LIST
)
1360 list
= rettv
.vval
.v_list
;
1365 vimvars
[VV_VAL
].vv_str
= NULL
;
1366 restore_vimvar(VV_VAL
, &save_val
);
1372 * "list" is supposed to contain two items: a word and a number. Return the
1373 * word in "pp" and the number as the return value.
1374 * Return -1 if anything isn't right.
1375 * Used to get the good word and score from the eval_spell_expr() result.
1378 get_spellword(list
, pp
)
1384 li
= list
->lv_first
;
1387 *pp
= get_tv_string(&li
->li_tv
);
1392 return get_tv_number(&li
->li_tv
);
1397 * Top level evaluation function.
1398 * Returns an allocated typval_T with the result.
1399 * Returns NULL when there is an error.
1402 eval_expr(arg
, nextcmd
)
1408 tv
= (typval_T
*)alloc(sizeof(typval_T
));
1409 if (tv
!= NULL
&& eval0(arg
, tv
, nextcmd
, TRUE
) == FAIL
)
1419 #if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1420 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1422 * Call some vimL function and return the result in "*rettv".
1423 * Uses argv[argc] for the function arguments.
1424 * Returns OK or FAIL.
1427 call_vim_function(func
, argc
, argv
, safe
, rettv
)
1431 int safe
; /* use the sandbox */
1439 void *save_funccalp
= NULL
;
1442 argvars
= (typval_T
*)alloc((unsigned)((argc
+ 1) * sizeof(typval_T
)));
1443 if (argvars
== NULL
)
1446 for (i
= 0; i
< argc
; i
++)
1448 /* Pass a NULL or empty argument as an empty string */
1449 if (argv
[i
] == NULL
|| *argv
[i
] == NUL
)
1451 argvars
[i
].v_type
= VAR_STRING
;
1452 argvars
[i
].vval
.v_string
= (char_u
*)"";
1456 /* Recognize a number argument, the others must be strings. */
1457 vim_str2nr(argv
[i
], NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
1458 if (len
!= 0 && len
== (int)STRLEN(argv
[i
]))
1460 argvars
[i
].v_type
= VAR_NUMBER
;
1461 argvars
[i
].vval
.v_number
= n
;
1465 argvars
[i
].v_type
= VAR_STRING
;
1466 argvars
[i
].vval
.v_string
= argv
[i
];
1472 save_funccalp
= save_funccal();
1476 rettv
->v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
1477 ret
= call_func(func
, (int)STRLEN(func
), rettv
, argc
, argvars
,
1478 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
1479 &doesrange
, TRUE
, NULL
);
1483 restore_funccal(save_funccalp
);
1493 # if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1495 * Call vimL function "func" and return the result as a string.
1496 * Returns NULL when calling the function fails.
1497 * Uses argv[argc] for the function arguments.
1500 call_func_retstr(func
, argc
, argv
, safe
)
1504 int safe
; /* use the sandbox */
1509 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1512 retval
= vim_strsave(get_tv_string(&rettv
));
1518 # if defined(FEAT_COMPL_FUNC) || defined(PROTO)
1520 * Call vimL function "func" and return the result as a number.
1521 * Returns -1 when calling the function fails.
1522 * Uses argv[argc] for the function arguments.
1525 call_func_retnr(func
, argc
, argv
, safe
)
1529 int safe
; /* use the sandbox */
1534 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1537 retval
= get_tv_number_chk(&rettv
, NULL
);
1544 * Call vimL function "func" and return the result as a list
1545 * Uses argv[argc] for the function arguments.
1548 call_func_retlist(func
, argc
, argv
, safe
)
1552 int safe
; /* use the sandbox */
1556 if (call_vim_function(func
, argc
, argv
, safe
, &rettv
) == FAIL
)
1559 if (rettv
.v_type
!= VAR_LIST
)
1565 return rettv
.vval
.v_list
;
1571 * Save the current function call pointer, and set it to NULL.
1572 * Used when executing autocommands and for ":source".
1577 funccall_T
*fc
= current_funccal
;
1579 current_funccal
= NULL
;
1584 restore_funccal(vfc
)
1587 funccall_T
*fc
= (funccall_T
*)vfc
;
1589 current_funccal
= fc
;
1592 #if defined(FEAT_PROFILE) || defined(PROTO)
1594 * Prepare profiling for entering a child or something else that is not
1595 * counted for the script/function itself.
1596 * Should always be called in pair with prof_child_exit().
1599 prof_child_enter(tm
)
1600 proftime_T
*tm
; /* place to store waittime */
1602 funccall_T
*fc
= current_funccal
;
1604 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1605 profile_start(&fc
->prof_child
);
1606 script_prof_save(tm
);
1610 * Take care of time spent in a child.
1611 * Should always be called after prof_child_enter().
1615 proftime_T
*tm
; /* where waittime was stored */
1617 funccall_T
*fc
= current_funccal
;
1619 if (fc
!= NULL
&& fc
->func
->uf_profiling
)
1621 profile_end(&fc
->prof_child
);
1622 profile_sub_wait(tm
, &fc
->prof_child
); /* don't count waiting time */
1623 profile_add(&fc
->func
->uf_tm_children
, &fc
->prof_child
);
1624 profile_add(&fc
->func
->uf_tml_children
, &fc
->prof_child
);
1626 script_prof_restore(tm
);
1633 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1634 * it in "*cp". Doesn't give error messages.
1637 eval_foldexpr(arg
, cp
)
1644 int use_sandbox
= was_set_insecurely((char_u
*)"foldexpr",
1652 if (eval0(arg
, &tv
, NULL
, TRUE
) == FAIL
)
1656 /* If the result is a number, just return the number. */
1657 if (tv
.v_type
== VAR_NUMBER
)
1658 retval
= tv
.vval
.v_number
;
1659 else if (tv
.v_type
!= VAR_STRING
|| tv
.vval
.v_string
== NULL
)
1663 /* If the result is a string, check if there is a non-digit before
1665 s
= tv
.vval
.v_string
;
1666 if (!VIM_ISDIGIT(*s
) && *s
!= '-')
1668 retval
= atol((char *)s
);
1682 * ":let" list all variable values
1683 * ":let var1 var2" list variable values
1684 * ":let var = expr" assignment command.
1685 * ":let var += expr" assignment command.
1686 * ":let var -= expr" assignment command.
1687 * ":let var .= expr" assignment command.
1688 * ":let [var1, var2] = expr" unpack list.
1694 char_u
*arg
= eap
->arg
;
1695 char_u
*expr
= NULL
;
1704 argend
= skip_var_list(arg
, &var_count
, &semicolon
);
1707 if (argend
> arg
&& argend
[-1] == '.') /* for var.='str' */
1709 expr
= vim_strchr(argend
, '=');
1713 * ":let" without "=": list variables
1717 else if (!ends_excmd(*arg
))
1718 /* ":let var1 var2" */
1719 arg
= list_arg_vars(eap
, arg
, &first
);
1720 else if (!eap
->skip
)
1723 list_glob_vars(&first
);
1724 list_buf_vars(&first
);
1725 list_win_vars(&first
);
1727 list_tab_vars(&first
);
1729 list_script_vars(&first
);
1730 list_func_vars(&first
);
1731 list_vim_vars(&first
);
1733 eap
->nextcmd
= check_nextcmd(arg
);
1741 if (vim_strchr((char_u
*)"+-.", expr
[-1]) != NULL
)
1742 op
[0] = expr
[-1]; /* +=, -= or .= */
1744 expr
= skipwhite(expr
+ 1);
1748 i
= eval0(expr
, &rettv
, &eap
->nextcmd
, !eap
->skip
);
1757 (void)ex_let_vars(eap
->arg
, &rettv
, FALSE
, semicolon
, var_count
,
1765 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1766 * Handles both "var" with any type and "[var, var; var]" with a list type.
1767 * When "nextchars" is not NULL it points to a string with characters that
1768 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1770 * Returns OK or FAIL;
1773 ex_let_vars(arg_start
, tv
, copy
, semicolon
, var_count
, nextchars
)
1776 int copy
; /* copy values from "tv", don't move */
1777 int semicolon
; /* from skip_var_list() */
1778 int var_count
; /* from skip_var_list() */
1781 char_u
*arg
= arg_start
;
1790 * ":let var = expr" or ":for var in list"
1792 if (ex_let_one(arg
, tv
, copy
, nextchars
, nextchars
) == NULL
)
1798 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1800 if (tv
->v_type
!= VAR_LIST
|| (l
= tv
->vval
.v_list
) == NULL
)
1807 if (semicolon
== 0 && var_count
< i
)
1809 EMSG(_("E687: Less targets than List items"));
1812 if (var_count
- semicolon
> i
)
1814 EMSG(_("E688: More targets than List items"));
1821 arg
= skipwhite(arg
+ 1);
1822 arg
= ex_let_one(arg
, &item
->li_tv
, TRUE
, (char_u
*)",;]", nextchars
);
1823 item
= item
->li_next
;
1827 arg
= skipwhite(arg
);
1830 /* Put the rest of the list (may be empty) in the var after ';'.
1831 * Create a new list for this. */
1835 while (item
!= NULL
)
1837 list_append_tv(l
, &item
->li_tv
);
1838 item
= item
->li_next
;
1841 ltv
.v_type
= VAR_LIST
;
1843 ltv
.vval
.v_list
= l
;
1846 arg
= ex_let_one(skipwhite(arg
+ 1), <v
, FALSE
,
1847 (char_u
*)"]", nextchars
);
1853 else if (*arg
!= ',' && *arg
!= ']')
1855 EMSG2(_(e_intern2
), "ex_let_vars()");
1864 * Skip over assignable variable "var" or list of variables "[var, var]".
1865 * Used for ":let varvar = expr" and ":for varvar in expr".
1866 * For "[var, var]" increment "*var_count" for each variable.
1867 * for "[var, var; var]" set "semicolon".
1868 * Return NULL for an error.
1871 skip_var_list(arg
, var_count
, semicolon
)
1880 /* "[var, var]": find the matching ']'. */
1884 p
= skipwhite(p
+ 1); /* skip whites after '[', ';' or ',' */
1885 s
= skip_var_one(p
);
1888 EMSG2(_(e_invarg2
), p
);
1898 if (*semicolon
== 1)
1900 EMSG(_("Double ; in list of variables"));
1907 EMSG2(_(e_invarg2
), p
);
1914 return skip_var_one(arg
);
1918 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1925 if (*arg
== '@' && arg
[1] != NUL
)
1927 return find_name_end(*arg
== '$' || *arg
== '&' ? arg
+ 1 : arg
,
1928 NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
1932 * List variables for hashtab "ht" with prefix "prefix".
1933 * If "empty" is TRUE also list NULL strings as empty strings.
1936 list_hashtable_vars(ht
, prefix
, empty
, first
)
1946 todo
= (int)ht
->ht_used
;
1947 for (hi
= ht
->ht_array
; todo
> 0 && !got_int
; ++hi
)
1949 if (!HASHITEM_EMPTY(hi
))
1953 if (empty
|| di
->di_tv
.v_type
!= VAR_STRING
1954 || di
->di_tv
.vval
.v_string
!= NULL
)
1955 list_one_var(di
, prefix
, first
);
1961 * List global variables.
1964 list_glob_vars(first
)
1967 list_hashtable_vars(&globvarht
, (char_u
*)"", TRUE
, first
);
1971 * List buffer variables.
1974 list_buf_vars(first
)
1977 char_u numbuf
[NUMBUFLEN
];
1979 list_hashtable_vars(&curbuf
->b_vars
.dv_hashtab
, (char_u
*)"b:",
1982 sprintf((char *)numbuf
, "%ld", (long)curbuf
->b_changedtick
);
1983 list_one_var_a((char_u
*)"b:", (char_u
*)"changedtick", VAR_NUMBER
,
1988 * List window variables.
1991 list_win_vars(first
)
1994 list_hashtable_vars(&curwin
->w_vars
.dv_hashtab
,
1995 (char_u
*)"w:", TRUE
, first
);
2000 * List tab page variables.
2003 list_tab_vars(first
)
2006 list_hashtable_vars(&curtab
->tp_vars
.dv_hashtab
,
2007 (char_u
*)"t:", TRUE
, first
);
2012 * List Vim variables.
2015 list_vim_vars(first
)
2018 list_hashtable_vars(&vimvarht
, (char_u
*)"v:", FALSE
, first
);
2022 * List script-local variables, if there is a script.
2025 list_script_vars(first
)
2028 if (current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
2029 list_hashtable_vars(&SCRIPT_VARS(current_SID
),
2030 (char_u
*)"s:", FALSE
, first
);
2034 * List function variables, if there is a function.
2037 list_func_vars(first
)
2040 if (current_funccal
!= NULL
)
2041 list_hashtable_vars(¤t_funccal
->l_vars
.dv_hashtab
,
2042 (char_u
*)"l:", FALSE
, first
);
2046 * List variables in "arg".
2049 list_arg_vars(eap
, arg
, first
)
2062 while (!ends_excmd(*arg
) && !got_int
)
2064 if (error
|| eap
->skip
)
2066 arg
= find_name_end(arg
, NULL
, NULL
, FNE_INCL_BR
| FNE_CHECK_START
);
2067 if (!vim_iswhite(*arg
) && !ends_excmd(*arg
))
2070 EMSG(_(e_trailing
));
2076 /* get_name_len() takes care of expanding curly braces */
2077 name_start
= name
= arg
;
2078 len
= get_name_len(&arg
, &tofree
, TRUE
, TRUE
);
2081 /* This is mainly to keep test 49 working: when expanding
2082 * curly braces fails overrule the exception error message. */
2083 if (len
< 0 && !aborting())
2086 EMSG2(_(e_invarg2
), arg
);
2095 if (get_var_tv(name
, len
, &tv
, TRUE
) == FAIL
)
2099 /* handle d.key, l[idx], f(expr) */
2101 if (handle_subscript(&arg
, &tv
, TRUE
, TRUE
) == FAIL
)
2105 if (arg
== arg_subsc
&& len
== 2 && name
[1] == ':')
2109 case 'g': list_glob_vars(first
); break;
2110 case 'b': list_buf_vars(first
); break;
2111 case 'w': list_win_vars(first
); break;
2113 case 't': list_tab_vars(first
); break;
2115 case 'v': list_vim_vars(first
); break;
2116 case 's': list_script_vars(first
); break;
2117 case 'l': list_func_vars(first
); break;
2119 EMSG2(_("E738: Can't list variables for %s"), name
);
2124 char_u numbuf
[NUMBUFLEN
];
2129 s
= echo_string(&tv
, &tf
, numbuf
, 0);
2132 list_one_var_a((char_u
*)"",
2133 arg
== arg_subsc
? name
: name_start
,
2135 s
== NULL
? (char_u
*)"" : s
,
2148 arg
= skipwhite(arg
);
2155 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2156 * Returns a pointer to the char just after the var name.
2157 * Returns NULL if there is an error.
2160 ex_let_one(arg
, tv
, copy
, endchars
, op
)
2161 char_u
*arg
; /* points to variable name */
2162 typval_T
*tv
; /* value to assign to variable */
2163 int copy
; /* copy value from "tv" */
2164 char_u
*endchars
; /* valid chars after variable name or NULL */
2165 char_u
*op
; /* "+", "-", "." or NULL*/
2170 char_u
*arg_end
= NULL
;
2173 char_u
*tofree
= NULL
;
2176 * ":let $VAR = expr": Set environment variable.
2180 /* Find the end of the name. */
2183 len
= get_env_len(&arg
);
2185 EMSG2(_(e_invarg2
), name
- 1);
2188 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2189 EMSG2(_(e_letwrong
), op
);
2190 else if (endchars
!= NULL
2191 && vim_strchr(endchars
, *skipwhite(arg
)) == NULL
)
2192 EMSG(_(e_letunexp
));
2197 p
= get_tv_string_chk(tv
);
2198 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2200 int mustfree
= FALSE
;
2201 char_u
*s
= vim_getenv(name
, &mustfree
);
2205 p
= tofree
= concat_str(s
, p
);
2212 vim_setenv(name
, p
);
2213 if (STRICMP(name
, "HOME") == 0)
2215 else if (didset_vim
&& STRICMP(name
, "VIM") == 0)
2217 else if (didset_vimruntime
2218 && STRICMP(name
, "VIMRUNTIME") == 0)
2219 didset_vimruntime
= FALSE
;
2229 * ":let &option = expr": Set option value.
2230 * ":let &l:option = expr": Set local option value.
2231 * ":let &g:option = expr": Set global option value.
2233 else if (*arg
== '&')
2235 /* Find the end of the name. */
2236 p
= find_option_end(&arg
, &opt_flags
);
2237 if (p
== NULL
|| (endchars
!= NULL
2238 && vim_strchr(endchars
, *skipwhite(p
)) == NULL
))
2239 EMSG(_(e_letunexp
));
2245 char_u
*stringval
= NULL
;
2251 n
= get_tv_number(tv
);
2252 s
= get_tv_string_chk(tv
); /* != NULL if number or string */
2253 if (s
!= NULL
&& op
!= NULL
&& *op
!= '=')
2255 opt_type
= get_option_value(arg
, &numval
,
2256 &stringval
, opt_flags
);
2257 if ((opt_type
== 1 && *op
== '.')
2258 || (opt_type
== 0 && *op
!= '.'))
2259 EMSG2(_(e_letwrong
), op
);
2262 if (opt_type
== 1) /* number */
2269 else if (opt_type
== 0 && stringval
!= NULL
) /* string */
2271 s
= concat_str(stringval
, s
);
2272 vim_free(stringval
);
2279 set_option_value(arg
, n
, s
, opt_flags
);
2283 vim_free(stringval
);
2288 * ":let @r = expr": Set register contents.
2290 else if (*arg
== '@')
2293 if (op
!= NULL
&& (*op
== '+' || *op
== '-'))
2294 EMSG2(_(e_letwrong
), op
);
2295 else if (endchars
!= NULL
2296 && vim_strchr(endchars
, *skipwhite(arg
+ 1)) == NULL
)
2297 EMSG(_(e_letunexp
));
2300 char_u
*ptofree
= NULL
;
2303 p
= get_tv_string_chk(tv
);
2304 if (p
!= NULL
&& op
!= NULL
&& *op
== '.')
2306 s
= get_reg_contents(*arg
== '@' ? '"' : *arg
, TRUE
, TRUE
);
2309 p
= ptofree
= concat_str(s
, p
);
2315 write_reg_contents(*arg
== '@' ? '"' : *arg
, p
, -1, FALSE
);
2323 * ":let var = expr": Set internal variable.
2324 * ":let {expr} = expr": Idem, name made with curly braces
2326 else if (eval_isnamec1(*arg
) || *arg
== '{')
2330 p
= get_lval(arg
, tv
, &lv
, FALSE
, FALSE
, FALSE
, FNE_CHECK_START
);
2331 if (p
!= NULL
&& lv
.ll_name
!= NULL
)
2333 if (endchars
!= NULL
&& vim_strchr(endchars
, *skipwhite(p
)) == NULL
)
2334 EMSG(_(e_letunexp
));
2337 set_var_lval(&lv
, p
, tv
, copy
, op
);
2345 EMSG2(_(e_invarg2
), arg
);
2351 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2354 check_changedtick(arg
)
2357 if (STRNCMP(arg
, "b:changedtick", 13) == 0 && !eval_isnamec(arg
[13]))
2359 EMSG2(_(e_readonlyvar
), arg
);
2366 * Get an lval: variable, Dict item or List item that can be assigned a value
2367 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2368 * "name.key", "name.key[expr]" etc.
2369 * Indexing only works if "name" is an existing List or Dictionary.
2370 * "name" points to the start of the name.
2371 * If "rettv" is not NULL it points to the value to be assigned.
2372 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2373 * wrong; must end in space or cmd separator.
2375 * Returns a pointer to just after the name, including indexes.
2376 * When an evaluation error occurs "lp->ll_name" is NULL;
2377 * Returns NULL for a parsing error. Still need to free items in "lp"!
2380 get_lval(name
, rettv
, lp
, unlet
, skip
, quiet
, fne_flags
)
2386 int quiet
; /* don't give error messages */
2387 int fne_flags
; /* flags for find_name_end() */
2390 char_u
*expr_start
, *expr_end
;
2401 /* Clear everything in "lp". */
2402 vim_memset(lp
, 0, sizeof(lval_T
));
2406 /* When skipping just find the end of the name. */
2408 return find_name_end(name
, NULL
, NULL
, FNE_INCL_BR
| fne_flags
);
2411 /* Find the end of the name. */
2412 p
= find_name_end(name
, &expr_start
, &expr_end
, fne_flags
);
2413 if (expr_start
!= NULL
)
2415 /* Don't expand the name when we already know there is an error. */
2416 if (unlet
&& !vim_iswhite(*p
) && !ends_excmd(*p
)
2417 && *p
!= '[' && *p
!= '.')
2419 EMSG(_(e_trailing
));
2423 lp
->ll_exp_name
= make_expanded_name(name
, expr_start
, expr_end
, p
);
2424 if (lp
->ll_exp_name
== NULL
)
2426 /* Report an invalid expression in braces, unless the
2427 * expression evaluation has been cancelled due to an
2428 * aborting error, an interrupt, or an exception. */
2429 if (!aborting() && !quiet
)
2432 EMSG2(_(e_invarg2
), name
);
2436 lp
->ll_name
= lp
->ll_exp_name
;
2441 /* Without [idx] or .key we are done. */
2442 if ((*p
!= '[' && *p
!= '.') || lp
->ll_name
== NULL
)
2447 v
= find_var(lp
->ll_name
, &ht
);
2448 if (v
== NULL
&& !quiet
)
2449 EMSG2(_(e_undefvar
), lp
->ll_name
);
2455 * Loop until no more [idx] or .key is following.
2457 lp
->ll_tv
= &v
->di_tv
;
2458 while (*p
== '[' || (*p
== '.' && lp
->ll_tv
->v_type
== VAR_DICT
))
2460 if (!(lp
->ll_tv
->v_type
== VAR_LIST
&& lp
->ll_tv
->vval
.v_list
!= NULL
)
2461 && !(lp
->ll_tv
->v_type
== VAR_DICT
2462 && lp
->ll_tv
->vval
.v_dict
!= NULL
))
2465 EMSG(_("E689: Can only index a List or Dictionary"));
2471 EMSG(_("E708: [:] must come last"));
2479 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
2484 EMSG(_(e_emptykey
));
2491 /* Get the index [expr] or the first index [expr: ]. */
2492 p
= skipwhite(p
+ 1);
2498 if (eval1(&p
, &var1
, TRUE
) == FAIL
) /* recursive! */
2500 if (get_tv_string_chk(&var1
) == NULL
)
2502 /* not a number or string */
2508 /* Optionally get the second index [ :expr]. */
2511 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2514 EMSG(_(e_dictrange
));
2519 if (rettv
!= NULL
&& (rettv
->v_type
!= VAR_LIST
2520 || rettv
->vval
.v_list
== NULL
))
2523 EMSG(_("E709: [:] requires a List value"));
2528 p
= skipwhite(p
+ 1);
2530 lp
->ll_empty2
= TRUE
;
2533 lp
->ll_empty2
= FALSE
;
2534 if (eval1(&p
, &var2
, TRUE
) == FAIL
) /* recursive! */
2540 if (get_tv_string_chk(&var2
) == NULL
)
2542 /* not a number or string */
2549 lp
->ll_range
= TRUE
;
2552 lp
->ll_range
= FALSE
;
2557 EMSG(_(e_missbrac
));
2560 if (lp
->ll_range
&& !lp
->ll_empty2
)
2565 /* Skip to past ']'. */
2569 if (lp
->ll_tv
->v_type
== VAR_DICT
)
2573 /* "[key]": get key from "var1" */
2574 key
= get_tv_string(&var1
); /* is number or string */
2578 EMSG(_(e_emptykey
));
2584 lp
->ll_dict
= lp
->ll_tv
->vval
.v_dict
;
2585 lp
->ll_di
= dict_find(lp
->ll_dict
, key
, len
);
2586 if (lp
->ll_di
== NULL
)
2588 /* Key does not exist in dict: may need to add it. */
2589 if (*p
== '[' || *p
== '.' || unlet
)
2592 EMSG2(_(e_dictkey
), key
);
2598 lp
->ll_newkey
= vim_strsave(key
);
2600 lp
->ll_newkey
= vim_strnsave(key
, len
);
2603 if (lp
->ll_newkey
== NULL
)
2609 lp
->ll_tv
= &lp
->ll_di
->di_tv
;
2614 * Get the number and item for the only or first index of the List.
2620 lp
->ll_n1
= get_tv_number(&var1
); /* is number or string */
2624 lp
->ll_list
= lp
->ll_tv
->vval
.v_list
;
2625 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2626 if (lp
->ll_li
== NULL
)
2631 lp
->ll_li
= list_find(lp
->ll_list
, lp
->ll_n1
);
2634 if (lp
->ll_li
== NULL
)
2636 if (lp
->ll_range
&& !lp
->ll_empty2
)
2642 * May need to find the item or absolute index for the second
2644 * When no index given: "lp->ll_empty2" is TRUE.
2645 * Otherwise "lp->ll_n2" is set to the second index.
2647 if (lp
->ll_range
&& !lp
->ll_empty2
)
2649 lp
->ll_n2
= get_tv_number(&var2
); /* is number or string */
2653 ni
= list_find(lp
->ll_list
, lp
->ll_n2
);
2656 lp
->ll_n2
= list_idx_of_item(lp
->ll_list
, ni
);
2659 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2661 lp
->ll_n1
= list_idx_of_item(lp
->ll_list
, lp
->ll_li
);
2662 if (lp
->ll_n2
< lp
->ll_n1
)
2666 lp
->ll_tv
= &lp
->ll_li
->li_tv
;
2674 * Clear lval "lp" that was filled by get_lval().
2680 vim_free(lp
->ll_exp_name
);
2681 vim_free(lp
->ll_newkey
);
2685 * Set a variable that was parsed by get_lval() to "rettv".
2686 * "endp" points to just after the parsed name.
2687 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
2690 set_var_lval(lp
, endp
, rettv
, copy
, op
)
2701 if (lp
->ll_tv
== NULL
)
2703 if (!check_changedtick(lp
->ll_name
))
2707 if (op
!= NULL
&& *op
!= '=')
2711 /* handle +=, -= and .= */
2712 if (get_var_tv(lp
->ll_name
, (int)STRLEN(lp
->ll_name
),
2715 if (tv_op(&tv
, rettv
, op
) == OK
)
2716 set_var(lp
->ll_name
, &tv
, FALSE
);
2721 set_var(lp
->ll_name
, rettv
, copy
);
2725 else if (tv_check_lock(lp
->ll_newkey
== NULL
2727 : lp
->ll_tv
->vval
.v_dict
->dv_lock
, lp
->ll_name
))
2729 else if (lp
->ll_range
)
2732 * Assign the List values to the list items.
2734 for (ri
= rettv
->vval
.v_list
->lv_first
; ri
!= NULL
; )
2736 if (op
!= NULL
&& *op
!= '=')
2737 tv_op(&lp
->ll_li
->li_tv
, &ri
->li_tv
, op
);
2740 clear_tv(&lp
->ll_li
->li_tv
);
2741 copy_tv(&ri
->li_tv
, &lp
->ll_li
->li_tv
);
2744 if (ri
== NULL
|| (!lp
->ll_empty2
&& lp
->ll_n2
== lp
->ll_n1
))
2746 if (lp
->ll_li
->li_next
== NULL
)
2748 /* Need to add an empty item. */
2749 if (list_append_number(lp
->ll_list
, 0) == FAIL
)
2755 lp
->ll_li
= lp
->ll_li
->li_next
;
2759 EMSG(_("E710: List value has more items than target"));
2760 else if (lp
->ll_empty2
2761 ? (lp
->ll_li
!= NULL
&& lp
->ll_li
->li_next
!= NULL
)
2762 : lp
->ll_n1
!= lp
->ll_n2
)
2763 EMSG(_("E711: List value has not enough items"));
2768 * Assign to a List or Dictionary item.
2770 if (lp
->ll_newkey
!= NULL
)
2772 if (op
!= NULL
&& *op
!= '=')
2774 EMSG2(_(e_letwrong
), op
);
2778 /* Need to add an item to the Dictionary. */
2779 di
= dictitem_alloc(lp
->ll_newkey
);
2782 if (dict_add(lp
->ll_tv
->vval
.v_dict
, di
) == FAIL
)
2787 lp
->ll_tv
= &di
->di_tv
;
2789 else if (op
!= NULL
&& *op
!= '=')
2791 tv_op(lp
->ll_tv
, rettv
, op
);
2795 clear_tv(lp
->ll_tv
);
2798 * Assign the value to the variable or list item.
2801 copy_tv(rettv
, lp
->ll_tv
);
2804 *lp
->ll_tv
= *rettv
;
2805 lp
->ll_tv
->v_lock
= 0;
2812 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2813 * Returns OK or FAIL.
2822 char_u numbuf
[NUMBUFLEN
];
2825 /* Can't do anything with a Funcref or a Dict on the right. */
2826 if (tv2
->v_type
!= VAR_FUNC
&& tv2
->v_type
!= VAR_DICT
)
2828 switch (tv1
->v_type
)
2835 if (*op
!= '+' || tv2
->v_type
!= VAR_LIST
)
2838 if (tv1
->vval
.v_list
!= NULL
&& tv2
->vval
.v_list
!= NULL
)
2839 list_extend(tv1
->vval
.v_list
, tv2
->vval
.v_list
, NULL
);
2844 if (tv2
->v_type
== VAR_LIST
)
2846 if (*op
== '+' || *op
== '-')
2848 /* nr += nr or nr -= nr*/
2849 n
= get_tv_number(tv1
);
2851 n
+= get_tv_number(tv2
);
2853 n
-= get_tv_number(tv2
);
2855 tv1
->v_type
= VAR_NUMBER
;
2856 tv1
->vval
.v_number
= n
;
2861 s
= get_tv_string(tv1
);
2862 s
= concat_str(s
, get_tv_string_buf(tv2
, numbuf
));
2864 tv1
->v_type
= VAR_STRING
;
2865 tv1
->vval
.v_string
= s
;
2871 EMSG2(_(e_letwrong
), op
);
2876 * Add a watcher to a list.
2879 list_add_watch(l
, lw
)
2883 lw
->lw_next
= l
->lv_watch
;
2888 * Remove a watcher from a list.
2889 * No warning when it isn't found...
2892 list_rem_watch(l
, lwrem
)
2896 listwatch_T
*lw
, **lwp
;
2899 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
2911 * Just before removing an item from a list: advance watchers to the next
2915 list_fix_watch(l
, item
)
2921 for (lw
= l
->lv_watch
; lw
!= NULL
; lw
= lw
->lw_next
)
2922 if (lw
->lw_item
== item
)
2923 lw
->lw_item
= item
->li_next
;
2927 * Evaluate the expression used in a ":for var in expr" command.
2928 * "arg" points to "var".
2929 * Set "*errp" to TRUE for an error, FALSE otherwise;
2930 * Return a pointer that holds the info. Null when there is an error.
2933 eval_for_line(arg
, errp
, nextcmdp
, skip
)
2944 *errp
= TRUE
; /* default: there is an error */
2946 fi
= (forinfo_T
*)alloc_clear(sizeof(forinfo_T
));
2950 expr
= skip_var_list(arg
, &fi
->fi_varcount
, &fi
->fi_semicolon
);
2954 expr
= skipwhite(expr
);
2955 if (expr
[0] != 'i' || expr
[1] != 'n' || !vim_iswhite(expr
[2]))
2957 EMSG(_("E690: Missing \"in\" after :for"));
2963 if (eval0(skipwhite(expr
+ 2), &tv
, nextcmdp
, !skip
) == OK
)
2969 if (tv
.v_type
!= VAR_LIST
|| l
== NULL
)
2976 /* No need to increment the refcount, it's already set for the
2977 * list being used in "tv". */
2979 list_add_watch(l
, &fi
->fi_lw
);
2980 fi
->fi_lw
.lw_item
= l
->lv_first
;
2991 * Use the first item in a ":for" list. Advance to the next.
2992 * Assign the values to the variable (list). "arg" points to the first one.
2993 * Return TRUE when a valid item was found, FALSE when at end of list or
2997 next_for_item(fi_void
, arg
)
3001 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3005 item
= fi
->fi_lw
.lw_item
;
3010 fi
->fi_lw
.lw_item
= item
->li_next
;
3011 result
= (ex_let_vars(arg
, &item
->li_tv
, TRUE
,
3012 fi
->fi_semicolon
, fi
->fi_varcount
, NULL
) == OK
);
3018 * Free the structure used to store info used by ":for".
3021 free_for_info(fi_void
)
3024 forinfo_T
*fi
= (forinfo_T
*)fi_void
;
3026 if (fi
!= NULL
&& fi
->fi_list
!= NULL
)
3028 list_rem_watch(fi
->fi_list
, &fi
->fi_lw
);
3029 list_unref(fi
->fi_list
);
3034 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3037 set_context_for_expression(xp
, arg
, cmdidx
)
3046 if (cmdidx
== CMD_let
)
3048 xp
->xp_context
= EXPAND_USER_VARS
;
3049 if (vim_strpbrk(arg
, (char_u
*)"\"'+-*/%.=!?~|&$([<>,#") == NULL
)
3051 /* ":let var1 var2 ...": find last space. */
3052 for (p
= arg
+ STRLEN(arg
); p
>= arg
; )
3055 mb_ptr_back(arg
, p
);
3056 if (vim_iswhite(*p
))
3063 xp
->xp_context
= cmdidx
== CMD_call
? EXPAND_FUNCTIONS
3064 : EXPAND_EXPRESSION
;
3065 while ((xp
->xp_pattern
= vim_strpbrk(arg
,
3066 (char_u
*)"\"'+-*/%.=!?~|&$([<>,#")) != NULL
)
3068 c
= *xp
->xp_pattern
;
3071 c
= xp
->xp_pattern
[1];
3075 xp
->xp_context
= cmdidx
!= CMD_let
|| got_eq
3076 ? EXPAND_EXPRESSION
: EXPAND_NOTHING
;
3080 xp
->xp_context
= EXPAND_SETTINGS
;
3081 if ((c
== 'l' || c
== 'g') && xp
->xp_pattern
[2] == ':')
3082 xp
->xp_pattern
+= 2;
3088 /* environment variable */
3089 xp
->xp_context
= EXPAND_ENV_VARS
;
3094 xp
->xp_context
= EXPAND_EXPRESSION
;
3097 && xp
->xp_context
== EXPAND_FUNCTIONS
3098 && vim_strchr(xp
->xp_pattern
, '(') == NULL
)
3100 /* Function name can start with "<SNR>" */
3103 else if (cmdidx
!= CMD_let
|| got_eq
)
3105 if (c
== '"') /* string */
3107 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '"')
3108 if (c
== '\\' && xp
->xp_pattern
[1] != NUL
)
3110 xp
->xp_context
= EXPAND_NOTHING
;
3112 else if (c
== '\'') /* literal string */
3114 /* Trick: '' is like stopping and starting a literal string. */
3115 while ((c
= *++xp
->xp_pattern
) != NUL
&& c
!= '\'')
3117 xp
->xp_context
= EXPAND_NOTHING
;
3121 if (xp
->xp_pattern
[1] == '|')
3124 xp
->xp_context
= EXPAND_EXPRESSION
;
3127 xp
->xp_context
= EXPAND_COMMANDS
;
3130 xp
->xp_context
= EXPAND_EXPRESSION
;
3133 /* Doesn't look like something valid, expand as an expression
3135 xp
->xp_context
= EXPAND_EXPRESSION
;
3136 arg
= xp
->xp_pattern
;
3138 while ((c
= *++arg
) != NUL
&& (c
== ' ' || c
== '\t'))
3141 xp
->xp_pattern
= arg
;
3144 #endif /* FEAT_CMDL_COMPL */
3147 * ":1,25call func(arg1, arg2)" function call.
3153 char_u
*arg
= eap
->arg
;
3164 tofree
= trans_function_name(&arg
, eap
->skip
, TFN_INT
, &fudi
);
3165 if (fudi
.fd_newkey
!= NULL
)
3167 /* Still need to give an error message for missing key. */
3168 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
3169 vim_free(fudi
.fd_newkey
);
3174 /* Increase refcount on dictionary, it could get deleted when evaluating
3176 if (fudi
.fd_dict
!= NULL
)
3177 ++fudi
.fd_dict
->dv_refcount
;
3179 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3180 len
= (int)STRLEN(tofree
);
3181 name
= deref_func_name(tofree
, &len
);
3183 /* Skip white space to allow ":call func ()". Not good, but required for
3184 * backward compatibility. */
3185 startarg
= skipwhite(arg
);
3186 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
3188 if (*startarg
!= '(')
3190 EMSG2(_("E107: Missing braces: %s"), eap
->arg
);
3195 * When skipping, evaluate the function once, to find the end of the
3197 * When the function takes a range, this is discovered after the first
3198 * call, and the loop is broken.
3203 lnum
= eap
->line2
; /* do it once, also with an invalid range */
3207 for ( ; lnum
<= eap
->line2
; ++lnum
)
3209 if (!eap
->skip
&& eap
->addr_count
> 0)
3211 curwin
->w_cursor
.lnum
= lnum
;
3212 curwin
->w_cursor
.col
= 0;
3215 if (get_func_tv(name
, (int)STRLEN(name
), &rettv
, &arg
,
3216 eap
->line1
, eap
->line2
, &doesrange
,
3217 !eap
->skip
, fudi
.fd_dict
) == FAIL
)
3223 /* Handle a function returning a Funcref, Dictionary or List. */
3224 if (handle_subscript(&arg
, &rettv
, !eap
->skip
, TRUE
) == FAIL
)
3231 if (doesrange
|| eap
->skip
)
3234 /* Stop when immediately aborting on error, or when an interrupt
3235 * occurred or an exception was thrown but not caught.
3236 * get_func_tv() returned OK, so that the check for trailing
3237 * characters below is executed. */
3246 /* Check for trailing illegal characters and a following command. */
3247 if (!ends_excmd(*arg
))
3250 EMSG(_(e_trailing
));
3253 eap
->nextcmd
= check_nextcmd(arg
);
3257 dict_unref(fudi
.fd_dict
);
3262 * ":unlet[!] var1 ... " command.
3268 ex_unletlock(eap
, eap
->arg
, 0);
3272 * ":lockvar" and ":unlockvar" commands
3278 char_u
*arg
= eap
->arg
;
3283 else if (vim_isdigit(*arg
))
3285 deep
= getdigits(&arg
);
3286 arg
= skipwhite(arg
);
3289 ex_unletlock(eap
, arg
, deep
);
3293 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3296 ex_unletlock(eap
, argstart
, deep
)
3301 char_u
*arg
= argstart
;
3308 /* Parse the name and find the end. */
3309 name_end
= get_lval(arg
, NULL
, &lv
, TRUE
, eap
->skip
|| error
, FALSE
,
3311 if (lv
.ll_name
== NULL
)
3312 error
= TRUE
; /* error but continue parsing */
3313 if (name_end
== NULL
|| (!vim_iswhite(*name_end
)
3314 && !ends_excmd(*name_end
)))
3316 if (name_end
!= NULL
)
3319 EMSG(_(e_trailing
));
3321 if (!(eap
->skip
|| error
))
3326 if (!error
&& !eap
->skip
)
3328 if (eap
->cmdidx
== CMD_unlet
)
3330 if (do_unlet_var(&lv
, name_end
, eap
->forceit
) == FAIL
)
3335 if (do_lock_var(&lv
, name_end
, deep
,
3336 eap
->cmdidx
== CMD_lockvar
) == FAIL
)
3344 arg
= skipwhite(name_end
);
3345 } while (!ends_excmd(*arg
));
3347 eap
->nextcmd
= check_nextcmd(arg
);
3351 do_unlet_var(lp
, name_end
, forceit
)
3359 if (lp
->ll_tv
== NULL
)
3364 /* Normal name or expanded name. */
3365 if (check_changedtick(lp
->ll_name
))
3367 else if (do_unlet(lp
->ll_name
, forceit
) == FAIL
)
3371 else if (tv_check_lock(lp
->ll_tv
->v_lock
, lp
->ll_name
))
3373 else if (lp
->ll_range
)
3377 /* Delete a range of List items. */
3378 while (lp
->ll_li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3380 li
= lp
->ll_li
->li_next
;
3381 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3388 if (lp
->ll_list
!= NULL
)
3389 /* unlet a List item. */
3390 listitem_remove(lp
->ll_list
, lp
->ll_li
);
3392 /* unlet a Dictionary item. */
3393 dictitem_remove(lp
->ll_dict
, lp
->ll_di
);
3400 * "unlet" a variable. Return OK if it existed, FAIL if not.
3401 * When "forceit" is TRUE don't complain if the variable doesn't exist.
3404 do_unlet(name
, forceit
)
3412 ht
= find_var_ht(name
, &varname
);
3413 if (ht
!= NULL
&& *varname
!= NUL
)
3415 hi
= hash_find(ht
, varname
);
3416 if (!HASHITEM_EMPTY(hi
))
3418 if (var_check_fixed(HI2DI(hi
)->di_flags
, name
))
3420 if (var_check_ro(HI2DI(hi
)->di_flags
, name
))
3428 EMSG2(_("E108: No such variable: \"%s\""), name
);
3433 * Lock or unlock variable indicated by "lp".
3434 * "deep" is the levels to go (-1 for unlimited);
3435 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3438 do_lock_var(lp
, name_end
, deep
, lock
)
3448 if (deep
== 0) /* nothing to do */
3451 if (lp
->ll_tv
== NULL
)
3456 /* Normal name or expanded name. */
3457 if (check_changedtick(lp
->ll_name
))
3461 di
= find_var(lp
->ll_name
, NULL
);
3467 di
->di_flags
|= DI_FLAGS_LOCK
;
3469 di
->di_flags
&= ~DI_FLAGS_LOCK
;
3470 item_lock(&di
->di_tv
, deep
, lock
);
3475 else if (lp
->ll_range
)
3477 listitem_T
*li
= lp
->ll_li
;
3479 /* (un)lock a range of List items. */
3480 while (li
!= NULL
&& (lp
->ll_empty2
|| lp
->ll_n2
>= lp
->ll_n1
))
3482 item_lock(&li
->li_tv
, deep
, lock
);
3487 else if (lp
->ll_list
!= NULL
)
3488 /* (un)lock a List item. */
3489 item_lock(&lp
->ll_li
->li_tv
, deep
, lock
);
3491 /* un(lock) a Dictionary item. */
3492 item_lock(&lp
->ll_di
->di_tv
, deep
, lock
);
3498 * Lock or unlock an item. "deep" is nr of levels to go.
3501 item_lock(tv
, deep
, lock
)
3506 static int recurse
= 0;
3513 if (recurse
>= DICT_MAXNEST
)
3515 EMSG(_("E743: variable nested too deep for (un)lock"));
3522 /* lock/unlock the item itself */
3524 tv
->v_lock
|= VAR_LOCKED
;
3526 tv
->v_lock
&= ~VAR_LOCKED
;
3531 if ((l
= tv
->vval
.v_list
) != NULL
)
3534 l
->lv_lock
|= VAR_LOCKED
;
3536 l
->lv_lock
&= ~VAR_LOCKED
;
3537 if (deep
< 0 || deep
> 1)
3538 /* recursive: lock/unlock the items the List contains */
3539 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
3540 item_lock(&li
->li_tv
, deep
- 1, lock
);
3544 if ((d
= tv
->vval
.v_dict
) != NULL
)
3547 d
->dv_lock
|= VAR_LOCKED
;
3549 d
->dv_lock
&= ~VAR_LOCKED
;
3550 if (deep
< 0 || deep
> 1)
3552 /* recursive: lock/unlock the items the List contains */
3553 todo
= (int)d
->dv_hashtab
.ht_used
;
3554 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
3556 if (!HASHITEM_EMPTY(hi
))
3559 item_lock(&HI2DI(hi
)->di_tv
, deep
- 1, lock
);
3569 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3570 * it refers to a List or Dictionary that is locked.
3576 return (tv
->v_lock
& VAR_LOCKED
)
3577 || (tv
->v_type
== VAR_LIST
3578 && tv
->vval
.v_list
!= NULL
3579 && (tv
->vval
.v_list
->lv_lock
& VAR_LOCKED
))
3580 || (tv
->v_type
== VAR_DICT
3581 && tv
->vval
.v_dict
!= NULL
3582 && (tv
->vval
.v_dict
->dv_lock
& VAR_LOCKED
));
3585 #if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3587 * Delete all "menutrans_" variables.
3590 del_menutrans_vars()
3595 hash_lock(&globvarht
);
3596 todo
= (int)globvarht
.ht_used
;
3597 for (hi
= globvarht
.ht_array
; todo
> 0 && !got_int
; ++hi
)
3599 if (!HASHITEM_EMPTY(hi
))
3602 if (STRNCMP(HI2DI(hi
)->di_key
, "menutrans_", 10) == 0)
3603 delete_var(&globvarht
, hi
);
3606 hash_unlock(&globvarht
);
3610 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3613 * Local string buffer for the next two functions to store a variable name
3614 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3615 * get_user_var_name().
3618 static char_u
*cat_prefix_varname
__ARGS((int prefix
, char_u
*name
));
3620 static char_u
*varnamebuf
= NULL
;
3621 static int varnamebuflen
= 0;
3624 * Function to concatenate a prefix and a variable name.
3627 cat_prefix_varname(prefix
, name
)
3633 len
= (int)STRLEN(name
) + 3;
3634 if (len
> varnamebuflen
)
3636 vim_free(varnamebuf
);
3637 len
+= 10; /* some additional space */
3638 varnamebuf
= alloc(len
);
3639 if (varnamebuf
== NULL
)
3644 varnamebuflen
= len
;
3646 *varnamebuf
= prefix
;
3647 varnamebuf
[1] = ':';
3648 STRCPY(varnamebuf
+ 2, name
);
3653 * Function given to ExpandGeneric() to obtain the list of user defined
3654 * (global/buffer/window/built-in) variable names.
3658 get_user_var_name(xp
, idx
)
3662 static long_u gdone
;
3663 static long_u bdone
;
3664 static long_u wdone
;
3666 static long_u tdone
;
3669 static hashitem_T
*hi
;
3674 gdone
= bdone
= wdone
= vidx
= 0;
3680 /* Global variables */
3681 if (gdone
< globvarht
.ht_used
)
3684 hi
= globvarht
.ht_array
;
3687 while (HASHITEM_EMPTY(hi
))
3689 if (STRNCMP("g:", xp
->xp_pattern
, 2) == 0)
3690 return cat_prefix_varname('g', hi
->hi_key
);
3695 ht
= &curbuf
->b_vars
.dv_hashtab
;
3696 if (bdone
< ht
->ht_used
)
3702 while (HASHITEM_EMPTY(hi
))
3704 return cat_prefix_varname('b', hi
->hi_key
);
3706 if (bdone
== ht
->ht_used
)
3709 return (char_u
*)"b:changedtick";
3713 ht
= &curwin
->w_vars
.dv_hashtab
;
3714 if (wdone
< ht
->ht_used
)
3720 while (HASHITEM_EMPTY(hi
))
3722 return cat_prefix_varname('w', hi
->hi_key
);
3727 ht
= &curtab
->tp_vars
.dv_hashtab
;
3728 if (tdone
< ht
->ht_used
)
3734 while (HASHITEM_EMPTY(hi
))
3736 return cat_prefix_varname('t', hi
->hi_key
);
3742 return cat_prefix_varname('v', (char_u
*)vimvars
[vidx
++].vv_name
);
3744 vim_free(varnamebuf
);
3750 #endif /* FEAT_CMDL_COMPL */
3753 * types for expressions.
3758 , TYPE_EQUAL
/* == */
3759 , TYPE_NEQUAL
/* != */
3760 , TYPE_GREATER
/* > */
3761 , TYPE_GEQUAL
/* >= */
3762 , TYPE_SMALLER
/* < */
3763 , TYPE_SEQUAL
/* <= */
3764 , TYPE_MATCH
/* =~ */
3765 , TYPE_NOMATCH
/* !~ */
3769 * The "evaluate" argument: When FALSE, the argument is only parsed but not
3770 * executed. The function may return OK, but the rettv will be of type
3771 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3775 * Handle zero level expression.
3776 * This calls eval1() and handles error message and nextcmd.
3777 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
3778 * Note: "rettv.v_lock" is not set.
3779 * Return OK or FAIL.
3782 eval0(arg
, rettv
, nextcmd
, evaluate
)
3792 ret
= eval1(&p
, rettv
, evaluate
);
3793 if (ret
== FAIL
|| !ends_excmd(*p
))
3798 * Report the invalid expression unless the expression evaluation has
3799 * been cancelled due to an aborting error, an interrupt, or an
3803 EMSG2(_(e_invexpr2
), arg
);
3806 if (nextcmd
!= NULL
)
3807 *nextcmd
= check_nextcmd(p
);
3813 * Handle top level expression:
3814 * expr1 ? expr0 : expr0
3816 * "arg" must point to the first non-white of the expression.
3817 * "arg" is advanced to the next non-white after the recognized expression.
3819 * Note: "rettv.v_lock" is not set.
3821 * Return OK or FAIL.
3824 eval1(arg
, rettv
, evaluate
)
3833 * Get the first variable.
3835 if (eval2(arg
, rettv
, evaluate
) == FAIL
)
3838 if ((*arg
)[0] == '?')
3845 if (get_tv_number_chk(rettv
, &error
) != 0)
3853 * Get the second variable.
3855 *arg
= skipwhite(*arg
+ 1);
3856 if (eval1(arg
, rettv
, evaluate
&& result
) == FAIL
) /* recursive! */
3860 * Check for the ":".
3862 if ((*arg
)[0] != ':')
3864 EMSG(_("E109: Missing ':' after '?'"));
3865 if (evaluate
&& result
)
3871 * Get the third variable.
3873 *arg
= skipwhite(*arg
+ 1);
3874 if (eval1(arg
, &var2
, evaluate
&& !result
) == FAIL
) /* recursive! */
3876 if (evaluate
&& result
)
3880 if (evaluate
&& !result
)
3888 * Handle first level expression:
3889 * expr2 || expr2 || expr2 logical OR
3891 * "arg" must point to the first non-white of the expression.
3892 * "arg" is advanced to the next non-white after the recognized expression.
3894 * Return OK or FAIL.
3897 eval2(arg
, rettv
, evaluate
)
3908 * Get the first variable.
3910 if (eval3(arg
, rettv
, evaluate
) == FAIL
)
3914 * Repeat until there is no following "||".
3918 while ((*arg
)[0] == '|' && (*arg
)[1] == '|')
3920 if (evaluate
&& first
)
3922 if (get_tv_number_chk(rettv
, &error
) != 0)
3931 * Get the second variable.
3933 *arg
= skipwhite(*arg
+ 2);
3934 if (eval3(arg
, &var2
, evaluate
&& !result
) == FAIL
)
3938 * Compute the result.
3940 if (evaluate
&& !result
)
3942 if (get_tv_number_chk(&var2
, &error
) != 0)
3950 rettv
->v_type
= VAR_NUMBER
;
3951 rettv
->vval
.v_number
= result
;
3959 * Handle second level expression:
3960 * expr3 && expr3 && expr3 logical AND
3962 * "arg" must point to the first non-white of the expression.
3963 * "arg" is advanced to the next non-white after the recognized expression.
3965 * Return OK or FAIL.
3968 eval3(arg
, rettv
, evaluate
)
3979 * Get the first variable.
3981 if (eval4(arg
, rettv
, evaluate
) == FAIL
)
3985 * Repeat until there is no following "&&".
3989 while ((*arg
)[0] == '&' && (*arg
)[1] == '&')
3991 if (evaluate
&& first
)
3993 if (get_tv_number_chk(rettv
, &error
) == 0)
4002 * Get the second variable.
4004 *arg
= skipwhite(*arg
+ 2);
4005 if (eval4(arg
, &var2
, evaluate
&& result
) == FAIL
)
4009 * Compute the result.
4011 if (evaluate
&& result
)
4013 if (get_tv_number_chk(&var2
, &error
) == 0)
4021 rettv
->v_type
= VAR_NUMBER
;
4022 rettv
->vval
.v_number
= result
;
4030 * Handle third level expression:
4042 * "arg" must point to the first non-white of the expression.
4043 * "arg" is advanced to the next non-white after the recognized expression.
4045 * Return OK or FAIL.
4048 eval4(arg
, rettv
, evaluate
)
4056 exptype_T type
= TYPE_UNKNOWN
;
4057 int type_is
= FALSE
; /* TRUE for "is" and "isnot" */
4061 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4062 regmatch_T regmatch
;
4067 * Get the first variable.
4069 if (eval5(arg
, rettv
, evaluate
) == FAIL
)
4075 case '=': if (p
[1] == '=')
4077 else if (p
[1] == '~')
4080 case '!': if (p
[1] == '=')
4082 else if (p
[1] == '~')
4083 type
= TYPE_NOMATCH
;
4085 case '>': if (p
[1] != '=')
4087 type
= TYPE_GREATER
;
4093 case '<': if (p
[1] != '=')
4095 type
= TYPE_SMALLER
;
4101 case 'i': if (p
[1] == 's')
4103 if (p
[2] == 'n' && p
[3] == 'o' && p
[4] == 't')
4105 if (!vim_isIDc(p
[len
]))
4107 type
= len
== 2 ? TYPE_EQUAL
: TYPE_NEQUAL
;
4115 * If there is a comparitive operator, use it.
4117 if (type
!= TYPE_UNKNOWN
)
4119 /* extra question mark appended: ignore case */
4125 /* extra '#' appended: match case */
4126 else if (p
[len
] == '#')
4131 /* nothing appened: use 'ignorecase' */
4136 * Get the second variable.
4138 *arg
= skipwhite(p
+ len
);
4139 if (eval5(arg
, &var2
, evaluate
) == FAIL
)
4147 if (type_is
&& rettv
->v_type
!= var2
.v_type
)
4149 /* For "is" a different type always means FALSE, for "notis"
4151 n1
= (type
== TYPE_NEQUAL
);
4153 else if (rettv
->v_type
== VAR_LIST
|| var2
.v_type
== VAR_LIST
)
4157 n1
= (rettv
->v_type
== var2
.v_type
4158 && rettv
->vval
.v_list
== var2
.vval
.v_list
);
4159 if (type
== TYPE_NEQUAL
)
4162 else if (rettv
->v_type
!= var2
.v_type
4163 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4165 if (rettv
->v_type
!= var2
.v_type
)
4166 EMSG(_("E691: Can only compare List with List"));
4168 EMSG(_("E692: Invalid operation for Lists"));
4175 /* Compare two Lists for being equal or unequal. */
4176 n1
= list_equal(rettv
->vval
.v_list
, var2
.vval
.v_list
, ic
);
4177 if (type
== TYPE_NEQUAL
)
4182 else if (rettv
->v_type
== VAR_DICT
|| var2
.v_type
== VAR_DICT
)
4186 n1
= (rettv
->v_type
== var2
.v_type
4187 && rettv
->vval
.v_dict
== var2
.vval
.v_dict
);
4188 if (type
== TYPE_NEQUAL
)
4191 else if (rettv
->v_type
!= var2
.v_type
4192 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4194 if (rettv
->v_type
!= var2
.v_type
)
4195 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4197 EMSG(_("E736: Invalid operation for Dictionary"));
4204 /* Compare two Dictionaries for being equal or unequal. */
4205 n1
= dict_equal(rettv
->vval
.v_dict
, var2
.vval
.v_dict
, ic
);
4206 if (type
== TYPE_NEQUAL
)
4211 else if (rettv
->v_type
== VAR_FUNC
|| var2
.v_type
== VAR_FUNC
)
4213 if (rettv
->v_type
!= var2
.v_type
4214 || (type
!= TYPE_EQUAL
&& type
!= TYPE_NEQUAL
))
4216 if (rettv
->v_type
!= var2
.v_type
)
4217 EMSG(_("E693: Can only compare Funcref with Funcref"));
4219 EMSG(_("E694: Invalid operation for Funcrefs"));
4226 /* Compare two Funcrefs for being equal or unequal. */
4227 if (rettv
->vval
.v_string
== NULL
4228 || var2
.vval
.v_string
== NULL
)
4231 n1
= STRCMP(rettv
->vval
.v_string
,
4232 var2
.vval
.v_string
) == 0;
4233 if (type
== TYPE_NEQUAL
)
4239 * If one of the two variables is a number, compare as a number.
4240 * When using "=~" or "!~", always compare as string.
4242 else if ((rettv
->v_type
== VAR_NUMBER
|| var2
.v_type
== VAR_NUMBER
)
4243 && type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4245 n1
= get_tv_number(rettv
);
4246 n2
= get_tv_number(&var2
);
4249 case TYPE_EQUAL
: n1
= (n1
== n2
); break;
4250 case TYPE_NEQUAL
: n1
= (n1
!= n2
); break;
4251 case TYPE_GREATER
: n1
= (n1
> n2
); break;
4252 case TYPE_GEQUAL
: n1
= (n1
>= n2
); break;
4253 case TYPE_SMALLER
: n1
= (n1
< n2
); break;
4254 case TYPE_SEQUAL
: n1
= (n1
<= n2
); break;
4257 case TYPE_NOMATCH
: break; /* avoid gcc warning */
4262 s1
= get_tv_string_buf(rettv
, buf1
);
4263 s2
= get_tv_string_buf(&var2
, buf2
);
4264 if (type
!= TYPE_MATCH
&& type
!= TYPE_NOMATCH
)
4265 i
= ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
);
4271 case TYPE_EQUAL
: n1
= (i
== 0); break;
4272 case TYPE_NEQUAL
: n1
= (i
!= 0); break;
4273 case TYPE_GREATER
: n1
= (i
> 0); break;
4274 case TYPE_GEQUAL
: n1
= (i
>= 0); break;
4275 case TYPE_SMALLER
: n1
= (i
< 0); break;
4276 case TYPE_SEQUAL
: n1
= (i
<= 0); break;
4280 /* avoid 'l' flag in 'cpoptions' */
4282 p_cpo
= (char_u
*)"";
4283 regmatch
.regprog
= vim_regcomp(s2
,
4284 RE_MAGIC
+ RE_STRING
);
4285 regmatch
.rm_ic
= ic
;
4286 if (regmatch
.regprog
!= NULL
)
4288 n1
= vim_regexec_nl(®match
, s1
, (colnr_T
)0);
4289 vim_free(regmatch
.regprog
);
4290 if (type
== TYPE_NOMATCH
)
4296 case TYPE_UNKNOWN
: break; /* avoid gcc warning */
4301 rettv
->v_type
= VAR_NUMBER
;
4302 rettv
->vval
.v_number
= n1
;
4310 * Handle fourth level expression:
4312 * - number subtraction
4313 * . string concatenation
4315 * "arg" must point to the first non-white of the expression.
4316 * "arg" is advanced to the next non-white after the recognized expression.
4318 * Return OK or FAIL.
4321 eval5(arg
, rettv
, evaluate
)
4331 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
4335 * Get the first variable.
4337 if (eval6(arg
, rettv
, evaluate
) == FAIL
)
4341 * Repeat computing, until no '+', '-' or '.' is following.
4346 if (op
!= '+' && op
!= '-' && op
!= '.')
4349 if (op
!= '+' || rettv
->v_type
!= VAR_LIST
)
4351 /* For "list + ...", an illegal use of the first operand as
4352 * a number cannot be determined before evaluating the 2nd
4353 * operand: if this is also a list, all is ok.
4354 * For "something . ...", "something - ..." or "non-list + ...",
4355 * we know that the first operand needs to be a string or number
4356 * without evaluating the 2nd operand. So check before to avoid
4357 * side effects after an error. */
4358 if (evaluate
&& get_tv_string_chk(rettv
) == NULL
)
4366 * Get the second variable.
4368 *arg
= skipwhite(*arg
+ 1);
4369 if (eval6(arg
, &var2
, evaluate
) == FAIL
)
4378 * Compute the result.
4382 s1
= get_tv_string_buf(rettv
, buf1
); /* already checked */
4383 s2
= get_tv_string_buf_chk(&var2
, buf2
);
4384 if (s2
== NULL
) /* type error ? */
4390 p
= concat_str(s1
, s2
);
4392 rettv
->v_type
= VAR_STRING
;
4393 rettv
->vval
.v_string
= p
;
4395 else if (op
== '+' && rettv
->v_type
== VAR_LIST
4396 && var2
.v_type
== VAR_LIST
)
4398 /* concatenate Lists */
4399 if (list_concat(rettv
->vval
.v_list
, var2
.vval
.v_list
,
4413 n1
= get_tv_number_chk(rettv
, &error
);
4416 /* This can only happen for "list + non-list".
4417 * For "non-list + ..." or "something - ...", we returned
4418 * before evaluating the 2nd operand. */
4422 n2
= get_tv_number_chk(&var2
, &error
);
4434 rettv
->v_type
= VAR_NUMBER
;
4435 rettv
->vval
.v_number
= n1
;
4444 * Handle fifth level expression:
4445 * * number multiplication
4449 * "arg" must point to the first non-white of the expression.
4450 * "arg" is advanced to the next non-white after the recognized expression.
4452 * Return OK or FAIL.
4455 eval6(arg
, rettv
, evaluate
)
4466 * Get the first variable.
4468 if (eval7(arg
, rettv
, evaluate
) == FAIL
)
4472 * Repeat computing, until no '*', '/' or '%' is following.
4477 if (op
!= '*' && op
!= '/' && op
!= '%')
4482 n1
= get_tv_number_chk(rettv
, &error
);
4491 * Get the second variable.
4493 *arg
= skipwhite(*arg
+ 1);
4494 if (eval7(arg
, &var2
, evaluate
) == FAIL
)
4499 n2
= get_tv_number_chk(&var2
, &error
);
4505 * Compute the result.
4511 if (n2
== 0) /* give an error message? */
4518 if (n2
== 0) /* give an error message? */
4523 rettv
->v_type
= VAR_NUMBER
;
4524 rettv
->vval
.v_number
= n1
;
4532 * Handle sixth level expression:
4533 * number number constant
4534 * "string" string constant
4535 * 'string' literal string constant
4536 * &option-name option value
4537 * @r register contents
4538 * identifier variable value
4539 * function() function call
4540 * $VAR environment variable
4541 * (expression) nested expression
4543 * {key: val, key: val} Dictionary
4546 * ! in front logical NOT
4547 * - in front unary minus
4548 * + in front unary plus (ignored)
4549 * trailing [] subscript in String or List
4550 * trailing .name entry in Dictionary
4552 * "arg" must point to the first non-white of the expression.
4553 * "arg" is advanced to the next non-white after the recognized expression.
4555 * Return OK or FAIL.
4558 eval7(arg
, rettv
, evaluate
)
4567 char_u
*start_leader
, *end_leader
;
4572 * Initialise variable so that clear_tv() can't mistake this for a
4573 * string and free a string that isn't there.
4575 rettv
->v_type
= VAR_UNKNOWN
;
4578 * Skip '!' and '-' characters. They are handled later.
4580 start_leader
= *arg
;
4581 while (**arg
== '!' || **arg
== '-' || **arg
== '+')
4582 *arg
= skipwhite(*arg
+ 1);
4600 vim_str2nr(*arg
, NULL
, &len
, TRUE
, TRUE
, &n
, NULL
);
4604 rettv
->v_type
= VAR_NUMBER
;
4605 rettv
->vval
.v_number
= n
;
4610 * String constant: "string".
4612 case '"': ret
= get_string_tv(arg
, rettv
, evaluate
);
4616 * Literal string constant: 'str''ing'.
4618 case '\'': ret
= get_lit_string_tv(arg
, rettv
, evaluate
);
4622 * List: [expr, expr]
4624 case '[': ret
= get_list_tv(arg
, rettv
, evaluate
);
4628 * Dictionary: {key: val, key: val}
4630 case '{': ret
= get_dict_tv(arg
, rettv
, evaluate
);
4634 * Option value: &name
4636 case '&': ret
= get_option_tv(arg
, rettv
, evaluate
);
4640 * Environment variable: $VAR.
4642 case '$': ret
= get_env_tv(arg
, rettv
, evaluate
);
4646 * Register contents: @r.
4651 rettv
->v_type
= VAR_STRING
;
4652 rettv
->vval
.v_string
= get_reg_contents(**arg
, TRUE
, TRUE
);
4659 * nested expression: (expression).
4661 case '(': *arg
= skipwhite(*arg
+ 1);
4662 ret
= eval1(arg
, rettv
, evaluate
); /* recursive! */
4667 EMSG(_("E110: Missing ')'"));
4673 default: ret
= NOTDONE
;
4680 * Must be a variable or function name.
4681 * Can also be a curly-braces kind of name: {expr}.
4684 len
= get_name_len(arg
, &alias
, evaluate
, TRUE
);
4692 if (**arg
== '(') /* recursive! */
4694 /* If "s" is the name of a variable of type VAR_FUNC
4695 * use its contents. */
4696 s
= deref_func_name(s
, &len
);
4698 /* Invoke the function. */
4699 ret
= get_func_tv(s
, len
, rettv
, arg
,
4700 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
4701 &len
, evaluate
, NULL
);
4702 /* Stop the expression evaluation when immediately
4703 * aborting on error, or when an interrupt occurred or
4704 * an exception was thrown but not caught. */
4713 ret
= get_var_tv(s
, len
, rettv
, TRUE
);
4722 *arg
= skipwhite(*arg
);
4724 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4727 ret
= handle_subscript(arg
, rettv
, evaluate
, TRUE
);
4730 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4732 if (ret
== OK
&& evaluate
&& end_leader
> start_leader
)
4736 val
= get_tv_number_chk(rettv
, &error
);
4744 while (end_leader
> start_leader
)
4747 if (*end_leader
== '!')
4749 else if (*end_leader
== '-')
4753 rettv
->v_type
= VAR_NUMBER
;
4754 rettv
->vval
.v_number
= val
;
4762 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4763 * "*arg" points to the '[' or '.'.
4764 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4767 eval_index(arg
, rettv
, evaluate
, verbose
)
4771 int verbose
; /* give error messages */
4773 int empty1
= FALSE
, empty2
= FALSE
;
4774 typval_T var1
, var2
;
4781 if (rettv
->v_type
== VAR_FUNC
)
4784 EMSG(_("E695: Cannot index a Funcref"));
4794 for (len
= 0; ASCII_ISALNUM(key
[len
]) || key
[len
] == '_'; ++len
)
4798 *arg
= skipwhite(key
+ len
);
4805 * Get the (first) variable from inside the [].
4807 *arg
= skipwhite(*arg
+ 1);
4810 else if (eval1(arg
, &var1
, evaluate
) == FAIL
) /* recursive! */
4812 else if (evaluate
&& get_tv_string_chk(&var1
) == NULL
)
4814 /* not a number or string */
4820 * Get the second variable from inside the [:].
4825 *arg
= skipwhite(*arg
+ 1);
4828 else if (eval1(arg
, &var2
, evaluate
) == FAIL
) /* recursive! */
4834 else if (evaluate
&& get_tv_string_chk(&var2
) == NULL
)
4836 /* not a number or string */
4844 /* Check for the ']'. */
4848 EMSG(_(e_missbrac
));
4854 *arg
= skipwhite(*arg
+ 1); /* skip the ']' */
4860 if (!empty1
&& rettv
->v_type
!= VAR_DICT
)
4862 n1
= get_tv_number(&var1
);
4871 n2
= get_tv_number(&var2
);
4876 switch (rettv
->v_type
)
4880 s
= get_tv_string(rettv
);
4881 len
= (long)STRLEN(s
);
4884 /* The resulting variable is a substring. If the indexes
4885 * are out of range the result is empty. */
4896 if (n1
>= len
|| n2
< 0 || n1
> n2
)
4899 s
= vim_strnsave(s
+ n1
, (int)(n2
- n1
+ 1));
4903 /* The resulting variable is a string of a single
4904 * character. If the index is too big or negative the
4905 * result is empty. */
4906 if (n1
>= len
|| n1
< 0)
4909 s
= vim_strnsave(s
+ n1
, 1);
4912 rettv
->v_type
= VAR_STRING
;
4913 rettv
->vval
.v_string
= s
;
4917 len
= list_len(rettv
->vval
.v_list
);
4920 if (!empty1
&& (n1
< 0 || n1
>= len
))
4922 /* For a range we allow invalid values and return an empty
4923 * list. A list index out of range is an error. */
4927 EMSGN(_(e_listidx
), n1
);
4941 if (!empty2
&& (n2
< 0 || n2
+ 1 < n1
))
4946 for (item
= list_find(rettv
->vval
.v_list
, n1
);
4949 if (list_append_tv(l
, &item
->li_tv
) == FAIL
)
4954 item
= item
->li_next
;
4957 rettv
->v_type
= VAR_LIST
;
4958 rettv
->vval
.v_list
= l
;
4963 copy_tv(&list_find(rettv
->vval
.v_list
, n1
)->li_tv
, &var1
);
4973 EMSG(_(e_dictrange
));
4983 key
= get_tv_string(&var1
);
4987 EMSG(_(e_emptykey
));
4993 item
= dict_find(rettv
->vval
.v_dict
, key
, (int)len
);
4995 if (item
== NULL
&& verbose
)
4996 EMSG2(_(e_dictkey
), key
);
5002 copy_tv(&item
->di_tv
, &var1
);
5014 * Get an option value.
5015 * "arg" points to the '&' or '+' before the option name.
5016 * "arg" is advanced to character after the option name.
5017 * Return OK or FAIL.
5020 get_option_tv(arg
, rettv
, evaluate
)
5022 typval_T
*rettv
; /* when NULL, only check if option exists */
5030 int working
= (**arg
== '+'); /* has("+option") */
5035 * Isolate the option name and find its value.
5037 option_end
= find_option_end(arg
, &opt_flags
);
5038 if (option_end
== NULL
)
5041 EMSG2(_("E112: Option name missing: %s"), *arg
);
5053 opt_type
= get_option_value(*arg
, &numval
,
5054 rettv
== NULL
? NULL
: &stringval
, opt_flags
);
5056 if (opt_type
== -3) /* invalid name */
5059 EMSG2(_("E113: Unknown option: %s"), *arg
);
5062 else if (rettv
!= NULL
)
5064 if (opt_type
== -2) /* hidden string option */
5066 rettv
->v_type
= VAR_STRING
;
5067 rettv
->vval
.v_string
= NULL
;
5069 else if (opt_type
== -1) /* hidden number option */
5071 rettv
->v_type
= VAR_NUMBER
;
5072 rettv
->vval
.v_number
= 0;
5074 else if (opt_type
== 1) /* number option */
5076 rettv
->v_type
= VAR_NUMBER
;
5077 rettv
->vval
.v_number
= numval
;
5079 else /* string option */
5081 rettv
->v_type
= VAR_STRING
;
5082 rettv
->vval
.v_string
= stringval
;
5085 else if (working
&& (opt_type
== -2 || opt_type
== -1))
5088 *option_end
= c
; /* put back for error messages */
5095 * Allocate a variable for a string constant.
5096 * Return OK or FAIL.
5099 get_string_tv(arg
, rettv
, evaluate
)
5109 * Find the end of the string, skipping backslashed characters.
5111 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
5113 if (*p
== '\\' && p
[1] != NUL
)
5116 /* A "\<x>" form occupies at least 4 characters, and produces up
5117 * to 6 characters: reserve space for 2 extra */
5125 EMSG2(_("E114: Missing quote: %s"), *arg
);
5129 /* If only parsing, set *arg and return here */
5137 * Copy the string into allocated memory, handling backslashed
5140 name
= alloc((unsigned)(p
- *arg
+ extra
));
5143 rettv
->v_type
= VAR_STRING
;
5144 rettv
->vval
.v_string
= name
;
5146 for (p
= *arg
+ 1; *p
!= NUL
&& *p
!= '"'; )
5152 case 'b': *name
++ = BS
; ++p
; break;
5153 case 'e': *name
++ = ESC
; ++p
; break;
5154 case 'f': *name
++ = FF
; ++p
; break;
5155 case 'n': *name
++ = NL
; ++p
; break;
5156 case 'r': *name
++ = CAR
; ++p
; break;
5157 case 't': *name
++ = TAB
; ++p
; break;
5159 case 'X': /* hex: "\x1", "\x12" */
5161 case 'u': /* Unicode: "\u0023" */
5163 if (vim_isxdigit(p
[1]))
5166 int c
= toupper(*p
);
5173 while (--n
>= 0 && vim_isxdigit(p
[1]))
5176 nr
= (nr
<< 4) + hex2nr(*p
);
5180 /* For "\u" store the number according to
5183 name
+= (*mb_char2bytes
)(nr
, name
);
5190 /* octal: "\1", "\12", "\123" */
5198 case '7': *name
= *p
++ - '0';
5199 if (*p
>= '0' && *p
<= '7')
5201 *name
= (*name
<< 3) + *p
++ - '0';
5202 if (*p
>= '0' && *p
<= '7')
5203 *name
= (*name
<< 3) + *p
++ - '0';
5208 /* Special key, e.g.: "\<C-W>" */
5209 case '<': extra
= trans_special(&p
, name
, TRUE
);
5217 default: MB_COPY_CHAR(p
, name
);
5222 MB_COPY_CHAR(p
, name
);
5232 * Allocate a variable for a 'str''ing' constant.
5233 * Return OK or FAIL.
5236 get_lit_string_tv(arg
, rettv
, evaluate
)
5246 * Find the end of the string, skipping ''.
5248 for (p
= *arg
+ 1; *p
!= NUL
; mb_ptr_adv(p
))
5261 EMSG2(_("E115: Missing quote: %s"), *arg
);
5265 /* If only parsing return after setting "*arg" */
5273 * Copy the string into allocated memory, handling '' to ' reduction.
5275 str
= alloc((unsigned)((p
- *arg
) - reduce
));
5278 rettv
->v_type
= VAR_STRING
;
5279 rettv
->vval
.v_string
= str
;
5281 for (p
= *arg
+ 1; *p
!= NUL
; )
5289 MB_COPY_CHAR(p
, str
);
5298 * Allocate a variable for a List and fill it from "*arg".
5299 * Return OK or FAIL.
5302 get_list_tv(arg
, rettv
, evaluate
)
5318 *arg
= skipwhite(*arg
+ 1);
5319 while (**arg
!= ']' && **arg
!= NUL
)
5321 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
5325 item
= listitem_alloc();
5329 item
->li_tv
.v_lock
= 0;
5330 list_append(l
, item
);
5340 EMSG2(_("E696: Missing comma in List: %s"), *arg
);
5343 *arg
= skipwhite(*arg
+ 1);
5348 EMSG2(_("E697: Missing end of List ']': %s"), *arg
);
5355 *arg
= skipwhite(*arg
+ 1);
5358 rettv
->v_type
= VAR_LIST
;
5359 rettv
->vval
.v_list
= l
;
5367 * Allocate an empty header for a list.
5368 * Caller should take care of the reference count.
5375 l
= (list_T
*)alloc_clear(sizeof(list_T
));
5378 /* Prepend the list to the list of lists for garbage collection. */
5379 if (first_list
!= NULL
)
5380 first_list
->lv_used_prev
= l
;
5381 l
->lv_used_prev
= NULL
;
5382 l
->lv_used_next
= first_list
;
5389 * Allocate an empty list for a return value.
5390 * Returns OK or FAIL.
5393 rettv_list_alloc(rettv
)
5396 list_T
*l
= list_alloc();
5401 rettv
->vval
.v_list
= l
;
5402 rettv
->v_type
= VAR_LIST
;
5408 * Unreference a list: decrement the reference count and free it when it
5415 if (l
!= NULL
&& --l
->lv_refcount
<= 0)
5420 * Free a list, including all items it points to.
5421 * Ignores the reference count.
5424 list_free(l
, recurse
)
5426 int recurse
; /* Free Lists and Dictionaries recursively. */
5430 /* Remove the list from the list of lists for garbage collection. */
5431 if (l
->lv_used_prev
== NULL
)
5432 first_list
= l
->lv_used_next
;
5434 l
->lv_used_prev
->lv_used_next
= l
->lv_used_next
;
5435 if (l
->lv_used_next
!= NULL
)
5436 l
->lv_used_next
->lv_used_prev
= l
->lv_used_prev
;
5438 for (item
= l
->lv_first
; item
!= NULL
; item
= l
->lv_first
)
5440 /* Remove the item before deleting it. */
5441 l
->lv_first
= item
->li_next
;
5442 if (recurse
|| (item
->li_tv
.v_type
!= VAR_LIST
5443 && item
->li_tv
.v_type
!= VAR_DICT
))
5444 clear_tv(&item
->li_tv
);
5451 * Allocate a list item.
5456 return (listitem_T
*)alloc(sizeof(listitem_T
));
5460 * Free a list item. Also clears the value. Does not notify watchers.
5466 clear_tv(&item
->li_tv
);
5471 * Remove a list item from a List and free it. Also clears the value.
5474 listitem_remove(l
, item
)
5478 list_remove(l
, item
, item
);
5479 listitem_free(item
);
5483 * Get the number of items in a list.
5495 * Return TRUE when two lists have exactly the same values.
5498 list_equal(l1
, l2
, ic
)
5501 int ic
; /* ignore case for strings */
5503 listitem_T
*item1
, *item2
;
5507 if (list_len(l1
) != list_len(l2
))
5510 for (item1
= l1
->lv_first
, item2
= l2
->lv_first
;
5511 item1
!= NULL
&& item2
!= NULL
;
5512 item1
= item1
->li_next
, item2
= item2
->li_next
)
5513 if (!tv_equal(&item1
->li_tv
, &item2
->li_tv
, ic
))
5515 return item1
== NULL
&& item2
== NULL
;
5518 #if defined(FEAT_PYTHON) || defined(PROTO)
5520 * Return the dictitem that an entry in a hashtable points to.
5531 * Return TRUE when two dictionaries have exactly the same key/values.
5534 dict_equal(d1
, d2
, ic
)
5537 int ic
; /* ignore case for strings */
5545 if (dict_len(d1
) != dict_len(d2
))
5548 todo
= (int)d1
->dv_hashtab
.ht_used
;
5549 for (hi
= d1
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
5551 if (!HASHITEM_EMPTY(hi
))
5553 item2
= dict_find(d2
, hi
->hi_key
, -1);
5556 if (!tv_equal(&HI2DI(hi
)->di_tv
, &item2
->di_tv
, ic
))
5565 * Return TRUE if "tv1" and "tv2" have the same value.
5566 * Compares the items just like "==" would compare them, but strings and
5567 * numbers are different.
5570 tv_equal(tv1
, tv2
, ic
)
5573 int ic
; /* ignore case */
5575 char_u buf1
[NUMBUFLEN
], buf2
[NUMBUFLEN
];
5577 static int recursive
= 0; /* cach recursive loops */
5580 if (tv1
->v_type
!= tv2
->v_type
)
5582 /* Catch lists and dicts that have an endless loop by limiting
5583 * recursiveness to 1000. We guess they are equal then. */
5584 if (recursive
>= 1000)
5587 switch (tv1
->v_type
)
5591 r
= list_equal(tv1
->vval
.v_list
, tv2
->vval
.v_list
, ic
);
5597 r
= dict_equal(tv1
->vval
.v_dict
, tv2
->vval
.v_dict
, ic
);
5602 return (tv1
->vval
.v_string
!= NULL
5603 && tv2
->vval
.v_string
!= NULL
5604 && STRCMP(tv1
->vval
.v_string
, tv2
->vval
.v_string
) == 0);
5607 return tv1
->vval
.v_number
== tv2
->vval
.v_number
;
5610 s1
= get_tv_string_buf(tv1
, buf1
);
5611 s2
= get_tv_string_buf(tv2
, buf2
);
5612 return ((ic
? MB_STRICMP(s1
, s2
) : STRCMP(s1
, s2
)) == 0);
5615 EMSG2(_(e_intern2
), "tv_equal()");
5620 * Locate item with index "n" in list "l" and return it.
5621 * A negative index is counted from the end; -1 is the last item.
5622 * Returns NULL when "n" is out of range.
5635 /* Negative index is relative to the end. */
5639 /* Check for index out of range. */
5640 if (n
< 0 || n
>= l
->lv_len
)
5643 /* When there is a cached index may start search from there. */
5644 if (l
->lv_idx_item
!= NULL
)
5646 if (n
< l
->lv_idx
/ 2)
5648 /* closest to the start of the list */
5652 else if (n
> (l
->lv_idx
+ l
->lv_len
) / 2)
5654 /* closest to the end of the list */
5656 idx
= l
->lv_len
- 1;
5660 /* closest to the cached index */
5661 item
= l
->lv_idx_item
;
5667 if (n
< l
->lv_len
/ 2)
5669 /* closest to the start of the list */
5675 /* closest to the end of the list */
5677 idx
= l
->lv_len
- 1;
5683 /* search forward */
5684 item
= item
->li_next
;
5689 /* search backward */
5690 item
= item
->li_prev
;
5694 /* cache the used index */
5696 l
->lv_idx_item
= item
;
5702 * Get list item "l[idx]" as a number.
5705 list_find_nr(l
, idx
, errorp
)
5708 int *errorp
; /* set to TRUE when something wrong */
5712 li
= list_find(l
, idx
);
5719 return get_tv_number_chk(&li
->li_tv
, errorp
);
5723 * Locate "item" list "l" and return its index.
5724 * Returns -1 when "item" is not in the list.
5727 list_idx_of_item(l
, item
)
5737 for (li
= l
->lv_first
; li
!= NULL
&& li
!= item
; li
= li
->li_next
)
5745 * Append item "item" to the end of list "l".
5748 list_append(l
, item
)
5752 if (l
->lv_last
== NULL
)
5757 item
->li_prev
= NULL
;
5761 l
->lv_last
->li_next
= item
;
5762 item
->li_prev
= l
->lv_last
;
5766 item
->li_next
= NULL
;
5770 * Append typval_T "tv" to the end of list "l".
5771 * Return FAIL when out of memory.
5774 list_append_tv(l
, tv
)
5778 listitem_T
*li
= listitem_alloc();
5782 copy_tv(tv
, &li
->li_tv
);
5788 * Add a dictionary to a list. Used by getqflist().
5789 * Return FAIL when out of memory.
5792 list_append_dict(list
, dict
)
5796 listitem_T
*li
= listitem_alloc();
5800 li
->li_tv
.v_type
= VAR_DICT
;
5801 li
->li_tv
.v_lock
= 0;
5802 li
->li_tv
.vval
.v_dict
= dict
;
5803 list_append(list
, li
);
5804 ++dict
->dv_refcount
;
5809 * Make a copy of "str" and append it as an item to list "l".
5810 * When "len" >= 0 use "str[len]".
5811 * Returns FAIL when out of memory.
5814 list_append_string(l
, str
, len
)
5819 listitem_T
*li
= listitem_alloc();
5824 li
->li_tv
.v_type
= VAR_STRING
;
5825 li
->li_tv
.v_lock
= 0;
5827 li
->li_tv
.vval
.v_string
= NULL
;
5828 else if ((li
->li_tv
.vval
.v_string
= (len
>= 0 ? vim_strnsave(str
, len
)
5829 : vim_strsave(str
))) == NULL
)
5835 * Append "n" to list "l".
5836 * Returns FAIL when out of memory.
5839 list_append_number(l
, n
)
5845 li
= listitem_alloc();
5848 li
->li_tv
.v_type
= VAR_NUMBER
;
5849 li
->li_tv
.v_lock
= 0;
5850 li
->li_tv
.vval
.v_number
= n
;
5856 * Insert typval_T "tv" in list "l" before "item".
5857 * If "item" is NULL append at the end.
5858 * Return FAIL when out of memory.
5861 list_insert_tv(l
, tv
, item
)
5866 listitem_T
*ni
= listitem_alloc();
5870 copy_tv(tv
, &ni
->li_tv
);
5872 /* Append new item at end of list. */
5876 /* Insert new item before existing item. */
5877 ni
->li_prev
= item
->li_prev
;
5879 if (item
->li_prev
== NULL
)
5886 item
->li_prev
->li_next
= ni
;
5887 l
->lv_idx_item
= NULL
;
5896 * Extend "l1" with "l2".
5897 * If "bef" is NULL append at the end, otherwise insert before this item.
5898 * Returns FAIL when out of memory.
5901 list_extend(l1
, l2
, bef
)
5908 for (item
= l2
->lv_first
; item
!= NULL
; item
= item
->li_next
)
5909 if (list_insert_tv(l1
, &item
->li_tv
, bef
) == FAIL
)
5915 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5916 * Return FAIL when out of memory.
5919 list_concat(l1
, l2
, tv
)
5926 /* make a copy of the first list. */
5927 l
= list_copy(l1
, FALSE
, 0);
5930 tv
->v_type
= VAR_LIST
;
5931 tv
->vval
.v_list
= l
;
5933 /* append all items from the second list */
5934 return list_extend(l
, l2
, NULL
);
5938 * Make a copy of list "orig". Shallow if "deep" is FALSE.
5939 * The refcount of the new list is set to 1.
5940 * See item_copy() for "copyID".
5941 * Returns NULL when out of memory.
5944 list_copy(orig
, deep
, copyID
)
5956 copy
= list_alloc();
5961 /* Do this before adding the items, because one of the items may
5962 * refer back to this list. */
5963 orig
->lv_copyID
= copyID
;
5964 orig
->lv_copylist
= copy
;
5966 for (item
= orig
->lv_first
; item
!= NULL
&& !got_int
;
5967 item
= item
->li_next
)
5969 ni
= listitem_alloc();
5974 if (item_copy(&item
->li_tv
, &ni
->li_tv
, deep
, copyID
) == FAIL
)
5981 copy_tv(&item
->li_tv
, &ni
->li_tv
);
5982 list_append(copy
, ni
);
5984 ++copy
->lv_refcount
;
5996 * Remove items "item" to "item2" from list "l".
5997 * Does not free the listitem or the value!
6000 list_remove(l
, item
, item2
)
6007 /* notify watchers */
6008 for (ip
= item
; ip
!= NULL
; ip
= ip
->li_next
)
6011 list_fix_watch(l
, ip
);
6016 if (item2
->li_next
== NULL
)
6017 l
->lv_last
= item
->li_prev
;
6019 item2
->li_next
->li_prev
= item
->li_prev
;
6020 if (item
->li_prev
== NULL
)
6021 l
->lv_first
= item2
->li_next
;
6023 item
->li_prev
->li_next
= item2
->li_next
;
6024 l
->lv_idx_item
= NULL
;
6028 * Return an allocated string with the string representation of a list.
6032 list2string(tv
, copyID
)
6038 if (tv
->vval
.v_list
== NULL
)
6040 ga_init2(&ga
, (int)sizeof(char), 80);
6041 ga_append(&ga
, '[');
6042 if (list_join(&ga
, tv
->vval
.v_list
, (char_u
*)", ", FALSE
, copyID
) == FAIL
)
6044 vim_free(ga
.ga_data
);
6047 ga_append(&ga
, ']');
6048 ga_append(&ga
, NUL
);
6049 return (char_u
*)ga
.ga_data
;
6053 * Join list "l" into a string in "*gap", using separator "sep".
6054 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
6055 * Return FAIL or OK.
6058 list_join(gap
, l
, sep
, echo
, copyID
)
6067 char_u numbuf
[NUMBUFLEN
];
6071 for (item
= l
->lv_first
; item
!= NULL
&& !got_int
; item
= item
->li_next
)
6076 ga_concat(gap
, sep
);
6079 s
= echo_string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6081 s
= tv2string(&item
->li_tv
, &tofree
, numbuf
, copyID
);
6092 * Garbage collection for lists and dictionaries.
6094 * We use reference counts to be able to free most items right away when they
6095 * are no longer used. But for composite items it's possible that it becomes
6096 * unused while the reference count is > 0: When there is a recursive
6097 * reference. Example:
6098 * :let l = [1, 2, 3]
6102 * Since this is quite unusual we handle this with garbage collection: every
6103 * once in a while find out which lists and dicts are not referenced from any
6106 * Here is a good reference text about garbage collection (refers to Python
6107 * but it applies to all reference-counting mechanisms):
6108 * http://python.ca/nas/python/gc/
6112 * Do garbage collection for lists and dicts.
6113 * Return TRUE if some memory was freed.
6120 int copyID
= ++current_copyID
;
6125 int did_free
= FALSE
;
6130 /* Only do this once. */
6131 want_garbage_collect
= FALSE
;
6132 may_garbage_collect
= FALSE
;
6135 * 1. Go through all accessible variables and mark all lists and dicts
6138 /* script-local variables */
6139 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
6140 set_ref_in_ht(&SCRIPT_VARS(i
), copyID
);
6142 /* buffer-local variables */
6143 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
6144 set_ref_in_ht(&buf
->b_vars
.dv_hashtab
, copyID
);
6146 /* window-local variables */
6147 FOR_ALL_TAB_WINDOWS(tp
, wp
)
6148 set_ref_in_ht(&wp
->w_vars
.dv_hashtab
, copyID
);
6151 /* tabpage-local variables */
6152 for (tp
= first_tabpage
; tp
!= NULL
; tp
= tp
->tp_next
)
6153 set_ref_in_ht(&tp
->tp_vars
.dv_hashtab
, copyID
);
6156 /* global variables */
6157 set_ref_in_ht(&globvarht
, copyID
);
6159 /* function-local variables */
6160 for (fc
= current_funccal
; fc
!= NULL
; fc
= fc
->caller
)
6162 set_ref_in_ht(&fc
->l_vars
.dv_hashtab
, copyID
);
6163 set_ref_in_ht(&fc
->l_avars
.dv_hashtab
, copyID
);
6167 * 2. Go through the list of dicts and free items without the copyID.
6169 for (dd
= first_dict
; dd
!= NULL
; )
6170 if (dd
->dv_copyID
!= copyID
)
6172 /* Free the Dictionary and ordinary items it contains, but don't
6173 * recurse into Lists and Dictionaries, they will be in the list
6174 * of dicts or list of lists. */
6175 dict_free(dd
, FALSE
);
6178 /* restart, next dict may also have been freed */
6182 dd
= dd
->dv_used_next
;
6185 * 3. Go through the list of lists and free items without the copyID.
6186 * But don't free a list that has a watcher (used in a for loop), these
6187 * are not referenced anywhere.
6189 for (ll
= first_list
; ll
!= NULL
; )
6190 if (ll
->lv_copyID
!= copyID
&& ll
->lv_watch
== NULL
)
6192 /* Free the List and ordinary items it contains, but don't recurse
6193 * into Lists and Dictionaries, they will be in the list of dicts
6194 * or list of lists. */
6195 list_free(ll
, FALSE
);
6198 /* restart, next list may also have been freed */
6202 ll
= ll
->lv_used_next
;
6208 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6211 set_ref_in_ht(ht
, copyID
)
6218 todo
= (int)ht
->ht_used
;
6219 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
6220 if (!HASHITEM_EMPTY(hi
))
6223 set_ref_in_item(&HI2DI(hi
)->di_tv
, copyID
);
6228 * Mark all lists and dicts referenced through list "l" with "copyID".
6231 set_ref_in_list(l
, copyID
)
6237 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
6238 set_ref_in_item(&li
->li_tv
, copyID
);
6242 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6245 set_ref_in_item(tv
, copyID
)
6255 dd
= tv
->vval
.v_dict
;
6256 if (dd
->dv_copyID
!= copyID
)
6258 /* Didn't see this dict yet. */
6259 dd
->dv_copyID
= copyID
;
6260 set_ref_in_ht(&dd
->dv_hashtab
, copyID
);
6265 ll
= tv
->vval
.v_list
;
6266 if (ll
->lv_copyID
!= copyID
)
6268 /* Didn't see this list yet. */
6269 ll
->lv_copyID
= copyID
;
6270 set_ref_in_list(ll
, copyID
);
6278 * Allocate an empty header for a dictionary.
6285 d
= (dict_T
*)alloc(sizeof(dict_T
));
6288 /* Add the list to the list of dicts for garbage collection. */
6289 if (first_dict
!= NULL
)
6290 first_dict
->dv_used_prev
= d
;
6291 d
->dv_used_next
= first_dict
;
6292 d
->dv_used_prev
= NULL
;
6295 hash_init(&d
->dv_hashtab
);
6304 * Unreference a Dictionary: decrement the reference count and free it when it
6311 if (d
!= NULL
&& --d
->dv_refcount
<= 0)
6316 * Free a Dictionary, including all items it contains.
6317 * Ignores the reference count.
6320 dict_free(d
, recurse
)
6322 int recurse
; /* Free Lists and Dictionaries recursively. */
6328 /* Remove the dict from the list of dicts for garbage collection. */
6329 if (d
->dv_used_prev
== NULL
)
6330 first_dict
= d
->dv_used_next
;
6332 d
->dv_used_prev
->dv_used_next
= d
->dv_used_next
;
6333 if (d
->dv_used_next
!= NULL
)
6334 d
->dv_used_next
->dv_used_prev
= d
->dv_used_prev
;
6336 /* Lock the hashtab, we don't want it to resize while freeing items. */
6337 hash_lock(&d
->dv_hashtab
);
6338 todo
= (int)d
->dv_hashtab
.ht_used
;
6339 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
6341 if (!HASHITEM_EMPTY(hi
))
6343 /* Remove the item before deleting it, just in case there is
6344 * something recursive causing trouble. */
6346 hash_remove(&d
->dv_hashtab
, hi
);
6347 if (recurse
|| (di
->di_tv
.v_type
!= VAR_LIST
6348 && di
->di_tv
.v_type
!= VAR_DICT
))
6349 clear_tv(&di
->di_tv
);
6354 hash_clear(&d
->dv_hashtab
);
6359 * Allocate a Dictionary item.
6360 * The "key" is copied to the new item.
6361 * Note that the value of the item "di_tv" still needs to be initialized!
6362 * Returns NULL when out of memory.
6370 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
) + STRLEN(key
)));
6373 STRCPY(di
->di_key
, key
);
6380 * Make a copy of a Dictionary item.
6388 di
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
6389 + STRLEN(org
->di_key
)));
6392 STRCPY(di
->di_key
, org
->di_key
);
6394 copy_tv(&org
->di_tv
, &di
->di_tv
);
6400 * Remove item "item" from Dictionary "dict" and free it.
6403 dictitem_remove(dict
, item
)
6409 hi
= hash_find(&dict
->dv_hashtab
, item
->di_key
);
6410 if (HASHITEM_EMPTY(hi
))
6411 EMSG2(_(e_intern2
), "dictitem_remove()");
6413 hash_remove(&dict
->dv_hashtab
, hi
);
6414 dictitem_free(item
);
6418 * Free a dict item. Also clears the value.
6424 clear_tv(&item
->di_tv
);
6429 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6430 * The refcount of the new dict is set to 1.
6431 * See item_copy() for "copyID".
6432 * Returns NULL when out of memory.
6435 dict_copy(orig
, deep
, copyID
)
6448 copy
= dict_alloc();
6453 orig
->dv_copyID
= copyID
;
6454 orig
->dv_copydict
= copy
;
6456 todo
= (int)orig
->dv_hashtab
.ht_used
;
6457 for (hi
= orig
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
6459 if (!HASHITEM_EMPTY(hi
))
6463 di
= dictitem_alloc(hi
->hi_key
);
6468 if (item_copy(&HI2DI(hi
)->di_tv
, &di
->di_tv
, deep
,
6476 copy_tv(&HI2DI(hi
)->di_tv
, &di
->di_tv
);
6477 if (dict_add(copy
, di
) == FAIL
)
6485 ++copy
->dv_refcount
;
6497 * Add item "item" to Dictionary "d".
6498 * Returns FAIL when out of memory and when key already existed.
6505 return hash_add(&d
->dv_hashtab
, item
->di_key
);
6509 * Add a number or string entry to dictionary "d".
6510 * When "str" is NULL use number "nr", otherwise use "str".
6511 * Returns FAIL when out of memory and when key already exists.
6514 dict_add_nr_str(d
, key
, nr
, str
)
6522 item
= dictitem_alloc((char_u
*)key
);
6525 item
->di_tv
.v_lock
= 0;
6528 item
->di_tv
.v_type
= VAR_NUMBER
;
6529 item
->di_tv
.vval
.v_number
= nr
;
6533 item
->di_tv
.v_type
= VAR_STRING
;
6534 item
->di_tv
.vval
.v_string
= vim_strsave(str
);
6536 if (dict_add(d
, item
) == FAIL
)
6538 dictitem_free(item
);
6545 * Get the number of items in a Dictionary.
6553 return (long)d
->dv_hashtab
.ht_used
;
6557 * Find item "key[len]" in Dictionary "d".
6558 * If "len" is negative use strlen(key).
6559 * Returns NULL when not found.
6562 dict_find(d
, key
, len
)
6568 char_u buf
[AKEYLEN
];
6570 char_u
*tofree
= NULL
;
6575 else if (len
>= AKEYLEN
)
6577 tofree
= akey
= vim_strnsave(key
, len
);
6583 /* Avoid a malloc/free by using buf[]. */
6584 vim_strncpy(buf
, key
, len
);
6588 hi
= hash_find(&d
->dv_hashtab
, akey
);
6590 if (HASHITEM_EMPTY(hi
))
6596 * Get a string item from a dictionary.
6597 * When "save" is TRUE allocate memory for it.
6598 * Returns NULL if the entry doesn't exist or out of memory.
6601 get_dict_string(d
, key
, save
)
6609 di
= dict_find(d
, key
, -1);
6612 s
= get_tv_string(&di
->di_tv
);
6613 if (save
&& s
!= NULL
)
6619 * Get a number item from a dictionary.
6620 * Returns 0 if the entry doesn't exist or out of memory.
6623 get_dict_number(d
, key
)
6629 di
= dict_find(d
, key
, -1);
6632 return get_tv_number(&di
->di_tv
);
6636 * Return an allocated string with the string representation of a Dictionary.
6640 dict2string(tv
, copyID
)
6647 char_u numbuf
[NUMBUFLEN
];
6653 if ((d
= tv
->vval
.v_dict
) == NULL
)
6655 ga_init2(&ga
, (int)sizeof(char), 80);
6656 ga_append(&ga
, '{');
6658 todo
= (int)d
->dv_hashtab
.ht_used
;
6659 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
6661 if (!HASHITEM_EMPTY(hi
))
6668 ga_concat(&ga
, (char_u
*)", ");
6670 tofree
= string_quote(hi
->hi_key
, FALSE
);
6673 ga_concat(&ga
, tofree
);
6676 ga_concat(&ga
, (char_u
*)": ");
6677 s
= tv2string(&HI2DI(hi
)->di_tv
, &tofree
, numbuf
, copyID
);
6687 vim_free(ga
.ga_data
);
6691 ga_append(&ga
, '}');
6692 ga_append(&ga
, NUL
);
6693 return (char_u
*)ga
.ga_data
;
6697 * Allocate a variable for a Dictionary and fill it from "*arg".
6698 * Return OK or FAIL. Returns NOTDONE for {expr}.
6701 get_dict_tv(arg
, rettv
, evaluate
)
6711 char_u
*start
= skipwhite(*arg
+ 1);
6712 char_u buf
[NUMBUFLEN
];
6715 * First check if it's not a curly-braces thing: {expr}.
6716 * Must do this without evaluating, otherwise a function may be called
6717 * twice. Unfortunately this means we need to call eval1() twice for the
6719 * But {} is an empty Dictionary.
6723 if (eval1(&start
, &tv
, FALSE
) == FAIL
) /* recursive! */
6735 tvkey
.v_type
= VAR_UNKNOWN
;
6736 tv
.v_type
= VAR_UNKNOWN
;
6738 *arg
= skipwhite(*arg
+ 1);
6739 while (**arg
!= '}' && **arg
!= NUL
)
6741 if (eval1(arg
, &tvkey
, evaluate
) == FAIL
) /* recursive! */
6745 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg
);
6749 key
= get_tv_string_buf_chk(&tvkey
, buf
);
6750 if (key
== NULL
|| *key
== NUL
)
6752 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6754 EMSG(_(e_emptykey
));
6759 *arg
= skipwhite(*arg
+ 1);
6760 if (eval1(arg
, &tv
, evaluate
) == FAIL
) /* recursive! */
6767 item
= dict_find(d
, key
, -1);
6770 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key
);
6775 item
= dictitem_alloc(key
);
6780 item
->di_tv
.v_lock
= 0;
6781 if (dict_add(d
, item
) == FAIL
)
6782 dictitem_free(item
);
6790 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg
);
6793 *arg
= skipwhite(*arg
+ 1);
6798 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg
);
6805 *arg
= skipwhite(*arg
+ 1);
6808 rettv
->v_type
= VAR_DICT
;
6809 rettv
->vval
.v_dict
= d
;
6817 * Return a string with the string representation of a variable.
6818 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6819 * "numbuf" is used for a number.
6820 * Does not put quotes around strings, as ":echo" displays values.
6821 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6825 echo_string(tv
, tofree
, numbuf
, copyID
)
6831 static int recurse
= 0;
6834 if (recurse
>= DICT_MAXNEST
)
6836 EMSG(_("E724: variable nested too deep for displaying"));
6846 r
= tv
->vval
.v_string
;
6850 if (tv
->vval
.v_list
== NULL
)
6855 else if (copyID
!= 0 && tv
->vval
.v_list
->lv_copyID
== copyID
)
6858 r
= (char_u
*)"[...]";
6862 tv
->vval
.v_list
->lv_copyID
= copyID
;
6863 *tofree
= list2string(tv
, copyID
);
6869 if (tv
->vval
.v_dict
== NULL
)
6874 else if (copyID
!= 0 && tv
->vval
.v_dict
->dv_copyID
== copyID
)
6877 r
= (char_u
*)"{...}";
6881 tv
->vval
.v_dict
->dv_copyID
= copyID
;
6882 *tofree
= dict2string(tv
, copyID
);
6890 r
= get_tv_string_buf(tv
, numbuf
);
6894 EMSG2(_(e_intern2
), "echo_string()");
6903 * Return a string with the string representation of a variable.
6904 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6905 * "numbuf" is used for a number.
6906 * Puts quotes around strings, so that they can be parsed back by eval().
6910 tv2string(tv
, tofree
, numbuf
, copyID
)
6919 *tofree
= string_quote(tv
->vval
.v_string
, TRUE
);
6922 *tofree
= string_quote(tv
->vval
.v_string
, FALSE
);
6929 EMSG2(_(e_intern2
), "tv2string()");
6931 return echo_string(tv
, tofree
, numbuf
, copyID
);
6935 * Return string "str" in ' quotes, doubling ' characters.
6936 * If "str" is NULL an empty string is assumed.
6937 * If "function" is TRUE make it function('string').
6940 string_quote(str
, function
)
6947 len
= (function
? 13 : 3);
6950 len
+= (unsigned)STRLEN(str
);
6951 for (p
= str
; *p
!= NUL
; mb_ptr_adv(p
))
6960 STRCPY(r
, "function('");
6966 for (p
= str
; *p
!= NUL
; )
6981 * Get the value of an environment variable.
6982 * "arg" is pointing to the '$'. It is advanced to after the name.
6983 * If the environment variable was not set, silently assume it is empty.
6987 get_env_tv(arg
, rettv
, evaluate
)
6992 char_u
*string
= NULL
;
6996 int mustfree
= FALSE
;
7000 len
= get_env_len(arg
);
7007 /* first try vim_getenv(), fast for normal environment vars */
7008 string
= vim_getenv(name
, &mustfree
);
7009 if (string
!= NULL
&& *string
!= NUL
)
7012 string
= vim_strsave(string
);
7019 /* next try expanding things like $VIM and ${HOME} */
7020 string
= expand_env_save(name
- 1);
7021 if (string
!= NULL
&& *string
== '$')
7029 rettv
->v_type
= VAR_STRING
;
7030 rettv
->vval
.v_string
= string
;
7037 * Array with names and number of arguments of all internal functions
7038 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7042 char *f_name
; /* function name */
7043 char f_min_argc
; /* minimal number of arguments */
7044 char f_max_argc
; /* maximal number of arguments */
7045 void (*f_func
) __ARGS((typval_T
*args
, typval_T
*rvar
));
7046 /* implementation of function */
7049 {"add", 2, 2, f_add
},
7050 {"append", 2, 2, f_append
},
7051 {"argc", 0, 0, f_argc
},
7052 {"argidx", 0, 0, f_argidx
},
7053 {"argv", 0, 1, f_argv
},
7054 {"browse", 4, 4, f_browse
},
7055 {"browsedir", 2, 2, f_browsedir
},
7056 {"bufexists", 1, 1, f_bufexists
},
7057 {"buffer_exists", 1, 1, f_bufexists
}, /* obsolete */
7058 {"buffer_name", 1, 1, f_bufname
}, /* obsolete */
7059 {"buffer_number", 1, 1, f_bufnr
}, /* obsolete */
7060 {"buflisted", 1, 1, f_buflisted
},
7061 {"bufloaded", 1, 1, f_bufloaded
},
7062 {"bufname", 1, 1, f_bufname
},
7063 {"bufnr", 1, 2, f_bufnr
},
7064 {"bufwinnr", 1, 1, f_bufwinnr
},
7065 {"byte2line", 1, 1, f_byte2line
},
7066 {"byteidx", 2, 2, f_byteidx
},
7067 {"call", 2, 3, f_call
},
7068 {"changenr", 0, 0, f_changenr
},
7069 {"char2nr", 1, 1, f_char2nr
},
7070 {"cindent", 1, 1, f_cindent
},
7071 {"clearmatches", 0, 0, f_clearmatches
},
7072 {"col", 1, 1, f_col
},
7073 #if defined(FEAT_INS_EXPAND)
7074 {"complete", 2, 2, f_complete
},
7075 {"complete_add", 1, 1, f_complete_add
},
7076 {"complete_check", 0, 0, f_complete_check
},
7078 {"confirm", 1, 4, f_confirm
},
7079 {"copy", 1, 1, f_copy
},
7080 {"count", 2, 4, f_count
},
7081 {"cscope_connection",0,3, f_cscope_connection
},
7082 {"cursor", 1, 3, f_cursor
},
7083 {"deepcopy", 1, 2, f_deepcopy
},
7084 {"delete", 1, 1, f_delete
},
7085 {"did_filetype", 0, 0, f_did_filetype
},
7086 {"diff_filler", 1, 1, f_diff_filler
},
7087 {"diff_hlID", 2, 2, f_diff_hlID
},
7088 {"empty", 1, 1, f_empty
},
7089 {"escape", 2, 2, f_escape
},
7090 {"eval", 1, 1, f_eval
},
7091 {"eventhandler", 0, 0, f_eventhandler
},
7092 {"executable", 1, 1, f_executable
},
7093 {"exists", 1, 1, f_exists
},
7094 {"expand", 1, 2, f_expand
},
7095 {"extend", 2, 3, f_extend
},
7096 {"feedkeys", 1, 2, f_feedkeys
},
7097 {"file_readable", 1, 1, f_filereadable
}, /* obsolete */
7098 {"filereadable", 1, 1, f_filereadable
},
7099 {"filewritable", 1, 1, f_filewritable
},
7100 {"filter", 2, 2, f_filter
},
7101 {"finddir", 1, 3, f_finddir
},
7102 {"findfile", 1, 3, f_findfile
},
7103 {"fnamemodify", 2, 2, f_fnamemodify
},
7104 {"foldclosed", 1, 1, f_foldclosed
},
7105 {"foldclosedend", 1, 1, f_foldclosedend
},
7106 {"foldlevel", 1, 1, f_foldlevel
},
7107 {"foldtext", 0, 0, f_foldtext
},
7108 {"foldtextresult", 1, 1, f_foldtextresult
},
7109 {"foreground", 0, 0, f_foreground
},
7110 {"function", 1, 1, f_function
},
7111 {"garbagecollect", 0, 0, f_garbagecollect
},
7112 {"get", 2, 3, f_get
},
7113 {"getbufline", 2, 3, f_getbufline
},
7114 {"getbufvar", 2, 2, f_getbufvar
},
7115 {"getchar", 0, 1, f_getchar
},
7116 {"getcharmod", 0, 0, f_getcharmod
},
7117 {"getcmdline", 0, 0, f_getcmdline
},
7118 {"getcmdpos", 0, 0, f_getcmdpos
},
7119 {"getcmdtype", 0, 0, f_getcmdtype
},
7120 {"getcwd", 0, 0, f_getcwd
},
7121 {"getfontname", 0, 1, f_getfontname
},
7122 {"getfperm", 1, 1, f_getfperm
},
7123 {"getfsize", 1, 1, f_getfsize
},
7124 {"getftime", 1, 1, f_getftime
},
7125 {"getftype", 1, 1, f_getftype
},
7126 {"getline", 1, 2, f_getline
},
7127 {"getloclist", 1, 1, f_getqflist
},
7128 {"getmatches", 0, 0, f_getmatches
},
7129 {"getpos", 1, 1, f_getpos
},
7130 {"getqflist", 0, 0, f_getqflist
},
7131 {"getreg", 0, 2, f_getreg
},
7132 {"getregtype", 0, 1, f_getregtype
},
7133 {"gettabwinvar", 3, 3, f_gettabwinvar
},
7134 {"getwinposx", 0, 0, f_getwinposx
},
7135 {"getwinposy", 0, 0, f_getwinposy
},
7136 {"getwinvar", 2, 2, f_getwinvar
},
7137 {"glob", 1, 1, f_glob
},
7138 {"globpath", 2, 2, f_globpath
},
7139 {"has", 1, 1, f_has
},
7140 {"has_key", 2, 2, f_has_key
},
7141 {"haslocaldir", 0, 0, f_haslocaldir
},
7142 {"hasmapto", 1, 3, f_hasmapto
},
7143 {"highlightID", 1, 1, f_hlID
}, /* obsolete */
7144 {"highlight_exists",1, 1, f_hlexists
}, /* obsolete */
7145 {"histadd", 2, 2, f_histadd
},
7146 {"histdel", 1, 2, f_histdel
},
7147 {"histget", 1, 2, f_histget
},
7148 {"histnr", 1, 1, f_histnr
},
7149 {"hlID", 1, 1, f_hlID
},
7150 {"hlexists", 1, 1, f_hlexists
},
7151 {"hostname", 0, 0, f_hostname
},
7152 {"iconv", 3, 3, f_iconv
},
7153 {"indent", 1, 1, f_indent
},
7154 {"index", 2, 4, f_index
},
7155 {"input", 1, 3, f_input
},
7156 {"inputdialog", 1, 3, f_inputdialog
},
7157 {"inputlist", 1, 1, f_inputlist
},
7158 {"inputrestore", 0, 0, f_inputrestore
},
7159 {"inputsave", 0, 0, f_inputsave
},
7160 {"inputsecret", 1, 2, f_inputsecret
},
7161 {"insert", 2, 3, f_insert
},
7162 {"isdirectory", 1, 1, f_isdirectory
},
7163 {"islocked", 1, 1, f_islocked
},
7164 {"items", 1, 1, f_items
},
7165 {"join", 1, 2, f_join
},
7166 {"keys", 1, 1, f_keys
},
7167 {"last_buffer_nr", 0, 0, f_last_buffer_nr
},/* obsolete */
7168 {"len", 1, 1, f_len
},
7169 {"libcall", 3, 3, f_libcall
},
7170 {"libcallnr", 3, 3, f_libcallnr
},
7171 {"line", 1, 1, f_line
},
7172 {"line2byte", 1, 1, f_line2byte
},
7173 {"lispindent", 1, 1, f_lispindent
},
7174 {"localtime", 0, 0, f_localtime
},
7175 {"map", 2, 2, f_map
},
7176 {"maparg", 1, 3, f_maparg
},
7177 {"mapcheck", 1, 3, f_mapcheck
},
7178 {"match", 2, 4, f_match
},
7179 {"matchadd", 2, 4, f_matchadd
},
7180 {"matcharg", 1, 1, f_matcharg
},
7181 {"matchdelete", 1, 1, f_matchdelete
},
7182 {"matchend", 2, 4, f_matchend
},
7183 {"matchlist", 2, 4, f_matchlist
},
7184 {"matchstr", 2, 4, f_matchstr
},
7185 {"max", 1, 1, f_max
},
7186 {"min", 1, 1, f_min
},
7188 {"mkdir", 1, 3, f_mkdir
},
7190 {"mode", 0, 0, f_mode
},
7191 {"nextnonblank", 1, 1, f_nextnonblank
},
7192 {"nr2char", 1, 1, f_nr2char
},
7193 {"pathshorten", 1, 1, f_pathshorten
},
7194 {"prevnonblank", 1, 1, f_prevnonblank
},
7195 {"printf", 2, 19, f_printf
},
7196 {"pumvisible", 0, 0, f_pumvisible
},
7197 {"range", 1, 3, f_range
},
7198 {"readfile", 1, 3, f_readfile
},
7199 {"reltime", 0, 2, f_reltime
},
7200 {"reltimestr", 1, 1, f_reltimestr
},
7201 {"remote_expr", 2, 3, f_remote_expr
},
7202 {"remote_foreground", 1, 1, f_remote_foreground
},
7203 {"remote_peek", 1, 2, f_remote_peek
},
7204 {"remote_read", 1, 1, f_remote_read
},
7205 {"remote_send", 2, 3, f_remote_send
},
7206 {"remove", 2, 3, f_remove
},
7207 {"rename", 2, 2, f_rename
},
7208 {"repeat", 2, 2, f_repeat
},
7209 {"resolve", 1, 1, f_resolve
},
7210 {"reverse", 1, 1, f_reverse
},
7211 {"search", 1, 3, f_search
},
7212 {"searchdecl", 1, 3, f_searchdecl
},
7213 {"searchpair", 3, 6, f_searchpair
},
7214 {"searchpairpos", 3, 6, f_searchpairpos
},
7215 {"searchpos", 1, 3, f_searchpos
},
7216 {"server2client", 2, 2, f_server2client
},
7217 {"serverlist", 0, 0, f_serverlist
},
7218 {"setbufvar", 3, 3, f_setbufvar
},
7219 {"setcmdpos", 1, 1, f_setcmdpos
},
7220 {"setline", 2, 2, f_setline
},
7221 {"setloclist", 2, 3, f_setloclist
},
7222 {"setmatches", 1, 1, f_setmatches
},
7223 {"setpos", 2, 2, f_setpos
},
7224 {"setqflist", 1, 2, f_setqflist
},
7225 {"setreg", 2, 3, f_setreg
},
7226 {"settabwinvar", 4, 4, f_settabwinvar
},
7227 {"setwinvar", 3, 3, f_setwinvar
},
7228 {"shellescape", 1, 1, f_shellescape
},
7229 {"simplify", 1, 1, f_simplify
},
7230 {"sort", 1, 2, f_sort
},
7231 {"soundfold", 1, 1, f_soundfold
},
7232 {"spellbadword", 0, 1, f_spellbadword
},
7233 {"spellsuggest", 1, 3, f_spellsuggest
},
7234 {"split", 1, 3, f_split
},
7235 {"str2nr", 1, 2, f_str2nr
},
7236 #ifdef HAVE_STRFTIME
7237 {"strftime", 1, 2, f_strftime
},
7239 {"stridx", 2, 3, f_stridx
},
7240 {"string", 1, 1, f_string
},
7241 {"strlen", 1, 1, f_strlen
},
7242 {"strpart", 2, 3, f_strpart
},
7243 {"strridx", 2, 3, f_strridx
},
7244 {"strtrans", 1, 1, f_strtrans
},
7245 {"submatch", 1, 1, f_submatch
},
7246 {"substitute", 4, 4, f_substitute
},
7247 {"synID", 3, 3, f_synID
},
7248 {"synIDattr", 2, 3, f_synIDattr
},
7249 {"synIDtrans", 1, 1, f_synIDtrans
},
7250 {"system", 1, 2, f_system
},
7251 {"tabpagebuflist", 0, 1, f_tabpagebuflist
},
7252 {"tabpagenr", 0, 1, f_tabpagenr
},
7253 {"tabpagewinnr", 1, 2, f_tabpagewinnr
},
7254 {"tagfiles", 0, 0, f_tagfiles
},
7255 {"taglist", 1, 1, f_taglist
},
7256 {"tempname", 0, 0, f_tempname
},
7257 {"test", 1, 1, f_test
},
7258 {"tolower", 1, 1, f_tolower
},
7259 {"toupper", 1, 1, f_toupper
},
7261 {"type", 1, 1, f_type
},
7262 {"values", 1, 1, f_values
},
7263 {"virtcol", 1, 1, f_virtcol
},
7264 {"visualmode", 0, 1, f_visualmode
},
7265 {"winbufnr", 1, 1, f_winbufnr
},
7266 {"wincol", 0, 0, f_wincol
},
7267 {"winheight", 1, 1, f_winheight
},
7268 {"winline", 0, 0, f_winline
},
7269 {"winnr", 0, 1, f_winnr
},
7270 {"winrestcmd", 0, 0, f_winrestcmd
},
7271 {"winrestview", 1, 1, f_winrestview
},
7272 {"winsaveview", 0, 0, f_winsaveview
},
7273 {"winwidth", 1, 1, f_winwidth
},
7274 {"writefile", 2, 3, f_writefile
},
7277 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
7280 * Function given to ExpandGeneric() to obtain the list of internal
7281 * or user defined function names.
7284 get_function_name(xp
, idx
)
7288 static int intidx
= -1;
7295 name
= get_user_func_name(xp
, idx
);
7299 if (++intidx
< (int)(sizeof(functions
) / sizeof(struct fst
)))
7301 STRCPY(IObuff
, functions
[intidx
].f_name
);
7302 STRCAT(IObuff
, "(");
7303 if (functions
[intidx
].f_max_argc
== 0)
7304 STRCAT(IObuff
, ")");
7312 * Function given to ExpandGeneric() to obtain the list of internal or
7313 * user defined variable or function names.
7317 get_expr_name(xp
, idx
)
7321 static int intidx
= -1;
7328 name
= get_function_name(xp
, idx
);
7332 return get_user_var_name(xp
, ++intidx
);
7335 #endif /* FEAT_CMDL_COMPL */
7338 * Find internal function in table above.
7339 * Return index, or -1 if not found
7342 find_internal_func(name
)
7343 char_u
*name
; /* name of the function */
7346 int last
= (int)(sizeof(functions
) / sizeof(struct fst
)) - 1;
7351 * Find the function name in the table. Binary search.
7353 while (first
<= last
)
7355 x
= first
+ ((unsigned)(last
- first
) >> 1);
7356 cmp
= STRCMP(name
, functions
[x
].f_name
);
7368 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
7369 * name it contains, otherwise return "name".
7372 deref_func_name(name
, lenp
)
7381 v
= find_var(name
, NULL
);
7383 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
7385 if (v
->di_tv
.vval
.v_string
== NULL
)
7388 return (char_u
*)""; /* just in case */
7390 *lenp
= (int)STRLEN(v
->di_tv
.vval
.v_string
);
7391 return v
->di_tv
.vval
.v_string
;
7398 * Allocate a variable for the result of a function.
7399 * Return OK or FAIL.
7402 get_func_tv(name
, len
, rettv
, arg
, firstline
, lastline
, doesrange
,
7404 char_u
*name
; /* name of the function */
7405 int len
; /* length of "name" */
7407 char_u
**arg
; /* argument, pointing to the '(' */
7408 linenr_T firstline
; /* first line of range */
7409 linenr_T lastline
; /* last line of range */
7410 int *doesrange
; /* return: function handled range */
7412 dict_T
*selfdict
; /* Dictionary for "self" */
7416 typval_T argvars
[MAX_FUNC_ARGS
+ 1]; /* vars for arguments */
7417 int argcount
= 0; /* number of arguments found */
7420 * Get the arguments.
7423 while (argcount
< MAX_FUNC_ARGS
)
7425 argp
= skipwhite(argp
+ 1); /* skip the '(' or ',' */
7426 if (*argp
== ')' || *argp
== ',' || *argp
== NUL
)
7428 if (eval1(&argp
, &argvars
[argcount
], evaluate
) == FAIL
)
7443 ret
= call_func(name
, len
, rettv
, argcount
, argvars
,
7444 firstline
, lastline
, doesrange
, evaluate
, selfdict
);
7445 else if (!aborting())
7447 if (argcount
== MAX_FUNC_ARGS
)
7448 emsg_funcname("E740: Too many arguments for function %s", name
);
7450 emsg_funcname("E116: Invalid arguments for function %s", name
);
7453 while (--argcount
>= 0)
7454 clear_tv(&argvars
[argcount
]);
7456 *arg
= skipwhite(argp
);
7462 * Call a function with its resolved parameters
7463 * Return OK when the function can't be called, FAIL otherwise.
7464 * Also returns OK when an error was encountered while executing the function.
7467 call_func(name
, len
, rettv
, argcount
, argvars
, firstline
, lastline
,
7468 doesrange
, evaluate
, selfdict
)
7469 char_u
*name
; /* name of the function */
7470 int len
; /* length of "name" */
7471 typval_T
*rettv
; /* return value goes here */
7472 int argcount
; /* number of "argvars" */
7473 typval_T
*argvars
; /* vars for arguments, must have "argcount"
7474 PLUS ONE elements! */
7475 linenr_T firstline
; /* first line of range */
7476 linenr_T lastline
; /* last line of range */
7477 int *doesrange
; /* return: function handled range */
7479 dict_T
*selfdict
; /* Dictionary for "self" */
7482 #define ERROR_UNKNOWN 0
7483 #define ERROR_TOOMANY 1
7484 #define ERROR_TOOFEW 2
7485 #define ERROR_SCRIPT 3
7486 #define ERROR_DICT 4
7487 #define ERROR_NONE 5
7488 #define ERROR_OTHER 6
7489 int error
= ERROR_NONE
;
7494 #define FLEN_FIXED 40
7495 char_u fname_buf
[FLEN_FIXED
+ 1];
7499 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7500 * Change <SNR>123_name() to K_SNR 123_name().
7501 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7505 llen
= eval_fname_script(name
);
7508 fname_buf
[0] = K_SPECIAL
;
7509 fname_buf
[1] = KS_EXTRA
;
7510 fname_buf
[2] = (int)KE_SNR
;
7512 if (eval_fname_sid(name
)) /* "<SID>" or "s:" */
7514 if (current_SID
<= 0)
7515 error
= ERROR_SCRIPT
;
7518 sprintf((char *)fname_buf
+ 3, "%ld_", (long)current_SID
);
7519 i
= (int)STRLEN(fname_buf
);
7522 if (i
+ STRLEN(name
+ llen
) < FLEN_FIXED
)
7524 STRCPY(fname_buf
+ i
, name
+ llen
);
7529 fname
= alloc((unsigned)(i
+ STRLEN(name
+ llen
) + 1));
7531 error
= ERROR_OTHER
;
7534 mch_memmove(fname
, fname_buf
, (size_t)i
);
7535 STRCPY(fname
+ i
, name
+ llen
);
7545 /* execute the function if no errors detected and executing */
7546 if (evaluate
&& error
== ERROR_NONE
)
7548 rettv
->v_type
= VAR_NUMBER
; /* default is number rettv */
7549 error
= ERROR_UNKNOWN
;
7551 if (!builtin_function(fname
))
7554 * User defined function.
7556 fp
= find_func(fname
);
7559 /* Trigger FuncUndefined event, may load the function. */
7561 && apply_autocmds(EVENT_FUNCUNDEFINED
,
7562 fname
, fname
, TRUE
, NULL
)
7565 /* executed an autocommand, search for the function again */
7566 fp
= find_func(fname
);
7569 /* Try loading a package. */
7570 if (fp
== NULL
&& script_autoload(fname
, TRUE
) && !aborting())
7572 /* loaded a package, search for the function again */
7573 fp
= find_func(fname
);
7578 if (fp
->uf_flags
& FC_RANGE
)
7580 if (argcount
< fp
->uf_args
.ga_len
)
7581 error
= ERROR_TOOFEW
;
7582 else if (!fp
->uf_varargs
&& argcount
> fp
->uf_args
.ga_len
)
7583 error
= ERROR_TOOMANY
;
7584 else if ((fp
->uf_flags
& FC_DICT
) && selfdict
== NULL
)
7589 * Call the user function.
7590 * Save and restore search patterns, script variables and
7593 save_search_patterns();
7596 call_user_func(fp
, argcount
, argvars
, rettv
,
7597 firstline
, lastline
,
7598 (fp
->uf_flags
& FC_DICT
) ? selfdict
: NULL
);
7599 if (--fp
->uf_calls
<= 0 && isdigit(*fp
->uf_name
)
7600 && fp
->uf_refcount
<= 0)
7601 /* Function was unreferenced while being used, free it
7605 restore_search_patterns();
7613 * Find the function name in the table, call its implementation.
7615 i
= find_internal_func(fname
);
7618 if (argcount
< functions
[i
].f_min_argc
)
7619 error
= ERROR_TOOFEW
;
7620 else if (argcount
> functions
[i
].f_max_argc
)
7621 error
= ERROR_TOOMANY
;
7624 argvars
[argcount
].v_type
= VAR_UNKNOWN
;
7625 functions
[i
].f_func(argvars
, rettv
);
7631 * The function call (or "FuncUndefined" autocommand sequence) might
7632 * have been aborted by an error, an interrupt, or an explicitly thrown
7633 * exception that has not been caught so far. This situation can be
7634 * tested for by calling aborting(). For an error in an internal
7635 * function or for the "E132" error in call_user_func(), however, the
7636 * throw point at which the "force_abort" flag (temporarily reset by
7637 * emsg()) is normally updated has not been reached yet. We need to
7638 * update that flag first to make aborting() reliable.
7640 update_force_abort();
7642 if (error
== ERROR_NONE
)
7646 * Report an error unless the argument evaluation or function call has been
7647 * cancelled due to an aborting error, an interrupt, or an exception.
7654 emsg_funcname(N_("E117: Unknown function: %s"), name
);
7657 emsg_funcname(e_toomanyarg
, name
);
7660 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
7664 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
7668 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
7675 if (fname
!= name
&& fname
!= fname_buf
)
7682 * Give an error message with a function name. Handle <SNR> things.
7685 emsg_funcname(ermsg
, name
)
7691 if (*name
== K_SPECIAL
)
7692 p
= concat_str((char_u
*)"<SNR>", name
+ 3);
7700 /*********************************************
7701 * Implementation of the built-in functions
7705 * "add(list, item)" function
7708 f_add(argvars
, rettv
)
7714 rettv
->vval
.v_number
= 1; /* Default: Failed */
7715 if (argvars
[0].v_type
== VAR_LIST
)
7717 if ((l
= argvars
[0].vval
.v_list
) != NULL
7718 && !tv_check_lock(l
->lv_lock
, (char_u
*)"add()")
7719 && list_append_tv(l
, &argvars
[1]) == OK
)
7720 copy_tv(&argvars
[0], rettv
);
7727 * "append(lnum, string/list)" function
7730 f_append(argvars
, rettv
)
7737 listitem_T
*li
= NULL
;
7741 lnum
= get_tv_lnum(argvars
);
7743 && lnum
<= curbuf
->b_ml
.ml_line_count
7744 && u_save(lnum
, lnum
+ 1) == OK
)
7746 if (argvars
[1].v_type
== VAR_LIST
)
7748 l
= argvars
[1].vval
.v_list
;
7753 rettv
->vval
.v_number
= 0; /* Default: Success */
7757 tv
= &argvars
[1]; /* append a string */
7758 else if (li
== NULL
)
7759 break; /* end of list */
7761 tv
= &li
->li_tv
; /* append item from list */
7762 line
= get_tv_string_chk(tv
);
7763 if (line
== NULL
) /* type error */
7765 rettv
->vval
.v_number
= 1; /* Failed */
7768 ml_append(lnum
+ added
, line
, (colnr_T
)0, FALSE
);
7775 appended_lines_mark(lnum
, added
);
7776 if (curwin
->w_cursor
.lnum
> lnum
)
7777 curwin
->w_cursor
.lnum
+= added
;
7780 rettv
->vval
.v_number
= 1; /* Failed */
7788 f_argc(argvars
, rettv
)
7792 rettv
->vval
.v_number
= ARGCOUNT
;
7796 * "argidx()" function
7800 f_argidx(argvars
, rettv
)
7804 rettv
->vval
.v_number
= curwin
->w_arg_idx
;
7808 * "argv(nr)" function
7811 f_argv(argvars
, rettv
)
7817 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
7819 idx
= get_tv_number_chk(&argvars
[0], NULL
);
7820 if (idx
>= 0 && idx
< ARGCOUNT
)
7821 rettv
->vval
.v_string
= vim_strsave(alist_name(&ARGLIST
[idx
]));
7823 rettv
->vval
.v_string
= NULL
;
7824 rettv
->v_type
= VAR_STRING
;
7826 else if (rettv_list_alloc(rettv
) == OK
)
7827 for (idx
= 0; idx
< ARGCOUNT
; ++idx
)
7828 list_append_string(rettv
->vval
.v_list
,
7829 alist_name(&ARGLIST
[idx
]), -1);
7833 * "browse(save, title, initdir, default)" function
7837 f_browse(argvars
, rettv
)
7846 char_u buf
[NUMBUFLEN
];
7847 char_u buf2
[NUMBUFLEN
];
7850 save
= get_tv_number_chk(&argvars
[0], &error
);
7851 title
= get_tv_string_chk(&argvars
[1]);
7852 initdir
= get_tv_string_buf_chk(&argvars
[2], buf
);
7853 defname
= get_tv_string_buf_chk(&argvars
[3], buf2
);
7855 if (error
|| title
== NULL
|| initdir
== NULL
|| defname
== NULL
)
7856 rettv
->vval
.v_string
= NULL
;
7858 rettv
->vval
.v_string
=
7859 do_browse(save
? BROWSE_SAVE
: 0,
7860 title
, defname
, NULL
, initdir
, NULL
, curbuf
);
7862 rettv
->vval
.v_string
= NULL
;
7864 rettv
->v_type
= VAR_STRING
;
7868 * "browsedir(title, initdir)" function
7872 f_browsedir(argvars
, rettv
)
7879 char_u buf
[NUMBUFLEN
];
7881 title
= get_tv_string_chk(&argvars
[0]);
7882 initdir
= get_tv_string_buf_chk(&argvars
[1], buf
);
7884 if (title
== NULL
|| initdir
== NULL
)
7885 rettv
->vval
.v_string
= NULL
;
7887 rettv
->vval
.v_string
= do_browse(BROWSE_DIR
,
7888 title
, NULL
, NULL
, initdir
, NULL
, curbuf
);
7890 rettv
->vval
.v_string
= NULL
;
7892 rettv
->v_type
= VAR_STRING
;
7895 static buf_T
*find_buffer
__ARGS((typval_T
*avar
));
7898 * Find a buffer by number or exact name.
7906 if (avar
->v_type
== VAR_NUMBER
)
7907 buf
= buflist_findnr((int)avar
->vval
.v_number
);
7908 else if (avar
->v_type
== VAR_STRING
&& avar
->vval
.v_string
!= NULL
)
7910 buf
= buflist_findname_exp(avar
->vval
.v_string
);
7913 /* No full path name match, try a match with a URL or a "nofile"
7914 * buffer, these don't use the full path. */
7915 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
7916 if (buf
->b_fname
!= NULL
7917 && (path_with_url(buf
->b_fname
)
7918 #ifdef FEAT_QUICKFIX
7922 && STRCMP(buf
->b_fname
, avar
->vval
.v_string
) == 0)
7930 * "bufexists(expr)" function
7933 f_bufexists(argvars
, rettv
)
7937 rettv
->vval
.v_number
= (find_buffer(&argvars
[0]) != NULL
);
7941 * "buflisted(expr)" function
7944 f_buflisted(argvars
, rettv
)
7950 buf
= find_buffer(&argvars
[0]);
7951 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_p_bl
);
7955 * "bufloaded(expr)" function
7958 f_bufloaded(argvars
, rettv
)
7964 buf
= find_buffer(&argvars
[0]);
7965 rettv
->vval
.v_number
= (buf
!= NULL
&& buf
->b_ml
.ml_mfp
!= NULL
);
7968 static buf_T
*get_buf_tv
__ARGS((typval_T
*tv
));
7971 * Get buffer by number or pattern.
7977 char_u
*name
= tv
->vval
.v_string
;
7982 if (tv
->v_type
== VAR_NUMBER
)
7983 return buflist_findnr((int)tv
->vval
.v_number
);
7984 if (tv
->v_type
!= VAR_STRING
)
7986 if (name
== NULL
|| *name
== NUL
)
7988 if (name
[0] == '$' && name
[1] == NUL
)
7991 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7992 save_magic
= p_magic
;
7995 p_cpo
= (char_u
*)"";
7997 buf
= buflist_findnr(buflist_findpat(name
, name
+ STRLEN(name
),
8000 p_magic
= save_magic
;
8003 /* If not found, try expanding the name, like done for bufexists(). */
8005 buf
= find_buffer(tv
);
8011 * "bufname(expr)" function
8014 f_bufname(argvars
, rettv
)
8020 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8022 buf
= get_buf_tv(&argvars
[0]);
8023 rettv
->v_type
= VAR_STRING
;
8024 if (buf
!= NULL
&& buf
->b_fname
!= NULL
)
8025 rettv
->vval
.v_string
= vim_strsave(buf
->b_fname
);
8027 rettv
->vval
.v_string
= NULL
;
8032 * "bufnr(expr)" function
8035 f_bufnr(argvars
, rettv
)
8043 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8045 buf
= get_buf_tv(&argvars
[0]);
8048 /* If the buffer isn't found and the second argument is not zero create a
8051 && argvars
[1].v_type
!= VAR_UNKNOWN
8052 && get_tv_number_chk(&argvars
[1], &error
) != 0
8054 && (name
= get_tv_string_chk(&argvars
[0])) != NULL
8056 buf
= buflist_new(name
, NULL
, (linenr_T
)1, 0);
8059 rettv
->vval
.v_number
= buf
->b_fnum
;
8061 rettv
->vval
.v_number
= -1;
8065 * "bufwinnr(nr)" function
8068 f_bufwinnr(argvars
, rettv
)
8078 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
8080 buf
= get_buf_tv(&argvars
[0]);
8082 for (wp
= firstwin
; wp
; wp
= wp
->w_next
)
8085 if (wp
->w_buffer
== buf
)
8088 rettv
->vval
.v_number
= (wp
!= NULL
? winnr
: -1);
8090 rettv
->vval
.v_number
= (curwin
->w_buffer
== buf
? 1 : -1);
8096 * "byte2line(byte)" function
8100 f_byte2line(argvars
, rettv
)
8104 #ifndef FEAT_BYTEOFF
8105 rettv
->vval
.v_number
= -1;
8109 boff
= get_tv_number(&argvars
[0]) - 1; /* boff gets -1 on type error */
8111 rettv
->vval
.v_number
= -1;
8113 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
,
8114 (linenr_T
)0, &boff
);
8119 * "byteidx()" function
8123 f_byteidx(argvars
, rettv
)
8133 str
= get_tv_string_chk(&argvars
[0]);
8134 idx
= get_tv_number_chk(&argvars
[1], NULL
);
8135 rettv
->vval
.v_number
= -1;
8136 if (str
== NULL
|| idx
< 0)
8141 for ( ; idx
> 0; idx
--)
8143 if (*t
== NUL
) /* EOL reached */
8145 t
+= (*mb_ptr2len
)(t
);
8147 rettv
->vval
.v_number
= (varnumber_T
)(t
- str
);
8149 if (idx
<= STRLEN(str
))
8150 rettv
->vval
.v_number
= idx
;
8155 * "call(func, arglist)" function
8158 f_call(argvars
, rettv
)
8163 typval_T argv
[MAX_FUNC_ARGS
+ 1];
8167 dict_T
*selfdict
= NULL
;
8169 rettv
->vval
.v_number
= 0;
8170 if (argvars
[1].v_type
!= VAR_LIST
)
8175 if (argvars
[1].vval
.v_list
== NULL
)
8178 if (argvars
[0].v_type
== VAR_FUNC
)
8179 func
= argvars
[0].vval
.v_string
;
8181 func
= get_tv_string(&argvars
[0]);
8183 return; /* type error or empty name */
8185 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8187 if (argvars
[2].v_type
!= VAR_DICT
)
8192 selfdict
= argvars
[2].vval
.v_dict
;
8195 for (item
= argvars
[1].vval
.v_list
->lv_first
; item
!= NULL
;
8196 item
= item
->li_next
)
8198 if (argc
== MAX_FUNC_ARGS
)
8200 EMSG(_("E699: Too many arguments"));
8203 /* Make a copy of each argument. This is needed to be able to set
8204 * v_lock to VAR_FIXED in the copy without changing the original list.
8206 copy_tv(&item
->li_tv
, &argv
[argc
++]);
8210 (void)call_func(func
, (int)STRLEN(func
), rettv
, argc
, argv
,
8211 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
8212 &dummy
, TRUE
, selfdict
);
8214 /* Free the arguments. */
8216 clear_tv(&argv
[--argc
]);
8220 * "changenr()" function
8224 f_changenr(argvars
, rettv
)
8228 rettv
->vval
.v_number
= curbuf
->b_u_seq_cur
;
8232 * "char2nr(string)" function
8235 f_char2nr(argvars
, rettv
)
8241 rettv
->vval
.v_number
= (*mb_ptr2char
)(get_tv_string(&argvars
[0]));
8244 rettv
->vval
.v_number
= get_tv_string(&argvars
[0])[0];
8248 * "cindent(lnum)" function
8251 f_cindent(argvars
, rettv
)
8259 pos
= curwin
->w_cursor
;
8260 lnum
= get_tv_lnum(argvars
);
8261 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
8263 curwin
->w_cursor
.lnum
= lnum
;
8264 rettv
->vval
.v_number
= get_c_indent();
8265 curwin
->w_cursor
= pos
;
8269 rettv
->vval
.v_number
= -1;
8273 * "clearmatches()" function
8277 f_clearmatches(argvars
, rettv
)
8281 #ifdef FEAT_SEARCH_EXTRA
8282 clear_matches(curwin
);
8287 * "col(string)" function
8290 f_col(argvars
, rettv
)
8296 int fnum
= curbuf
->b_fnum
;
8298 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
8299 if (fp
!= NULL
&& fnum
== curbuf
->b_fnum
)
8301 if (fp
->col
== MAXCOL
)
8303 /* '> can be MAXCOL, get the length of the line then */
8304 if (fp
->lnum
<= curbuf
->b_ml
.ml_line_count
)
8305 col
= (colnr_T
)STRLEN(ml_get(fp
->lnum
)) + 1;
8312 #ifdef FEAT_VIRTUALEDIT
8313 /* col(".") when the cursor is on the NUL at the end of the line
8314 * because of "coladd" can be seen as an extra column. */
8315 if (virtual_active() && fp
== &curwin
->w_cursor
)
8317 char_u
*p
= ml_get_cursor();
8319 if (curwin
->w_cursor
.coladd
>= (colnr_T
)chartabsize(p
,
8320 curwin
->w_virtcol
- curwin
->w_cursor
.coladd
))
8325 if (*p
!= NUL
&& p
[(l
= (*mb_ptr2len
)(p
))] == NUL
)
8328 if (*p
!= NUL
&& p
[1] == NUL
)
8336 rettv
->vval
.v_number
= col
;
8339 #if defined(FEAT_INS_EXPAND)
8341 * "complete()" function
8345 f_complete(argvars
, rettv
)
8351 if ((State
& INSERT
) == 0)
8353 EMSG(_("E785: complete() can only be used in Insert mode"));
8357 /* Check for undo allowed here, because if something was already inserted
8358 * the line was already saved for undo and this check isn't done. */
8359 if (!undo_allowed())
8362 if (argvars
[1].v_type
!= VAR_LIST
|| argvars
[1].vval
.v_list
== NULL
)
8368 startcol
= get_tv_number_chk(&argvars
[0], NULL
);
8372 set_completion(startcol
- 1, argvars
[1].vval
.v_list
);
8376 * "complete_add()" function
8380 f_complete_add(argvars
, rettv
)
8384 rettv
->vval
.v_number
= ins_compl_add_tv(&argvars
[0], 0);
8388 * "complete_check()" function
8392 f_complete_check(argvars
, rettv
)
8396 int saved
= RedrawingDisabled
;
8398 RedrawingDisabled
= 0;
8399 ins_compl_check_keys(0);
8400 rettv
->vval
.v_number
= compl_interrupted
;
8401 RedrawingDisabled
= saved
;
8406 * "confirm(message, buttons[, default [, type]])" function
8410 f_confirm(argvars
, rettv
)
8414 #if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
8416 char_u
*buttons
= NULL
;
8417 char_u buf
[NUMBUFLEN
];
8418 char_u buf2
[NUMBUFLEN
];
8420 int type
= VIM_GENERIC
;
8424 message
= get_tv_string_chk(&argvars
[0]);
8425 if (message
== NULL
)
8427 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
8429 buttons
= get_tv_string_buf_chk(&argvars
[1], buf
);
8430 if (buttons
== NULL
)
8432 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8434 def
= get_tv_number_chk(&argvars
[2], &error
);
8435 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
8437 typestr
= get_tv_string_buf_chk(&argvars
[3], buf2
);
8438 if (typestr
== NULL
)
8442 switch (TOUPPER_ASC(*typestr
))
8444 case 'E': type
= VIM_ERROR
; break;
8445 case 'Q': type
= VIM_QUESTION
; break;
8446 case 'I': type
= VIM_INFO
; break;
8447 case 'W': type
= VIM_WARNING
; break;
8448 case 'G': type
= VIM_GENERIC
; break;
8455 if (buttons
== NULL
|| *buttons
== NUL
)
8456 buttons
= (char_u
*)_("&Ok");
8459 rettv
->vval
.v_number
= 0;
8461 rettv
->vval
.v_number
= do_dialog(type
, NULL
, message
, buttons
,
8464 rettv
->vval
.v_number
= 0;
8472 f_copy(argvars
, rettv
)
8476 item_copy(&argvars
[0], rettv
, FALSE
, 0);
8480 * "count()" function
8483 f_count(argvars
, rettv
)
8490 if (argvars
[0].v_type
== VAR_LIST
)
8496 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
8499 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8503 ic
= get_tv_number_chk(&argvars
[2], &error
);
8504 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
8506 idx
= get_tv_number_chk(&argvars
[3], &error
);
8509 li
= list_find(l
, idx
);
8511 EMSGN(_(e_listidx
), idx
);
8518 for ( ; li
!= NULL
; li
= li
->li_next
)
8519 if (tv_equal(&li
->li_tv
, &argvars
[1], ic
))
8523 else if (argvars
[0].v_type
== VAR_DICT
)
8529 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
8533 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8535 ic
= get_tv_number_chk(&argvars
[2], &error
);
8536 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
8540 todo
= error
? 0 : (int)d
->dv_hashtab
.ht_used
;
8541 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
8543 if (!HASHITEM_EMPTY(hi
))
8546 if (tv_equal(&HI2DI(hi
)->di_tv
, &argvars
[1], ic
))
8553 EMSG2(_(e_listdictarg
), "count()");
8554 rettv
->vval
.v_number
= n
;
8558 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8560 * Checks the existence of a cscope connection.
8564 f_cscope_connection(argvars
, rettv
)
8570 char_u
*dbpath
= NULL
;
8571 char_u
*prepend
= NULL
;
8572 char_u buf
[NUMBUFLEN
];
8574 if (argvars
[0].v_type
!= VAR_UNKNOWN
8575 && argvars
[1].v_type
!= VAR_UNKNOWN
)
8577 num
= (int)get_tv_number(&argvars
[0]);
8578 dbpath
= get_tv_string(&argvars
[1]);
8579 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8580 prepend
= get_tv_string_buf(&argvars
[2], buf
);
8583 rettv
->vval
.v_number
= cs_connection(num
, dbpath
, prepend
);
8585 rettv
->vval
.v_number
= 0;
8590 * "cursor(lnum, col)" function
8592 * Moves the cursor to the specified line and column
8596 f_cursor(argvars
, rettv
)
8601 #ifdef FEAT_VIRTUALEDIT
8605 if (argvars
[1].v_type
== VAR_UNKNOWN
)
8609 if (list2fpos(argvars
, &pos
, NULL
) == FAIL
)
8613 #ifdef FEAT_VIRTUALEDIT
8614 coladd
= pos
.coladd
;
8619 line
= get_tv_lnum(argvars
);
8620 col
= get_tv_number_chk(&argvars
[1], NULL
);
8621 #ifdef FEAT_VIRTUALEDIT
8622 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
8623 coladd
= get_tv_number_chk(&argvars
[2], NULL
);
8626 if (line
< 0 || col
< 0
8627 #ifdef FEAT_VIRTUALEDIT
8631 return; /* type error; errmsg already given */
8633 curwin
->w_cursor
.lnum
= line
;
8635 curwin
->w_cursor
.col
= col
- 1;
8636 #ifdef FEAT_VIRTUALEDIT
8637 curwin
->w_cursor
.coladd
= coladd
;
8640 /* Make sure the cursor is in a valid position. */
8643 /* Correct cursor for multi-byte character. */
8648 curwin
->w_set_curswant
= TRUE
;
8652 * "deepcopy()" function
8655 f_deepcopy(argvars
, rettv
)
8661 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
8662 noref
= get_tv_number_chk(&argvars
[1], NULL
);
8663 if (noref
< 0 || noref
> 1)
8666 item_copy(&argvars
[0], rettv
, TRUE
, noref
== 0 ? ++current_copyID
: 0);
8670 * "delete()" function
8673 f_delete(argvars
, rettv
)
8677 if (check_restricted() || check_secure())
8678 rettv
->vval
.v_number
= -1;
8680 rettv
->vval
.v_number
= mch_remove(get_tv_string(&argvars
[0]));
8684 * "did_filetype()" function
8688 f_did_filetype(argvars
, rettv
)
8693 rettv
->vval
.v_number
= did_filetype
;
8695 rettv
->vval
.v_number
= 0;
8700 * "diff_filler()" function
8704 f_diff_filler(argvars
, rettv
)
8709 rettv
->vval
.v_number
= diff_check_fill(curwin
, get_tv_lnum(argvars
));
8714 * "diff_hlID()" function
8718 f_diff_hlID(argvars
, rettv
)
8723 linenr_T lnum
= get_tv_lnum(argvars
);
8724 static linenr_T prev_lnum
= 0;
8725 static int changedtick
= 0;
8726 static int fnum
= 0;
8727 static int change_start
= 0;
8728 static int change_end
= 0;
8729 static hlf_T hlID
= 0;
8733 if (lnum
< 0) /* ignore type error in {lnum} arg */
8735 if (lnum
!= prev_lnum
8736 || changedtick
!= curbuf
->b_changedtick
8737 || fnum
!= curbuf
->b_fnum
)
8739 /* New line, buffer, change: need to get the values. */
8740 filler_lines
= diff_check(curwin
, lnum
);
8741 if (filler_lines
< 0)
8743 if (filler_lines
== -1)
8745 change_start
= MAXCOL
;
8747 if (diff_find_change(curwin
, lnum
, &change_start
, &change_end
))
8748 hlID
= HLF_ADD
; /* added line */
8750 hlID
= HLF_CHD
; /* changed line */
8753 hlID
= HLF_ADD
; /* added line */
8758 changedtick
= curbuf
->b_changedtick
;
8759 fnum
= curbuf
->b_fnum
;
8762 if (hlID
== HLF_CHD
|| hlID
== HLF_TXD
)
8764 col
= get_tv_number(&argvars
[1]) - 1; /* ignore type error in {col} */
8765 if (col
>= change_start
&& col
<= change_end
)
8766 hlID
= HLF_TXD
; /* changed text */
8768 hlID
= HLF_CHD
; /* changed line */
8770 rettv
->vval
.v_number
= hlID
== (hlf_T
)0 ? 0 : (int)hlID
;
8775 * "empty({expr})" function
8778 f_empty(argvars
, rettv
)
8784 switch (argvars
[0].v_type
)
8788 n
= argvars
[0].vval
.v_string
== NULL
8789 || *argvars
[0].vval
.v_string
== NUL
;
8792 n
= argvars
[0].vval
.v_number
== 0;
8795 n
= argvars
[0].vval
.v_list
== NULL
8796 || argvars
[0].vval
.v_list
->lv_first
== NULL
;
8799 n
= argvars
[0].vval
.v_dict
== NULL
8800 || argvars
[0].vval
.v_dict
->dv_hashtab
.ht_used
== 0;
8803 EMSG2(_(e_intern2
), "f_empty()");
8807 rettv
->vval
.v_number
= n
;
8811 * "escape({string}, {chars})" function
8814 f_escape(argvars
, rettv
)
8818 char_u buf
[NUMBUFLEN
];
8820 rettv
->vval
.v_string
= vim_strsave_escaped(get_tv_string(&argvars
[0]),
8821 get_tv_string_buf(&argvars
[1], buf
));
8822 rettv
->v_type
= VAR_STRING
;
8830 f_eval(argvars
, rettv
)
8836 s
= get_tv_string_chk(&argvars
[0]);
8840 if (s
== NULL
|| eval1(&s
, rettv
, TRUE
) == FAIL
)
8842 rettv
->v_type
= VAR_NUMBER
;
8843 rettv
->vval
.v_number
= 0;
8846 EMSG(_(e_trailing
));
8850 * "eventhandler()" function
8854 f_eventhandler(argvars
, rettv
)
8858 rettv
->vval
.v_number
= vgetc_busy
;
8862 * "executable()" function
8865 f_executable(argvars
, rettv
)
8869 rettv
->vval
.v_number
= mch_can_exe(get_tv_string(&argvars
[0]));
8873 * "exists()" function
8876 f_exists(argvars
, rettv
)
8885 p
= get_tv_string(&argvars
[0]);
8886 if (*p
== '$') /* environment variable */
8888 /* first try "normal" environment variables (fast) */
8889 if (mch_getenv(p
+ 1) != NULL
)
8893 /* try expanding things like $VIM and ${HOME} */
8894 p
= expand_env_save(p
);
8895 if (p
!= NULL
&& *p
!= '$')
8900 else if (*p
== '&' || *p
== '+') /* option */
8902 n
= (get_option_tv(&p
, NULL
, TRUE
) == OK
);
8903 if (*skipwhite(p
) != NUL
)
8904 n
= FALSE
; /* trailing garbage */
8906 else if (*p
== '*') /* internal or user defined function */
8908 n
= function_exists(p
+ 1);
8912 n
= cmd_exists(p
+ 1);
8918 n
= autocmd_supported(p
+ 2);
8920 n
= au_exists(p
+ 1);
8923 else /* internal variable */
8928 /* get_name_len() takes care of expanding curly braces */
8930 len
= get_name_len(&p
, &tofree
, TRUE
, FALSE
);
8935 n
= (get_var_tv(name
, len
, &tv
, FALSE
) == OK
);
8938 /* handle d.key, l[idx], f(expr) */
8939 n
= (handle_subscript(&p
, &tv
, TRUE
, FALSE
) == OK
);
8950 rettv
->vval
.v_number
= n
;
8954 * "expand()" function
8957 f_expand(argvars
, rettv
)
8964 int flags
= WILD_SILENT
|WILD_USE_NL
|WILD_LIST_NOTFOUND
;
8968 rettv
->v_type
= VAR_STRING
;
8969 s
= get_tv_string(&argvars
[0]);
8970 if (*s
== '%' || *s
== '#' || *s
== '<')
8973 rettv
->vval
.v_string
= eval_vars(s
, s
, &len
, NULL
, &errormsg
, NULL
);
8978 /* When the optional second argument is non-zero, don't remove matches
8979 * for 'suffixes' and 'wildignore' */
8980 if (argvars
[1].v_type
!= VAR_UNKNOWN
8981 && get_tv_number_chk(&argvars
[1], &error
))
8982 flags
|= WILD_KEEP_ALL
;
8986 xpc
.xp_context
= EXPAND_FILES
;
8987 rettv
->vval
.v_string
= ExpandOne(&xpc
, s
, NULL
, flags
, WILD_ALL
);
8990 rettv
->vval
.v_string
= NULL
;
8995 * "extend(list, list [, idx])" function
8996 * "extend(dict, dict [, action])" function
8999 f_extend(argvars
, rettv
)
9003 rettv
->vval
.v_number
= 0;
9004 if (argvars
[0].v_type
== VAR_LIST
&& argvars
[1].v_type
== VAR_LIST
)
9011 l1
= argvars
[0].vval
.v_list
;
9012 l2
= argvars
[1].vval
.v_list
;
9013 if (l1
!= NULL
&& !tv_check_lock(l1
->lv_lock
, (char_u
*)"extend()")
9016 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9018 before
= get_tv_number_chk(&argvars
[2], &error
);
9020 return; /* type error; errmsg already given */
9022 if (before
== l1
->lv_len
)
9026 item
= list_find(l1
, before
);
9029 EMSGN(_(e_listidx
), before
);
9036 list_extend(l1
, l2
, item
);
9038 copy_tv(&argvars
[0], rettv
);
9041 else if (argvars
[0].v_type
== VAR_DICT
&& argvars
[1].v_type
== VAR_DICT
)
9050 d1
= argvars
[0].vval
.v_dict
;
9051 d2
= argvars
[1].vval
.v_dict
;
9052 if (d1
!= NULL
&& !tv_check_lock(d1
->dv_lock
, (char_u
*)"extend()")
9055 /* Check the third argument. */
9056 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9058 static char *(av
[]) = {"keep", "force", "error"};
9060 action
= get_tv_string_chk(&argvars
[2]);
9062 return; /* type error; errmsg already given */
9063 for (i
= 0; i
< 3; ++i
)
9064 if (STRCMP(action
, av
[i
]) == 0)
9068 EMSG2(_(e_invarg2
), action
);
9073 action
= (char_u
*)"force";
9075 /* Go over all entries in the second dict and add them to the
9077 todo
= (int)d2
->dv_hashtab
.ht_used
;
9078 for (hi2
= d2
->dv_hashtab
.ht_array
; todo
> 0; ++hi2
)
9080 if (!HASHITEM_EMPTY(hi2
))
9083 di1
= dict_find(d1
, hi2
->hi_key
, -1);
9086 di1
= dictitem_copy(HI2DI(hi2
));
9087 if (di1
!= NULL
&& dict_add(d1
, di1
) == FAIL
)
9090 else if (*action
== 'e')
9092 EMSG2(_("E737: Key already exists: %s"), hi2
->hi_key
);
9095 else if (*action
== 'f')
9097 clear_tv(&di1
->di_tv
);
9098 copy_tv(&HI2DI(hi2
)->di_tv
, &di1
->di_tv
);
9103 copy_tv(&argvars
[0], rettv
);
9107 EMSG2(_(e_listdictarg
), "extend()");
9111 * "feedkeys()" function
9115 f_feedkeys(argvars
, rettv
)
9120 char_u
*keys
, *flags
;
9121 char_u nbuf
[NUMBUFLEN
];
9125 /* This is not allowed in the sandbox. If the commands would still be
9126 * executed in the sandbox it would be OK, but it probably happens later,
9127 * when "sandbox" is no longer set. */
9131 rettv
->vval
.v_number
= 0;
9132 keys
= get_tv_string(&argvars
[0]);
9135 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9137 flags
= get_tv_string_buf(&argvars
[1], nbuf
);
9138 for ( ; *flags
!= NUL
; ++flags
)
9142 case 'n': remap
= FALSE
; break;
9143 case 'm': remap
= TRUE
; break;
9144 case 't': typed
= TRUE
; break;
9149 /* Need to escape K_SPECIAL and CSI before putting the string in the
9150 * typeahead buffer. */
9151 keys_esc
= vim_strsave_escape_csi(keys
);
9152 if (keys_esc
!= NULL
)
9154 ins_typebuf(keys_esc
, (remap
? REMAP_YES
: REMAP_NONE
),
9155 typebuf
.tb_len
, !typed
, FALSE
);
9158 typebuf_was_filled
= TRUE
;
9164 * "filereadable()" function
9167 f_filereadable(argvars
, rettv
)
9175 p
= get_tv_string(&argvars
[0]);
9176 if (*p
&& !mch_isdir(p
) && (fd
= mch_fopen((char *)p
, "r")) != NULL
)
9184 rettv
->vval
.v_number
= n
;
9188 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
9189 * rights to write into.
9192 f_filewritable(argvars
, rettv
)
9196 rettv
->vval
.v_number
= filewritable(get_tv_string(&argvars
[0]));
9199 static void findfilendir
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int dir
));
9202 findfilendir(argvars
, rettv
, dir
)
9207 #ifdef FEAT_SEARCHPATH
9209 char_u
*fresult
= NULL
;
9210 char_u
*path
= *curbuf
->b_p_path
== NUL
? p_path
: curbuf
->b_p_path
;
9212 char_u pathbuf
[NUMBUFLEN
];
9218 rettv
->vval
.v_string
= NULL
;
9219 rettv
->v_type
= VAR_STRING
;
9221 #ifdef FEAT_SEARCHPATH
9222 fname
= get_tv_string(&argvars
[0]);
9224 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
9226 p
= get_tv_string_buf_chk(&argvars
[1], pathbuf
);
9234 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
9235 count
= get_tv_number_chk(&argvars
[2], &error
);
9239 if (count
< 0 && rettv_list_alloc(rettv
) == FAIL
)
9242 if (*fname
!= NUL
&& !error
)
9246 if (rettv
->v_type
== VAR_STRING
)
9248 fresult
= find_file_in_path_option(first
? fname
: NULL
,
9249 first
? (int)STRLEN(fname
) : 0,
9250 0, first
, path
, dir
, curbuf
->b_ffname
,
9251 dir
? (char_u
*)"" : curbuf
->b_p_sua
);
9254 if (fresult
!= NULL
&& rettv
->v_type
== VAR_LIST
)
9255 list_append_string(rettv
->vval
.v_list
, fresult
, -1);
9257 } while ((rettv
->v_type
== VAR_LIST
|| --count
> 0) && fresult
!= NULL
);
9260 if (rettv
->v_type
== VAR_STRING
)
9261 rettv
->vval
.v_string
= fresult
;
9265 static void filter_map
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int map
));
9266 static int filter_map_one
__ARGS((typval_T
*tv
, char_u
*expr
, int map
, int *remp
));
9269 * Implementation of map() and filter().
9272 filter_map(argvars
, rettv
, map
)
9277 char_u buf
[NUMBUFLEN
];
9279 listitem_T
*li
, *nli
;
9289 char_u
*ermsg
= map
? (char_u
*)"map()" : (char_u
*)"filter()";
9292 rettv
->vval
.v_number
= 0;
9293 if (argvars
[0].v_type
== VAR_LIST
)
9295 if ((l
= argvars
[0].vval
.v_list
) == NULL
9296 || (map
&& tv_check_lock(l
->lv_lock
, ermsg
)))
9299 else if (argvars
[0].v_type
== VAR_DICT
)
9301 if ((d
= argvars
[0].vval
.v_dict
) == NULL
9302 || (map
&& tv_check_lock(d
->dv_lock
, ermsg
)))
9307 EMSG2(_(e_listdictarg
), ermsg
);
9311 expr
= get_tv_string_buf_chk(&argvars
[1], buf
);
9312 /* On type errors, the preceding call has already displayed an error
9313 * message. Avoid a misleading error message for an empty string that
9314 * was not passed as argument. */
9317 prepare_vimvar(VV_VAL
, &save_val
);
9318 expr
= skipwhite(expr
);
9320 /* We reset "did_emsg" to be able to detect whether an error
9321 * occurred during evaluation of the expression. */
9322 save_did_emsg
= did_emsg
;
9325 if (argvars
[0].v_type
== VAR_DICT
)
9327 prepare_vimvar(VV_KEY
, &save_key
);
9328 vimvars
[VV_KEY
].vv_type
= VAR_STRING
;
9330 ht
= &d
->dv_hashtab
;
9332 todo
= (int)ht
->ht_used
;
9333 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
9335 if (!HASHITEM_EMPTY(hi
))
9339 if (tv_check_lock(di
->di_tv
.v_lock
, ermsg
))
9341 vimvars
[VV_KEY
].vv_str
= vim_strsave(di
->di_key
);
9342 if (filter_map_one(&di
->di_tv
, expr
, map
, &rem
) == FAIL
9346 dictitem_remove(d
, di
);
9347 clear_tv(&vimvars
[VV_KEY
].vv_tv
);
9352 restore_vimvar(VV_KEY
, &save_key
);
9356 for (li
= l
->lv_first
; li
!= NULL
; li
= nli
)
9358 if (tv_check_lock(li
->li_tv
.v_lock
, ermsg
))
9361 if (filter_map_one(&li
->li_tv
, expr
, map
, &rem
) == FAIL
9365 listitem_remove(l
, li
);
9369 restore_vimvar(VV_VAL
, &save_val
);
9371 did_emsg
|= save_did_emsg
;
9374 copy_tv(&argvars
[0], rettv
);
9378 filter_map_one(tv
, expr
, map
, remp
)
9387 copy_tv(tv
, &vimvars
[VV_VAL
].vv_tv
);
9389 if (eval1(&s
, &rettv
, TRUE
) == FAIL
)
9391 if (*s
!= NUL
) /* check for trailing chars after expr */
9393 EMSG2(_(e_invexpr2
), s
);
9398 /* map(): replace the list item value */
9407 /* filter(): when expr is zero remove the item */
9408 *remp
= (get_tv_number_chk(&rettv
, &error
) == 0);
9410 /* On type error, nothing has been removed; return FAIL to stop the
9411 * loop. The error message was given by get_tv_number_chk(). */
9415 clear_tv(&vimvars
[VV_VAL
].vv_tv
);
9420 * "filter()" function
9423 f_filter(argvars
, rettv
)
9427 filter_map(argvars
, rettv
, FALSE
);
9431 * "finddir({fname}[, {path}[, {count}]])" function
9434 f_finddir(argvars
, rettv
)
9438 findfilendir(argvars
, rettv
, TRUE
);
9442 * "findfile({fname}[, {path}[, {count}]])" function
9445 f_findfile(argvars
, rettv
)
9449 findfilendir(argvars
, rettv
, FALSE
);
9453 * "fnamemodify({fname}, {mods})" function
9456 f_fnamemodify(argvars
, rettv
)
9464 char_u
*fbuf
= NULL
;
9465 char_u buf
[NUMBUFLEN
];
9467 fname
= get_tv_string_chk(&argvars
[0]);
9468 mods
= get_tv_string_buf_chk(&argvars
[1], buf
);
9469 if (fname
== NULL
|| mods
== NULL
)
9473 len
= (int)STRLEN(fname
);
9474 (void)modify_fname(mods
, &usedlen
, &fname
, &fbuf
, &len
);
9477 rettv
->v_type
= VAR_STRING
;
9479 rettv
->vval
.v_string
= NULL
;
9481 rettv
->vval
.v_string
= vim_strnsave(fname
, len
);
9485 static void foldclosed_both
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int end
));
9488 * "foldclosed()" function
9491 foldclosed_both(argvars
, rettv
, end
)
9498 linenr_T first
, last
;
9500 lnum
= get_tv_lnum(argvars
);
9501 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
9503 if (hasFoldingWin(curwin
, lnum
, &first
, &last
, FALSE
, NULL
))
9506 rettv
->vval
.v_number
= (varnumber_T
)last
;
9508 rettv
->vval
.v_number
= (varnumber_T
)first
;
9513 rettv
->vval
.v_number
= -1;
9517 * "foldclosed()" function
9520 f_foldclosed(argvars
, rettv
)
9524 foldclosed_both(argvars
, rettv
, FALSE
);
9528 * "foldclosedend()" function
9531 f_foldclosedend(argvars
, rettv
)
9535 foldclosed_both(argvars
, rettv
, TRUE
);
9539 * "foldlevel()" function
9542 f_foldlevel(argvars
, rettv
)
9549 lnum
= get_tv_lnum(argvars
);
9550 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
9551 rettv
->vval
.v_number
= foldLevel(lnum
);
9554 rettv
->vval
.v_number
= 0;
9558 * "foldtext()" function
9562 f_foldtext(argvars
, rettv
)
9574 rettv
->v_type
= VAR_STRING
;
9575 rettv
->vval
.v_string
= NULL
;
9577 if ((linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
> 0
9578 && (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
9579 <= curbuf
->b_ml
.ml_line_count
9580 && vimvars
[VV_FOLDDASHES
].vv_str
!= NULL
)
9582 /* Find first non-empty line in the fold. */
9583 lnum
= (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
;
9584 while (lnum
< (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
9586 if (!linewhite(lnum
))
9591 /* Find interesting text in this line. */
9592 s
= skipwhite(ml_get(lnum
));
9593 /* skip C comment-start */
9594 if (s
[0] == '/' && (s
[1] == '*' || s
[1] == '/'))
9596 s
= skipwhite(s
+ 2);
9597 if (*skipwhite(s
) == NUL
9598 && lnum
+ 1 < (linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
)
9600 s
= skipwhite(ml_get(lnum
+ 1));
9602 s
= skipwhite(s
+ 1);
9605 txt
= _("+-%s%3ld lines: ");
9606 r
= alloc((unsigned)(STRLEN(txt
)
9607 + STRLEN(vimvars
[VV_FOLDDASHES
].vv_str
) /* for %s */
9609 + STRLEN(s
))); /* concatenated */
9612 sprintf((char *)r
, txt
, vimvars
[VV_FOLDDASHES
].vv_str
,
9613 (long)((linenr_T
)vimvars
[VV_FOLDEND
].vv_nr
9614 - (linenr_T
)vimvars
[VV_FOLDSTART
].vv_nr
+ 1));
9615 len
= (int)STRLEN(r
);
9617 /* remove 'foldmarker' and 'commentstring' */
9618 foldtext_cleanup(r
+ len
);
9619 rettv
->vval
.v_string
= r
;
9626 * "foldtextresult(lnum)" function
9630 f_foldtextresult(argvars
, rettv
)
9638 foldinfo_T foldinfo
;
9642 rettv
->v_type
= VAR_STRING
;
9643 rettv
->vval
.v_string
= NULL
;
9645 lnum
= get_tv_lnum(argvars
);
9646 /* treat illegal types and illegal string values for {lnum} the same */
9649 fold_count
= foldedCount(curwin
, lnum
, &foldinfo
);
9652 text
= get_foldtext(curwin
, lnum
, lnum
+ fold_count
- 1,
9655 text
= vim_strsave(text
);
9656 rettv
->vval
.v_string
= text
;
9662 * "foreground()" function
9666 f_foreground(argvars
, rettv
)
9670 rettv
->vval
.v_number
= 0;
9673 gui_mch_set_foreground();
9676 win32_set_foreground();
9682 * "function()" function
9686 f_function(argvars
, rettv
)
9692 rettv
->vval
.v_number
= 0;
9693 s
= get_tv_string(&argvars
[0]);
9694 if (s
== NULL
|| *s
== NUL
|| VIM_ISDIGIT(*s
))
9695 EMSG2(_(e_invarg2
), s
);
9696 else if (!function_exists(s
))
9697 EMSG2(_("E700: Unknown function: %s"), s
);
9700 rettv
->vval
.v_string
= vim_strsave(s
);
9701 rettv
->v_type
= VAR_FUNC
;
9706 * "garbagecollect()" function
9710 f_garbagecollect(argvars
, rettv
)
9714 /* This is postponed until we are back at the toplevel, because we may be
9715 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
9716 want_garbage_collect
= TRUE
;
9723 f_get(argvars
, rettv
)
9731 typval_T
*tv
= NULL
;
9733 if (argvars
[0].v_type
== VAR_LIST
)
9735 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
9739 li
= list_find(l
, get_tv_number_chk(&argvars
[1], &error
));
9740 if (!error
&& li
!= NULL
)
9744 else if (argvars
[0].v_type
== VAR_DICT
)
9746 if ((d
= argvars
[0].vval
.v_dict
) != NULL
)
9748 di
= dict_find(d
, get_tv_string(&argvars
[1]), -1);
9754 EMSG2(_(e_listdictarg
), "get()");
9758 if (argvars
[2].v_type
== VAR_UNKNOWN
)
9759 rettv
->vval
.v_number
= 0;
9761 copy_tv(&argvars
[2], rettv
);
9767 static void get_buffer_lines
__ARGS((buf_T
*buf
, linenr_T start
, linenr_T end
, int retlist
, typval_T
*rettv
));
9770 * Get line or list of lines from buffer "buf" into "rettv".
9771 * Return a range (from start to end) of lines in rettv from the specified
9773 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
9776 get_buffer_lines(buf
, start
, end
, retlist
, rettv
)
9787 if (rettv_list_alloc(rettv
) == FAIL
)
9791 rettv
->vval
.v_number
= 0;
9793 if (buf
== NULL
|| buf
->b_ml
.ml_mfp
== NULL
|| start
< 0)
9798 if (start
>= 1 && start
<= buf
->b_ml
.ml_line_count
)
9799 p
= ml_get_buf(buf
, start
, FALSE
);
9803 rettv
->v_type
= VAR_STRING
;
9804 rettv
->vval
.v_string
= vim_strsave(p
);
9813 if (end
> buf
->b_ml
.ml_line_count
)
9814 end
= buf
->b_ml
.ml_line_count
;
9815 while (start
<= end
)
9816 if (list_append_string(rettv
->vval
.v_list
,
9817 ml_get_buf(buf
, start
++, FALSE
), -1) == FAIL
)
9823 * "getbufline()" function
9826 f_getbufline(argvars
, rettv
)
9834 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
9836 buf
= get_buf_tv(&argvars
[0]);
9839 lnum
= get_tv_lnum_buf(&argvars
[1], buf
);
9840 if (argvars
[2].v_type
== VAR_UNKNOWN
)
9843 end
= get_tv_lnum_buf(&argvars
[2], buf
);
9845 get_buffer_lines(buf
, lnum
, end
, TRUE
, rettv
);
9849 * "getbufvar()" function
9852 f_getbufvar(argvars
, rettv
)
9861 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
9862 varname
= get_tv_string_chk(&argvars
[1]);
9864 buf
= get_buf_tv(&argvars
[0]);
9866 rettv
->v_type
= VAR_STRING
;
9867 rettv
->vval
.v_string
= NULL
;
9869 if (buf
!= NULL
&& varname
!= NULL
)
9871 if (*varname
== '&') /* buffer-local-option */
9873 /* set curbuf to be our buf, temporarily */
9874 save_curbuf
= curbuf
;
9877 get_option_tv(&varname
, rettv
, TRUE
);
9879 /* restore previous notion of curbuf */
9880 curbuf
= save_curbuf
;
9884 if (*varname
== NUL
)
9885 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9886 * scope prefix before the NUL byte is required by
9887 * find_var_in_ht(). */
9888 varname
= (char_u
*)"b:" + 2;
9889 /* look up the variable */
9890 v
= find_var_in_ht(&buf
->b_vars
.dv_hashtab
, varname
, FALSE
);
9892 copy_tv(&v
->di_tv
, rettv
);
9900 * "getchar()" function
9903 f_getchar(argvars
, rettv
)
9910 /* Position the cursor. Needed after a message that ends in a space. */
9911 windgoto(msg_row
, msg_col
);
9917 if (argvars
[0].v_type
== VAR_UNKNOWN
)
9918 /* getchar(): blocking wait. */
9920 else if (get_tv_number_chk(&argvars
[0], &error
) == 1)
9921 /* getchar(1): only check if char avail */
9923 else if (error
|| vpeekc() == NUL
)
9924 /* illegal argument or getchar(0) and no char avail: return zero */
9927 /* getchar(0) and char avail: return char */
9936 vimvars
[VV_MOUSE_WIN
].vv_nr
= 0;
9937 vimvars
[VV_MOUSE_LNUM
].vv_nr
= 0;
9938 vimvars
[VV_MOUSE_COL
].vv_nr
= 0;
9940 rettv
->vval
.v_number
= n
;
9941 if (IS_SPECIAL(n
) || mod_mask
!= 0)
9943 char_u temp
[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9946 /* Turn a special key into three bytes, plus modifier. */
9949 temp
[i
++] = K_SPECIAL
;
9950 temp
[i
++] = KS_MODIFIER
;
9951 temp
[i
++] = mod_mask
;
9955 temp
[i
++] = K_SPECIAL
;
9956 temp
[i
++] = K_SECOND(n
);
9957 temp
[i
++] = K_THIRD(n
);
9961 i
+= (*mb_char2bytes
)(n
, temp
+ i
);
9966 rettv
->v_type
= VAR_STRING
;
9967 rettv
->vval
.v_string
= vim_strsave(temp
);
9970 if (n
== K_LEFTMOUSE
9971 || n
== K_LEFTMOUSE_NM
9973 || n
== K_LEFTRELEASE
9974 || n
== K_LEFTRELEASE_NM
9975 || n
== K_MIDDLEMOUSE
9976 || n
== K_MIDDLEDRAG
9977 || n
== K_MIDDLERELEASE
9978 || n
== K_RIGHTMOUSE
9980 || n
== K_RIGHTRELEASE
9990 int row
= mouse_row
;
9991 int col
= mouse_col
;
9994 # ifdef FEAT_WINDOWS
9999 if (row
>= 0 && col
>= 0)
10001 /* Find the window at the mouse coordinates and compute the
10002 * text position. */
10003 win
= mouse_find_win(&row
, &col
);
10004 (void)mouse_comp_pos(win
, &row
, &col
, &lnum
);
10005 # ifdef FEAT_WINDOWS
10006 for (wp
= firstwin
; wp
!= win
; wp
= wp
->w_next
)
10009 vimvars
[VV_MOUSE_WIN
].vv_nr
= n
;
10010 vimvars
[VV_MOUSE_LNUM
].vv_nr
= lnum
;
10011 vimvars
[VV_MOUSE_COL
].vv_nr
= col
+ 1;
10019 * "getcharmod()" function
10023 f_getcharmod(argvars
, rettv
)
10027 rettv
->vval
.v_number
= mod_mask
;
10031 * "getcmdline()" function
10035 f_getcmdline(argvars
, rettv
)
10039 rettv
->v_type
= VAR_STRING
;
10040 rettv
->vval
.v_string
= get_cmdline_str();
10044 * "getcmdpos()" function
10048 f_getcmdpos(argvars
, rettv
)
10052 rettv
->vval
.v_number
= get_cmdline_pos() + 1;
10056 * "getcmdtype()" function
10060 f_getcmdtype(argvars
, rettv
)
10064 rettv
->v_type
= VAR_STRING
;
10065 rettv
->vval
.v_string
= alloc(2);
10066 if (rettv
->vval
.v_string
!= NULL
)
10068 rettv
->vval
.v_string
[0] = get_cmdline_type();
10069 rettv
->vval
.v_string
[1] = NUL
;
10074 * "getcwd()" function
10078 f_getcwd(argvars
, rettv
)
10082 char_u cwd
[MAXPATHL
];
10084 rettv
->v_type
= VAR_STRING
;
10085 if (mch_dirname(cwd
, MAXPATHL
) == FAIL
)
10086 rettv
->vval
.v_string
= NULL
;
10089 rettv
->vval
.v_string
= vim_strsave(cwd
);
10090 #ifdef BACKSLASH_IN_FILENAME
10091 if (rettv
->vval
.v_string
!= NULL
)
10092 slash_adjust(rettv
->vval
.v_string
);
10098 * "getfontname()" function
10102 f_getfontname(argvars
, rettv
)
10106 rettv
->v_type
= VAR_STRING
;
10107 rettv
->vval
.v_string
= NULL
;
10112 char_u
*name
= NULL
;
10114 if (argvars
[0].v_type
== VAR_UNKNOWN
)
10116 /* Get the "Normal" font. Either the name saved by
10117 * hl_set_font_name() or from the font ID. */
10118 font
= gui
.norm_font
;
10119 name
= hl_get_font_name();
10123 name
= get_tv_string(&argvars
[0]);
10124 if (STRCMP(name
, "*") == 0) /* don't use font dialog */
10126 font
= gui_mch_get_font(name
, FALSE
);
10127 if (font
== NOFONT
)
10128 return; /* Invalid font name, return empty string. */
10130 rettv
->vval
.v_string
= gui_mch_get_fontname(font
, name
);
10131 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
10132 gui_mch_free_font(font
);
10138 * "getfperm({fname})" function
10141 f_getfperm(argvars
, rettv
)
10147 char_u
*perm
= NULL
;
10148 char_u flags
[] = "rwx";
10151 fname
= get_tv_string(&argvars
[0]);
10153 rettv
->v_type
= VAR_STRING
;
10154 if (mch_stat((char *)fname
, &st
) >= 0)
10156 perm
= vim_strsave((char_u
*)"---------");
10159 for (i
= 0; i
< 9; i
++)
10161 if (st
.st_mode
& (1 << (8 - i
)))
10162 perm
[i
] = flags
[i
% 3];
10166 rettv
->vval
.v_string
= perm
;
10170 * "getfsize({fname})" function
10173 f_getfsize(argvars
, rettv
)
10180 fname
= get_tv_string(&argvars
[0]);
10182 rettv
->v_type
= VAR_NUMBER
;
10184 if (mch_stat((char *)fname
, &st
) >= 0)
10186 if (mch_isdir(fname
))
10187 rettv
->vval
.v_number
= 0;
10190 rettv
->vval
.v_number
= (varnumber_T
)st
.st_size
;
10192 /* non-perfect check for overflow */
10193 if ((off_t
)rettv
->vval
.v_number
!= (off_t
)st
.st_size
)
10194 rettv
->vval
.v_number
= -2;
10198 rettv
->vval
.v_number
= -1;
10202 * "getftime({fname})" function
10205 f_getftime(argvars
, rettv
)
10212 fname
= get_tv_string(&argvars
[0]);
10214 if (mch_stat((char *)fname
, &st
) >= 0)
10215 rettv
->vval
.v_number
= (varnumber_T
)st
.st_mtime
;
10217 rettv
->vval
.v_number
= -1;
10221 * "getftype({fname})" function
10224 f_getftype(argvars
, rettv
)
10230 char_u
*type
= NULL
;
10233 fname
= get_tv_string(&argvars
[0]);
10235 rettv
->v_type
= VAR_STRING
;
10236 if (mch_lstat((char *)fname
, &st
) >= 0)
10239 if (S_ISREG(st
.st_mode
))
10241 else if (S_ISDIR(st
.st_mode
))
10244 else if (S_ISLNK(st
.st_mode
))
10248 else if (S_ISBLK(st
.st_mode
))
10252 else if (S_ISCHR(st
.st_mode
))
10256 else if (S_ISFIFO(st
.st_mode
))
10260 else if (S_ISSOCK(st
.st_mode
))
10267 switch (st
.st_mode
& S_IFMT
)
10269 case S_IFREG
: t
= "file"; break;
10270 case S_IFDIR
: t
= "dir"; break;
10272 case S_IFLNK
: t
= "link"; break;
10275 case S_IFBLK
: t
= "bdev"; break;
10278 case S_IFCHR
: t
= "cdev"; break;
10281 case S_IFIFO
: t
= "fifo"; break;
10284 case S_IFSOCK
: t
= "socket"; break;
10286 default: t
= "other";
10289 if (mch_isdir(fname
))
10295 type
= vim_strsave((char_u
*)t
);
10297 rettv
->vval
.v_string
= type
;
10301 * "getline(lnum, [end])" function
10304 f_getline(argvars
, rettv
)
10312 lnum
= get_tv_lnum(argvars
);
10313 if (argvars
[1].v_type
== VAR_UNKNOWN
)
10320 end
= get_tv_lnum(&argvars
[1]);
10324 get_buffer_lines(curbuf
, lnum
, end
, retlist
, rettv
);
10328 * "getmatches()" function
10332 f_getmatches(argvars
, rettv
)
10336 #ifdef FEAT_SEARCH_EXTRA
10338 matchitem_T
*cur
= curwin
->w_match_head
;
10340 rettv
->vval
.v_number
= 0;
10342 if (rettv_list_alloc(rettv
) == OK
)
10344 while (cur
!= NULL
)
10346 dict
= dict_alloc();
10349 ++dict
->dv_refcount
;
10350 dict_add_nr_str(dict
, "group", 0L, syn_id2name(cur
->hlg_id
));
10351 dict_add_nr_str(dict
, "pattern", 0L, cur
->pattern
);
10352 dict_add_nr_str(dict
, "priority", (long)cur
->priority
, NULL
);
10353 dict_add_nr_str(dict
, "id", (long)cur
->id
, NULL
);
10354 list_append_dict(rettv
->vval
.v_list
, dict
);
10362 * "getpos(string)" function
10365 f_getpos(argvars
, rettv
)
10373 if (rettv_list_alloc(rettv
) == OK
)
10375 l
= rettv
->vval
.v_list
;
10376 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
10378 list_append_number(l
, (varnumber_T
)fnum
);
10380 list_append_number(l
, (varnumber_T
)0);
10381 list_append_number(l
, (fp
!= NULL
) ? (varnumber_T
)fp
->lnum
10383 list_append_number(l
, (fp
!= NULL
) ? (varnumber_T
)fp
->col
+ 1
10385 list_append_number(l
,
10386 #ifdef FEAT_VIRTUALEDIT
10387 (fp
!= NULL
) ? (varnumber_T
)fp
->coladd
:
10392 rettv
->vval
.v_number
= FALSE
;
10396 * "getqflist()" and "getloclist()" functions
10400 f_getqflist(argvars
, rettv
)
10404 #ifdef FEAT_QUICKFIX
10408 rettv
->vval
.v_number
= 0;
10409 #ifdef FEAT_QUICKFIX
10410 if (rettv_list_alloc(rettv
) == OK
)
10413 if (argvars
[0].v_type
!= VAR_UNKNOWN
) /* getloclist() */
10415 wp
= find_win_by_nr(&argvars
[0], NULL
);
10420 (void)get_errorlist(wp
, rettv
->vval
.v_list
);
10426 * "getreg()" function
10429 f_getreg(argvars
, rettv
)
10433 char_u
*strregname
;
10438 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
10440 strregname
= get_tv_string_chk(&argvars
[0]);
10441 error
= strregname
== NULL
;
10442 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
10443 arg2
= get_tv_number_chk(&argvars
[1], &error
);
10446 strregname
= vimvars
[VV_REG
].vv_str
;
10447 regname
= (strregname
== NULL
? '"' : *strregname
);
10451 rettv
->v_type
= VAR_STRING
;
10452 rettv
->vval
.v_string
= error
? NULL
:
10453 get_reg_contents(regname
, TRUE
, arg2
);
10457 * "getregtype()" function
10460 f_getregtype(argvars
, rettv
)
10464 char_u
*strregname
;
10466 char_u buf
[NUMBUFLEN
+ 2];
10469 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
10471 strregname
= get_tv_string_chk(&argvars
[0]);
10472 if (strregname
== NULL
) /* type error; errmsg already given */
10474 rettv
->v_type
= VAR_STRING
;
10475 rettv
->vval
.v_string
= NULL
;
10480 /* Default to v:register */
10481 strregname
= vimvars
[VV_REG
].vv_str
;
10483 regname
= (strregname
== NULL
? '"' : *strregname
);
10489 switch (get_reg_type(regname
, ®len
))
10491 case MLINE
: buf
[0] = 'V'; break;
10492 case MCHAR
: buf
[0] = 'v'; break;
10496 sprintf((char *)buf
+ 1, "%ld", reglen
+ 1);
10500 rettv
->v_type
= VAR_STRING
;
10501 rettv
->vval
.v_string
= vim_strsave(buf
);
10505 * "gettabwinvar()" function
10508 f_gettabwinvar(argvars
, rettv
)
10512 getwinvar(argvars
, rettv
, 1);
10516 * "getwinposx()" function
10520 f_getwinposx(argvars
, rettv
)
10524 rettv
->vval
.v_number
= -1;
10530 if (gui_mch_get_winpos(&x
, &y
) == OK
)
10531 rettv
->vval
.v_number
= x
;
10537 * "getwinposy()" function
10541 f_getwinposy(argvars
, rettv
)
10545 rettv
->vval
.v_number
= -1;
10551 if (gui_mch_get_winpos(&x
, &y
) == OK
)
10552 rettv
->vval
.v_number
= y
;
10558 * Find window specifed by "vp" in tabpage "tp".
10561 find_win_by_nr(vp
, tp
)
10563 tabpage_T
*tp
; /* NULL for current tab page */
10565 #ifdef FEAT_WINDOWS
10570 nr
= get_tv_number_chk(vp
, NULL
);
10572 #ifdef FEAT_WINDOWS
10578 for (wp
= (tp
== NULL
|| tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
10579 wp
!= NULL
; wp
= wp
->w_next
)
10584 if (nr
== 0 || nr
== 1)
10591 * "getwinvar()" function
10594 f_getwinvar(argvars
, rettv
)
10598 getwinvar(argvars
, rettv
, 0);
10602 * getwinvar() and gettabwinvar()
10605 getwinvar(argvars
, rettv
, off
)
10608 int off
; /* 1 for gettabwinvar() */
10610 win_T
*win
, *oldcurwin
;
10615 #ifdef FEAT_WINDOWS
10617 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
10621 win
= find_win_by_nr(&argvars
[off
], tp
);
10622 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
10625 rettv
->v_type
= VAR_STRING
;
10626 rettv
->vval
.v_string
= NULL
;
10628 if (win
!= NULL
&& varname
!= NULL
)
10630 /* Set curwin to be our win, temporarily. Also set curbuf, so
10631 * that we can get buffer-local options. */
10632 oldcurwin
= curwin
;
10634 curbuf
= win
->w_buffer
;
10636 if (*varname
== '&') /* window-local-option */
10637 get_option_tv(&varname
, rettv
, 1);
10640 if (*varname
== NUL
)
10641 /* let getwinvar({nr}, "") return the "w:" dictionary. The
10642 * scope prefix before the NUL byte is required by
10643 * find_var_in_ht(). */
10644 varname
= (char_u
*)"w:" + 2;
10645 /* look up the variable */
10646 v
= find_var_in_ht(&win
->w_vars
.dv_hashtab
, varname
, FALSE
);
10648 copy_tv(&v
->di_tv
, rettv
);
10651 /* restore previous notion of curwin */
10652 curwin
= oldcurwin
;
10653 curbuf
= curwin
->w_buffer
;
10660 * "glob()" function
10663 f_glob(argvars
, rettv
)
10670 xpc
.xp_context
= EXPAND_FILES
;
10671 rettv
->v_type
= VAR_STRING
;
10672 rettv
->vval
.v_string
= ExpandOne(&xpc
, get_tv_string(&argvars
[0]),
10673 NULL
, WILD_USE_NL
|WILD_SILENT
, WILD_ALL
);
10677 * "globpath()" function
10680 f_globpath(argvars
, rettv
)
10684 char_u buf1
[NUMBUFLEN
];
10685 char_u
*file
= get_tv_string_buf_chk(&argvars
[1], buf1
);
10687 rettv
->v_type
= VAR_STRING
;
10689 rettv
->vval
.v_string
= NULL
;
10691 rettv
->vval
.v_string
= globpath(get_tv_string(&argvars
[0]), file
);
10698 f_has(argvars
, rettv
)
10705 static char *(has_list
[]) =
10726 #if defined(MACOS_X_UNIX)
10750 #if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10759 #ifndef CASE_INSENSITIVE_FILENAME
10765 #ifdef FEAT_AUTOCMD
10770 # ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10771 "balloon_multiline",
10774 #if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10776 # ifdef ALL_BUILTIN_TCAPS
10777 "all_builtin_terms",
10780 #ifdef FEAT_BYTEOFF
10783 #ifdef FEAT_CINDENT
10786 #ifdef FEAT_CLIENTSERVER
10789 #ifdef FEAT_CLIPBOARD
10792 #ifdef FEAT_CMDL_COMPL
10795 #ifdef FEAT_CMDHIST
10798 #ifdef FEAT_COMMENTS
10807 #ifdef CURSOR_SHAPE
10813 #ifdef FEAT_CON_DIALOG
10816 #ifdef FEAT_GUI_DIALOG
10822 #ifdef FEAT_DIGRAPHS
10828 #ifdef FEAT_EMACS_TAGS
10831 "eval", /* always present, of course! */
10832 #ifdef FEAT_EX_EXTRA
10835 #ifdef FEAT_SEARCH_EXTRA
10841 #ifdef FEAT_SEARCHPATH
10844 #if defined(UNIX) && !defined(USE_SYSTEM)
10847 #ifdef FEAT_FIND_ID
10850 #ifdef FEAT_FOLDING
10856 #if !defined(USE_SYSTEM) && defined(UNIX)
10859 #ifdef FEAT_GETTEXT
10865 #ifdef FEAT_GUI_ATHENA
10866 # ifdef FEAT_GUI_NEXTAW
10872 #ifdef FEAT_GUI_GTK
10878 #ifdef FEAT_GUI_MAC
10881 #ifdef FEAT_GUI_MOTIF
10884 #ifdef FEAT_GUI_PHOTON
10887 #ifdef FEAT_GUI_W16
10890 #ifdef FEAT_GUI_W32
10893 #ifdef FEAT_HANGULIN
10896 #if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10899 #ifdef FEAT_INS_EXPAND
10902 #ifdef FEAT_JUMPLIST
10908 #ifdef FEAT_LANGMAP
10911 #ifdef FEAT_LIBCALL
10914 #ifdef FEAT_LINEBREAK
10920 #ifdef FEAT_LISTCMDS
10923 #ifdef FEAT_LOCALMAP
10929 #ifdef FEAT_SESSION
10932 #ifdef FEAT_MODIFY_FNAME
10938 #ifdef FEAT_MOUSESHAPE
10941 #if defined(UNIX) || defined(VMS)
10942 # ifdef FEAT_MOUSE_DEC
10945 # ifdef FEAT_MOUSE_GPM
10948 # ifdef FEAT_MOUSE_JSB
10951 # ifdef FEAT_MOUSE_NET
10954 # ifdef FEAT_MOUSE_PTERM
10957 # ifdef FEAT_MOUSE_XTERM
10964 #ifdef FEAT_MBYTE_IME
10967 #ifdef FEAT_MULTI_LANG
10970 #ifdef FEAT_MZSCHEME
10971 #ifndef DYNAMIC_MZSCHEME
10978 #ifdef FEAT_OSFILETYPE
10981 #ifdef FEAT_PATH_EXTRA
10985 #ifndef DYNAMIC_PERL
10990 #ifndef DYNAMIC_PYTHON
10994 #ifdef FEAT_POSTSCRIPT
10997 #ifdef FEAT_PRINTER
11000 #ifdef FEAT_PROFILE
11003 #ifdef FEAT_RELTIME
11006 #ifdef FEAT_QUICKFIX
11009 #ifdef FEAT_RIGHTLEFT
11012 #if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
11015 #ifdef FEAT_SCROLLBIND
11018 #ifdef FEAT_CMDL_INFO
11025 #ifdef FEAT_SMARTINDENT
11031 #ifdef FEAT_STL_OPT
11034 #ifdef FEAT_SUN_WORKSHOP
11037 #ifdef FEAT_NETBEANS_INTG
11046 #if defined(USE_SYSTEM) || !defined(UNIX)
11049 #ifdef FEAT_TAG_BINS
11052 #ifdef FEAT_TAG_OLDSTATIC
11055 #ifdef FEAT_TAG_ANYWHITE
11059 # ifndef DYNAMIC_TCL
11066 #ifdef FEAT_TERMRESPONSE
11069 #ifdef FEAT_TEXTOBJ
11072 #ifdef HAVE_TGETENT
11078 #ifdef FEAT_TOOLBAR
11081 #ifdef FEAT_USR_CMDS
11082 "user-commands", /* was accidentally included in 5.4 */
11085 #ifdef FEAT_VIMINFO
11088 #ifdef FEAT_VERTSPLIT
11091 #ifdef FEAT_VIRTUALEDIT
11097 #ifdef FEAT_VISUALEXTRA
11100 #ifdef FEAT_VREPLACE
11103 #ifdef FEAT_WILDIGN
11106 #ifdef FEAT_WILDMENU
11109 #ifdef FEAT_WINDOWS
11115 #ifdef FEAT_WRITEBACKUP
11121 #ifdef FEAT_XFONTSET
11127 #ifdef USE_XSMP_INTERACT
11130 #ifdef FEAT_XCLIPBOARD
11133 #ifdef FEAT_XTERM_SAVE
11136 #if defined(UNIX) && defined(FEAT_X11)
11142 name
= get_tv_string(&argvars
[0]);
11143 for (i
= 0; has_list
[i
] != NULL
; ++i
)
11144 if (STRICMP(name
, has_list
[i
]) == 0)
11152 if (STRNICMP(name
, "patch", 5) == 0)
11153 n
= has_patch(atoi((char *)name
+ 5));
11154 else if (STRICMP(name
, "vim_starting") == 0)
11155 n
= (starting
!= 0);
11156 #if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
11157 else if (STRICMP(name
, "balloon_multiline") == 0)
11158 n
= multiline_balloon_available();
11161 else if (STRICMP(name
, "tcl") == 0)
11162 n
= tcl_enabled(FALSE
);
11164 #if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
11165 else if (STRICMP(name
, "iconv") == 0)
11166 n
= iconv_enabled(FALSE
);
11168 #ifdef DYNAMIC_MZSCHEME
11169 else if (STRICMP(name
, "mzscheme") == 0)
11170 n
= mzscheme_enabled(FALSE
);
11172 #ifdef DYNAMIC_RUBY
11173 else if (STRICMP(name
, "ruby") == 0)
11174 n
= ruby_enabled(FALSE
);
11176 #ifdef DYNAMIC_PYTHON
11177 else if (STRICMP(name
, "python") == 0)
11178 n
= python_enabled(FALSE
);
11180 #ifdef DYNAMIC_PERL
11181 else if (STRICMP(name
, "perl") == 0)
11182 n
= perl_enabled(FALSE
);
11185 else if (STRICMP(name
, "gui_running") == 0)
11186 n
= (gui
.in_use
|| gui
.starting
);
11187 # ifdef FEAT_GUI_W32
11188 else if (STRICMP(name
, "gui_win32s") == 0)
11189 n
= gui_is_win32s();
11191 # ifdef FEAT_BROWSE
11192 else if (STRICMP(name
, "browse") == 0)
11193 n
= gui
.in_use
; /* gui_mch_browse() works when GUI is running */
11197 else if (STRICMP(name
, "syntax_items") == 0)
11198 n
= syntax_present(curbuf
);
11200 #if defined(WIN3264)
11201 else if (STRICMP(name
, "win95") == 0)
11202 n
= mch_windows95();
11204 #ifdef FEAT_NETBEANS_INTG
11205 else if (STRICMP(name
, "netbeans_enabled") == 0)
11210 rettv
->vval
.v_number
= n
;
11214 * "has_key()" function
11217 f_has_key(argvars
, rettv
)
11221 rettv
->vval
.v_number
= 0;
11222 if (argvars
[0].v_type
!= VAR_DICT
)
11224 EMSG(_(e_dictreq
));
11227 if (argvars
[0].vval
.v_dict
== NULL
)
11230 rettv
->vval
.v_number
= dict_find(argvars
[0].vval
.v_dict
,
11231 get_tv_string(&argvars
[1]), -1) != NULL
;
11235 * "haslocaldir()" function
11239 f_haslocaldir(argvars
, rettv
)
11243 rettv
->vval
.v_number
= (curwin
->w_localdir
!= NULL
);
11247 * "hasmapto()" function
11250 f_hasmapto(argvars
, rettv
)
11256 char_u buf
[NUMBUFLEN
];
11259 name
= get_tv_string(&argvars
[0]);
11260 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11261 mode
= (char_u
*)"nvo";
11264 mode
= get_tv_string_buf(&argvars
[1], buf
);
11265 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
11266 abbr
= get_tv_number(&argvars
[2]);
11269 if (map_to_exists(name
, mode
, abbr
))
11270 rettv
->vval
.v_number
= TRUE
;
11272 rettv
->vval
.v_number
= FALSE
;
11276 * "histadd()" function
11280 f_histadd(argvars
, rettv
)
11284 #ifdef FEAT_CMDHIST
11287 char_u buf
[NUMBUFLEN
];
11290 rettv
->vval
.v_number
= FALSE
;
11291 if (check_restricted() || check_secure())
11293 #ifdef FEAT_CMDHIST
11294 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11295 histype
= str
!= NULL
? get_histtype(str
) : -1;
11298 str
= get_tv_string_buf(&argvars
[1], buf
);
11301 add_to_history(histype
, str
, FALSE
, NUL
);
11302 rettv
->vval
.v_number
= TRUE
;
11310 * "histdel()" function
11314 f_histdel(argvars
, rettv
)
11318 #ifdef FEAT_CMDHIST
11320 char_u buf
[NUMBUFLEN
];
11323 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11326 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
11327 /* only one argument: clear entire history */
11328 n
= clr_history(get_histtype(str
));
11329 else if (argvars
[1].v_type
== VAR_NUMBER
)
11330 /* index given: remove that entry */
11331 n
= del_history_idx(get_histtype(str
),
11332 (int)get_tv_number(&argvars
[1]));
11334 /* string given: remove all matching entries */
11335 n
= del_history_entry(get_histtype(str
),
11336 get_tv_string_buf(&argvars
[1], buf
));
11337 rettv
->vval
.v_number
= n
;
11339 rettv
->vval
.v_number
= 0;
11344 * "histget()" function
11348 f_histget(argvars
, rettv
)
11352 #ifdef FEAT_CMDHIST
11357 str
= get_tv_string_chk(&argvars
[0]); /* NULL on type error */
11359 rettv
->vval
.v_string
= NULL
;
11362 type
= get_histtype(str
);
11363 if (argvars
[1].v_type
== VAR_UNKNOWN
)
11364 idx
= get_history_idx(type
);
11366 idx
= (int)get_tv_number_chk(&argvars
[1], NULL
);
11367 /* -1 on type error */
11368 rettv
->vval
.v_string
= vim_strsave(get_history_entry(type
, idx
));
11371 rettv
->vval
.v_string
= NULL
;
11373 rettv
->v_type
= VAR_STRING
;
11377 * "histnr()" function
11381 f_histnr(argvars
, rettv
)
11387 #ifdef FEAT_CMDHIST
11388 char_u
*history
= get_tv_string_chk(&argvars
[0]);
11390 i
= history
== NULL
? HIST_CMD
- 1 : get_histtype(history
);
11391 if (i
>= HIST_CMD
&& i
< HIST_COUNT
)
11392 i
= get_history_idx(i
);
11396 rettv
->vval
.v_number
= i
;
11400 * "highlightID(name)" function
11403 f_hlID(argvars
, rettv
)
11407 rettv
->vval
.v_number
= syn_name2id(get_tv_string(&argvars
[0]));
11411 * "highlight_exists()" function
11414 f_hlexists(argvars
, rettv
)
11418 rettv
->vval
.v_number
= highlight_exists(get_tv_string(&argvars
[0]));
11422 * "hostname()" function
11426 f_hostname(argvars
, rettv
)
11430 char_u hostname
[256];
11432 mch_get_host_name(hostname
, 256);
11433 rettv
->v_type
= VAR_STRING
;
11434 rettv
->vval
.v_string
= vim_strsave(hostname
);
11442 f_iconv(argvars
, rettv
)
11447 char_u buf1
[NUMBUFLEN
];
11448 char_u buf2
[NUMBUFLEN
];
11449 char_u
*from
, *to
, *str
;
11453 rettv
->v_type
= VAR_STRING
;
11454 rettv
->vval
.v_string
= NULL
;
11457 str
= get_tv_string(&argvars
[0]);
11458 from
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[1], buf1
)));
11459 to
= enc_canonize(enc_skip(get_tv_string_buf(&argvars
[2], buf2
)));
11460 vimconv
.vc_type
= CONV_NONE
;
11461 convert_setup(&vimconv
, from
, to
);
11463 /* If the encodings are equal, no conversion needed. */
11464 if (vimconv
.vc_type
== CONV_NONE
)
11465 rettv
->vval
.v_string
= vim_strsave(str
);
11467 rettv
->vval
.v_string
= string_convert(&vimconv
, str
, NULL
);
11469 convert_setup(&vimconv
, NULL
, NULL
);
11476 * "indent()" function
11479 f_indent(argvars
, rettv
)
11485 lnum
= get_tv_lnum(argvars
);
11486 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
11487 rettv
->vval
.v_number
= get_indent_lnum(lnum
);
11489 rettv
->vval
.v_number
= -1;
11493 * "index()" function
11496 f_index(argvars
, rettv
)
11505 rettv
->vval
.v_number
= -1;
11506 if (argvars
[0].v_type
!= VAR_LIST
)
11508 EMSG(_(e_listreq
));
11511 l
= argvars
[0].vval
.v_list
;
11514 item
= l
->lv_first
;
11515 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
11519 /* Start at specified item. Use the cached index that list_find()
11520 * sets, so that a negative number also works. */
11521 item
= list_find(l
, get_tv_number_chk(&argvars
[2], &error
));
11523 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
11524 ic
= get_tv_number_chk(&argvars
[3], &error
);
11529 for ( ; item
!= NULL
; item
= item
->li_next
, ++idx
)
11530 if (tv_equal(&item
->li_tv
, &argvars
[1], ic
))
11532 rettv
->vval
.v_number
= idx
;
11538 static int inputsecret_flag
= 0;
11540 static void get_user_input
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int inputdialog
));
11543 * This function is used by f_input() and f_inputdialog() functions. The third
11544 * argument to f_input() specifies the type of completion to use at the
11545 * prompt. The third argument to f_inputdialog() specifies the value to return
11546 * when the user cancels the prompt.
11549 get_user_input(argvars
, rettv
, inputdialog
)
11554 char_u
*prompt
= get_tv_string_chk(&argvars
[0]);
11557 char_u buf
[NUMBUFLEN
];
11558 int cmd_silent_save
= cmd_silent
;
11559 char_u
*defstr
= (char_u
*)"";
11560 int xp_type
= EXPAND_NOTHING
;
11561 char_u
*xp_arg
= NULL
;
11563 rettv
->v_type
= VAR_STRING
;
11565 #ifdef NO_CONSOLE_INPUT
11566 /* While starting up, there is no place to enter text. */
11567 if (no_console_input())
11569 rettv
->vval
.v_string
= NULL
;
11574 cmd_silent
= FALSE
; /* Want to see the prompt. */
11575 if (prompt
!= NULL
)
11577 /* Only the part of the message after the last NL is considered as
11578 * prompt for the command line */
11579 p
= vim_strrchr(prompt
, '\n');
11589 msg_puts_attr(prompt
, echo_attr
);
11590 msg_didout
= FALSE
;
11594 cmdline_row
= msg_row
;
11596 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
11598 defstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
11599 if (defstr
!= NULL
)
11600 stuffReadbuffSpec(defstr
);
11602 if (!inputdialog
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
11608 rettv
->vval
.v_string
= NULL
;
11610 xp_name
= get_tv_string_buf_chk(&argvars
[2], buf
);
11611 if (xp_name
== NULL
)
11614 xp_namelen
= (int)STRLEN(xp_name
);
11616 if (parse_compl_arg(xp_name
, xp_namelen
, &xp_type
, &argt
,
11622 if (defstr
!= NULL
)
11623 rettv
->vval
.v_string
=
11624 getcmdline_prompt(inputsecret_flag
? NUL
: '@', p
, echo_attr
,
11629 /* since the user typed this, no need to wait for return */
11630 need_wait_return
= FALSE
;
11631 msg_didout
= FALSE
;
11633 cmd_silent
= cmd_silent_save
;
11637 * "input()" function
11638 * Also handles inputsecret() when inputsecret is set.
11641 f_input(argvars
, rettv
)
11645 get_user_input(argvars
, rettv
, FALSE
);
11649 * "inputdialog()" function
11652 f_inputdialog(argvars
, rettv
)
11656 #if defined(FEAT_GUI_TEXTDIALOG)
11657 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
11658 if (gui
.in_use
&& vim_strchr(p_go
, GO_CONDIALOG
) == NULL
)
11661 char_u buf
[NUMBUFLEN
];
11662 char_u
*defstr
= (char_u
*)"";
11664 message
= get_tv_string_chk(&argvars
[0]);
11665 if (argvars
[1].v_type
!= VAR_UNKNOWN
11666 && (defstr
= get_tv_string_buf_chk(&argvars
[1], buf
)) != NULL
)
11667 vim_strncpy(IObuff
, defstr
, IOSIZE
- 1);
11670 if (message
!= NULL
&& defstr
!= NULL
11671 && do_dialog(VIM_QUESTION
, NULL
, message
,
11672 (char_u
*)_("&OK\n&Cancel"), 1, IObuff
) == 1)
11673 rettv
->vval
.v_string
= vim_strsave(IObuff
);
11676 if (message
!= NULL
&& defstr
!= NULL
11677 && argvars
[1].v_type
!= VAR_UNKNOWN
11678 && argvars
[2].v_type
!= VAR_UNKNOWN
)
11679 rettv
->vval
.v_string
= vim_strsave(
11680 get_tv_string_buf(&argvars
[2], buf
));
11682 rettv
->vval
.v_string
= NULL
;
11684 rettv
->v_type
= VAR_STRING
;
11688 get_user_input(argvars
, rettv
, TRUE
);
11692 * "inputlist()" function
11695 f_inputlist(argvars
, rettv
)
11703 rettv
->vval
.v_number
= 0;
11704 #ifdef NO_CONSOLE_INPUT
11705 /* While starting up, there is no place to enter text. */
11706 if (no_console_input())
11709 if (argvars
[0].v_type
!= VAR_LIST
|| argvars
[0].vval
.v_list
== NULL
)
11711 EMSG2(_(e_listarg
), "inputlist()");
11716 msg_row
= Rows
- 1; /* for when 'cmdheight' > 1 */
11717 lines_left
= Rows
; /* avoid more prompt */
11721 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
; li
= li
->li_next
)
11723 msg_puts(get_tv_string(&li
->li_tv
));
11727 /* Ask for choice. */
11728 selected
= prompt_for_number(&mouse_used
);
11730 selected
-= lines_left
;
11732 rettv
->vval
.v_number
= selected
;
11736 static garray_T ga_userinput
= {0, 0, sizeof(tasave_T
), 4, NULL
};
11739 * "inputrestore()" function
11743 f_inputrestore(argvars
, rettv
)
11747 if (ga_userinput
.ga_len
> 0)
11749 --ga_userinput
.ga_len
;
11750 restore_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
11751 + ga_userinput
.ga_len
);
11752 rettv
->vval
.v_number
= 0; /* OK */
11754 else if (p_verbose
> 1)
11756 verb_msg((char_u
*)_("called inputrestore() more often than inputsave()"));
11757 rettv
->vval
.v_number
= 1; /* Failed */
11762 * "inputsave()" function
11766 f_inputsave(argvars
, rettv
)
11770 /* Add an entry to the stack of typehead storage. */
11771 if (ga_grow(&ga_userinput
, 1) == OK
)
11773 save_typeahead((tasave_T
*)(ga_userinput
.ga_data
)
11774 + ga_userinput
.ga_len
);
11775 ++ga_userinput
.ga_len
;
11776 rettv
->vval
.v_number
= 0; /* OK */
11779 rettv
->vval
.v_number
= 1; /* Failed */
11783 * "inputsecret()" function
11786 f_inputsecret(argvars
, rettv
)
11791 ++inputsecret_flag
;
11792 f_input(argvars
, rettv
);
11794 --inputsecret_flag
;
11798 * "insert()" function
11801 f_insert(argvars
, rettv
)
11810 rettv
->vval
.v_number
= 0;
11811 if (argvars
[0].v_type
!= VAR_LIST
)
11812 EMSG2(_(e_listarg
), "insert()");
11813 else if ((l
= argvars
[0].vval
.v_list
) != NULL
11814 && !tv_check_lock(l
->lv_lock
, (char_u
*)"insert()"))
11816 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
11817 before
= get_tv_number_chk(&argvars
[2], &error
);
11819 return; /* type error; errmsg already given */
11821 if (before
== l
->lv_len
)
11825 item
= list_find(l
, before
);
11828 EMSGN(_(e_listidx
), before
);
11834 list_insert_tv(l
, &argvars
[1], item
);
11835 copy_tv(&argvars
[0], rettv
);
11841 * "isdirectory()" function
11844 f_isdirectory(argvars
, rettv
)
11848 rettv
->vval
.v_number
= mch_isdir(get_tv_string(&argvars
[0]));
11852 * "islocked()" function
11855 f_islocked(argvars
, rettv
)
11863 rettv
->vval
.v_number
= -1;
11864 end
= get_lval(get_tv_string(&argvars
[0]), NULL
, &lv
, FALSE
, FALSE
, FALSE
,
11866 if (end
!= NULL
&& lv
.ll_name
!= NULL
)
11869 EMSG(_(e_trailing
));
11872 if (lv
.ll_tv
== NULL
)
11874 if (check_changedtick(lv
.ll_name
))
11875 rettv
->vval
.v_number
= 1; /* always locked */
11878 di
= find_var(lv
.ll_name
, NULL
);
11881 /* Consider a variable locked when:
11882 * 1. the variable itself is locked
11883 * 2. the value of the variable is locked.
11884 * 3. the List or Dict value is locked.
11886 rettv
->vval
.v_number
= ((di
->di_flags
& DI_FLAGS_LOCK
)
11887 || tv_islocked(&di
->di_tv
));
11891 else if (lv
.ll_range
)
11892 EMSG(_("E786: Range not allowed"));
11893 else if (lv
.ll_newkey
!= NULL
)
11894 EMSG2(_(e_dictkey
), lv
.ll_newkey
);
11895 else if (lv
.ll_list
!= NULL
)
11897 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_li
->li_tv
);
11899 /* Dictionary item. */
11900 rettv
->vval
.v_number
= tv_islocked(&lv
.ll_di
->di_tv
);
11907 static void dict_list
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int what
));
11910 * Turn a dict into a list:
11911 * "what" == 0: list of keys
11912 * "what" == 1: list of values
11913 * "what" == 2: list of items
11916 dict_list(argvars
, rettv
, what
)
11929 rettv
->vval
.v_number
= 0;
11930 if (argvars
[0].v_type
!= VAR_DICT
)
11932 EMSG(_(e_dictreq
));
11935 if ((d
= argvars
[0].vval
.v_dict
) == NULL
)
11938 if (rettv_list_alloc(rettv
) == FAIL
)
11941 todo
= (int)d
->dv_hashtab
.ht_used
;
11942 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
11944 if (!HASHITEM_EMPTY(hi
))
11949 li
= listitem_alloc();
11952 list_append(rettv
->vval
.v_list
, li
);
11957 li
->li_tv
.v_type
= VAR_STRING
;
11958 li
->li_tv
.v_lock
= 0;
11959 li
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
11961 else if (what
== 1)
11964 copy_tv(&di
->di_tv
, &li
->li_tv
);
11970 li
->li_tv
.v_type
= VAR_LIST
;
11971 li
->li_tv
.v_lock
= 0;
11972 li
->li_tv
.vval
.v_list
= l2
;
11977 li2
= listitem_alloc();
11980 list_append(l2
, li2
);
11981 li2
->li_tv
.v_type
= VAR_STRING
;
11982 li2
->li_tv
.v_lock
= 0;
11983 li2
->li_tv
.vval
.v_string
= vim_strsave(di
->di_key
);
11985 li2
= listitem_alloc();
11988 list_append(l2
, li2
);
11989 copy_tv(&di
->di_tv
, &li2
->li_tv
);
11996 * "items(dict)" function
11999 f_items(argvars
, rettv
)
12003 dict_list(argvars
, rettv
, 2);
12007 * "join()" function
12010 f_join(argvars
, rettv
)
12017 rettv
->vval
.v_number
= 0;
12018 if (argvars
[0].v_type
!= VAR_LIST
)
12020 EMSG(_(e_listreq
));
12023 if (argvars
[0].vval
.v_list
== NULL
)
12025 if (argvars
[1].v_type
== VAR_UNKNOWN
)
12026 sep
= (char_u
*)" ";
12028 sep
= get_tv_string_chk(&argvars
[1]);
12030 rettv
->v_type
= VAR_STRING
;
12034 ga_init2(&ga
, (int)sizeof(char), 80);
12035 list_join(&ga
, argvars
[0].vval
.v_list
, sep
, TRUE
, 0);
12036 ga_append(&ga
, NUL
);
12037 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
12040 rettv
->vval
.v_string
= NULL
;
12044 * "keys()" function
12047 f_keys(argvars
, rettv
)
12051 dict_list(argvars
, rettv
, 0);
12055 * "last_buffer_nr()" function.
12059 f_last_buffer_nr(argvars
, rettv
)
12066 for (buf
= firstbuf
; buf
!= NULL
; buf
= buf
->b_next
)
12067 if (n
< buf
->b_fnum
)
12070 rettv
->vval
.v_number
= n
;
12077 f_len(argvars
, rettv
)
12081 switch (argvars
[0].v_type
)
12085 rettv
->vval
.v_number
= (varnumber_T
)STRLEN(
12086 get_tv_string(&argvars
[0]));
12089 rettv
->vval
.v_number
= list_len(argvars
[0].vval
.v_list
);
12092 rettv
->vval
.v_number
= dict_len(argvars
[0].vval
.v_dict
);
12095 EMSG(_("E701: Invalid type for len()"));
12100 static void libcall_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int type
));
12103 libcall_common(argvars
, rettv
, type
)
12108 #ifdef FEAT_LIBCALL
12110 char_u
**string_result
;
12114 rettv
->v_type
= type
;
12115 if (type
== VAR_NUMBER
)
12116 rettv
->vval
.v_number
= 0;
12118 rettv
->vval
.v_string
= NULL
;
12120 if (check_restricted() || check_secure())
12123 #ifdef FEAT_LIBCALL
12124 /* The first two args must be strings, otherwise its meaningless */
12125 if (argvars
[0].v_type
== VAR_STRING
&& argvars
[1].v_type
== VAR_STRING
)
12128 if (argvars
[2].v_type
== VAR_STRING
)
12129 string_in
= argvars
[2].vval
.v_string
;
12130 if (type
== VAR_NUMBER
)
12131 string_result
= NULL
;
12133 string_result
= &rettv
->vval
.v_string
;
12134 if (mch_libcall(argvars
[0].vval
.v_string
,
12135 argvars
[1].vval
.v_string
,
12137 argvars
[2].vval
.v_number
,
12140 && type
== VAR_NUMBER
)
12141 rettv
->vval
.v_number
= nr_result
;
12147 * "libcall()" function
12150 f_libcall(argvars
, rettv
)
12154 libcall_common(argvars
, rettv
, VAR_STRING
);
12158 * "libcallnr()" function
12161 f_libcallnr(argvars
, rettv
)
12165 libcall_common(argvars
, rettv
, VAR_NUMBER
);
12169 * "line(string)" function
12172 f_line(argvars
, rettv
)
12180 fp
= var2fpos(&argvars
[0], TRUE
, &fnum
);
12183 rettv
->vval
.v_number
= lnum
;
12187 * "line2byte(lnum)" function
12191 f_line2byte(argvars
, rettv
)
12195 #ifndef FEAT_BYTEOFF
12196 rettv
->vval
.v_number
= -1;
12200 lnum
= get_tv_lnum(argvars
);
12201 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
12202 rettv
->vval
.v_number
= -1;
12204 rettv
->vval
.v_number
= ml_find_line_or_offset(curbuf
, lnum
, NULL
);
12205 if (rettv
->vval
.v_number
>= 0)
12206 ++rettv
->vval
.v_number
;
12211 * "lispindent(lnum)" function
12214 f_lispindent(argvars
, rettv
)
12222 pos
= curwin
->w_cursor
;
12223 lnum
= get_tv_lnum(argvars
);
12224 if (lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
)
12226 curwin
->w_cursor
.lnum
= lnum
;
12227 rettv
->vval
.v_number
= get_lisp_indent();
12228 curwin
->w_cursor
= pos
;
12232 rettv
->vval
.v_number
= -1;
12236 * "localtime()" function
12240 f_localtime(argvars
, rettv
)
12244 rettv
->vval
.v_number
= (varnumber_T
)time(NULL
);
12247 static void get_maparg
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int exact
));
12250 get_maparg(argvars
, rettv
, exact
)
12257 char_u buf
[NUMBUFLEN
];
12258 char_u
*keys_buf
= NULL
;
12264 /* return empty string for failure */
12265 rettv
->v_type
= VAR_STRING
;
12266 rettv
->vval
.v_string
= NULL
;
12268 keys
= get_tv_string(&argvars
[0]);
12272 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12274 which
= get_tv_string_buf_chk(&argvars
[1], buf
);
12275 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12276 abbr
= get_tv_number(&argvars
[2]);
12279 which
= (char_u
*)"";
12283 mode
= get_map_mode(&which
, 0);
12285 keys
= replace_termcodes(keys
, &keys_buf
, TRUE
, TRUE
, FALSE
);
12286 rhs
= check_map(keys
, mode
, exact
, FALSE
, abbr
);
12287 vim_free(keys_buf
);
12291 ga
.ga_itemsize
= 1;
12292 ga
.ga_growsize
= 40;
12294 while (*rhs
!= NUL
)
12295 ga_concat(&ga
, str2special(&rhs
, FALSE
));
12297 ga_append(&ga
, NUL
);
12298 rettv
->vval
.v_string
= (char_u
*)ga
.ga_data
;
12306 f_map(argvars
, rettv
)
12310 filter_map(argvars
, rettv
, TRUE
);
12314 * "maparg()" function
12317 f_maparg(argvars
, rettv
)
12321 get_maparg(argvars
, rettv
, TRUE
);
12325 * "mapcheck()" function
12328 f_mapcheck(argvars
, rettv
)
12332 get_maparg(argvars
, rettv
, FALSE
);
12335 static void find_some_match
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int start
));
12338 find_some_match(argvars
, rettv
, type
)
12343 char_u
*str
= NULL
;
12344 char_u
*expr
= NULL
;
12346 regmatch_T regmatch
;
12347 char_u patbuf
[NUMBUFLEN
];
12348 char_u strbuf
[NUMBUFLEN
];
12352 colnr_T startcol
= 0;
12355 listitem_T
*li
= NULL
;
12357 char_u
*tofree
= NULL
;
12359 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12361 p_cpo
= (char_u
*)"";
12363 rettv
->vval
.v_number
= -1;
12366 /* return empty list when there are no matches */
12367 if (rettv_list_alloc(rettv
) == FAIL
)
12370 else if (type
== 2)
12372 rettv
->v_type
= VAR_STRING
;
12373 rettv
->vval
.v_string
= NULL
;
12376 if (argvars
[0].v_type
== VAR_LIST
)
12378 if ((l
= argvars
[0].vval
.v_list
) == NULL
)
12383 expr
= str
= get_tv_string(&argvars
[0]);
12385 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
12389 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12393 start
= get_tv_number_chk(&argvars
[2], &error
);
12398 li
= list_find(l
, start
);
12401 idx
= l
->lv_idx
; /* use the cached index */
12407 if (start
> (long)STRLEN(str
))
12409 /* When "count" argument is there ignore matches before "start",
12410 * otherwise skip part of the string. Differs when pattern is "^"
12412 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
12418 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
12419 nth
= get_tv_number_chk(&argvars
[3], &error
);
12424 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
12425 if (regmatch
.regprog
!= NULL
)
12427 regmatch
.rm_ic
= p_ic
;
12439 str
= echo_string(&li
->li_tv
, &tofree
, strbuf
, 0);
12444 match
= vim_regexec_nl(®match
, str
, (colnr_T
)startcol
);
12446 if (match
&& --nth
<= 0)
12448 if (l
== NULL
&& !match
)
12451 /* Advance to just after the match. */
12460 startcol
= (colnr_T
)(regmatch
.startp
[0]
12461 + (*mb_ptr2len
)(regmatch
.startp
[0]) - str
);
12463 startcol
= regmatch
.startp
[0] + 1 - str
;
12474 /* return list with matched string and submatches */
12475 for (i
= 0; i
< NSUBEXP
; ++i
)
12477 if (regmatch
.endp
[i
] == NULL
)
12479 if (list_append_string(rettv
->vval
.v_list
,
12480 (char_u
*)"", 0) == FAIL
)
12483 else if (list_append_string(rettv
->vval
.v_list
,
12484 regmatch
.startp
[i
],
12485 (int)(regmatch
.endp
[i
] - regmatch
.startp
[i
]))
12490 else if (type
== 2)
12492 /* return matched string */
12494 copy_tv(&li
->li_tv
, rettv
);
12496 rettv
->vval
.v_string
= vim_strnsave(regmatch
.startp
[0],
12497 (int)(regmatch
.endp
[0] - regmatch
.startp
[0]));
12499 else if (l
!= NULL
)
12500 rettv
->vval
.v_number
= idx
;
12504 rettv
->vval
.v_number
=
12505 (varnumber_T
)(regmatch
.startp
[0] - str
);
12507 rettv
->vval
.v_number
=
12508 (varnumber_T
)(regmatch
.endp
[0] - str
);
12509 rettv
->vval
.v_number
+= (varnumber_T
)(str
- expr
);
12512 vim_free(regmatch
.regprog
);
12521 * "match()" function
12524 f_match(argvars
, rettv
)
12528 find_some_match(argvars
, rettv
, 1);
12532 * "matchadd()" function
12535 f_matchadd(argvars
, rettv
)
12539 #ifdef FEAT_SEARCH_EXTRA
12540 char_u buf
[NUMBUFLEN
];
12541 char_u
*grp
= get_tv_string_buf_chk(&argvars
[0], buf
); /* group */
12542 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], buf
); /* pattern */
12543 int prio
= 10; /* default priority */
12547 rettv
->vval
.v_number
= -1;
12549 if (grp
== NULL
|| pat
== NULL
)
12551 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12553 prio
= get_tv_number_chk(&argvars
[2], &error
);
12554 if (argvars
[3].v_type
!= VAR_UNKNOWN
)
12555 id
= get_tv_number_chk(&argvars
[3], &error
);
12559 if (id
>= 1 && id
<= 3)
12561 EMSGN("E798: ID is reserved for \":match\": %ld", id
);
12565 rettv
->vval
.v_number
= match_add(curwin
, grp
, pat
, prio
, id
);
12570 * "matcharg()" function
12573 f_matcharg(argvars
, rettv
)
12577 if (rettv_list_alloc(rettv
) == OK
)
12579 #ifdef FEAT_SEARCH_EXTRA
12580 int id
= get_tv_number(&argvars
[0]);
12583 if (id
>= 1 && id
<= 3)
12585 if ((m
= (matchitem_T
*)get_match(curwin
, id
)) != NULL
)
12587 list_append_string(rettv
->vval
.v_list
,
12588 syn_id2name(m
->hlg_id
), -1);
12589 list_append_string(rettv
->vval
.v_list
, m
->pattern
, -1);
12593 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
12594 list_append_string(rettv
->vval
.v_list
, NUL
, -1);
12602 * "matchdelete()" function
12605 f_matchdelete(argvars
, rettv
)
12609 #ifdef FEAT_SEARCH_EXTRA
12610 rettv
->vval
.v_number
= match_delete(curwin
,
12611 (int)get_tv_number(&argvars
[0]), TRUE
);
12616 * "matchend()" function
12619 f_matchend(argvars
, rettv
)
12623 find_some_match(argvars
, rettv
, 0);
12627 * "matchlist()" function
12630 f_matchlist(argvars
, rettv
)
12634 find_some_match(argvars
, rettv
, 3);
12638 * "matchstr()" function
12641 f_matchstr(argvars
, rettv
)
12645 find_some_match(argvars
, rettv
, 2);
12648 static void max_min
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int domax
));
12651 max_min(argvars
, rettv
, domax
)
12660 if (argvars
[0].v_type
== VAR_LIST
)
12665 l
= argvars
[0].vval
.v_list
;
12671 n
= get_tv_number_chk(&li
->li_tv
, &error
);
12677 i
= get_tv_number_chk(&li
->li_tv
, &error
);
12678 if (domax
? i
> n
: i
< n
)
12684 else if (argvars
[0].v_type
== VAR_DICT
)
12691 d
= argvars
[0].vval
.v_dict
;
12694 todo
= (int)d
->dv_hashtab
.ht_used
;
12695 for (hi
= d
->dv_hashtab
.ht_array
; todo
> 0; ++hi
)
12697 if (!HASHITEM_EMPTY(hi
))
12700 i
= get_tv_number_chk(&HI2DI(hi
)->di_tv
, &error
);
12706 else if (domax
? i
> n
: i
< n
)
12713 EMSG(_(e_listdictarg
));
12714 rettv
->vval
.v_number
= error
? 0 : n
;
12721 f_max(argvars
, rettv
)
12725 max_min(argvars
, rettv
, TRUE
);
12732 f_min(argvars
, rettv
)
12736 max_min(argvars
, rettv
, FALSE
);
12739 static int mkdir_recurse
__ARGS((char_u
*dir
, int prot
));
12742 * Create the directory in which "dir" is located, and higher levels when
12746 mkdir_recurse(dir
, prot
)
12754 /* Get end of directory name in "dir".
12755 * We're done when it's "/" or "c:/". */
12756 p
= gettail_sep(dir
);
12757 if (p
<= get_past_head(dir
))
12760 /* If the directory exists we're done. Otherwise: create it.*/
12761 updir
= vim_strnsave(dir
, (int)(p
- dir
));
12764 if (mch_isdir(updir
))
12766 else if (mkdir_recurse(updir
, prot
) == OK
)
12767 r
= vim_mkdir_emsg(updir
, prot
);
12774 * "mkdir()" function
12777 f_mkdir(argvars
, rettv
)
12782 char_u buf
[NUMBUFLEN
];
12785 rettv
->vval
.v_number
= FAIL
;
12786 if (check_restricted() || check_secure())
12789 dir
= get_tv_string_buf(&argvars
[0], buf
);
12790 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
12792 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
12793 prot
= get_tv_number_chk(&argvars
[2], NULL
);
12794 if (prot
!= -1 && STRCMP(get_tv_string(&argvars
[1]), "p") == 0)
12795 mkdir_recurse(dir
, prot
);
12797 rettv
->vval
.v_number
= prot
!= -1 ? vim_mkdir_emsg(dir
, prot
) : 0;
12802 * "mode()" function
12806 f_mode(argvars
, rettv
)
12816 buf
[0] = VIsual_mode
+ 's' - 'v';
12818 buf
[0] = VIsual_mode
;
12822 if (State
== HITRETURN
|| State
== ASKMORE
|| State
== SETWSIZE
)
12824 else if (State
& INSERT
)
12826 if (State
& REPLACE_FLAG
)
12831 else if (State
& CMDLINE
)
12837 rettv
->vval
.v_string
= vim_strsave(buf
);
12838 rettv
->v_type
= VAR_STRING
;
12842 * "nextnonblank()" function
12845 f_nextnonblank(argvars
, rettv
)
12851 for (lnum
= get_tv_lnum(argvars
); ; ++lnum
)
12853 if (lnum
< 0 || lnum
> curbuf
->b_ml
.ml_line_count
)
12858 if (*skipwhite(ml_get(lnum
)) != NUL
)
12861 rettv
->vval
.v_number
= lnum
;
12865 * "nr2char()" function
12868 f_nr2char(argvars
, rettv
)
12872 char_u buf
[NUMBUFLEN
];
12876 buf
[(*mb_char2bytes
)((int)get_tv_number(&argvars
[0]), buf
)] = NUL
;
12880 buf
[0] = (char_u
)get_tv_number(&argvars
[0]);
12883 rettv
->v_type
= VAR_STRING
;
12884 rettv
->vval
.v_string
= vim_strsave(buf
);
12888 * "pathshorten()" function
12891 f_pathshorten(argvars
, rettv
)
12897 rettv
->v_type
= VAR_STRING
;
12898 p
= get_tv_string_chk(&argvars
[0]);
12900 rettv
->vval
.v_string
= NULL
;
12903 p
= vim_strsave(p
);
12904 rettv
->vval
.v_string
= p
;
12911 * "prevnonblank()" function
12914 f_prevnonblank(argvars
, rettv
)
12920 lnum
= get_tv_lnum(argvars
);
12921 if (lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
)
12924 while (lnum
>= 1 && *skipwhite(ml_get(lnum
)) == NUL
)
12926 rettv
->vval
.v_number
= lnum
;
12929 #ifdef HAVE_STDARG_H
12930 /* This dummy va_list is here because:
12931 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12932 * - locally in the function results in a "used before set" warning
12933 * - using va_start() to initialize it gives "function with fixed args" error */
12938 * "printf()" function
12941 f_printf(argvars
, rettv
)
12945 rettv
->v_type
= VAR_STRING
;
12946 rettv
->vval
.v_string
= NULL
;
12947 #ifdef HAVE_STDARG_H /* only very old compilers can't do this */
12949 char_u buf
[NUMBUFLEN
];
12952 int saved_did_emsg
= did_emsg
;
12955 /* Get the required length, allocate the buffer and do it for real. */
12957 fmt
= (char *)get_tv_string_buf(&argvars
[0], buf
);
12958 len
= vim_vsnprintf(NULL
, 0, fmt
, ap
, argvars
+ 1);
12961 s
= alloc(len
+ 1);
12964 rettv
->vval
.v_string
= s
;
12965 (void)vim_vsnprintf((char *)s
, len
+ 1, fmt
, ap
, argvars
+ 1);
12968 did_emsg
|= saved_did_emsg
;
12974 * "pumvisible()" function
12978 f_pumvisible(argvars
, rettv
)
12982 rettv
->vval
.v_number
= 0;
12983 #ifdef FEAT_INS_EXPAND
12985 rettv
->vval
.v_number
= 1;
12990 * "range()" function
12993 f_range(argvars
, rettv
)
13003 start
= get_tv_number_chk(&argvars
[0], &error
);
13004 if (argvars
[1].v_type
== VAR_UNKNOWN
)
13011 end
= get_tv_number_chk(&argvars
[1], &error
);
13012 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13013 stride
= get_tv_number_chk(&argvars
[2], &error
);
13016 rettv
->vval
.v_number
= 0;
13018 return; /* type error; errmsg already given */
13020 EMSG(_("E726: Stride is zero"));
13021 else if (stride
> 0 ? end
+ 1 < start
: end
- 1 > start
)
13022 EMSG(_("E727: Start past end"));
13025 if (rettv_list_alloc(rettv
) == OK
)
13026 for (i
= start
; stride
> 0 ? i
<= end
: i
>= end
; i
+= stride
)
13027 if (list_append_number(rettv
->vval
.v_list
,
13028 (varnumber_T
)i
) == FAIL
)
13034 * "readfile()" function
13037 f_readfile(argvars
, rettv
)
13041 int binary
= FALSE
;
13045 #define FREAD_SIZE 200 /* optimized for text lines */
13046 char_u buf
[FREAD_SIZE
];
13047 int readlen
; /* size of last fread() */
13048 int buflen
; /* nr of valid chars in buf[] */
13049 int filtd
; /* how much in buf[] was NUL -> '\n' filtered */
13050 int tolist
; /* first byte in buf[] still to be put in list */
13051 int chop
; /* how many CR to chop off */
13052 char_u
*prev
= NULL
; /* previously read bytes, if any */
13053 int prevlen
= 0; /* length of "prev" if not NULL */
13056 long maxline
= MAXLNUM
;
13059 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
13061 if (STRCMP(get_tv_string(&argvars
[1]), "b") == 0)
13063 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13064 maxline
= get_tv_number(&argvars
[2]);
13067 if (rettv_list_alloc(rettv
) == FAIL
)
13070 /* Always open the file in binary mode, library functions have a mind of
13071 * their own about CR-LF conversion. */
13072 fname
= get_tv_string(&argvars
[0]);
13073 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, READBIN
)) == NULL
)
13075 EMSG2(_(e_notopen
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
13080 while (cnt
< maxline
|| maxline
< 0)
13082 readlen
= (int)fread(buf
+ filtd
, 1, FREAD_SIZE
- filtd
, fd
);
13083 buflen
= filtd
+ readlen
;
13085 for ( ; filtd
< buflen
|| readlen
<= 0; ++filtd
)
13087 if (buf
[filtd
] == '\n' || readlen
<= 0)
13089 /* Only when in binary mode add an empty list item when the
13090 * last line ends in a '\n'. */
13091 if (!binary
&& readlen
== 0 && filtd
== 0)
13094 /* Found end-of-line or end-of-file: add a text line to the
13098 while (filtd
- chop
- 1 >= tolist
13099 && buf
[filtd
- chop
- 1] == '\r')
13101 len
= filtd
- tolist
- chop
;
13103 s
= vim_strnsave(buf
+ tolist
, len
);
13106 s
= alloc((unsigned)(prevlen
+ len
+ 1));
13109 mch_memmove(s
, prev
, prevlen
);
13112 mch_memmove(s
+ prevlen
, buf
+ tolist
, len
);
13113 s
[prevlen
+ len
] = NUL
;
13116 tolist
= filtd
+ 1;
13118 li
= listitem_alloc();
13124 li
->li_tv
.v_type
= VAR_STRING
;
13125 li
->li_tv
.v_lock
= 0;
13126 li
->li_tv
.vval
.v_string
= s
;
13127 list_append(rettv
->vval
.v_list
, li
);
13129 if (++cnt
>= maxline
&& maxline
>= 0)
13134 else if (buf
[filtd
] == NUL
)
13142 /* "buf" is full, need to move text to an allocated buffer */
13145 prev
= vim_strnsave(buf
, buflen
);
13150 s
= alloc((unsigned)(prevlen
+ buflen
));
13153 mch_memmove(s
, prev
, prevlen
);
13154 mch_memmove(s
+ prevlen
, buf
, buflen
);
13164 mch_memmove(buf
, buf
+ tolist
, buflen
- tolist
);
13170 * For a negative line count use only the lines at the end of the file,
13174 while (cnt
> -maxline
)
13176 listitem_remove(rettv
->vval
.v_list
, rettv
->vval
.v_list
->lv_first
);
13184 #if defined(FEAT_RELTIME)
13185 static int list2proftime
__ARGS((typval_T
*arg
, proftime_T
*tm
));
13188 * Convert a List to proftime_T.
13189 * Return FAIL when there is something wrong.
13192 list2proftime(arg
, tm
)
13199 if (arg
->v_type
!= VAR_LIST
|| arg
->vval
.v_list
== NULL
13200 || arg
->vval
.v_list
->lv_len
!= 2)
13202 n1
= list_find_nr(arg
->vval
.v_list
, 0L, &error
);
13203 n2
= list_find_nr(arg
->vval
.v_list
, 1L, &error
);
13211 return error
? FAIL
: OK
;
13213 #endif /* FEAT_RELTIME */
13216 * "reltime()" function
13219 f_reltime(argvars
, rettv
)
13223 #ifdef FEAT_RELTIME
13227 if (argvars
[0].v_type
== VAR_UNKNOWN
)
13229 /* No arguments: get current time. */
13230 profile_start(&res
);
13232 else if (argvars
[1].v_type
== VAR_UNKNOWN
)
13234 if (list2proftime(&argvars
[0], &res
) == FAIL
)
13240 /* Two arguments: compute the difference. */
13241 if (list2proftime(&argvars
[0], &start
) == FAIL
13242 || list2proftime(&argvars
[1], &res
) == FAIL
)
13244 profile_sub(&res
, &start
);
13247 if (rettv_list_alloc(rettv
) == OK
)
13258 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n1
);
13259 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n2
);
13265 * "reltimestr()" function
13268 f_reltimestr(argvars
, rettv
)
13272 #ifdef FEAT_RELTIME
13276 rettv
->v_type
= VAR_STRING
;
13277 rettv
->vval
.v_string
= NULL
;
13278 #ifdef FEAT_RELTIME
13279 if (list2proftime(&argvars
[0], &tm
) == OK
)
13280 rettv
->vval
.v_string
= vim_strsave((char_u
*)profile_msg(&tm
));
13284 #if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
13285 static void make_connection
__ARGS((void));
13286 static int check_connection
__ARGS((void));
13291 if (X_DISPLAY
== NULL
13297 x_force_connect
= TRUE
;
13299 x_force_connect
= FALSE
;
13307 if (X_DISPLAY
== NULL
)
13309 EMSG(_("E240: No connection to Vim server"));
13316 #ifdef FEAT_CLIENTSERVER
13317 static void remote_common
__ARGS((typval_T
*argvars
, typval_T
*rettv
, int expr
));
13320 remote_common(argvars
, rettv
, expr
)
13325 char_u
*server_name
;
13328 char_u buf
[NUMBUFLEN
];
13335 if (check_restricted() || check_secure())
13339 if (check_connection() == FAIL
)
13343 server_name
= get_tv_string_chk(&argvars
[0]);
13344 if (server_name
== NULL
)
13345 return; /* type error; errmsg already given */
13346 keys
= get_tv_string_buf(&argvars
[1], buf
);
13348 if (serverSendToVim(server_name
, keys
, &r
, &w
, expr
, TRUE
) < 0)
13350 if (serverSendToVim(X_DISPLAY
, server_name
, keys
, &r
, &w
, expr
, 0, TRUE
)
13355 EMSG(r
); /* sending worked but evaluation failed */
13357 EMSG2(_("E241: Unable to send to %s"), server_name
);
13361 rettv
->vval
.v_string
= r
;
13363 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13369 sprintf((char *)str
, PRINTF_HEX_LONG_U
, (long_u
)w
);
13370 v
.di_tv
.v_type
= VAR_STRING
;
13371 v
.di_tv
.vval
.v_string
= vim_strsave(str
);
13372 idvar
= get_tv_string_chk(&argvars
[2]);
13374 set_var(idvar
, &v
.di_tv
, FALSE
);
13375 vim_free(v
.di_tv
.vval
.v_string
);
13381 * "remote_expr()" function
13385 f_remote_expr(argvars
, rettv
)
13389 rettv
->v_type
= VAR_STRING
;
13390 rettv
->vval
.v_string
= NULL
;
13391 #ifdef FEAT_CLIENTSERVER
13392 remote_common(argvars
, rettv
, TRUE
);
13397 * "remote_foreground()" function
13401 f_remote_foreground(argvars
, rettv
)
13405 rettv
->vval
.v_number
= 0;
13406 #ifdef FEAT_CLIENTSERVER
13408 /* On Win32 it's done in this application. */
13410 char_u
*server_name
= get_tv_string_chk(&argvars
[0]);
13412 if (server_name
!= NULL
)
13413 serverForeground(server_name
);
13416 /* Send a foreground() expression to the server. */
13417 argvars
[1].v_type
= VAR_STRING
;
13418 argvars
[1].vval
.v_string
= vim_strsave((char_u
*)"foreground()");
13419 argvars
[2].v_type
= VAR_UNKNOWN
;
13420 remote_common(argvars
, rettv
, TRUE
);
13421 vim_free(argvars
[1].vval
.v_string
);
13428 f_remote_peek(argvars
, rettv
)
13432 #ifdef FEAT_CLIENTSERVER
13440 if (check_restricted() || check_secure())
13442 rettv
->vval
.v_number
= -1;
13445 serverid
= get_tv_string_chk(&argvars
[0]);
13446 if (serverid
== NULL
)
13448 rettv
->vval
.v_number
= -1;
13449 return; /* type error; errmsg already given */
13452 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
13454 rettv
->vval
.v_number
= -1;
13457 s
= serverGetReply((HWND
)n
, FALSE
, FALSE
, FALSE
);
13458 rettv
->vval
.v_number
= (s
!= NULL
);
13461 rettv
->vval
.v_number
= 0;
13462 if (check_connection() == FAIL
)
13465 rettv
->vval
.v_number
= serverPeekReply(X_DISPLAY
,
13466 serverStrToWin(serverid
), &s
);
13469 if (argvars
[1].v_type
!= VAR_UNKNOWN
&& rettv
->vval
.v_number
> 0)
13473 v
.di_tv
.v_type
= VAR_STRING
;
13474 v
.di_tv
.vval
.v_string
= vim_strsave(s
);
13475 retvar
= get_tv_string_chk(&argvars
[1]);
13476 if (retvar
!= NULL
)
13477 set_var(retvar
, &v
.di_tv
, FALSE
);
13478 vim_free(v
.di_tv
.vval
.v_string
);
13481 rettv
->vval
.v_number
= -1;
13487 f_remote_read(argvars
, rettv
)
13493 #ifdef FEAT_CLIENTSERVER
13494 char_u
*serverid
= get_tv_string_chk(&argvars
[0]);
13496 if (serverid
!= NULL
&& !check_restricted() && !check_secure())
13499 /* The server's HWND is encoded in the 'id' parameter */
13502 sscanf(serverid
, SCANF_HEX_LONG_U
, &n
);
13504 r
= serverGetReply((HWND
)n
, FALSE
, TRUE
, TRUE
);
13507 if (check_connection() == FAIL
|| serverReadReply(X_DISPLAY
,
13508 serverStrToWin(serverid
), &r
, FALSE
) < 0)
13510 EMSG(_("E277: Unable to read a server reply"));
13513 rettv
->v_type
= VAR_STRING
;
13514 rettv
->vval
.v_string
= r
;
13518 * "remote_send()" function
13522 f_remote_send(argvars
, rettv
)
13526 rettv
->v_type
= VAR_STRING
;
13527 rettv
->vval
.v_string
= NULL
;
13528 #ifdef FEAT_CLIENTSERVER
13529 remote_common(argvars
, rettv
, FALSE
);
13534 * "remove()" function
13537 f_remove(argvars
, rettv
)
13542 listitem_T
*item
, *item2
;
13550 rettv
->vval
.v_number
= 0;
13551 if (argvars
[0].v_type
== VAR_DICT
)
13553 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
13554 EMSG2(_(e_toomanyarg
), "remove()");
13555 else if ((d
= argvars
[0].vval
.v_dict
) != NULL
13556 && !tv_check_lock(d
->dv_lock
, (char_u
*)"remove() argument"))
13558 key
= get_tv_string_chk(&argvars
[1]);
13561 di
= dict_find(d
, key
, -1);
13563 EMSG2(_(e_dictkey
), key
);
13566 *rettv
= di
->di_tv
;
13567 init_tv(&di
->di_tv
);
13568 dictitem_remove(d
, di
);
13573 else if (argvars
[0].v_type
!= VAR_LIST
)
13574 EMSG2(_(e_listdictarg
), "remove()");
13575 else if ((l
= argvars
[0].vval
.v_list
) != NULL
13576 && !tv_check_lock(l
->lv_lock
, (char_u
*)"remove() argument"))
13580 idx
= get_tv_number_chk(&argvars
[1], &error
);
13582 ; /* type error: do nothing, errmsg already given */
13583 else if ((item
= list_find(l
, idx
)) == NULL
)
13584 EMSGN(_(e_listidx
), idx
);
13587 if (argvars
[2].v_type
== VAR_UNKNOWN
)
13589 /* Remove one item, return its value. */
13590 list_remove(l
, item
, item
);
13591 *rettv
= item
->li_tv
;
13596 /* Remove range of items, return list with values. */
13597 end
= get_tv_number_chk(&argvars
[2], &error
);
13599 ; /* type error: do nothing */
13600 else if ((item2
= list_find(l
, end
)) == NULL
)
13601 EMSGN(_(e_listidx
), end
);
13606 for (li
= item
; li
!= NULL
; li
= li
->li_next
)
13612 if (li
== NULL
) /* didn't find "item2" after "item" */
13613 EMSG(_(e_invrange
));
13616 list_remove(l
, item
, item2
);
13617 if (rettv_list_alloc(rettv
) == OK
)
13619 l
= rettv
->vval
.v_list
;
13620 l
->lv_first
= item
;
13621 l
->lv_last
= item2
;
13622 item
->li_prev
= NULL
;
13623 item2
->li_next
= NULL
;
13634 * "rename({from}, {to})" function
13637 f_rename(argvars
, rettv
)
13641 char_u buf
[NUMBUFLEN
];
13643 if (check_restricted() || check_secure())
13644 rettv
->vval
.v_number
= -1;
13646 rettv
->vval
.v_number
= vim_rename(get_tv_string(&argvars
[0]),
13647 get_tv_string_buf(&argvars
[1], buf
));
13651 * "repeat()" function
13655 f_repeat(argvars
, rettv
)
13666 n
= get_tv_number(&argvars
[1]);
13667 if (argvars
[0].v_type
== VAR_LIST
)
13669 if (rettv_list_alloc(rettv
) == OK
&& argvars
[0].vval
.v_list
!= NULL
)
13671 if (list_extend(rettv
->vval
.v_list
,
13672 argvars
[0].vval
.v_list
, NULL
) == FAIL
)
13677 p
= get_tv_string(&argvars
[0]);
13678 rettv
->v_type
= VAR_STRING
;
13679 rettv
->vval
.v_string
= NULL
;
13681 slen
= (int)STRLEN(p
);
13686 r
= alloc(len
+ 1);
13689 for (i
= 0; i
< n
; i
++)
13690 mch_memmove(r
+ i
* slen
, p
, (size_t)slen
);
13694 rettv
->vval
.v_string
= r
;
13699 * "resolve()" function
13702 f_resolve(argvars
, rettv
)
13708 p
= get_tv_string(&argvars
[0]);
13709 #ifdef FEAT_SHORTCUT
13713 v
= mch_resolve_shortcut(p
);
13715 rettv
->vval
.v_string
= v
;
13717 rettv
->vval
.v_string
= vim_strsave(p
);
13720 # ifdef HAVE_READLINK
13722 char_u buf
[MAXPATHL
+ 1];
13725 char_u
*remain
= NULL
;
13727 int is_relative_to_current
= FALSE
;
13728 int has_trailing_pathsep
= FALSE
;
13731 p
= vim_strsave(p
);
13733 if (p
[0] == '.' && (vim_ispathsep(p
[1])
13734 || (p
[1] == '.' && (vim_ispathsep(p
[2])))))
13735 is_relative_to_current
= TRUE
;
13738 if (len
> 0 && after_pathsep(p
, p
+ len
))
13739 has_trailing_pathsep
= TRUE
;
13741 q
= getnextcomp(p
);
13744 /* Separate the first path component in "p", and keep the
13745 * remainder (beginning with the path separator). */
13746 remain
= vim_strsave(q
- 1);
13754 len
= readlink((char *)p
, (char *)buf
, MAXPATHL
);
13763 EMSG(_("E655: Too many symbolic links (cycle?)"));
13764 rettv
->vval
.v_string
= NULL
;
13768 /* Ensure that the result will have a trailing path separator
13769 * if the argument has one. */
13770 if (remain
== NULL
&& has_trailing_pathsep
)
13773 /* Separate the first path component in the link value and
13774 * concatenate the remainders. */
13775 q
= getnextcomp(vim_ispathsep(*buf
) ? buf
+ 1 : buf
);
13778 if (remain
== NULL
)
13779 remain
= vim_strsave(q
- 1);
13782 cpy
= concat_str(q
- 1, remain
);
13793 if (q
> p
&& *q
== NUL
)
13795 /* Ignore trailing path separator. */
13799 if (q
> p
&& !mch_isFullName(buf
))
13801 /* symlink is relative to directory of argument */
13802 cpy
= alloc((unsigned)(STRLEN(p
) + STRLEN(buf
) + 1));
13806 STRCPY(gettail(cpy
), buf
);
13814 p
= vim_strsave(buf
);
13818 if (remain
== NULL
)
13821 /* Append the first path component of "remain" to "p". */
13822 q
= getnextcomp(remain
+ 1);
13823 len
= q
- remain
- (*q
!= NUL
);
13824 cpy
= vim_strnsave(p
, STRLEN(p
) + len
);
13827 STRNCAT(cpy
, remain
, len
);
13831 /* Shorten "remain". */
13833 mch_memmove(remain
, q
- 1, STRLEN(q
- 1) + 1);
13841 /* If the result is a relative path name, make it explicitly relative to
13842 * the current directory if and only if the argument had this form. */
13843 if (!vim_ispathsep(*p
))
13845 if (is_relative_to_current
13849 || vim_ispathsep(p
[1])
13852 || vim_ispathsep(p
[2]))))))
13854 /* Prepend "./". */
13855 cpy
= concat_str((char_u
*)"./", p
);
13862 else if (!is_relative_to_current
)
13864 /* Strip leading "./". */
13866 while (q
[0] == '.' && vim_ispathsep(q
[1]))
13869 mch_memmove(p
, p
+ 2, STRLEN(p
+ 2) + (size_t)1);
13873 /* Ensure that the result will have no trailing path separator
13874 * if the argument had none. But keep "/" or "//". */
13875 if (!has_trailing_pathsep
)
13878 if (after_pathsep(p
, q
))
13879 *gettail_sep(p
) = NUL
;
13882 rettv
->vval
.v_string
= p
;
13885 rettv
->vval
.v_string
= vim_strsave(p
);
13889 simplify_filename(rettv
->vval
.v_string
);
13891 #ifdef HAVE_READLINK
13894 rettv
->v_type
= VAR_STRING
;
13898 * "reverse({list})" function
13901 f_reverse(argvars
, rettv
)
13906 listitem_T
*li
, *ni
;
13908 rettv
->vval
.v_number
= 0;
13909 if (argvars
[0].v_type
!= VAR_LIST
)
13910 EMSG2(_(e_listarg
), "reverse()");
13911 else if ((l
= argvars
[0].vval
.v_list
) != NULL
13912 && !tv_check_lock(l
->lv_lock
, (char_u
*)"reverse()"))
13915 l
->lv_first
= l
->lv_last
= NULL
;
13920 list_append(l
, li
);
13923 rettv
->vval
.v_list
= l
;
13924 rettv
->v_type
= VAR_LIST
;
13929 #define SP_NOMOVE 0x01 /* don't move cursor */
13930 #define SP_REPEAT 0x02 /* repeat to find outer pair */
13931 #define SP_RETCOUNT 0x04 /* return matchcount */
13932 #define SP_SETPCMARK 0x08 /* set previous context mark */
13933 #define SP_START 0x10 /* accept match at start position */
13934 #define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
13935 #define SP_END 0x40 /* leave cursor at end of match */
13937 static int get_search_arg
__ARGS((typval_T
*varp
, int *flagsp
));
13940 * Get flags for a search function.
13941 * Possibly sets "p_ws".
13942 * Returns BACKWARD, FORWARD or zero (for an error).
13945 get_search_arg(varp
, flagsp
)
13951 char_u nbuf
[NUMBUFLEN
];
13954 if (varp
->v_type
!= VAR_UNKNOWN
)
13956 flags
= get_tv_string_buf_chk(varp
, nbuf
);
13958 return 0; /* type error; errmsg already given */
13959 while (*flags
!= NUL
)
13963 case 'b': dir
= BACKWARD
; break;
13964 case 'w': p_ws
= TRUE
; break;
13965 case 'W': p_ws
= FALSE
; break;
13967 if (flagsp
!= NULL
)
13970 case 'c': mask
= SP_START
; break;
13971 case 'e': mask
= SP_END
; break;
13972 case 'm': mask
= SP_RETCOUNT
; break;
13973 case 'n': mask
= SP_NOMOVE
; break;
13974 case 'p': mask
= SP_SUBPAT
; break;
13975 case 'r': mask
= SP_REPEAT
; break;
13976 case 's': mask
= SP_SETPCMARK
; break;
13980 EMSG2(_(e_invarg2
), flags
);
13995 * Shared by search() and searchpos() functions
13998 search_cmn(argvars
, match_pos
, flagsp
)
14007 int save_p_ws
= p_ws
;
14009 int retval
= 0; /* default: FAIL */
14010 long lnum_stop
= 0;
14011 int options
= SEARCH_KEEP
;
14014 pat
= get_tv_string(&argvars
[0]);
14015 dir
= get_search_arg(&argvars
[1], flagsp
); /* may set p_ws */
14019 if (flags
& SP_START
)
14020 options
|= SEARCH_START
;
14021 if (flags
& SP_END
)
14022 options
|= SEARCH_END
;
14024 /* Optional extra argument: line number to stop searching. */
14025 if (argvars
[1].v_type
!= VAR_UNKNOWN
14026 && argvars
[2].v_type
!= VAR_UNKNOWN
)
14028 lnum_stop
= get_tv_number_chk(&argvars
[2], NULL
);
14034 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
14035 * Check to make sure only those flags are set.
14036 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
14037 * flags cannot be set. Check for that condition also.
14039 if (((flags
& (SP_REPEAT
| SP_RETCOUNT
)) != 0)
14040 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
14042 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[1]));
14046 pos
= save_cursor
= curwin
->w_cursor
;
14047 subpatnum
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
14048 options
, RE_SEARCH
, (linenr_T
)lnum_stop
);
14049 if (subpatnum
!= FAIL
)
14051 if (flags
& SP_SUBPAT
)
14052 retval
= subpatnum
;
14055 if (flags
& SP_SETPCMARK
)
14057 curwin
->w_cursor
= pos
;
14058 if (match_pos
!= NULL
)
14060 /* Store the match cursor position */
14061 match_pos
->lnum
= pos
.lnum
;
14062 match_pos
->col
= pos
.col
+ 1;
14064 /* "/$" will put the cursor after the end of the line, may need to
14065 * correct that here */
14069 /* If 'n' flag is used: restore cursor position. */
14070 if (flags
& SP_NOMOVE
)
14071 curwin
->w_cursor
= save_cursor
;
14073 curwin
->w_set_curswant
= TRUE
;
14081 * "search()" function
14084 f_search(argvars
, rettv
)
14090 rettv
->vval
.v_number
= search_cmn(argvars
, NULL
, &flags
);
14094 * "searchdecl()" function
14097 f_searchdecl(argvars
, rettv
)
14106 rettv
->vval
.v_number
= 1; /* default: FAIL */
14108 name
= get_tv_string_chk(&argvars
[0]);
14109 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
14111 locally
= get_tv_number_chk(&argvars
[1], &error
) == 0;
14112 if (!error
&& argvars
[2].v_type
!= VAR_UNKNOWN
)
14113 thisblock
= get_tv_number_chk(&argvars
[2], &error
) != 0;
14115 if (!error
&& name
!= NULL
)
14116 rettv
->vval
.v_number
= find_decl(name
, (int)STRLEN(name
),
14117 locally
, thisblock
, SEARCH_KEEP
) == FAIL
;
14121 * Used by searchpair() and searchpairpos()
14124 searchpair_cmn(argvars
, match_pos
)
14128 char_u
*spat
, *mpat
, *epat
;
14130 int save_p_ws
= p_ws
;
14133 char_u nbuf1
[NUMBUFLEN
];
14134 char_u nbuf2
[NUMBUFLEN
];
14135 char_u nbuf3
[NUMBUFLEN
];
14136 int retval
= 0; /* default: FAIL */
14137 long lnum_stop
= 0;
14139 /* Get the three pattern arguments: start, middle, end. */
14140 spat
= get_tv_string_chk(&argvars
[0]);
14141 mpat
= get_tv_string_buf_chk(&argvars
[1], nbuf1
);
14142 epat
= get_tv_string_buf_chk(&argvars
[2], nbuf2
);
14143 if (spat
== NULL
|| mpat
== NULL
|| epat
== NULL
)
14144 goto theend
; /* type error */
14146 /* Handle the optional fourth argument: flags */
14147 dir
= get_search_arg(&argvars
[3], &flags
); /* may set p_ws */
14151 /* Don't accept SP_END or SP_SUBPAT.
14152 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
14154 if ((flags
& (SP_END
| SP_SUBPAT
)) != 0
14155 || ((flags
& SP_NOMOVE
) && (flags
& SP_SETPCMARK
)))
14157 EMSG2(_(e_invarg2
), get_tv_string(&argvars
[3]));
14161 /* Optional fifth argument: skip expression */
14162 if (argvars
[3].v_type
== VAR_UNKNOWN
14163 || argvars
[4].v_type
== VAR_UNKNOWN
)
14164 skip
= (char_u
*)"";
14167 skip
= get_tv_string_buf_chk(&argvars
[4], nbuf3
);
14168 if (argvars
[5].v_type
!= VAR_UNKNOWN
)
14170 lnum_stop
= get_tv_number_chk(&argvars
[5], NULL
);
14176 goto theend
; /* type error */
14178 retval
= do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
,
14179 match_pos
, lnum_stop
);
14188 * "searchpair()" function
14191 f_searchpair(argvars
, rettv
)
14195 rettv
->vval
.v_number
= searchpair_cmn(argvars
, NULL
);
14199 * "searchpairpos()" function
14202 f_searchpairpos(argvars
, rettv
)
14210 rettv
->vval
.v_number
= 0;
14212 if (rettv_list_alloc(rettv
) == FAIL
)
14215 if (searchpair_cmn(argvars
, &match_pos
) > 0)
14217 lnum
= match_pos
.lnum
;
14218 col
= match_pos
.col
;
14221 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
14222 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
14226 * Search for a start/middle/end thing.
14227 * Used by searchpair(), see its documentation for the details.
14228 * Returns 0 or -1 for no match,
14231 do_searchpair(spat
, mpat
, epat
, dir
, skip
, flags
, match_pos
, lnum_stop
)
14232 char_u
*spat
; /* start pattern */
14233 char_u
*mpat
; /* middle pattern */
14234 char_u
*epat
; /* end pattern */
14235 int dir
; /* BACKWARD or FORWARD */
14236 char_u
*skip
; /* skip expression */
14237 int flags
; /* SP_SETPCMARK and other SP_ values */
14239 linenr_T lnum_stop
; /* stop at this line if not zero */
14242 char_u
*pat
, *pat2
= NULL
, *pat3
= NULL
;
14253 int options
= SEARCH_KEEP
;
14255 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
14257 p_cpo
= (char_u
*)"";
14259 /* Make two search patterns: start/end (pat2, for in nested pairs) and
14260 * start/middle/end (pat3, for the top pair). */
14261 pat2
= alloc((unsigned)(STRLEN(spat
) + STRLEN(epat
) + 15));
14262 pat3
= alloc((unsigned)(STRLEN(spat
) + STRLEN(mpat
) + STRLEN(epat
) + 23));
14263 if (pat2
== NULL
|| pat3
== NULL
)
14265 sprintf((char *)pat2
, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat
, epat
);
14267 STRCPY(pat3
, pat2
);
14269 sprintf((char *)pat3
, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
14271 if (flags
& SP_START
)
14272 options
|= SEARCH_START
;
14274 save_cursor
= curwin
->w_cursor
;
14275 pos
= curwin
->w_cursor
;
14276 clearpos(&firstpos
);
14277 clearpos(&foundpos
);
14281 n
= searchit(curwin
, curbuf
, &pos
, dir
, pat
, 1L,
14282 options
, RE_SEARCH
, lnum_stop
);
14283 if (n
== FAIL
|| (firstpos
.lnum
!= 0 && equalpos(pos
, firstpos
)))
14284 /* didn't find it or found the first match again: FAIL */
14287 if (firstpos
.lnum
== 0)
14289 if (equalpos(pos
, foundpos
))
14291 /* Found the same position again. Can happen with a pattern that
14292 * has "\zs" at the end and searching backwards. Advance one
14293 * character and try again. */
14294 if (dir
== BACKWARD
)
14301 /* If the skip pattern matches, ignore this match. */
14304 save_pos
= curwin
->w_cursor
;
14305 curwin
->w_cursor
= pos
;
14306 r
= eval_to_bool(skip
, &err
, NULL
, FALSE
);
14307 curwin
->w_cursor
= save_pos
;
14310 /* Evaluating {skip} caused an error, break here. */
14311 curwin
->w_cursor
= save_cursor
;
14319 if ((dir
== BACKWARD
&& n
== 3) || (dir
== FORWARD
&& n
== 2))
14321 /* Found end when searching backwards or start when searching
14322 * forward: nested pair. */
14324 pat
= pat2
; /* nested, don't search for middle */
14328 /* Found end when searching forward or start when searching
14329 * backward: end of (nested) pair; or found middle in outer pair. */
14331 pat
= pat3
; /* outer level, search for middle */
14336 /* Found the match: return matchcount or line number. */
14337 if (flags
& SP_RETCOUNT
)
14341 if (flags
& SP_SETPCMARK
)
14343 curwin
->w_cursor
= pos
;
14344 if (!(flags
& SP_REPEAT
))
14346 nest
= 1; /* search for next unmatched */
14350 if (match_pos
!= NULL
)
14352 /* Store the match cursor position */
14353 match_pos
->lnum
= curwin
->w_cursor
.lnum
;
14354 match_pos
->col
= curwin
->w_cursor
.col
+ 1;
14357 /* If 'n' flag is used or search failed: restore cursor position. */
14358 if ((flags
& SP_NOMOVE
) || retval
== 0)
14359 curwin
->w_cursor
= save_cursor
;
14370 * "searchpos()" function
14373 f_searchpos(argvars
, rettv
)
14383 rettv
->vval
.v_number
= 0;
14385 if (rettv_list_alloc(rettv
) == FAIL
)
14388 n
= search_cmn(argvars
, &match_pos
, &flags
);
14391 lnum
= match_pos
.lnum
;
14392 col
= match_pos
.col
;
14395 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)lnum
);
14396 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)col
);
14397 if (flags
& SP_SUBPAT
)
14398 list_append_number(rettv
->vval
.v_list
, (varnumber_T
)n
);
14404 f_server2client(argvars
, rettv
)
14408 #ifdef FEAT_CLIENTSERVER
14409 char_u buf
[NUMBUFLEN
];
14410 char_u
*server
= get_tv_string_chk(&argvars
[0]);
14411 char_u
*reply
= get_tv_string_buf_chk(&argvars
[1], buf
);
14413 rettv
->vval
.v_number
= -1;
14414 if (server
== NULL
|| reply
== NULL
)
14416 if (check_restricted() || check_secure())
14419 if (check_connection() == FAIL
)
14423 if (serverSendReply(server
, reply
) < 0)
14425 EMSG(_("E258: Unable to send to client"));
14428 rettv
->vval
.v_number
= 0;
14430 rettv
->vval
.v_number
= -1;
14436 f_serverlist(argvars
, rettv
)
14442 #ifdef FEAT_CLIENTSERVER
14444 r
= serverGetVimNames();
14447 if (X_DISPLAY
!= NULL
)
14448 r
= serverGetVimNames(X_DISPLAY
);
14451 rettv
->v_type
= VAR_STRING
;
14452 rettv
->vval
.v_string
= r
;
14456 * "setbufvar()" function
14460 f_setbufvar(argvars
, rettv
)
14466 char_u
*varname
, *bufvarname
;
14468 char_u nbuf
[NUMBUFLEN
];
14470 rettv
->vval
.v_number
= 0;
14472 if (check_restricted() || check_secure())
14474 (void)get_tv_number(&argvars
[0]); /* issue errmsg if type error */
14475 varname
= get_tv_string_chk(&argvars
[1]);
14476 buf
= get_buf_tv(&argvars
[0]);
14477 varp
= &argvars
[2];
14479 if (buf
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
14481 /* set curbuf to be our buf, temporarily */
14482 aucmd_prepbuf(&aco
, buf
);
14484 if (*varname
== '&')
14491 numval
= get_tv_number_chk(varp
, &error
);
14492 strval
= get_tv_string_buf_chk(varp
, nbuf
);
14493 if (!error
&& strval
!= NULL
)
14494 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
14498 bufvarname
= alloc((unsigned)STRLEN(varname
) + 3);
14499 if (bufvarname
!= NULL
)
14501 STRCPY(bufvarname
, "b:");
14502 STRCPY(bufvarname
+ 2, varname
);
14503 set_var(bufvarname
, varp
, TRUE
);
14504 vim_free(bufvarname
);
14508 /* reset notion of buffer */
14509 aucmd_restbuf(&aco
);
14514 * "setcmdpos()" function
14517 f_setcmdpos(argvars
, rettv
)
14521 int pos
= (int)get_tv_number(&argvars
[0]) - 1;
14524 rettv
->vval
.v_number
= set_cmdline_pos(pos
);
14528 * "setline()" function
14531 f_setline(argvars
, rettv
)
14536 char_u
*line
= NULL
;
14538 listitem_T
*li
= NULL
;
14540 linenr_T lcount
= curbuf
->b_ml
.ml_line_count
;
14542 lnum
= get_tv_lnum(&argvars
[0]);
14543 if (argvars
[1].v_type
== VAR_LIST
)
14545 l
= argvars
[1].vval
.v_list
;
14549 line
= get_tv_string_chk(&argvars
[1]);
14551 rettv
->vval
.v_number
= 0; /* OK */
14556 /* list argument, get next string */
14559 line
= get_tv_string_chk(&li
->li_tv
);
14563 rettv
->vval
.v_number
= 1; /* FAIL */
14564 if (line
== NULL
|| lnum
< 1 || lnum
> curbuf
->b_ml
.ml_line_count
+ 1)
14566 if (lnum
<= curbuf
->b_ml
.ml_line_count
)
14568 /* existing line, replace it */
14569 if (u_savesub(lnum
) == OK
&& ml_replace(lnum
, line
, TRUE
) == OK
)
14571 changed_bytes(lnum
, 0);
14572 if (lnum
== curwin
->w_cursor
.lnum
)
14573 check_cursor_col();
14574 rettv
->vval
.v_number
= 0; /* OK */
14577 else if (added
> 0 || u_save(lnum
- 1, lnum
) == OK
)
14579 /* lnum is one past the last line, append the line */
14581 if (ml_append(lnum
- 1, line
, (colnr_T
)0, FALSE
) == OK
)
14582 rettv
->vval
.v_number
= 0; /* OK */
14585 if (l
== NULL
) /* only one string argument */
14591 appended_lines_mark(lcount
, added
);
14595 * Used by "setqflist()" and "setloclist()" functions
14599 set_qf_ll_list(wp
, list_arg
, action_arg
, rettv
)
14601 typval_T
*list_arg
;
14602 typval_T
*action_arg
;
14605 #ifdef FEAT_QUICKFIX
14610 rettv
->vval
.v_number
= -1;
14612 #ifdef FEAT_QUICKFIX
14613 if (list_arg
->v_type
!= VAR_LIST
)
14614 EMSG(_(e_listreq
));
14617 list_T
*l
= list_arg
->vval
.v_list
;
14619 if (action_arg
->v_type
== VAR_STRING
)
14621 act
= get_tv_string_chk(action_arg
);
14623 return; /* type error; errmsg already given */
14624 if (*act
== 'a' || *act
== 'r')
14628 if (l
!= NULL
&& set_errorlist(wp
, l
, action
) == OK
)
14629 rettv
->vval
.v_number
= 0;
14635 * "setloclist()" function
14639 f_setloclist(argvars
, rettv
)
14645 rettv
->vval
.v_number
= -1;
14647 win
= find_win_by_nr(&argvars
[0], NULL
);
14649 set_qf_ll_list(win
, &argvars
[1], &argvars
[2], rettv
);
14653 * "setmatches()" function
14656 f_setmatches(argvars
, rettv
)
14660 #ifdef FEAT_SEARCH_EXTRA
14665 rettv
->vval
.v_number
= -1;
14666 if (argvars
[0].v_type
!= VAR_LIST
)
14668 EMSG(_(e_listreq
));
14671 if ((l
= argvars
[0].vval
.v_list
) != NULL
)
14674 /* To some extent make sure that we are dealing with a list from
14675 * "getmatches()". */
14679 if (li
->li_tv
.v_type
!= VAR_DICT
14680 || (d
= li
->li_tv
.vval
.v_dict
) == NULL
)
14685 if (!(dict_find(d
, (char_u
*)"group", -1) != NULL
14686 && dict_find(d
, (char_u
*)"pattern", -1) != NULL
14687 && dict_find(d
, (char_u
*)"priority", -1) != NULL
14688 && dict_find(d
, (char_u
*)"id", -1) != NULL
))
14696 clear_matches(curwin
);
14700 d
= li
->li_tv
.vval
.v_dict
;
14701 match_add(curwin
, get_dict_string(d
, (char_u
*)"group", FALSE
),
14702 get_dict_string(d
, (char_u
*)"pattern", FALSE
),
14703 (int)get_dict_number(d
, (char_u
*)"priority"),
14704 (int)get_dict_number(d
, (char_u
*)"id"));
14707 rettv
->vval
.v_number
= 0;
14713 * "setpos()" function
14717 f_setpos(argvars
, rettv
)
14725 name
= get_tv_string_chk(argvars
);
14728 if (list2fpos(&argvars
[1], &pos
, &fnum
) == OK
)
14731 if (name
[0] == '.') /* cursor */
14733 if (fnum
== curbuf
->b_fnum
)
14735 curwin
->w_cursor
= pos
;
14741 else if (name
[0] == '\'') /* mark */
14742 (void)setmark_pos(name
[1], &pos
, fnum
);
14750 * "setqflist()" function
14754 f_setqflist(argvars
, rettv
)
14758 set_qf_ll_list(NULL
, &argvars
[0], &argvars
[1], rettv
);
14762 * "setreg()" function
14765 f_setreg(argvars
, rettv
)
14770 char_u
*strregname
;
14781 strregname
= get_tv_string_chk(argvars
);
14782 rettv
->vval
.v_number
= 1; /* FAIL is default */
14784 if (strregname
== NULL
)
14785 return; /* type error; errmsg already given */
14786 regname
= *strregname
;
14787 if (regname
== 0 || regname
== '@')
14789 else if (regname
== '=')
14792 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
14794 stropt
= get_tv_string_chk(&argvars
[2]);
14795 if (stropt
== NULL
)
14796 return; /* type error */
14797 for (; *stropt
!= NUL
; ++stropt
)
14800 case 'a': case 'A': /* append */
14803 case 'v': case 'c': /* character-wise selection */
14806 case 'V': case 'l': /* line-wise selection */
14810 case 'b': case Ctrl_V
: /* block-wise selection */
14811 yank_type
= MBLOCK
;
14812 if (VIM_ISDIGIT(stropt
[1]))
14815 block_len
= getdigits(&stropt
) - 1;
14823 strval
= get_tv_string_chk(&argvars
[1]);
14824 if (strval
!= NULL
)
14825 write_reg_contents_ex(regname
, strval
, -1,
14826 append
, yank_type
, block_len
);
14827 rettv
->vval
.v_number
= 0;
14831 * "settabwinvar()" function
14834 f_settabwinvar(argvars
, rettv
)
14838 setwinvar(argvars
, rettv
, 1);
14842 * "setwinvar()" function
14845 f_setwinvar(argvars
, rettv
)
14849 setwinvar(argvars
, rettv
, 0);
14853 * "setwinvar()" and "settabwinvar()" functions
14856 setwinvar(argvars
, rettv
, off
)
14862 #ifdef FEAT_WINDOWS
14863 win_T
*save_curwin
;
14864 tabpage_T
*save_curtab
;
14866 char_u
*varname
, *winvarname
;
14868 char_u nbuf
[NUMBUFLEN
];
14871 rettv
->vval
.v_number
= 0;
14873 if (check_restricted() || check_secure())
14876 #ifdef FEAT_WINDOWS
14878 tp
= find_tabpage((int)get_tv_number_chk(&argvars
[0], NULL
));
14882 win
= find_win_by_nr(&argvars
[off
], tp
);
14883 varname
= get_tv_string_chk(&argvars
[off
+ 1]);
14884 varp
= &argvars
[off
+ 2];
14886 if (win
!= NULL
&& varname
!= NULL
&& varp
!= NULL
)
14888 #ifdef FEAT_WINDOWS
14889 /* set curwin to be our win, temporarily */
14890 save_curwin
= curwin
;
14891 save_curtab
= curtab
;
14892 goto_tabpage_tp(tp
);
14893 if (!win_valid(win
))
14896 curbuf
= curwin
->w_buffer
;
14899 if (*varname
== '&')
14906 numval
= get_tv_number_chk(varp
, &error
);
14907 strval
= get_tv_string_buf_chk(varp
, nbuf
);
14908 if (!error
&& strval
!= NULL
)
14909 set_option_value(varname
, numval
, strval
, OPT_LOCAL
);
14913 winvarname
= alloc((unsigned)STRLEN(varname
) + 3);
14914 if (winvarname
!= NULL
)
14916 STRCPY(winvarname
, "w:");
14917 STRCPY(winvarname
+ 2, varname
);
14918 set_var(winvarname
, varp
, TRUE
);
14919 vim_free(winvarname
);
14923 #ifdef FEAT_WINDOWS
14924 /* Restore current tabpage and window, if still valid (autocomands can
14925 * make them invalid). */
14926 if (valid_tabpage(save_curtab
))
14927 goto_tabpage_tp(save_curtab
);
14928 if (win_valid(save_curwin
))
14930 curwin
= save_curwin
;
14931 curbuf
= curwin
->w_buffer
;
14938 * "shellescape({string})" function
14941 f_shellescape(argvars
, rettv
)
14945 rettv
->vval
.v_string
= vim_strsave_shellescape(get_tv_string(&argvars
[0]));
14946 rettv
->v_type
= VAR_STRING
;
14950 * "simplify()" function
14953 f_simplify(argvars
, rettv
)
14959 p
= get_tv_string(&argvars
[0]);
14960 rettv
->vval
.v_string
= vim_strsave(p
);
14961 simplify_filename(rettv
->vval
.v_string
); /* simplify in place */
14962 rettv
->v_type
= VAR_STRING
;
14966 #ifdef __BORLANDC__
14969 item_compare
__ARGS((const void *s1
, const void *s2
));
14971 #ifdef __BORLANDC__
14974 item_compare2
__ARGS((const void *s1
, const void *s2
));
14976 static int item_compare_ic
;
14977 static char_u
*item_compare_func
;
14978 static int item_compare_func_err
;
14979 #define ITEM_COMPARE_FAIL 999
14982 * Compare functions for f_sort() below.
14985 #ifdef __BORLANDC__
14988 item_compare(s1
, s2
)
14993 char_u
*tofree1
, *tofree2
;
14995 char_u numbuf1
[NUMBUFLEN
];
14996 char_u numbuf2
[NUMBUFLEN
];
14998 p1
= tv2string(&(*(listitem_T
**)s1
)->li_tv
, &tofree1
, numbuf1
, 0);
14999 p2
= tv2string(&(*(listitem_T
**)s2
)->li_tv
, &tofree2
, numbuf2
, 0);
15004 if (item_compare_ic
)
15005 res
= STRICMP(p1
, p2
);
15007 res
= STRCMP(p1
, p2
);
15014 #ifdef __BORLANDC__
15017 item_compare2(s1
, s2
)
15026 /* shortcut after failure in previous call; compare all items equal */
15027 if (item_compare_func_err
)
15030 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
15031 * in the copy without changing the original list items. */
15032 copy_tv(&(*(listitem_T
**)s1
)->li_tv
, &argv
[0]);
15033 copy_tv(&(*(listitem_T
**)s2
)->li_tv
, &argv
[1]);
15035 rettv
.v_type
= VAR_UNKNOWN
; /* clear_tv() uses this */
15036 res
= call_func(item_compare_func
, (int)STRLEN(item_compare_func
),
15037 &rettv
, 2, argv
, 0L, 0L, &dummy
, TRUE
, NULL
);
15038 clear_tv(&argv
[0]);
15039 clear_tv(&argv
[1]);
15042 res
= ITEM_COMPARE_FAIL
;
15044 /* return value has wrong type */
15045 res
= get_tv_number_chk(&rettv
, &item_compare_func_err
);
15046 if (item_compare_func_err
)
15047 res
= ITEM_COMPARE_FAIL
;
15053 * "sort({list})" function
15056 f_sort(argvars
, rettv
)
15066 rettv
->vval
.v_number
= 0;
15067 if (argvars
[0].v_type
!= VAR_LIST
)
15068 EMSG2(_(e_listarg
), "sort()");
15071 l
= argvars
[0].vval
.v_list
;
15072 if (l
== NULL
|| tv_check_lock(l
->lv_lock
, (char_u
*)"sort()"))
15074 rettv
->vval
.v_list
= l
;
15075 rettv
->v_type
= VAR_LIST
;
15080 return; /* short list sorts pretty quickly */
15082 item_compare_ic
= FALSE
;
15083 item_compare_func
= NULL
;
15084 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15086 if (argvars
[1].v_type
== VAR_FUNC
)
15087 item_compare_func
= argvars
[1].vval
.v_string
;
15092 i
= get_tv_number_chk(&argvars
[1], &error
);
15094 return; /* type error; errmsg already given */
15096 item_compare_ic
= TRUE
;
15098 item_compare_func
= get_tv_string(&argvars
[1]);
15102 /* Make an array with each entry pointing to an item in the List. */
15103 ptrs
= (listitem_T
**)alloc((int)(len
* sizeof(listitem_T
*)));
15107 for (li
= l
->lv_first
; li
!= NULL
; li
= li
->li_next
)
15110 item_compare_func_err
= FALSE
;
15111 /* test the compare function */
15112 if (item_compare_func
!= NULL
15113 && item_compare2((void *)&ptrs
[0], (void *)&ptrs
[1])
15114 == ITEM_COMPARE_FAIL
)
15115 EMSG(_("E702: Sort compare function failed"));
15118 /* Sort the array with item pointers. */
15119 qsort((void *)ptrs
, (size_t)len
, sizeof(listitem_T
*),
15120 item_compare_func
== NULL
? item_compare
: item_compare2
);
15122 if (!item_compare_func_err
)
15124 /* Clear the List and append the items in the sorted order. */
15125 l
->lv_first
= l
->lv_last
= NULL
;
15127 for (i
= 0; i
< len
; ++i
)
15128 list_append(l
, ptrs
[i
]);
15137 * "soundfold({word})" function
15140 f_soundfold(argvars
, rettv
)
15146 rettv
->v_type
= VAR_STRING
;
15147 s
= get_tv_string(&argvars
[0]);
15149 rettv
->vval
.v_string
= eval_soundfold(s
);
15151 rettv
->vval
.v_string
= vim_strsave(s
);
15156 * "spellbadword()" function
15160 f_spellbadword(argvars
, rettv
)
15164 char_u
*word
= (char_u
*)"";
15165 hlf_T attr
= HLF_COUNT
;
15168 if (rettv_list_alloc(rettv
) == FAIL
)
15172 if (argvars
[0].v_type
== VAR_UNKNOWN
)
15174 /* Find the start and length of the badly spelled word. */
15175 len
= spell_move_to(curwin
, FORWARD
, TRUE
, TRUE
, &attr
);
15177 word
= ml_get_cursor();
15179 else if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
15181 char_u
*str
= get_tv_string_chk(&argvars
[0]);
15186 /* Check the argument for spelling. */
15187 while (*str
!= NUL
)
15189 len
= spell_check(curwin
, str
, &attr
, &capcol
, FALSE
);
15190 if (attr
!= HLF_COUNT
)
15201 list_append_string(rettv
->vval
.v_list
, word
, len
);
15202 list_append_string(rettv
->vval
.v_list
, (char_u
*)(
15203 attr
== HLF_SPB
? "bad" :
15204 attr
== HLF_SPR
? "rare" :
15205 attr
== HLF_SPL
? "local" :
15206 attr
== HLF_SPC
? "caps" :
15211 * "spellsuggest()" function
15215 f_spellsuggest(argvars
, rettv
)
15221 int typeerr
= FALSE
;
15226 int need_capital
= FALSE
;
15229 if (rettv_list_alloc(rettv
) == FAIL
)
15233 if (curwin
->w_p_spell
&& *curbuf
->b_p_spl
!= NUL
)
15235 str
= get_tv_string(&argvars
[0]);
15236 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15238 maxcount
= get_tv_number_chk(&argvars
[1], &typeerr
);
15241 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15243 need_capital
= get_tv_number_chk(&argvars
[2], &typeerr
);
15251 spell_suggest_list(&ga
, str
, maxcount
, need_capital
, FALSE
);
15253 for (i
= 0; i
< ga
.ga_len
; ++i
)
15255 str
= ((char_u
**)ga
.ga_data
)[i
];
15257 li
= listitem_alloc();
15262 li
->li_tv
.v_type
= VAR_STRING
;
15263 li
->li_tv
.v_lock
= 0;
15264 li
->li_tv
.vval
.v_string
= str
;
15265 list_append(rettv
->vval
.v_list
, li
);
15274 f_split(argvars
, rettv
)
15280 char_u
*pat
= NULL
;
15281 regmatch_T regmatch
;
15282 char_u patbuf
[NUMBUFLEN
];
15286 int keepempty
= FALSE
;
15287 int typeerr
= FALSE
;
15289 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15291 p_cpo
= (char_u
*)"";
15293 str
= get_tv_string(&argvars
[0]);
15294 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15296 pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
15299 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15300 keepempty
= get_tv_number_chk(&argvars
[2], &typeerr
);
15302 if (pat
== NULL
|| *pat
== NUL
)
15303 pat
= (char_u
*)"[\\x01- ]\\+";
15305 if (rettv_list_alloc(rettv
) == FAIL
)
15310 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
15311 if (regmatch
.regprog
!= NULL
)
15313 regmatch
.rm_ic
= FALSE
;
15314 while (*str
!= NUL
|| keepempty
)
15317 match
= FALSE
; /* empty item at the end */
15319 match
= vim_regexec_nl(®match
, str
, col
);
15321 end
= regmatch
.startp
[0];
15323 end
= str
+ STRLEN(str
);
15324 if (keepempty
|| end
> str
|| (rettv
->vval
.v_list
->lv_len
> 0
15325 && *str
!= NUL
&& match
&& end
< regmatch
.endp
[0]))
15327 if (list_append_string(rettv
->vval
.v_list
, str
,
15328 (int)(end
- str
)) == FAIL
)
15333 /* Advance to just after the match. */
15334 if (regmatch
.endp
[0] > str
)
15338 /* Don't get stuck at the same match. */
15340 col
= (*mb_ptr2len
)(regmatch
.endp
[0]);
15345 str
= regmatch
.endp
[0];
15348 vim_free(regmatch
.regprog
);
15355 * "str2nr()" function
15358 f_str2nr(argvars
, rettv
)
15366 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15368 base
= get_tv_number(&argvars
[1]);
15369 if (base
!= 8 && base
!= 10 && base
!= 16)
15376 p
= skipwhite(get_tv_string(&argvars
[0]));
15377 vim_str2nr(p
, NULL
, NULL
, base
== 8 ? 2 : 0, base
== 16 ? 2 : 0, &n
, NULL
);
15378 rettv
->vval
.v_number
= n
;
15381 #ifdef HAVE_STRFTIME
15383 * "strftime({format}[, {time}])" function
15386 f_strftime(argvars
, rettv
)
15390 char_u result_buf
[256];
15391 struct tm
*curtime
;
15395 rettv
->v_type
= VAR_STRING
;
15397 p
= get_tv_string(&argvars
[0]);
15398 if (argvars
[1].v_type
== VAR_UNKNOWN
)
15399 seconds
= time(NULL
);
15401 seconds
= (time_t)get_tv_number(&argvars
[1]);
15402 curtime
= localtime(&seconds
);
15403 /* MSVC returns NULL for an invalid value of seconds. */
15404 if (curtime
== NULL
)
15405 rettv
->vval
.v_string
= vim_strsave((char_u
*)_("(Invalid)"));
15412 conv
.vc_type
= CONV_NONE
;
15413 enc
= enc_locale();
15414 convert_setup(&conv
, p_enc
, enc
);
15415 if (conv
.vc_type
!= CONV_NONE
)
15416 p
= string_convert(&conv
, p
, NULL
);
15419 (void)strftime((char *)result_buf
, sizeof(result_buf
),
15420 (char *)p
, curtime
);
15422 result_buf
[0] = NUL
;
15425 if (conv
.vc_type
!= CONV_NONE
)
15427 convert_setup(&conv
, enc
, p_enc
);
15428 if (conv
.vc_type
!= CONV_NONE
)
15429 rettv
->vval
.v_string
= string_convert(&conv
, result_buf
, NULL
);
15432 rettv
->vval
.v_string
= vim_strsave(result_buf
);
15435 /* Release conversion descriptors */
15436 convert_setup(&conv
, NULL
, NULL
);
15444 * "stridx()" function
15447 f_stridx(argvars
, rettv
)
15451 char_u buf
[NUMBUFLEN
];
15454 char_u
*save_haystack
;
15458 needle
= get_tv_string_chk(&argvars
[1]);
15459 save_haystack
= haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
15460 rettv
->vval
.v_number
= -1;
15461 if (needle
== NULL
|| haystack
== NULL
)
15462 return; /* type error; errmsg already given */
15464 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15468 start_idx
= get_tv_number_chk(&argvars
[2], &error
);
15469 if (error
|| start_idx
>= (int)STRLEN(haystack
))
15471 if (start_idx
>= 0)
15472 haystack
+= start_idx
;
15475 pos
= (char_u
*)strstr((char *)haystack
, (char *)needle
);
15477 rettv
->vval
.v_number
= (varnumber_T
)(pos
- save_haystack
);
15481 * "string()" function
15484 f_string(argvars
, rettv
)
15489 char_u numbuf
[NUMBUFLEN
];
15491 rettv
->v_type
= VAR_STRING
;
15492 rettv
->vval
.v_string
= tv2string(&argvars
[0], &tofree
, numbuf
, 0);
15493 /* Make a copy if we have a value but it's not in allocate memory. */
15494 if (rettv
->vval
.v_string
!= NULL
&& tofree
== NULL
)
15495 rettv
->vval
.v_string
= vim_strsave(rettv
->vval
.v_string
);
15499 * "strlen()" function
15502 f_strlen(argvars
, rettv
)
15506 rettv
->vval
.v_number
= (varnumber_T
)(STRLEN(
15507 get_tv_string(&argvars
[0])));
15511 * "strpart()" function
15514 f_strpart(argvars
, rettv
)
15524 p
= get_tv_string(&argvars
[0]);
15525 slen
= (int)STRLEN(p
);
15527 n
= get_tv_number_chk(&argvars
[1], &error
);
15530 else if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15531 len
= get_tv_number(&argvars
[2]);
15533 len
= slen
- n
; /* default len: all bytes that are available. */
15536 * Only return the overlap between the specified part and the actual
15548 else if (n
+ len
> slen
)
15551 rettv
->v_type
= VAR_STRING
;
15552 rettv
->vval
.v_string
= vim_strnsave(p
+ n
, len
);
15556 * "strridx()" function
15559 f_strridx(argvars
, rettv
)
15563 char_u buf
[NUMBUFLEN
];
15567 char_u
*lastmatch
= NULL
;
15568 int haystack_len
, end_idx
;
15570 needle
= get_tv_string_chk(&argvars
[1]);
15571 haystack
= get_tv_string_buf_chk(&argvars
[0], buf
);
15573 rettv
->vval
.v_number
= -1;
15574 if (needle
== NULL
|| haystack
== NULL
)
15575 return; /* type error; errmsg already given */
15577 haystack_len
= (int)STRLEN(haystack
);
15578 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15580 /* Third argument: upper limit for index */
15581 end_idx
= get_tv_number_chk(&argvars
[2], NULL
);
15583 return; /* can never find a match */
15586 end_idx
= haystack_len
;
15588 if (*needle
== NUL
)
15590 /* Empty string matches past the end. */
15591 lastmatch
= haystack
+ end_idx
;
15595 for (rest
= haystack
; *rest
!= '\0'; ++rest
)
15597 rest
= (char_u
*)strstr((char *)rest
, (char *)needle
);
15598 if (rest
== NULL
|| rest
> haystack
+ end_idx
)
15604 if (lastmatch
== NULL
)
15605 rettv
->vval
.v_number
= -1;
15607 rettv
->vval
.v_number
= (varnumber_T
)(lastmatch
- haystack
);
15611 * "strtrans()" function
15614 f_strtrans(argvars
, rettv
)
15618 rettv
->v_type
= VAR_STRING
;
15619 rettv
->vval
.v_string
= transstr(get_tv_string(&argvars
[0]));
15623 * "submatch()" function
15626 f_submatch(argvars
, rettv
)
15630 rettv
->v_type
= VAR_STRING
;
15631 rettv
->vval
.v_string
=
15632 reg_submatch((int)get_tv_number_chk(&argvars
[0], NULL
));
15636 * "substitute()" function
15639 f_substitute(argvars
, rettv
)
15643 char_u patbuf
[NUMBUFLEN
];
15644 char_u subbuf
[NUMBUFLEN
];
15645 char_u flagsbuf
[NUMBUFLEN
];
15647 char_u
*str
= get_tv_string_chk(&argvars
[0]);
15648 char_u
*pat
= get_tv_string_buf_chk(&argvars
[1], patbuf
);
15649 char_u
*sub
= get_tv_string_buf_chk(&argvars
[2], subbuf
);
15650 char_u
*flg
= get_tv_string_buf_chk(&argvars
[3], flagsbuf
);
15652 rettv
->v_type
= VAR_STRING
;
15653 if (str
== NULL
|| pat
== NULL
|| sub
== NULL
|| flg
== NULL
)
15654 rettv
->vval
.v_string
= NULL
;
15656 rettv
->vval
.v_string
= do_string_sub(str
, pat
, sub
, flg
);
15660 * "synID(lnum, col, trans)" function
15664 f_synID(argvars
, rettv
)
15673 int transerr
= FALSE
;
15675 lnum
= get_tv_lnum(argvars
); /* -1 on type error */
15676 col
= get_tv_number(&argvars
[1]) - 1; /* -1 on type error */
15677 trans
= get_tv_number_chk(&argvars
[2], &transerr
);
15679 if (!transerr
&& lnum
>= 1 && lnum
<= curbuf
->b_ml
.ml_line_count
15680 && col
>= 0 && col
< (long)STRLEN(ml_get(lnum
)))
15681 id
= syn_get_id(curwin
, lnum
, (colnr_T
)col
, trans
, NULL
);
15684 rettv
->vval
.v_number
= id
;
15688 * "synIDattr(id, what [, mode])" function
15692 f_synIDattr(argvars
, rettv
)
15701 char_u modebuf
[NUMBUFLEN
];
15704 id
= get_tv_number(&argvars
[0]);
15705 what
= get_tv_string(&argvars
[1]);
15706 if (argvars
[2].v_type
!= VAR_UNKNOWN
)
15708 mode
= get_tv_string_buf(&argvars
[2], modebuf
);
15709 modec
= TOLOWER_ASC(mode
[0]);
15710 if (modec
!= 't' && modec
!= 'c'
15715 modec
= 0; /* replace invalid with current */
15731 switch (TOLOWER_ASC(what
[0]))
15734 if (TOLOWER_ASC(what
[1]) == 'g') /* bg[#] */
15735 p
= highlight_color(id
, what
, modec
);
15737 p
= highlight_has_attr(id
, HL_BOLD
, modec
);
15740 case 'f': /* fg[#] */
15741 p
= highlight_color(id
, what
, modec
);
15745 if (TOLOWER_ASC(what
[1]) == 'n') /* inverse */
15746 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
15748 p
= highlight_has_attr(id
, HL_ITALIC
, modec
);
15751 case 'n': /* name */
15752 p
= get_highlight_name(NULL
, id
- 1);
15755 case 'r': /* reverse */
15756 p
= highlight_has_attr(id
, HL_INVERSE
, modec
);
15759 case 's': /* standout */
15760 p
= highlight_has_attr(id
, HL_STANDOUT
, modec
);
15764 if (STRLEN(what
) <= 5 || TOLOWER_ASC(what
[5]) != 'c')
15766 p
= highlight_has_attr(id
, HL_UNDERLINE
, modec
);
15769 p
= highlight_has_attr(id
, HL_UNDERCURL
, modec
);
15774 p
= vim_strsave(p
);
15776 rettv
->v_type
= VAR_STRING
;
15777 rettv
->vval
.v_string
= p
;
15781 * "synIDtrans(id)" function
15785 f_synIDtrans(argvars
, rettv
)
15792 id
= get_tv_number(&argvars
[0]);
15795 id
= syn_get_final_id(id
);
15800 rettv
->vval
.v_number
= id
;
15804 * "system()" function
15807 f_system(argvars
, rettv
)
15811 char_u
*res
= NULL
;
15813 char_u
*infile
= NULL
;
15814 char_u buf
[NUMBUFLEN
];
15818 if (check_restricted() || check_secure())
15821 if (argvars
[1].v_type
!= VAR_UNKNOWN
)
15824 * Write the string to a temp file, to be used for input of the shell
15827 if ((infile
= vim_tempname('i')) == NULL
)
15833 fd
= mch_fopen((char *)infile
, WRITEBIN
);
15836 EMSG2(_(e_notopen
), infile
);
15839 p
= get_tv_string_buf_chk(&argvars
[1], buf
);
15843 goto done
; /* type error; errmsg already given */
15845 if (fwrite(p
, STRLEN(p
), 1, fd
) != 1)
15847 if (fclose(fd
) != 0)
15851 EMSG(_("E677: Error writing temp file"));
15856 res
= get_cmd_output(get_tv_string(&argvars
[0]), infile
,
15857 SHELL_SILENT
| SHELL_COOKED
);
15860 /* translate <CR> into <NL> */
15865 for (s
= res
; *s
; ++s
)
15873 /* translate <CR><NL> into <NL> */
15879 for (s
= res
; *s
; ++s
)
15881 if (s
[0] == CAR
&& s
[1] == NL
)
15891 if (infile
!= NULL
)
15893 mch_remove(infile
);
15896 rettv
->v_type
= VAR_STRING
;
15897 rettv
->vval
.v_string
= res
;
15901 * "tabpagebuflist()" function
15905 f_tabpagebuflist(argvars
, rettv
)
15909 #ifndef FEAT_WINDOWS
15910 rettv
->vval
.v_number
= 0;
15915 if (argvars
[0].v_type
== VAR_UNKNOWN
)
15919 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
15921 wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
15924 rettv
->vval
.v_number
= 0;
15927 if (rettv_list_alloc(rettv
) == FAIL
)
15928 rettv
->vval
.v_number
= 0;
15931 for (; wp
!= NULL
; wp
= wp
->w_next
)
15932 if (list_append_number(rettv
->vval
.v_list
,
15933 wp
->w_buffer
->b_fnum
) == FAIL
)
15942 * "tabpagenr()" function
15946 f_tabpagenr(argvars
, rettv
)
15951 #ifdef FEAT_WINDOWS
15954 if (argvars
[0].v_type
!= VAR_UNKNOWN
)
15956 arg
= get_tv_string_chk(&argvars
[0]);
15960 if (STRCMP(arg
, "$") == 0)
15961 nr
= tabpage_index(NULL
) - 1;
15963 EMSG2(_(e_invexpr2
), arg
);
15967 nr
= tabpage_index(curtab
);
15969 rettv
->vval
.v_number
= nr
;
15973 #ifdef FEAT_WINDOWS
15974 static int get_winnr
__ARGS((tabpage_T
*tp
, typval_T
*argvar
));
15977 * Common code for tabpagewinnr() and winnr().
15980 get_winnr(tp
, argvar
)
15989 twin
= (tp
== curtab
) ? curwin
: tp
->tp_curwin
;
15990 if (argvar
->v_type
!= VAR_UNKNOWN
)
15992 arg
= get_tv_string_chk(argvar
);
15994 nr
= 0; /* type error; errmsg already given */
15995 else if (STRCMP(arg
, "$") == 0)
15996 twin
= (tp
== curtab
) ? lastwin
: tp
->tp_lastwin
;
15997 else if (STRCMP(arg
, "#") == 0)
15999 twin
= (tp
== curtab
) ? prevwin
: tp
->tp_prevwin
;
16005 EMSG2(_(e_invexpr2
), arg
);
16011 for (wp
= (tp
== curtab
) ? firstwin
: tp
->tp_firstwin
;
16012 wp
!= twin
; wp
= wp
->w_next
)
16016 /* didn't find it in this tabpage */
16027 * "tabpagewinnr()" function
16031 f_tabpagewinnr(argvars
, rettv
)
16036 #ifdef FEAT_WINDOWS
16039 tp
= find_tabpage((int)get_tv_number(&argvars
[0]));
16043 nr
= get_winnr(tp
, &argvars
[1]);
16045 rettv
->vval
.v_number
= nr
;
16050 * "tagfiles()" function
16054 f_tagfiles(argvars
, rettv
)
16058 char_u fname
[MAXPATHL
+ 1];
16062 if (rettv_list_alloc(rettv
) == FAIL
)
16064 rettv
->vval
.v_number
= 0;
16068 for (first
= TRUE
; ; first
= FALSE
)
16069 if (get_tagfname(&tn
, first
, fname
) == FAIL
16070 || list_append_string(rettv
->vval
.v_list
, fname
, -1) == FAIL
)
16076 * "taglist()" function
16079 f_taglist(argvars
, rettv
)
16083 char_u
*tag_pattern
;
16085 tag_pattern
= get_tv_string(&argvars
[0]);
16087 rettv
->vval
.v_number
= FALSE
;
16088 if (*tag_pattern
== NUL
)
16091 if (rettv_list_alloc(rettv
) == OK
)
16092 (void)get_tags(rettv
->vval
.v_list
, tag_pattern
);
16096 * "tempname()" function
16100 f_tempname(argvars
, rettv
)
16104 static int x
= 'A';
16106 rettv
->v_type
= VAR_STRING
;
16107 rettv
->vval
.v_string
= vim_tempname(x
);
16109 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
16110 * names. Skip 'I' and 'O', they are used for shell redirection. */
16128 } while (x
== 'I' || x
== 'O');
16132 * "test(list)" function: Just checking the walls...
16136 f_test(argvars
, rettv
)
16140 /* Used for unit testing. Change the code below to your liking. */
16144 char_u
*bad
, *good
;
16146 if (argvars
[0].v_type
!= VAR_LIST
)
16148 l
= argvars
[0].vval
.v_list
;
16154 bad
= get_tv_string(&li
->li_tv
);
16158 good
= get_tv_string(&li
->li_tv
);
16159 rettv
->vval
.v_number
= test_edit_score(bad
, good
);
16164 * "tolower(string)" function
16167 f_tolower(argvars
, rettv
)
16173 p
= vim_strsave(get_tv_string(&argvars
[0]));
16174 rettv
->v_type
= VAR_STRING
;
16175 rettv
->vval
.v_string
= p
;
16187 c
= utf_ptr2char(p
);
16188 lc
= utf_tolower(c
);
16189 l
= utf_ptr2len(p
);
16190 /* TODO: reallocate string when byte count changes. */
16191 if (utf_char2len(lc
) == l
)
16192 utf_char2bytes(lc
, p
);
16195 else if (has_mbyte
&& (l
= (*mb_ptr2len
)(p
)) > 1)
16196 p
+= l
; /* skip multi-byte character */
16200 *p
= TOLOWER_LOC(*p
); /* note that tolower() can be a macro */
16207 * "toupper(string)" function
16210 f_toupper(argvars
, rettv
)
16214 rettv
->v_type
= VAR_STRING
;
16215 rettv
->vval
.v_string
= strup_save(get_tv_string(&argvars
[0]));
16219 * "tr(string, fromstr, tostr)" function
16222 f_tr(argvars
, rettv
)
16239 char_u buf
[NUMBUFLEN
];
16240 char_u buf2
[NUMBUFLEN
];
16243 instr
= get_tv_string(&argvars
[0]);
16244 fromstr
= get_tv_string_buf_chk(&argvars
[1], buf
);
16245 tostr
= get_tv_string_buf_chk(&argvars
[2], buf2
);
16247 /* Default return value: empty string. */
16248 rettv
->v_type
= VAR_STRING
;
16249 rettv
->vval
.v_string
= NULL
;
16250 if (fromstr
== NULL
|| tostr
== NULL
)
16251 return; /* type error; errmsg already given */
16252 ga_init2(&ga
, (int)sizeof(char), 80);
16257 /* not multi-byte: fromstr and tostr must be the same length */
16258 if (STRLEN(fromstr
) != STRLEN(tostr
))
16263 EMSG2(_(e_invarg2
), fromstr
);
16268 /* fromstr and tostr have to contain the same number of chars */
16269 while (*instr
!= NUL
)
16274 inlen
= (*mb_ptr2len
)(instr
);
16278 for (p
= fromstr
; *p
!= NUL
; p
+= fromlen
)
16280 fromlen
= (*mb_ptr2len
)(p
);
16281 if (fromlen
== inlen
&& STRNCMP(instr
, p
, inlen
) == 0)
16283 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
16285 tolen
= (*mb_ptr2len
)(p
);
16293 if (*p
== NUL
) /* tostr is shorter than fromstr */
16300 if (first
&& cpstr
== instr
)
16302 /* Check that fromstr and tostr have the same number of
16303 * (multi-byte) characters. Done only once when a character
16304 * of instr doesn't appear in fromstr. */
16306 for (p
= tostr
; *p
!= NUL
; p
+= tolen
)
16308 tolen
= (*mb_ptr2len
)(p
);
16315 ga_grow(&ga
, cplen
);
16316 mch_memmove((char *)ga
.ga_data
+ ga
.ga_len
, cpstr
, (size_t)cplen
);
16317 ga
.ga_len
+= cplen
;
16324 /* When not using multi-byte chars we can do it faster. */
16325 p
= vim_strchr(fromstr
, *instr
);
16327 ga_append(&ga
, tostr
[p
- fromstr
]);
16329 ga_append(&ga
, *instr
);
16334 /* add a terminating NUL */
16336 ga_append(&ga
, NUL
);
16338 rettv
->vval
.v_string
= ga
.ga_data
;
16342 * "type(expr)" function
16345 f_type(argvars
, rettv
)
16351 switch (argvars
[0].v_type
)
16353 case VAR_NUMBER
: n
= 0; break;
16354 case VAR_STRING
: n
= 1; break;
16355 case VAR_FUNC
: n
= 2; break;
16356 case VAR_LIST
: n
= 3; break;
16357 case VAR_DICT
: n
= 4; break;
16358 default: EMSG2(_(e_intern2
), "f_type()"); n
= 0; break;
16360 rettv
->vval
.v_number
= n
;
16364 * "values(dict)" function
16367 f_values(argvars
, rettv
)
16371 dict_list(argvars
, rettv
, 1);
16375 * "virtcol(string)" function
16378 f_virtcol(argvars
, rettv
)
16384 int fnum
= curbuf
->b_fnum
;
16386 fp
= var2fpos(&argvars
[0], FALSE
, &fnum
);
16387 if (fp
!= NULL
&& fp
->lnum
<= curbuf
->b_ml
.ml_line_count
16388 && fnum
== curbuf
->b_fnum
)
16390 getvvcol(curwin
, fp
, NULL
, NULL
, &vcol
);
16394 rettv
->vval
.v_number
= vcol
;
16398 * "visualmode()" function
16402 f_visualmode(argvars
, rettv
)
16409 rettv
->v_type
= VAR_STRING
;
16410 str
[0] = curbuf
->b_visual_mode_eval
;
16412 rettv
->vval
.v_string
= vim_strsave(str
);
16414 /* A non-zero number or non-empty string argument: reset mode. */
16415 if ((argvars
[0].v_type
== VAR_NUMBER
16416 && argvars
[0].vval
.v_number
!= 0)
16417 || (argvars
[0].v_type
== VAR_STRING
16418 && *get_tv_string(&argvars
[0]) != NUL
))
16419 curbuf
->b_visual_mode_eval
= NUL
;
16421 rettv
->vval
.v_number
= 0; /* return anything, it won't work anyway */
16426 * "winbufnr(nr)" function
16429 f_winbufnr(argvars
, rettv
)
16435 wp
= find_win_by_nr(&argvars
[0], NULL
);
16437 rettv
->vval
.v_number
= -1;
16439 rettv
->vval
.v_number
= wp
->w_buffer
->b_fnum
;
16443 * "wincol()" function
16447 f_wincol(argvars
, rettv
)
16452 rettv
->vval
.v_number
= curwin
->w_wcol
+ 1;
16456 * "winheight(nr)" function
16459 f_winheight(argvars
, rettv
)
16465 wp
= find_win_by_nr(&argvars
[0], NULL
);
16467 rettv
->vval
.v_number
= -1;
16469 rettv
->vval
.v_number
= wp
->w_height
;
16473 * "winline()" function
16477 f_winline(argvars
, rettv
)
16482 rettv
->vval
.v_number
= curwin
->w_wrow
+ 1;
16486 * "winnr()" function
16490 f_winnr(argvars
, rettv
)
16496 #ifdef FEAT_WINDOWS
16497 nr
= get_winnr(curtab
, &argvars
[0]);
16499 rettv
->vval
.v_number
= nr
;
16503 * "winrestcmd()" function
16507 f_winrestcmd(argvars
, rettv
)
16511 #ifdef FEAT_WINDOWS
16517 ga_init2(&ga
, (int)sizeof(char), 70);
16518 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
16520 sprintf((char *)buf
, "%dresize %d|", winnr
, wp
->w_height
);
16521 ga_concat(&ga
, buf
);
16522 # ifdef FEAT_VERTSPLIT
16523 sprintf((char *)buf
, "vert %dresize %d|", winnr
, wp
->w_width
);
16524 ga_concat(&ga
, buf
);
16528 ga_append(&ga
, NUL
);
16530 rettv
->vval
.v_string
= ga
.ga_data
;
16532 rettv
->vval
.v_string
= NULL
;
16534 rettv
->v_type
= VAR_STRING
;
16538 * "winrestview()" function
16542 f_winrestview(argvars
, rettv
)
16548 if (argvars
[0].v_type
!= VAR_DICT
16549 || (dict
= argvars
[0].vval
.v_dict
) == NULL
)
16553 curwin
->w_cursor
.lnum
= get_dict_number(dict
, (char_u
*)"lnum");
16554 curwin
->w_cursor
.col
= get_dict_number(dict
, (char_u
*)"col");
16555 #ifdef FEAT_VIRTUALEDIT
16556 curwin
->w_cursor
.coladd
= get_dict_number(dict
, (char_u
*)"coladd");
16558 curwin
->w_curswant
= get_dict_number(dict
, (char_u
*)"curswant");
16559 curwin
->w_set_curswant
= FALSE
;
16561 set_topline(curwin
, get_dict_number(dict
, (char_u
*)"topline"));
16563 curwin
->w_topfill
= get_dict_number(dict
, (char_u
*)"topfill");
16565 curwin
->w_leftcol
= get_dict_number(dict
, (char_u
*)"leftcol");
16566 curwin
->w_skipcol
= get_dict_number(dict
, (char_u
*)"skipcol");
16569 changed_cline_bef_curs();
16570 invalidate_botline();
16571 redraw_later(VALID
);
16573 if (curwin
->w_topline
== 0)
16574 curwin
->w_topline
= 1;
16575 if (curwin
->w_topline
> curbuf
->b_ml
.ml_line_count
)
16576 curwin
->w_topline
= curbuf
->b_ml
.ml_line_count
;
16578 check_topfill(curwin
, TRUE
);
16584 * "winsaveview()" function
16588 f_winsaveview(argvars
, rettv
)
16594 dict
= dict_alloc();
16597 rettv
->v_type
= VAR_DICT
;
16598 rettv
->vval
.v_dict
= dict
;
16599 ++dict
->dv_refcount
;
16601 dict_add_nr_str(dict
, "lnum", (long)curwin
->w_cursor
.lnum
, NULL
);
16602 dict_add_nr_str(dict
, "col", (long)curwin
->w_cursor
.col
, NULL
);
16603 #ifdef FEAT_VIRTUALEDIT
16604 dict_add_nr_str(dict
, "coladd", (long)curwin
->w_cursor
.coladd
, NULL
);
16607 dict_add_nr_str(dict
, "curswant", (long)curwin
->w_curswant
, NULL
);
16609 dict_add_nr_str(dict
, "topline", (long)curwin
->w_topline
, NULL
);
16611 dict_add_nr_str(dict
, "topfill", (long)curwin
->w_topfill
, NULL
);
16613 dict_add_nr_str(dict
, "leftcol", (long)curwin
->w_leftcol
, NULL
);
16614 dict_add_nr_str(dict
, "skipcol", (long)curwin
->w_skipcol
, NULL
);
16618 * "winwidth(nr)" function
16621 f_winwidth(argvars
, rettv
)
16627 wp
= find_win_by_nr(&argvars
[0], NULL
);
16629 rettv
->vval
.v_number
= -1;
16631 #ifdef FEAT_VERTSPLIT
16632 rettv
->vval
.v_number
= wp
->w_width
;
16634 rettv
->vval
.v_number
= Columns
;
16639 * "writefile()" function
16642 f_writefile(argvars
, rettv
)
16646 int binary
= FALSE
;
16654 if (check_restricted() || check_secure())
16657 if (argvars
[0].v_type
!= VAR_LIST
)
16659 EMSG2(_(e_listarg
), "writefile()");
16662 if (argvars
[0].vval
.v_list
== NULL
)
16665 if (argvars
[2].v_type
!= VAR_UNKNOWN
16666 && STRCMP(get_tv_string(&argvars
[2]), "b") == 0)
16669 /* Always open the file in binary mode, library functions have a mind of
16670 * their own about CR-LF conversion. */
16671 fname
= get_tv_string(&argvars
[1]);
16672 if (*fname
== NUL
|| (fd
= mch_fopen((char *)fname
, WRITEBIN
)) == NULL
)
16674 EMSG2(_(e_notcreate
), *fname
== NUL
? (char_u
*)_("<empty>") : fname
);
16679 for (li
= argvars
[0].vval
.v_list
->lv_first
; li
!= NULL
;
16682 for (s
= get_tv_string(&li
->li_tv
); *s
!= NUL
; ++s
)
16694 if (!binary
|| li
->li_next
!= NULL
)
16695 if (putc('\n', fd
) == EOF
)
16709 rettv
->vval
.v_number
= ret
;
16713 * Translate a String variable into a position.
16714 * Returns NULL when there is an error.
16717 var2fpos(varp
, dollar_lnum
, fnum
)
16719 int dollar_lnum
; /* TRUE when $ is last line */
16720 int *fnum
; /* set to fnum for '0, 'A, etc. */
16726 /* Argument can be [lnum, col, coladd]. */
16727 if (varp
->v_type
== VAR_LIST
)
16734 l
= varp
->vval
.v_list
;
16738 /* Get the line number */
16739 pos
.lnum
= list_find_nr(l
, 0L, &error
);
16740 if (error
|| pos
.lnum
<= 0 || pos
.lnum
> curbuf
->b_ml
.ml_line_count
)
16741 return NULL
; /* invalid line number */
16743 /* Get the column number */
16744 pos
.col
= list_find_nr(l
, 1L, &error
);
16747 len
= (long)STRLEN(ml_get(pos
.lnum
));
16749 /* We accept "$" for the column number: last column. */
16750 li
= list_find(l
, 1L);
16751 if (li
!= NULL
&& li
->li_tv
.v_type
== VAR_STRING
16752 && li
->li_tv
.vval
.v_string
!= NULL
16753 && STRCMP(li
->li_tv
.vval
.v_string
, "$") == 0)
16756 /* Accept a position up to the NUL after the line. */
16757 if (pos
.col
== 0 || (int)pos
.col
> len
+ 1)
16758 return NULL
; /* invalid column number */
16761 #ifdef FEAT_VIRTUALEDIT
16762 /* Get the virtual offset. Defaults to zero. */
16763 pos
.coladd
= list_find_nr(l
, 2L, &error
);
16771 name
= get_tv_string_chk(varp
);
16774 if (name
[0] == '.') /* cursor */
16775 return &curwin
->w_cursor
;
16776 if (name
[0] == '\'') /* mark */
16778 pp
= getmark_fnum(name
[1], FALSE
, fnum
);
16779 if (pp
== NULL
|| pp
== (pos_T
*)-1 || pp
->lnum
<= 0)
16784 #ifdef FEAT_VIRTUALEDIT
16788 if (name
[0] == 'w' && dollar_lnum
)
16791 if (name
[1] == '0') /* "w0": first visible line */
16794 pos
.lnum
= curwin
->w_topline
;
16797 else if (name
[1] == '$') /* "w$": last visible line */
16799 validate_botline();
16800 pos
.lnum
= curwin
->w_botline
- 1;
16804 else if (name
[0] == '$') /* last column or line */
16808 pos
.lnum
= curbuf
->b_ml
.ml_line_count
;
16813 pos
.lnum
= curwin
->w_cursor
.lnum
;
16814 pos
.col
= (colnr_T
)STRLEN(ml_get_curline());
16822 * Convert list in "arg" into a position and optional file number.
16823 * When "fnump" is NULL there is no file number, only 3 items.
16824 * Note that the column is passed on as-is, the caller may want to decrement
16825 * it to use 1 for the first column.
16826 * Return FAIL when conversion is not possible, doesn't check the position for
16830 list2fpos(arg
, posp
, fnump
)
16835 list_T
*l
= arg
->vval
.v_list
;
16839 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
16840 * when "fnump" isn't NULL and "coladd" is optional. */
16841 if (arg
->v_type
!= VAR_LIST
16843 || l
->lv_len
< (fnump
== NULL
? 2 : 3)
16844 || l
->lv_len
> (fnump
== NULL
? 3 : 4))
16849 n
= list_find_nr(l
, i
++, NULL
); /* fnum */
16853 n
= curbuf
->b_fnum
; /* current buffer */
16857 n
= list_find_nr(l
, i
++, NULL
); /* lnum */
16862 n
= list_find_nr(l
, i
++, NULL
); /* col */
16867 #ifdef FEAT_VIRTUALEDIT
16868 n
= list_find_nr(l
, i
, NULL
);
16879 * Get the length of an environment variable name.
16880 * Advance "arg" to the first character after the name.
16881 * Return 0 for error.
16890 for (p
= *arg
; vim_isIDc(*p
); ++p
)
16892 if (p
== *arg
) /* no name found */
16895 len
= (int)(p
- *arg
);
16901 * Get the length of the name of a function or internal variable.
16902 * "arg" is advanced to the first non-white character after the name.
16903 * Return 0 if something is wrong.
16912 /* Find the end of the name. */
16913 for (p
= *arg
; eval_isnamec(*p
); ++p
)
16915 if (p
== *arg
) /* no name found */
16918 len
= (int)(p
- *arg
);
16919 *arg
= skipwhite(p
);
16925 * Get the length of the name of a variable or function.
16926 * Only the name is recognized, does not handle ".key" or "[idx]".
16927 * "arg" is advanced to the first non-white character after the name.
16928 * Return -1 if curly braces expansion failed.
16929 * Return 0 if something else is wrong.
16930 * If the name contains 'magic' {}'s, expand them and return the
16931 * expanded name in an allocated string via 'alias' - caller must free.
16934 get_name_len(arg
, alias
, evaluate
, verbose
)
16942 char_u
*expr_start
;
16945 *alias
= NULL
; /* default to no alias */
16947 if ((*arg
)[0] == K_SPECIAL
&& (*arg
)[1] == KS_EXTRA
16948 && (*arg
)[2] == (int)KE_SNR
)
16950 /* hard coded <SNR>, already translated */
16952 return get_id_len(arg
) + 3;
16954 len
= eval_fname_script(*arg
);
16957 /* literal "<SID>", "s:" or "<SNR>" */
16962 * Find the end of the name; check for {} construction.
16964 p
= find_name_end(*arg
, &expr_start
, &expr_end
,
16965 len
> 0 ? 0 : FNE_CHECK_START
);
16966 if (expr_start
!= NULL
)
16968 char_u
*temp_string
;
16972 len
+= (int)(p
- *arg
);
16973 *arg
= skipwhite(p
);
16978 * Include any <SID> etc in the expanded string:
16979 * Thus the -len here.
16981 temp_string
= make_expanded_name(*arg
- len
, expr_start
, expr_end
, p
);
16982 if (temp_string
== NULL
)
16984 *alias
= temp_string
;
16985 *arg
= skipwhite(p
);
16986 return (int)STRLEN(temp_string
);
16989 len
+= get_id_len(arg
);
16990 if (len
== 0 && verbose
)
16991 EMSG2(_(e_invexpr2
), *arg
);
16997 * Find the end of a variable or function name, taking care of magic braces.
16998 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
16999 * start and end of the first magic braces item.
17000 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
17001 * Return a pointer to just after the name. Equal to "arg" if there is no
17005 find_name_end(arg
, expr_start
, expr_end
, flags
)
17007 char_u
**expr_start
;
17015 if (expr_start
!= NULL
)
17017 *expr_start
= NULL
;
17021 /* Quick check for valid starting character. */
17022 if ((flags
& FNE_CHECK_START
) && !eval_isnamec1(*arg
) && *arg
!= '{')
17025 for (p
= arg
; *p
!= NUL
17026 && (eval_isnamec(*p
)
17028 || ((flags
& FNE_INCL_BR
) && (*p
== '[' || *p
== '.'))
17030 || br_nest
!= 0); mb_ptr_adv(p
))
17034 /* skip over 'string' to avoid counting [ and ] inside it. */
17035 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '\''; mb_ptr_adv(p
))
17040 else if (*p
== '"')
17042 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
17043 for (p
= p
+ 1; *p
!= NUL
&& *p
!= '"'; mb_ptr_adv(p
))
17044 if (*p
== '\\' && p
[1] != NUL
)
17054 else if (*p
== ']')
17063 if (expr_start
!= NULL
&& *expr_start
== NULL
)
17066 else if (*p
== '}')
17069 if (expr_start
!= NULL
&& mb_nest
== 0 && *expr_end
== NULL
)
17079 * Expands out the 'magic' {}'s in a variable/function name.
17080 * Note that this can call itself recursively, to deal with
17081 * constructs like foo{bar}{baz}{bam}
17082 * The four pointer arguments point to "foo{expre}ss{ion}bar"
17088 * Returns a new allocated string, which the caller must free.
17089 * Returns NULL for failure.
17092 make_expanded_name(in_start
, expr_start
, expr_end
, in_end
)
17094 char_u
*expr_start
;
17099 char_u
*retval
= NULL
;
17100 char_u
*temp_result
;
17101 char_u
*nextcmd
= NULL
;
17103 if (expr_end
== NULL
|| in_end
== NULL
)
17110 temp_result
= eval_to_string(expr_start
+ 1, &nextcmd
, FALSE
);
17111 if (temp_result
!= NULL
&& nextcmd
== NULL
)
17113 retval
= alloc((unsigned)(STRLEN(temp_result
) + (expr_start
- in_start
)
17114 + (in_end
- expr_end
) + 1));
17115 if (retval
!= NULL
)
17117 STRCPY(retval
, in_start
);
17118 STRCAT(retval
, temp_result
);
17119 STRCAT(retval
, expr_end
+ 1);
17122 vim_free(temp_result
);
17124 *in_end
= c1
; /* put char back for error messages */
17128 if (retval
!= NULL
)
17130 temp_result
= find_name_end(retval
, &expr_start
, &expr_end
, 0);
17131 if (expr_start
!= NULL
)
17133 /* Further expansion! */
17134 temp_result
= make_expanded_name(retval
, expr_start
,
17135 expr_end
, temp_result
);
17137 retval
= temp_result
;
17145 * Return TRUE if character "c" can be used in a variable or function name.
17146 * Does not include '{' or '}' for magic braces.
17152 return (ASCII_ISALNUM(c
) || c
== '_' || c
== ':' || c
== AUTOLOAD_CHAR
);
17156 * Return TRUE if character "c" can be used as the first character in a
17157 * variable or function name (excluding '{' and '}').
17163 return (ASCII_ISALPHA(c
) || c
== '_');
17167 * Set number v: variable to "val".
17170 set_vim_var_nr(idx
, val
)
17174 vimvars
[idx
].vv_nr
= val
;
17178 * Get number v: variable value.
17181 get_vim_var_nr(idx
)
17184 return vimvars
[idx
].vv_nr
;
17187 #if defined(FEAT_AUTOCMD) || defined(PROTO)
17189 * Get string v: variable value. Uses a static buffer, can only be used once.
17192 get_vim_var_str(idx
)
17195 return get_tv_string(&vimvars
[idx
].vv_tv
);
17200 * Set v:count, v:count1 and v:prevcount.
17203 set_vcount(count
, count1
)
17207 vimvars
[VV_PREVCOUNT
].vv_nr
= vimvars
[VV_COUNT
].vv_nr
;
17208 vimvars
[VV_COUNT
].vv_nr
= count
;
17209 vimvars
[VV_COUNT1
].vv_nr
= count1
;
17213 * Set string v: variable to a copy of "val".
17216 set_vim_var_string(idx
, val
, len
)
17219 int len
; /* length of "val" to use or -1 (whole string) */
17221 /* Need to do this (at least) once, since we can't initialize a union.
17222 * Will always be invoked when "v:progname" is set. */
17223 vimvars
[VV_VERSION
].vv_nr
= VIM_VERSION_100
;
17225 vim_free(vimvars
[idx
].vv_str
);
17227 vimvars
[idx
].vv_str
= NULL
;
17228 else if (len
== -1)
17229 vimvars
[idx
].vv_str
= vim_strsave(val
);
17231 vimvars
[idx
].vv_str
= vim_strnsave(val
, len
);
17235 * Set v:register if needed.
17243 if (c
== 0 || c
== ' ')
17247 /* Avoid free/alloc when the value is already right. */
17248 if (vimvars
[VV_REG
].vv_str
== NULL
|| vimvars
[VV_REG
].vv_str
[0] != c
)
17249 set_vim_var_string(VV_REG
, ®name
, 1);
17253 * Get or set v:exception. If "oldval" == NULL, return the current value.
17254 * Otherwise, restore the value to "oldval" and return NULL.
17255 * Must always be called in pairs to save and restore v:exception! Does not
17256 * take care of memory allocations.
17259 v_exception(oldval
)
17262 if (oldval
== NULL
)
17263 return vimvars
[VV_EXCEPTION
].vv_str
;
17265 vimvars
[VV_EXCEPTION
].vv_str
= oldval
;
17270 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
17271 * Otherwise, restore the value to "oldval" and return NULL.
17272 * Must always be called in pairs to save and restore v:throwpoint! Does not
17273 * take care of memory allocations.
17276 v_throwpoint(oldval
)
17279 if (oldval
== NULL
)
17280 return vimvars
[VV_THROWPOINT
].vv_str
;
17282 vimvars
[VV_THROWPOINT
].vv_str
= oldval
;
17286 #if defined(FEAT_AUTOCMD) || defined(PROTO)
17289 * If "eap" != NULL, use "eap" to generate the value and return the old value.
17290 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
17291 * Must always be called in pairs!
17294 set_cmdarg(eap
, oldarg
)
17302 oldval
= vimvars
[VV_CMDARG
].vv_str
;
17306 vimvars
[VV_CMDARG
].vv_str
= oldarg
;
17310 if (eap
->force_bin
== FORCE_BIN
)
17312 else if (eap
->force_bin
== FORCE_NOBIN
)
17317 if (eap
->read_edit
)
17320 if (eap
->force_ff
!= 0)
17321 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_ff
) + 6;
17323 if (eap
->force_enc
!= 0)
17324 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->force_enc
) + 7;
17325 if (eap
->bad_char
!= 0)
17326 len
+= (unsigned)STRLEN(eap
->cmd
+ eap
->bad_char
) + 7;
17329 newval
= alloc(len
+ 1);
17330 if (newval
== NULL
)
17333 if (eap
->force_bin
== FORCE_BIN
)
17334 sprintf((char *)newval
, " ++bin");
17335 else if (eap
->force_bin
== FORCE_NOBIN
)
17336 sprintf((char *)newval
, " ++nobin");
17340 if (eap
->read_edit
)
17341 STRCAT(newval
, " ++edit");
17343 if (eap
->force_ff
!= 0)
17344 sprintf((char *)newval
+ STRLEN(newval
), " ++ff=%s",
17345 eap
->cmd
+ eap
->force_ff
);
17347 if (eap
->force_enc
!= 0)
17348 sprintf((char *)newval
+ STRLEN(newval
), " ++enc=%s",
17349 eap
->cmd
+ eap
->force_enc
);
17350 if (eap
->bad_char
!= 0)
17351 sprintf((char *)newval
+ STRLEN(newval
), " ++bad=%s",
17352 eap
->cmd
+ eap
->bad_char
);
17354 vimvars
[VV_CMDARG
].vv_str
= newval
;
17360 * Get the value of internal variable "name".
17361 * Return OK or FAIL.
17364 get_var_tv(name
, len
, rettv
, verbose
)
17366 int len
; /* length of "name" */
17367 typval_T
*rettv
; /* NULL when only checking existence */
17368 int verbose
; /* may give error message */
17371 typval_T
*tv
= NULL
;
17376 /* truncate the name, so that we can use strcmp() */
17381 * Check for "b:changedtick".
17383 if (STRCMP(name
, "b:changedtick") == 0)
17385 atv
.v_type
= VAR_NUMBER
;
17386 atv
.vval
.v_number
= curbuf
->b_changedtick
;
17391 * Check for user-defined variables.
17395 v
= find_var(name
, NULL
);
17402 if (rettv
!= NULL
&& verbose
)
17403 EMSG2(_(e_undefvar
), name
);
17406 else if (rettv
!= NULL
)
17407 copy_tv(tv
, rettv
);
17415 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
17416 * Also handle function call with Funcref variable: func(expr)
17417 * Can all be combined: dict.func(expr)[idx]['func'](expr)
17420 handle_subscript(arg
, rettv
, evaluate
, verbose
)
17423 int evaluate
; /* do more than finding the end */
17424 int verbose
; /* give error messages */
17427 dict_T
*selfdict
= NULL
;
17434 || (**arg
== '.' && rettv
->v_type
== VAR_DICT
)
17435 || (**arg
== '(' && rettv
->v_type
== VAR_FUNC
))
17436 && !vim_iswhite(*(*arg
- 1)))
17440 /* need to copy the funcref so that we can clear rettv */
17442 rettv
->v_type
= VAR_UNKNOWN
;
17444 /* Invoke the function. Recursive! */
17445 s
= functv
.vval
.v_string
;
17446 ret
= get_func_tv(s
, (int)STRLEN(s
), rettv
, arg
,
17447 curwin
->w_cursor
.lnum
, curwin
->w_cursor
.lnum
,
17448 &len
, evaluate
, selfdict
);
17450 /* Clear the funcref afterwards, so that deleting it while
17451 * evaluating the arguments is possible (see test55). */
17454 /* Stop the expression evaluation when immediately aborting on
17455 * error, or when an interrupt occurred or an exception was thrown
17456 * but not caught. */
17463 dict_unref(selfdict
);
17466 else /* **arg == '[' || **arg == '.' */
17468 dict_unref(selfdict
);
17469 if (rettv
->v_type
== VAR_DICT
)
17471 selfdict
= rettv
->vval
.v_dict
;
17472 if (selfdict
!= NULL
)
17473 ++selfdict
->dv_refcount
;
17477 if (eval_index(arg
, rettv
, evaluate
, verbose
) == FAIL
)
17484 dict_unref(selfdict
);
17489 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
17495 return (typval_T
*)alloc_clear((unsigned)sizeof(typval_T
));
17499 * Allocate memory for a variable type-value, and assign a string to it.
17500 * The string "s" must have been allocated, it is consumed.
17501 * Return NULL for out of memory, the variable otherwise.
17509 rettv
= alloc_tv();
17512 rettv
->v_type
= VAR_STRING
;
17513 rettv
->vval
.v_string
= s
;
17521 * Free the memory for a variable type-value.
17529 switch (varp
->v_type
)
17532 func_unref(varp
->vval
.v_string
);
17535 vim_free(varp
->vval
.v_string
);
17538 list_unref(varp
->vval
.v_list
);
17541 dict_unref(varp
->vval
.v_dict
);
17547 EMSG2(_(e_intern2
), "free_tv()");
17555 * Free the memory for a variable value and set the value to NULL or 0.
17563 switch (varp
->v_type
)
17566 func_unref(varp
->vval
.v_string
);
17569 vim_free(varp
->vval
.v_string
);
17570 varp
->vval
.v_string
= NULL
;
17573 list_unref(varp
->vval
.v_list
);
17574 varp
->vval
.v_list
= NULL
;
17577 dict_unref(varp
->vval
.v_dict
);
17578 varp
->vval
.v_dict
= NULL
;
17581 varp
->vval
.v_number
= 0;
17586 EMSG2(_(e_intern2
), "clear_tv()");
17593 * Set the value of a variable to NULL without freeing items.
17600 vim_memset(varp
, 0, sizeof(typval_T
));
17604 * Get the number value of a variable.
17605 * If it is a String variable, uses vim_str2nr().
17606 * For incompatible types, return 0.
17607 * get_tv_number_chk() is similar to get_tv_number(), but informs the
17608 * caller of incompatible types: it sets *denote to TRUE if "denote"
17609 * is not NULL or returns -1 otherwise.
17612 get_tv_number(varp
)
17617 return get_tv_number_chk(varp
, &error
); /* return 0L on error */
17621 get_tv_number_chk(varp
, denote
)
17627 switch (varp
->v_type
)
17630 return (long)(varp
->vval
.v_number
);
17632 EMSG(_("E703: Using a Funcref as a number"));
17635 if (varp
->vval
.v_string
!= NULL
)
17636 vim_str2nr(varp
->vval
.v_string
, NULL
, NULL
,
17637 TRUE
, TRUE
, &n
, NULL
);
17640 EMSG(_("E745: Using a List as a number"));
17643 EMSG(_("E728: Using a Dictionary as a number"));
17646 EMSG2(_(e_intern2
), "get_tv_number()");
17649 if (denote
== NULL
) /* useful for values that must be unsigned */
17657 * Get the lnum from the first argument.
17658 * Also accepts ".", "$", etc., but that only works for the current buffer.
17659 * Returns -1 on error.
17662 get_tv_lnum(argvars
)
17668 lnum
= get_tv_number_chk(&argvars
[0], NULL
);
17669 if (lnum
== 0) /* no valid number, try using line() */
17671 rettv
.v_type
= VAR_NUMBER
;
17672 f_line(argvars
, &rettv
);
17673 lnum
= rettv
.vval
.v_number
;
17680 * Get the lnum from the first argument.
17681 * Also accepts "$", then "buf" is used.
17682 * Returns 0 on error.
17685 get_tv_lnum_buf(argvars
, buf
)
17689 if (argvars
[0].v_type
== VAR_STRING
17690 && argvars
[0].vval
.v_string
!= NULL
17691 && argvars
[0].vval
.v_string
[0] == '$'
17693 return buf
->b_ml
.ml_line_count
;
17694 return get_tv_number_chk(&argvars
[0], NULL
);
17698 * Get the string value of a variable.
17699 * If it is a Number variable, the number is converted into a string.
17700 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
17701 * get_tv_string_buf() uses a given buffer.
17702 * If the String variable has never been set, return an empty string.
17703 * Never returns NULL;
17704 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
17708 get_tv_string(varp
)
17711 static char_u mybuf
[NUMBUFLEN
];
17713 return get_tv_string_buf(varp
, mybuf
);
17717 get_tv_string_buf(varp
, buf
)
17721 char_u
*res
= get_tv_string_buf_chk(varp
, buf
);
17723 return res
!= NULL
? res
: (char_u
*)"";
17727 get_tv_string_chk(varp
)
17730 static char_u mybuf
[NUMBUFLEN
];
17732 return get_tv_string_buf_chk(varp
, mybuf
);
17736 get_tv_string_buf_chk(varp
, buf
)
17740 switch (varp
->v_type
)
17743 sprintf((char *)buf
, "%ld", (long)varp
->vval
.v_number
);
17746 EMSG(_("E729: using Funcref as a String"));
17749 EMSG(_("E730: using List as a String"));
17752 EMSG(_("E731: using Dictionary as a String"));
17755 if (varp
->vval
.v_string
!= NULL
)
17756 return varp
->vval
.v_string
;
17757 return (char_u
*)"";
17759 EMSG2(_(e_intern2
), "get_tv_string_buf()");
17766 * Find variable "name" in the list of variables.
17767 * Return a pointer to it if found, NULL if not found.
17768 * Careful: "a:0" variables don't have a name.
17769 * When "htp" is not NULL we are writing to the variable, set "htp" to the
17772 static dictitem_T
*
17773 find_var(name
, htp
)
17780 ht
= find_var_ht(name
, &varname
);
17785 return find_var_in_ht(ht
, varname
, htp
!= NULL
);
17789 * Find variable "varname" in hashtab "ht".
17790 * Returns NULL if not found.
17792 static dictitem_T
*
17793 find_var_in_ht(ht
, varname
, writing
)
17800 if (*varname
== NUL
)
17802 /* Must be something like "s:", otherwise "ht" would be NULL. */
17803 switch (varname
[-2])
17805 case 's': return &SCRIPT_SV(current_SID
).sv_var
;
17806 case 'g': return &globvars_var
;
17807 case 'v': return &vimvars_var
;
17808 case 'b': return &curbuf
->b_bufvar
;
17809 case 'w': return &curwin
->w_winvar
;
17810 #ifdef FEAT_WINDOWS
17811 case 't': return &curtab
->tp_winvar
;
17813 case 'l': return current_funccal
== NULL
17814 ? NULL
: ¤t_funccal
->l_vars_var
;
17815 case 'a': return current_funccal
== NULL
17816 ? NULL
: ¤t_funccal
->l_avars_var
;
17821 hi
= hash_find(ht
, varname
);
17822 if (HASHITEM_EMPTY(hi
))
17824 /* For global variables we may try auto-loading the script. If it
17825 * worked find the variable again. Don't auto-load a script if it was
17826 * loaded already, otherwise it would be loaded every time when
17827 * checking if a function name is a Funcref variable. */
17828 if (ht
== &globvarht
&& !writing
17829 && script_autoload(varname
, FALSE
) && !aborting())
17830 hi
= hash_find(ht
, varname
);
17831 if (HASHITEM_EMPTY(hi
))
17838 * Find the hashtab used for a variable name.
17839 * Set "varname" to the start of name without ':'.
17842 find_var_ht(name
, varname
)
17848 if (name
[1] != ':')
17850 /* The name must not start with a colon or #. */
17851 if (name
[0] == ':' || name
[0] == AUTOLOAD_CHAR
)
17855 /* "version" is "v:version" in all scopes */
17856 hi
= hash_find(&compat_hashtab
, name
);
17857 if (!HASHITEM_EMPTY(hi
))
17858 return &compat_hashtab
;
17860 if (current_funccal
== NULL
)
17861 return &globvarht
; /* global variable */
17862 return ¤t_funccal
->l_vars
.dv_hashtab
; /* l: variable */
17864 *varname
= name
+ 2;
17865 if (*name
== 'g') /* global variable */
17867 /* There must be no ':' or '#' in the rest of the name, unless g: is used
17869 if (vim_strchr(name
+ 2, ':') != NULL
17870 || vim_strchr(name
+ 2, AUTOLOAD_CHAR
) != NULL
)
17872 if (*name
== 'b') /* buffer variable */
17873 return &curbuf
->b_vars
.dv_hashtab
;
17874 if (*name
== 'w') /* window variable */
17875 return &curwin
->w_vars
.dv_hashtab
;
17876 #ifdef FEAT_WINDOWS
17877 if (*name
== 't') /* tab page variable */
17878 return &curtab
->tp_vars
.dv_hashtab
;
17880 if (*name
== 'v') /* v: variable */
17882 if (*name
== 'a' && current_funccal
!= NULL
) /* function argument */
17883 return ¤t_funccal
->l_avars
.dv_hashtab
;
17884 if (*name
== 'l' && current_funccal
!= NULL
) /* local function variable */
17885 return ¤t_funccal
->l_vars
.dv_hashtab
;
17886 if (*name
== 's' /* script variable */
17887 && current_SID
> 0 && current_SID
<= ga_scripts
.ga_len
)
17888 return &SCRIPT_VARS(current_SID
);
17893 * Get the string value of a (global/local) variable.
17894 * Returns NULL when it doesn't exist.
17897 get_var_value(name
)
17902 v
= find_var(name
, NULL
);
17905 return get_tv_string(&v
->di_tv
);
17909 * Allocate a new hashtab for a sourced script. It will be used while
17910 * sourcing this script and when executing functions defined in the script.
17913 new_script_vars(id
)
17920 if (ga_grow(&ga_scripts
, (int)(id
- ga_scripts
.ga_len
)) == OK
)
17922 /* Re-allocating ga_data means that an ht_array pointing to
17923 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
17924 * at its init value. Also reset "v_dict", it's always the same. */
17925 for (i
= 1; i
<= ga_scripts
.ga_len
; ++i
)
17927 ht
= &SCRIPT_VARS(i
);
17928 if (ht
->ht_mask
== HT_INIT_SIZE
- 1)
17929 ht
->ht_array
= ht
->ht_smallarray
;
17930 sv
= &SCRIPT_SV(i
);
17931 sv
->sv_var
.di_tv
.vval
.v_dict
= &sv
->sv_dict
;
17934 while (ga_scripts
.ga_len
< id
)
17936 sv
= &SCRIPT_SV(ga_scripts
.ga_len
+ 1);
17937 init_var_dict(&sv
->sv_dict
, &sv
->sv_var
);
17938 ++ga_scripts
.ga_len
;
17944 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
17948 init_var_dict(dict
, dict_var
)
17950 dictitem_T
*dict_var
;
17952 hash_init(&dict
->dv_hashtab
);
17953 dict
->dv_refcount
= 99999;
17954 dict_var
->di_tv
.vval
.v_dict
= dict
;
17955 dict_var
->di_tv
.v_type
= VAR_DICT
;
17956 dict_var
->di_tv
.v_lock
= VAR_FIXED
;
17957 dict_var
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
17958 dict_var
->di_key
[0] = NUL
;
17962 * Clean up a list of internal variables.
17963 * Frees all allocated variables and the value they contain.
17964 * Clears hashtab "ht", does not free it.
17970 vars_clear_ext(ht
, TRUE
);
17974 * Like vars_clear(), but only free the value if "free_val" is TRUE.
17977 vars_clear_ext(ht
, free_val
)
17986 todo
= (int)ht
->ht_used
;
17987 for (hi
= ht
->ht_array
; todo
> 0; ++hi
)
17989 if (!HASHITEM_EMPTY(hi
))
17993 /* Free the variable. Don't remove it from the hashtab,
17994 * ht_array might change then. hash_clear() takes care of it
17998 clear_tv(&v
->di_tv
);
17999 if ((v
->di_flags
& DI_FLAGS_FIX
) == 0)
18008 * Delete a variable from hashtab "ht" at item "hi".
18009 * Clear the variable value and free the dictitem.
18016 dictitem_T
*di
= HI2DI(hi
);
18018 hash_remove(ht
, hi
);
18019 clear_tv(&di
->di_tv
);
18024 * List the value of one internal variable.
18027 list_one_var(v
, prefix
, first
)
18034 char_u numbuf
[NUMBUFLEN
];
18036 s
= echo_string(&v
->di_tv
, &tofree
, numbuf
, ++current_copyID
);
18037 list_one_var_a(prefix
, v
->di_key
, v
->di_tv
.v_type
,
18038 s
== NULL
? (char_u
*)"" : s
, first
);
18043 list_one_var_a(prefix
, name
, type
, string
, first
)
18048 int *first
; /* when TRUE clear rest of screen and set to FALSE */
18050 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
18053 if (name
!= NULL
) /* "a:" vars don't have a name stored */
18057 if (type
== VAR_NUMBER
)
18059 else if (type
== VAR_FUNC
)
18061 else if (type
== VAR_LIST
)
18064 if (*string
== '[')
18067 else if (type
== VAR_DICT
)
18070 if (*string
== '{')
18076 msg_outtrans(string
);
18078 if (type
== VAR_FUNC
)
18079 msg_puts((char_u
*)"()");
18088 * Set variable "name" to value in "tv".
18089 * If the variable already exists, the value is updated.
18090 * Otherwise the variable is created.
18093 set_var(name
, tv
, copy
)
18096 int copy
; /* make copy of value in "tv" */
18103 if (tv
->v_type
== VAR_FUNC
)
18105 if (!(vim_strchr((char_u
*)"wbs", name
[0]) != NULL
&& name
[1] == ':')
18106 && !ASCII_ISUPPER((name
[0] != NUL
&& name
[1] == ':')
18107 ? name
[2] : name
[0]))
18109 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name
);
18112 if (function_exists(name
))
18114 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
18120 ht
= find_var_ht(name
, &varname
);
18121 if (ht
== NULL
|| *varname
== NUL
)
18123 EMSG2(_(e_illvar
), name
);
18127 v
= find_var_in_ht(ht
, varname
, TRUE
);
18130 /* existing variable, need to clear the value */
18131 if (var_check_ro(v
->di_flags
, name
)
18132 || tv_check_lock(v
->di_tv
.v_lock
, name
))
18134 if (v
->di_tv
.v_type
!= tv
->v_type
18135 && !((v
->di_tv
.v_type
== VAR_STRING
18136 || v
->di_tv
.v_type
== VAR_NUMBER
)
18137 && (tv
->v_type
== VAR_STRING
18138 || tv
->v_type
== VAR_NUMBER
)))
18140 EMSG2(_("E706: Variable type mismatch for: %s"), name
);
18145 * Handle setting internal v: variables separately: we don't change
18148 if (ht
== &vimvarht
)
18150 if (v
->di_tv
.v_type
== VAR_STRING
)
18152 vim_free(v
->di_tv
.vval
.v_string
);
18153 if (copy
|| tv
->v_type
!= VAR_STRING
)
18154 v
->di_tv
.vval
.v_string
= vim_strsave(get_tv_string(tv
));
18157 /* Take over the string to avoid an extra alloc/free. */
18158 v
->di_tv
.vval
.v_string
= tv
->vval
.v_string
;
18159 tv
->vval
.v_string
= NULL
;
18162 else if (v
->di_tv
.v_type
!= VAR_NUMBER
)
18163 EMSG2(_(e_intern2
), "set_var()");
18165 v
->di_tv
.vval
.v_number
= get_tv_number(tv
);
18169 clear_tv(&v
->di_tv
);
18171 else /* add a new variable */
18173 /* Can't add "v:" variable. */
18174 if (ht
== &vimvarht
)
18176 EMSG2(_(e_illvar
), name
);
18180 /* Make sure the variable name is valid. */
18181 for (p
= varname
; *p
!= NUL
; ++p
)
18182 if (!eval_isnamec1(*p
) && (p
== varname
|| !VIM_ISDIGIT(*p
))
18183 && *p
!= AUTOLOAD_CHAR
)
18185 EMSG2(_(e_illvar
), varname
);
18189 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
18190 + STRLEN(varname
)));
18193 STRCPY(v
->di_key
, varname
);
18194 if (hash_add(ht
, DI2HIKEY(v
)) == FAIL
)
18202 if (copy
|| tv
->v_type
== VAR_NUMBER
)
18203 copy_tv(tv
, &v
->di_tv
);
18207 v
->di_tv
.v_lock
= 0;
18213 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
18214 * Also give an error message.
18217 var_check_ro(flags
, name
)
18221 if (flags
& DI_FLAGS_RO
)
18223 EMSG2(_(e_readonlyvar
), name
);
18226 if ((flags
& DI_FLAGS_RO_SBX
) && sandbox
)
18228 EMSG2(_(e_readonlysbx
), name
);
18235 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
18236 * Also give an error message.
18239 var_check_fixed(flags
, name
)
18243 if (flags
& DI_FLAGS_FIX
)
18245 EMSG2(_("E795: Cannot delete variable %s"), name
);
18252 * Return TRUE if typeval "tv" is set to be locked (immutable).
18253 * Also give an error message, using "name".
18256 tv_check_lock(lock
, name
)
18260 if (lock
& VAR_LOCKED
)
18262 EMSG2(_("E741: Value is locked: %s"),
18263 name
== NULL
? (char_u
*)_("Unknown") : name
);
18266 if (lock
& VAR_FIXED
)
18268 EMSG2(_("E742: Cannot change value of %s"),
18269 name
== NULL
? (char_u
*)_("Unknown") : name
);
18276 * Copy the values from typval_T "from" to typval_T "to".
18277 * When needed allocates string or increases reference count.
18278 * Does not make a copy of a list or dict but copies the reference!
18285 to
->v_type
= from
->v_type
;
18287 switch (from
->v_type
)
18290 to
->vval
.v_number
= from
->vval
.v_number
;
18294 if (from
->vval
.v_string
== NULL
)
18295 to
->vval
.v_string
= NULL
;
18298 to
->vval
.v_string
= vim_strsave(from
->vval
.v_string
);
18299 if (from
->v_type
== VAR_FUNC
)
18300 func_ref(to
->vval
.v_string
);
18304 if (from
->vval
.v_list
== NULL
)
18305 to
->vval
.v_list
= NULL
;
18308 to
->vval
.v_list
= from
->vval
.v_list
;
18309 ++to
->vval
.v_list
->lv_refcount
;
18313 if (from
->vval
.v_dict
== NULL
)
18314 to
->vval
.v_dict
= NULL
;
18317 to
->vval
.v_dict
= from
->vval
.v_dict
;
18318 ++to
->vval
.v_dict
->dv_refcount
;
18322 EMSG2(_(e_intern2
), "copy_tv()");
18328 * Make a copy of an item.
18329 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
18330 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
18331 * reference to an already copied list/dict can be used.
18332 * Returns FAIL or OK.
18335 item_copy(from
, to
, deep
, copyID
)
18341 static int recurse
= 0;
18344 if (recurse
>= DICT_MAXNEST
)
18346 EMSG(_("E698: variable nested too deep for making a copy"));
18351 switch (from
->v_type
)
18359 to
->v_type
= VAR_LIST
;
18361 if (from
->vval
.v_list
== NULL
)
18362 to
->vval
.v_list
= NULL
;
18363 else if (copyID
!= 0 && from
->vval
.v_list
->lv_copyID
== copyID
)
18365 /* use the copy made earlier */
18366 to
->vval
.v_list
= from
->vval
.v_list
->lv_copylist
;
18367 ++to
->vval
.v_list
->lv_refcount
;
18370 to
->vval
.v_list
= list_copy(from
->vval
.v_list
, deep
, copyID
);
18371 if (to
->vval
.v_list
== NULL
)
18375 to
->v_type
= VAR_DICT
;
18377 if (from
->vval
.v_dict
== NULL
)
18378 to
->vval
.v_dict
= NULL
;
18379 else if (copyID
!= 0 && from
->vval
.v_dict
->dv_copyID
== copyID
)
18381 /* use the copy made earlier */
18382 to
->vval
.v_dict
= from
->vval
.v_dict
->dv_copydict
;
18383 ++to
->vval
.v_dict
->dv_refcount
;
18386 to
->vval
.v_dict
= dict_copy(from
->vval
.v_dict
, deep
, copyID
);
18387 if (to
->vval
.v_dict
== NULL
)
18391 EMSG2(_(e_intern2
), "item_copy()");
18399 * ":echo expr1 ..." print each argument separated with a space, add a
18400 * newline at the end.
18401 * ":echon expr1 ..." print each argument plain.
18407 char_u
*arg
= eap
->arg
;
18411 int needclr
= TRUE
;
18412 int atstart
= TRUE
;
18413 char_u numbuf
[NUMBUFLEN
];
18417 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n' && !got_int
)
18420 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
18423 * Report the invalid expression unless the expression evaluation
18424 * has been cancelled due to an aborting error, an interrupt, or an
18428 EMSG2(_(e_invexpr2
), p
);
18436 /* Call msg_start() after eval1(), evaluating the expression
18437 * may cause a message to appear. */
18438 if (eap
->cmdidx
== CMD_echo
)
18441 else if (eap
->cmdidx
== CMD_echo
)
18442 msg_puts_attr((char_u
*)" ", echo_attr
);
18443 p
= echo_string(&rettv
, &tofree
, numbuf
, ++current_copyID
);
18445 for ( ; *p
!= NUL
&& !got_int
; ++p
)
18447 if (*p
== '\n' || *p
== '\r' || *p
== TAB
)
18449 if (*p
!= TAB
&& needclr
)
18451 /* remove any text still there from the command */
18455 msg_putchar_attr(*p
, echo_attr
);
18462 int i
= (*mb_ptr2len
)(p
);
18464 (void)msg_outtrans_len_attr(p
, i
, echo_attr
);
18469 (void)msg_outtrans_len_attr(p
, 1, echo_attr
);
18475 arg
= skipwhite(arg
);
18477 eap
->nextcmd
= check_nextcmd(arg
);
18483 /* remove text that may still be there from the command */
18486 if (eap
->cmdidx
== CMD_echo
)
18492 * ":echohl {name}".
18500 id
= syn_name2id(eap
->arg
);
18504 echo_attr
= syn_id2attr(id
);
18508 * ":execute expr1 ..." execute the result of an expression.
18509 * ":echomsg expr1 ..." Print a message
18510 * ":echoerr expr1 ..." Print an error
18511 * Each gets spaces around each argument and a newline at the end for
18518 char_u
*arg
= eap
->arg
;
18526 ga_init2(&ga
, 1, 80);
18530 while (*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
18533 if (eval1(&arg
, &rettv
, !eap
->skip
) == FAIL
)
18536 * Report the invalid expression unless the expression evaluation
18537 * has been cancelled due to an aborting error, an interrupt, or an
18541 EMSG2(_(e_invexpr2
), p
);
18548 p
= get_tv_string(&rettv
);
18549 len
= (int)STRLEN(p
);
18550 if (ga_grow(&ga
, len
+ 2) == FAIL
)
18557 ((char_u
*)(ga
.ga_data
))[ga
.ga_len
++] = ' ';
18558 STRCPY((char_u
*)(ga
.ga_data
) + ga
.ga_len
, p
);
18563 arg
= skipwhite(arg
);
18566 if (ret
!= FAIL
&& ga
.ga_data
!= NULL
)
18568 if (eap
->cmdidx
== CMD_echomsg
)
18570 MSG_ATTR(ga
.ga_data
, echo_attr
);
18573 else if (eap
->cmdidx
== CMD_echoerr
)
18575 /* We don't want to abort following commands, restore did_emsg. */
18576 save_did_emsg
= did_emsg
;
18577 EMSG((char_u
*)ga
.ga_data
);
18579 did_emsg
= save_did_emsg
;
18581 else if (eap
->cmdidx
== CMD_execute
)
18582 do_cmdline((char_u
*)ga
.ga_data
,
18583 eap
->getline
, eap
->cookie
, DOCMD_NOWAIT
|DOCMD_VERBOSE
);
18591 eap
->nextcmd
= check_nextcmd(arg
);
18595 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
18596 * "arg" points to the "&" or '+' when called, to "option" when returning.
18597 * Returns NULL when no option name found. Otherwise pointer to the char
18598 * after the option name.
18601 find_option_end(arg
, opt_flags
)
18608 if (*p
== 'g' && p
[1] == ':')
18610 *opt_flags
= OPT_GLOBAL
;
18613 else if (*p
== 'l' && p
[1] == ':')
18615 *opt_flags
= OPT_LOCAL
;
18621 if (!ASCII_ISALPHA(*p
))
18625 if (p
[0] == 't' && p
[1] == '_' && p
[2] != NUL
&& p
[3] != NUL
)
18626 p
+= 4; /* termcap option */
18628 while (ASCII_ISALPHA(*p
))
18643 int saved_did_emsg
;
18644 char_u
*name
= NULL
;
18647 char_u
*line_arg
= NULL
;
18650 int varargs
= FALSE
;
18651 int mustend
= FALSE
;
18656 char_u
*skip_until
= NULL
;
18659 static int func_nr
= 0; /* number for nameless function */
18664 int sourcing_lnum_off
;
18667 * ":function" without argument: list functions.
18669 if (ends_excmd(*eap
->arg
))
18673 todo
= (int)func_hashtab
.ht_used
;
18674 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
18676 if (!HASHITEM_EMPTY(hi
))
18680 if (!isdigit(*fp
->uf_name
))
18681 list_func_head(fp
, FALSE
);
18685 eap
->nextcmd
= check_nextcmd(eap
->arg
);
18690 * ":function /pat": list functions matching pattern.
18692 if (*eap
->arg
== '/')
18694 p
= skip_regexp(eap
->arg
+ 1, '/', TRUE
, NULL
);
18697 regmatch_T regmatch
;
18701 regmatch
.regprog
= vim_regcomp(eap
->arg
+ 1, RE_MAGIC
);
18703 if (regmatch
.regprog
!= NULL
)
18705 regmatch
.rm_ic
= p_ic
;
18707 todo
= (int)func_hashtab
.ht_used
;
18708 for (hi
= func_hashtab
.ht_array
; todo
> 0 && !got_int
; ++hi
)
18710 if (!HASHITEM_EMPTY(hi
))
18714 if (!isdigit(*fp
->uf_name
)
18715 && vim_regexec(®match
, fp
->uf_name
, 0))
18716 list_func_head(fp
, FALSE
);
18723 eap
->nextcmd
= check_nextcmd(p
);
18728 * Get the function name. There are these situations:
18729 * func normal function name
18730 * "name" == func, "fudi.fd_dict" == NULL
18731 * dict.func new dictionary entry
18732 * "name" == NULL, "fudi.fd_dict" set,
18733 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
18734 * dict.func existing dict entry with a Funcref
18735 * "name" == func, "fudi.fd_dict" set,
18736 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18737 * dict.func existing dict entry that's not a Funcref
18738 * "name" == NULL, "fudi.fd_dict" set,
18739 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
18742 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
18743 paren
= (vim_strchr(p
, '(') != NULL
);
18744 if (name
== NULL
&& (fudi
.fd_dict
== NULL
|| !paren
) && !eap
->skip
)
18747 * Return on an invalid expression in braces, unless the expression
18748 * evaluation has been cancelled due to an aborting error, an
18749 * interrupt, or an exception.
18753 if (!eap
->skip
&& fudi
.fd_newkey
!= NULL
)
18754 EMSG2(_(e_dictkey
), fudi
.fd_newkey
);
18755 vim_free(fudi
.fd_newkey
);
18762 /* An error in a function call during evaluation of an expression in magic
18763 * braces should not cause the function not to be defined. */
18764 saved_did_emsg
= did_emsg
;
18768 * ":function func" with only function name: list function.
18772 if (!ends_excmd(*skipwhite(p
)))
18774 EMSG(_(e_trailing
));
18777 eap
->nextcmd
= check_nextcmd(p
);
18778 if (eap
->nextcmd
!= NULL
)
18780 if (!eap
->skip
&& !got_int
)
18782 fp
= find_func(name
);
18785 list_func_head(fp
, TRUE
);
18786 for (j
= 0; j
< fp
->uf_lines
.ga_len
&& !got_int
; ++j
)
18788 if (FUNCLINE(fp
, j
) == NULL
)
18791 msg_outnum((long)(j
+ 1));
18796 msg_prt_line(FUNCLINE(fp
, j
), FALSE
);
18797 out_flush(); /* show a line at a time */
18803 msg_puts((char_u
*)" endfunction");
18807 emsg_funcname("E123: Undefined function: %s", name
);
18813 * ":function name(arg1, arg2)" Define function.
18820 EMSG2(_("E124: Missing '(': %s"), eap
->arg
);
18823 /* attempt to continue by skipping some text */
18824 if (vim_strchr(p
, '(') != NULL
)
18825 p
= vim_strchr(p
, '(');
18827 p
= skipwhite(p
+ 1);
18829 ga_init2(&newargs
, (int)sizeof(char_u
*), 3);
18830 ga_init2(&newlines
, (int)sizeof(char_u
*), 3);
18834 /* Check the name of the function. Unless it's a dictionary function
18835 * (that we are overwriting). */
18839 arg
= fudi
.fd_newkey
;
18840 if (arg
!= NULL
&& (fudi
.fd_di
== NULL
18841 || fudi
.fd_di
->di_tv
.v_type
!= VAR_FUNC
))
18843 if (*arg
== K_SPECIAL
)
18847 while (arg
[j
] != NUL
&& (j
== 0 ? eval_isnamec1(arg
[j
])
18848 : eval_isnamec(arg
[j
])))
18851 emsg_funcname(_(e_invarg2
), arg
);
18856 * Isolate the arguments: "arg1, arg2, ...)"
18860 if (p
[0] == '.' && p
[1] == '.' && p
[2] == '.')
18869 while (ASCII_ISALNUM(*p
) || *p
== '_')
18871 if (arg
== p
|| isdigit(*arg
)
18872 || (p
- arg
== 9 && STRNCMP(arg
, "firstline", 9) == 0)
18873 || (p
- arg
== 8 && STRNCMP(arg
, "lastline", 8) == 0))
18876 EMSG2(_("E125: Illegal argument: %s"), arg
);
18879 if (ga_grow(&newargs
, 1) == FAIL
)
18883 arg
= vim_strsave(arg
);
18886 ((char_u
**)(newargs
.ga_data
))[newargs
.ga_len
] = arg
;
18895 if (mustend
&& *p
!= ')')
18898 EMSG2(_(e_invarg2
), eap
->arg
);
18902 ++p
; /* skip the ')' */
18904 /* find extra arguments "range", "dict" and "abort" */
18908 if (STRNCMP(p
, "range", 5) == 0)
18913 else if (STRNCMP(p
, "dict", 4) == 0)
18918 else if (STRNCMP(p
, "abort", 5) == 0)
18927 /* When there is a line break use what follows for the function body.
18928 * Makes 'exe "func Test()\n...\nendfunc"' work. */
18931 else if (*p
!= NUL
&& *p
!= '"' && !eap
->skip
&& !did_emsg
)
18932 EMSG(_(e_trailing
));
18935 * Read the body of the function, until ":endfunction" is found.
18939 /* Check if the function already exists, don't let the user type the
18940 * whole function before telling him it doesn't work! For a script we
18941 * need to skip the body to be able to find what follows. */
18942 if (!eap
->skip
&& !eap
->forceit
)
18944 if (fudi
.fd_dict
!= NULL
&& fudi
.fd_newkey
== NULL
)
18945 EMSG(_(e_funcdict
));
18946 else if (name
!= NULL
&& find_func(name
) != NULL
)
18947 emsg_funcname(e_funcexts
, name
);
18950 if (!eap
->skip
&& did_emsg
)
18953 msg_putchar('\n'); /* don't overwrite the function name */
18954 cmdline_row
= msg_row
;
18962 need_wait_return
= FALSE
;
18963 sourcing_lnum_off
= sourcing_lnum
;
18965 if (line_arg
!= NULL
)
18967 /* Use eap->arg, split up in parts by line breaks. */
18968 theline
= line_arg
;
18969 p
= vim_strchr(theline
, '\n');
18971 line_arg
+= STRLEN(line_arg
);
18978 else if (eap
->getline
== NULL
)
18979 theline
= getcmdline(':', 0L, indent
);
18981 theline
= eap
->getline(':', eap
->cookie
, indent
);
18983 lines_left
= Rows
- 1;
18984 if (theline
== NULL
)
18986 EMSG(_("E126: Missing :endfunction"));
18990 /* Detect line continuation: sourcing_lnum increased more than one. */
18991 if (sourcing_lnum
> sourcing_lnum_off
+ 1)
18992 sourcing_lnum_off
= sourcing_lnum
- sourcing_lnum_off
- 1;
18994 sourcing_lnum_off
= 0;
18996 if (skip_until
!= NULL
)
18998 /* between ":append" and "." and between ":python <<EOF" and "EOF"
18999 * don't check for ":endfunc". */
19000 if (STRCMP(theline
, skip_until
) == 0)
19002 vim_free(skip_until
);
19008 /* skip ':' and blanks*/
19009 for (p
= theline
; vim_iswhite(*p
) || *p
== ':'; ++p
)
19012 /* Check for "endfunction". */
19013 if (checkforcmd(&p
, "endfunction", 4) && nesting
-- == 0)
19015 if (line_arg
== NULL
)
19020 /* Increase indent inside "if", "while", "for" and "try", decrease
19022 if (indent
> 2 && STRNCMP(p
, "end", 3) == 0)
19024 else if (STRNCMP(p
, "if", 2) == 0
19025 || STRNCMP(p
, "wh", 2) == 0
19026 || STRNCMP(p
, "for", 3) == 0
19027 || STRNCMP(p
, "try", 3) == 0)
19030 /* Check for defining a function inside this function. */
19031 if (checkforcmd(&p
, "function", 2))
19034 p
= skipwhite(p
+ 1);
19035 p
+= eval_fname_script(p
);
19036 if (ASCII_ISALPHA(*p
))
19038 vim_free(trans_function_name(&p
, TRUE
, 0, NULL
));
19039 if (*skipwhite(p
) == '(')
19047 /* Check for ":append" or ":insert". */
19048 p
= skip_range(p
, NULL
);
19049 if ((p
[0] == 'a' && (!ASCII_ISALPHA(p
[1]) || p
[1] == 'p'))
19051 && (!ASCII_ISALPHA(p
[1]) || (p
[1] == 'n'
19052 && (!ASCII_ISALPHA(p
[2]) || (p
[2] == 's'))))))
19053 skip_until
= vim_strsave((char_u
*)".");
19055 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
19056 arg
= skipwhite(skiptowhite(p
));
19057 if (arg
[0] == '<' && arg
[1] =='<'
19058 && ((p
[0] == 'p' && p
[1] == 'y'
19059 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 't'))
19060 || (p
[0] == 'p' && p
[1] == 'e'
19061 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'r'))
19062 || (p
[0] == 't' && p
[1] == 'c'
19063 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 'l'))
19064 || (p
[0] == 'r' && p
[1] == 'u' && p
[2] == 'b'
19065 && (!ASCII_ISALPHA(p
[3]) || p
[3] == 'y'))
19066 || (p
[0] == 'm' && p
[1] == 'z'
19067 && (!ASCII_ISALPHA(p
[2]) || p
[2] == 's'))
19070 /* ":python <<" continues until a dot, like ":append" */
19071 p
= skipwhite(arg
+ 2);
19073 skip_until
= vim_strsave((char_u
*)".");
19075 skip_until
= vim_strsave(p
);
19079 /* Add the line to the function. */
19080 if (ga_grow(&newlines
, 1 + sourcing_lnum_off
) == FAIL
)
19082 if (line_arg
== NULL
)
19087 /* Copy the line to newly allocated memory. get_one_sourceline()
19088 * allocates 250 bytes per line, this saves 80% on average. The cost
19089 * is an extra alloc/free. */
19090 p
= vim_strsave(theline
);
19093 if (line_arg
== NULL
)
19098 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = theline
;
19100 /* Add NULL lines for continuation lines, so that the line count is
19101 * equal to the index in the growarray. */
19102 while (sourcing_lnum_off
-- > 0)
19103 ((char_u
**)(newlines
.ga_data
))[newlines
.ga_len
++] = NULL
;
19105 /* Check for end of eap->arg. */
19106 if (line_arg
!= NULL
&& *line_arg
== NUL
)
19110 /* Don't define the function when skipping commands or when an error was
19112 if (eap
->skip
|| did_emsg
)
19116 * If there are no errors, add the function
19118 if (fudi
.fd_dict
== NULL
)
19120 v
= find_var(name
, &ht
);
19121 if (v
!= NULL
&& v
->di_tv
.v_type
== VAR_FUNC
)
19123 emsg_funcname("E707: Function name conflicts with variable: %s",
19128 fp
= find_func(name
);
19133 emsg_funcname(e_funcexts
, name
);
19136 if (fp
->uf_calls
> 0)
19138 emsg_funcname("E127: Cannot redefine function %s: It is in use",
19142 /* redefine existing function */
19143 ga_clear_strings(&(fp
->uf_args
));
19144 ga_clear_strings(&(fp
->uf_lines
));
19154 if (fudi
.fd_newkey
== NULL
&& !eap
->forceit
)
19156 EMSG(_(e_funcdict
));
19159 if (fudi
.fd_di
== NULL
)
19161 /* Can't add a function to a locked dictionary */
19162 if (tv_check_lock(fudi
.fd_dict
->dv_lock
, eap
->arg
))
19165 /* Can't change an existing function if it is locked */
19166 else if (tv_check_lock(fudi
.fd_di
->di_tv
.v_lock
, eap
->arg
))
19169 /* Give the function a sequential number. Can only be used with a
19172 sprintf(numbuf
, "%d", ++func_nr
);
19173 name
= vim_strsave((char_u
*)numbuf
);
19180 if (fudi
.fd_dict
== NULL
&& vim_strchr(name
, AUTOLOAD_CHAR
) != NULL
)
19183 char_u
*scriptname
;
19185 /* Check that the autoload name matches the script name. */
19187 if (sourcing_name
!= NULL
)
19189 scriptname
= autoload_name(name
);
19190 if (scriptname
!= NULL
)
19192 p
= vim_strchr(scriptname
, '/');
19193 plen
= (int)STRLEN(p
);
19194 slen
= (int)STRLEN(sourcing_name
);
19195 if (slen
> plen
&& fnamecmp(p
,
19196 sourcing_name
+ slen
- plen
) == 0)
19198 vim_free(scriptname
);
19203 EMSG2(_("E746: Function name does not match script file name: %s"), name
);
19208 fp
= (ufunc_T
*)alloc((unsigned)(sizeof(ufunc_T
) + STRLEN(name
)));
19212 if (fudi
.fd_dict
!= NULL
)
19214 if (fudi
.fd_di
== NULL
)
19216 /* add new dict entry */
19217 fudi
.fd_di
= dictitem_alloc(fudi
.fd_newkey
);
19218 if (fudi
.fd_di
== NULL
)
19223 if (dict_add(fudi
.fd_dict
, fudi
.fd_di
) == FAIL
)
19225 vim_free(fudi
.fd_di
);
19231 /* overwrite existing dict entry */
19232 clear_tv(&fudi
.fd_di
->di_tv
);
19233 fudi
.fd_di
->di_tv
.v_type
= VAR_FUNC
;
19234 fudi
.fd_di
->di_tv
.v_lock
= 0;
19235 fudi
.fd_di
->di_tv
.vval
.v_string
= vim_strsave(name
);
19236 fp
->uf_refcount
= 1;
19238 /* behave like "dict" was used */
19242 /* insert the new function in the function list */
19243 STRCPY(fp
->uf_name
, name
);
19244 hash_add(&func_hashtab
, UF2HIKEY(fp
));
19246 fp
->uf_args
= newargs
;
19247 fp
->uf_lines
= newlines
;
19248 #ifdef FEAT_PROFILE
19249 fp
->uf_tml_count
= NULL
;
19250 fp
->uf_tml_total
= NULL
;
19251 fp
->uf_tml_self
= NULL
;
19252 fp
->uf_profiling
= FALSE
;
19253 if (prof_def_func())
19254 func_do_profile(fp
);
19256 fp
->uf_varargs
= varargs
;
19257 fp
->uf_flags
= flags
;
19259 fp
->uf_script_ID
= current_SID
;
19263 ga_clear_strings(&newargs
);
19264 ga_clear_strings(&newlines
);
19266 vim_free(skip_until
);
19267 vim_free(fudi
.fd_newkey
);
19269 did_emsg
|= saved_did_emsg
;
19273 * Get a function name, translating "<SID>" and "<SNR>".
19274 * Also handles a Funcref in a List or Dictionary.
19275 * Returns the function name in allocated memory, or NULL for failure.
19277 * TFN_INT: internal function name OK
19278 * TFN_QUIET: be quiet
19279 * Advances "pp" to just after the function name (if no error).
19282 trans_function_name(pp
, skip
, flags
, fdp
)
19284 int skip
; /* only find the end, don't evaluate */
19286 funcdict_T
*fdp
; /* return: info about dictionary used */
19288 char_u
*name
= NULL
;
19292 char_u sid_buf
[20];
19297 vim_memset(fdp
, 0, sizeof(funcdict_T
));
19300 /* Check for hard coded <SNR>: already translated function ID (from a user
19302 if ((*pp
)[0] == K_SPECIAL
&& (*pp
)[1] == KS_EXTRA
19303 && (*pp
)[2] == (int)KE_SNR
)
19306 len
= get_id_len(pp
) + 3;
19307 return vim_strnsave(start
, len
);
19310 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
19311 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
19312 lead
= eval_fname_script(start
);
19316 end
= get_lval(start
, NULL
, &lv
, FALSE
, skip
, flags
& TFN_QUIET
,
19317 lead
> 2 ? 0 : FNE_CHECK_START
);
19321 EMSG(_("E129: Function name required"));
19324 if (end
== NULL
|| (lv
.ll_tv
!= NULL
&& (lead
> 2 || lv
.ll_range
)))
19327 * Report an invalid expression in braces, unless the expression
19328 * evaluation has been cancelled due to an aborting error, an
19329 * interrupt, or an exception.
19334 EMSG2(_(e_invarg2
), start
);
19337 *pp
= find_name_end(start
, NULL
, NULL
, FNE_INCL_BR
);
19341 if (lv
.ll_tv
!= NULL
)
19345 fdp
->fd_dict
= lv
.ll_dict
;
19346 fdp
->fd_newkey
= lv
.ll_newkey
;
19347 lv
.ll_newkey
= NULL
;
19348 fdp
->fd_di
= lv
.ll_di
;
19350 if (lv
.ll_tv
->v_type
== VAR_FUNC
&& lv
.ll_tv
->vval
.v_string
!= NULL
)
19352 name
= vim_strsave(lv
.ll_tv
->vval
.v_string
);
19357 if (!skip
&& !(flags
& TFN_QUIET
) && (fdp
== NULL
19358 || lv
.ll_dict
== NULL
|| fdp
->fd_newkey
== NULL
))
19359 EMSG(_(e_funcref
));
19367 if (lv
.ll_name
== NULL
)
19369 /* Error found, but continue after the function name. */
19374 if (lv
.ll_exp_name
!= NULL
)
19376 len
= (int)STRLEN(lv
.ll_exp_name
);
19377 if (lead
<= 2 && lv
.ll_name
== lv
.ll_exp_name
19378 && STRNCMP(lv
.ll_name
, "s:", 2) == 0)
19380 /* When there was "s:" already or the name expanded to get a
19381 * leading "s:" then remove it. */
19389 if (lead
== 2) /* skip over "s:" */
19391 len
= (int)(end
- lv
.ll_name
);
19395 * Copy the function name to allocated memory.
19396 * Accept <SID>name() inside a script, translate into <SNR>123_name().
19397 * Accept <SNR>123_name() outside a script.
19400 lead
= 0; /* do nothing */
19404 if ((lv
.ll_exp_name
!= NULL
&& eval_fname_sid(lv
.ll_exp_name
))
19405 || eval_fname_sid(*pp
))
19407 /* It's "s:" or "<SID>" */
19408 if (current_SID
<= 0)
19410 EMSG(_(e_usingsid
));
19413 sprintf((char *)sid_buf
, "%ld_", (long)current_SID
);
19414 lead
+= (int)STRLEN(sid_buf
);
19417 else if (!(flags
& TFN_INT
) && builtin_function(lv
.ll_name
))
19419 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv
.ll_name
);
19422 name
= alloc((unsigned)(len
+ lead
+ 1));
19427 name
[0] = K_SPECIAL
;
19428 name
[1] = KS_EXTRA
;
19429 name
[2] = (int)KE_SNR
;
19430 if (lead
> 3) /* If it's "<SID>" */
19431 STRCPY(name
+ 3, sid_buf
);
19433 mch_memmove(name
+ lead
, lv
.ll_name
, (size_t)len
);
19434 name
[len
+ lead
] = NUL
;
19444 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
19445 * Return 2 if "p" starts with "s:".
19446 * Return 0 otherwise.
19449 eval_fname_script(p
)
19452 if (p
[0] == '<' && (STRNICMP(p
+ 1, "SID>", 4) == 0
19453 || STRNICMP(p
+ 1, "SNR>", 4) == 0))
19455 if (p
[0] == 's' && p
[1] == ':')
19461 * Return TRUE if "p" starts with "<SID>" or "s:".
19462 * Only works if eval_fname_script() returned non-zero for "p"!
19468 return (*p
== 's' || TOUPPER_ASC(p
[2]) == 'I');
19472 * List the head of the function: "name(arg1, arg2)".
19475 list_func_head(fp
, indent
)
19484 MSG_PUTS("function ");
19485 if (fp
->uf_name
[0] == K_SPECIAL
)
19487 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8
));
19488 msg_puts(fp
->uf_name
+ 3);
19491 msg_puts(fp
->uf_name
);
19493 for (j
= 0; j
< fp
->uf_args
.ga_len
; ++j
)
19497 msg_puts(FUNCARG(fp
, j
));
19499 if (fp
->uf_varargs
)
19508 last_set_msg(fp
->uf_script_ID
);
19512 * Find a function by name, return pointer to it in ufuncs.
19513 * Return NULL for unknown function.
19521 hi
= hash_find(&func_hashtab
, name
);
19522 if (!HASHITEM_EMPTY(hi
))
19527 #if defined(EXITFREE) || defined(PROTO)
19529 free_all_functions()
19533 /* Need to start all over every time, because func_free() may change the
19535 while (func_hashtab
.ht_used
> 0)
19536 for (hi
= func_hashtab
.ht_array
; ; ++hi
)
19537 if (!HASHITEM_EMPTY(hi
))
19539 func_free(HI2UF(hi
));
19546 * Return TRUE if a function "name" exists.
19549 function_exists(name
)
19556 p
= trans_function_name(&nm
, FALSE
, TFN_INT
|TFN_QUIET
, NULL
);
19557 nm
= skipwhite(nm
);
19559 /* Only accept "funcname", "funcname ", "funcname (..." and
19560 * "funcname(...", not "funcname!...". */
19561 if (p
!= NULL
&& (*nm
== NUL
|| *nm
== '('))
19563 if (builtin_function(p
))
19564 n
= (find_internal_func(p
) >= 0);
19566 n
= (find_func(p
) != NULL
);
19573 * Return TRUE if "name" looks like a builtin function name: starts with a
19574 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
19577 builtin_function(name
)
19580 return ASCII_ISLOWER(name
[0]) && vim_strchr(name
, ':') == NULL
19581 && vim_strchr(name
, AUTOLOAD_CHAR
) == NULL
;
19584 #if defined(FEAT_PROFILE) || defined(PROTO)
19586 * Start profiling function "fp".
19589 func_do_profile(fp
)
19592 fp
->uf_tm_count
= 0;
19593 profile_zero(&fp
->uf_tm_self
);
19594 profile_zero(&fp
->uf_tm_total
);
19595 if (fp
->uf_tml_count
== NULL
)
19596 fp
->uf_tml_count
= (int *)alloc_clear((unsigned)
19597 (sizeof(int) * fp
->uf_lines
.ga_len
));
19598 if (fp
->uf_tml_total
== NULL
)
19599 fp
->uf_tml_total
= (proftime_T
*)alloc_clear((unsigned)
19600 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
19601 if (fp
->uf_tml_self
== NULL
)
19602 fp
->uf_tml_self
= (proftime_T
*)alloc_clear((unsigned)
19603 (sizeof(proftime_T
) * fp
->uf_lines
.ga_len
));
19604 fp
->uf_tml_idx
= -1;
19605 if (fp
->uf_tml_count
== NULL
|| fp
->uf_tml_total
== NULL
19606 || fp
->uf_tml_self
== NULL
)
19607 return; /* out of memory */
19609 fp
->uf_profiling
= TRUE
;
19613 * Dump the profiling results for all functions in file "fd".
19616 func_dump_profile(fd
)
19626 todo
= (int)func_hashtab
.ht_used
;
19627 sorttab
= (ufunc_T
**)alloc((unsigned)(sizeof(ufunc_T
) * todo
));
19629 for (hi
= func_hashtab
.ht_array
; todo
> 0; ++hi
)
19631 if (!HASHITEM_EMPTY(hi
))
19635 if (fp
->uf_profiling
)
19637 if (sorttab
!= NULL
)
19638 sorttab
[st_len
++] = fp
;
19640 if (fp
->uf_name
[0] == K_SPECIAL
)
19641 fprintf(fd
, "FUNCTION <SNR>%s()\n", fp
->uf_name
+ 3);
19643 fprintf(fd
, "FUNCTION %s()\n", fp
->uf_name
);
19644 if (fp
->uf_tm_count
== 1)
19645 fprintf(fd
, "Called 1 time\n");
19647 fprintf(fd
, "Called %d times\n", fp
->uf_tm_count
);
19648 fprintf(fd
, "Total time: %s\n", profile_msg(&fp
->uf_tm_total
));
19649 fprintf(fd
, " Self time: %s\n", profile_msg(&fp
->uf_tm_self
));
19651 fprintf(fd
, "count total (s) self (s)\n");
19653 for (i
= 0; i
< fp
->uf_lines
.ga_len
; ++i
)
19655 if (FUNCLINE(fp
, i
) == NULL
)
19657 prof_func_line(fd
, fp
->uf_tml_count
[i
],
19658 &fp
->uf_tml_total
[i
], &fp
->uf_tml_self
[i
], TRUE
);
19659 fprintf(fd
, "%s\n", FUNCLINE(fp
, i
));
19666 if (sorttab
!= NULL
&& st_len
> 0)
19668 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
19670 prof_sort_list(fd
, sorttab
, st_len
, "TOTAL", FALSE
);
19671 qsort((void *)sorttab
, (size_t)st_len
, sizeof(ufunc_T
*),
19673 prof_sort_list(fd
, sorttab
, st_len
, "SELF", TRUE
);
19678 prof_sort_list(fd
, sorttab
, st_len
, title
, prefer_self
)
19683 int prefer_self
; /* when equal print only self time */
19688 fprintf(fd
, "FUNCTIONS SORTED ON %s TIME\n", title
);
19689 fprintf(fd
, "count total (s) self (s) function\n");
19690 for (i
= 0; i
< 20 && i
< st_len
; ++i
)
19693 prof_func_line(fd
, fp
->uf_tm_count
, &fp
->uf_tm_total
, &fp
->uf_tm_self
,
19695 if (fp
->uf_name
[0] == K_SPECIAL
)
19696 fprintf(fd
, " <SNR>%s()\n", fp
->uf_name
+ 3);
19698 fprintf(fd
, " %s()\n", fp
->uf_name
);
19704 * Print the count and times for one function or function line.
19707 prof_func_line(fd
, count
, total
, self
, prefer_self
)
19712 int prefer_self
; /* when equal print only self time */
19716 fprintf(fd
, "%5d ", count
);
19717 if (prefer_self
&& profile_equal(total
, self
))
19720 fprintf(fd
, "%s ", profile_msg(total
));
19721 if (!prefer_self
&& profile_equal(total
, self
))
19724 fprintf(fd
, "%s ", profile_msg(self
));
19731 * Compare function for total time sorting.
19734 #ifdef __BORLANDC__
19737 prof_total_cmp(s1
, s2
)
19743 p1
= *(ufunc_T
**)s1
;
19744 p2
= *(ufunc_T
**)s2
;
19745 return profile_cmp(&p1
->uf_tm_total
, &p2
->uf_tm_total
);
19749 * Compare function for self time sorting.
19752 #ifdef __BORLANDC__
19755 prof_self_cmp(s1
, s2
)
19761 p1
= *(ufunc_T
**)s1
;
19762 p2
= *(ufunc_T
**)s2
;
19763 return profile_cmp(&p1
->uf_tm_self
, &p2
->uf_tm_self
);
19769 * If "name" has a package name try autoloading the script for it.
19770 * Return TRUE if a package was loaded.
19773 script_autoload(name
, reload
)
19775 int reload
; /* load script again when already loaded */
19778 char_u
*scriptname
, *tofree
;
19782 /* If there is no '#' after name[0] there is no package name. */
19783 p
= vim_strchr(name
, AUTOLOAD_CHAR
);
19784 if (p
== NULL
|| p
== name
)
19787 tofree
= scriptname
= autoload_name(name
);
19789 /* Find the name in the list of previously loaded package names. Skip
19790 * "autoload/", it's always the same. */
19791 for (i
= 0; i
< ga_loaded
.ga_len
; ++i
)
19792 if (STRCMP(((char_u
**)ga_loaded
.ga_data
)[i
] + 9, scriptname
+ 9) == 0)
19794 if (!reload
&& i
< ga_loaded
.ga_len
)
19795 ret
= FALSE
; /* was loaded already */
19798 /* Remember the name if it wasn't loaded already. */
19799 if (i
== ga_loaded
.ga_len
&& ga_grow(&ga_loaded
, 1) == OK
)
19801 ((char_u
**)ga_loaded
.ga_data
)[ga_loaded
.ga_len
++] = scriptname
;
19805 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
19806 if (source_runtime(scriptname
, FALSE
) == OK
)
19815 * Return the autoload script name for a function or variable name.
19816 * Returns NULL when out of memory.
19819 autoload_name(name
)
19823 char_u
*scriptname
;
19825 /* Get the script file name: replace '#' with '/', append ".vim". */
19826 scriptname
= alloc((unsigned)(STRLEN(name
) + 14));
19827 if (scriptname
== NULL
)
19829 STRCPY(scriptname
, "autoload/");
19830 STRCAT(scriptname
, name
);
19831 *vim_strrchr(scriptname
, AUTOLOAD_CHAR
) = NUL
;
19832 STRCAT(scriptname
, ".vim");
19833 while ((p
= vim_strchr(scriptname
, AUTOLOAD_CHAR
)) != NULL
)
19838 #if defined(FEAT_CMDL_COMPL) || defined(PROTO)
19841 * Function given to ExpandGeneric() to obtain the list of user defined
19845 get_user_func_name(xp
, idx
)
19849 static long_u done
;
19850 static hashitem_T
*hi
;
19856 hi
= func_hashtab
.ht_array
;
19858 if (done
< func_hashtab
.ht_used
)
19862 while (HASHITEM_EMPTY(hi
))
19866 if (STRLEN(fp
->uf_name
) + 4 >= IOSIZE
)
19867 return fp
->uf_name
; /* prevents overflow */
19869 cat_func_name(IObuff
, fp
);
19870 if (xp
->xp_context
!= EXPAND_USER_FUNC
)
19872 STRCAT(IObuff
, "(");
19873 if (!fp
->uf_varargs
&& fp
->uf_args
.ga_len
== 0)
19874 STRCAT(IObuff
, ")");
19881 #endif /* FEAT_CMDL_COMPL */
19884 * Copy the function name of "fp" to buffer "buf".
19885 * "buf" must be able to hold the function name plus three bytes.
19886 * Takes care of script-local function names.
19889 cat_func_name(buf
, fp
)
19893 if (fp
->uf_name
[0] == K_SPECIAL
)
19895 STRCPY(buf
, "<SNR>");
19896 STRCAT(buf
, fp
->uf_name
+ 3);
19899 STRCPY(buf
, fp
->uf_name
);
19903 * ":delfunction {name}"
19906 ex_delfunction(eap
)
19909 ufunc_T
*fp
= NULL
;
19915 name
= trans_function_name(&p
, eap
->skip
, 0, &fudi
);
19916 vim_free(fudi
.fd_newkey
);
19919 if (fudi
.fd_dict
!= NULL
&& !eap
->skip
)
19920 EMSG(_(e_funcref
));
19923 if (!ends_excmd(*skipwhite(p
)))
19926 EMSG(_(e_trailing
));
19929 eap
->nextcmd
= check_nextcmd(p
);
19930 if (eap
->nextcmd
!= NULL
)
19934 fp
= find_func(name
);
19941 EMSG2(_(e_nofunc
), eap
->arg
);
19944 if (fp
->uf_calls
> 0)
19946 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap
->arg
);
19950 if (fudi
.fd_dict
!= NULL
)
19952 /* Delete the dict item that refers to the function, it will
19953 * invoke func_unref() and possibly delete the function. */
19954 dictitem_remove(fudi
.fd_dict
, fudi
.fd_di
);
19962 * Free a function and remove it from the list of functions.
19970 /* clear this function */
19971 ga_clear_strings(&(fp
->uf_args
));
19972 ga_clear_strings(&(fp
->uf_lines
));
19973 #ifdef FEAT_PROFILE
19974 vim_free(fp
->uf_tml_count
);
19975 vim_free(fp
->uf_tml_total
);
19976 vim_free(fp
->uf_tml_self
);
19979 /* remove the function from the function hashtable */
19980 hi
= hash_find(&func_hashtab
, UF2HIKEY(fp
));
19981 if (HASHITEM_EMPTY(hi
))
19982 EMSG2(_(e_intern2
), "func_free()");
19984 hash_remove(&func_hashtab
, hi
);
19990 * Unreference a Function: decrement the reference count and free it when it
19991 * becomes zero. Only for numbered functions.
19999 if (name
!= NULL
&& isdigit(*name
))
20001 fp
= find_func(name
);
20003 EMSG2(_(e_intern2
), "func_unref()");
20004 else if (--fp
->uf_refcount
<= 0)
20006 /* Only delete it when it's not being used. Otherwise it's done
20007 * when "uf_calls" becomes zero. */
20008 if (fp
->uf_calls
== 0)
20015 * Count a reference to a Function.
20023 if (name
!= NULL
&& isdigit(*name
))
20025 fp
= find_func(name
);
20027 EMSG2(_(e_intern2
), "func_ref()");
20034 * Call a user function.
20037 call_user_func(fp
, argcount
, argvars
, rettv
, firstline
, lastline
, selfdict
)
20038 ufunc_T
*fp
; /* pointer to function */
20039 int argcount
; /* nr of args */
20040 typval_T
*argvars
; /* arguments */
20041 typval_T
*rettv
; /* return value */
20042 linenr_T firstline
; /* first line of range */
20043 linenr_T lastline
; /* last line of range */
20044 dict_T
*selfdict
; /* Dictionary for "self" */
20046 char_u
*save_sourcing_name
;
20047 linenr_T save_sourcing_lnum
;
20048 scid_T save_current_SID
;
20051 static int depth
= 0;
20053 int fixvar_idx
= 0; /* index in fixvar[] */
20056 char_u numbuf
[NUMBUFLEN
];
20058 #ifdef FEAT_PROFILE
20059 proftime_T wait_start
;
20060 proftime_T call_start
;
20063 /* If depth of calling is getting too high, don't execute the function */
20064 if (depth
>= p_mfd
)
20066 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
20067 rettv
->v_type
= VAR_NUMBER
;
20068 rettv
->vval
.v_number
= -1;
20073 line_breakcheck(); /* check for CTRL-C hit */
20075 fc
.caller
= current_funccal
;
20076 current_funccal
= &fc
;
20079 rettv
->vval
.v_number
= 0;
20081 fc
.returned
= FALSE
;
20082 fc
.level
= ex_nesting_level
;
20083 /* Check if this function has a breakpoint. */
20084 fc
.breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
, (linenr_T
)0);
20085 fc
.dbg_tick
= debug_tick
;
20088 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
20089 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
20090 * each argument variable and saves a lot of time.
20093 * Init l: variables.
20095 init_var_dict(&fc
.l_vars
, &fc
.l_vars_var
);
20096 if (selfdict
!= NULL
)
20098 /* Set l:self to "selfdict". Use "name" to avoid a warning from
20099 * some compiler that checks the destination size. */
20100 v
= &fc
.fixvar
[fixvar_idx
++].var
;
20102 STRCPY(name
, "self");
20103 v
->di_flags
= DI_FLAGS_RO
+ DI_FLAGS_FIX
;
20104 hash_add(&fc
.l_vars
.dv_hashtab
, DI2HIKEY(v
));
20105 v
->di_tv
.v_type
= VAR_DICT
;
20106 v
->di_tv
.v_lock
= 0;
20107 v
->di_tv
.vval
.v_dict
= selfdict
;
20108 ++selfdict
->dv_refcount
;
20112 * Init a: variables.
20113 * Set a:0 to "argcount".
20114 * Set a:000 to a list with room for the "..." arguments.
20116 init_var_dict(&fc
.l_avars
, &fc
.l_avars_var
);
20117 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "0",
20118 (varnumber_T
)(argcount
- fp
->uf_args
.ga_len
));
20119 v
= &fc
.fixvar
[fixvar_idx
++].var
;
20120 STRCPY(v
->di_key
, "000");
20121 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
20122 hash_add(&fc
.l_avars
.dv_hashtab
, DI2HIKEY(v
));
20123 v
->di_tv
.v_type
= VAR_LIST
;
20124 v
->di_tv
.v_lock
= VAR_FIXED
;
20125 v
->di_tv
.vval
.v_list
= &fc
.l_varlist
;
20126 vim_memset(&fc
.l_varlist
, 0, sizeof(list_T
));
20127 fc
.l_varlist
.lv_refcount
= 99999;
20128 fc
.l_varlist
.lv_lock
= VAR_FIXED
;
20131 * Set a:firstline to "firstline" and a:lastline to "lastline".
20132 * Set a:name to named arguments.
20133 * Set a:N to the "..." arguments.
20135 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "firstline",
20136 (varnumber_T
)firstline
);
20137 add_nr_var(&fc
.l_avars
, &fc
.fixvar
[fixvar_idx
++].var
, "lastline",
20138 (varnumber_T
)lastline
);
20139 for (i
= 0; i
< argcount
; ++i
)
20141 ai
= i
- fp
->uf_args
.ga_len
;
20143 /* named argument a:name */
20144 name
= FUNCARG(fp
, i
);
20147 /* "..." argument a:1, a:2, etc. */
20148 sprintf((char *)numbuf
, "%d", ai
+ 1);
20151 if (fixvar_idx
< FIXVAR_CNT
&& STRLEN(name
) <= VAR_SHORT_LEN
)
20153 v
= &fc
.fixvar
[fixvar_idx
++].var
;
20154 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
20158 v
= (dictitem_T
*)alloc((unsigned)(sizeof(dictitem_T
)
20162 v
->di_flags
= DI_FLAGS_RO
;
20164 STRCPY(v
->di_key
, name
);
20165 hash_add(&fc
.l_avars
.dv_hashtab
, DI2HIKEY(v
));
20167 /* Note: the values are copied directly to avoid alloc/free.
20168 * "argvars" must have VAR_FIXED for v_lock. */
20169 v
->di_tv
= argvars
[i
];
20170 v
->di_tv
.v_lock
= VAR_FIXED
;
20172 if (ai
>= 0 && ai
< MAX_FUNC_ARGS
)
20174 list_append(&fc
.l_varlist
, &fc
.l_listitems
[ai
]);
20175 fc
.l_listitems
[ai
].li_tv
= argvars
[i
];
20176 fc
.l_listitems
[ai
].li_tv
.v_lock
= VAR_FIXED
;
20180 /* Don't redraw while executing the function. */
20181 ++RedrawingDisabled
;
20182 save_sourcing_name
= sourcing_name
;
20183 save_sourcing_lnum
= sourcing_lnum
;
20185 sourcing_name
= alloc((unsigned)((save_sourcing_name
== NULL
? 0
20186 : STRLEN(save_sourcing_name
)) + STRLEN(fp
->uf_name
) + 13));
20187 if (sourcing_name
!= NULL
)
20189 if (save_sourcing_name
!= NULL
20190 && STRNCMP(save_sourcing_name
, "function ", 9) == 0)
20191 sprintf((char *)sourcing_name
, "%s..", save_sourcing_name
);
20193 STRCPY(sourcing_name
, "function ");
20194 cat_func_name(sourcing_name
+ STRLEN(sourcing_name
), fp
);
20196 if (p_verbose
>= 12)
20199 verbose_enter_scroll();
20201 smsg((char_u
*)_("calling %s"), sourcing_name
);
20202 if (p_verbose
>= 14)
20204 char_u buf
[MSG_BUF_LEN
];
20205 char_u numbuf2
[NUMBUFLEN
];
20209 msg_puts((char_u
*)"(");
20210 for (i
= 0; i
< argcount
; ++i
)
20213 msg_puts((char_u
*)", ");
20214 if (argvars
[i
].v_type
== VAR_NUMBER
)
20215 msg_outnum((long)argvars
[i
].vval
.v_number
);
20218 s
= tv2string(&argvars
[i
], &tofree
, numbuf2
, 0);
20221 trunc_string(s
, buf
, MSG_BUF_CLEN
);
20227 msg_puts((char_u
*)")");
20229 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
20231 verbose_leave_scroll();
20235 #ifdef FEAT_PROFILE
20236 if (do_profiling
== PROF_YES
)
20238 if (!fp
->uf_profiling
&& has_profiling(FALSE
, fp
->uf_name
, NULL
))
20239 func_do_profile(fp
);
20240 if (fp
->uf_profiling
20241 || (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
))
20244 profile_start(&call_start
);
20245 profile_zero(&fp
->uf_tm_children
);
20247 script_prof_save(&wait_start
);
20251 save_current_SID
= current_SID
;
20252 current_SID
= fp
->uf_script_ID
;
20253 save_did_emsg
= did_emsg
;
20256 /* call do_cmdline() to execute the lines */
20257 do_cmdline(NULL
, get_func_line
, (void *)&fc
,
20258 DOCMD_NOWAIT
|DOCMD_VERBOSE
|DOCMD_REPEAT
);
20260 --RedrawingDisabled
;
20262 /* when the function was aborted because of an error, return -1 */
20263 if ((did_emsg
&& (fp
->uf_flags
& FC_ABORT
)) || rettv
->v_type
== VAR_UNKNOWN
)
20266 rettv
->v_type
= VAR_NUMBER
;
20267 rettv
->vval
.v_number
= -1;
20270 #ifdef FEAT_PROFILE
20271 if (do_profiling
== PROF_YES
&& (fp
->uf_profiling
20272 || (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
)))
20274 profile_end(&call_start
);
20275 profile_sub_wait(&wait_start
, &call_start
);
20276 profile_add(&fp
->uf_tm_total
, &call_start
);
20277 profile_self(&fp
->uf_tm_self
, &call_start
, &fp
->uf_tm_children
);
20278 if (fc
.caller
!= NULL
&& &fc
.caller
->func
->uf_profiling
)
20280 profile_add(&fc
.caller
->func
->uf_tm_children
, &call_start
);
20281 profile_add(&fc
.caller
->func
->uf_tml_children
, &call_start
);
20286 /* when being verbose, mention the return value */
20287 if (p_verbose
>= 12)
20290 verbose_enter_scroll();
20293 smsg((char_u
*)_("%s aborted"), sourcing_name
);
20294 else if (fc
.rettv
->v_type
== VAR_NUMBER
)
20295 smsg((char_u
*)_("%s returning #%ld"), sourcing_name
,
20296 (long)fc
.rettv
->vval
.v_number
);
20299 char_u buf
[MSG_BUF_LEN
];
20300 char_u numbuf2
[NUMBUFLEN
];
20304 /* The value may be very long. Skip the middle part, so that we
20305 * have some idea how it starts and ends. smsg() would always
20306 * truncate it at the end. */
20307 s
= tv2string(fc
.rettv
, &tofree
, numbuf2
, 0);
20310 trunc_string(s
, buf
, MSG_BUF_CLEN
);
20311 smsg((char_u
*)_("%s returning %s"), sourcing_name
, buf
);
20315 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
20317 verbose_leave_scroll();
20321 vim_free(sourcing_name
);
20322 sourcing_name
= save_sourcing_name
;
20323 sourcing_lnum
= save_sourcing_lnum
;
20324 current_SID
= save_current_SID
;
20325 #ifdef FEAT_PROFILE
20326 if (do_profiling
== PROF_YES
)
20327 script_prof_restore(&wait_start
);
20330 if (p_verbose
>= 12 && sourcing_name
!= NULL
)
20333 verbose_enter_scroll();
20335 smsg((char_u
*)_("continuing in %s"), sourcing_name
);
20336 msg_puts((char_u
*)"\n"); /* don't overwrite this either */
20338 verbose_leave_scroll();
20342 did_emsg
|= save_did_emsg
;
20343 current_funccal
= fc
.caller
;
20345 /* The a: variables typevals were not alloced, only free the allocated
20347 vars_clear_ext(&fc
.l_avars
.dv_hashtab
, FALSE
);
20349 vars_clear(&fc
.l_vars
.dv_hashtab
); /* free all l: variables */
20354 * Add a number variable "name" to dict "dp" with value "nr".
20357 add_nr_var(dp
, v
, name
, nr
)
20363 STRCPY(v
->di_key
, name
);
20364 v
->di_flags
= DI_FLAGS_RO
| DI_FLAGS_FIX
;
20365 hash_add(&dp
->dv_hashtab
, DI2HIKEY(v
));
20366 v
->di_tv
.v_type
= VAR_NUMBER
;
20367 v
->di_tv
.v_lock
= VAR_FIXED
;
20368 v
->di_tv
.vval
.v_number
= nr
;
20378 char_u
*arg
= eap
->arg
;
20380 int returning
= FALSE
;
20382 if (current_funccal
== NULL
)
20384 EMSG(_("E133: :return not inside a function"));
20391 eap
->nextcmd
= NULL
;
20392 if ((*arg
!= NUL
&& *arg
!= '|' && *arg
!= '\n')
20393 && eval0(arg
, &rettv
, &eap
->nextcmd
, !eap
->skip
) != FAIL
)
20396 returning
= do_return(eap
, FALSE
, TRUE
, &rettv
);
20400 /* It's safer to return also on error. */
20401 else if (!eap
->skip
)
20404 * Return unless the expression evaluation has been cancelled due to an
20405 * aborting error, an interrupt, or an exception.
20408 returning
= do_return(eap
, FALSE
, TRUE
, NULL
);
20411 /* When skipping or the return gets pending, advance to the next command
20412 * in this line (!returning). Otherwise, ignore the rest of the line.
20413 * Following lines will be ignored by get_func_line(). */
20415 eap
->nextcmd
= NULL
;
20416 else if (eap
->nextcmd
== NULL
) /* no argument */
20417 eap
->nextcmd
= check_nextcmd(arg
);
20424 * Return from a function. Possibly makes the return pending. Also called
20425 * for a pending return at the ":endtry" or after returning from an extra
20426 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
20427 * when called due to a ":return" command. "rettv" may point to a typval_T
20428 * with the return rettv. Returns TRUE when the return can be carried out,
20429 * FALSE when the return gets pending.
20432 do_return(eap
, reanimate
, is_cmd
, rettv
)
20439 struct condstack
*cstack
= eap
->cstack
;
20442 /* Undo the return. */
20443 current_funccal
->returned
= FALSE
;
20446 * Cleanup (and inactivate) conditionals, but stop when a try conditional
20447 * not in its finally clause (which then is to be executed next) is found.
20448 * In this case, make the ":return" pending for execution at the ":endtry".
20449 * Otherwise, return normally.
20451 idx
= cleanup_conditionals(eap
->cstack
, 0, TRUE
);
20454 cstack
->cs_pending
[idx
] = CSTP_RETURN
;
20456 if (!is_cmd
&& !reanimate
)
20457 /* A pending return again gets pending. "rettv" points to an
20458 * allocated variable with the rettv of the original ":return"'s
20459 * argument if present or is NULL else. */
20460 cstack
->cs_rettv
[idx
] = rettv
;
20463 /* When undoing a return in order to make it pending, get the stored
20466 rettv
= current_funccal
->rettv
;
20470 /* Store the value of the pending return. */
20471 if ((cstack
->cs_rettv
[idx
] = alloc_tv()) != NULL
)
20472 *(typval_T
*)cstack
->cs_rettv
[idx
] = *(typval_T
*)rettv
;
20474 EMSG(_(e_outofmem
));
20477 cstack
->cs_rettv
[idx
] = NULL
;
20481 /* The pending return value could be overwritten by a ":return"
20482 * without argument in a finally clause; reset the default
20484 current_funccal
->rettv
->v_type
= VAR_NUMBER
;
20485 current_funccal
->rettv
->vval
.v_number
= 0;
20488 report_make_pending(CSTP_RETURN
, rettv
);
20492 current_funccal
->returned
= TRUE
;
20494 /* If the return is carried out now, store the return value. For
20495 * a return immediately after reanimation, the value is already
20497 if (!reanimate
&& rettv
!= NULL
)
20499 clear_tv(current_funccal
->rettv
);
20500 *current_funccal
->rettv
= *(typval_T
*)rettv
;
20510 * Free the variable with a pending return value.
20513 discard_pending_return(rettv
)
20516 free_tv((typval_T
*)rettv
);
20520 * Generate a return command for producing the value of "rettv". The result
20521 * is an allocated string. Used by report_pending() for verbose messages.
20524 get_return_cmd(rettv
)
20528 char_u
*tofree
= NULL
;
20529 char_u numbuf
[NUMBUFLEN
];
20532 s
= echo_string((typval_T
*)rettv
, &tofree
, numbuf
, 0);
20536 STRCPY(IObuff
, ":return ");
20537 STRNCPY(IObuff
+ 8, s
, IOSIZE
- 8);
20538 if (STRLEN(s
) + 8 >= IOSIZE
)
20539 STRCPY(IObuff
+ IOSIZE
- 4, "...");
20541 return vim_strsave(IObuff
);
20545 * Get next function line.
20546 * Called by do_cmdline() to get the next line.
20547 * Returns allocated string, or NULL for end of function.
20551 get_func_line(c
, cookie
, indent
)
20552 int c
; /* not used */
20554 int indent
; /* not used */
20556 funccall_T
*fcp
= (funccall_T
*)cookie
;
20557 ufunc_T
*fp
= fcp
->func
;
20559 garray_T
*gap
; /* growarray with function lines */
20561 /* If breakpoints have been added/deleted need to check for it. */
20562 if (fcp
->dbg_tick
!= debug_tick
)
20564 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
20566 fcp
->dbg_tick
= debug_tick
;
20568 #ifdef FEAT_PROFILE
20569 if (do_profiling
== PROF_YES
)
20570 func_line_end(cookie
);
20573 gap
= &fp
->uf_lines
;
20574 if (((fp
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
20579 /* Skip NULL lines (continuation lines). */
20580 while (fcp
->linenr
< gap
->ga_len
20581 && ((char_u
**)(gap
->ga_data
))[fcp
->linenr
] == NULL
)
20583 if (fcp
->linenr
>= gap
->ga_len
)
20587 retval
= vim_strsave(((char_u
**)(gap
->ga_data
))[fcp
->linenr
++]);
20588 sourcing_lnum
= fcp
->linenr
;
20589 #ifdef FEAT_PROFILE
20590 if (do_profiling
== PROF_YES
)
20591 func_line_start(cookie
);
20596 /* Did we encounter a breakpoint? */
20597 if (fcp
->breakpoint
!= 0 && fcp
->breakpoint
<= sourcing_lnum
)
20599 dbg_breakpoint(fp
->uf_name
, sourcing_lnum
);
20600 /* Find next breakpoint. */
20601 fcp
->breakpoint
= dbg_find_breakpoint(FALSE
, fp
->uf_name
,
20603 fcp
->dbg_tick
= debug_tick
;
20609 #if defined(FEAT_PROFILE) || defined(PROTO)
20611 * Called when starting to read a function line.
20612 * "sourcing_lnum" must be correct!
20613 * When skipping lines it may not actually be executed, but we won't find out
20614 * until later and we need to store the time now.
20617 func_line_start(cookie
)
20620 funccall_T
*fcp
= (funccall_T
*)cookie
;
20621 ufunc_T
*fp
= fcp
->func
;
20623 if (fp
->uf_profiling
&& sourcing_lnum
>= 1
20624 && sourcing_lnum
<= fp
->uf_lines
.ga_len
)
20626 fp
->uf_tml_idx
= sourcing_lnum
- 1;
20627 /* Skip continuation lines. */
20628 while (fp
->uf_tml_idx
> 0 && FUNCLINE(fp
, fp
->uf_tml_idx
) == NULL
)
20630 fp
->uf_tml_execed
= FALSE
;
20631 profile_start(&fp
->uf_tml_start
);
20632 profile_zero(&fp
->uf_tml_children
);
20633 profile_get_wait(&fp
->uf_tml_wait
);
20638 * Called when actually executing a function line.
20641 func_line_exec(cookie
)
20644 funccall_T
*fcp
= (funccall_T
*)cookie
;
20645 ufunc_T
*fp
= fcp
->func
;
20647 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
20648 fp
->uf_tml_execed
= TRUE
;
20652 * Called when done with a function line.
20655 func_line_end(cookie
)
20658 funccall_T
*fcp
= (funccall_T
*)cookie
;
20659 ufunc_T
*fp
= fcp
->func
;
20661 if (fp
->uf_profiling
&& fp
->uf_tml_idx
>= 0)
20663 if (fp
->uf_tml_execed
)
20665 ++fp
->uf_tml_count
[fp
->uf_tml_idx
];
20666 profile_end(&fp
->uf_tml_start
);
20667 profile_sub_wait(&fp
->uf_tml_wait
, &fp
->uf_tml_start
);
20668 profile_add(&fp
->uf_tml_total
[fp
->uf_tml_idx
], &fp
->uf_tml_start
);
20669 profile_self(&fp
->uf_tml_self
[fp
->uf_tml_idx
], &fp
->uf_tml_start
,
20670 &fp
->uf_tml_children
);
20672 fp
->uf_tml_idx
= -1;
20678 * Return TRUE if the currently active function should be ended, because a
20679 * return was encountered or an error occured. Used inside a ":while".
20682 func_has_ended(cookie
)
20685 funccall_T
*fcp
= (funccall_T
*)cookie
;
20687 /* Ignore the "abort" flag if the abortion behavior has been changed due to
20688 * an error inside a try conditional. */
20689 return (((fcp
->func
->uf_flags
& FC_ABORT
) && did_emsg
&& !aborted_in_try())
20694 * return TRUE if cookie indicates a function which "abort"s on errors.
20697 func_has_abort(cookie
)
20700 return ((funccall_T
*)cookie
)->func
->uf_flags
& FC_ABORT
;
20703 #if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
20706 VAR_FLAVOUR_DEFAULT
,
20707 VAR_FLAVOUR_SESSION
,
20708 VAR_FLAVOUR_VIMINFO
20711 static var_flavour_T var_flavour
__ARGS((char_u
*varname
));
20713 static var_flavour_T
20714 var_flavour(varname
)
20717 char_u
*p
= varname
;
20719 if (ASCII_ISUPPER(*p
))
20722 if (ASCII_ISLOWER(*p
))
20723 return VAR_FLAVOUR_SESSION
;
20724 return VAR_FLAVOUR_VIMINFO
;
20727 return VAR_FLAVOUR_DEFAULT
;
20731 #if defined(FEAT_VIMINFO) || defined(PROTO)
20733 * Restore global vars that start with a capital from the viminfo file
20736 read_viminfo_varlist(virp
, writing
)
20741 int is_string
= FALSE
;
20744 if (!writing
&& (find_viminfo_parameter('!') != NULL
))
20746 tab
= vim_strchr(virp
->vir_line
+ 1, '\t');
20749 *tab
++ = '\0'; /* isolate the variable name */
20750 if (*tab
== 'S') /* string var */
20753 tab
= vim_strchr(tab
, '\t');
20758 tv
.v_type
= VAR_STRING
;
20759 tv
.vval
.v_string
= viminfo_readstring(virp
,
20760 (int)(tab
- virp
->vir_line
+ 1), TRUE
);
20764 tv
.v_type
= VAR_NUMBER
;
20765 tv
.vval
.v_number
= atol((char *)tab
+ 1);
20767 set_var(virp
->vir_line
+ 1, &tv
, FALSE
);
20769 vim_free(tv
.vval
.v_string
);
20774 return viminfo_readline(virp
);
20778 * Write global vars that start with a capital to the viminfo file
20781 write_viminfo_varlist(fp
)
20785 dictitem_T
*this_var
;
20790 char_u numbuf
[NUMBUFLEN
];
20792 if (find_viminfo_parameter('!') == NULL
)
20795 fprintf(fp
, _("\n# global variables:\n"));
20797 todo
= (int)globvarht
.ht_used
;
20798 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
20800 if (!HASHITEM_EMPTY(hi
))
20803 this_var
= HI2DI(hi
);
20804 if (var_flavour(this_var
->di_key
) == VAR_FLAVOUR_VIMINFO
)
20806 switch (this_var
->di_tv
.v_type
)
20808 case VAR_STRING
: s
= "STR"; break;
20809 case VAR_NUMBER
: s
= "NUM"; break;
20812 fprintf(fp
, "!%s\t%s\t", this_var
->di_key
, s
);
20813 p
= echo_string(&this_var
->di_tv
, &tofree
, numbuf
, 0);
20815 viminfo_writestring(fp
, p
);
20823 #if defined(FEAT_SESSION) || defined(PROTO)
20825 store_session_globals(fd
)
20829 dictitem_T
*this_var
;
20833 todo
= (int)globvarht
.ht_used
;
20834 for (hi
= globvarht
.ht_array
; todo
> 0; ++hi
)
20836 if (!HASHITEM_EMPTY(hi
))
20839 this_var
= HI2DI(hi
);
20840 if ((this_var
->di_tv
.v_type
== VAR_NUMBER
20841 || this_var
->di_tv
.v_type
== VAR_STRING
)
20842 && var_flavour(this_var
->di_key
) == VAR_FLAVOUR_SESSION
)
20844 /* Escape special characters with a backslash. Turn a LF and
20845 * CR into \n and \r. */
20846 p
= vim_strsave_escaped(get_tv_string(&this_var
->di_tv
),
20847 (char_u
*)"\\\"\n\r");
20848 if (p
== NULL
) /* out of memory */
20850 for (t
= p
; *t
!= NUL
; ++t
)
20853 else if (*t
== '\r')
20855 if ((fprintf(fd
, "let %s = %c%s%c",
20857 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
20860 (this_var
->di_tv
.v_type
== VAR_STRING
) ? '"'
20862 || put_eol(fd
) == FAIL
)
20876 * Display script name where an item was last set.
20877 * Should only be invoked when 'verbose' is non-zero.
20880 last_set_msg(scriptID
)
20887 p
= home_replace_save(NULL
, get_scriptname(scriptID
));
20891 MSG_PUTS(_("\n\tLast set from "));
20899 #endif /* FEAT_EVAL */
20901 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
20906 * Functions for ":8" filename modifier: get 8.3 version of a filename.
20908 static int get_short_pathname
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
20909 static int shortpath_for_invalid_fname
__ARGS((char_u
**fname
, char_u
**bufp
, int *fnamelen
));
20910 static int shortpath_for_partial
__ARGS((char_u
**fnamep
, char_u
**bufp
, int *fnamelen
));
20913 * Get the short pathname of a file.
20914 * Returns 1 on success. *fnamelen is 0 for nonexistent path.
20917 get_short_pathname(fnamep
, bufp
, fnamelen
)
20927 l
= GetShortPathName(*fnamep
, *fnamep
, len
);
20930 /* If that doesn't work (not enough space), then save the string
20931 * and try again with a new buffer big enough
20933 newbuf
= vim_strnsave(*fnamep
, l
);
20934 if (newbuf
== NULL
)
20938 *fnamep
= *bufp
= newbuf
;
20940 l
= GetShortPathName(*fnamep
,*fnamep
,l
+1);
20942 /* Really should always succeed, as the buffer is big enough */
20950 * Create a short path name. Returns the length of the buffer it needs.
20951 * Doesn't copy over the end of the buffer passed in.
20954 shortpath_for_invalid_fname(fname
, bufp
, fnamelen
)
20959 char_u
*s
, *p
, *pbuf2
, *pbuf3
;
20961 int len
, len2
, plen
, slen
;
20965 pbuf2
= vim_strnsave(*fname
, len2
);
20968 s
= pbuf2
+ len2
- 1; /* Find the end */
20972 if (after_pathsep(pbuf2
, s
+ 1))
20981 /* Go back one path-separator */
20982 while (s
> pbuf2
&& !after_pathsep(pbuf2
, s
+ 1))
20991 /* Remember the character that is about to be splatted */
20993 *s
= 0; /* get_short_pathname requires a null-terminated string */
20995 /* Try it in situ */
20997 if (!get_short_pathname(&p
, &pbuf3
, &plen
))
21002 *s
= ch
; /* Preserve the string */
21003 } while (plen
== 0);
21007 /* Remember the length of the new string. */
21008 *fnamelen
= len
= plen
+ slen
;
21012 /* If there's not enough space in the currently allocated string,
21013 * then copy it to a buffer big enough.
21015 *fname
= *bufp
= vim_strnsave(p
, len
);
21016 if (*fname
== NULL
)
21021 /* Transfer pbuf2 to being the main buffer (it's big enough) */
21022 *fname
= *bufp
= pbuf2
;
21024 strncpy(*fname
, p
, plen
);
21027 /* Concat the next bit */
21028 strncpy(*fname
+ plen
, s
, slen
);
21029 (*fname
)[len
] = '\0';
21037 * Get a pathname for a partial path.
21040 shortpath_for_partial(fnamep
, bufp
, fnamelen
)
21045 int sepcount
, len
, tflen
;
21047 char_u
*pbuf
, *tfname
;
21050 /* Count up the path seperators from the RHS.. so we know which part
21051 * of the path to return.
21054 for (p
= *fnamep
; p
< *fnamep
+ *fnamelen
; mb_ptr_adv(p
))
21055 if (vim_ispathsep(*p
))
21058 /* Need full path first (use expand_env() to remove a "~/") */
21059 hasTilde
= (**fnamep
== '~');
21061 pbuf
= tfname
= expand_env_save(*fnamep
);
21063 pbuf
= tfname
= FullName_save(*fnamep
, FALSE
);
21065 len
= tflen
= (int)STRLEN(tfname
);
21067 if (!get_short_pathname(&tfname
, &pbuf
, &len
))
21072 /* Don't have a valid filename, so shorten the rest of the
21073 * path if we can. This CAN give us invalid 8.3 filenames, but
21074 * there's not a lot of point in guessing what it might be.
21077 if (shortpath_for_invalid_fname(&tfname
, &pbuf
, &len
) == -1)
21081 /* Count the paths backward to find the beginning of the desired string. */
21082 for (p
= tfname
+ len
- 1; p
>= tfname
; --p
)
21086 p
-= mb_head_off(tfname
, p
);
21088 if (vim_ispathsep(*p
))
21090 if (sepcount
== 0 || (hasTilde
&& sepcount
== 1))
21107 /* Copy in the string - p indexes into tfname - allocated at pbuf */
21109 *fnamelen
= (int)STRLEN(p
);
21115 #endif /* WIN3264 */
21118 * Adjust a filename, according to a string of modifiers.
21119 * *fnamep must be NUL terminated when called. When returning, the length is
21120 * determined by *fnamelen.
21121 * Returns valid flags.
21122 * When there is an error, *fnamep is set to NULL.
21125 modify_fname(src
, usedlen
, fnamep
, bufp
, fnamelen
)
21126 char_u
*src
; /* string with modifiers */
21127 int *usedlen
; /* characters after src that are used */
21128 char_u
**fnamep
; /* file name so far */
21129 char_u
**bufp
; /* buffer for allocated file name or NULL */
21130 int *fnamelen
; /* length of fnamep */
21134 char_u
*s
, *p
, *pbuf
;
21135 char_u dirname
[MAXPATHL
];
21137 int has_fullname
= 0;
21139 int has_shortname
= 0;
21143 /* ":p" - full path/file_name */
21144 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'p')
21148 valid
|= VALID_PATH
;
21151 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
21152 if ((*fnamep
)[0] == '~'
21153 #if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
21154 && ((*fnamep
)[1] == '/'
21155 # ifdef BACKSLASH_IN_FILENAME
21156 || (*fnamep
)[1] == '\\'
21158 || (*fnamep
)[1] == NUL
)
21163 *fnamep
= expand_env_save(*fnamep
);
21164 vim_free(*bufp
); /* free any allocated file name */
21166 if (*fnamep
== NULL
)
21170 /* When "/." or "/.." is used: force expansion to get rid of it. */
21171 for (p
= *fnamep
; *p
!= NUL
; mb_ptr_adv(p
))
21173 if (vim_ispathsep(*p
)
21176 || vim_ispathsep(p
[2])
21178 && (p
[3] == NUL
|| vim_ispathsep(p
[3])))))
21182 /* FullName_save() is slow, don't use it when not needed. */
21183 if (*p
!= NUL
|| !vim_isAbsName(*fnamep
))
21185 *fnamep
= FullName_save(*fnamep
, *p
!= NUL
);
21186 vim_free(*bufp
); /* free any allocated file name */
21188 if (*fnamep
== NULL
)
21192 /* Append a path separator to a directory. */
21193 if (mch_isdir(*fnamep
))
21195 /* Make room for one or two extra characters. */
21196 *fnamep
= vim_strnsave(*fnamep
, (int)STRLEN(*fnamep
) + 2);
21197 vim_free(*bufp
); /* free any allocated file name */
21199 if (*fnamep
== NULL
)
21201 add_pathsep(*fnamep
);
21205 /* ":." - path relative to the current directory */
21206 /* ":~" - path relative to the home directory */
21207 /* ":8" - shortname path - postponed till after */
21208 while (src
[*usedlen
] == ':'
21209 && ((c
= src
[*usedlen
+ 1]) == '.' || c
== '~' || c
== '8'))
21215 has_shortname
= 1; /* Postpone this. */
21220 /* Need full path first (use expand_env() to remove a "~/") */
21223 if (c
== '.' && **fnamep
== '~')
21224 p
= pbuf
= expand_env_save(*fnamep
);
21226 p
= pbuf
= FullName_save(*fnamep
, FALSE
);
21237 mch_dirname(dirname
, MAXPATHL
);
21238 s
= shorten_fname(p
, dirname
);
21244 vim_free(*bufp
); /* free any allocated file name */
21252 home_replace(NULL
, p
, dirname
, MAXPATHL
, TRUE
);
21253 /* Only replace it when it starts with '~' */
21254 if (*dirname
== '~')
21256 s
= vim_strsave(dirname
);
21269 tail
= gettail(*fnamep
);
21270 *fnamelen
= (int)STRLEN(*fnamep
);
21272 /* ":h" - head, remove "/file_name", can be repeated */
21273 /* Don't remove the first "/" or "c:\" */
21274 while (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 'h')
21276 valid
|= VALID_HEAD
;
21278 s
= get_past_head(*fnamep
);
21279 while (tail
> s
&& after_pathsep(s
, tail
))
21281 *fnamelen
= (int)(tail
- *fnamep
);
21284 *fnamelen
+= 1; /* the path separator is part of the path */
21286 while (tail
> s
&& !after_pathsep(s
, tail
))
21287 mb_ptr_back(*fnamep
, tail
);
21290 /* ":8" - shortname */
21291 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == '8')
21300 /* Check shortname after we have done 'heads' and before we do 'tails'
21305 /* Copy the string if it is shortened by :h */
21306 if (*fnamelen
< (int)STRLEN(*fnamep
))
21308 p
= vim_strnsave(*fnamep
, *fnamelen
);
21312 *bufp
= *fnamep
= p
;
21315 /* Split into two implementations - makes it easier. First is where
21316 * there isn't a full name already, second is where there is.
21318 if (!has_fullname
&& !vim_isAbsName(*fnamep
))
21320 if (shortpath_for_partial(fnamep
, bufp
, fnamelen
) == -1)
21327 /* Simple case, already have the full-name
21328 * Nearly always shorter, so try first time. */
21330 if (!get_short_pathname(fnamep
, bufp
, &l
))
21335 /* Couldn't find the filename.. search the paths.
21338 if (shortpath_for_invalid_fname(fnamep
, bufp
, &l
) == -1)
21344 #endif /* WIN3264 */
21346 /* ":t" - tail, just the basename */
21347 if (src
[*usedlen
] == ':' && src
[*usedlen
+ 1] == 't')
21350 *fnamelen
-= (int)(tail
- *fnamep
);
21354 /* ":e" - extension, can be repeated */
21355 /* ":r" - root, without extension, can be repeated */
21356 while (src
[*usedlen
] == ':'
21357 && (src
[*usedlen
+ 1] == 'e' || src
[*usedlen
+ 1] == 'r'))
21359 /* find a '.' in the tail:
21360 * - for second :e: before the current fname
21361 * - otherwise: The last '.'
21363 if (src
[*usedlen
+ 1] == 'e' && *fnamep
> tail
)
21366 s
= *fnamep
+ *fnamelen
- 1;
21367 for ( ; s
> tail
; --s
)
21370 if (src
[*usedlen
+ 1] == 'e') /* :e */
21374 *fnamelen
+= (int)(*fnamep
- (s
+ 1));
21377 /* cut version from the extension */
21378 s
= *fnamep
+ *fnamelen
- 1;
21379 for ( ; s
> *fnamep
; --s
)
21383 *fnamelen
= s
- *fnamep
;
21386 else if (*fnamep
<= tail
)
21391 if (s
> tail
) /* remove one extension */
21392 *fnamelen
= (int)(s
- *fnamep
);
21397 /* ":s?pat?foo?" - substitute */
21398 /* ":gs?pat?foo?" - global substitute */
21399 if (src
[*usedlen
] == ':'
21400 && (src
[*usedlen
+ 1] == 's'
21401 || (src
[*usedlen
+ 1] == 'g' && src
[*usedlen
+ 2] == 's')))
21410 flags
= (char_u
*)"";
21411 s
= src
+ *usedlen
+ 2;
21412 if (src
[*usedlen
+ 1] == 'g')
21414 flags
= (char_u
*)"g";
21421 /* find end of pattern */
21422 p
= vim_strchr(s
, sep
);
21425 pat
= vim_strnsave(s
, (int)(p
- s
));
21429 /* find end of substitution */
21430 p
= vim_strchr(s
, sep
);
21433 sub
= vim_strnsave(s
, (int)(p
- s
));
21434 str
= vim_strnsave(*fnamep
, *fnamelen
);
21435 if (sub
!= NULL
&& str
!= NULL
)
21437 *usedlen
= (int)(p
+ 1 - src
);
21438 s
= do_string_sub(str
, pat
, sub
, flags
);
21442 *fnamelen
= (int)STRLEN(s
);
21454 /* after using ":s", repeat all the modifiers */
21464 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
21465 * "flags" can be "g" to do a global substitute.
21466 * Returns an allocated string, NULL for error.
21469 do_string_sub(str
, pat
, sub
, flags
)
21476 regmatch_T regmatch
;
21484 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
21486 p_cpo
= (char_u
*)"";
21488 ga_init2(&ga
, 1, 200);
21490 do_all
= (flags
[0] == 'g');
21492 regmatch
.rm_ic
= p_ic
;
21493 regmatch
.regprog
= vim_regcomp(pat
, RE_MAGIC
+ RE_STRING
);
21494 if (regmatch
.regprog
!= NULL
)
21497 while (vim_regexec_nl(®match
, str
, (colnr_T
)(tail
- str
)))
21500 * Get some space for a temporary buffer to do the substitution
21501 * into. It will contain:
21502 * - The text up to where the match is.
21503 * - The substituted text.
21504 * - The text after the match.
21506 sublen
= vim_regsub(®match
, sub
, tail
, FALSE
, TRUE
, FALSE
);
21507 if (ga_grow(&ga
, (int)(STRLEN(tail
) + sublen
-
21508 (regmatch
.endp
[0] - regmatch
.startp
[0]))) == FAIL
)
21514 /* copy the text up to where the match is */
21515 i
= (int)(regmatch
.startp
[0] - tail
);
21516 mch_memmove((char_u
*)ga
.ga_data
+ ga
.ga_len
, tail
, (size_t)i
);
21517 /* add the substituted text */
21518 (void)vim_regsub(®match
, sub
, (char_u
*)ga
.ga_data
21519 + ga
.ga_len
+ i
, TRUE
, TRUE
, FALSE
);
21520 ga
.ga_len
+= i
+ sublen
- 1;
21521 /* avoid getting stuck on a match with an empty string */
21522 if (tail
== regmatch
.endp
[0])
21526 *((char_u
*)ga
.ga_data
+ ga
.ga_len
) = *tail
++;
21531 tail
= regmatch
.endp
[0];
21539 if (ga
.ga_data
!= NULL
)
21540 STRCPY((char *)ga
.ga_data
+ ga
.ga_len
, tail
);
21542 vim_free(regmatch
.regprog
);
21545 ret
= vim_strsave(ga
.ga_data
== NULL
? str
: (char_u
*)ga
.ga_data
);
21552 #endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */