blob: e087b7aabe679aa531b558d01f45436276a417cb [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
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.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * 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.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
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
67 * "newkey" is NULL
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.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
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. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 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:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * 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.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * 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.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static 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)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
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_start; /* time at function call */
170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000197 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static 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)
210
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]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#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 */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
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 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 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 */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * 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.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 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 */
290} vimvars[VV_LEN] =
291{
292 /*
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.
295 */
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},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {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},
Bram Moolenaar33570922005-01-25 22:26:29 +0000340};
341
342/* shorthand */
343#define vv_type vv_di.di_tv.v_type
344#define vv_nr vv_di.di_tv.vval.v_number
345#define vv_str vv_di.di_tv.vval.v_string
346#define vv_tv vv_di.di_tv
347
348/*
349 * The v: variables are stored in dictionary "vimvardict".
350 * "vimvars_var" is the variable that is used for the "l:" scope.
351 */
352static dict_T vimvardict;
353static dictitem_T vimvars_var;
354#define vimvarht vimvardict.dv_hashtab
355
Bram Moolenaara40058a2005-07-11 22:42:07 +0000356static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
357static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
358#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
359static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
360#endif
361static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
362static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
363static char_u *skip_var_one __ARGS((char_u *arg));
364static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
365static void list_glob_vars __ARGS((void));
366static void list_buf_vars __ARGS((void));
367static void list_win_vars __ARGS((void));
368static void list_vim_vars __ARGS((void));
369static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
370static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
371static int check_changedtick __ARGS((char_u *arg));
372static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
373static void clear_lval __ARGS((lval_T *lp));
374static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
375static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
376static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
377static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
378static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
379static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
380static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
381static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
382static void item_lock __ARGS((typval_T *tv, int deep, int lock));
383static int tv_islocked __ARGS((typval_T *tv));
384
Bram Moolenaar33570922005-01-25 22:26:29 +0000385static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
386static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
387static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
388static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
389static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
390static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
391static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
392static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000393
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000394static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000395static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
397static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
398static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
399static list_T *list_alloc __ARGS((void));
Bram Moolenaar33570922005-01-25 22:26:29 +0000400static void list_free __ARGS((list_T *l));
401static listitem_T *listitem_alloc __ARGS((void));
402static void listitem_free __ARGS((listitem_T *item));
403static void listitem_remove __ARGS((list_T *l, listitem_T *item));
404static long list_len __ARGS((list_T *l));
405static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
406static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
407static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000408static listitem_T *list_find __ARGS((list_T *l, long n));
409static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static void list_append __ARGS((list_T *l, listitem_T *item));
411static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000412static int list_append_string __ARGS((list_T *l, char_u *str));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
414static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
415static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000416static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
418static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000419static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000420static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
421static void set_ref_in_list __ARGS((list_T *l, int copyID));
422static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static void dict_unref __ARGS((dict_T *d));
424static void dict_free __ARGS((dict_T *d));
425static dictitem_T *dictitem_alloc __ARGS((char_u *key));
426static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
427static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
428static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000429static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static int dict_add __ARGS((dict_T *d, dictitem_T *item));
431static long dict_len __ARGS((dict_T *d));
432static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
433static char_u *dict2string __ARGS((typval_T *tv));
434static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
436static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
437static char_u *string_quote __ARGS((char_u *str, int function));
438static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
439static int find_internal_func __ARGS((char_u *name));
440static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
441static 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));
442static 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));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000443static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444
445static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000464#if defined(FEAT_INS_EXPAND)
465static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
473static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000499static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000500static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000501static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000502static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000514static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000537static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000543static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000544static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000560static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000564#ifdef vim_mkdir
565static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
566#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000567static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000571static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000572static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000573static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000574static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000585static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000586static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000592static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000593static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000597static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000598static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000600static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
601#ifdef HAVE_STRFTIME
602static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
603#endif
604static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000616static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000617static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000618static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000619static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000620static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
633static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000634static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000635
Bram Moolenaar33570922005-01-25 22:26:29 +0000636static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
637static int get_env_len __ARGS((char_u **arg));
638static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000639static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000640static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
641#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
642#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
643 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000644static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000646static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000647static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
648static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000649static typval_T *alloc_tv __ARGS((void));
650static typval_T *alloc_string_tv __ARGS((char_u *string));
651static void free_tv __ARGS((typval_T *varp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static void init_tv __ARGS((typval_T *varp));
653static long get_tv_number __ARGS((typval_T *varp));
654static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000655static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000656static char_u *get_tv_string __ARGS((typval_T *varp));
657static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000658static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000659static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000660static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000661static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
662static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
663static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
664static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
665static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
666static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
667static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000668static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000670static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
672static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
673static int eval_fname_script __ARGS((char_u *p));
674static int eval_fname_sid __ARGS((char_u *p));
675static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000676static ufunc_T *find_func __ARGS((char_u *name));
677static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000678static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000679#ifdef FEAT_PROFILE
680static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000681static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
682static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
683static int
684# ifdef __BORLANDC__
685 _RTLENTRYF
686# endif
687 prof_total_cmp __ARGS((const void *s1, const void *s2));
688static int
689# ifdef __BORLANDC__
690 _RTLENTRYF
691# endif
692 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000693#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000694static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000695static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000696static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000697static void func_free __ARGS((ufunc_T *fp));
698static void func_unref __ARGS((char_u *name));
699static void func_ref __ARGS((char_u *name));
700static 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));
701static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
702
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000703/* Character used as separated in autoload function/variable names. */
704#define AUTOLOAD_CHAR '#'
705
Bram Moolenaar33570922005-01-25 22:26:29 +0000706/*
707 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000708 */
709 void
710eval_init()
711{
Bram Moolenaar33570922005-01-25 22:26:29 +0000712 int i;
713 struct vimvar *p;
714
715 init_var_dict(&globvardict, &globvars_var);
716 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000717 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000718 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000719
720 for (i = 0; i < VV_LEN; ++i)
721 {
722 p = &vimvars[i];
723 STRCPY(p->vv_di.di_key, p->vv_name);
724 if (p->vv_flags & VV_RO)
725 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
726 else if (p->vv_flags & VV_RO_SBX)
727 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
728 else
729 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000730
731 /* add to v: scope dict, unless the value is not always available */
732 if (p->vv_type != VAR_UNKNOWN)
733 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000734 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000735 /* add to compat scope dict */
736 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000737 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000738}
739
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000740#if defined(EXITFREE) || defined(PROTO)
741 void
742eval_clear()
743{
744 int i;
745 struct vimvar *p;
746
747 for (i = 0; i < VV_LEN; ++i)
748 {
749 p = &vimvars[i];
750 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000751 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000752 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000753 p->vv_di.di_tv.vval.v_string = NULL;
754 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000755 }
756 hash_clear(&vimvarht);
757 hash_clear(&compat_hashtab);
758
759 /* script-local variables */
760 for (i = 1; i <= ga_scripts.ga_len; ++i)
761 vars_clear(&SCRIPT_VARS(i));
762 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000763 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000764
765 /* global variables */
766 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000767
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000768 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000769 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000770 hash_clear(&func_hashtab);
771
772 /* unreferenced lists and dicts */
773 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000774}
775#endif
776
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000777/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 * Return the name of the executed function.
779 */
780 char_u *
781func_name(cookie)
782 void *cookie;
783{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000784 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785}
786
787/*
788 * Return the address holding the next breakpoint line for a funccall cookie.
789 */
790 linenr_T *
791func_breakpoint(cookie)
792 void *cookie;
793{
Bram Moolenaar33570922005-01-25 22:26:29 +0000794 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795}
796
797/*
798 * Return the address holding the debug tick for a funccall cookie.
799 */
800 int *
801func_dbg_tick(cookie)
802 void *cookie;
803{
Bram Moolenaar33570922005-01-25 22:26:29 +0000804 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805}
806
807/*
808 * Return the nesting level for a funccall cookie.
809 */
810 int
811func_level(cookie)
812 void *cookie;
813{
Bram Moolenaar33570922005-01-25 22:26:29 +0000814 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815}
816
817/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000818funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819
820/*
821 * Return TRUE when a function was ended by a ":return" command.
822 */
823 int
824current_func_returned()
825{
826 return current_funccal->returned;
827}
828
829
830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 * Set an internal variable to a string value. Creates the variable if it does
832 * not already exist.
833 */
834 void
835set_internal_string_var(name, value)
836 char_u *name;
837 char_u *value;
838{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000839 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000840 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841
842 val = vim_strsave(value);
843 if (val != NULL)
844 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000845 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000846 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000848 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000849 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851 }
852}
853
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000854static lval_T *redir_lval = NULL;
855static char_u *redir_endp = NULL;
856static char_u *redir_varname = NULL;
857
858/*
859 * Start recording command output to a variable
860 * Returns OK if successfully completed the setup. FAIL otherwise.
861 */
862 int
863var_redir_start(name, append)
864 char_u *name;
865 int append; /* append to an existing variable */
866{
867 int save_emsg;
868 int err;
869 typval_T tv;
870
871 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000872 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000873 {
874 EMSG(_(e_invarg));
875 return FAIL;
876 }
877
878 redir_varname = vim_strsave(name);
879 if (redir_varname == NULL)
880 return FAIL;
881
882 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
883 if (redir_lval == NULL)
884 {
885 var_redir_stop();
886 return FAIL;
887 }
888
889 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000890 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
891 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000892 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
893 {
894 if (redir_endp != NULL && *redir_endp != NUL)
895 /* Trailing characters are present after the variable name */
896 EMSG(_(e_trailing));
897 else
898 EMSG(_(e_invarg));
899 var_redir_stop();
900 return FAIL;
901 }
902
903 /* check if we can write to the variable: set it to or append an empty
904 * string */
905 save_emsg = did_emsg;
906 did_emsg = FALSE;
907 tv.v_type = VAR_STRING;
908 tv.vval.v_string = (char_u *)"";
909 if (append)
910 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
911 else
912 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
913 err = did_emsg;
914 did_emsg += save_emsg;
915 if (err)
916 {
917 var_redir_stop();
918 return FAIL;
919 }
920 if (redir_lval->ll_newkey != NULL)
921 {
922 /* Dictionary item was created, don't do it again. */
923 vim_free(redir_lval->ll_newkey);
924 redir_lval->ll_newkey = NULL;
925 }
926
927 return OK;
928}
929
930/*
931 * Append "value[len]" to the variable set by var_redir_start().
932 */
933 void
934var_redir_str(value, len)
935 char_u *value;
936 int len;
937{
938 char_u *val;
939 typval_T tv;
940 int save_emsg;
941 int err;
942
943 if (redir_lval == NULL)
944 return;
945
946 if (len == -1)
947 /* Append the entire string */
948 val = vim_strsave(value);
949 else
950 /* Append only the specified number of characters */
951 val = vim_strnsave(value, len);
952 if (val == NULL)
953 return;
954
955 tv.v_type = VAR_STRING;
956 tv.vval.v_string = val;
957
958 save_emsg = did_emsg;
959 did_emsg = FALSE;
960 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
961 err = did_emsg;
962 did_emsg += save_emsg;
963 if (err)
964 var_redir_stop();
965
966 vim_free(tv.vval.v_string);
967}
968
969/*
970 * Stop redirecting command output to a variable.
971 */
972 void
973var_redir_stop()
974{
975 if (redir_lval != NULL)
976 {
977 clear_lval(redir_lval);
978 vim_free(redir_lval);
979 redir_lval = NULL;
980 }
981 vim_free(redir_varname);
982 redir_varname = NULL;
983}
984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985# if defined(FEAT_MBYTE) || defined(PROTO)
986 int
987eval_charconvert(enc_from, enc_to, fname_from, fname_to)
988 char_u *enc_from;
989 char_u *enc_to;
990 char_u *fname_from;
991 char_u *fname_to;
992{
993 int err = FALSE;
994
995 set_vim_var_string(VV_CC_FROM, enc_from, -1);
996 set_vim_var_string(VV_CC_TO, enc_to, -1);
997 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
998 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
999 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1000 err = TRUE;
1001 set_vim_var_string(VV_CC_FROM, NULL, -1);
1002 set_vim_var_string(VV_CC_TO, NULL, -1);
1003 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1004 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1005
1006 if (err)
1007 return FAIL;
1008 return OK;
1009}
1010# endif
1011
1012# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1013 int
1014eval_printexpr(fname, args)
1015 char_u *fname;
1016 char_u *args;
1017{
1018 int err = FALSE;
1019
1020 set_vim_var_string(VV_FNAME_IN, fname, -1);
1021 set_vim_var_string(VV_CMDARG, args, -1);
1022 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1023 err = TRUE;
1024 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1025 set_vim_var_string(VV_CMDARG, NULL, -1);
1026
1027 if (err)
1028 {
1029 mch_remove(fname);
1030 return FAIL;
1031 }
1032 return OK;
1033}
1034# endif
1035
1036# if defined(FEAT_DIFF) || defined(PROTO)
1037 void
1038eval_diff(origfile, newfile, outfile)
1039 char_u *origfile;
1040 char_u *newfile;
1041 char_u *outfile;
1042{
1043 int err = FALSE;
1044
1045 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1046 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1047 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1048 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1049 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1050 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1051 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1052}
1053
1054 void
1055eval_patch(origfile, difffile, outfile)
1056 char_u *origfile;
1057 char_u *difffile;
1058 char_u *outfile;
1059{
1060 int err;
1061
1062 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1063 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1064 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1065 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1066 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1067 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1068 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1069}
1070# endif
1071
1072/*
1073 * Top level evaluation function, returning a boolean.
1074 * Sets "error" to TRUE if there was an error.
1075 * Return TRUE or FALSE.
1076 */
1077 int
1078eval_to_bool(arg, error, nextcmd, skip)
1079 char_u *arg;
1080 int *error;
1081 char_u **nextcmd;
1082 int skip; /* only parse, don't execute */
1083{
Bram Moolenaar33570922005-01-25 22:26:29 +00001084 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 int retval = FALSE;
1086
1087 if (skip)
1088 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001089 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 else
1092 {
1093 *error = FALSE;
1094 if (!skip)
1095 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001096 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001097 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 }
1099 }
1100 if (skip)
1101 --emsg_skip;
1102
1103 return retval;
1104}
1105
1106/*
1107 * Top level evaluation function, returning a string. If "skip" is TRUE,
1108 * only parsing to "nextcmd" is done, without reporting errors. Return
1109 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1110 */
1111 char_u *
1112eval_to_string_skip(arg, nextcmd, skip)
1113 char_u *arg;
1114 char_u **nextcmd;
1115 int skip; /* only parse, don't execute */
1116{
Bram Moolenaar33570922005-01-25 22:26:29 +00001117 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 char_u *retval;
1119
1120 if (skip)
1121 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001122 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 retval = NULL;
1124 else
1125 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001126 retval = vim_strsave(get_tv_string(&tv));
1127 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 }
1129 if (skip)
1130 --emsg_skip;
1131
1132 return retval;
1133}
1134
1135/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001136 * Skip over an expression at "*pp".
1137 * Return FAIL for an error, OK otherwise.
1138 */
1139 int
1140skip_expr(pp)
1141 char_u **pp;
1142{
Bram Moolenaar33570922005-01-25 22:26:29 +00001143 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001144
1145 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001146 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001147}
1148
1149/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 * Top level evaluation function, returning a string.
1151 * Return pointer to allocated memory, or NULL for failure.
1152 */
1153 char_u *
1154eval_to_string(arg, nextcmd)
1155 char_u *arg;
1156 char_u **nextcmd;
1157{
Bram Moolenaar33570922005-01-25 22:26:29 +00001158 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 char_u *retval;
1160
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001161 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 retval = NULL;
1163 else
1164 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001165 retval = vim_strsave(get_tv_string(&tv));
1166 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 }
1168
1169 return retval;
1170}
1171
1172/*
1173 * Call eval_to_string() with "sandbox" set and not using local variables.
1174 */
1175 char_u *
1176eval_to_string_safe(arg, nextcmd)
1177 char_u *arg;
1178 char_u **nextcmd;
1179{
1180 char_u *retval;
1181 void *save_funccalp;
1182
1183 save_funccalp = save_funccal();
1184 ++sandbox;
1185 retval = eval_to_string(arg, nextcmd);
1186 --sandbox;
1187 restore_funccal(save_funccalp);
1188 return retval;
1189}
1190
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191/*
1192 * Top level evaluation function, returning a number.
1193 * Evaluates "expr" silently.
1194 * Returns -1 for an error.
1195 */
1196 int
1197eval_to_number(expr)
1198 char_u *expr;
1199{
Bram Moolenaar33570922005-01-25 22:26:29 +00001200 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001202 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203
1204 ++emsg_off;
1205
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001206 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 retval = -1;
1208 else
1209 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001210 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001211 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 }
1213 --emsg_off;
1214
1215 return retval;
1216}
1217
Bram Moolenaara40058a2005-07-11 22:42:07 +00001218/*
1219 * Prepare v: variable "idx" to be used.
1220 * Save the current typeval in "save_tv".
1221 * When not used yet add the variable to the v: hashtable.
1222 */
1223 static void
1224prepare_vimvar(idx, save_tv)
1225 int idx;
1226 typval_T *save_tv;
1227{
1228 *save_tv = vimvars[idx].vv_tv;
1229 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1230 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1231}
1232
1233/*
1234 * Restore v: variable "idx" to typeval "save_tv".
1235 * When no longer defined, remove the variable from the v: hashtable.
1236 */
1237 static void
1238restore_vimvar(idx, save_tv)
1239 int idx;
1240 typval_T *save_tv;
1241{
1242 hashitem_T *hi;
1243
1244 clear_tv(&vimvars[idx].vv_tv);
1245 vimvars[idx].vv_tv = *save_tv;
1246 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1247 {
1248 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1249 if (HASHITEM_EMPTY(hi))
1250 EMSG2(_(e_intern2), "restore_vimvar()");
1251 else
1252 hash_remove(&vimvarht, hi);
1253 }
1254}
1255
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001256#if defined(FEAT_SYN_HL) || defined(PROTO)
1257/*
1258 * Evaluate an expression to a list with suggestions.
1259 * For the "expr:" part of 'spellsuggest'.
1260 */
1261 list_T *
1262eval_spell_expr(badword, expr)
1263 char_u *badword;
1264 char_u *expr;
1265{
1266 typval_T save_val;
1267 typval_T rettv;
1268 list_T *list = NULL;
1269 char_u *p = skipwhite(expr);
1270
1271 /* Set "v:val" to the bad word. */
1272 prepare_vimvar(VV_VAL, &save_val);
1273 vimvars[VV_VAL].vv_type = VAR_STRING;
1274 vimvars[VV_VAL].vv_str = badword;
1275 if (p_verbose == 0)
1276 ++emsg_off;
1277
1278 if (eval1(&p, &rettv, TRUE) == OK)
1279 {
1280 if (rettv.v_type != VAR_LIST)
1281 clear_tv(&rettv);
1282 else
1283 list = rettv.vval.v_list;
1284 }
1285
1286 if (p_verbose == 0)
1287 --emsg_off;
1288 vimvars[VV_VAL].vv_str = NULL;
1289 restore_vimvar(VV_VAL, &save_val);
1290
1291 return list;
1292}
1293
1294/*
1295 * "list" is supposed to contain two items: a word and a number. Return the
1296 * word in "pp" and the number as the return value.
1297 * Return -1 if anything isn't right.
1298 * Used to get the good word and score from the eval_spell_expr() result.
1299 */
1300 int
1301get_spellword(list, pp)
1302 list_T *list;
1303 char_u **pp;
1304{
1305 listitem_T *li;
1306
1307 li = list->lv_first;
1308 if (li == NULL)
1309 return -1;
1310 *pp = get_tv_string(&li->li_tv);
1311
1312 li = li->li_next;
1313 if (li == NULL)
1314 return -1;
1315 return get_tv_number(&li->li_tv);
1316}
1317#endif
1318
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001319/*
1320 * Top level evaluation function,
1321 */
1322 typval_T *
1323eval_expr(arg, nextcmd)
1324 char_u *arg;
1325 char_u **nextcmd;
1326{
1327 typval_T *tv;
1328
1329 tv = (typval_T *)alloc(sizeof(typval_T));
1330 if (!tv)
1331 return NULL;
1332
1333 if (eval0(arg, tv, nextcmd, TRUE) == FAIL)
1334 {
1335 vim_free(tv);
1336 return NULL;
1337 }
1338
1339 return tv;
1340}
1341
1342
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1344/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001345 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001347 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001349 static int
1350call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 char_u *func;
1352 int argc;
1353 char_u **argv;
1354 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356{
Bram Moolenaar33570922005-01-25 22:26:29 +00001357 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 long n;
1359 int len;
1360 int i;
1361 int doesrange;
1362 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001363 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364
Bram Moolenaar33570922005-01-25 22:26:29 +00001365 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001367 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368
1369 for (i = 0; i < argc; i++)
1370 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001371 /* Pass a NULL or empty argument as an empty string */
1372 if (argv[i] == NULL || *argv[i] == NUL)
1373 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001374 argvars[i].v_type = VAR_STRING;
1375 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001376 continue;
1377 }
1378
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 /* Recognize a number argument, the others must be strings. */
1380 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1381 if (len != 0 && len == (int)STRLEN(argv[i]))
1382 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001383 argvars[i].v_type = VAR_NUMBER;
1384 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
1386 else
1387 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001388 argvars[i].v_type = VAR_STRING;
1389 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391 }
1392
1393 if (safe)
1394 {
1395 save_funccalp = save_funccal();
1396 ++sandbox;
1397 }
1398
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001399 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1400 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001402 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 if (safe)
1404 {
1405 --sandbox;
1406 restore_funccal(save_funccalp);
1407 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001408 vim_free(argvars);
1409
1410 if (ret == FAIL)
1411 clear_tv(rettv);
1412
1413 return ret;
1414}
1415
1416/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001417 * Call vimL function "func" and return the result as a string.
1418 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001419 * Uses argv[argc] for the function arguments.
1420 */
1421 void *
1422call_func_retstr(func, argc, argv, safe)
1423 char_u *func;
1424 int argc;
1425 char_u **argv;
1426 int safe; /* use the sandbox */
1427{
1428 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001429 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001430
1431 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1432 return NULL;
1433
1434 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001435 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 return retval;
1437}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001438
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001439#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001440/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001441 * Call vimL function "func" and return the result as a number.
1442 * Returns -1 when calling the function fails.
1443 * Uses argv[argc] for the function arguments.
1444 */
1445 long
1446call_func_retnr(func, argc, argv, safe)
1447 char_u *func;
1448 int argc;
1449 char_u **argv;
1450 int safe; /* use the sandbox */
1451{
1452 typval_T rettv;
1453 long retval;
1454
1455 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1456 return -1;
1457
1458 retval = get_tv_number_chk(&rettv, NULL);
1459 clear_tv(&rettv);
1460 return retval;
1461}
1462#endif
1463
1464/*
1465 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001466 * Uses argv[argc] for the function arguments.
1467 */
1468 void *
1469call_func_retlist(func, argc, argv, safe)
1470 char_u *func;
1471 int argc;
1472 char_u **argv;
1473 int safe; /* use the sandbox */
1474{
1475 typval_T rettv;
1476
1477 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1478 return NULL;
1479
1480 if (rettv.v_type != VAR_LIST)
1481 {
1482 clear_tv(&rettv);
1483 return NULL;
1484 }
1485
1486 return rettv.vval.v_list;
1487}
1488
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489#endif
1490
1491/*
1492 * Save the current function call pointer, and set it to NULL.
1493 * Used when executing autocommands and for ":source".
1494 */
1495 void *
1496save_funccal()
1497{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001498 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 current_funccal = NULL;
1501 return (void *)fc;
1502}
1503
1504 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001505restore_funccal(vfc)
1506 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001508 funccall_T *fc = (funccall_T *)vfc;
1509
1510 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511}
1512
Bram Moolenaar05159a02005-02-26 23:04:13 +00001513#if defined(FEAT_PROFILE) || defined(PROTO)
1514/*
1515 * Prepare profiling for entering a child or something else that is not
1516 * counted for the script/function itself.
1517 * Should always be called in pair with prof_child_exit().
1518 */
1519 void
1520prof_child_enter(tm)
1521 proftime_T *tm; /* place to store waittime */
1522{
1523 funccall_T *fc = current_funccal;
1524
1525 if (fc != NULL && fc->func->uf_profiling)
1526 profile_start(&fc->prof_child);
1527 script_prof_save(tm);
1528}
1529
1530/*
1531 * Take care of time spent in a child.
1532 * Should always be called after prof_child_enter().
1533 */
1534 void
1535prof_child_exit(tm)
1536 proftime_T *tm; /* where waittime was stored */
1537{
1538 funccall_T *fc = current_funccal;
1539
1540 if (fc != NULL && fc->func->uf_profiling)
1541 {
1542 profile_end(&fc->prof_child);
1543 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1544 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1545 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1546 }
1547 script_prof_restore(tm);
1548}
1549#endif
1550
1551
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552#ifdef FEAT_FOLDING
1553/*
1554 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1555 * it in "*cp". Doesn't give error messages.
1556 */
1557 int
1558eval_foldexpr(arg, cp)
1559 char_u *arg;
1560 int *cp;
1561{
Bram Moolenaar33570922005-01-25 22:26:29 +00001562 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 int retval;
1564 char_u *s;
1565
1566 ++emsg_off;
1567 ++sandbox;
1568 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001569 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 retval = 0;
1571 else
1572 {
1573 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001574 if (tv.v_type == VAR_NUMBER)
1575 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001576 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 retval = 0;
1578 else
1579 {
1580 /* If the result is a string, check if there is a non-digit before
1581 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001582 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 if (!VIM_ISDIGIT(*s) && *s != '-')
1584 *cp = *s++;
1585 retval = atol((char *)s);
1586 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001587 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 }
1589 --emsg_off;
1590 --sandbox;
1591
1592 return retval;
1593}
1594#endif
1595
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001597 * ":let" list all variable values
1598 * ":let var1 var2" list variable values
1599 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001600 * ":let var += expr" assignment command.
1601 * ":let var -= expr" assignment command.
1602 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001603 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 */
1605 void
1606ex_let(eap)
1607 exarg_T *eap;
1608{
1609 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001610 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001611 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001613 int var_count = 0;
1614 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001615 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001617 expr = skip_var_list(arg, &var_count, &semicolon);
1618 if (expr == NULL)
1619 return;
1620 expr = vim_strchr(expr, '=');
1621 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001623 /*
1624 * ":let" without "=": list variables
1625 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001626 if (*arg == '[')
1627 EMSG(_(e_invarg));
1628 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001629 /* ":let var1 var2" */
1630 arg = list_arg_vars(eap, arg);
1631 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001632 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001633 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001634 list_glob_vars();
1635 list_buf_vars();
1636 list_win_vars();
1637 list_vim_vars();
1638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 eap->nextcmd = check_nextcmd(arg);
1640 }
1641 else
1642 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001643 op[0] = '=';
1644 op[1] = NUL;
1645 if (expr > arg)
1646 {
1647 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1648 op[0] = expr[-1]; /* +=, -= or .= */
1649 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001650 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001651
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 if (eap->skip)
1653 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001654 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if (eap->skip)
1656 {
1657 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001658 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 --emsg_skip;
1660 }
1661 else if (i != FAIL)
1662 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001663 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001664 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001665 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 }
1667 }
1668}
1669
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001670/*
1671 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1672 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001673 * When "nextchars" is not NULL it points to a string with characters that
1674 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1675 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001676 * Returns OK or FAIL;
1677 */
1678 static int
1679ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1680 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001681 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001682 int copy; /* copy values from "tv", don't move */
1683 int semicolon; /* from skip_var_list() */
1684 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001685 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001686{
1687 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001688 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001689 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001690 listitem_T *item;
1691 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692
1693 if (*arg != '[')
1694 {
1695 /*
1696 * ":let var = expr" or ":for var in list"
1697 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001698 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001699 return FAIL;
1700 return OK;
1701 }
1702
1703 /*
1704 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1705 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001706 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001707 {
1708 EMSG(_(e_listreq));
1709 return FAIL;
1710 }
1711
1712 i = list_len(l);
1713 if (semicolon == 0 && var_count < i)
1714 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001715 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001716 return FAIL;
1717 }
1718 if (var_count - semicolon > i)
1719 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001720 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001721 return FAIL;
1722 }
1723
1724 item = l->lv_first;
1725 while (*arg != ']')
1726 {
1727 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001728 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001729 item = item->li_next;
1730 if (arg == NULL)
1731 return FAIL;
1732
1733 arg = skipwhite(arg);
1734 if (*arg == ';')
1735 {
1736 /* Put the rest of the list (may be empty) in the var after ';'.
1737 * Create a new list for this. */
1738 l = list_alloc();
1739 if (l == NULL)
1740 return FAIL;
1741 while (item != NULL)
1742 {
1743 list_append_tv(l, &item->li_tv);
1744 item = item->li_next;
1745 }
1746
1747 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001748 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001749 ltv.vval.v_list = l;
1750 l->lv_refcount = 1;
1751
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001752 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1753 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001754 clear_tv(&ltv);
1755 if (arg == NULL)
1756 return FAIL;
1757 break;
1758 }
1759 else if (*arg != ',' && *arg != ']')
1760 {
1761 EMSG2(_(e_intern2), "ex_let_vars()");
1762 return FAIL;
1763 }
1764 }
1765
1766 return OK;
1767}
1768
1769/*
1770 * Skip over assignable variable "var" or list of variables "[var, var]".
1771 * Used for ":let varvar = expr" and ":for varvar in expr".
1772 * For "[var, var]" increment "*var_count" for each variable.
1773 * for "[var, var; var]" set "semicolon".
1774 * Return NULL for an error.
1775 */
1776 static char_u *
1777skip_var_list(arg, var_count, semicolon)
1778 char_u *arg;
1779 int *var_count;
1780 int *semicolon;
1781{
1782 char_u *p, *s;
1783
1784 if (*arg == '[')
1785 {
1786 /* "[var, var]": find the matching ']'. */
1787 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001788 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001789 {
1790 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1791 s = skip_var_one(p);
1792 if (s == p)
1793 {
1794 EMSG2(_(e_invarg2), p);
1795 return NULL;
1796 }
1797 ++*var_count;
1798
1799 p = skipwhite(s);
1800 if (*p == ']')
1801 break;
1802 else if (*p == ';')
1803 {
1804 if (*semicolon == 1)
1805 {
1806 EMSG(_("Double ; in list of variables"));
1807 return NULL;
1808 }
1809 *semicolon = 1;
1810 }
1811 else if (*p != ',')
1812 {
1813 EMSG2(_(e_invarg2), p);
1814 return NULL;
1815 }
1816 }
1817 return p + 1;
1818 }
1819 else
1820 return skip_var_one(arg);
1821}
1822
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001823/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001824 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1825 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001826 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001827 static char_u *
1828skip_var_one(arg)
1829 char_u *arg;
1830{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001831 if (*arg == '@' && arg[1] != NUL)
1832 return arg + 2;
1833 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1834 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001835}
1836
Bram Moolenaara7043832005-01-21 11:56:39 +00001837/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001838 * List variables for hashtab "ht" with prefix "prefix".
1839 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001840 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001842list_hashtable_vars(ht, prefix, empty)
1843 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001844 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001845 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001846{
Bram Moolenaar33570922005-01-25 22:26:29 +00001847 hashitem_T *hi;
1848 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001849 int todo;
1850
1851 todo = ht->ht_used;
1852 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1853 {
1854 if (!HASHITEM_EMPTY(hi))
1855 {
1856 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001857 di = HI2DI(hi);
1858 if (empty || di->di_tv.v_type != VAR_STRING
1859 || di->di_tv.vval.v_string != NULL)
1860 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001861 }
1862 }
1863}
1864
1865/*
1866 * List global variables.
1867 */
1868 static void
1869list_glob_vars()
1870{
Bram Moolenaar33570922005-01-25 22:26:29 +00001871 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001872}
1873
1874/*
1875 * List buffer variables.
1876 */
1877 static void
1878list_buf_vars()
1879{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001880 char_u numbuf[NUMBUFLEN];
1881
Bram Moolenaar33570922005-01-25 22:26:29 +00001882 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001883
1884 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1885 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001886}
1887
1888/*
1889 * List window variables.
1890 */
1891 static void
1892list_win_vars()
1893{
Bram Moolenaar33570922005-01-25 22:26:29 +00001894 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001895}
1896
1897/*
1898 * List Vim variables.
1899 */
1900 static void
1901list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902{
Bram Moolenaar33570922005-01-25 22:26:29 +00001903 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904}
1905
1906/*
1907 * List variables in "arg".
1908 */
1909 static char_u *
1910list_arg_vars(eap, arg)
1911 exarg_T *eap;
1912 char_u *arg;
1913{
1914 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001915 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001916 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001917 char_u *name_start;
1918 char_u *arg_subsc;
1919 char_u *tofree;
1920 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001921
1922 while (!ends_excmd(*arg) && !got_int)
1923 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001924 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001926 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001927 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1928 {
1929 emsg_severe = TRUE;
1930 EMSG(_(e_trailing));
1931 break;
1932 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001934 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001935 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001936 /* get_name_len() takes care of expanding curly braces */
1937 name_start = name = arg;
1938 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1939 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001940 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001941 /* This is mainly to keep test 49 working: when expanding
1942 * curly braces fails overrule the exception error message. */
1943 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001944 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001945 emsg_severe = TRUE;
1946 EMSG2(_(e_invarg2), arg);
1947 break;
1948 }
1949 error = TRUE;
1950 }
1951 else
1952 {
1953 if (tofree != NULL)
1954 name = tofree;
1955 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 else
1958 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001959 /* handle d.key, l[idx], f(expr) */
1960 arg_subsc = arg;
1961 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001962 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001964 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001965 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001966 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001967 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001968 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001969 case 'g': list_glob_vars(); break;
1970 case 'b': list_buf_vars(); break;
1971 case 'w': list_win_vars(); break;
1972 case 'v': list_vim_vars(); break;
1973 default:
1974 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001975 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001976 }
1977 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001978 {
1979 char_u numbuf[NUMBUFLEN];
1980 char_u *tf;
1981 int c;
1982 char_u *s;
1983
1984 s = echo_string(&tv, &tf, numbuf);
1985 c = *arg;
1986 *arg = NUL;
1987 list_one_var_a((char_u *)"",
1988 arg == arg_subsc ? name : name_start,
1989 tv.v_type, s == NULL ? (char_u *)"" : s);
1990 *arg = c;
1991 vim_free(tf);
1992 }
1993 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001994 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001995 }
1996 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001997
1998 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001999 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002000
2001 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002002 }
2003
2004 return arg;
2005}
2006
2007/*
2008 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2009 * Returns a pointer to the char just after the var name.
2010 * Returns NULL if there is an error.
2011 */
2012 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002013ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002014 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002015 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002016 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002017 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002018 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002019{
2020 int c1;
2021 char_u *name;
2022 char_u *p;
2023 char_u *arg_end = NULL;
2024 int len;
2025 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002026 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002027
2028 /*
2029 * ":let $VAR = expr": Set environment variable.
2030 */
2031 if (*arg == '$')
2032 {
2033 /* Find the end of the name. */
2034 ++arg;
2035 name = arg;
2036 len = get_env_len(&arg);
2037 if (len == 0)
2038 EMSG2(_(e_invarg2), name - 1);
2039 else
2040 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002041 if (op != NULL && (*op == '+' || *op == '-'))
2042 EMSG2(_(e_letwrong), op);
2043 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002044 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002045 EMSG(_(e_letunexp));
2046 else
2047 {
2048 c1 = name[len];
2049 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002050 p = get_tv_string_chk(tv);
2051 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002052 {
2053 int mustfree = FALSE;
2054 char_u *s = vim_getenv(name, &mustfree);
2055
2056 if (s != NULL)
2057 {
2058 p = tofree = concat_str(s, p);
2059 if (mustfree)
2060 vim_free(s);
2061 }
2062 }
2063 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002064 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002065 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002066 if (STRICMP(name, "HOME") == 0)
2067 init_homedir();
2068 else if (didset_vim && STRICMP(name, "VIM") == 0)
2069 didset_vim = FALSE;
2070 else if (didset_vimruntime
2071 && STRICMP(name, "VIMRUNTIME") == 0)
2072 didset_vimruntime = FALSE;
2073 arg_end = arg;
2074 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002075 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002076 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002077 }
2078 }
2079 }
2080
2081 /*
2082 * ":let &option = expr": Set option value.
2083 * ":let &l:option = expr": Set local option value.
2084 * ":let &g:option = expr": Set global option value.
2085 */
2086 else if (*arg == '&')
2087 {
2088 /* Find the end of the name. */
2089 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002090 if (p == NULL || (endchars != NULL
2091 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002092 EMSG(_(e_letunexp));
2093 else
2094 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002095 long n;
2096 int opt_type;
2097 long numval;
2098 char_u *stringval = NULL;
2099 char_u *s;
2100
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002101 c1 = *p;
2102 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002103
2104 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002105 s = get_tv_string_chk(tv); /* != NULL if number or string */
2106 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002107 {
2108 opt_type = get_option_value(arg, &numval,
2109 &stringval, opt_flags);
2110 if ((opt_type == 1 && *op == '.')
2111 || (opt_type == 0 && *op != '.'))
2112 EMSG2(_(e_letwrong), op);
2113 else
2114 {
2115 if (opt_type == 1) /* number */
2116 {
2117 if (*op == '+')
2118 n = numval + n;
2119 else
2120 n = numval - n;
2121 }
2122 else if (opt_type == 0 && stringval != NULL) /* string */
2123 {
2124 s = concat_str(stringval, s);
2125 vim_free(stringval);
2126 stringval = s;
2127 }
2128 }
2129 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002130 if (s != NULL)
2131 {
2132 set_option_value(arg, n, s, opt_flags);
2133 arg_end = p;
2134 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002135 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002136 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002137 }
2138 }
2139
2140 /*
2141 * ":let @r = expr": Set register contents.
2142 */
2143 else if (*arg == '@')
2144 {
2145 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002146 if (op != NULL && (*op == '+' || *op == '-'))
2147 EMSG2(_(e_letwrong), op);
2148 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002149 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002150 EMSG(_(e_letunexp));
2151 else
2152 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002153 char_u *tofree = NULL;
2154 char_u *s;
2155
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002156 p = get_tv_string_chk(tv);
2157 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002158 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002159 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002160 if (s != NULL)
2161 {
2162 p = tofree = concat_str(s, p);
2163 vim_free(s);
2164 }
2165 }
2166 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002167 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002168 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002169 arg_end = arg + 1;
2170 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002171 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172 }
2173 }
2174
2175 /*
2176 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002177 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002178 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002179 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002180 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002181 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002183 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002184 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2187 EMSG(_(e_letunexp));
2188 else
2189 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002190 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 arg_end = p;
2192 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002194 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 }
2196
2197 else
2198 EMSG2(_(e_invarg2), arg);
2199
2200 return arg_end;
2201}
2202
2203/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002204 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2205 */
2206 static int
2207check_changedtick(arg)
2208 char_u *arg;
2209{
2210 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2211 {
2212 EMSG2(_(e_readonlyvar), arg);
2213 return TRUE;
2214 }
2215 return FALSE;
2216}
2217
2218/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002219 * Get an lval: variable, Dict item or List item that can be assigned a value
2220 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2221 * "name.key", "name.key[expr]" etc.
2222 * Indexing only works if "name" is an existing List or Dictionary.
2223 * "name" points to the start of the name.
2224 * If "rettv" is not NULL it points to the value to be assigned.
2225 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2226 * wrong; must end in space or cmd separator.
2227 *
2228 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002229 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002230 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002231 */
2232 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002233get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002234 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002235 typval_T *rettv;
2236 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002237 int unlet;
2238 int skip;
2239 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002240 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002243 char_u *expr_start, *expr_end;
2244 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002245 dictitem_T *v;
2246 typval_T var1;
2247 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002248 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002249 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002250 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002251 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002252 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002254 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002255 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002256
2257 if (skip)
2258 {
2259 /* When skipping just find the end of the name. */
2260 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002261 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262 }
2263
2264 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002265 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002266 if (expr_start != NULL)
2267 {
2268 /* Don't expand the name when we already know there is an error. */
2269 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2270 && *p != '[' && *p != '.')
2271 {
2272 EMSG(_(e_trailing));
2273 return NULL;
2274 }
2275
2276 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2277 if (lp->ll_exp_name == NULL)
2278 {
2279 /* Report an invalid expression in braces, unless the
2280 * expression evaluation has been cancelled due to an
2281 * aborting error, an interrupt, or an exception. */
2282 if (!aborting() && !quiet)
2283 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002284 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002285 EMSG2(_(e_invarg2), name);
2286 return NULL;
2287 }
2288 }
2289 lp->ll_name = lp->ll_exp_name;
2290 }
2291 else
2292 lp->ll_name = name;
2293
2294 /* Without [idx] or .key we are done. */
2295 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2296 return p;
2297
2298 cc = *p;
2299 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002300 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002301 if (v == NULL && !quiet)
2302 EMSG2(_(e_undefvar), lp->ll_name);
2303 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 if (v == NULL)
2305 return NULL;
2306
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307 /*
2308 * Loop until no more [idx] or .key is following.
2309 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002310 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002311 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002312 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002313 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2314 && !(lp->ll_tv->v_type == VAR_DICT
2315 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002317 if (!quiet)
2318 EMSG(_("E689: Can only index a List or Dictionary"));
2319 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002320 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002321 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002322 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002323 if (!quiet)
2324 EMSG(_("E708: [:] must come last"));
2325 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002326 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002327
Bram Moolenaar8c711452005-01-14 21:53:12 +00002328 len = -1;
2329 if (*p == '.')
2330 {
2331 key = p + 1;
2332 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2333 ;
2334 if (len == 0)
2335 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002336 if (!quiet)
2337 EMSG(_(e_emptykey));
2338 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002339 }
2340 p = key + len;
2341 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002342 else
2343 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002344 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002345 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002346 if (*p == ':')
2347 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002348 else
2349 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002350 empty1 = FALSE;
2351 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002352 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002353 if (get_tv_string_chk(&var1) == NULL)
2354 {
2355 /* not a number or string */
2356 clear_tv(&var1);
2357 return NULL;
2358 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002359 }
2360
2361 /* Optionally get the second index [ :expr]. */
2362 if (*p == ':')
2363 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002364 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002365 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002366 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002367 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002368 if (!empty1)
2369 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002370 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002371 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002372 if (rettv != NULL && (rettv->v_type != VAR_LIST
2373 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002374 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002375 if (!quiet)
2376 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002377 if (!empty1)
2378 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002379 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002380 }
2381 p = skipwhite(p + 1);
2382 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002383 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002384 else
2385 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002386 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002387 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2388 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002389 if (!empty1)
2390 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002391 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002392 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002393 if (get_tv_string_chk(&var2) == NULL)
2394 {
2395 /* not a number or string */
2396 if (!empty1)
2397 clear_tv(&var1);
2398 clear_tv(&var2);
2399 return NULL;
2400 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002401 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002402 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002403 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002404 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002405 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002406
Bram Moolenaar8c711452005-01-14 21:53:12 +00002407 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002408 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002409 if (!quiet)
2410 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002411 if (!empty1)
2412 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002414 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002416 }
2417
2418 /* Skip to past ']'. */
2419 ++p;
2420 }
2421
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002422 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002423 {
2424 if (len == -1)
2425 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002426 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002427 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002428 if (*key == NUL)
2429 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 if (!quiet)
2431 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002432 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002434 }
2435 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002436 lp->ll_list = NULL;
2437 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002438 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002439 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002440 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002441 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002442 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002443 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002445 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002446 if (len == -1)
2447 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002448 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 }
2450 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002451 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002452 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002454 if (len == -1)
2455 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002456 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002457 p = NULL;
2458 break;
2459 }
2460 if (len == -1)
2461 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002463 }
2464 else
2465 {
2466 /*
2467 * Get the number and item for the only or first index of the List.
2468 */
2469 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002470 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002471 else
2472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002473 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002474 clear_tv(&var1);
2475 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002476 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 lp->ll_list = lp->ll_tv->vval.v_list;
2478 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2479 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002480 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 if (!quiet)
2482 EMSGN(_(e_listidx), lp->ll_n1);
2483 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002484 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002485 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002486 }
2487
2488 /*
2489 * May need to find the item or absolute index for the second
2490 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002491 * When no index given: "lp->ll_empty2" is TRUE.
2492 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002493 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002495 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002496 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002497 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002498 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002499 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002500 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002501 if (ni == NULL)
2502 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002503 if (!quiet)
2504 EMSGN(_(e_listidx), lp->ll_n2);
2505 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002506 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002507 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002508 }
2509
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002510 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2511 if (lp->ll_n1 < 0)
2512 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2513 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002514 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002515 if (!quiet)
2516 EMSGN(_(e_listidx), lp->ll_n2);
2517 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002518 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002519 }
2520
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002522 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 }
2524
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 return p;
2526}
2527
2528/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002529 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 */
2531 static void
2532clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002534{
2535 vim_free(lp->ll_exp_name);
2536 vim_free(lp->ll_newkey);
2537}
2538
2539/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002540 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002541 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002542 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 */
2544 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002545set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002546 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002548 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002549 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002550 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551{
2552 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002553 listitem_T *ni;
2554 listitem_T *ri;
2555 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556
2557 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002558 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002560 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002561 cc = *endp;
2562 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002563 if (op != NULL && *op != '=')
2564 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002565 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002566
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002567 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002568 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2569 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002570 {
2571 if (tv_op(&tv, rettv, op) == OK)
2572 set_var(lp->ll_name, &tv, FALSE);
2573 clear_tv(&tv);
2574 }
2575 }
2576 else
2577 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002579 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002581 else if (tv_check_lock(lp->ll_newkey == NULL
2582 ? lp->ll_tv->v_lock
2583 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2584 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 else if (lp->ll_range)
2586 {
2587 /*
2588 * Assign the List values to the list items.
2589 */
2590 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002591 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002592 if (op != NULL && *op != '=')
2593 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2594 else
2595 {
2596 clear_tv(&lp->ll_li->li_tv);
2597 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2598 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 ri = ri->li_next;
2600 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2601 break;
2602 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002603 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 /* Need to add an empty item. */
2605 ni = listitem_alloc();
2606 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002607 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 ri = NULL;
2609 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002610 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002612 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 ni->li_tv.vval.v_number = 0;
2614 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002615 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 lp->ll_li = lp->ll_li->li_next;
2617 ++lp->ll_n1;
2618 }
2619 if (ri != NULL)
2620 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002621 else if (lp->ll_empty2
2622 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002623 : lp->ll_n1 != lp->ll_n2)
2624 EMSG(_("E711: List value has not enough items"));
2625 }
2626 else
2627 {
2628 /*
2629 * Assign to a List or Dictionary item.
2630 */
2631 if (lp->ll_newkey != NULL)
2632 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002633 if (op != NULL && *op != '=')
2634 {
2635 EMSG2(_(e_letwrong), op);
2636 return;
2637 }
2638
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002639 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002640 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 if (di == NULL)
2642 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002643 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2644 {
2645 vim_free(di);
2646 return;
2647 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002648 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002649 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002650 else if (op != NULL && *op != '=')
2651 {
2652 tv_op(lp->ll_tv, rettv, op);
2653 return;
2654 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 /*
2659 * Assign the value to the variable or list item.
2660 */
2661 if (copy)
2662 copy_tv(rettv, lp->ll_tv);
2663 else
2664 {
2665 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002666 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002668 }
2669 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002670}
2671
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002672/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002673 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2674 * Returns OK or FAIL.
2675 */
2676 static int
2677tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002678 typval_T *tv1;
2679 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002680 char_u *op;
2681{
2682 long n;
2683 char_u numbuf[NUMBUFLEN];
2684 char_u *s;
2685
2686 /* Can't do anything with a Funcref or a Dict on the right. */
2687 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2688 {
2689 switch (tv1->v_type)
2690 {
2691 case VAR_DICT:
2692 case VAR_FUNC:
2693 break;
2694
2695 case VAR_LIST:
2696 if (*op != '+' || tv2->v_type != VAR_LIST)
2697 break;
2698 /* List += List */
2699 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2700 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2701 return OK;
2702
2703 case VAR_NUMBER:
2704 case VAR_STRING:
2705 if (tv2->v_type == VAR_LIST)
2706 break;
2707 if (*op == '+' || *op == '-')
2708 {
2709 /* nr += nr or nr -= nr*/
2710 n = get_tv_number(tv1);
2711 if (*op == '+')
2712 n += get_tv_number(tv2);
2713 else
2714 n -= get_tv_number(tv2);
2715 clear_tv(tv1);
2716 tv1->v_type = VAR_NUMBER;
2717 tv1->vval.v_number = n;
2718 }
2719 else
2720 {
2721 /* str .= str */
2722 s = get_tv_string(tv1);
2723 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2724 clear_tv(tv1);
2725 tv1->v_type = VAR_STRING;
2726 tv1->vval.v_string = s;
2727 }
2728 return OK;
2729 }
2730 }
2731
2732 EMSG2(_(e_letwrong), op);
2733 return FAIL;
2734}
2735
2736/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002737 * Add a watcher to a list.
2738 */
2739 static void
2740list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002741 list_T *l;
2742 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002743{
2744 lw->lw_next = l->lv_watch;
2745 l->lv_watch = lw;
2746}
2747
2748/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002749 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002750 * No warning when it isn't found...
2751 */
2752 static void
2753list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002754 list_T *l;
2755 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002756{
Bram Moolenaar33570922005-01-25 22:26:29 +00002757 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002758
2759 lwp = &l->lv_watch;
2760 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2761 {
2762 if (lw == lwrem)
2763 {
2764 *lwp = lw->lw_next;
2765 break;
2766 }
2767 lwp = &lw->lw_next;
2768 }
2769}
2770
2771/*
2772 * Just before removing an item from a list: advance watchers to the next
2773 * item.
2774 */
2775 static void
2776list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002777 list_T *l;
2778 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002779{
Bram Moolenaar33570922005-01-25 22:26:29 +00002780 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002781
2782 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2783 if (lw->lw_item == item)
2784 lw->lw_item = item->li_next;
2785}
2786
2787/*
2788 * Evaluate the expression used in a ":for var in expr" command.
2789 * "arg" points to "var".
2790 * Set "*errp" to TRUE for an error, FALSE otherwise;
2791 * Return a pointer that holds the info. Null when there is an error.
2792 */
2793 void *
2794eval_for_line(arg, errp, nextcmdp, skip)
2795 char_u *arg;
2796 int *errp;
2797 char_u **nextcmdp;
2798 int skip;
2799{
Bram Moolenaar33570922005-01-25 22:26:29 +00002800 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002801 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002802 typval_T tv;
2803 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002804
2805 *errp = TRUE; /* default: there is an error */
2806
Bram Moolenaar33570922005-01-25 22:26:29 +00002807 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002808 if (fi == NULL)
2809 return NULL;
2810
2811 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2812 if (expr == NULL)
2813 return fi;
2814
2815 expr = skipwhite(expr);
2816 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2817 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002818 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002819 return fi;
2820 }
2821
2822 if (skip)
2823 ++emsg_skip;
2824 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2825 {
2826 *errp = FALSE;
2827 if (!skip)
2828 {
2829 l = tv.vval.v_list;
2830 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002831 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002832 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002833 clear_tv(&tv);
2834 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002835 else
2836 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002837 /* No need to increment the refcount, it's already set for the
2838 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002839 fi->fi_list = l;
2840 list_add_watch(l, &fi->fi_lw);
2841 fi->fi_lw.lw_item = l->lv_first;
2842 }
2843 }
2844 }
2845 if (skip)
2846 --emsg_skip;
2847
2848 return fi;
2849}
2850
2851/*
2852 * Use the first item in a ":for" list. Advance to the next.
2853 * Assign the values to the variable (list). "arg" points to the first one.
2854 * Return TRUE when a valid item was found, FALSE when at end of list or
2855 * something wrong.
2856 */
2857 int
2858next_for_item(fi_void, arg)
2859 void *fi_void;
2860 char_u *arg;
2861{
Bram Moolenaar33570922005-01-25 22:26:29 +00002862 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002863 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002864 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002865
2866 item = fi->fi_lw.lw_item;
2867 if (item == NULL)
2868 result = FALSE;
2869 else
2870 {
2871 fi->fi_lw.lw_item = item->li_next;
2872 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2873 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2874 }
2875 return result;
2876}
2877
2878/*
2879 * Free the structure used to store info used by ":for".
2880 */
2881 void
2882free_for_info(fi_void)
2883 void *fi_void;
2884{
Bram Moolenaar33570922005-01-25 22:26:29 +00002885 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002886
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002887 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002888 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002889 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002890 list_unref(fi->fi_list);
2891 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002892 vim_free(fi);
2893}
2894
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2896
2897 void
2898set_context_for_expression(xp, arg, cmdidx)
2899 expand_T *xp;
2900 char_u *arg;
2901 cmdidx_T cmdidx;
2902{
2903 int got_eq = FALSE;
2904 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002905 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002907 if (cmdidx == CMD_let)
2908 {
2909 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002910 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002911 {
2912 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002913 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002914 {
2915 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002916 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002917 if (vim_iswhite(*p))
2918 break;
2919 }
2920 return;
2921 }
2922 }
2923 else
2924 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2925 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 while ((xp->xp_pattern = vim_strpbrk(arg,
2927 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2928 {
2929 c = *xp->xp_pattern;
2930 if (c == '&')
2931 {
2932 c = xp->xp_pattern[1];
2933 if (c == '&')
2934 {
2935 ++xp->xp_pattern;
2936 xp->xp_context = cmdidx != CMD_let || got_eq
2937 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2938 }
2939 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002940 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002942 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2943 xp->xp_pattern += 2;
2944
2945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 }
2947 else if (c == '$')
2948 {
2949 /* environment variable */
2950 xp->xp_context = EXPAND_ENV_VARS;
2951 }
2952 else if (c == '=')
2953 {
2954 got_eq = TRUE;
2955 xp->xp_context = EXPAND_EXPRESSION;
2956 }
2957 else if (c == '<'
2958 && xp->xp_context == EXPAND_FUNCTIONS
2959 && vim_strchr(xp->xp_pattern, '(') == NULL)
2960 {
2961 /* Function name can start with "<SNR>" */
2962 break;
2963 }
2964 else if (cmdidx != CMD_let || got_eq)
2965 {
2966 if (c == '"') /* string */
2967 {
2968 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2969 if (c == '\\' && xp->xp_pattern[1] != NUL)
2970 ++xp->xp_pattern;
2971 xp->xp_context = EXPAND_NOTHING;
2972 }
2973 else if (c == '\'') /* literal string */
2974 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002975 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2977 /* skip */ ;
2978 xp->xp_context = EXPAND_NOTHING;
2979 }
2980 else if (c == '|')
2981 {
2982 if (xp->xp_pattern[1] == '|')
2983 {
2984 ++xp->xp_pattern;
2985 xp->xp_context = EXPAND_EXPRESSION;
2986 }
2987 else
2988 xp->xp_context = EXPAND_COMMANDS;
2989 }
2990 else
2991 xp->xp_context = EXPAND_EXPRESSION;
2992 }
2993 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002994 /* Doesn't look like something valid, expand as an expression
2995 * anyway. */
2996 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 arg = xp->xp_pattern;
2998 if (*arg != NUL)
2999 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3000 /* skip */ ;
3001 }
3002 xp->xp_pattern = arg;
3003}
3004
3005#endif /* FEAT_CMDL_COMPL */
3006
3007/*
3008 * ":1,25call func(arg1, arg2)" function call.
3009 */
3010 void
3011ex_call(eap)
3012 exarg_T *eap;
3013{
3014 char_u *arg = eap->arg;
3015 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003017 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003019 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 linenr_T lnum;
3021 int doesrange;
3022 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003023 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003025 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3026 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003027 if (tofree == NULL)
3028 return;
3029
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003030 /* Increase refcount on dictionary, it could get deleted when evaluating
3031 * the arguments. */
3032 if (fudi.fd_dict != NULL)
3033 ++fudi.fd_dict->dv_refcount;
3034
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003035 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3036 len = STRLEN(tofree);
3037 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038
Bram Moolenaar532c7802005-01-27 14:44:31 +00003039 /* Skip white space to allow ":call func ()". Not good, but required for
3040 * backward compatibility. */
3041 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003042 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
3044 if (*startarg != '(')
3045 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003046 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 goto end;
3048 }
3049
3050 /*
3051 * When skipping, evaluate the function once, to find the end of the
3052 * arguments.
3053 * When the function takes a range, this is discovered after the first
3054 * call, and the loop is broken.
3055 */
3056 if (eap->skip)
3057 {
3058 ++emsg_skip;
3059 lnum = eap->line2; /* do it once, also with an invalid range */
3060 }
3061 else
3062 lnum = eap->line1;
3063 for ( ; lnum <= eap->line2; ++lnum)
3064 {
3065 if (!eap->skip && eap->addr_count > 0)
3066 {
3067 curwin->w_cursor.lnum = lnum;
3068 curwin->w_cursor.col = 0;
3069 }
3070 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003071 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003072 eap->line1, eap->line2, &doesrange,
3073 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 {
3075 failed = TRUE;
3076 break;
3077 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003078 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 if (doesrange || eap->skip)
3080 break;
3081 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003082 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003083 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003084 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 if (aborting())
3086 break;
3087 }
3088 if (eap->skip)
3089 --emsg_skip;
3090
3091 if (!failed)
3092 {
3093 /* Check for trailing illegal characters and a following command. */
3094 if (!ends_excmd(*arg))
3095 {
3096 emsg_severe = TRUE;
3097 EMSG(_(e_trailing));
3098 }
3099 else
3100 eap->nextcmd = check_nextcmd(arg);
3101 }
3102
3103end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003104 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003105 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106}
3107
3108/*
3109 * ":unlet[!] var1 ... " command.
3110 */
3111 void
3112ex_unlet(eap)
3113 exarg_T *eap;
3114{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003115 ex_unletlock(eap, eap->arg, 0);
3116}
3117
3118/*
3119 * ":lockvar" and ":unlockvar" commands
3120 */
3121 void
3122ex_lockvar(eap)
3123 exarg_T *eap;
3124{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003126 int deep = 2;
3127
3128 if (eap->forceit)
3129 deep = -1;
3130 else if (vim_isdigit(*arg))
3131 {
3132 deep = getdigits(&arg);
3133 arg = skipwhite(arg);
3134 }
3135
3136 ex_unletlock(eap, arg, deep);
3137}
3138
3139/*
3140 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3141 */
3142 static void
3143ex_unletlock(eap, argstart, deep)
3144 exarg_T *eap;
3145 char_u *argstart;
3146 int deep;
3147{
3148 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003151 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152
3153 do
3154 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003155 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003156 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3157 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003158 if (lv.ll_name == NULL)
3159 error = TRUE; /* error but continue parsing */
3160 if (name_end == NULL || (!vim_iswhite(*name_end)
3161 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003163 if (name_end != NULL)
3164 {
3165 emsg_severe = TRUE;
3166 EMSG(_(e_trailing));
3167 }
3168 if (!(eap->skip || error))
3169 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 break;
3171 }
3172
3173 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003174 {
3175 if (eap->cmdidx == CMD_unlet)
3176 {
3177 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3178 error = TRUE;
3179 }
3180 else
3181 {
3182 if (do_lock_var(&lv, name_end, deep,
3183 eap->cmdidx == CMD_lockvar) == FAIL)
3184 error = TRUE;
3185 }
3186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003188 if (!eap->skip)
3189 clear_lval(&lv);
3190
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 arg = skipwhite(name_end);
3192 } while (!ends_excmd(*arg));
3193
3194 eap->nextcmd = check_nextcmd(arg);
3195}
3196
Bram Moolenaar8c711452005-01-14 21:53:12 +00003197 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003198do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003199 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003200 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003201 int forceit;
3202{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003203 int ret = OK;
3204 int cc;
3205
3206 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003207 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003208 cc = *name_end;
3209 *name_end = NUL;
3210
3211 /* Normal name or expanded name. */
3212 if (check_changedtick(lp->ll_name))
3213 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003214 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003215 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003216 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003217 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003218 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3219 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003220 else if (lp->ll_range)
3221 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003222 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003223
3224 /* Delete a range of List items. */
3225 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3226 {
3227 li = lp->ll_li->li_next;
3228 listitem_remove(lp->ll_list, lp->ll_li);
3229 lp->ll_li = li;
3230 ++lp->ll_n1;
3231 }
3232 }
3233 else
3234 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003235 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003236 /* unlet a List item. */
3237 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003238 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003239 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003240 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003241 }
3242
3243 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003244}
3245
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246/*
3247 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003248 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 */
3250 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003251do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003253 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254{
Bram Moolenaar33570922005-01-25 22:26:29 +00003255 hashtab_T *ht;
3256 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003257 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
Bram Moolenaar33570922005-01-25 22:26:29 +00003259 ht = find_var_ht(name, &varname);
3260 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003262 hi = hash_find(ht, varname);
3263 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003264 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003265 if (var_check_ro(HI2DI(hi)->di_flags, name))
3266 return FAIL;
3267 delete_var(ht, hi);
3268 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003271 if (forceit)
3272 return OK;
3273 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 return FAIL;
3275}
3276
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003277/*
3278 * Lock or unlock variable indicated by "lp".
3279 * "deep" is the levels to go (-1 for unlimited);
3280 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3281 */
3282 static int
3283do_lock_var(lp, name_end, deep, lock)
3284 lval_T *lp;
3285 char_u *name_end;
3286 int deep;
3287 int lock;
3288{
3289 int ret = OK;
3290 int cc;
3291 dictitem_T *di;
3292
3293 if (deep == 0) /* nothing to do */
3294 return OK;
3295
3296 if (lp->ll_tv == NULL)
3297 {
3298 cc = *name_end;
3299 *name_end = NUL;
3300
3301 /* Normal name or expanded name. */
3302 if (check_changedtick(lp->ll_name))
3303 ret = FAIL;
3304 else
3305 {
3306 di = find_var(lp->ll_name, NULL);
3307 if (di == NULL)
3308 ret = FAIL;
3309 else
3310 {
3311 if (lock)
3312 di->di_flags |= DI_FLAGS_LOCK;
3313 else
3314 di->di_flags &= ~DI_FLAGS_LOCK;
3315 item_lock(&di->di_tv, deep, lock);
3316 }
3317 }
3318 *name_end = cc;
3319 }
3320 else if (lp->ll_range)
3321 {
3322 listitem_T *li = lp->ll_li;
3323
3324 /* (un)lock a range of List items. */
3325 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3326 {
3327 item_lock(&li->li_tv, deep, lock);
3328 li = li->li_next;
3329 ++lp->ll_n1;
3330 }
3331 }
3332 else if (lp->ll_list != NULL)
3333 /* (un)lock a List item. */
3334 item_lock(&lp->ll_li->li_tv, deep, lock);
3335 else
3336 /* un(lock) a Dictionary item. */
3337 item_lock(&lp->ll_di->di_tv, deep, lock);
3338
3339 return ret;
3340}
3341
3342/*
3343 * Lock or unlock an item. "deep" is nr of levels to go.
3344 */
3345 static void
3346item_lock(tv, deep, lock)
3347 typval_T *tv;
3348 int deep;
3349 int lock;
3350{
3351 static int recurse = 0;
3352 list_T *l;
3353 listitem_T *li;
3354 dict_T *d;
3355 hashitem_T *hi;
3356 int todo;
3357
3358 if (recurse >= DICT_MAXNEST)
3359 {
3360 EMSG(_("E743: variable nested too deep for (un)lock"));
3361 return;
3362 }
3363 if (deep == 0)
3364 return;
3365 ++recurse;
3366
3367 /* lock/unlock the item itself */
3368 if (lock)
3369 tv->v_lock |= VAR_LOCKED;
3370 else
3371 tv->v_lock &= ~VAR_LOCKED;
3372
3373 switch (tv->v_type)
3374 {
3375 case VAR_LIST:
3376 if ((l = tv->vval.v_list) != NULL)
3377 {
3378 if (lock)
3379 l->lv_lock |= VAR_LOCKED;
3380 else
3381 l->lv_lock &= ~VAR_LOCKED;
3382 if (deep < 0 || deep > 1)
3383 /* recursive: lock/unlock the items the List contains */
3384 for (li = l->lv_first; li != NULL; li = li->li_next)
3385 item_lock(&li->li_tv, deep - 1, lock);
3386 }
3387 break;
3388 case VAR_DICT:
3389 if ((d = tv->vval.v_dict) != NULL)
3390 {
3391 if (lock)
3392 d->dv_lock |= VAR_LOCKED;
3393 else
3394 d->dv_lock &= ~VAR_LOCKED;
3395 if (deep < 0 || deep > 1)
3396 {
3397 /* recursive: lock/unlock the items the List contains */
3398 todo = d->dv_hashtab.ht_used;
3399 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3400 {
3401 if (!HASHITEM_EMPTY(hi))
3402 {
3403 --todo;
3404 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3405 }
3406 }
3407 }
3408 }
3409 }
3410 --recurse;
3411}
3412
Bram Moolenaara40058a2005-07-11 22:42:07 +00003413/*
3414 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3415 * it refers to a List or Dictionary that is locked.
3416 */
3417 static int
3418tv_islocked(tv)
3419 typval_T *tv;
3420{
3421 return (tv->v_lock & VAR_LOCKED)
3422 || (tv->v_type == VAR_LIST
3423 && tv->vval.v_list != NULL
3424 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3425 || (tv->v_type == VAR_DICT
3426 && tv->vval.v_dict != NULL
3427 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3428}
3429
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3431/*
3432 * Delete all "menutrans_" variables.
3433 */
3434 void
3435del_menutrans_vars()
3436{
Bram Moolenaar33570922005-01-25 22:26:29 +00003437 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003438 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
Bram Moolenaar33570922005-01-25 22:26:29 +00003440 hash_lock(&globvarht);
3441 todo = globvarht.ht_used;
3442 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003443 {
3444 if (!HASHITEM_EMPTY(hi))
3445 {
3446 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003447 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3448 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003449 }
3450 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003451 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452}
3453#endif
3454
3455#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3456
3457/*
3458 * Local string buffer for the next two functions to store a variable name
3459 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3460 * get_user_var_name().
3461 */
3462
3463static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3464
3465static char_u *varnamebuf = NULL;
3466static int varnamebuflen = 0;
3467
3468/*
3469 * Function to concatenate a prefix and a variable name.
3470 */
3471 static char_u *
3472cat_prefix_varname(prefix, name)
3473 int prefix;
3474 char_u *name;
3475{
3476 int len;
3477
3478 len = (int)STRLEN(name) + 3;
3479 if (len > varnamebuflen)
3480 {
3481 vim_free(varnamebuf);
3482 len += 10; /* some additional space */
3483 varnamebuf = alloc(len);
3484 if (varnamebuf == NULL)
3485 {
3486 varnamebuflen = 0;
3487 return NULL;
3488 }
3489 varnamebuflen = len;
3490 }
3491 *varnamebuf = prefix;
3492 varnamebuf[1] = ':';
3493 STRCPY(varnamebuf + 2, name);
3494 return varnamebuf;
3495}
3496
3497/*
3498 * Function given to ExpandGeneric() to obtain the list of user defined
3499 * (global/buffer/window/built-in) variable names.
3500 */
3501/*ARGSUSED*/
3502 char_u *
3503get_user_var_name(xp, idx)
3504 expand_T *xp;
3505 int idx;
3506{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003507 static long_u gdone;
3508 static long_u bdone;
3509 static long_u wdone;
3510 static int vidx;
3511 static hashitem_T *hi;
3512 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513
3514 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003515 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003516
3517 /* Global variables */
3518 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003520 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003521 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003522 else
3523 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003524 while (HASHITEM_EMPTY(hi))
3525 ++hi;
3526 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3527 return cat_prefix_varname('g', hi->hi_key);
3528 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003530
3531 /* b: variables */
3532 ht = &curbuf->b_vars.dv_hashtab;
3533 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003535 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003536 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003537 else
3538 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003539 while (HASHITEM_EMPTY(hi))
3540 ++hi;
3541 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003543 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003545 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 return (char_u *)"b:changedtick";
3547 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003548
3549 /* w: variables */
3550 ht = &curwin->w_vars.dv_hashtab;
3551 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003553 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003554 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003555 else
3556 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003557 while (HASHITEM_EMPTY(hi))
3558 ++hi;
3559 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003561
3562 /* v: variables */
3563 if (vidx < VV_LEN)
3564 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565
3566 vim_free(varnamebuf);
3567 varnamebuf = NULL;
3568 varnamebuflen = 0;
3569 return NULL;
3570}
3571
3572#endif /* FEAT_CMDL_COMPL */
3573
3574/*
3575 * types for expressions.
3576 */
3577typedef enum
3578{
3579 TYPE_UNKNOWN = 0
3580 , TYPE_EQUAL /* == */
3581 , TYPE_NEQUAL /* != */
3582 , TYPE_GREATER /* > */
3583 , TYPE_GEQUAL /* >= */
3584 , TYPE_SMALLER /* < */
3585 , TYPE_SEQUAL /* <= */
3586 , TYPE_MATCH /* =~ */
3587 , TYPE_NOMATCH /* !~ */
3588} exptype_T;
3589
3590/*
3591 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003592 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3594 */
3595
3596/*
3597 * Handle zero level expression.
3598 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003599 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 * Return OK or FAIL.
3601 */
3602 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003603eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 char_u **nextcmd;
3607 int evaluate;
3608{
3609 int ret;
3610 char_u *p;
3611
3612 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003613 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (ret == FAIL || !ends_excmd(*p))
3615 {
3616 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003617 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 /*
3619 * Report the invalid expression unless the expression evaluation has
3620 * been cancelled due to an aborting error, an interrupt, or an
3621 * exception.
3622 */
3623 if (!aborting())
3624 EMSG2(_(e_invexpr2), arg);
3625 ret = FAIL;
3626 }
3627 if (nextcmd != NULL)
3628 *nextcmd = check_nextcmd(p);
3629
3630 return ret;
3631}
3632
3633/*
3634 * Handle top level expression:
3635 * expr1 ? expr0 : expr0
3636 *
3637 * "arg" must point to the first non-white of the expression.
3638 * "arg" is advanced to the next non-white after the recognized expression.
3639 *
3640 * Return OK or FAIL.
3641 */
3642 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003643eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 int evaluate;
3647{
3648 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003649 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
3651 /*
3652 * Get the first variable.
3653 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003654 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 return FAIL;
3656
3657 if ((*arg)[0] == '?')
3658 {
3659 result = FALSE;
3660 if (evaluate)
3661 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003662 int error = FALSE;
3663
3664 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003666 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003667 if (error)
3668 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 }
3670
3671 /*
3672 * Get the second variable.
3673 */
3674 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003675 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 return FAIL;
3677
3678 /*
3679 * Check for the ":".
3680 */
3681 if ((*arg)[0] != ':')
3682 {
3683 EMSG(_("E109: Missing ':' after '?'"));
3684 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003685 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 return FAIL;
3687 }
3688
3689 /*
3690 * Get the third variable.
3691 */
3692 *arg = skipwhite(*arg + 1);
3693 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3694 {
3695 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003696 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 return FAIL;
3698 }
3699 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003700 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 }
3702
3703 return OK;
3704}
3705
3706/*
3707 * Handle first level expression:
3708 * expr2 || expr2 || expr2 logical OR
3709 *
3710 * "arg" must point to the first non-white of the expression.
3711 * "arg" is advanced to the next non-white after the recognized expression.
3712 *
3713 * Return OK or FAIL.
3714 */
3715 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003716eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 int evaluate;
3720{
Bram Moolenaar33570922005-01-25 22:26:29 +00003721 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 long result;
3723 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003724 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725
3726 /*
3727 * Get the first variable.
3728 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003729 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 return FAIL;
3731
3732 /*
3733 * Repeat until there is no following "||".
3734 */
3735 first = TRUE;
3736 result = FALSE;
3737 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3738 {
3739 if (evaluate && first)
3740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003741 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003743 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003744 if (error)
3745 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 first = FALSE;
3747 }
3748
3749 /*
3750 * Get the second variable.
3751 */
3752 *arg = skipwhite(*arg + 2);
3753 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3754 return FAIL;
3755
3756 /*
3757 * Compute the result.
3758 */
3759 if (evaluate && !result)
3760 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003761 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003763 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003764 if (error)
3765 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 }
3767 if (evaluate)
3768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003769 rettv->v_type = VAR_NUMBER;
3770 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
3772 }
3773
3774 return OK;
3775}
3776
3777/*
3778 * Handle second level expression:
3779 * expr3 && expr3 && expr3 logical AND
3780 *
3781 * "arg" must point to the first non-white of the expression.
3782 * "arg" is advanced to the next non-white after the recognized expression.
3783 *
3784 * Return OK or FAIL.
3785 */
3786 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003787eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 int evaluate;
3791{
Bram Moolenaar33570922005-01-25 22:26:29 +00003792 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 long result;
3794 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003795 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796
3797 /*
3798 * Get the first variable.
3799 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003800 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 return FAIL;
3802
3803 /*
3804 * Repeat until there is no following "&&".
3805 */
3806 first = TRUE;
3807 result = TRUE;
3808 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3809 {
3810 if (evaluate && first)
3811 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003812 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003814 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003815 if (error)
3816 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 first = FALSE;
3818 }
3819
3820 /*
3821 * Get the second variable.
3822 */
3823 *arg = skipwhite(*arg + 2);
3824 if (eval4(arg, &var2, evaluate && result) == FAIL)
3825 return FAIL;
3826
3827 /*
3828 * Compute the result.
3829 */
3830 if (evaluate && result)
3831 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003832 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003834 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003835 if (error)
3836 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 }
3838 if (evaluate)
3839 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003840 rettv->v_type = VAR_NUMBER;
3841 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
3843 }
3844
3845 return OK;
3846}
3847
3848/*
3849 * Handle third level expression:
3850 * var1 == var2
3851 * var1 =~ var2
3852 * var1 != var2
3853 * var1 !~ var2
3854 * var1 > var2
3855 * var1 >= var2
3856 * var1 < var2
3857 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003858 * var1 is var2
3859 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 *
3861 * "arg" must point to the first non-white of the expression.
3862 * "arg" is advanced to the next non-white after the recognized expression.
3863 *
3864 * Return OK or FAIL.
3865 */
3866 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003867eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 int evaluate;
3871{
Bram Moolenaar33570922005-01-25 22:26:29 +00003872 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 char_u *p;
3874 int i;
3875 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003876 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 int len = 2;
3878 long n1, n2;
3879 char_u *s1, *s2;
3880 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3881 regmatch_T regmatch;
3882 int ic;
3883 char_u *save_cpo;
3884
3885 /*
3886 * Get the first variable.
3887 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003888 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 return FAIL;
3890
3891 p = *arg;
3892 switch (p[0])
3893 {
3894 case '=': if (p[1] == '=')
3895 type = TYPE_EQUAL;
3896 else if (p[1] == '~')
3897 type = TYPE_MATCH;
3898 break;
3899 case '!': if (p[1] == '=')
3900 type = TYPE_NEQUAL;
3901 else if (p[1] == '~')
3902 type = TYPE_NOMATCH;
3903 break;
3904 case '>': if (p[1] != '=')
3905 {
3906 type = TYPE_GREATER;
3907 len = 1;
3908 }
3909 else
3910 type = TYPE_GEQUAL;
3911 break;
3912 case '<': if (p[1] != '=')
3913 {
3914 type = TYPE_SMALLER;
3915 len = 1;
3916 }
3917 else
3918 type = TYPE_SEQUAL;
3919 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003920 case 'i': if (p[1] == 's')
3921 {
3922 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3923 len = 5;
3924 if (!vim_isIDc(p[len]))
3925 {
3926 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3927 type_is = TRUE;
3928 }
3929 }
3930 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 }
3932
3933 /*
3934 * If there is a comparitive operator, use it.
3935 */
3936 if (type != TYPE_UNKNOWN)
3937 {
3938 /* extra question mark appended: ignore case */
3939 if (p[len] == '?')
3940 {
3941 ic = TRUE;
3942 ++len;
3943 }
3944 /* extra '#' appended: match case */
3945 else if (p[len] == '#')
3946 {
3947 ic = FALSE;
3948 ++len;
3949 }
3950 /* nothing appened: use 'ignorecase' */
3951 else
3952 ic = p_ic;
3953
3954 /*
3955 * Get the second variable.
3956 */
3957 *arg = skipwhite(p + len);
3958 if (eval5(arg, &var2, evaluate) == FAIL)
3959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003960 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 return FAIL;
3962 }
3963
3964 if (evaluate)
3965 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003966 if (type_is && rettv->v_type != var2.v_type)
3967 {
3968 /* For "is" a different type always means FALSE, for "notis"
3969 * it means TRUE. */
3970 n1 = (type == TYPE_NEQUAL);
3971 }
3972 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3973 {
3974 if (type_is)
3975 {
3976 n1 = (rettv->v_type == var2.v_type
3977 && rettv->vval.v_list == var2.vval.v_list);
3978 if (type == TYPE_NEQUAL)
3979 n1 = !n1;
3980 }
3981 else if (rettv->v_type != var2.v_type
3982 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3983 {
3984 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003985 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003986 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003987 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003988 clear_tv(rettv);
3989 clear_tv(&var2);
3990 return FAIL;
3991 }
3992 else
3993 {
3994 /* Compare two Lists for being equal or unequal. */
3995 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3996 if (type == TYPE_NEQUAL)
3997 n1 = !n1;
3998 }
3999 }
4000
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004001 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4002 {
4003 if (type_is)
4004 {
4005 n1 = (rettv->v_type == var2.v_type
4006 && rettv->vval.v_dict == var2.vval.v_dict);
4007 if (type == TYPE_NEQUAL)
4008 n1 = !n1;
4009 }
4010 else if (rettv->v_type != var2.v_type
4011 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4012 {
4013 if (rettv->v_type != var2.v_type)
4014 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4015 else
4016 EMSG(_("E736: Invalid operation for Dictionary"));
4017 clear_tv(rettv);
4018 clear_tv(&var2);
4019 return FAIL;
4020 }
4021 else
4022 {
4023 /* Compare two Dictionaries for being equal or unequal. */
4024 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4025 if (type == TYPE_NEQUAL)
4026 n1 = !n1;
4027 }
4028 }
4029
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004030 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4031 {
4032 if (rettv->v_type != var2.v_type
4033 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4034 {
4035 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004036 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004037 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004038 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004039 clear_tv(rettv);
4040 clear_tv(&var2);
4041 return FAIL;
4042 }
4043 else
4044 {
4045 /* Compare two Funcrefs for being equal or unequal. */
4046 if (rettv->vval.v_string == NULL
4047 || var2.vval.v_string == NULL)
4048 n1 = FALSE;
4049 else
4050 n1 = STRCMP(rettv->vval.v_string,
4051 var2.vval.v_string) == 0;
4052 if (type == TYPE_NEQUAL)
4053 n1 = !n1;
4054 }
4055 }
4056
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 /*
4058 * If one of the two variables is a number, compare as a number.
4059 * When using "=~" or "!~", always compare as string.
4060 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004061 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4063 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004064 n1 = get_tv_number(rettv);
4065 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 switch (type)
4067 {
4068 case TYPE_EQUAL: n1 = (n1 == n2); break;
4069 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4070 case TYPE_GREATER: n1 = (n1 > n2); break;
4071 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4072 case TYPE_SMALLER: n1 = (n1 < n2); break;
4073 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4074 case TYPE_UNKNOWN:
4075 case TYPE_MATCH:
4076 case TYPE_NOMATCH: break; /* avoid gcc warning */
4077 }
4078 }
4079 else
4080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081 s1 = get_tv_string_buf(rettv, buf1);
4082 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4084 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4085 else
4086 i = 0;
4087 n1 = FALSE;
4088 switch (type)
4089 {
4090 case TYPE_EQUAL: n1 = (i == 0); break;
4091 case TYPE_NEQUAL: n1 = (i != 0); break;
4092 case TYPE_GREATER: n1 = (i > 0); break;
4093 case TYPE_GEQUAL: n1 = (i >= 0); break;
4094 case TYPE_SMALLER: n1 = (i < 0); break;
4095 case TYPE_SEQUAL: n1 = (i <= 0); break;
4096
4097 case TYPE_MATCH:
4098 case TYPE_NOMATCH:
4099 /* avoid 'l' flag in 'cpoptions' */
4100 save_cpo = p_cpo;
4101 p_cpo = (char_u *)"";
4102 regmatch.regprog = vim_regcomp(s2,
4103 RE_MAGIC + RE_STRING);
4104 regmatch.rm_ic = ic;
4105 if (regmatch.regprog != NULL)
4106 {
4107 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4108 vim_free(regmatch.regprog);
4109 if (type == TYPE_NOMATCH)
4110 n1 = !n1;
4111 }
4112 p_cpo = save_cpo;
4113 break;
4114
4115 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4116 }
4117 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004118 clear_tv(rettv);
4119 clear_tv(&var2);
4120 rettv->v_type = VAR_NUMBER;
4121 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 }
4123 }
4124
4125 return OK;
4126}
4127
4128/*
4129 * Handle fourth level expression:
4130 * + number addition
4131 * - number subtraction
4132 * . string concatenation
4133 *
4134 * "arg" must point to the first non-white of the expression.
4135 * "arg" is advanced to the next non-white after the recognized expression.
4136 *
4137 * Return OK or FAIL.
4138 */
4139 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004142 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 int evaluate;
4144{
Bram Moolenaar33570922005-01-25 22:26:29 +00004145 typval_T var2;
4146 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 int op;
4148 long n1, n2;
4149 char_u *s1, *s2;
4150 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4151 char_u *p;
4152
4153 /*
4154 * Get the first variable.
4155 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004156 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 return FAIL;
4158
4159 /*
4160 * Repeat computing, until no '+', '-' or '.' is following.
4161 */
4162 for (;;)
4163 {
4164 op = **arg;
4165 if (op != '+' && op != '-' && op != '.')
4166 break;
4167
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168 if (op != '+' || rettv->v_type != VAR_LIST)
4169 {
4170 /* For "list + ...", an illegal use of the first operand as
4171 * a number cannot be determined before evaluating the 2nd
4172 * operand: if this is also a list, all is ok.
4173 * For "something . ...", "something - ..." or "non-list + ...",
4174 * we know that the first operand needs to be a string or number
4175 * without evaluating the 2nd operand. So check before to avoid
4176 * side effects after an error. */
4177 if (evaluate && get_tv_string_chk(rettv) == NULL)
4178 {
4179 clear_tv(rettv);
4180 return FAIL;
4181 }
4182 }
4183
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 /*
4185 * Get the second variable.
4186 */
4187 *arg = skipwhite(*arg + 1);
4188 if (eval6(arg, &var2, evaluate) == FAIL)
4189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 return FAIL;
4192 }
4193
4194 if (evaluate)
4195 {
4196 /*
4197 * Compute the result.
4198 */
4199 if (op == '.')
4200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004201 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4202 s2 = get_tv_string_buf_chk(&var2, buf2);
4203 if (s2 == NULL) /* type error ? */
4204 {
4205 clear_tv(rettv);
4206 clear_tv(&var2);
4207 return FAIL;
4208 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004209 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004210 clear_tv(rettv);
4211 rettv->v_type = VAR_STRING;
4212 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004214 else if (op == '+' && rettv->v_type == VAR_LIST
4215 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004216 {
4217 /* concatenate Lists */
4218 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4219 &var3) == FAIL)
4220 {
4221 clear_tv(rettv);
4222 clear_tv(&var2);
4223 return FAIL;
4224 }
4225 clear_tv(rettv);
4226 *rettv = var3;
4227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 else
4229 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004230 int error = FALSE;
4231
4232 n1 = get_tv_number_chk(rettv, &error);
4233 if (error)
4234 {
4235 /* This can only happen for "list + non-list".
4236 * For "non-list + ..." or "something - ...", we returned
4237 * before evaluating the 2nd operand. */
4238 clear_tv(rettv);
4239 return FAIL;
4240 }
4241 n2 = get_tv_number_chk(&var2, &error);
4242 if (error)
4243 {
4244 clear_tv(rettv);
4245 clear_tv(&var2);
4246 return FAIL;
4247 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004248 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 if (op == '+')
4250 n1 = n1 + n2;
4251 else
4252 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 rettv->v_type = VAR_NUMBER;
4254 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004256 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 }
4258 }
4259 return OK;
4260}
4261
4262/*
4263 * Handle fifth level expression:
4264 * * number multiplication
4265 * / number division
4266 * % number modulo
4267 *
4268 * "arg" must point to the first non-white of the expression.
4269 * "arg" is advanced to the next non-white after the recognized expression.
4270 *
4271 * Return OK or FAIL.
4272 */
4273 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004274eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004276 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 int evaluate;
4278{
Bram Moolenaar33570922005-01-25 22:26:29 +00004279 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 int op;
4281 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004282 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283
4284 /*
4285 * Get the first variable.
4286 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004287 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 return FAIL;
4289
4290 /*
4291 * Repeat computing, until no '*', '/' or '%' is following.
4292 */
4293 for (;;)
4294 {
4295 op = **arg;
4296 if (op != '*' && op != '/' && op != '%')
4297 break;
4298
4299 if (evaluate)
4300 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004301 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004302 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004303 if (error)
4304 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306 else
4307 n1 = 0;
4308
4309 /*
4310 * Get the second variable.
4311 */
4312 *arg = skipwhite(*arg + 1);
4313 if (eval7(arg, &var2, evaluate) == FAIL)
4314 return FAIL;
4315
4316 if (evaluate)
4317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004318 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004319 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004320 if (error)
4321 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
4323 /*
4324 * Compute the result.
4325 */
4326 if (op == '*')
4327 n1 = n1 * n2;
4328 else if (op == '/')
4329 {
4330 if (n2 == 0) /* give an error message? */
4331 n1 = 0x7fffffffL;
4332 else
4333 n1 = n1 / n2;
4334 }
4335 else
4336 {
4337 if (n2 == 0) /* give an error message? */
4338 n1 = 0;
4339 else
4340 n1 = n1 % n2;
4341 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342 rettv->v_type = VAR_NUMBER;
4343 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
4345 }
4346
4347 return OK;
4348}
4349
4350/*
4351 * Handle sixth level expression:
4352 * number number constant
4353 * "string" string contstant
4354 * 'string' literal string contstant
4355 * &option-name option value
4356 * @r register contents
4357 * identifier variable value
4358 * function() function call
4359 * $VAR environment variable
4360 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004361 * [expr, expr] List
4362 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 *
4364 * Also handle:
4365 * ! in front logical NOT
4366 * - in front unary minus
4367 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004368 * trailing [] subscript in String or List
4369 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 *
4371 * "arg" must point to the first non-white of the expression.
4372 * "arg" is advanced to the next non-white after the recognized expression.
4373 *
4374 * Return OK or FAIL.
4375 */
4376 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004377eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 int evaluate;
4381{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 long n;
4383 int len;
4384 char_u *s;
4385 int val;
4386 char_u *start_leader, *end_leader;
4387 int ret = OK;
4388 char_u *alias;
4389
4390 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004391 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004392 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004394 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395
4396 /*
4397 * Skip '!' and '-' characters. They are handled later.
4398 */
4399 start_leader = *arg;
4400 while (**arg == '!' || **arg == '-' || **arg == '+')
4401 *arg = skipwhite(*arg + 1);
4402 end_leader = *arg;
4403
4404 switch (**arg)
4405 {
4406 /*
4407 * Number constant.
4408 */
4409 case '0':
4410 case '1':
4411 case '2':
4412 case '3':
4413 case '4':
4414 case '5':
4415 case '6':
4416 case '7':
4417 case '8':
4418 case '9':
4419 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4420 *arg += len;
4421 if (evaluate)
4422 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004423 rettv->v_type = VAR_NUMBER;
4424 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 }
4426 break;
4427
4428 /*
4429 * String constant: "string".
4430 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004431 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 break;
4433
4434 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004435 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004437 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004438 break;
4439
4440 /*
4441 * List: [expr, expr]
4442 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004443 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 break;
4445
4446 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004447 * Dictionary: {key: val, key: val}
4448 */
4449 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4450 break;
4451
4452 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004453 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004455 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 break;
4457
4458 /*
4459 * Environment variable: $VAR.
4460 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004461 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 break;
4463
4464 /*
4465 * Register contents: @r.
4466 */
4467 case '@': ++*arg;
4468 if (evaluate)
4469 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004470 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004471 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473 if (**arg != NUL)
4474 ++*arg;
4475 break;
4476
4477 /*
4478 * nested expression: (expression).
4479 */
4480 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004481 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 if (**arg == ')')
4483 ++*arg;
4484 else if (ret == OK)
4485 {
4486 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004487 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 ret = FAIL;
4489 }
4490 break;
4491
Bram Moolenaar8c711452005-01-14 21:53:12 +00004492 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 break;
4494 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004495
4496 if (ret == NOTDONE)
4497 {
4498 /*
4499 * Must be a variable or function name.
4500 * Can also be a curly-braces kind of name: {expr}.
4501 */
4502 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004503 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004504 if (alias != NULL)
4505 s = alias;
4506
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004507 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004508 ret = FAIL;
4509 else
4510 {
4511 if (**arg == '(') /* recursive! */
4512 {
4513 /* If "s" is the name of a variable of type VAR_FUNC
4514 * use its contents. */
4515 s = deref_func_name(s, &len);
4516
4517 /* Invoke the function. */
4518 ret = get_func_tv(s, len, rettv, arg,
4519 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004520 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004521 /* Stop the expression evaluation when immediately
4522 * aborting on error, or when an interrupt occurred or
4523 * an exception was thrown but not caught. */
4524 if (aborting())
4525 {
4526 if (ret == OK)
4527 clear_tv(rettv);
4528 ret = FAIL;
4529 }
4530 }
4531 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004532 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004533 else
4534 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004535 }
4536
4537 if (alias != NULL)
4538 vim_free(alias);
4539 }
4540
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 *arg = skipwhite(*arg);
4542
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004543 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4544 * expr(expr). */
4545 if (ret == OK)
4546 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
4548 /*
4549 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4550 */
4551 if (ret == OK && evaluate && end_leader > start_leader)
4552 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004553 int error = FALSE;
4554
4555 val = get_tv_number_chk(rettv, &error);
4556 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004558 clear_tv(rettv);
4559 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004561 else
4562 {
4563 while (end_leader > start_leader)
4564 {
4565 --end_leader;
4566 if (*end_leader == '!')
4567 val = !val;
4568 else if (*end_leader == '-')
4569 val = -val;
4570 }
4571 clear_tv(rettv);
4572 rettv->v_type = VAR_NUMBER;
4573 rettv->vval.v_number = val;
4574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 }
4576
4577 return ret;
4578}
4579
4580/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004581 * Evaluate an "[expr]" or "[expr:expr]" index.
4582 * "*arg" points to the '['.
4583 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4584 */
4585 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004586eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004587 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004588 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004589 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004590 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004591{
4592 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004593 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004594 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004595 long len = -1;
4596 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004597 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004598 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004599
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004600 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004601 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004602 if (verbose)
4603 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004604 return FAIL;
4605 }
4606
Bram Moolenaar8c711452005-01-14 21:53:12 +00004607 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004608 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004609 /*
4610 * dict.name
4611 */
4612 key = *arg + 1;
4613 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4614 ;
4615 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004616 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004617 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004618 }
4619 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004620 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004621 /*
4622 * something[idx]
4623 *
4624 * Get the (first) variable from inside the [].
4625 */
4626 *arg = skipwhite(*arg + 1);
4627 if (**arg == ':')
4628 empty1 = TRUE;
4629 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4630 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004631 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4632 {
4633 /* not a number or string */
4634 clear_tv(&var1);
4635 return FAIL;
4636 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004637
4638 /*
4639 * Get the second variable from inside the [:].
4640 */
4641 if (**arg == ':')
4642 {
4643 range = TRUE;
4644 *arg = skipwhite(*arg + 1);
4645 if (**arg == ']')
4646 empty2 = TRUE;
4647 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4648 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004649 if (!empty1)
4650 clear_tv(&var1);
4651 return FAIL;
4652 }
4653 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4654 {
4655 /* not a number or string */
4656 if (!empty1)
4657 clear_tv(&var1);
4658 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004659 return FAIL;
4660 }
4661 }
4662
4663 /* Check for the ']'. */
4664 if (**arg != ']')
4665 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004666 if (verbose)
4667 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004668 clear_tv(&var1);
4669 if (range)
4670 clear_tv(&var2);
4671 return FAIL;
4672 }
4673 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004674 }
4675
4676 if (evaluate)
4677 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004678 n1 = 0;
4679 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004681 n1 = get_tv_number(&var1);
4682 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004683 }
4684 if (range)
4685 {
4686 if (empty2)
4687 n2 = -1;
4688 else
4689 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004690 n2 = get_tv_number(&var2);
4691 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004692 }
4693 }
4694
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004695 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004696 {
4697 case VAR_NUMBER:
4698 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004699 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004700 len = (long)STRLEN(s);
4701 if (range)
4702 {
4703 /* The resulting variable is a substring. If the indexes
4704 * are out of range the result is empty. */
4705 if (n1 < 0)
4706 {
4707 n1 = len + n1;
4708 if (n1 < 0)
4709 n1 = 0;
4710 }
4711 if (n2 < 0)
4712 n2 = len + n2;
4713 else if (n2 >= len)
4714 n2 = len;
4715 if (n1 >= len || n2 < 0 || n1 > n2)
4716 s = NULL;
4717 else
4718 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4719 }
4720 else
4721 {
4722 /* The resulting variable is a string of a single
4723 * character. If the index is too big or negative the
4724 * result is empty. */
4725 if (n1 >= len || n1 < 0)
4726 s = NULL;
4727 else
4728 s = vim_strnsave(s + n1, 1);
4729 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004730 clear_tv(rettv);
4731 rettv->v_type = VAR_STRING;
4732 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004733 break;
4734
4735 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004736 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004737 if (n1 < 0)
4738 n1 = len + n1;
4739 if (!empty1 && (n1 < 0 || n1 >= len))
4740 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004741 if (verbose)
4742 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004743 return FAIL;
4744 }
4745 if (range)
4746 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004747 list_T *l;
4748 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749
4750 if (n2 < 0)
4751 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004752 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004753 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004754 if (verbose)
4755 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004756 return FAIL;
4757 }
4758 l = list_alloc();
4759 if (l == NULL)
4760 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004761 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004762 n1 <= n2; ++n1)
4763 {
4764 if (list_append_tv(l, &item->li_tv) == FAIL)
4765 {
4766 list_free(l);
4767 return FAIL;
4768 }
4769 item = item->li_next;
4770 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004771 clear_tv(rettv);
4772 rettv->v_type = VAR_LIST;
4773 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004774 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004775 }
4776 else
4777 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004778 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004779 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004780 clear_tv(rettv);
4781 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004782 }
4783 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004784
4785 case VAR_DICT:
4786 if (range)
4787 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004788 if (verbose)
4789 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004790 if (len == -1)
4791 clear_tv(&var1);
4792 return FAIL;
4793 }
4794 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004795 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004796
4797 if (len == -1)
4798 {
4799 key = get_tv_string(&var1);
4800 if (*key == NUL)
4801 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004802 if (verbose)
4803 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004804 clear_tv(&var1);
4805 return FAIL;
4806 }
4807 }
4808
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004809 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004810
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004811 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004812 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004813 if (len == -1)
4814 clear_tv(&var1);
4815 if (item == NULL)
4816 return FAIL;
4817
4818 copy_tv(&item->di_tv, &var1);
4819 clear_tv(rettv);
4820 *rettv = var1;
4821 }
4822 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004823 }
4824 }
4825
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004826 return OK;
4827}
4828
4829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 * Get an option value.
4831 * "arg" points to the '&' or '+' before the option name.
4832 * "arg" is advanced to character after the option name.
4833 * Return OK or FAIL.
4834 */
4835 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004836get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004838 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 int evaluate;
4840{
4841 char_u *option_end;
4842 long numval;
4843 char_u *stringval;
4844 int opt_type;
4845 int c;
4846 int working = (**arg == '+'); /* has("+option") */
4847 int ret = OK;
4848 int opt_flags;
4849
4850 /*
4851 * Isolate the option name and find its value.
4852 */
4853 option_end = find_option_end(arg, &opt_flags);
4854 if (option_end == NULL)
4855 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004856 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 EMSG2(_("E112: Option name missing: %s"), *arg);
4858 return FAIL;
4859 }
4860
4861 if (!evaluate)
4862 {
4863 *arg = option_end;
4864 return OK;
4865 }
4866
4867 c = *option_end;
4868 *option_end = NUL;
4869 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004870 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
4872 if (opt_type == -3) /* invalid name */
4873 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004874 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 EMSG2(_("E113: Unknown option: %s"), *arg);
4876 ret = FAIL;
4877 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004878 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
4880 if (opt_type == -2) /* hidden string option */
4881 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004882 rettv->v_type = VAR_STRING;
4883 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 }
4885 else if (opt_type == -1) /* hidden number option */
4886 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004887 rettv->v_type = VAR_NUMBER;
4888 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 }
4890 else if (opt_type == 1) /* number option */
4891 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004892 rettv->v_type = VAR_NUMBER;
4893 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 }
4895 else /* string option */
4896 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004897 rettv->v_type = VAR_STRING;
4898 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 }
4900 }
4901 else if (working && (opt_type == -2 || opt_type == -1))
4902 ret = FAIL;
4903
4904 *option_end = c; /* put back for error messages */
4905 *arg = option_end;
4906
4907 return ret;
4908}
4909
4910/*
4911 * Allocate a variable for a string constant.
4912 * Return OK or FAIL.
4913 */
4914 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004915get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 int evaluate;
4919{
4920 char_u *p;
4921 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 int extra = 0;
4923
4924 /*
4925 * Find the end of the string, skipping backslashed characters.
4926 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004927 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 {
4929 if (*p == '\\' && p[1] != NUL)
4930 {
4931 ++p;
4932 /* A "\<x>" form occupies at least 4 characters, and produces up
4933 * to 6 characters: reserve space for 2 extra */
4934 if (*p == '<')
4935 extra += 2;
4936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
4938
4939 if (*p != '"')
4940 {
4941 EMSG2(_("E114: Missing quote: %s"), *arg);
4942 return FAIL;
4943 }
4944
4945 /* If only parsing, set *arg and return here */
4946 if (!evaluate)
4947 {
4948 *arg = p + 1;
4949 return OK;
4950 }
4951
4952 /*
4953 * Copy the string into allocated memory, handling backslashed
4954 * characters.
4955 */
4956 name = alloc((unsigned)(p - *arg + extra));
4957 if (name == NULL)
4958 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004959 rettv->v_type = VAR_STRING;
4960 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961
Bram Moolenaar8c711452005-01-14 21:53:12 +00004962 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 {
4964 if (*p == '\\')
4965 {
4966 switch (*++p)
4967 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004968 case 'b': *name++ = BS; ++p; break;
4969 case 'e': *name++ = ESC; ++p; break;
4970 case 'f': *name++ = FF; ++p; break;
4971 case 'n': *name++ = NL; ++p; break;
4972 case 'r': *name++ = CAR; ++p; break;
4973 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974
4975 case 'X': /* hex: "\x1", "\x12" */
4976 case 'x':
4977 case 'u': /* Unicode: "\u0023" */
4978 case 'U':
4979 if (vim_isxdigit(p[1]))
4980 {
4981 int n, nr;
4982 int c = toupper(*p);
4983
4984 if (c == 'X')
4985 n = 2;
4986 else
4987 n = 4;
4988 nr = 0;
4989 while (--n >= 0 && vim_isxdigit(p[1]))
4990 {
4991 ++p;
4992 nr = (nr << 4) + hex2nr(*p);
4993 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004994 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995#ifdef FEAT_MBYTE
4996 /* For "\u" store the number according to
4997 * 'encoding'. */
4998 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004999 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 else
5001#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005002 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 break;
5005
5006 /* octal: "\1", "\12", "\123" */
5007 case '0':
5008 case '1':
5009 case '2':
5010 case '3':
5011 case '4':
5012 case '5':
5013 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005014 case '7': *name = *p++ - '0';
5015 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005017 *name = (*name << 3) + *p++ - '0';
5018 if (*p >= '0' && *p <= '7')
5019 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005021 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 break;
5023
5024 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005025 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 if (extra != 0)
5027 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005028 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 break;
5030 }
5031 /* FALLTHROUGH */
5032
Bram Moolenaar8c711452005-01-14 21:53:12 +00005033 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 break;
5035 }
5036 }
5037 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005041 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 *arg = p + 1;
5043
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 return OK;
5045}
5046
5047/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 * Return OK or FAIL.
5050 */
5051 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005052get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 int evaluate;
5056{
5057 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005058 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005059 int reduce = 0;
5060
5061 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005062 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005063 */
5064 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5065 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005066 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005067 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005068 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005069 break;
5070 ++reduce;
5071 ++p;
5072 }
5073 }
5074
Bram Moolenaar8c711452005-01-14 21:53:12 +00005075 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005076 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005078 return FAIL;
5079 }
5080
Bram Moolenaar8c711452005-01-14 21:53:12 +00005081 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005082 if (!evaluate)
5083 {
5084 *arg = p + 1;
5085 return OK;
5086 }
5087
5088 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005089 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005090 */
5091 str = alloc((unsigned)((p - *arg) - reduce));
5092 if (str == NULL)
5093 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 rettv->v_type = VAR_STRING;
5095 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005096
Bram Moolenaar8c711452005-01-14 21:53:12 +00005097 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005098 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005099 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005100 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005101 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005102 break;
5103 ++p;
5104 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005105 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005106 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005107 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005108 *arg = p + 1;
5109
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005110 return OK;
5111}
5112
5113/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005114 * Allocate a variable for a List and fill it from "*arg".
5115 * Return OK or FAIL.
5116 */
5117 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005118get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005119 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005120 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005121 int evaluate;
5122{
Bram Moolenaar33570922005-01-25 22:26:29 +00005123 list_T *l = NULL;
5124 typval_T tv;
5125 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005126
5127 if (evaluate)
5128 {
5129 l = list_alloc();
5130 if (l == NULL)
5131 return FAIL;
5132 }
5133
5134 *arg = skipwhite(*arg + 1);
5135 while (**arg != ']' && **arg != NUL)
5136 {
5137 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5138 goto failret;
5139 if (evaluate)
5140 {
5141 item = listitem_alloc();
5142 if (item != NULL)
5143 {
5144 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005145 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005146 list_append(l, item);
5147 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005148 else
5149 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005150 }
5151
5152 if (**arg == ']')
5153 break;
5154 if (**arg != ',')
5155 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005156 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005157 goto failret;
5158 }
5159 *arg = skipwhite(*arg + 1);
5160 }
5161
5162 if (**arg != ']')
5163 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005164 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165failret:
5166 if (evaluate)
5167 list_free(l);
5168 return FAIL;
5169 }
5170
5171 *arg = skipwhite(*arg + 1);
5172 if (evaluate)
5173 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005174 rettv->v_type = VAR_LIST;
5175 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005176 ++l->lv_refcount;
5177 }
5178
5179 return OK;
5180}
5181
5182/*
5183 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005184 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005185 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005186 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005187list_alloc()
5188{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005189 list_T *l;
5190
5191 l = (list_T *)alloc_clear(sizeof(list_T));
5192 if (l != NULL)
5193 {
5194 /* Prepend the list to the list of lists for garbage collection. */
5195 if (first_list != NULL)
5196 first_list->lv_used_prev = l;
5197 l->lv_used_prev = NULL;
5198 l->lv_used_next = first_list;
5199 first_list = l;
5200 }
5201 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005202}
5203
5204/*
5205 * Unreference a list: decrement the reference count and free it when it
5206 * becomes zero.
5207 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005208 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005209list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005210 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005211{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005212 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5213 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005214}
5215
5216/*
5217 * Free a list, including all items it points to.
5218 * Ignores the reference count.
5219 */
5220 static void
5221list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005222 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005223{
Bram Moolenaar33570922005-01-25 22:26:29 +00005224 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005225
Bram Moolenaard9fba312005-06-26 22:34:35 +00005226 /* Avoid that recursive reference to the list frees us again. */
5227 l->lv_refcount = DEL_REFCOUNT;
5228
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005229 /* Remove the list from the list of lists for garbage collection. */
5230 if (l->lv_used_prev == NULL)
5231 first_list = l->lv_used_next;
5232 else
5233 l->lv_used_prev->lv_used_next = l->lv_used_next;
5234 if (l->lv_used_next != NULL)
5235 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5236
Bram Moolenaard9fba312005-06-26 22:34:35 +00005237 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005238 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005239 /* Remove the item before deleting it. */
5240 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005241 listitem_free(item);
5242 }
5243 vim_free(l);
5244}
5245
5246/*
5247 * Allocate a list item.
5248 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005249 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250listitem_alloc()
5251{
Bram Moolenaar33570922005-01-25 22:26:29 +00005252 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253}
5254
5255/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005256 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 */
5258 static void
5259listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005260 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005262 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 vim_free(item);
5264}
5265
5266/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005267 * Remove a list item from a List and free it. Also clears the value.
5268 */
5269 static void
5270listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005271 list_T *l;
5272 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005273{
5274 list_remove(l, item, item);
5275 listitem_free(item);
5276}
5277
5278/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 * Get the number of items in a list.
5280 */
5281 static long
5282list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005283 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005285 if (l == NULL)
5286 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005287 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288}
5289
5290/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005291 * Return TRUE when two lists have exactly the same values.
5292 */
5293 static int
5294list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005295 list_T *l1;
5296 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005297 int ic; /* ignore case for strings */
5298{
Bram Moolenaar33570922005-01-25 22:26:29 +00005299 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005300
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005301 if (list_len(l1) != list_len(l2))
5302 return FALSE;
5303
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005304 for (item1 = l1->lv_first, item2 = l2->lv_first;
5305 item1 != NULL && item2 != NULL;
5306 item1 = item1->li_next, item2 = item2->li_next)
5307 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5308 return FALSE;
5309 return item1 == NULL && item2 == NULL;
5310}
5311
5312/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005313 * Return TRUE when two dictionaries have exactly the same key/values.
5314 */
5315 static int
5316dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005317 dict_T *d1;
5318 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005319 int ic; /* ignore case for strings */
5320{
Bram Moolenaar33570922005-01-25 22:26:29 +00005321 hashitem_T *hi;
5322 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005323 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005324
5325 if (dict_len(d1) != dict_len(d2))
5326 return FALSE;
5327
Bram Moolenaar33570922005-01-25 22:26:29 +00005328 todo = d1->dv_hashtab.ht_used;
5329 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005330 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005331 if (!HASHITEM_EMPTY(hi))
5332 {
5333 item2 = dict_find(d2, hi->hi_key, -1);
5334 if (item2 == NULL)
5335 return FALSE;
5336 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5337 return FALSE;
5338 --todo;
5339 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005340 }
5341 return TRUE;
5342}
5343
5344/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005345 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005346 * Compares the items just like "==" would compare them, but strings and
5347 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005348 */
5349 static int
5350tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005351 typval_T *tv1;
5352 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005353 int ic; /* ignore case */
5354{
5355 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005356 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005357
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005358 if (tv1->v_type != tv2->v_type)
5359 return FALSE;
5360
5361 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005362 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005363 case VAR_LIST:
5364 /* recursive! */
5365 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5366
5367 case VAR_DICT:
5368 /* recursive! */
5369 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5370
5371 case VAR_FUNC:
5372 return (tv1->vval.v_string != NULL
5373 && tv2->vval.v_string != NULL
5374 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5375
5376 case VAR_NUMBER:
5377 return tv1->vval.v_number == tv2->vval.v_number;
5378
5379 case VAR_STRING:
5380 s1 = get_tv_string_buf(tv1, buf1);
5381 s2 = get_tv_string_buf(tv2, buf2);
5382 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005383 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005384
5385 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005386 return TRUE;
5387}
5388
5389/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005390 * Locate item with index "n" in list "l" and return it.
5391 * A negative index is counted from the end; -1 is the last item.
5392 * Returns NULL when "n" is out of range.
5393 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005394 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005396 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397 long n;
5398{
Bram Moolenaar33570922005-01-25 22:26:29 +00005399 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 long idx;
5401
5402 if (l == NULL)
5403 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005404
5405 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005407 n = l->lv_len + n;
5408
5409 /* Check for index out of range. */
5410 if (n < 0 || n >= l->lv_len)
5411 return NULL;
5412
5413 /* When there is a cached index may start search from there. */
5414 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005416 if (n < l->lv_idx / 2)
5417 {
5418 /* closest to the start of the list */
5419 item = l->lv_first;
5420 idx = 0;
5421 }
5422 else if (n > (l->lv_idx + l->lv_len) / 2)
5423 {
5424 /* closest to the end of the list */
5425 item = l->lv_last;
5426 idx = l->lv_len - 1;
5427 }
5428 else
5429 {
5430 /* closest to the cached index */
5431 item = l->lv_idx_item;
5432 idx = l->lv_idx;
5433 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 }
5435 else
5436 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005437 if (n < l->lv_len / 2)
5438 {
5439 /* closest to the start of the list */
5440 item = l->lv_first;
5441 idx = 0;
5442 }
5443 else
5444 {
5445 /* closest to the end of the list */
5446 item = l->lv_last;
5447 idx = l->lv_len - 1;
5448 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005450
5451 while (n > idx)
5452 {
5453 /* search forward */
5454 item = item->li_next;
5455 ++idx;
5456 }
5457 while (n < idx)
5458 {
5459 /* search backward */
5460 item = item->li_prev;
5461 --idx;
5462 }
5463
5464 /* cache the used index */
5465 l->lv_idx = idx;
5466 l->lv_idx_item = item;
5467
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 return item;
5469}
5470
5471/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005472 * Locate "item" list "l" and return its index.
5473 * Returns -1 when "item" is not in the list.
5474 */
5475 static long
5476list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005477 list_T *l;
5478 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005479{
5480 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005481 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005482
5483 if (l == NULL)
5484 return -1;
5485 idx = 0;
5486 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5487 ++idx;
5488 if (li == NULL)
5489 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005490 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005491}
5492
5493/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 * Append item "item" to the end of list "l".
5495 */
5496 static void
5497list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005498 list_T *l;
5499 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005500{
5501 if (l->lv_last == NULL)
5502 {
5503 /* empty list */
5504 l->lv_first = item;
5505 l->lv_last = item;
5506 item->li_prev = NULL;
5507 }
5508 else
5509 {
5510 l->lv_last->li_next = item;
5511 item->li_prev = l->lv_last;
5512 l->lv_last = item;
5513 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005514 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005515 item->li_next = NULL;
5516}
5517
5518/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005519 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005520 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005521 */
5522 static int
5523list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005524 list_T *l;
5525 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005526{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005527 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005528
Bram Moolenaar05159a02005-02-26 23:04:13 +00005529 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005531 copy_tv(tv, &li->li_tv);
5532 list_append(l, li);
5533 return OK;
5534}
5535
5536/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005537 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005538 * Return FAIL when out of memory.
5539 */
5540 int
5541list_append_dict(list, dict)
5542 list_T *list;
5543 dict_T *dict;
5544{
5545 listitem_T *li = listitem_alloc();
5546
5547 if (li == NULL)
5548 return FAIL;
5549 li->li_tv.v_type = VAR_DICT;
5550 li->li_tv.v_lock = 0;
5551 li->li_tv.vval.v_dict = dict;
5552 list_append(list, li);
5553 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554 return OK;
5555}
5556
5557/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005558 * Make a copy of "str" and append it as an item to list "l".
5559 * Returns FAIL when out of memory.
5560 */
5561 static int
5562list_append_string(l, str)
5563 list_T *l;
5564 char_u *str;
5565{
5566 listitem_T *li = listitem_alloc();
5567
5568 if (li == NULL)
5569 return FAIL;
5570 list_append(l, li);
5571 li->li_tv.v_type = VAR_STRING;
5572 li->li_tv.v_lock = 0;
5573 if ((li->li_tv.vval.v_string = vim_strsave(str)) == NULL)
5574 return FAIL;
5575 return OK;
5576}
5577
5578/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005579 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005580 * If "item" is NULL append at the end.
5581 * Return FAIL when out of memory.
5582 */
5583 static int
5584list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005585 list_T *l;
5586 typval_T *tv;
5587 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005588{
Bram Moolenaar33570922005-01-25 22:26:29 +00005589 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005590
5591 if (ni == NULL)
5592 return FAIL;
5593 copy_tv(tv, &ni->li_tv);
5594 if (item == NULL)
5595 /* Append new item at end of list. */
5596 list_append(l, ni);
5597 else
5598 {
5599 /* Insert new item before existing item. */
5600 ni->li_prev = item->li_prev;
5601 ni->li_next = item;
5602 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005603 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005604 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005605 ++l->lv_idx;
5606 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005607 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005608 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005609 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005610 l->lv_idx_item = NULL;
5611 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005612 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005613 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005614 }
5615 return OK;
5616}
5617
5618/*
5619 * Extend "l1" with "l2".
5620 * If "bef" is NULL append at the end, otherwise insert before this item.
5621 * Returns FAIL when out of memory.
5622 */
5623 static int
5624list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005625 list_T *l1;
5626 list_T *l2;
5627 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005628{
Bram Moolenaar33570922005-01-25 22:26:29 +00005629 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005630
5631 for (item = l2->lv_first; item != NULL; item = item->li_next)
5632 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5633 return FAIL;
5634 return OK;
5635}
5636
5637/*
5638 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5639 * Return FAIL when out of memory.
5640 */
5641 static int
5642list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005643 list_T *l1;
5644 list_T *l2;
5645 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005646{
Bram Moolenaar33570922005-01-25 22:26:29 +00005647 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005648
5649 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005650 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005651 if (l == NULL)
5652 return FAIL;
5653 tv->v_type = VAR_LIST;
5654 tv->vval.v_list = l;
5655
5656 /* append all items from the second list */
5657 return list_extend(l, l2, NULL);
5658}
5659
5660/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005661 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005662 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005663 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005664 * Returns NULL when out of memory.
5665 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005666 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005667list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005668 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005669 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005670 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005671{
Bram Moolenaar33570922005-01-25 22:26:29 +00005672 list_T *copy;
5673 listitem_T *item;
5674 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005675
5676 if (orig == NULL)
5677 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005678
5679 copy = list_alloc();
5680 if (copy != NULL)
5681 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005682 if (copyID != 0)
5683 {
5684 /* Do this before adding the items, because one of the items may
5685 * refer back to this list. */
5686 orig->lv_copyID = copyID;
5687 orig->lv_copylist = copy;
5688 }
5689 for (item = orig->lv_first; item != NULL && !got_int;
5690 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005691 {
5692 ni = listitem_alloc();
5693 if (ni == NULL)
5694 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005695 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005696 {
5697 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5698 {
5699 vim_free(ni);
5700 break;
5701 }
5702 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005703 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005704 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005705 list_append(copy, ni);
5706 }
5707 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005708 if (item != NULL)
5709 {
5710 list_unref(copy);
5711 copy = NULL;
5712 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005713 }
5714
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005715 return copy;
5716}
5717
5718/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005719 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005720 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005721 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005722 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005723list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005724 list_T *l;
5725 listitem_T *item;
5726 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005727{
Bram Moolenaar33570922005-01-25 22:26:29 +00005728 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005729
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005730 /* notify watchers */
5731 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005732 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005733 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005734 list_fix_watch(l, ip);
5735 if (ip == item2)
5736 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005737 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005738
5739 if (item2->li_next == NULL)
5740 l->lv_last = item->li_prev;
5741 else
5742 item2->li_next->li_prev = item->li_prev;
5743 if (item->li_prev == NULL)
5744 l->lv_first = item2->li_next;
5745 else
5746 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005747 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005748}
5749
5750/*
5751 * Return an allocated string with the string representation of a list.
5752 * May return NULL.
5753 */
5754 static char_u *
5755list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005756 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005757{
5758 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005759
5760 if (tv->vval.v_list == NULL)
5761 return NULL;
5762 ga_init2(&ga, (int)sizeof(char), 80);
5763 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005764 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5765 {
5766 vim_free(ga.ga_data);
5767 return NULL;
5768 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005769 ga_append(&ga, ']');
5770 ga_append(&ga, NUL);
5771 return (char_u *)ga.ga_data;
5772}
5773
5774/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005775 * Join list "l" into a string in "*gap", using separator "sep".
5776 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005777 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005778 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005779 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005780list_join(gap, l, sep, echo)
5781 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005782 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005783 char_u *sep;
5784 int echo;
5785{
5786 int first = TRUE;
5787 char_u *tofree;
5788 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005789 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005790 char_u *s;
5791
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005792 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005793 {
5794 if (first)
5795 first = FALSE;
5796 else
5797 ga_concat(gap, sep);
5798
5799 if (echo)
5800 s = echo_string(&item->li_tv, &tofree, numbuf);
5801 else
5802 s = tv2string(&item->li_tv, &tofree, numbuf);
5803 if (s != NULL)
5804 ga_concat(gap, s);
5805 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005806 if (s == NULL)
5807 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005808 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005809 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005810}
5811
5812/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005813 * Garbage collection for lists and dictionaries.
5814 *
5815 * We use reference counts to be able to free most items right away when they
5816 * are no longer used. But for composite items it's possible that it becomes
5817 * unused while the reference count is > 0: When there is a recursive
5818 * reference. Example:
5819 * :let l = [1, 2, 3]
5820 * :let d = {9: l}
5821 * :let l[1] = d
5822 *
5823 * Since this is quite unusual we handle this with garbage collection: every
5824 * once in a while find out which lists and dicts are not referenced from any
5825 * variable.
5826 *
5827 * Here is a good reference text about garbage collection (refers to Python
5828 * but it applies to all reference-counting mechanisms):
5829 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005830 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005831
5832/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005833 * Do garbage collection for lists and dicts.
5834 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005835 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005836 int
5837garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005838{
5839 dict_T *dd;
5840 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005841 int copyID = ++current_copyID;
5842 buf_T *buf;
5843 win_T *wp;
5844 int i;
5845 funccall_T *fc;
5846 int did_free = FALSE;
5847
5848 /*
5849 * 1. Go through all accessible variables and mark all lists and dicts
5850 * with copyID.
5851 */
5852 /* script-local variables */
5853 for (i = 1; i <= ga_scripts.ga_len; ++i)
5854 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5855
5856 /* buffer-local variables */
5857 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5858 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5859
5860 /* window-local variables */
5861 FOR_ALL_WINDOWS(wp)
5862 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5863
5864 /* global variables */
5865 set_ref_in_ht(&globvarht, copyID);
5866
5867 /* function-local variables */
5868 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5869 {
5870 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5871 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5872 }
5873
5874 /*
5875 * 2. Go through the list of dicts and free items without the copyID.
5876 */
5877 for (dd = first_dict; dd != NULL; )
5878 if (dd->dv_copyID != copyID)
5879 {
5880 dict_free(dd);
5881 did_free = TRUE;
5882
5883 /* restart, next dict may also have been freed */
5884 dd = first_dict;
5885 }
5886 else
5887 dd = dd->dv_used_next;
5888
5889 /*
5890 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005891 * But don't free a list that has a watcher (used in a for loop), these
5892 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005893 */
5894 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005895 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005896 {
5897 list_free(ll);
5898 did_free = TRUE;
5899
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005900 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005901 ll = first_list;
5902 }
5903 else
5904 ll = ll->lv_used_next;
5905
5906 return did_free;
5907}
5908
5909/*
5910 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5911 */
5912 static void
5913set_ref_in_ht(ht, copyID)
5914 hashtab_T *ht;
5915 int copyID;
5916{
5917 int todo;
5918 hashitem_T *hi;
5919
5920 todo = ht->ht_used;
5921 for (hi = ht->ht_array; todo > 0; ++hi)
5922 if (!HASHITEM_EMPTY(hi))
5923 {
5924 --todo;
5925 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5926 }
5927}
5928
5929/*
5930 * Mark all lists and dicts referenced through list "l" with "copyID".
5931 */
5932 static void
5933set_ref_in_list(l, copyID)
5934 list_T *l;
5935 int copyID;
5936{
5937 listitem_T *li;
5938
5939 for (li = l->lv_first; li != NULL; li = li->li_next)
5940 set_ref_in_item(&li->li_tv, copyID);
5941}
5942
5943/*
5944 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5945 */
5946 static void
5947set_ref_in_item(tv, copyID)
5948 typval_T *tv;
5949 int copyID;
5950{
5951 dict_T *dd;
5952 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005953
5954 switch (tv->v_type)
5955 {
5956 case VAR_DICT:
5957 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005958 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005959 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005960 /* Didn't see this dict yet. */
5961 dd->dv_copyID = copyID;
5962 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005963 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005964 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005965
5966 case VAR_LIST:
5967 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005968 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005969 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005970 /* Didn't see this list yet. */
5971 ll->lv_copyID = copyID;
5972 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005973 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005974 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005975 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005976 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005977}
5978
5979/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005980 * Allocate an empty header for a dictionary.
5981 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005982 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005983dict_alloc()
5984{
Bram Moolenaar33570922005-01-25 22:26:29 +00005985 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005986
Bram Moolenaar33570922005-01-25 22:26:29 +00005987 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005988 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005989 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005990 /* Add the list to the hashtable for garbage collection. */
5991 if (first_dict != NULL)
5992 first_dict->dv_used_prev = d;
5993 d->dv_used_next = first_dict;
5994 d->dv_used_prev = NULL;
5995
Bram Moolenaar33570922005-01-25 22:26:29 +00005996 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005997 d->dv_lock = 0;
5998 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005999 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006000 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006001 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006002}
6003
6004/*
6005 * Unreference a Dictionary: decrement the reference count and free it when it
6006 * becomes zero.
6007 */
6008 static void
6009dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006010 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006011{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006012 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6013 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006014}
6015
6016/*
6017 * Free a Dictionary, including all items it contains.
6018 * Ignores the reference count.
6019 */
6020 static void
6021dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006022 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006023{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006024 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006025 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006026 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006027
Bram Moolenaard9fba312005-06-26 22:34:35 +00006028 /* Avoid that recursive reference to the dict frees us again. */
6029 d->dv_refcount = DEL_REFCOUNT;
6030
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006031 /* Remove the dict from the list of dicts for garbage collection. */
6032 if (d->dv_used_prev == NULL)
6033 first_dict = d->dv_used_next;
6034 else
6035 d->dv_used_prev->dv_used_next = d->dv_used_next;
6036 if (d->dv_used_next != NULL)
6037 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6038
6039 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006040 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006041 todo = d->dv_hashtab.ht_used;
6042 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006043 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006044 if (!HASHITEM_EMPTY(hi))
6045 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006046 /* Remove the item before deleting it, just in case there is
6047 * something recursive causing trouble. */
6048 di = HI2DI(hi);
6049 hash_remove(&d->dv_hashtab, hi);
6050 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006051 --todo;
6052 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006053 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006054 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006055 vim_free(d);
6056}
6057
6058/*
6059 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006060 * The "key" is copied to the new item.
6061 * Note that the value of the item "di_tv" still needs to be initialized!
6062 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006063 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006065dictitem_alloc(key)
6066 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006067{
Bram Moolenaar33570922005-01-25 22:26:29 +00006068 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006069
Bram Moolenaar33570922005-01-25 22:26:29 +00006070 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006071 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006072 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006073 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006074 di->di_flags = 0;
6075 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006076 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006077}
6078
6079/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006080 * Make a copy of a Dictionary item.
6081 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006082 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006083dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006084 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006085{
Bram Moolenaar33570922005-01-25 22:26:29 +00006086 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006087
Bram Moolenaar33570922005-01-25 22:26:29 +00006088 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006089 if (di != NULL)
6090 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006091 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006092 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006093 copy_tv(&org->di_tv, &di->di_tv);
6094 }
6095 return di;
6096}
6097
6098/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006099 * Remove item "item" from Dictionary "dict" and free it.
6100 */
6101 static void
6102dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006103 dict_T *dict;
6104 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006105{
Bram Moolenaar33570922005-01-25 22:26:29 +00006106 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006107
Bram Moolenaar33570922005-01-25 22:26:29 +00006108 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006109 if (HASHITEM_EMPTY(hi))
6110 EMSG2(_(e_intern2), "dictitem_remove()");
6111 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006112 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006113 dictitem_free(item);
6114}
6115
6116/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006117 * Free a dict item. Also clears the value.
6118 */
6119 static void
6120dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006121 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006122{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006123 clear_tv(&item->di_tv);
6124 vim_free(item);
6125}
6126
6127/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006128 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6129 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006130 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006131 * Returns NULL when out of memory.
6132 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006133 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006134dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006135 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006136 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006137 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006138{
Bram Moolenaar33570922005-01-25 22:26:29 +00006139 dict_T *copy;
6140 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006141 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006142 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006143
6144 if (orig == NULL)
6145 return NULL;
6146
6147 copy = dict_alloc();
6148 if (copy != NULL)
6149 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006150 if (copyID != 0)
6151 {
6152 orig->dv_copyID = copyID;
6153 orig->dv_copydict = copy;
6154 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006155 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006156 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006157 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006158 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006159 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006160 --todo;
6161
6162 di = dictitem_alloc(hi->hi_key);
6163 if (di == NULL)
6164 break;
6165 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006166 {
6167 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6168 copyID) == FAIL)
6169 {
6170 vim_free(di);
6171 break;
6172 }
6173 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006174 else
6175 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6176 if (dict_add(copy, di) == FAIL)
6177 {
6178 dictitem_free(di);
6179 break;
6180 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006181 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006182 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006183
Bram Moolenaare9a41262005-01-15 22:18:47 +00006184 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006185 if (todo > 0)
6186 {
6187 dict_unref(copy);
6188 copy = NULL;
6189 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006190 }
6191
6192 return copy;
6193}
6194
6195/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006196 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006197 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006198 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006199 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006200dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006201 dict_T *d;
6202 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006203{
Bram Moolenaar33570922005-01-25 22:26:29 +00006204 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006205}
6206
Bram Moolenaar8c711452005-01-14 21:53:12 +00006207/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006208 * Add a number or string entry to dictionary "d".
6209 * When "str" is NULL use number "nr", otherwise use "str".
6210 * Returns FAIL when out of memory and when key already exists.
6211 */
6212 int
6213dict_add_nr_str(d, key, nr, str)
6214 dict_T *d;
6215 char *key;
6216 long nr;
6217 char_u *str;
6218{
6219 dictitem_T *item;
6220
6221 item = dictitem_alloc((char_u *)key);
6222 if (item == NULL)
6223 return FAIL;
6224 item->di_tv.v_lock = 0;
6225 if (str == NULL)
6226 {
6227 item->di_tv.v_type = VAR_NUMBER;
6228 item->di_tv.vval.v_number = nr;
6229 }
6230 else
6231 {
6232 item->di_tv.v_type = VAR_STRING;
6233 item->di_tv.vval.v_string = vim_strsave(str);
6234 }
6235 if (dict_add(d, item) == FAIL)
6236 {
6237 dictitem_free(item);
6238 return FAIL;
6239 }
6240 return OK;
6241}
6242
6243/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006244 * Get the number of items in a Dictionary.
6245 */
6246 static long
6247dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006248 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006249{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006250 if (d == NULL)
6251 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006252 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006253}
6254
6255/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006256 * Find item "key[len]" in Dictionary "d".
6257 * If "len" is negative use strlen(key).
6258 * Returns NULL when not found.
6259 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006260 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006261dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006262 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006263 char_u *key;
6264 int len;
6265{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006266#define AKEYLEN 200
6267 char_u buf[AKEYLEN];
6268 char_u *akey;
6269 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006270 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006271
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006272 if (len < 0)
6273 akey = key;
6274 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006275 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006276 tofree = akey = vim_strnsave(key, len);
6277 if (akey == NULL)
6278 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006279 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006280 else
6281 {
6282 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006283 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006284 akey = buf;
6285 }
6286
Bram Moolenaar33570922005-01-25 22:26:29 +00006287 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006288 vim_free(tofree);
6289 if (HASHITEM_EMPTY(hi))
6290 return NULL;
6291 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006292}
6293
6294/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006295 * Get a string item from a dictionary in allocated memory.
6296 * Returns NULL if the entry doesn't exist or out of memory.
6297 */
6298 char_u *
6299get_dict_string(d, key)
6300 dict_T *d;
6301 char_u *key;
6302{
6303 dictitem_T *di;
6304
6305 di = dict_find(d, key, -1);
6306 if (di == NULL)
6307 return NULL;
6308 return vim_strsave(get_tv_string(&di->di_tv));
6309}
6310
6311/*
6312 * Get a number item from a dictionary.
6313 * Returns 0 if the entry doesn't exist or out of memory.
6314 */
6315 long
6316get_dict_number(d, key)
6317 dict_T *d;
6318 char_u *key;
6319{
6320 dictitem_T *di;
6321
6322 di = dict_find(d, key, -1);
6323 if (di == NULL)
6324 return 0;
6325 return get_tv_number(&di->di_tv);
6326}
6327
6328/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006329 * Return an allocated string with the string representation of a Dictionary.
6330 * May return NULL.
6331 */
6332 static char_u *
6333dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006334 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006335{
6336 garray_T ga;
6337 int first = TRUE;
6338 char_u *tofree;
6339 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006340 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006341 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006342 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006343 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006344
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006345 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006346 return NULL;
6347 ga_init2(&ga, (int)sizeof(char), 80);
6348 ga_append(&ga, '{');
6349
Bram Moolenaar33570922005-01-25 22:26:29 +00006350 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006351 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006352 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006353 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006354 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006355 --todo;
6356
6357 if (first)
6358 first = FALSE;
6359 else
6360 ga_concat(&ga, (char_u *)", ");
6361
6362 tofree = string_quote(hi->hi_key, FALSE);
6363 if (tofree != NULL)
6364 {
6365 ga_concat(&ga, tofree);
6366 vim_free(tofree);
6367 }
6368 ga_concat(&ga, (char_u *)": ");
6369 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6370 if (s != NULL)
6371 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006372 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006373 if (s == NULL)
6374 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006375 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006376 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006377 if (todo > 0)
6378 {
6379 vim_free(ga.ga_data);
6380 return NULL;
6381 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006382
6383 ga_append(&ga, '}');
6384 ga_append(&ga, NUL);
6385 return (char_u *)ga.ga_data;
6386}
6387
6388/*
6389 * Allocate a variable for a Dictionary and fill it from "*arg".
6390 * Return OK or FAIL. Returns NOTDONE for {expr}.
6391 */
6392 static int
6393get_dict_tv(arg, rettv, evaluate)
6394 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006395 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006396 int evaluate;
6397{
Bram Moolenaar33570922005-01-25 22:26:29 +00006398 dict_T *d = NULL;
6399 typval_T tvkey;
6400 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006401 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006402 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006403 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006404 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006405
6406 /*
6407 * First check if it's not a curly-braces thing: {expr}.
6408 * Must do this without evaluating, otherwise a function may be called
6409 * twice. Unfortunately this means we need to call eval1() twice for the
6410 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006411 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006412 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006413 if (*start != '}')
6414 {
6415 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6416 return FAIL;
6417 if (*start == '}')
6418 return NOTDONE;
6419 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006420
6421 if (evaluate)
6422 {
6423 d = dict_alloc();
6424 if (d == NULL)
6425 return FAIL;
6426 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006427 tvkey.v_type = VAR_UNKNOWN;
6428 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006429
6430 *arg = skipwhite(*arg + 1);
6431 while (**arg != '}' && **arg != NUL)
6432 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006433 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006434 goto failret;
6435 if (**arg != ':')
6436 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006437 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006438 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006439 goto failret;
6440 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006441 key = get_tv_string_buf_chk(&tvkey, buf);
6442 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006444 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6445 if (key != NULL)
6446 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006447 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006448 goto failret;
6449 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006450
6451 *arg = skipwhite(*arg + 1);
6452 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6453 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006454 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006455 goto failret;
6456 }
6457 if (evaluate)
6458 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006459 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006460 if (item != NULL)
6461 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006462 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006463 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006464 clear_tv(&tv);
6465 goto failret;
6466 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006467 item = dictitem_alloc(key);
6468 clear_tv(&tvkey);
6469 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006470 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006471 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006472 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006473 if (dict_add(d, item) == FAIL)
6474 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006475 }
6476 }
6477
6478 if (**arg == '}')
6479 break;
6480 if (**arg != ',')
6481 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006482 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006483 goto failret;
6484 }
6485 *arg = skipwhite(*arg + 1);
6486 }
6487
6488 if (**arg != '}')
6489 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006490 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006491failret:
6492 if (evaluate)
6493 dict_free(d);
6494 return FAIL;
6495 }
6496
6497 *arg = skipwhite(*arg + 1);
6498 if (evaluate)
6499 {
6500 rettv->v_type = VAR_DICT;
6501 rettv->vval.v_dict = d;
6502 ++d->dv_refcount;
6503 }
6504
6505 return OK;
6506}
6507
Bram Moolenaar8c711452005-01-14 21:53:12 +00006508/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006509 * Return a string with the string representation of a variable.
6510 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006511 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006512 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006513 * May return NULL;
6514 */
6515 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006516echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006517 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006518 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006519 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006520{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006521 static int recurse = 0;
6522 char_u *r = NULL;
6523
Bram Moolenaar33570922005-01-25 22:26:29 +00006524 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006525 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006526 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006527 *tofree = NULL;
6528 return NULL;
6529 }
6530 ++recurse;
6531
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006532 switch (tv->v_type)
6533 {
6534 case VAR_FUNC:
6535 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006536 r = tv->vval.v_string;
6537 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006538 case VAR_LIST:
6539 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006540 r = *tofree;
6541 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006542 case VAR_DICT:
6543 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006544 r = *tofree;
6545 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006546 case VAR_STRING:
6547 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006548 *tofree = NULL;
6549 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006551 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006552 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006553 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006554 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006555
6556 --recurse;
6557 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006558}
6559
6560/*
6561 * Return a string with the string representation of a variable.
6562 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6563 * "numbuf" is used for a number.
6564 * Puts quotes around strings, so that they can be parsed back by eval().
6565 * May return NULL;
6566 */
6567 static char_u *
6568tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006569 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006570 char_u **tofree;
6571 char_u *numbuf;
6572{
6573 switch (tv->v_type)
6574 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006575 case VAR_FUNC:
6576 *tofree = string_quote(tv->vval.v_string, TRUE);
6577 return *tofree;
6578 case VAR_STRING:
6579 *tofree = string_quote(tv->vval.v_string, FALSE);
6580 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006581 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006582 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006583 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006584 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006585 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006586 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006588 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589}
6590
6591/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006592 * Return string "str" in ' quotes, doubling ' characters.
6593 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006594 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006595 */
6596 static char_u *
6597string_quote(str, function)
6598 char_u *str;
6599 int function;
6600{
Bram Moolenaar33570922005-01-25 22:26:29 +00006601 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006602 char_u *p, *r, *s;
6603
Bram Moolenaar33570922005-01-25 22:26:29 +00006604 len = (function ? 13 : 3);
6605 if (str != NULL)
6606 {
6607 len += STRLEN(str);
6608 for (p = str; *p != NUL; mb_ptr_adv(p))
6609 if (*p == '\'')
6610 ++len;
6611 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006612 s = r = alloc(len);
6613 if (r != NULL)
6614 {
6615 if (function)
6616 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006617 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006618 r += 10;
6619 }
6620 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006621 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006622 if (str != NULL)
6623 for (p = str; *p != NUL; )
6624 {
6625 if (*p == '\'')
6626 *r++ = '\'';
6627 MB_COPY_CHAR(p, r);
6628 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006629 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006630 if (function)
6631 *r++ = ')';
6632 *r++ = NUL;
6633 }
6634 return s;
6635}
6636
6637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 * Get the value of an environment variable.
6639 * "arg" is pointing to the '$'. It is advanced to after the name.
6640 * If the environment variable was not set, silently assume it is empty.
6641 * Always return OK.
6642 */
6643 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006644get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 int evaluate;
6648{
6649 char_u *string = NULL;
6650 int len;
6651 int cc;
6652 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006653 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654
6655 ++*arg;
6656 name = *arg;
6657 len = get_env_len(arg);
6658 if (evaluate)
6659 {
6660 if (len != 0)
6661 {
6662 cc = name[len];
6663 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006664 /* first try vim_getenv(), fast for normal environment vars */
6665 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006667 {
6668 if (!mustfree)
6669 string = vim_strsave(string);
6670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 else
6672 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006673 if (mustfree)
6674 vim_free(string);
6675
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 /* next try expanding things like $VIM and ${HOME} */
6677 string = expand_env_save(name - 1);
6678 if (string != NULL && *string == '$')
6679 {
6680 vim_free(string);
6681 string = NULL;
6682 }
6683 }
6684 name[len] = cc;
6685 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006686 rettv->v_type = VAR_STRING;
6687 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 }
6689
6690 return OK;
6691}
6692
6693/*
6694 * Array with names and number of arguments of all internal functions
6695 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6696 */
6697static struct fst
6698{
6699 char *f_name; /* function name */
6700 char f_min_argc; /* minimal number of arguments */
6701 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006702 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006703 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704} functions[] =
6705{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006706 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 {"append", 2, 2, f_append},
6708 {"argc", 0, 0, f_argc},
6709 {"argidx", 0, 0, f_argidx},
6710 {"argv", 1, 1, f_argv},
6711 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006712 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 {"bufexists", 1, 1, f_bufexists},
6714 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6715 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6716 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6717 {"buflisted", 1, 1, f_buflisted},
6718 {"bufloaded", 1, 1, f_bufloaded},
6719 {"bufname", 1, 1, f_bufname},
6720 {"bufnr", 1, 1, f_bufnr},
6721 {"bufwinnr", 1, 1, f_bufwinnr},
6722 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006723 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006724 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 {"char2nr", 1, 1, f_char2nr},
6726 {"cindent", 1, 1, f_cindent},
6727 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006728#if defined(FEAT_INS_EXPAND)
6729 {"complete_add", 1, 1, f_complete_add},
6730 {"complete_check", 0, 0, f_complete_check},
6731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006733 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006734 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 {"cscope_connection",0,3, f_cscope_connection},
6736 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006737 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 {"delete", 1, 1, f_delete},
6739 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006740 {"diff_filler", 1, 1, f_diff_filler},
6741 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006742 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006744 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 {"eventhandler", 0, 0, f_eventhandler},
6746 {"executable", 1, 1, f_executable},
6747 {"exists", 1, 1, f_exists},
6748 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006749 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6751 {"filereadable", 1, 1, f_filereadable},
6752 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006753 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006754 {"finddir", 1, 3, f_finddir},
6755 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 {"fnamemodify", 2, 2, f_fnamemodify},
6757 {"foldclosed", 1, 1, f_foldclosed},
6758 {"foldclosedend", 1, 1, f_foldclosedend},
6759 {"foldlevel", 1, 1, f_foldlevel},
6760 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006761 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006763 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006764 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006765 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006766 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 {"getbufvar", 2, 2, f_getbufvar},
6768 {"getchar", 0, 1, f_getchar},
6769 {"getcharmod", 0, 0, f_getcharmod},
6770 {"getcmdline", 0, 0, f_getcmdline},
6771 {"getcmdpos", 0, 0, f_getcmdpos},
6772 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006773 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006774 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 {"getfsize", 1, 1, f_getfsize},
6776 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006777 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006778 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006779 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006780 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 {"getregtype", 0, 1, f_getregtype},
6782 {"getwinposx", 0, 0, f_getwinposx},
6783 {"getwinposy", 0, 0, f_getwinposy},
6784 {"getwinvar", 2, 2, f_getwinvar},
6785 {"glob", 1, 1, f_glob},
6786 {"globpath", 2, 2, f_globpath},
6787 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006788 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 {"hasmapto", 1, 2, f_hasmapto},
6790 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6791 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6792 {"histadd", 2, 2, f_histadd},
6793 {"histdel", 1, 2, f_histdel},
6794 {"histget", 1, 2, f_histget},
6795 {"histnr", 1, 1, f_histnr},
6796 {"hlID", 1, 1, f_hlID},
6797 {"hlexists", 1, 1, f_hlexists},
6798 {"hostname", 0, 0, f_hostname},
6799 {"iconv", 3, 3, f_iconv},
6800 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006801 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 {"input", 1, 2, f_input},
6803 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00006804 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 {"inputrestore", 0, 0, f_inputrestore},
6806 {"inputsave", 0, 0, f_inputsave},
6807 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006808 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006810 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006811 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006812 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006813 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006815 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 {"libcall", 3, 3, f_libcall},
6817 {"libcallnr", 3, 3, f_libcallnr},
6818 {"line", 1, 1, f_line},
6819 {"line2byte", 1, 1, f_line2byte},
6820 {"lispindent", 1, 1, f_lispindent},
6821 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006822 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 {"maparg", 1, 2, f_maparg},
6824 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006825 {"match", 2, 4, f_match},
6826 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006827 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006828 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006829 {"max", 1, 1, f_max},
6830 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006831#ifdef vim_mkdir
6832 {"mkdir", 1, 3, f_mkdir},
6833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 {"mode", 0, 0, f_mode},
6835 {"nextnonblank", 1, 1, f_nextnonblank},
6836 {"nr2char", 1, 1, f_nr2char},
6837 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006838 {"printf", 2, 19, f_printf},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006839 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006840 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 {"remote_expr", 2, 3, f_remote_expr},
6842 {"remote_foreground", 1, 1, f_remote_foreground},
6843 {"remote_peek", 1, 2, f_remote_peek},
6844 {"remote_read", 1, 1, f_remote_read},
6845 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006846 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006848 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006850 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 {"search", 1, 2, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00006852 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 {"searchpair", 3, 5, f_searchpair},
6854 {"server2client", 2, 2, f_server2client},
6855 {"serverlist", 0, 0, f_serverlist},
6856 {"setbufvar", 3, 3, f_setbufvar},
6857 {"setcmdpos", 1, 1, f_setcmdpos},
6858 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006859 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 {"setreg", 2, 3, f_setreg},
6861 {"setwinvar", 3, 3, f_setwinvar},
6862 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006863 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006864 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006865 {"spellbadword", 0, 0, f_spellbadword},
6866 {"spellsuggest", 1, 2, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006867 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868#ifdef HAVE_STRFTIME
6869 {"strftime", 1, 2, f_strftime},
6870#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006871 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006872 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 {"strlen", 1, 1, f_strlen},
6874 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006875 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 {"strtrans", 1, 1, f_strtrans},
6877 {"submatch", 1, 1, f_submatch},
6878 {"substitute", 4, 4, f_substitute},
6879 {"synID", 3, 3, f_synID},
6880 {"synIDattr", 2, 3, f_synIDattr},
6881 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006882 {"system", 1, 2, f_system},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006883 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006884 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00006886 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 {"tolower", 1, 1, f_tolower},
6888 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006889 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006891 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 {"virtcol", 1, 1, f_virtcol},
6893 {"visualmode", 0, 1, f_visualmode},
6894 {"winbufnr", 1, 1, f_winbufnr},
6895 {"wincol", 0, 0, f_wincol},
6896 {"winheight", 1, 1, f_winheight},
6897 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006898 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 {"winrestcmd", 0, 0, f_winrestcmd},
6900 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006901 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902};
6903
6904#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6905
6906/*
6907 * Function given to ExpandGeneric() to obtain the list of internal
6908 * or user defined function names.
6909 */
6910 char_u *
6911get_function_name(xp, idx)
6912 expand_T *xp;
6913 int idx;
6914{
6915 static int intidx = -1;
6916 char_u *name;
6917
6918 if (idx == 0)
6919 intidx = -1;
6920 if (intidx < 0)
6921 {
6922 name = get_user_func_name(xp, idx);
6923 if (name != NULL)
6924 return name;
6925 }
6926 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6927 {
6928 STRCPY(IObuff, functions[intidx].f_name);
6929 STRCAT(IObuff, "(");
6930 if (functions[intidx].f_max_argc == 0)
6931 STRCAT(IObuff, ")");
6932 return IObuff;
6933 }
6934
6935 return NULL;
6936}
6937
6938/*
6939 * Function given to ExpandGeneric() to obtain the list of internal or
6940 * user defined variable or function names.
6941 */
6942/*ARGSUSED*/
6943 char_u *
6944get_expr_name(xp, idx)
6945 expand_T *xp;
6946 int idx;
6947{
6948 static int intidx = -1;
6949 char_u *name;
6950
6951 if (idx == 0)
6952 intidx = -1;
6953 if (intidx < 0)
6954 {
6955 name = get_function_name(xp, idx);
6956 if (name != NULL)
6957 return name;
6958 }
6959 return get_user_var_name(xp, ++intidx);
6960}
6961
6962#endif /* FEAT_CMDL_COMPL */
6963
6964/*
6965 * Find internal function in table above.
6966 * Return index, or -1 if not found
6967 */
6968 static int
6969find_internal_func(name)
6970 char_u *name; /* name of the function */
6971{
6972 int first = 0;
6973 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6974 int cmp;
6975 int x;
6976
6977 /*
6978 * Find the function name in the table. Binary search.
6979 */
6980 while (first <= last)
6981 {
6982 x = first + ((unsigned)(last - first) >> 1);
6983 cmp = STRCMP(name, functions[x].f_name);
6984 if (cmp < 0)
6985 last = x - 1;
6986 else if (cmp > 0)
6987 first = x + 1;
6988 else
6989 return x;
6990 }
6991 return -1;
6992}
6993
6994/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006995 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6996 * name it contains, otherwise return "name".
6997 */
6998 static char_u *
6999deref_func_name(name, lenp)
7000 char_u *name;
7001 int *lenp;
7002{
Bram Moolenaar33570922005-01-25 22:26:29 +00007003 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007004 int cc;
7005
7006 cc = name[*lenp];
7007 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007008 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007009 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007010 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007011 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007012 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007013 {
7014 *lenp = 0;
7015 return (char_u *)""; /* just in case */
7016 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007017 *lenp = STRLEN(v->di_tv.vval.v_string);
7018 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007019 }
7020
7021 return name;
7022}
7023
7024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 * Allocate a variable for the result of a function.
7026 * Return OK or FAIL.
7027 */
7028 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007029get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7030 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 char_u *name; /* name of the function */
7032 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 char_u **arg; /* argument, pointing to the '(' */
7035 linenr_T firstline; /* first line of range */
7036 linenr_T lastline; /* last line of range */
7037 int *doesrange; /* return: function handled range */
7038 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007039 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040{
7041 char_u *argp;
7042 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007043 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 int argcount = 0; /* number of arguments found */
7045
7046 /*
7047 * Get the arguments.
7048 */
7049 argp = *arg;
7050 while (argcount < MAX_FUNC_ARGS)
7051 {
7052 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7053 if (*argp == ')' || *argp == ',' || *argp == NUL)
7054 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7056 {
7057 ret = FAIL;
7058 break;
7059 }
7060 ++argcount;
7061 if (*argp != ',')
7062 break;
7063 }
7064 if (*argp == ')')
7065 ++argp;
7066 else
7067 ret = FAIL;
7068
7069 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007070 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007071 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007073 {
7074 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007075 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007076 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007077 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
7080 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007081 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082
7083 *arg = skipwhite(argp);
7084 return ret;
7085}
7086
7087
7088/*
7089 * Call a function with its resolved parameters
7090 * Return OK or FAIL.
7091 */
7092 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007093call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007094 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 char_u *name; /* name of the function */
7096 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007097 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007099 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 linenr_T firstline; /* first line of range */
7101 linenr_T lastline; /* last line of range */
7102 int *doesrange; /* return: function handled range */
7103 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007104 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105{
7106 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107#define ERROR_UNKNOWN 0
7108#define ERROR_TOOMANY 1
7109#define ERROR_TOOFEW 2
7110#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007111#define ERROR_DICT 4
7112#define ERROR_NONE 5
7113#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 int error = ERROR_NONE;
7115 int i;
7116 int llen;
7117 ufunc_T *fp;
7118 int cc;
7119#define FLEN_FIXED 40
7120 char_u fname_buf[FLEN_FIXED + 1];
7121 char_u *fname;
7122
7123 /*
7124 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7125 * Change <SNR>123_name() to K_SNR 123_name().
7126 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7127 */
7128 cc = name[len];
7129 name[len] = NUL;
7130 llen = eval_fname_script(name);
7131 if (llen > 0)
7132 {
7133 fname_buf[0] = K_SPECIAL;
7134 fname_buf[1] = KS_EXTRA;
7135 fname_buf[2] = (int)KE_SNR;
7136 i = 3;
7137 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7138 {
7139 if (current_SID <= 0)
7140 error = ERROR_SCRIPT;
7141 else
7142 {
7143 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7144 i = (int)STRLEN(fname_buf);
7145 }
7146 }
7147 if (i + STRLEN(name + llen) < FLEN_FIXED)
7148 {
7149 STRCPY(fname_buf + i, name + llen);
7150 fname = fname_buf;
7151 }
7152 else
7153 {
7154 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7155 if (fname == NULL)
7156 error = ERROR_OTHER;
7157 else
7158 {
7159 mch_memmove(fname, fname_buf, (size_t)i);
7160 STRCPY(fname + i, name + llen);
7161 }
7162 }
7163 }
7164 else
7165 fname = name;
7166
7167 *doesrange = FALSE;
7168
7169
7170 /* execute the function if no errors detected and executing */
7171 if (evaluate && error == ERROR_NONE)
7172 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007173 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 error = ERROR_UNKNOWN;
7175
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007176 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 {
7178 /*
7179 * User defined function.
7180 */
7181 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007182
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007184 /* Trigger FuncUndefined event, may load the function. */
7185 if (fp == NULL
7186 && apply_autocmds(EVENT_FUNCUNDEFINED,
7187 fname, fname, TRUE, NULL)
7188 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007190 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 fp = find_func(fname);
7192 }
7193#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007194 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007195 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007196 {
7197 /* loaded a package, search for the function again */
7198 fp = find_func(fname);
7199 }
7200
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 if (fp != NULL)
7202 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007203 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007205 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007207 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007209 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007210 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 else
7212 {
7213 /*
7214 * Call the user function.
7215 * Save and restore search patterns, script variables and
7216 * redo buffer.
7217 */
7218 save_search_patterns();
7219 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007220 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007221 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007222 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007223 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7224 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7225 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007226 /* Function was unreferenced while being used, free it
7227 * now. */
7228 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 restoreRedobuff();
7230 restore_search_patterns();
7231 error = ERROR_NONE;
7232 }
7233 }
7234 }
7235 else
7236 {
7237 /*
7238 * Find the function name in the table, call its implementation.
7239 */
7240 i = find_internal_func(fname);
7241 if (i >= 0)
7242 {
7243 if (argcount < functions[i].f_min_argc)
7244 error = ERROR_TOOFEW;
7245 else if (argcount > functions[i].f_max_argc)
7246 error = ERROR_TOOMANY;
7247 else
7248 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007249 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007250 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 error = ERROR_NONE;
7252 }
7253 }
7254 }
7255 /*
7256 * The function call (or "FuncUndefined" autocommand sequence) might
7257 * have been aborted by an error, an interrupt, or an explicitly thrown
7258 * exception that has not been caught so far. This situation can be
7259 * tested for by calling aborting(). For an error in an internal
7260 * function or for the "E132" error in call_user_func(), however, the
7261 * throw point at which the "force_abort" flag (temporarily reset by
7262 * emsg()) is normally updated has not been reached yet. We need to
7263 * update that flag first to make aborting() reliable.
7264 */
7265 update_force_abort();
7266 }
7267 if (error == ERROR_NONE)
7268 ret = OK;
7269
7270 /*
7271 * Report an error unless the argument evaluation or function call has been
7272 * cancelled due to an aborting error, an interrupt, or an exception.
7273 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007274 if (!aborting())
7275 {
7276 switch (error)
7277 {
7278 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007279 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007280 break;
7281 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007282 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007283 break;
7284 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007285 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007286 name);
7287 break;
7288 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007289 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007290 name);
7291 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007292 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007293 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007294 name);
7295 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007296 }
7297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298
7299 name[len] = cc;
7300 if (fname != name && fname != fname_buf)
7301 vim_free(fname);
7302
7303 return ret;
7304}
7305
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007306/*
7307 * Give an error message with a function name. Handle <SNR> things.
7308 */
7309 static void
7310emsg_funcname(msg, name)
7311 char *msg;
7312 char_u *name;
7313{
7314 char_u *p;
7315
7316 if (*name == K_SPECIAL)
7317 p = concat_str((char_u *)"<SNR>", name + 3);
7318 else
7319 p = name;
7320 EMSG2(_(msg), p);
7321 if (p != name)
7322 vim_free(p);
7323}
7324
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325/*********************************************
7326 * Implementation of the built-in functions
7327 */
7328
7329/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007330 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 */
7332 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007333f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 typval_T *argvars;
7335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336{
Bram Moolenaar33570922005-01-25 22:26:29 +00007337 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007339 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007340 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007342 if ((l = argvars[0].vval.v_list) != NULL
7343 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7344 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007345 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007346 }
7347 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007348 EMSG(_(e_listreq));
7349}
7350
7351/*
7352 * "append(lnum, string/list)" function
7353 */
7354 static void
7355f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 typval_T *argvars;
7357 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007358{
7359 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007360 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007361 list_T *l = NULL;
7362 listitem_T *li = NULL;
7363 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007364 long added = 0;
7365
Bram Moolenaar0d660222005-01-07 21:51:51 +00007366 lnum = get_tv_lnum(argvars);
7367 if (lnum >= 0
7368 && lnum <= curbuf->b_ml.ml_line_count
7369 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007370 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007371 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007372 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007373 l = argvars[1].vval.v_list;
7374 if (l == NULL)
7375 return;
7376 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007377 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007378 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007379 for (;;)
7380 {
7381 if (l == NULL)
7382 tv = &argvars[1]; /* append a string */
7383 else if (li == NULL)
7384 break; /* end of list */
7385 else
7386 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007387 line = get_tv_string_chk(tv);
7388 if (line == NULL) /* type error */
7389 {
7390 rettv->vval.v_number = 1; /* Failed */
7391 break;
7392 }
7393 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007394 ++added;
7395 if (l == NULL)
7396 break;
7397 li = li->li_next;
7398 }
7399
7400 appended_lines_mark(lnum, added);
7401 if (curwin->w_cursor.lnum > lnum)
7402 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007404 else
7405 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406}
7407
7408/*
7409 * "argc()" function
7410 */
7411/* ARGSUSED */
7412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007413f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007414 typval_T *argvars;
7415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007417 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418}
7419
7420/*
7421 * "argidx()" function
7422 */
7423/* ARGSUSED */
7424 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007425f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007426 typval_T *argvars;
7427 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007429 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430}
7431
7432/*
7433 * "argv(nr)" function
7434 */
7435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007436f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007437 typval_T *argvars;
7438 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439{
7440 int idx;
7441
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007442 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007444 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007446 rettv->vval.v_string = NULL;
7447 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448}
7449
7450/*
7451 * "browse(save, title, initdir, default)" function
7452 */
7453/* ARGSUSED */
7454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007455f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007456 typval_T *argvars;
7457 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458{
7459#ifdef FEAT_BROWSE
7460 int save;
7461 char_u *title;
7462 char_u *initdir;
7463 char_u *defname;
7464 char_u buf[NUMBUFLEN];
7465 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007466 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007468 save = get_tv_number_chk(&argvars[0], &error);
7469 title = get_tv_string_chk(&argvars[1]);
7470 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7471 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007473 if (error || title == NULL || initdir == NULL || defname == NULL)
7474 rettv->vval.v_string = NULL;
7475 else
7476 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007477 do_browse(save ? BROWSE_SAVE : 0,
7478 title, defname, NULL, initdir, NULL, curbuf);
7479#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007480 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007481#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007482 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007483}
7484
7485/*
7486 * "browsedir(title, initdir)" function
7487 */
7488/* ARGSUSED */
7489 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007490f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007491 typval_T *argvars;
7492 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007493{
7494#ifdef FEAT_BROWSE
7495 char_u *title;
7496 char_u *initdir;
7497 char_u buf[NUMBUFLEN];
7498
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007499 title = get_tv_string_chk(&argvars[0]);
7500 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007501
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007502 if (title == NULL || initdir == NULL)
7503 rettv->vval.v_string = NULL;
7504 else
7505 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007506 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007508 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007510 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511}
7512
Bram Moolenaar33570922005-01-25 22:26:29 +00007513static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007514
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515/*
7516 * Find a buffer by number or exact name.
7517 */
7518 static buf_T *
7519find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007520 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521{
7522 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007524 if (avar->v_type == VAR_NUMBER)
7525 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007526 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007528 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007529 if (buf == NULL)
7530 {
7531 /* No full path name match, try a match with a URL or a "nofile"
7532 * buffer, these don't use the full path. */
7533 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7534 if (buf->b_fname != NULL
7535 && (path_with_url(buf->b_fname)
7536#ifdef FEAT_QUICKFIX
7537 || bt_nofile(buf)
7538#endif
7539 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007540 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007541 break;
7542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 }
7544 return buf;
7545}
7546
7547/*
7548 * "bufexists(expr)" function
7549 */
7550 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007551f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007552 typval_T *argvars;
7553 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007555 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556}
7557
7558/*
7559 * "buflisted(expr)" function
7560 */
7561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007562f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007563 typval_T *argvars;
7564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565{
7566 buf_T *buf;
7567
7568 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007569 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570}
7571
7572/*
7573 * "bufloaded(expr)" function
7574 */
7575 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007576f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007577 typval_T *argvars;
7578 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579{
7580 buf_T *buf;
7581
7582 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007583 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584}
7585
Bram Moolenaar33570922005-01-25 22:26:29 +00007586static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007587
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588/*
7589 * Get buffer by number or pattern.
7590 */
7591 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007592get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007595 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 int save_magic;
7597 char_u *save_cpo;
7598 buf_T *buf;
7599
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007600 if (tv->v_type == VAR_NUMBER)
7601 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007602 if (tv->v_type != VAR_STRING)
7603 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604 if (name == NULL || *name == NUL)
7605 return curbuf;
7606 if (name[0] == '$' && name[1] == NUL)
7607 return lastbuf;
7608
7609 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7610 save_magic = p_magic;
7611 p_magic = TRUE;
7612 save_cpo = p_cpo;
7613 p_cpo = (char_u *)"";
7614
7615 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7616 TRUE, FALSE));
7617
7618 p_magic = save_magic;
7619 p_cpo = save_cpo;
7620
7621 /* If not found, try expanding the name, like done for bufexists(). */
7622 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007623 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624
7625 return buf;
7626}
7627
7628/*
7629 * "bufname(expr)" function
7630 */
7631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007632f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007633 typval_T *argvars;
7634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635{
7636 buf_T *buf;
7637
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007638 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007640 buf = get_buf_tv(&argvars[0]);
7641 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007643 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007645 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 --emsg_off;
7647}
7648
7649/*
7650 * "bufnr(expr)" function
7651 */
7652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007653f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007654 typval_T *argvars;
7655 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656{
7657 buf_T *buf;
7658
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007659 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007661 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007663 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007665 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 --emsg_off;
7667}
7668
7669/*
7670 * "bufwinnr(nr)" function
7671 */
7672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007673f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007674 typval_T *argvars;
7675 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676{
7677#ifdef FEAT_WINDOWS
7678 win_T *wp;
7679 int winnr = 0;
7680#endif
7681 buf_T *buf;
7682
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007683 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007685 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686#ifdef FEAT_WINDOWS
7687 for (wp = firstwin; wp; wp = wp->w_next)
7688 {
7689 ++winnr;
7690 if (wp->w_buffer == buf)
7691 break;
7692 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007693 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007695 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696#endif
7697 --emsg_off;
7698}
7699
7700/*
7701 * "byte2line(byte)" function
7702 */
7703/*ARGSUSED*/
7704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007705f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007706 typval_T *argvars;
7707 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708{
7709#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007710 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711#else
7712 long boff = 0;
7713
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007714 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007716 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007718 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 (linenr_T)0, &boff);
7720#endif
7721}
7722
7723/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007724 * "byteidx()" function
7725 */
7726/*ARGSUSED*/
7727 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007728f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007729 typval_T *argvars;
7730 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007731{
7732#ifdef FEAT_MBYTE
7733 char_u *t;
7734#endif
7735 char_u *str;
7736 long idx;
7737
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007738 str = get_tv_string_chk(&argvars[0]);
7739 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007740 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007741 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007742 return;
7743
7744#ifdef FEAT_MBYTE
7745 t = str;
7746 for ( ; idx > 0; idx--)
7747 {
7748 if (*t == NUL) /* EOL reached */
7749 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007750 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007751 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007752 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007753#else
7754 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007755 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007756#endif
7757}
7758
7759/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007760 * "call(func, arglist)" function
7761 */
7762 static void
7763f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007764 typval_T *argvars;
7765 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007766{
7767 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007768 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007769 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007770 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007771 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007772 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007773
7774 rettv->vval.v_number = 0;
7775 if (argvars[1].v_type != VAR_LIST)
7776 {
7777 EMSG(_(e_listreq));
7778 return;
7779 }
7780 if (argvars[1].vval.v_list == NULL)
7781 return;
7782
7783 if (argvars[0].v_type == VAR_FUNC)
7784 func = argvars[0].vval.v_string;
7785 else
7786 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007787 if (*func == NUL)
7788 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007789
Bram Moolenaare9a41262005-01-15 22:18:47 +00007790 if (argvars[2].v_type != VAR_UNKNOWN)
7791 {
7792 if (argvars[2].v_type != VAR_DICT)
7793 {
7794 EMSG(_(e_dictreq));
7795 return;
7796 }
7797 selfdict = argvars[2].vval.v_dict;
7798 }
7799
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007800 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7801 item = item->li_next)
7802 {
7803 if (argc == MAX_FUNC_ARGS)
7804 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007805 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007806 break;
7807 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007808 /* Make a copy of each argument. This is needed to be able to set
7809 * v_lock to VAR_FIXED in the copy without changing the original list.
7810 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007811 copy_tv(&item->li_tv, &argv[argc++]);
7812 }
7813
7814 if (item == NULL)
7815 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007816 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7817 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007818
7819 /* Free the arguments. */
7820 while (argc > 0)
7821 clear_tv(&argv[--argc]);
7822}
7823
7824/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 * "char2nr(string)" function
7826 */
7827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007828f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007829 typval_T *argvars;
7830 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831{
7832#ifdef FEAT_MBYTE
7833 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007834 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 else
7836#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007837 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838}
7839
7840/*
7841 * "cindent(lnum)" function
7842 */
7843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007844f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007845 typval_T *argvars;
7846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847{
7848#ifdef FEAT_CINDENT
7849 pos_T pos;
7850 linenr_T lnum;
7851
7852 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007853 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7855 {
7856 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007857 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 curwin->w_cursor = pos;
7859 }
7860 else
7861#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007862 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863}
7864
7865/*
7866 * "col(string)" function
7867 */
7868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007869f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007870 typval_T *argvars;
7871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872{
7873 colnr_T col = 0;
7874 pos_T *fp;
7875
7876 fp = var2fpos(&argvars[0], FALSE);
7877 if (fp != NULL)
7878 {
7879 if (fp->col == MAXCOL)
7880 {
7881 /* '> can be MAXCOL, get the length of the line then */
7882 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7883 col = STRLEN(ml_get(fp->lnum)) + 1;
7884 else
7885 col = MAXCOL;
7886 }
7887 else
7888 {
7889 col = fp->col + 1;
7890#ifdef FEAT_VIRTUALEDIT
7891 /* col(".") when the cursor is on the NUL at the end of the line
7892 * because of "coladd" can be seen as an extra column. */
7893 if (virtual_active() && fp == &curwin->w_cursor)
7894 {
7895 char_u *p = ml_get_cursor();
7896
7897 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7898 curwin->w_virtcol - curwin->w_cursor.coladd))
7899 {
7900# ifdef FEAT_MBYTE
7901 int l;
7902
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007903 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 col += l;
7905# else
7906 if (*p != NUL && p[1] == NUL)
7907 ++col;
7908# endif
7909 }
7910 }
7911#endif
7912 }
7913 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007914 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915}
7916
Bram Moolenaar572cb562005-08-05 21:35:02 +00007917#if defined(FEAT_INS_EXPAND)
7918/*
7919 * "complete_add()" function
7920 */
7921/*ARGSUSED*/
7922 static void
7923f_complete_add(argvars, rettv)
7924 typval_T *argvars;
7925 typval_T *rettv;
7926{
7927 char_u *s;
7928
7929 s = get_tv_string_chk(&argvars[0]);
7930 if (s != NULL)
7931 rettv->vval.v_number = ins_compl_add(s, -1, NULL, FORWARD, 0);
7932}
7933
7934/*
7935 * "complete_check()" function
7936 */
7937/*ARGSUSED*/
7938 static void
7939f_complete_check(argvars, rettv)
7940 typval_T *argvars;
7941 typval_T *rettv;
7942{
7943 int saved = RedrawingDisabled;
7944
7945 RedrawingDisabled = 0;
7946 ins_compl_check_keys(0);
7947 rettv->vval.v_number = compl_interrupted;
7948 RedrawingDisabled = saved;
7949}
7950#endif
7951
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952/*
7953 * "confirm(message, buttons[, default [, type]])" function
7954 */
7955/*ARGSUSED*/
7956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007957f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007958 typval_T *argvars;
7959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960{
7961#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7962 char_u *message;
7963 char_u *buttons = NULL;
7964 char_u buf[NUMBUFLEN];
7965 char_u buf2[NUMBUFLEN];
7966 int def = 1;
7967 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007968 char_u *typestr;
7969 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007971 message = get_tv_string_chk(&argvars[0]);
7972 if (message == NULL)
7973 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007974 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007976 buttons = get_tv_string_buf_chk(&argvars[1], buf);
7977 if (buttons == NULL)
7978 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007979 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007981 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007982 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007984 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
7985 if (typestr == NULL)
7986 error = TRUE;
7987 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007989 switch (TOUPPER_ASC(*typestr))
7990 {
7991 case 'E': type = VIM_ERROR; break;
7992 case 'Q': type = VIM_QUESTION; break;
7993 case 'I': type = VIM_INFO; break;
7994 case 'W': type = VIM_WARNING; break;
7995 case 'G': type = VIM_GENERIC; break;
7996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 }
7998 }
7999 }
8000 }
8001
8002 if (buttons == NULL || *buttons == NUL)
8003 buttons = (char_u *)_("&Ok");
8004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008005 if (error)
8006 rettv->vval.v_number = 0;
8007 else
8008 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 def, NULL);
8010#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008011 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012#endif
8013}
8014
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008015/*
8016 * "copy()" function
8017 */
8018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008019f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008020 typval_T *argvars;
8021 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008022{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008023 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008024}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025
8026/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008027 * "count()" function
8028 */
8029 static void
8030f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008031 typval_T *argvars;
8032 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008033{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008034 long n = 0;
8035 int ic = FALSE;
8036
Bram Moolenaare9a41262005-01-15 22:18:47 +00008037 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008038 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008039 listitem_T *li;
8040 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008041 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008042
Bram Moolenaare9a41262005-01-15 22:18:47 +00008043 if ((l = argvars[0].vval.v_list) != NULL)
8044 {
8045 li = l->lv_first;
8046 if (argvars[2].v_type != VAR_UNKNOWN)
8047 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008048 int error = FALSE;
8049
8050 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008051 if (argvars[3].v_type != VAR_UNKNOWN)
8052 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008053 idx = get_tv_number_chk(&argvars[3], &error);
8054 if (!error)
8055 {
8056 li = list_find(l, idx);
8057 if (li == NULL)
8058 EMSGN(_(e_listidx), idx);
8059 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008060 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008061 if (error)
8062 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008063 }
8064
8065 for ( ; li != NULL; li = li->li_next)
8066 if (tv_equal(&li->li_tv, &argvars[1], ic))
8067 ++n;
8068 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008069 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008070 else if (argvars[0].v_type == VAR_DICT)
8071 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008072 int todo;
8073 dict_T *d;
8074 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008075
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008076 if ((d = argvars[0].vval.v_dict) != NULL)
8077 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008078 int error = FALSE;
8079
Bram Moolenaare9a41262005-01-15 22:18:47 +00008080 if (argvars[2].v_type != VAR_UNKNOWN)
8081 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008082 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008083 if (argvars[3].v_type != VAR_UNKNOWN)
8084 EMSG(_(e_invarg));
8085 }
8086
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008087 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008088 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008089 {
8090 if (!HASHITEM_EMPTY(hi))
8091 {
8092 --todo;
8093 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8094 ++n;
8095 }
8096 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008097 }
8098 }
8099 else
8100 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008101 rettv->vval.v_number = n;
8102}
8103
8104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8106 *
8107 * Checks the existence of a cscope connection.
8108 */
8109/*ARGSUSED*/
8110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008111f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008112 typval_T *argvars;
8113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114{
8115#ifdef FEAT_CSCOPE
8116 int num = 0;
8117 char_u *dbpath = NULL;
8118 char_u *prepend = NULL;
8119 char_u buf[NUMBUFLEN];
8120
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008121 if (argvars[0].v_type != VAR_UNKNOWN
8122 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008124 num = (int)get_tv_number(&argvars[0]);
8125 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008126 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008127 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 }
8129
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008130 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008132 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133#endif
8134}
8135
8136/*
8137 * "cursor(lnum, col)" function
8138 *
8139 * Moves the cursor to the specified line and column
8140 */
8141/*ARGSUSED*/
8142 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008143f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008144 typval_T *argvars;
8145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146{
8147 long line, col;
8148
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008149 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008150 col = get_tv_number_chk(&argvars[1], NULL);
8151 if (line < 0 || col < 0)
8152 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 if (line > 0)
8154 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 if (col > 0)
8156 curwin->w_cursor.col = col - 1;
8157#ifdef FEAT_VIRTUALEDIT
8158 curwin->w_cursor.coladd = 0;
8159#endif
8160
8161 /* Make sure the cursor is in a valid position. */
8162 check_cursor();
8163#ifdef FEAT_MBYTE
8164 /* Correct cursor for multi-byte character. */
8165 if (has_mbyte)
8166 mb_adjust_cursor();
8167#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008168
8169 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170}
8171
8172/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008173 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 */
8175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008176f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008177 typval_T *argvars;
8178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008180 int noref = 0;
8181
8182 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008183 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008184 if (noref < 0 || noref > 1)
8185 EMSG(_(e_invarg));
8186 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008187 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188}
8189
8190/*
8191 * "delete()" function
8192 */
8193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008194f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008195 typval_T *argvars;
8196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197{
8198 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008199 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008201 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202}
8203
8204/*
8205 * "did_filetype()" function
8206 */
8207/*ARGSUSED*/
8208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008209f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008210 typval_T *argvars;
8211 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212{
8213#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008214 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008216 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217#endif
8218}
8219
8220/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008221 * "diff_filler()" function
8222 */
8223/*ARGSUSED*/
8224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008225f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008226 typval_T *argvars;
8227 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008228{
8229#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008230 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008231#endif
8232}
8233
8234/*
8235 * "diff_hlID()" function
8236 */
8237/*ARGSUSED*/
8238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008239f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008240 typval_T *argvars;
8241 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008242{
8243#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008244 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008245 static linenr_T prev_lnum = 0;
8246 static int changedtick = 0;
8247 static int fnum = 0;
8248 static int change_start = 0;
8249 static int change_end = 0;
8250 static enum hlf_value hlID = 0;
8251 int filler_lines;
8252 int col;
8253
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008254 if (lnum < 0) /* ignore type error in {lnum} arg */
8255 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008256 if (lnum != prev_lnum
8257 || changedtick != curbuf->b_changedtick
8258 || fnum != curbuf->b_fnum)
8259 {
8260 /* New line, buffer, change: need to get the values. */
8261 filler_lines = diff_check(curwin, lnum);
8262 if (filler_lines < 0)
8263 {
8264 if (filler_lines == -1)
8265 {
8266 change_start = MAXCOL;
8267 change_end = -1;
8268 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8269 hlID = HLF_ADD; /* added line */
8270 else
8271 hlID = HLF_CHD; /* changed line */
8272 }
8273 else
8274 hlID = HLF_ADD; /* added line */
8275 }
8276 else
8277 hlID = (enum hlf_value)0;
8278 prev_lnum = lnum;
8279 changedtick = curbuf->b_changedtick;
8280 fnum = curbuf->b_fnum;
8281 }
8282
8283 if (hlID == HLF_CHD || hlID == HLF_TXD)
8284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008285 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008286 if (col >= change_start && col <= change_end)
8287 hlID = HLF_TXD; /* changed text */
8288 else
8289 hlID = HLF_CHD; /* changed line */
8290 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008291 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008292#endif
8293}
8294
8295/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008296 * "empty({expr})" function
8297 */
8298 static void
8299f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008300 typval_T *argvars;
8301 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008302{
8303 int n;
8304
8305 switch (argvars[0].v_type)
8306 {
8307 case VAR_STRING:
8308 case VAR_FUNC:
8309 n = argvars[0].vval.v_string == NULL
8310 || *argvars[0].vval.v_string == NUL;
8311 break;
8312 case VAR_NUMBER:
8313 n = argvars[0].vval.v_number == 0;
8314 break;
8315 case VAR_LIST:
8316 n = argvars[0].vval.v_list == NULL
8317 || argvars[0].vval.v_list->lv_first == NULL;
8318 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008319 case VAR_DICT:
8320 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008321 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008322 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008323 default:
8324 EMSG2(_(e_intern2), "f_empty()");
8325 n = 0;
8326 }
8327
8328 rettv->vval.v_number = n;
8329}
8330
8331/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 * "escape({string}, {chars})" function
8333 */
8334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008335f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008336 typval_T *argvars;
8337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338{
8339 char_u buf[NUMBUFLEN];
8340
Bram Moolenaar758711c2005-02-02 23:11:38 +00008341 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8342 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008343 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344}
8345
8346/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008347 * "eval()" function
8348 */
8349/*ARGSUSED*/
8350 static void
8351f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008352 typval_T *argvars;
8353 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008354{
8355 char_u *s;
8356
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008357 s = get_tv_string_chk(&argvars[0]);
8358 if (s != NULL)
8359 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008360
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008361 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8362 {
8363 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008364 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008365 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008366 else if (*s != NUL)
8367 EMSG(_(e_trailing));
8368}
8369
8370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 * "eventhandler()" function
8372 */
8373/*ARGSUSED*/
8374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008375f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008376 typval_T *argvars;
8377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008379 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380}
8381
8382/*
8383 * "executable()" function
8384 */
8385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008386f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008387 typval_T *argvars;
8388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008390 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391}
8392
8393/*
8394 * "exists()" function
8395 */
8396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008397f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008398 typval_T *argvars;
8399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400{
8401 char_u *p;
8402 char_u *name;
8403 int n = FALSE;
8404 int len = 0;
8405
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008406 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 if (*p == '$') /* environment variable */
8408 {
8409 /* first try "normal" environment variables (fast) */
8410 if (mch_getenv(p + 1) != NULL)
8411 n = TRUE;
8412 else
8413 {
8414 /* try expanding things like $VIM and ${HOME} */
8415 p = expand_env_save(p);
8416 if (p != NULL && *p != '$')
8417 n = TRUE;
8418 vim_free(p);
8419 }
8420 }
8421 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008422 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 else if (*p == '*') /* internal or user defined function */
8424 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008425 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 }
8427 else if (*p == ':')
8428 {
8429 n = cmd_exists(p + 1);
8430 }
8431 else if (*p == '#')
8432 {
8433#ifdef FEAT_AUTOCMD
8434 name = p + 1;
8435 p = vim_strchr(name, '#');
8436 if (p != NULL)
8437 n = au_exists(name, p, p + 1);
8438 else
8439 n = au_exists(name, name + STRLEN(name), NULL);
8440#endif
8441 }
8442 else /* internal variable */
8443 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008444 char_u *tofree;
8445 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008447 /* get_name_len() takes care of expanding curly braces */
8448 name = p;
8449 len = get_name_len(&p, &tofree, TRUE, FALSE);
8450 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008452 if (tofree != NULL)
8453 name = tofree;
8454 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8455 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008457 /* handle d.key, l[idx], f(expr) */
8458 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8459 if (n)
8460 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 }
8462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008464 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 }
8466
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008467 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468}
8469
8470/*
8471 * "expand()" function
8472 */
8473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008474f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008475 typval_T *argvars;
8476 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477{
8478 char_u *s;
8479 int len;
8480 char_u *errormsg;
8481 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8482 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008483 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008485 rettv->v_type = VAR_STRING;
8486 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 if (*s == '%' || *s == '#' || *s == '<')
8488 {
8489 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008490 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 --emsg_off;
8492 }
8493 else
8494 {
8495 /* When the optional second argument is non-zero, don't remove matches
8496 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008497 if (argvars[1].v_type != VAR_UNKNOWN
8498 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008500 if (!error)
8501 {
8502 ExpandInit(&xpc);
8503 xpc.xp_context = EXPAND_FILES;
8504 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8505 ExpandCleanup(&xpc);
8506 }
8507 else
8508 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 }
8510}
8511
8512/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008513 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008514 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008515 */
8516 static void
8517f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008518 typval_T *argvars;
8519 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008520{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008521 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008522 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008523 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008524 list_T *l1, *l2;
8525 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008526 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008527 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008528
Bram Moolenaare9a41262005-01-15 22:18:47 +00008529 l1 = argvars[0].vval.v_list;
8530 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008531 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8532 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008533 {
8534 if (argvars[2].v_type != VAR_UNKNOWN)
8535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008536 before = get_tv_number_chk(&argvars[2], &error);
8537 if (error)
8538 return; /* type error; errmsg already given */
8539
Bram Moolenaar758711c2005-02-02 23:11:38 +00008540 if (before == l1->lv_len)
8541 item = NULL;
8542 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008543 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008544 item = list_find(l1, before);
8545 if (item == NULL)
8546 {
8547 EMSGN(_(e_listidx), before);
8548 return;
8549 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008550 }
8551 }
8552 else
8553 item = NULL;
8554 list_extend(l1, l2, item);
8555
Bram Moolenaare9a41262005-01-15 22:18:47 +00008556 copy_tv(&argvars[0], rettv);
8557 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008558 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008559 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8560 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008561 dict_T *d1, *d2;
8562 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008563 char_u *action;
8564 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008565 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008566 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008567
8568 d1 = argvars[0].vval.v_dict;
8569 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008570 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8571 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008572 {
8573 /* Check the third argument. */
8574 if (argvars[2].v_type != VAR_UNKNOWN)
8575 {
8576 static char *(av[]) = {"keep", "force", "error"};
8577
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008578 action = get_tv_string_chk(&argvars[2]);
8579 if (action == NULL)
8580 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008581 for (i = 0; i < 3; ++i)
8582 if (STRCMP(action, av[i]) == 0)
8583 break;
8584 if (i == 3)
8585 {
8586 EMSGN(_(e_invarg2), action);
8587 return;
8588 }
8589 }
8590 else
8591 action = (char_u *)"force";
8592
8593 /* Go over all entries in the second dict and add them to the
8594 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008595 todo = d2->dv_hashtab.ht_used;
8596 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008597 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008598 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008599 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008600 --todo;
8601 di1 = dict_find(d1, hi2->hi_key, -1);
8602 if (di1 == NULL)
8603 {
8604 di1 = dictitem_copy(HI2DI(hi2));
8605 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8606 dictitem_free(di1);
8607 }
8608 else if (*action == 'e')
8609 {
8610 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8611 break;
8612 }
8613 else if (*action == 'f')
8614 {
8615 clear_tv(&di1->di_tv);
8616 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8617 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008618 }
8619 }
8620
Bram Moolenaare9a41262005-01-15 22:18:47 +00008621 copy_tv(&argvars[0], rettv);
8622 }
8623 }
8624 else
8625 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008626}
8627
8628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 * "filereadable()" function
8630 */
8631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008632f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008633 typval_T *argvars;
8634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635{
8636 FILE *fd;
8637 char_u *p;
8638 int n;
8639
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008640 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8642 {
8643 n = TRUE;
8644 fclose(fd);
8645 }
8646 else
8647 n = FALSE;
8648
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008649 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650}
8651
8652/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008653 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 * rights to write into.
8655 */
8656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008657f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008658 typval_T *argvars;
8659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008661 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662}
8663
Bram Moolenaar33570922005-01-25 22:26:29 +00008664static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008665
8666 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008667findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008668 typval_T *argvars;
8669 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008670 int dir;
8671{
8672#ifdef FEAT_SEARCHPATH
8673 char_u *fname;
8674 char_u *fresult = NULL;
8675 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8676 char_u *p;
8677 char_u pathbuf[NUMBUFLEN];
8678 int count = 1;
8679 int first = TRUE;
8680
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008681 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008682
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008683 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008685 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8686 if (p == NULL)
8687 count = -1; /* error */
8688 else
8689 {
8690 if (*p != NUL)
8691 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008692
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008693 if (argvars[2].v_type != VAR_UNKNOWN)
8694 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8695 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008696 }
8697
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008698 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008699 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008700 do
8701 {
8702 vim_free(fresult);
8703 fresult = find_file_in_path_option(first ? fname : NULL,
8704 first ? (int)STRLEN(fname) : 0,
8705 0, first, path, dir, NULL);
8706 first = FALSE;
8707 } while (--count > 0 && fresult != NULL);
8708 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008709
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008710 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008711#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008712 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008713#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008714 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008715}
8716
Bram Moolenaar33570922005-01-25 22:26:29 +00008717static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8718static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008719
8720/*
8721 * Implementation of map() and filter().
8722 */
8723 static void
8724filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008725 typval_T *argvars;
8726 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008727 int map;
8728{
8729 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008730 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008731 listitem_T *li, *nli;
8732 list_T *l = NULL;
8733 dictitem_T *di;
8734 hashtab_T *ht;
8735 hashitem_T *hi;
8736 dict_T *d = NULL;
8737 typval_T save_val;
8738 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008739 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008740 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008741 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8742
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008743
8744 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008745 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008746 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008747 if ((l = argvars[0].vval.v_list) == NULL
8748 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008749 return;
8750 }
8751 else if (argvars[0].v_type == VAR_DICT)
8752 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008753 if ((d = argvars[0].vval.v_dict) == NULL
8754 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008755 return;
8756 }
8757 else
8758 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008759 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008760 return;
8761 }
8762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008763 expr = get_tv_string_buf_chk(&argvars[1], buf);
8764 /* On type errors, the preceding call has already displayed an error
8765 * message. Avoid a misleading error message for an empty string that
8766 * was not passed as argument. */
8767 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008769 prepare_vimvar(VV_VAL, &save_val);
8770 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008771
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008772 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008773 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008774 prepare_vimvar(VV_KEY, &save_key);
8775 vimvars[VV_KEY].vv_type = VAR_STRING;
8776
8777 ht = &d->dv_hashtab;
8778 hash_lock(ht);
8779 todo = ht->ht_used;
8780 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008782 if (!HASHITEM_EMPTY(hi))
8783 {
8784 --todo;
8785 di = HI2DI(hi);
8786 if (tv_check_lock(di->di_tv.v_lock, msg))
8787 break;
8788 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8789 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8790 break;
8791 if (!map && rem)
8792 dictitem_remove(d, di);
8793 clear_tv(&vimvars[VV_KEY].vv_tv);
8794 }
8795 }
8796 hash_unlock(ht);
8797
8798 restore_vimvar(VV_KEY, &save_key);
8799 }
8800 else
8801 {
8802 for (li = l->lv_first; li != NULL; li = nli)
8803 {
8804 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008805 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008806 nli = li->li_next;
8807 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008808 break;
8809 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008810 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008811 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008812 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008813
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008814 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008815 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008816
8817 copy_tv(&argvars[0], rettv);
8818}
8819
8820 static int
8821filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008822 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008823 char_u *expr;
8824 int map;
8825 int *remp;
8826{
Bram Moolenaar33570922005-01-25 22:26:29 +00008827 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008828 char_u *s;
8829
Bram Moolenaar33570922005-01-25 22:26:29 +00008830 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008831 s = expr;
8832 if (eval1(&s, &rettv, TRUE) == FAIL)
8833 return FAIL;
8834 if (*s != NUL) /* check for trailing chars after expr */
8835 {
8836 EMSG2(_(e_invexpr2), s);
8837 return FAIL;
8838 }
8839 if (map)
8840 {
8841 /* map(): replace the list item value */
8842 clear_tv(tv);
8843 *tv = rettv;
8844 }
8845 else
8846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008847 int error = FALSE;
8848
Bram Moolenaare9a41262005-01-15 22:18:47 +00008849 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008850 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008851 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008852 /* On type error, nothing has been removed; return FAIL to stop the
8853 * loop. The error message was given by get_tv_number_chk(). */
8854 if (error)
8855 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008856 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008857 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008858 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008859}
8860
8861/*
8862 * "filter()" function
8863 */
8864 static void
8865f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008866 typval_T *argvars;
8867 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008868{
8869 filter_map(argvars, rettv, FALSE);
8870}
8871
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008872/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008873 * "finddir({fname}[, {path}[, {count}]])" function
8874 */
8875 static void
8876f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008877 typval_T *argvars;
8878 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008879{
8880 findfilendir(argvars, rettv, TRUE);
8881}
8882
8883/*
8884 * "findfile({fname}[, {path}[, {count}]])" function
8885 */
8886 static void
8887f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008888 typval_T *argvars;
8889 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008890{
8891 findfilendir(argvars, rettv, FALSE);
8892}
8893
8894/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 * "fnamemodify({fname}, {mods})" function
8896 */
8897 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008898f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008899 typval_T *argvars;
8900 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901{
8902 char_u *fname;
8903 char_u *mods;
8904 int usedlen = 0;
8905 int len;
8906 char_u *fbuf = NULL;
8907 char_u buf[NUMBUFLEN];
8908
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008909 fname = get_tv_string_chk(&argvars[0]);
8910 mods = get_tv_string_buf_chk(&argvars[1], buf);
8911 if (fname == NULL || mods == NULL)
8912 fname = NULL;
8913 else
8914 {
8915 len = (int)STRLEN(fname);
8916 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008919 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008921 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008923 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 vim_free(fbuf);
8925}
8926
Bram Moolenaar33570922005-01-25 22:26:29 +00008927static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928
8929/*
8930 * "foldclosed()" function
8931 */
8932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008933foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008934 typval_T *argvars;
8935 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 int end;
8937{
8938#ifdef FEAT_FOLDING
8939 linenr_T lnum;
8940 linenr_T first, last;
8941
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008942 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8944 {
8945 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8946 {
8947 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008948 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008950 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 return;
8952 }
8953 }
8954#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008955 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956}
8957
8958/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008959 * "foldclosed()" function
8960 */
8961 static void
8962f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008963 typval_T *argvars;
8964 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008965{
8966 foldclosed_both(argvars, rettv, FALSE);
8967}
8968
8969/*
8970 * "foldclosedend()" function
8971 */
8972 static void
8973f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008974 typval_T *argvars;
8975 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008976{
8977 foldclosed_both(argvars, rettv, TRUE);
8978}
8979
8980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 * "foldlevel()" function
8982 */
8983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008984f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008985 typval_T *argvars;
8986 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987{
8988#ifdef FEAT_FOLDING
8989 linenr_T lnum;
8990
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008991 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008993 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 else
8995#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008996 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997}
8998
8999/*
9000 * "foldtext()" function
9001 */
9002/*ARGSUSED*/
9003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009004f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009005 typval_T *argvars;
9006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007{
9008#ifdef FEAT_FOLDING
9009 linenr_T lnum;
9010 char_u *s;
9011 char_u *r;
9012 int len;
9013 char *txt;
9014#endif
9015
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009016 rettv->v_type = VAR_STRING;
9017 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009019 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9020 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9021 <= curbuf->b_ml.ml_line_count
9022 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023 {
9024 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009025 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9026 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 {
9028 if (!linewhite(lnum))
9029 break;
9030 ++lnum;
9031 }
9032
9033 /* Find interesting text in this line. */
9034 s = skipwhite(ml_get(lnum));
9035 /* skip C comment-start */
9036 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009037 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009039 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009040 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009041 {
9042 s = skipwhite(ml_get(lnum + 1));
9043 if (*s == '*')
9044 s = skipwhite(s + 1);
9045 }
9046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 txt = _("+-%s%3ld lines: ");
9048 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009049 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 + 20 /* for %3ld */
9051 + STRLEN(s))); /* concatenated */
9052 if (r != NULL)
9053 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009054 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9055 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9056 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 len = (int)STRLEN(r);
9058 STRCAT(r, s);
9059 /* remove 'foldmarker' and 'commentstring' */
9060 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009061 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 }
9063 }
9064#endif
9065}
9066
9067/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009068 * "foldtextresult(lnum)" function
9069 */
9070/*ARGSUSED*/
9071 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009072f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009073 typval_T *argvars;
9074 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009075{
9076#ifdef FEAT_FOLDING
9077 linenr_T lnum;
9078 char_u *text;
9079 char_u buf[51];
9080 foldinfo_T foldinfo;
9081 int fold_count;
9082#endif
9083
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009084 rettv->v_type = VAR_STRING;
9085 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009086#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009087 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009088 /* treat illegal types and illegal string values for {lnum} the same */
9089 if (lnum < 0)
9090 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009091 fold_count = foldedCount(curwin, lnum, &foldinfo);
9092 if (fold_count > 0)
9093 {
9094 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9095 &foldinfo, buf);
9096 if (text == buf)
9097 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009099 }
9100#endif
9101}
9102
9103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 * "foreground()" function
9105 */
9106/*ARGSUSED*/
9107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009109 typval_T *argvars;
9110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113#ifdef FEAT_GUI
9114 if (gui.in_use)
9115 gui_mch_set_foreground();
9116#else
9117# ifdef WIN32
9118 win32_set_foreground();
9119# endif
9120#endif
9121}
9122
9123/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009124 * "function()" function
9125 */
9126/*ARGSUSED*/
9127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009128f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009129 typval_T *argvars;
9130 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009131{
9132 char_u *s;
9133
Bram Moolenaara7043832005-01-21 11:56:39 +00009134 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009135 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009136 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009137 EMSG2(_(e_invarg2), s);
9138 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009139 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009140 else
9141 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142 rettv->vval.v_string = vim_strsave(s);
9143 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009144 }
9145}
9146
9147/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009148 * "garbagecollect()" function
9149 */
9150/*ARGSUSED*/
9151 static void
9152f_garbagecollect(argvars, rettv)
9153 typval_T *argvars;
9154 typval_T *rettv;
9155{
9156 garbage_collect();
9157}
9158
9159/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009160 * "get()" function
9161 */
9162 static void
9163f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009164 typval_T *argvars;
9165 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009166{
Bram Moolenaar33570922005-01-25 22:26:29 +00009167 listitem_T *li;
9168 list_T *l;
9169 dictitem_T *di;
9170 dict_T *d;
9171 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009172
Bram Moolenaare9a41262005-01-15 22:18:47 +00009173 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009174 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009175 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009176 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009177 int error = FALSE;
9178
9179 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9180 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009181 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009182 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009183 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009184 else if (argvars[0].v_type == VAR_DICT)
9185 {
9186 if ((d = argvars[0].vval.v_dict) != NULL)
9187 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009188 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009189 if (di != NULL)
9190 tv = &di->di_tv;
9191 }
9192 }
9193 else
9194 EMSG2(_(e_listdictarg), "get()");
9195
9196 if (tv == NULL)
9197 {
9198 if (argvars[2].v_type == VAR_UNKNOWN)
9199 rettv->vval.v_number = 0;
9200 else
9201 copy_tv(&argvars[2], rettv);
9202 }
9203 else
9204 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009205}
9206
Bram Moolenaar342337a2005-07-21 21:11:17 +00009207static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009208
9209/*
9210 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009211 * Return a range (from start to end) of lines in rettv from the specified
9212 * buffer.
9213 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009214 */
9215 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009216get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009217 buf_T *buf;
9218 linenr_T start;
9219 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009220 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009221 typval_T *rettv;
9222{
9223 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009224 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009225
Bram Moolenaar342337a2005-07-21 21:11:17 +00009226 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009227 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009228 l = list_alloc();
9229 if (l == NULL)
9230 return;
9231
9232 rettv->vval.v_list = l;
9233 rettv->v_type = VAR_LIST;
9234 ++l->lv_refcount;
9235 }
9236 else
9237 rettv->vval.v_number = 0;
9238
9239 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9240 return;
9241
9242 if (!retlist)
9243 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009244 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9245 p = ml_get_buf(buf, start, FALSE);
9246 else
9247 p = (char_u *)"";
9248
9249 rettv->v_type = VAR_STRING;
9250 rettv->vval.v_string = vim_strsave(p);
9251 }
9252 else
9253 {
9254 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009255 return;
9256
9257 if (start < 1)
9258 start = 1;
9259 if (end > buf->b_ml.ml_line_count)
9260 end = buf->b_ml.ml_line_count;
9261 while (start <= end)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00009262 if (list_append_string(l, ml_get_buf(buf, start++, FALSE)) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009263 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009264 }
9265}
9266
9267/*
9268 * "getbufline()" function
9269 */
9270 static void
9271f_getbufline(argvars, rettv)
9272 typval_T *argvars;
9273 typval_T *rettv;
9274{
9275 linenr_T lnum;
9276 linenr_T end;
9277 buf_T *buf;
9278
9279 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9280 ++emsg_off;
9281 buf = get_buf_tv(&argvars[0]);
9282 --emsg_off;
9283
Bram Moolenaar661b1822005-07-28 22:36:45 +00009284 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009285 if (argvars[2].v_type == VAR_UNKNOWN)
9286 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009287 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009288 end = get_tv_lnum_buf(&argvars[2], buf);
9289
Bram Moolenaar342337a2005-07-21 21:11:17 +00009290 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009291}
9292
Bram Moolenaar0d660222005-01-07 21:51:51 +00009293/*
9294 * "getbufvar()" function
9295 */
9296 static void
9297f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009298 typval_T *argvars;
9299 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009300{
9301 buf_T *buf;
9302 buf_T *save_curbuf;
9303 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009304 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009305
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009306 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9307 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009308 ++emsg_off;
9309 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009310
9311 rettv->v_type = VAR_STRING;
9312 rettv->vval.v_string = NULL;
9313
9314 if (buf != NULL && varname != NULL)
9315 {
9316 if (*varname == '&') /* buffer-local-option */
9317 {
9318 /* set curbuf to be our buf, temporarily */
9319 save_curbuf = curbuf;
9320 curbuf = buf;
9321
9322 get_option_tv(&varname, rettv, TRUE);
9323
9324 /* restore previous notion of curbuf */
9325 curbuf = save_curbuf;
9326 }
9327 else
9328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009329 if (*varname == NUL)
9330 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9331 * scope prefix before the NUL byte is required by
9332 * find_var_in_ht(). */
9333 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009334 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009335 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009336 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009337 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009338 }
9339 }
9340
9341 --emsg_off;
9342}
9343
9344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345 * "getchar()" function
9346 */
9347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009348f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009349 typval_T *argvars;
9350 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009351{
9352 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009353 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354
9355 ++no_mapping;
9356 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009357 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 /* getchar(): blocking wait. */
9359 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009360 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 /* getchar(1): only check if char avail */
9362 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009363 else if (error || vpeekc() == NUL)
9364 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 n = 0;
9366 else
9367 /* getchar(0) and char avail: return char */
9368 n = safe_vgetc();
9369 --no_mapping;
9370 --allow_keys;
9371
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 if (IS_SPECIAL(n) || mod_mask != 0)
9374 {
9375 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9376 int i = 0;
9377
9378 /* Turn a special key into three bytes, plus modifier. */
9379 if (mod_mask != 0)
9380 {
9381 temp[i++] = K_SPECIAL;
9382 temp[i++] = KS_MODIFIER;
9383 temp[i++] = mod_mask;
9384 }
9385 if (IS_SPECIAL(n))
9386 {
9387 temp[i++] = K_SPECIAL;
9388 temp[i++] = K_SECOND(n);
9389 temp[i++] = K_THIRD(n);
9390 }
9391#ifdef FEAT_MBYTE
9392 else if (has_mbyte)
9393 i += (*mb_char2bytes)(n, temp + i);
9394#endif
9395 else
9396 temp[i++] = n;
9397 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009398 rettv->v_type = VAR_STRING;
9399 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 }
9401}
9402
9403/*
9404 * "getcharmod()" function
9405 */
9406/*ARGSUSED*/
9407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009408f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009409 typval_T *argvars;
9410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009412 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413}
9414
9415/*
9416 * "getcmdline()" function
9417 */
9418/*ARGSUSED*/
9419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009420f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009421 typval_T *argvars;
9422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009424 rettv->v_type = VAR_STRING;
9425 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426}
9427
9428/*
9429 * "getcmdpos()" function
9430 */
9431/*ARGSUSED*/
9432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009433f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009434 typval_T *argvars;
9435 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009437 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438}
9439
9440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 * "getcwd()" function
9442 */
9443/*ARGSUSED*/
9444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009445f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009446 typval_T *argvars;
9447 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448{
9449 char_u cwd[MAXPATHL];
9450
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009451 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009453 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 else
9455 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009458 if (rettv->vval.v_string != NULL)
9459 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460#endif
9461 }
9462}
9463
9464/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009465 * "getfontname()" function
9466 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009467/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009469f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009470 typval_T *argvars;
9471 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009472{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009473 rettv->v_type = VAR_STRING;
9474 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009475#ifdef FEAT_GUI
9476 if (gui.in_use)
9477 {
9478 GuiFont font;
9479 char_u *name = NULL;
9480
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009481 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009482 {
9483 /* Get the "Normal" font. Either the name saved by
9484 * hl_set_font_name() or from the font ID. */
9485 font = gui.norm_font;
9486 name = hl_get_font_name();
9487 }
9488 else
9489 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009491 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9492 return;
9493 font = gui_mch_get_font(name, FALSE);
9494 if (font == NOFONT)
9495 return; /* Invalid font name, return empty string. */
9496 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009497 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009498 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009499 gui_mch_free_font(font);
9500 }
9501#endif
9502}
9503
9504/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009505 * "getfperm({fname})" function
9506 */
9507 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009508f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009509 typval_T *argvars;
9510 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009511{
9512 char_u *fname;
9513 struct stat st;
9514 char_u *perm = NULL;
9515 char_u flags[] = "rwx";
9516 int i;
9517
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009518 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009519
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009520 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009521 if (mch_stat((char *)fname, &st) >= 0)
9522 {
9523 perm = vim_strsave((char_u *)"---------");
9524 if (perm != NULL)
9525 {
9526 for (i = 0; i < 9; i++)
9527 {
9528 if (st.st_mode & (1 << (8 - i)))
9529 perm[i] = flags[i % 3];
9530 }
9531 }
9532 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009533 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009534}
9535
9536/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 * "getfsize({fname})" function
9538 */
9539 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009540f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009541 typval_T *argvars;
9542 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543{
9544 char_u *fname;
9545 struct stat st;
9546
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009549 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009550
9551 if (mch_stat((char *)fname, &st) >= 0)
9552 {
9553 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009554 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557 }
9558 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009559 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560}
9561
9562/*
9563 * "getftime({fname})" function
9564 */
9565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009567 typval_T *argvars;
9568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569{
9570 char_u *fname;
9571 struct stat st;
9572
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009573 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574
9575 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009576 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009578 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579}
9580
9581/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009582 * "getftype({fname})" function
9583 */
9584 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009585f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009586 typval_T *argvars;
9587 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009588{
9589 char_u *fname;
9590 struct stat st;
9591 char_u *type = NULL;
9592 char *t;
9593
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009594 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009595
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009596 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009597 if (mch_lstat((char *)fname, &st) >= 0)
9598 {
9599#ifdef S_ISREG
9600 if (S_ISREG(st.st_mode))
9601 t = "file";
9602 else if (S_ISDIR(st.st_mode))
9603 t = "dir";
9604# ifdef S_ISLNK
9605 else if (S_ISLNK(st.st_mode))
9606 t = "link";
9607# endif
9608# ifdef S_ISBLK
9609 else if (S_ISBLK(st.st_mode))
9610 t = "bdev";
9611# endif
9612# ifdef S_ISCHR
9613 else if (S_ISCHR(st.st_mode))
9614 t = "cdev";
9615# endif
9616# ifdef S_ISFIFO
9617 else if (S_ISFIFO(st.st_mode))
9618 t = "fifo";
9619# endif
9620# ifdef S_ISSOCK
9621 else if (S_ISSOCK(st.st_mode))
9622 t = "fifo";
9623# endif
9624 else
9625 t = "other";
9626#else
9627# ifdef S_IFMT
9628 switch (st.st_mode & S_IFMT)
9629 {
9630 case S_IFREG: t = "file"; break;
9631 case S_IFDIR: t = "dir"; break;
9632# ifdef S_IFLNK
9633 case S_IFLNK: t = "link"; break;
9634# endif
9635# ifdef S_IFBLK
9636 case S_IFBLK: t = "bdev"; break;
9637# endif
9638# ifdef S_IFCHR
9639 case S_IFCHR: t = "cdev"; break;
9640# endif
9641# ifdef S_IFIFO
9642 case S_IFIFO: t = "fifo"; break;
9643# endif
9644# ifdef S_IFSOCK
9645 case S_IFSOCK: t = "socket"; break;
9646# endif
9647 default: t = "other";
9648 }
9649# else
9650 if (mch_isdir(fname))
9651 t = "dir";
9652 else
9653 t = "file";
9654# endif
9655#endif
9656 type = vim_strsave((char_u *)t);
9657 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009658 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009659}
9660
9661/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009662 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009663 */
9664 static void
9665f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009666 typval_T *argvars;
9667 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009668{
9669 linenr_T lnum;
9670 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009671 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009672
9673 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009674 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009675 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009676 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009677 retlist = FALSE;
9678 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009679 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009680 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009681 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009682 retlist = TRUE;
9683 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009684
Bram Moolenaar342337a2005-07-21 21:11:17 +00009685 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009686}
9687
9688/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009689 * "getqflist()" function
9690 */
9691/*ARGSUSED*/
9692 static void
9693f_getqflist(argvars, rettv)
9694 typval_T *argvars;
9695 typval_T *rettv;
9696{
9697#ifdef FEAT_QUICKFIX
9698 list_T *l;
9699#endif
9700
9701 rettv->vval.v_number = FALSE;
9702#ifdef FEAT_QUICKFIX
9703 l = list_alloc();
9704 if (l != NULL)
9705 {
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00009706 rettv->vval.v_list = l;
9707 rettv->v_type = VAR_LIST;
9708 ++l->lv_refcount;
9709 (void)get_errorlist(l);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009710 }
9711#endif
9712}
9713
9714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 * "getreg()" function
9716 */
9717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009718f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009719 typval_T *argvars;
9720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721{
9722 char_u *strregname;
9723 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009724 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009725 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009727 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009728 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009729 strregname = get_tv_string_chk(&argvars[0]);
9730 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009731 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009732 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009735 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 regname = (strregname == NULL ? '"' : *strregname);
9737 if (regname == 0)
9738 regname = '"';
9739
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009740 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009741 rettv->vval.v_string = error ? NULL :
9742 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743}
9744
9745/*
9746 * "getregtype()" function
9747 */
9748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009749f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009750 typval_T *argvars;
9751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752{
9753 char_u *strregname;
9754 int regname;
9755 char_u buf[NUMBUFLEN + 2];
9756 long reglen = 0;
9757
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009758 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009759 {
9760 strregname = get_tv_string_chk(&argvars[0]);
9761 if (strregname == NULL) /* type error; errmsg already given */
9762 {
9763 rettv->v_type = VAR_STRING;
9764 rettv->vval.v_string = NULL;
9765 return;
9766 }
9767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 else
9769 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009770 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771
9772 regname = (strregname == NULL ? '"' : *strregname);
9773 if (regname == 0)
9774 regname = '"';
9775
9776 buf[0] = NUL;
9777 buf[1] = NUL;
9778 switch (get_reg_type(regname, &reglen))
9779 {
9780 case MLINE: buf[0] = 'V'; break;
9781 case MCHAR: buf[0] = 'v'; break;
9782#ifdef FEAT_VISUAL
9783 case MBLOCK:
9784 buf[0] = Ctrl_V;
9785 sprintf((char *)buf + 1, "%ld", reglen + 1);
9786 break;
9787#endif
9788 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009789 rettv->v_type = VAR_STRING;
9790 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791}
9792
9793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 * "getwinposx()" function
9795 */
9796/*ARGSUSED*/
9797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009798f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009799 typval_T *argvars;
9800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009802 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803#ifdef FEAT_GUI
9804 if (gui.in_use)
9805 {
9806 int x, y;
9807
9808 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 }
9811#endif
9812}
9813
9814/*
9815 * "getwinposy()" function
9816 */
9817/*ARGSUSED*/
9818 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009819f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009820 typval_T *argvars;
9821 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009823 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824#ifdef FEAT_GUI
9825 if (gui.in_use)
9826 {
9827 int x, y;
9828
9829 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009830 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009831 }
9832#endif
9833}
9834
Bram Moolenaara40058a2005-07-11 22:42:07 +00009835static win_T *find_win_by_nr __ARGS((typval_T *vp));
9836
9837 static win_T *
9838find_win_by_nr(vp)
9839 typval_T *vp;
9840{
9841#ifdef FEAT_WINDOWS
9842 win_T *wp;
9843#endif
9844 int nr;
9845
9846 nr = get_tv_number_chk(vp, NULL);
9847
9848#ifdef FEAT_WINDOWS
9849 if (nr < 0)
9850 return NULL;
9851 if (nr == 0)
9852 return curwin;
9853
9854 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9855 if (--nr <= 0)
9856 break;
9857 return wp;
9858#else
9859 if (nr == 0 || nr == 1)
9860 return curwin;
9861 return NULL;
9862#endif
9863}
9864
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865/*
9866 * "getwinvar()" function
9867 */
9868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009869f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009870 typval_T *argvars;
9871 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872{
9873 win_T *win, *oldcurwin;
9874 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009875 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009878 varname = get_tv_string_chk(&argvars[1]);
9879 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009881 rettv->v_type = VAR_STRING;
9882 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883
9884 if (win != NULL && varname != NULL)
9885 {
9886 if (*varname == '&') /* window-local-option */
9887 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009888 /* Set curwin to be our win, temporarily. Also set curbuf, so
9889 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 oldcurwin = curwin;
9891 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009892 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009894 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895
9896 /* restore previous notion of curwin */
9897 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009898 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899 }
9900 else
9901 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009902 if (*varname == NUL)
9903 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9904 * scope prefix before the NUL byte is required by
9905 * find_var_in_ht(). */
9906 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009908 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009910 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 }
9912 }
9913
9914 --emsg_off;
9915}
9916
9917/*
9918 * "glob()" function
9919 */
9920 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009921f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009922 typval_T *argvars;
9923 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924{
9925 expand_T xpc;
9926
9927 ExpandInit(&xpc);
9928 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009929 rettv->v_type = VAR_STRING;
9930 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9932 ExpandCleanup(&xpc);
9933}
9934
9935/*
9936 * "globpath()" function
9937 */
9938 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009939f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009940 typval_T *argvars;
9941 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942{
9943 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009944 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009946 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009947 if (file == NULL)
9948 rettv->vval.v_string = NULL;
9949 else
9950 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009951}
9952
9953/*
9954 * "has()" function
9955 */
9956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009957f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009958 typval_T *argvars;
9959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009960{
9961 int i;
9962 char_u *name;
9963 int n = FALSE;
9964 static char *(has_list[]) =
9965 {
9966#ifdef AMIGA
9967 "amiga",
9968# ifdef FEAT_ARP
9969 "arp",
9970# endif
9971#endif
9972#ifdef __BEOS__
9973 "beos",
9974#endif
9975#ifdef MSDOS
9976# ifdef DJGPP
9977 "dos32",
9978# else
9979 "dos16",
9980# endif
9981#endif
9982#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9983 "mac",
9984#endif
9985#if defined(MACOS_X_UNIX)
9986 "macunix",
9987#endif
9988#ifdef OS2
9989 "os2",
9990#endif
9991#ifdef __QNX__
9992 "qnx",
9993#endif
9994#ifdef RISCOS
9995 "riscos",
9996#endif
9997#ifdef UNIX
9998 "unix",
9999#endif
10000#ifdef VMS
10001 "vms",
10002#endif
10003#ifdef WIN16
10004 "win16",
10005#endif
10006#ifdef WIN32
10007 "win32",
10008#endif
10009#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10010 "win32unix",
10011#endif
10012#ifdef WIN64
10013 "win64",
10014#endif
10015#ifdef EBCDIC
10016 "ebcdic",
10017#endif
10018#ifndef CASE_INSENSITIVE_FILENAME
10019 "fname_case",
10020#endif
10021#ifdef FEAT_ARABIC
10022 "arabic",
10023#endif
10024#ifdef FEAT_AUTOCMD
10025 "autocmd",
10026#endif
10027#ifdef FEAT_BEVAL
10028 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010029# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10030 "balloon_multiline",
10031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032#endif
10033#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10034 "builtin_terms",
10035# ifdef ALL_BUILTIN_TCAPS
10036 "all_builtin_terms",
10037# endif
10038#endif
10039#ifdef FEAT_BYTEOFF
10040 "byte_offset",
10041#endif
10042#ifdef FEAT_CINDENT
10043 "cindent",
10044#endif
10045#ifdef FEAT_CLIENTSERVER
10046 "clientserver",
10047#endif
10048#ifdef FEAT_CLIPBOARD
10049 "clipboard",
10050#endif
10051#ifdef FEAT_CMDL_COMPL
10052 "cmdline_compl",
10053#endif
10054#ifdef FEAT_CMDHIST
10055 "cmdline_hist",
10056#endif
10057#ifdef FEAT_COMMENTS
10058 "comments",
10059#endif
10060#ifdef FEAT_CRYPT
10061 "cryptv",
10062#endif
10063#ifdef FEAT_CSCOPE
10064 "cscope",
10065#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010066#ifdef CURSOR_SHAPE
10067 "cursorshape",
10068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069#ifdef DEBUG
10070 "debug",
10071#endif
10072#ifdef FEAT_CON_DIALOG
10073 "dialog_con",
10074#endif
10075#ifdef FEAT_GUI_DIALOG
10076 "dialog_gui",
10077#endif
10078#ifdef FEAT_DIFF
10079 "diff",
10080#endif
10081#ifdef FEAT_DIGRAPHS
10082 "digraphs",
10083#endif
10084#ifdef FEAT_DND
10085 "dnd",
10086#endif
10087#ifdef FEAT_EMACS_TAGS
10088 "emacs_tags",
10089#endif
10090 "eval", /* always present, of course! */
10091#ifdef FEAT_EX_EXTRA
10092 "ex_extra",
10093#endif
10094#ifdef FEAT_SEARCH_EXTRA
10095 "extra_search",
10096#endif
10097#ifdef FEAT_FKMAP
10098 "farsi",
10099#endif
10100#ifdef FEAT_SEARCHPATH
10101 "file_in_path",
10102#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010103#if defined(UNIX) && !defined(USE_SYSTEM)
10104 "filterpipe",
10105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106#ifdef FEAT_FIND_ID
10107 "find_in_path",
10108#endif
10109#ifdef FEAT_FOLDING
10110 "folding",
10111#endif
10112#ifdef FEAT_FOOTER
10113 "footer",
10114#endif
10115#if !defined(USE_SYSTEM) && defined(UNIX)
10116 "fork",
10117#endif
10118#ifdef FEAT_GETTEXT
10119 "gettext",
10120#endif
10121#ifdef FEAT_GUI
10122 "gui",
10123#endif
10124#ifdef FEAT_GUI_ATHENA
10125# ifdef FEAT_GUI_NEXTAW
10126 "gui_neXtaw",
10127# else
10128 "gui_athena",
10129# endif
10130#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +000010131#ifdef FEAT_GUI_KDE
10132 "gui_kde",
10133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134#ifdef FEAT_GUI_GTK
10135 "gui_gtk",
10136# ifdef HAVE_GTK2
10137 "gui_gtk2",
10138# endif
10139#endif
10140#ifdef FEAT_GUI_MAC
10141 "gui_mac",
10142#endif
10143#ifdef FEAT_GUI_MOTIF
10144 "gui_motif",
10145#endif
10146#ifdef FEAT_GUI_PHOTON
10147 "gui_photon",
10148#endif
10149#ifdef FEAT_GUI_W16
10150 "gui_win16",
10151#endif
10152#ifdef FEAT_GUI_W32
10153 "gui_win32",
10154#endif
10155#ifdef FEAT_HANGULIN
10156 "hangul_input",
10157#endif
10158#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10159 "iconv",
10160#endif
10161#ifdef FEAT_INS_EXPAND
10162 "insert_expand",
10163#endif
10164#ifdef FEAT_JUMPLIST
10165 "jumplist",
10166#endif
10167#ifdef FEAT_KEYMAP
10168 "keymap",
10169#endif
10170#ifdef FEAT_LANGMAP
10171 "langmap",
10172#endif
10173#ifdef FEAT_LIBCALL
10174 "libcall",
10175#endif
10176#ifdef FEAT_LINEBREAK
10177 "linebreak",
10178#endif
10179#ifdef FEAT_LISP
10180 "lispindent",
10181#endif
10182#ifdef FEAT_LISTCMDS
10183 "listcmds",
10184#endif
10185#ifdef FEAT_LOCALMAP
10186 "localmap",
10187#endif
10188#ifdef FEAT_MENU
10189 "menu",
10190#endif
10191#ifdef FEAT_SESSION
10192 "mksession",
10193#endif
10194#ifdef FEAT_MODIFY_FNAME
10195 "modify_fname",
10196#endif
10197#ifdef FEAT_MOUSE
10198 "mouse",
10199#endif
10200#ifdef FEAT_MOUSESHAPE
10201 "mouseshape",
10202#endif
10203#if defined(UNIX) || defined(VMS)
10204# ifdef FEAT_MOUSE_DEC
10205 "mouse_dec",
10206# endif
10207# ifdef FEAT_MOUSE_GPM
10208 "mouse_gpm",
10209# endif
10210# ifdef FEAT_MOUSE_JSB
10211 "mouse_jsbterm",
10212# endif
10213# ifdef FEAT_MOUSE_NET
10214 "mouse_netterm",
10215# endif
10216# ifdef FEAT_MOUSE_PTERM
10217 "mouse_pterm",
10218# endif
10219# ifdef FEAT_MOUSE_XTERM
10220 "mouse_xterm",
10221# endif
10222#endif
10223#ifdef FEAT_MBYTE
10224 "multi_byte",
10225#endif
10226#ifdef FEAT_MBYTE_IME
10227 "multi_byte_ime",
10228#endif
10229#ifdef FEAT_MULTI_LANG
10230 "multi_lang",
10231#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010232#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010233#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010234 "mzscheme",
10235#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237#ifdef FEAT_OLE
10238 "ole",
10239#endif
10240#ifdef FEAT_OSFILETYPE
10241 "osfiletype",
10242#endif
10243#ifdef FEAT_PATH_EXTRA
10244 "path_extra",
10245#endif
10246#ifdef FEAT_PERL
10247#ifndef DYNAMIC_PERL
10248 "perl",
10249#endif
10250#endif
10251#ifdef FEAT_PYTHON
10252#ifndef DYNAMIC_PYTHON
10253 "python",
10254#endif
10255#endif
10256#ifdef FEAT_POSTSCRIPT
10257 "postscript",
10258#endif
10259#ifdef FEAT_PRINTER
10260 "printer",
10261#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010262#ifdef FEAT_PROFILE
10263 "profile",
10264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265#ifdef FEAT_QUICKFIX
10266 "quickfix",
10267#endif
10268#ifdef FEAT_RIGHTLEFT
10269 "rightleft",
10270#endif
10271#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10272 "ruby",
10273#endif
10274#ifdef FEAT_SCROLLBIND
10275 "scrollbind",
10276#endif
10277#ifdef FEAT_CMDL_INFO
10278 "showcmd",
10279 "cmdline_info",
10280#endif
10281#ifdef FEAT_SIGNS
10282 "signs",
10283#endif
10284#ifdef FEAT_SMARTINDENT
10285 "smartindent",
10286#endif
10287#ifdef FEAT_SNIFF
10288 "sniff",
10289#endif
10290#ifdef FEAT_STL_OPT
10291 "statusline",
10292#endif
10293#ifdef FEAT_SUN_WORKSHOP
10294 "sun_workshop",
10295#endif
10296#ifdef FEAT_NETBEANS_INTG
10297 "netbeans_intg",
10298#endif
10299#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010300 "spell",
10301#endif
10302#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 "syntax",
10304#endif
10305#if defined(USE_SYSTEM) || !defined(UNIX)
10306 "system",
10307#endif
10308#ifdef FEAT_TAG_BINS
10309 "tag_binary",
10310#endif
10311#ifdef FEAT_TAG_OLDSTATIC
10312 "tag_old_static",
10313#endif
10314#ifdef FEAT_TAG_ANYWHITE
10315 "tag_any_white",
10316#endif
10317#ifdef FEAT_TCL
10318# ifndef DYNAMIC_TCL
10319 "tcl",
10320# endif
10321#endif
10322#ifdef TERMINFO
10323 "terminfo",
10324#endif
10325#ifdef FEAT_TERMRESPONSE
10326 "termresponse",
10327#endif
10328#ifdef FEAT_TEXTOBJ
10329 "textobjects",
10330#endif
10331#ifdef HAVE_TGETENT
10332 "tgetent",
10333#endif
10334#ifdef FEAT_TITLE
10335 "title",
10336#endif
10337#ifdef FEAT_TOOLBAR
10338 "toolbar",
10339#endif
10340#ifdef FEAT_USR_CMDS
10341 "user-commands", /* was accidentally included in 5.4 */
10342 "user_commands",
10343#endif
10344#ifdef FEAT_VIMINFO
10345 "viminfo",
10346#endif
10347#ifdef FEAT_VERTSPLIT
10348 "vertsplit",
10349#endif
10350#ifdef FEAT_VIRTUALEDIT
10351 "virtualedit",
10352#endif
10353#ifdef FEAT_VISUAL
10354 "visual",
10355#endif
10356#ifdef FEAT_VISUALEXTRA
10357 "visualextra",
10358#endif
10359#ifdef FEAT_VREPLACE
10360 "vreplace",
10361#endif
10362#ifdef FEAT_WILDIGN
10363 "wildignore",
10364#endif
10365#ifdef FEAT_WILDMENU
10366 "wildmenu",
10367#endif
10368#ifdef FEAT_WINDOWS
10369 "windows",
10370#endif
10371#ifdef FEAT_WAK
10372 "winaltkeys",
10373#endif
10374#ifdef FEAT_WRITEBACKUP
10375 "writebackup",
10376#endif
10377#ifdef FEAT_XIM
10378 "xim",
10379#endif
10380#ifdef FEAT_XFONTSET
10381 "xfontset",
10382#endif
10383#ifdef USE_XSMP
10384 "xsmp",
10385#endif
10386#ifdef USE_XSMP_INTERACT
10387 "xsmp_interact",
10388#endif
10389#ifdef FEAT_XCLIPBOARD
10390 "xterm_clipboard",
10391#endif
10392#ifdef FEAT_XTERM_SAVE
10393 "xterm_save",
10394#endif
10395#if defined(UNIX) && defined(FEAT_X11)
10396 "X11",
10397#endif
10398 NULL
10399 };
10400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010401 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 for (i = 0; has_list[i] != NULL; ++i)
10403 if (STRICMP(name, has_list[i]) == 0)
10404 {
10405 n = TRUE;
10406 break;
10407 }
10408
10409 if (n == FALSE)
10410 {
10411 if (STRNICMP(name, "patch", 5) == 0)
10412 n = has_patch(atoi((char *)name + 5));
10413 else if (STRICMP(name, "vim_starting") == 0)
10414 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010415#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10416 else if (STRICMP(name, "balloon_multiline") == 0)
10417 n = multiline_balloon_available();
10418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419#ifdef DYNAMIC_TCL
10420 else if (STRICMP(name, "tcl") == 0)
10421 n = tcl_enabled(FALSE);
10422#endif
10423#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10424 else if (STRICMP(name, "iconv") == 0)
10425 n = iconv_enabled(FALSE);
10426#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010427#ifdef DYNAMIC_MZSCHEME
10428 else if (STRICMP(name, "mzscheme") == 0)
10429 n = mzscheme_enabled(FALSE);
10430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431#ifdef DYNAMIC_RUBY
10432 else if (STRICMP(name, "ruby") == 0)
10433 n = ruby_enabled(FALSE);
10434#endif
10435#ifdef DYNAMIC_PYTHON
10436 else if (STRICMP(name, "python") == 0)
10437 n = python_enabled(FALSE);
10438#endif
10439#ifdef DYNAMIC_PERL
10440 else if (STRICMP(name, "perl") == 0)
10441 n = perl_enabled(FALSE);
10442#endif
10443#ifdef FEAT_GUI
10444 else if (STRICMP(name, "gui_running") == 0)
10445 n = (gui.in_use || gui.starting);
10446# ifdef FEAT_GUI_W32
10447 else if (STRICMP(name, "gui_win32s") == 0)
10448 n = gui_is_win32s();
10449# endif
10450# ifdef FEAT_BROWSE
10451 else if (STRICMP(name, "browse") == 0)
10452 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10453# endif
10454#endif
10455#ifdef FEAT_SYN_HL
10456 else if (STRICMP(name, "syntax_items") == 0)
10457 n = syntax_present(curbuf);
10458#endif
10459#if defined(WIN3264)
10460 else if (STRICMP(name, "win95") == 0)
10461 n = mch_windows95();
10462#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010463#ifdef FEAT_NETBEANS_INTG
10464 else if (STRICMP(name, "netbeans_enabled") == 0)
10465 n = usingNetbeans;
10466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 }
10468
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010469 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470}
10471
10472/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010473 * "has_key()" function
10474 */
10475 static void
10476f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010477 typval_T *argvars;
10478 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010479{
10480 rettv->vval.v_number = 0;
10481 if (argvars[0].v_type != VAR_DICT)
10482 {
10483 EMSG(_(e_dictreq));
10484 return;
10485 }
10486 if (argvars[0].vval.v_dict == NULL)
10487 return;
10488
10489 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010490 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010491}
10492
10493/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 * "hasmapto()" function
10495 */
10496 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010497f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010498 typval_T *argvars;
10499 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500{
10501 char_u *name;
10502 char_u *mode;
10503 char_u buf[NUMBUFLEN];
10504
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010505 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010506 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 mode = (char_u *)"nvo";
10508 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010509 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510
10511 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010512 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010514 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515}
10516
10517/*
10518 * "histadd()" function
10519 */
10520/*ARGSUSED*/
10521 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010522f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010523 typval_T *argvars;
10524 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525{
10526#ifdef FEAT_CMDHIST
10527 int histype;
10528 char_u *str;
10529 char_u buf[NUMBUFLEN];
10530#endif
10531
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010532 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010533 if (check_restricted() || check_secure())
10534 return;
10535#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010536 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10537 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 if (histype >= 0)
10539 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010540 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541 if (*str != NUL)
10542 {
10543 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010544 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 return;
10546 }
10547 }
10548#endif
10549}
10550
10551/*
10552 * "histdel()" function
10553 */
10554/*ARGSUSED*/
10555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010556f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010557 typval_T *argvars;
10558 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559{
10560#ifdef FEAT_CMDHIST
10561 int n;
10562 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010563 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010564
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010565 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10566 if (str == NULL)
10567 n = 0;
10568 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010570 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010571 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010573 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010574 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575 else
10576 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010577 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010578 get_tv_string_buf(&argvars[1], buf));
10579 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010581 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582#endif
10583}
10584
10585/*
10586 * "histget()" function
10587 */
10588/*ARGSUSED*/
10589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010590f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010591 typval_T *argvars;
10592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593{
10594#ifdef FEAT_CMDHIST
10595 int type;
10596 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010597 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010599 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10600 if (str == NULL)
10601 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010603 {
10604 type = get_histtype(str);
10605 if (argvars[1].v_type == VAR_UNKNOWN)
10606 idx = get_history_idx(type);
10607 else
10608 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10609 /* -1 on type error */
10610 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010613 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010615 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616}
10617
10618/*
10619 * "histnr()" function
10620 */
10621/*ARGSUSED*/
10622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010623f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010624 typval_T *argvars;
10625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010626{
10627 int i;
10628
10629#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010630 char_u *history = get_tv_string_chk(&argvars[0]);
10631
10632 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633 if (i >= HIST_CMD && i < HIST_COUNT)
10634 i = get_history_idx(i);
10635 else
10636#endif
10637 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010638 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639}
10640
10641/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 * "highlightID(name)" function
10643 */
10644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010645f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010646 typval_T *argvars;
10647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010649 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650}
10651
10652/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010653 * "highlight_exists()" function
10654 */
10655 static void
10656f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010657 typval_T *argvars;
10658 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010659{
10660 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10661}
10662
10663/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010664 * "hostname()" function
10665 */
10666/*ARGSUSED*/
10667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010668f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010669 typval_T *argvars;
10670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671{
10672 char_u hostname[256];
10673
10674 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010675 rettv->v_type = VAR_STRING;
10676 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010677}
10678
10679/*
10680 * iconv() function
10681 */
10682/*ARGSUSED*/
10683 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010684f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010685 typval_T *argvars;
10686 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687{
10688#ifdef FEAT_MBYTE
10689 char_u buf1[NUMBUFLEN];
10690 char_u buf2[NUMBUFLEN];
10691 char_u *from, *to, *str;
10692 vimconv_T vimconv;
10693#endif
10694
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695 rettv->v_type = VAR_STRING;
10696 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697
10698#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010699 str = get_tv_string(&argvars[0]);
10700 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10701 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702 vimconv.vc_type = CONV_NONE;
10703 convert_setup(&vimconv, from, to);
10704
10705 /* If the encodings are equal, no conversion needed. */
10706 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010707 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010709 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710
10711 convert_setup(&vimconv, NULL, NULL);
10712 vim_free(from);
10713 vim_free(to);
10714#endif
10715}
10716
10717/*
10718 * "indent()" function
10719 */
10720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010721f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010722 typval_T *argvars;
10723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724{
10725 linenr_T lnum;
10726
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010727 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010729 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010731 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732}
10733
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010734/*
10735 * "index()" function
10736 */
10737 static void
10738f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010739 typval_T *argvars;
10740 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010741{
Bram Moolenaar33570922005-01-25 22:26:29 +000010742 list_T *l;
10743 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010744 long idx = 0;
10745 int ic = FALSE;
10746
10747 rettv->vval.v_number = -1;
10748 if (argvars[0].v_type != VAR_LIST)
10749 {
10750 EMSG(_(e_listreq));
10751 return;
10752 }
10753 l = argvars[0].vval.v_list;
10754 if (l != NULL)
10755 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010756 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010757 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010758 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010759 int error = FALSE;
10760
Bram Moolenaar758711c2005-02-02 23:11:38 +000010761 /* Start at specified item. Use the cached index that list_find()
10762 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010763 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010764 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010765 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010766 ic = get_tv_number_chk(&argvars[3], &error);
10767 if (error)
10768 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010769 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010770
Bram Moolenaar758711c2005-02-02 23:11:38 +000010771 for ( ; item != NULL; item = item->li_next, ++idx)
10772 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010773 {
10774 rettv->vval.v_number = idx;
10775 break;
10776 }
10777 }
10778}
10779
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780static int inputsecret_flag = 0;
10781
10782/*
10783 * "input()" function
10784 * Also handles inputsecret() when inputsecret is set.
10785 */
10786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010787f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010788 typval_T *argvars;
10789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010791 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792 char_u *p = NULL;
10793 int c;
10794 char_u buf[NUMBUFLEN];
10795 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010796 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010798 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799
10800#ifdef NO_CONSOLE_INPUT
10801 /* While starting up, there is no place to enter text. */
10802 if (no_console_input())
10803 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010804 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805 return;
10806 }
10807#endif
10808
10809 cmd_silent = FALSE; /* Want to see the prompt. */
10810 if (prompt != NULL)
10811 {
10812 /* Only the part of the message after the last NL is considered as
10813 * prompt for the command line */
10814 p = vim_strrchr(prompt, '\n');
10815 if (p == NULL)
10816 p = prompt;
10817 else
10818 {
10819 ++p;
10820 c = *p;
10821 *p = NUL;
10822 msg_start();
10823 msg_clr_eos();
10824 msg_puts_attr(prompt, echo_attr);
10825 msg_didout = FALSE;
10826 msg_starthere();
10827 *p = c;
10828 }
10829 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010831 if (argvars[1].v_type != VAR_UNKNOWN)
10832 {
10833 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10834 if (defstr != NULL)
10835 stuffReadbuffSpec(defstr);
10836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010838 if (defstr != NULL)
10839 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
10841
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010842 /* since the user typed this, no need to wait for return */
10843 need_wait_return = FALSE;
10844 msg_didout = FALSE;
10845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 cmd_silent = cmd_silent_save;
10847}
10848
10849/*
10850 * "inputdialog()" function
10851 */
10852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010853f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010854 typval_T *argvars;
10855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856{
10857#if defined(FEAT_GUI_TEXTDIALOG)
10858 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10859 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10860 {
10861 char_u *message;
10862 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010863 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010864
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010865 message = get_tv_string_chk(&argvars[0]);
10866 if (argvars[1].v_type != VAR_UNKNOWN
10867 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000010868 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869 else
10870 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010871 if (message != NULL && defstr != NULL
10872 && do_dialog(VIM_QUESTION, NULL, message,
10873 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010874 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 else
10876 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010877 if (message != NULL && defstr != NULL
10878 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010879 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010880 rettv->vval.v_string = vim_strsave(
10881 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010883 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010885 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010886 }
10887 else
10888#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010889 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890}
10891
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000010892/*
10893 * "inputlist()" function
10894 */
10895 static void
10896f_inputlist(argvars, rettv)
10897 typval_T *argvars;
10898 typval_T *rettv;
10899{
10900 listitem_T *li;
10901 int selected;
10902 int mouse_used;
10903
10904 rettv->vval.v_number = 0;
10905#ifdef NO_CONSOLE_INPUT
10906 /* While starting up, there is no place to enter text. */
10907 if (no_console_input())
10908 return;
10909#endif
10910 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
10911 {
10912 EMSG2(_(e_listarg), "inputlist()");
10913 return;
10914 }
10915
10916 msg_start();
10917 lines_left = Rows; /* avoid more prompt */
10918 msg_scroll = TRUE;
10919 msg_clr_eos();
10920
10921 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
10922 {
10923 msg_puts(get_tv_string(&li->li_tv));
10924 msg_putchar('\n');
10925 }
10926
10927 /* Ask for choice. */
10928 selected = prompt_for_number(&mouse_used);
10929 if (mouse_used)
10930 selected -= lines_left;
10931
10932 rettv->vval.v_number = selected;
10933}
10934
10935
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10937
10938/*
10939 * "inputrestore()" function
10940 */
10941/*ARGSUSED*/
10942 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010943f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010944 typval_T *argvars;
10945 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946{
10947 if (ga_userinput.ga_len > 0)
10948 {
10949 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010950 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10951 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010952 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953 }
10954 else if (p_verbose > 1)
10955 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000010956 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010957 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 }
10959}
10960
10961/*
10962 * "inputsave()" function
10963 */
10964/*ARGSUSED*/
10965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010966f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010967 typval_T *argvars;
10968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969{
10970 /* Add an entry to the stack of typehead storage. */
10971 if (ga_grow(&ga_userinput, 1) == OK)
10972 {
10973 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10974 + ga_userinput.ga_len);
10975 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010976 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977 }
10978 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010979 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010980}
10981
10982/*
10983 * "inputsecret()" function
10984 */
10985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010986f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010987 typval_T *argvars;
10988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989{
10990 ++cmdline_star;
10991 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010992 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993 --cmdline_star;
10994 --inputsecret_flag;
10995}
10996
10997/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010998 * "insert()" function
10999 */
11000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011001f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011002 typval_T *argvars;
11003 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011004{
11005 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011006 listitem_T *item;
11007 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011008 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011009
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011010 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011011 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011012 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011013 else if ((l = argvars[0].vval.v_list) != NULL
11014 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011015 {
11016 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011017 before = get_tv_number_chk(&argvars[2], &error);
11018 if (error)
11019 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011020
Bram Moolenaar758711c2005-02-02 23:11:38 +000011021 if (before == l->lv_len)
11022 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011023 else
11024 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011025 item = list_find(l, before);
11026 if (item == NULL)
11027 {
11028 EMSGN(_(e_listidx), before);
11029 l = NULL;
11030 }
11031 }
11032 if (l != NULL)
11033 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011034 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011035 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011036 }
11037 }
11038}
11039
11040/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041 * "isdirectory()" function
11042 */
11043 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011045 typval_T *argvars;
11046 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011048 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049}
11050
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011051/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011052 * "islocked()" function
11053 */
11054 static void
11055f_islocked(argvars, rettv)
11056 typval_T *argvars;
11057 typval_T *rettv;
11058{
11059 lval_T lv;
11060 char_u *end;
11061 dictitem_T *di;
11062
11063 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011064 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11065 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011066 if (end != NULL && lv.ll_name != NULL)
11067 {
11068 if (*end != NUL)
11069 EMSG(_(e_trailing));
11070 else
11071 {
11072 if (lv.ll_tv == NULL)
11073 {
11074 if (check_changedtick(lv.ll_name))
11075 rettv->vval.v_number = 1; /* always locked */
11076 else
11077 {
11078 di = find_var(lv.ll_name, NULL);
11079 if (di != NULL)
11080 {
11081 /* Consider a variable locked when:
11082 * 1. the variable itself is locked
11083 * 2. the value of the variable is locked.
11084 * 3. the List or Dict value is locked.
11085 */
11086 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11087 || tv_islocked(&di->di_tv));
11088 }
11089 }
11090 }
11091 else if (lv.ll_range)
11092 EMSG(_("E745: Range not allowed"));
11093 else if (lv.ll_newkey != NULL)
11094 EMSG2(_(e_dictkey), lv.ll_newkey);
11095 else if (lv.ll_list != NULL)
11096 /* List item. */
11097 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11098 else
11099 /* Dictionary item. */
11100 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11101 }
11102 }
11103
11104 clear_lval(&lv);
11105}
11106
Bram Moolenaar33570922005-01-25 22:26:29 +000011107static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011108
11109/*
11110 * Turn a dict into a list:
11111 * "what" == 0: list of keys
11112 * "what" == 1: list of values
11113 * "what" == 2: list of items
11114 */
11115 static void
11116dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011117 typval_T *argvars;
11118 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011119 int what;
11120{
Bram Moolenaar33570922005-01-25 22:26:29 +000011121 list_T *l;
11122 list_T *l2;
11123 dictitem_T *di;
11124 hashitem_T *hi;
11125 listitem_T *li;
11126 listitem_T *li2;
11127 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011128 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011129
11130 rettv->vval.v_number = 0;
11131 if (argvars[0].v_type != VAR_DICT)
11132 {
11133 EMSG(_(e_dictreq));
11134 return;
11135 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011136 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011137 return;
11138
11139 l = list_alloc();
11140 if (l == NULL)
11141 return;
11142 rettv->v_type = VAR_LIST;
11143 rettv->vval.v_list = l;
11144 ++l->lv_refcount;
11145
Bram Moolenaar33570922005-01-25 22:26:29 +000011146 todo = d->dv_hashtab.ht_used;
11147 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011148 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011149 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011150 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011151 --todo;
11152 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011153
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011154 li = listitem_alloc();
11155 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011156 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011157 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011158
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011159 if (what == 0)
11160 {
11161 /* keys() */
11162 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011163 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011164 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11165 }
11166 else if (what == 1)
11167 {
11168 /* values() */
11169 copy_tv(&di->di_tv, &li->li_tv);
11170 }
11171 else
11172 {
11173 /* items() */
11174 l2 = list_alloc();
11175 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011176 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011177 li->li_tv.vval.v_list = l2;
11178 if (l2 == NULL)
11179 break;
11180 ++l2->lv_refcount;
11181
11182 li2 = listitem_alloc();
11183 if (li2 == NULL)
11184 break;
11185 list_append(l2, li2);
11186 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011187 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011188 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11189
11190 li2 = listitem_alloc();
11191 if (li2 == NULL)
11192 break;
11193 list_append(l2, li2);
11194 copy_tv(&di->di_tv, &li2->li_tv);
11195 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011196 }
11197 }
11198}
11199
11200/*
11201 * "items(dict)" function
11202 */
11203 static void
11204f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011205 typval_T *argvars;
11206 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011207{
11208 dict_list(argvars, rettv, 2);
11209}
11210
Bram Moolenaar071d4272004-06-13 20:20:40 +000011211/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011212 * "join()" function
11213 */
11214 static void
11215f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011216 typval_T *argvars;
11217 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011218{
11219 garray_T ga;
11220 char_u *sep;
11221
11222 rettv->vval.v_number = 0;
11223 if (argvars[0].v_type != VAR_LIST)
11224 {
11225 EMSG(_(e_listreq));
11226 return;
11227 }
11228 if (argvars[0].vval.v_list == NULL)
11229 return;
11230 if (argvars[1].v_type == VAR_UNKNOWN)
11231 sep = (char_u *)" ";
11232 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011233 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011234
11235 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011236
11237 if (sep != NULL)
11238 {
11239 ga_init2(&ga, (int)sizeof(char), 80);
11240 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
11241 ga_append(&ga, NUL);
11242 rettv->vval.v_string = (char_u *)ga.ga_data;
11243 }
11244 else
11245 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011246}
11247
11248/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011249 * "keys()" function
11250 */
11251 static void
11252f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011253 typval_T *argvars;
11254 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011255{
11256 dict_list(argvars, rettv, 0);
11257}
11258
11259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 * "last_buffer_nr()" function.
11261 */
11262/*ARGSUSED*/
11263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011264f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011265 typval_T *argvars;
11266 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011267{
11268 int n = 0;
11269 buf_T *buf;
11270
11271 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11272 if (n < buf->b_fnum)
11273 n = buf->b_fnum;
11274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011276}
11277
11278/*
11279 * "len()" function
11280 */
11281 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011282f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011283 typval_T *argvars;
11284 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011285{
11286 switch (argvars[0].v_type)
11287 {
11288 case VAR_STRING:
11289 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011290 rettv->vval.v_number = (varnumber_T)STRLEN(
11291 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011292 break;
11293 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011294 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011295 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011296 case VAR_DICT:
11297 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11298 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011299 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011300 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011301 break;
11302 }
11303}
11304
Bram Moolenaar33570922005-01-25 22:26:29 +000011305static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011306
11307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011308libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011309 typval_T *argvars;
11310 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011311 int type;
11312{
11313#ifdef FEAT_LIBCALL
11314 char_u *string_in;
11315 char_u **string_result;
11316 int nr_result;
11317#endif
11318
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011319 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011320 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011321 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011322 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011323 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011324
11325 if (check_restricted() || check_secure())
11326 return;
11327
11328#ifdef FEAT_LIBCALL
11329 /* The first two args must be strings, otherwise its meaningless */
11330 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11331 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011332 string_in = NULL;
11333 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011334 string_in = argvars[2].vval.v_string;
11335 if (type == VAR_NUMBER)
11336 string_result = NULL;
11337 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011338 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011339 if (mch_libcall(argvars[0].vval.v_string,
11340 argvars[1].vval.v_string,
11341 string_in,
11342 argvars[2].vval.v_number,
11343 string_result,
11344 &nr_result) == OK
11345 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011346 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011347 }
11348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349}
11350
11351/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011352 * "libcall()" function
11353 */
11354 static void
11355f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011356 typval_T *argvars;
11357 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011358{
11359 libcall_common(argvars, rettv, VAR_STRING);
11360}
11361
11362/*
11363 * "libcallnr()" function
11364 */
11365 static void
11366f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011367 typval_T *argvars;
11368 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011369{
11370 libcall_common(argvars, rettv, VAR_NUMBER);
11371}
11372
11373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374 * "line(string)" function
11375 */
11376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011377f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011378 typval_T *argvars;
11379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380{
11381 linenr_T lnum = 0;
11382 pos_T *fp;
11383
11384 fp = var2fpos(&argvars[0], TRUE);
11385 if (fp != NULL)
11386 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011387 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388}
11389
11390/*
11391 * "line2byte(lnum)" function
11392 */
11393/*ARGSUSED*/
11394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011395f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011396 typval_T *argvars;
11397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011398{
11399#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011400 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401#else
11402 linenr_T lnum;
11403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011404 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011405 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011406 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011408 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11409 if (rettv->vval.v_number >= 0)
11410 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411#endif
11412}
11413
11414/*
11415 * "lispindent(lnum)" function
11416 */
11417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011418f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011419 typval_T *argvars;
11420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421{
11422#ifdef FEAT_LISP
11423 pos_T pos;
11424 linenr_T lnum;
11425
11426 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011427 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011428 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11429 {
11430 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011431 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011432 curwin->w_cursor = pos;
11433 }
11434 else
11435#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011437}
11438
11439/*
11440 * "localtime()" function
11441 */
11442/*ARGSUSED*/
11443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011444f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011445 typval_T *argvars;
11446 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011447{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449}
11450
Bram Moolenaar33570922005-01-25 22:26:29 +000011451static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452
11453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011454get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011455 typval_T *argvars;
11456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457 int exact;
11458{
11459 char_u *keys;
11460 char_u *which;
11461 char_u buf[NUMBUFLEN];
11462 char_u *keys_buf = NULL;
11463 char_u *rhs;
11464 int mode;
11465 garray_T ga;
11466
11467 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011468 rettv->v_type = VAR_STRING;
11469 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472 if (*keys == NUL)
11473 return;
11474
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011475 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011476 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477 else
11478 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011479 if (which == NULL)
11480 return;
11481
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482 mode = get_map_mode(&which, 0);
11483
11484 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011485 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486 vim_free(keys_buf);
11487 if (rhs != NULL)
11488 {
11489 ga_init(&ga);
11490 ga.ga_itemsize = 1;
11491 ga.ga_growsize = 40;
11492
11493 while (*rhs != NUL)
11494 ga_concat(&ga, str2special(&rhs, FALSE));
11495
11496 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011497 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498 }
11499}
11500
11501/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011502 * "map()" function
11503 */
11504 static void
11505f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011506 typval_T *argvars;
11507 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011508{
11509 filter_map(argvars, rettv, TRUE);
11510}
11511
11512/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011513 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514 */
11515 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011516f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011517 typval_T *argvars;
11518 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011519{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011520 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521}
11522
11523/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011524 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525 */
11526 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011527f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011528 typval_T *argvars;
11529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011531 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011532}
11533
Bram Moolenaar33570922005-01-25 22:26:29 +000011534static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011535
11536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011537find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011538 typval_T *argvars;
11539 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540 int type;
11541{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011542 char_u *str = NULL;
11543 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544 char_u *pat;
11545 regmatch_T regmatch;
11546 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011547 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011548 char_u *save_cpo;
11549 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011550 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011551 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011552 list_T *l = NULL;
11553 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011554 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011555 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011556
11557 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11558 save_cpo = p_cpo;
11559 p_cpo = (char_u *)"";
11560
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011561 rettv->vval.v_number = -1;
11562 if (type == 3)
11563 {
11564 /* return empty list when there are no matches */
11565 if ((rettv->vval.v_list = list_alloc()) == NULL)
11566 goto theend;
11567 rettv->v_type = VAR_LIST;
11568 ++rettv->vval.v_list->lv_refcount;
11569 }
11570 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011571 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011572 rettv->v_type = VAR_STRING;
11573 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011576 if (argvars[0].v_type == VAR_LIST)
11577 {
11578 if ((l = argvars[0].vval.v_list) == NULL)
11579 goto theend;
11580 li = l->lv_first;
11581 }
11582 else
11583 expr = str = get_tv_string(&argvars[0]);
11584
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011585 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11586 if (pat == NULL)
11587 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011588
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011589 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011590 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011591 int error = FALSE;
11592
11593 start = get_tv_number_chk(&argvars[2], &error);
11594 if (error)
11595 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011596 if (l != NULL)
11597 {
11598 li = list_find(l, start);
11599 if (li == NULL)
11600 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011601 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011602 }
11603 else
11604 {
11605 if (start < 0)
11606 start = 0;
11607 if (start > (long)STRLEN(str))
11608 goto theend;
11609 str += start;
11610 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011611
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011612 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011613 nth = get_tv_number_chk(&argvars[3], &error);
11614 if (error)
11615 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616 }
11617
11618 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11619 if (regmatch.regprog != NULL)
11620 {
11621 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011622
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011623 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011624 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011625 if (l != NULL)
11626 {
11627 if (li == NULL)
11628 {
11629 match = FALSE;
11630 break;
11631 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011632 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011633 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011634 if (str == NULL)
11635 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011636 }
11637
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011638 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011639
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011640 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011641 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011642 if (l == NULL && !match)
11643 break;
11644
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011645 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011646 if (l != NULL)
11647 {
11648 li = li->li_next;
11649 ++idx;
11650 }
11651 else
11652 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011653#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011654 str = regmatch.startp[0] + (*mb_ptr2len)(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011655#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011656 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011657#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011658 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011659 }
11660
11661 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011663 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011664 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011665 int i;
11666
11667 /* return list with matched string and submatches */
11668 for (i = 0; i < NSUBEXP; ++i)
11669 {
11670 if (regmatch.endp[i] == NULL)
11671 break;
11672 li = listitem_alloc();
11673 if (li == NULL)
11674 break;
11675 li->li_tv.v_type = VAR_STRING;
11676 li->li_tv.v_lock = 0;
11677 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
11678 (int)(regmatch.endp[i] - regmatch.startp[i]));
11679 list_append(rettv->vval.v_list, li);
11680 }
11681 }
11682 else if (type == 2)
11683 {
11684 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011685 if (l != NULL)
11686 copy_tv(&li->li_tv, rettv);
11687 else
11688 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011690 }
11691 else if (l != NULL)
11692 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011693 else
11694 {
11695 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011696 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697 (varnumber_T)(regmatch.startp[0] - str);
11698 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011699 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011701 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702 }
11703 }
11704 vim_free(regmatch.regprog);
11705 }
11706
11707theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011708 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011709 p_cpo = save_cpo;
11710}
11711
11712/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011713 * "match()" function
11714 */
11715 static void
11716f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011717 typval_T *argvars;
11718 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011719{
11720 find_some_match(argvars, rettv, 1);
11721}
11722
11723/*
11724 * "matchend()" function
11725 */
11726 static void
11727f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011728 typval_T *argvars;
11729 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011730{
11731 find_some_match(argvars, rettv, 0);
11732}
11733
11734/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011735 * "matchlist()" function
11736 */
11737 static void
11738f_matchlist(argvars, rettv)
11739 typval_T *argvars;
11740 typval_T *rettv;
11741{
11742 find_some_match(argvars, rettv, 3);
11743}
11744
11745/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011746 * "matchstr()" function
11747 */
11748 static void
11749f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011750 typval_T *argvars;
11751 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011752{
11753 find_some_match(argvars, rettv, 2);
11754}
11755
Bram Moolenaar33570922005-01-25 22:26:29 +000011756static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011757
11758 static void
11759max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011760 typval_T *argvars;
11761 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011762 int domax;
11763{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011764 long n = 0;
11765 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011766 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011767
11768 if (argvars[0].v_type == VAR_LIST)
11769 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011770 list_T *l;
11771 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011772
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011773 l = argvars[0].vval.v_list;
11774 if (l != NULL)
11775 {
11776 li = l->lv_first;
11777 if (li != NULL)
11778 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011779 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011780 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011781 {
11782 li = li->li_next;
11783 if (li == NULL)
11784 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011785 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011786 if (domax ? i > n : i < n)
11787 n = i;
11788 }
11789 }
11790 }
11791 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011792 else if (argvars[0].v_type == VAR_DICT)
11793 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011794 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011795 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011796 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011797 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011798
11799 d = argvars[0].vval.v_dict;
11800 if (d != NULL)
11801 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011802 todo = d->dv_hashtab.ht_used;
11803 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011804 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011805 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011806 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011807 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011808 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011809 if (first)
11810 {
11811 n = i;
11812 first = FALSE;
11813 }
11814 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011815 n = i;
11816 }
11817 }
11818 }
11819 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011820 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011821 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011822 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011823}
11824
11825/*
11826 * "max()" function
11827 */
11828 static void
11829f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011830 typval_T *argvars;
11831 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011832{
11833 max_min(argvars, rettv, TRUE);
11834}
11835
11836/*
11837 * "min()" function
11838 */
11839 static void
11840f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011841 typval_T *argvars;
11842 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011843{
11844 max_min(argvars, rettv, FALSE);
11845}
11846
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011847static int mkdir_recurse __ARGS((char_u *dir, int prot));
11848
11849/*
11850 * Create the directory in which "dir" is located, and higher levels when
11851 * needed.
11852 */
11853 static int
11854mkdir_recurse(dir, prot)
11855 char_u *dir;
11856 int prot;
11857{
11858 char_u *p;
11859 char_u *updir;
11860 int r = FAIL;
11861
11862 /* Get end of directory name in "dir".
11863 * We're done when it's "/" or "c:/". */
11864 p = gettail_sep(dir);
11865 if (p <= get_past_head(dir))
11866 return OK;
11867
11868 /* If the directory exists we're done. Otherwise: create it.*/
11869 updir = vim_strnsave(dir, (int)(p - dir));
11870 if (updir == NULL)
11871 return FAIL;
11872 if (mch_isdir(updir))
11873 r = OK;
11874 else if (mkdir_recurse(updir, prot) == OK)
11875 r = vim_mkdir_emsg(updir, prot);
11876 vim_free(updir);
11877 return r;
11878}
11879
11880#ifdef vim_mkdir
11881/*
11882 * "mkdir()" function
11883 */
11884 static void
11885f_mkdir(argvars, rettv)
11886 typval_T *argvars;
11887 typval_T *rettv;
11888{
11889 char_u *dir;
11890 char_u buf[NUMBUFLEN];
11891 int prot = 0755;
11892
11893 rettv->vval.v_number = FAIL;
11894 if (check_restricted() || check_secure())
11895 return;
11896
11897 dir = get_tv_string_buf(&argvars[0], buf);
11898 if (argvars[1].v_type != VAR_UNKNOWN)
11899 {
11900 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011901 prot = get_tv_number_chk(&argvars[2], NULL);
11902 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011903 mkdir_recurse(dir, prot);
11904 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011905 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011906}
11907#endif
11908
Bram Moolenaar0d660222005-01-07 21:51:51 +000011909/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011910 * "mode()" function
11911 */
11912/*ARGSUSED*/
11913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011914f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011915 typval_T *argvars;
11916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011917{
11918 char_u buf[2];
11919
11920#ifdef FEAT_VISUAL
11921 if (VIsual_active)
11922 {
11923 if (VIsual_select)
11924 buf[0] = VIsual_mode + 's' - 'v';
11925 else
11926 buf[0] = VIsual_mode;
11927 }
11928 else
11929#endif
11930 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11931 buf[0] = 'r';
11932 else if (State & INSERT)
11933 {
11934 if (State & REPLACE_FLAG)
11935 buf[0] = 'R';
11936 else
11937 buf[0] = 'i';
11938 }
11939 else if (State & CMDLINE)
11940 buf[0] = 'c';
11941 else
11942 buf[0] = 'n';
11943
11944 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011945 rettv->vval.v_string = vim_strsave(buf);
11946 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011947}
11948
11949/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011950 * "nextnonblank()" function
11951 */
11952 static void
11953f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011954 typval_T *argvars;
11955 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011956{
11957 linenr_T lnum;
11958
11959 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11960 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011961 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011962 {
11963 lnum = 0;
11964 break;
11965 }
11966 if (*skipwhite(ml_get(lnum)) != NUL)
11967 break;
11968 }
11969 rettv->vval.v_number = lnum;
11970}
11971
11972/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973 * "nr2char()" function
11974 */
11975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011976f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011977 typval_T *argvars;
11978 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979{
11980 char_u buf[NUMBUFLEN];
11981
11982#ifdef FEAT_MBYTE
11983 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011984 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985 else
11986#endif
11987 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011988 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011989 buf[1] = NUL;
11990 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011991 rettv->v_type = VAR_STRING;
11992 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011993}
11994
11995/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011996 * "prevnonblank()" function
11997 */
11998 static void
11999f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012000 typval_T *argvars;
12001 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012002{
12003 linenr_T lnum;
12004
12005 lnum = get_tv_lnum(argvars);
12006 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
12007 lnum = 0;
12008 else
12009 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
12010 --lnum;
12011 rettv->vval.v_number = lnum;
12012}
12013
Bram Moolenaara6c840d2005-08-22 22:59:46 +000012014#ifdef HAVE_STDARG_H
12015/* This dummy va_list is here because:
12016 * - passing a NULL pointer doesn't work when va_list isn't a pointer
12017 * - locally in the function results in a "used before set" warning
12018 * - using va_start() to initialize it gives "function with fixed args" error */
12019static va_list ap;
12020#endif
12021
Bram Moolenaar8c711452005-01-14 21:53:12 +000012022/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012023 * "printf()" function
12024 */
12025 static void
12026f_printf(argvars, rettv)
12027 typval_T *argvars;
12028 typval_T *rettv;
12029{
12030 rettv->v_type = VAR_STRING;
12031 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000012032#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012033 {
12034 char_u buf[NUMBUFLEN];
12035 int len;
12036 char_u *s;
12037 int saved_did_emsg = did_emsg;
12038 char *fmt;
12039
12040 /* Get the required length, allocate the buffer and do it for real. */
12041 did_emsg = FALSE;
12042 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012043 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012044 if (!did_emsg)
12045 {
12046 s = alloc(len + 1);
12047 if (s != NULL)
12048 {
12049 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012050 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012051 }
12052 }
12053 did_emsg |= saved_did_emsg;
12054 }
12055#endif
12056}
12057
12058/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012059 * "range()" function
12060 */
12061 static void
12062f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012063 typval_T *argvars;
12064 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012065{
12066 long start;
12067 long end;
12068 long stride = 1;
12069 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012070 list_T *l;
12071 listitem_T *li;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012072 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012074 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012075 if (argvars[1].v_type == VAR_UNKNOWN)
12076 {
12077 end = start - 1;
12078 start = 0;
12079 }
12080 else
12081 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012082 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012083 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012084 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012085 }
12086
12087 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012088 if (error)
12089 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012090 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012091 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012092 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012093 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012094 else
12095 {
12096 l = list_alloc();
12097 if (l != NULL)
12098 {
12099 rettv->v_type = VAR_LIST;
12100 rettv->vval.v_list = l;
12101 ++l->lv_refcount;
12102
12103 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
12104 {
12105 li = listitem_alloc();
12106 if (li == NULL)
12107 break;
12108 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012109 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012110 li->li_tv.vval.v_number = i;
12111 list_append(l, li);
12112 }
12113 }
12114 }
12115}
12116
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012117/*
12118 * "readfile()" function
12119 */
12120 static void
12121f_readfile(argvars, rettv)
12122 typval_T *argvars;
12123 typval_T *rettv;
12124{
12125 int binary = FALSE;
12126 char_u *fname;
12127 FILE *fd;
12128 list_T *l;
12129 listitem_T *li;
12130#define FREAD_SIZE 200 /* optimized for text lines */
12131 char_u buf[FREAD_SIZE];
12132 int readlen; /* size of last fread() */
12133 int buflen; /* nr of valid chars in buf[] */
12134 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12135 int tolist; /* first byte in buf[] still to be put in list */
12136 int chop; /* how many CR to chop off */
12137 char_u *prev = NULL; /* previously read bytes, if any */
12138 int prevlen = 0; /* length of "prev" if not NULL */
12139 char_u *s;
12140 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012141 long maxline = MAXLNUM;
12142 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012143
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012144 if (argvars[1].v_type != VAR_UNKNOWN)
12145 {
12146 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12147 binary = TRUE;
12148 if (argvars[2].v_type != VAR_UNKNOWN)
12149 maxline = get_tv_number(&argvars[2]);
12150 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012151
12152 l = list_alloc();
12153 if (l == NULL)
12154 return;
12155 rettv->v_type = VAR_LIST;
12156 rettv->vval.v_list = l;
12157 l->lv_refcount = 1;
12158
12159 /* Always open the file in binary mode, library functions have a mind of
12160 * their own about CR-LF conversion. */
12161 fname = get_tv_string(&argvars[0]);
12162 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12163 {
12164 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12165 return;
12166 }
12167
12168 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012169 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012170 {
12171 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12172 buflen = filtd + readlen;
12173 tolist = 0;
12174 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12175 {
12176 if (buf[filtd] == '\n' || readlen <= 0)
12177 {
12178 /* Only when in binary mode add an empty list item when the
12179 * last line ends in a '\n'. */
12180 if (!binary && readlen == 0 && filtd == 0)
12181 break;
12182
12183 /* Found end-of-line or end-of-file: add a text line to the
12184 * list. */
12185 chop = 0;
12186 if (!binary)
12187 while (filtd - chop - 1 >= tolist
12188 && buf[filtd - chop - 1] == '\r')
12189 ++chop;
12190 len = filtd - tolist - chop;
12191 if (prev == NULL)
12192 s = vim_strnsave(buf + tolist, len);
12193 else
12194 {
12195 s = alloc((unsigned)(prevlen + len + 1));
12196 if (s != NULL)
12197 {
12198 mch_memmove(s, prev, prevlen);
12199 vim_free(prev);
12200 prev = NULL;
12201 mch_memmove(s + prevlen, buf + tolist, len);
12202 s[prevlen + len] = NUL;
12203 }
12204 }
12205 tolist = filtd + 1;
12206
12207 li = listitem_alloc();
12208 if (li == NULL)
12209 {
12210 vim_free(s);
12211 break;
12212 }
12213 li->li_tv.v_type = VAR_STRING;
12214 li->li_tv.v_lock = 0;
12215 li->li_tv.vval.v_string = s;
12216 list_append(l, li);
12217
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012218 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012219 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012220 if (readlen <= 0)
12221 break;
12222 }
12223 else if (buf[filtd] == NUL)
12224 buf[filtd] = '\n';
12225 }
12226 if (readlen <= 0)
12227 break;
12228
12229 if (tolist == 0)
12230 {
12231 /* "buf" is full, need to move text to an allocated buffer */
12232 if (prev == NULL)
12233 {
12234 prev = vim_strnsave(buf, buflen);
12235 prevlen = buflen;
12236 }
12237 else
12238 {
12239 s = alloc((unsigned)(prevlen + buflen));
12240 if (s != NULL)
12241 {
12242 mch_memmove(s, prev, prevlen);
12243 mch_memmove(s + prevlen, buf, buflen);
12244 vim_free(prev);
12245 prev = s;
12246 prevlen += buflen;
12247 }
12248 }
12249 filtd = 0;
12250 }
12251 else
12252 {
12253 mch_memmove(buf, buf + tolist, buflen - tolist);
12254 filtd -= tolist;
12255 }
12256 }
12257
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012258 /*
12259 * For a negative line count use only the lines at the end of the file,
12260 * free the rest.
12261 */
12262 if (maxline < 0)
12263 while (cnt > -maxline)
12264 {
12265 listitem_remove(l, l->lv_first);
12266 --cnt;
12267 }
12268
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012269 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012270 fclose(fd);
12271}
12272
12273
Bram Moolenaar0d660222005-01-07 21:51:51 +000012274#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12275static void make_connection __ARGS((void));
12276static int check_connection __ARGS((void));
12277
12278 static void
12279make_connection()
12280{
12281 if (X_DISPLAY == NULL
12282# ifdef FEAT_GUI
12283 && !gui.in_use
12284# endif
12285 )
12286 {
12287 x_force_connect = TRUE;
12288 setup_term_clip();
12289 x_force_connect = FALSE;
12290 }
12291}
12292
12293 static int
12294check_connection()
12295{
12296 make_connection();
12297 if (X_DISPLAY == NULL)
12298 {
12299 EMSG(_("E240: No connection to Vim server"));
12300 return FAIL;
12301 }
12302 return OK;
12303}
12304#endif
12305
12306#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012307static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012308
12309 static void
12310remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012311 typval_T *argvars;
12312 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012313 int expr;
12314{
12315 char_u *server_name;
12316 char_u *keys;
12317 char_u *r = NULL;
12318 char_u buf[NUMBUFLEN];
12319# ifdef WIN32
12320 HWND w;
12321# else
12322 Window w;
12323# endif
12324
12325 if (check_restricted() || check_secure())
12326 return;
12327
12328# ifdef FEAT_X11
12329 if (check_connection() == FAIL)
12330 return;
12331# endif
12332
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012333 server_name = get_tv_string_chk(&argvars[0]);
12334 if (server_name == NULL)
12335 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012336 keys = get_tv_string_buf(&argvars[1], buf);
12337# ifdef WIN32
12338 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12339# else
12340 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12341 < 0)
12342# endif
12343 {
12344 if (r != NULL)
12345 EMSG(r); /* sending worked but evaluation failed */
12346 else
12347 EMSG2(_("E241: Unable to send to %s"), server_name);
12348 return;
12349 }
12350
12351 rettv->vval.v_string = r;
12352
12353 if (argvars[2].v_type != VAR_UNKNOWN)
12354 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012355 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012356 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012357 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012358
12359 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012360 v.di_tv.v_type = VAR_STRING;
12361 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012362 idvar = get_tv_string_chk(&argvars[2]);
12363 if (idvar != NULL)
12364 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012365 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012366 }
12367}
12368#endif
12369
12370/*
12371 * "remote_expr()" function
12372 */
12373/*ARGSUSED*/
12374 static void
12375f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012376 typval_T *argvars;
12377 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012378{
12379 rettv->v_type = VAR_STRING;
12380 rettv->vval.v_string = NULL;
12381#ifdef FEAT_CLIENTSERVER
12382 remote_common(argvars, rettv, TRUE);
12383#endif
12384}
12385
12386/*
12387 * "remote_foreground()" function
12388 */
12389/*ARGSUSED*/
12390 static void
12391f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012392 typval_T *argvars;
12393 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012394{
12395 rettv->vval.v_number = 0;
12396#ifdef FEAT_CLIENTSERVER
12397# ifdef WIN32
12398 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012399 {
12400 char_u *server_name = get_tv_string_chk(&argvars[0]);
12401
12402 if (server_name != NULL)
12403 serverForeground(server_name);
12404 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012405# else
12406 /* Send a foreground() expression to the server. */
12407 argvars[1].v_type = VAR_STRING;
12408 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12409 argvars[2].v_type = VAR_UNKNOWN;
12410 remote_common(argvars, rettv, TRUE);
12411 vim_free(argvars[1].vval.v_string);
12412# endif
12413#endif
12414}
12415
12416/*ARGSUSED*/
12417 static void
12418f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012419 typval_T *argvars;
12420 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012421{
12422#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012423 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012424 char_u *s = NULL;
12425# ifdef WIN32
12426 int n = 0;
12427# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012428 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012429
12430 if (check_restricted() || check_secure())
12431 {
12432 rettv->vval.v_number = -1;
12433 return;
12434 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012435 serverid = get_tv_string_chk(&argvars[0]);
12436 if (serverid == NULL)
12437 {
12438 rettv->vval.v_number = -1;
12439 return; /* type error; errmsg already given */
12440 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012441# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012442 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012443 if (n == 0)
12444 rettv->vval.v_number = -1;
12445 else
12446 {
12447 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12448 rettv->vval.v_number = (s != NULL);
12449 }
12450# else
12451 rettv->vval.v_number = 0;
12452 if (check_connection() == FAIL)
12453 return;
12454
12455 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012456 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012457# endif
12458
12459 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12460 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012461 char_u *retvar;
12462
Bram Moolenaar33570922005-01-25 22:26:29 +000012463 v.di_tv.v_type = VAR_STRING;
12464 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012465 retvar = get_tv_string_chk(&argvars[1]);
12466 if (retvar != NULL)
12467 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012468 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012469 }
12470#else
12471 rettv->vval.v_number = -1;
12472#endif
12473}
12474
12475/*ARGSUSED*/
12476 static void
12477f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012478 typval_T *argvars;
12479 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012480{
12481 char_u *r = NULL;
12482
12483#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012484 char_u *serverid = get_tv_string_chk(&argvars[0]);
12485
12486 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012487 {
12488# ifdef WIN32
12489 /* The server's HWND is encoded in the 'id' parameter */
12490 int n = 0;
12491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012492 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012493 if (n != 0)
12494 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12495 if (r == NULL)
12496# else
12497 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012498 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012499# endif
12500 EMSG(_("E277: Unable to read a server reply"));
12501 }
12502#endif
12503 rettv->v_type = VAR_STRING;
12504 rettv->vval.v_string = r;
12505}
12506
12507/*
12508 * "remote_send()" function
12509 */
12510/*ARGSUSED*/
12511 static void
12512f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012513 typval_T *argvars;
12514 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012515{
12516 rettv->v_type = VAR_STRING;
12517 rettv->vval.v_string = NULL;
12518#ifdef FEAT_CLIENTSERVER
12519 remote_common(argvars, rettv, FALSE);
12520#endif
12521}
12522
12523/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012524 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012525 */
12526 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012527f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012528 typval_T *argvars;
12529 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012530{
Bram Moolenaar33570922005-01-25 22:26:29 +000012531 list_T *l;
12532 listitem_T *item, *item2;
12533 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012534 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012535 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012536 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012537 dict_T *d;
12538 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012539
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012540 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012541 if (argvars[0].v_type == VAR_DICT)
12542 {
12543 if (argvars[2].v_type != VAR_UNKNOWN)
12544 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012545 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012546 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012547 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012548 key = get_tv_string_chk(&argvars[1]);
12549 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012550 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012551 di = dict_find(d, key, -1);
12552 if (di == NULL)
12553 EMSG2(_(e_dictkey), key);
12554 else
12555 {
12556 *rettv = di->di_tv;
12557 init_tv(&di->di_tv);
12558 dictitem_remove(d, di);
12559 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012560 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012561 }
12562 }
12563 else if (argvars[0].v_type != VAR_LIST)
12564 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012565 else if ((l = argvars[0].vval.v_list) != NULL
12566 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012567 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012568 int error = FALSE;
12569
12570 idx = get_tv_number_chk(&argvars[1], &error);
12571 if (error)
12572 ; /* type error: do nothing, errmsg already given */
12573 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012574 EMSGN(_(e_listidx), idx);
12575 else
12576 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012577 if (argvars[2].v_type == VAR_UNKNOWN)
12578 {
12579 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012580 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012581 *rettv = item->li_tv;
12582 vim_free(item);
12583 }
12584 else
12585 {
12586 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012587 end = get_tv_number_chk(&argvars[2], &error);
12588 if (error)
12589 ; /* type error: do nothing */
12590 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012591 EMSGN(_(e_listidx), end);
12592 else
12593 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012594 int cnt = 0;
12595
12596 for (li = item; li != NULL; li = li->li_next)
12597 {
12598 ++cnt;
12599 if (li == item2)
12600 break;
12601 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012602 if (li == NULL) /* didn't find "item2" after "item" */
12603 EMSG(_(e_invrange));
12604 else
12605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012606 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012607 l = list_alloc();
12608 if (l != NULL)
12609 {
12610 rettv->v_type = VAR_LIST;
12611 rettv->vval.v_list = l;
12612 l->lv_first = item;
12613 l->lv_last = item2;
12614 l->lv_refcount = 1;
12615 item->li_prev = NULL;
12616 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012617 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012618 }
12619 }
12620 }
12621 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012622 }
12623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624}
12625
12626/*
12627 * "rename({from}, {to})" function
12628 */
12629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012630f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012631 typval_T *argvars;
12632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633{
12634 char_u buf[NUMBUFLEN];
12635
12636 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012637 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012638 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012639 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12640 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641}
12642
12643/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012644 * "repeat()" function
12645 */
12646/*ARGSUSED*/
12647 static void
12648f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012649 typval_T *argvars;
12650 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012651{
12652 char_u *p;
12653 int n;
12654 int slen;
12655 int len;
12656 char_u *r;
12657 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012658 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012659
12660 n = get_tv_number(&argvars[1]);
12661 if (argvars[0].v_type == VAR_LIST)
12662 {
12663 l = list_alloc();
12664 if (l != NULL && argvars[0].vval.v_list != NULL)
12665 {
12666 l->lv_refcount = 1;
12667 while (n-- > 0)
12668 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12669 break;
12670 }
12671 rettv->v_type = VAR_LIST;
12672 rettv->vval.v_list = l;
12673 }
12674 else
12675 {
12676 p = get_tv_string(&argvars[0]);
12677 rettv->v_type = VAR_STRING;
12678 rettv->vval.v_string = NULL;
12679
12680 slen = (int)STRLEN(p);
12681 len = slen * n;
12682 if (len <= 0)
12683 return;
12684
12685 r = alloc(len + 1);
12686 if (r != NULL)
12687 {
12688 for (i = 0; i < n; i++)
12689 mch_memmove(r + i * slen, p, (size_t)slen);
12690 r[len] = NUL;
12691 }
12692
12693 rettv->vval.v_string = r;
12694 }
12695}
12696
12697/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698 * "resolve()" function
12699 */
12700 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012701f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012702 typval_T *argvars;
12703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704{
12705 char_u *p;
12706
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012707 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708#ifdef FEAT_SHORTCUT
12709 {
12710 char_u *v = NULL;
12711
12712 v = mch_resolve_shortcut(p);
12713 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012716 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717 }
12718#else
12719# ifdef HAVE_READLINK
12720 {
12721 char_u buf[MAXPATHL + 1];
12722 char_u *cpy;
12723 int len;
12724 char_u *remain = NULL;
12725 char_u *q;
12726 int is_relative_to_current = FALSE;
12727 int has_trailing_pathsep = FALSE;
12728 int limit = 100;
12729
12730 p = vim_strsave(p);
12731
12732 if (p[0] == '.' && (vim_ispathsep(p[1])
12733 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12734 is_relative_to_current = TRUE;
12735
12736 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012737 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738 has_trailing_pathsep = TRUE;
12739
12740 q = getnextcomp(p);
12741 if (*q != NUL)
12742 {
12743 /* Separate the first path component in "p", and keep the
12744 * remainder (beginning with the path separator). */
12745 remain = vim_strsave(q - 1);
12746 q[-1] = NUL;
12747 }
12748
12749 for (;;)
12750 {
12751 for (;;)
12752 {
12753 len = readlink((char *)p, (char *)buf, MAXPATHL);
12754 if (len <= 0)
12755 break;
12756 buf[len] = NUL;
12757
12758 if (limit-- == 0)
12759 {
12760 vim_free(p);
12761 vim_free(remain);
12762 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012763 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764 goto fail;
12765 }
12766
12767 /* Ensure that the result will have a trailing path separator
12768 * if the argument has one. */
12769 if (remain == NULL && has_trailing_pathsep)
12770 add_pathsep(buf);
12771
12772 /* Separate the first path component in the link value and
12773 * concatenate the remainders. */
12774 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12775 if (*q != NUL)
12776 {
12777 if (remain == NULL)
12778 remain = vim_strsave(q - 1);
12779 else
12780 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012781 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012782 if (cpy != NULL)
12783 {
12784 STRCAT(cpy, remain);
12785 vim_free(remain);
12786 remain = cpy;
12787 }
12788 }
12789 q[-1] = NUL;
12790 }
12791
12792 q = gettail(p);
12793 if (q > p && *q == NUL)
12794 {
12795 /* Ignore trailing path separator. */
12796 q[-1] = NUL;
12797 q = gettail(p);
12798 }
12799 if (q > p && !mch_isFullName(buf))
12800 {
12801 /* symlink is relative to directory of argument */
12802 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12803 if (cpy != NULL)
12804 {
12805 STRCPY(cpy, p);
12806 STRCPY(gettail(cpy), buf);
12807 vim_free(p);
12808 p = cpy;
12809 }
12810 }
12811 else
12812 {
12813 vim_free(p);
12814 p = vim_strsave(buf);
12815 }
12816 }
12817
12818 if (remain == NULL)
12819 break;
12820
12821 /* Append the first path component of "remain" to "p". */
12822 q = getnextcomp(remain + 1);
12823 len = q - remain - (*q != NUL);
12824 cpy = vim_strnsave(p, STRLEN(p) + len);
12825 if (cpy != NULL)
12826 {
12827 STRNCAT(cpy, remain, len);
12828 vim_free(p);
12829 p = cpy;
12830 }
12831 /* Shorten "remain". */
12832 if (*q != NUL)
12833 STRCPY(remain, q - 1);
12834 else
12835 {
12836 vim_free(remain);
12837 remain = NULL;
12838 }
12839 }
12840
12841 /* If the result is a relative path name, make it explicitly relative to
12842 * the current directory if and only if the argument had this form. */
12843 if (!vim_ispathsep(*p))
12844 {
12845 if (is_relative_to_current
12846 && *p != NUL
12847 && !(p[0] == '.'
12848 && (p[1] == NUL
12849 || vim_ispathsep(p[1])
12850 || (p[1] == '.'
12851 && (p[2] == NUL
12852 || vim_ispathsep(p[2]))))))
12853 {
12854 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012855 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856 if (cpy != NULL)
12857 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858 vim_free(p);
12859 p = cpy;
12860 }
12861 }
12862 else if (!is_relative_to_current)
12863 {
12864 /* Strip leading "./". */
12865 q = p;
12866 while (q[0] == '.' && vim_ispathsep(q[1]))
12867 q += 2;
12868 if (q > p)
12869 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12870 }
12871 }
12872
12873 /* Ensure that the result will have no trailing path separator
12874 * if the argument had none. But keep "/" or "//". */
12875 if (!has_trailing_pathsep)
12876 {
12877 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012878 if (after_pathsep(p, q))
12879 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880 }
12881
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012882 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883 }
12884# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886# endif
12887#endif
12888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012889 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890
12891#ifdef HAVE_READLINK
12892fail:
12893#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012894 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895}
12896
12897/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012898 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012899 */
12900 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012901f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012902 typval_T *argvars;
12903 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904{
Bram Moolenaar33570922005-01-25 22:26:29 +000012905 list_T *l;
12906 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907
Bram Moolenaar0d660222005-01-07 21:51:51 +000012908 rettv->vval.v_number = 0;
12909 if (argvars[0].v_type != VAR_LIST)
12910 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012911 else if ((l = argvars[0].vval.v_list) != NULL
12912 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012913 {
12914 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012915 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012916 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012917 while (li != NULL)
12918 {
12919 ni = li->li_prev;
12920 list_append(l, li);
12921 li = ni;
12922 }
12923 rettv->vval.v_list = l;
12924 rettv->v_type = VAR_LIST;
12925 ++l->lv_refcount;
12926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927}
12928
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012929#define SP_NOMOVE 1 /* don't move cursor */
12930#define SP_REPEAT 2 /* repeat to find outer pair */
12931#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000012932#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012933
Bram Moolenaar33570922005-01-25 22:26:29 +000012934static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012935
12936/*
12937 * Get flags for a search function.
12938 * Possibly sets "p_ws".
12939 * Returns BACKWARD, FORWARD or zero (for an error).
12940 */
12941 static int
12942get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012943 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012944 int *flagsp;
12945{
12946 int dir = FORWARD;
12947 char_u *flags;
12948 char_u nbuf[NUMBUFLEN];
12949 int mask;
12950
12951 if (varp->v_type != VAR_UNKNOWN)
12952 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012953 flags = get_tv_string_buf_chk(varp, nbuf);
12954 if (flags == NULL)
12955 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012956 while (*flags != NUL)
12957 {
12958 switch (*flags)
12959 {
12960 case 'b': dir = BACKWARD; break;
12961 case 'w': p_ws = TRUE; break;
12962 case 'W': p_ws = FALSE; break;
12963 default: mask = 0;
12964 if (flagsp != NULL)
12965 switch (*flags)
12966 {
12967 case 'n': mask = SP_NOMOVE; break;
12968 case 'r': mask = SP_REPEAT; break;
12969 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012970 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012971 }
12972 if (mask == 0)
12973 {
12974 EMSG2(_(e_invarg2), flags);
12975 dir = 0;
12976 }
12977 else
12978 *flagsp |= mask;
12979 }
12980 if (dir == 0)
12981 break;
12982 ++flags;
12983 }
12984 }
12985 return dir;
12986}
12987
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988/*
12989 * "search()" function
12990 */
12991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012992f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012993 typval_T *argvars;
12994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995{
12996 char_u *pat;
12997 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012998 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999 int save_p_ws = p_ws;
13000 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013001 int flags = 0;
13002
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013003 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013005 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013006 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
13007 if (dir == 0)
13008 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013009 /*
13010 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
13011 * Check to make sure only those flags are set.
13012 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
13013 * flags cannot be set. Check for that condition also.
13014 */
13015 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
13016 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013017 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013018 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013019 goto theend;
13020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013021
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013022 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
13024 SEARCH_KEEP, RE_SEARCH) != FAIL)
13025 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013026 rettv->vval.v_number = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013027 if (flags & SP_SETPCMARK)
13028 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029 curwin->w_cursor = pos;
13030 /* "/$" will put the cursor after the end of the line, may need to
13031 * correct that here */
13032 check_cursor();
13033 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013034
13035 /* If 'n' flag is used: restore cursor position. */
13036 if (flags & SP_NOMOVE)
13037 curwin->w_cursor = save_cursor;
13038theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039 p_ws = save_p_ws;
13040}
13041
Bram Moolenaar071d4272004-06-13 20:20:40 +000013042/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013043 * "searchdecl()" function
13044 */
13045 static void
13046f_searchdecl(argvars, rettv)
13047 typval_T *argvars;
13048 typval_T *rettv;
13049{
13050 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013051 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013052 int error = FALSE;
13053 char_u *name;
13054
13055 rettv->vval.v_number = 1; /* default: FAIL */
13056
13057 name = get_tv_string_chk(&argvars[0]);
13058 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000013059 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013060 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000013061 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13062 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
13063 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013064 if (!error && name != NULL)
13065 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000013066 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000013067}
13068
13069/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070 * "searchpair()" function
13071 */
13072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013073f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013074 typval_T *argvars;
13075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076{
13077 char_u *spat, *mpat, *epat;
13078 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013080 int dir;
13081 int flags = 0;
13082 char_u nbuf1[NUMBUFLEN];
13083 char_u nbuf2[NUMBUFLEN];
13084 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013086 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013087
Bram Moolenaar071d4272004-06-13 20:20:40 +000013088 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013089 spat = get_tv_string_chk(&argvars[0]);
13090 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13091 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13092 if (spat == NULL || mpat == NULL || epat == NULL)
13093 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094
Bram Moolenaar071d4272004-06-13 20:20:40 +000013095 /* Handle the optional fourth argument: flags */
13096 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013097 if (dir == 0)
13098 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013099 /*
13100 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13101 */
13102 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13103 {
13104 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13105 goto theend;
13106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107
13108 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013109 if (argvars[3].v_type == VAR_UNKNOWN
13110 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111 skip = (char_u *)"";
13112 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013113 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
13114 if (skip == NULL)
13115 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013117 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
13118
13119theend:
13120 p_ws = save_p_ws;
13121}
13122
13123/*
13124 * Search for a start/middle/end thing.
13125 * Used by searchpair(), see its documentation for the details.
13126 * Returns 0 or -1 for no match,
13127 */
13128 long
13129do_searchpair(spat, mpat, epat, dir, skip, flags)
13130 char_u *spat; /* start pattern */
13131 char_u *mpat; /* middle pattern */
13132 char_u *epat; /* end pattern */
13133 int dir; /* BACKWARD or FORWARD */
13134 char_u *skip; /* skip expression */
13135 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
13136{
13137 char_u *save_cpo;
13138 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13139 long retval = 0;
13140 pos_T pos;
13141 pos_T firstpos;
13142 pos_T foundpos;
13143 pos_T save_cursor;
13144 pos_T save_pos;
13145 int n;
13146 int r;
13147 int nest = 1;
13148 int err;
13149
13150 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13151 save_cpo = p_cpo;
13152 p_cpo = (char_u *)"";
13153
13154 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13155 * start/middle/end (pat3, for the top pair). */
13156 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13157 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13158 if (pat2 == NULL || pat3 == NULL)
13159 goto theend;
13160 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13161 if (*mpat == NUL)
13162 STRCPY(pat3, pat2);
13163 else
13164 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13165 spat, epat, mpat);
13166
Bram Moolenaar071d4272004-06-13 20:20:40 +000013167 save_cursor = curwin->w_cursor;
13168 pos = curwin->w_cursor;
13169 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013170 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171 pat = pat3;
13172 for (;;)
13173 {
13174 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13175 SEARCH_KEEP, RE_SEARCH);
13176 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13177 /* didn't find it or found the first match again: FAIL */
13178 break;
13179
13180 if (firstpos.lnum == 0)
13181 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013182 if (equalpos(pos, foundpos))
13183 {
13184 /* Found the same position again. Can happen with a pattern that
13185 * has "\zs" at the end and searching backwards. Advance one
13186 * character and try again. */
13187 if (dir == BACKWARD)
13188 decl(&pos);
13189 else
13190 incl(&pos);
13191 }
13192 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013193
13194 /* If the skip pattern matches, ignore this match. */
13195 if (*skip != NUL)
13196 {
13197 save_pos = curwin->w_cursor;
13198 curwin->w_cursor = pos;
13199 r = eval_to_bool(skip, &err, NULL, FALSE);
13200 curwin->w_cursor = save_pos;
13201 if (err)
13202 {
13203 /* Evaluating {skip} caused an error, break here. */
13204 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013205 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013206 break;
13207 }
13208 if (r)
13209 continue;
13210 }
13211
13212 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13213 {
13214 /* Found end when searching backwards or start when searching
13215 * forward: nested pair. */
13216 ++nest;
13217 pat = pat2; /* nested, don't search for middle */
13218 }
13219 else
13220 {
13221 /* Found end when searching forward or start when searching
13222 * backward: end of (nested) pair; or found middle in outer pair. */
13223 if (--nest == 1)
13224 pat = pat3; /* outer level, search for middle */
13225 }
13226
13227 if (nest == 0)
13228 {
13229 /* Found the match: return matchcount or line number. */
13230 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013231 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013233 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013234 if (flags & SP_SETPCMARK)
13235 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236 curwin->w_cursor = pos;
13237 if (!(flags & SP_REPEAT))
13238 break;
13239 nest = 1; /* search for next unmatched */
13240 }
13241 }
13242
13243 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013244 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245 curwin->w_cursor = save_cursor;
13246
13247theend:
13248 vim_free(pat2);
13249 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013251
13252 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013253}
13254
Bram Moolenaar0d660222005-01-07 21:51:51 +000013255/*ARGSUSED*/
13256 static void
13257f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013258 typval_T *argvars;
13259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013261#ifdef FEAT_CLIENTSERVER
13262 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013263 char_u *server = get_tv_string_chk(&argvars[0]);
13264 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265
Bram Moolenaar0d660222005-01-07 21:51:51 +000013266 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013267 if (server == NULL || reply == NULL)
13268 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013269 if (check_restricted() || check_secure())
13270 return;
13271# ifdef FEAT_X11
13272 if (check_connection() == FAIL)
13273 return;
13274# endif
13275
13276 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013278 EMSG(_("E258: Unable to send to client"));
13279 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013280 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013281 rettv->vval.v_number = 0;
13282#else
13283 rettv->vval.v_number = -1;
13284#endif
13285}
13286
13287/*ARGSUSED*/
13288 static void
13289f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013290 typval_T *argvars;
13291 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013292{
13293 char_u *r = NULL;
13294
13295#ifdef FEAT_CLIENTSERVER
13296# ifdef WIN32
13297 r = serverGetVimNames();
13298# else
13299 make_connection();
13300 if (X_DISPLAY != NULL)
13301 r = serverGetVimNames(X_DISPLAY);
13302# endif
13303#endif
13304 rettv->v_type = VAR_STRING;
13305 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306}
13307
13308/*
13309 * "setbufvar()" function
13310 */
13311/*ARGSUSED*/
13312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013313f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013314 typval_T *argvars;
13315 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316{
13317 buf_T *buf;
13318#ifdef FEAT_AUTOCMD
13319 aco_save_T aco;
13320#else
13321 buf_T *save_curbuf;
13322#endif
13323 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013324 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325 char_u nbuf[NUMBUFLEN];
13326
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013327 rettv->vval.v_number = 0;
13328
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329 if (check_restricted() || check_secure())
13330 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013331 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13332 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013333 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013334 varp = &argvars[2];
13335
13336 if (buf != NULL && varname != NULL && varp != NULL)
13337 {
13338 /* set curbuf to be our buf, temporarily */
13339#ifdef FEAT_AUTOCMD
13340 aucmd_prepbuf(&aco, buf);
13341#else
13342 save_curbuf = curbuf;
13343 curbuf = buf;
13344#endif
13345
13346 if (*varname == '&')
13347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013348 long numval;
13349 char_u *strval;
13350 int error = FALSE;
13351
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013353 numval = get_tv_number_chk(varp, &error);
13354 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013355 if (!error && strval != NULL)
13356 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357 }
13358 else
13359 {
13360 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13361 if (bufvarname != NULL)
13362 {
13363 STRCPY(bufvarname, "b:");
13364 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013365 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366 vim_free(bufvarname);
13367 }
13368 }
13369
13370 /* reset notion of buffer */
13371#ifdef FEAT_AUTOCMD
13372 aucmd_restbuf(&aco);
13373#else
13374 curbuf = save_curbuf;
13375#endif
13376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377}
13378
13379/*
13380 * "setcmdpos()" function
13381 */
13382 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013383f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013384 typval_T *argvars;
13385 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013386{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013387 int pos = (int)get_tv_number(&argvars[0]) - 1;
13388
13389 if (pos >= 0)
13390 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391}
13392
13393/*
13394 * "setline()" function
13395 */
13396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013397f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013398 typval_T *argvars;
13399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013400{
13401 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013402 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013403 list_T *l = NULL;
13404 listitem_T *li = NULL;
13405 long added = 0;
13406 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013408 lnum = get_tv_lnum(&argvars[0]);
13409 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013410 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013411 l = argvars[1].vval.v_list;
13412 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013414 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013415 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013416
13417 rettv->vval.v_number = 0; /* OK */
13418 for (;;)
13419 {
13420 if (l != NULL)
13421 {
13422 /* list argument, get next string */
13423 if (li == NULL)
13424 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013425 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013426 li = li->li_next;
13427 }
13428
13429 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013430 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013431 break;
13432 if (lnum <= curbuf->b_ml.ml_line_count)
13433 {
13434 /* existing line, replace it */
13435 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13436 {
13437 changed_bytes(lnum, 0);
13438 check_cursor_col();
13439 rettv->vval.v_number = 0; /* OK */
13440 }
13441 }
13442 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13443 {
13444 /* lnum is one past the last line, append the line */
13445 ++added;
13446 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13447 rettv->vval.v_number = 0; /* OK */
13448 }
13449
13450 if (l == NULL) /* only one string argument */
13451 break;
13452 ++lnum;
13453 }
13454
13455 if (added > 0)
13456 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013457}
13458
13459/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013460 * "setqflist()" function
13461 */
13462/*ARGSUSED*/
13463 static void
13464f_setqflist(argvars, rettv)
13465 typval_T *argvars;
13466 typval_T *rettv;
13467{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013468 char_u *act;
13469 int action = ' ';
13470
Bram Moolenaar2641f772005-03-25 21:58:17 +000013471 rettv->vval.v_number = -1;
13472
13473#ifdef FEAT_QUICKFIX
13474 if (argvars[0].v_type != VAR_LIST)
13475 EMSG(_(e_listreq));
13476 else
13477 {
13478 list_T *l = argvars[0].vval.v_list;
13479
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013480 if (argvars[1].v_type == VAR_STRING)
13481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013482 act = get_tv_string_chk(&argvars[1]);
13483 if (act == NULL)
13484 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013485 if (*act == 'a' || *act == 'r')
13486 action = *act;
13487 }
13488
13489 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013490 rettv->vval.v_number = 0;
13491 }
13492#endif
13493}
13494
13495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496 * "setreg()" function
13497 */
13498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013499f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013500 typval_T *argvars;
13501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502{
13503 int regname;
13504 char_u *strregname;
13505 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013506 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507 int append;
13508 char_u yank_type;
13509 long block_len;
13510
13511 block_len = -1;
13512 yank_type = MAUTO;
13513 append = FALSE;
13514
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013515 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013516 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013518 if (strregname == NULL)
13519 return; /* type error; errmsg already given */
13520 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013521 if (regname == 0 || regname == '@')
13522 regname = '"';
13523 else if (regname == '=')
13524 return;
13525
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013526 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013527 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013528 stropt = get_tv_string_chk(&argvars[2]);
13529 if (stropt == NULL)
13530 return; /* type error */
13531 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013532 switch (*stropt)
13533 {
13534 case 'a': case 'A': /* append */
13535 append = TRUE;
13536 break;
13537 case 'v': case 'c': /* character-wise selection */
13538 yank_type = MCHAR;
13539 break;
13540 case 'V': case 'l': /* line-wise selection */
13541 yank_type = MLINE;
13542 break;
13543#ifdef FEAT_VISUAL
13544 case 'b': case Ctrl_V: /* block-wise selection */
13545 yank_type = MBLOCK;
13546 if (VIM_ISDIGIT(stropt[1]))
13547 {
13548 ++stropt;
13549 block_len = getdigits(&stropt) - 1;
13550 --stropt;
13551 }
13552 break;
13553#endif
13554 }
13555 }
13556
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013557 strval = get_tv_string_chk(&argvars[1]);
13558 if (strval != NULL)
13559 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013561 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013562}
13563
13564
13565/*
13566 * "setwinvar(expr)" function
13567 */
13568/*ARGSUSED*/
13569 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013570f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013571 typval_T *argvars;
13572 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573{
13574 win_T *win;
13575#ifdef FEAT_WINDOWS
13576 win_T *save_curwin;
13577#endif
13578 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013579 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580 char_u nbuf[NUMBUFLEN];
13581
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013582 rettv->vval.v_number = 0;
13583
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584 if (check_restricted() || check_secure())
13585 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013587 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588 varp = &argvars[2];
13589
13590 if (win != NULL && varname != NULL && varp != NULL)
13591 {
13592#ifdef FEAT_WINDOWS
13593 /* set curwin to be our win, temporarily */
13594 save_curwin = curwin;
13595 curwin = win;
13596 curbuf = curwin->w_buffer;
13597#endif
13598
13599 if (*varname == '&')
13600 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013601 long numval;
13602 char_u *strval;
13603 int error = FALSE;
13604
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013606 numval = get_tv_number_chk(varp, &error);
13607 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013608 if (!error && strval != NULL)
13609 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610 }
13611 else
13612 {
13613 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13614 if (winvarname != NULL)
13615 {
13616 STRCPY(winvarname, "w:");
13617 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013618 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619 vim_free(winvarname);
13620 }
13621 }
13622
13623#ifdef FEAT_WINDOWS
13624 /* Restore current window, if it's still valid (autocomands can make
13625 * it invalid). */
13626 if (win_valid(save_curwin))
13627 {
13628 curwin = save_curwin;
13629 curbuf = curwin->w_buffer;
13630 }
13631#endif
13632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633}
13634
13635/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013636 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 */
13638 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013639f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013640 typval_T *argvars;
13641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013643 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644
Bram Moolenaar0d660222005-01-07 21:51:51 +000013645 p = get_tv_string(&argvars[0]);
13646 rettv->vval.v_string = vim_strsave(p);
13647 simplify_filename(rettv->vval.v_string); /* simplify in place */
13648 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649}
13650
Bram Moolenaar0d660222005-01-07 21:51:51 +000013651static int
13652#ifdef __BORLANDC__
13653 _RTLENTRYF
13654#endif
13655 item_compare __ARGS((const void *s1, const void *s2));
13656static int
13657#ifdef __BORLANDC__
13658 _RTLENTRYF
13659#endif
13660 item_compare2 __ARGS((const void *s1, const void *s2));
13661
13662static int item_compare_ic;
13663static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013664static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013665#define ITEM_COMPARE_FAIL 999
13666
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013668 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013670 static int
13671#ifdef __BORLANDC__
13672_RTLENTRYF
13673#endif
13674item_compare(s1, s2)
13675 const void *s1;
13676 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013678 char_u *p1, *p2;
13679 char_u *tofree1, *tofree2;
13680 int res;
13681 char_u numbuf1[NUMBUFLEN];
13682 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683
Bram Moolenaar33570922005-01-25 22:26:29 +000013684 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13685 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013686 if (item_compare_ic)
13687 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013689 res = STRCMP(p1, p2);
13690 vim_free(tofree1);
13691 vim_free(tofree2);
13692 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693}
13694
13695 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013696#ifdef __BORLANDC__
13697_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013698#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013699item_compare2(s1, s2)
13700 const void *s1;
13701 const void *s2;
13702{
13703 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013704 typval_T rettv;
13705 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013706 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013708 /* shortcut after failure in previous call; compare all items equal */
13709 if (item_compare_func_err)
13710 return 0;
13711
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013712 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13713 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013714 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13715 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013716
13717 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13718 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013719 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013720 clear_tv(&argv[0]);
13721 clear_tv(&argv[1]);
13722
13723 if (res == FAIL)
13724 res = ITEM_COMPARE_FAIL;
13725 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013726 /* return value has wrong type */
13727 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13728 if (item_compare_func_err)
13729 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013730 clear_tv(&rettv);
13731 return res;
13732}
13733
13734/*
13735 * "sort({list})" function
13736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013738f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013739 typval_T *argvars;
13740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741{
Bram Moolenaar33570922005-01-25 22:26:29 +000013742 list_T *l;
13743 listitem_T *li;
13744 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013745 long len;
13746 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747
Bram Moolenaar0d660222005-01-07 21:51:51 +000013748 rettv->vval.v_number = 0;
13749 if (argvars[0].v_type != VAR_LIST)
13750 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751 else
13752 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013753 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013754 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013755 return;
13756 rettv->vval.v_list = l;
13757 rettv->v_type = VAR_LIST;
13758 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759
Bram Moolenaar0d660222005-01-07 21:51:51 +000013760 len = list_len(l);
13761 if (len <= 1)
13762 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763
Bram Moolenaar0d660222005-01-07 21:51:51 +000013764 item_compare_ic = FALSE;
13765 item_compare_func = NULL;
13766 if (argvars[1].v_type != VAR_UNKNOWN)
13767 {
13768 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013769 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013770 else
13771 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013772 int error = FALSE;
13773
13774 i = get_tv_number_chk(&argvars[1], &error);
13775 if (error)
13776 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013777 if (i == 1)
13778 item_compare_ic = TRUE;
13779 else
13780 item_compare_func = get_tv_string(&argvars[1]);
13781 }
13782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013783
Bram Moolenaar0d660222005-01-07 21:51:51 +000013784 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013785 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013786 if (ptrs == NULL)
13787 return;
13788 i = 0;
13789 for (li = l->lv_first; li != NULL; li = li->li_next)
13790 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013792 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013793 /* test the compare function */
13794 if (item_compare_func != NULL
13795 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13796 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013797 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013799 {
13800 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013801 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013802 item_compare_func == NULL ? item_compare : item_compare2);
13803
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013804 if (!item_compare_func_err)
13805 {
13806 /* Clear the List and append the items in the sorted order. */
13807 l->lv_first = l->lv_last = NULL;
13808 l->lv_len = 0;
13809 for (i = 0; i < len; ++i)
13810 list_append(l, ptrs[i]);
13811 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013812 }
13813
13814 vim_free(ptrs);
13815 }
13816}
13817
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013818/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013819 * "soundfold({word})" function
13820 */
13821 static void
13822f_soundfold(argvars, rettv)
13823 typval_T *argvars;
13824 typval_T *rettv;
13825{
13826 char_u *s;
13827
13828 rettv->v_type = VAR_STRING;
13829 s = get_tv_string(&argvars[0]);
13830#ifdef FEAT_SYN_HL
13831 rettv->vval.v_string = eval_soundfold(s);
13832#else
13833 rettv->vval.v_string = vim_strsave(s);
13834#endif
13835}
13836
13837/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013838 * "spellbadword()" function
13839 */
13840/* ARGSUSED */
13841 static void
13842f_spellbadword(argvars, rettv)
13843 typval_T *argvars;
13844 typval_T *rettv;
13845{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013846 int len;
13847
13848 rettv->vval.v_string = NULL;
13849 rettv->v_type = VAR_STRING;
13850
13851#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +000013852 /* Find the start and length of the badly spelled word. */
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000013853 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +000013854 if (len != 0)
13855 rettv->vval.v_string = vim_strnsave(ml_get_cursor(), len);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013856#endif
13857}
13858
13859/*
13860 * "spellsuggest()" function
13861 */
13862 static void
13863f_spellsuggest(argvars, rettv)
13864 typval_T *argvars;
13865 typval_T *rettv;
13866{
13867 char_u *str;
13868 int maxcount;
13869 garray_T ga;
13870 list_T *l;
13871 listitem_T *li;
13872 int i;
13873
13874 l = list_alloc();
13875 if (l == NULL)
13876 return;
13877 rettv->v_type = VAR_LIST;
13878 rettv->vval.v_list = l;
13879 ++l->lv_refcount;
13880
13881#ifdef FEAT_SYN_HL
13882 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13883 {
13884 str = get_tv_string(&argvars[0]);
13885 if (argvars[1].v_type != VAR_UNKNOWN)
13886 {
13887 maxcount = get_tv_number(&argvars[1]);
13888 if (maxcount <= 0)
13889 return;
13890 }
13891 else
13892 maxcount = 25;
13893
Bram Moolenaar8c45cdf2005-08-11 20:11:38 +000013894 spell_suggest_list(&ga, str, maxcount, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013895
13896 for (i = 0; i < ga.ga_len; ++i)
13897 {
13898 str = ((char_u **)ga.ga_data)[i];
13899
13900 li = listitem_alloc();
13901 if (li == NULL)
13902 vim_free(str);
13903 else
13904 {
13905 li->li_tv.v_type = VAR_STRING;
13906 li->li_tv.v_lock = 0;
13907 li->li_tv.vval.v_string = str;
13908 list_append(l, li);
13909 }
13910 }
13911 ga_clear(&ga);
13912 }
13913#endif
13914}
13915
Bram Moolenaar0d660222005-01-07 21:51:51 +000013916 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013917f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013918 typval_T *argvars;
13919 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013920{
13921 char_u *str;
13922 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013923 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013924 regmatch_T regmatch;
13925 char_u patbuf[NUMBUFLEN];
13926 char_u *save_cpo;
13927 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000013928 listitem_T *ni;
13929 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013930 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013931 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013932 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013933
13934 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13935 save_cpo = p_cpo;
13936 p_cpo = (char_u *)"";
13937
13938 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013939 if (argvars[1].v_type != VAR_UNKNOWN)
13940 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013941 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13942 if (pat == NULL)
13943 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013944 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013945 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013946 }
13947 if (pat == NULL || *pat == NUL)
13948 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000013949
13950 l = list_alloc();
13951 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013953 rettv->v_type = VAR_LIST;
13954 rettv->vval.v_list = l;
13955 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013956 if (typeerr)
13957 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958
Bram Moolenaar0d660222005-01-07 21:51:51 +000013959 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13960 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013962 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013963 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013964 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013965 if (*str == NUL)
13966 match = FALSE; /* empty item at the end */
13967 else
13968 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013969 if (match)
13970 end = regmatch.startp[0];
13971 else
13972 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013973 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
13974 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013975 {
13976 ni = listitem_alloc();
13977 if (ni == NULL)
13978 break;
13979 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013980 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013981 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
13982 list_append(l, ni);
13983 }
13984 if (!match)
13985 break;
13986 /* Advance to just after the match. */
13987 if (regmatch.endp[0] > str)
13988 col = 0;
13989 else
13990 {
13991 /* Don't get stuck at the same match. */
13992#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013993 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013994#else
13995 col = 1;
13996#endif
13997 }
13998 str = regmatch.endp[0];
13999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014000
Bram Moolenaar0d660222005-01-07 21:51:51 +000014001 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003
Bram Moolenaar0d660222005-01-07 21:51:51 +000014004 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005}
14006
14007#ifdef HAVE_STRFTIME
14008/*
14009 * "strftime({format}[, {time}])" function
14010 */
14011 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014012f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014013 typval_T *argvars;
14014 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015{
14016 char_u result_buf[256];
14017 struct tm *curtime;
14018 time_t seconds;
14019 char_u *p;
14020
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014021 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014023 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014024 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025 seconds = time(NULL);
14026 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014027 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014028 curtime = localtime(&seconds);
14029 /* MSVC returns NULL for an invalid value of seconds. */
14030 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014031 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032 else
14033 {
14034# ifdef FEAT_MBYTE
14035 vimconv_T conv;
14036 char_u *enc;
14037
14038 conv.vc_type = CONV_NONE;
14039 enc = enc_locale();
14040 convert_setup(&conv, p_enc, enc);
14041 if (conv.vc_type != CONV_NONE)
14042 p = string_convert(&conv, p, NULL);
14043# endif
14044 if (p != NULL)
14045 (void)strftime((char *)result_buf, sizeof(result_buf),
14046 (char *)p, curtime);
14047 else
14048 result_buf[0] = NUL;
14049
14050# ifdef FEAT_MBYTE
14051 if (conv.vc_type != CONV_NONE)
14052 vim_free(p);
14053 convert_setup(&conv, enc, p_enc);
14054 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014055 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056 else
14057# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014058 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059
14060# ifdef FEAT_MBYTE
14061 /* Release conversion descriptors */
14062 convert_setup(&conv, NULL, NULL);
14063 vim_free(enc);
14064# endif
14065 }
14066}
14067#endif
14068
14069/*
14070 * "stridx()" function
14071 */
14072 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014073f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014074 typval_T *argvars;
14075 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076{
14077 char_u buf[NUMBUFLEN];
14078 char_u *needle;
14079 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014080 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014082 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014084 needle = get_tv_string_chk(&argvars[1]);
14085 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014086 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014087 if (needle == NULL || haystack == NULL)
14088 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089
Bram Moolenaar33570922005-01-25 22:26:29 +000014090 if (argvars[2].v_type != VAR_UNKNOWN)
14091 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014092 int error = FALSE;
14093
14094 start_idx = get_tv_number_chk(&argvars[2], &error);
14095 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014096 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014097 if (start_idx >= 0)
14098 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014099 }
14100
14101 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14102 if (pos != NULL)
14103 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104}
14105
14106/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014107 * "string()" function
14108 */
14109 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014110f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014111 typval_T *argvars;
14112 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014113{
14114 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014115 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014116
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014117 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014118 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014119 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014120 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121}
14122
14123/*
14124 * "strlen()" function
14125 */
14126 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014127f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014128 typval_T *argvars;
14129 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014131 rettv->vval.v_number = (varnumber_T)(STRLEN(
14132 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014133}
14134
14135/*
14136 * "strpart()" function
14137 */
14138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014139f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014140 typval_T *argvars;
14141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014142{
14143 char_u *p;
14144 int n;
14145 int len;
14146 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014147 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014149 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150 slen = (int)STRLEN(p);
14151
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014152 n = get_tv_number_chk(&argvars[1], &error);
14153 if (error)
14154 len = 0;
14155 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014156 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157 else
14158 len = slen - n; /* default len: all bytes that are available. */
14159
14160 /*
14161 * Only return the overlap between the specified part and the actual
14162 * string.
14163 */
14164 if (n < 0)
14165 {
14166 len += n;
14167 n = 0;
14168 }
14169 else if (n > slen)
14170 n = slen;
14171 if (len < 0)
14172 len = 0;
14173 else if (n + len > slen)
14174 len = slen - n;
14175
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014176 rettv->v_type = VAR_STRING;
14177 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014178}
14179
14180/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014181 * "strridx()" function
14182 */
14183 static void
14184f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014185 typval_T *argvars;
14186 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014187{
14188 char_u buf[NUMBUFLEN];
14189 char_u *needle;
14190 char_u *haystack;
14191 char_u *rest;
14192 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014193 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014194
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014195 needle = get_tv_string_chk(&argvars[1]);
14196 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014197 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014198
14199 rettv->vval.v_number = -1;
14200 if (needle == NULL || haystack == NULL)
14201 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014202 if (argvars[2].v_type != VAR_UNKNOWN)
14203 {
14204 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014205 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014206 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014207 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014208 }
14209 else
14210 end_idx = haystack_len;
14211
Bram Moolenaar0d660222005-01-07 21:51:51 +000014212 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014213 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014214 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014215 lastmatch = haystack + end_idx;
14216 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014217 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014218 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014219 for (rest = haystack; *rest != '\0'; ++rest)
14220 {
14221 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014222 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014223 break;
14224 lastmatch = rest;
14225 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014226 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014227
14228 if (lastmatch == NULL)
14229 rettv->vval.v_number = -1;
14230 else
14231 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14232}
14233
14234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235 * "strtrans()" function
14236 */
14237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014238f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014239 typval_T *argvars;
14240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014242 rettv->v_type = VAR_STRING;
14243 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244}
14245
14246/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014247 * "submatch()" function
14248 */
14249 static void
14250f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014251 typval_T *argvars;
14252 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014253{
14254 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014255 rettv->vval.v_string =
14256 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014257}
14258
14259/*
14260 * "substitute()" function
14261 */
14262 static void
14263f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014264 typval_T *argvars;
14265 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014266{
14267 char_u patbuf[NUMBUFLEN];
14268 char_u subbuf[NUMBUFLEN];
14269 char_u flagsbuf[NUMBUFLEN];
14270
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014271 char_u *str = get_tv_string_chk(&argvars[0]);
14272 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14273 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14274 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14275
Bram Moolenaar0d660222005-01-07 21:51:51 +000014276 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014277 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14278 rettv->vval.v_string = NULL;
14279 else
14280 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014281}
14282
14283/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014284 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285 */
14286/*ARGSUSED*/
14287 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014288f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014289 typval_T *argvars;
14290 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291{
14292 int id = 0;
14293#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014294 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295 long col;
14296 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014297 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014299 lnum = get_tv_lnum(argvars); /* -1 on type error */
14300 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14301 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014302
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014303 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014304 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014305 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014306#endif
14307
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014308 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309}
14310
14311/*
14312 * "synIDattr(id, what [, mode])" function
14313 */
14314/*ARGSUSED*/
14315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014316f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014317 typval_T *argvars;
14318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319{
14320 char_u *p = NULL;
14321#ifdef FEAT_SYN_HL
14322 int id;
14323 char_u *what;
14324 char_u *mode;
14325 char_u modebuf[NUMBUFLEN];
14326 int modec;
14327
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014328 id = get_tv_number(&argvars[0]);
14329 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014330 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014332 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333 modec = TOLOWER_ASC(mode[0]);
14334 if (modec != 't' && modec != 'c'
14335#ifdef FEAT_GUI
14336 && modec != 'g'
14337#endif
14338 )
14339 modec = 0; /* replace invalid with current */
14340 }
14341 else
14342 {
14343#ifdef FEAT_GUI
14344 if (gui.in_use)
14345 modec = 'g';
14346 else
14347#endif
14348 if (t_colors > 1)
14349 modec = 'c';
14350 else
14351 modec = 't';
14352 }
14353
14354
14355 switch (TOLOWER_ASC(what[0]))
14356 {
14357 case 'b':
14358 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14359 p = highlight_color(id, what, modec);
14360 else /* bold */
14361 p = highlight_has_attr(id, HL_BOLD, modec);
14362 break;
14363
14364 case 'f': /* fg[#] */
14365 p = highlight_color(id, what, modec);
14366 break;
14367
14368 case 'i':
14369 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14370 p = highlight_has_attr(id, HL_INVERSE, modec);
14371 else /* italic */
14372 p = highlight_has_attr(id, HL_ITALIC, modec);
14373 break;
14374
14375 case 'n': /* name */
14376 p = get_highlight_name(NULL, id - 1);
14377 break;
14378
14379 case 'r': /* reverse */
14380 p = highlight_has_attr(id, HL_INVERSE, modec);
14381 break;
14382
14383 case 's': /* standout */
14384 p = highlight_has_attr(id, HL_STANDOUT, modec);
14385 break;
14386
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014387 case 'u':
14388 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14389 /* underline */
14390 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14391 else
14392 /* undercurl */
14393 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014394 break;
14395 }
14396
14397 if (p != NULL)
14398 p = vim_strsave(p);
14399#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014400 rettv->v_type = VAR_STRING;
14401 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402}
14403
14404/*
14405 * "synIDtrans(id)" function
14406 */
14407/*ARGSUSED*/
14408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014409f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014410 typval_T *argvars;
14411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412{
14413 int id;
14414
14415#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014416 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417
14418 if (id > 0)
14419 id = syn_get_final_id(id);
14420 else
14421#endif
14422 id = 0;
14423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014424 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425}
14426
14427/*
14428 * "system()" function
14429 */
14430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014431f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014432 typval_T *argvars;
14433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014434{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014435 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014437 char_u *infile = NULL;
14438 char_u buf[NUMBUFLEN];
14439 int err = FALSE;
14440 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014442 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014443 {
14444 /*
14445 * Write the string to a temp file, to be used for input of the shell
14446 * command.
14447 */
14448 if ((infile = vim_tempname('i')) == NULL)
14449 {
14450 EMSG(_(e_notmp));
14451 return;
14452 }
14453
14454 fd = mch_fopen((char *)infile, WRITEBIN);
14455 if (fd == NULL)
14456 {
14457 EMSG2(_(e_notopen), infile);
14458 goto done;
14459 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014460 p = get_tv_string_buf_chk(&argvars[1], buf);
14461 if (p == NULL)
14462 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014463 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14464 err = TRUE;
14465 if (fclose(fd) != 0)
14466 err = TRUE;
14467 if (err)
14468 {
14469 EMSG(_("E677: Error writing temp file"));
14470 goto done;
14471 }
14472 }
14473
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014474 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014475
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476#ifdef USE_CR
14477 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014478 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479 {
14480 char_u *s;
14481
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014482 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483 {
14484 if (*s == CAR)
14485 *s = NL;
14486 }
14487 }
14488#else
14489# ifdef USE_CRNL
14490 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014491 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492 {
14493 char_u *s, *d;
14494
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014495 d = res;
14496 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497 {
14498 if (s[0] == CAR && s[1] == NL)
14499 ++s;
14500 *d++ = *s;
14501 }
14502 *d = NUL;
14503 }
14504# endif
14505#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014506
14507done:
14508 if (infile != NULL)
14509 {
14510 mch_remove(infile);
14511 vim_free(infile);
14512 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014513 rettv->v_type = VAR_STRING;
14514 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515}
14516
14517/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014518 * "tagfiles()" function
14519 */
14520/*ARGSUSED*/
14521 static void
14522f_tagfiles(argvars, rettv)
14523 typval_T *argvars;
14524 typval_T *rettv;
14525{
14526 char_u fname[MAXPATHL + 1];
14527 list_T *l;
14528
14529 l = list_alloc();
14530 if (l == NULL)
14531 {
14532 rettv->vval.v_number = 0;
14533 return;
14534 }
14535 rettv->vval.v_list = l;
14536 rettv->v_type = VAR_LIST;
14537 ++l->lv_refcount;
14538
14539 get_tagfname(TRUE, NULL);
14540 for (;;)
14541 if (get_tagfname(FALSE, fname) == FAIL
14542 || list_append_string(l, fname) == FAIL)
14543 break;
14544}
14545
14546/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014547 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014548 */
14549 static void
14550f_taglist(argvars, rettv)
14551 typval_T *argvars;
14552 typval_T *rettv;
14553{
14554 char_u *tag_pattern;
14555 list_T *l;
14556
14557 tag_pattern = get_tv_string(&argvars[0]);
14558
14559 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014560 if (*tag_pattern == NUL)
14561 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014562
14563 l = list_alloc();
14564 if (l != NULL)
14565 {
14566 if (get_tags(l, tag_pattern) != FAIL)
14567 {
14568 rettv->vval.v_list = l;
14569 rettv->v_type = VAR_LIST;
14570 ++l->lv_refcount;
14571 }
14572 else
14573 list_free(l);
14574 }
14575}
14576
14577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014578 * "tempname()" function
14579 */
14580/*ARGSUSED*/
14581 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014582f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014583 typval_T *argvars;
14584 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585{
14586 static int x = 'A';
14587
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014588 rettv->v_type = VAR_STRING;
14589 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590
14591 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14592 * names. Skip 'I' and 'O', they are used for shell redirection. */
14593 do
14594 {
14595 if (x == 'Z')
14596 x = '0';
14597 else if (x == '9')
14598 x = 'A';
14599 else
14600 {
14601#ifdef EBCDIC
14602 if (x == 'I')
14603 x = 'J';
14604 else if (x == 'R')
14605 x = 'S';
14606 else
14607#endif
14608 ++x;
14609 }
14610 } while (x == 'I' || x == 'O');
14611}
14612
14613/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000014614 * "test(list)" function: Just checking the walls...
14615 */
14616/*ARGSUSED*/
14617 static void
14618f_test(argvars, rettv)
14619 typval_T *argvars;
14620 typval_T *rettv;
14621{
14622 /* Used for unit testing. Change the code below to your liking. */
14623#if 0
14624 listitem_T *li;
14625 list_T *l;
14626 char_u *bad, *good;
14627
14628 if (argvars[0].v_type != VAR_LIST)
14629 return;
14630 l = argvars[0].vval.v_list;
14631 if (l == NULL)
14632 return;
14633 li = l->lv_first;
14634 if (li == NULL)
14635 return;
14636 bad = get_tv_string(&li->li_tv);
14637 li = li->li_next;
14638 if (li == NULL)
14639 return;
14640 good = get_tv_string(&li->li_tv);
14641 rettv->vval.v_number = test_edit_score(bad, good);
14642#endif
14643}
14644
14645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014646 * "tolower(string)" function
14647 */
14648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014649f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014650 typval_T *argvars;
14651 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652{
14653 char_u *p;
14654
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014655 p = vim_strsave(get_tv_string(&argvars[0]));
14656 rettv->v_type = VAR_STRING;
14657 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014658
14659 if (p != NULL)
14660 while (*p != NUL)
14661 {
14662#ifdef FEAT_MBYTE
14663 int l;
14664
14665 if (enc_utf8)
14666 {
14667 int c, lc;
14668
14669 c = utf_ptr2char(p);
14670 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014671 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672 /* TODO: reallocate string when byte count changes. */
14673 if (utf_char2len(lc) == l)
14674 utf_char2bytes(lc, p);
14675 p += l;
14676 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014677 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014678 p += l; /* skip multi-byte character */
14679 else
14680#endif
14681 {
14682 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14683 ++p;
14684 }
14685 }
14686}
14687
14688/*
14689 * "toupper(string)" function
14690 */
14691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014692f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014693 typval_T *argvars;
14694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014695{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014696 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014697 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014698}
14699
14700/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014701 * "tr(string, fromstr, tostr)" function
14702 */
14703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014704f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014705 typval_T *argvars;
14706 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014707{
14708 char_u *instr;
14709 char_u *fromstr;
14710 char_u *tostr;
14711 char_u *p;
14712#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000014713 int inlen;
14714 int fromlen;
14715 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014716 int idx;
14717 char_u *cpstr;
14718 int cplen;
14719 int first = TRUE;
14720#endif
14721 char_u buf[NUMBUFLEN];
14722 char_u buf2[NUMBUFLEN];
14723 garray_T ga;
14724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014725 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014726 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14727 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014728
14729 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014730 rettv->v_type = VAR_STRING;
14731 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014732 if (fromstr == NULL || tostr == NULL)
14733 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014734 ga_init2(&ga, (int)sizeof(char), 80);
14735
14736#ifdef FEAT_MBYTE
14737 if (!has_mbyte)
14738#endif
14739 /* not multi-byte: fromstr and tostr must be the same length */
14740 if (STRLEN(fromstr) != STRLEN(tostr))
14741 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014742#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014743error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014744#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014745 EMSG2(_(e_invarg2), fromstr);
14746 ga_clear(&ga);
14747 return;
14748 }
14749
14750 /* fromstr and tostr have to contain the same number of chars */
14751 while (*instr != NUL)
14752 {
14753#ifdef FEAT_MBYTE
14754 if (has_mbyte)
14755 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014756 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014757 cpstr = instr;
14758 cplen = inlen;
14759 idx = 0;
14760 for (p = fromstr; *p != NUL; p += fromlen)
14761 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014762 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014763 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14764 {
14765 for (p = tostr; *p != NUL; p += tolen)
14766 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014767 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014768 if (idx-- == 0)
14769 {
14770 cplen = tolen;
14771 cpstr = p;
14772 break;
14773 }
14774 }
14775 if (*p == NUL) /* tostr is shorter than fromstr */
14776 goto error;
14777 break;
14778 }
14779 ++idx;
14780 }
14781
14782 if (first && cpstr == instr)
14783 {
14784 /* Check that fromstr and tostr have the same number of
14785 * (multi-byte) characters. Done only once when a character
14786 * of instr doesn't appear in fromstr. */
14787 first = FALSE;
14788 for (p = tostr; *p != NUL; p += tolen)
14789 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014790 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014791 --idx;
14792 }
14793 if (idx != 0)
14794 goto error;
14795 }
14796
14797 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014798 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014799 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014800
14801 instr += inlen;
14802 }
14803 else
14804#endif
14805 {
14806 /* When not using multi-byte chars we can do it faster. */
14807 p = vim_strchr(fromstr, *instr);
14808 if (p != NULL)
14809 ga_append(&ga, tostr[p - fromstr]);
14810 else
14811 ga_append(&ga, *instr);
14812 ++instr;
14813 }
14814 }
14815
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014816 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014817}
14818
14819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820 * "type(expr)" function
14821 */
14822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014823f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014824 typval_T *argvars;
14825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014826{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014827 int n;
14828
14829 switch (argvars[0].v_type)
14830 {
14831 case VAR_NUMBER: n = 0; break;
14832 case VAR_STRING: n = 1; break;
14833 case VAR_FUNC: n = 2; break;
14834 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014835 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014836 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14837 }
14838 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014839}
14840
14841/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014842 * "values(dict)" function
14843 */
14844 static void
14845f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014846 typval_T *argvars;
14847 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014848{
14849 dict_list(argvars, rettv, 1);
14850}
14851
14852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853 * "virtcol(string)" function
14854 */
14855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014856f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014857 typval_T *argvars;
14858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014859{
14860 colnr_T vcol = 0;
14861 pos_T *fp;
14862
14863 fp = var2fpos(&argvars[0], FALSE);
14864 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14865 {
14866 getvvcol(curwin, fp, NULL, NULL, &vcol);
14867 ++vcol;
14868 }
14869
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014870 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014871}
14872
14873/*
14874 * "visualmode()" function
14875 */
14876/*ARGSUSED*/
14877 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014878f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014879 typval_T *argvars;
14880 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014881{
14882#ifdef FEAT_VISUAL
14883 char_u str[2];
14884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014885 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014886 str[0] = curbuf->b_visual_mode_eval;
14887 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014888 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014889
14890 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014891 if ((argvars[0].v_type == VAR_NUMBER
14892 && argvars[0].vval.v_number != 0)
14893 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014894 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895 curbuf->b_visual_mode_eval = NUL;
14896#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014897 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014898#endif
14899}
14900
14901/*
14902 * "winbufnr(nr)" function
14903 */
14904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014905f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014906 typval_T *argvars;
14907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908{
14909 win_T *wp;
14910
14911 wp = find_win_by_nr(&argvars[0]);
14912 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014913 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014914 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014915 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014916}
14917
14918/*
14919 * "wincol()" function
14920 */
14921/*ARGSUSED*/
14922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014923f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014924 typval_T *argvars;
14925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014926{
14927 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014928 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014929}
14930
14931/*
14932 * "winheight(nr)" function
14933 */
14934 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014935f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014936 typval_T *argvars;
14937 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014938{
14939 win_T *wp;
14940
14941 wp = find_win_by_nr(&argvars[0]);
14942 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014943 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014944 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014945 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014946}
14947
14948/*
14949 * "winline()" function
14950 */
14951/*ARGSUSED*/
14952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014953f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014954 typval_T *argvars;
14955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014956{
14957 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014958 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959}
14960
14961/*
14962 * "winnr()" function
14963 */
14964/* ARGSUSED */
14965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014966f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014967 typval_T *argvars;
14968 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014969{
14970 int nr = 1;
14971#ifdef FEAT_WINDOWS
14972 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014973 win_T *twin = curwin;
14974 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014975
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014976 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014977 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014978 arg = get_tv_string_chk(&argvars[0]);
14979 if (arg == NULL)
14980 nr = 0; /* type error; errmsg already given */
14981 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014982 twin = lastwin;
14983 else if (STRCMP(arg, "#") == 0)
14984 {
14985 twin = prevwin;
14986 if (prevwin == NULL)
14987 nr = 0;
14988 }
14989 else
14990 {
14991 EMSG2(_(e_invexpr2), arg);
14992 nr = 0;
14993 }
14994 }
14995
14996 if (nr > 0)
14997 for (wp = firstwin; wp != twin; wp = wp->w_next)
14998 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014999#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015000 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015001}
15002
15003/*
15004 * "winrestcmd()" function
15005 */
15006/* ARGSUSED */
15007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015008f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015009 typval_T *argvars;
15010 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011{
15012#ifdef FEAT_WINDOWS
15013 win_T *wp;
15014 int winnr = 1;
15015 garray_T ga;
15016 char_u buf[50];
15017
15018 ga_init2(&ga, (int)sizeof(char), 70);
15019 for (wp = firstwin; wp != NULL; wp = wp->w_next)
15020 {
15021 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
15022 ga_concat(&ga, buf);
15023# ifdef FEAT_VERTSPLIT
15024 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
15025 ga_concat(&ga, buf);
15026# endif
15027 ++winnr;
15028 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000015029 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015030
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015031 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015033 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015035 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015036}
15037
15038/*
15039 * "winwidth(nr)" function
15040 */
15041 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015042f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015043 typval_T *argvars;
15044 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045{
15046 win_T *wp;
15047
15048 wp = find_win_by_nr(&argvars[0]);
15049 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015050 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015051 else
15052#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015053 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015055 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015056#endif
15057}
15058
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015060 * "writefile()" function
15061 */
15062 static void
15063f_writefile(argvars, rettv)
15064 typval_T *argvars;
15065 typval_T *rettv;
15066{
15067 int binary = FALSE;
15068 char_u *fname;
15069 FILE *fd;
15070 listitem_T *li;
15071 char_u *s;
15072 int ret = 0;
15073 int c;
15074
15075 if (argvars[0].v_type != VAR_LIST)
15076 {
15077 EMSG2(_(e_listarg), "writefile()");
15078 return;
15079 }
15080 if (argvars[0].vval.v_list == NULL)
15081 return;
15082
15083 if (argvars[2].v_type != VAR_UNKNOWN
15084 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15085 binary = TRUE;
15086
15087 /* Always open the file in binary mode, library functions have a mind of
15088 * their own about CR-LF conversion. */
15089 fname = get_tv_string(&argvars[1]);
15090 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15091 {
15092 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15093 ret = -1;
15094 }
15095 else
15096 {
15097 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15098 li = li->li_next)
15099 {
15100 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15101 {
15102 if (*s == '\n')
15103 c = putc(NUL, fd);
15104 else
15105 c = putc(*s, fd);
15106 if (c == EOF)
15107 {
15108 ret = -1;
15109 break;
15110 }
15111 }
15112 if (!binary || li->li_next != NULL)
15113 if (putc('\n', fd) == EOF)
15114 {
15115 ret = -1;
15116 break;
15117 }
15118 if (ret < 0)
15119 {
15120 EMSG(_(e_write));
15121 break;
15122 }
15123 }
15124 fclose(fd);
15125 }
15126
15127 rettv->vval.v_number = ret;
15128}
15129
15130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015131 * Translate a String variable into a position.
15132 */
15133 static pos_T *
15134var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015135 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015136 int lnum; /* TRUE when $ is last line */
15137{
15138 char_u *name;
15139 static pos_T pos;
15140 pos_T *pp;
15141
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015142 name = get_tv_string_chk(varp);
15143 if (name == NULL)
15144 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145 if (name[0] == '.') /* cursor */
15146 return &curwin->w_cursor;
15147 if (name[0] == '\'') /* mark */
15148 {
15149 pp = getmark(name[1], FALSE);
15150 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15151 return NULL;
15152 return pp;
15153 }
15154 if (name[0] == '$') /* last column or line */
15155 {
15156 if (lnum)
15157 {
15158 pos.lnum = curbuf->b_ml.ml_line_count;
15159 pos.col = 0;
15160 }
15161 else
15162 {
15163 pos.lnum = curwin->w_cursor.lnum;
15164 pos.col = (colnr_T)STRLEN(ml_get_curline());
15165 }
15166 return &pos;
15167 }
15168 return NULL;
15169}
15170
15171/*
15172 * Get the length of an environment variable name.
15173 * Advance "arg" to the first character after the name.
15174 * Return 0 for error.
15175 */
15176 static int
15177get_env_len(arg)
15178 char_u **arg;
15179{
15180 char_u *p;
15181 int len;
15182
15183 for (p = *arg; vim_isIDc(*p); ++p)
15184 ;
15185 if (p == *arg) /* no name found */
15186 return 0;
15187
15188 len = (int)(p - *arg);
15189 *arg = p;
15190 return len;
15191}
15192
15193/*
15194 * Get the length of the name of a function or internal variable.
15195 * "arg" is advanced to the first non-white character after the name.
15196 * Return 0 if something is wrong.
15197 */
15198 static int
15199get_id_len(arg)
15200 char_u **arg;
15201{
15202 char_u *p;
15203 int len;
15204
15205 /* Find the end of the name. */
15206 for (p = *arg; eval_isnamec(*p); ++p)
15207 ;
15208 if (p == *arg) /* no name found */
15209 return 0;
15210
15211 len = (int)(p - *arg);
15212 *arg = skipwhite(p);
15213
15214 return len;
15215}
15216
15217/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015218 * Get the length of the name of a variable or function.
15219 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015220 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015221 * Return -1 if curly braces expansion failed.
15222 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223 * If the name contains 'magic' {}'s, expand them and return the
15224 * expanded name in an allocated string via 'alias' - caller must free.
15225 */
15226 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015227get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015228 char_u **arg;
15229 char_u **alias;
15230 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015231 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015232{
15233 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015234 char_u *p;
15235 char_u *expr_start;
15236 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015237
15238 *alias = NULL; /* default to no alias */
15239
15240 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15241 && (*arg)[2] == (int)KE_SNR)
15242 {
15243 /* hard coded <SNR>, already translated */
15244 *arg += 3;
15245 return get_id_len(arg) + 3;
15246 }
15247 len = eval_fname_script(*arg);
15248 if (len > 0)
15249 {
15250 /* literal "<SID>", "s:" or "<SNR>" */
15251 *arg += len;
15252 }
15253
Bram Moolenaar071d4272004-06-13 20:20:40 +000015254 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015255 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015256 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015257 p = find_name_end(*arg, &expr_start, &expr_end,
15258 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015259 if (expr_start != NULL)
15260 {
15261 char_u *temp_string;
15262
15263 if (!evaluate)
15264 {
15265 len += (int)(p - *arg);
15266 *arg = skipwhite(p);
15267 return len;
15268 }
15269
15270 /*
15271 * Include any <SID> etc in the expanded string:
15272 * Thus the -len here.
15273 */
15274 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15275 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015276 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015277 *alias = temp_string;
15278 *arg = skipwhite(p);
15279 return (int)STRLEN(temp_string);
15280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015281
15282 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015283 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015284 EMSG2(_(e_invexpr2), *arg);
15285
15286 return len;
15287}
15288
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015289/*
15290 * Find the end of a variable or function name, taking care of magic braces.
15291 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15292 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015293 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015294 * Return a pointer to just after the name. Equal to "arg" if there is no
15295 * valid name.
15296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015297 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015298find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015299 char_u *arg;
15300 char_u **expr_start;
15301 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015302 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015304 int mb_nest = 0;
15305 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306 char_u *p;
15307
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015308 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015310 *expr_start = NULL;
15311 *expr_end = NULL;
15312 }
15313
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015314 /* Quick check for valid starting character. */
15315 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15316 return arg;
15317
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015318 for (p = arg; *p != NUL
15319 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015320 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015321 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015322 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015323 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015324 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015325 if (*p == '\'')
15326 {
15327 /* skip over 'string' to avoid counting [ and ] inside it. */
15328 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15329 ;
15330 if (*p == NUL)
15331 break;
15332 }
15333 else if (*p == '"')
15334 {
15335 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15336 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15337 if (*p == '\\' && p[1] != NUL)
15338 ++p;
15339 if (*p == NUL)
15340 break;
15341 }
15342
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015343 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015344 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015345 if (*p == '[')
15346 ++br_nest;
15347 else if (*p == ']')
15348 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015349 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015350
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015351 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015352 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015353 if (*p == '{')
15354 {
15355 mb_nest++;
15356 if (expr_start != NULL && *expr_start == NULL)
15357 *expr_start = p;
15358 }
15359 else if (*p == '}')
15360 {
15361 mb_nest--;
15362 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15363 *expr_end = p;
15364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366 }
15367
15368 return p;
15369}
15370
15371/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015372 * Expands out the 'magic' {}'s in a variable/function name.
15373 * Note that this can call itself recursively, to deal with
15374 * constructs like foo{bar}{baz}{bam}
15375 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15376 * "in_start" ^
15377 * "expr_start" ^
15378 * "expr_end" ^
15379 * "in_end" ^
15380 *
15381 * Returns a new allocated string, which the caller must free.
15382 * Returns NULL for failure.
15383 */
15384 static char_u *
15385make_expanded_name(in_start, expr_start, expr_end, in_end)
15386 char_u *in_start;
15387 char_u *expr_start;
15388 char_u *expr_end;
15389 char_u *in_end;
15390{
15391 char_u c1;
15392 char_u *retval = NULL;
15393 char_u *temp_result;
15394 char_u *nextcmd = NULL;
15395
15396 if (expr_end == NULL || in_end == NULL)
15397 return NULL;
15398 *expr_start = NUL;
15399 *expr_end = NUL;
15400 c1 = *in_end;
15401 *in_end = NUL;
15402
15403 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15404 if (temp_result != NULL && nextcmd == NULL)
15405 {
15406 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15407 + (in_end - expr_end) + 1));
15408 if (retval != NULL)
15409 {
15410 STRCPY(retval, in_start);
15411 STRCAT(retval, temp_result);
15412 STRCAT(retval, expr_end + 1);
15413 }
15414 }
15415 vim_free(temp_result);
15416
15417 *in_end = c1; /* put char back for error messages */
15418 *expr_start = '{';
15419 *expr_end = '}';
15420
15421 if (retval != NULL)
15422 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015423 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015424 if (expr_start != NULL)
15425 {
15426 /* Further expansion! */
15427 temp_result = make_expanded_name(retval, expr_start,
15428 expr_end, temp_result);
15429 vim_free(retval);
15430 retval = temp_result;
15431 }
15432 }
15433
15434 return retval;
15435}
15436
15437/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015439 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440 */
15441 static int
15442eval_isnamec(c)
15443 int c;
15444{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015445 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15446}
15447
15448/*
15449 * Return TRUE if character "c" can be used as the first character in a
15450 * variable or function name (excluding '{' and '}').
15451 */
15452 static int
15453eval_isnamec1(c)
15454 int c;
15455{
15456 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457}
15458
15459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460 * Set number v: variable to "val".
15461 */
15462 void
15463set_vim_var_nr(idx, val)
15464 int idx;
15465 long val;
15466{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015467 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468}
15469
15470/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015471 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472 */
15473 long
15474get_vim_var_nr(idx)
15475 int idx;
15476{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015477 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015478}
15479
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015480#if defined(FEAT_AUTOCMD) || defined(PROTO)
15481/*
15482 * Get string v: variable value. Uses a static buffer, can only be used once.
15483 */
15484 char_u *
15485get_vim_var_str(idx)
15486 int idx;
15487{
15488 return get_tv_string(&vimvars[idx].vv_tv);
15489}
15490#endif
15491
Bram Moolenaar071d4272004-06-13 20:20:40 +000015492/*
15493 * Set v:count, v:count1 and v:prevcount.
15494 */
15495 void
15496set_vcount(count, count1)
15497 long count;
15498 long count1;
15499{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015500 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15501 vimvars[VV_COUNT].vv_nr = count;
15502 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503}
15504
15505/*
15506 * Set string v: variable to a copy of "val".
15507 */
15508 void
15509set_vim_var_string(idx, val, len)
15510 int idx;
15511 char_u *val;
15512 int len; /* length of "val" to use or -1 (whole string) */
15513{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015514 /* Need to do this (at least) once, since we can't initialize a union.
15515 * Will always be invoked when "v:progname" is set. */
15516 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15517
Bram Moolenaare9a41262005-01-15 22:18:47 +000015518 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015520 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015521 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015522 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015524 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525}
15526
15527/*
15528 * Set v:register if needed.
15529 */
15530 void
15531set_reg_var(c)
15532 int c;
15533{
15534 char_u regname;
15535
15536 if (c == 0 || c == ' ')
15537 regname = '"';
15538 else
15539 regname = c;
15540 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015541 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542 set_vim_var_string(VV_REG, &regname, 1);
15543}
15544
15545/*
15546 * Get or set v:exception. If "oldval" == NULL, return the current value.
15547 * Otherwise, restore the value to "oldval" and return NULL.
15548 * Must always be called in pairs to save and restore v:exception! Does not
15549 * take care of memory allocations.
15550 */
15551 char_u *
15552v_exception(oldval)
15553 char_u *oldval;
15554{
15555 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015556 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557
Bram Moolenaare9a41262005-01-15 22:18:47 +000015558 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559 return NULL;
15560}
15561
15562/*
15563 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15564 * Otherwise, restore the value to "oldval" and return NULL.
15565 * Must always be called in pairs to save and restore v:throwpoint! Does not
15566 * take care of memory allocations.
15567 */
15568 char_u *
15569v_throwpoint(oldval)
15570 char_u *oldval;
15571{
15572 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015573 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015574
Bram Moolenaare9a41262005-01-15 22:18:47 +000015575 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576 return NULL;
15577}
15578
15579#if defined(FEAT_AUTOCMD) || defined(PROTO)
15580/*
15581 * Set v:cmdarg.
15582 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15583 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15584 * Must always be called in pairs!
15585 */
15586 char_u *
15587set_cmdarg(eap, oldarg)
15588 exarg_T *eap;
15589 char_u *oldarg;
15590{
15591 char_u *oldval;
15592 char_u *newval;
15593 unsigned len;
15594
Bram Moolenaare9a41262005-01-15 22:18:47 +000015595 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015596 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015597 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015598 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015599 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015600 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015601 }
15602
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015603 if (eap->force_bin == FORCE_BIN)
15604 len = 6;
15605 else if (eap->force_bin == FORCE_NOBIN)
15606 len = 8;
15607 else
15608 len = 0;
15609 if (eap->force_ff != 0)
15610 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15611# ifdef FEAT_MBYTE
15612 if (eap->force_enc != 0)
15613 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
15614# endif
15615
15616 newval = alloc(len + 1);
15617 if (newval == NULL)
15618 return NULL;
15619
15620 if (eap->force_bin == FORCE_BIN)
15621 sprintf((char *)newval, " ++bin");
15622 else if (eap->force_bin == FORCE_NOBIN)
15623 sprintf((char *)newval, " ++nobin");
15624 else
15625 *newval = NUL;
15626 if (eap->force_ff != 0)
15627 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15628 eap->cmd + eap->force_ff);
15629# ifdef FEAT_MBYTE
15630 if (eap->force_enc != 0)
15631 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15632 eap->cmd + eap->force_enc);
15633# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015634 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015635 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636}
15637#endif
15638
15639/*
15640 * Get the value of internal variable "name".
15641 * Return OK or FAIL.
15642 */
15643 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015644get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015645 char_u *name;
15646 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015647 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015648 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015649{
15650 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015651 typval_T *tv = NULL;
15652 typval_T atv;
15653 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655
15656 /* truncate the name, so that we can use strcmp() */
15657 cc = name[len];
15658 name[len] = NUL;
15659
15660 /*
15661 * Check for "b:changedtick".
15662 */
15663 if (STRCMP(name, "b:changedtick") == 0)
15664 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015665 atv.v_type = VAR_NUMBER;
15666 atv.vval.v_number = curbuf->b_changedtick;
15667 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 }
15669
15670 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671 * Check for user-defined variables.
15672 */
15673 else
15674 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015675 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015677 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015678 }
15679
Bram Moolenaare9a41262005-01-15 22:18:47 +000015680 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015681 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015682 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015683 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684 ret = FAIL;
15685 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015686 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015687 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688
15689 name[len] = cc;
15690
15691 return ret;
15692}
15693
15694/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015695 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15696 * Also handle function call with Funcref variable: func(expr)
15697 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15698 */
15699 static int
15700handle_subscript(arg, rettv, evaluate, verbose)
15701 char_u **arg;
15702 typval_T *rettv;
15703 int evaluate; /* do more than finding the end */
15704 int verbose; /* give error messages */
15705{
15706 int ret = OK;
15707 dict_T *selfdict = NULL;
15708 char_u *s;
15709 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015710 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015711
15712 while (ret == OK
15713 && (**arg == '['
15714 || (**arg == '.' && rettv->v_type == VAR_DICT)
15715 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15716 && !vim_iswhite(*(*arg - 1)))
15717 {
15718 if (**arg == '(')
15719 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015720 /* need to copy the funcref so that we can clear rettv */
15721 functv = *rettv;
15722 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015723
15724 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015725 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015726 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015727 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15728 &len, evaluate, selfdict);
15729
15730 /* Clear the funcref afterwards, so that deleting it while
15731 * evaluating the arguments is possible (see test55). */
15732 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015733
15734 /* Stop the expression evaluation when immediately aborting on
15735 * error, or when an interrupt occurred or an exception was thrown
15736 * but not caught. */
15737 if (aborting())
15738 {
15739 if (ret == OK)
15740 clear_tv(rettv);
15741 ret = FAIL;
15742 }
15743 dict_unref(selfdict);
15744 selfdict = NULL;
15745 }
15746 else /* **arg == '[' || **arg == '.' */
15747 {
15748 dict_unref(selfdict);
15749 if (rettv->v_type == VAR_DICT)
15750 {
15751 selfdict = rettv->vval.v_dict;
15752 if (selfdict != NULL)
15753 ++selfdict->dv_refcount;
15754 }
15755 else
15756 selfdict = NULL;
15757 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15758 {
15759 clear_tv(rettv);
15760 ret = FAIL;
15761 }
15762 }
15763 }
15764 dict_unref(selfdict);
15765 return ret;
15766}
15767
15768/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015769 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15770 * value).
15771 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015772 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015773alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015774{
Bram Moolenaar33570922005-01-25 22:26:29 +000015775 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015776}
15777
15778/*
15779 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015780 * The string "s" must have been allocated, it is consumed.
15781 * Return NULL for out of memory, the variable otherwise.
15782 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015783 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015784alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015785 char_u *s;
15786{
Bram Moolenaar33570922005-01-25 22:26:29 +000015787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015789 rettv = alloc_tv();
15790 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015791 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015792 rettv->v_type = VAR_STRING;
15793 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015794 }
15795 else
15796 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015797 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015798}
15799
15800/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015801 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802 */
15803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015804free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015805 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015806{
15807 if (varp != NULL)
15808 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015809 switch (varp->v_type)
15810 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015811 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015812 func_unref(varp->vval.v_string);
15813 /*FALLTHROUGH*/
15814 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015815 vim_free(varp->vval.v_string);
15816 break;
15817 case VAR_LIST:
15818 list_unref(varp->vval.v_list);
15819 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015820 case VAR_DICT:
15821 dict_unref(varp->vval.v_dict);
15822 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015823 case VAR_NUMBER:
15824 case VAR_UNKNOWN:
15825 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015826 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015827 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015828 break;
15829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015830 vim_free(varp);
15831 }
15832}
15833
15834/*
15835 * Free the memory for a variable value and set the value to NULL or 0.
15836 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015837 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015838clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015839 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015840{
15841 if (varp != NULL)
15842 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015843 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015844 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015845 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015846 func_unref(varp->vval.v_string);
15847 /*FALLTHROUGH*/
15848 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015849 vim_free(varp->vval.v_string);
15850 varp->vval.v_string = NULL;
15851 break;
15852 case VAR_LIST:
15853 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015854 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015855 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015856 case VAR_DICT:
15857 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015858 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015859 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015860 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015861 varp->vval.v_number = 0;
15862 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015863 case VAR_UNKNOWN:
15864 break;
15865 default:
15866 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015867 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015868 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869 }
15870}
15871
15872/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015873 * Set the value of a variable to NULL without freeing items.
15874 */
15875 static void
15876init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015877 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015878{
15879 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015880 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015881}
15882
15883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884 * Get the number value of a variable.
15885 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015886 * For incompatible types, return 0.
15887 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15888 * caller of incompatible types: it sets *denote to TRUE if "denote"
15889 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015890 */
15891 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015892get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015893 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015894{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015895 int error = FALSE;
15896
15897 return get_tv_number_chk(varp, &error); /* return 0L on error */
15898}
15899
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015900 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015901get_tv_number_chk(varp, denote)
15902 typval_T *varp;
15903 int *denote;
15904{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015905 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015906
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015907 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015908 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015909 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015910 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015911 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015912 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015913 break;
15914 case VAR_STRING:
15915 if (varp->vval.v_string != NULL)
15916 vim_str2nr(varp->vval.v_string, NULL, NULL,
15917 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015918 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015919 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015920 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015921 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015922 case VAR_DICT:
15923 EMSG(_("E728: Using a Dictionary as a number"));
15924 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015925 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015926 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015927 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015928 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015929 if (denote == NULL) /* useful for values that must be unsigned */
15930 n = -1;
15931 else
15932 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015933 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015934}
15935
15936/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000015937 * Get the lnum from the first argument.
15938 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015939 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015940 */
15941 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015942get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000015943 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015944{
Bram Moolenaar33570922005-01-25 22:26:29 +000015945 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015946 linenr_T lnum;
15947
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015948 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015949 if (lnum == 0) /* no valid number, try using line() */
15950 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015951 rettv.v_type = VAR_NUMBER;
15952 f_line(argvars, &rettv);
15953 lnum = rettv.vval.v_number;
15954 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015955 }
15956 return lnum;
15957}
15958
15959/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000015960 * Get the lnum from the first argument.
15961 * Also accepts "$", then "buf" is used.
15962 * Returns 0 on error.
15963 */
15964 static linenr_T
15965get_tv_lnum_buf(argvars, buf)
15966 typval_T *argvars;
15967 buf_T *buf;
15968{
15969 if (argvars[0].v_type == VAR_STRING
15970 && argvars[0].vval.v_string != NULL
15971 && argvars[0].vval.v_string[0] == '$'
15972 && buf != NULL)
15973 return buf->b_ml.ml_line_count;
15974 return get_tv_number_chk(&argvars[0], NULL);
15975}
15976
15977/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015978 * Get the string value of a variable.
15979 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000015980 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
15981 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015982 * If the String variable has never been set, return an empty string.
15983 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015984 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
15985 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015986 */
15987 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015988get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015989 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990{
15991 static char_u mybuf[NUMBUFLEN];
15992
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015993 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015994}
15995
15996 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015997get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000015998 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015999 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016000{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016001 char_u *res = get_tv_string_buf_chk(varp, buf);
16002
16003 return res != NULL ? res : (char_u *)"";
16004}
16005
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016006 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016007get_tv_string_chk(varp)
16008 typval_T *varp;
16009{
16010 static char_u mybuf[NUMBUFLEN];
16011
16012 return get_tv_string_buf_chk(varp, mybuf);
16013}
16014
16015 static char_u *
16016get_tv_string_buf_chk(varp, buf)
16017 typval_T *varp;
16018 char_u *buf;
16019{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016020 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016021 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016022 case VAR_NUMBER:
16023 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
16024 return buf;
16025 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016026 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016027 break;
16028 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016029 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016030 break;
16031 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016032 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016033 break;
16034 case VAR_STRING:
16035 if (varp->vval.v_string != NULL)
16036 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016037 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016038 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016039 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016040 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016041 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016042 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016043}
16044
16045/*
16046 * Find variable "name" in the list of variables.
16047 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016048 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016049 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000016050 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016052 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016053find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016054 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016055 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016057 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016058 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016059
Bram Moolenaara7043832005-01-21 11:56:39 +000016060 ht = find_var_ht(name, &varname);
16061 if (htp != NULL)
16062 *htp = ht;
16063 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016064 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016065 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016066}
16067
16068/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016069 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016070 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016071 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016072 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016073find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016074 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016075 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016076 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016077{
Bram Moolenaar33570922005-01-25 22:26:29 +000016078 hashitem_T *hi;
16079
16080 if (*varname == NUL)
16081 {
16082 /* Must be something like "s:", otherwise "ht" would be NULL. */
16083 switch (varname[-2])
16084 {
16085 case 's': return &SCRIPT_SV(current_SID).sv_var;
16086 case 'g': return &globvars_var;
16087 case 'v': return &vimvars_var;
16088 case 'b': return &curbuf->b_bufvar;
16089 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016090 case 'l': return current_funccal == NULL
16091 ? NULL : &current_funccal->l_vars_var;
16092 case 'a': return current_funccal == NULL
16093 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016094 }
16095 return NULL;
16096 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016097
16098 hi = hash_find(ht, varname);
16099 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016100 {
16101 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016102 * worked find the variable again. Don't auto-load a script if it was
16103 * loaded already, otherwise it would be loaded every time when
16104 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016105 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016106 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016107 hi = hash_find(ht, varname);
16108 if (HASHITEM_EMPTY(hi))
16109 return NULL;
16110 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016111 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016112}
16113
16114/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016115 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016116 * Set "varname" to the start of name without ':'.
16117 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016118 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016119find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016120 char_u *name;
16121 char_u **varname;
16122{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016123 hashitem_T *hi;
16124
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125 if (name[1] != ':')
16126 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016127 /* The name must not start with a colon or #. */
16128 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016129 return NULL;
16130 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016131
16132 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016133 hi = hash_find(&compat_hashtab, name);
16134 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016135 return &compat_hashtab;
16136
Bram Moolenaar071d4272004-06-13 20:20:40 +000016137 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016138 return &globvarht; /* global variable */
16139 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016140 }
16141 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016142 if (*name == 'g') /* global variable */
16143 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016144 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16145 */
16146 if (vim_strchr(name + 2, ':') != NULL
16147 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016148 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016150 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016151 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016152 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016153 if (*name == 'v') /* v: variable */
16154 return &vimvarht;
16155 if (*name == 'a' && current_funccal != NULL) /* function argument */
16156 return &current_funccal->l_avars.dv_hashtab;
16157 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16158 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016159 if (*name == 's' /* script variable */
16160 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16161 return &SCRIPT_VARS(current_SID);
16162 return NULL;
16163}
16164
16165/*
16166 * Get the string value of a (global/local) variable.
16167 * Returns NULL when it doesn't exist.
16168 */
16169 char_u *
16170get_var_value(name)
16171 char_u *name;
16172{
Bram Moolenaar33570922005-01-25 22:26:29 +000016173 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174
Bram Moolenaara7043832005-01-21 11:56:39 +000016175 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016176 if (v == NULL)
16177 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016178 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016179}
16180
16181/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016182 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183 * sourcing this script and when executing functions defined in the script.
16184 */
16185 void
16186new_script_vars(id)
16187 scid_T id;
16188{
Bram Moolenaara7043832005-01-21 11:56:39 +000016189 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016190 hashtab_T *ht;
16191 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016192
Bram Moolenaar071d4272004-06-13 20:20:40 +000016193 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16194 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016195 /* Re-allocating ga_data means that an ht_array pointing to
16196 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016197 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016198 for (i = 1; i <= ga_scripts.ga_len; ++i)
16199 {
16200 ht = &SCRIPT_VARS(i);
16201 if (ht->ht_mask == HT_INIT_SIZE - 1)
16202 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016203 sv = &SCRIPT_SV(i);
16204 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016205 }
16206
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207 while (ga_scripts.ga_len < id)
16208 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016209 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16210 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016211 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212 }
16213 }
16214}
16215
16216/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016217 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16218 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016219 */
16220 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016221init_var_dict(dict, dict_var)
16222 dict_T *dict;
16223 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224{
Bram Moolenaar33570922005-01-25 22:26:29 +000016225 hash_init(&dict->dv_hashtab);
16226 dict->dv_refcount = 99999;
16227 dict_var->di_tv.vval.v_dict = dict;
16228 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016229 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016230 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16231 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232}
16233
16234/*
16235 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016236 * Frees all allocated variables and the value they contain.
16237 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016238 */
16239 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016240vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016241 hashtab_T *ht;
16242{
16243 vars_clear_ext(ht, TRUE);
16244}
16245
16246/*
16247 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16248 */
16249 static void
16250vars_clear_ext(ht, free_val)
16251 hashtab_T *ht;
16252 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253{
Bram Moolenaara7043832005-01-21 11:56:39 +000016254 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016255 hashitem_T *hi;
16256 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016257
Bram Moolenaar33570922005-01-25 22:26:29 +000016258 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016259 todo = ht->ht_used;
16260 for (hi = ht->ht_array; todo > 0; ++hi)
16261 {
16262 if (!HASHITEM_EMPTY(hi))
16263 {
16264 --todo;
16265
Bram Moolenaar33570922005-01-25 22:26:29 +000016266 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016267 * ht_array might change then. hash_clear() takes care of it
16268 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016269 v = HI2DI(hi);
16270 if (free_val)
16271 clear_tv(&v->di_tv);
16272 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16273 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016274 }
16275 }
16276 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016277 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016278}
16279
Bram Moolenaara7043832005-01-21 11:56:39 +000016280/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016281 * Delete a variable from hashtab "ht" at item "hi".
16282 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016283 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016285delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016286 hashtab_T *ht;
16287 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016288{
Bram Moolenaar33570922005-01-25 22:26:29 +000016289 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016290
16291 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016292 clear_tv(&di->di_tv);
16293 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294}
16295
16296/*
16297 * List the value of one internal variable.
16298 */
16299 static void
16300list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016301 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302 char_u *prefix;
16303{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016304 char_u *tofree;
16305 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016306 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016307
Bram Moolenaar33570922005-01-25 22:26:29 +000016308 s = echo_string(&v->di_tv, &tofree, numbuf);
16309 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016310 s == NULL ? (char_u *)"" : s);
16311 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312}
16313
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314 static void
16315list_one_var_a(prefix, name, type, string)
16316 char_u *prefix;
16317 char_u *name;
16318 int type;
16319 char_u *string;
16320{
16321 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16322 if (name != NULL) /* "a:" vars don't have a name stored */
16323 msg_puts(name);
16324 msg_putchar(' ');
16325 msg_advance(22);
16326 if (type == VAR_NUMBER)
16327 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016328 else if (type == VAR_FUNC)
16329 msg_putchar('*');
16330 else if (type == VAR_LIST)
16331 {
16332 msg_putchar('[');
16333 if (*string == '[')
16334 ++string;
16335 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016336 else if (type == VAR_DICT)
16337 {
16338 msg_putchar('{');
16339 if (*string == '{')
16340 ++string;
16341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342 else
16343 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016344
Bram Moolenaar071d4272004-06-13 20:20:40 +000016345 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016346
16347 if (type == VAR_FUNC)
16348 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016349}
16350
16351/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016352 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016353 * If the variable already exists, the value is updated.
16354 * Otherwise the variable is created.
16355 */
16356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016357set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016358 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016359 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016360 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361{
Bram Moolenaar33570922005-01-25 22:26:29 +000016362 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016363 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016364 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016365 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016366
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016367 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016368 {
16369 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16370 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16371 ? name[2] : name[0]))
16372 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016373 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016374 return;
16375 }
16376 if (function_exists(name))
16377 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016378 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016379 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016380 return;
16381 }
16382 }
16383
Bram Moolenaara7043832005-01-21 11:56:39 +000016384 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016385 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016386 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016387 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016388 return;
16389 }
16390
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016391 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016392 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016394 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016395 if (var_check_ro(v->di_flags, name)
16396 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016397 return;
16398 if (v->di_tv.v_type != tv->v_type
16399 && !((v->di_tv.v_type == VAR_STRING
16400 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016401 && (tv->v_type == VAR_STRING
16402 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016403 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016404 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016405 return;
16406 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016407
16408 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016409 * Handle setting internal v: variables separately: we don't change
16410 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016411 */
16412 if (ht == &vimvarht)
16413 {
16414 if (v->di_tv.v_type == VAR_STRING)
16415 {
16416 vim_free(v->di_tv.vval.v_string);
16417 if (copy || tv->v_type != VAR_STRING)
16418 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16419 else
16420 {
16421 /* Take over the string to avoid an extra alloc/free. */
16422 v->di_tv.vval.v_string = tv->vval.v_string;
16423 tv->vval.v_string = NULL;
16424 }
16425 }
16426 else if (v->di_tv.v_type != VAR_NUMBER)
16427 EMSG2(_(e_intern2), "set_var()");
16428 else
16429 v->di_tv.vval.v_number = get_tv_number(tv);
16430 return;
16431 }
16432
16433 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016434 }
16435 else /* add a new variable */
16436 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016437 /* Make sure the variable name is valid. */
16438 for (p = varname; *p != NUL; ++p)
16439 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)))
16440 {
16441 EMSG2(_(e_illvar), varname);
16442 return;
16443 }
16444
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016445 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16446 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016447 if (v == NULL)
16448 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016449 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016450 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016451 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016452 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453 return;
16454 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016455 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016456 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016457
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016458 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016459 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016460 else
16461 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016462 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016463 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016464 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016466}
16467
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016468/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016469 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16470 * Also give an error message.
16471 */
16472 static int
16473var_check_ro(flags, name)
16474 int flags;
16475 char_u *name;
16476{
16477 if (flags & DI_FLAGS_RO)
16478 {
16479 EMSG2(_(e_readonlyvar), name);
16480 return TRUE;
16481 }
16482 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16483 {
16484 EMSG2(_(e_readonlysbx), name);
16485 return TRUE;
16486 }
16487 return FALSE;
16488}
16489
16490/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016491 * Return TRUE if typeval "tv" is set to be locked (immutable).
16492 * Also give an error message, using "name".
16493 */
16494 static int
16495tv_check_lock(lock, name)
16496 int lock;
16497 char_u *name;
16498{
16499 if (lock & VAR_LOCKED)
16500 {
16501 EMSG2(_("E741: Value is locked: %s"),
16502 name == NULL ? (char_u *)_("Unknown") : name);
16503 return TRUE;
16504 }
16505 if (lock & VAR_FIXED)
16506 {
16507 EMSG2(_("E742: Cannot change value of %s"),
16508 name == NULL ? (char_u *)_("Unknown") : name);
16509 return TRUE;
16510 }
16511 return FALSE;
16512}
16513
16514/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016515 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016516 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016517 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016518 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016520copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016521 typval_T *from;
16522 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016523{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016524 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016525 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016526 switch (from->v_type)
16527 {
16528 case VAR_NUMBER:
16529 to->vval.v_number = from->vval.v_number;
16530 break;
16531 case VAR_STRING:
16532 case VAR_FUNC:
16533 if (from->vval.v_string == NULL)
16534 to->vval.v_string = NULL;
16535 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016536 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016537 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016538 if (from->v_type == VAR_FUNC)
16539 func_ref(to->vval.v_string);
16540 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016541 break;
16542 case VAR_LIST:
16543 if (from->vval.v_list == NULL)
16544 to->vval.v_list = NULL;
16545 else
16546 {
16547 to->vval.v_list = from->vval.v_list;
16548 ++to->vval.v_list->lv_refcount;
16549 }
16550 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016551 case VAR_DICT:
16552 if (from->vval.v_dict == NULL)
16553 to->vval.v_dict = NULL;
16554 else
16555 {
16556 to->vval.v_dict = from->vval.v_dict;
16557 ++to->vval.v_dict->dv_refcount;
16558 }
16559 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016560 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016561 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016562 break;
16563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016564}
16565
16566/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016567 * Make a copy of an item.
16568 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016569 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16570 * reference to an already copied list/dict can be used.
16571 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016572 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016573 static int
16574item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016575 typval_T *from;
16576 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016577 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016578 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016579{
16580 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016581 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016582
Bram Moolenaar33570922005-01-25 22:26:29 +000016583 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016584 {
16585 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016586 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016587 }
16588 ++recurse;
16589
16590 switch (from->v_type)
16591 {
16592 case VAR_NUMBER:
16593 case VAR_STRING:
16594 case VAR_FUNC:
16595 copy_tv(from, to);
16596 break;
16597 case VAR_LIST:
16598 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016599 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016600 if (from->vval.v_list == NULL)
16601 to->vval.v_list = NULL;
16602 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16603 {
16604 /* use the copy made earlier */
16605 to->vval.v_list = from->vval.v_list->lv_copylist;
16606 ++to->vval.v_list->lv_refcount;
16607 }
16608 else
16609 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16610 if (to->vval.v_list == NULL)
16611 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016612 break;
16613 case VAR_DICT:
16614 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016615 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016616 if (from->vval.v_dict == NULL)
16617 to->vval.v_dict = NULL;
16618 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16619 {
16620 /* use the copy made earlier */
16621 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16622 ++to->vval.v_dict->dv_refcount;
16623 }
16624 else
16625 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16626 if (to->vval.v_dict == NULL)
16627 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016628 break;
16629 default:
16630 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016631 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016632 }
16633 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016634 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016635}
16636
16637/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016638 * ":echo expr1 ..." print each argument separated with a space, add a
16639 * newline at the end.
16640 * ":echon expr1 ..." print each argument plain.
16641 */
16642 void
16643ex_echo(eap)
16644 exarg_T *eap;
16645{
16646 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016647 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016648 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649 char_u *p;
16650 int needclr = TRUE;
16651 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016652 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016653
16654 if (eap->skip)
16655 ++emsg_skip;
16656 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16657 {
16658 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016659 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016660 {
16661 /*
16662 * Report the invalid expression unless the expression evaluation
16663 * has been cancelled due to an aborting error, an interrupt, or an
16664 * exception.
16665 */
16666 if (!aborting())
16667 EMSG2(_(e_invexpr2), p);
16668 break;
16669 }
16670 if (!eap->skip)
16671 {
16672 if (atstart)
16673 {
16674 atstart = FALSE;
16675 /* Call msg_start() after eval1(), evaluating the expression
16676 * may cause a message to appear. */
16677 if (eap->cmdidx == CMD_echo)
16678 msg_start();
16679 }
16680 else if (eap->cmdidx == CMD_echo)
16681 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016682 p = echo_string(&rettv, &tofree, numbuf);
16683 if (p != NULL)
16684 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016685 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016686 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016687 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016688 if (*p != TAB && needclr)
16689 {
16690 /* remove any text still there from the command */
16691 msg_clr_eos();
16692 needclr = FALSE;
16693 }
16694 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016695 }
16696 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016697 {
16698#ifdef FEAT_MBYTE
16699 if (has_mbyte)
16700 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016701 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016702
16703 (void)msg_outtrans_len_attr(p, i, echo_attr);
16704 p += i - 1;
16705 }
16706 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016707#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016708 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016710 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016711 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016713 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016714 arg = skipwhite(arg);
16715 }
16716 eap->nextcmd = check_nextcmd(arg);
16717
16718 if (eap->skip)
16719 --emsg_skip;
16720 else
16721 {
16722 /* remove text that may still be there from the command */
16723 if (needclr)
16724 msg_clr_eos();
16725 if (eap->cmdidx == CMD_echo)
16726 msg_end();
16727 }
16728}
16729
16730/*
16731 * ":echohl {name}".
16732 */
16733 void
16734ex_echohl(eap)
16735 exarg_T *eap;
16736{
16737 int id;
16738
16739 id = syn_name2id(eap->arg);
16740 if (id == 0)
16741 echo_attr = 0;
16742 else
16743 echo_attr = syn_id2attr(id);
16744}
16745
16746/*
16747 * ":execute expr1 ..." execute the result of an expression.
16748 * ":echomsg expr1 ..." Print a message
16749 * ":echoerr expr1 ..." Print an error
16750 * Each gets spaces around each argument and a newline at the end for
16751 * echo commands
16752 */
16753 void
16754ex_execute(eap)
16755 exarg_T *eap;
16756{
16757 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016758 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016759 int ret = OK;
16760 char_u *p;
16761 garray_T ga;
16762 int len;
16763 int save_did_emsg;
16764
16765 ga_init2(&ga, 1, 80);
16766
16767 if (eap->skip)
16768 ++emsg_skip;
16769 while (*arg != NUL && *arg != '|' && *arg != '\n')
16770 {
16771 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016772 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773 {
16774 /*
16775 * Report the invalid expression unless the expression evaluation
16776 * has been cancelled due to an aborting error, an interrupt, or an
16777 * exception.
16778 */
16779 if (!aborting())
16780 EMSG2(_(e_invexpr2), p);
16781 ret = FAIL;
16782 break;
16783 }
16784
16785 if (!eap->skip)
16786 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016787 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788 len = (int)STRLEN(p);
16789 if (ga_grow(&ga, len + 2) == FAIL)
16790 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016791 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792 ret = FAIL;
16793 break;
16794 }
16795 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016796 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016797 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016798 ga.ga_len += len;
16799 }
16800
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016801 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802 arg = skipwhite(arg);
16803 }
16804
16805 if (ret != FAIL && ga.ga_data != NULL)
16806 {
16807 if (eap->cmdidx == CMD_echomsg)
16808 MSG_ATTR(ga.ga_data, echo_attr);
16809 else if (eap->cmdidx == CMD_echoerr)
16810 {
16811 /* We don't want to abort following commands, restore did_emsg. */
16812 save_did_emsg = did_emsg;
16813 EMSG((char_u *)ga.ga_data);
16814 if (!force_abort)
16815 did_emsg = save_did_emsg;
16816 }
16817 else if (eap->cmdidx == CMD_execute)
16818 do_cmdline((char_u *)ga.ga_data,
16819 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16820 }
16821
16822 ga_clear(&ga);
16823
16824 if (eap->skip)
16825 --emsg_skip;
16826
16827 eap->nextcmd = check_nextcmd(arg);
16828}
16829
16830/*
16831 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16832 * "arg" points to the "&" or '+' when called, to "option" when returning.
16833 * Returns NULL when no option name found. Otherwise pointer to the char
16834 * after the option name.
16835 */
16836 static char_u *
16837find_option_end(arg, opt_flags)
16838 char_u **arg;
16839 int *opt_flags;
16840{
16841 char_u *p = *arg;
16842
16843 ++p;
16844 if (*p == 'g' && p[1] == ':')
16845 {
16846 *opt_flags = OPT_GLOBAL;
16847 p += 2;
16848 }
16849 else if (*p == 'l' && p[1] == ':')
16850 {
16851 *opt_flags = OPT_LOCAL;
16852 p += 2;
16853 }
16854 else
16855 *opt_flags = 0;
16856
16857 if (!ASCII_ISALPHA(*p))
16858 return NULL;
16859 *arg = p;
16860
16861 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16862 p += 4; /* termcap option */
16863 else
16864 while (ASCII_ISALPHA(*p))
16865 ++p;
16866 return p;
16867}
16868
16869/*
16870 * ":function"
16871 */
16872 void
16873ex_function(eap)
16874 exarg_T *eap;
16875{
16876 char_u *theline;
16877 int j;
16878 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880 char_u *name = NULL;
16881 char_u *p;
16882 char_u *arg;
16883 garray_T newargs;
16884 garray_T newlines;
16885 int varargs = FALSE;
16886 int mustend = FALSE;
16887 int flags = 0;
16888 ufunc_T *fp;
16889 int indent;
16890 int nesting;
16891 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016892 dictitem_T *v;
16893 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016894 static int func_nr = 0; /* number for nameless function */
16895 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016896 hashtab_T *ht;
16897 int todo;
16898 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016899
16900 /*
16901 * ":function" without argument: list functions.
16902 */
16903 if (ends_excmd(*eap->arg))
16904 {
16905 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016906 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000016907 todo = func_hashtab.ht_used;
16908 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016909 {
16910 if (!HASHITEM_EMPTY(hi))
16911 {
16912 --todo;
16913 fp = HI2UF(hi);
16914 if (!isdigit(*fp->uf_name))
16915 list_func_head(fp, FALSE);
16916 }
16917 }
16918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919 eap->nextcmd = check_nextcmd(eap->arg);
16920 return;
16921 }
16922
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016923 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016924 * ":function /pat": list functions matching pattern.
16925 */
16926 if (*eap->arg == '/')
16927 {
16928 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
16929 if (!eap->skip)
16930 {
16931 regmatch_T regmatch;
16932
16933 c = *p;
16934 *p = NUL;
16935 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
16936 *p = c;
16937 if (regmatch.regprog != NULL)
16938 {
16939 regmatch.rm_ic = p_ic;
16940
16941 todo = func_hashtab.ht_used;
16942 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
16943 {
16944 if (!HASHITEM_EMPTY(hi))
16945 {
16946 --todo;
16947 fp = HI2UF(hi);
16948 if (!isdigit(*fp->uf_name)
16949 && vim_regexec(&regmatch, fp->uf_name, 0))
16950 list_func_head(fp, FALSE);
16951 }
16952 }
16953 }
16954 }
16955 if (*p == '/')
16956 ++p;
16957 eap->nextcmd = check_nextcmd(p);
16958 return;
16959 }
16960
16961 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016962 * Get the function name. There are these situations:
16963 * func normal function name
16964 * "name" == func, "fudi.fd_dict" == NULL
16965 * dict.func new dictionary entry
16966 * "name" == NULL, "fudi.fd_dict" set,
16967 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
16968 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016969 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016970 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16971 * dict.func existing dict entry that's not a Funcref
16972 * "name" == NULL, "fudi.fd_dict" set,
16973 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16974 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016975 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016976 name = trans_function_name(&p, eap->skip, 0, &fudi);
16977 paren = (vim_strchr(p, '(') != NULL);
16978 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979 {
16980 /*
16981 * Return on an invalid expression in braces, unless the expression
16982 * evaluation has been cancelled due to an aborting error, an
16983 * interrupt, or an exception.
16984 */
16985 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016986 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016987 if (!eap->skip && fudi.fd_newkey != NULL)
16988 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016989 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016992 else
16993 eap->skip = TRUE;
16994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016995 /* An error in a function call during evaluation of an expression in magic
16996 * braces should not cause the function not to be defined. */
16997 saved_did_emsg = did_emsg;
16998 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999
17000 /*
17001 * ":function func" with only function name: list function.
17002 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017003 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004 {
17005 if (!ends_excmd(*skipwhite(p)))
17006 {
17007 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017008 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009 }
17010 eap->nextcmd = check_nextcmd(p);
17011 if (eap->nextcmd != NULL)
17012 *p = NUL;
17013 if (!eap->skip && !got_int)
17014 {
17015 fp = find_func(name);
17016 if (fp != NULL)
17017 {
17018 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017019 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 {
17021 msg_putchar('\n');
17022 msg_outnum((long)(j + 1));
17023 if (j < 9)
17024 msg_putchar(' ');
17025 if (j < 99)
17026 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017027 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 out_flush(); /* show a line at a time */
17029 ui_breakcheck();
17030 }
17031 if (!got_int)
17032 {
17033 msg_putchar('\n');
17034 msg_puts((char_u *)" endfunction");
17035 }
17036 }
17037 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017038 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017040 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017041 }
17042
17043 /*
17044 * ":function name(arg1, arg2)" Define function.
17045 */
17046 p = skipwhite(p);
17047 if (*p != '(')
17048 {
17049 if (!eap->skip)
17050 {
17051 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017052 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017053 }
17054 /* attempt to continue by skipping some text */
17055 if (vim_strchr(p, '(') != NULL)
17056 p = vim_strchr(p, '(');
17057 }
17058 p = skipwhite(p + 1);
17059
17060 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17061 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17062
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017063 if (!eap->skip)
17064 {
17065 /* Check the name of the function. */
17066 if (name != NULL)
17067 arg = name;
17068 else
17069 arg = fudi.fd_newkey;
17070 if (arg != NULL)
17071 {
17072 if (*arg == K_SPECIAL)
17073 j = 3;
17074 else
17075 j = 0;
17076 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17077 : eval_isnamec(arg[j])))
17078 ++j;
17079 if (arg[j] != NUL)
17080 emsg_funcname(_(e_invarg2), arg);
17081 }
17082 }
17083
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084 /*
17085 * Isolate the arguments: "arg1, arg2, ...)"
17086 */
17087 while (*p != ')')
17088 {
17089 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17090 {
17091 varargs = TRUE;
17092 p += 3;
17093 mustend = TRUE;
17094 }
17095 else
17096 {
17097 arg = p;
17098 while (ASCII_ISALNUM(*p) || *p == '_')
17099 ++p;
17100 if (arg == p || isdigit(*arg)
17101 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17102 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17103 {
17104 if (!eap->skip)
17105 EMSG2(_("E125: Illegal argument: %s"), arg);
17106 break;
17107 }
17108 if (ga_grow(&newargs, 1) == FAIL)
17109 goto erret;
17110 c = *p;
17111 *p = NUL;
17112 arg = vim_strsave(arg);
17113 if (arg == NULL)
17114 goto erret;
17115 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17116 *p = c;
17117 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118 if (*p == ',')
17119 ++p;
17120 else
17121 mustend = TRUE;
17122 }
17123 p = skipwhite(p);
17124 if (mustend && *p != ')')
17125 {
17126 if (!eap->skip)
17127 EMSG2(_(e_invarg2), eap->arg);
17128 break;
17129 }
17130 }
17131 ++p; /* skip the ')' */
17132
Bram Moolenaare9a41262005-01-15 22:18:47 +000017133 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134 for (;;)
17135 {
17136 p = skipwhite(p);
17137 if (STRNCMP(p, "range", 5) == 0)
17138 {
17139 flags |= FC_RANGE;
17140 p += 5;
17141 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017142 else if (STRNCMP(p, "dict", 4) == 0)
17143 {
17144 flags |= FC_DICT;
17145 p += 4;
17146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017147 else if (STRNCMP(p, "abort", 5) == 0)
17148 {
17149 flags |= FC_ABORT;
17150 p += 5;
17151 }
17152 else
17153 break;
17154 }
17155
17156 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
17157 EMSG(_(e_trailing));
17158
17159 /*
17160 * Read the body of the function, until ":endfunction" is found.
17161 */
17162 if (KeyTyped)
17163 {
17164 /* Check if the function already exists, don't let the user type the
17165 * whole function before telling him it doesn't work! For a script we
17166 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017167 if (!eap->skip && !eap->forceit)
17168 {
17169 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17170 EMSG(_(e_funcdict));
17171 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017172 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017174
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017175 if (!eap->skip && did_emsg)
17176 goto erret;
17177
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178 msg_putchar('\n'); /* don't overwrite the function name */
17179 cmdline_row = msg_row;
17180 }
17181
17182 indent = 2;
17183 nesting = 0;
17184 for (;;)
17185 {
17186 msg_scroll = TRUE;
17187 need_wait_return = FALSE;
17188 if (eap->getline == NULL)
17189 theline = getcmdline(':', 0L, indent);
17190 else
17191 theline = eap->getline(':', eap->cookie, indent);
17192 if (KeyTyped)
17193 lines_left = Rows - 1;
17194 if (theline == NULL)
17195 {
17196 EMSG(_("E126: Missing :endfunction"));
17197 goto erret;
17198 }
17199
17200 if (skip_until != NULL)
17201 {
17202 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17203 * don't check for ":endfunc". */
17204 if (STRCMP(theline, skip_until) == 0)
17205 {
17206 vim_free(skip_until);
17207 skip_until = NULL;
17208 }
17209 }
17210 else
17211 {
17212 /* skip ':' and blanks*/
17213 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17214 ;
17215
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017216 /* Check for "endfunction". */
17217 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218 {
17219 vim_free(theline);
17220 break;
17221 }
17222
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017223 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017224 * at "end". */
17225 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17226 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017227 else if (STRNCMP(p, "if", 2) == 0
17228 || STRNCMP(p, "wh", 2) == 0
17229 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017230 || STRNCMP(p, "try", 3) == 0)
17231 indent += 2;
17232
17233 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017234 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017236 if (*p == '!')
17237 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017238 p += eval_fname_script(p);
17239 if (ASCII_ISALPHA(*p))
17240 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017241 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017242 if (*skipwhite(p) == '(')
17243 {
17244 ++nesting;
17245 indent += 2;
17246 }
17247 }
17248 }
17249
17250 /* Check for ":append" or ":insert". */
17251 p = skip_range(p, NULL);
17252 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17253 || (p[0] == 'i'
17254 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17255 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17256 skip_until = vim_strsave((char_u *)".");
17257
17258 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17259 arg = skipwhite(skiptowhite(p));
17260 if (arg[0] == '<' && arg[1] =='<'
17261 && ((p[0] == 'p' && p[1] == 'y'
17262 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17263 || (p[0] == 'p' && p[1] == 'e'
17264 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17265 || (p[0] == 't' && p[1] == 'c'
17266 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17267 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17268 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017269 || (p[0] == 'm' && p[1] == 'z'
17270 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271 ))
17272 {
17273 /* ":python <<" continues until a dot, like ":append" */
17274 p = skipwhite(arg + 2);
17275 if (*p == NUL)
17276 skip_until = vim_strsave((char_u *)".");
17277 else
17278 skip_until = vim_strsave(p);
17279 }
17280 }
17281
17282 /* Add the line to the function. */
17283 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017284 {
17285 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017287 }
17288
17289 /* Copy the line to newly allocated memory. get_one_sourceline()
17290 * allocates 250 bytes per line, this saves 80% on average. The cost
17291 * is an extra alloc/free. */
17292 p = vim_strsave(theline);
17293 if (p != NULL)
17294 {
17295 vim_free(theline);
17296 theline = p;
17297 }
17298
Bram Moolenaar071d4272004-06-13 20:20:40 +000017299 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17300 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017301 }
17302
17303 /* Don't define the function when skipping commands or when an error was
17304 * detected. */
17305 if (eap->skip || did_emsg)
17306 goto erret;
17307
17308 /*
17309 * If there are no errors, add the function
17310 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017311 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017312 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017313 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017314 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017315 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017316 emsg_funcname("E707: Function name conflicts with variable: %s",
17317 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017318 goto erret;
17319 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017320
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017321 fp = find_func(name);
17322 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017324 if (!eap->forceit)
17325 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017326 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017327 goto erret;
17328 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017329 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017330 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017331 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017332 name);
17333 goto erret;
17334 }
17335 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017336 ga_clear_strings(&(fp->uf_args));
17337 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017338 vim_free(name);
17339 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017341 }
17342 else
17343 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017344 char numbuf[20];
17345
17346 fp = NULL;
17347 if (fudi.fd_newkey == NULL && !eap->forceit)
17348 {
17349 EMSG(_(e_funcdict));
17350 goto erret;
17351 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017352 if (fudi.fd_di == NULL)
17353 {
17354 /* Can't add a function to a locked dictionary */
17355 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17356 goto erret;
17357 }
17358 /* Can't change an existing function if it is locked */
17359 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17360 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017361
17362 /* Give the function a sequential number. Can only be used with a
17363 * Funcref! */
17364 vim_free(name);
17365 sprintf(numbuf, "%d", ++func_nr);
17366 name = vim_strsave((char_u *)numbuf);
17367 if (name == NULL)
17368 goto erret;
17369 }
17370
17371 if (fp == NULL)
17372 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017373 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017374 {
17375 int slen, plen;
17376 char_u *scriptname;
17377
17378 /* Check that the autoload name matches the script name. */
17379 j = FAIL;
17380 if (sourcing_name != NULL)
17381 {
17382 scriptname = autoload_name(name);
17383 if (scriptname != NULL)
17384 {
17385 p = vim_strchr(scriptname, '/');
17386 plen = STRLEN(p);
17387 slen = STRLEN(sourcing_name);
17388 if (slen > plen && fnamecmp(p,
17389 sourcing_name + slen - plen) == 0)
17390 j = OK;
17391 vim_free(scriptname);
17392 }
17393 }
17394 if (j == FAIL)
17395 {
17396 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17397 goto erret;
17398 }
17399 }
17400
17401 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017402 if (fp == NULL)
17403 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017404
17405 if (fudi.fd_dict != NULL)
17406 {
17407 if (fudi.fd_di == NULL)
17408 {
17409 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017410 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017411 if (fudi.fd_di == NULL)
17412 {
17413 vim_free(fp);
17414 goto erret;
17415 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017416 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17417 {
17418 vim_free(fudi.fd_di);
17419 goto erret;
17420 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017421 }
17422 else
17423 /* overwrite existing dict entry */
17424 clear_tv(&fudi.fd_di->di_tv);
17425 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017426 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017427 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017428 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017429 }
17430
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017432 STRCPY(fp->uf_name, name);
17433 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017434 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017435 fp->uf_args = newargs;
17436 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017437#ifdef FEAT_PROFILE
17438 fp->uf_tml_count = NULL;
17439 fp->uf_tml_total = NULL;
17440 fp->uf_tml_self = NULL;
17441 fp->uf_profiling = FALSE;
17442 if (prof_def_func())
17443 func_do_profile(fp);
17444#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017445 fp->uf_varargs = varargs;
17446 fp->uf_flags = flags;
17447 fp->uf_calls = 0;
17448 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017449 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450
17451erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452 ga_clear_strings(&newargs);
17453 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017454ret_free:
17455 vim_free(skip_until);
17456 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017458 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017459}
17460
17461/*
17462 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017463 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017464 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017465 * flags:
17466 * TFN_INT: internal function name OK
17467 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017468 * Advances "pp" to just after the function name (if no error).
17469 */
17470 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017471trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017472 char_u **pp;
17473 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017474 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017475 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017477 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 char_u *start;
17479 char_u *end;
17480 int lead;
17481 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017483 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017484
17485 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017486 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017487 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017488
17489 /* Check for hard coded <SNR>: already translated function ID (from a user
17490 * command). */
17491 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17492 && (*pp)[2] == (int)KE_SNR)
17493 {
17494 *pp += 3;
17495 len = get_id_len(pp) + 3;
17496 return vim_strnsave(start, len);
17497 }
17498
17499 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17500 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017502 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017504
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017505 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17506 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017507 if (end == start)
17508 {
17509 if (!skip)
17510 EMSG(_("E129: Function name required"));
17511 goto theend;
17512 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017513 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017514 {
17515 /*
17516 * Report an invalid expression in braces, unless the expression
17517 * evaluation has been cancelled due to an aborting error, an
17518 * interrupt, or an exception.
17519 */
17520 if (!aborting())
17521 {
17522 if (end != NULL)
17523 EMSG2(_(e_invarg2), start);
17524 }
17525 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017526 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017527 goto theend;
17528 }
17529
17530 if (lv.ll_tv != NULL)
17531 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017532 if (fdp != NULL)
17533 {
17534 fdp->fd_dict = lv.ll_dict;
17535 fdp->fd_newkey = lv.ll_newkey;
17536 lv.ll_newkey = NULL;
17537 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017538 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017539 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17540 {
17541 name = vim_strsave(lv.ll_tv->vval.v_string);
17542 *pp = end;
17543 }
17544 else
17545 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017546 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17547 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017548 EMSG(_(e_funcref));
17549 else
17550 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017551 name = NULL;
17552 }
17553 goto theend;
17554 }
17555
17556 if (lv.ll_name == NULL)
17557 {
17558 /* Error found, but continue after the function name. */
17559 *pp = end;
17560 goto theend;
17561 }
17562
17563 if (lv.ll_exp_name != NULL)
17564 len = STRLEN(lv.ll_exp_name);
17565 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017566 {
17567 if (lead == 2) /* skip over "s:" */
17568 lv.ll_name += 2;
17569 len = (int)(end - lv.ll_name);
17570 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017571
17572 /*
17573 * Copy the function name to allocated memory.
17574 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17575 * Accept <SNR>123_name() outside a script.
17576 */
17577 if (skip)
17578 lead = 0; /* do nothing */
17579 else if (lead > 0)
17580 {
17581 lead = 3;
17582 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17583 {
17584 if (current_SID <= 0)
17585 {
17586 EMSG(_(e_usingsid));
17587 goto theend;
17588 }
17589 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17590 lead += (int)STRLEN(sid_buf);
17591 }
17592 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017593 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017594 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017595 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017596 goto theend;
17597 }
17598 name = alloc((unsigned)(len + lead + 1));
17599 if (name != NULL)
17600 {
17601 if (lead > 0)
17602 {
17603 name[0] = K_SPECIAL;
17604 name[1] = KS_EXTRA;
17605 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017606 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017607 STRCPY(name + 3, sid_buf);
17608 }
17609 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17610 name[len + lead] = NUL;
17611 }
17612 *pp = end;
17613
17614theend:
17615 clear_lval(&lv);
17616 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617}
17618
17619/*
17620 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17621 * Return 2 if "p" starts with "s:".
17622 * Return 0 otherwise.
17623 */
17624 static int
17625eval_fname_script(p)
17626 char_u *p;
17627{
17628 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17629 || STRNICMP(p + 1, "SNR>", 4) == 0))
17630 return 5;
17631 if (p[0] == 's' && p[1] == ':')
17632 return 2;
17633 return 0;
17634}
17635
17636/*
17637 * Return TRUE if "p" starts with "<SID>" or "s:".
17638 * Only works if eval_fname_script() returned non-zero for "p"!
17639 */
17640 static int
17641eval_fname_sid(p)
17642 char_u *p;
17643{
17644 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17645}
17646
17647/*
17648 * List the head of the function: "name(arg1, arg2)".
17649 */
17650 static void
17651list_func_head(fp, indent)
17652 ufunc_T *fp;
17653 int indent;
17654{
17655 int j;
17656
17657 msg_start();
17658 if (indent)
17659 MSG_PUTS(" ");
17660 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017661 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662 {
17663 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017664 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 }
17666 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017667 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017668 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017669 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670 {
17671 if (j)
17672 MSG_PUTS(", ");
17673 msg_puts(FUNCARG(fp, j));
17674 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017675 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017676 {
17677 if (j)
17678 MSG_PUTS(", ");
17679 MSG_PUTS("...");
17680 }
17681 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000017682 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000017683#ifdef FEAT_EVAL
17684 if (p_verbose > 0)
17685 last_set_msg(fp->uf_script_ID);
17686#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017687}
17688
17689/*
17690 * Find a function by name, return pointer to it in ufuncs.
17691 * Return NULL for unknown function.
17692 */
17693 static ufunc_T *
17694find_func(name)
17695 char_u *name;
17696{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017697 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017698
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017699 hi = hash_find(&func_hashtab, name);
17700 if (!HASHITEM_EMPTY(hi))
17701 return HI2UF(hi);
17702 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017703}
17704
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017705#if defined(EXITFREE) || defined(PROTO)
17706 void
17707free_all_functions()
17708{
17709 hashitem_T *hi;
17710
17711 /* Need to start all over every time, because func_free() may change the
17712 * hash table. */
17713 while (func_hashtab.ht_used > 0)
17714 for (hi = func_hashtab.ht_array; ; ++hi)
17715 if (!HASHITEM_EMPTY(hi))
17716 {
17717 func_free(HI2UF(hi));
17718 break;
17719 }
17720}
17721#endif
17722
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017723/*
17724 * Return TRUE if a function "name" exists.
17725 */
17726 static int
17727function_exists(name)
17728 char_u *name;
17729{
17730 char_u *p = name;
17731 int n = FALSE;
17732
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017733 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017734 if (p != NULL)
17735 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017736 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017737 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017738 else
17739 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017740 vim_free(p);
17741 }
17742 return n;
17743}
17744
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017745/*
17746 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017747 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017748 */
17749 static int
17750builtin_function(name)
17751 char_u *name;
17752{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017753 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17754 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017755}
17756
Bram Moolenaar05159a02005-02-26 23:04:13 +000017757#if defined(FEAT_PROFILE) || defined(PROTO)
17758/*
17759 * Start profiling function "fp".
17760 */
17761 static void
17762func_do_profile(fp)
17763 ufunc_T *fp;
17764{
17765 fp->uf_tm_count = 0;
17766 profile_zero(&fp->uf_tm_self);
17767 profile_zero(&fp->uf_tm_total);
17768 if (fp->uf_tml_count == NULL)
17769 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17770 (sizeof(int) * fp->uf_lines.ga_len));
17771 if (fp->uf_tml_total == NULL)
17772 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17773 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17774 if (fp->uf_tml_self == NULL)
17775 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17776 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17777 fp->uf_tml_idx = -1;
17778 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17779 || fp->uf_tml_self == NULL)
17780 return; /* out of memory */
17781
17782 fp->uf_profiling = TRUE;
17783}
17784
17785/*
17786 * Dump the profiling results for all functions in file "fd".
17787 */
17788 void
17789func_dump_profile(fd)
17790 FILE *fd;
17791{
17792 hashitem_T *hi;
17793 int todo;
17794 ufunc_T *fp;
17795 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017796 ufunc_T **sorttab;
17797 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017798
17799 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017800 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17801
Bram Moolenaar05159a02005-02-26 23:04:13 +000017802 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17803 {
17804 if (!HASHITEM_EMPTY(hi))
17805 {
17806 --todo;
17807 fp = HI2UF(hi);
17808 if (fp->uf_profiling)
17809 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017810 if (sorttab != NULL)
17811 sorttab[st_len++] = fp;
17812
Bram Moolenaar05159a02005-02-26 23:04:13 +000017813 if (fp->uf_name[0] == K_SPECIAL)
17814 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17815 else
17816 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17817 if (fp->uf_tm_count == 1)
17818 fprintf(fd, "Called 1 time\n");
17819 else
17820 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17821 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17822 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17823 fprintf(fd, "\n");
17824 fprintf(fd, "count total (s) self (s)\n");
17825
17826 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17827 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017828 prof_func_line(fd, fp->uf_tml_count[i],
17829 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017830 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17831 }
17832 fprintf(fd, "\n");
17833 }
17834 }
17835 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017836
17837 if (sorttab != NULL && st_len > 0)
17838 {
17839 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17840 prof_total_cmp);
17841 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17842 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17843 prof_self_cmp);
17844 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17845 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017846}
Bram Moolenaar73830342005-02-28 22:48:19 +000017847
17848 static void
17849prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17850 FILE *fd;
17851 ufunc_T **sorttab;
17852 int st_len;
17853 char *title;
17854 int prefer_self; /* when equal print only self time */
17855{
17856 int i;
17857 ufunc_T *fp;
17858
17859 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17860 fprintf(fd, "count total (s) self (s) function\n");
17861 for (i = 0; i < 20 && i < st_len; ++i)
17862 {
17863 fp = sorttab[i];
17864 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17865 prefer_self);
17866 if (fp->uf_name[0] == K_SPECIAL)
17867 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17868 else
17869 fprintf(fd, " %s()\n", fp->uf_name);
17870 }
17871 fprintf(fd, "\n");
17872}
17873
17874/*
17875 * Print the count and times for one function or function line.
17876 */
17877 static void
17878prof_func_line(fd, count, total, self, prefer_self)
17879 FILE *fd;
17880 int count;
17881 proftime_T *total;
17882 proftime_T *self;
17883 int prefer_self; /* when equal print only self time */
17884{
17885 if (count > 0)
17886 {
17887 fprintf(fd, "%5d ", count);
17888 if (prefer_self && profile_equal(total, self))
17889 fprintf(fd, " ");
17890 else
17891 fprintf(fd, "%s ", profile_msg(total));
17892 if (!prefer_self && profile_equal(total, self))
17893 fprintf(fd, " ");
17894 else
17895 fprintf(fd, "%s ", profile_msg(self));
17896 }
17897 else
17898 fprintf(fd, " ");
17899}
17900
17901/*
17902 * Compare function for total time sorting.
17903 */
17904 static int
17905#ifdef __BORLANDC__
17906_RTLENTRYF
17907#endif
17908prof_total_cmp(s1, s2)
17909 const void *s1;
17910 const void *s2;
17911{
17912 ufunc_T *p1, *p2;
17913
17914 p1 = *(ufunc_T **)s1;
17915 p2 = *(ufunc_T **)s2;
17916 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
17917}
17918
17919/*
17920 * Compare function for self time sorting.
17921 */
17922 static int
17923#ifdef __BORLANDC__
17924_RTLENTRYF
17925#endif
17926prof_self_cmp(s1, s2)
17927 const void *s1;
17928 const void *s2;
17929{
17930 ufunc_T *p1, *p2;
17931
17932 p1 = *(ufunc_T **)s1;
17933 p2 = *(ufunc_T **)s2;
17934 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
17935}
17936
Bram Moolenaar05159a02005-02-26 23:04:13 +000017937#endif
17938
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017939/* The names of packages that once were loaded is remembered. */
17940static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
17941
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017942/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017943 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017944 * Return TRUE if a package was loaded.
17945 */
17946 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017947script_autoload(name, reload)
17948 char_u *name;
17949 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017950{
17951 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017952 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017953 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017954 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017955
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017956 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017957 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017958 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017959 return FALSE;
17960
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017961 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017962
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017963 /* Find the name in the list of previously loaded package names. Skip
17964 * "autoload/", it's always the same. */
17965 for (i = 0; i < ga_loaded.ga_len; ++i)
17966 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
17967 break;
17968 if (!reload && i < ga_loaded.ga_len)
17969 ret = FALSE; /* was loaded already */
17970 else
17971 {
17972 /* Remember the name if it wasn't loaded already. */
17973 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
17974 {
17975 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
17976 tofree = NULL;
17977 }
17978
17979 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000017980 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017981 ret = TRUE;
17982 }
17983
17984 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017985 return ret;
17986}
17987
17988/*
17989 * Return the autoload script name for a function or variable name.
17990 * Returns NULL when out of memory.
17991 */
17992 static char_u *
17993autoload_name(name)
17994 char_u *name;
17995{
17996 char_u *p;
17997 char_u *scriptname;
17998
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017999 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018000 scriptname = alloc((unsigned)(STRLEN(name) + 14));
18001 if (scriptname == NULL)
18002 return FALSE;
18003 STRCPY(scriptname, "autoload/");
18004 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018005 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018006 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018007 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018008 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018009 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000018010}
18011
Bram Moolenaar071d4272004-06-13 20:20:40 +000018012#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
18013
18014/*
18015 * Function given to ExpandGeneric() to obtain the list of user defined
18016 * function names.
18017 */
18018 char_u *
18019get_user_func_name(xp, idx)
18020 expand_T *xp;
18021 int idx;
18022{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018023 static long_u done;
18024 static hashitem_T *hi;
18025 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026
18027 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018028 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018029 done = 0;
18030 hi = func_hashtab.ht_array;
18031 }
18032 if (done < func_hashtab.ht_used)
18033 {
18034 if (done++ > 0)
18035 ++hi;
18036 while (HASHITEM_EMPTY(hi))
18037 ++hi;
18038 fp = HI2UF(hi);
18039
18040 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
18041 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042
18043 cat_func_name(IObuff, fp);
18044 if (xp->xp_context != EXPAND_USER_FUNC)
18045 {
18046 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018047 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018048 STRCAT(IObuff, ")");
18049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 return IObuff;
18051 }
18052 return NULL;
18053}
18054
18055#endif /* FEAT_CMDL_COMPL */
18056
18057/*
18058 * Copy the function name of "fp" to buffer "buf".
18059 * "buf" must be able to hold the function name plus three bytes.
18060 * Takes care of script-local function names.
18061 */
18062 static void
18063cat_func_name(buf, fp)
18064 char_u *buf;
18065 ufunc_T *fp;
18066{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018067 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068 {
18069 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018070 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071 }
18072 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018073 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074}
18075
18076/*
18077 * ":delfunction {name}"
18078 */
18079 void
18080ex_delfunction(eap)
18081 exarg_T *eap;
18082{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018083 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018084 char_u *p;
18085 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018086 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018087
18088 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018089 name = trans_function_name(&p, eap->skip, 0, &fudi);
18090 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018092 {
18093 if (fudi.fd_dict != NULL && !eap->skip)
18094 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018095 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018097 if (!ends_excmd(*skipwhite(p)))
18098 {
18099 vim_free(name);
18100 EMSG(_(e_trailing));
18101 return;
18102 }
18103 eap->nextcmd = check_nextcmd(p);
18104 if (eap->nextcmd != NULL)
18105 *p = NUL;
18106
18107 if (!eap->skip)
18108 fp = find_func(name);
18109 vim_free(name);
18110
18111 if (!eap->skip)
18112 {
18113 if (fp == NULL)
18114 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018115 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116 return;
18117 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018118 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119 {
18120 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18121 return;
18122 }
18123
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018124 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018125 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018126 /* Delete the dict item that refers to the function, it will
18127 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018128 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018129 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018130 else
18131 func_free(fp);
18132 }
18133}
18134
18135/*
18136 * Free a function and remove it from the list of functions.
18137 */
18138 static void
18139func_free(fp)
18140 ufunc_T *fp;
18141{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018142 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018143
18144 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018145 ga_clear_strings(&(fp->uf_args));
18146 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000018147#ifdef FEAT_PROFILE
18148 vim_free(fp->uf_tml_count);
18149 vim_free(fp->uf_tml_total);
18150 vim_free(fp->uf_tml_self);
18151#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018152
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018153 /* remove the function from the function hashtable */
18154 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
18155 if (HASHITEM_EMPTY(hi))
18156 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018157 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018158 hash_remove(&func_hashtab, hi);
18159
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018160 vim_free(fp);
18161}
18162
18163/*
18164 * Unreference a Function: decrement the reference count and free it when it
18165 * becomes zero. Only for numbered functions.
18166 */
18167 static void
18168func_unref(name)
18169 char_u *name;
18170{
18171 ufunc_T *fp;
18172
18173 if (name != NULL && isdigit(*name))
18174 {
18175 fp = find_func(name);
18176 if (fp == NULL)
18177 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018178 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018179 {
18180 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018181 * when "uf_calls" becomes zero. */
18182 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018183 func_free(fp);
18184 }
18185 }
18186}
18187
18188/*
18189 * Count a reference to a Function.
18190 */
18191 static void
18192func_ref(name)
18193 char_u *name;
18194{
18195 ufunc_T *fp;
18196
18197 if (name != NULL && isdigit(*name))
18198 {
18199 fp = find_func(name);
18200 if (fp == NULL)
18201 EMSG2(_(e_intern2), "func_ref()");
18202 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018203 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018204 }
18205}
18206
18207/*
18208 * Call a user function.
18209 */
18210 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018211call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018212 ufunc_T *fp; /* pointer to function */
18213 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018214 typval_T *argvars; /* arguments */
18215 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018216 linenr_T firstline; /* first line of range */
18217 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018218 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219{
Bram Moolenaar33570922005-01-25 22:26:29 +000018220 char_u *save_sourcing_name;
18221 linenr_T save_sourcing_lnum;
18222 scid_T save_current_SID;
18223 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018224 int save_did_emsg;
18225 static int depth = 0;
18226 dictitem_T *v;
18227 int fixvar_idx = 0; /* index in fixvar[] */
18228 int i;
18229 int ai;
18230 char_u numbuf[NUMBUFLEN];
18231 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018232#ifdef FEAT_PROFILE
18233 proftime_T wait_start;
18234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018235
18236 /* If depth of calling is getting too high, don't execute the function */
18237 if (depth >= p_mfd)
18238 {
18239 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018240 rettv->v_type = VAR_NUMBER;
18241 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242 return;
18243 }
18244 ++depth;
18245
18246 line_breakcheck(); /* check for CTRL-C hit */
18247
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018248 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018249 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018250 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018251 fc.rettv = rettv;
18252 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018253 fc.linenr = 0;
18254 fc.returned = FALSE;
18255 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018257 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258 fc.dbg_tick = debug_tick;
18259
Bram Moolenaar33570922005-01-25 22:26:29 +000018260 /*
18261 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18262 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18263 * each argument variable and saves a lot of time.
18264 */
18265 /*
18266 * Init l: variables.
18267 */
18268 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018269 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018270 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018271 /* Set l:self to "selfdict". */
18272 v = &fc.fixvar[fixvar_idx++].var;
18273 STRCPY(v->di_key, "self");
18274 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18275 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18276 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018277 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018278 v->di_tv.vval.v_dict = selfdict;
18279 ++selfdict->dv_refcount;
18280 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018281
Bram Moolenaar33570922005-01-25 22:26:29 +000018282 /*
18283 * Init a: variables.
18284 * Set a:0 to "argcount".
18285 * Set a:000 to a list with room for the "..." arguments.
18286 */
18287 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18288 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018289 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018290 v = &fc.fixvar[fixvar_idx++].var;
18291 STRCPY(v->di_key, "000");
18292 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18293 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18294 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018295 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018296 v->di_tv.vval.v_list = &fc.l_varlist;
18297 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18298 fc.l_varlist.lv_refcount = 99999;
18299
18300 /*
18301 * Set a:firstline to "firstline" and a:lastline to "lastline".
18302 * Set a:name to named arguments.
18303 * Set a:N to the "..." arguments.
18304 */
18305 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18306 (varnumber_T)firstline);
18307 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18308 (varnumber_T)lastline);
18309 for (i = 0; i < argcount; ++i)
18310 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018311 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018312 if (ai < 0)
18313 /* named argument a:name */
18314 name = FUNCARG(fp, i);
18315 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018316 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018317 /* "..." argument a:1, a:2, etc. */
18318 sprintf((char *)numbuf, "%d", ai + 1);
18319 name = numbuf;
18320 }
18321 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18322 {
18323 v = &fc.fixvar[fixvar_idx++].var;
18324 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18325 }
18326 else
18327 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018328 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18329 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018330 if (v == NULL)
18331 break;
18332 v->di_flags = DI_FLAGS_RO;
18333 }
18334 STRCPY(v->di_key, name);
18335 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18336
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018337 /* Note: the values are copied directly to avoid alloc/free.
18338 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018339 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018340 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018341
18342 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18343 {
18344 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18345 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018346 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018347 }
18348 }
18349
Bram Moolenaar071d4272004-06-13 20:20:40 +000018350 /* Don't redraw while executing the function. */
18351 ++RedrawingDisabled;
18352 save_sourcing_name = sourcing_name;
18353 save_sourcing_lnum = sourcing_lnum;
18354 sourcing_lnum = 1;
18355 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018356 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018357 if (sourcing_name != NULL)
18358 {
18359 if (save_sourcing_name != NULL
18360 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18361 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18362 else
18363 STRCPY(sourcing_name, "function ");
18364 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18365
18366 if (p_verbose >= 12)
18367 {
18368 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018369 verbose_enter_scroll();
18370
Bram Moolenaar555b2802005-05-19 21:08:39 +000018371 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018372 if (p_verbose >= 14)
18373 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018374 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018375 char_u numbuf[NUMBUFLEN];
18376 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018377
18378 msg_puts((char_u *)"(");
18379 for (i = 0; i < argcount; ++i)
18380 {
18381 if (i > 0)
18382 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018383 if (argvars[i].v_type == VAR_NUMBER)
18384 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018385 else
18386 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018387 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018388 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018389 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018390 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391 }
18392 }
18393 msg_puts((char_u *)")");
18394 }
18395 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018396
18397 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398 --no_wait_return;
18399 }
18400 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018401#ifdef FEAT_PROFILE
18402 if (do_profiling)
18403 {
18404 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18405 func_do_profile(fp);
18406 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018407 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018408 {
18409 ++fp->uf_tm_count;
18410 profile_start(&fp->uf_tm_start);
18411 profile_zero(&fp->uf_tm_children);
18412 }
18413 script_prof_save(&wait_start);
18414 }
18415#endif
18416
Bram Moolenaar071d4272004-06-13 20:20:40 +000018417 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018418 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419 save_did_emsg = did_emsg;
18420 did_emsg = FALSE;
18421
18422 /* call do_cmdline() to execute the lines */
18423 do_cmdline(NULL, get_func_line, (void *)&fc,
18424 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18425
18426 --RedrawingDisabled;
18427
18428 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018429 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018430 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018431 clear_tv(rettv);
18432 rettv->v_type = VAR_NUMBER;
18433 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434 }
18435
Bram Moolenaar05159a02005-02-26 23:04:13 +000018436#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018437 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018438 {
18439 profile_end(&fp->uf_tm_start);
18440 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18441 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18442 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18443 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018444 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018445 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018446 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18447 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018448 }
18449 }
18450#endif
18451
Bram Moolenaar071d4272004-06-13 20:20:40 +000018452 /* when being verbose, mention the return value */
18453 if (p_verbose >= 12)
18454 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018456 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018457
Bram Moolenaar071d4272004-06-13 20:20:40 +000018458 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018459 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018460 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018461 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18462 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018463 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018465 char_u buf[MSG_BUF_LEN];
18466 char_u numbuf[NUMBUFLEN];
18467 char_u *tofree;
18468
Bram Moolenaar555b2802005-05-19 21:08:39 +000018469 /* The value may be very long. Skip the middle part, so that we
18470 * have some idea how it starts and ends. smsg() would always
18471 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000018472 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018473 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018474 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018475 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018476 }
18477 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018478
18479 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018480 --no_wait_return;
18481 }
18482
18483 vim_free(sourcing_name);
18484 sourcing_name = save_sourcing_name;
18485 sourcing_lnum = save_sourcing_lnum;
18486 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018487#ifdef FEAT_PROFILE
18488 if (do_profiling)
18489 script_prof_restore(&wait_start);
18490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491
18492 if (p_verbose >= 12 && sourcing_name != NULL)
18493 {
18494 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018495 verbose_enter_scroll();
18496
Bram Moolenaar555b2802005-05-19 21:08:39 +000018497 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018498 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018499
18500 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501 --no_wait_return;
18502 }
18503
18504 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018505 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018506
Bram Moolenaar33570922005-01-25 22:26:29 +000018507 /* The a: variables typevals were not alloced, only free the allocated
18508 * variables. */
18509 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18510
18511 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018512 --depth;
18513}
18514
18515/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018516 * Add a number variable "name" to dict "dp" with value "nr".
18517 */
18518 static void
18519add_nr_var(dp, v, name, nr)
18520 dict_T *dp;
18521 dictitem_T *v;
18522 char *name;
18523 varnumber_T nr;
18524{
18525 STRCPY(v->di_key, name);
18526 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18527 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18528 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018529 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018530 v->di_tv.vval.v_number = nr;
18531}
18532
18533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018534 * ":return [expr]"
18535 */
18536 void
18537ex_return(eap)
18538 exarg_T *eap;
18539{
18540 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018541 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018542 int returning = FALSE;
18543
18544 if (current_funccal == NULL)
18545 {
18546 EMSG(_("E133: :return not inside a function"));
18547 return;
18548 }
18549
18550 if (eap->skip)
18551 ++emsg_skip;
18552
18553 eap->nextcmd = NULL;
18554 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018555 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018556 {
18557 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018558 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018560 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018561 }
18562 /* It's safer to return also on error. */
18563 else if (!eap->skip)
18564 {
18565 /*
18566 * Return unless the expression evaluation has been cancelled due to an
18567 * aborting error, an interrupt, or an exception.
18568 */
18569 if (!aborting())
18570 returning = do_return(eap, FALSE, TRUE, NULL);
18571 }
18572
18573 /* When skipping or the return gets pending, advance to the next command
18574 * in this line (!returning). Otherwise, ignore the rest of the line.
18575 * Following lines will be ignored by get_func_line(). */
18576 if (returning)
18577 eap->nextcmd = NULL;
18578 else if (eap->nextcmd == NULL) /* no argument */
18579 eap->nextcmd = check_nextcmd(arg);
18580
18581 if (eap->skip)
18582 --emsg_skip;
18583}
18584
18585/*
18586 * Return from a function. Possibly makes the return pending. Also called
18587 * for a pending return at the ":endtry" or after returning from an extra
18588 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018589 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018590 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018591 * FALSE when the return gets pending.
18592 */
18593 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018594do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018595 exarg_T *eap;
18596 int reanimate;
18597 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018598 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018599{
18600 int idx;
18601 struct condstack *cstack = eap->cstack;
18602
18603 if (reanimate)
18604 /* Undo the return. */
18605 current_funccal->returned = FALSE;
18606
18607 /*
18608 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18609 * not in its finally clause (which then is to be executed next) is found.
18610 * In this case, make the ":return" pending for execution at the ":endtry".
18611 * Otherwise, return normally.
18612 */
18613 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18614 if (idx >= 0)
18615 {
18616 cstack->cs_pending[idx] = CSTP_RETURN;
18617
18618 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018619 /* A pending return again gets pending. "rettv" points to an
18620 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018621 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018622 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018623 else
18624 {
18625 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018626 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018628 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018630 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018631 {
18632 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018633 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018634 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635 else
18636 EMSG(_(e_outofmem));
18637 }
18638 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018639 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018640
18641 if (reanimate)
18642 {
18643 /* The pending return value could be overwritten by a ":return"
18644 * without argument in a finally clause; reset the default
18645 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018646 current_funccal->rettv->v_type = VAR_NUMBER;
18647 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018648 }
18649 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018650 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651 }
18652 else
18653 {
18654 current_funccal->returned = TRUE;
18655
18656 /* If the return is carried out now, store the return value. For
18657 * a return immediately after reanimation, the value is already
18658 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018659 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018660 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018661 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018662 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018664 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018665 }
18666 }
18667
18668 return idx < 0;
18669}
18670
18671/*
18672 * Free the variable with a pending return value.
18673 */
18674 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018675discard_pending_return(rettv)
18676 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677{
Bram Moolenaar33570922005-01-25 22:26:29 +000018678 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679}
18680
18681/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018682 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683 * is an allocated string. Used by report_pending() for verbose messages.
18684 */
18685 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018686get_return_cmd(rettv)
18687 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018689 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018690 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018691 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018692
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018693 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018694 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018695 if (s == NULL)
18696 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018697
18698 STRCPY(IObuff, ":return ");
18699 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18700 if (STRLEN(s) + 8 >= IOSIZE)
18701 STRCPY(IObuff + IOSIZE - 4, "...");
18702 vim_free(tofree);
18703 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018704}
18705
18706/*
18707 * Get next function line.
18708 * Called by do_cmdline() to get the next line.
18709 * Returns allocated string, or NULL for end of function.
18710 */
18711/* ARGSUSED */
18712 char_u *
18713get_func_line(c, cookie, indent)
18714 int c; /* not used */
18715 void *cookie;
18716 int indent; /* not used */
18717{
Bram Moolenaar33570922005-01-25 22:26:29 +000018718 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018719 ufunc_T *fp = fcp->func;
18720 char_u *retval;
18721 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722
18723 /* If breakpoints have been added/deleted need to check for it. */
18724 if (fcp->dbg_tick != debug_tick)
18725 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018726 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018727 sourcing_lnum);
18728 fcp->dbg_tick = debug_tick;
18729 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018730#ifdef FEAT_PROFILE
18731 if (do_profiling)
18732 func_line_end(cookie);
18733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734
Bram Moolenaar05159a02005-02-26 23:04:13 +000018735 gap = &fp->uf_lines;
18736 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737 retval = NULL;
18738 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18739 retval = NULL;
18740 else
18741 {
18742 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18743 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018744#ifdef FEAT_PROFILE
18745 if (do_profiling)
18746 func_line_start(cookie);
18747#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748 }
18749
18750 /* Did we encounter a breakpoint? */
18751 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18752 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018753 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018755 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 sourcing_lnum);
18757 fcp->dbg_tick = debug_tick;
18758 }
18759
18760 return retval;
18761}
18762
Bram Moolenaar05159a02005-02-26 23:04:13 +000018763#if defined(FEAT_PROFILE) || defined(PROTO)
18764/*
18765 * Called when starting to read a function line.
18766 * "sourcing_lnum" must be correct!
18767 * When skipping lines it may not actually be executed, but we won't find out
18768 * until later and we need to store the time now.
18769 */
18770 void
18771func_line_start(cookie)
18772 void *cookie;
18773{
18774 funccall_T *fcp = (funccall_T *)cookie;
18775 ufunc_T *fp = fcp->func;
18776
18777 if (fp->uf_profiling && sourcing_lnum >= 1
18778 && sourcing_lnum <= fp->uf_lines.ga_len)
18779 {
18780 fp->uf_tml_idx = sourcing_lnum - 1;
18781 fp->uf_tml_execed = FALSE;
18782 profile_start(&fp->uf_tml_start);
18783 profile_zero(&fp->uf_tml_children);
18784 profile_get_wait(&fp->uf_tml_wait);
18785 }
18786}
18787
18788/*
18789 * Called when actually executing a function line.
18790 */
18791 void
18792func_line_exec(cookie)
18793 void *cookie;
18794{
18795 funccall_T *fcp = (funccall_T *)cookie;
18796 ufunc_T *fp = fcp->func;
18797
18798 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18799 fp->uf_tml_execed = TRUE;
18800}
18801
18802/*
18803 * Called when done with a function line.
18804 */
18805 void
18806func_line_end(cookie)
18807 void *cookie;
18808{
18809 funccall_T *fcp = (funccall_T *)cookie;
18810 ufunc_T *fp = fcp->func;
18811
18812 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18813 {
18814 if (fp->uf_tml_execed)
18815 {
18816 ++fp->uf_tml_count[fp->uf_tml_idx];
18817 profile_end(&fp->uf_tml_start);
18818 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18819 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18820 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18821 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18822 }
18823 fp->uf_tml_idx = -1;
18824 }
18825}
18826#endif
18827
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828/*
18829 * Return TRUE if the currently active function should be ended, because a
18830 * return was encountered or an error occured. Used inside a ":while".
18831 */
18832 int
18833func_has_ended(cookie)
18834 void *cookie;
18835{
Bram Moolenaar33570922005-01-25 22:26:29 +000018836 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837
18838 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18839 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018840 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018841 || fcp->returned);
18842}
18843
18844/*
18845 * return TRUE if cookie indicates a function which "abort"s on errors.
18846 */
18847 int
18848func_has_abort(cookie)
18849 void *cookie;
18850{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018851 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852}
18853
18854#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18855typedef enum
18856{
18857 VAR_FLAVOUR_DEFAULT,
18858 VAR_FLAVOUR_SESSION,
18859 VAR_FLAVOUR_VIMINFO
18860} var_flavour_T;
18861
18862static var_flavour_T var_flavour __ARGS((char_u *varname));
18863
18864 static var_flavour_T
18865var_flavour(varname)
18866 char_u *varname;
18867{
18868 char_u *p = varname;
18869
18870 if (ASCII_ISUPPER(*p))
18871 {
18872 while (*(++p))
18873 if (ASCII_ISLOWER(*p))
18874 return VAR_FLAVOUR_SESSION;
18875 return VAR_FLAVOUR_VIMINFO;
18876 }
18877 else
18878 return VAR_FLAVOUR_DEFAULT;
18879}
18880#endif
18881
18882#if defined(FEAT_VIMINFO) || defined(PROTO)
18883/*
18884 * Restore global vars that start with a capital from the viminfo file
18885 */
18886 int
18887read_viminfo_varlist(virp, writing)
18888 vir_T *virp;
18889 int writing;
18890{
18891 char_u *tab;
18892 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018893 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018894
18895 if (!writing && (find_viminfo_parameter('!') != NULL))
18896 {
18897 tab = vim_strchr(virp->vir_line + 1, '\t');
18898 if (tab != NULL)
18899 {
18900 *tab++ = '\0'; /* isolate the variable name */
18901 if (*tab == 'S') /* string var */
18902 is_string = TRUE;
18903
18904 tab = vim_strchr(tab, '\t');
18905 if (tab != NULL)
18906 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907 if (is_string)
18908 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018909 tv.v_type = VAR_STRING;
18910 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018912 }
18913 else
18914 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018915 tv.v_type = VAR_NUMBER;
18916 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018917 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018918 set_var(virp->vir_line + 1, &tv, FALSE);
18919 if (is_string)
18920 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018921 }
18922 }
18923 }
18924
18925 return viminfo_readline(virp);
18926}
18927
18928/*
18929 * Write global vars that start with a capital to the viminfo file
18930 */
18931 void
18932write_viminfo_varlist(fp)
18933 FILE *fp;
18934{
Bram Moolenaar33570922005-01-25 22:26:29 +000018935 hashitem_T *hi;
18936 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018937 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018938 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018939 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018940 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018941 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018942
18943 if (find_viminfo_parameter('!') == NULL)
18944 return;
18945
18946 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000018947
Bram Moolenaar33570922005-01-25 22:26:29 +000018948 todo = globvarht.ht_used;
18949 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018950 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018951 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018952 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018953 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018954 this_var = HI2DI(hi);
18955 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018956 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018957 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000018958 {
18959 case VAR_STRING: s = "STR"; break;
18960 case VAR_NUMBER: s = "NUM"; break;
18961 default: continue;
18962 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018963 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018964 p = echo_string(&this_var->di_tv, &tofree, numbuf);
18965 if (p != NULL)
18966 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000018967 vim_free(tofree);
18968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969 }
18970 }
18971}
18972#endif
18973
18974#if defined(FEAT_SESSION) || defined(PROTO)
18975 int
18976store_session_globals(fd)
18977 FILE *fd;
18978{
Bram Moolenaar33570922005-01-25 22:26:29 +000018979 hashitem_T *hi;
18980 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018981 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018982 char_u *p, *t;
18983
Bram Moolenaar33570922005-01-25 22:26:29 +000018984 todo = globvarht.ht_used;
18985 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018986 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018987 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018989 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018990 this_var = HI2DI(hi);
18991 if ((this_var->di_tv.v_type == VAR_NUMBER
18992 || this_var->di_tv.v_type == VAR_STRING)
18993 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018994 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018995 /* Escape special characters with a backslash. Turn a LF and
18996 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018997 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000018998 (char_u *)"\\\"\n\r");
18999 if (p == NULL) /* out of memory */
19000 break;
19001 for (t = p; *t != NUL; ++t)
19002 if (*t == '\n')
19003 *t = 'n';
19004 else if (*t == '\r')
19005 *t = 'r';
19006 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000019007 this_var->di_key,
19008 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19009 : ' ',
19010 p,
19011 (this_var->di_tv.v_type == VAR_STRING) ? '"'
19012 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000019013 || put_eol(fd) == FAIL)
19014 {
19015 vim_free(p);
19016 return FAIL;
19017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019018 vim_free(p);
19019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020 }
19021 }
19022 return OK;
19023}
19024#endif
19025
Bram Moolenaar661b1822005-07-28 22:36:45 +000019026/*
19027 * Display script name where an item was last set.
19028 * Should only be invoked when 'verbose' is non-zero.
19029 */
19030 void
19031last_set_msg(scriptID)
19032 scid_T scriptID;
19033{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019034 char_u *p;
19035
Bram Moolenaar661b1822005-07-28 22:36:45 +000019036 if (scriptID != 0)
19037 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000019038 p = home_replace_save(NULL, get_scriptname(scriptID));
19039 if (p != NULL)
19040 {
19041 verbose_enter();
19042 MSG_PUTS(_("\n\tLast set from "));
19043 MSG_PUTS(p);
19044 vim_free(p);
19045 verbose_leave();
19046 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000019047 }
19048}
19049
Bram Moolenaar071d4272004-06-13 20:20:40 +000019050#endif /* FEAT_EVAL */
19051
19052#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19053
19054
19055#ifdef WIN3264
19056/*
19057 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19058 */
19059static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19060static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19061static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19062
19063/*
19064 * Get the short pathname of a file.
19065 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19066 */
19067 static int
19068get_short_pathname(fnamep, bufp, fnamelen)
19069 char_u **fnamep;
19070 char_u **bufp;
19071 int *fnamelen;
19072{
19073 int l,len;
19074 char_u *newbuf;
19075
19076 len = *fnamelen;
19077
19078 l = GetShortPathName(*fnamep, *fnamep, len);
19079 if (l > len - 1)
19080 {
19081 /* If that doesn't work (not enough space), then save the string
19082 * and try again with a new buffer big enough
19083 */
19084 newbuf = vim_strnsave(*fnamep, l);
19085 if (newbuf == NULL)
19086 return 0;
19087
19088 vim_free(*bufp);
19089 *fnamep = *bufp = newbuf;
19090
19091 l = GetShortPathName(*fnamep,*fnamep,l+1);
19092
19093 /* Really should always succeed, as the buffer is big enough */
19094 }
19095
19096 *fnamelen = l;
19097 return 1;
19098}
19099
19100/*
19101 * Create a short path name. Returns the length of the buffer it needs.
19102 * Doesn't copy over the end of the buffer passed in.
19103 */
19104 static int
19105shortpath_for_invalid_fname(fname, bufp, fnamelen)
19106 char_u **fname;
19107 char_u **bufp;
19108 int *fnamelen;
19109{
19110 char_u *s, *p, *pbuf2, *pbuf3;
19111 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019112 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113
19114 /* Make a copy */
19115 len2 = *fnamelen;
19116 pbuf2 = vim_strnsave(*fname, len2);
19117 pbuf3 = NULL;
19118
19119 s = pbuf2 + len2 - 1; /* Find the end */
19120 slen = 1;
19121 plen = len2;
19122
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019123 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019124 {
19125 --s;
19126 ++slen;
19127 --plen;
19128 }
19129
19130 do
19131 {
19132 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019133 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134 {
19135 --s;
19136 ++slen;
19137 --plen;
19138 }
19139 if (s <= pbuf2)
19140 break;
19141
19142 /* Remeber the character that is about to be blatted */
19143 ch = *s;
19144 *s = 0; /* get_short_pathname requires a null-terminated string */
19145
19146 /* Try it in situ */
19147 p = pbuf2;
19148 if (!get_short_pathname(&p, &pbuf3, &plen))
19149 {
19150 vim_free(pbuf2);
19151 return -1;
19152 }
19153 *s = ch; /* Preserve the string */
19154 } while (plen == 0);
19155
19156 if (plen > 0)
19157 {
19158 /* Remeber the length of the new string. */
19159 *fnamelen = len = plen + slen;
19160 vim_free(*bufp);
19161 if (len > len2)
19162 {
19163 /* If there's not enough space in the currently allocated string,
19164 * then copy it to a buffer big enough.
19165 */
19166 *fname= *bufp = vim_strnsave(p, len);
19167 if (*fname == NULL)
19168 return -1;
19169 }
19170 else
19171 {
19172 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19173 *fname = *bufp = pbuf2;
19174 if (p != pbuf2)
19175 strncpy(*fname, p, plen);
19176 pbuf2 = NULL;
19177 }
19178 /* Concat the next bit */
19179 strncpy(*fname + plen, s, slen);
19180 (*fname)[len] = '\0';
19181 }
19182 vim_free(pbuf3);
19183 vim_free(pbuf2);
19184 return 0;
19185}
19186
19187/*
19188 * Get a pathname for a partial path.
19189 */
19190 static int
19191shortpath_for_partial(fnamep, bufp, fnamelen)
19192 char_u **fnamep;
19193 char_u **bufp;
19194 int *fnamelen;
19195{
19196 int sepcount, len, tflen;
19197 char_u *p;
19198 char_u *pbuf, *tfname;
19199 int hasTilde;
19200
19201 /* Count up the path seperators from the RHS.. so we know which part
19202 * of the path to return.
19203 */
19204 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019205 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206 if (vim_ispathsep(*p))
19207 ++sepcount;
19208
19209 /* Need full path first (use expand_env() to remove a "~/") */
19210 hasTilde = (**fnamep == '~');
19211 if (hasTilde)
19212 pbuf = tfname = expand_env_save(*fnamep);
19213 else
19214 pbuf = tfname = FullName_save(*fnamep, FALSE);
19215
19216 len = tflen = STRLEN(tfname);
19217
19218 if (!get_short_pathname(&tfname, &pbuf, &len))
19219 return -1;
19220
19221 if (len == 0)
19222 {
19223 /* Don't have a valid filename, so shorten the rest of the
19224 * path if we can. This CAN give us invalid 8.3 filenames, but
19225 * there's not a lot of point in guessing what it might be.
19226 */
19227 len = tflen;
19228 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19229 return -1;
19230 }
19231
19232 /* Count the paths backward to find the beginning of the desired string. */
19233 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019234 {
19235#ifdef FEAT_MBYTE
19236 if (has_mbyte)
19237 p -= mb_head_off(tfname, p);
19238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019239 if (vim_ispathsep(*p))
19240 {
19241 if (sepcount == 0 || (hasTilde && sepcount == 1))
19242 break;
19243 else
19244 sepcount --;
19245 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247 if (hasTilde)
19248 {
19249 --p;
19250 if (p >= tfname)
19251 *p = '~';
19252 else
19253 return -1;
19254 }
19255 else
19256 ++p;
19257
19258 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19259 vim_free(*bufp);
19260 *fnamelen = (int)STRLEN(p);
19261 *bufp = pbuf;
19262 *fnamep = p;
19263
19264 return 0;
19265}
19266#endif /* WIN3264 */
19267
19268/*
19269 * Adjust a filename, according to a string of modifiers.
19270 * *fnamep must be NUL terminated when called. When returning, the length is
19271 * determined by *fnamelen.
19272 * Returns valid flags.
19273 * When there is an error, *fnamep is set to NULL.
19274 */
19275 int
19276modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19277 char_u *src; /* string with modifiers */
19278 int *usedlen; /* characters after src that are used */
19279 char_u **fnamep; /* file name so far */
19280 char_u **bufp; /* buffer for allocated file name or NULL */
19281 int *fnamelen; /* length of fnamep */
19282{
19283 int valid = 0;
19284 char_u *tail;
19285 char_u *s, *p, *pbuf;
19286 char_u dirname[MAXPATHL];
19287 int c;
19288 int has_fullname = 0;
19289#ifdef WIN3264
19290 int has_shortname = 0;
19291#endif
19292
19293repeat:
19294 /* ":p" - full path/file_name */
19295 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19296 {
19297 has_fullname = 1;
19298
19299 valid |= VALID_PATH;
19300 *usedlen += 2;
19301
19302 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19303 if ((*fnamep)[0] == '~'
19304#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19305 && ((*fnamep)[1] == '/'
19306# ifdef BACKSLASH_IN_FILENAME
19307 || (*fnamep)[1] == '\\'
19308# endif
19309 || (*fnamep)[1] == NUL)
19310
19311#endif
19312 )
19313 {
19314 *fnamep = expand_env_save(*fnamep);
19315 vim_free(*bufp); /* free any allocated file name */
19316 *bufp = *fnamep;
19317 if (*fnamep == NULL)
19318 return -1;
19319 }
19320
19321 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019322 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323 {
19324 if (vim_ispathsep(*p)
19325 && p[1] == '.'
19326 && (p[2] == NUL
19327 || vim_ispathsep(p[2])
19328 || (p[2] == '.'
19329 && (p[3] == NUL || vim_ispathsep(p[3])))))
19330 break;
19331 }
19332
19333 /* FullName_save() is slow, don't use it when not needed. */
19334 if (*p != NUL || !vim_isAbsName(*fnamep))
19335 {
19336 *fnamep = FullName_save(*fnamep, *p != NUL);
19337 vim_free(*bufp); /* free any allocated file name */
19338 *bufp = *fnamep;
19339 if (*fnamep == NULL)
19340 return -1;
19341 }
19342
19343 /* Append a path separator to a directory. */
19344 if (mch_isdir(*fnamep))
19345 {
19346 /* Make room for one or two extra characters. */
19347 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19348 vim_free(*bufp); /* free any allocated file name */
19349 *bufp = *fnamep;
19350 if (*fnamep == NULL)
19351 return -1;
19352 add_pathsep(*fnamep);
19353 }
19354 }
19355
19356 /* ":." - path relative to the current directory */
19357 /* ":~" - path relative to the home directory */
19358 /* ":8" - shortname path - postponed till after */
19359 while (src[*usedlen] == ':'
19360 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19361 {
19362 *usedlen += 2;
19363 if (c == '8')
19364 {
19365#ifdef WIN3264
19366 has_shortname = 1; /* Postpone this. */
19367#endif
19368 continue;
19369 }
19370 pbuf = NULL;
19371 /* Need full path first (use expand_env() to remove a "~/") */
19372 if (!has_fullname)
19373 {
19374 if (c == '.' && **fnamep == '~')
19375 p = pbuf = expand_env_save(*fnamep);
19376 else
19377 p = pbuf = FullName_save(*fnamep, FALSE);
19378 }
19379 else
19380 p = *fnamep;
19381
19382 has_fullname = 0;
19383
19384 if (p != NULL)
19385 {
19386 if (c == '.')
19387 {
19388 mch_dirname(dirname, MAXPATHL);
19389 s = shorten_fname(p, dirname);
19390 if (s != NULL)
19391 {
19392 *fnamep = s;
19393 if (pbuf != NULL)
19394 {
19395 vim_free(*bufp); /* free any allocated file name */
19396 *bufp = pbuf;
19397 pbuf = NULL;
19398 }
19399 }
19400 }
19401 else
19402 {
19403 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19404 /* Only replace it when it starts with '~' */
19405 if (*dirname == '~')
19406 {
19407 s = vim_strsave(dirname);
19408 if (s != NULL)
19409 {
19410 *fnamep = s;
19411 vim_free(*bufp);
19412 *bufp = s;
19413 }
19414 }
19415 }
19416 vim_free(pbuf);
19417 }
19418 }
19419
19420 tail = gettail(*fnamep);
19421 *fnamelen = (int)STRLEN(*fnamep);
19422
19423 /* ":h" - head, remove "/file_name", can be repeated */
19424 /* Don't remove the first "/" or "c:\" */
19425 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19426 {
19427 valid |= VALID_HEAD;
19428 *usedlen += 2;
19429 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019430 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431 --tail;
19432 *fnamelen = (int)(tail - *fnamep);
19433#ifdef VMS
19434 if (*fnamelen > 0)
19435 *fnamelen += 1; /* the path separator is part of the path */
19436#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019437 while (tail > s && !after_pathsep(s, tail))
19438 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439 }
19440
19441 /* ":8" - shortname */
19442 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19443 {
19444 *usedlen += 2;
19445#ifdef WIN3264
19446 has_shortname = 1;
19447#endif
19448 }
19449
19450#ifdef WIN3264
19451 /* Check shortname after we have done 'heads' and before we do 'tails'
19452 */
19453 if (has_shortname)
19454 {
19455 pbuf = NULL;
19456 /* Copy the string if it is shortened by :h */
19457 if (*fnamelen < (int)STRLEN(*fnamep))
19458 {
19459 p = vim_strnsave(*fnamep, *fnamelen);
19460 if (p == 0)
19461 return -1;
19462 vim_free(*bufp);
19463 *bufp = *fnamep = p;
19464 }
19465
19466 /* Split into two implementations - makes it easier. First is where
19467 * there isn't a full name already, second is where there is.
19468 */
19469 if (!has_fullname && !vim_isAbsName(*fnamep))
19470 {
19471 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19472 return -1;
19473 }
19474 else
19475 {
19476 int l;
19477
19478 /* Simple case, already have the full-name
19479 * Nearly always shorter, so try first time. */
19480 l = *fnamelen;
19481 if (!get_short_pathname(fnamep, bufp, &l))
19482 return -1;
19483
19484 if (l == 0)
19485 {
19486 /* Couldn't find the filename.. search the paths.
19487 */
19488 l = *fnamelen;
19489 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19490 return -1;
19491 }
19492 *fnamelen = l;
19493 }
19494 }
19495#endif /* WIN3264 */
19496
19497 /* ":t" - tail, just the basename */
19498 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19499 {
19500 *usedlen += 2;
19501 *fnamelen -= (int)(tail - *fnamep);
19502 *fnamep = tail;
19503 }
19504
19505 /* ":e" - extension, can be repeated */
19506 /* ":r" - root, without extension, can be repeated */
19507 while (src[*usedlen] == ':'
19508 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19509 {
19510 /* find a '.' in the tail:
19511 * - for second :e: before the current fname
19512 * - otherwise: The last '.'
19513 */
19514 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19515 s = *fnamep - 2;
19516 else
19517 s = *fnamep + *fnamelen - 1;
19518 for ( ; s > tail; --s)
19519 if (s[0] == '.')
19520 break;
19521 if (src[*usedlen + 1] == 'e') /* :e */
19522 {
19523 if (s > tail)
19524 {
19525 *fnamelen += (int)(*fnamep - (s + 1));
19526 *fnamep = s + 1;
19527#ifdef VMS
19528 /* cut version from the extension */
19529 s = *fnamep + *fnamelen - 1;
19530 for ( ; s > *fnamep; --s)
19531 if (s[0] == ';')
19532 break;
19533 if (s > *fnamep)
19534 *fnamelen = s - *fnamep;
19535#endif
19536 }
19537 else if (*fnamep <= tail)
19538 *fnamelen = 0;
19539 }
19540 else /* :r */
19541 {
19542 if (s > tail) /* remove one extension */
19543 *fnamelen = (int)(s - *fnamep);
19544 }
19545 *usedlen += 2;
19546 }
19547
19548 /* ":s?pat?foo?" - substitute */
19549 /* ":gs?pat?foo?" - global substitute */
19550 if (src[*usedlen] == ':'
19551 && (src[*usedlen + 1] == 's'
19552 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19553 {
19554 char_u *str;
19555 char_u *pat;
19556 char_u *sub;
19557 int sep;
19558 char_u *flags;
19559 int didit = FALSE;
19560
19561 flags = (char_u *)"";
19562 s = src + *usedlen + 2;
19563 if (src[*usedlen + 1] == 'g')
19564 {
19565 flags = (char_u *)"g";
19566 ++s;
19567 }
19568
19569 sep = *s++;
19570 if (sep)
19571 {
19572 /* find end of pattern */
19573 p = vim_strchr(s, sep);
19574 if (p != NULL)
19575 {
19576 pat = vim_strnsave(s, (int)(p - s));
19577 if (pat != NULL)
19578 {
19579 s = p + 1;
19580 /* find end of substitution */
19581 p = vim_strchr(s, sep);
19582 if (p != NULL)
19583 {
19584 sub = vim_strnsave(s, (int)(p - s));
19585 str = vim_strnsave(*fnamep, *fnamelen);
19586 if (sub != NULL && str != NULL)
19587 {
19588 *usedlen = (int)(p + 1 - src);
19589 s = do_string_sub(str, pat, sub, flags);
19590 if (s != NULL)
19591 {
19592 *fnamep = s;
19593 *fnamelen = (int)STRLEN(s);
19594 vim_free(*bufp);
19595 *bufp = s;
19596 didit = TRUE;
19597 }
19598 }
19599 vim_free(sub);
19600 vim_free(str);
19601 }
19602 vim_free(pat);
19603 }
19604 }
19605 /* after using ":s", repeat all the modifiers */
19606 if (didit)
19607 goto repeat;
19608 }
19609 }
19610
19611 return valid;
19612}
19613
19614/*
19615 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19616 * "flags" can be "g" to do a global substitute.
19617 * Returns an allocated string, NULL for error.
19618 */
19619 char_u *
19620do_string_sub(str, pat, sub, flags)
19621 char_u *str;
19622 char_u *pat;
19623 char_u *sub;
19624 char_u *flags;
19625{
19626 int sublen;
19627 regmatch_T regmatch;
19628 int i;
19629 int do_all;
19630 char_u *tail;
19631 garray_T ga;
19632 char_u *ret;
19633 char_u *save_cpo;
19634
19635 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19636 save_cpo = p_cpo;
19637 p_cpo = (char_u *)"";
19638
19639 ga_init2(&ga, 1, 200);
19640
19641 do_all = (flags[0] == 'g');
19642
19643 regmatch.rm_ic = p_ic;
19644 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19645 if (regmatch.regprog != NULL)
19646 {
19647 tail = str;
19648 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19649 {
19650 /*
19651 * Get some space for a temporary buffer to do the substitution
19652 * into. It will contain:
19653 * - The text up to where the match is.
19654 * - The substituted text.
19655 * - The text after the match.
19656 */
19657 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19658 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19659 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19660 {
19661 ga_clear(&ga);
19662 break;
19663 }
19664
19665 /* copy the text up to where the match is */
19666 i = (int)(regmatch.startp[0] - tail);
19667 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19668 /* add the substituted text */
19669 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19670 + ga.ga_len + i, TRUE, TRUE, FALSE);
19671 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672 /* avoid getting stuck on a match with an empty string */
19673 if (tail == regmatch.endp[0])
19674 {
19675 if (*tail == NUL)
19676 break;
19677 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19678 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019679 }
19680 else
19681 {
19682 tail = regmatch.endp[0];
19683 if (*tail == NUL)
19684 break;
19685 }
19686 if (!do_all)
19687 break;
19688 }
19689
19690 if (ga.ga_data != NULL)
19691 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19692
19693 vim_free(regmatch.regprog);
19694 }
19695
19696 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19697 ga_clear(&ga);
19698 p_cpo = save_cpo;
19699
19700 return ret;
19701}
19702
19703#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */