blob: 66aed74e2ad384d1140384c51f842e7e03b1881b [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
356static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
357static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
358static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
359static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
360static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
361static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
362static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
363static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000364static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000365static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
366static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
367static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
368static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
369static list_T *list_alloc __ARGS((void));
Bram Moolenaar33570922005-01-25 22:26:29 +0000370static void list_free __ARGS((list_T *l));
371static listitem_T *listitem_alloc __ARGS((void));
372static void listitem_free __ARGS((listitem_T *item));
373static void listitem_remove __ARGS((list_T *l, listitem_T *item));
374static long list_len __ARGS((list_T *l));
375static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
376static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
377static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
378static int string_isa_number __ARGS((char_u *s));
379static listitem_T *list_find __ARGS((list_T *l, long n));
380static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000381static void list_append __ARGS((list_T *l, listitem_T *item));
382static int list_append_tv __ARGS((list_T *l, typval_T *tv));
383static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
384static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
385static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000386static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000387static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
388static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000389static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar33570922005-01-25 22:26:29 +0000390
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000391static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
392static void set_ref_in_list __ARGS((list_T *l, int copyID));
393static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaard9fba312005-06-26 22:34:35 +0000394
Bram Moolenaar33570922005-01-25 22:26:29 +0000395static void dict_unref __ARGS((dict_T *d));
396static void dict_free __ARGS((dict_T *d));
397static dictitem_T *dictitem_alloc __ARGS((char_u *key));
398static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
399static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
400static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000401static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000402static int dict_add __ARGS((dict_T *d, dictitem_T *item));
403static long dict_len __ARGS((dict_T *d));
404static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
405static char_u *dict2string __ARGS((typval_T *tv));
406static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
407
408static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
409static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
410static char_u *string_quote __ARGS((char_u *str, int function));
411static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
412static int find_internal_func __ARGS((char_u *name));
413static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
414static 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));
415static 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 +0000416static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417
418static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
419static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
420static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
421static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
422static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
423static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
424static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
425static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
426static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
427static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
428static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
429static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
430static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
431static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
432static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
433static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
434static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
435static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
436static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
437static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
438static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
439static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
440static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
441static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
442static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
443static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
444static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000468static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000469static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000482static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000483static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000510static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000511static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000527static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000528static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000531#ifdef vim_mkdir
532static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
533#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000534static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000539static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000557static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000558static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
559static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000562static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000563static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000565static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
566#ifdef HAVE_STRFTIME
567static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
568#endif
569static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000581static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000582static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000597static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000598
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000599static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
600static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000601static win_T *find_win_by_nr __ARGS((typval_T *vp));
602static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
603static int get_env_len __ARGS((char_u **arg));
604static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000605static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000606static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
607#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
608#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
609 valid character */
Bram Moolenaar33570922005-01-25 22:26:29 +0000610static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000611static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000612static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
613static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000614static typval_T *alloc_tv __ARGS((void));
615static typval_T *alloc_string_tv __ARGS((char_u *string));
616static void free_tv __ARGS((typval_T *varp));
617static void clear_tv __ARGS((typval_T *varp));
618static void init_tv __ARGS((typval_T *varp));
619static long get_tv_number __ARGS((typval_T *varp));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000620static long get_tv_number_chk __ARGS((typval_T *varp, int *denote));
Bram Moolenaar33570922005-01-25 22:26:29 +0000621static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
622static char_u *get_tv_string __ARGS((typval_T *varp));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000623static char_u *get_tv_string_chk __ARGS((typval_T *varp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000625static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000626static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000627static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000628static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
629static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
630static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
631static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
632static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
633static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
634static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000635static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000636static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000637static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000638static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
639static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
640static int eval_fname_script __ARGS((char_u *p));
641static int eval_fname_sid __ARGS((char_u *p));
642static void list_func_head __ARGS((ufunc_T *fp, int indent));
643static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
644static ufunc_T *find_func __ARGS((char_u *name));
645static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000646static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000647#ifdef FEAT_PROFILE
648static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000649static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
650static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
651static int
652# ifdef __BORLANDC__
653 _RTLENTRYF
654# endif
655 prof_total_cmp __ARGS((const void *s1, const void *s2));
656static int
657# ifdef __BORLANDC__
658 _RTLENTRYF
659# endif
660 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000661#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000662static int script_autoload __ARGS((char_u *name));
663static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static void func_free __ARGS((ufunc_T *fp));
665static void func_unref __ARGS((char_u *name));
666static void func_ref __ARGS((char_u *name));
667static 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));
668static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
669
670static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
671
672static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
673static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
674static char_u *skip_var_one __ARGS((char_u *arg));
675static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
676static void list_glob_vars __ARGS((void));
677static void list_buf_vars __ARGS((void));
678static void list_win_vars __ARGS((void));
679static void list_vim_vars __ARGS((void));
680static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
681static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
682static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000683static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
Bram Moolenaar33570922005-01-25 22:26:29 +0000684static void clear_lval __ARGS((lval_T *lp));
685static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
686static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
687static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
688static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
689static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000690static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
Bram Moolenaar33570922005-01-25 22:26:29 +0000691static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000692static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
693static void item_lock __ARGS((typval_T *tv, int deep, int lock));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000694static int tv_islocked __ARGS((typval_T *tv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000695
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000696/* Character used as separated in autoload function/variable names. */
697#define AUTOLOAD_CHAR '#'
698
Bram Moolenaar33570922005-01-25 22:26:29 +0000699/*
700 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000701 */
702 void
703eval_init()
704{
Bram Moolenaar33570922005-01-25 22:26:29 +0000705 int i;
706 struct vimvar *p;
707
708 init_var_dict(&globvardict, &globvars_var);
709 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000710 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000711 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000712
713 for (i = 0; i < VV_LEN; ++i)
714 {
715 p = &vimvars[i];
716 STRCPY(p->vv_di.di_key, p->vv_name);
717 if (p->vv_flags & VV_RO)
718 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
719 else if (p->vv_flags & VV_RO_SBX)
720 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
721 else
722 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000723
724 /* add to v: scope dict, unless the value is not always available */
725 if (p->vv_type != VAR_UNKNOWN)
726 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000727 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000728 /* add to compat scope dict */
729 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000730 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000731}
732
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000733#if defined(EXITFREE) || defined(PROTO)
734 void
735eval_clear()
736{
737 int i;
738 struct vimvar *p;
739
740 for (i = 0; i < VV_LEN; ++i)
741 {
742 p = &vimvars[i];
743 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000744 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000745 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000746 p->vv_di.di_tv.vval.v_string = NULL;
747 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000748 }
749 hash_clear(&vimvarht);
750 hash_clear(&compat_hashtab);
751
752 /* script-local variables */
753 for (i = 1; i <= ga_scripts.ga_len; ++i)
754 vars_clear(&SCRIPT_VARS(i));
755 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000756 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000757
758 /* global variables */
759 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000760
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000761 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000762 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000763 hash_clear(&func_hashtab);
764
765 /* unreferenced lists and dicts */
766 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000767}
768#endif
769
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 * Return the name of the executed function.
772 */
773 char_u *
774func_name(cookie)
775 void *cookie;
776{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000777 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778}
779
780/*
781 * Return the address holding the next breakpoint line for a funccall cookie.
782 */
783 linenr_T *
784func_breakpoint(cookie)
785 void *cookie;
786{
Bram Moolenaar33570922005-01-25 22:26:29 +0000787 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788}
789
790/*
791 * Return the address holding the debug tick for a funccall cookie.
792 */
793 int *
794func_dbg_tick(cookie)
795 void *cookie;
796{
Bram Moolenaar33570922005-01-25 22:26:29 +0000797 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798}
799
800/*
801 * Return the nesting level for a funccall cookie.
802 */
803 int
804func_level(cookie)
805 void *cookie;
806{
Bram Moolenaar33570922005-01-25 22:26:29 +0000807 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808}
809
810/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000811funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812
813/*
814 * Return TRUE when a function was ended by a ":return" command.
815 */
816 int
817current_func_returned()
818{
819 return current_funccal->returned;
820}
821
822
823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 * Set an internal variable to a string value. Creates the variable if it does
825 * not already exist.
826 */
827 void
828set_internal_string_var(name, value)
829 char_u *name;
830 char_u *value;
831{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000832 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000833 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834
835 val = vim_strsave(value);
836 if (val != NULL)
837 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000838 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000839 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000841 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000842 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 }
844 }
845}
846
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000847static lval_T *redir_lval = NULL;
848static char_u *redir_endp = NULL;
849static char_u *redir_varname = NULL;
850
851/*
852 * Start recording command output to a variable
853 * Returns OK if successfully completed the setup. FAIL otherwise.
854 */
855 int
856var_redir_start(name, append)
857 char_u *name;
858 int append; /* append to an existing variable */
859{
860 int save_emsg;
861 int err;
862 typval_T tv;
863
864 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000865 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000866 {
867 EMSG(_(e_invarg));
868 return FAIL;
869 }
870
871 redir_varname = vim_strsave(name);
872 if (redir_varname == NULL)
873 return FAIL;
874
875 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
876 if (redir_lval == NULL)
877 {
878 var_redir_stop();
879 return FAIL;
880 }
881
882 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000883 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
884 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000885 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
886 {
887 if (redir_endp != NULL && *redir_endp != NUL)
888 /* Trailing characters are present after the variable name */
889 EMSG(_(e_trailing));
890 else
891 EMSG(_(e_invarg));
892 var_redir_stop();
893 return FAIL;
894 }
895
896 /* check if we can write to the variable: set it to or append an empty
897 * string */
898 save_emsg = did_emsg;
899 did_emsg = FALSE;
900 tv.v_type = VAR_STRING;
901 tv.vval.v_string = (char_u *)"";
902 if (append)
903 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
904 else
905 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
906 err = did_emsg;
907 did_emsg += save_emsg;
908 if (err)
909 {
910 var_redir_stop();
911 return FAIL;
912 }
913 if (redir_lval->ll_newkey != NULL)
914 {
915 /* Dictionary item was created, don't do it again. */
916 vim_free(redir_lval->ll_newkey);
917 redir_lval->ll_newkey = NULL;
918 }
919
920 return OK;
921}
922
923/*
924 * Append "value[len]" to the variable set by var_redir_start().
925 */
926 void
927var_redir_str(value, len)
928 char_u *value;
929 int len;
930{
931 char_u *val;
932 typval_T tv;
933 int save_emsg;
934 int err;
935
936 if (redir_lval == NULL)
937 return;
938
939 if (len == -1)
940 /* Append the entire string */
941 val = vim_strsave(value);
942 else
943 /* Append only the specified number of characters */
944 val = vim_strnsave(value, len);
945 if (val == NULL)
946 return;
947
948 tv.v_type = VAR_STRING;
949 tv.vval.v_string = val;
950
951 save_emsg = did_emsg;
952 did_emsg = FALSE;
953 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
954 err = did_emsg;
955 did_emsg += save_emsg;
956 if (err)
957 var_redir_stop();
958
959 vim_free(tv.vval.v_string);
960}
961
962/*
963 * Stop redirecting command output to a variable.
964 */
965 void
966var_redir_stop()
967{
968 if (redir_lval != NULL)
969 {
970 clear_lval(redir_lval);
971 vim_free(redir_lval);
972 redir_lval = NULL;
973 }
974 vim_free(redir_varname);
975 redir_varname = NULL;
976}
977
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978# if defined(FEAT_MBYTE) || defined(PROTO)
979 int
980eval_charconvert(enc_from, enc_to, fname_from, fname_to)
981 char_u *enc_from;
982 char_u *enc_to;
983 char_u *fname_from;
984 char_u *fname_to;
985{
986 int err = FALSE;
987
988 set_vim_var_string(VV_CC_FROM, enc_from, -1);
989 set_vim_var_string(VV_CC_TO, enc_to, -1);
990 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
991 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
992 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
993 err = TRUE;
994 set_vim_var_string(VV_CC_FROM, NULL, -1);
995 set_vim_var_string(VV_CC_TO, NULL, -1);
996 set_vim_var_string(VV_FNAME_IN, NULL, -1);
997 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
998
999 if (err)
1000 return FAIL;
1001 return OK;
1002}
1003# endif
1004
1005# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1006 int
1007eval_printexpr(fname, args)
1008 char_u *fname;
1009 char_u *args;
1010{
1011 int err = FALSE;
1012
1013 set_vim_var_string(VV_FNAME_IN, fname, -1);
1014 set_vim_var_string(VV_CMDARG, args, -1);
1015 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1016 err = TRUE;
1017 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1018 set_vim_var_string(VV_CMDARG, NULL, -1);
1019
1020 if (err)
1021 {
1022 mch_remove(fname);
1023 return FAIL;
1024 }
1025 return OK;
1026}
1027# endif
1028
1029# if defined(FEAT_DIFF) || defined(PROTO)
1030 void
1031eval_diff(origfile, newfile, outfile)
1032 char_u *origfile;
1033 char_u *newfile;
1034 char_u *outfile;
1035{
1036 int err = FALSE;
1037
1038 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1039 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1040 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1041 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1042 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1043 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1044 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1045}
1046
1047 void
1048eval_patch(origfile, difffile, outfile)
1049 char_u *origfile;
1050 char_u *difffile;
1051 char_u *outfile;
1052{
1053 int err;
1054
1055 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1056 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1057 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1058 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1059 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1060 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1061 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1062}
1063# endif
1064
1065/*
1066 * Top level evaluation function, returning a boolean.
1067 * Sets "error" to TRUE if there was an error.
1068 * Return TRUE or FALSE.
1069 */
1070 int
1071eval_to_bool(arg, error, nextcmd, skip)
1072 char_u *arg;
1073 int *error;
1074 char_u **nextcmd;
1075 int skip; /* only parse, don't execute */
1076{
Bram Moolenaar33570922005-01-25 22:26:29 +00001077 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 int retval = FALSE;
1079
1080 if (skip)
1081 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001082 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 else
1085 {
1086 *error = FALSE;
1087 if (!skip)
1088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001089 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001090 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 }
1092 }
1093 if (skip)
1094 --emsg_skip;
1095
1096 return retval;
1097}
1098
1099/*
1100 * Top level evaluation function, returning a string. If "skip" is TRUE,
1101 * only parsing to "nextcmd" is done, without reporting errors. Return
1102 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1103 */
1104 char_u *
1105eval_to_string_skip(arg, nextcmd, skip)
1106 char_u *arg;
1107 char_u **nextcmd;
1108 int skip; /* only parse, don't execute */
1109{
Bram Moolenaar33570922005-01-25 22:26:29 +00001110 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 char_u *retval;
1112
1113 if (skip)
1114 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001115 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 retval = NULL;
1117 else
1118 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001119 retval = vim_strsave(get_tv_string(&tv));
1120 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 }
1122 if (skip)
1123 --emsg_skip;
1124
1125 return retval;
1126}
1127
1128/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001129 * Skip over an expression at "*pp".
1130 * Return FAIL for an error, OK otherwise.
1131 */
1132 int
1133skip_expr(pp)
1134 char_u **pp;
1135{
Bram Moolenaar33570922005-01-25 22:26:29 +00001136 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001137
1138 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001139 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001140}
1141
1142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 * Top level evaluation function, returning a string.
1144 * Return pointer to allocated memory, or NULL for failure.
1145 */
1146 char_u *
1147eval_to_string(arg, nextcmd)
1148 char_u *arg;
1149 char_u **nextcmd;
1150{
Bram Moolenaar33570922005-01-25 22:26:29 +00001151 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 char_u *retval;
1153
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001154 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 retval = NULL;
1156 else
1157 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001158 retval = vim_strsave(get_tv_string(&tv));
1159 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 }
1161
1162 return retval;
1163}
1164
1165/*
1166 * Call eval_to_string() with "sandbox" set and not using local variables.
1167 */
1168 char_u *
1169eval_to_string_safe(arg, nextcmd)
1170 char_u *arg;
1171 char_u **nextcmd;
1172{
1173 char_u *retval;
1174 void *save_funccalp;
1175
1176 save_funccalp = save_funccal();
1177 ++sandbox;
1178 retval = eval_to_string(arg, nextcmd);
1179 --sandbox;
1180 restore_funccal(save_funccalp);
1181 return retval;
1182}
1183
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184/*
1185 * Top level evaluation function, returning a number.
1186 * Evaluates "expr" silently.
1187 * Returns -1 for an error.
1188 */
1189 int
1190eval_to_number(expr)
1191 char_u *expr;
1192{
Bram Moolenaar33570922005-01-25 22:26:29 +00001193 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001195 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196
1197 ++emsg_off;
1198
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001199 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 retval = -1;
1201 else
1202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001203 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001204 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 }
1206 --emsg_off;
1207
1208 return retval;
1209}
1210
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001211#if defined(FEAT_SYN_HL) || defined(PROTO)
1212/*
1213 * Evaluate an expression to a list with suggestions.
1214 * For the "expr:" part of 'spellsuggest'.
1215 */
1216 list_T *
1217eval_spell_expr(badword, expr)
1218 char_u *badword;
1219 char_u *expr;
1220{
1221 typval_T save_val;
1222 typval_T rettv;
1223 list_T *list = NULL;
1224 char_u *p = skipwhite(expr);
1225
1226 /* Set "v:val" to the bad word. */
1227 prepare_vimvar(VV_VAL, &save_val);
1228 vimvars[VV_VAL].vv_type = VAR_STRING;
1229 vimvars[VV_VAL].vv_str = badword;
1230 if (p_verbose == 0)
1231 ++emsg_off;
1232
1233 if (eval1(&p, &rettv, TRUE) == OK)
1234 {
1235 if (rettv.v_type != VAR_LIST)
1236 clear_tv(&rettv);
1237 else
1238 list = rettv.vval.v_list;
1239 }
1240
1241 if (p_verbose == 0)
1242 --emsg_off;
1243 vimvars[VV_VAL].vv_str = NULL;
1244 restore_vimvar(VV_VAL, &save_val);
1245
1246 return list;
1247}
1248
1249/*
1250 * "list" is supposed to contain two items: a word and a number. Return the
1251 * word in "pp" and the number as the return value.
1252 * Return -1 if anything isn't right.
1253 * Used to get the good word and score from the eval_spell_expr() result.
1254 */
1255 int
1256get_spellword(list, pp)
1257 list_T *list;
1258 char_u **pp;
1259{
1260 listitem_T *li;
1261
1262 li = list->lv_first;
1263 if (li == NULL)
1264 return -1;
1265 *pp = get_tv_string(&li->li_tv);
1266
1267 li = li->li_next;
1268 if (li == NULL)
1269 return -1;
1270 return get_tv_number(&li->li_tv);
1271}
1272#endif
1273
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1275/*
1276 * Call some vimL function and return the result as a string
1277 * Uses argv[argc] for the function arguments.
1278 */
1279 char_u *
1280call_vim_function(func, argc, argv, safe)
1281 char_u *func;
1282 int argc;
1283 char_u **argv;
1284 int safe; /* use the sandbox */
1285{
1286 char_u *retval = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001287 typval_T rettv;
1288 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 long n;
1290 int len;
1291 int i;
1292 int doesrange;
1293 void *save_funccalp = NULL;
1294
Bram Moolenaar33570922005-01-25 22:26:29 +00001295 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 if (argvars == NULL)
1297 return NULL;
1298
1299 for (i = 0; i < argc; i++)
1300 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001301 /* Pass a NULL or empty argument as an empty string */
1302 if (argv[i] == NULL || *argv[i] == NUL)
1303 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001304 argvars[i].v_type = VAR_STRING;
1305 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001306 continue;
1307 }
1308
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 /* Recognize a number argument, the others must be strings. */
1310 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1311 if (len != 0 && len == (int)STRLEN(argv[i]))
1312 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001313 argvars[i].v_type = VAR_NUMBER;
1314 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 }
1316 else
1317 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001318 argvars[i].v_type = VAR_STRING;
1319 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
1321 }
1322
1323 if (safe)
1324 {
1325 save_funccalp = save_funccal();
1326 ++sandbox;
1327 }
1328
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1330 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00001332 &doesrange, TRUE, NULL) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001333 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001335 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 vim_free(argvars);
1337
1338 if (safe)
1339 {
1340 --sandbox;
1341 restore_funccal(save_funccalp);
1342 }
1343 return retval;
1344}
1345#endif
1346
1347/*
1348 * Save the current function call pointer, and set it to NULL.
1349 * Used when executing autocommands and for ":source".
1350 */
1351 void *
1352save_funccal()
1353{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001354 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 current_funccal = NULL;
1357 return (void *)fc;
1358}
1359
1360 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001361restore_funccal(vfc)
1362 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001364 funccall_T *fc = (funccall_T *)vfc;
1365
1366 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367}
1368
Bram Moolenaar05159a02005-02-26 23:04:13 +00001369#if defined(FEAT_PROFILE) || defined(PROTO)
1370/*
1371 * Prepare profiling for entering a child or something else that is not
1372 * counted for the script/function itself.
1373 * Should always be called in pair with prof_child_exit().
1374 */
1375 void
1376prof_child_enter(tm)
1377 proftime_T *tm; /* place to store waittime */
1378{
1379 funccall_T *fc = current_funccal;
1380
1381 if (fc != NULL && fc->func->uf_profiling)
1382 profile_start(&fc->prof_child);
1383 script_prof_save(tm);
1384}
1385
1386/*
1387 * Take care of time spent in a child.
1388 * Should always be called after prof_child_enter().
1389 */
1390 void
1391prof_child_exit(tm)
1392 proftime_T *tm; /* where waittime was stored */
1393{
1394 funccall_T *fc = current_funccal;
1395
1396 if (fc != NULL && fc->func->uf_profiling)
1397 {
1398 profile_end(&fc->prof_child);
1399 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1400 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1401 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1402 }
1403 script_prof_restore(tm);
1404}
1405#endif
1406
1407
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408#ifdef FEAT_FOLDING
1409/*
1410 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1411 * it in "*cp". Doesn't give error messages.
1412 */
1413 int
1414eval_foldexpr(arg, cp)
1415 char_u *arg;
1416 int *cp;
1417{
Bram Moolenaar33570922005-01-25 22:26:29 +00001418 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 int retval;
1420 char_u *s;
1421
1422 ++emsg_off;
1423 ++sandbox;
1424 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001425 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 retval = 0;
1427 else
1428 {
1429 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001430 if (tv.v_type == VAR_NUMBER)
1431 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001432 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 retval = 0;
1434 else
1435 {
1436 /* If the result is a string, check if there is a non-digit before
1437 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001438 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 if (!VIM_ISDIGIT(*s) && *s != '-')
1440 *cp = *s++;
1441 retval = atol((char *)s);
1442 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001443 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 }
1445 --emsg_off;
1446 --sandbox;
1447
1448 return retval;
1449}
1450#endif
1451
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001453 * ":let" list all variable values
1454 * ":let var1 var2" list variable values
1455 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001456 * ":let var += expr" assignment command.
1457 * ":let var -= expr" assignment command.
1458 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001459 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 */
1461 void
1462ex_let(eap)
1463 exarg_T *eap;
1464{
1465 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001466 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001467 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001469 int var_count = 0;
1470 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001471 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001473 expr = skip_var_list(arg, &var_count, &semicolon);
1474 if (expr == NULL)
1475 return;
1476 expr = vim_strchr(expr, '=');
1477 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001479 /*
1480 * ":let" without "=": list variables
1481 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001482 if (*arg == '[')
1483 EMSG(_(e_invarg));
1484 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001485 /* ":let var1 var2" */
1486 arg = list_arg_vars(eap, arg);
1487 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001488 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001489 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001490 list_glob_vars();
1491 list_buf_vars();
1492 list_win_vars();
1493 list_vim_vars();
1494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 eap->nextcmd = check_nextcmd(arg);
1496 }
1497 else
1498 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001499 op[0] = '=';
1500 op[1] = NUL;
1501 if (expr > arg)
1502 {
1503 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1504 op[0] = expr[-1]; /* +=, -= or .= */
1505 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001506 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001507
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 if (eap->skip)
1509 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001510 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 if (eap->skip)
1512 {
1513 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001514 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 --emsg_skip;
1516 }
1517 else if (i != FAIL)
1518 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001519 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001520 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001521 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 }
1523 }
1524}
1525
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001526/*
1527 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1528 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001529 * When "nextchars" is not NULL it points to a string with characters that
1530 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1531 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001532 * Returns OK or FAIL;
1533 */
1534 static int
1535ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1536 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001537 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001538 int copy; /* copy values from "tv", don't move */
1539 int semicolon; /* from skip_var_list() */
1540 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001541 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001542{
1543 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001544 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001545 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001546 listitem_T *item;
1547 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001548
1549 if (*arg != '[')
1550 {
1551 /*
1552 * ":let var = expr" or ":for var in list"
1553 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001554 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001555 return FAIL;
1556 return OK;
1557 }
1558
1559 /*
1560 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1561 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001562 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001563 {
1564 EMSG(_(e_listreq));
1565 return FAIL;
1566 }
1567
1568 i = list_len(l);
1569 if (semicolon == 0 && var_count < i)
1570 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001571 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001572 return FAIL;
1573 }
1574 if (var_count - semicolon > i)
1575 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001576 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001577 return FAIL;
1578 }
1579
1580 item = l->lv_first;
1581 while (*arg != ']')
1582 {
1583 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001584 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001585 item = item->li_next;
1586 if (arg == NULL)
1587 return FAIL;
1588
1589 arg = skipwhite(arg);
1590 if (*arg == ';')
1591 {
1592 /* Put the rest of the list (may be empty) in the var after ';'.
1593 * Create a new list for this. */
1594 l = list_alloc();
1595 if (l == NULL)
1596 return FAIL;
1597 while (item != NULL)
1598 {
1599 list_append_tv(l, &item->li_tv);
1600 item = item->li_next;
1601 }
1602
1603 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001604 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001605 ltv.vval.v_list = l;
1606 l->lv_refcount = 1;
1607
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001608 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1609 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001610 clear_tv(&ltv);
1611 if (arg == NULL)
1612 return FAIL;
1613 break;
1614 }
1615 else if (*arg != ',' && *arg != ']')
1616 {
1617 EMSG2(_(e_intern2), "ex_let_vars()");
1618 return FAIL;
1619 }
1620 }
1621
1622 return OK;
1623}
1624
1625/*
1626 * Skip over assignable variable "var" or list of variables "[var, var]".
1627 * Used for ":let varvar = expr" and ":for varvar in expr".
1628 * For "[var, var]" increment "*var_count" for each variable.
1629 * for "[var, var; var]" set "semicolon".
1630 * Return NULL for an error.
1631 */
1632 static char_u *
1633skip_var_list(arg, var_count, semicolon)
1634 char_u *arg;
1635 int *var_count;
1636 int *semicolon;
1637{
1638 char_u *p, *s;
1639
1640 if (*arg == '[')
1641 {
1642 /* "[var, var]": find the matching ']'. */
1643 p = arg;
1644 while (1)
1645 {
1646 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1647 s = skip_var_one(p);
1648 if (s == p)
1649 {
1650 EMSG2(_(e_invarg2), p);
1651 return NULL;
1652 }
1653 ++*var_count;
1654
1655 p = skipwhite(s);
1656 if (*p == ']')
1657 break;
1658 else if (*p == ';')
1659 {
1660 if (*semicolon == 1)
1661 {
1662 EMSG(_("Double ; in list of variables"));
1663 return NULL;
1664 }
1665 *semicolon = 1;
1666 }
1667 else if (*p != ',')
1668 {
1669 EMSG2(_(e_invarg2), p);
1670 return NULL;
1671 }
1672 }
1673 return p + 1;
1674 }
1675 else
1676 return skip_var_one(arg);
1677}
1678
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001679/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001680 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1681 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001682 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001683 static char_u *
1684skip_var_one(arg)
1685 char_u *arg;
1686{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001687 if (*arg == '@' && arg[1] != NUL)
1688 return arg + 2;
1689 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1690 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001691}
1692
Bram Moolenaara7043832005-01-21 11:56:39 +00001693/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001694 * List variables for hashtab "ht" with prefix "prefix".
1695 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001696 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001697 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001698list_hashtable_vars(ht, prefix, empty)
1699 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001700 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001701 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001702{
Bram Moolenaar33570922005-01-25 22:26:29 +00001703 hashitem_T *hi;
1704 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001705 int todo;
1706
1707 todo = ht->ht_used;
1708 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1709 {
1710 if (!HASHITEM_EMPTY(hi))
1711 {
1712 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001713 di = HI2DI(hi);
1714 if (empty || di->di_tv.v_type != VAR_STRING
1715 || di->di_tv.vval.v_string != NULL)
1716 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001717 }
1718 }
1719}
1720
1721/*
1722 * List global variables.
1723 */
1724 static void
1725list_glob_vars()
1726{
Bram Moolenaar33570922005-01-25 22:26:29 +00001727 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001728}
1729
1730/*
1731 * List buffer variables.
1732 */
1733 static void
1734list_buf_vars()
1735{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001736 char_u numbuf[NUMBUFLEN];
1737
Bram Moolenaar33570922005-01-25 22:26:29 +00001738 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001739
1740 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1741 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001742}
1743
1744/*
1745 * List window variables.
1746 */
1747 static void
1748list_win_vars()
1749{
Bram Moolenaar33570922005-01-25 22:26:29 +00001750 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001751}
1752
1753/*
1754 * List Vim variables.
1755 */
1756 static void
1757list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001758{
Bram Moolenaar33570922005-01-25 22:26:29 +00001759 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001760}
1761
1762/*
1763 * List variables in "arg".
1764 */
1765 static char_u *
1766list_arg_vars(eap, arg)
1767 exarg_T *eap;
1768 char_u *arg;
1769{
1770 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001771 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001772 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001773 char_u *name_start;
1774 char_u *arg_subsc;
1775 char_u *tofree;
1776 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001777
1778 while (!ends_excmd(*arg) && !got_int)
1779 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001780 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001781 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001782 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001783 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1784 {
1785 emsg_severe = TRUE;
1786 EMSG(_(e_trailing));
1787 break;
1788 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001789 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001790 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001791 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001792 /* get_name_len() takes care of expanding curly braces */
1793 name_start = name = arg;
1794 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1795 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001796 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001797 /* This is mainly to keep test 49 working: when expanding
1798 * curly braces fails overrule the exception error message. */
1799 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001801 emsg_severe = TRUE;
1802 EMSG2(_(e_invarg2), arg);
1803 break;
1804 }
1805 error = TRUE;
1806 }
1807 else
1808 {
1809 if (tofree != NULL)
1810 name = tofree;
1811 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001813 else
1814 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001815 /* handle d.key, l[idx], f(expr) */
1816 arg_subsc = arg;
1817 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001818 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001819 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001820 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001821 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001822 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001823 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001824 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001825 case 'g': list_glob_vars(); break;
1826 case 'b': list_buf_vars(); break;
1827 case 'w': list_win_vars(); break;
1828 case 'v': list_vim_vars(); break;
1829 default:
1830 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001831 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001832 }
1833 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001834 {
1835 char_u numbuf[NUMBUFLEN];
1836 char_u *tf;
1837 int c;
1838 char_u *s;
1839
1840 s = echo_string(&tv, &tf, numbuf);
1841 c = *arg;
1842 *arg = NUL;
1843 list_one_var_a((char_u *)"",
1844 arg == arg_subsc ? name : name_start,
1845 tv.v_type, s == NULL ? (char_u *)"" : s);
1846 *arg = c;
1847 vim_free(tf);
1848 }
1849 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001850 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001851 }
1852 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001853
1854 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001855 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001856
1857 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 }
1859
1860 return arg;
1861}
1862
1863/*
1864 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1865 * Returns a pointer to the char just after the var name.
1866 * Returns NULL if there is an error.
1867 */
1868 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001869ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001870 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001871 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001873 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001874 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875{
1876 int c1;
1877 char_u *name;
1878 char_u *p;
1879 char_u *arg_end = NULL;
1880 int len;
1881 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001882 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883
1884 /*
1885 * ":let $VAR = expr": Set environment variable.
1886 */
1887 if (*arg == '$')
1888 {
1889 /* Find the end of the name. */
1890 ++arg;
1891 name = arg;
1892 len = get_env_len(&arg);
1893 if (len == 0)
1894 EMSG2(_(e_invarg2), name - 1);
1895 else
1896 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001897 if (op != NULL && (*op == '+' || *op == '-'))
1898 EMSG2(_(e_letwrong), op);
1899 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 EMSG(_(e_letunexp));
1902 else
1903 {
1904 c1 = name[len];
1905 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001906 p = get_tv_string_chk(tv);
1907 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001908 {
1909 int mustfree = FALSE;
1910 char_u *s = vim_getenv(name, &mustfree);
1911
1912 if (s != NULL)
1913 {
1914 p = tofree = concat_str(s, p);
1915 if (mustfree)
1916 vim_free(s);
1917 }
1918 }
1919 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001920 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001922 if (STRICMP(name, "HOME") == 0)
1923 init_homedir();
1924 else if (didset_vim && STRICMP(name, "VIM") == 0)
1925 didset_vim = FALSE;
1926 else if (didset_vimruntime
1927 && STRICMP(name, "VIMRUNTIME") == 0)
1928 didset_vimruntime = FALSE;
1929 arg_end = arg;
1930 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001932 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 }
1934 }
1935 }
1936
1937 /*
1938 * ":let &option = expr": Set option value.
1939 * ":let &l:option = expr": Set local option value.
1940 * ":let &g:option = expr": Set global option value.
1941 */
1942 else if (*arg == '&')
1943 {
1944 /* Find the end of the name. */
1945 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 if (p == NULL || (endchars != NULL
1947 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948 EMSG(_(e_letunexp));
1949 else
1950 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001951 long n;
1952 int opt_type;
1953 long numval;
1954 char_u *stringval = NULL;
1955 char_u *s;
1956
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 c1 = *p;
1958 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001959
1960 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001961 s = get_tv_string_chk(tv); /* != NULL if number or string */
1962 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001963 {
1964 opt_type = get_option_value(arg, &numval,
1965 &stringval, opt_flags);
1966 if ((opt_type == 1 && *op == '.')
1967 || (opt_type == 0 && *op != '.'))
1968 EMSG2(_(e_letwrong), op);
1969 else
1970 {
1971 if (opt_type == 1) /* number */
1972 {
1973 if (*op == '+')
1974 n = numval + n;
1975 else
1976 n = numval - n;
1977 }
1978 else if (opt_type == 0 && stringval != NULL) /* string */
1979 {
1980 s = concat_str(stringval, s);
1981 vim_free(stringval);
1982 stringval = s;
1983 }
1984 }
1985 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001986 if (s != NULL)
1987 {
1988 set_option_value(arg, n, s, opt_flags);
1989 arg_end = p;
1990 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001991 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001992 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001993 }
1994 }
1995
1996 /*
1997 * ":let @r = expr": Set register contents.
1998 */
1999 else if (*arg == '@')
2000 {
2001 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002002 if (op != NULL && (*op == '+' || *op == '-'))
2003 EMSG2(_(e_letwrong), op);
2004 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002005 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002006 EMSG(_(e_letunexp));
2007 else
2008 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002009 char_u *tofree = NULL;
2010 char_u *s;
2011
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002012 p = get_tv_string_chk(tv);
2013 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002014 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002015 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002016 if (s != NULL)
2017 {
2018 p = tofree = concat_str(s, p);
2019 vim_free(s);
2020 }
2021 }
2022 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002023 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002024 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002025 arg_end = arg + 1;
2026 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002027 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002028 }
2029 }
2030
2031 /*
2032 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002033 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002034 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002035 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002036 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002037 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002038
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002039 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002040 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002041 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002042 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2043 EMSG(_(e_letunexp));
2044 else
2045 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002046 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002047 arg_end = p;
2048 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002049 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002050 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002051 }
2052
2053 else
2054 EMSG2(_(e_invarg2), arg);
2055
2056 return arg_end;
2057}
2058
2059/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002060 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2061 */
2062 static int
2063check_changedtick(arg)
2064 char_u *arg;
2065{
2066 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2067 {
2068 EMSG2(_(e_readonlyvar), arg);
2069 return TRUE;
2070 }
2071 return FALSE;
2072}
2073
2074/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002075 * Get an lval: variable, Dict item or List item that can be assigned a value
2076 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2077 * "name.key", "name.key[expr]" etc.
2078 * Indexing only works if "name" is an existing List or Dictionary.
2079 * "name" points to the start of the name.
2080 * If "rettv" is not NULL it points to the value to be assigned.
2081 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2082 * wrong; must end in space or cmd separator.
2083 *
2084 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002085 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002086 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002087 */
2088 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002089get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002090 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002091 typval_T *rettv;
2092 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002093 int unlet;
2094 int skip;
2095 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002096 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002097{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002098 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002099 char_u *expr_start, *expr_end;
2100 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002101 dictitem_T *v;
2102 typval_T var1;
2103 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002104 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002105 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002106 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002107 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002108 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002109
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002110 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002111 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002112
2113 if (skip)
2114 {
2115 /* When skipping just find the end of the name. */
2116 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002117 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002118 }
2119
2120 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002121 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002122 if (expr_start != NULL)
2123 {
2124 /* Don't expand the name when we already know there is an error. */
2125 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2126 && *p != '[' && *p != '.')
2127 {
2128 EMSG(_(e_trailing));
2129 return NULL;
2130 }
2131
2132 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2133 if (lp->ll_exp_name == NULL)
2134 {
2135 /* Report an invalid expression in braces, unless the
2136 * expression evaluation has been cancelled due to an
2137 * aborting error, an interrupt, or an exception. */
2138 if (!aborting() && !quiet)
2139 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002140 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002141 EMSG2(_(e_invarg2), name);
2142 return NULL;
2143 }
2144 }
2145 lp->ll_name = lp->ll_exp_name;
2146 }
2147 else
2148 lp->ll_name = name;
2149
2150 /* Without [idx] or .key we are done. */
2151 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2152 return p;
2153
2154 cc = *p;
2155 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002156 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002157 if (v == NULL && !quiet)
2158 EMSG2(_(e_undefvar), lp->ll_name);
2159 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160 if (v == NULL)
2161 return NULL;
2162
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002163 /*
2164 * Loop until no more [idx] or .key is following.
2165 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002166 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002167 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002168 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002169 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2170 && !(lp->ll_tv->v_type == VAR_DICT
2171 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002172 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002173 if (!quiet)
2174 EMSG(_("E689: Can only index a List or Dictionary"));
2175 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002176 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002177 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002178 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002179 if (!quiet)
2180 EMSG(_("E708: [:] must come last"));
2181 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002182 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002183
Bram Moolenaar8c711452005-01-14 21:53:12 +00002184 len = -1;
2185 if (*p == '.')
2186 {
2187 key = p + 1;
2188 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2189 ;
2190 if (len == 0)
2191 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002192 if (!quiet)
2193 EMSG(_(e_emptykey));
2194 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002195 }
2196 p = key + len;
2197 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002198 else
2199 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002200 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002201 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002202 if (*p == ':')
2203 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002204 else
2205 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002206 empty1 = FALSE;
2207 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002208 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002209 if (get_tv_string_chk(&var1) == NULL)
2210 {
2211 /* not a number or string */
2212 clear_tv(&var1);
2213 return NULL;
2214 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002215 }
2216
2217 /* Optionally get the second index [ :expr]. */
2218 if (*p == ':')
2219 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002220 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002221 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002222 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002223 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002224 if (!empty1)
2225 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002226 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002227 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002228 if (rettv != NULL && (rettv->v_type != VAR_LIST
2229 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002230 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002231 if (!quiet)
2232 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002233 if (!empty1)
2234 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002235 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002236 }
2237 p = skipwhite(p + 1);
2238 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002239 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002240 else
2241 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002242 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002243 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2244 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002245 if (!empty1)
2246 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002247 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002248 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002249 if (get_tv_string_chk(&var2) == NULL)
2250 {
2251 /* not a number or string */
2252 if (!empty1)
2253 clear_tv(&var1);
2254 clear_tv(&var2);
2255 return NULL;
2256 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002257 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002258 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002259 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002260 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002261 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002262
Bram Moolenaar8c711452005-01-14 21:53:12 +00002263 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002264 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002265 if (!quiet)
2266 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002267 if (!empty1)
2268 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002269 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002270 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002271 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002272 }
2273
2274 /* Skip to past ']'. */
2275 ++p;
2276 }
2277
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002278 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002279 {
2280 if (len == -1)
2281 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002282 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002283 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002284 if (*key == NUL)
2285 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002286 if (!quiet)
2287 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002288 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002289 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002290 }
2291 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002292 lp->ll_list = NULL;
2293 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002294 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002295 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002296 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002297 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002298 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002299 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002300 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002301 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002302 if (len == -1)
2303 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002304 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002305 }
2306 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002308 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002309 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002310 if (len == -1)
2311 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002312 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002313 p = NULL;
2314 break;
2315 }
2316 if (len == -1)
2317 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002318 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002319 }
2320 else
2321 {
2322 /*
2323 * Get the number and item for the only or first index of the List.
2324 */
2325 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002326 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002327 else
2328 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002329 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002330 clear_tv(&var1);
2331 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002332 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002333 lp->ll_list = lp->ll_tv->vval.v_list;
2334 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2335 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002336 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002337 if (!quiet)
2338 EMSGN(_(e_listidx), lp->ll_n1);
2339 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002340 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002341 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002342 }
2343
2344 /*
2345 * May need to find the item or absolute index for the second
2346 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002347 * When no index given: "lp->ll_empty2" is TRUE.
2348 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002349 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002350 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002351 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002352 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002353 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002354 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002355 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002356 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002357 if (ni == NULL)
2358 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002359 if (!quiet)
2360 EMSGN(_(e_listidx), lp->ll_n2);
2361 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002362 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002363 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002364 }
2365
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002366 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2367 if (lp->ll_n1 < 0)
2368 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2369 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002370 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002371 if (!quiet)
2372 EMSGN(_(e_listidx), lp->ll_n2);
2373 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002374 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002375 }
2376
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002377 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002378 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 }
2380
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002381 return p;
2382}
2383
2384/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002385 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002386 */
2387 static void
2388clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002389 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002390{
2391 vim_free(lp->ll_exp_name);
2392 vim_free(lp->ll_newkey);
2393}
2394
2395/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002396 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002397 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002398 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002399 */
2400 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002401set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002402 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002403 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002404 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002405 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002406 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002407{
2408 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002409 listitem_T *ni;
2410 listitem_T *ri;
2411 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002412
2413 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002415 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002416 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002417 cc = *endp;
2418 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 if (op != NULL && *op != '=')
2420 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002421 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002422
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002423 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002424 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2425 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002426 {
2427 if (tv_op(&tv, rettv, op) == OK)
2428 set_var(lp->ll_name, &tv, FALSE);
2429 clear_tv(&tv);
2430 }
2431 }
2432 else
2433 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002434 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002435 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002436 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002437 else if (tv_check_lock(lp->ll_newkey == NULL
2438 ? lp->ll_tv->v_lock
2439 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2440 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002441 else if (lp->ll_range)
2442 {
2443 /*
2444 * Assign the List values to the list items.
2445 */
2446 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002447 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 if (op != NULL && *op != '=')
2449 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2450 else
2451 {
2452 clear_tv(&lp->ll_li->li_tv);
2453 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2454 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 ri = ri->li_next;
2456 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2457 break;
2458 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002459 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 /* Need to add an empty item. */
2461 ni = listitem_alloc();
2462 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002463 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 ri = NULL;
2465 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002466 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002468 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 ni->li_tv.vval.v_number = 0;
2470 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002471 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002472 lp->ll_li = lp->ll_li->li_next;
2473 ++lp->ll_n1;
2474 }
2475 if (ri != NULL)
2476 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002477 else if (lp->ll_empty2
2478 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 : lp->ll_n1 != lp->ll_n2)
2480 EMSG(_("E711: List value has not enough items"));
2481 }
2482 else
2483 {
2484 /*
2485 * Assign to a List or Dictionary item.
2486 */
2487 if (lp->ll_newkey != NULL)
2488 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002489 if (op != NULL && *op != '=')
2490 {
2491 EMSG2(_(e_letwrong), op);
2492 return;
2493 }
2494
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002495 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002496 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 if (di == NULL)
2498 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002499 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2500 {
2501 vim_free(di);
2502 return;
2503 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002505 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002506 else if (op != NULL && *op != '=')
2507 {
2508 tv_op(lp->ll_tv, rettv, op);
2509 return;
2510 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002512 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002513
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 /*
2515 * Assign the value to the variable or list item.
2516 */
2517 if (copy)
2518 copy_tv(rettv, lp->ll_tv);
2519 else
2520 {
2521 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002522 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 }
2525 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526}
2527
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002528/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002529 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2530 * Returns OK or FAIL.
2531 */
2532 static int
2533tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002534 typval_T *tv1;
2535 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002536 char_u *op;
2537{
2538 long n;
2539 char_u numbuf[NUMBUFLEN];
2540 char_u *s;
2541
2542 /* Can't do anything with a Funcref or a Dict on the right. */
2543 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2544 {
2545 switch (tv1->v_type)
2546 {
2547 case VAR_DICT:
2548 case VAR_FUNC:
2549 break;
2550
2551 case VAR_LIST:
2552 if (*op != '+' || tv2->v_type != VAR_LIST)
2553 break;
2554 /* List += List */
2555 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2556 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2557 return OK;
2558
2559 case VAR_NUMBER:
2560 case VAR_STRING:
2561 if (tv2->v_type == VAR_LIST)
2562 break;
2563 if (*op == '+' || *op == '-')
2564 {
2565 /* nr += nr or nr -= nr*/
2566 n = get_tv_number(tv1);
2567 if (*op == '+')
2568 n += get_tv_number(tv2);
2569 else
2570 n -= get_tv_number(tv2);
2571 clear_tv(tv1);
2572 tv1->v_type = VAR_NUMBER;
2573 tv1->vval.v_number = n;
2574 }
2575 else
2576 {
2577 /* str .= str */
2578 s = get_tv_string(tv1);
2579 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2580 clear_tv(tv1);
2581 tv1->v_type = VAR_STRING;
2582 tv1->vval.v_string = s;
2583 }
2584 return OK;
2585 }
2586 }
2587
2588 EMSG2(_(e_letwrong), op);
2589 return FAIL;
2590}
2591
2592/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002593 * Add a watcher to a list.
2594 */
2595 static void
2596list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002597 list_T *l;
2598 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002599{
2600 lw->lw_next = l->lv_watch;
2601 l->lv_watch = lw;
2602}
2603
2604/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002605 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002606 * No warning when it isn't found...
2607 */
2608 static void
2609list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002610 list_T *l;
2611 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002612{
Bram Moolenaar33570922005-01-25 22:26:29 +00002613 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002614
2615 lwp = &l->lv_watch;
2616 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2617 {
2618 if (lw == lwrem)
2619 {
2620 *lwp = lw->lw_next;
2621 break;
2622 }
2623 lwp = &lw->lw_next;
2624 }
2625}
2626
2627/*
2628 * Just before removing an item from a list: advance watchers to the next
2629 * item.
2630 */
2631 static void
2632list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002633 list_T *l;
2634 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002635{
Bram Moolenaar33570922005-01-25 22:26:29 +00002636 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002637
2638 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2639 if (lw->lw_item == item)
2640 lw->lw_item = item->li_next;
2641}
2642
2643/*
2644 * Evaluate the expression used in a ":for var in expr" command.
2645 * "arg" points to "var".
2646 * Set "*errp" to TRUE for an error, FALSE otherwise;
2647 * Return a pointer that holds the info. Null when there is an error.
2648 */
2649 void *
2650eval_for_line(arg, errp, nextcmdp, skip)
2651 char_u *arg;
2652 int *errp;
2653 char_u **nextcmdp;
2654 int skip;
2655{
Bram Moolenaar33570922005-01-25 22:26:29 +00002656 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002657 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002658 typval_T tv;
2659 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002660
2661 *errp = TRUE; /* default: there is an error */
2662
Bram Moolenaar33570922005-01-25 22:26:29 +00002663 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002664 if (fi == NULL)
2665 return NULL;
2666
2667 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2668 if (expr == NULL)
2669 return fi;
2670
2671 expr = skipwhite(expr);
2672 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2673 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002674 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002675 return fi;
2676 }
2677
2678 if (skip)
2679 ++emsg_skip;
2680 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2681 {
2682 *errp = FALSE;
2683 if (!skip)
2684 {
2685 l = tv.vval.v_list;
2686 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002687 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002688 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002689 clear_tv(&tv);
2690 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002691 else
2692 {
2693 fi->fi_list = l;
2694 list_add_watch(l, &fi->fi_lw);
2695 fi->fi_lw.lw_item = l->lv_first;
2696 }
2697 }
2698 }
2699 if (skip)
2700 --emsg_skip;
2701
2702 return fi;
2703}
2704
2705/*
2706 * Use the first item in a ":for" list. Advance to the next.
2707 * Assign the values to the variable (list). "arg" points to the first one.
2708 * Return TRUE when a valid item was found, FALSE when at end of list or
2709 * something wrong.
2710 */
2711 int
2712next_for_item(fi_void, arg)
2713 void *fi_void;
2714 char_u *arg;
2715{
Bram Moolenaar33570922005-01-25 22:26:29 +00002716 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002717 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002718 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002719
2720 item = fi->fi_lw.lw_item;
2721 if (item == NULL)
2722 result = FALSE;
2723 else
2724 {
2725 fi->fi_lw.lw_item = item->li_next;
2726 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2727 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2728 }
2729 return result;
2730}
2731
2732/*
2733 * Free the structure used to store info used by ":for".
2734 */
2735 void
2736free_for_info(fi_void)
2737 void *fi_void;
2738{
Bram Moolenaar33570922005-01-25 22:26:29 +00002739 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002740
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002741 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002742 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002743 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002744 list_unref(fi->fi_list);
2745 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002746 vim_free(fi);
2747}
2748
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2750
2751 void
2752set_context_for_expression(xp, arg, cmdidx)
2753 expand_T *xp;
2754 char_u *arg;
2755 cmdidx_T cmdidx;
2756{
2757 int got_eq = FALSE;
2758 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002759 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002761 if (cmdidx == CMD_let)
2762 {
2763 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002764 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002765 {
2766 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002767 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002768 {
2769 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002770 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002771 if (vim_iswhite(*p))
2772 break;
2773 }
2774 return;
2775 }
2776 }
2777 else
2778 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2779 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 while ((xp->xp_pattern = vim_strpbrk(arg,
2781 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2782 {
2783 c = *xp->xp_pattern;
2784 if (c == '&')
2785 {
2786 c = xp->xp_pattern[1];
2787 if (c == '&')
2788 {
2789 ++xp->xp_pattern;
2790 xp->xp_context = cmdidx != CMD_let || got_eq
2791 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2792 }
2793 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002794 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002796 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2797 xp->xp_pattern += 2;
2798
2799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 }
2801 else if (c == '$')
2802 {
2803 /* environment variable */
2804 xp->xp_context = EXPAND_ENV_VARS;
2805 }
2806 else if (c == '=')
2807 {
2808 got_eq = TRUE;
2809 xp->xp_context = EXPAND_EXPRESSION;
2810 }
2811 else if (c == '<'
2812 && xp->xp_context == EXPAND_FUNCTIONS
2813 && vim_strchr(xp->xp_pattern, '(') == NULL)
2814 {
2815 /* Function name can start with "<SNR>" */
2816 break;
2817 }
2818 else if (cmdidx != CMD_let || got_eq)
2819 {
2820 if (c == '"') /* string */
2821 {
2822 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2823 if (c == '\\' && xp->xp_pattern[1] != NUL)
2824 ++xp->xp_pattern;
2825 xp->xp_context = EXPAND_NOTHING;
2826 }
2827 else if (c == '\'') /* literal string */
2828 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2831 /* skip */ ;
2832 xp->xp_context = EXPAND_NOTHING;
2833 }
2834 else if (c == '|')
2835 {
2836 if (xp->xp_pattern[1] == '|')
2837 {
2838 ++xp->xp_pattern;
2839 xp->xp_context = EXPAND_EXPRESSION;
2840 }
2841 else
2842 xp->xp_context = EXPAND_COMMANDS;
2843 }
2844 else
2845 xp->xp_context = EXPAND_EXPRESSION;
2846 }
2847 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002848 /* Doesn't look like something valid, expand as an expression
2849 * anyway. */
2850 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 arg = xp->xp_pattern;
2852 if (*arg != NUL)
2853 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2854 /* skip */ ;
2855 }
2856 xp->xp_pattern = arg;
2857}
2858
2859#endif /* FEAT_CMDL_COMPL */
2860
2861/*
2862 * ":1,25call func(arg1, arg2)" function call.
2863 */
2864 void
2865ex_call(eap)
2866 exarg_T *eap;
2867{
2868 char_u *arg = eap->arg;
2869 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002871 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002873 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 linenr_T lnum;
2875 int doesrange;
2876 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002877 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002879 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2880 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002881 if (tofree == NULL)
2882 return;
2883
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002884 /* Increase refcount on dictionary, it could get deleted when evaluating
2885 * the arguments. */
2886 if (fudi.fd_dict != NULL)
2887 ++fudi.fd_dict->dv_refcount;
2888
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002889 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2890 len = STRLEN(tofree);
2891 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892
Bram Moolenaar532c7802005-01-27 14:44:31 +00002893 /* Skip white space to allow ":call func ()". Not good, but required for
2894 * backward compatibility. */
2895 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002896 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897
2898 if (*startarg != '(')
2899 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00002900 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 goto end;
2902 }
2903
2904 /*
2905 * When skipping, evaluate the function once, to find the end of the
2906 * arguments.
2907 * When the function takes a range, this is discovered after the first
2908 * call, and the loop is broken.
2909 */
2910 if (eap->skip)
2911 {
2912 ++emsg_skip;
2913 lnum = eap->line2; /* do it once, also with an invalid range */
2914 }
2915 else
2916 lnum = eap->line1;
2917 for ( ; lnum <= eap->line2; ++lnum)
2918 {
2919 if (!eap->skip && eap->addr_count > 0)
2920 {
2921 curwin->w_cursor.lnum = lnum;
2922 curwin->w_cursor.col = 0;
2923 }
2924 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002926 eap->line1, eap->line2, &doesrange,
2927 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 {
2929 failed = TRUE;
2930 break;
2931 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002932 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 if (doesrange || eap->skip)
2934 break;
2935 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002936 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002937 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002938 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 if (aborting())
2940 break;
2941 }
2942 if (eap->skip)
2943 --emsg_skip;
2944
2945 if (!failed)
2946 {
2947 /* Check for trailing illegal characters and a following command. */
2948 if (!ends_excmd(*arg))
2949 {
2950 emsg_severe = TRUE;
2951 EMSG(_(e_trailing));
2952 }
2953 else
2954 eap->nextcmd = check_nextcmd(arg);
2955 }
2956
2957end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002958 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002959 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960}
2961
2962/*
2963 * ":unlet[!] var1 ... " command.
2964 */
2965 void
2966ex_unlet(eap)
2967 exarg_T *eap;
2968{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002969 ex_unletlock(eap, eap->arg, 0);
2970}
2971
2972/*
2973 * ":lockvar" and ":unlockvar" commands
2974 */
2975 void
2976ex_lockvar(eap)
2977 exarg_T *eap;
2978{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002980 int deep = 2;
2981
2982 if (eap->forceit)
2983 deep = -1;
2984 else if (vim_isdigit(*arg))
2985 {
2986 deep = getdigits(&arg);
2987 arg = skipwhite(arg);
2988 }
2989
2990 ex_unletlock(eap, arg, deep);
2991}
2992
2993/*
2994 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2995 */
2996 static void
2997ex_unletlock(eap, argstart, deep)
2998 exarg_T *eap;
2999 char_u *argstart;
3000 int deep;
3001{
3002 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003005 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006
3007 do
3008 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003009 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003010 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3011 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 if (lv.ll_name == NULL)
3013 error = TRUE; /* error but continue parsing */
3014 if (name_end == NULL || (!vim_iswhite(*name_end)
3015 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003017 if (name_end != NULL)
3018 {
3019 emsg_severe = TRUE;
3020 EMSG(_(e_trailing));
3021 }
3022 if (!(eap->skip || error))
3023 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 break;
3025 }
3026
3027 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003028 {
3029 if (eap->cmdidx == CMD_unlet)
3030 {
3031 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3032 error = TRUE;
3033 }
3034 else
3035 {
3036 if (do_lock_var(&lv, name_end, deep,
3037 eap->cmdidx == CMD_lockvar) == FAIL)
3038 error = TRUE;
3039 }
3040 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 if (!eap->skip)
3043 clear_lval(&lv);
3044
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 arg = skipwhite(name_end);
3046 } while (!ends_excmd(*arg));
3047
3048 eap->nextcmd = check_nextcmd(arg);
3049}
3050
Bram Moolenaar8c711452005-01-14 21:53:12 +00003051 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003053 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003054 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003055 int forceit;
3056{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003057 int ret = OK;
3058 int cc;
3059
3060 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003061 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003062 cc = *name_end;
3063 *name_end = NUL;
3064
3065 /* Normal name or expanded name. */
3066 if (check_changedtick(lp->ll_name))
3067 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003068 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003069 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003070 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003071 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003072 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3073 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003074 else if (lp->ll_range)
3075 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003076 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003077
3078 /* Delete a range of List items. */
3079 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3080 {
3081 li = lp->ll_li->li_next;
3082 listitem_remove(lp->ll_list, lp->ll_li);
3083 lp->ll_li = li;
3084 ++lp->ll_n1;
3085 }
3086 }
3087 else
3088 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003089 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003090 /* unlet a List item. */
3091 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003092 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003093 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003094 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003095 }
3096
3097 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003098}
3099
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100/*
3101 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003102 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 */
3104 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003105do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003107 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
Bram Moolenaar33570922005-01-25 22:26:29 +00003109 hashtab_T *ht;
3110 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003111 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112
Bram Moolenaar33570922005-01-25 22:26:29 +00003113 ht = find_var_ht(name, &varname);
3114 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003116 hi = hash_find(ht, varname);
3117 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003118 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003119 if (var_check_ro(HI2DI(hi)->di_flags, name))
3120 return FAIL;
3121 delete_var(ht, hi);
3122 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003125 if (forceit)
3126 return OK;
3127 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 return FAIL;
3129}
3130
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003131/*
3132 * Lock or unlock variable indicated by "lp".
3133 * "deep" is the levels to go (-1 for unlimited);
3134 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3135 */
3136 static int
3137do_lock_var(lp, name_end, deep, lock)
3138 lval_T *lp;
3139 char_u *name_end;
3140 int deep;
3141 int lock;
3142{
3143 int ret = OK;
3144 int cc;
3145 dictitem_T *di;
3146
3147 if (deep == 0) /* nothing to do */
3148 return OK;
3149
3150 if (lp->ll_tv == NULL)
3151 {
3152 cc = *name_end;
3153 *name_end = NUL;
3154
3155 /* Normal name or expanded name. */
3156 if (check_changedtick(lp->ll_name))
3157 ret = FAIL;
3158 else
3159 {
3160 di = find_var(lp->ll_name, NULL);
3161 if (di == NULL)
3162 ret = FAIL;
3163 else
3164 {
3165 if (lock)
3166 di->di_flags |= DI_FLAGS_LOCK;
3167 else
3168 di->di_flags &= ~DI_FLAGS_LOCK;
3169 item_lock(&di->di_tv, deep, lock);
3170 }
3171 }
3172 *name_end = cc;
3173 }
3174 else if (lp->ll_range)
3175 {
3176 listitem_T *li = lp->ll_li;
3177
3178 /* (un)lock a range of List items. */
3179 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3180 {
3181 item_lock(&li->li_tv, deep, lock);
3182 li = li->li_next;
3183 ++lp->ll_n1;
3184 }
3185 }
3186 else if (lp->ll_list != NULL)
3187 /* (un)lock a List item. */
3188 item_lock(&lp->ll_li->li_tv, deep, lock);
3189 else
3190 /* un(lock) a Dictionary item. */
3191 item_lock(&lp->ll_di->di_tv, deep, lock);
3192
3193 return ret;
3194}
3195
3196/*
3197 * Lock or unlock an item. "deep" is nr of levels to go.
3198 */
3199 static void
3200item_lock(tv, deep, lock)
3201 typval_T *tv;
3202 int deep;
3203 int lock;
3204{
3205 static int recurse = 0;
3206 list_T *l;
3207 listitem_T *li;
3208 dict_T *d;
3209 hashitem_T *hi;
3210 int todo;
3211
3212 if (recurse >= DICT_MAXNEST)
3213 {
3214 EMSG(_("E743: variable nested too deep for (un)lock"));
3215 return;
3216 }
3217 if (deep == 0)
3218 return;
3219 ++recurse;
3220
3221 /* lock/unlock the item itself */
3222 if (lock)
3223 tv->v_lock |= VAR_LOCKED;
3224 else
3225 tv->v_lock &= ~VAR_LOCKED;
3226
3227 switch (tv->v_type)
3228 {
3229 case VAR_LIST:
3230 if ((l = tv->vval.v_list) != NULL)
3231 {
3232 if (lock)
3233 l->lv_lock |= VAR_LOCKED;
3234 else
3235 l->lv_lock &= ~VAR_LOCKED;
3236 if (deep < 0 || deep > 1)
3237 /* recursive: lock/unlock the items the List contains */
3238 for (li = l->lv_first; li != NULL; li = li->li_next)
3239 item_lock(&li->li_tv, deep - 1, lock);
3240 }
3241 break;
3242 case VAR_DICT:
3243 if ((d = tv->vval.v_dict) != NULL)
3244 {
3245 if (lock)
3246 d->dv_lock |= VAR_LOCKED;
3247 else
3248 d->dv_lock &= ~VAR_LOCKED;
3249 if (deep < 0 || deep > 1)
3250 {
3251 /* recursive: lock/unlock the items the List contains */
3252 todo = d->dv_hashtab.ht_used;
3253 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3254 {
3255 if (!HASHITEM_EMPTY(hi))
3256 {
3257 --todo;
3258 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3259 }
3260 }
3261 }
3262 }
3263 }
3264 --recurse;
3265}
3266
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3268/*
3269 * Delete all "menutrans_" variables.
3270 */
3271 void
3272del_menutrans_vars()
3273{
Bram Moolenaar33570922005-01-25 22:26:29 +00003274 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003275 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276
Bram Moolenaar33570922005-01-25 22:26:29 +00003277 hash_lock(&globvarht);
3278 todo = globvarht.ht_used;
3279 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003280 {
3281 if (!HASHITEM_EMPTY(hi))
3282 {
3283 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003284 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3285 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003286 }
3287 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003288 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289}
3290#endif
3291
3292#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3293
3294/*
3295 * Local string buffer for the next two functions to store a variable name
3296 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3297 * get_user_var_name().
3298 */
3299
3300static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3301
3302static char_u *varnamebuf = NULL;
3303static int varnamebuflen = 0;
3304
3305/*
3306 * Function to concatenate a prefix and a variable name.
3307 */
3308 static char_u *
3309cat_prefix_varname(prefix, name)
3310 int prefix;
3311 char_u *name;
3312{
3313 int len;
3314
3315 len = (int)STRLEN(name) + 3;
3316 if (len > varnamebuflen)
3317 {
3318 vim_free(varnamebuf);
3319 len += 10; /* some additional space */
3320 varnamebuf = alloc(len);
3321 if (varnamebuf == NULL)
3322 {
3323 varnamebuflen = 0;
3324 return NULL;
3325 }
3326 varnamebuflen = len;
3327 }
3328 *varnamebuf = prefix;
3329 varnamebuf[1] = ':';
3330 STRCPY(varnamebuf + 2, name);
3331 return varnamebuf;
3332}
3333
3334/*
3335 * Function given to ExpandGeneric() to obtain the list of user defined
3336 * (global/buffer/window/built-in) variable names.
3337 */
3338/*ARGSUSED*/
3339 char_u *
3340get_user_var_name(xp, idx)
3341 expand_T *xp;
3342 int idx;
3343{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003344 static long_u gdone;
3345 static long_u bdone;
3346 static long_u wdone;
3347 static int vidx;
3348 static hashitem_T *hi;
3349 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
3351 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003352 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003353
3354 /* Global variables */
3355 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003357 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003358 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003359 else
3360 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003361 while (HASHITEM_EMPTY(hi))
3362 ++hi;
3363 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3364 return cat_prefix_varname('g', hi->hi_key);
3365 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003367
3368 /* b: variables */
3369 ht = &curbuf->b_vars.dv_hashtab;
3370 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003372 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003373 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003374 else
3375 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003376 while (HASHITEM_EMPTY(hi))
3377 ++hi;
3378 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003380 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003382 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 return (char_u *)"b:changedtick";
3384 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003385
3386 /* w: variables */
3387 ht = &curwin->w_vars.dv_hashtab;
3388 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003390 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003391 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003392 else
3393 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003394 while (HASHITEM_EMPTY(hi))
3395 ++hi;
3396 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003398
3399 /* v: variables */
3400 if (vidx < VV_LEN)
3401 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402
3403 vim_free(varnamebuf);
3404 varnamebuf = NULL;
3405 varnamebuflen = 0;
3406 return NULL;
3407}
3408
3409#endif /* FEAT_CMDL_COMPL */
3410
3411/*
3412 * types for expressions.
3413 */
3414typedef enum
3415{
3416 TYPE_UNKNOWN = 0
3417 , TYPE_EQUAL /* == */
3418 , TYPE_NEQUAL /* != */
3419 , TYPE_GREATER /* > */
3420 , TYPE_GEQUAL /* >= */
3421 , TYPE_SMALLER /* < */
3422 , TYPE_SEQUAL /* <= */
3423 , TYPE_MATCH /* =~ */
3424 , TYPE_NOMATCH /* !~ */
3425} exptype_T;
3426
3427/*
3428 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003429 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3431 */
3432
3433/*
3434 * Handle zero level expression.
3435 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003436 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 * Return OK or FAIL.
3438 */
3439 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003440eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003442 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 char_u **nextcmd;
3444 int evaluate;
3445{
3446 int ret;
3447 char_u *p;
3448
3449 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003450 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 if (ret == FAIL || !ends_excmd(*p))
3452 {
3453 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003454 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 /*
3456 * Report the invalid expression unless the expression evaluation has
3457 * been cancelled due to an aborting error, an interrupt, or an
3458 * exception.
3459 */
3460 if (!aborting())
3461 EMSG2(_(e_invexpr2), arg);
3462 ret = FAIL;
3463 }
3464 if (nextcmd != NULL)
3465 *nextcmd = check_nextcmd(p);
3466
3467 return ret;
3468}
3469
3470/*
3471 * Handle top level expression:
3472 * expr1 ? expr0 : expr0
3473 *
3474 * "arg" must point to the first non-white of the expression.
3475 * "arg" is advanced to the next non-white after the recognized expression.
3476 *
3477 * Return OK or FAIL.
3478 */
3479 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003480eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 int evaluate;
3484{
3485 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003486 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487
3488 /*
3489 * Get the first variable.
3490 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003491 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 return FAIL;
3493
3494 if ((*arg)[0] == '?')
3495 {
3496 result = FALSE;
3497 if (evaluate)
3498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003499 int error = FALSE;
3500
3501 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003503 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003504 if (error)
3505 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 }
3507
3508 /*
3509 * Get the second variable.
3510 */
3511 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003512 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 return FAIL;
3514
3515 /*
3516 * Check for the ":".
3517 */
3518 if ((*arg)[0] != ':')
3519 {
3520 EMSG(_("E109: Missing ':' after '?'"));
3521 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003522 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 return FAIL;
3524 }
3525
3526 /*
3527 * Get the third variable.
3528 */
3529 *arg = skipwhite(*arg + 1);
3530 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3531 {
3532 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003533 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 return FAIL;
3535 }
3536 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003537 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 }
3539
3540 return OK;
3541}
3542
3543/*
3544 * Handle first level expression:
3545 * expr2 || expr2 || expr2 logical OR
3546 *
3547 * "arg" must point to the first non-white of the expression.
3548 * "arg" is advanced to the next non-white after the recognized expression.
3549 *
3550 * Return OK or FAIL.
3551 */
3552 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003553eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003555 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 int evaluate;
3557{
Bram Moolenaar33570922005-01-25 22:26:29 +00003558 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 long result;
3560 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003561 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562
3563 /*
3564 * Get the first variable.
3565 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003566 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 return FAIL;
3568
3569 /*
3570 * Repeat until there is no following "||".
3571 */
3572 first = TRUE;
3573 result = FALSE;
3574 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3575 {
3576 if (evaluate && first)
3577 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003578 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003580 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003581 if (error)
3582 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 first = FALSE;
3584 }
3585
3586 /*
3587 * Get the second variable.
3588 */
3589 *arg = skipwhite(*arg + 2);
3590 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3591 return FAIL;
3592
3593 /*
3594 * Compute the result.
3595 */
3596 if (evaluate && !result)
3597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003598 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003600 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003601 if (error)
3602 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 }
3604 if (evaluate)
3605 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003606 rettv->v_type = VAR_NUMBER;
3607 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 }
3609 }
3610
3611 return OK;
3612}
3613
3614/*
3615 * Handle second level expression:
3616 * expr3 && expr3 && expr3 logical AND
3617 *
3618 * "arg" must point to the first non-white of the expression.
3619 * "arg" is advanced to the next non-white after the recognized expression.
3620 *
3621 * Return OK or FAIL.
3622 */
3623 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003624eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 int evaluate;
3628{
Bram Moolenaar33570922005-01-25 22:26:29 +00003629 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 long result;
3631 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003632 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
3634 /*
3635 * Get the first variable.
3636 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003637 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 return FAIL;
3639
3640 /*
3641 * Repeat until there is no following "&&".
3642 */
3643 first = TRUE;
3644 result = TRUE;
3645 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3646 {
3647 if (evaluate && first)
3648 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003649 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003651 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003652 if (error)
3653 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 first = FALSE;
3655 }
3656
3657 /*
3658 * Get the second variable.
3659 */
3660 *arg = skipwhite(*arg + 2);
3661 if (eval4(arg, &var2, evaluate && result) == FAIL)
3662 return FAIL;
3663
3664 /*
3665 * Compute the result.
3666 */
3667 if (evaluate && result)
3668 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003669 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003671 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003672 if (error)
3673 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 }
3675 if (evaluate)
3676 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003677 rettv->v_type = VAR_NUMBER;
3678 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
3680 }
3681
3682 return OK;
3683}
3684
3685/*
3686 * Handle third level expression:
3687 * var1 == var2
3688 * var1 =~ var2
3689 * var1 != var2
3690 * var1 !~ var2
3691 * var1 > var2
3692 * var1 >= var2
3693 * var1 < var2
3694 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003695 * var1 is var2
3696 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 *
3698 * "arg" must point to the first non-white of the expression.
3699 * "arg" is advanced to the next non-white after the recognized expression.
3700 *
3701 * Return OK or FAIL.
3702 */
3703 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003704eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 int evaluate;
3708{
Bram Moolenaar33570922005-01-25 22:26:29 +00003709 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 char_u *p;
3711 int i;
3712 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003713 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 int len = 2;
3715 long n1, n2;
3716 char_u *s1, *s2;
3717 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3718 regmatch_T regmatch;
3719 int ic;
3720 char_u *save_cpo;
3721
3722 /*
3723 * Get the first variable.
3724 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003725 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 return FAIL;
3727
3728 p = *arg;
3729 switch (p[0])
3730 {
3731 case '=': if (p[1] == '=')
3732 type = TYPE_EQUAL;
3733 else if (p[1] == '~')
3734 type = TYPE_MATCH;
3735 break;
3736 case '!': if (p[1] == '=')
3737 type = TYPE_NEQUAL;
3738 else if (p[1] == '~')
3739 type = TYPE_NOMATCH;
3740 break;
3741 case '>': if (p[1] != '=')
3742 {
3743 type = TYPE_GREATER;
3744 len = 1;
3745 }
3746 else
3747 type = TYPE_GEQUAL;
3748 break;
3749 case '<': if (p[1] != '=')
3750 {
3751 type = TYPE_SMALLER;
3752 len = 1;
3753 }
3754 else
3755 type = TYPE_SEQUAL;
3756 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003757 case 'i': if (p[1] == 's')
3758 {
3759 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3760 len = 5;
3761 if (!vim_isIDc(p[len]))
3762 {
3763 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3764 type_is = TRUE;
3765 }
3766 }
3767 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
3769
3770 /*
3771 * If there is a comparitive operator, use it.
3772 */
3773 if (type != TYPE_UNKNOWN)
3774 {
3775 /* extra question mark appended: ignore case */
3776 if (p[len] == '?')
3777 {
3778 ic = TRUE;
3779 ++len;
3780 }
3781 /* extra '#' appended: match case */
3782 else if (p[len] == '#')
3783 {
3784 ic = FALSE;
3785 ++len;
3786 }
3787 /* nothing appened: use 'ignorecase' */
3788 else
3789 ic = p_ic;
3790
3791 /*
3792 * Get the second variable.
3793 */
3794 *arg = skipwhite(p + len);
3795 if (eval5(arg, &var2, evaluate) == FAIL)
3796 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003797 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 return FAIL;
3799 }
3800
3801 if (evaluate)
3802 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003803 if (type_is && rettv->v_type != var2.v_type)
3804 {
3805 /* For "is" a different type always means FALSE, for "notis"
3806 * it means TRUE. */
3807 n1 = (type == TYPE_NEQUAL);
3808 }
3809 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3810 {
3811 if (type_is)
3812 {
3813 n1 = (rettv->v_type == var2.v_type
3814 && rettv->vval.v_list == var2.vval.v_list);
3815 if (type == TYPE_NEQUAL)
3816 n1 = !n1;
3817 }
3818 else if (rettv->v_type != var2.v_type
3819 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3820 {
3821 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003822 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003823 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003824 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003825 clear_tv(rettv);
3826 clear_tv(&var2);
3827 return FAIL;
3828 }
3829 else
3830 {
3831 /* Compare two Lists for being equal or unequal. */
3832 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3833 if (type == TYPE_NEQUAL)
3834 n1 = !n1;
3835 }
3836 }
3837
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003838 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3839 {
3840 if (type_is)
3841 {
3842 n1 = (rettv->v_type == var2.v_type
3843 && rettv->vval.v_dict == var2.vval.v_dict);
3844 if (type == TYPE_NEQUAL)
3845 n1 = !n1;
3846 }
3847 else if (rettv->v_type != var2.v_type
3848 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3849 {
3850 if (rettv->v_type != var2.v_type)
3851 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3852 else
3853 EMSG(_("E736: Invalid operation for Dictionary"));
3854 clear_tv(rettv);
3855 clear_tv(&var2);
3856 return FAIL;
3857 }
3858 else
3859 {
3860 /* Compare two Dictionaries for being equal or unequal. */
3861 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3862 if (type == TYPE_NEQUAL)
3863 n1 = !n1;
3864 }
3865 }
3866
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003867 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3868 {
3869 if (rettv->v_type != var2.v_type
3870 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3871 {
3872 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003873 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003874 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003875 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003876 clear_tv(rettv);
3877 clear_tv(&var2);
3878 return FAIL;
3879 }
3880 else
3881 {
3882 /* Compare two Funcrefs for being equal or unequal. */
3883 if (rettv->vval.v_string == NULL
3884 || var2.vval.v_string == NULL)
3885 n1 = FALSE;
3886 else
3887 n1 = STRCMP(rettv->vval.v_string,
3888 var2.vval.v_string) == 0;
3889 if (type == TYPE_NEQUAL)
3890 n1 = !n1;
3891 }
3892 }
3893
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 /*
3895 * If one of the two variables is a number, compare as a number.
3896 * When using "=~" or "!~", always compare as string.
3897 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003898 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003901 n1 = get_tv_number(rettv);
3902 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 switch (type)
3904 {
3905 case TYPE_EQUAL: n1 = (n1 == n2); break;
3906 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3907 case TYPE_GREATER: n1 = (n1 > n2); break;
3908 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3909 case TYPE_SMALLER: n1 = (n1 < n2); break;
3910 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3911 case TYPE_UNKNOWN:
3912 case TYPE_MATCH:
3913 case TYPE_NOMATCH: break; /* avoid gcc warning */
3914 }
3915 }
3916 else
3917 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003918 s1 = get_tv_string_buf(rettv, buf1);
3919 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3921 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3922 else
3923 i = 0;
3924 n1 = FALSE;
3925 switch (type)
3926 {
3927 case TYPE_EQUAL: n1 = (i == 0); break;
3928 case TYPE_NEQUAL: n1 = (i != 0); break;
3929 case TYPE_GREATER: n1 = (i > 0); break;
3930 case TYPE_GEQUAL: n1 = (i >= 0); break;
3931 case TYPE_SMALLER: n1 = (i < 0); break;
3932 case TYPE_SEQUAL: n1 = (i <= 0); break;
3933
3934 case TYPE_MATCH:
3935 case TYPE_NOMATCH:
3936 /* avoid 'l' flag in 'cpoptions' */
3937 save_cpo = p_cpo;
3938 p_cpo = (char_u *)"";
3939 regmatch.regprog = vim_regcomp(s2,
3940 RE_MAGIC + RE_STRING);
3941 regmatch.rm_ic = ic;
3942 if (regmatch.regprog != NULL)
3943 {
3944 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3945 vim_free(regmatch.regprog);
3946 if (type == TYPE_NOMATCH)
3947 n1 = !n1;
3948 }
3949 p_cpo = save_cpo;
3950 break;
3951
3952 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3953 }
3954 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003955 clear_tv(rettv);
3956 clear_tv(&var2);
3957 rettv->v_type = VAR_NUMBER;
3958 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 }
3960 }
3961
3962 return OK;
3963}
3964
3965/*
3966 * Handle fourth level expression:
3967 * + number addition
3968 * - number subtraction
3969 * . string concatenation
3970 *
3971 * "arg" must point to the first non-white of the expression.
3972 * "arg" is advanced to the next non-white after the recognized expression.
3973 *
3974 * Return OK or FAIL.
3975 */
3976 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003977eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003979 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 int evaluate;
3981{
Bram Moolenaar33570922005-01-25 22:26:29 +00003982 typval_T var2;
3983 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 int op;
3985 long n1, n2;
3986 char_u *s1, *s2;
3987 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3988 char_u *p;
3989
3990 /*
3991 * Get the first variable.
3992 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003993 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 return FAIL;
3995
3996 /*
3997 * Repeat computing, until no '+', '-' or '.' is following.
3998 */
3999 for (;;)
4000 {
4001 op = **arg;
4002 if (op != '+' && op != '-' && op != '.')
4003 break;
4004
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004005 if (op != '+' || rettv->v_type != VAR_LIST)
4006 {
4007 /* For "list + ...", an illegal use of the first operand as
4008 * a number cannot be determined before evaluating the 2nd
4009 * operand: if this is also a list, all is ok.
4010 * For "something . ...", "something - ..." or "non-list + ...",
4011 * we know that the first operand needs to be a string or number
4012 * without evaluating the 2nd operand. So check before to avoid
4013 * side effects after an error. */
4014 if (evaluate && get_tv_string_chk(rettv) == NULL)
4015 {
4016 clear_tv(rettv);
4017 return FAIL;
4018 }
4019 }
4020
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 /*
4022 * Get the second variable.
4023 */
4024 *arg = skipwhite(*arg + 1);
4025 if (eval6(arg, &var2, evaluate) == FAIL)
4026 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 return FAIL;
4029 }
4030
4031 if (evaluate)
4032 {
4033 /*
4034 * Compute the result.
4035 */
4036 if (op == '.')
4037 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004038 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4039 s2 = get_tv_string_buf_chk(&var2, buf2);
4040 if (s2 == NULL) /* type error ? */
4041 {
4042 clear_tv(rettv);
4043 clear_tv(&var2);
4044 return FAIL;
4045 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004046 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004047 clear_tv(rettv);
4048 rettv->v_type = VAR_STRING;
4049 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004051 else if (op == '+' && rettv->v_type == VAR_LIST
4052 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004053 {
4054 /* concatenate Lists */
4055 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4056 &var3) == FAIL)
4057 {
4058 clear_tv(rettv);
4059 clear_tv(&var2);
4060 return FAIL;
4061 }
4062 clear_tv(rettv);
4063 *rettv = var3;
4064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 else
4066 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004067 int error = FALSE;
4068
4069 n1 = get_tv_number_chk(rettv, &error);
4070 if (error)
4071 {
4072 /* This can only happen for "list + non-list".
4073 * For "non-list + ..." or "something - ...", we returned
4074 * before evaluating the 2nd operand. */
4075 clear_tv(rettv);
4076 return FAIL;
4077 }
4078 n2 = get_tv_number_chk(&var2, &error);
4079 if (error)
4080 {
4081 clear_tv(rettv);
4082 clear_tv(&var2);
4083 return FAIL;
4084 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004085 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 if (op == '+')
4087 n1 = n1 + n2;
4088 else
4089 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004090 rettv->v_type = VAR_NUMBER;
4091 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004093 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 }
4095 }
4096 return OK;
4097}
4098
4099/*
4100 * Handle fifth level expression:
4101 * * number multiplication
4102 * / number division
4103 * % number modulo
4104 *
4105 * "arg" must point to the first non-white of the expression.
4106 * "arg" is advanced to the next non-white after the recognized expression.
4107 *
4108 * Return OK or FAIL.
4109 */
4110 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004113 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 int evaluate;
4115{
Bram Moolenaar33570922005-01-25 22:26:29 +00004116 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 int op;
4118 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004119 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120
4121 /*
4122 * Get the first variable.
4123 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004124 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 return FAIL;
4126
4127 /*
4128 * Repeat computing, until no '*', '/' or '%' is following.
4129 */
4130 for (;;)
4131 {
4132 op = **arg;
4133 if (op != '*' && op != '/' && op != '%')
4134 break;
4135
4136 if (evaluate)
4137 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004138 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004140 if (error)
4141 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 }
4143 else
4144 n1 = 0;
4145
4146 /*
4147 * Get the second variable.
4148 */
4149 *arg = skipwhite(*arg + 1);
4150 if (eval7(arg, &var2, evaluate) == FAIL)
4151 return FAIL;
4152
4153 if (evaluate)
4154 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004155 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004156 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004157 if (error)
4158 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159
4160 /*
4161 * Compute the result.
4162 */
4163 if (op == '*')
4164 n1 = n1 * n2;
4165 else if (op == '/')
4166 {
4167 if (n2 == 0) /* give an error message? */
4168 n1 = 0x7fffffffL;
4169 else
4170 n1 = n1 / n2;
4171 }
4172 else
4173 {
4174 if (n2 == 0) /* give an error message? */
4175 n1 = 0;
4176 else
4177 n1 = n1 % n2;
4178 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004179 rettv->v_type = VAR_NUMBER;
4180 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 }
4182 }
4183
4184 return OK;
4185}
4186
4187/*
4188 * Handle sixth level expression:
4189 * number number constant
4190 * "string" string contstant
4191 * 'string' literal string contstant
4192 * &option-name option value
4193 * @r register contents
4194 * identifier variable value
4195 * function() function call
4196 * $VAR environment variable
4197 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004198 * [expr, expr] List
4199 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 *
4201 * Also handle:
4202 * ! in front logical NOT
4203 * - in front unary minus
4204 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004205 * trailing [] subscript in String or List
4206 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 *
4208 * "arg" must point to the first non-white of the expression.
4209 * "arg" is advanced to the next non-white after the recognized expression.
4210 *
4211 * Return OK or FAIL.
4212 */
4213 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004214eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004216 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 int evaluate;
4218{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 long n;
4220 int len;
4221 char_u *s;
4222 int val;
4223 char_u *start_leader, *end_leader;
4224 int ret = OK;
4225 char_u *alias;
4226
4227 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004228 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004229 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004231 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232
4233 /*
4234 * Skip '!' and '-' characters. They are handled later.
4235 */
4236 start_leader = *arg;
4237 while (**arg == '!' || **arg == '-' || **arg == '+')
4238 *arg = skipwhite(*arg + 1);
4239 end_leader = *arg;
4240
4241 switch (**arg)
4242 {
4243 /*
4244 * Number constant.
4245 */
4246 case '0':
4247 case '1':
4248 case '2':
4249 case '3':
4250 case '4':
4251 case '5':
4252 case '6':
4253 case '7':
4254 case '8':
4255 case '9':
4256 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4257 *arg += len;
4258 if (evaluate)
4259 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 rettv->v_type = VAR_NUMBER;
4261 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
4263 break;
4264
4265 /*
4266 * String constant: "string".
4267 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 break;
4270
4271 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004272 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004274 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004275 break;
4276
4277 /*
4278 * List: [expr, expr]
4279 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004280 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 break;
4282
4283 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004284 * Dictionary: {key: val, key: val}
4285 */
4286 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4287 break;
4288
4289 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004290 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004292 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 break;
4294
4295 /*
4296 * Environment variable: $VAR.
4297 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004298 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 break;
4300
4301 /*
4302 * Register contents: @r.
4303 */
4304 case '@': ++*arg;
4305 if (evaluate)
4306 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004307 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004308 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 }
4310 if (**arg != NUL)
4311 ++*arg;
4312 break;
4313
4314 /*
4315 * nested expression: (expression).
4316 */
4317 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004318 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 if (**arg == ')')
4320 ++*arg;
4321 else if (ret == OK)
4322 {
4323 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 ret = FAIL;
4326 }
4327 break;
4328
Bram Moolenaar8c711452005-01-14 21:53:12 +00004329 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 break;
4331 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004332
4333 if (ret == NOTDONE)
4334 {
4335 /*
4336 * Must be a variable or function name.
4337 * Can also be a curly-braces kind of name: {expr}.
4338 */
4339 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004340 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004341 if (alias != NULL)
4342 s = alias;
4343
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004344 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004345 ret = FAIL;
4346 else
4347 {
4348 if (**arg == '(') /* recursive! */
4349 {
4350 /* If "s" is the name of a variable of type VAR_FUNC
4351 * use its contents. */
4352 s = deref_func_name(s, &len);
4353
4354 /* Invoke the function. */
4355 ret = get_func_tv(s, len, rettv, arg,
4356 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004357 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004358 /* Stop the expression evaluation when immediately
4359 * aborting on error, or when an interrupt occurred or
4360 * an exception was thrown but not caught. */
4361 if (aborting())
4362 {
4363 if (ret == OK)
4364 clear_tv(rettv);
4365 ret = FAIL;
4366 }
4367 }
4368 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004369 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004370 else
4371 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004372 }
4373
4374 if (alias != NULL)
4375 vim_free(alias);
4376 }
4377
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 *arg = skipwhite(*arg);
4379
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004380 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4381 * expr(expr). */
4382 if (ret == OK)
4383 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384
4385 /*
4386 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4387 */
4388 if (ret == OK && evaluate && end_leader > start_leader)
4389 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004390 int error = FALSE;
4391
4392 val = get_tv_number_chk(rettv, &error);
4393 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004395 clear_tv(rettv);
4396 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004398 else
4399 {
4400 while (end_leader > start_leader)
4401 {
4402 --end_leader;
4403 if (*end_leader == '!')
4404 val = !val;
4405 else if (*end_leader == '-')
4406 val = -val;
4407 }
4408 clear_tv(rettv);
4409 rettv->v_type = VAR_NUMBER;
4410 rettv->vval.v_number = val;
4411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 }
4413
4414 return ret;
4415}
4416
4417/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004418 * Evaluate an "[expr]" or "[expr:expr]" index.
4419 * "*arg" points to the '['.
4420 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4421 */
4422 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004423eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004424 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004425 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004426 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004427 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004428{
4429 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004430 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004431 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004432 long len = -1;
4433 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004434 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004435 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004436
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004437 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004438 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004439 if (verbose)
4440 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004441 return FAIL;
4442 }
4443
Bram Moolenaar8c711452005-01-14 21:53:12 +00004444 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004445 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004446 /*
4447 * dict.name
4448 */
4449 key = *arg + 1;
4450 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4451 ;
4452 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004453 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004454 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004455 }
4456 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004457 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004458 /*
4459 * something[idx]
4460 *
4461 * Get the (first) variable from inside the [].
4462 */
4463 *arg = skipwhite(*arg + 1);
4464 if (**arg == ':')
4465 empty1 = TRUE;
4466 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4467 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004468 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4469 {
4470 /* not a number or string */
4471 clear_tv(&var1);
4472 return FAIL;
4473 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004474
4475 /*
4476 * Get the second variable from inside the [:].
4477 */
4478 if (**arg == ':')
4479 {
4480 range = TRUE;
4481 *arg = skipwhite(*arg + 1);
4482 if (**arg == ']')
4483 empty2 = TRUE;
4484 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4485 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004486 if (!empty1)
4487 clear_tv(&var1);
4488 return FAIL;
4489 }
4490 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4491 {
4492 /* not a number or string */
4493 if (!empty1)
4494 clear_tv(&var1);
4495 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004496 return FAIL;
4497 }
4498 }
4499
4500 /* Check for the ']'. */
4501 if (**arg != ']')
4502 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004503 if (verbose)
4504 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004505 clear_tv(&var1);
4506 if (range)
4507 clear_tv(&var2);
4508 return FAIL;
4509 }
4510 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004511 }
4512
4513 if (evaluate)
4514 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004515 n1 = 0;
4516 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004517 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004518 n1 = get_tv_number(&var1);
4519 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004520 }
4521 if (range)
4522 {
4523 if (empty2)
4524 n2 = -1;
4525 else
4526 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004527 n2 = get_tv_number(&var2);
4528 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004529 }
4530 }
4531
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004532 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004533 {
4534 case VAR_NUMBER:
4535 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004536 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004537 len = (long)STRLEN(s);
4538 if (range)
4539 {
4540 /* The resulting variable is a substring. If the indexes
4541 * are out of range the result is empty. */
4542 if (n1 < 0)
4543 {
4544 n1 = len + n1;
4545 if (n1 < 0)
4546 n1 = 0;
4547 }
4548 if (n2 < 0)
4549 n2 = len + n2;
4550 else if (n2 >= len)
4551 n2 = len;
4552 if (n1 >= len || n2 < 0 || n1 > n2)
4553 s = NULL;
4554 else
4555 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4556 }
4557 else
4558 {
4559 /* The resulting variable is a string of a single
4560 * character. If the index is too big or negative the
4561 * result is empty. */
4562 if (n1 >= len || n1 < 0)
4563 s = NULL;
4564 else
4565 s = vim_strnsave(s + n1, 1);
4566 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004567 clear_tv(rettv);
4568 rettv->v_type = VAR_STRING;
4569 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004570 break;
4571
4572 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004573 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004574 if (n1 < 0)
4575 n1 = len + n1;
4576 if (!empty1 && (n1 < 0 || n1 >= len))
4577 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004578 if (verbose)
4579 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004580 return FAIL;
4581 }
4582 if (range)
4583 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004584 list_T *l;
4585 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004586
4587 if (n2 < 0)
4588 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004589 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004590 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004591 if (verbose)
4592 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004593 return FAIL;
4594 }
4595 l = list_alloc();
4596 if (l == NULL)
4597 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004598 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004599 n1 <= n2; ++n1)
4600 {
4601 if (list_append_tv(l, &item->li_tv) == FAIL)
4602 {
4603 list_free(l);
4604 return FAIL;
4605 }
4606 item = item->li_next;
4607 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004608 clear_tv(rettv);
4609 rettv->v_type = VAR_LIST;
4610 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004611 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004612 }
4613 else
4614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004615 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004616 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004617 clear_tv(rettv);
4618 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 }
4620 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004621
4622 case VAR_DICT:
4623 if (range)
4624 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004625 if (verbose)
4626 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004627 if (len == -1)
4628 clear_tv(&var1);
4629 return FAIL;
4630 }
4631 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004632 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004633
4634 if (len == -1)
4635 {
4636 key = get_tv_string(&var1);
4637 if (*key == NUL)
4638 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004639 if (verbose)
4640 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004641 clear_tv(&var1);
4642 return FAIL;
4643 }
4644 }
4645
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004646 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004647
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004648 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004649 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004650 if (len == -1)
4651 clear_tv(&var1);
4652 if (item == NULL)
4653 return FAIL;
4654
4655 copy_tv(&item->di_tv, &var1);
4656 clear_tv(rettv);
4657 *rettv = var1;
4658 }
4659 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004660 }
4661 }
4662
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004663 return OK;
4664}
4665
4666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 * Get an option value.
4668 * "arg" points to the '&' or '+' before the option name.
4669 * "arg" is advanced to character after the option name.
4670 * Return OK or FAIL.
4671 */
4672 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004673get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004675 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 int evaluate;
4677{
4678 char_u *option_end;
4679 long numval;
4680 char_u *stringval;
4681 int opt_type;
4682 int c;
4683 int working = (**arg == '+'); /* has("+option") */
4684 int ret = OK;
4685 int opt_flags;
4686
4687 /*
4688 * Isolate the option name and find its value.
4689 */
4690 option_end = find_option_end(arg, &opt_flags);
4691 if (option_end == NULL)
4692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004693 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 EMSG2(_("E112: Option name missing: %s"), *arg);
4695 return FAIL;
4696 }
4697
4698 if (!evaluate)
4699 {
4700 *arg = option_end;
4701 return OK;
4702 }
4703
4704 c = *option_end;
4705 *option_end = NUL;
4706 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004707 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708
4709 if (opt_type == -3) /* invalid name */
4710 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004711 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 EMSG2(_("E113: Unknown option: %s"), *arg);
4713 ret = FAIL;
4714 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004715 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 {
4717 if (opt_type == -2) /* hidden string option */
4718 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004719 rettv->v_type = VAR_STRING;
4720 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 }
4722 else if (opt_type == -1) /* hidden number option */
4723 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004724 rettv->v_type = VAR_NUMBER;
4725 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 }
4727 else if (opt_type == 1) /* number option */
4728 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004729 rettv->v_type = VAR_NUMBER;
4730 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 }
4732 else /* string option */
4733 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004734 rettv->v_type = VAR_STRING;
4735 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 }
4737 }
4738 else if (working && (opt_type == -2 || opt_type == -1))
4739 ret = FAIL;
4740
4741 *option_end = c; /* put back for error messages */
4742 *arg = option_end;
4743
4744 return ret;
4745}
4746
4747/*
4748 * Allocate a variable for a string constant.
4749 * Return OK or FAIL.
4750 */
4751 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004752get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 int evaluate;
4756{
4757 char_u *p;
4758 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 int extra = 0;
4760
4761 /*
4762 * Find the end of the string, skipping backslashed characters.
4763 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004764 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
4766 if (*p == '\\' && p[1] != NUL)
4767 {
4768 ++p;
4769 /* A "\<x>" form occupies at least 4 characters, and produces up
4770 * to 6 characters: reserve space for 2 extra */
4771 if (*p == '<')
4772 extra += 2;
4773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 }
4775
4776 if (*p != '"')
4777 {
4778 EMSG2(_("E114: Missing quote: %s"), *arg);
4779 return FAIL;
4780 }
4781
4782 /* If only parsing, set *arg and return here */
4783 if (!evaluate)
4784 {
4785 *arg = p + 1;
4786 return OK;
4787 }
4788
4789 /*
4790 * Copy the string into allocated memory, handling backslashed
4791 * characters.
4792 */
4793 name = alloc((unsigned)(p - *arg + extra));
4794 if (name == NULL)
4795 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004796 rettv->v_type = VAR_STRING;
4797 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798
Bram Moolenaar8c711452005-01-14 21:53:12 +00004799 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 {
4801 if (*p == '\\')
4802 {
4803 switch (*++p)
4804 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004805 case 'b': *name++ = BS; ++p; break;
4806 case 'e': *name++ = ESC; ++p; break;
4807 case 'f': *name++ = FF; ++p; break;
4808 case 'n': *name++ = NL; ++p; break;
4809 case 'r': *name++ = CAR; ++p; break;
4810 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
4812 case 'X': /* hex: "\x1", "\x12" */
4813 case 'x':
4814 case 'u': /* Unicode: "\u0023" */
4815 case 'U':
4816 if (vim_isxdigit(p[1]))
4817 {
4818 int n, nr;
4819 int c = toupper(*p);
4820
4821 if (c == 'X')
4822 n = 2;
4823 else
4824 n = 4;
4825 nr = 0;
4826 while (--n >= 0 && vim_isxdigit(p[1]))
4827 {
4828 ++p;
4829 nr = (nr << 4) + hex2nr(*p);
4830 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004831 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832#ifdef FEAT_MBYTE
4833 /* For "\u" store the number according to
4834 * 'encoding'. */
4835 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004836 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 else
4838#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004839 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 break;
4842
4843 /* octal: "\1", "\12", "\123" */
4844 case '0':
4845 case '1':
4846 case '2':
4847 case '3':
4848 case '4':
4849 case '5':
4850 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004851 case '7': *name = *p++ - '0';
4852 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004854 *name = (*name << 3) + *p++ - '0';
4855 if (*p >= '0' && *p <= '7')
4856 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004858 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 break;
4860
4861 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004862 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 if (extra != 0)
4864 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004865 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 break;
4867 }
4868 /* FALLTHROUGH */
4869
Bram Moolenaar8c711452005-01-14 21:53:12 +00004870 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 break;
4872 }
4873 }
4874 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004875 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004878 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 *arg = p + 1;
4880
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 return OK;
4882}
4883
4884/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004885 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 * Return OK or FAIL.
4887 */
4888 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004889get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004891 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 int evaluate;
4893{
4894 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004895 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004896 int reduce = 0;
4897
4898 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004899 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004900 */
4901 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4902 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004903 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004904 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004905 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004906 break;
4907 ++reduce;
4908 ++p;
4909 }
4910 }
4911
Bram Moolenaar8c711452005-01-14 21:53:12 +00004912 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004913 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004914 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004915 return FAIL;
4916 }
4917
Bram Moolenaar8c711452005-01-14 21:53:12 +00004918 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004919 if (!evaluate)
4920 {
4921 *arg = p + 1;
4922 return OK;
4923 }
4924
4925 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004926 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004927 */
4928 str = alloc((unsigned)((p - *arg) - reduce));
4929 if (str == NULL)
4930 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004931 rettv->v_type = VAR_STRING;
4932 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004933
Bram Moolenaar8c711452005-01-14 21:53:12 +00004934 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004935 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004936 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004937 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004938 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004939 break;
4940 ++p;
4941 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004942 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004943 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004944 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004945 *arg = p + 1;
4946
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004947 return OK;
4948}
4949
4950/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004951 * Allocate a variable for a List and fill it from "*arg".
4952 * Return OK or FAIL.
4953 */
4954 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004955get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004956 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004957 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004958 int evaluate;
4959{
Bram Moolenaar33570922005-01-25 22:26:29 +00004960 list_T *l = NULL;
4961 typval_T tv;
4962 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004963
4964 if (evaluate)
4965 {
4966 l = list_alloc();
4967 if (l == NULL)
4968 return FAIL;
4969 }
4970
4971 *arg = skipwhite(*arg + 1);
4972 while (**arg != ']' && **arg != NUL)
4973 {
4974 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4975 goto failret;
4976 if (evaluate)
4977 {
4978 item = listitem_alloc();
4979 if (item != NULL)
4980 {
4981 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004982 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004983 list_append(l, item);
4984 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004985 else
4986 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004987 }
4988
4989 if (**arg == ']')
4990 break;
4991 if (**arg != ',')
4992 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004993 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004994 goto failret;
4995 }
4996 *arg = skipwhite(*arg + 1);
4997 }
4998
4999 if (**arg != ']')
5000 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005001 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005002failret:
5003 if (evaluate)
5004 list_free(l);
5005 return FAIL;
5006 }
5007
5008 *arg = skipwhite(*arg + 1);
5009 if (evaluate)
5010 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005011 rettv->v_type = VAR_LIST;
5012 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005013 ++l->lv_refcount;
5014 }
5015
5016 return OK;
5017}
5018
5019/*
5020 * Allocate an empty header for a list.
5021 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005022 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005023list_alloc()
5024{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005025 list_T *l;
5026
5027 l = (list_T *)alloc_clear(sizeof(list_T));
5028 if (l != NULL)
5029 {
5030 /* Prepend the list to the list of lists for garbage collection. */
5031 if (first_list != NULL)
5032 first_list->lv_used_prev = l;
5033 l->lv_used_prev = NULL;
5034 l->lv_used_next = first_list;
5035 first_list = l;
5036 }
5037 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005038}
5039
5040/*
5041 * Unreference a list: decrement the reference count and free it when it
5042 * becomes zero.
5043 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005044 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005045list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005046 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005047{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005048 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5049 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005050}
5051
5052/*
5053 * Free a list, including all items it points to.
5054 * Ignores the reference count.
5055 */
5056 static void
5057list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005058 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005059{
Bram Moolenaar33570922005-01-25 22:26:29 +00005060 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005061
Bram Moolenaard9fba312005-06-26 22:34:35 +00005062 /* Avoid that recursive reference to the list frees us again. */
5063 l->lv_refcount = DEL_REFCOUNT;
5064
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005065 /* Remove the list from the list of lists for garbage collection. */
5066 if (l->lv_used_prev == NULL)
5067 first_list = l->lv_used_next;
5068 else
5069 l->lv_used_prev->lv_used_next = l->lv_used_next;
5070 if (l->lv_used_next != NULL)
5071 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5072
Bram Moolenaard9fba312005-06-26 22:34:35 +00005073 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005074 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005075 /* Remove the item before deleting it. */
5076 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005077 listitem_free(item);
5078 }
5079 vim_free(l);
5080}
5081
5082/*
5083 * Allocate a list item.
5084 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005085 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005086listitem_alloc()
5087{
Bram Moolenaar33570922005-01-25 22:26:29 +00005088 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005089}
5090
5091/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005092 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005093 */
5094 static void
5095listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005096 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005097{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005098 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005099 vim_free(item);
5100}
5101
5102/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005103 * Remove a list item from a List and free it. Also clears the value.
5104 */
5105 static void
5106listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005107 list_T *l;
5108 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005109{
5110 list_remove(l, item, item);
5111 listitem_free(item);
5112}
5113
5114/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005115 * Get the number of items in a list.
5116 */
5117 static long
5118list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005119 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005120{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005121 if (l == NULL)
5122 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005123 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005124}
5125
5126/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005127 * Return TRUE when two lists have exactly the same values.
5128 */
5129 static int
5130list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005131 list_T *l1;
5132 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005133 int ic; /* ignore case for strings */
5134{
Bram Moolenaar33570922005-01-25 22:26:29 +00005135 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005136
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005137 if (list_len(l1) != list_len(l2))
5138 return FALSE;
5139
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005140 for (item1 = l1->lv_first, item2 = l2->lv_first;
5141 item1 != NULL && item2 != NULL;
5142 item1 = item1->li_next, item2 = item2->li_next)
5143 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5144 return FALSE;
5145 return item1 == NULL && item2 == NULL;
5146}
5147
5148/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005149 * Return TRUE when two dictionaries have exactly the same key/values.
5150 */
5151 static int
5152dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005153 dict_T *d1;
5154 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005155 int ic; /* ignore case for strings */
5156{
Bram Moolenaar33570922005-01-25 22:26:29 +00005157 hashitem_T *hi;
5158 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005159 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005160
5161 if (dict_len(d1) != dict_len(d2))
5162 return FALSE;
5163
Bram Moolenaar33570922005-01-25 22:26:29 +00005164 todo = d1->dv_hashtab.ht_used;
5165 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005166 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005167 if (!HASHITEM_EMPTY(hi))
5168 {
5169 item2 = dict_find(d2, hi->hi_key, -1);
5170 if (item2 == NULL)
5171 return FALSE;
5172 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5173 return FALSE;
5174 --todo;
5175 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005176 }
5177 return TRUE;
5178}
5179
5180/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005181 * Return TRUE if "tv1" and "tv2" have the same value.
5182 * Compares the items just like "==" would compare them.
5183 */
5184 static int
5185tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005186 typval_T *tv1;
5187 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005188 int ic; /* ignore case */
5189{
5190 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005191 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005192
5193 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
5194 {
5195 /* recursive! */
5196 if (tv1->v_type != tv2->v_type
5197 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
5198 return FALSE;
5199 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005200 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
5201 {
5202 /* recursive! */
5203 if (tv1->v_type != tv2->v_type
5204 || !dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic))
5205 return FALSE;
5206 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005207 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
5208 {
5209 if (tv1->v_type != tv2->v_type
5210 || tv1->vval.v_string == NULL
5211 || tv2->vval.v_string == NULL
5212 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
5213 return FALSE;
5214 }
5215 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
5216 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005217 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
5218 * Don't consider "4x" to be equal to 4. */
5219 if ((tv1->v_type == VAR_STRING
5220 && !string_isa_number(tv1->vval.v_string))
5221 || (tv2->v_type == VAR_STRING
5222 && !string_isa_number(tv2->vval.v_string)))
5223 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005224 if (get_tv_number(tv1) != get_tv_number(tv2))
5225 return FALSE;
5226 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005227 else
5228 {
5229 s1 = get_tv_string_buf(tv1, buf1);
5230 s2 = get_tv_string_buf(tv2, buf2);
5231 if ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) != 0)
5232 return FALSE;
5233 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005234 return TRUE;
5235}
5236
5237/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005238 * Return TRUE if "tv" is a number without other non-white characters.
5239 */
5240 static int
5241string_isa_number(s)
5242 char_u *s;
5243{
5244 int len;
5245
5246 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
5247 return len > 0 && *skipwhite(s + len) == NUL;
5248}
5249
5250/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005251 * Locate item with index "n" in list "l" and return it.
5252 * A negative index is counted from the end; -1 is the last item.
5253 * Returns NULL when "n" is out of range.
5254 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005255 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005257 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 long n;
5259{
Bram Moolenaar33570922005-01-25 22:26:29 +00005260 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 long idx;
5262
5263 if (l == NULL)
5264 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005265
5266 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005267 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005268 n = l->lv_len + n;
5269
5270 /* Check for index out of range. */
5271 if (n < 0 || n >= l->lv_len)
5272 return NULL;
5273
5274 /* When there is a cached index may start search from there. */
5275 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005277 if (n < l->lv_idx / 2)
5278 {
5279 /* closest to the start of the list */
5280 item = l->lv_first;
5281 idx = 0;
5282 }
5283 else if (n > (l->lv_idx + l->lv_len) / 2)
5284 {
5285 /* closest to the end of the list */
5286 item = l->lv_last;
5287 idx = l->lv_len - 1;
5288 }
5289 else
5290 {
5291 /* closest to the cached index */
5292 item = l->lv_idx_item;
5293 idx = l->lv_idx;
5294 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005295 }
5296 else
5297 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005298 if (n < l->lv_len / 2)
5299 {
5300 /* closest to the start of the list */
5301 item = l->lv_first;
5302 idx = 0;
5303 }
5304 else
5305 {
5306 /* closest to the end of the list */
5307 item = l->lv_last;
5308 idx = l->lv_len - 1;
5309 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005310 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005311
5312 while (n > idx)
5313 {
5314 /* search forward */
5315 item = item->li_next;
5316 ++idx;
5317 }
5318 while (n < idx)
5319 {
5320 /* search backward */
5321 item = item->li_prev;
5322 --idx;
5323 }
5324
5325 /* cache the used index */
5326 l->lv_idx = idx;
5327 l->lv_idx_item = item;
5328
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005329 return item;
5330}
5331
5332/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005333 * Locate "item" list "l" and return its index.
5334 * Returns -1 when "item" is not in the list.
5335 */
5336 static long
5337list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005338 list_T *l;
5339 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005340{
5341 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005342 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005343
5344 if (l == NULL)
5345 return -1;
5346 idx = 0;
5347 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5348 ++idx;
5349 if (li == NULL)
5350 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005351 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005352}
5353
5354/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 * Append item "item" to the end of list "l".
5356 */
5357 static void
5358list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005359 list_T *l;
5360 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361{
5362 if (l->lv_last == NULL)
5363 {
5364 /* empty list */
5365 l->lv_first = item;
5366 l->lv_last = item;
5367 item->li_prev = NULL;
5368 }
5369 else
5370 {
5371 l->lv_last->li_next = item;
5372 item->li_prev = l->lv_last;
5373 l->lv_last = item;
5374 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005375 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376 item->li_next = NULL;
5377}
5378
5379/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005380 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005381 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 */
5383 static int
5384list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005385 list_T *l;
5386 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005387{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005388 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389
Bram Moolenaar05159a02005-02-26 23:04:13 +00005390 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005391 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005392 copy_tv(tv, &li->li_tv);
5393 list_append(l, li);
5394 return OK;
5395}
5396
5397/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005398 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005399 * Return FAIL when out of memory.
5400 */
5401 int
5402list_append_dict(list, dict)
5403 list_T *list;
5404 dict_T *dict;
5405{
5406 listitem_T *li = listitem_alloc();
5407
5408 if (li == NULL)
5409 return FAIL;
5410 li->li_tv.v_type = VAR_DICT;
5411 li->li_tv.v_lock = 0;
5412 li->li_tv.vval.v_dict = dict;
5413 list_append(list, li);
5414 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415 return OK;
5416}
5417
5418/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005419 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005420 * If "item" is NULL append at the end.
5421 * Return FAIL when out of memory.
5422 */
5423 static int
5424list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005425 list_T *l;
5426 typval_T *tv;
5427 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005428{
Bram Moolenaar33570922005-01-25 22:26:29 +00005429 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005430
5431 if (ni == NULL)
5432 return FAIL;
5433 copy_tv(tv, &ni->li_tv);
5434 if (item == NULL)
5435 /* Append new item at end of list. */
5436 list_append(l, ni);
5437 else
5438 {
5439 /* Insert new item before existing item. */
5440 ni->li_prev = item->li_prev;
5441 ni->li_next = item;
5442 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005443 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005444 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005445 ++l->lv_idx;
5446 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005447 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005448 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005449 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005450 l->lv_idx_item = NULL;
5451 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005452 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005453 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005454 }
5455 return OK;
5456}
5457
5458/*
5459 * Extend "l1" with "l2".
5460 * If "bef" is NULL append at the end, otherwise insert before this item.
5461 * Returns FAIL when out of memory.
5462 */
5463 static int
5464list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005465 list_T *l1;
5466 list_T *l2;
5467 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005468{
Bram Moolenaar33570922005-01-25 22:26:29 +00005469 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005470
5471 for (item = l2->lv_first; item != NULL; item = item->li_next)
5472 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5473 return FAIL;
5474 return OK;
5475}
5476
5477/*
5478 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5479 * Return FAIL when out of memory.
5480 */
5481 static int
5482list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005483 list_T *l1;
5484 list_T *l2;
5485 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005486{
Bram Moolenaar33570922005-01-25 22:26:29 +00005487 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005488
5489 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005490 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005491 if (l == NULL)
5492 return FAIL;
5493 tv->v_type = VAR_LIST;
5494 tv->vval.v_list = l;
5495
5496 /* append all items from the second list */
5497 return list_extend(l, l2, NULL);
5498}
5499
5500/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005501 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005502 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005503 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005504 * Returns NULL when out of memory.
5505 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005506 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005507list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005508 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005510 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005511{
Bram Moolenaar33570922005-01-25 22:26:29 +00005512 list_T *copy;
5513 listitem_T *item;
5514 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005515
5516 if (orig == NULL)
5517 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005518
5519 copy = list_alloc();
5520 if (copy != NULL)
5521 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005522 if (copyID != 0)
5523 {
5524 /* Do this before adding the items, because one of the items may
5525 * refer back to this list. */
5526 orig->lv_copyID = copyID;
5527 orig->lv_copylist = copy;
5528 }
5529 for (item = orig->lv_first; item != NULL && !got_int;
5530 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005531 {
5532 ni = listitem_alloc();
5533 if (ni == NULL)
5534 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005535 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005536 {
5537 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5538 {
5539 vim_free(ni);
5540 break;
5541 }
5542 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005543 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005544 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545 list_append(copy, ni);
5546 }
5547 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005548 if (item != NULL)
5549 {
5550 list_unref(copy);
5551 copy = NULL;
5552 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553 }
5554
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 return copy;
5556}
5557
5558/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005559 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005560 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005561 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005562 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005563list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005564 list_T *l;
5565 listitem_T *item;
5566 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005567{
Bram Moolenaar33570922005-01-25 22:26:29 +00005568 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005569
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005570 /* notify watchers */
5571 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005572 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005573 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005574 list_fix_watch(l, ip);
5575 if (ip == item2)
5576 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005577 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005578
5579 if (item2->li_next == NULL)
5580 l->lv_last = item->li_prev;
5581 else
5582 item2->li_next->li_prev = item->li_prev;
5583 if (item->li_prev == NULL)
5584 l->lv_first = item2->li_next;
5585 else
5586 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005587 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005588}
5589
5590/*
5591 * Return an allocated string with the string representation of a list.
5592 * May return NULL.
5593 */
5594 static char_u *
5595list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005596 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005597{
5598 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005599
5600 if (tv->vval.v_list == NULL)
5601 return NULL;
5602 ga_init2(&ga, (int)sizeof(char), 80);
5603 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005604 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5605 {
5606 vim_free(ga.ga_data);
5607 return NULL;
5608 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005609 ga_append(&ga, ']');
5610 ga_append(&ga, NUL);
5611 return (char_u *)ga.ga_data;
5612}
5613
5614/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005615 * Join list "l" into a string in "*gap", using separator "sep".
5616 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005617 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005618 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005619 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005620list_join(gap, l, sep, echo)
5621 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005622 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005623 char_u *sep;
5624 int echo;
5625{
5626 int first = TRUE;
5627 char_u *tofree;
5628 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005629 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005630 char_u *s;
5631
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005632 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005633 {
5634 if (first)
5635 first = FALSE;
5636 else
5637 ga_concat(gap, sep);
5638
5639 if (echo)
5640 s = echo_string(&item->li_tv, &tofree, numbuf);
5641 else
5642 s = tv2string(&item->li_tv, &tofree, numbuf);
5643 if (s != NULL)
5644 ga_concat(gap, s);
5645 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005646 if (s == NULL)
5647 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005648 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005649 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005650}
5651
5652/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005653 * Garbage collection for lists and dictionaries.
5654 *
5655 * We use reference counts to be able to free most items right away when they
5656 * are no longer used. But for composite items it's possible that it becomes
5657 * unused while the reference count is > 0: When there is a recursive
5658 * reference. Example:
5659 * :let l = [1, 2, 3]
5660 * :let d = {9: l}
5661 * :let l[1] = d
5662 *
5663 * Since this is quite unusual we handle this with garbage collection: every
5664 * once in a while find out which lists and dicts are not referenced from any
5665 * variable.
5666 *
5667 * Here is a good reference text about garbage collection (refers to Python
5668 * but it applies to all reference-counting mechanisms):
5669 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005670 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005671
5672/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005673 * Do garbage collection for lists and dicts.
5674 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005675 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005676 int
5677garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005678{
5679 dict_T *dd;
5680 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005681 int copyID = ++current_copyID;
5682 buf_T *buf;
5683 win_T *wp;
5684 int i;
5685 funccall_T *fc;
5686 int did_free = FALSE;
5687
5688 /*
5689 * 1. Go through all accessible variables and mark all lists and dicts
5690 * with copyID.
5691 */
5692 /* script-local variables */
5693 for (i = 1; i <= ga_scripts.ga_len; ++i)
5694 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5695
5696 /* buffer-local variables */
5697 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5698 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5699
5700 /* window-local variables */
5701 FOR_ALL_WINDOWS(wp)
5702 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5703
5704 /* global variables */
5705 set_ref_in_ht(&globvarht, copyID);
5706
5707 /* function-local variables */
5708 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5709 {
5710 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5711 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5712 }
5713
5714 /*
5715 * 2. Go through the list of dicts and free items without the copyID.
5716 */
5717 for (dd = first_dict; dd != NULL; )
5718 if (dd->dv_copyID != copyID)
5719 {
5720 dict_free(dd);
5721 did_free = TRUE;
5722
5723 /* restart, next dict may also have been freed */
5724 dd = first_dict;
5725 }
5726 else
5727 dd = dd->dv_used_next;
5728
5729 /*
5730 * 3. Go through the list of lists and free items without the copyID.
5731 */
5732 for (ll = first_list; ll != NULL; )
5733 if (ll->lv_copyID != copyID)
5734 {
5735 list_free(ll);
5736 did_free = TRUE;
5737
5738 /* restart, next dict may also have been freed */
5739 ll = first_list;
5740 }
5741 else
5742 ll = ll->lv_used_next;
5743
5744 return did_free;
5745}
5746
5747/*
5748 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5749 */
5750 static void
5751set_ref_in_ht(ht, copyID)
5752 hashtab_T *ht;
5753 int copyID;
5754{
5755 int todo;
5756 hashitem_T *hi;
5757
5758 todo = ht->ht_used;
5759 for (hi = ht->ht_array; todo > 0; ++hi)
5760 if (!HASHITEM_EMPTY(hi))
5761 {
5762 --todo;
5763 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5764 }
5765}
5766
5767/*
5768 * Mark all lists and dicts referenced through list "l" with "copyID".
5769 */
5770 static void
5771set_ref_in_list(l, copyID)
5772 list_T *l;
5773 int copyID;
5774{
5775 listitem_T *li;
5776
5777 for (li = l->lv_first; li != NULL; li = li->li_next)
5778 set_ref_in_item(&li->li_tv, copyID);
5779}
5780
5781/*
5782 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5783 */
5784 static void
5785set_ref_in_item(tv, copyID)
5786 typval_T *tv;
5787 int copyID;
5788{
5789 dict_T *dd;
5790 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005791
5792 switch (tv->v_type)
5793 {
5794 case VAR_DICT:
5795 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005796 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005797 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005798 /* Didn't see this dict yet. */
5799 dd->dv_copyID = copyID;
5800 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005801 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005802 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005803
5804 case VAR_LIST:
5805 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005806 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005807 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005808 /* Didn't see this list yet. */
5809 ll->lv_copyID = copyID;
5810 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005811 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005812 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005813 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005814 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005815}
5816
5817/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 * Allocate an empty header for a dictionary.
5819 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005820 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005821dict_alloc()
5822{
Bram Moolenaar33570922005-01-25 22:26:29 +00005823 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005824
Bram Moolenaar33570922005-01-25 22:26:29 +00005825 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005826 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005827 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005828 /* Add the list to the hashtable for garbage collection. */
5829 if (first_dict != NULL)
5830 first_dict->dv_used_prev = d;
5831 d->dv_used_next = first_dict;
5832 d->dv_used_prev = NULL;
5833
Bram Moolenaar33570922005-01-25 22:26:29 +00005834 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005835 d->dv_lock = 0;
5836 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005837 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005838 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005839 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840}
5841
5842/*
5843 * Unreference a Dictionary: decrement the reference count and free it when it
5844 * becomes zero.
5845 */
5846 static void
5847dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005848 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005850 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
5851 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852}
5853
5854/*
5855 * Free a Dictionary, including all items it contains.
5856 * Ignores the reference count.
5857 */
5858 static void
5859dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005860 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005862 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005863 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005864 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005865
Bram Moolenaard9fba312005-06-26 22:34:35 +00005866 /* Avoid that recursive reference to the dict frees us again. */
5867 d->dv_refcount = DEL_REFCOUNT;
5868
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005869 /* Remove the dict from the list of dicts for garbage collection. */
5870 if (d->dv_used_prev == NULL)
5871 first_dict = d->dv_used_next;
5872 else
5873 d->dv_used_prev->dv_used_next = d->dv_used_next;
5874 if (d->dv_used_next != NULL)
5875 d->dv_used_next->dv_used_prev = d->dv_used_prev;
5876
5877 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005878 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00005879 todo = d->dv_hashtab.ht_used;
5880 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005882 if (!HASHITEM_EMPTY(hi))
5883 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005884 /* Remove the item before deleting it, just in case there is
5885 * something recursive causing trouble. */
5886 di = HI2DI(hi);
5887 hash_remove(&d->dv_hashtab, hi);
5888 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005889 --todo;
5890 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005891 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005892 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005893 vim_free(d);
5894}
5895
5896/*
5897 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005898 * The "key" is copied to the new item.
5899 * Note that the value of the item "di_tv" still needs to be initialized!
5900 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005901 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005902 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005903dictitem_alloc(key)
5904 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005905{
Bram Moolenaar33570922005-01-25 22:26:29 +00005906 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005907
Bram Moolenaar33570922005-01-25 22:26:29 +00005908 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005909 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005910 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005911 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005912 di->di_flags = 0;
5913 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005914 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005915}
5916
5917/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005918 * Make a copy of a Dictionary item.
5919 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005920 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00005921dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00005922 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005923{
Bram Moolenaar33570922005-01-25 22:26:29 +00005924 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005925
Bram Moolenaar33570922005-01-25 22:26:29 +00005926 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005927 if (di != NULL)
5928 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005929 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005930 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005931 copy_tv(&org->di_tv, &di->di_tv);
5932 }
5933 return di;
5934}
5935
5936/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005937 * Remove item "item" from Dictionary "dict" and free it.
5938 */
5939 static void
5940dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005941 dict_T *dict;
5942 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005943{
Bram Moolenaar33570922005-01-25 22:26:29 +00005944 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005945
Bram Moolenaar33570922005-01-25 22:26:29 +00005946 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005947 if (HASHITEM_EMPTY(hi))
5948 EMSG2(_(e_intern2), "dictitem_remove()");
5949 else
Bram Moolenaar33570922005-01-25 22:26:29 +00005950 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005951 dictitem_free(item);
5952}
5953
5954/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005955 * Free a dict item. Also clears the value.
5956 */
5957 static void
5958dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005959 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005960{
Bram Moolenaar8c711452005-01-14 21:53:12 +00005961 clear_tv(&item->di_tv);
5962 vim_free(item);
5963}
5964
5965/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005966 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5967 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005968 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00005969 * Returns NULL when out of memory.
5970 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005971 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005972dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005973 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005974 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005975 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005976{
Bram Moolenaar33570922005-01-25 22:26:29 +00005977 dict_T *copy;
5978 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005979 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005980 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005981
5982 if (orig == NULL)
5983 return NULL;
5984
5985 copy = dict_alloc();
5986 if (copy != NULL)
5987 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005988 if (copyID != 0)
5989 {
5990 orig->dv_copyID = copyID;
5991 orig->dv_copydict = copy;
5992 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005993 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005994 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005995 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005996 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00005997 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005998 --todo;
5999
6000 di = dictitem_alloc(hi->hi_key);
6001 if (di == NULL)
6002 break;
6003 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006004 {
6005 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6006 copyID) == FAIL)
6007 {
6008 vim_free(di);
6009 break;
6010 }
6011 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006012 else
6013 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6014 if (dict_add(copy, di) == FAIL)
6015 {
6016 dictitem_free(di);
6017 break;
6018 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006019 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006020 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006021
Bram Moolenaare9a41262005-01-15 22:18:47 +00006022 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006023 if (todo > 0)
6024 {
6025 dict_unref(copy);
6026 copy = NULL;
6027 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006028 }
6029
6030 return copy;
6031}
6032
6033/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006034 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006035 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006036 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006037 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006038dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006039 dict_T *d;
6040 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006041{
Bram Moolenaar33570922005-01-25 22:26:29 +00006042 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006043}
6044
Bram Moolenaar8c711452005-01-14 21:53:12 +00006045/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006046 * Add a number or string entry to dictionary "d".
6047 * When "str" is NULL use number "nr", otherwise use "str".
6048 * Returns FAIL when out of memory and when key already exists.
6049 */
6050 int
6051dict_add_nr_str(d, key, nr, str)
6052 dict_T *d;
6053 char *key;
6054 long nr;
6055 char_u *str;
6056{
6057 dictitem_T *item;
6058
6059 item = dictitem_alloc((char_u *)key);
6060 if (item == NULL)
6061 return FAIL;
6062 item->di_tv.v_lock = 0;
6063 if (str == NULL)
6064 {
6065 item->di_tv.v_type = VAR_NUMBER;
6066 item->di_tv.vval.v_number = nr;
6067 }
6068 else
6069 {
6070 item->di_tv.v_type = VAR_STRING;
6071 item->di_tv.vval.v_string = vim_strsave(str);
6072 }
6073 if (dict_add(d, item) == FAIL)
6074 {
6075 dictitem_free(item);
6076 return FAIL;
6077 }
6078 return OK;
6079}
6080
6081/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006082 * Get the number of items in a Dictionary.
6083 */
6084 static long
6085dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006086 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006087{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006088 if (d == NULL)
6089 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006090 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006091}
6092
6093/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006094 * Find item "key[len]" in Dictionary "d".
6095 * If "len" is negative use strlen(key).
6096 * Returns NULL when not found.
6097 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006098 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006099dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006100 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006101 char_u *key;
6102 int len;
6103{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006104#define AKEYLEN 200
6105 char_u buf[AKEYLEN];
6106 char_u *akey;
6107 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006108 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006109
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006110 if (len < 0)
6111 akey = key;
6112 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006113 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006114 tofree = akey = vim_strnsave(key, len);
6115 if (akey == NULL)
6116 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006117 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006118 else
6119 {
6120 /* Avoid a malloc/free by using buf[]. */
6121 STRNCPY(buf, key, len);
6122 buf[len] = NUL;
6123 akey = buf;
6124 }
6125
Bram Moolenaar33570922005-01-25 22:26:29 +00006126 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006127 vim_free(tofree);
6128 if (HASHITEM_EMPTY(hi))
6129 return NULL;
6130 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006131}
6132
6133/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006134 * Get a string item from a dictionary in allocated memory.
6135 * Returns NULL if the entry doesn't exist or out of memory.
6136 */
6137 char_u *
6138get_dict_string(d, key)
6139 dict_T *d;
6140 char_u *key;
6141{
6142 dictitem_T *di;
6143
6144 di = dict_find(d, key, -1);
6145 if (di == NULL)
6146 return NULL;
6147 return vim_strsave(get_tv_string(&di->di_tv));
6148}
6149
6150/*
6151 * Get a number item from a dictionary.
6152 * Returns 0 if the entry doesn't exist or out of memory.
6153 */
6154 long
6155get_dict_number(d, key)
6156 dict_T *d;
6157 char_u *key;
6158{
6159 dictitem_T *di;
6160
6161 di = dict_find(d, key, -1);
6162 if (di == NULL)
6163 return 0;
6164 return get_tv_number(&di->di_tv);
6165}
6166
6167/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006168 * Return an allocated string with the string representation of a Dictionary.
6169 * May return NULL.
6170 */
6171 static char_u *
6172dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006173 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006174{
6175 garray_T ga;
6176 int first = TRUE;
6177 char_u *tofree;
6178 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006179 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006180 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006181 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006182 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006183
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006184 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006185 return NULL;
6186 ga_init2(&ga, (int)sizeof(char), 80);
6187 ga_append(&ga, '{');
6188
Bram Moolenaar33570922005-01-25 22:26:29 +00006189 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006190 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006191 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006192 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006193 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006194 --todo;
6195
6196 if (first)
6197 first = FALSE;
6198 else
6199 ga_concat(&ga, (char_u *)", ");
6200
6201 tofree = string_quote(hi->hi_key, FALSE);
6202 if (tofree != NULL)
6203 {
6204 ga_concat(&ga, tofree);
6205 vim_free(tofree);
6206 }
6207 ga_concat(&ga, (char_u *)": ");
6208 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6209 if (s != NULL)
6210 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006211 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006212 if (s == NULL)
6213 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006214 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006215 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006216 if (todo > 0)
6217 {
6218 vim_free(ga.ga_data);
6219 return NULL;
6220 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006221
6222 ga_append(&ga, '}');
6223 ga_append(&ga, NUL);
6224 return (char_u *)ga.ga_data;
6225}
6226
6227/*
6228 * Allocate a variable for a Dictionary and fill it from "*arg".
6229 * Return OK or FAIL. Returns NOTDONE for {expr}.
6230 */
6231 static int
6232get_dict_tv(arg, rettv, evaluate)
6233 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006234 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006235 int evaluate;
6236{
Bram Moolenaar33570922005-01-25 22:26:29 +00006237 dict_T *d = NULL;
6238 typval_T tvkey;
6239 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006240 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006241 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006242 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006243 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006244
6245 /*
6246 * First check if it's not a curly-braces thing: {expr}.
6247 * Must do this without evaluating, otherwise a function may be called
6248 * twice. Unfortunately this means we need to call eval1() twice for the
6249 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006250 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006251 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006252 if (*start != '}')
6253 {
6254 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6255 return FAIL;
6256 if (*start == '}')
6257 return NOTDONE;
6258 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006259
6260 if (evaluate)
6261 {
6262 d = dict_alloc();
6263 if (d == NULL)
6264 return FAIL;
6265 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006266 tvkey.v_type = VAR_UNKNOWN;
6267 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006268
6269 *arg = skipwhite(*arg + 1);
6270 while (**arg != '}' && **arg != NUL)
6271 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006272 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006273 goto failret;
6274 if (**arg != ':')
6275 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006276 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006277 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006278 goto failret;
6279 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006280 key = get_tv_string_buf_chk(&tvkey, buf);
6281 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006283 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6284 if (key != NULL)
6285 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006286 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006287 goto failret;
6288 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006289
6290 *arg = skipwhite(*arg + 1);
6291 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6292 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006293 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006294 goto failret;
6295 }
6296 if (evaluate)
6297 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006298 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006299 if (item != NULL)
6300 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006301 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006302 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006303 clear_tv(&tv);
6304 goto failret;
6305 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006306 item = dictitem_alloc(key);
6307 clear_tv(&tvkey);
6308 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006309 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006310 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006311 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006312 if (dict_add(d, item) == FAIL)
6313 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006314 }
6315 }
6316
6317 if (**arg == '}')
6318 break;
6319 if (**arg != ',')
6320 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006321 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006322 goto failret;
6323 }
6324 *arg = skipwhite(*arg + 1);
6325 }
6326
6327 if (**arg != '}')
6328 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006329 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006330failret:
6331 if (evaluate)
6332 dict_free(d);
6333 return FAIL;
6334 }
6335
6336 *arg = skipwhite(*arg + 1);
6337 if (evaluate)
6338 {
6339 rettv->v_type = VAR_DICT;
6340 rettv->vval.v_dict = d;
6341 ++d->dv_refcount;
6342 }
6343
6344 return OK;
6345}
6346
Bram Moolenaar8c711452005-01-14 21:53:12 +00006347/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006348 * Return a string with the string representation of a variable.
6349 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006350 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006351 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006352 * May return NULL;
6353 */
6354 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006355echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006356 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006357 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006358 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006359{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006360 static int recurse = 0;
6361 char_u *r = NULL;
6362
Bram Moolenaar33570922005-01-25 22:26:29 +00006363 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006364 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006365 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006366 *tofree = NULL;
6367 return NULL;
6368 }
6369 ++recurse;
6370
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006371 switch (tv->v_type)
6372 {
6373 case VAR_FUNC:
6374 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006375 r = tv->vval.v_string;
6376 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006377 case VAR_LIST:
6378 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006379 r = *tofree;
6380 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006381 case VAR_DICT:
6382 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006383 r = *tofree;
6384 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006385 case VAR_STRING:
6386 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006387 *tofree = NULL;
6388 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006389 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006390 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006391 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006392 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006393 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006394
6395 --recurse;
6396 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006397}
6398
6399/*
6400 * Return a string with the string representation of a variable.
6401 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6402 * "numbuf" is used for a number.
6403 * Puts quotes around strings, so that they can be parsed back by eval().
6404 * May return NULL;
6405 */
6406 static char_u *
6407tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006408 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006409 char_u **tofree;
6410 char_u *numbuf;
6411{
6412 switch (tv->v_type)
6413 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006414 case VAR_FUNC:
6415 *tofree = string_quote(tv->vval.v_string, TRUE);
6416 return *tofree;
6417 case VAR_STRING:
6418 *tofree = string_quote(tv->vval.v_string, FALSE);
6419 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006420 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006421 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006422 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006423 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006424 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006425 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006427 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006428}
6429
6430/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006431 * Return string "str" in ' quotes, doubling ' characters.
6432 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006433 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006434 */
6435 static char_u *
6436string_quote(str, function)
6437 char_u *str;
6438 int function;
6439{
Bram Moolenaar33570922005-01-25 22:26:29 +00006440 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006441 char_u *p, *r, *s;
6442
Bram Moolenaar33570922005-01-25 22:26:29 +00006443 len = (function ? 13 : 3);
6444 if (str != NULL)
6445 {
6446 len += STRLEN(str);
6447 for (p = str; *p != NUL; mb_ptr_adv(p))
6448 if (*p == '\'')
6449 ++len;
6450 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006451 s = r = alloc(len);
6452 if (r != NULL)
6453 {
6454 if (function)
6455 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006456 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006457 r += 10;
6458 }
6459 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006460 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006461 if (str != NULL)
6462 for (p = str; *p != NUL; )
6463 {
6464 if (*p == '\'')
6465 *r++ = '\'';
6466 MB_COPY_CHAR(p, r);
6467 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006468 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006469 if (function)
6470 *r++ = ')';
6471 *r++ = NUL;
6472 }
6473 return s;
6474}
6475
6476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 * Get the value of an environment variable.
6478 * "arg" is pointing to the '$'. It is advanced to after the name.
6479 * If the environment variable was not set, silently assume it is empty.
6480 * Always return OK.
6481 */
6482 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006483get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 int evaluate;
6487{
6488 char_u *string = NULL;
6489 int len;
6490 int cc;
6491 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006492 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493
6494 ++*arg;
6495 name = *arg;
6496 len = get_env_len(arg);
6497 if (evaluate)
6498 {
6499 if (len != 0)
6500 {
6501 cc = name[len];
6502 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006503 /* first try vim_getenv(), fast for normal environment vars */
6504 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006506 {
6507 if (!mustfree)
6508 string = vim_strsave(string);
6509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 else
6511 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006512 if (mustfree)
6513 vim_free(string);
6514
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 /* next try expanding things like $VIM and ${HOME} */
6516 string = expand_env_save(name - 1);
6517 if (string != NULL && *string == '$')
6518 {
6519 vim_free(string);
6520 string = NULL;
6521 }
6522 }
6523 name[len] = cc;
6524 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006525 rettv->v_type = VAR_STRING;
6526 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 }
6528
6529 return OK;
6530}
6531
6532/*
6533 * Array with names and number of arguments of all internal functions
6534 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6535 */
6536static struct fst
6537{
6538 char *f_name; /* function name */
6539 char f_min_argc; /* minimal number of arguments */
6540 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006541 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006542 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543} functions[] =
6544{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006545 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 {"append", 2, 2, f_append},
6547 {"argc", 0, 0, f_argc},
6548 {"argidx", 0, 0, f_argidx},
6549 {"argv", 1, 1, f_argv},
6550 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006551 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 {"bufexists", 1, 1, f_bufexists},
6553 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6554 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6555 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6556 {"buflisted", 1, 1, f_buflisted},
6557 {"bufloaded", 1, 1, f_bufloaded},
6558 {"bufname", 1, 1, f_bufname},
6559 {"bufnr", 1, 1, f_bufnr},
6560 {"bufwinnr", 1, 1, f_bufwinnr},
6561 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006562 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006563 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 {"char2nr", 1, 1, f_char2nr},
6565 {"cindent", 1, 1, f_cindent},
6566 {"col", 1, 1, f_col},
6567 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006568 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006569 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 {"cscope_connection",0,3, f_cscope_connection},
6571 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006572 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 {"delete", 1, 1, f_delete},
6574 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006575 {"diff_filler", 1, 1, f_diff_filler},
6576 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006577 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006579 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 {"eventhandler", 0, 0, f_eventhandler},
6581 {"executable", 1, 1, f_executable},
6582 {"exists", 1, 1, f_exists},
6583 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006584 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6586 {"filereadable", 1, 1, f_filereadable},
6587 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006588 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006589 {"finddir", 1, 3, f_finddir},
6590 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 {"fnamemodify", 2, 2, f_fnamemodify},
6592 {"foldclosed", 1, 1, f_foldclosed},
6593 {"foldclosedend", 1, 1, f_foldclosedend},
6594 {"foldlevel", 1, 1, f_foldlevel},
6595 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006596 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006599 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006600 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 {"getbufvar", 2, 2, f_getbufvar},
6602 {"getchar", 0, 1, f_getchar},
6603 {"getcharmod", 0, 0, f_getcharmod},
6604 {"getcmdline", 0, 0, f_getcmdline},
6605 {"getcmdpos", 0, 0, f_getcmdpos},
6606 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006607 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006608 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 {"getfsize", 1, 1, f_getfsize},
6610 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006611 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006612 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006613 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006614 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 {"getregtype", 0, 1, f_getregtype},
6616 {"getwinposx", 0, 0, f_getwinposx},
6617 {"getwinposy", 0, 0, f_getwinposy},
6618 {"getwinvar", 2, 2, f_getwinvar},
6619 {"glob", 1, 1, f_glob},
6620 {"globpath", 2, 2, f_globpath},
6621 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006622 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 {"hasmapto", 1, 2, f_hasmapto},
6624 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6625 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6626 {"histadd", 2, 2, f_histadd},
6627 {"histdel", 1, 2, f_histdel},
6628 {"histget", 1, 2, f_histget},
6629 {"histnr", 1, 1, f_histnr},
6630 {"hlID", 1, 1, f_hlID},
6631 {"hlexists", 1, 1, f_hlexists},
6632 {"hostname", 0, 0, f_hostname},
6633 {"iconv", 3, 3, f_iconv},
6634 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006635 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 {"input", 1, 2, f_input},
6637 {"inputdialog", 1, 3, f_inputdialog},
6638 {"inputrestore", 0, 0, f_inputrestore},
6639 {"inputsave", 0, 0, f_inputsave},
6640 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006641 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006643 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006644 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006645 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006646 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006648 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 {"libcall", 3, 3, f_libcall},
6650 {"libcallnr", 3, 3, f_libcallnr},
6651 {"line", 1, 1, f_line},
6652 {"line2byte", 1, 1, f_line2byte},
6653 {"lispindent", 1, 1, f_lispindent},
6654 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006655 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 {"maparg", 1, 2, f_maparg},
6657 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006658 {"match", 2, 4, f_match},
6659 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006660 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006661 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006662 {"max", 1, 1, f_max},
6663 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006664#ifdef vim_mkdir
6665 {"mkdir", 1, 3, f_mkdir},
6666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 {"mode", 0, 0, f_mode},
6668 {"nextnonblank", 1, 1, f_nextnonblank},
6669 {"nr2char", 1, 1, f_nr2char},
6670 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006671 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006672 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 {"remote_expr", 2, 3, f_remote_expr},
6674 {"remote_foreground", 1, 1, f_remote_foreground},
6675 {"remote_peek", 1, 2, f_remote_peek},
6676 {"remote_read", 1, 1, f_remote_read},
6677 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006678 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006680 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006682 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 {"search", 1, 2, f_search},
6684 {"searchpair", 3, 5, f_searchpair},
6685 {"server2client", 2, 2, f_server2client},
6686 {"serverlist", 0, 0, f_serverlist},
6687 {"setbufvar", 3, 3, f_setbufvar},
6688 {"setcmdpos", 1, 1, f_setcmdpos},
6689 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006690 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 {"setreg", 2, 3, f_setreg},
6692 {"setwinvar", 3, 3, f_setwinvar},
6693 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006694 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006695 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006696 {"spellbadword", 0, 0, f_spellbadword},
6697 {"spellsuggest", 1, 2, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006698 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699#ifdef HAVE_STRFTIME
6700 {"strftime", 1, 2, f_strftime},
6701#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006702 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006703 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 {"strlen", 1, 1, f_strlen},
6705 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006706 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 {"strtrans", 1, 1, f_strtrans},
6708 {"submatch", 1, 1, f_submatch},
6709 {"substitute", 4, 4, f_substitute},
6710 {"synID", 3, 3, f_synID},
6711 {"synIDattr", 2, 3, f_synIDattr},
6712 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006713 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006714 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 {"tempname", 0, 0, f_tempname},
6716 {"tolower", 1, 1, f_tolower},
6717 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006718 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006720 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 {"virtcol", 1, 1, f_virtcol},
6722 {"visualmode", 0, 1, f_visualmode},
6723 {"winbufnr", 1, 1, f_winbufnr},
6724 {"wincol", 0, 0, f_wincol},
6725 {"winheight", 1, 1, f_winheight},
6726 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006727 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 {"winrestcmd", 0, 0, f_winrestcmd},
6729 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006730 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731};
6732
6733#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6734
6735/*
6736 * Function given to ExpandGeneric() to obtain the list of internal
6737 * or user defined function names.
6738 */
6739 char_u *
6740get_function_name(xp, idx)
6741 expand_T *xp;
6742 int idx;
6743{
6744 static int intidx = -1;
6745 char_u *name;
6746
6747 if (idx == 0)
6748 intidx = -1;
6749 if (intidx < 0)
6750 {
6751 name = get_user_func_name(xp, idx);
6752 if (name != NULL)
6753 return name;
6754 }
6755 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6756 {
6757 STRCPY(IObuff, functions[intidx].f_name);
6758 STRCAT(IObuff, "(");
6759 if (functions[intidx].f_max_argc == 0)
6760 STRCAT(IObuff, ")");
6761 return IObuff;
6762 }
6763
6764 return NULL;
6765}
6766
6767/*
6768 * Function given to ExpandGeneric() to obtain the list of internal or
6769 * user defined variable or function names.
6770 */
6771/*ARGSUSED*/
6772 char_u *
6773get_expr_name(xp, idx)
6774 expand_T *xp;
6775 int idx;
6776{
6777 static int intidx = -1;
6778 char_u *name;
6779
6780 if (idx == 0)
6781 intidx = -1;
6782 if (intidx < 0)
6783 {
6784 name = get_function_name(xp, idx);
6785 if (name != NULL)
6786 return name;
6787 }
6788 return get_user_var_name(xp, ++intidx);
6789}
6790
6791#endif /* FEAT_CMDL_COMPL */
6792
6793/*
6794 * Find internal function in table above.
6795 * Return index, or -1 if not found
6796 */
6797 static int
6798find_internal_func(name)
6799 char_u *name; /* name of the function */
6800{
6801 int first = 0;
6802 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6803 int cmp;
6804 int x;
6805
6806 /*
6807 * Find the function name in the table. Binary search.
6808 */
6809 while (first <= last)
6810 {
6811 x = first + ((unsigned)(last - first) >> 1);
6812 cmp = STRCMP(name, functions[x].f_name);
6813 if (cmp < 0)
6814 last = x - 1;
6815 else if (cmp > 0)
6816 first = x + 1;
6817 else
6818 return x;
6819 }
6820 return -1;
6821}
6822
6823/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006824 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6825 * name it contains, otherwise return "name".
6826 */
6827 static char_u *
6828deref_func_name(name, lenp)
6829 char_u *name;
6830 int *lenp;
6831{
Bram Moolenaar33570922005-01-25 22:26:29 +00006832 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006833 int cc;
6834
6835 cc = name[*lenp];
6836 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006837 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006838 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006839 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006840 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006841 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006842 {
6843 *lenp = 0;
6844 return (char_u *)""; /* just in case */
6845 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006846 *lenp = STRLEN(v->di_tv.vval.v_string);
6847 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006848 }
6849
6850 return name;
6851}
6852
6853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 * Allocate a variable for the result of a function.
6855 * Return OK or FAIL.
6856 */
6857 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006858get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6859 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 char_u *name; /* name of the function */
6861 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 char_u **arg; /* argument, pointing to the '(' */
6864 linenr_T firstline; /* first line of range */
6865 linenr_T lastline; /* last line of range */
6866 int *doesrange; /* return: function handled range */
6867 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006868 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869{
6870 char_u *argp;
6871 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006872 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 int argcount = 0; /* number of arguments found */
6874
6875 /*
6876 * Get the arguments.
6877 */
6878 argp = *arg;
6879 while (argcount < MAX_FUNC_ARGS)
6880 {
6881 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6882 if (*argp == ')' || *argp == ',' || *argp == NUL)
6883 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6885 {
6886 ret = FAIL;
6887 break;
6888 }
6889 ++argcount;
6890 if (*argp != ',')
6891 break;
6892 }
6893 if (*argp == ')')
6894 ++argp;
6895 else
6896 ret = FAIL;
6897
6898 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006899 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006900 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00006902 {
6903 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006904 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006905 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006906 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908
6909 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006910 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911
6912 *arg = skipwhite(argp);
6913 return ret;
6914}
6915
6916
6917/*
6918 * Call a function with its resolved parameters
6919 * Return OK or FAIL.
6920 */
6921 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006922call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006923 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 char_u *name; /* name of the function */
6925 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006926 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006928 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 linenr_T firstline; /* first line of range */
6930 linenr_T lastline; /* last line of range */
6931 int *doesrange; /* return: function handled range */
6932 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006933 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934{
6935 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936#define ERROR_UNKNOWN 0
6937#define ERROR_TOOMANY 1
6938#define ERROR_TOOFEW 2
6939#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00006940#define ERROR_DICT 4
6941#define ERROR_NONE 5
6942#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 int error = ERROR_NONE;
6944 int i;
6945 int llen;
6946 ufunc_T *fp;
6947 int cc;
6948#define FLEN_FIXED 40
6949 char_u fname_buf[FLEN_FIXED + 1];
6950 char_u *fname;
6951
6952 /*
6953 * In a script change <SID>name() and s:name() to K_SNR 123_name().
6954 * Change <SNR>123_name() to K_SNR 123_name().
6955 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
6956 */
6957 cc = name[len];
6958 name[len] = NUL;
6959 llen = eval_fname_script(name);
6960 if (llen > 0)
6961 {
6962 fname_buf[0] = K_SPECIAL;
6963 fname_buf[1] = KS_EXTRA;
6964 fname_buf[2] = (int)KE_SNR;
6965 i = 3;
6966 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
6967 {
6968 if (current_SID <= 0)
6969 error = ERROR_SCRIPT;
6970 else
6971 {
6972 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
6973 i = (int)STRLEN(fname_buf);
6974 }
6975 }
6976 if (i + STRLEN(name + llen) < FLEN_FIXED)
6977 {
6978 STRCPY(fname_buf + i, name + llen);
6979 fname = fname_buf;
6980 }
6981 else
6982 {
6983 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
6984 if (fname == NULL)
6985 error = ERROR_OTHER;
6986 else
6987 {
6988 mch_memmove(fname, fname_buf, (size_t)i);
6989 STRCPY(fname + i, name + llen);
6990 }
6991 }
6992 }
6993 else
6994 fname = name;
6995
6996 *doesrange = FALSE;
6997
6998
6999 /* execute the function if no errors detected and executing */
7000 if (evaluate && error == ERROR_NONE)
7001 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007002 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 error = ERROR_UNKNOWN;
7004
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007005 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 {
7007 /*
7008 * User defined function.
7009 */
7010 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007011
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007013 /* Trigger FuncUndefined event, may load the function. */
7014 if (fp == NULL
7015 && apply_autocmds(EVENT_FUNCUNDEFINED,
7016 fname, fname, TRUE, NULL)
7017 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007019 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 fp = find_func(fname);
7021 }
7022#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007023 /* Try loading a package. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007024 if (fp == NULL && script_autoload(fname) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007025 {
7026 /* loaded a package, search for the function again */
7027 fp = find_func(fname);
7028 }
7029
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 if (fp != NULL)
7031 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007032 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007034 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007036 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007038 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007039 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 else
7041 {
7042 /*
7043 * Call the user function.
7044 * Save and restore search patterns, script variables and
7045 * redo buffer.
7046 */
7047 save_search_patterns();
7048 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007049 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007050 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007051 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007052 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7053 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7054 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007055 /* Function was unreferenced while being used, free it
7056 * now. */
7057 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 restoreRedobuff();
7059 restore_search_patterns();
7060 error = ERROR_NONE;
7061 }
7062 }
7063 }
7064 else
7065 {
7066 /*
7067 * Find the function name in the table, call its implementation.
7068 */
7069 i = find_internal_func(fname);
7070 if (i >= 0)
7071 {
7072 if (argcount < functions[i].f_min_argc)
7073 error = ERROR_TOOFEW;
7074 else if (argcount > functions[i].f_max_argc)
7075 error = ERROR_TOOMANY;
7076 else
7077 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007078 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007079 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 error = ERROR_NONE;
7081 }
7082 }
7083 }
7084 /*
7085 * The function call (or "FuncUndefined" autocommand sequence) might
7086 * have been aborted by an error, an interrupt, or an explicitly thrown
7087 * exception that has not been caught so far. This situation can be
7088 * tested for by calling aborting(). For an error in an internal
7089 * function or for the "E132" error in call_user_func(), however, the
7090 * throw point at which the "force_abort" flag (temporarily reset by
7091 * emsg()) is normally updated has not been reached yet. We need to
7092 * update that flag first to make aborting() reliable.
7093 */
7094 update_force_abort();
7095 }
7096 if (error == ERROR_NONE)
7097 ret = OK;
7098
7099 /*
7100 * Report an error unless the argument evaluation or function call has been
7101 * cancelled due to an aborting error, an interrupt, or an exception.
7102 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007103 if (!aborting())
7104 {
7105 switch (error)
7106 {
7107 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007108 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007109 break;
7110 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007111 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007112 break;
7113 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007114 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007115 name);
7116 break;
7117 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007118 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007119 name);
7120 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007121 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007122 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007123 name);
7124 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007125 }
7126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127
7128 name[len] = cc;
7129 if (fname != name && fname != fname_buf)
7130 vim_free(fname);
7131
7132 return ret;
7133}
7134
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007135/*
7136 * Give an error message with a function name. Handle <SNR> things.
7137 */
7138 static void
7139emsg_funcname(msg, name)
7140 char *msg;
7141 char_u *name;
7142{
7143 char_u *p;
7144
7145 if (*name == K_SPECIAL)
7146 p = concat_str((char_u *)"<SNR>", name + 3);
7147 else
7148 p = name;
7149 EMSG2(_(msg), p);
7150 if (p != name)
7151 vim_free(p);
7152}
7153
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154/*********************************************
7155 * Implementation of the built-in functions
7156 */
7157
7158/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007159 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 */
7161 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007162f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007163 typval_T *argvars;
7164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165{
Bram Moolenaar33570922005-01-25 22:26:29 +00007166 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007168 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007169 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007171 if ((l = argvars[0].vval.v_list) != NULL
7172 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7173 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007174 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007175 }
7176 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007177 EMSG(_(e_listreq));
7178}
7179
7180/*
7181 * "append(lnum, string/list)" function
7182 */
7183 static void
7184f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007185 typval_T *argvars;
7186 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007187{
7188 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007189 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007190 list_T *l = NULL;
7191 listitem_T *li = NULL;
7192 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007193 long added = 0;
7194
Bram Moolenaar0d660222005-01-07 21:51:51 +00007195 lnum = get_tv_lnum(argvars);
7196 if (lnum >= 0
7197 && lnum <= curbuf->b_ml.ml_line_count
7198 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007199 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007200 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007201 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007202 l = argvars[1].vval.v_list;
7203 if (l == NULL)
7204 return;
7205 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007206 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007207 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007208 for (;;)
7209 {
7210 if (l == NULL)
7211 tv = &argvars[1]; /* append a string */
7212 else if (li == NULL)
7213 break; /* end of list */
7214 else
7215 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007216 line = get_tv_string_chk(tv);
7217 if (line == NULL) /* type error */
7218 {
7219 rettv->vval.v_number = 1; /* Failed */
7220 break;
7221 }
7222 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007223 ++added;
7224 if (l == NULL)
7225 break;
7226 li = li->li_next;
7227 }
7228
7229 appended_lines_mark(lnum, added);
7230 if (curwin->w_cursor.lnum > lnum)
7231 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007233 else
7234 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235}
7236
7237/*
7238 * "argc()" function
7239 */
7240/* ARGSUSED */
7241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007242f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007243 typval_T *argvars;
7244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007246 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247}
7248
7249/*
7250 * "argidx()" function
7251 */
7252/* ARGSUSED */
7253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007254f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007255 typval_T *argvars;
7256 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007258 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259}
7260
7261/*
7262 * "argv(nr)" function
7263 */
7264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007265f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007266 typval_T *argvars;
7267 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268{
7269 int idx;
7270
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007271 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007273 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007275 rettv->vval.v_string = NULL;
7276 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277}
7278
7279/*
7280 * "browse(save, title, initdir, default)" function
7281 */
7282/* ARGSUSED */
7283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007284f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007285 typval_T *argvars;
7286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287{
7288#ifdef FEAT_BROWSE
7289 int save;
7290 char_u *title;
7291 char_u *initdir;
7292 char_u *defname;
7293 char_u buf[NUMBUFLEN];
7294 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007295 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007297 save = get_tv_number_chk(&argvars[0], &error);
7298 title = get_tv_string_chk(&argvars[1]);
7299 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7300 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007302 if (error || title == NULL || initdir == NULL || defname == NULL)
7303 rettv->vval.v_string = NULL;
7304 else
7305 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007306 do_browse(save ? BROWSE_SAVE : 0,
7307 title, defname, NULL, initdir, NULL, curbuf);
7308#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007309 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007310#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007311 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007312}
7313
7314/*
7315 * "browsedir(title, initdir)" function
7316 */
7317/* ARGSUSED */
7318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007319f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007320 typval_T *argvars;
7321 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007322{
7323#ifdef FEAT_BROWSE
7324 char_u *title;
7325 char_u *initdir;
7326 char_u buf[NUMBUFLEN];
7327
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007328 title = get_tv_string_chk(&argvars[0]);
7329 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007330
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007331 if (title == NULL || initdir == NULL)
7332 rettv->vval.v_string = NULL;
7333 else
7334 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007335 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007337 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007339 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340}
7341
Bram Moolenaar33570922005-01-25 22:26:29 +00007342static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007343
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344/*
7345 * Find a buffer by number or exact name.
7346 */
7347 static buf_T *
7348find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007349 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350{
7351 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007353 if (avar->v_type == VAR_NUMBER)
7354 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007355 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007357 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007358 if (buf == NULL)
7359 {
7360 /* No full path name match, try a match with a URL or a "nofile"
7361 * buffer, these don't use the full path. */
7362 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7363 if (buf->b_fname != NULL
7364 && (path_with_url(buf->b_fname)
7365#ifdef FEAT_QUICKFIX
7366 || bt_nofile(buf)
7367#endif
7368 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007369 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007370 break;
7371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 }
7373 return buf;
7374}
7375
7376/*
7377 * "bufexists(expr)" function
7378 */
7379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007380f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007381 typval_T *argvars;
7382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007384 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385}
7386
7387/*
7388 * "buflisted(expr)" function
7389 */
7390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007391f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007392 typval_T *argvars;
7393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394{
7395 buf_T *buf;
7396
7397 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007398 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399}
7400
7401/*
7402 * "bufloaded(expr)" function
7403 */
7404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007405f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007406 typval_T *argvars;
7407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408{
7409 buf_T *buf;
7410
7411 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007412 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413}
7414
Bram Moolenaar33570922005-01-25 22:26:29 +00007415static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007416
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417/*
7418 * Get buffer by number or pattern.
7419 */
7420 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007421get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007422 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007424 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 int save_magic;
7426 char_u *save_cpo;
7427 buf_T *buf;
7428
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007429 if (tv->v_type == VAR_NUMBER)
7430 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007431 if (tv->v_type != VAR_STRING)
7432 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 if (name == NULL || *name == NUL)
7434 return curbuf;
7435 if (name[0] == '$' && name[1] == NUL)
7436 return lastbuf;
7437
7438 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7439 save_magic = p_magic;
7440 p_magic = TRUE;
7441 save_cpo = p_cpo;
7442 p_cpo = (char_u *)"";
7443
7444 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7445 TRUE, FALSE));
7446
7447 p_magic = save_magic;
7448 p_cpo = save_cpo;
7449
7450 /* If not found, try expanding the name, like done for bufexists(). */
7451 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007452 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453
7454 return buf;
7455}
7456
7457/*
7458 * "bufname(expr)" function
7459 */
7460 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007461f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007462 typval_T *argvars;
7463 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464{
7465 buf_T *buf;
7466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007467 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007469 buf = get_buf_tv(&argvars[0]);
7470 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007472 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007474 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 --emsg_off;
7476}
7477
7478/*
7479 * "bufnr(expr)" function
7480 */
7481 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007482f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007483 typval_T *argvars;
7484 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485{
7486 buf_T *buf;
7487
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007488 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007490 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007492 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007494 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 --emsg_off;
7496}
7497
7498/*
7499 * "bufwinnr(nr)" function
7500 */
7501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007502f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007503 typval_T *argvars;
7504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505{
7506#ifdef FEAT_WINDOWS
7507 win_T *wp;
7508 int winnr = 0;
7509#endif
7510 buf_T *buf;
7511
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007512 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007514 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515#ifdef FEAT_WINDOWS
7516 for (wp = firstwin; wp; wp = wp->w_next)
7517 {
7518 ++winnr;
7519 if (wp->w_buffer == buf)
7520 break;
7521 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007522 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007524 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525#endif
7526 --emsg_off;
7527}
7528
7529/*
7530 * "byte2line(byte)" function
7531 */
7532/*ARGSUSED*/
7533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007534f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007535 typval_T *argvars;
7536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537{
7538#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007539 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540#else
7541 long boff = 0;
7542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007543 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007545 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007547 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 (linenr_T)0, &boff);
7549#endif
7550}
7551
7552/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007553 * "byteidx()" function
7554 */
7555/*ARGSUSED*/
7556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007557f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007558 typval_T *argvars;
7559 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007560{
7561#ifdef FEAT_MBYTE
7562 char_u *t;
7563#endif
7564 char_u *str;
7565 long idx;
7566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007567 str = get_tv_string_chk(&argvars[0]);
7568 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007569 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007570 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007571 return;
7572
7573#ifdef FEAT_MBYTE
7574 t = str;
7575 for ( ; idx > 0; idx--)
7576 {
7577 if (*t == NUL) /* EOL reached */
7578 return;
7579 t += mb_ptr2len_check(t);
7580 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007581 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007582#else
7583 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007584 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007585#endif
7586}
7587
7588/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007589 * "call(func, arglist)" function
7590 */
7591 static void
7592f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 typval_T *argvars;
7594 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007595{
7596 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007597 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007598 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007599 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007600 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007601 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007602
7603 rettv->vval.v_number = 0;
7604 if (argvars[1].v_type != VAR_LIST)
7605 {
7606 EMSG(_(e_listreq));
7607 return;
7608 }
7609 if (argvars[1].vval.v_list == NULL)
7610 return;
7611
7612 if (argvars[0].v_type == VAR_FUNC)
7613 func = argvars[0].vval.v_string;
7614 else
7615 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007616 if (*func == NUL)
7617 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007618
Bram Moolenaare9a41262005-01-15 22:18:47 +00007619 if (argvars[2].v_type != VAR_UNKNOWN)
7620 {
7621 if (argvars[2].v_type != VAR_DICT)
7622 {
7623 EMSG(_(e_dictreq));
7624 return;
7625 }
7626 selfdict = argvars[2].vval.v_dict;
7627 }
7628
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007629 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7630 item = item->li_next)
7631 {
7632 if (argc == MAX_FUNC_ARGS)
7633 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007634 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007635 break;
7636 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007637 /* Make a copy of each argument. This is needed to be able to set
7638 * v_lock to VAR_FIXED in the copy without changing the original list.
7639 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007640 copy_tv(&item->li_tv, &argv[argc++]);
7641 }
7642
7643 if (item == NULL)
7644 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007645 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7646 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007647
7648 /* Free the arguments. */
7649 while (argc > 0)
7650 clear_tv(&argv[--argc]);
7651}
7652
7653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 * "char2nr(string)" function
7655 */
7656 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007657f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007658 typval_T *argvars;
7659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660{
7661#ifdef FEAT_MBYTE
7662 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007663 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 else
7665#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007666 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667}
7668
7669/*
7670 * "cindent(lnum)" function
7671 */
7672 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007673f_cindent(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_CINDENT
7678 pos_T pos;
7679 linenr_T lnum;
7680
7681 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007682 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7684 {
7685 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007686 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 curwin->w_cursor = pos;
7688 }
7689 else
7690#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007691 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692}
7693
7694/*
7695 * "col(string)" function
7696 */
7697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007698f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007699 typval_T *argvars;
7700 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701{
7702 colnr_T col = 0;
7703 pos_T *fp;
7704
7705 fp = var2fpos(&argvars[0], FALSE);
7706 if (fp != NULL)
7707 {
7708 if (fp->col == MAXCOL)
7709 {
7710 /* '> can be MAXCOL, get the length of the line then */
7711 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7712 col = STRLEN(ml_get(fp->lnum)) + 1;
7713 else
7714 col = MAXCOL;
7715 }
7716 else
7717 {
7718 col = fp->col + 1;
7719#ifdef FEAT_VIRTUALEDIT
7720 /* col(".") when the cursor is on the NUL at the end of the line
7721 * because of "coladd" can be seen as an extra column. */
7722 if (virtual_active() && fp == &curwin->w_cursor)
7723 {
7724 char_u *p = ml_get_cursor();
7725
7726 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7727 curwin->w_virtcol - curwin->w_cursor.coladd))
7728 {
7729# ifdef FEAT_MBYTE
7730 int l;
7731
7732 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
7733 col += l;
7734# else
7735 if (*p != NUL && p[1] == NUL)
7736 ++col;
7737# endif
7738 }
7739 }
7740#endif
7741 }
7742 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007743 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744}
7745
7746/*
7747 * "confirm(message, buttons[, default [, type]])" function
7748 */
7749/*ARGSUSED*/
7750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007751f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007752 typval_T *argvars;
7753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754{
7755#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7756 char_u *message;
7757 char_u *buttons = NULL;
7758 char_u buf[NUMBUFLEN];
7759 char_u buf2[NUMBUFLEN];
7760 int def = 1;
7761 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007762 char_u *typestr;
7763 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007765 message = get_tv_string_chk(&argvars[0]);
7766 if (message == NULL)
7767 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007768 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007770 buttons = get_tv_string_buf_chk(&argvars[1], buf);
7771 if (buttons == NULL)
7772 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007773 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007775 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007776 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007778 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
7779 if (typestr == NULL)
7780 error = TRUE;
7781 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007783 switch (TOUPPER_ASC(*typestr))
7784 {
7785 case 'E': type = VIM_ERROR; break;
7786 case 'Q': type = VIM_QUESTION; break;
7787 case 'I': type = VIM_INFO; break;
7788 case 'W': type = VIM_WARNING; break;
7789 case 'G': type = VIM_GENERIC; break;
7790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 }
7792 }
7793 }
7794 }
7795
7796 if (buttons == NULL || *buttons == NUL)
7797 buttons = (char_u *)_("&Ok");
7798
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007799 if (error)
7800 rettv->vval.v_number = 0;
7801 else
7802 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 def, NULL);
7804#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007805 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806#endif
7807}
7808
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007809/*
7810 * "copy()" function
7811 */
7812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007813f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007814 typval_T *argvars;
7815 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007816{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007817 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007818}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819
7820/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007821 * "count()" function
7822 */
7823 static void
7824f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007825 typval_T *argvars;
7826 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007827{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007828 long n = 0;
7829 int ic = FALSE;
7830
Bram Moolenaare9a41262005-01-15 22:18:47 +00007831 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007832 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007833 listitem_T *li;
7834 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007835 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007836
Bram Moolenaare9a41262005-01-15 22:18:47 +00007837 if ((l = argvars[0].vval.v_list) != NULL)
7838 {
7839 li = l->lv_first;
7840 if (argvars[2].v_type != VAR_UNKNOWN)
7841 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007842 int error = FALSE;
7843
7844 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007845 if (argvars[3].v_type != VAR_UNKNOWN)
7846 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007847 idx = get_tv_number_chk(&argvars[3], &error);
7848 if (!error)
7849 {
7850 li = list_find(l, idx);
7851 if (li == NULL)
7852 EMSGN(_(e_listidx), idx);
7853 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007854 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007855 if (error)
7856 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007857 }
7858
7859 for ( ; li != NULL; li = li->li_next)
7860 if (tv_equal(&li->li_tv, &argvars[1], ic))
7861 ++n;
7862 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007863 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007864 else if (argvars[0].v_type == VAR_DICT)
7865 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007866 int todo;
7867 dict_T *d;
7868 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007869
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007870 if ((d = argvars[0].vval.v_dict) != NULL)
7871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007872 int error = FALSE;
7873
Bram Moolenaare9a41262005-01-15 22:18:47 +00007874 if (argvars[2].v_type != VAR_UNKNOWN)
7875 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007876 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007877 if (argvars[3].v_type != VAR_UNKNOWN)
7878 EMSG(_(e_invarg));
7879 }
7880
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007881 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007882 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007883 {
7884 if (!HASHITEM_EMPTY(hi))
7885 {
7886 --todo;
7887 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7888 ++n;
7889 }
7890 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007891 }
7892 }
7893 else
7894 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007895 rettv->vval.v_number = n;
7896}
7897
7898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
7900 *
7901 * Checks the existence of a cscope connection.
7902 */
7903/*ARGSUSED*/
7904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007905f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007906 typval_T *argvars;
7907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908{
7909#ifdef FEAT_CSCOPE
7910 int num = 0;
7911 char_u *dbpath = NULL;
7912 char_u *prepend = NULL;
7913 char_u buf[NUMBUFLEN];
7914
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007915 if (argvars[0].v_type != VAR_UNKNOWN
7916 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007918 num = (int)get_tv_number(&argvars[0]);
7919 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007920 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007921 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 }
7923
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007924 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007926 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927#endif
7928}
7929
7930/*
7931 * "cursor(lnum, col)" function
7932 *
7933 * Moves the cursor to the specified line and column
7934 */
7935/*ARGSUSED*/
7936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007937f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007938 typval_T *argvars;
7939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940{
7941 long line, col;
7942
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007943 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007944 col = get_tv_number_chk(&argvars[1], NULL);
7945 if (line < 0 || col < 0)
7946 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 if (line > 0)
7948 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 if (col > 0)
7950 curwin->w_cursor.col = col - 1;
7951#ifdef FEAT_VIRTUALEDIT
7952 curwin->w_cursor.coladd = 0;
7953#endif
7954
7955 /* Make sure the cursor is in a valid position. */
7956 check_cursor();
7957#ifdef FEAT_MBYTE
7958 /* Correct cursor for multi-byte character. */
7959 if (has_mbyte)
7960 mb_adjust_cursor();
7961#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007962
7963 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964}
7965
7966/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007967 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 */
7969 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007970f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007971 typval_T *argvars;
7972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007974 int noref = 0;
7975
7976 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007977 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007978 if (noref < 0 || noref > 1)
7979 EMSG(_(e_invarg));
7980 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00007981 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982}
7983
7984/*
7985 * "delete()" function
7986 */
7987 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007988f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007989 typval_T *argvars;
7990 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991{
7992 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007993 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007995 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996}
7997
7998/*
7999 * "did_filetype()" function
8000 */
8001/*ARGSUSED*/
8002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008003f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008004 typval_T *argvars;
8005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006{
8007#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008008 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008010 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011#endif
8012}
8013
8014/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008015 * "diff_filler()" function
8016 */
8017/*ARGSUSED*/
8018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008019f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008020 typval_T *argvars;
8021 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008022{
8023#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008024 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008025#endif
8026}
8027
8028/*
8029 * "diff_hlID()" function
8030 */
8031/*ARGSUSED*/
8032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008033f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008034 typval_T *argvars;
8035 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008036{
8037#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008038 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008039 static linenr_T prev_lnum = 0;
8040 static int changedtick = 0;
8041 static int fnum = 0;
8042 static int change_start = 0;
8043 static int change_end = 0;
8044 static enum hlf_value hlID = 0;
8045 int filler_lines;
8046 int col;
8047
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008048 if (lnum < 0) /* ignore type error in {lnum} arg */
8049 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008050 if (lnum != prev_lnum
8051 || changedtick != curbuf->b_changedtick
8052 || fnum != curbuf->b_fnum)
8053 {
8054 /* New line, buffer, change: need to get the values. */
8055 filler_lines = diff_check(curwin, lnum);
8056 if (filler_lines < 0)
8057 {
8058 if (filler_lines == -1)
8059 {
8060 change_start = MAXCOL;
8061 change_end = -1;
8062 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8063 hlID = HLF_ADD; /* added line */
8064 else
8065 hlID = HLF_CHD; /* changed line */
8066 }
8067 else
8068 hlID = HLF_ADD; /* added line */
8069 }
8070 else
8071 hlID = (enum hlf_value)0;
8072 prev_lnum = lnum;
8073 changedtick = curbuf->b_changedtick;
8074 fnum = curbuf->b_fnum;
8075 }
8076
8077 if (hlID == HLF_CHD || hlID == HLF_TXD)
8078 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008079 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008080 if (col >= change_start && col <= change_end)
8081 hlID = HLF_TXD; /* changed text */
8082 else
8083 hlID = HLF_CHD; /* changed line */
8084 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008085 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008086#endif
8087}
8088
8089/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008090 * "empty({expr})" function
8091 */
8092 static void
8093f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008094 typval_T *argvars;
8095 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008096{
8097 int n;
8098
8099 switch (argvars[0].v_type)
8100 {
8101 case VAR_STRING:
8102 case VAR_FUNC:
8103 n = argvars[0].vval.v_string == NULL
8104 || *argvars[0].vval.v_string == NUL;
8105 break;
8106 case VAR_NUMBER:
8107 n = argvars[0].vval.v_number == 0;
8108 break;
8109 case VAR_LIST:
8110 n = argvars[0].vval.v_list == NULL
8111 || argvars[0].vval.v_list->lv_first == NULL;
8112 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008113 case VAR_DICT:
8114 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008115 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008116 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008117 default:
8118 EMSG2(_(e_intern2), "f_empty()");
8119 n = 0;
8120 }
8121
8122 rettv->vval.v_number = n;
8123}
8124
8125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 * "escape({string}, {chars})" function
8127 */
8128 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008129f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008130 typval_T *argvars;
8131 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132{
8133 char_u buf[NUMBUFLEN];
8134
Bram Moolenaar758711c2005-02-02 23:11:38 +00008135 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8136 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008137 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138}
8139
8140/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008141 * "eval()" function
8142 */
8143/*ARGSUSED*/
8144 static void
8145f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008146 typval_T *argvars;
8147 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008148{
8149 char_u *s;
8150
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008151 s = get_tv_string_chk(&argvars[0]);
8152 if (s != NULL)
8153 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008154
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008155 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8156 {
8157 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008158 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008159 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008160 else if (*s != NUL)
8161 EMSG(_(e_trailing));
8162}
8163
8164/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 * "eventhandler()" function
8166 */
8167/*ARGSUSED*/
8168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008169f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008170 typval_T *argvars;
8171 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008173 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174}
8175
8176/*
8177 * "executable()" function
8178 */
8179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008180f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008181 typval_T *argvars;
8182 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008184 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185}
8186
8187/*
8188 * "exists()" function
8189 */
8190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008191f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008192 typval_T *argvars;
8193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194{
8195 char_u *p;
8196 char_u *name;
8197 int n = FALSE;
8198 int len = 0;
8199
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008200 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 if (*p == '$') /* environment variable */
8202 {
8203 /* first try "normal" environment variables (fast) */
8204 if (mch_getenv(p + 1) != NULL)
8205 n = TRUE;
8206 else
8207 {
8208 /* try expanding things like $VIM and ${HOME} */
8209 p = expand_env_save(p);
8210 if (p != NULL && *p != '$')
8211 n = TRUE;
8212 vim_free(p);
8213 }
8214 }
8215 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008216 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 else if (*p == '*') /* internal or user defined function */
8218 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008219 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 }
8221 else if (*p == ':')
8222 {
8223 n = cmd_exists(p + 1);
8224 }
8225 else if (*p == '#')
8226 {
8227#ifdef FEAT_AUTOCMD
8228 name = p + 1;
8229 p = vim_strchr(name, '#');
8230 if (p != NULL)
8231 n = au_exists(name, p, p + 1);
8232 else
8233 n = au_exists(name, name + STRLEN(name), NULL);
8234#endif
8235 }
8236 else /* internal variable */
8237 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008238 char_u *tofree;
8239 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008241 /* get_name_len() takes care of expanding curly braces */
8242 name = p;
8243 len = get_name_len(&p, &tofree, TRUE, FALSE);
8244 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008246 if (tofree != NULL)
8247 name = tofree;
8248 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8249 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008251 /* handle d.key, l[idx], f(expr) */
8252 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8253 if (n)
8254 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 }
8256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008258 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 }
8260
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008261 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262}
8263
8264/*
8265 * "expand()" function
8266 */
8267 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008268f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008269 typval_T *argvars;
8270 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271{
8272 char_u *s;
8273 int len;
8274 char_u *errormsg;
8275 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8276 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008277 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008279 rettv->v_type = VAR_STRING;
8280 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 if (*s == '%' || *s == '#' || *s == '<')
8282 {
8283 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008284 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 --emsg_off;
8286 }
8287 else
8288 {
8289 /* When the optional second argument is non-zero, don't remove matches
8290 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008291 if (argvars[1].v_type != VAR_UNKNOWN
8292 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008294 if (!error)
8295 {
8296 ExpandInit(&xpc);
8297 xpc.xp_context = EXPAND_FILES;
8298 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8299 ExpandCleanup(&xpc);
8300 }
8301 else
8302 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 }
8304}
8305
8306/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008307 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008308 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008309 */
8310 static void
8311f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008312 typval_T *argvars;
8313 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008314{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008315 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008316 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008317 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008318 list_T *l1, *l2;
8319 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008320 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008321 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008322
Bram Moolenaare9a41262005-01-15 22:18:47 +00008323 l1 = argvars[0].vval.v_list;
8324 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008325 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8326 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008327 {
8328 if (argvars[2].v_type != VAR_UNKNOWN)
8329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008330 before = get_tv_number_chk(&argvars[2], &error);
8331 if (error)
8332 return; /* type error; errmsg already given */
8333
Bram Moolenaar758711c2005-02-02 23:11:38 +00008334 if (before == l1->lv_len)
8335 item = NULL;
8336 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008337 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008338 item = list_find(l1, before);
8339 if (item == NULL)
8340 {
8341 EMSGN(_(e_listidx), before);
8342 return;
8343 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008344 }
8345 }
8346 else
8347 item = NULL;
8348 list_extend(l1, l2, item);
8349
Bram Moolenaare9a41262005-01-15 22:18:47 +00008350 copy_tv(&argvars[0], rettv);
8351 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008352 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008353 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8354 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008355 dict_T *d1, *d2;
8356 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008357 char_u *action;
8358 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008359 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008360 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008361
8362 d1 = argvars[0].vval.v_dict;
8363 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008364 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8365 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008366 {
8367 /* Check the third argument. */
8368 if (argvars[2].v_type != VAR_UNKNOWN)
8369 {
8370 static char *(av[]) = {"keep", "force", "error"};
8371
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008372 action = get_tv_string_chk(&argvars[2]);
8373 if (action == NULL)
8374 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008375 for (i = 0; i < 3; ++i)
8376 if (STRCMP(action, av[i]) == 0)
8377 break;
8378 if (i == 3)
8379 {
8380 EMSGN(_(e_invarg2), action);
8381 return;
8382 }
8383 }
8384 else
8385 action = (char_u *)"force";
8386
8387 /* Go over all entries in the second dict and add them to the
8388 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008389 todo = d2->dv_hashtab.ht_used;
8390 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008391 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008392 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008393 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008394 --todo;
8395 di1 = dict_find(d1, hi2->hi_key, -1);
8396 if (di1 == NULL)
8397 {
8398 di1 = dictitem_copy(HI2DI(hi2));
8399 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8400 dictitem_free(di1);
8401 }
8402 else if (*action == 'e')
8403 {
8404 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8405 break;
8406 }
8407 else if (*action == 'f')
8408 {
8409 clear_tv(&di1->di_tv);
8410 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8411 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008412 }
8413 }
8414
Bram Moolenaare9a41262005-01-15 22:18:47 +00008415 copy_tv(&argvars[0], rettv);
8416 }
8417 }
8418 else
8419 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008420}
8421
8422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 * "filereadable()" function
8424 */
8425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008426f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008427 typval_T *argvars;
8428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429{
8430 FILE *fd;
8431 char_u *p;
8432 int n;
8433
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008434 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8436 {
8437 n = TRUE;
8438 fclose(fd);
8439 }
8440 else
8441 n = FALSE;
8442
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008443 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444}
8445
8446/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008447 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 * rights to write into.
8449 */
8450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008451f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008452 typval_T *argvars;
8453 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008455 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456}
8457
Bram Moolenaar33570922005-01-25 22:26:29 +00008458static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008459
8460 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008461findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008462 typval_T *argvars;
8463 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008464 int dir;
8465{
8466#ifdef FEAT_SEARCHPATH
8467 char_u *fname;
8468 char_u *fresult = NULL;
8469 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8470 char_u *p;
8471 char_u pathbuf[NUMBUFLEN];
8472 int count = 1;
8473 int first = TRUE;
8474
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008475 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008476
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008477 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008478 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008479 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8480 if (p == NULL)
8481 count = -1; /* error */
8482 else
8483 {
8484 if (*p != NUL)
8485 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008486
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008487 if (argvars[2].v_type != VAR_UNKNOWN)
8488 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8489 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008490 }
8491
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008492 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008493 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008494 do
8495 {
8496 vim_free(fresult);
8497 fresult = find_file_in_path_option(first ? fname : NULL,
8498 first ? (int)STRLEN(fname) : 0,
8499 0, first, path, dir, NULL);
8500 first = FALSE;
8501 } while (--count > 0 && fresult != NULL);
8502 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008503
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008504 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008505#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008506 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008507#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008508 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008509}
8510
Bram Moolenaar33570922005-01-25 22:26:29 +00008511static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8512static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008513
8514/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008515 * Prepare v: variable "idx" to be used.
8516 * Save the current typeval in "save_tv".
8517 * When not used yet add the variable to the v: hashtable.
8518 */
8519 static void
8520prepare_vimvar(idx, save_tv)
8521 int idx;
8522 typval_T *save_tv;
8523{
8524 *save_tv = vimvars[idx].vv_tv;
8525 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8526 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
8527}
8528
8529/*
8530 * Restore v: variable "idx" to typeval "save_tv".
8531 * When no longer defined, remove the variable from the v: hashtable.
8532 */
8533 static void
8534restore_vimvar(idx, save_tv)
8535 int idx;
8536 typval_T *save_tv;
8537{
8538 hashitem_T *hi;
8539
8540 clear_tv(&vimvars[idx].vv_tv);
8541 vimvars[idx].vv_tv = *save_tv;
8542 if (vimvars[idx].vv_type == VAR_UNKNOWN)
8543 {
8544 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
8545 if (HASHITEM_EMPTY(hi))
8546 EMSG2(_(e_intern2), "restore_vimvar()");
8547 else
8548 hash_remove(&vimvarht, hi);
8549 }
8550}
8551
8552/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008553 * Implementation of map() and filter().
8554 */
8555 static void
8556filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008557 typval_T *argvars;
8558 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008559 int map;
8560{
8561 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008562 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008563 listitem_T *li, *nli;
8564 list_T *l = NULL;
8565 dictitem_T *di;
8566 hashtab_T *ht;
8567 hashitem_T *hi;
8568 dict_T *d = NULL;
8569 typval_T save_val;
8570 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008571 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008572 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008573 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8574
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008575
8576 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008577 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008578 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008579 if ((l = argvars[0].vval.v_list) == NULL
8580 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008581 return;
8582 }
8583 else if (argvars[0].v_type == VAR_DICT)
8584 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008585 if ((d = argvars[0].vval.v_dict) == NULL
8586 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008587 return;
8588 }
8589 else
8590 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008591 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008592 return;
8593 }
8594
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008595 expr = get_tv_string_buf_chk(&argvars[1], buf);
8596 /* On type errors, the preceding call has already displayed an error
8597 * message. Avoid a misleading error message for an empty string that
8598 * was not passed as argument. */
8599 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008600 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008601 prepare_vimvar(VV_VAL, &save_val);
8602 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008603
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008604 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008605 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008606 prepare_vimvar(VV_KEY, &save_key);
8607 vimvars[VV_KEY].vv_type = VAR_STRING;
8608
8609 ht = &d->dv_hashtab;
8610 hash_lock(ht);
8611 todo = ht->ht_used;
8612 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008614 if (!HASHITEM_EMPTY(hi))
8615 {
8616 --todo;
8617 di = HI2DI(hi);
8618 if (tv_check_lock(di->di_tv.v_lock, msg))
8619 break;
8620 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8621 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8622 break;
8623 if (!map && rem)
8624 dictitem_remove(d, di);
8625 clear_tv(&vimvars[VV_KEY].vv_tv);
8626 }
8627 }
8628 hash_unlock(ht);
8629
8630 restore_vimvar(VV_KEY, &save_key);
8631 }
8632 else
8633 {
8634 for (li = l->lv_first; li != NULL; li = nli)
8635 {
8636 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008637 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008638 nli = li->li_next;
8639 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008640 break;
8641 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008642 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008643 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008644 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008646 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008647 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008648
8649 copy_tv(&argvars[0], rettv);
8650}
8651
8652 static int
8653filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008654 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008655 char_u *expr;
8656 int map;
8657 int *remp;
8658{
Bram Moolenaar33570922005-01-25 22:26:29 +00008659 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008660 char_u *s;
8661
Bram Moolenaar33570922005-01-25 22:26:29 +00008662 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008663 s = expr;
8664 if (eval1(&s, &rettv, TRUE) == FAIL)
8665 return FAIL;
8666 if (*s != NUL) /* check for trailing chars after expr */
8667 {
8668 EMSG2(_(e_invexpr2), s);
8669 return FAIL;
8670 }
8671 if (map)
8672 {
8673 /* map(): replace the list item value */
8674 clear_tv(tv);
8675 *tv = rettv;
8676 }
8677 else
8678 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008679 int error = FALSE;
8680
Bram Moolenaare9a41262005-01-15 22:18:47 +00008681 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008682 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008683 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008684 /* On type error, nothing has been removed; return FAIL to stop the
8685 * loop. The error message was given by get_tv_number_chk(). */
8686 if (error)
8687 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008688 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008689 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008690 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008691}
8692
8693/*
8694 * "filter()" function
8695 */
8696 static void
8697f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008698 typval_T *argvars;
8699 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008700{
8701 filter_map(argvars, rettv, FALSE);
8702}
8703
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008704/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008705 * "finddir({fname}[, {path}[, {count}]])" function
8706 */
8707 static void
8708f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008709 typval_T *argvars;
8710 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008711{
8712 findfilendir(argvars, rettv, TRUE);
8713}
8714
8715/*
8716 * "findfile({fname}[, {path}[, {count}]])" function
8717 */
8718 static void
8719f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008720 typval_T *argvars;
8721 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008722{
8723 findfilendir(argvars, rettv, FALSE);
8724}
8725
8726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 * "fnamemodify({fname}, {mods})" function
8728 */
8729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008730f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008731 typval_T *argvars;
8732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733{
8734 char_u *fname;
8735 char_u *mods;
8736 int usedlen = 0;
8737 int len;
8738 char_u *fbuf = NULL;
8739 char_u buf[NUMBUFLEN];
8740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008741 fname = get_tv_string_chk(&argvars[0]);
8742 mods = get_tv_string_buf_chk(&argvars[1], buf);
8743 if (fname == NULL || mods == NULL)
8744 fname = NULL;
8745 else
8746 {
8747 len = (int)STRLEN(fname);
8748 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008751 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008753 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008755 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 vim_free(fbuf);
8757}
8758
Bram Moolenaar33570922005-01-25 22:26:29 +00008759static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760
8761/*
8762 * "foldclosed()" function
8763 */
8764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008765foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008766 typval_T *argvars;
8767 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 int end;
8769{
8770#ifdef FEAT_FOLDING
8771 linenr_T lnum;
8772 linenr_T first, last;
8773
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008774 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8776 {
8777 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8778 {
8779 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008780 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008782 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 return;
8784 }
8785 }
8786#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008787 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788}
8789
8790/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008791 * "foldclosed()" function
8792 */
8793 static void
8794f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008795 typval_T *argvars;
8796 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008797{
8798 foldclosed_both(argvars, rettv, FALSE);
8799}
8800
8801/*
8802 * "foldclosedend()" function
8803 */
8804 static void
8805f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008806 typval_T *argvars;
8807 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008808{
8809 foldclosed_both(argvars, rettv, TRUE);
8810}
8811
8812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 * "foldlevel()" function
8814 */
8815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008816f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008817 typval_T *argvars;
8818 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819{
8820#ifdef FEAT_FOLDING
8821 linenr_T lnum;
8822
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008823 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008825 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 else
8827#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008828 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829}
8830
8831/*
8832 * "foldtext()" function
8833 */
8834/*ARGSUSED*/
8835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008836f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008837 typval_T *argvars;
8838 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839{
8840#ifdef FEAT_FOLDING
8841 linenr_T lnum;
8842 char_u *s;
8843 char_u *r;
8844 int len;
8845 char *txt;
8846#endif
8847
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008848 rettv->v_type = VAR_STRING;
8849 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008851 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8852 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8853 <= curbuf->b_ml.ml_line_count
8854 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 {
8856 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008857 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8858 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 {
8860 if (!linewhite(lnum))
8861 break;
8862 ++lnum;
8863 }
8864
8865 /* Find interesting text in this line. */
8866 s = skipwhite(ml_get(lnum));
8867 /* skip C comment-start */
8868 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008869 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008871 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008872 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008873 {
8874 s = skipwhite(ml_get(lnum + 1));
8875 if (*s == '*')
8876 s = skipwhite(s + 1);
8877 }
8878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 txt = _("+-%s%3ld lines: ");
8880 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008881 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 + 20 /* for %3ld */
8883 + STRLEN(s))); /* concatenated */
8884 if (r != NULL)
8885 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008886 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8887 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8888 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 len = (int)STRLEN(r);
8890 STRCAT(r, s);
8891 /* remove 'foldmarker' and 'commentstring' */
8892 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008893 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 }
8895 }
8896#endif
8897}
8898
8899/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008900 * "foldtextresult(lnum)" function
8901 */
8902/*ARGSUSED*/
8903 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008904f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008905 typval_T *argvars;
8906 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008907{
8908#ifdef FEAT_FOLDING
8909 linenr_T lnum;
8910 char_u *text;
8911 char_u buf[51];
8912 foldinfo_T foldinfo;
8913 int fold_count;
8914#endif
8915
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008916 rettv->v_type = VAR_STRING;
8917 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008918#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008919 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008920 /* treat illegal types and illegal string values for {lnum} the same */
8921 if (lnum < 0)
8922 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008923 fold_count = foldedCount(curwin, lnum, &foldinfo);
8924 if (fold_count > 0)
8925 {
8926 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8927 &foldinfo, buf);
8928 if (text == buf)
8929 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008930 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008931 }
8932#endif
8933}
8934
8935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 * "foreground()" function
8937 */
8938/*ARGSUSED*/
8939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008940f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008941 typval_T *argvars;
8942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945#ifdef FEAT_GUI
8946 if (gui.in_use)
8947 gui_mch_set_foreground();
8948#else
8949# ifdef WIN32
8950 win32_set_foreground();
8951# endif
8952#endif
8953}
8954
8955/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008956 * "function()" function
8957 */
8958/*ARGSUSED*/
8959 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008960f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008961 typval_T *argvars;
8962 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008963{
8964 char_u *s;
8965
Bram Moolenaara7043832005-01-21 11:56:39 +00008966 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008967 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008968 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008969 EMSG2(_(e_invarg2), s);
8970 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008971 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008972 else
8973 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008974 rettv->vval.v_string = vim_strsave(s);
8975 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008976 }
8977}
8978
8979/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00008980 * "garbagecollect()" function
8981 */
8982/*ARGSUSED*/
8983 static void
8984f_garbagecollect(argvars, rettv)
8985 typval_T *argvars;
8986 typval_T *rettv;
8987{
8988 garbage_collect();
8989}
8990
8991/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008992 * "get()" function
8993 */
8994 static void
8995f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008996 typval_T *argvars;
8997 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008998{
Bram Moolenaar33570922005-01-25 22:26:29 +00008999 listitem_T *li;
9000 list_T *l;
9001 dictitem_T *di;
9002 dict_T *d;
9003 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009004
Bram Moolenaare9a41262005-01-15 22:18:47 +00009005 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009006 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009007 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009008 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009009 int error = FALSE;
9010
9011 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9012 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009013 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009014 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009015 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009016 else if (argvars[0].v_type == VAR_DICT)
9017 {
9018 if ((d = argvars[0].vval.v_dict) != NULL)
9019 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009020 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009021 if (di != NULL)
9022 tv = &di->di_tv;
9023 }
9024 }
9025 else
9026 EMSG2(_(e_listdictarg), "get()");
9027
9028 if (tv == NULL)
9029 {
9030 if (argvars[2].v_type == VAR_UNKNOWN)
9031 rettv->vval.v_number = 0;
9032 else
9033 copy_tv(&argvars[2], rettv);
9034 }
9035 else
9036 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009037}
9038
9039/*
9040 * "getbufvar()" function
9041 */
9042 static void
9043f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009044 typval_T *argvars;
9045 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009046{
9047 buf_T *buf;
9048 buf_T *save_curbuf;
9049 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009050 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009052 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9053 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009054 ++emsg_off;
9055 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009056
9057 rettv->v_type = VAR_STRING;
9058 rettv->vval.v_string = NULL;
9059
9060 if (buf != NULL && varname != NULL)
9061 {
9062 if (*varname == '&') /* buffer-local-option */
9063 {
9064 /* set curbuf to be our buf, temporarily */
9065 save_curbuf = curbuf;
9066 curbuf = buf;
9067
9068 get_option_tv(&varname, rettv, TRUE);
9069
9070 /* restore previous notion of curbuf */
9071 curbuf = save_curbuf;
9072 }
9073 else
9074 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009075 if (*varname == NUL)
9076 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9077 * scope prefix before the NUL byte is required by
9078 * find_var_in_ht(). */
9079 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009080 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009081 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009082 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009083 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009084 }
9085 }
9086
9087 --emsg_off;
9088}
9089
9090/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 * "getchar()" function
9092 */
9093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009094f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009095 typval_T *argvars;
9096 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097{
9098 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009099 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100
9101 ++no_mapping;
9102 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009103 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 /* getchar(): blocking wait. */
9105 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009106 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 /* getchar(1): only check if char avail */
9108 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009109 else if (error || vpeekc() == NUL)
9110 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 n = 0;
9112 else
9113 /* getchar(0) and char avail: return char */
9114 n = safe_vgetc();
9115 --no_mapping;
9116 --allow_keys;
9117
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009118 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009119 if (IS_SPECIAL(n) || mod_mask != 0)
9120 {
9121 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9122 int i = 0;
9123
9124 /* Turn a special key into three bytes, plus modifier. */
9125 if (mod_mask != 0)
9126 {
9127 temp[i++] = K_SPECIAL;
9128 temp[i++] = KS_MODIFIER;
9129 temp[i++] = mod_mask;
9130 }
9131 if (IS_SPECIAL(n))
9132 {
9133 temp[i++] = K_SPECIAL;
9134 temp[i++] = K_SECOND(n);
9135 temp[i++] = K_THIRD(n);
9136 }
9137#ifdef FEAT_MBYTE
9138 else if (has_mbyte)
9139 i += (*mb_char2bytes)(n, temp + i);
9140#endif
9141 else
9142 temp[i++] = n;
9143 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009144 rettv->v_type = VAR_STRING;
9145 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 }
9147}
9148
9149/*
9150 * "getcharmod()" function
9151 */
9152/*ARGSUSED*/
9153 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009154f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009155 typval_T *argvars;
9156 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009158 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159}
9160
9161/*
9162 * "getcmdline()" function
9163 */
9164/*ARGSUSED*/
9165 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009166f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009167 typval_T *argvars;
9168 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009170 rettv->v_type = VAR_STRING;
9171 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172}
9173
9174/*
9175 * "getcmdpos()" function
9176 */
9177/*ARGSUSED*/
9178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009179f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009180 typval_T *argvars;
9181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009183 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184}
9185
9186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 * "getcwd()" function
9188 */
9189/*ARGSUSED*/
9190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009191f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009192 typval_T *argvars;
9193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194{
9195 char_u cwd[MAXPATHL];
9196
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009197 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009199 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200 else
9201 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009202 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009204 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205#endif
9206 }
9207}
9208
9209/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009210 * "getfontname()" function
9211 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009212/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009213 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009214f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009215 typval_T *argvars;
9216 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009217{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009218 rettv->v_type = VAR_STRING;
9219 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009220#ifdef FEAT_GUI
9221 if (gui.in_use)
9222 {
9223 GuiFont font;
9224 char_u *name = NULL;
9225
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009226 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009227 {
9228 /* Get the "Normal" font. Either the name saved by
9229 * hl_set_font_name() or from the font ID. */
9230 font = gui.norm_font;
9231 name = hl_get_font_name();
9232 }
9233 else
9234 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009235 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009236 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9237 return;
9238 font = gui_mch_get_font(name, FALSE);
9239 if (font == NOFONT)
9240 return; /* Invalid font name, return empty string. */
9241 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009242 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009243 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009244 gui_mch_free_font(font);
9245 }
9246#endif
9247}
9248
9249/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009250 * "getfperm({fname})" function
9251 */
9252 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009253f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009254 typval_T *argvars;
9255 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009256{
9257 char_u *fname;
9258 struct stat st;
9259 char_u *perm = NULL;
9260 char_u flags[] = "rwx";
9261 int i;
9262
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009263 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009264
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009265 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009266 if (mch_stat((char *)fname, &st) >= 0)
9267 {
9268 perm = vim_strsave((char_u *)"---------");
9269 if (perm != NULL)
9270 {
9271 for (i = 0; i < 9; i++)
9272 {
9273 if (st.st_mode & (1 << (8 - i)))
9274 perm[i] = flags[i % 3];
9275 }
9276 }
9277 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009278 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009279}
9280
9281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282 * "getfsize({fname})" function
9283 */
9284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009285f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009286 typval_T *argvars;
9287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288{
9289 char_u *fname;
9290 struct stat st;
9291
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009292 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009293
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009294 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295
9296 if (mch_stat((char *)fname, &st) >= 0)
9297 {
9298 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009299 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009301 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 }
9303 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009304 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305}
9306
9307/*
9308 * "getftime({fname})" function
9309 */
9310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009311f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009312 typval_T *argvars;
9313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314{
9315 char_u *fname;
9316 struct stat st;
9317
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009318 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319
9320 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009321 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009323 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324}
9325
9326/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009327 * "getftype({fname})" function
9328 */
9329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009330f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009331 typval_T *argvars;
9332 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009333{
9334 char_u *fname;
9335 struct stat st;
9336 char_u *type = NULL;
9337 char *t;
9338
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009339 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009340
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009341 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009342 if (mch_lstat((char *)fname, &st) >= 0)
9343 {
9344#ifdef S_ISREG
9345 if (S_ISREG(st.st_mode))
9346 t = "file";
9347 else if (S_ISDIR(st.st_mode))
9348 t = "dir";
9349# ifdef S_ISLNK
9350 else if (S_ISLNK(st.st_mode))
9351 t = "link";
9352# endif
9353# ifdef S_ISBLK
9354 else if (S_ISBLK(st.st_mode))
9355 t = "bdev";
9356# endif
9357# ifdef S_ISCHR
9358 else if (S_ISCHR(st.st_mode))
9359 t = "cdev";
9360# endif
9361# ifdef S_ISFIFO
9362 else if (S_ISFIFO(st.st_mode))
9363 t = "fifo";
9364# endif
9365# ifdef S_ISSOCK
9366 else if (S_ISSOCK(st.st_mode))
9367 t = "fifo";
9368# endif
9369 else
9370 t = "other";
9371#else
9372# ifdef S_IFMT
9373 switch (st.st_mode & S_IFMT)
9374 {
9375 case S_IFREG: t = "file"; break;
9376 case S_IFDIR: t = "dir"; break;
9377# ifdef S_IFLNK
9378 case S_IFLNK: t = "link"; break;
9379# endif
9380# ifdef S_IFBLK
9381 case S_IFBLK: t = "bdev"; break;
9382# endif
9383# ifdef S_IFCHR
9384 case S_IFCHR: t = "cdev"; break;
9385# endif
9386# ifdef S_IFIFO
9387 case S_IFIFO: t = "fifo"; break;
9388# endif
9389# ifdef S_IFSOCK
9390 case S_IFSOCK: t = "socket"; break;
9391# endif
9392 default: t = "other";
9393 }
9394# else
9395 if (mch_isdir(fname))
9396 t = "dir";
9397 else
9398 t = "file";
9399# endif
9400#endif
9401 type = vim_strsave((char_u *)t);
9402 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009403 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009404}
9405
9406/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009407 * "getline(lnum)" function
9408 */
9409 static void
9410f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009411 typval_T *argvars;
9412 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009413{
9414 linenr_T lnum;
9415 linenr_T end;
9416 char_u *p;
Bram Moolenaar33570922005-01-25 22:26:29 +00009417 list_T *l;
9418 listitem_T *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009419
9420 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009421 if (lnum < 0)
9422 rettv->vval.v_number = 0; /* failure; error message already given */
9423 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009424 {
9425 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9426 p = ml_get(lnum);
9427 else
9428 p = (char_u *)"";
9429
9430 rettv->v_type = VAR_STRING;
9431 rettv->vval.v_string = vim_strsave(p);
9432 }
9433 else
9434 {
9435 end = get_tv_lnum(&argvars[1]);
9436 if (end < lnum)
9437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009438 if (end >= 0) /* else: error message already given */
9439 EMSG(_(e_invrange));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009440 rettv->vval.v_number = 0;
9441 }
9442 else
9443 {
9444 l = list_alloc();
9445 if (l != NULL)
9446 {
9447 if (lnum < 1)
9448 lnum = 1;
9449 if (end > curbuf->b_ml.ml_line_count)
9450 end = curbuf->b_ml.ml_line_count;
9451 while (lnum <= end)
9452 {
9453 li = listitem_alloc();
9454 if (li == NULL)
9455 break;
9456 list_append(l, li);
9457 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009458 li->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009459 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
9460 }
9461 rettv->vval.v_list = l;
9462 rettv->v_type = VAR_LIST;
9463 ++l->lv_refcount;
9464 }
9465 }
9466 }
9467}
9468
9469/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009470 * "getqflist()" function
9471 */
9472/*ARGSUSED*/
9473 static void
9474f_getqflist(argvars, rettv)
9475 typval_T *argvars;
9476 typval_T *rettv;
9477{
9478#ifdef FEAT_QUICKFIX
9479 list_T *l;
9480#endif
9481
9482 rettv->vval.v_number = FALSE;
9483#ifdef FEAT_QUICKFIX
9484 l = list_alloc();
9485 if (l != NULL)
9486 {
9487 if (get_errorlist(l) != FAIL)
9488 {
9489 rettv->vval.v_list = l;
9490 rettv->v_type = VAR_LIST;
9491 ++l->lv_refcount;
9492 }
9493 else
9494 list_free(l);
9495 }
9496#endif
9497}
9498
9499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500 * "getreg()" function
9501 */
9502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009503f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009504 typval_T *argvars;
9505 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506{
9507 char_u *strregname;
9508 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009509 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009510 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009512 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009513 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009514 strregname = get_tv_string_chk(&argvars[0]);
9515 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009516 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009517 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009520 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 regname = (strregname == NULL ? '"' : *strregname);
9522 if (regname == 0)
9523 regname = '"';
9524
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009525 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009526 rettv->vval.v_string = error ? NULL :
9527 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528}
9529
9530/*
9531 * "getregtype()" function
9532 */
9533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009534f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009535 typval_T *argvars;
9536 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537{
9538 char_u *strregname;
9539 int regname;
9540 char_u buf[NUMBUFLEN + 2];
9541 long reglen = 0;
9542
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009543 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009544 {
9545 strregname = get_tv_string_chk(&argvars[0]);
9546 if (strregname == NULL) /* type error; errmsg already given */
9547 {
9548 rettv->v_type = VAR_STRING;
9549 rettv->vval.v_string = NULL;
9550 return;
9551 }
9552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 else
9554 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009555 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556
9557 regname = (strregname == NULL ? '"' : *strregname);
9558 if (regname == 0)
9559 regname = '"';
9560
9561 buf[0] = NUL;
9562 buf[1] = NUL;
9563 switch (get_reg_type(regname, &reglen))
9564 {
9565 case MLINE: buf[0] = 'V'; break;
9566 case MCHAR: buf[0] = 'v'; break;
9567#ifdef FEAT_VISUAL
9568 case MBLOCK:
9569 buf[0] = Ctrl_V;
9570 sprintf((char *)buf + 1, "%ld", reglen + 1);
9571 break;
9572#endif
9573 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009574 rettv->v_type = VAR_STRING;
9575 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009576}
9577
9578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 * "getwinposx()" function
9580 */
9581/*ARGSUSED*/
9582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009583f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009584 typval_T *argvars;
9585 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009587 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588#ifdef FEAT_GUI
9589 if (gui.in_use)
9590 {
9591 int x, y;
9592
9593 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009594 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 }
9596#endif
9597}
9598
9599/*
9600 * "getwinposy()" function
9601 */
9602/*ARGSUSED*/
9603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009604f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009605 typval_T *argvars;
9606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009608 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609#ifdef FEAT_GUI
9610 if (gui.in_use)
9611 {
9612 int x, y;
9613
9614 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009615 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 }
9617#endif
9618}
9619
9620/*
9621 * "getwinvar()" function
9622 */
9623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009624f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009625 typval_T *argvars;
9626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627{
9628 win_T *win, *oldcurwin;
9629 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009630 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009633 varname = get_tv_string_chk(&argvars[1]);
9634 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009636 rettv->v_type = VAR_STRING;
9637 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638
9639 if (win != NULL && varname != NULL)
9640 {
9641 if (*varname == '&') /* window-local-option */
9642 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009643 /* Set curwin to be our win, temporarily. Also set curbuf, so
9644 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 oldcurwin = curwin;
9646 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009647 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650
9651 /* restore previous notion of curwin */
9652 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009653 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 }
9655 else
9656 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009657 if (*varname == NUL)
9658 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9659 * scope prefix before the NUL byte is required by
9660 * find_var_in_ht(). */
9661 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009663 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009665 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 }
9667 }
9668
9669 --emsg_off;
9670}
9671
9672/*
9673 * "glob()" function
9674 */
9675 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009676f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009677 typval_T *argvars;
9678 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679{
9680 expand_T xpc;
9681
9682 ExpandInit(&xpc);
9683 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009684 rettv->v_type = VAR_STRING;
9685 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9687 ExpandCleanup(&xpc);
9688}
9689
9690/*
9691 * "globpath()" function
9692 */
9693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009694f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009695 typval_T *argvars;
9696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697{
9698 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009699 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009701 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009702 if (file == NULL)
9703 rettv->vval.v_string = NULL;
9704 else
9705 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706}
9707
9708/*
9709 * "has()" function
9710 */
9711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009712f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009713 typval_T *argvars;
9714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715{
9716 int i;
9717 char_u *name;
9718 int n = FALSE;
9719 static char *(has_list[]) =
9720 {
9721#ifdef AMIGA
9722 "amiga",
9723# ifdef FEAT_ARP
9724 "arp",
9725# endif
9726#endif
9727#ifdef __BEOS__
9728 "beos",
9729#endif
9730#ifdef MSDOS
9731# ifdef DJGPP
9732 "dos32",
9733# else
9734 "dos16",
9735# endif
9736#endif
9737#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9738 "mac",
9739#endif
9740#if defined(MACOS_X_UNIX)
9741 "macunix",
9742#endif
9743#ifdef OS2
9744 "os2",
9745#endif
9746#ifdef __QNX__
9747 "qnx",
9748#endif
9749#ifdef RISCOS
9750 "riscos",
9751#endif
9752#ifdef UNIX
9753 "unix",
9754#endif
9755#ifdef VMS
9756 "vms",
9757#endif
9758#ifdef WIN16
9759 "win16",
9760#endif
9761#ifdef WIN32
9762 "win32",
9763#endif
9764#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9765 "win32unix",
9766#endif
9767#ifdef WIN64
9768 "win64",
9769#endif
9770#ifdef EBCDIC
9771 "ebcdic",
9772#endif
9773#ifndef CASE_INSENSITIVE_FILENAME
9774 "fname_case",
9775#endif
9776#ifdef FEAT_ARABIC
9777 "arabic",
9778#endif
9779#ifdef FEAT_AUTOCMD
9780 "autocmd",
9781#endif
9782#ifdef FEAT_BEVAL
9783 "balloon_eval",
9784#endif
9785#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9786 "builtin_terms",
9787# ifdef ALL_BUILTIN_TCAPS
9788 "all_builtin_terms",
9789# endif
9790#endif
9791#ifdef FEAT_BYTEOFF
9792 "byte_offset",
9793#endif
9794#ifdef FEAT_CINDENT
9795 "cindent",
9796#endif
9797#ifdef FEAT_CLIENTSERVER
9798 "clientserver",
9799#endif
9800#ifdef FEAT_CLIPBOARD
9801 "clipboard",
9802#endif
9803#ifdef FEAT_CMDL_COMPL
9804 "cmdline_compl",
9805#endif
9806#ifdef FEAT_CMDHIST
9807 "cmdline_hist",
9808#endif
9809#ifdef FEAT_COMMENTS
9810 "comments",
9811#endif
9812#ifdef FEAT_CRYPT
9813 "cryptv",
9814#endif
9815#ifdef FEAT_CSCOPE
9816 "cscope",
9817#endif
9818#ifdef DEBUG
9819 "debug",
9820#endif
9821#ifdef FEAT_CON_DIALOG
9822 "dialog_con",
9823#endif
9824#ifdef FEAT_GUI_DIALOG
9825 "dialog_gui",
9826#endif
9827#ifdef FEAT_DIFF
9828 "diff",
9829#endif
9830#ifdef FEAT_DIGRAPHS
9831 "digraphs",
9832#endif
9833#ifdef FEAT_DND
9834 "dnd",
9835#endif
9836#ifdef FEAT_EMACS_TAGS
9837 "emacs_tags",
9838#endif
9839 "eval", /* always present, of course! */
9840#ifdef FEAT_EX_EXTRA
9841 "ex_extra",
9842#endif
9843#ifdef FEAT_SEARCH_EXTRA
9844 "extra_search",
9845#endif
9846#ifdef FEAT_FKMAP
9847 "farsi",
9848#endif
9849#ifdef FEAT_SEARCHPATH
9850 "file_in_path",
9851#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009852#if defined(UNIX) && !defined(USE_SYSTEM)
9853 "filterpipe",
9854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855#ifdef FEAT_FIND_ID
9856 "find_in_path",
9857#endif
9858#ifdef FEAT_FOLDING
9859 "folding",
9860#endif
9861#ifdef FEAT_FOOTER
9862 "footer",
9863#endif
9864#if !defined(USE_SYSTEM) && defined(UNIX)
9865 "fork",
9866#endif
9867#ifdef FEAT_GETTEXT
9868 "gettext",
9869#endif
9870#ifdef FEAT_GUI
9871 "gui",
9872#endif
9873#ifdef FEAT_GUI_ATHENA
9874# ifdef FEAT_GUI_NEXTAW
9875 "gui_neXtaw",
9876# else
9877 "gui_athena",
9878# endif
9879#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009880#ifdef FEAT_GUI_KDE
9881 "gui_kde",
9882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883#ifdef FEAT_GUI_GTK
9884 "gui_gtk",
9885# ifdef HAVE_GTK2
9886 "gui_gtk2",
9887# endif
9888#endif
9889#ifdef FEAT_GUI_MAC
9890 "gui_mac",
9891#endif
9892#ifdef FEAT_GUI_MOTIF
9893 "gui_motif",
9894#endif
9895#ifdef FEAT_GUI_PHOTON
9896 "gui_photon",
9897#endif
9898#ifdef FEAT_GUI_W16
9899 "gui_win16",
9900#endif
9901#ifdef FEAT_GUI_W32
9902 "gui_win32",
9903#endif
9904#ifdef FEAT_HANGULIN
9905 "hangul_input",
9906#endif
9907#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
9908 "iconv",
9909#endif
9910#ifdef FEAT_INS_EXPAND
9911 "insert_expand",
9912#endif
9913#ifdef FEAT_JUMPLIST
9914 "jumplist",
9915#endif
9916#ifdef FEAT_KEYMAP
9917 "keymap",
9918#endif
9919#ifdef FEAT_LANGMAP
9920 "langmap",
9921#endif
9922#ifdef FEAT_LIBCALL
9923 "libcall",
9924#endif
9925#ifdef FEAT_LINEBREAK
9926 "linebreak",
9927#endif
9928#ifdef FEAT_LISP
9929 "lispindent",
9930#endif
9931#ifdef FEAT_LISTCMDS
9932 "listcmds",
9933#endif
9934#ifdef FEAT_LOCALMAP
9935 "localmap",
9936#endif
9937#ifdef FEAT_MENU
9938 "menu",
9939#endif
9940#ifdef FEAT_SESSION
9941 "mksession",
9942#endif
9943#ifdef FEAT_MODIFY_FNAME
9944 "modify_fname",
9945#endif
9946#ifdef FEAT_MOUSE
9947 "mouse",
9948#endif
9949#ifdef FEAT_MOUSESHAPE
9950 "mouseshape",
9951#endif
9952#if defined(UNIX) || defined(VMS)
9953# ifdef FEAT_MOUSE_DEC
9954 "mouse_dec",
9955# endif
9956# ifdef FEAT_MOUSE_GPM
9957 "mouse_gpm",
9958# endif
9959# ifdef FEAT_MOUSE_JSB
9960 "mouse_jsbterm",
9961# endif
9962# ifdef FEAT_MOUSE_NET
9963 "mouse_netterm",
9964# endif
9965# ifdef FEAT_MOUSE_PTERM
9966 "mouse_pterm",
9967# endif
9968# ifdef FEAT_MOUSE_XTERM
9969 "mouse_xterm",
9970# endif
9971#endif
9972#ifdef FEAT_MBYTE
9973 "multi_byte",
9974#endif
9975#ifdef FEAT_MBYTE_IME
9976 "multi_byte_ime",
9977#endif
9978#ifdef FEAT_MULTI_LANG
9979 "multi_lang",
9980#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009981#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +00009982#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009983 "mzscheme",
9984#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986#ifdef FEAT_OLE
9987 "ole",
9988#endif
9989#ifdef FEAT_OSFILETYPE
9990 "osfiletype",
9991#endif
9992#ifdef FEAT_PATH_EXTRA
9993 "path_extra",
9994#endif
9995#ifdef FEAT_PERL
9996#ifndef DYNAMIC_PERL
9997 "perl",
9998#endif
9999#endif
10000#ifdef FEAT_PYTHON
10001#ifndef DYNAMIC_PYTHON
10002 "python",
10003#endif
10004#endif
10005#ifdef FEAT_POSTSCRIPT
10006 "postscript",
10007#endif
10008#ifdef FEAT_PRINTER
10009 "printer",
10010#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010011#ifdef FEAT_PROFILE
10012 "profile",
10013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014#ifdef FEAT_QUICKFIX
10015 "quickfix",
10016#endif
10017#ifdef FEAT_RIGHTLEFT
10018 "rightleft",
10019#endif
10020#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10021 "ruby",
10022#endif
10023#ifdef FEAT_SCROLLBIND
10024 "scrollbind",
10025#endif
10026#ifdef FEAT_CMDL_INFO
10027 "showcmd",
10028 "cmdline_info",
10029#endif
10030#ifdef FEAT_SIGNS
10031 "signs",
10032#endif
10033#ifdef FEAT_SMARTINDENT
10034 "smartindent",
10035#endif
10036#ifdef FEAT_SNIFF
10037 "sniff",
10038#endif
10039#ifdef FEAT_STL_OPT
10040 "statusline",
10041#endif
10042#ifdef FEAT_SUN_WORKSHOP
10043 "sun_workshop",
10044#endif
10045#ifdef FEAT_NETBEANS_INTG
10046 "netbeans_intg",
10047#endif
10048#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010049 "spell",
10050#endif
10051#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052 "syntax",
10053#endif
10054#if defined(USE_SYSTEM) || !defined(UNIX)
10055 "system",
10056#endif
10057#ifdef FEAT_TAG_BINS
10058 "tag_binary",
10059#endif
10060#ifdef FEAT_TAG_OLDSTATIC
10061 "tag_old_static",
10062#endif
10063#ifdef FEAT_TAG_ANYWHITE
10064 "tag_any_white",
10065#endif
10066#ifdef FEAT_TCL
10067# ifndef DYNAMIC_TCL
10068 "tcl",
10069# endif
10070#endif
10071#ifdef TERMINFO
10072 "terminfo",
10073#endif
10074#ifdef FEAT_TERMRESPONSE
10075 "termresponse",
10076#endif
10077#ifdef FEAT_TEXTOBJ
10078 "textobjects",
10079#endif
10080#ifdef HAVE_TGETENT
10081 "tgetent",
10082#endif
10083#ifdef FEAT_TITLE
10084 "title",
10085#endif
10086#ifdef FEAT_TOOLBAR
10087 "toolbar",
10088#endif
10089#ifdef FEAT_USR_CMDS
10090 "user-commands", /* was accidentally included in 5.4 */
10091 "user_commands",
10092#endif
10093#ifdef FEAT_VIMINFO
10094 "viminfo",
10095#endif
10096#ifdef FEAT_VERTSPLIT
10097 "vertsplit",
10098#endif
10099#ifdef FEAT_VIRTUALEDIT
10100 "virtualedit",
10101#endif
10102#ifdef FEAT_VISUAL
10103 "visual",
10104#endif
10105#ifdef FEAT_VISUALEXTRA
10106 "visualextra",
10107#endif
10108#ifdef FEAT_VREPLACE
10109 "vreplace",
10110#endif
10111#ifdef FEAT_WILDIGN
10112 "wildignore",
10113#endif
10114#ifdef FEAT_WILDMENU
10115 "wildmenu",
10116#endif
10117#ifdef FEAT_WINDOWS
10118 "windows",
10119#endif
10120#ifdef FEAT_WAK
10121 "winaltkeys",
10122#endif
10123#ifdef FEAT_WRITEBACKUP
10124 "writebackup",
10125#endif
10126#ifdef FEAT_XIM
10127 "xim",
10128#endif
10129#ifdef FEAT_XFONTSET
10130 "xfontset",
10131#endif
10132#ifdef USE_XSMP
10133 "xsmp",
10134#endif
10135#ifdef USE_XSMP_INTERACT
10136 "xsmp_interact",
10137#endif
10138#ifdef FEAT_XCLIPBOARD
10139 "xterm_clipboard",
10140#endif
10141#ifdef FEAT_XTERM_SAVE
10142 "xterm_save",
10143#endif
10144#if defined(UNIX) && defined(FEAT_X11)
10145 "X11",
10146#endif
10147 NULL
10148 };
10149
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010150 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 for (i = 0; has_list[i] != NULL; ++i)
10152 if (STRICMP(name, has_list[i]) == 0)
10153 {
10154 n = TRUE;
10155 break;
10156 }
10157
10158 if (n == FALSE)
10159 {
10160 if (STRNICMP(name, "patch", 5) == 0)
10161 n = has_patch(atoi((char *)name + 5));
10162 else if (STRICMP(name, "vim_starting") == 0)
10163 n = (starting != 0);
10164#ifdef DYNAMIC_TCL
10165 else if (STRICMP(name, "tcl") == 0)
10166 n = tcl_enabled(FALSE);
10167#endif
10168#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10169 else if (STRICMP(name, "iconv") == 0)
10170 n = iconv_enabled(FALSE);
10171#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010172#ifdef DYNAMIC_MZSCHEME
10173 else if (STRICMP(name, "mzscheme") == 0)
10174 n = mzscheme_enabled(FALSE);
10175#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010176#ifdef DYNAMIC_RUBY
10177 else if (STRICMP(name, "ruby") == 0)
10178 n = ruby_enabled(FALSE);
10179#endif
10180#ifdef DYNAMIC_PYTHON
10181 else if (STRICMP(name, "python") == 0)
10182 n = python_enabled(FALSE);
10183#endif
10184#ifdef DYNAMIC_PERL
10185 else if (STRICMP(name, "perl") == 0)
10186 n = perl_enabled(FALSE);
10187#endif
10188#ifdef FEAT_GUI
10189 else if (STRICMP(name, "gui_running") == 0)
10190 n = (gui.in_use || gui.starting);
10191# ifdef FEAT_GUI_W32
10192 else if (STRICMP(name, "gui_win32s") == 0)
10193 n = gui_is_win32s();
10194# endif
10195# ifdef FEAT_BROWSE
10196 else if (STRICMP(name, "browse") == 0)
10197 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10198# endif
10199#endif
10200#ifdef FEAT_SYN_HL
10201 else if (STRICMP(name, "syntax_items") == 0)
10202 n = syntax_present(curbuf);
10203#endif
10204#if defined(WIN3264)
10205 else if (STRICMP(name, "win95") == 0)
10206 n = mch_windows95();
10207#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010208#ifdef FEAT_NETBEANS_INTG
10209 else if (STRICMP(name, "netbeans_enabled") == 0)
10210 n = usingNetbeans;
10211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 }
10213
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010214 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215}
10216
10217/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010218 * "has_key()" function
10219 */
10220 static void
10221f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010222 typval_T *argvars;
10223 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010224{
10225 rettv->vval.v_number = 0;
10226 if (argvars[0].v_type != VAR_DICT)
10227 {
10228 EMSG(_(e_dictreq));
10229 return;
10230 }
10231 if (argvars[0].vval.v_dict == NULL)
10232 return;
10233
10234 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010235 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010236}
10237
10238/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010239 * "hasmapto()" function
10240 */
10241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010242f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010243 typval_T *argvars;
10244 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010245{
10246 char_u *name;
10247 char_u *mode;
10248 char_u buf[NUMBUFLEN];
10249
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010250 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010251 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010252 mode = (char_u *)"nvo";
10253 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010254 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255
10256 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010257 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010259 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260}
10261
10262/*
10263 * "histadd()" function
10264 */
10265/*ARGSUSED*/
10266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010267f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010268 typval_T *argvars;
10269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270{
10271#ifdef FEAT_CMDHIST
10272 int histype;
10273 char_u *str;
10274 char_u buf[NUMBUFLEN];
10275#endif
10276
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010277 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 if (check_restricted() || check_secure())
10279 return;
10280#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010281 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10282 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 if (histype >= 0)
10284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010285 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286 if (*str != NUL)
10287 {
10288 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010289 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 return;
10291 }
10292 }
10293#endif
10294}
10295
10296/*
10297 * "histdel()" function
10298 */
10299/*ARGSUSED*/
10300 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010301f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010302 typval_T *argvars;
10303 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010304{
10305#ifdef FEAT_CMDHIST
10306 int n;
10307 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010308 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010310 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10311 if (str == NULL)
10312 n = 0;
10313 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010315 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010316 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010318 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010319 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 else
10321 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010322 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010323 get_tv_string_buf(&argvars[1], buf));
10324 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010326 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327#endif
10328}
10329
10330/*
10331 * "histget()" function
10332 */
10333/*ARGSUSED*/
10334 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010335f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010336 typval_T *argvars;
10337 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338{
10339#ifdef FEAT_CMDHIST
10340 int type;
10341 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010342 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010344 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10345 if (str == NULL)
10346 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010348 {
10349 type = get_histtype(str);
10350 if (argvars[1].v_type == VAR_UNKNOWN)
10351 idx = get_history_idx(type);
10352 else
10353 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10354 /* -1 on type error */
10355 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010358 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010360 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010361}
10362
10363/*
10364 * "histnr()" function
10365 */
10366/*ARGSUSED*/
10367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010368f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010369 typval_T *argvars;
10370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371{
10372 int i;
10373
10374#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010375 char_u *history = get_tv_string_chk(&argvars[0]);
10376
10377 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010378 if (i >= HIST_CMD && i < HIST_COUNT)
10379 i = get_history_idx(i);
10380 else
10381#endif
10382 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010383 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384}
10385
10386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 * "highlightID(name)" function
10388 */
10389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010390f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010391 typval_T *argvars;
10392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010394 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395}
10396
10397/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010398 * "highlight_exists()" function
10399 */
10400 static void
10401f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010402 typval_T *argvars;
10403 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010404{
10405 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10406}
10407
10408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409 * "hostname()" function
10410 */
10411/*ARGSUSED*/
10412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010413f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010414 typval_T *argvars;
10415 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416{
10417 char_u hostname[256];
10418
10419 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010420 rettv->v_type = VAR_STRING;
10421 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422}
10423
10424/*
10425 * iconv() function
10426 */
10427/*ARGSUSED*/
10428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010429f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010430 typval_T *argvars;
10431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432{
10433#ifdef FEAT_MBYTE
10434 char_u buf1[NUMBUFLEN];
10435 char_u buf2[NUMBUFLEN];
10436 char_u *from, *to, *str;
10437 vimconv_T vimconv;
10438#endif
10439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010440 rettv->v_type = VAR_STRING;
10441 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442
10443#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010444 str = get_tv_string(&argvars[0]);
10445 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10446 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 vimconv.vc_type = CONV_NONE;
10448 convert_setup(&vimconv, from, to);
10449
10450 /* If the encodings are equal, no conversion needed. */
10451 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010452 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010454 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455
10456 convert_setup(&vimconv, NULL, NULL);
10457 vim_free(from);
10458 vim_free(to);
10459#endif
10460}
10461
10462/*
10463 * "indent()" function
10464 */
10465 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010466f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010467 typval_T *argvars;
10468 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469{
10470 linenr_T lnum;
10471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010472 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010474 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010476 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477}
10478
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010479/*
10480 * "index()" function
10481 */
10482 static void
10483f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010484 typval_T *argvars;
10485 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010486{
Bram Moolenaar33570922005-01-25 22:26:29 +000010487 list_T *l;
10488 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010489 long idx = 0;
10490 int ic = FALSE;
10491
10492 rettv->vval.v_number = -1;
10493 if (argvars[0].v_type != VAR_LIST)
10494 {
10495 EMSG(_(e_listreq));
10496 return;
10497 }
10498 l = argvars[0].vval.v_list;
10499 if (l != NULL)
10500 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010501 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010502 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010503 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010504 int error = FALSE;
10505
Bram Moolenaar758711c2005-02-02 23:11:38 +000010506 /* Start at specified item. Use the cached index that list_find()
10507 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010508 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010509 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010510 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010511 ic = get_tv_number_chk(&argvars[3], &error);
10512 if (error)
10513 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010514 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010515
Bram Moolenaar758711c2005-02-02 23:11:38 +000010516 for ( ; item != NULL; item = item->li_next, ++idx)
10517 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010518 {
10519 rettv->vval.v_number = idx;
10520 break;
10521 }
10522 }
10523}
10524
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525static int inputsecret_flag = 0;
10526
10527/*
10528 * "input()" function
10529 * Also handles inputsecret() when inputsecret is set.
10530 */
10531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010532f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010533 typval_T *argvars;
10534 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010535{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010536 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537 char_u *p = NULL;
10538 int c;
10539 char_u buf[NUMBUFLEN];
10540 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010541 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010542
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010543 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010544
10545#ifdef NO_CONSOLE_INPUT
10546 /* While starting up, there is no place to enter text. */
10547 if (no_console_input())
10548 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010549 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 return;
10551 }
10552#endif
10553
10554 cmd_silent = FALSE; /* Want to see the prompt. */
10555 if (prompt != NULL)
10556 {
10557 /* Only the part of the message after the last NL is considered as
10558 * prompt for the command line */
10559 p = vim_strrchr(prompt, '\n');
10560 if (p == NULL)
10561 p = prompt;
10562 else
10563 {
10564 ++p;
10565 c = *p;
10566 *p = NUL;
10567 msg_start();
10568 msg_clr_eos();
10569 msg_puts_attr(prompt, echo_attr);
10570 msg_didout = FALSE;
10571 msg_starthere();
10572 *p = c;
10573 }
10574 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010576 if (argvars[1].v_type != VAR_UNKNOWN)
10577 {
10578 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10579 if (defstr != NULL)
10580 stuffReadbuffSpec(defstr);
10581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010583 if (defstr != NULL)
10584 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
10586
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010587 /* since the user typed this, no need to wait for return */
10588 need_wait_return = FALSE;
10589 msg_didout = FALSE;
10590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 cmd_silent = cmd_silent_save;
10592}
10593
10594/*
10595 * "inputdialog()" function
10596 */
10597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010598f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010599 typval_T *argvars;
10600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601{
10602#if defined(FEAT_GUI_TEXTDIALOG)
10603 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10604 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10605 {
10606 char_u *message;
10607 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010608 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010609
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010610 message = get_tv_string_chk(&argvars[0]);
10611 if (argvars[1].v_type != VAR_UNKNOWN
10612 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010614 STRNCPY(IObuff, defstr, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 IObuff[IOSIZE - 1] = NUL;
10616 }
10617 else
10618 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010619 if (message != NULL && defstr != NULL
10620 && do_dialog(VIM_QUESTION, NULL, message,
10621 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010622 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623 else
10624 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010625 if (message != NULL && defstr != NULL
10626 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010627 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010628 rettv->vval.v_string = vim_strsave(
10629 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010631 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010632 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010633 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634 }
10635 else
10636#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010637 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638}
10639
10640static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10641
10642/*
10643 * "inputrestore()" function
10644 */
10645/*ARGSUSED*/
10646 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010647f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010648 typval_T *argvars;
10649 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650{
10651 if (ga_userinput.ga_len > 0)
10652 {
10653 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10655 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010656 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 }
10658 else if (p_verbose > 1)
10659 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000010660 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010661 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 }
10663}
10664
10665/*
10666 * "inputsave()" function
10667 */
10668/*ARGSUSED*/
10669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010670f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010671 typval_T *argvars;
10672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673{
10674 /* Add an entry to the stack of typehead storage. */
10675 if (ga_grow(&ga_userinput, 1) == OK)
10676 {
10677 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10678 + ga_userinput.ga_len);
10679 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010680 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010681 }
10682 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010683 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684}
10685
10686/*
10687 * "inputsecret()" function
10688 */
10689 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010690f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010691 typval_T *argvars;
10692 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010693{
10694 ++cmdline_star;
10695 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010696 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 --cmdline_star;
10698 --inputsecret_flag;
10699}
10700
10701/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010702 * "insert()" function
10703 */
10704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010705f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010706 typval_T *argvars;
10707 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010708{
10709 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010710 listitem_T *item;
10711 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010712 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010713
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010714 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010715 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010716 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010717 else if ((l = argvars[0].vval.v_list) != NULL
10718 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010719 {
10720 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010721 before = get_tv_number_chk(&argvars[2], &error);
10722 if (error)
10723 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010724
Bram Moolenaar758711c2005-02-02 23:11:38 +000010725 if (before == l->lv_len)
10726 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010727 else
10728 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010729 item = list_find(l, before);
10730 if (item == NULL)
10731 {
10732 EMSGN(_(e_listidx), before);
10733 l = NULL;
10734 }
10735 }
10736 if (l != NULL)
10737 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010738 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010739 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010740 }
10741 }
10742}
10743
10744/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 * "isdirectory()" function
10746 */
10747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010748f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010749 typval_T *argvars;
10750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753}
10754
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010755/*
10756 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
10757 * it refers to a List or Dictionary that is locked.
10758 */
10759 static int
10760tv_islocked(tv)
10761 typval_T *tv;
10762{
10763 return (tv->v_lock & VAR_LOCKED)
10764 || (tv->v_type == VAR_LIST
10765 && tv->vval.v_list != NULL
10766 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
10767 || (tv->v_type == VAR_DICT
10768 && tv->vval.v_dict != NULL
10769 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
10770}
10771
10772/*
10773 * "islocked()" function
10774 */
10775 static void
10776f_islocked(argvars, rettv)
10777 typval_T *argvars;
10778 typval_T *rettv;
10779{
10780 lval_T lv;
10781 char_u *end;
10782 dictitem_T *di;
10783
10784 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000010785 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
10786 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010787 if (end != NULL && lv.ll_name != NULL)
10788 {
10789 if (*end != NUL)
10790 EMSG(_(e_trailing));
10791 else
10792 {
10793 if (lv.ll_tv == NULL)
10794 {
10795 if (check_changedtick(lv.ll_name))
10796 rettv->vval.v_number = 1; /* always locked */
10797 else
10798 {
10799 di = find_var(lv.ll_name, NULL);
10800 if (di != NULL)
10801 {
10802 /* Consider a variable locked when:
10803 * 1. the variable itself is locked
10804 * 2. the value of the variable is locked.
10805 * 3. the List or Dict value is locked.
10806 */
10807 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10808 || tv_islocked(&di->di_tv));
10809 }
10810 }
10811 }
10812 else if (lv.ll_range)
10813 EMSG(_("E745: Range not allowed"));
10814 else if (lv.ll_newkey != NULL)
10815 EMSG2(_(e_dictkey), lv.ll_newkey);
10816 else if (lv.ll_list != NULL)
10817 /* List item. */
10818 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10819 else
10820 /* Dictionary item. */
10821 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10822 }
10823 }
10824
10825 clear_lval(&lv);
10826}
10827
Bram Moolenaar33570922005-01-25 22:26:29 +000010828static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010829
10830/*
10831 * Turn a dict into a list:
10832 * "what" == 0: list of keys
10833 * "what" == 1: list of values
10834 * "what" == 2: list of items
10835 */
10836 static void
10837dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010838 typval_T *argvars;
10839 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010840 int what;
10841{
Bram Moolenaar33570922005-01-25 22:26:29 +000010842 list_T *l;
10843 list_T *l2;
10844 dictitem_T *di;
10845 hashitem_T *hi;
10846 listitem_T *li;
10847 listitem_T *li2;
10848 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010849 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010850
10851 rettv->vval.v_number = 0;
10852 if (argvars[0].v_type != VAR_DICT)
10853 {
10854 EMSG(_(e_dictreq));
10855 return;
10856 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010857 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010858 return;
10859
10860 l = list_alloc();
10861 if (l == NULL)
10862 return;
10863 rettv->v_type = VAR_LIST;
10864 rettv->vval.v_list = l;
10865 ++l->lv_refcount;
10866
Bram Moolenaar33570922005-01-25 22:26:29 +000010867 todo = d->dv_hashtab.ht_used;
10868 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010869 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010870 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000010871 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010872 --todo;
10873 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010874
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010875 li = listitem_alloc();
10876 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010877 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010878 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010879
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010880 if (what == 0)
10881 {
10882 /* keys() */
10883 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010884 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010885 li->li_tv.vval.v_string = vim_strsave(di->di_key);
10886 }
10887 else if (what == 1)
10888 {
10889 /* values() */
10890 copy_tv(&di->di_tv, &li->li_tv);
10891 }
10892 else
10893 {
10894 /* items() */
10895 l2 = list_alloc();
10896 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010897 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010898 li->li_tv.vval.v_list = l2;
10899 if (l2 == NULL)
10900 break;
10901 ++l2->lv_refcount;
10902
10903 li2 = listitem_alloc();
10904 if (li2 == NULL)
10905 break;
10906 list_append(l2, li2);
10907 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010908 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010909 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
10910
10911 li2 = listitem_alloc();
10912 if (li2 == NULL)
10913 break;
10914 list_append(l2, li2);
10915 copy_tv(&di->di_tv, &li2->li_tv);
10916 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000010917 }
10918 }
10919}
10920
10921/*
10922 * "items(dict)" function
10923 */
10924 static void
10925f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010926 typval_T *argvars;
10927 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010928{
10929 dict_list(argvars, rettv, 2);
10930}
10931
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010933 * "join()" function
10934 */
10935 static void
10936f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010937 typval_T *argvars;
10938 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010939{
10940 garray_T ga;
10941 char_u *sep;
10942
10943 rettv->vval.v_number = 0;
10944 if (argvars[0].v_type != VAR_LIST)
10945 {
10946 EMSG(_(e_listreq));
10947 return;
10948 }
10949 if (argvars[0].vval.v_list == NULL)
10950 return;
10951 if (argvars[1].v_type == VAR_UNKNOWN)
10952 sep = (char_u *)" ";
10953 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010954 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010955
10956 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010957
10958 if (sep != NULL)
10959 {
10960 ga_init2(&ga, (int)sizeof(char), 80);
10961 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
10962 ga_append(&ga, NUL);
10963 rettv->vval.v_string = (char_u *)ga.ga_data;
10964 }
10965 else
10966 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010967}
10968
10969/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000010970 * "keys()" function
10971 */
10972 static void
10973f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010974 typval_T *argvars;
10975 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010976{
10977 dict_list(argvars, rettv, 0);
10978}
10979
10980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 * "last_buffer_nr()" function.
10982 */
10983/*ARGSUSED*/
10984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010985f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010986 typval_T *argvars;
10987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988{
10989 int n = 0;
10990 buf_T *buf;
10991
10992 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10993 if (n < buf->b_fnum)
10994 n = buf->b_fnum;
10995
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010996 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010997}
10998
10999/*
11000 * "len()" function
11001 */
11002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011003f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011004 typval_T *argvars;
11005 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011006{
11007 switch (argvars[0].v_type)
11008 {
11009 case VAR_STRING:
11010 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011011 rettv->vval.v_number = (varnumber_T)STRLEN(
11012 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011013 break;
11014 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011015 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011016 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011017 case VAR_DICT:
11018 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11019 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011020 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011021 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011022 break;
11023 }
11024}
11025
Bram Moolenaar33570922005-01-25 22:26:29 +000011026static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011027
11028 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011029libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011030 typval_T *argvars;
11031 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011032 int type;
11033{
11034#ifdef FEAT_LIBCALL
11035 char_u *string_in;
11036 char_u **string_result;
11037 int nr_result;
11038#endif
11039
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011040 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011041 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011042 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011043 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011044 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011045
11046 if (check_restricted() || check_secure())
11047 return;
11048
11049#ifdef FEAT_LIBCALL
11050 /* The first two args must be strings, otherwise its meaningless */
11051 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11052 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011053 string_in = NULL;
11054 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011055 string_in = argvars[2].vval.v_string;
11056 if (type == VAR_NUMBER)
11057 string_result = NULL;
11058 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011059 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011060 if (mch_libcall(argvars[0].vval.v_string,
11061 argvars[1].vval.v_string,
11062 string_in,
11063 argvars[2].vval.v_number,
11064 string_result,
11065 &nr_result) == OK
11066 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011067 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011068 }
11069#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070}
11071
11072/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011073 * "libcall()" function
11074 */
11075 static void
11076f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011077 typval_T *argvars;
11078 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011079{
11080 libcall_common(argvars, rettv, VAR_STRING);
11081}
11082
11083/*
11084 * "libcallnr()" function
11085 */
11086 static void
11087f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011088 typval_T *argvars;
11089 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011090{
11091 libcall_common(argvars, rettv, VAR_NUMBER);
11092}
11093
11094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095 * "line(string)" function
11096 */
11097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011098f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011099 typval_T *argvars;
11100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101{
11102 linenr_T lnum = 0;
11103 pos_T *fp;
11104
11105 fp = var2fpos(&argvars[0], TRUE);
11106 if (fp != NULL)
11107 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011108 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109}
11110
11111/*
11112 * "line2byte(lnum)" function
11113 */
11114/*ARGSUSED*/
11115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011116f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011117 typval_T *argvars;
11118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119{
11120#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011121 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011122#else
11123 linenr_T lnum;
11124
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011125 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011127 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011128 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011129 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11130 if (rettv->vval.v_number >= 0)
11131 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132#endif
11133}
11134
11135/*
11136 * "lispindent(lnum)" function
11137 */
11138 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011139f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011140 typval_T *argvars;
11141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011142{
11143#ifdef FEAT_LISP
11144 pos_T pos;
11145 linenr_T lnum;
11146
11147 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011148 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11150 {
11151 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011152 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153 curwin->w_cursor = pos;
11154 }
11155 else
11156#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011157 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158}
11159
11160/*
11161 * "localtime()" function
11162 */
11163/*ARGSUSED*/
11164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011165f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011166 typval_T *argvars;
11167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011168{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011169 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011170}
11171
Bram Moolenaar33570922005-01-25 22:26:29 +000011172static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011173
11174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011175get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011176 typval_T *argvars;
11177 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 int exact;
11179{
11180 char_u *keys;
11181 char_u *which;
11182 char_u buf[NUMBUFLEN];
11183 char_u *keys_buf = NULL;
11184 char_u *rhs;
11185 int mode;
11186 garray_T ga;
11187
11188 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011189 rettv->v_type = VAR_STRING;
11190 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011192 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 if (*keys == NUL)
11194 return;
11195
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011196 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011197 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011198 else
11199 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011200 if (which == NULL)
11201 return;
11202
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203 mode = get_map_mode(&which, 0);
11204
11205 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011206 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207 vim_free(keys_buf);
11208 if (rhs != NULL)
11209 {
11210 ga_init(&ga);
11211 ga.ga_itemsize = 1;
11212 ga.ga_growsize = 40;
11213
11214 while (*rhs != NUL)
11215 ga_concat(&ga, str2special(&rhs, FALSE));
11216
11217 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219 }
11220}
11221
11222/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011223 * "map()" function
11224 */
11225 static void
11226f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011227 typval_T *argvars;
11228 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011229{
11230 filter_map(argvars, rettv, TRUE);
11231}
11232
11233/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011234 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011235 */
11236 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011237f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011238 typval_T *argvars;
11239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011241 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242}
11243
11244/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011245 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246 */
11247 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011248f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011249 typval_T *argvars;
11250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011252 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253}
11254
Bram Moolenaar33570922005-01-25 22:26:29 +000011255static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256
11257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011258find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011259 typval_T *argvars;
11260 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 int type;
11262{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011263 char_u *str = NULL;
11264 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265 char_u *pat;
11266 regmatch_T regmatch;
11267 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011268 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269 char_u *save_cpo;
11270 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011271 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011272 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011273 list_T *l = NULL;
11274 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011275 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011276 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011277
11278 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11279 save_cpo = p_cpo;
11280 p_cpo = (char_u *)"";
11281
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011282 rettv->vval.v_number = -1;
11283 if (type == 3)
11284 {
11285 /* return empty list when there are no matches */
11286 if ((rettv->vval.v_list = list_alloc()) == NULL)
11287 goto theend;
11288 rettv->v_type = VAR_LIST;
11289 ++rettv->vval.v_list->lv_refcount;
11290 }
11291 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011293 rettv->v_type = VAR_STRING;
11294 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011297 if (argvars[0].v_type == VAR_LIST)
11298 {
11299 if ((l = argvars[0].vval.v_list) == NULL)
11300 goto theend;
11301 li = l->lv_first;
11302 }
11303 else
11304 expr = str = get_tv_string(&argvars[0]);
11305
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011306 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11307 if (pat == NULL)
11308 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011309
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011310 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011312 int error = FALSE;
11313
11314 start = get_tv_number_chk(&argvars[2], &error);
11315 if (error)
11316 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011317 if (l != NULL)
11318 {
11319 li = list_find(l, start);
11320 if (li == NULL)
11321 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011322 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011323 }
11324 else
11325 {
11326 if (start < 0)
11327 start = 0;
11328 if (start > (long)STRLEN(str))
11329 goto theend;
11330 str += start;
11331 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011332
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011333 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011334 nth = get_tv_number_chk(&argvars[3], &error);
11335 if (error)
11336 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337 }
11338
11339 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11340 if (regmatch.regprog != NULL)
11341 {
11342 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011343
11344 while (1)
11345 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011346 if (l != NULL)
11347 {
11348 if (li == NULL)
11349 {
11350 match = FALSE;
11351 break;
11352 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011353 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011354 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011355 if (str == NULL)
11356 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011357 }
11358
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011359 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011360
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011361 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011362 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011363 if (l == NULL && !match)
11364 break;
11365
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011366 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011367 if (l != NULL)
11368 {
11369 li = li->li_next;
11370 ++idx;
11371 }
11372 else
11373 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011374#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011375 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011376#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011377 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011378#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011379 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011380 }
11381
11382 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011384 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011385 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011386 int i;
11387
11388 /* return list with matched string and submatches */
11389 for (i = 0; i < NSUBEXP; ++i)
11390 {
11391 if (regmatch.endp[i] == NULL)
11392 break;
11393 li = listitem_alloc();
11394 if (li == NULL)
11395 break;
11396 li->li_tv.v_type = VAR_STRING;
11397 li->li_tv.v_lock = 0;
11398 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
11399 (int)(regmatch.endp[i] - regmatch.startp[i]));
11400 list_append(rettv->vval.v_list, li);
11401 }
11402 }
11403 else if (type == 2)
11404 {
11405 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011406 if (l != NULL)
11407 copy_tv(&li->li_tv, rettv);
11408 else
11409 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011411 }
11412 else if (l != NULL)
11413 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414 else
11415 {
11416 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011417 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011418 (varnumber_T)(regmatch.startp[0] - str);
11419 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011420 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011421 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011422 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011423 }
11424 }
11425 vim_free(regmatch.regprog);
11426 }
11427
11428theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011429 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011430 p_cpo = save_cpo;
11431}
11432
11433/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011434 * "match()" function
11435 */
11436 static void
11437f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011438 typval_T *argvars;
11439 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011440{
11441 find_some_match(argvars, rettv, 1);
11442}
11443
11444/*
11445 * "matchend()" function
11446 */
11447 static void
11448f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011449 typval_T *argvars;
11450 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011451{
11452 find_some_match(argvars, rettv, 0);
11453}
11454
11455/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011456 * "matchlist()" function
11457 */
11458 static void
11459f_matchlist(argvars, rettv)
11460 typval_T *argvars;
11461 typval_T *rettv;
11462{
11463 find_some_match(argvars, rettv, 3);
11464}
11465
11466/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011467 * "matchstr()" function
11468 */
11469 static void
11470f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011471 typval_T *argvars;
11472 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011473{
11474 find_some_match(argvars, rettv, 2);
11475}
11476
Bram Moolenaar33570922005-01-25 22:26:29 +000011477static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011478
11479 static void
11480max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011481 typval_T *argvars;
11482 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011483 int domax;
11484{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011485 long n = 0;
11486 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011487 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011488
11489 if (argvars[0].v_type == VAR_LIST)
11490 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011491 list_T *l;
11492 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011493
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011494 l = argvars[0].vval.v_list;
11495 if (l != NULL)
11496 {
11497 li = l->lv_first;
11498 if (li != NULL)
11499 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011500 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011501 while (1)
11502 {
11503 li = li->li_next;
11504 if (li == NULL)
11505 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011506 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011507 if (domax ? i > n : i < n)
11508 n = i;
11509 }
11510 }
11511 }
11512 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011513 else if (argvars[0].v_type == VAR_DICT)
11514 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011515 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011516 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011517 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011518 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011519
11520 d = argvars[0].vval.v_dict;
11521 if (d != NULL)
11522 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011523 todo = d->dv_hashtab.ht_used;
11524 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011525 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011526 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011527 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011528 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011529 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011530 if (first)
11531 {
11532 n = i;
11533 first = FALSE;
11534 }
11535 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011536 n = i;
11537 }
11538 }
11539 }
11540 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011541 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011542 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011543 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011544}
11545
11546/*
11547 * "max()" function
11548 */
11549 static void
11550f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011551 typval_T *argvars;
11552 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011553{
11554 max_min(argvars, rettv, TRUE);
11555}
11556
11557/*
11558 * "min()" function
11559 */
11560 static void
11561f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011562 typval_T *argvars;
11563 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011564{
11565 max_min(argvars, rettv, FALSE);
11566}
11567
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011568static int mkdir_recurse __ARGS((char_u *dir, int prot));
11569
11570/*
11571 * Create the directory in which "dir" is located, and higher levels when
11572 * needed.
11573 */
11574 static int
11575mkdir_recurse(dir, prot)
11576 char_u *dir;
11577 int prot;
11578{
11579 char_u *p;
11580 char_u *updir;
11581 int r = FAIL;
11582
11583 /* Get end of directory name in "dir".
11584 * We're done when it's "/" or "c:/". */
11585 p = gettail_sep(dir);
11586 if (p <= get_past_head(dir))
11587 return OK;
11588
11589 /* If the directory exists we're done. Otherwise: create it.*/
11590 updir = vim_strnsave(dir, (int)(p - dir));
11591 if (updir == NULL)
11592 return FAIL;
11593 if (mch_isdir(updir))
11594 r = OK;
11595 else if (mkdir_recurse(updir, prot) == OK)
11596 r = vim_mkdir_emsg(updir, prot);
11597 vim_free(updir);
11598 return r;
11599}
11600
11601#ifdef vim_mkdir
11602/*
11603 * "mkdir()" function
11604 */
11605 static void
11606f_mkdir(argvars, rettv)
11607 typval_T *argvars;
11608 typval_T *rettv;
11609{
11610 char_u *dir;
11611 char_u buf[NUMBUFLEN];
11612 int prot = 0755;
11613
11614 rettv->vval.v_number = FAIL;
11615 if (check_restricted() || check_secure())
11616 return;
11617
11618 dir = get_tv_string_buf(&argvars[0], buf);
11619 if (argvars[1].v_type != VAR_UNKNOWN)
11620 {
11621 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011622 prot = get_tv_number_chk(&argvars[2], NULL);
11623 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011624 mkdir_recurse(dir, prot);
11625 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011626 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011627}
11628#endif
11629
Bram Moolenaar0d660222005-01-07 21:51:51 +000011630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631 * "mode()" function
11632 */
11633/*ARGSUSED*/
11634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011635f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011636 typval_T *argvars;
11637 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638{
11639 char_u buf[2];
11640
11641#ifdef FEAT_VISUAL
11642 if (VIsual_active)
11643 {
11644 if (VIsual_select)
11645 buf[0] = VIsual_mode + 's' - 'v';
11646 else
11647 buf[0] = VIsual_mode;
11648 }
11649 else
11650#endif
11651 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11652 buf[0] = 'r';
11653 else if (State & INSERT)
11654 {
11655 if (State & REPLACE_FLAG)
11656 buf[0] = 'R';
11657 else
11658 buf[0] = 'i';
11659 }
11660 else if (State & CMDLINE)
11661 buf[0] = 'c';
11662 else
11663 buf[0] = 'n';
11664
11665 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011666 rettv->vval.v_string = vim_strsave(buf);
11667 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011668}
11669
11670/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011671 * "nextnonblank()" function
11672 */
11673 static void
11674f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011675 typval_T *argvars;
11676 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011677{
11678 linenr_T lnum;
11679
11680 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11681 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011682 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011683 {
11684 lnum = 0;
11685 break;
11686 }
11687 if (*skipwhite(ml_get(lnum)) != NUL)
11688 break;
11689 }
11690 rettv->vval.v_number = lnum;
11691}
11692
11693/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011694 * "nr2char()" function
11695 */
11696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011697f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011698 typval_T *argvars;
11699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700{
11701 char_u buf[NUMBUFLEN];
11702
11703#ifdef FEAT_MBYTE
11704 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011705 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706 else
11707#endif
11708 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011709 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710 buf[1] = NUL;
11711 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011712 rettv->v_type = VAR_STRING;
11713 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011714}
11715
11716/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011717 * "prevnonblank()" function
11718 */
11719 static void
11720f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011721 typval_T *argvars;
11722 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011723{
11724 linenr_T lnum;
11725
11726 lnum = get_tv_lnum(argvars);
11727 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11728 lnum = 0;
11729 else
11730 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11731 --lnum;
11732 rettv->vval.v_number = lnum;
11733}
11734
Bram Moolenaar8c711452005-01-14 21:53:12 +000011735/*
11736 * "range()" function
11737 */
11738 static void
11739f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011740 typval_T *argvars;
11741 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011742{
11743 long start;
11744 long end;
11745 long stride = 1;
11746 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011747 list_T *l;
11748 listitem_T *li;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011749 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011750
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011751 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011752 if (argvars[1].v_type == VAR_UNKNOWN)
11753 {
11754 end = start - 1;
11755 start = 0;
11756 }
11757 else
11758 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011759 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011760 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011761 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011762 }
11763
11764 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011765 if (error)
11766 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000011767 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011768 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000011769 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011770 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011771 else
11772 {
11773 l = list_alloc();
11774 if (l != NULL)
11775 {
11776 rettv->v_type = VAR_LIST;
11777 rettv->vval.v_list = l;
11778 ++l->lv_refcount;
11779
11780 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11781 {
11782 li = listitem_alloc();
11783 if (li == NULL)
11784 break;
11785 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011786 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011787 li->li_tv.vval.v_number = i;
11788 list_append(l, li);
11789 }
11790 }
11791 }
11792}
11793
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011794/*
11795 * "readfile()" function
11796 */
11797 static void
11798f_readfile(argvars, rettv)
11799 typval_T *argvars;
11800 typval_T *rettv;
11801{
11802 int binary = FALSE;
11803 char_u *fname;
11804 FILE *fd;
11805 list_T *l;
11806 listitem_T *li;
11807#define FREAD_SIZE 200 /* optimized for text lines */
11808 char_u buf[FREAD_SIZE];
11809 int readlen; /* size of last fread() */
11810 int buflen; /* nr of valid chars in buf[] */
11811 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11812 int tolist; /* first byte in buf[] still to be put in list */
11813 int chop; /* how many CR to chop off */
11814 char_u *prev = NULL; /* previously read bytes, if any */
11815 int prevlen = 0; /* length of "prev" if not NULL */
11816 char_u *s;
11817 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011818 long maxline = MAXLNUM;
11819 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011820
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011821 if (argvars[1].v_type != VAR_UNKNOWN)
11822 {
11823 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
11824 binary = TRUE;
11825 if (argvars[2].v_type != VAR_UNKNOWN)
11826 maxline = get_tv_number(&argvars[2]);
11827 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011828
11829 l = list_alloc();
11830 if (l == NULL)
11831 return;
11832 rettv->v_type = VAR_LIST;
11833 rettv->vval.v_list = l;
11834 l->lv_refcount = 1;
11835
11836 /* Always open the file in binary mode, library functions have a mind of
11837 * their own about CR-LF conversion. */
11838 fname = get_tv_string(&argvars[0]);
11839 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
11840 {
11841 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
11842 return;
11843 }
11844
11845 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011846 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011847 {
11848 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
11849 buflen = filtd + readlen;
11850 tolist = 0;
11851 for ( ; filtd < buflen || readlen <= 0; ++filtd)
11852 {
11853 if (buf[filtd] == '\n' || readlen <= 0)
11854 {
11855 /* Only when in binary mode add an empty list item when the
11856 * last line ends in a '\n'. */
11857 if (!binary && readlen == 0 && filtd == 0)
11858 break;
11859
11860 /* Found end-of-line or end-of-file: add a text line to the
11861 * list. */
11862 chop = 0;
11863 if (!binary)
11864 while (filtd - chop - 1 >= tolist
11865 && buf[filtd - chop - 1] == '\r')
11866 ++chop;
11867 len = filtd - tolist - chop;
11868 if (prev == NULL)
11869 s = vim_strnsave(buf + tolist, len);
11870 else
11871 {
11872 s = alloc((unsigned)(prevlen + len + 1));
11873 if (s != NULL)
11874 {
11875 mch_memmove(s, prev, prevlen);
11876 vim_free(prev);
11877 prev = NULL;
11878 mch_memmove(s + prevlen, buf + tolist, len);
11879 s[prevlen + len] = NUL;
11880 }
11881 }
11882 tolist = filtd + 1;
11883
11884 li = listitem_alloc();
11885 if (li == NULL)
11886 {
11887 vim_free(s);
11888 break;
11889 }
11890 li->li_tv.v_type = VAR_STRING;
11891 li->li_tv.v_lock = 0;
11892 li->li_tv.vval.v_string = s;
11893 list_append(l, li);
11894
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011895 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011896 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011897 if (readlen <= 0)
11898 break;
11899 }
11900 else if (buf[filtd] == NUL)
11901 buf[filtd] = '\n';
11902 }
11903 if (readlen <= 0)
11904 break;
11905
11906 if (tolist == 0)
11907 {
11908 /* "buf" is full, need to move text to an allocated buffer */
11909 if (prev == NULL)
11910 {
11911 prev = vim_strnsave(buf, buflen);
11912 prevlen = buflen;
11913 }
11914 else
11915 {
11916 s = alloc((unsigned)(prevlen + buflen));
11917 if (s != NULL)
11918 {
11919 mch_memmove(s, prev, prevlen);
11920 mch_memmove(s + prevlen, buf, buflen);
11921 vim_free(prev);
11922 prev = s;
11923 prevlen += buflen;
11924 }
11925 }
11926 filtd = 0;
11927 }
11928 else
11929 {
11930 mch_memmove(buf, buf + tolist, buflen - tolist);
11931 filtd -= tolist;
11932 }
11933 }
11934
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011935 /*
11936 * For a negative line count use only the lines at the end of the file,
11937 * free the rest.
11938 */
11939 if (maxline < 0)
11940 while (cnt > -maxline)
11941 {
11942 listitem_remove(l, l->lv_first);
11943 --cnt;
11944 }
11945
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011946 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011947 fclose(fd);
11948}
11949
11950
Bram Moolenaar0d660222005-01-07 21:51:51 +000011951#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
11952static void make_connection __ARGS((void));
11953static int check_connection __ARGS((void));
11954
11955 static void
11956make_connection()
11957{
11958 if (X_DISPLAY == NULL
11959# ifdef FEAT_GUI
11960 && !gui.in_use
11961# endif
11962 )
11963 {
11964 x_force_connect = TRUE;
11965 setup_term_clip();
11966 x_force_connect = FALSE;
11967 }
11968}
11969
11970 static int
11971check_connection()
11972{
11973 make_connection();
11974 if (X_DISPLAY == NULL)
11975 {
11976 EMSG(_("E240: No connection to Vim server"));
11977 return FAIL;
11978 }
11979 return OK;
11980}
11981#endif
11982
11983#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011984static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011985
11986 static void
11987remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000011988 typval_T *argvars;
11989 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011990 int expr;
11991{
11992 char_u *server_name;
11993 char_u *keys;
11994 char_u *r = NULL;
11995 char_u buf[NUMBUFLEN];
11996# ifdef WIN32
11997 HWND w;
11998# else
11999 Window w;
12000# endif
12001
12002 if (check_restricted() || check_secure())
12003 return;
12004
12005# ifdef FEAT_X11
12006 if (check_connection() == FAIL)
12007 return;
12008# endif
12009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012010 server_name = get_tv_string_chk(&argvars[0]);
12011 if (server_name == NULL)
12012 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012013 keys = get_tv_string_buf(&argvars[1], buf);
12014# ifdef WIN32
12015 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12016# else
12017 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12018 < 0)
12019# endif
12020 {
12021 if (r != NULL)
12022 EMSG(r); /* sending worked but evaluation failed */
12023 else
12024 EMSG2(_("E241: Unable to send to %s"), server_name);
12025 return;
12026 }
12027
12028 rettv->vval.v_string = r;
12029
12030 if (argvars[2].v_type != VAR_UNKNOWN)
12031 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012032 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012033 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012034 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012035
12036 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012037 v.di_tv.v_type = VAR_STRING;
12038 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012039 idvar = get_tv_string_chk(&argvars[2]);
12040 if (idvar != NULL)
12041 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012042 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012043 }
12044}
12045#endif
12046
12047/*
12048 * "remote_expr()" function
12049 */
12050/*ARGSUSED*/
12051 static void
12052f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012053 typval_T *argvars;
12054 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012055{
12056 rettv->v_type = VAR_STRING;
12057 rettv->vval.v_string = NULL;
12058#ifdef FEAT_CLIENTSERVER
12059 remote_common(argvars, rettv, TRUE);
12060#endif
12061}
12062
12063/*
12064 * "remote_foreground()" function
12065 */
12066/*ARGSUSED*/
12067 static void
12068f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012069 typval_T *argvars;
12070 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012071{
12072 rettv->vval.v_number = 0;
12073#ifdef FEAT_CLIENTSERVER
12074# ifdef WIN32
12075 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012076 {
12077 char_u *server_name = get_tv_string_chk(&argvars[0]);
12078
12079 if (server_name != NULL)
12080 serverForeground(server_name);
12081 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012082# else
12083 /* Send a foreground() expression to the server. */
12084 argvars[1].v_type = VAR_STRING;
12085 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12086 argvars[2].v_type = VAR_UNKNOWN;
12087 remote_common(argvars, rettv, TRUE);
12088 vim_free(argvars[1].vval.v_string);
12089# endif
12090#endif
12091}
12092
12093/*ARGSUSED*/
12094 static void
12095f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012096 typval_T *argvars;
12097 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012098{
12099#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012100 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012101 char_u *s = NULL;
12102# ifdef WIN32
12103 int n = 0;
12104# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012105 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012106
12107 if (check_restricted() || check_secure())
12108 {
12109 rettv->vval.v_number = -1;
12110 return;
12111 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012112 serverid = get_tv_string_chk(&argvars[0]);
12113 if (serverid == NULL)
12114 {
12115 rettv->vval.v_number = -1;
12116 return; /* type error; errmsg already given */
12117 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012118# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012119 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012120 if (n == 0)
12121 rettv->vval.v_number = -1;
12122 else
12123 {
12124 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12125 rettv->vval.v_number = (s != NULL);
12126 }
12127# else
12128 rettv->vval.v_number = 0;
12129 if (check_connection() == FAIL)
12130 return;
12131
12132 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012133 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012134# endif
12135
12136 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12137 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012138 char_u *retvar;
12139
Bram Moolenaar33570922005-01-25 22:26:29 +000012140 v.di_tv.v_type = VAR_STRING;
12141 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012142 retvar = get_tv_string_chk(&argvars[1]);
12143 if (retvar != NULL)
12144 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012145 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012146 }
12147#else
12148 rettv->vval.v_number = -1;
12149#endif
12150}
12151
12152/*ARGSUSED*/
12153 static void
12154f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012155 typval_T *argvars;
12156 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012157{
12158 char_u *r = NULL;
12159
12160#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012161 char_u *serverid = get_tv_string_chk(&argvars[0]);
12162
12163 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012164 {
12165# ifdef WIN32
12166 /* The server's HWND is encoded in the 'id' parameter */
12167 int n = 0;
12168
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012169 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012170 if (n != 0)
12171 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12172 if (r == NULL)
12173# else
12174 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012175 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012176# endif
12177 EMSG(_("E277: Unable to read a server reply"));
12178 }
12179#endif
12180 rettv->v_type = VAR_STRING;
12181 rettv->vval.v_string = r;
12182}
12183
12184/*
12185 * "remote_send()" function
12186 */
12187/*ARGSUSED*/
12188 static void
12189f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012190 typval_T *argvars;
12191 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012192{
12193 rettv->v_type = VAR_STRING;
12194 rettv->vval.v_string = NULL;
12195#ifdef FEAT_CLIENTSERVER
12196 remote_common(argvars, rettv, FALSE);
12197#endif
12198}
12199
12200/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012201 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012202 */
12203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012204f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012205 typval_T *argvars;
12206 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012207{
Bram Moolenaar33570922005-01-25 22:26:29 +000012208 list_T *l;
12209 listitem_T *item, *item2;
12210 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012211 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012212 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012213 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012214 dict_T *d;
12215 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012216
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012217 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012218 if (argvars[0].v_type == VAR_DICT)
12219 {
12220 if (argvars[2].v_type != VAR_UNKNOWN)
12221 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012222 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012223 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012224 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012225 key = get_tv_string_chk(&argvars[1]);
12226 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012228 di = dict_find(d, key, -1);
12229 if (di == NULL)
12230 EMSG2(_(e_dictkey), key);
12231 else
12232 {
12233 *rettv = di->di_tv;
12234 init_tv(&di->di_tv);
12235 dictitem_remove(d, di);
12236 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012237 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012238 }
12239 }
12240 else if (argvars[0].v_type != VAR_LIST)
12241 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012242 else if ((l = argvars[0].vval.v_list) != NULL
12243 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012244 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012245 int error = FALSE;
12246
12247 idx = get_tv_number_chk(&argvars[1], &error);
12248 if (error)
12249 ; /* type error: do nothing, errmsg already given */
12250 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012251 EMSGN(_(e_listidx), idx);
12252 else
12253 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012254 if (argvars[2].v_type == VAR_UNKNOWN)
12255 {
12256 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012257 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012258 *rettv = item->li_tv;
12259 vim_free(item);
12260 }
12261 else
12262 {
12263 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012264 end = get_tv_number_chk(&argvars[2], &error);
12265 if (error)
12266 ; /* type error: do nothing */
12267 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012268 EMSGN(_(e_listidx), end);
12269 else
12270 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012271 int cnt = 0;
12272
12273 for (li = item; li != NULL; li = li->li_next)
12274 {
12275 ++cnt;
12276 if (li == item2)
12277 break;
12278 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012279 if (li == NULL) /* didn't find "item2" after "item" */
12280 EMSG(_(e_invrange));
12281 else
12282 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012283 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012284 l = list_alloc();
12285 if (l != NULL)
12286 {
12287 rettv->v_type = VAR_LIST;
12288 rettv->vval.v_list = l;
12289 l->lv_first = item;
12290 l->lv_last = item2;
12291 l->lv_refcount = 1;
12292 item->li_prev = NULL;
12293 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012294 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012295 }
12296 }
12297 }
12298 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012299 }
12300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301}
12302
12303/*
12304 * "rename({from}, {to})" function
12305 */
12306 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012307f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012308 typval_T *argvars;
12309 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310{
12311 char_u buf[NUMBUFLEN];
12312
12313 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012314 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012316 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12317 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012318}
12319
12320/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012321 * "repeat()" function
12322 */
12323/*ARGSUSED*/
12324 static void
12325f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012326 typval_T *argvars;
12327 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012328{
12329 char_u *p;
12330 int n;
12331 int slen;
12332 int len;
12333 char_u *r;
12334 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012335 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012336
12337 n = get_tv_number(&argvars[1]);
12338 if (argvars[0].v_type == VAR_LIST)
12339 {
12340 l = list_alloc();
12341 if (l != NULL && argvars[0].vval.v_list != NULL)
12342 {
12343 l->lv_refcount = 1;
12344 while (n-- > 0)
12345 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12346 break;
12347 }
12348 rettv->v_type = VAR_LIST;
12349 rettv->vval.v_list = l;
12350 }
12351 else
12352 {
12353 p = get_tv_string(&argvars[0]);
12354 rettv->v_type = VAR_STRING;
12355 rettv->vval.v_string = NULL;
12356
12357 slen = (int)STRLEN(p);
12358 len = slen * n;
12359 if (len <= 0)
12360 return;
12361
12362 r = alloc(len + 1);
12363 if (r != NULL)
12364 {
12365 for (i = 0; i < n; i++)
12366 mch_memmove(r + i * slen, p, (size_t)slen);
12367 r[len] = NUL;
12368 }
12369
12370 rettv->vval.v_string = r;
12371 }
12372}
12373
12374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375 * "resolve()" function
12376 */
12377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012378f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012379 typval_T *argvars;
12380 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381{
12382 char_u *p;
12383
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012384 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385#ifdef FEAT_SHORTCUT
12386 {
12387 char_u *v = NULL;
12388
12389 v = mch_resolve_shortcut(p);
12390 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012391 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012393 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394 }
12395#else
12396# ifdef HAVE_READLINK
12397 {
12398 char_u buf[MAXPATHL + 1];
12399 char_u *cpy;
12400 int len;
12401 char_u *remain = NULL;
12402 char_u *q;
12403 int is_relative_to_current = FALSE;
12404 int has_trailing_pathsep = FALSE;
12405 int limit = 100;
12406
12407 p = vim_strsave(p);
12408
12409 if (p[0] == '.' && (vim_ispathsep(p[1])
12410 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12411 is_relative_to_current = TRUE;
12412
12413 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012414 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415 has_trailing_pathsep = TRUE;
12416
12417 q = getnextcomp(p);
12418 if (*q != NUL)
12419 {
12420 /* Separate the first path component in "p", and keep the
12421 * remainder (beginning with the path separator). */
12422 remain = vim_strsave(q - 1);
12423 q[-1] = NUL;
12424 }
12425
12426 for (;;)
12427 {
12428 for (;;)
12429 {
12430 len = readlink((char *)p, (char *)buf, MAXPATHL);
12431 if (len <= 0)
12432 break;
12433 buf[len] = NUL;
12434
12435 if (limit-- == 0)
12436 {
12437 vim_free(p);
12438 vim_free(remain);
12439 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012440 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012441 goto fail;
12442 }
12443
12444 /* Ensure that the result will have a trailing path separator
12445 * if the argument has one. */
12446 if (remain == NULL && has_trailing_pathsep)
12447 add_pathsep(buf);
12448
12449 /* Separate the first path component in the link value and
12450 * concatenate the remainders. */
12451 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12452 if (*q != NUL)
12453 {
12454 if (remain == NULL)
12455 remain = vim_strsave(q - 1);
12456 else
12457 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012458 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012459 if (cpy != NULL)
12460 {
12461 STRCAT(cpy, remain);
12462 vim_free(remain);
12463 remain = cpy;
12464 }
12465 }
12466 q[-1] = NUL;
12467 }
12468
12469 q = gettail(p);
12470 if (q > p && *q == NUL)
12471 {
12472 /* Ignore trailing path separator. */
12473 q[-1] = NUL;
12474 q = gettail(p);
12475 }
12476 if (q > p && !mch_isFullName(buf))
12477 {
12478 /* symlink is relative to directory of argument */
12479 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12480 if (cpy != NULL)
12481 {
12482 STRCPY(cpy, p);
12483 STRCPY(gettail(cpy), buf);
12484 vim_free(p);
12485 p = cpy;
12486 }
12487 }
12488 else
12489 {
12490 vim_free(p);
12491 p = vim_strsave(buf);
12492 }
12493 }
12494
12495 if (remain == NULL)
12496 break;
12497
12498 /* Append the first path component of "remain" to "p". */
12499 q = getnextcomp(remain + 1);
12500 len = q - remain - (*q != NUL);
12501 cpy = vim_strnsave(p, STRLEN(p) + len);
12502 if (cpy != NULL)
12503 {
12504 STRNCAT(cpy, remain, len);
12505 vim_free(p);
12506 p = cpy;
12507 }
12508 /* Shorten "remain". */
12509 if (*q != NUL)
12510 STRCPY(remain, q - 1);
12511 else
12512 {
12513 vim_free(remain);
12514 remain = NULL;
12515 }
12516 }
12517
12518 /* If the result is a relative path name, make it explicitly relative to
12519 * the current directory if and only if the argument had this form. */
12520 if (!vim_ispathsep(*p))
12521 {
12522 if (is_relative_to_current
12523 && *p != NUL
12524 && !(p[0] == '.'
12525 && (p[1] == NUL
12526 || vim_ispathsep(p[1])
12527 || (p[1] == '.'
12528 && (p[2] == NUL
12529 || vim_ispathsep(p[2]))))))
12530 {
12531 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012532 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533 if (cpy != NULL)
12534 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535 vim_free(p);
12536 p = cpy;
12537 }
12538 }
12539 else if (!is_relative_to_current)
12540 {
12541 /* Strip leading "./". */
12542 q = p;
12543 while (q[0] == '.' && vim_ispathsep(q[1]))
12544 q += 2;
12545 if (q > p)
12546 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12547 }
12548 }
12549
12550 /* Ensure that the result will have no trailing path separator
12551 * if the argument had none. But keep "/" or "//". */
12552 if (!has_trailing_pathsep)
12553 {
12554 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012555 if (after_pathsep(p, q))
12556 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012557 }
12558
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012559 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012560 }
12561# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012562 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563# endif
12564#endif
12565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012566 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567
12568#ifdef HAVE_READLINK
12569fail:
12570#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012571 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012572}
12573
12574/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012575 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576 */
12577 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012578f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012579 typval_T *argvars;
12580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012581{
Bram Moolenaar33570922005-01-25 22:26:29 +000012582 list_T *l;
12583 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584
Bram Moolenaar0d660222005-01-07 21:51:51 +000012585 rettv->vval.v_number = 0;
12586 if (argvars[0].v_type != VAR_LIST)
12587 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012588 else if ((l = argvars[0].vval.v_list) != NULL
12589 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012590 {
12591 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012592 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012593 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012594 while (li != NULL)
12595 {
12596 ni = li->li_prev;
12597 list_append(l, li);
12598 li = ni;
12599 }
12600 rettv->vval.v_list = l;
12601 rettv->v_type = VAR_LIST;
12602 ++l->lv_refcount;
12603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012604}
12605
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012606#define SP_NOMOVE 1 /* don't move cursor */
12607#define SP_REPEAT 2 /* repeat to find outer pair */
12608#define SP_RETCOUNT 4 /* return matchcount */
12609
Bram Moolenaar33570922005-01-25 22:26:29 +000012610static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012611
12612/*
12613 * Get flags for a search function.
12614 * Possibly sets "p_ws".
12615 * Returns BACKWARD, FORWARD or zero (for an error).
12616 */
12617 static int
12618get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012619 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012620 int *flagsp;
12621{
12622 int dir = FORWARD;
12623 char_u *flags;
12624 char_u nbuf[NUMBUFLEN];
12625 int mask;
12626
12627 if (varp->v_type != VAR_UNKNOWN)
12628 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012629 flags = get_tv_string_buf_chk(varp, nbuf);
12630 if (flags == NULL)
12631 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012632 while (*flags != NUL)
12633 {
12634 switch (*flags)
12635 {
12636 case 'b': dir = BACKWARD; break;
12637 case 'w': p_ws = TRUE; break;
12638 case 'W': p_ws = FALSE; break;
12639 default: mask = 0;
12640 if (flagsp != NULL)
12641 switch (*flags)
12642 {
12643 case 'n': mask = SP_NOMOVE; break;
12644 case 'r': mask = SP_REPEAT; break;
12645 case 'm': mask = SP_RETCOUNT; break;
12646 }
12647 if (mask == 0)
12648 {
12649 EMSG2(_(e_invarg2), flags);
12650 dir = 0;
12651 }
12652 else
12653 *flagsp |= mask;
12654 }
12655 if (dir == 0)
12656 break;
12657 ++flags;
12658 }
12659 }
12660 return dir;
12661}
12662
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663/*
12664 * "search()" function
12665 */
12666 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012667f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012668 typval_T *argvars;
12669 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012670{
12671 char_u *pat;
12672 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012673 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012674 int save_p_ws = p_ws;
12675 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012676 int flags = 0;
12677
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012678 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012680 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012681 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12682 if (dir == 0)
12683 goto theend;
12684 if ((flags & ~SP_NOMOVE) != 0)
12685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012686 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012687 goto theend;
12688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012690 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12692 SEARCH_KEEP, RE_SEARCH) != FAIL)
12693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012694 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695 curwin->w_cursor = pos;
12696 /* "/$" will put the cursor after the end of the line, may need to
12697 * correct that here */
12698 check_cursor();
12699 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012700
12701 /* If 'n' flag is used: restore cursor position. */
12702 if (flags & SP_NOMOVE)
12703 curwin->w_cursor = save_cursor;
12704theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705 p_ws = save_p_ws;
12706}
12707
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708/*
12709 * "searchpair()" function
12710 */
12711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012712f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012713 typval_T *argvars;
12714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715{
12716 char_u *spat, *mpat, *epat;
12717 char_u *skip;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012718 char_u *pat, *pat2 = NULL, *pat3 = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719 pos_T pos;
12720 pos_T firstpos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012721 pos_T foundpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722 pos_T save_cursor;
12723 pos_T save_pos;
12724 int save_p_ws = p_ws;
12725 char_u *save_cpo;
12726 int dir;
12727 int flags = 0;
12728 char_u nbuf1[NUMBUFLEN];
12729 char_u nbuf2[NUMBUFLEN];
12730 char_u nbuf3[NUMBUFLEN];
12731 int n;
12732 int r;
12733 int nest = 1;
12734 int err;
12735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012736 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737
12738 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12739 save_cpo = p_cpo;
12740 p_cpo = (char_u *)"";
12741
12742 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012743 spat = get_tv_string_chk(&argvars[0]);
12744 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
12745 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
12746 if (spat == NULL || mpat == NULL || epat == NULL)
12747 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748
12749 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12750 * start/middle/end (pat3, for the top pair). */
12751 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12752 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12753 if (pat2 == NULL || pat3 == NULL)
12754 goto theend;
12755 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12756 if (*mpat == NUL)
12757 STRCPY(pat3, pat2);
12758 else
12759 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12760 spat, epat, mpat);
12761
12762 /* Handle the optional fourth argument: flags */
12763 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012764 if (dir == 0)
12765 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766
12767 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012768 if (argvars[3].v_type == VAR_UNKNOWN
12769 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770 skip = (char_u *)"";
12771 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012772 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
12773 if (skip == NULL)
12774 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775
12776 save_cursor = curwin->w_cursor;
12777 pos = curwin->w_cursor;
12778 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012779 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780 pat = pat3;
12781 for (;;)
12782 {
12783 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
12784 SEARCH_KEEP, RE_SEARCH);
12785 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
12786 /* didn't find it or found the first match again: FAIL */
12787 break;
12788
12789 if (firstpos.lnum == 0)
12790 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012791 if (equalpos(pos, foundpos))
12792 {
12793 /* Found the same position again. Can happen with a pattern that
12794 * has "\zs" at the end and searching backwards. Advance one
12795 * character and try again. */
12796 if (dir == BACKWARD)
12797 decl(&pos);
12798 else
12799 incl(&pos);
12800 }
12801 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012802
12803 /* If the skip pattern matches, ignore this match. */
12804 if (*skip != NUL)
12805 {
12806 save_pos = curwin->w_cursor;
12807 curwin->w_cursor = pos;
12808 r = eval_to_bool(skip, &err, NULL, FALSE);
12809 curwin->w_cursor = save_pos;
12810 if (err)
12811 {
12812 /* Evaluating {skip} caused an error, break here. */
12813 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012814 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815 break;
12816 }
12817 if (r)
12818 continue;
12819 }
12820
12821 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
12822 {
12823 /* Found end when searching backwards or start when searching
12824 * forward: nested pair. */
12825 ++nest;
12826 pat = pat2; /* nested, don't search for middle */
12827 }
12828 else
12829 {
12830 /* Found end when searching forward or start when searching
12831 * backward: end of (nested) pair; or found middle in outer pair. */
12832 if (--nest == 1)
12833 pat = pat3; /* outer level, search for middle */
12834 }
12835
12836 if (nest == 0)
12837 {
12838 /* Found the match: return matchcount or line number. */
12839 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012840 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012842 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843 curwin->w_cursor = pos;
12844 if (!(flags & SP_REPEAT))
12845 break;
12846 nest = 1; /* search for next unmatched */
12847 }
12848 }
12849
12850 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012851 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 curwin->w_cursor = save_cursor;
12853
12854theend:
12855 vim_free(pat2);
12856 vim_free(pat3);
12857 p_ws = save_p_ws;
12858 p_cpo = save_cpo;
12859}
12860
Bram Moolenaar0d660222005-01-07 21:51:51 +000012861/*ARGSUSED*/
12862 static void
12863f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012864 typval_T *argvars;
12865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012866{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012867#ifdef FEAT_CLIENTSERVER
12868 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012869 char_u *server = get_tv_string_chk(&argvars[0]);
12870 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871
Bram Moolenaar0d660222005-01-07 21:51:51 +000012872 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012873 if (server == NULL || reply == NULL)
12874 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012875 if (check_restricted() || check_secure())
12876 return;
12877# ifdef FEAT_X11
12878 if (check_connection() == FAIL)
12879 return;
12880# endif
12881
12882 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012884 EMSG(_("E258: Unable to send to client"));
12885 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012887 rettv->vval.v_number = 0;
12888#else
12889 rettv->vval.v_number = -1;
12890#endif
12891}
12892
12893/*ARGSUSED*/
12894 static void
12895f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012896 typval_T *argvars;
12897 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012898{
12899 char_u *r = NULL;
12900
12901#ifdef FEAT_CLIENTSERVER
12902# ifdef WIN32
12903 r = serverGetVimNames();
12904# else
12905 make_connection();
12906 if (X_DISPLAY != NULL)
12907 r = serverGetVimNames(X_DISPLAY);
12908# endif
12909#endif
12910 rettv->v_type = VAR_STRING;
12911 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912}
12913
12914/*
12915 * "setbufvar()" function
12916 */
12917/*ARGSUSED*/
12918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012919f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012920 typval_T *argvars;
12921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922{
12923 buf_T *buf;
12924#ifdef FEAT_AUTOCMD
12925 aco_save_T aco;
12926#else
12927 buf_T *save_curbuf;
12928#endif
12929 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012930 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931 char_u nbuf[NUMBUFLEN];
12932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012933 rettv->vval.v_number = 0;
12934
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935 if (check_restricted() || check_secure())
12936 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012937 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12938 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012939 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940 varp = &argvars[2];
12941
12942 if (buf != NULL && varname != NULL && varp != NULL)
12943 {
12944 /* set curbuf to be our buf, temporarily */
12945#ifdef FEAT_AUTOCMD
12946 aucmd_prepbuf(&aco, buf);
12947#else
12948 save_curbuf = curbuf;
12949 curbuf = buf;
12950#endif
12951
12952 if (*varname == '&')
12953 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012954 long numval;
12955 char_u *strval;
12956 int error = FALSE;
12957
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012959 numval = get_tv_number_chk(varp, &error);
12960 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012961 if (!error && strval != NULL)
12962 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963 }
12964 else
12965 {
12966 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
12967 if (bufvarname != NULL)
12968 {
12969 STRCPY(bufvarname, "b:");
12970 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012971 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012972 vim_free(bufvarname);
12973 }
12974 }
12975
12976 /* reset notion of buffer */
12977#ifdef FEAT_AUTOCMD
12978 aucmd_restbuf(&aco);
12979#else
12980 curbuf = save_curbuf;
12981#endif
12982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983}
12984
12985/*
12986 * "setcmdpos()" function
12987 */
12988 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012989f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012990 typval_T *argvars;
12991 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012993 int pos = (int)get_tv_number(&argvars[0]) - 1;
12994
12995 if (pos >= 0)
12996 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997}
12998
12999/*
13000 * "setline()" function
13001 */
13002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013003f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013004 typval_T *argvars;
13005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013006{
13007 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013008 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013009 list_T *l = NULL;
13010 listitem_T *li = NULL;
13011 long added = 0;
13012 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013013
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013014 lnum = get_tv_lnum(&argvars[0]);
13015 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013017 l = argvars[1].vval.v_list;
13018 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013020 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013021 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013022
13023 rettv->vval.v_number = 0; /* OK */
13024 for (;;)
13025 {
13026 if (l != NULL)
13027 {
13028 /* list argument, get next string */
13029 if (li == NULL)
13030 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013031 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013032 li = li->li_next;
13033 }
13034
13035 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013036 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013037 break;
13038 if (lnum <= curbuf->b_ml.ml_line_count)
13039 {
13040 /* existing line, replace it */
13041 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13042 {
13043 changed_bytes(lnum, 0);
13044 check_cursor_col();
13045 rettv->vval.v_number = 0; /* OK */
13046 }
13047 }
13048 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13049 {
13050 /* lnum is one past the last line, append the line */
13051 ++added;
13052 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13053 rettv->vval.v_number = 0; /* OK */
13054 }
13055
13056 if (l == NULL) /* only one string argument */
13057 break;
13058 ++lnum;
13059 }
13060
13061 if (added > 0)
13062 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063}
13064
13065/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013066 * "setqflist()" function
13067 */
13068/*ARGSUSED*/
13069 static void
13070f_setqflist(argvars, rettv)
13071 typval_T *argvars;
13072 typval_T *rettv;
13073{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013074 char_u *act;
13075 int action = ' ';
13076
Bram Moolenaar2641f772005-03-25 21:58:17 +000013077 rettv->vval.v_number = -1;
13078
13079#ifdef FEAT_QUICKFIX
13080 if (argvars[0].v_type != VAR_LIST)
13081 EMSG(_(e_listreq));
13082 else
13083 {
13084 list_T *l = argvars[0].vval.v_list;
13085
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013086 if (argvars[1].v_type == VAR_STRING)
13087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013088 act = get_tv_string_chk(&argvars[1]);
13089 if (act == NULL)
13090 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013091 if (*act == 'a' || *act == 'r')
13092 action = *act;
13093 }
13094
13095 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013096 rettv->vval.v_number = 0;
13097 }
13098#endif
13099}
13100
13101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102 * "setreg()" function
13103 */
13104 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013105f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013106 typval_T *argvars;
13107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108{
13109 int regname;
13110 char_u *strregname;
13111 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013112 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113 int append;
13114 char_u yank_type;
13115 long block_len;
13116
13117 block_len = -1;
13118 yank_type = MAUTO;
13119 append = FALSE;
13120
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013121 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013122 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013124 if (strregname == NULL)
13125 return; /* type error; errmsg already given */
13126 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127 if (regname == 0 || regname == '@')
13128 regname = '"';
13129 else if (regname == '=')
13130 return;
13131
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013132 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013133 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013134 stropt = get_tv_string_chk(&argvars[2]);
13135 if (stropt == NULL)
13136 return; /* type error */
13137 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138 switch (*stropt)
13139 {
13140 case 'a': case 'A': /* append */
13141 append = TRUE;
13142 break;
13143 case 'v': case 'c': /* character-wise selection */
13144 yank_type = MCHAR;
13145 break;
13146 case 'V': case 'l': /* line-wise selection */
13147 yank_type = MLINE;
13148 break;
13149#ifdef FEAT_VISUAL
13150 case 'b': case Ctrl_V: /* block-wise selection */
13151 yank_type = MBLOCK;
13152 if (VIM_ISDIGIT(stropt[1]))
13153 {
13154 ++stropt;
13155 block_len = getdigits(&stropt) - 1;
13156 --stropt;
13157 }
13158 break;
13159#endif
13160 }
13161 }
13162
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013163 strval = get_tv_string_chk(&argvars[1]);
13164 if (strval != NULL)
13165 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013167 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013168}
13169
13170
13171/*
13172 * "setwinvar(expr)" function
13173 */
13174/*ARGSUSED*/
13175 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013176f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013177 typval_T *argvars;
13178 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013179{
13180 win_T *win;
13181#ifdef FEAT_WINDOWS
13182 win_T *save_curwin;
13183#endif
13184 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013185 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186 char_u nbuf[NUMBUFLEN];
13187
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013188 rettv->vval.v_number = 0;
13189
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190 if (check_restricted() || check_secure())
13191 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013193 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194 varp = &argvars[2];
13195
13196 if (win != NULL && varname != NULL && varp != NULL)
13197 {
13198#ifdef FEAT_WINDOWS
13199 /* set curwin to be our win, temporarily */
13200 save_curwin = curwin;
13201 curwin = win;
13202 curbuf = curwin->w_buffer;
13203#endif
13204
13205 if (*varname == '&')
13206 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013207 long numval;
13208 char_u *strval;
13209 int error = FALSE;
13210
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013212 numval = get_tv_number_chk(varp, &error);
13213 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013214 if (!error && strval != NULL)
13215 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216 }
13217 else
13218 {
13219 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13220 if (winvarname != NULL)
13221 {
13222 STRCPY(winvarname, "w:");
13223 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013224 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225 vim_free(winvarname);
13226 }
13227 }
13228
13229#ifdef FEAT_WINDOWS
13230 /* Restore current window, if it's still valid (autocomands can make
13231 * it invalid). */
13232 if (win_valid(save_curwin))
13233 {
13234 curwin = save_curwin;
13235 curbuf = curwin->w_buffer;
13236 }
13237#endif
13238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239}
13240
13241/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013242 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013243 */
13244 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013245f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013246 typval_T *argvars;
13247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013249 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013250
Bram Moolenaar0d660222005-01-07 21:51:51 +000013251 p = get_tv_string(&argvars[0]);
13252 rettv->vval.v_string = vim_strsave(p);
13253 simplify_filename(rettv->vval.v_string); /* simplify in place */
13254 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255}
13256
Bram Moolenaar0d660222005-01-07 21:51:51 +000013257static int
13258#ifdef __BORLANDC__
13259 _RTLENTRYF
13260#endif
13261 item_compare __ARGS((const void *s1, const void *s2));
13262static int
13263#ifdef __BORLANDC__
13264 _RTLENTRYF
13265#endif
13266 item_compare2 __ARGS((const void *s1, const void *s2));
13267
13268static int item_compare_ic;
13269static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013270static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013271#define ITEM_COMPARE_FAIL 999
13272
Bram Moolenaar071d4272004-06-13 20:20:40 +000013273/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013274 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013276 static int
13277#ifdef __BORLANDC__
13278_RTLENTRYF
13279#endif
13280item_compare(s1, s2)
13281 const void *s1;
13282 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013283{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013284 char_u *p1, *p2;
13285 char_u *tofree1, *tofree2;
13286 int res;
13287 char_u numbuf1[NUMBUFLEN];
13288 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289
Bram Moolenaar33570922005-01-25 22:26:29 +000013290 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13291 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013292 if (item_compare_ic)
13293 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013295 res = STRCMP(p1, p2);
13296 vim_free(tofree1);
13297 vim_free(tofree2);
13298 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013299}
13300
13301 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013302#ifdef __BORLANDC__
13303_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013304#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013305item_compare2(s1, s2)
13306 const void *s1;
13307 const void *s2;
13308{
13309 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013310 typval_T rettv;
13311 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013312 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013314 /* shortcut after failure in previous call; compare all items equal */
13315 if (item_compare_func_err)
13316 return 0;
13317
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013318 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13319 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013320 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13321 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013322
13323 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13324 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013325 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013326 clear_tv(&argv[0]);
13327 clear_tv(&argv[1]);
13328
13329 if (res == FAIL)
13330 res = ITEM_COMPARE_FAIL;
13331 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013332 /* return value has wrong type */
13333 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13334 if (item_compare_func_err)
13335 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013336 clear_tv(&rettv);
13337 return res;
13338}
13339
13340/*
13341 * "sort({list})" function
13342 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013344f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013345 typval_T *argvars;
13346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347{
Bram Moolenaar33570922005-01-25 22:26:29 +000013348 list_T *l;
13349 listitem_T *li;
13350 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013351 long len;
13352 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353
Bram Moolenaar0d660222005-01-07 21:51:51 +000013354 rettv->vval.v_number = 0;
13355 if (argvars[0].v_type != VAR_LIST)
13356 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013357 else
13358 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013359 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013360 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013361 return;
13362 rettv->vval.v_list = l;
13363 rettv->v_type = VAR_LIST;
13364 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013365
Bram Moolenaar0d660222005-01-07 21:51:51 +000013366 len = list_len(l);
13367 if (len <= 1)
13368 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369
Bram Moolenaar0d660222005-01-07 21:51:51 +000013370 item_compare_ic = FALSE;
13371 item_compare_func = NULL;
13372 if (argvars[1].v_type != VAR_UNKNOWN)
13373 {
13374 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013375 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013376 else
13377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013378 int error = FALSE;
13379
13380 i = get_tv_number_chk(&argvars[1], &error);
13381 if (error)
13382 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013383 if (i == 1)
13384 item_compare_ic = TRUE;
13385 else
13386 item_compare_func = get_tv_string(&argvars[1]);
13387 }
13388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389
Bram Moolenaar0d660222005-01-07 21:51:51 +000013390 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013391 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013392 if (ptrs == NULL)
13393 return;
13394 i = 0;
13395 for (li = l->lv_first; li != NULL; li = li->li_next)
13396 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013397
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013398 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013399 /* test the compare function */
13400 if (item_compare_func != NULL
13401 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13402 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013403 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013405 {
13406 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013407 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013408 item_compare_func == NULL ? item_compare : item_compare2);
13409
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013410 if (!item_compare_func_err)
13411 {
13412 /* Clear the List and append the items in the sorted order. */
13413 l->lv_first = l->lv_last = NULL;
13414 l->lv_len = 0;
13415 for (i = 0; i < len; ++i)
13416 list_append(l, ptrs[i]);
13417 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013418 }
13419
13420 vim_free(ptrs);
13421 }
13422}
13423
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013424/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013425 * "soundfold({word})" function
13426 */
13427 static void
13428f_soundfold(argvars, rettv)
13429 typval_T *argvars;
13430 typval_T *rettv;
13431{
13432 char_u *s;
13433
13434 rettv->v_type = VAR_STRING;
13435 s = get_tv_string(&argvars[0]);
13436#ifdef FEAT_SYN_HL
13437 rettv->vval.v_string = eval_soundfold(s);
13438#else
13439 rettv->vval.v_string = vim_strsave(s);
13440#endif
13441}
13442
13443/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013444 * "spellbadword()" function
13445 */
13446/* ARGSUSED */
13447 static void
13448f_spellbadword(argvars, rettv)
13449 typval_T *argvars;
13450 typval_T *rettv;
13451{
13452 int attr;
13453 char_u *ptr;
13454 int len;
13455
13456 rettv->vval.v_string = NULL;
13457 rettv->v_type = VAR_STRING;
13458
13459#ifdef FEAT_SYN_HL
13460 /* Find the start of the badly spelled word. */
13461 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL)
13462 return;
13463
13464 /* Get the length of the word and copy it. */
13465 ptr = ml_get_cursor();
13466 len = spell_check(curwin, ptr, &attr);
13467 rettv->vval.v_string = vim_strnsave(ptr, len);
13468#endif
13469}
13470
13471/*
13472 * "spellsuggest()" function
13473 */
13474 static void
13475f_spellsuggest(argvars, rettv)
13476 typval_T *argvars;
13477 typval_T *rettv;
13478{
13479 char_u *str;
13480 int maxcount;
13481 garray_T ga;
13482 list_T *l;
13483 listitem_T *li;
13484 int i;
13485
13486 l = list_alloc();
13487 if (l == NULL)
13488 return;
13489 rettv->v_type = VAR_LIST;
13490 rettv->vval.v_list = l;
13491 ++l->lv_refcount;
13492
13493#ifdef FEAT_SYN_HL
13494 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13495 {
13496 str = get_tv_string(&argvars[0]);
13497 if (argvars[1].v_type != VAR_UNKNOWN)
13498 {
13499 maxcount = get_tv_number(&argvars[1]);
13500 if (maxcount <= 0)
13501 return;
13502 }
13503 else
13504 maxcount = 25;
13505
13506 spell_suggest_list(&ga, str, maxcount);
13507
13508 for (i = 0; i < ga.ga_len; ++i)
13509 {
13510 str = ((char_u **)ga.ga_data)[i];
13511
13512 li = listitem_alloc();
13513 if (li == NULL)
13514 vim_free(str);
13515 else
13516 {
13517 li->li_tv.v_type = VAR_STRING;
13518 li->li_tv.v_lock = 0;
13519 li->li_tv.vval.v_string = str;
13520 list_append(l, li);
13521 }
13522 }
13523 ga_clear(&ga);
13524 }
13525#endif
13526}
13527
Bram Moolenaar0d660222005-01-07 21:51:51 +000013528 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013529f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013530 typval_T *argvars;
13531 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013532{
13533 char_u *str;
13534 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013535 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013536 regmatch_T regmatch;
13537 char_u patbuf[NUMBUFLEN];
13538 char_u *save_cpo;
13539 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000013540 listitem_T *ni;
13541 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013542 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013543 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013544 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013545
13546 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13547 save_cpo = p_cpo;
13548 p_cpo = (char_u *)"";
13549
13550 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013551 if (argvars[1].v_type != VAR_UNKNOWN)
13552 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013553 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13554 if (pat == NULL)
13555 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013556 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013557 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013558 }
13559 if (pat == NULL || *pat == NUL)
13560 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000013561
13562 l = list_alloc();
13563 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013564 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013565 rettv->v_type = VAR_LIST;
13566 rettv->vval.v_list = l;
13567 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013568 if (typeerr)
13569 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013570
Bram Moolenaar0d660222005-01-07 21:51:51 +000013571 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13572 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013574 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013575 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013576 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013577 if (*str == NUL)
13578 match = FALSE; /* empty item at the end */
13579 else
13580 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013581 if (match)
13582 end = regmatch.startp[0];
13583 else
13584 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013585 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
13586 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013587 {
13588 ni = listitem_alloc();
13589 if (ni == NULL)
13590 break;
13591 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013592 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013593 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
13594 list_append(l, ni);
13595 }
13596 if (!match)
13597 break;
13598 /* Advance to just after the match. */
13599 if (regmatch.endp[0] > str)
13600 col = 0;
13601 else
13602 {
13603 /* Don't get stuck at the same match. */
13604#ifdef FEAT_MBYTE
13605 col = mb_ptr2len_check(regmatch.endp[0]);
13606#else
13607 col = 1;
13608#endif
13609 }
13610 str = regmatch.endp[0];
13611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013612
Bram Moolenaar0d660222005-01-07 21:51:51 +000013613 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615
Bram Moolenaar0d660222005-01-07 21:51:51 +000013616 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617}
13618
13619#ifdef HAVE_STRFTIME
13620/*
13621 * "strftime({format}[, {time}])" function
13622 */
13623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013624f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013625 typval_T *argvars;
13626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627{
13628 char_u result_buf[256];
13629 struct tm *curtime;
13630 time_t seconds;
13631 char_u *p;
13632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013633 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013635 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013636 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 seconds = time(NULL);
13638 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013639 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640 curtime = localtime(&seconds);
13641 /* MSVC returns NULL for an invalid value of seconds. */
13642 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013643 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013644 else
13645 {
13646# ifdef FEAT_MBYTE
13647 vimconv_T conv;
13648 char_u *enc;
13649
13650 conv.vc_type = CONV_NONE;
13651 enc = enc_locale();
13652 convert_setup(&conv, p_enc, enc);
13653 if (conv.vc_type != CONV_NONE)
13654 p = string_convert(&conv, p, NULL);
13655# endif
13656 if (p != NULL)
13657 (void)strftime((char *)result_buf, sizeof(result_buf),
13658 (char *)p, curtime);
13659 else
13660 result_buf[0] = NUL;
13661
13662# ifdef FEAT_MBYTE
13663 if (conv.vc_type != CONV_NONE)
13664 vim_free(p);
13665 convert_setup(&conv, enc, p_enc);
13666 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013667 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668 else
13669# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013670 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671
13672# ifdef FEAT_MBYTE
13673 /* Release conversion descriptors */
13674 convert_setup(&conv, NULL, NULL);
13675 vim_free(enc);
13676# endif
13677 }
13678}
13679#endif
13680
13681/*
13682 * "stridx()" function
13683 */
13684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013685f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013686 typval_T *argvars;
13687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688{
13689 char_u buf[NUMBUFLEN];
13690 char_u *needle;
13691 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000013692 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000013694 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013696 needle = get_tv_string_chk(&argvars[1]);
13697 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000013698 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013699 if (needle == NULL || haystack == NULL)
13700 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701
Bram Moolenaar33570922005-01-25 22:26:29 +000013702 if (argvars[2].v_type != VAR_UNKNOWN)
13703 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013704 int error = FALSE;
13705
13706 start_idx = get_tv_number_chk(&argvars[2], &error);
13707 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000013708 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013709 if (start_idx >= 0)
13710 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000013711 }
13712
13713 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13714 if (pos != NULL)
13715 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013716}
13717
13718/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013719 * "string()" function
13720 */
13721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013722f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013723 typval_T *argvars;
13724 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013725{
13726 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013727 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013728
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013729 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013730 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013731 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013732 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733}
13734
13735/*
13736 * "strlen()" function
13737 */
13738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013739f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013740 typval_T *argvars;
13741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013743 rettv->vval.v_number = (varnumber_T)(STRLEN(
13744 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745}
13746
13747/*
13748 * "strpart()" function
13749 */
13750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013751f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013752 typval_T *argvars;
13753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754{
13755 char_u *p;
13756 int n;
13757 int len;
13758 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013759 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013761 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013762 slen = (int)STRLEN(p);
13763
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013764 n = get_tv_number_chk(&argvars[1], &error);
13765 if (error)
13766 len = 0;
13767 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013768 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013769 else
13770 len = slen - n; /* default len: all bytes that are available. */
13771
13772 /*
13773 * Only return the overlap between the specified part and the actual
13774 * string.
13775 */
13776 if (n < 0)
13777 {
13778 len += n;
13779 n = 0;
13780 }
13781 else if (n > slen)
13782 n = slen;
13783 if (len < 0)
13784 len = 0;
13785 else if (n + len > slen)
13786 len = slen - n;
13787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013788 rettv->v_type = VAR_STRING;
13789 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790}
13791
13792/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013793 * "strridx()" function
13794 */
13795 static void
13796f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013797 typval_T *argvars;
13798 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013799{
13800 char_u buf[NUMBUFLEN];
13801 char_u *needle;
13802 char_u *haystack;
13803 char_u *rest;
13804 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013805 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013806
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013807 needle = get_tv_string_chk(&argvars[1]);
13808 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013809 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013810
13811 rettv->vval.v_number = -1;
13812 if (needle == NULL || haystack == NULL)
13813 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013814 if (argvars[2].v_type != VAR_UNKNOWN)
13815 {
13816 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013817 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000013818 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013819 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013820 }
13821 else
13822 end_idx = haystack_len;
13823
Bram Moolenaar0d660222005-01-07 21:51:51 +000013824 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000013825 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013826 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013827 lastmatch = haystack + end_idx;
13828 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013829 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000013830 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013831 for (rest = haystack; *rest != '\0'; ++rest)
13832 {
13833 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013834 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013835 break;
13836 lastmatch = rest;
13837 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000013838 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013839
13840 if (lastmatch == NULL)
13841 rettv->vval.v_number = -1;
13842 else
13843 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13844}
13845
13846/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847 * "strtrans()" function
13848 */
13849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013850f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013851 typval_T *argvars;
13852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013853{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013854 rettv->v_type = VAR_STRING;
13855 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856}
13857
13858/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013859 * "submatch()" function
13860 */
13861 static void
13862f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013863 typval_T *argvars;
13864 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013865{
13866 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013867 rettv->vval.v_string =
13868 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013869}
13870
13871/*
13872 * "substitute()" function
13873 */
13874 static void
13875f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013876 typval_T *argvars;
13877 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013878{
13879 char_u patbuf[NUMBUFLEN];
13880 char_u subbuf[NUMBUFLEN];
13881 char_u flagsbuf[NUMBUFLEN];
13882
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013883 char_u *str = get_tv_string_chk(&argvars[0]);
13884 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13885 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
13886 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
13887
Bram Moolenaar0d660222005-01-07 21:51:51 +000013888 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013889 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
13890 rettv->vval.v_string = NULL;
13891 else
13892 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013893}
13894
13895/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013896 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013897 */
13898/*ARGSUSED*/
13899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013900f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013901 typval_T *argvars;
13902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903{
13904 int id = 0;
13905#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013906 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907 long col;
13908 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000013909 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013911 lnum = get_tv_lnum(argvars); /* -1 on type error */
13912 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
13913 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013914
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013915 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013916 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13917 id = syn_get_id(lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918#endif
13919
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013920 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921}
13922
13923/*
13924 * "synIDattr(id, what [, mode])" function
13925 */
13926/*ARGSUSED*/
13927 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013928f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013929 typval_T *argvars;
13930 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013931{
13932 char_u *p = NULL;
13933#ifdef FEAT_SYN_HL
13934 int id;
13935 char_u *what;
13936 char_u *mode;
13937 char_u modebuf[NUMBUFLEN];
13938 int modec;
13939
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013940 id = get_tv_number(&argvars[0]);
13941 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013942 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013943 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013944 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945 modec = TOLOWER_ASC(mode[0]);
13946 if (modec != 't' && modec != 'c'
13947#ifdef FEAT_GUI
13948 && modec != 'g'
13949#endif
13950 )
13951 modec = 0; /* replace invalid with current */
13952 }
13953 else
13954 {
13955#ifdef FEAT_GUI
13956 if (gui.in_use)
13957 modec = 'g';
13958 else
13959#endif
13960 if (t_colors > 1)
13961 modec = 'c';
13962 else
13963 modec = 't';
13964 }
13965
13966
13967 switch (TOLOWER_ASC(what[0]))
13968 {
13969 case 'b':
13970 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
13971 p = highlight_color(id, what, modec);
13972 else /* bold */
13973 p = highlight_has_attr(id, HL_BOLD, modec);
13974 break;
13975
13976 case 'f': /* fg[#] */
13977 p = highlight_color(id, what, modec);
13978 break;
13979
13980 case 'i':
13981 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
13982 p = highlight_has_attr(id, HL_INVERSE, modec);
13983 else /* italic */
13984 p = highlight_has_attr(id, HL_ITALIC, modec);
13985 break;
13986
13987 case 'n': /* name */
13988 p = get_highlight_name(NULL, id - 1);
13989 break;
13990
13991 case 'r': /* reverse */
13992 p = highlight_has_attr(id, HL_INVERSE, modec);
13993 break;
13994
13995 case 's': /* standout */
13996 p = highlight_has_attr(id, HL_STANDOUT, modec);
13997 break;
13998
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000013999 case 'u':
14000 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14001 /* underline */
14002 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14003 else
14004 /* undercurl */
14005 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006 break;
14007 }
14008
14009 if (p != NULL)
14010 p = vim_strsave(p);
14011#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014012 rettv->v_type = VAR_STRING;
14013 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014}
14015
14016/*
14017 * "synIDtrans(id)" function
14018 */
14019/*ARGSUSED*/
14020 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014021f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014022 typval_T *argvars;
14023 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024{
14025 int id;
14026
14027#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014028 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029
14030 if (id > 0)
14031 id = syn_get_final_id(id);
14032 else
14033#endif
14034 id = 0;
14035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014036 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037}
14038
14039/*
14040 * "system()" function
14041 */
14042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014043f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014044 typval_T *argvars;
14045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014047 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014049 char_u *infile = NULL;
14050 char_u buf[NUMBUFLEN];
14051 int err = FALSE;
14052 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014054 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014055 {
14056 /*
14057 * Write the string to a temp file, to be used for input of the shell
14058 * command.
14059 */
14060 if ((infile = vim_tempname('i')) == NULL)
14061 {
14062 EMSG(_(e_notmp));
14063 return;
14064 }
14065
14066 fd = mch_fopen((char *)infile, WRITEBIN);
14067 if (fd == NULL)
14068 {
14069 EMSG2(_(e_notopen), infile);
14070 goto done;
14071 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014072 p = get_tv_string_buf_chk(&argvars[1], buf);
14073 if (p == NULL)
14074 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014075 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14076 err = TRUE;
14077 if (fclose(fd) != 0)
14078 err = TRUE;
14079 if (err)
14080 {
14081 EMSG(_("E677: Error writing temp file"));
14082 goto done;
14083 }
14084 }
14085
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014086 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014087
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088#ifdef USE_CR
14089 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014090 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091 {
14092 char_u *s;
14093
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014094 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014095 {
14096 if (*s == CAR)
14097 *s = NL;
14098 }
14099 }
14100#else
14101# ifdef USE_CRNL
14102 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014103 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014104 {
14105 char_u *s, *d;
14106
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014107 d = res;
14108 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014109 {
14110 if (s[0] == CAR && s[1] == NL)
14111 ++s;
14112 *d++ = *s;
14113 }
14114 *d = NUL;
14115 }
14116# endif
14117#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014118
14119done:
14120 if (infile != NULL)
14121 {
14122 mch_remove(infile);
14123 vim_free(infile);
14124 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014125 rettv->v_type = VAR_STRING;
14126 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127}
14128
14129/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014130 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014131 */
14132 static void
14133f_taglist(argvars, rettv)
14134 typval_T *argvars;
14135 typval_T *rettv;
14136{
14137 char_u *tag_pattern;
14138 list_T *l;
14139
14140 tag_pattern = get_tv_string(&argvars[0]);
14141
14142 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014143 if (*tag_pattern == NUL)
14144 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014145
14146 l = list_alloc();
14147 if (l != NULL)
14148 {
14149 if (get_tags(l, tag_pattern) != FAIL)
14150 {
14151 rettv->vval.v_list = l;
14152 rettv->v_type = VAR_LIST;
14153 ++l->lv_refcount;
14154 }
14155 else
14156 list_free(l);
14157 }
14158}
14159
14160/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161 * "tempname()" function
14162 */
14163/*ARGSUSED*/
14164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014165f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014166 typval_T *argvars;
14167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168{
14169 static int x = 'A';
14170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014171 rettv->v_type = VAR_STRING;
14172 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173
14174 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14175 * names. Skip 'I' and 'O', they are used for shell redirection. */
14176 do
14177 {
14178 if (x == 'Z')
14179 x = '0';
14180 else if (x == '9')
14181 x = 'A';
14182 else
14183 {
14184#ifdef EBCDIC
14185 if (x == 'I')
14186 x = 'J';
14187 else if (x == 'R')
14188 x = 'S';
14189 else
14190#endif
14191 ++x;
14192 }
14193 } while (x == 'I' || x == 'O');
14194}
14195
14196/*
14197 * "tolower(string)" function
14198 */
14199 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014200f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014201 typval_T *argvars;
14202 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014203{
14204 char_u *p;
14205
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014206 p = vim_strsave(get_tv_string(&argvars[0]));
14207 rettv->v_type = VAR_STRING;
14208 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014209
14210 if (p != NULL)
14211 while (*p != NUL)
14212 {
14213#ifdef FEAT_MBYTE
14214 int l;
14215
14216 if (enc_utf8)
14217 {
14218 int c, lc;
14219
14220 c = utf_ptr2char(p);
14221 lc = utf_tolower(c);
14222 l = utf_ptr2len_check(p);
14223 /* TODO: reallocate string when byte count changes. */
14224 if (utf_char2len(lc) == l)
14225 utf_char2bytes(lc, p);
14226 p += l;
14227 }
14228 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
14229 p += l; /* skip multi-byte character */
14230 else
14231#endif
14232 {
14233 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14234 ++p;
14235 }
14236 }
14237}
14238
14239/*
14240 * "toupper(string)" function
14241 */
14242 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014243f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014244 typval_T *argvars;
14245 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014247 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014248 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249}
14250
14251/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014252 * "tr(string, fromstr, tostr)" function
14253 */
14254 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014255f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014256 typval_T *argvars;
14257 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014258{
14259 char_u *instr;
14260 char_u *fromstr;
14261 char_u *tostr;
14262 char_u *p;
14263#ifdef FEAT_MBYTE
14264 int inlen;
14265 int fromlen;
14266 int tolen;
14267 int idx;
14268 char_u *cpstr;
14269 int cplen;
14270 int first = TRUE;
14271#endif
14272 char_u buf[NUMBUFLEN];
14273 char_u buf2[NUMBUFLEN];
14274 garray_T ga;
14275
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014276 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014277 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14278 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014279
14280 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014281 rettv->v_type = VAR_STRING;
14282 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014283 if (fromstr == NULL || tostr == NULL)
14284 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014285 ga_init2(&ga, (int)sizeof(char), 80);
14286
14287#ifdef FEAT_MBYTE
14288 if (!has_mbyte)
14289#endif
14290 /* not multi-byte: fromstr and tostr must be the same length */
14291 if (STRLEN(fromstr) != STRLEN(tostr))
14292 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014293#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014294error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014295#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014296 EMSG2(_(e_invarg2), fromstr);
14297 ga_clear(&ga);
14298 return;
14299 }
14300
14301 /* fromstr and tostr have to contain the same number of chars */
14302 while (*instr != NUL)
14303 {
14304#ifdef FEAT_MBYTE
14305 if (has_mbyte)
14306 {
14307 inlen = mb_ptr2len_check(instr);
14308 cpstr = instr;
14309 cplen = inlen;
14310 idx = 0;
14311 for (p = fromstr; *p != NUL; p += fromlen)
14312 {
14313 fromlen = mb_ptr2len_check(p);
14314 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14315 {
14316 for (p = tostr; *p != NUL; p += tolen)
14317 {
14318 tolen = mb_ptr2len_check(p);
14319 if (idx-- == 0)
14320 {
14321 cplen = tolen;
14322 cpstr = p;
14323 break;
14324 }
14325 }
14326 if (*p == NUL) /* tostr is shorter than fromstr */
14327 goto error;
14328 break;
14329 }
14330 ++idx;
14331 }
14332
14333 if (first && cpstr == instr)
14334 {
14335 /* Check that fromstr and tostr have the same number of
14336 * (multi-byte) characters. Done only once when a character
14337 * of instr doesn't appear in fromstr. */
14338 first = FALSE;
14339 for (p = tostr; *p != NUL; p += tolen)
14340 {
14341 tolen = mb_ptr2len_check(p);
14342 --idx;
14343 }
14344 if (idx != 0)
14345 goto error;
14346 }
14347
14348 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014349 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014350 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014351
14352 instr += inlen;
14353 }
14354 else
14355#endif
14356 {
14357 /* When not using multi-byte chars we can do it faster. */
14358 p = vim_strchr(fromstr, *instr);
14359 if (p != NULL)
14360 ga_append(&ga, tostr[p - fromstr]);
14361 else
14362 ga_append(&ga, *instr);
14363 ++instr;
14364 }
14365 }
14366
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014367 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014368}
14369
14370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371 * "type(expr)" function
14372 */
14373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014374f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014375 typval_T *argvars;
14376 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014377{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014378 int n;
14379
14380 switch (argvars[0].v_type)
14381 {
14382 case VAR_NUMBER: n = 0; break;
14383 case VAR_STRING: n = 1; break;
14384 case VAR_FUNC: n = 2; break;
14385 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014386 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014387 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14388 }
14389 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390}
14391
14392/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014393 * "values(dict)" function
14394 */
14395 static void
14396f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014397 typval_T *argvars;
14398 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014399{
14400 dict_list(argvars, rettv, 1);
14401}
14402
14403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404 * "virtcol(string)" function
14405 */
14406 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014407f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014408 typval_T *argvars;
14409 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410{
14411 colnr_T vcol = 0;
14412 pos_T *fp;
14413
14414 fp = var2fpos(&argvars[0], FALSE);
14415 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14416 {
14417 getvvcol(curwin, fp, NULL, NULL, &vcol);
14418 ++vcol;
14419 }
14420
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014421 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422}
14423
14424/*
14425 * "visualmode()" function
14426 */
14427/*ARGSUSED*/
14428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014429f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014430 typval_T *argvars;
14431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432{
14433#ifdef FEAT_VISUAL
14434 char_u str[2];
14435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014436 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437 str[0] = curbuf->b_visual_mode_eval;
14438 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014439 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440
14441 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014442 if ((argvars[0].v_type == VAR_NUMBER
14443 && argvars[0].vval.v_number != 0)
14444 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014445 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446 curbuf->b_visual_mode_eval = NUL;
14447#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014448 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449#endif
14450}
14451
14452/*
14453 * "winbufnr(nr)" function
14454 */
14455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014456f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014457 typval_T *argvars;
14458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459{
14460 win_T *wp;
14461
14462 wp = find_win_by_nr(&argvars[0]);
14463 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014464 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014466 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467}
14468
14469/*
14470 * "wincol()" function
14471 */
14472/*ARGSUSED*/
14473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014474f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014475 typval_T *argvars;
14476 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477{
14478 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014479 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480}
14481
14482/*
14483 * "winheight(nr)" function
14484 */
14485 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014486f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014487 typval_T *argvars;
14488 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489{
14490 win_T *wp;
14491
14492 wp = find_win_by_nr(&argvars[0]);
14493 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014494 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014496 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014497}
14498
14499/*
14500 * "winline()" function
14501 */
14502/*ARGSUSED*/
14503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014504f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014505 typval_T *argvars;
14506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507{
14508 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014509 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510}
14511
14512/*
14513 * "winnr()" function
14514 */
14515/* ARGSUSED */
14516 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014517f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014518 typval_T *argvars;
14519 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520{
14521 int nr = 1;
14522#ifdef FEAT_WINDOWS
14523 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014524 win_T *twin = curwin;
14525 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014527 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014528 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014529 arg = get_tv_string_chk(&argvars[0]);
14530 if (arg == NULL)
14531 nr = 0; /* type error; errmsg already given */
14532 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014533 twin = lastwin;
14534 else if (STRCMP(arg, "#") == 0)
14535 {
14536 twin = prevwin;
14537 if (prevwin == NULL)
14538 nr = 0;
14539 }
14540 else
14541 {
14542 EMSG2(_(e_invexpr2), arg);
14543 nr = 0;
14544 }
14545 }
14546
14547 if (nr > 0)
14548 for (wp = firstwin; wp != twin; wp = wp->w_next)
14549 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014551 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552}
14553
14554/*
14555 * "winrestcmd()" function
14556 */
14557/* ARGSUSED */
14558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014559f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014560 typval_T *argvars;
14561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562{
14563#ifdef FEAT_WINDOWS
14564 win_T *wp;
14565 int winnr = 1;
14566 garray_T ga;
14567 char_u buf[50];
14568
14569 ga_init2(&ga, (int)sizeof(char), 70);
14570 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14571 {
14572 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14573 ga_concat(&ga, buf);
14574# ifdef FEAT_VERTSPLIT
14575 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14576 ga_concat(&ga, buf);
14577# endif
14578 ++winnr;
14579 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000014580 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014581
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014582 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014584 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014586 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014587}
14588
14589/*
14590 * "winwidth(nr)" function
14591 */
14592 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014593f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014594 typval_T *argvars;
14595 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596{
14597 win_T *wp;
14598
14599 wp = find_win_by_nr(&argvars[0]);
14600 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014601 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014602 else
14603#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014604 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014606 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014607#endif
14608}
14609
14610 static win_T *
14611find_win_by_nr(vp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014612 typval_T *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014613{
14614#ifdef FEAT_WINDOWS
14615 win_T *wp;
14616#endif
14617 int nr;
14618
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014619 nr = get_tv_number_chk(vp, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014620
14621#ifdef FEAT_WINDOWS
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014622 if (nr < 0)
14623 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014624 if (nr == 0)
14625 return curwin;
14626
14627 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14628 if (--nr <= 0)
14629 break;
14630 return wp;
14631#else
14632 if (nr == 0 || nr == 1)
14633 return curwin;
14634 return NULL;
14635#endif
14636}
14637
14638/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014639 * "writefile()" function
14640 */
14641 static void
14642f_writefile(argvars, rettv)
14643 typval_T *argvars;
14644 typval_T *rettv;
14645{
14646 int binary = FALSE;
14647 char_u *fname;
14648 FILE *fd;
14649 listitem_T *li;
14650 char_u *s;
14651 int ret = 0;
14652 int c;
14653
14654 if (argvars[0].v_type != VAR_LIST)
14655 {
14656 EMSG2(_(e_listarg), "writefile()");
14657 return;
14658 }
14659 if (argvars[0].vval.v_list == NULL)
14660 return;
14661
14662 if (argvars[2].v_type != VAR_UNKNOWN
14663 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
14664 binary = TRUE;
14665
14666 /* Always open the file in binary mode, library functions have a mind of
14667 * their own about CR-LF conversion. */
14668 fname = get_tv_string(&argvars[1]);
14669 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
14670 {
14671 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14672 ret = -1;
14673 }
14674 else
14675 {
14676 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
14677 li = li->li_next)
14678 {
14679 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
14680 {
14681 if (*s == '\n')
14682 c = putc(NUL, fd);
14683 else
14684 c = putc(*s, fd);
14685 if (c == EOF)
14686 {
14687 ret = -1;
14688 break;
14689 }
14690 }
14691 if (!binary || li->li_next != NULL)
14692 if (putc('\n', fd) == EOF)
14693 {
14694 ret = -1;
14695 break;
14696 }
14697 if (ret < 0)
14698 {
14699 EMSG(_(e_write));
14700 break;
14701 }
14702 }
14703 fclose(fd);
14704 }
14705
14706 rettv->vval.v_number = ret;
14707}
14708
14709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014710 * Translate a String variable into a position.
14711 */
14712 static pos_T *
14713var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000014714 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014715 int lnum; /* TRUE when $ is last line */
14716{
14717 char_u *name;
14718 static pos_T pos;
14719 pos_T *pp;
14720
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014721 name = get_tv_string_chk(varp);
14722 if (name == NULL)
14723 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014724 if (name[0] == '.') /* cursor */
14725 return &curwin->w_cursor;
14726 if (name[0] == '\'') /* mark */
14727 {
14728 pp = getmark(name[1], FALSE);
14729 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
14730 return NULL;
14731 return pp;
14732 }
14733 if (name[0] == '$') /* last column or line */
14734 {
14735 if (lnum)
14736 {
14737 pos.lnum = curbuf->b_ml.ml_line_count;
14738 pos.col = 0;
14739 }
14740 else
14741 {
14742 pos.lnum = curwin->w_cursor.lnum;
14743 pos.col = (colnr_T)STRLEN(ml_get_curline());
14744 }
14745 return &pos;
14746 }
14747 return NULL;
14748}
14749
14750/*
14751 * Get the length of an environment variable name.
14752 * Advance "arg" to the first character after the name.
14753 * Return 0 for error.
14754 */
14755 static int
14756get_env_len(arg)
14757 char_u **arg;
14758{
14759 char_u *p;
14760 int len;
14761
14762 for (p = *arg; vim_isIDc(*p); ++p)
14763 ;
14764 if (p == *arg) /* no name found */
14765 return 0;
14766
14767 len = (int)(p - *arg);
14768 *arg = p;
14769 return len;
14770}
14771
14772/*
14773 * Get the length of the name of a function or internal variable.
14774 * "arg" is advanced to the first non-white character after the name.
14775 * Return 0 if something is wrong.
14776 */
14777 static int
14778get_id_len(arg)
14779 char_u **arg;
14780{
14781 char_u *p;
14782 int len;
14783
14784 /* Find the end of the name. */
14785 for (p = *arg; eval_isnamec(*p); ++p)
14786 ;
14787 if (p == *arg) /* no name found */
14788 return 0;
14789
14790 len = (int)(p - *arg);
14791 *arg = skipwhite(p);
14792
14793 return len;
14794}
14795
14796/*
Bram Moolenaara7043832005-01-21 11:56:39 +000014797 * Get the length of the name of a variable or function.
14798 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014799 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014800 * Return -1 if curly braces expansion failed.
14801 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014802 * If the name contains 'magic' {}'s, expand them and return the
14803 * expanded name in an allocated string via 'alias' - caller must free.
14804 */
14805 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014806get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014807 char_u **arg;
14808 char_u **alias;
14809 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014810 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014811{
14812 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014813 char_u *p;
14814 char_u *expr_start;
14815 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014816
14817 *alias = NULL; /* default to no alias */
14818
14819 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
14820 && (*arg)[2] == (int)KE_SNR)
14821 {
14822 /* hard coded <SNR>, already translated */
14823 *arg += 3;
14824 return get_id_len(arg) + 3;
14825 }
14826 len = eval_fname_script(*arg);
14827 if (len > 0)
14828 {
14829 /* literal "<SID>", "s:" or "<SNR>" */
14830 *arg += len;
14831 }
14832
Bram Moolenaar071d4272004-06-13 20:20:40 +000014833 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014834 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014836 p = find_name_end(*arg, &expr_start, &expr_end,
14837 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838 if (expr_start != NULL)
14839 {
14840 char_u *temp_string;
14841
14842 if (!evaluate)
14843 {
14844 len += (int)(p - *arg);
14845 *arg = skipwhite(p);
14846 return len;
14847 }
14848
14849 /*
14850 * Include any <SID> etc in the expanded string:
14851 * Thus the -len here.
14852 */
14853 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
14854 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014855 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856 *alias = temp_string;
14857 *arg = skipwhite(p);
14858 return (int)STRLEN(temp_string);
14859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014860
14861 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014862 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863 EMSG2(_(e_invexpr2), *arg);
14864
14865 return len;
14866}
14867
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014868/*
14869 * Find the end of a variable or function name, taking care of magic braces.
14870 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
14871 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014872 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014873 * Return a pointer to just after the name. Equal to "arg" if there is no
14874 * valid name.
14875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014876 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014877find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014878 char_u *arg;
14879 char_u **expr_start;
14880 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014881 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014882{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014883 int mb_nest = 0;
14884 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014885 char_u *p;
14886
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014887 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014888 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014889 *expr_start = NULL;
14890 *expr_end = NULL;
14891 }
14892
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014893 /* Quick check for valid starting character. */
14894 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
14895 return arg;
14896
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014897 for (p = arg; *p != NUL
14898 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014899 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014900 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014901 || mb_nest != 0
14902 || br_nest != 0); ++p)
14903 {
14904 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014905 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014906 if (*p == '[')
14907 ++br_nest;
14908 else if (*p == ']')
14909 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014910 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014911 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014912 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014913 if (*p == '{')
14914 {
14915 mb_nest++;
14916 if (expr_start != NULL && *expr_start == NULL)
14917 *expr_start = p;
14918 }
14919 else if (*p == '}')
14920 {
14921 mb_nest--;
14922 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
14923 *expr_end = p;
14924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014926 }
14927
14928 return p;
14929}
14930
14931/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014932 * Expands out the 'magic' {}'s in a variable/function name.
14933 * Note that this can call itself recursively, to deal with
14934 * constructs like foo{bar}{baz}{bam}
14935 * The four pointer arguments point to "foo{expre}ss{ion}bar"
14936 * "in_start" ^
14937 * "expr_start" ^
14938 * "expr_end" ^
14939 * "in_end" ^
14940 *
14941 * Returns a new allocated string, which the caller must free.
14942 * Returns NULL for failure.
14943 */
14944 static char_u *
14945make_expanded_name(in_start, expr_start, expr_end, in_end)
14946 char_u *in_start;
14947 char_u *expr_start;
14948 char_u *expr_end;
14949 char_u *in_end;
14950{
14951 char_u c1;
14952 char_u *retval = NULL;
14953 char_u *temp_result;
14954 char_u *nextcmd = NULL;
14955
14956 if (expr_end == NULL || in_end == NULL)
14957 return NULL;
14958 *expr_start = NUL;
14959 *expr_end = NUL;
14960 c1 = *in_end;
14961 *in_end = NUL;
14962
14963 temp_result = eval_to_string(expr_start + 1, &nextcmd);
14964 if (temp_result != NULL && nextcmd == NULL)
14965 {
14966 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
14967 + (in_end - expr_end) + 1));
14968 if (retval != NULL)
14969 {
14970 STRCPY(retval, in_start);
14971 STRCAT(retval, temp_result);
14972 STRCAT(retval, expr_end + 1);
14973 }
14974 }
14975 vim_free(temp_result);
14976
14977 *in_end = c1; /* put char back for error messages */
14978 *expr_start = '{';
14979 *expr_end = '}';
14980
14981 if (retval != NULL)
14982 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014983 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014984 if (expr_start != NULL)
14985 {
14986 /* Further expansion! */
14987 temp_result = make_expanded_name(retval, expr_start,
14988 expr_end, temp_result);
14989 vim_free(retval);
14990 retval = temp_result;
14991 }
14992 }
14993
14994 return retval;
14995}
14996
14997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014998 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000014999 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000 */
15001 static int
15002eval_isnamec(c)
15003 int c;
15004{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015005 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15006}
15007
15008/*
15009 * Return TRUE if character "c" can be used as the first character in a
15010 * variable or function name (excluding '{' and '}').
15011 */
15012 static int
15013eval_isnamec1(c)
15014 int c;
15015{
15016 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015017}
15018
15019/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015020 * Set number v: variable to "val".
15021 */
15022 void
15023set_vim_var_nr(idx, val)
15024 int idx;
15025 long val;
15026{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015027 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015028}
15029
15030/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015031 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032 */
15033 long
15034get_vim_var_nr(idx)
15035 int idx;
15036{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015037 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038}
15039
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015040#if defined(FEAT_AUTOCMD) || defined(PROTO)
15041/*
15042 * Get string v: variable value. Uses a static buffer, can only be used once.
15043 */
15044 char_u *
15045get_vim_var_str(idx)
15046 int idx;
15047{
15048 return get_tv_string(&vimvars[idx].vv_tv);
15049}
15050#endif
15051
Bram Moolenaar071d4272004-06-13 20:20:40 +000015052/*
15053 * Set v:count, v:count1 and v:prevcount.
15054 */
15055 void
15056set_vcount(count, count1)
15057 long count;
15058 long count1;
15059{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015060 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15061 vimvars[VV_COUNT].vv_nr = count;
15062 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015063}
15064
15065/*
15066 * Set string v: variable to a copy of "val".
15067 */
15068 void
15069set_vim_var_string(idx, val, len)
15070 int idx;
15071 char_u *val;
15072 int len; /* length of "val" to use or -1 (whole string) */
15073{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015074 /* Need to do this (at least) once, since we can't initialize a union.
15075 * Will always be invoked when "v:progname" is set. */
15076 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15077
Bram Moolenaare9a41262005-01-15 22:18:47 +000015078 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015079 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015080 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015081 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015082 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015083 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015084 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015085}
15086
15087/*
15088 * Set v:register if needed.
15089 */
15090 void
15091set_reg_var(c)
15092 int c;
15093{
15094 char_u regname;
15095
15096 if (c == 0 || c == ' ')
15097 regname = '"';
15098 else
15099 regname = c;
15100 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015101 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015102 set_vim_var_string(VV_REG, &regname, 1);
15103}
15104
15105/*
15106 * Get or set v:exception. If "oldval" == NULL, return the current value.
15107 * Otherwise, restore the value to "oldval" and return NULL.
15108 * Must always be called in pairs to save and restore v:exception! Does not
15109 * take care of memory allocations.
15110 */
15111 char_u *
15112v_exception(oldval)
15113 char_u *oldval;
15114{
15115 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015116 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015117
Bram Moolenaare9a41262005-01-15 22:18:47 +000015118 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119 return NULL;
15120}
15121
15122/*
15123 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15124 * Otherwise, restore the value to "oldval" and return NULL.
15125 * Must always be called in pairs to save and restore v:throwpoint! Does not
15126 * take care of memory allocations.
15127 */
15128 char_u *
15129v_throwpoint(oldval)
15130 char_u *oldval;
15131{
15132 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015133 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015134
Bram Moolenaare9a41262005-01-15 22:18:47 +000015135 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015136 return NULL;
15137}
15138
15139#if defined(FEAT_AUTOCMD) || defined(PROTO)
15140/*
15141 * Set v:cmdarg.
15142 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15143 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15144 * Must always be called in pairs!
15145 */
15146 char_u *
15147set_cmdarg(eap, oldarg)
15148 exarg_T *eap;
15149 char_u *oldarg;
15150{
15151 char_u *oldval;
15152 char_u *newval;
15153 unsigned len;
15154
Bram Moolenaare9a41262005-01-15 22:18:47 +000015155 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015156 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015158 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015159 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015160 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161 }
15162
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015163 if (eap->force_bin == FORCE_BIN)
15164 len = 6;
15165 else if (eap->force_bin == FORCE_NOBIN)
15166 len = 8;
15167 else
15168 len = 0;
15169 if (eap->force_ff != 0)
15170 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15171# ifdef FEAT_MBYTE
15172 if (eap->force_enc != 0)
15173 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
15174# endif
15175
15176 newval = alloc(len + 1);
15177 if (newval == NULL)
15178 return NULL;
15179
15180 if (eap->force_bin == FORCE_BIN)
15181 sprintf((char *)newval, " ++bin");
15182 else if (eap->force_bin == FORCE_NOBIN)
15183 sprintf((char *)newval, " ++nobin");
15184 else
15185 *newval = NUL;
15186 if (eap->force_ff != 0)
15187 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15188 eap->cmd + eap->force_ff);
15189# ifdef FEAT_MBYTE
15190 if (eap->force_enc != 0)
15191 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15192 eap->cmd + eap->force_enc);
15193# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015194 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015195 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015196}
15197#endif
15198
15199/*
15200 * Get the value of internal variable "name".
15201 * Return OK or FAIL.
15202 */
15203 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015204get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205 char_u *name;
15206 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015207 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015208 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209{
15210 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015211 typval_T *tv = NULL;
15212 typval_T atv;
15213 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015214 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015215
15216 /* truncate the name, so that we can use strcmp() */
15217 cc = name[len];
15218 name[len] = NUL;
15219
15220 /*
15221 * Check for "b:changedtick".
15222 */
15223 if (STRCMP(name, "b:changedtick") == 0)
15224 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015225 atv.v_type = VAR_NUMBER;
15226 atv.vval.v_number = curbuf->b_changedtick;
15227 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015228 }
15229
15230 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015231 * Check for user-defined variables.
15232 */
15233 else
15234 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015235 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015236 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015237 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015238 }
15239
Bram Moolenaare9a41262005-01-15 22:18:47 +000015240 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015242 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015243 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244 ret = FAIL;
15245 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015246 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015247 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015248
15249 name[len] = cc;
15250
15251 return ret;
15252}
15253
15254/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015255 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15256 * Also handle function call with Funcref variable: func(expr)
15257 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15258 */
15259 static int
15260handle_subscript(arg, rettv, evaluate, verbose)
15261 char_u **arg;
15262 typval_T *rettv;
15263 int evaluate; /* do more than finding the end */
15264 int verbose; /* give error messages */
15265{
15266 int ret = OK;
15267 dict_T *selfdict = NULL;
15268 char_u *s;
15269 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015270 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015271
15272 while (ret == OK
15273 && (**arg == '['
15274 || (**arg == '.' && rettv->v_type == VAR_DICT)
15275 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15276 && !vim_iswhite(*(*arg - 1)))
15277 {
15278 if (**arg == '(')
15279 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015280 /* need to copy the funcref so that we can clear rettv */
15281 functv = *rettv;
15282 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015283
15284 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015285 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015286 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015287 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15288 &len, evaluate, selfdict);
15289
15290 /* Clear the funcref afterwards, so that deleting it while
15291 * evaluating the arguments is possible (see test55). */
15292 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015293
15294 /* Stop the expression evaluation when immediately aborting on
15295 * error, or when an interrupt occurred or an exception was thrown
15296 * but not caught. */
15297 if (aborting())
15298 {
15299 if (ret == OK)
15300 clear_tv(rettv);
15301 ret = FAIL;
15302 }
15303 dict_unref(selfdict);
15304 selfdict = NULL;
15305 }
15306 else /* **arg == '[' || **arg == '.' */
15307 {
15308 dict_unref(selfdict);
15309 if (rettv->v_type == VAR_DICT)
15310 {
15311 selfdict = rettv->vval.v_dict;
15312 if (selfdict != NULL)
15313 ++selfdict->dv_refcount;
15314 }
15315 else
15316 selfdict = NULL;
15317 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15318 {
15319 clear_tv(rettv);
15320 ret = FAIL;
15321 }
15322 }
15323 }
15324 dict_unref(selfdict);
15325 return ret;
15326}
15327
15328/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015329 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15330 * value).
15331 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015332 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015333alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015334{
Bram Moolenaar33570922005-01-25 22:26:29 +000015335 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015336}
15337
15338/*
15339 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015340 * The string "s" must have been allocated, it is consumed.
15341 * Return NULL for out of memory, the variable otherwise.
15342 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015343 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015344alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345 char_u *s;
15346{
Bram Moolenaar33570922005-01-25 22:26:29 +000015347 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015349 rettv = alloc_tv();
15350 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015352 rettv->v_type = VAR_STRING;
15353 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015354 }
15355 else
15356 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015357 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358}
15359
15360/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015361 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362 */
15363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015364free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015365 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366{
15367 if (varp != NULL)
15368 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015369 switch (varp->v_type)
15370 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015371 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015372 func_unref(varp->vval.v_string);
15373 /*FALLTHROUGH*/
15374 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015375 vim_free(varp->vval.v_string);
15376 break;
15377 case VAR_LIST:
15378 list_unref(varp->vval.v_list);
15379 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015380 case VAR_DICT:
15381 dict_unref(varp->vval.v_dict);
15382 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015383 case VAR_NUMBER:
15384 case VAR_UNKNOWN:
15385 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015386 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015387 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015388 break;
15389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390 vim_free(varp);
15391 }
15392}
15393
15394/*
15395 * Free the memory for a variable value and set the value to NULL or 0.
15396 */
15397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015398clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015399 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400{
15401 if (varp != NULL)
15402 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015403 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015404 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015405 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015406 func_unref(varp->vval.v_string);
15407 /*FALLTHROUGH*/
15408 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015409 vim_free(varp->vval.v_string);
15410 varp->vval.v_string = NULL;
15411 break;
15412 case VAR_LIST:
15413 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015414 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015415 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015416 case VAR_DICT:
15417 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015418 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015419 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015420 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015421 varp->vval.v_number = 0;
15422 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015423 case VAR_UNKNOWN:
15424 break;
15425 default:
15426 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015428 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015429 }
15430}
15431
15432/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015433 * Set the value of a variable to NULL without freeing items.
15434 */
15435 static void
15436init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015437 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015438{
15439 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015440 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015441}
15442
15443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444 * Get the number value of a variable.
15445 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015446 * For incompatible types, return 0.
15447 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15448 * caller of incompatible types: it sets *denote to TRUE if "denote"
15449 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450 */
15451 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015452get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015453 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015454{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015455 int error = FALSE;
15456
15457 return get_tv_number_chk(varp, &error); /* return 0L on error */
15458}
15459
15460 static long
15461get_tv_number_chk(varp, denote)
15462 typval_T *varp;
15463 int *denote;
15464{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015465 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015467 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015469 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015470 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015471 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015472 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015473 break;
15474 case VAR_STRING:
15475 if (varp->vval.v_string != NULL)
15476 vim_str2nr(varp->vval.v_string, NULL, NULL,
15477 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015478 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015479 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015480 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015481 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015482 case VAR_DICT:
15483 EMSG(_("E728: Using a Dictionary as a number"));
15484 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015485 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015486 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015487 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015488 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015489 if (denote == NULL) /* useful for values that must be unsigned */
15490 n = -1;
15491 else
15492 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015493 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494}
15495
15496/*
15497 * Get the lnum from the first argument. Also accepts ".", "$", etc.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015498 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015499 */
15500 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015501get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000015502 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015503{
Bram Moolenaar33570922005-01-25 22:26:29 +000015504 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015505 linenr_T lnum;
15506
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015507 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508 if (lnum == 0) /* no valid number, try using line() */
15509 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015510 rettv.v_type = VAR_NUMBER;
15511 f_line(argvars, &rettv);
15512 lnum = rettv.vval.v_number;
15513 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514 }
15515 return lnum;
15516}
15517
15518/*
15519 * Get the string value of a variable.
15520 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000015521 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
15522 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523 * If the String variable has never been set, return an empty string.
15524 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015525 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
15526 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015527 */
15528 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015529get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015530 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015531{
15532 static char_u mybuf[NUMBUFLEN];
15533
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015534 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535}
15536
15537 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015538get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000015539 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015540 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015541{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015542 char_u *res = get_tv_string_buf_chk(varp, buf);
15543
15544 return res != NULL ? res : (char_u *)"";
15545}
15546
15547 static char_u *
15548get_tv_string_chk(varp)
15549 typval_T *varp;
15550{
15551 static char_u mybuf[NUMBUFLEN];
15552
15553 return get_tv_string_buf_chk(varp, mybuf);
15554}
15555
15556 static char_u *
15557get_tv_string_buf_chk(varp, buf)
15558 typval_T *varp;
15559 char_u *buf;
15560{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015561 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015563 case VAR_NUMBER:
15564 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
15565 return buf;
15566 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015567 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015568 break;
15569 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015570 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015571 break;
15572 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015573 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015574 break;
15575 case VAR_STRING:
15576 if (varp->vval.v_string != NULL)
15577 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015578 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015579 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015580 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015581 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015583 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584}
15585
15586/*
15587 * Find variable "name" in the list of variables.
15588 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015589 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015590 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000015591 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015592 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015593 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015594find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015595 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015596 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015597{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015599 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015600
Bram Moolenaara7043832005-01-21 11:56:39 +000015601 ht = find_var_ht(name, &varname);
15602 if (htp != NULL)
15603 *htp = ht;
15604 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015605 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015606 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607}
15608
15609/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015610 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000015611 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015612 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015613 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015614find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000015615 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000015616 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015617 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000015618{
Bram Moolenaar33570922005-01-25 22:26:29 +000015619 hashitem_T *hi;
15620
15621 if (*varname == NUL)
15622 {
15623 /* Must be something like "s:", otherwise "ht" would be NULL. */
15624 switch (varname[-2])
15625 {
15626 case 's': return &SCRIPT_SV(current_SID).sv_var;
15627 case 'g': return &globvars_var;
15628 case 'v': return &vimvars_var;
15629 case 'b': return &curbuf->b_bufvar;
15630 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015631 case 'l': return current_funccal == NULL
15632 ? NULL : &current_funccal->l_vars_var;
15633 case 'a': return current_funccal == NULL
15634 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000015635 }
15636 return NULL;
15637 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015638
15639 hi = hash_find(ht, varname);
15640 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015641 {
15642 /* For global variables we may try auto-loading the script. If it
15643 * worked find the variable again. */
15644 if (ht == &globvarht && !writing
15645 && script_autoload(varname) && !aborting())
15646 hi = hash_find(ht, varname);
15647 if (HASHITEM_EMPTY(hi))
15648 return NULL;
15649 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015650 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015651}
15652
15653/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015654 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015655 * Set "varname" to the start of name without ':'.
15656 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015657 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015658find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015659 char_u *name;
15660 char_u **varname;
15661{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015662 hashitem_T *hi;
15663
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664 if (name[1] != ':')
15665 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015666 /* The name must not start with a colon or #. */
15667 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 return NULL;
15669 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015670
15671 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015672 hi = hash_find(&compat_hashtab, name);
15673 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000015674 return &compat_hashtab;
15675
Bram Moolenaar071d4272004-06-13 20:20:40 +000015676 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015677 return &globvarht; /* global variable */
15678 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015679 }
15680 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015681 if (*name == 'g') /* global variable */
15682 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015683 /* There must be no ':' or '#' in the rest of the name, unless g: is used
15684 */
15685 if (vim_strchr(name + 2, ':') != NULL
15686 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015687 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015688 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015689 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015691 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000015692 if (*name == 'v') /* v: variable */
15693 return &vimvarht;
15694 if (*name == 'a' && current_funccal != NULL) /* function argument */
15695 return &current_funccal->l_avars.dv_hashtab;
15696 if (*name == 'l' && current_funccal != NULL) /* local function variable */
15697 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698 if (*name == 's' /* script variable */
15699 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
15700 return &SCRIPT_VARS(current_SID);
15701 return NULL;
15702}
15703
15704/*
15705 * Get the string value of a (global/local) variable.
15706 * Returns NULL when it doesn't exist.
15707 */
15708 char_u *
15709get_var_value(name)
15710 char_u *name;
15711{
Bram Moolenaar33570922005-01-25 22:26:29 +000015712 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713
Bram Moolenaara7043832005-01-21 11:56:39 +000015714 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715 if (v == NULL)
15716 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015717 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718}
15719
15720/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015721 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722 * sourcing this script and when executing functions defined in the script.
15723 */
15724 void
15725new_script_vars(id)
15726 scid_T id;
15727{
Bram Moolenaara7043832005-01-21 11:56:39 +000015728 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000015729 hashtab_T *ht;
15730 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000015731
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
15733 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015734 /* Re-allocating ga_data means that an ht_array pointing to
15735 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000015736 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000015737 for (i = 1; i <= ga_scripts.ga_len; ++i)
15738 {
15739 ht = &SCRIPT_VARS(i);
15740 if (ht->ht_mask == HT_INIT_SIZE - 1)
15741 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000015742 sv = &SCRIPT_SV(i);
15743 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000015744 }
15745
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746 while (ga_scripts.ga_len < id)
15747 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015748 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
15749 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015751 }
15752 }
15753}
15754
15755/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015756 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
15757 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758 */
15759 void
Bram Moolenaar33570922005-01-25 22:26:29 +000015760init_var_dict(dict, dict_var)
15761 dict_T *dict;
15762 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763{
Bram Moolenaar33570922005-01-25 22:26:29 +000015764 hash_init(&dict->dv_hashtab);
15765 dict->dv_refcount = 99999;
15766 dict_var->di_tv.vval.v_dict = dict;
15767 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015768 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015769 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15770 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771}
15772
15773/*
15774 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000015775 * Frees all allocated variables and the value they contain.
15776 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777 */
15778 void
Bram Moolenaara7043832005-01-21 11:56:39 +000015779vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000015780 hashtab_T *ht;
15781{
15782 vars_clear_ext(ht, TRUE);
15783}
15784
15785/*
15786 * Like vars_clear(), but only free the value if "free_val" is TRUE.
15787 */
15788 static void
15789vars_clear_ext(ht, free_val)
15790 hashtab_T *ht;
15791 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015792{
Bram Moolenaara7043832005-01-21 11:56:39 +000015793 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000015794 hashitem_T *hi;
15795 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015796
Bram Moolenaar33570922005-01-25 22:26:29 +000015797 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000015798 todo = ht->ht_used;
15799 for (hi = ht->ht_array; todo > 0; ++hi)
15800 {
15801 if (!HASHITEM_EMPTY(hi))
15802 {
15803 --todo;
15804
Bram Moolenaar33570922005-01-25 22:26:29 +000015805 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000015806 * ht_array might change then. hash_clear() takes care of it
15807 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015808 v = HI2DI(hi);
15809 if (free_val)
15810 clear_tv(&v->di_tv);
15811 if ((v->di_flags & DI_FLAGS_FIX) == 0)
15812 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000015813 }
15814 }
15815 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015816 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817}
15818
Bram Moolenaara7043832005-01-21 11:56:39 +000015819/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015820 * Delete a variable from hashtab "ht" at item "hi".
15821 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000015822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000015824delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000015825 hashtab_T *ht;
15826 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015827{
Bram Moolenaar33570922005-01-25 22:26:29 +000015828 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015829
15830 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000015831 clear_tv(&di->di_tv);
15832 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833}
15834
15835/*
15836 * List the value of one internal variable.
15837 */
15838 static void
15839list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000015840 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015841 char_u *prefix;
15842{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015843 char_u *tofree;
15844 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015845 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015846
Bram Moolenaar33570922005-01-25 22:26:29 +000015847 s = echo_string(&v->di_tv, &tofree, numbuf);
15848 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015849 s == NULL ? (char_u *)"" : s);
15850 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851}
15852
Bram Moolenaar071d4272004-06-13 20:20:40 +000015853 static void
15854list_one_var_a(prefix, name, type, string)
15855 char_u *prefix;
15856 char_u *name;
15857 int type;
15858 char_u *string;
15859{
15860 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
15861 if (name != NULL) /* "a:" vars don't have a name stored */
15862 msg_puts(name);
15863 msg_putchar(' ');
15864 msg_advance(22);
15865 if (type == VAR_NUMBER)
15866 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015867 else if (type == VAR_FUNC)
15868 msg_putchar('*');
15869 else if (type == VAR_LIST)
15870 {
15871 msg_putchar('[');
15872 if (*string == '[')
15873 ++string;
15874 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015875 else if (type == VAR_DICT)
15876 {
15877 msg_putchar('{');
15878 if (*string == '{')
15879 ++string;
15880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015881 else
15882 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015883
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015885
15886 if (type == VAR_FUNC)
15887 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888}
15889
15890/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015891 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015892 * If the variable already exists, the value is updated.
15893 * Otherwise the variable is created.
15894 */
15895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015896set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015898 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015899 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900{
Bram Moolenaar33570922005-01-25 22:26:29 +000015901 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015902 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015903 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015904 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015905
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015906 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015907 {
15908 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
15909 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
15910 ? name[2] : name[0]))
15911 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015912 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015913 return;
15914 }
15915 if (function_exists(name))
15916 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015917 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015918 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015919 return;
15920 }
15921 }
15922
Bram Moolenaara7043832005-01-21 11:56:39 +000015923 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000015924 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000015925 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000015926 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000015927 return;
15928 }
15929
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015930 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015931 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015932 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015933 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015934 if (var_check_ro(v->di_flags, name)
15935 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000015936 return;
15937 if (v->di_tv.v_type != tv->v_type
15938 && !((v->di_tv.v_type == VAR_STRING
15939 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015940 && (tv->v_type == VAR_STRING
15941 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015942 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015943 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015944 return;
15945 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015946
15947 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000015948 * Handle setting internal v: variables separately: we don't change
15949 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000015950 */
15951 if (ht == &vimvarht)
15952 {
15953 if (v->di_tv.v_type == VAR_STRING)
15954 {
15955 vim_free(v->di_tv.vval.v_string);
15956 if (copy || tv->v_type != VAR_STRING)
15957 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
15958 else
15959 {
15960 /* Take over the string to avoid an extra alloc/free. */
15961 v->di_tv.vval.v_string = tv->vval.v_string;
15962 tv->vval.v_string = NULL;
15963 }
15964 }
15965 else if (v->di_tv.v_type != VAR_NUMBER)
15966 EMSG2(_(e_intern2), "set_var()");
15967 else
15968 v->di_tv.vval.v_number = get_tv_number(tv);
15969 return;
15970 }
15971
15972 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015973 }
15974 else /* add a new variable */
15975 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000015976 /* Make sure the variable name is valid. */
15977 for (p = varname; *p != NUL; ++p)
15978 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)))
15979 {
15980 EMSG2(_(e_illvar), varname);
15981 return;
15982 }
15983
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015984 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
15985 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000015986 if (v == NULL)
15987 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000015988 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000015989 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015991 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992 return;
15993 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015994 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015995 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015996
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015997 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000015998 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000015999 else
16000 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016001 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016002 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016003 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016005}
16006
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016007/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016008 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16009 * Also give an error message.
16010 */
16011 static int
16012var_check_ro(flags, name)
16013 int flags;
16014 char_u *name;
16015{
16016 if (flags & DI_FLAGS_RO)
16017 {
16018 EMSG2(_(e_readonlyvar), name);
16019 return TRUE;
16020 }
16021 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16022 {
16023 EMSG2(_(e_readonlysbx), name);
16024 return TRUE;
16025 }
16026 return FALSE;
16027}
16028
16029/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016030 * Return TRUE if typeval "tv" is set to be locked (immutable).
16031 * Also give an error message, using "name".
16032 */
16033 static int
16034tv_check_lock(lock, name)
16035 int lock;
16036 char_u *name;
16037{
16038 if (lock & VAR_LOCKED)
16039 {
16040 EMSG2(_("E741: Value is locked: %s"),
16041 name == NULL ? (char_u *)_("Unknown") : name);
16042 return TRUE;
16043 }
16044 if (lock & VAR_FIXED)
16045 {
16046 EMSG2(_("E742: Cannot change value of %s"),
16047 name == NULL ? (char_u *)_("Unknown") : name);
16048 return TRUE;
16049 }
16050 return FALSE;
16051}
16052
16053/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016054 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016055 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016056 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016059copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016060 typval_T *from;
16061 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016062{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016063 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016064 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016065 switch (from->v_type)
16066 {
16067 case VAR_NUMBER:
16068 to->vval.v_number = from->vval.v_number;
16069 break;
16070 case VAR_STRING:
16071 case VAR_FUNC:
16072 if (from->vval.v_string == NULL)
16073 to->vval.v_string = NULL;
16074 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016075 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016076 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016077 if (from->v_type == VAR_FUNC)
16078 func_ref(to->vval.v_string);
16079 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016080 break;
16081 case VAR_LIST:
16082 if (from->vval.v_list == NULL)
16083 to->vval.v_list = NULL;
16084 else
16085 {
16086 to->vval.v_list = from->vval.v_list;
16087 ++to->vval.v_list->lv_refcount;
16088 }
16089 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016090 case VAR_DICT:
16091 if (from->vval.v_dict == NULL)
16092 to->vval.v_dict = NULL;
16093 else
16094 {
16095 to->vval.v_dict = from->vval.v_dict;
16096 ++to->vval.v_dict->dv_refcount;
16097 }
16098 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016099 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016100 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016101 break;
16102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103}
16104
16105/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016106 * Make a copy of an item.
16107 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016108 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16109 * reference to an already copied list/dict can be used.
16110 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016111 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016112 static int
16113item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016114 typval_T *from;
16115 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016116 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016117 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016118{
16119 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016120 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016121
Bram Moolenaar33570922005-01-25 22:26:29 +000016122 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016123 {
16124 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016125 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016126 }
16127 ++recurse;
16128
16129 switch (from->v_type)
16130 {
16131 case VAR_NUMBER:
16132 case VAR_STRING:
16133 case VAR_FUNC:
16134 copy_tv(from, to);
16135 break;
16136 case VAR_LIST:
16137 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016138 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016139 if (from->vval.v_list == NULL)
16140 to->vval.v_list = NULL;
16141 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16142 {
16143 /* use the copy made earlier */
16144 to->vval.v_list = from->vval.v_list->lv_copylist;
16145 ++to->vval.v_list->lv_refcount;
16146 }
16147 else
16148 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16149 if (to->vval.v_list == NULL)
16150 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016151 break;
16152 case VAR_DICT:
16153 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016154 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016155 if (from->vval.v_dict == NULL)
16156 to->vval.v_dict = NULL;
16157 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16158 {
16159 /* use the copy made earlier */
16160 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16161 ++to->vval.v_dict->dv_refcount;
16162 }
16163 else
16164 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16165 if (to->vval.v_dict == NULL)
16166 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016167 break;
16168 default:
16169 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016170 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016171 }
16172 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016173 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016174}
16175
16176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016177 * ":echo expr1 ..." print each argument separated with a space, add a
16178 * newline at the end.
16179 * ":echon expr1 ..." print each argument plain.
16180 */
16181 void
16182ex_echo(eap)
16183 exarg_T *eap;
16184{
16185 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016186 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016187 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016188 char_u *p;
16189 int needclr = TRUE;
16190 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016191 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016192
16193 if (eap->skip)
16194 ++emsg_skip;
16195 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16196 {
16197 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016198 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199 {
16200 /*
16201 * Report the invalid expression unless the expression evaluation
16202 * has been cancelled due to an aborting error, an interrupt, or an
16203 * exception.
16204 */
16205 if (!aborting())
16206 EMSG2(_(e_invexpr2), p);
16207 break;
16208 }
16209 if (!eap->skip)
16210 {
16211 if (atstart)
16212 {
16213 atstart = FALSE;
16214 /* Call msg_start() after eval1(), evaluating the expression
16215 * may cause a message to appear. */
16216 if (eap->cmdidx == CMD_echo)
16217 msg_start();
16218 }
16219 else if (eap->cmdidx == CMD_echo)
16220 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016221 p = echo_string(&rettv, &tofree, numbuf);
16222 if (p != NULL)
16223 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016224 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016225 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016227 if (*p != TAB && needclr)
16228 {
16229 /* remove any text still there from the command */
16230 msg_clr_eos();
16231 needclr = FALSE;
16232 }
16233 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016234 }
16235 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016236 {
16237#ifdef FEAT_MBYTE
16238 if (has_mbyte)
16239 {
16240 int i = (*mb_ptr2len_check)(p);
16241
16242 (void)msg_outtrans_len_attr(p, i, echo_attr);
16243 p += i - 1;
16244 }
16245 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016247 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016249 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016250 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016252 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253 arg = skipwhite(arg);
16254 }
16255 eap->nextcmd = check_nextcmd(arg);
16256
16257 if (eap->skip)
16258 --emsg_skip;
16259 else
16260 {
16261 /* remove text that may still be there from the command */
16262 if (needclr)
16263 msg_clr_eos();
16264 if (eap->cmdidx == CMD_echo)
16265 msg_end();
16266 }
16267}
16268
16269/*
16270 * ":echohl {name}".
16271 */
16272 void
16273ex_echohl(eap)
16274 exarg_T *eap;
16275{
16276 int id;
16277
16278 id = syn_name2id(eap->arg);
16279 if (id == 0)
16280 echo_attr = 0;
16281 else
16282 echo_attr = syn_id2attr(id);
16283}
16284
16285/*
16286 * ":execute expr1 ..." execute the result of an expression.
16287 * ":echomsg expr1 ..." Print a message
16288 * ":echoerr expr1 ..." Print an error
16289 * Each gets spaces around each argument and a newline at the end for
16290 * echo commands
16291 */
16292 void
16293ex_execute(eap)
16294 exarg_T *eap;
16295{
16296 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016297 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298 int ret = OK;
16299 char_u *p;
16300 garray_T ga;
16301 int len;
16302 int save_did_emsg;
16303
16304 ga_init2(&ga, 1, 80);
16305
16306 if (eap->skip)
16307 ++emsg_skip;
16308 while (*arg != NUL && *arg != '|' && *arg != '\n')
16309 {
16310 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016311 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312 {
16313 /*
16314 * Report the invalid expression unless the expression evaluation
16315 * has been cancelled due to an aborting error, an interrupt, or an
16316 * exception.
16317 */
16318 if (!aborting())
16319 EMSG2(_(e_invexpr2), p);
16320 ret = FAIL;
16321 break;
16322 }
16323
16324 if (!eap->skip)
16325 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016326 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016327 len = (int)STRLEN(p);
16328 if (ga_grow(&ga, len + 2) == FAIL)
16329 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016330 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331 ret = FAIL;
16332 break;
16333 }
16334 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016335 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337 ga.ga_len += len;
16338 }
16339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016340 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016341 arg = skipwhite(arg);
16342 }
16343
16344 if (ret != FAIL && ga.ga_data != NULL)
16345 {
16346 if (eap->cmdidx == CMD_echomsg)
16347 MSG_ATTR(ga.ga_data, echo_attr);
16348 else if (eap->cmdidx == CMD_echoerr)
16349 {
16350 /* We don't want to abort following commands, restore did_emsg. */
16351 save_did_emsg = did_emsg;
16352 EMSG((char_u *)ga.ga_data);
16353 if (!force_abort)
16354 did_emsg = save_did_emsg;
16355 }
16356 else if (eap->cmdidx == CMD_execute)
16357 do_cmdline((char_u *)ga.ga_data,
16358 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16359 }
16360
16361 ga_clear(&ga);
16362
16363 if (eap->skip)
16364 --emsg_skip;
16365
16366 eap->nextcmd = check_nextcmd(arg);
16367}
16368
16369/*
16370 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16371 * "arg" points to the "&" or '+' when called, to "option" when returning.
16372 * Returns NULL when no option name found. Otherwise pointer to the char
16373 * after the option name.
16374 */
16375 static char_u *
16376find_option_end(arg, opt_flags)
16377 char_u **arg;
16378 int *opt_flags;
16379{
16380 char_u *p = *arg;
16381
16382 ++p;
16383 if (*p == 'g' && p[1] == ':')
16384 {
16385 *opt_flags = OPT_GLOBAL;
16386 p += 2;
16387 }
16388 else if (*p == 'l' && p[1] == ':')
16389 {
16390 *opt_flags = OPT_LOCAL;
16391 p += 2;
16392 }
16393 else
16394 *opt_flags = 0;
16395
16396 if (!ASCII_ISALPHA(*p))
16397 return NULL;
16398 *arg = p;
16399
16400 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16401 p += 4; /* termcap option */
16402 else
16403 while (ASCII_ISALPHA(*p))
16404 ++p;
16405 return p;
16406}
16407
16408/*
16409 * ":function"
16410 */
16411 void
16412ex_function(eap)
16413 exarg_T *eap;
16414{
16415 char_u *theline;
16416 int j;
16417 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016419 char_u *name = NULL;
16420 char_u *p;
16421 char_u *arg;
16422 garray_T newargs;
16423 garray_T newlines;
16424 int varargs = FALSE;
16425 int mustend = FALSE;
16426 int flags = 0;
16427 ufunc_T *fp;
16428 int indent;
16429 int nesting;
16430 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016431 dictitem_T *v;
16432 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016433 static int func_nr = 0; /* number for nameless function */
16434 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016435 hashtab_T *ht;
16436 int todo;
16437 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016438
16439 /*
16440 * ":function" without argument: list functions.
16441 */
16442 if (ends_excmd(*eap->arg))
16443 {
16444 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016445 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000016446 todo = func_hashtab.ht_used;
16447 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016448 {
16449 if (!HASHITEM_EMPTY(hi))
16450 {
16451 --todo;
16452 fp = HI2UF(hi);
16453 if (!isdigit(*fp->uf_name))
16454 list_func_head(fp, FALSE);
16455 }
16456 }
16457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458 eap->nextcmd = check_nextcmd(eap->arg);
16459 return;
16460 }
16461
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016462 /*
16463 * Get the function name. There are these situations:
16464 * func normal function name
16465 * "name" == func, "fudi.fd_dict" == NULL
16466 * dict.func new dictionary entry
16467 * "name" == NULL, "fudi.fd_dict" set,
16468 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
16469 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016470 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016471 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16472 * dict.func existing dict entry that's not a Funcref
16473 * "name" == NULL, "fudi.fd_dict" set,
16474 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16475 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016476 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016477 name = trans_function_name(&p, eap->skip, 0, &fudi);
16478 paren = (vim_strchr(p, '(') != NULL);
16479 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016480 {
16481 /*
16482 * Return on an invalid expression in braces, unless the expression
16483 * evaluation has been cancelled due to an aborting error, an
16484 * interrupt, or an exception.
16485 */
16486 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016487 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016488 if (!eap->skip && fudi.fd_newkey != NULL)
16489 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016490 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016491 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016493 else
16494 eap->skip = TRUE;
16495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016496 /* An error in a function call during evaluation of an expression in magic
16497 * braces should not cause the function not to be defined. */
16498 saved_did_emsg = did_emsg;
16499 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016500
16501 /*
16502 * ":function func" with only function name: list function.
16503 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016504 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016505 {
16506 if (!ends_excmd(*skipwhite(p)))
16507 {
16508 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016509 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016510 }
16511 eap->nextcmd = check_nextcmd(p);
16512 if (eap->nextcmd != NULL)
16513 *p = NUL;
16514 if (!eap->skip && !got_int)
16515 {
16516 fp = find_func(name);
16517 if (fp != NULL)
16518 {
16519 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016520 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521 {
16522 msg_putchar('\n');
16523 msg_outnum((long)(j + 1));
16524 if (j < 9)
16525 msg_putchar(' ');
16526 if (j < 99)
16527 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016528 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016529 out_flush(); /* show a line at a time */
16530 ui_breakcheck();
16531 }
16532 if (!got_int)
16533 {
16534 msg_putchar('\n');
16535 msg_puts((char_u *)" endfunction");
16536 }
16537 }
16538 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016539 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016540 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016541 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016542 }
16543
16544 /*
16545 * ":function name(arg1, arg2)" Define function.
16546 */
16547 p = skipwhite(p);
16548 if (*p != '(')
16549 {
16550 if (!eap->skip)
16551 {
16552 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016553 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016554 }
16555 /* attempt to continue by skipping some text */
16556 if (vim_strchr(p, '(') != NULL)
16557 p = vim_strchr(p, '(');
16558 }
16559 p = skipwhite(p + 1);
16560
16561 ga_init2(&newargs, (int)sizeof(char_u *), 3);
16562 ga_init2(&newlines, (int)sizeof(char_u *), 3);
16563
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016564 if (!eap->skip)
16565 {
16566 /* Check the name of the function. */
16567 if (name != NULL)
16568 arg = name;
16569 else
16570 arg = fudi.fd_newkey;
16571 if (arg != NULL)
16572 {
16573 if (*arg == K_SPECIAL)
16574 j = 3;
16575 else
16576 j = 0;
16577 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
16578 : eval_isnamec(arg[j])))
16579 ++j;
16580 if (arg[j] != NUL)
16581 emsg_funcname(_(e_invarg2), arg);
16582 }
16583 }
16584
Bram Moolenaar071d4272004-06-13 20:20:40 +000016585 /*
16586 * Isolate the arguments: "arg1, arg2, ...)"
16587 */
16588 while (*p != ')')
16589 {
16590 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
16591 {
16592 varargs = TRUE;
16593 p += 3;
16594 mustend = TRUE;
16595 }
16596 else
16597 {
16598 arg = p;
16599 while (ASCII_ISALNUM(*p) || *p == '_')
16600 ++p;
16601 if (arg == p || isdigit(*arg)
16602 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
16603 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
16604 {
16605 if (!eap->skip)
16606 EMSG2(_("E125: Illegal argument: %s"), arg);
16607 break;
16608 }
16609 if (ga_grow(&newargs, 1) == FAIL)
16610 goto erret;
16611 c = *p;
16612 *p = NUL;
16613 arg = vim_strsave(arg);
16614 if (arg == NULL)
16615 goto erret;
16616 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
16617 *p = c;
16618 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619 if (*p == ',')
16620 ++p;
16621 else
16622 mustend = TRUE;
16623 }
16624 p = skipwhite(p);
16625 if (mustend && *p != ')')
16626 {
16627 if (!eap->skip)
16628 EMSG2(_(e_invarg2), eap->arg);
16629 break;
16630 }
16631 }
16632 ++p; /* skip the ')' */
16633
Bram Moolenaare9a41262005-01-15 22:18:47 +000016634 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016635 for (;;)
16636 {
16637 p = skipwhite(p);
16638 if (STRNCMP(p, "range", 5) == 0)
16639 {
16640 flags |= FC_RANGE;
16641 p += 5;
16642 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016643 else if (STRNCMP(p, "dict", 4) == 0)
16644 {
16645 flags |= FC_DICT;
16646 p += 4;
16647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648 else if (STRNCMP(p, "abort", 5) == 0)
16649 {
16650 flags |= FC_ABORT;
16651 p += 5;
16652 }
16653 else
16654 break;
16655 }
16656
16657 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
16658 EMSG(_(e_trailing));
16659
16660 /*
16661 * Read the body of the function, until ":endfunction" is found.
16662 */
16663 if (KeyTyped)
16664 {
16665 /* Check if the function already exists, don't let the user type the
16666 * whole function before telling him it doesn't work! For a script we
16667 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016668 if (!eap->skip && !eap->forceit)
16669 {
16670 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
16671 EMSG(_(e_funcdict));
16672 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016673 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016675
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016676 if (!eap->skip && did_emsg)
16677 goto erret;
16678
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679 msg_putchar('\n'); /* don't overwrite the function name */
16680 cmdline_row = msg_row;
16681 }
16682
16683 indent = 2;
16684 nesting = 0;
16685 for (;;)
16686 {
16687 msg_scroll = TRUE;
16688 need_wait_return = FALSE;
16689 if (eap->getline == NULL)
16690 theline = getcmdline(':', 0L, indent);
16691 else
16692 theline = eap->getline(':', eap->cookie, indent);
16693 if (KeyTyped)
16694 lines_left = Rows - 1;
16695 if (theline == NULL)
16696 {
16697 EMSG(_("E126: Missing :endfunction"));
16698 goto erret;
16699 }
16700
16701 if (skip_until != NULL)
16702 {
16703 /* between ":append" and "." and between ":python <<EOF" and "EOF"
16704 * don't check for ":endfunc". */
16705 if (STRCMP(theline, skip_until) == 0)
16706 {
16707 vim_free(skip_until);
16708 skip_until = NULL;
16709 }
16710 }
16711 else
16712 {
16713 /* skip ':' and blanks*/
16714 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
16715 ;
16716
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016717 /* Check for "endfunction". */
16718 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016719 {
16720 vim_free(theline);
16721 break;
16722 }
16723
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016724 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000016725 * at "end". */
16726 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
16727 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016728 else if (STRNCMP(p, "if", 2) == 0
16729 || STRNCMP(p, "wh", 2) == 0
16730 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000016731 || STRNCMP(p, "try", 3) == 0)
16732 indent += 2;
16733
16734 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016735 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016736 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016737 if (*p == '!')
16738 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016739 p += eval_fname_script(p);
16740 if (ASCII_ISALPHA(*p))
16741 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016742 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016743 if (*skipwhite(p) == '(')
16744 {
16745 ++nesting;
16746 indent += 2;
16747 }
16748 }
16749 }
16750
16751 /* Check for ":append" or ":insert". */
16752 p = skip_range(p, NULL);
16753 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
16754 || (p[0] == 'i'
16755 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
16756 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
16757 skip_until = vim_strsave((char_u *)".");
16758
16759 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
16760 arg = skipwhite(skiptowhite(p));
16761 if (arg[0] == '<' && arg[1] =='<'
16762 && ((p[0] == 'p' && p[1] == 'y'
16763 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
16764 || (p[0] == 'p' && p[1] == 'e'
16765 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
16766 || (p[0] == 't' && p[1] == 'c'
16767 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
16768 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
16769 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000016770 || (p[0] == 'm' && p[1] == 'z'
16771 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772 ))
16773 {
16774 /* ":python <<" continues until a dot, like ":append" */
16775 p = skipwhite(arg + 2);
16776 if (*p == NUL)
16777 skip_until = vim_strsave((char_u *)".");
16778 else
16779 skip_until = vim_strsave(p);
16780 }
16781 }
16782
16783 /* Add the line to the function. */
16784 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016785 {
16786 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016787 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016788 }
16789
16790 /* Copy the line to newly allocated memory. get_one_sourceline()
16791 * allocates 250 bytes per line, this saves 80% on average. The cost
16792 * is an extra alloc/free. */
16793 p = vim_strsave(theline);
16794 if (p != NULL)
16795 {
16796 vim_free(theline);
16797 theline = p;
16798 }
16799
Bram Moolenaar071d4272004-06-13 20:20:40 +000016800 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
16801 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802 }
16803
16804 /* Don't define the function when skipping commands or when an error was
16805 * detected. */
16806 if (eap->skip || did_emsg)
16807 goto erret;
16808
16809 /*
16810 * If there are no errors, add the function
16811 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016812 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016813 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016814 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000016815 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016816 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016817 emsg_funcname("E707: Function name conflicts with variable: %s",
16818 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016819 goto erret;
16820 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016821
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016822 fp = find_func(name);
16823 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016825 if (!eap->forceit)
16826 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016827 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016828 goto erret;
16829 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016830 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016831 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016832 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016833 name);
16834 goto erret;
16835 }
16836 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016837 ga_clear_strings(&(fp->uf_args));
16838 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016839 vim_free(name);
16840 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842 }
16843 else
16844 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016845 char numbuf[20];
16846
16847 fp = NULL;
16848 if (fudi.fd_newkey == NULL && !eap->forceit)
16849 {
16850 EMSG(_(e_funcdict));
16851 goto erret;
16852 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000016853 if (fudi.fd_di == NULL)
16854 {
16855 /* Can't add a function to a locked dictionary */
16856 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
16857 goto erret;
16858 }
16859 /* Can't change an existing function if it is locked */
16860 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
16861 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016862
16863 /* Give the function a sequential number. Can only be used with a
16864 * Funcref! */
16865 vim_free(name);
16866 sprintf(numbuf, "%d", ++func_nr);
16867 name = vim_strsave((char_u *)numbuf);
16868 if (name == NULL)
16869 goto erret;
16870 }
16871
16872 if (fp == NULL)
16873 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016874 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016875 {
16876 int slen, plen;
16877 char_u *scriptname;
16878
16879 /* Check that the autoload name matches the script name. */
16880 j = FAIL;
16881 if (sourcing_name != NULL)
16882 {
16883 scriptname = autoload_name(name);
16884 if (scriptname != NULL)
16885 {
16886 p = vim_strchr(scriptname, '/');
16887 plen = STRLEN(p);
16888 slen = STRLEN(sourcing_name);
16889 if (slen > plen && fnamecmp(p,
16890 sourcing_name + slen - plen) == 0)
16891 j = OK;
16892 vim_free(scriptname);
16893 }
16894 }
16895 if (j == FAIL)
16896 {
16897 EMSG2(_("E746: Function name does not match script file name: %s"), name);
16898 goto erret;
16899 }
16900 }
16901
16902 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016903 if (fp == NULL)
16904 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016905
16906 if (fudi.fd_dict != NULL)
16907 {
16908 if (fudi.fd_di == NULL)
16909 {
16910 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016911 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016912 if (fudi.fd_di == NULL)
16913 {
16914 vim_free(fp);
16915 goto erret;
16916 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016917 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
16918 {
16919 vim_free(fudi.fd_di);
16920 goto erret;
16921 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016922 }
16923 else
16924 /* overwrite existing dict entry */
16925 clear_tv(&fudi.fd_di->di_tv);
16926 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016927 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016928 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016929 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016930 }
16931
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016933 STRCPY(fp->uf_name, name);
16934 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016935 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016936 fp->uf_args = newargs;
16937 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016938#ifdef FEAT_PROFILE
16939 fp->uf_tml_count = NULL;
16940 fp->uf_tml_total = NULL;
16941 fp->uf_tml_self = NULL;
16942 fp->uf_profiling = FALSE;
16943 if (prof_def_func())
16944 func_do_profile(fp);
16945#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016946 fp->uf_varargs = varargs;
16947 fp->uf_flags = flags;
16948 fp->uf_calls = 0;
16949 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016950 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951
16952erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953 ga_clear_strings(&newargs);
16954 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016955ret_free:
16956 vim_free(skip_until);
16957 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016959 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960}
16961
16962/*
16963 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000016964 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016965 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016966 * flags:
16967 * TFN_INT: internal function name OK
16968 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969 * Advances "pp" to just after the function name (if no error).
16970 */
16971 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016972trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973 char_u **pp;
16974 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016975 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000016976 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000016978 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016979 char_u *start;
16980 char_u *end;
16981 int lead;
16982 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000016984 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016985
16986 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016987 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000016989
16990 /* Check for hard coded <SNR>: already translated function ID (from a user
16991 * command). */
16992 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
16993 && (*pp)[2] == (int)KE_SNR)
16994 {
16995 *pp += 3;
16996 len = get_id_len(pp) + 3;
16997 return vim_strnsave(start, len);
16998 }
16999
17000 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17001 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017003 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017005
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017006 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17007 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017008 if (end == start)
17009 {
17010 if (!skip)
17011 EMSG(_("E129: Function name required"));
17012 goto theend;
17013 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017014 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017015 {
17016 /*
17017 * Report an invalid expression in braces, unless the expression
17018 * evaluation has been cancelled due to an aborting error, an
17019 * interrupt, or an exception.
17020 */
17021 if (!aborting())
17022 {
17023 if (end != NULL)
17024 EMSG2(_(e_invarg2), start);
17025 }
17026 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017027 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017028 goto theend;
17029 }
17030
17031 if (lv.ll_tv != NULL)
17032 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017033 if (fdp != NULL)
17034 {
17035 fdp->fd_dict = lv.ll_dict;
17036 fdp->fd_newkey = lv.ll_newkey;
17037 lv.ll_newkey = NULL;
17038 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017039 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017040 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17041 {
17042 name = vim_strsave(lv.ll_tv->vval.v_string);
17043 *pp = end;
17044 }
17045 else
17046 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017047 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17048 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017049 EMSG(_(e_funcref));
17050 else
17051 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017052 name = NULL;
17053 }
17054 goto theend;
17055 }
17056
17057 if (lv.ll_name == NULL)
17058 {
17059 /* Error found, but continue after the function name. */
17060 *pp = end;
17061 goto theend;
17062 }
17063
17064 if (lv.ll_exp_name != NULL)
17065 len = STRLEN(lv.ll_exp_name);
17066 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017067 {
17068 if (lead == 2) /* skip over "s:" */
17069 lv.ll_name += 2;
17070 len = (int)(end - lv.ll_name);
17071 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017072
17073 /*
17074 * Copy the function name to allocated memory.
17075 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17076 * Accept <SNR>123_name() outside a script.
17077 */
17078 if (skip)
17079 lead = 0; /* do nothing */
17080 else if (lead > 0)
17081 {
17082 lead = 3;
17083 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17084 {
17085 if (current_SID <= 0)
17086 {
17087 EMSG(_(e_usingsid));
17088 goto theend;
17089 }
17090 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17091 lead += (int)STRLEN(sid_buf);
17092 }
17093 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017094 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017095 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017096 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017097 goto theend;
17098 }
17099 name = alloc((unsigned)(len + lead + 1));
17100 if (name != NULL)
17101 {
17102 if (lead > 0)
17103 {
17104 name[0] = K_SPECIAL;
17105 name[1] = KS_EXTRA;
17106 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017107 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017108 STRCPY(name + 3, sid_buf);
17109 }
17110 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17111 name[len + lead] = NUL;
17112 }
17113 *pp = end;
17114
17115theend:
17116 clear_lval(&lv);
17117 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017118}
17119
17120/*
17121 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17122 * Return 2 if "p" starts with "s:".
17123 * Return 0 otherwise.
17124 */
17125 static int
17126eval_fname_script(p)
17127 char_u *p;
17128{
17129 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17130 || STRNICMP(p + 1, "SNR>", 4) == 0))
17131 return 5;
17132 if (p[0] == 's' && p[1] == ':')
17133 return 2;
17134 return 0;
17135}
17136
17137/*
17138 * Return TRUE if "p" starts with "<SID>" or "s:".
17139 * Only works if eval_fname_script() returned non-zero for "p"!
17140 */
17141 static int
17142eval_fname_sid(p)
17143 char_u *p;
17144{
17145 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17146}
17147
17148/*
17149 * List the head of the function: "name(arg1, arg2)".
17150 */
17151 static void
17152list_func_head(fp, indent)
17153 ufunc_T *fp;
17154 int indent;
17155{
17156 int j;
17157
17158 msg_start();
17159 if (indent)
17160 MSG_PUTS(" ");
17161 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017162 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163 {
17164 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017165 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017166 }
17167 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017168 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017169 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017170 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017171 {
17172 if (j)
17173 MSG_PUTS(", ");
17174 msg_puts(FUNCARG(fp, j));
17175 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017176 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017177 {
17178 if (j)
17179 MSG_PUTS(", ");
17180 MSG_PUTS("...");
17181 }
17182 msg_putchar(')');
17183}
17184
17185/*
17186 * Find a function by name, return pointer to it in ufuncs.
17187 * Return NULL for unknown function.
17188 */
17189 static ufunc_T *
17190find_func(name)
17191 char_u *name;
17192{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017193 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017194
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017195 hi = hash_find(&func_hashtab, name);
17196 if (!HASHITEM_EMPTY(hi))
17197 return HI2UF(hi);
17198 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199}
17200
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017201#if defined(EXITFREE) || defined(PROTO)
17202 void
17203free_all_functions()
17204{
17205 hashitem_T *hi;
17206
17207 /* Need to start all over every time, because func_free() may change the
17208 * hash table. */
17209 while (func_hashtab.ht_used > 0)
17210 for (hi = func_hashtab.ht_array; ; ++hi)
17211 if (!HASHITEM_EMPTY(hi))
17212 {
17213 func_free(HI2UF(hi));
17214 break;
17215 }
17216}
17217#endif
17218
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017219/*
17220 * Return TRUE if a function "name" exists.
17221 */
17222 static int
17223function_exists(name)
17224 char_u *name;
17225{
17226 char_u *p = name;
17227 int n = FALSE;
17228
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017229 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017230 if (p != NULL)
17231 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017232 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017233 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017234 else
17235 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017236 vim_free(p);
17237 }
17238 return n;
17239}
17240
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017241/*
17242 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017243 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017244 */
17245 static int
17246builtin_function(name)
17247 char_u *name;
17248{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017249 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17250 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017251}
17252
Bram Moolenaar05159a02005-02-26 23:04:13 +000017253#if defined(FEAT_PROFILE) || defined(PROTO)
17254/*
17255 * Start profiling function "fp".
17256 */
17257 static void
17258func_do_profile(fp)
17259 ufunc_T *fp;
17260{
17261 fp->uf_tm_count = 0;
17262 profile_zero(&fp->uf_tm_self);
17263 profile_zero(&fp->uf_tm_total);
17264 if (fp->uf_tml_count == NULL)
17265 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17266 (sizeof(int) * fp->uf_lines.ga_len));
17267 if (fp->uf_tml_total == NULL)
17268 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17269 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17270 if (fp->uf_tml_self == NULL)
17271 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17272 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17273 fp->uf_tml_idx = -1;
17274 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17275 || fp->uf_tml_self == NULL)
17276 return; /* out of memory */
17277
17278 fp->uf_profiling = TRUE;
17279}
17280
17281/*
17282 * Dump the profiling results for all functions in file "fd".
17283 */
17284 void
17285func_dump_profile(fd)
17286 FILE *fd;
17287{
17288 hashitem_T *hi;
17289 int todo;
17290 ufunc_T *fp;
17291 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017292 ufunc_T **sorttab;
17293 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017294
17295 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017296 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17297
Bram Moolenaar05159a02005-02-26 23:04:13 +000017298 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17299 {
17300 if (!HASHITEM_EMPTY(hi))
17301 {
17302 --todo;
17303 fp = HI2UF(hi);
17304 if (fp->uf_profiling)
17305 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017306 if (sorttab != NULL)
17307 sorttab[st_len++] = fp;
17308
Bram Moolenaar05159a02005-02-26 23:04:13 +000017309 if (fp->uf_name[0] == K_SPECIAL)
17310 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17311 else
17312 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17313 if (fp->uf_tm_count == 1)
17314 fprintf(fd, "Called 1 time\n");
17315 else
17316 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17317 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17318 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17319 fprintf(fd, "\n");
17320 fprintf(fd, "count total (s) self (s)\n");
17321
17322 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17323 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017324 prof_func_line(fd, fp->uf_tml_count[i],
17325 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017326 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17327 }
17328 fprintf(fd, "\n");
17329 }
17330 }
17331 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017332
17333 if (sorttab != NULL && st_len > 0)
17334 {
17335 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17336 prof_total_cmp);
17337 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17338 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17339 prof_self_cmp);
17340 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17341 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017342}
Bram Moolenaar73830342005-02-28 22:48:19 +000017343
17344 static void
17345prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17346 FILE *fd;
17347 ufunc_T **sorttab;
17348 int st_len;
17349 char *title;
17350 int prefer_self; /* when equal print only self time */
17351{
17352 int i;
17353 ufunc_T *fp;
17354
17355 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17356 fprintf(fd, "count total (s) self (s) function\n");
17357 for (i = 0; i < 20 && i < st_len; ++i)
17358 {
17359 fp = sorttab[i];
17360 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17361 prefer_self);
17362 if (fp->uf_name[0] == K_SPECIAL)
17363 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17364 else
17365 fprintf(fd, " %s()\n", fp->uf_name);
17366 }
17367 fprintf(fd, "\n");
17368}
17369
17370/*
17371 * Print the count and times for one function or function line.
17372 */
17373 static void
17374prof_func_line(fd, count, total, self, prefer_self)
17375 FILE *fd;
17376 int count;
17377 proftime_T *total;
17378 proftime_T *self;
17379 int prefer_self; /* when equal print only self time */
17380{
17381 if (count > 0)
17382 {
17383 fprintf(fd, "%5d ", count);
17384 if (prefer_self && profile_equal(total, self))
17385 fprintf(fd, " ");
17386 else
17387 fprintf(fd, "%s ", profile_msg(total));
17388 if (!prefer_self && profile_equal(total, self))
17389 fprintf(fd, " ");
17390 else
17391 fprintf(fd, "%s ", profile_msg(self));
17392 }
17393 else
17394 fprintf(fd, " ");
17395}
17396
17397/*
17398 * Compare function for total time sorting.
17399 */
17400 static int
17401#ifdef __BORLANDC__
17402_RTLENTRYF
17403#endif
17404prof_total_cmp(s1, s2)
17405 const void *s1;
17406 const void *s2;
17407{
17408 ufunc_T *p1, *p2;
17409
17410 p1 = *(ufunc_T **)s1;
17411 p2 = *(ufunc_T **)s2;
17412 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
17413}
17414
17415/*
17416 * Compare function for self time sorting.
17417 */
17418 static int
17419#ifdef __BORLANDC__
17420_RTLENTRYF
17421#endif
17422prof_self_cmp(s1, s2)
17423 const void *s1;
17424 const void *s2;
17425{
17426 ufunc_T *p1, *p2;
17427
17428 p1 = *(ufunc_T **)s1;
17429 p2 = *(ufunc_T **)s2;
17430 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
17431}
17432
Bram Moolenaar05159a02005-02-26 23:04:13 +000017433#endif
17434
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017435/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017436 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017437 * Return TRUE if a package was loaded.
17438 */
17439 static int
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017440script_autoload(name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017441 char_u *name;
17442{
17443 char_u *p;
17444 char_u *scriptname;
17445 int ret = FALSE;
17446
17447 /* If there is no colon after name[1] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017448 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017449 if (p == NULL || p <= name + 2)
17450 return FALSE;
17451
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017452 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
17453 scriptname = autoload_name(name);
17454 if (cmd_runtime(scriptname, FALSE) == OK)
17455 ret = TRUE;
17456
17457 vim_free(scriptname);
17458 return ret;
17459}
17460
17461/*
17462 * Return the autoload script name for a function or variable name.
17463 * Returns NULL when out of memory.
17464 */
17465 static char_u *
17466autoload_name(name)
17467 char_u *name;
17468{
17469 char_u *p;
17470 char_u *scriptname;
17471
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017472 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017473 scriptname = alloc((unsigned)(STRLEN(name) + 14));
17474 if (scriptname == NULL)
17475 return FALSE;
17476 STRCPY(scriptname, "autoload/");
17477 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017478 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017479 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017480 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017481 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017482 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017483}
17484
Bram Moolenaar071d4272004-06-13 20:20:40 +000017485#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
17486
17487/*
17488 * Function given to ExpandGeneric() to obtain the list of user defined
17489 * function names.
17490 */
17491 char_u *
17492get_user_func_name(xp, idx)
17493 expand_T *xp;
17494 int idx;
17495{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017496 static long_u done;
17497 static hashitem_T *hi;
17498 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499
17500 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017502 done = 0;
17503 hi = func_hashtab.ht_array;
17504 }
17505 if (done < func_hashtab.ht_used)
17506 {
17507 if (done++ > 0)
17508 ++hi;
17509 while (HASHITEM_EMPTY(hi))
17510 ++hi;
17511 fp = HI2UF(hi);
17512
17513 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
17514 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017515
17516 cat_func_name(IObuff, fp);
17517 if (xp->xp_context != EXPAND_USER_FUNC)
17518 {
17519 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017520 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521 STRCAT(IObuff, ")");
17522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523 return IObuff;
17524 }
17525 return NULL;
17526}
17527
17528#endif /* FEAT_CMDL_COMPL */
17529
17530/*
17531 * Copy the function name of "fp" to buffer "buf".
17532 * "buf" must be able to hold the function name plus three bytes.
17533 * Takes care of script-local function names.
17534 */
17535 static void
17536cat_func_name(buf, fp)
17537 char_u *buf;
17538 ufunc_T *fp;
17539{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017540 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017541 {
17542 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017543 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017544 }
17545 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017546 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017547}
17548
17549/*
17550 * ":delfunction {name}"
17551 */
17552 void
17553ex_delfunction(eap)
17554 exarg_T *eap;
17555{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017556 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017557 char_u *p;
17558 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017559 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560
17561 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017562 name = trans_function_name(&p, eap->skip, 0, &fudi);
17563 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017564 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017565 {
17566 if (fudi.fd_dict != NULL && !eap->skip)
17567 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017568 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570 if (!ends_excmd(*skipwhite(p)))
17571 {
17572 vim_free(name);
17573 EMSG(_(e_trailing));
17574 return;
17575 }
17576 eap->nextcmd = check_nextcmd(p);
17577 if (eap->nextcmd != NULL)
17578 *p = NUL;
17579
17580 if (!eap->skip)
17581 fp = find_func(name);
17582 vim_free(name);
17583
17584 if (!eap->skip)
17585 {
17586 if (fp == NULL)
17587 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017588 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589 return;
17590 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017591 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592 {
17593 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
17594 return;
17595 }
17596
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017597 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017599 /* Delete the dict item that refers to the function, it will
17600 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017601 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017603 else
17604 func_free(fp);
17605 }
17606}
17607
17608/*
17609 * Free a function and remove it from the list of functions.
17610 */
17611 static void
17612func_free(fp)
17613 ufunc_T *fp;
17614{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017615 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017616
17617 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017618 ga_clear_strings(&(fp->uf_args));
17619 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000017620#ifdef FEAT_PROFILE
17621 vim_free(fp->uf_tml_count);
17622 vim_free(fp->uf_tml_total);
17623 vim_free(fp->uf_tml_self);
17624#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017625
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017626 /* remove the function from the function hashtable */
17627 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
17628 if (HASHITEM_EMPTY(hi))
17629 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017630 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017631 hash_remove(&func_hashtab, hi);
17632
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017633 vim_free(fp);
17634}
17635
17636/*
17637 * Unreference a Function: decrement the reference count and free it when it
17638 * becomes zero. Only for numbered functions.
17639 */
17640 static void
17641func_unref(name)
17642 char_u *name;
17643{
17644 ufunc_T *fp;
17645
17646 if (name != NULL && isdigit(*name))
17647 {
17648 fp = find_func(name);
17649 if (fp == NULL)
17650 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017651 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017652 {
17653 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017654 * when "uf_calls" becomes zero. */
17655 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017656 func_free(fp);
17657 }
17658 }
17659}
17660
17661/*
17662 * Count a reference to a Function.
17663 */
17664 static void
17665func_ref(name)
17666 char_u *name;
17667{
17668 ufunc_T *fp;
17669
17670 if (name != NULL && isdigit(*name))
17671 {
17672 fp = find_func(name);
17673 if (fp == NULL)
17674 EMSG2(_(e_intern2), "func_ref()");
17675 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017676 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677 }
17678}
17679
17680/*
17681 * Call a user function.
17682 */
17683 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000017684call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685 ufunc_T *fp; /* pointer to function */
17686 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000017687 typval_T *argvars; /* arguments */
17688 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017689 linenr_T firstline; /* first line of range */
17690 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000017691 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017692{
Bram Moolenaar33570922005-01-25 22:26:29 +000017693 char_u *save_sourcing_name;
17694 linenr_T save_sourcing_lnum;
17695 scid_T save_current_SID;
17696 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000017697 int save_did_emsg;
17698 static int depth = 0;
17699 dictitem_T *v;
17700 int fixvar_idx = 0; /* index in fixvar[] */
17701 int i;
17702 int ai;
17703 char_u numbuf[NUMBUFLEN];
17704 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017705#ifdef FEAT_PROFILE
17706 proftime_T wait_start;
17707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017708
17709 /* If depth of calling is getting too high, don't execute the function */
17710 if (depth >= p_mfd)
17711 {
17712 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017713 rettv->v_type = VAR_NUMBER;
17714 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715 return;
17716 }
17717 ++depth;
17718
17719 line_breakcheck(); /* check for CTRL-C hit */
17720
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017721 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000017722 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017723 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017724 fc.rettv = rettv;
17725 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 fc.linenr = 0;
17727 fc.returned = FALSE;
17728 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017730 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731 fc.dbg_tick = debug_tick;
17732
Bram Moolenaar33570922005-01-25 22:26:29 +000017733 /*
17734 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
17735 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
17736 * each argument variable and saves a lot of time.
17737 */
17738 /*
17739 * Init l: variables.
17740 */
17741 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000017742 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017743 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017744 /* Set l:self to "selfdict". */
17745 v = &fc.fixvar[fixvar_idx++].var;
17746 STRCPY(v->di_key, "self");
17747 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
17748 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
17749 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017750 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000017751 v->di_tv.vval.v_dict = selfdict;
17752 ++selfdict->dv_refcount;
17753 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017754
Bram Moolenaar33570922005-01-25 22:26:29 +000017755 /*
17756 * Init a: variables.
17757 * Set a:0 to "argcount".
17758 * Set a:000 to a list with room for the "..." arguments.
17759 */
17760 init_var_dict(&fc.l_avars, &fc.l_avars_var);
17761 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017762 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000017763 v = &fc.fixvar[fixvar_idx++].var;
17764 STRCPY(v->di_key, "000");
17765 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17766 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17767 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017768 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017769 v->di_tv.vval.v_list = &fc.l_varlist;
17770 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
17771 fc.l_varlist.lv_refcount = 99999;
17772
17773 /*
17774 * Set a:firstline to "firstline" and a:lastline to "lastline".
17775 * Set a:name to named arguments.
17776 * Set a:N to the "..." arguments.
17777 */
17778 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
17779 (varnumber_T)firstline);
17780 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
17781 (varnumber_T)lastline);
17782 for (i = 0; i < argcount; ++i)
17783 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017784 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017785 if (ai < 0)
17786 /* named argument a:name */
17787 name = FUNCARG(fp, i);
17788 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017789 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017790 /* "..." argument a:1, a:2, etc. */
17791 sprintf((char *)numbuf, "%d", ai + 1);
17792 name = numbuf;
17793 }
17794 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
17795 {
17796 v = &fc.fixvar[fixvar_idx++].var;
17797 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17798 }
17799 else
17800 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017801 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17802 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000017803 if (v == NULL)
17804 break;
17805 v->di_flags = DI_FLAGS_RO;
17806 }
17807 STRCPY(v->di_key, name);
17808 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17809
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017810 /* Note: the values are copied directly to avoid alloc/free.
17811 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017812 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017813 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017814
17815 if (ai >= 0 && ai < MAX_FUNC_ARGS)
17816 {
17817 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
17818 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017819 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017820 }
17821 }
17822
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823 /* Don't redraw while executing the function. */
17824 ++RedrawingDisabled;
17825 save_sourcing_name = sourcing_name;
17826 save_sourcing_lnum = sourcing_lnum;
17827 sourcing_lnum = 1;
17828 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017829 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017830 if (sourcing_name != NULL)
17831 {
17832 if (save_sourcing_name != NULL
17833 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
17834 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
17835 else
17836 STRCPY(sourcing_name, "function ");
17837 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
17838
17839 if (p_verbose >= 12)
17840 {
17841 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017842 verbose_enter_scroll();
17843
Bram Moolenaar555b2802005-05-19 21:08:39 +000017844 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017845 if (p_verbose >= 14)
17846 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017847 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000017848 char_u numbuf[NUMBUFLEN];
17849 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017850
17851 msg_puts((char_u *)"(");
17852 for (i = 0; i < argcount; ++i)
17853 {
17854 if (i > 0)
17855 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017856 if (argvars[i].v_type == VAR_NUMBER)
17857 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858 else
17859 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017860 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017861 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017863 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017864 }
17865 }
17866 msg_puts((char_u *)")");
17867 }
17868 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017869
17870 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871 --no_wait_return;
17872 }
17873 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017874#ifdef FEAT_PROFILE
17875 if (do_profiling)
17876 {
17877 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
17878 func_do_profile(fp);
17879 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017880 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000017881 {
17882 ++fp->uf_tm_count;
17883 profile_start(&fp->uf_tm_start);
17884 profile_zero(&fp->uf_tm_children);
17885 }
17886 script_prof_save(&wait_start);
17887 }
17888#endif
17889
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017891 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 save_did_emsg = did_emsg;
17893 did_emsg = FALSE;
17894
17895 /* call do_cmdline() to execute the lines */
17896 do_cmdline(NULL, get_func_line, (void *)&fc,
17897 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
17898
17899 --RedrawingDisabled;
17900
17901 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017902 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017904 clear_tv(rettv);
17905 rettv->v_type = VAR_NUMBER;
17906 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017907 }
17908
Bram Moolenaar05159a02005-02-26 23:04:13 +000017909#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017910 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000017911 {
17912 profile_end(&fp->uf_tm_start);
17913 profile_sub_wait(&wait_start, &fp->uf_tm_start);
17914 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
17915 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
17916 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017917 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017918 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017919 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
17920 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017921 }
17922 }
17923#endif
17924
Bram Moolenaar071d4272004-06-13 20:20:40 +000017925 /* when being verbose, mention the return value */
17926 if (p_verbose >= 12)
17927 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017928 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017929 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930
Bram Moolenaar071d4272004-06-13 20:20:40 +000017931 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000017932 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017933 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000017934 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
17935 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017936 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017937 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017938 char_u buf[MSG_BUF_LEN];
17939 char_u numbuf[NUMBUFLEN];
17940 char_u *tofree;
17941
Bram Moolenaar555b2802005-05-19 21:08:39 +000017942 /* The value may be very long. Skip the middle part, so that we
17943 * have some idea how it starts and ends. smsg() would always
17944 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000017945 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017946 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000017947 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017948 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017949 }
17950 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017951
17952 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017953 --no_wait_return;
17954 }
17955
17956 vim_free(sourcing_name);
17957 sourcing_name = save_sourcing_name;
17958 sourcing_lnum = save_sourcing_lnum;
17959 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017960#ifdef FEAT_PROFILE
17961 if (do_profiling)
17962 script_prof_restore(&wait_start);
17963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964
17965 if (p_verbose >= 12 && sourcing_name != NULL)
17966 {
17967 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017968 verbose_enter_scroll();
17969
Bram Moolenaar555b2802005-05-19 21:08:39 +000017970 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017971 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017972
17973 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017974 --no_wait_return;
17975 }
17976
17977 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017978 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017979
Bram Moolenaar33570922005-01-25 22:26:29 +000017980 /* The a: variables typevals were not alloced, only free the allocated
17981 * variables. */
17982 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
17983
17984 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017985 --depth;
17986}
17987
17988/*
Bram Moolenaar33570922005-01-25 22:26:29 +000017989 * Add a number variable "name" to dict "dp" with value "nr".
17990 */
17991 static void
17992add_nr_var(dp, v, name, nr)
17993 dict_T *dp;
17994 dictitem_T *v;
17995 char *name;
17996 varnumber_T nr;
17997{
17998 STRCPY(v->di_key, name);
17999 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18000 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18001 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018002 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018003 v->di_tv.vval.v_number = nr;
18004}
18005
18006/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007 * ":return [expr]"
18008 */
18009 void
18010ex_return(eap)
18011 exarg_T *eap;
18012{
18013 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018014 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018015 int returning = FALSE;
18016
18017 if (current_funccal == NULL)
18018 {
18019 EMSG(_("E133: :return not inside a function"));
18020 return;
18021 }
18022
18023 if (eap->skip)
18024 ++emsg_skip;
18025
18026 eap->nextcmd = NULL;
18027 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018028 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029 {
18030 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018031 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018033 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018034 }
18035 /* It's safer to return also on error. */
18036 else if (!eap->skip)
18037 {
18038 /*
18039 * Return unless the expression evaluation has been cancelled due to an
18040 * aborting error, an interrupt, or an exception.
18041 */
18042 if (!aborting())
18043 returning = do_return(eap, FALSE, TRUE, NULL);
18044 }
18045
18046 /* When skipping or the return gets pending, advance to the next command
18047 * in this line (!returning). Otherwise, ignore the rest of the line.
18048 * Following lines will be ignored by get_func_line(). */
18049 if (returning)
18050 eap->nextcmd = NULL;
18051 else if (eap->nextcmd == NULL) /* no argument */
18052 eap->nextcmd = check_nextcmd(arg);
18053
18054 if (eap->skip)
18055 --emsg_skip;
18056}
18057
18058/*
18059 * Return from a function. Possibly makes the return pending. Also called
18060 * for a pending return at the ":endtry" or after returning from an extra
18061 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018062 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018063 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064 * FALSE when the return gets pending.
18065 */
18066 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018067do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068 exarg_T *eap;
18069 int reanimate;
18070 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018071 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018072{
18073 int idx;
18074 struct condstack *cstack = eap->cstack;
18075
18076 if (reanimate)
18077 /* Undo the return. */
18078 current_funccal->returned = FALSE;
18079
18080 /*
18081 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18082 * not in its finally clause (which then is to be executed next) is found.
18083 * In this case, make the ":return" pending for execution at the ":endtry".
18084 * Otherwise, return normally.
18085 */
18086 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18087 if (idx >= 0)
18088 {
18089 cstack->cs_pending[idx] = CSTP_RETURN;
18090
18091 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018092 /* A pending return again gets pending. "rettv" points to an
18093 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018095 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096 else
18097 {
18098 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018099 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018100 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018101 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018102
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018103 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104 {
18105 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018106 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018107 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108 else
18109 EMSG(_(e_outofmem));
18110 }
18111 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018112 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113
18114 if (reanimate)
18115 {
18116 /* The pending return value could be overwritten by a ":return"
18117 * without argument in a finally clause; reset the default
18118 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018119 current_funccal->rettv->v_type = VAR_NUMBER;
18120 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121 }
18122 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018123 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018124 }
18125 else
18126 {
18127 current_funccal->returned = TRUE;
18128
18129 /* If the return is carried out now, store the return value. For
18130 * a return immediately after reanimation, the value is already
18131 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018132 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018133 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018134 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018135 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018136 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018137 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018138 }
18139 }
18140
18141 return idx < 0;
18142}
18143
18144/*
18145 * Free the variable with a pending return value.
18146 */
18147 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018148discard_pending_return(rettv)
18149 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018150{
Bram Moolenaar33570922005-01-25 22:26:29 +000018151 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018152}
18153
18154/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018155 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018156 * is an allocated string. Used by report_pending() for verbose messages.
18157 */
18158 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018159get_return_cmd(rettv)
18160 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018162 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018163 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018164 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018166 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018167 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018168 if (s == NULL)
18169 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018170
18171 STRCPY(IObuff, ":return ");
18172 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18173 if (STRLEN(s) + 8 >= IOSIZE)
18174 STRCPY(IObuff + IOSIZE - 4, "...");
18175 vim_free(tofree);
18176 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018177}
18178
18179/*
18180 * Get next function line.
18181 * Called by do_cmdline() to get the next line.
18182 * Returns allocated string, or NULL for end of function.
18183 */
18184/* ARGSUSED */
18185 char_u *
18186get_func_line(c, cookie, indent)
18187 int c; /* not used */
18188 void *cookie;
18189 int indent; /* not used */
18190{
Bram Moolenaar33570922005-01-25 22:26:29 +000018191 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018192 ufunc_T *fp = fcp->func;
18193 char_u *retval;
18194 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195
18196 /* If breakpoints have been added/deleted need to check for it. */
18197 if (fcp->dbg_tick != debug_tick)
18198 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018199 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200 sourcing_lnum);
18201 fcp->dbg_tick = debug_tick;
18202 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018203#ifdef FEAT_PROFILE
18204 if (do_profiling)
18205 func_line_end(cookie);
18206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207
Bram Moolenaar05159a02005-02-26 23:04:13 +000018208 gap = &fp->uf_lines;
18209 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018210 retval = NULL;
18211 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18212 retval = NULL;
18213 else
18214 {
18215 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18216 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018217#ifdef FEAT_PROFILE
18218 if (do_profiling)
18219 func_line_start(cookie);
18220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221 }
18222
18223 /* Did we encounter a breakpoint? */
18224 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18225 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018226 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018227 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018228 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229 sourcing_lnum);
18230 fcp->dbg_tick = debug_tick;
18231 }
18232
18233 return retval;
18234}
18235
Bram Moolenaar05159a02005-02-26 23:04:13 +000018236#if defined(FEAT_PROFILE) || defined(PROTO)
18237/*
18238 * Called when starting to read a function line.
18239 * "sourcing_lnum" must be correct!
18240 * When skipping lines it may not actually be executed, but we won't find out
18241 * until later and we need to store the time now.
18242 */
18243 void
18244func_line_start(cookie)
18245 void *cookie;
18246{
18247 funccall_T *fcp = (funccall_T *)cookie;
18248 ufunc_T *fp = fcp->func;
18249
18250 if (fp->uf_profiling && sourcing_lnum >= 1
18251 && sourcing_lnum <= fp->uf_lines.ga_len)
18252 {
18253 fp->uf_tml_idx = sourcing_lnum - 1;
18254 fp->uf_tml_execed = FALSE;
18255 profile_start(&fp->uf_tml_start);
18256 profile_zero(&fp->uf_tml_children);
18257 profile_get_wait(&fp->uf_tml_wait);
18258 }
18259}
18260
18261/*
18262 * Called when actually executing a function line.
18263 */
18264 void
18265func_line_exec(cookie)
18266 void *cookie;
18267{
18268 funccall_T *fcp = (funccall_T *)cookie;
18269 ufunc_T *fp = fcp->func;
18270
18271 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18272 fp->uf_tml_execed = TRUE;
18273}
18274
18275/*
18276 * Called when done with a function line.
18277 */
18278 void
18279func_line_end(cookie)
18280 void *cookie;
18281{
18282 funccall_T *fcp = (funccall_T *)cookie;
18283 ufunc_T *fp = fcp->func;
18284
18285 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18286 {
18287 if (fp->uf_tml_execed)
18288 {
18289 ++fp->uf_tml_count[fp->uf_tml_idx];
18290 profile_end(&fp->uf_tml_start);
18291 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18292 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18293 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18294 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18295 }
18296 fp->uf_tml_idx = -1;
18297 }
18298}
18299#endif
18300
Bram Moolenaar071d4272004-06-13 20:20:40 +000018301/*
18302 * Return TRUE if the currently active function should be ended, because a
18303 * return was encountered or an error occured. Used inside a ":while".
18304 */
18305 int
18306func_has_ended(cookie)
18307 void *cookie;
18308{
Bram Moolenaar33570922005-01-25 22:26:29 +000018309 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310
18311 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18312 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018313 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018314 || fcp->returned);
18315}
18316
18317/*
18318 * return TRUE if cookie indicates a function which "abort"s on errors.
18319 */
18320 int
18321func_has_abort(cookie)
18322 void *cookie;
18323{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018324 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325}
18326
18327#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18328typedef enum
18329{
18330 VAR_FLAVOUR_DEFAULT,
18331 VAR_FLAVOUR_SESSION,
18332 VAR_FLAVOUR_VIMINFO
18333} var_flavour_T;
18334
18335static var_flavour_T var_flavour __ARGS((char_u *varname));
18336
18337 static var_flavour_T
18338var_flavour(varname)
18339 char_u *varname;
18340{
18341 char_u *p = varname;
18342
18343 if (ASCII_ISUPPER(*p))
18344 {
18345 while (*(++p))
18346 if (ASCII_ISLOWER(*p))
18347 return VAR_FLAVOUR_SESSION;
18348 return VAR_FLAVOUR_VIMINFO;
18349 }
18350 else
18351 return VAR_FLAVOUR_DEFAULT;
18352}
18353#endif
18354
18355#if defined(FEAT_VIMINFO) || defined(PROTO)
18356/*
18357 * Restore global vars that start with a capital from the viminfo file
18358 */
18359 int
18360read_viminfo_varlist(virp, writing)
18361 vir_T *virp;
18362 int writing;
18363{
18364 char_u *tab;
18365 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018366 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367
18368 if (!writing && (find_viminfo_parameter('!') != NULL))
18369 {
18370 tab = vim_strchr(virp->vir_line + 1, '\t');
18371 if (tab != NULL)
18372 {
18373 *tab++ = '\0'; /* isolate the variable name */
18374 if (*tab == 'S') /* string var */
18375 is_string = TRUE;
18376
18377 tab = vim_strchr(tab, '\t');
18378 if (tab != NULL)
18379 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018380 if (is_string)
18381 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018382 tv.v_type = VAR_STRING;
18383 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018385 }
18386 else
18387 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018388 tv.v_type = VAR_NUMBER;
18389 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018390 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018391 set_var(virp->vir_line + 1, &tv, FALSE);
18392 if (is_string)
18393 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394 }
18395 }
18396 }
18397
18398 return viminfo_readline(virp);
18399}
18400
18401/*
18402 * Write global vars that start with a capital to the viminfo file
18403 */
18404 void
18405write_viminfo_varlist(fp)
18406 FILE *fp;
18407{
Bram Moolenaar33570922005-01-25 22:26:29 +000018408 hashitem_T *hi;
18409 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018410 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018411 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018412 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018413 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018414 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415
18416 if (find_viminfo_parameter('!') == NULL)
18417 return;
18418
18419 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000018420
Bram Moolenaar33570922005-01-25 22:26:29 +000018421 todo = globvarht.ht_used;
18422 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018423 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018424 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018425 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018426 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018427 this_var = HI2DI(hi);
18428 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018429 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018430 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000018431 {
18432 case VAR_STRING: s = "STR"; break;
18433 case VAR_NUMBER: s = "NUM"; break;
18434 default: continue;
18435 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018436 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018437 p = echo_string(&this_var->di_tv, &tofree, numbuf);
18438 if (p != NULL)
18439 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000018440 vim_free(tofree);
18441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018442 }
18443 }
18444}
18445#endif
18446
18447#if defined(FEAT_SESSION) || defined(PROTO)
18448 int
18449store_session_globals(fd)
18450 FILE *fd;
18451{
Bram Moolenaar33570922005-01-25 22:26:29 +000018452 hashitem_T *hi;
18453 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018454 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455 char_u *p, *t;
18456
Bram Moolenaar33570922005-01-25 22:26:29 +000018457 todo = globvarht.ht_used;
18458 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018459 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018460 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018462 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018463 this_var = HI2DI(hi);
18464 if ((this_var->di_tv.v_type == VAR_NUMBER
18465 || this_var->di_tv.v_type == VAR_STRING)
18466 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018467 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018468 /* Escape special characters with a backslash. Turn a LF and
18469 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018470 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000018471 (char_u *)"\\\"\n\r");
18472 if (p == NULL) /* out of memory */
18473 break;
18474 for (t = p; *t != NUL; ++t)
18475 if (*t == '\n')
18476 *t = 'n';
18477 else if (*t == '\r')
18478 *t = 'r';
18479 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000018480 this_var->di_key,
18481 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18482 : ' ',
18483 p,
18484 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18485 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000018486 || put_eol(fd) == FAIL)
18487 {
18488 vim_free(p);
18489 return FAIL;
18490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491 vim_free(p);
18492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018493 }
18494 }
18495 return OK;
18496}
18497#endif
18498
18499#endif /* FEAT_EVAL */
18500
18501#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
18502
18503
18504#ifdef WIN3264
18505/*
18506 * Functions for ":8" filename modifier: get 8.3 version of a filename.
18507 */
18508static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18509static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
18510static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18511
18512/*
18513 * Get the short pathname of a file.
18514 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
18515 */
18516 static int
18517get_short_pathname(fnamep, bufp, fnamelen)
18518 char_u **fnamep;
18519 char_u **bufp;
18520 int *fnamelen;
18521{
18522 int l,len;
18523 char_u *newbuf;
18524
18525 len = *fnamelen;
18526
18527 l = GetShortPathName(*fnamep, *fnamep, len);
18528 if (l > len - 1)
18529 {
18530 /* If that doesn't work (not enough space), then save the string
18531 * and try again with a new buffer big enough
18532 */
18533 newbuf = vim_strnsave(*fnamep, l);
18534 if (newbuf == NULL)
18535 return 0;
18536
18537 vim_free(*bufp);
18538 *fnamep = *bufp = newbuf;
18539
18540 l = GetShortPathName(*fnamep,*fnamep,l+1);
18541
18542 /* Really should always succeed, as the buffer is big enough */
18543 }
18544
18545 *fnamelen = l;
18546 return 1;
18547}
18548
18549/*
18550 * Create a short path name. Returns the length of the buffer it needs.
18551 * Doesn't copy over the end of the buffer passed in.
18552 */
18553 static int
18554shortpath_for_invalid_fname(fname, bufp, fnamelen)
18555 char_u **fname;
18556 char_u **bufp;
18557 int *fnamelen;
18558{
18559 char_u *s, *p, *pbuf2, *pbuf3;
18560 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018561 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018562
18563 /* Make a copy */
18564 len2 = *fnamelen;
18565 pbuf2 = vim_strnsave(*fname, len2);
18566 pbuf3 = NULL;
18567
18568 s = pbuf2 + len2 - 1; /* Find the end */
18569 slen = 1;
18570 plen = len2;
18571
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018572 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573 {
18574 --s;
18575 ++slen;
18576 --plen;
18577 }
18578
18579 do
18580 {
18581 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018582 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018583 {
18584 --s;
18585 ++slen;
18586 --plen;
18587 }
18588 if (s <= pbuf2)
18589 break;
18590
18591 /* Remeber the character that is about to be blatted */
18592 ch = *s;
18593 *s = 0; /* get_short_pathname requires a null-terminated string */
18594
18595 /* Try it in situ */
18596 p = pbuf2;
18597 if (!get_short_pathname(&p, &pbuf3, &plen))
18598 {
18599 vim_free(pbuf2);
18600 return -1;
18601 }
18602 *s = ch; /* Preserve the string */
18603 } while (plen == 0);
18604
18605 if (plen > 0)
18606 {
18607 /* Remeber the length of the new string. */
18608 *fnamelen = len = plen + slen;
18609 vim_free(*bufp);
18610 if (len > len2)
18611 {
18612 /* If there's not enough space in the currently allocated string,
18613 * then copy it to a buffer big enough.
18614 */
18615 *fname= *bufp = vim_strnsave(p, len);
18616 if (*fname == NULL)
18617 return -1;
18618 }
18619 else
18620 {
18621 /* Transfer pbuf2 to being the main buffer (it's big enough) */
18622 *fname = *bufp = pbuf2;
18623 if (p != pbuf2)
18624 strncpy(*fname, p, plen);
18625 pbuf2 = NULL;
18626 }
18627 /* Concat the next bit */
18628 strncpy(*fname + plen, s, slen);
18629 (*fname)[len] = '\0';
18630 }
18631 vim_free(pbuf3);
18632 vim_free(pbuf2);
18633 return 0;
18634}
18635
18636/*
18637 * Get a pathname for a partial path.
18638 */
18639 static int
18640shortpath_for_partial(fnamep, bufp, fnamelen)
18641 char_u **fnamep;
18642 char_u **bufp;
18643 int *fnamelen;
18644{
18645 int sepcount, len, tflen;
18646 char_u *p;
18647 char_u *pbuf, *tfname;
18648 int hasTilde;
18649
18650 /* Count up the path seperators from the RHS.. so we know which part
18651 * of the path to return.
18652 */
18653 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018654 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655 if (vim_ispathsep(*p))
18656 ++sepcount;
18657
18658 /* Need full path first (use expand_env() to remove a "~/") */
18659 hasTilde = (**fnamep == '~');
18660 if (hasTilde)
18661 pbuf = tfname = expand_env_save(*fnamep);
18662 else
18663 pbuf = tfname = FullName_save(*fnamep, FALSE);
18664
18665 len = tflen = STRLEN(tfname);
18666
18667 if (!get_short_pathname(&tfname, &pbuf, &len))
18668 return -1;
18669
18670 if (len == 0)
18671 {
18672 /* Don't have a valid filename, so shorten the rest of the
18673 * path if we can. This CAN give us invalid 8.3 filenames, but
18674 * there's not a lot of point in guessing what it might be.
18675 */
18676 len = tflen;
18677 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
18678 return -1;
18679 }
18680
18681 /* Count the paths backward to find the beginning of the desired string. */
18682 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018683 {
18684#ifdef FEAT_MBYTE
18685 if (has_mbyte)
18686 p -= mb_head_off(tfname, p);
18687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688 if (vim_ispathsep(*p))
18689 {
18690 if (sepcount == 0 || (hasTilde && sepcount == 1))
18691 break;
18692 else
18693 sepcount --;
18694 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018696 if (hasTilde)
18697 {
18698 --p;
18699 if (p >= tfname)
18700 *p = '~';
18701 else
18702 return -1;
18703 }
18704 else
18705 ++p;
18706
18707 /* Copy in the string - p indexes into tfname - allocated at pbuf */
18708 vim_free(*bufp);
18709 *fnamelen = (int)STRLEN(p);
18710 *bufp = pbuf;
18711 *fnamep = p;
18712
18713 return 0;
18714}
18715#endif /* WIN3264 */
18716
18717/*
18718 * Adjust a filename, according to a string of modifiers.
18719 * *fnamep must be NUL terminated when called. When returning, the length is
18720 * determined by *fnamelen.
18721 * Returns valid flags.
18722 * When there is an error, *fnamep is set to NULL.
18723 */
18724 int
18725modify_fname(src, usedlen, fnamep, bufp, fnamelen)
18726 char_u *src; /* string with modifiers */
18727 int *usedlen; /* characters after src that are used */
18728 char_u **fnamep; /* file name so far */
18729 char_u **bufp; /* buffer for allocated file name or NULL */
18730 int *fnamelen; /* length of fnamep */
18731{
18732 int valid = 0;
18733 char_u *tail;
18734 char_u *s, *p, *pbuf;
18735 char_u dirname[MAXPATHL];
18736 int c;
18737 int has_fullname = 0;
18738#ifdef WIN3264
18739 int has_shortname = 0;
18740#endif
18741
18742repeat:
18743 /* ":p" - full path/file_name */
18744 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
18745 {
18746 has_fullname = 1;
18747
18748 valid |= VALID_PATH;
18749 *usedlen += 2;
18750
18751 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
18752 if ((*fnamep)[0] == '~'
18753#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
18754 && ((*fnamep)[1] == '/'
18755# ifdef BACKSLASH_IN_FILENAME
18756 || (*fnamep)[1] == '\\'
18757# endif
18758 || (*fnamep)[1] == NUL)
18759
18760#endif
18761 )
18762 {
18763 *fnamep = expand_env_save(*fnamep);
18764 vim_free(*bufp); /* free any allocated file name */
18765 *bufp = *fnamep;
18766 if (*fnamep == NULL)
18767 return -1;
18768 }
18769
18770 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018771 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772 {
18773 if (vim_ispathsep(*p)
18774 && p[1] == '.'
18775 && (p[2] == NUL
18776 || vim_ispathsep(p[2])
18777 || (p[2] == '.'
18778 && (p[3] == NUL || vim_ispathsep(p[3])))))
18779 break;
18780 }
18781
18782 /* FullName_save() is slow, don't use it when not needed. */
18783 if (*p != NUL || !vim_isAbsName(*fnamep))
18784 {
18785 *fnamep = FullName_save(*fnamep, *p != NUL);
18786 vim_free(*bufp); /* free any allocated file name */
18787 *bufp = *fnamep;
18788 if (*fnamep == NULL)
18789 return -1;
18790 }
18791
18792 /* Append a path separator to a directory. */
18793 if (mch_isdir(*fnamep))
18794 {
18795 /* Make room for one or two extra characters. */
18796 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
18797 vim_free(*bufp); /* free any allocated file name */
18798 *bufp = *fnamep;
18799 if (*fnamep == NULL)
18800 return -1;
18801 add_pathsep(*fnamep);
18802 }
18803 }
18804
18805 /* ":." - path relative to the current directory */
18806 /* ":~" - path relative to the home directory */
18807 /* ":8" - shortname path - postponed till after */
18808 while (src[*usedlen] == ':'
18809 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
18810 {
18811 *usedlen += 2;
18812 if (c == '8')
18813 {
18814#ifdef WIN3264
18815 has_shortname = 1; /* Postpone this. */
18816#endif
18817 continue;
18818 }
18819 pbuf = NULL;
18820 /* Need full path first (use expand_env() to remove a "~/") */
18821 if (!has_fullname)
18822 {
18823 if (c == '.' && **fnamep == '~')
18824 p = pbuf = expand_env_save(*fnamep);
18825 else
18826 p = pbuf = FullName_save(*fnamep, FALSE);
18827 }
18828 else
18829 p = *fnamep;
18830
18831 has_fullname = 0;
18832
18833 if (p != NULL)
18834 {
18835 if (c == '.')
18836 {
18837 mch_dirname(dirname, MAXPATHL);
18838 s = shorten_fname(p, dirname);
18839 if (s != NULL)
18840 {
18841 *fnamep = s;
18842 if (pbuf != NULL)
18843 {
18844 vim_free(*bufp); /* free any allocated file name */
18845 *bufp = pbuf;
18846 pbuf = NULL;
18847 }
18848 }
18849 }
18850 else
18851 {
18852 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
18853 /* Only replace it when it starts with '~' */
18854 if (*dirname == '~')
18855 {
18856 s = vim_strsave(dirname);
18857 if (s != NULL)
18858 {
18859 *fnamep = s;
18860 vim_free(*bufp);
18861 *bufp = s;
18862 }
18863 }
18864 }
18865 vim_free(pbuf);
18866 }
18867 }
18868
18869 tail = gettail(*fnamep);
18870 *fnamelen = (int)STRLEN(*fnamep);
18871
18872 /* ":h" - head, remove "/file_name", can be repeated */
18873 /* Don't remove the first "/" or "c:\" */
18874 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
18875 {
18876 valid |= VALID_HEAD;
18877 *usedlen += 2;
18878 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018879 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018880 --tail;
18881 *fnamelen = (int)(tail - *fnamep);
18882#ifdef VMS
18883 if (*fnamelen > 0)
18884 *fnamelen += 1; /* the path separator is part of the path */
18885#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018886 while (tail > s && !after_pathsep(s, tail))
18887 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018888 }
18889
18890 /* ":8" - shortname */
18891 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
18892 {
18893 *usedlen += 2;
18894#ifdef WIN3264
18895 has_shortname = 1;
18896#endif
18897 }
18898
18899#ifdef WIN3264
18900 /* Check shortname after we have done 'heads' and before we do 'tails'
18901 */
18902 if (has_shortname)
18903 {
18904 pbuf = NULL;
18905 /* Copy the string if it is shortened by :h */
18906 if (*fnamelen < (int)STRLEN(*fnamep))
18907 {
18908 p = vim_strnsave(*fnamep, *fnamelen);
18909 if (p == 0)
18910 return -1;
18911 vim_free(*bufp);
18912 *bufp = *fnamep = p;
18913 }
18914
18915 /* Split into two implementations - makes it easier. First is where
18916 * there isn't a full name already, second is where there is.
18917 */
18918 if (!has_fullname && !vim_isAbsName(*fnamep))
18919 {
18920 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
18921 return -1;
18922 }
18923 else
18924 {
18925 int l;
18926
18927 /* Simple case, already have the full-name
18928 * Nearly always shorter, so try first time. */
18929 l = *fnamelen;
18930 if (!get_short_pathname(fnamep, bufp, &l))
18931 return -1;
18932
18933 if (l == 0)
18934 {
18935 /* Couldn't find the filename.. search the paths.
18936 */
18937 l = *fnamelen;
18938 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
18939 return -1;
18940 }
18941 *fnamelen = l;
18942 }
18943 }
18944#endif /* WIN3264 */
18945
18946 /* ":t" - tail, just the basename */
18947 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
18948 {
18949 *usedlen += 2;
18950 *fnamelen -= (int)(tail - *fnamep);
18951 *fnamep = tail;
18952 }
18953
18954 /* ":e" - extension, can be repeated */
18955 /* ":r" - root, without extension, can be repeated */
18956 while (src[*usedlen] == ':'
18957 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
18958 {
18959 /* find a '.' in the tail:
18960 * - for second :e: before the current fname
18961 * - otherwise: The last '.'
18962 */
18963 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
18964 s = *fnamep - 2;
18965 else
18966 s = *fnamep + *fnamelen - 1;
18967 for ( ; s > tail; --s)
18968 if (s[0] == '.')
18969 break;
18970 if (src[*usedlen + 1] == 'e') /* :e */
18971 {
18972 if (s > tail)
18973 {
18974 *fnamelen += (int)(*fnamep - (s + 1));
18975 *fnamep = s + 1;
18976#ifdef VMS
18977 /* cut version from the extension */
18978 s = *fnamep + *fnamelen - 1;
18979 for ( ; s > *fnamep; --s)
18980 if (s[0] == ';')
18981 break;
18982 if (s > *fnamep)
18983 *fnamelen = s - *fnamep;
18984#endif
18985 }
18986 else if (*fnamep <= tail)
18987 *fnamelen = 0;
18988 }
18989 else /* :r */
18990 {
18991 if (s > tail) /* remove one extension */
18992 *fnamelen = (int)(s - *fnamep);
18993 }
18994 *usedlen += 2;
18995 }
18996
18997 /* ":s?pat?foo?" - substitute */
18998 /* ":gs?pat?foo?" - global substitute */
18999 if (src[*usedlen] == ':'
19000 && (src[*usedlen + 1] == 's'
19001 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19002 {
19003 char_u *str;
19004 char_u *pat;
19005 char_u *sub;
19006 int sep;
19007 char_u *flags;
19008 int didit = FALSE;
19009
19010 flags = (char_u *)"";
19011 s = src + *usedlen + 2;
19012 if (src[*usedlen + 1] == 'g')
19013 {
19014 flags = (char_u *)"g";
19015 ++s;
19016 }
19017
19018 sep = *s++;
19019 if (sep)
19020 {
19021 /* find end of pattern */
19022 p = vim_strchr(s, sep);
19023 if (p != NULL)
19024 {
19025 pat = vim_strnsave(s, (int)(p - s));
19026 if (pat != NULL)
19027 {
19028 s = p + 1;
19029 /* find end of substitution */
19030 p = vim_strchr(s, sep);
19031 if (p != NULL)
19032 {
19033 sub = vim_strnsave(s, (int)(p - s));
19034 str = vim_strnsave(*fnamep, *fnamelen);
19035 if (sub != NULL && str != NULL)
19036 {
19037 *usedlen = (int)(p + 1 - src);
19038 s = do_string_sub(str, pat, sub, flags);
19039 if (s != NULL)
19040 {
19041 *fnamep = s;
19042 *fnamelen = (int)STRLEN(s);
19043 vim_free(*bufp);
19044 *bufp = s;
19045 didit = TRUE;
19046 }
19047 }
19048 vim_free(sub);
19049 vim_free(str);
19050 }
19051 vim_free(pat);
19052 }
19053 }
19054 /* after using ":s", repeat all the modifiers */
19055 if (didit)
19056 goto repeat;
19057 }
19058 }
19059
19060 return valid;
19061}
19062
19063/*
19064 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19065 * "flags" can be "g" to do a global substitute.
19066 * Returns an allocated string, NULL for error.
19067 */
19068 char_u *
19069do_string_sub(str, pat, sub, flags)
19070 char_u *str;
19071 char_u *pat;
19072 char_u *sub;
19073 char_u *flags;
19074{
19075 int sublen;
19076 regmatch_T regmatch;
19077 int i;
19078 int do_all;
19079 char_u *tail;
19080 garray_T ga;
19081 char_u *ret;
19082 char_u *save_cpo;
19083
19084 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19085 save_cpo = p_cpo;
19086 p_cpo = (char_u *)"";
19087
19088 ga_init2(&ga, 1, 200);
19089
19090 do_all = (flags[0] == 'g');
19091
19092 regmatch.rm_ic = p_ic;
19093 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19094 if (regmatch.regprog != NULL)
19095 {
19096 tail = str;
19097 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19098 {
19099 /*
19100 * Get some space for a temporary buffer to do the substitution
19101 * into. It will contain:
19102 * - The text up to where the match is.
19103 * - The substituted text.
19104 * - The text after the match.
19105 */
19106 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19107 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19108 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19109 {
19110 ga_clear(&ga);
19111 break;
19112 }
19113
19114 /* copy the text up to where the match is */
19115 i = (int)(regmatch.startp[0] - tail);
19116 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19117 /* add the substituted text */
19118 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19119 + ga.ga_len + i, TRUE, TRUE, FALSE);
19120 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019121 /* avoid getting stuck on a match with an empty string */
19122 if (tail == regmatch.endp[0])
19123 {
19124 if (*tail == NUL)
19125 break;
19126 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19127 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128 }
19129 else
19130 {
19131 tail = regmatch.endp[0];
19132 if (*tail == NUL)
19133 break;
19134 }
19135 if (!do_all)
19136 break;
19137 }
19138
19139 if (ga.ga_data != NULL)
19140 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19141
19142 vim_free(regmatch.regprog);
19143 }
19144
19145 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19146 ga_clear(&ga);
19147 p_cpo = save_cpo;
19148
19149 return ret;
19150}
19151
19152#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */