blob: 689af1fbfa7d4c356a4ae000b0bc0ac5db43a4e0 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
13#if defined(MSDOS) || defined(MSWIN)
14# include <io.h> /* for mch_open(), must be before vim.h */
15#endif
16
17#include "vim.h"
18
19#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
23#ifdef MACOS
24# include <time.h> /* for time_t */
25#endif
26
27#ifdef HAVE_FCNTL_H
28# include <fcntl.h>
29#endif
30
31#if defined(FEAT_EVAL) || defined(PROTO)
32
Bram Moolenaar33570922005-01-25 22:26:29 +000033#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000034
35/*
Bram Moolenaar33570922005-01-25 22:26:29 +000036 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
37 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000038 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
39 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
40 * HI2DI() converts a hashitem pointer to a dictitem pointer.
41 */
Bram Moolenaar33570922005-01-25 22:26:29 +000042static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000043#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000044#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000045#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000046
47/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000048 * Structure returned by get_lval() and used by set_var_lval().
49 * For a plain name:
50 * "name" points to the variable name.
51 * "exp_name" is NULL.
52 * "tv" is NULL
53 * For a magic braces name:
54 * "name" points to the expanded variable name.
55 * "exp_name" is non-NULL, to be freed later.
56 * "tv" is NULL
57 * For an index in a list:
58 * "name" points to the (expanded) variable name.
59 * "exp_name" NULL or non-NULL, to be freed later.
60 * "tv" points to the (first) list item value
61 * "li" points to the (first) list item
62 * "range", "n1", "n2" and "empty2" indicate what items are used.
63 * For an existing Dict item:
64 * "name" points to the (expanded) variable name.
65 * "exp_name" NULL or non-NULL, to be freed later.
66 * "tv" points to the dict item value
67 * "newkey" is NULL
68 * For a non-existing Dict item:
69 * "name" points to the (expanded) variable name.
70 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000071 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000072 * "newkey" is the key for the new item.
73 */
74typedef struct lval_S
75{
76 char_u *ll_name; /* start of variable name (can be NULL) */
77 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000078 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000079 isn't NULL it's the Dict to which to add
80 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000081 listitem_T *ll_li; /* The list item or NULL. */
82 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 int ll_range; /* TRUE when a [i:j] range was used */
84 long ll_n1; /* First index for list */
85 long ll_n2; /* Second index for list range */
86 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000087 dict_T *ll_dict; /* The Dictionary or NULL */
88 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000089 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000090} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000091
Bram Moolenaar8c711452005-01-14 21:53:12 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000099static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000100static char *e_listreq = N_("E714: List required");
101static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000102static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000103static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
104static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
105static char *e_funcdict = N_("E717: Dictionary entry already exists");
106static char *e_funcref = N_("E718: Funcref required");
107static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
108static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000109static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000110static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000111/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000112 * All user-defined global variables are stored in dictionary "globvardict".
113 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000115static dict_T globvardict;
116static dictitem_T globvars_var;
117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
128 */
129static int current_copyID = 0;
130
131/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000132 * Array to hold the hashtab with variables local to each sourced script.
133 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000135typedef struct
136{
137 dictitem_T sv_var;
138 dict_T sv_dict;
139} scriptvar_T;
140
141static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
142#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
143#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144
145static int echo_attr = 0; /* attributes used for ":echo" */
146
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000147/* Values for trans_function_name() argument: */
148#define TFN_INT 1 /* internal function name OK */
149#define TFN_QUIET 2 /* no error messages */
150
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151/*
152 * Structure to hold info for a user function.
153 */
154typedef struct ufunc ufunc_T;
155
156struct ufunc
157{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000158 int uf_varargs; /* variable nr of arguments */
159 int uf_flags;
160 int uf_calls; /* nr of active calls */
161 garray_T uf_args; /* arguments */
162 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000163#ifdef FEAT_PROFILE
164 int uf_profiling; /* TRUE when func is being profiled */
165 /* profiling the function as a whole */
166 int uf_tm_count; /* nr of calls */
167 proftime_T uf_tm_total; /* time spend in function + children */
168 proftime_T uf_tm_self; /* time spend in function itself */
169 proftime_T uf_tm_start; /* time at function call */
170 proftime_T uf_tm_children; /* time spent in children this call */
171 /* profiling the function per line */
172 int *uf_tml_count; /* nr of times line was executed */
173 proftime_T *uf_tml_total; /* time spend in a line + children */
174 proftime_T *uf_tml_self; /* time spend in a line itself */
175 proftime_T uf_tml_start; /* start time for current line */
176 proftime_T uf_tml_children; /* time spent in children for this line */
177 proftime_T uf_tml_wait; /* start wait time for current line */
178 int uf_tml_idx; /* index of line being timed; -1 if none */
179 int uf_tml_execed; /* line being timed was executed */
180#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000181 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183 int uf_refcount; /* for numbered function: reference count */
184 char_u uf_name[1]; /* name of function (actually longer); can
185 start with <SNR>123_ (<SNR> is K_SPECIAL
186 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187};
188
189/* function flags */
190#define FC_ABORT 1 /* abort function on error */
191#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000192#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193
Bram Moolenaard9fba312005-06-26 22:34:35 +0000194#define DEL_REFCOUNT 999999 /* list/dict is being deleted */
195
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000197 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000199static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000201/* list heads for garbage collection */
202static dict_T *first_dict = NULL; /* list of all dicts */
203static list_T *first_list = NULL; /* list of all lists */
204
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000205/* From user function to hashitem and back. */
206static ufunc_T dumuf;
207#define UF2HIKEY(fp) ((fp)->uf_name)
208#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
209#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
210
211#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
212#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213
Bram Moolenaar33570922005-01-25 22:26:29 +0000214#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
215#define VAR_SHORT_LEN 20 /* short variable name length */
216#define FIXVAR_CNT 12 /* number of fixed variables */
217
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000219typedef struct funccall_S funccall_T;
220
221struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222{
223 ufunc_T *func; /* function being called */
224 int linenr; /* next line to be executed */
225 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000226 struct /* fixed variables for arguments */
227 {
228 dictitem_T var; /* variable (without room for name) */
229 char_u room[VAR_SHORT_LEN]; /* room for the name */
230 } fixvar[FIXVAR_CNT];
231 dict_T l_vars; /* l: local function variables */
232 dictitem_T l_vars_var; /* variable for l: scope */
233 dict_T l_avars; /* a: argument variables */
234 dictitem_T l_avars_var; /* variable for a: scope */
235 list_T l_varlist; /* list for a:000 */
236 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
237 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238 linenr_T breakpoint; /* next line with breakpoint or zero */
239 int dbg_tick; /* debug_tick when breakpoint was set */
240 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000241#ifdef FEAT_PROFILE
242 proftime_T prof_child; /* time spent in a child */
243#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000244 funccall_T *caller; /* calling function or NULL */
245};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000248 * Info used by a ":for" loop.
249 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000250typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000251{
252 int fi_semicolon; /* TRUE if ending in '; var]' */
253 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000254 listwatch_T fi_lw; /* keep an eye on the item used. */
255 list_T *fi_list; /* list being used */
256} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000258/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000259 * Struct used by trans_function_name()
260 */
261typedef struct
262{
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000264 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 dictitem_T *fd_di; /* Dictionary item used */
266} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000267
Bram Moolenaara7043832005-01-21 11:56:39 +0000268
269/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000270 * Array to hold the value of v: variables.
271 * The value is in a dictitem, so that it can also be used in the v: scope.
272 * The reason to use this table anyway is for very quick access to the
273 * variables with the VV_ defines.
274 */
275#include "version.h"
276
277/* values for vv_flags: */
278#define VV_COMPAT 1 /* compatible, also used without "v:" */
279#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000280#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000282#define VV_NAME(s, t) s, {{t}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000283
284static struct vimvar
285{
286 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000287 dictitem_T vv_di; /* value and name for key */
288 char vv_filler[16]; /* space for LONGEST name below!!! */
289 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
290} vimvars[VV_LEN] =
291{
292 /*
293 * The order here must match the VV_ defines in vim.h!
294 * Initializing a union does not work, leave tv.vval empty to get zero's.
295 */
296 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
297 {VV_NAME("count1", VAR_NUMBER), VV_RO},
298 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
299 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
300 {VV_NAME("warningmsg", VAR_STRING), 0},
301 {VV_NAME("statusmsg", VAR_STRING), 0},
302 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
303 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
304 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
305 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
306 {VV_NAME("termresponse", VAR_STRING), VV_RO},
307 {VV_NAME("fname", VAR_STRING), VV_RO},
308 {VV_NAME("lang", VAR_STRING), VV_RO},
309 {VV_NAME("lc_time", VAR_STRING), VV_RO},
310 {VV_NAME("ctype", VAR_STRING), VV_RO},
311 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
312 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
313 {VV_NAME("fname_in", VAR_STRING), VV_RO},
314 {VV_NAME("fname_out", VAR_STRING), VV_RO},
315 {VV_NAME("fname_new", VAR_STRING), VV_RO},
316 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
317 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
318 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
320 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
321 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
322 {VV_NAME("progname", VAR_STRING), VV_RO},
323 {VV_NAME("servername", VAR_STRING), VV_RO},
324 {VV_NAME("dying", VAR_NUMBER), VV_RO},
325 {VV_NAME("exception", VAR_STRING), VV_RO},
326 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
327 {VV_NAME("register", VAR_STRING), VV_RO},
328 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
329 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000330 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
331 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000332 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000333 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
334 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000335 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
336 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
337 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
338 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
339 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000340};
341
342/* shorthand */
343#define vv_type vv_di.di_tv.v_type
344#define vv_nr vv_di.di_tv.vval.v_number
345#define vv_str vv_di.di_tv.vval.v_string
346#define vv_tv vv_di.di_tv
347
348/*
349 * The v: variables are stored in dictionary "vimvardict".
350 * "vimvars_var" is the variable that is used for the "l:" scope.
351 */
352static dict_T vimvardict;
353static dictitem_T vimvars_var;
354#define vimvarht vimvardict.dv_hashtab
355
Bram Moolenaara40058a2005-07-11 22:42:07 +0000356static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
357static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
358#if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
359static int call_vim_function __ARGS((char_u *func, int argc, char_u **argv, int safe, typval_T *rettv));
360#endif
361static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
362static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
363static char_u *skip_var_one __ARGS((char_u *arg));
364static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
365static void list_glob_vars __ARGS((void));
366static void list_buf_vars __ARGS((void));
367static void list_win_vars __ARGS((void));
368static void list_vim_vars __ARGS((void));
369static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
370static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
371static int check_changedtick __ARGS((char_u *arg));
372static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
373static void clear_lval __ARGS((lval_T *lp));
374static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
375static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
376static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
377static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
378static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
379static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
380static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
381static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
382static void item_lock __ARGS((typval_T *tv, int deep, int lock));
383static int tv_islocked __ARGS((typval_T *tv));
384
Bram Moolenaar33570922005-01-25 22:26:29 +0000385static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
386static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
387static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
388static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
389static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
390static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
391static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
392static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000393
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000394static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000395static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
396static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
397static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
398static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
399static list_T *list_alloc __ARGS((void));
Bram Moolenaar33570922005-01-25 22:26:29 +0000400static void list_free __ARGS((list_T *l));
401static listitem_T *listitem_alloc __ARGS((void));
402static void listitem_free __ARGS((listitem_T *item));
403static void listitem_remove __ARGS((list_T *l, listitem_T *item));
404static long list_len __ARGS((list_T *l));
405static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
406static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
407static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
Bram Moolenaar33570922005-01-25 22:26:29 +0000408static listitem_T *list_find __ARGS((list_T *l, long n));
409static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000410static void list_append __ARGS((list_T *l, listitem_T *item));
411static int list_append_tv __ARGS((list_T *l, typval_T *tv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000412static int list_append_string __ARGS((list_T *l, char_u *str));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
414static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
415static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000416static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000417static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
418static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000419static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000420static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
421static void set_ref_in_list __ARGS((list_T *l, int copyID));
422static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static void dict_unref __ARGS((dict_T *d));
424static void dict_free __ARGS((dict_T *d));
425static dictitem_T *dictitem_alloc __ARGS((char_u *key));
426static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
427static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
428static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000429static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000430static int dict_add __ARGS((dict_T *d, dictitem_T *item));
431static long dict_len __ARGS((dict_T *d));
432static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
433static char_u *dict2string __ARGS((typval_T *tv));
434static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar33570922005-01-25 22:26:29 +0000435static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
436static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
437static char_u *string_quote __ARGS((char_u *str, int function));
438static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
439static int find_internal_func __ARGS((char_u *name));
440static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
441static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
442static int call_func __ARGS((char_u *name, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000443static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000444
445static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000464#if defined(FEAT_INS_EXPAND)
465static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
473static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000499static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000500static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000501static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000502static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000514static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000542static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000543static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
557static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000559static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000563#ifdef vim_mkdir
564static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
565#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000570static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000571static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000572static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000573static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000584static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
589static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000591static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000596static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000597static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000599static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
600#ifdef HAVE_STRFTIME
601static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
602#endif
603static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
607static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
608static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000615static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000616static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000617static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000618static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000619static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
623static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
624static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
625static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
626static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
627static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
629static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
631static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
632static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000633static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000634
Bram Moolenaar33570922005-01-25 22:26:29 +0000635static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
636static int get_env_len __ARGS((char_u **arg));
637static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000638static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000639static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
640#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
641#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
642 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000643static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000644static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000645static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000646static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
647static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000648static typval_T *alloc_tv __ARGS((void));
649static typval_T *alloc_string_tv __ARGS((char_u *string));
650static void free_tv __ARGS((typval_T *varp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000651static void init_tv __ARGS((typval_T *varp));
652static long get_tv_number __ARGS((typval_T *varp));
653static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000654static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000655static char_u *get_tv_string __ARGS((typval_T *varp));
656static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000657static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000658static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000659static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
661static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
662static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
663static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
664static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
665static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
666static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000667static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000668static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000669static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000670static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
671static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
672static int eval_fname_script __ARGS((char_u *p));
673static int eval_fname_sid __ARGS((char_u *p));
674static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000675static ufunc_T *find_func __ARGS((char_u *name));
676static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000677static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000678#ifdef FEAT_PROFILE
679static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000680static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
681static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
682static int
683# ifdef __BORLANDC__
684 _RTLENTRYF
685# endif
686 prof_total_cmp __ARGS((const void *s1, const void *s2));
687static int
688# ifdef __BORLANDC__
689 _RTLENTRYF
690# endif
691 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000692#endif
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000693static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000694static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000695static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static void func_free __ARGS((ufunc_T *fp));
697static void func_unref __ARGS((char_u *name));
698static void func_ref __ARGS((char_u *name));
699static 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));
700static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
701
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000702/* Character used as separated in autoload function/variable names. */
703#define AUTOLOAD_CHAR '#'
704
Bram Moolenaar33570922005-01-25 22:26:29 +0000705/*
706 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000707 */
708 void
709eval_init()
710{
Bram Moolenaar33570922005-01-25 22:26:29 +0000711 int i;
712 struct vimvar *p;
713
714 init_var_dict(&globvardict, &globvars_var);
715 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000716 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000717 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000718
719 for (i = 0; i < VV_LEN; ++i)
720 {
721 p = &vimvars[i];
722 STRCPY(p->vv_di.di_key, p->vv_name);
723 if (p->vv_flags & VV_RO)
724 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
725 else if (p->vv_flags & VV_RO_SBX)
726 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
727 else
728 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000729
730 /* add to v: scope dict, unless the value is not always available */
731 if (p->vv_type != VAR_UNKNOWN)
732 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000733 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000734 /* add to compat scope dict */
735 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000736 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000737}
738
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000739#if defined(EXITFREE) || defined(PROTO)
740 void
741eval_clear()
742{
743 int i;
744 struct vimvar *p;
745
746 for (i = 0; i < VV_LEN; ++i)
747 {
748 p = &vimvars[i];
749 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000750 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000751 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000752 p->vv_di.di_tv.vval.v_string = NULL;
753 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000754 }
755 hash_clear(&vimvarht);
756 hash_clear(&compat_hashtab);
757
758 /* script-local variables */
759 for (i = 1; i <= ga_scripts.ga_len; ++i)
760 vars_clear(&SCRIPT_VARS(i));
761 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000762 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000763
764 /* global variables */
765 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000766
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000767 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000768 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000769 hash_clear(&func_hashtab);
770
771 /* unreferenced lists and dicts */
772 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000773}
774#endif
775
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 * Return the name of the executed function.
778 */
779 char_u *
780func_name(cookie)
781 void *cookie;
782{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000783 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784}
785
786/*
787 * Return the address holding the next breakpoint line for a funccall cookie.
788 */
789 linenr_T *
790func_breakpoint(cookie)
791 void *cookie;
792{
Bram Moolenaar33570922005-01-25 22:26:29 +0000793 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794}
795
796/*
797 * Return the address holding the debug tick for a funccall cookie.
798 */
799 int *
800func_dbg_tick(cookie)
801 void *cookie;
802{
Bram Moolenaar33570922005-01-25 22:26:29 +0000803 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804}
805
806/*
807 * Return the nesting level for a funccall cookie.
808 */
809 int
810func_level(cookie)
811 void *cookie;
812{
Bram Moolenaar33570922005-01-25 22:26:29 +0000813 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814}
815
816/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000817funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818
819/*
820 * Return TRUE when a function was ended by a ":return" command.
821 */
822 int
823current_func_returned()
824{
825 return current_funccal->returned;
826}
827
828
829/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 * Set an internal variable to a string value. Creates the variable if it does
831 * not already exist.
832 */
833 void
834set_internal_string_var(name, value)
835 char_u *name;
836 char_u *value;
837{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000838 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000839 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840
841 val = vim_strsave(value);
842 if (val != NULL)
843 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000844 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000845 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000847 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000848 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 }
850 }
851}
852
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000853static lval_T *redir_lval = NULL;
854static char_u *redir_endp = NULL;
855static char_u *redir_varname = NULL;
856
857/*
858 * Start recording command output to a variable
859 * Returns OK if successfully completed the setup. FAIL otherwise.
860 */
861 int
862var_redir_start(name, append)
863 char_u *name;
864 int append; /* append to an existing variable */
865{
866 int save_emsg;
867 int err;
868 typval_T tv;
869
870 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000871 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000872 {
873 EMSG(_(e_invarg));
874 return FAIL;
875 }
876
877 redir_varname = vim_strsave(name);
878 if (redir_varname == NULL)
879 return FAIL;
880
881 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
882 if (redir_lval == NULL)
883 {
884 var_redir_stop();
885 return FAIL;
886 }
887
888 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000889 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
890 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000891 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
892 {
893 if (redir_endp != NULL && *redir_endp != NUL)
894 /* Trailing characters are present after the variable name */
895 EMSG(_(e_trailing));
896 else
897 EMSG(_(e_invarg));
898 var_redir_stop();
899 return FAIL;
900 }
901
902 /* check if we can write to the variable: set it to or append an empty
903 * string */
904 save_emsg = did_emsg;
905 did_emsg = FALSE;
906 tv.v_type = VAR_STRING;
907 tv.vval.v_string = (char_u *)"";
908 if (append)
909 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
910 else
911 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
912 err = did_emsg;
913 did_emsg += save_emsg;
914 if (err)
915 {
916 var_redir_stop();
917 return FAIL;
918 }
919 if (redir_lval->ll_newkey != NULL)
920 {
921 /* Dictionary item was created, don't do it again. */
922 vim_free(redir_lval->ll_newkey);
923 redir_lval->ll_newkey = NULL;
924 }
925
926 return OK;
927}
928
929/*
930 * Append "value[len]" to the variable set by var_redir_start().
931 */
932 void
933var_redir_str(value, len)
934 char_u *value;
935 int len;
936{
937 char_u *val;
938 typval_T tv;
939 int save_emsg;
940 int err;
941
942 if (redir_lval == NULL)
943 return;
944
945 if (len == -1)
946 /* Append the entire string */
947 val = vim_strsave(value);
948 else
949 /* Append only the specified number of characters */
950 val = vim_strnsave(value, len);
951 if (val == NULL)
952 return;
953
954 tv.v_type = VAR_STRING;
955 tv.vval.v_string = val;
956
957 save_emsg = did_emsg;
958 did_emsg = FALSE;
959 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
960 err = did_emsg;
961 did_emsg += save_emsg;
962 if (err)
963 var_redir_stop();
964
965 vim_free(tv.vval.v_string);
966}
967
968/*
969 * Stop redirecting command output to a variable.
970 */
971 void
972var_redir_stop()
973{
974 if (redir_lval != NULL)
975 {
976 clear_lval(redir_lval);
977 vim_free(redir_lval);
978 redir_lval = NULL;
979 }
980 vim_free(redir_varname);
981 redir_varname = NULL;
982}
983
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984# if defined(FEAT_MBYTE) || defined(PROTO)
985 int
986eval_charconvert(enc_from, enc_to, fname_from, fname_to)
987 char_u *enc_from;
988 char_u *enc_to;
989 char_u *fname_from;
990 char_u *fname_to;
991{
992 int err = FALSE;
993
994 set_vim_var_string(VV_CC_FROM, enc_from, -1);
995 set_vim_var_string(VV_CC_TO, enc_to, -1);
996 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
997 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
998 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
999 err = TRUE;
1000 set_vim_var_string(VV_CC_FROM, NULL, -1);
1001 set_vim_var_string(VV_CC_TO, NULL, -1);
1002 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1003 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1004
1005 if (err)
1006 return FAIL;
1007 return OK;
1008}
1009# endif
1010
1011# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1012 int
1013eval_printexpr(fname, args)
1014 char_u *fname;
1015 char_u *args;
1016{
1017 int err = FALSE;
1018
1019 set_vim_var_string(VV_FNAME_IN, fname, -1);
1020 set_vim_var_string(VV_CMDARG, args, -1);
1021 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1022 err = TRUE;
1023 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1024 set_vim_var_string(VV_CMDARG, NULL, -1);
1025
1026 if (err)
1027 {
1028 mch_remove(fname);
1029 return FAIL;
1030 }
1031 return OK;
1032}
1033# endif
1034
1035# if defined(FEAT_DIFF) || defined(PROTO)
1036 void
1037eval_diff(origfile, newfile, outfile)
1038 char_u *origfile;
1039 char_u *newfile;
1040 char_u *outfile;
1041{
1042 int err = FALSE;
1043
1044 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1045 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1046 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1047 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1048 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1049 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1050 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1051}
1052
1053 void
1054eval_patch(origfile, difffile, outfile)
1055 char_u *origfile;
1056 char_u *difffile;
1057 char_u *outfile;
1058{
1059 int err;
1060
1061 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1062 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1063 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1064 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1065 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1066 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1067 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1068}
1069# endif
1070
1071/*
1072 * Top level evaluation function, returning a boolean.
1073 * Sets "error" to TRUE if there was an error.
1074 * Return TRUE or FALSE.
1075 */
1076 int
1077eval_to_bool(arg, error, nextcmd, skip)
1078 char_u *arg;
1079 int *error;
1080 char_u **nextcmd;
1081 int skip; /* only parse, don't execute */
1082{
Bram Moolenaar33570922005-01-25 22:26:29 +00001083 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 int retval = FALSE;
1085
1086 if (skip)
1087 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001088 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 else
1091 {
1092 *error = FALSE;
1093 if (!skip)
1094 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001095 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001096 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 }
1098 }
1099 if (skip)
1100 --emsg_skip;
1101
1102 return retval;
1103}
1104
1105/*
1106 * Top level evaluation function, returning a string. If "skip" is TRUE,
1107 * only parsing to "nextcmd" is done, without reporting errors. Return
1108 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1109 */
1110 char_u *
1111eval_to_string_skip(arg, nextcmd, skip)
1112 char_u *arg;
1113 char_u **nextcmd;
1114 int skip; /* only parse, don't execute */
1115{
Bram Moolenaar33570922005-01-25 22:26:29 +00001116 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 char_u *retval;
1118
1119 if (skip)
1120 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001121 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 retval = NULL;
1123 else
1124 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001125 retval = vim_strsave(get_tv_string(&tv));
1126 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 }
1128 if (skip)
1129 --emsg_skip;
1130
1131 return retval;
1132}
1133
1134/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001135 * Skip over an expression at "*pp".
1136 * Return FAIL for an error, OK otherwise.
1137 */
1138 int
1139skip_expr(pp)
1140 char_u **pp;
1141{
Bram Moolenaar33570922005-01-25 22:26:29 +00001142 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001143
1144 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001145 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001146}
1147
1148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 * Top level evaluation function, returning a string.
1150 * Return pointer to allocated memory, or NULL for failure.
1151 */
1152 char_u *
1153eval_to_string(arg, nextcmd)
1154 char_u *arg;
1155 char_u **nextcmd;
1156{
Bram Moolenaar33570922005-01-25 22:26:29 +00001157 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 char_u *retval;
1159
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001160 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 retval = NULL;
1162 else
1163 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001164 retval = vim_strsave(get_tv_string(&tv));
1165 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 }
1167
1168 return retval;
1169}
1170
1171/*
1172 * Call eval_to_string() with "sandbox" set and not using local variables.
1173 */
1174 char_u *
1175eval_to_string_safe(arg, nextcmd)
1176 char_u *arg;
1177 char_u **nextcmd;
1178{
1179 char_u *retval;
1180 void *save_funccalp;
1181
1182 save_funccalp = save_funccal();
1183 ++sandbox;
1184 retval = eval_to_string(arg, nextcmd);
1185 --sandbox;
1186 restore_funccal(save_funccalp);
1187 return retval;
1188}
1189
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190/*
1191 * Top level evaluation function, returning a number.
1192 * Evaluates "expr" silently.
1193 * Returns -1 for an error.
1194 */
1195 int
1196eval_to_number(expr)
1197 char_u *expr;
1198{
Bram Moolenaar33570922005-01-25 22:26:29 +00001199 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001201 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202
1203 ++emsg_off;
1204
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001205 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 retval = -1;
1207 else
1208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001209 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001210 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 }
1212 --emsg_off;
1213
1214 return retval;
1215}
1216
Bram Moolenaara40058a2005-07-11 22:42:07 +00001217/*
1218 * Prepare v: variable "idx" to be used.
1219 * Save the current typeval in "save_tv".
1220 * When not used yet add the variable to the v: hashtable.
1221 */
1222 static void
1223prepare_vimvar(idx, save_tv)
1224 int idx;
1225 typval_T *save_tv;
1226{
1227 *save_tv = vimvars[idx].vv_tv;
1228 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1229 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1230}
1231
1232/*
1233 * Restore v: variable "idx" to typeval "save_tv".
1234 * When no longer defined, remove the variable from the v: hashtable.
1235 */
1236 static void
1237restore_vimvar(idx, save_tv)
1238 int idx;
1239 typval_T *save_tv;
1240{
1241 hashitem_T *hi;
1242
1243 clear_tv(&vimvars[idx].vv_tv);
1244 vimvars[idx].vv_tv = *save_tv;
1245 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1246 {
1247 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1248 if (HASHITEM_EMPTY(hi))
1249 EMSG2(_(e_intern2), "restore_vimvar()");
1250 else
1251 hash_remove(&vimvarht, hi);
1252 }
1253}
1254
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001255#if defined(FEAT_SYN_HL) || defined(PROTO)
1256/*
1257 * Evaluate an expression to a list with suggestions.
1258 * For the "expr:" part of 'spellsuggest'.
1259 */
1260 list_T *
1261eval_spell_expr(badword, expr)
1262 char_u *badword;
1263 char_u *expr;
1264{
1265 typval_T save_val;
1266 typval_T rettv;
1267 list_T *list = NULL;
1268 char_u *p = skipwhite(expr);
1269
1270 /* Set "v:val" to the bad word. */
1271 prepare_vimvar(VV_VAL, &save_val);
1272 vimvars[VV_VAL].vv_type = VAR_STRING;
1273 vimvars[VV_VAL].vv_str = badword;
1274 if (p_verbose == 0)
1275 ++emsg_off;
1276
1277 if (eval1(&p, &rettv, TRUE) == OK)
1278 {
1279 if (rettv.v_type != VAR_LIST)
1280 clear_tv(&rettv);
1281 else
1282 list = rettv.vval.v_list;
1283 }
1284
1285 if (p_verbose == 0)
1286 --emsg_off;
1287 vimvars[VV_VAL].vv_str = NULL;
1288 restore_vimvar(VV_VAL, &save_val);
1289
1290 return list;
1291}
1292
1293/*
1294 * "list" is supposed to contain two items: a word and a number. Return the
1295 * word in "pp" and the number as the return value.
1296 * Return -1 if anything isn't right.
1297 * Used to get the good word and score from the eval_spell_expr() result.
1298 */
1299 int
1300get_spellword(list, pp)
1301 list_T *list;
1302 char_u **pp;
1303{
1304 listitem_T *li;
1305
1306 li = list->lv_first;
1307 if (li == NULL)
1308 return -1;
1309 *pp = get_tv_string(&li->li_tv);
1310
1311 li = li->li_next;
1312 if (li == NULL)
1313 return -1;
1314 return get_tv_number(&li->li_tv);
1315}
1316#endif
1317
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001318/*
1319 * Top level evaluation function,
1320 */
1321 typval_T *
1322eval_expr(arg, nextcmd)
1323 char_u *arg;
1324 char_u **nextcmd;
1325{
1326 typval_T *tv;
1327
1328 tv = (typval_T *)alloc(sizeof(typval_T));
1329 if (!tv)
1330 return NULL;
1331
1332 if (eval0(arg, tv, nextcmd, TRUE) == FAIL)
1333 {
1334 vim_free(tv);
1335 return NULL;
1336 }
1337
1338 return tv;
1339}
1340
1341
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1343/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001344 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001346 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001348 static int
1349call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 char_u *func;
1351 int argc;
1352 char_u **argv;
1353 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355{
Bram Moolenaar33570922005-01-25 22:26:29 +00001356 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 long n;
1358 int len;
1359 int i;
1360 int doesrange;
1361 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001362 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363
Bram Moolenaar33570922005-01-25 22:26:29 +00001364 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001366 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
1368 for (i = 0; i < argc; i++)
1369 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001370 /* Pass a NULL or empty argument as an empty string */
1371 if (argv[i] == NULL || *argv[i] == NUL)
1372 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001373 argvars[i].v_type = VAR_STRING;
1374 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001375 continue;
1376 }
1377
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 /* Recognize a number argument, the others must be strings. */
1379 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1380 if (len != 0 && len == (int)STRLEN(argv[i]))
1381 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001382 argvars[i].v_type = VAR_NUMBER;
1383 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 }
1385 else
1386 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001387 argvars[i].v_type = VAR_STRING;
1388 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 }
1390 }
1391
1392 if (safe)
1393 {
1394 save_funccalp = save_funccal();
1395 ++sandbox;
1396 }
1397
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001398 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1399 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001401 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 if (safe)
1403 {
1404 --sandbox;
1405 restore_funccal(save_funccalp);
1406 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001407 vim_free(argvars);
1408
1409 if (ret == FAIL)
1410 clear_tv(rettv);
1411
1412 return ret;
1413}
1414
1415/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001416 * Call vimL function "func" and return the result as a string.
1417 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001418 * Uses argv[argc] for the function arguments.
1419 */
1420 void *
1421call_func_retstr(func, argc, argv, safe)
1422 char_u *func;
1423 int argc;
1424 char_u **argv;
1425 int safe; /* use the sandbox */
1426{
1427 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001428 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001429
1430 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1431 return NULL;
1432
1433 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001434 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 return retval;
1436}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001437
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001438#if defined(FEAT_COMPL_FUNC) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001439/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001440 * Call vimL function "func" and return the result as a number.
1441 * Returns -1 when calling the function fails.
1442 * Uses argv[argc] for the function arguments.
1443 */
1444 long
1445call_func_retnr(func, argc, argv, safe)
1446 char_u *func;
1447 int argc;
1448 char_u **argv;
1449 int safe; /* use the sandbox */
1450{
1451 typval_T rettv;
1452 long retval;
1453
1454 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1455 return -1;
1456
1457 retval = get_tv_number_chk(&rettv, NULL);
1458 clear_tv(&rettv);
1459 return retval;
1460}
1461#endif
1462
1463/*
1464 * Call vimL function "func" and return the result as a list
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001465 * Uses argv[argc] for the function arguments.
1466 */
1467 void *
1468call_func_retlist(func, argc, argv, safe)
1469 char_u *func;
1470 int argc;
1471 char_u **argv;
1472 int safe; /* use the sandbox */
1473{
1474 typval_T rettv;
1475
1476 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1477 return NULL;
1478
1479 if (rettv.v_type != VAR_LIST)
1480 {
1481 clear_tv(&rettv);
1482 return NULL;
1483 }
1484
1485 return rettv.vval.v_list;
1486}
1487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488#endif
1489
1490/*
1491 * Save the current function call pointer, and set it to NULL.
1492 * Used when executing autocommands and for ":source".
1493 */
1494 void *
1495save_funccal()
1496{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001497 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 current_funccal = NULL;
1500 return (void *)fc;
1501}
1502
1503 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001504restore_funccal(vfc)
1505 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001507 funccall_T *fc = (funccall_T *)vfc;
1508
1509 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510}
1511
Bram Moolenaar05159a02005-02-26 23:04:13 +00001512#if defined(FEAT_PROFILE) || defined(PROTO)
1513/*
1514 * Prepare profiling for entering a child or something else that is not
1515 * counted for the script/function itself.
1516 * Should always be called in pair with prof_child_exit().
1517 */
1518 void
1519prof_child_enter(tm)
1520 proftime_T *tm; /* place to store waittime */
1521{
1522 funccall_T *fc = current_funccal;
1523
1524 if (fc != NULL && fc->func->uf_profiling)
1525 profile_start(&fc->prof_child);
1526 script_prof_save(tm);
1527}
1528
1529/*
1530 * Take care of time spent in a child.
1531 * Should always be called after prof_child_enter().
1532 */
1533 void
1534prof_child_exit(tm)
1535 proftime_T *tm; /* where waittime was stored */
1536{
1537 funccall_T *fc = current_funccal;
1538
1539 if (fc != NULL && fc->func->uf_profiling)
1540 {
1541 profile_end(&fc->prof_child);
1542 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1543 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1544 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1545 }
1546 script_prof_restore(tm);
1547}
1548#endif
1549
1550
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551#ifdef FEAT_FOLDING
1552/*
1553 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1554 * it in "*cp". Doesn't give error messages.
1555 */
1556 int
1557eval_foldexpr(arg, cp)
1558 char_u *arg;
1559 int *cp;
1560{
Bram Moolenaar33570922005-01-25 22:26:29 +00001561 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 int retval;
1563 char_u *s;
1564
1565 ++emsg_off;
1566 ++sandbox;
1567 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001568 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 retval = 0;
1570 else
1571 {
1572 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001573 if (tv.v_type == VAR_NUMBER)
1574 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001575 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 retval = 0;
1577 else
1578 {
1579 /* If the result is a string, check if there is a non-digit before
1580 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001581 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 if (!VIM_ISDIGIT(*s) && *s != '-')
1583 *cp = *s++;
1584 retval = atol((char *)s);
1585 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001586 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 }
1588 --emsg_off;
1589 --sandbox;
1590
1591 return retval;
1592}
1593#endif
1594
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001596 * ":let" list all variable values
1597 * ":let var1 var2" list variable values
1598 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001599 * ":let var += expr" assignment command.
1600 * ":let var -= expr" assignment command.
1601 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001602 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 */
1604 void
1605ex_let(eap)
1606 exarg_T *eap;
1607{
1608 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001609 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001610 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001612 int var_count = 0;
1613 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001614 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001616 expr = skip_var_list(arg, &var_count, &semicolon);
1617 if (expr == NULL)
1618 return;
1619 expr = vim_strchr(expr, '=');
1620 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001622 /*
1623 * ":let" without "=": list variables
1624 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001625 if (*arg == '[')
1626 EMSG(_(e_invarg));
1627 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001628 /* ":let var1 var2" */
1629 arg = list_arg_vars(eap, arg);
1630 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001632 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001633 list_glob_vars();
1634 list_buf_vars();
1635 list_win_vars();
1636 list_vim_vars();
1637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 eap->nextcmd = check_nextcmd(arg);
1639 }
1640 else
1641 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001642 op[0] = '=';
1643 op[1] = NUL;
1644 if (expr > arg)
1645 {
1646 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1647 op[0] = expr[-1]; /* +=, -= or .= */
1648 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001649 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001650
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 if (eap->skip)
1652 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001653 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 if (eap->skip)
1655 {
1656 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001657 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 --emsg_skip;
1659 }
1660 else if (i != FAIL)
1661 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001662 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001663 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001664 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 }
1666 }
1667}
1668
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001669/*
1670 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1671 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001672 * When "nextchars" is not NULL it points to a string with characters that
1673 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1674 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001675 * Returns OK or FAIL;
1676 */
1677 static int
1678ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1679 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001680 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 int copy; /* copy values from "tv", don't move */
1682 int semicolon; /* from skip_var_list() */
1683 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001684 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001685{
1686 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001687 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001689 listitem_T *item;
1690 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001691
1692 if (*arg != '[')
1693 {
1694 /*
1695 * ":let var = expr" or ":for var in list"
1696 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001697 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001698 return FAIL;
1699 return OK;
1700 }
1701
1702 /*
1703 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1704 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001705 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001706 {
1707 EMSG(_(e_listreq));
1708 return FAIL;
1709 }
1710
1711 i = list_len(l);
1712 if (semicolon == 0 && var_count < i)
1713 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001714 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001715 return FAIL;
1716 }
1717 if (var_count - semicolon > i)
1718 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001719 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001720 return FAIL;
1721 }
1722
1723 item = l->lv_first;
1724 while (*arg != ']')
1725 {
1726 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001727 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001728 item = item->li_next;
1729 if (arg == NULL)
1730 return FAIL;
1731
1732 arg = skipwhite(arg);
1733 if (*arg == ';')
1734 {
1735 /* Put the rest of the list (may be empty) in the var after ';'.
1736 * Create a new list for this. */
1737 l = list_alloc();
1738 if (l == NULL)
1739 return FAIL;
1740 while (item != NULL)
1741 {
1742 list_append_tv(l, &item->li_tv);
1743 item = item->li_next;
1744 }
1745
1746 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001747 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748 ltv.vval.v_list = l;
1749 l->lv_refcount = 1;
1750
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001751 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1752 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001753 clear_tv(&ltv);
1754 if (arg == NULL)
1755 return FAIL;
1756 break;
1757 }
1758 else if (*arg != ',' && *arg != ']')
1759 {
1760 EMSG2(_(e_intern2), "ex_let_vars()");
1761 return FAIL;
1762 }
1763 }
1764
1765 return OK;
1766}
1767
1768/*
1769 * Skip over assignable variable "var" or list of variables "[var, var]".
1770 * Used for ":let varvar = expr" and ":for varvar in expr".
1771 * For "[var, var]" increment "*var_count" for each variable.
1772 * for "[var, var; var]" set "semicolon".
1773 * Return NULL for an error.
1774 */
1775 static char_u *
1776skip_var_list(arg, var_count, semicolon)
1777 char_u *arg;
1778 int *var_count;
1779 int *semicolon;
1780{
1781 char_u *p, *s;
1782
1783 if (*arg == '[')
1784 {
1785 /* "[var, var]": find the matching ']'. */
1786 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001787 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001788 {
1789 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1790 s = skip_var_one(p);
1791 if (s == p)
1792 {
1793 EMSG2(_(e_invarg2), p);
1794 return NULL;
1795 }
1796 ++*var_count;
1797
1798 p = skipwhite(s);
1799 if (*p == ']')
1800 break;
1801 else if (*p == ';')
1802 {
1803 if (*semicolon == 1)
1804 {
1805 EMSG(_("Double ; in list of variables"));
1806 return NULL;
1807 }
1808 *semicolon = 1;
1809 }
1810 else if (*p != ',')
1811 {
1812 EMSG2(_(e_invarg2), p);
1813 return NULL;
1814 }
1815 }
1816 return p + 1;
1817 }
1818 else
1819 return skip_var_one(arg);
1820}
1821
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001822/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001823 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1824 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001825 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001826 static char_u *
1827skip_var_one(arg)
1828 char_u *arg;
1829{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001830 if (*arg == '@' && arg[1] != NUL)
1831 return arg + 2;
1832 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1833 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001834}
1835
Bram Moolenaara7043832005-01-21 11:56:39 +00001836/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001837 * List variables for hashtab "ht" with prefix "prefix".
1838 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001839 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001841list_hashtable_vars(ht, prefix, empty)
1842 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001843 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001844 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001845{
Bram Moolenaar33570922005-01-25 22:26:29 +00001846 hashitem_T *hi;
1847 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001848 int todo;
1849
1850 todo = ht->ht_used;
1851 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1852 {
1853 if (!HASHITEM_EMPTY(hi))
1854 {
1855 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001856 di = HI2DI(hi);
1857 if (empty || di->di_tv.v_type != VAR_STRING
1858 || di->di_tv.vval.v_string != NULL)
1859 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001860 }
1861 }
1862}
1863
1864/*
1865 * List global variables.
1866 */
1867 static void
1868list_glob_vars()
1869{
Bram Moolenaar33570922005-01-25 22:26:29 +00001870 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001871}
1872
1873/*
1874 * List buffer variables.
1875 */
1876 static void
1877list_buf_vars()
1878{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001879 char_u numbuf[NUMBUFLEN];
1880
Bram Moolenaar33570922005-01-25 22:26:29 +00001881 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001882
1883 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1884 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001885}
1886
1887/*
1888 * List window variables.
1889 */
1890 static void
1891list_win_vars()
1892{
Bram Moolenaar33570922005-01-25 22:26:29 +00001893 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001894}
1895
1896/*
1897 * List Vim variables.
1898 */
1899 static void
1900list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901{
Bram Moolenaar33570922005-01-25 22:26:29 +00001902 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903}
1904
1905/*
1906 * List variables in "arg".
1907 */
1908 static char_u *
1909list_arg_vars(eap, arg)
1910 exarg_T *eap;
1911 char_u *arg;
1912{
1913 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001914 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001915 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001916 char_u *name_start;
1917 char_u *arg_subsc;
1918 char_u *tofree;
1919 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001920
1921 while (!ends_excmd(*arg) && !got_int)
1922 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001923 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001925 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001926 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1927 {
1928 emsg_severe = TRUE;
1929 EMSG(_(e_trailing));
1930 break;
1931 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001932 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001933 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001935 /* get_name_len() takes care of expanding curly braces */
1936 name_start = name = arg;
1937 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1938 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001940 /* This is mainly to keep test 49 working: when expanding
1941 * curly braces fails overrule the exception error message. */
1942 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001943 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001944 emsg_severe = TRUE;
1945 EMSG2(_(e_invarg2), arg);
1946 break;
1947 }
1948 error = TRUE;
1949 }
1950 else
1951 {
1952 if (tofree != NULL)
1953 name = tofree;
1954 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956 else
1957 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001958 /* handle d.key, l[idx], f(expr) */
1959 arg_subsc = arg;
1960 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001961 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001963 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001964 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001965 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001966 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001967 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001968 case 'g': list_glob_vars(); break;
1969 case 'b': list_buf_vars(); break;
1970 case 'w': list_win_vars(); break;
1971 case 'v': list_vim_vars(); break;
1972 default:
1973 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001974 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001975 }
1976 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001977 {
1978 char_u numbuf[NUMBUFLEN];
1979 char_u *tf;
1980 int c;
1981 char_u *s;
1982
1983 s = echo_string(&tv, &tf, numbuf);
1984 c = *arg;
1985 *arg = NUL;
1986 list_one_var_a((char_u *)"",
1987 arg == arg_subsc ? name : name_start,
1988 tv.v_type, s == NULL ? (char_u *)"" : s);
1989 *arg = c;
1990 vim_free(tf);
1991 }
1992 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001993 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001994 }
1995 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001996
1997 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001998 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001999
2000 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002001 }
2002
2003 return arg;
2004}
2005
2006/*
2007 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2008 * Returns a pointer to the char just after the var name.
2009 * Returns NULL if there is an error.
2010 */
2011 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002012ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002013 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002014 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002015 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002016 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002017 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002018{
2019 int c1;
2020 char_u *name;
2021 char_u *p;
2022 char_u *arg_end = NULL;
2023 int len;
2024 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002025 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002026
2027 /*
2028 * ":let $VAR = expr": Set environment variable.
2029 */
2030 if (*arg == '$')
2031 {
2032 /* Find the end of the name. */
2033 ++arg;
2034 name = arg;
2035 len = get_env_len(&arg);
2036 if (len == 0)
2037 EMSG2(_(e_invarg2), name - 1);
2038 else
2039 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002040 if (op != NULL && (*op == '+' || *op == '-'))
2041 EMSG2(_(e_letwrong), op);
2042 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002043 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002044 EMSG(_(e_letunexp));
2045 else
2046 {
2047 c1 = name[len];
2048 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002049 p = get_tv_string_chk(tv);
2050 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002051 {
2052 int mustfree = FALSE;
2053 char_u *s = vim_getenv(name, &mustfree);
2054
2055 if (s != NULL)
2056 {
2057 p = tofree = concat_str(s, p);
2058 if (mustfree)
2059 vim_free(s);
2060 }
2061 }
2062 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002063 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002064 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002065 if (STRICMP(name, "HOME") == 0)
2066 init_homedir();
2067 else if (didset_vim && STRICMP(name, "VIM") == 0)
2068 didset_vim = FALSE;
2069 else if (didset_vimruntime
2070 && STRICMP(name, "VIMRUNTIME") == 0)
2071 didset_vimruntime = FALSE;
2072 arg_end = arg;
2073 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002074 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002075 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002076 }
2077 }
2078 }
2079
2080 /*
2081 * ":let &option = expr": Set option value.
2082 * ":let &l:option = expr": Set local option value.
2083 * ":let &g:option = expr": Set global option value.
2084 */
2085 else if (*arg == '&')
2086 {
2087 /* Find the end of the name. */
2088 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002089 if (p == NULL || (endchars != NULL
2090 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002091 EMSG(_(e_letunexp));
2092 else
2093 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002094 long n;
2095 int opt_type;
2096 long numval;
2097 char_u *stringval = NULL;
2098 char_u *s;
2099
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002100 c1 = *p;
2101 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002102
2103 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002104 s = get_tv_string_chk(tv); /* != NULL if number or string */
2105 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002106 {
2107 opt_type = get_option_value(arg, &numval,
2108 &stringval, opt_flags);
2109 if ((opt_type == 1 && *op == '.')
2110 || (opt_type == 0 && *op != '.'))
2111 EMSG2(_(e_letwrong), op);
2112 else
2113 {
2114 if (opt_type == 1) /* number */
2115 {
2116 if (*op == '+')
2117 n = numval + n;
2118 else
2119 n = numval - n;
2120 }
2121 else if (opt_type == 0 && stringval != NULL) /* string */
2122 {
2123 s = concat_str(stringval, s);
2124 vim_free(stringval);
2125 stringval = s;
2126 }
2127 }
2128 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002129 if (s != NULL)
2130 {
2131 set_option_value(arg, n, s, opt_flags);
2132 arg_end = p;
2133 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002134 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002135 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002136 }
2137 }
2138
2139 /*
2140 * ":let @r = expr": Set register contents.
2141 */
2142 else if (*arg == '@')
2143 {
2144 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002145 if (op != NULL && (*op == '+' || *op == '-'))
2146 EMSG2(_(e_letwrong), op);
2147 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002148 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002149 EMSG(_(e_letunexp));
2150 else
2151 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002152 char_u *tofree = NULL;
2153 char_u *s;
2154
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002155 p = get_tv_string_chk(tv);
2156 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002157 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002158 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002159 if (s != NULL)
2160 {
2161 p = tofree = concat_str(s, p);
2162 vim_free(s);
2163 }
2164 }
2165 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002166 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002167 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002168 arg_end = arg + 1;
2169 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002170 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002171 }
2172 }
2173
2174 /*
2175 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002176 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002178 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002179 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002180 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002182 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002183 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002185 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2186 EMSG(_(e_letunexp));
2187 else
2188 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002189 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002190 arg_end = p;
2191 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002193 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002194 }
2195
2196 else
2197 EMSG2(_(e_invarg2), arg);
2198
2199 return arg_end;
2200}
2201
2202/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002203 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2204 */
2205 static int
2206check_changedtick(arg)
2207 char_u *arg;
2208{
2209 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2210 {
2211 EMSG2(_(e_readonlyvar), arg);
2212 return TRUE;
2213 }
2214 return FALSE;
2215}
2216
2217/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002218 * Get an lval: variable, Dict item or List item that can be assigned a value
2219 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2220 * "name.key", "name.key[expr]" etc.
2221 * Indexing only works if "name" is an existing List or Dictionary.
2222 * "name" points to the start of the name.
2223 * If "rettv" is not NULL it points to the value to be assigned.
2224 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2225 * wrong; must end in space or cmd separator.
2226 *
2227 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002228 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002229 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002230 */
2231 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002232get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002233 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002234 typval_T *rettv;
2235 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002236 int unlet;
2237 int skip;
2238 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002239 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002242 char_u *expr_start, *expr_end;
2243 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002244 dictitem_T *v;
2245 typval_T var1;
2246 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002247 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002248 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002249 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002250 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002251 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002253 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002254 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255
2256 if (skip)
2257 {
2258 /* When skipping just find the end of the name. */
2259 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002260 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002261 }
2262
2263 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002264 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002265 if (expr_start != NULL)
2266 {
2267 /* Don't expand the name when we already know there is an error. */
2268 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2269 && *p != '[' && *p != '.')
2270 {
2271 EMSG(_(e_trailing));
2272 return NULL;
2273 }
2274
2275 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2276 if (lp->ll_exp_name == NULL)
2277 {
2278 /* Report an invalid expression in braces, unless the
2279 * expression evaluation has been cancelled due to an
2280 * aborting error, an interrupt, or an exception. */
2281 if (!aborting() && !quiet)
2282 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002283 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002284 EMSG2(_(e_invarg2), name);
2285 return NULL;
2286 }
2287 }
2288 lp->ll_name = lp->ll_exp_name;
2289 }
2290 else
2291 lp->ll_name = name;
2292
2293 /* Without [idx] or .key we are done. */
2294 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2295 return p;
2296
2297 cc = *p;
2298 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002300 if (v == NULL && !quiet)
2301 EMSG2(_(e_undefvar), lp->ll_name);
2302 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 if (v == NULL)
2304 return NULL;
2305
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002306 /*
2307 * Loop until no more [idx] or .key is following.
2308 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002309 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002310 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002312 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2313 && !(lp->ll_tv->v_type == VAR_DICT
2314 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002316 if (!quiet)
2317 EMSG(_("E689: Can only index a List or Dictionary"));
2318 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002320 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002321 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002322 if (!quiet)
2323 EMSG(_("E708: [:] must come last"));
2324 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002325 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002326
Bram Moolenaar8c711452005-01-14 21:53:12 +00002327 len = -1;
2328 if (*p == '.')
2329 {
2330 key = p + 1;
2331 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2332 ;
2333 if (len == 0)
2334 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002335 if (!quiet)
2336 EMSG(_(e_emptykey));
2337 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002338 }
2339 p = key + len;
2340 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002341 else
2342 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002343 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002344 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002345 if (*p == ':')
2346 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002347 else
2348 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002349 empty1 = FALSE;
2350 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002351 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002352 if (get_tv_string_chk(&var1) == NULL)
2353 {
2354 /* not a number or string */
2355 clear_tv(&var1);
2356 return NULL;
2357 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002358 }
2359
2360 /* Optionally get the second index [ :expr]. */
2361 if (*p == ':')
2362 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002363 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002364 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002365 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002366 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002367 if (!empty1)
2368 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002370 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002371 if (rettv != NULL && (rettv->v_type != VAR_LIST
2372 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002373 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002374 if (!quiet)
2375 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002376 if (!empty1)
2377 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002378 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002379 }
2380 p = skipwhite(p + 1);
2381 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002382 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002383 else
2384 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002385 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002386 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2387 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002388 if (!empty1)
2389 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002390 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002391 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002392 if (get_tv_string_chk(&var2) == NULL)
2393 {
2394 /* not a number or string */
2395 if (!empty1)
2396 clear_tv(&var1);
2397 clear_tv(&var2);
2398 return NULL;
2399 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002400 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002401 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002402 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002403 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002404 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002405
Bram Moolenaar8c711452005-01-14 21:53:12 +00002406 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002407 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002408 if (!quiet)
2409 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002410 if (!empty1)
2411 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002412 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002413 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002414 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002415 }
2416
2417 /* Skip to past ']'. */
2418 ++p;
2419 }
2420
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002421 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002422 {
2423 if (len == -1)
2424 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002425 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002426 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002427 if (*key == NUL)
2428 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002429 if (!quiet)
2430 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002431 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002432 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002433 }
2434 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435 lp->ll_list = NULL;
2436 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002437 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002438 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002439 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002440 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002441 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002442 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002443 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002445 if (len == -1)
2446 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002447 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002448 }
2449 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002451 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002452 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002453 if (len == -1)
2454 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002455 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002456 p = NULL;
2457 break;
2458 }
2459 if (len == -1)
2460 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002461 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002462 }
2463 else
2464 {
2465 /*
2466 * Get the number and item for the only or first index of the List.
2467 */
2468 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002470 else
2471 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002472 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002473 clear_tv(&var1);
2474 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 lp->ll_list = lp->ll_tv->vval.v_list;
2477 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2478 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002479 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 if (!quiet)
2481 EMSGN(_(e_listidx), lp->ll_n1);
2482 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002483 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002485 }
2486
2487 /*
2488 * May need to find the item or absolute index for the second
2489 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 * When no index given: "lp->ll_empty2" is TRUE.
2491 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002492 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002493 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002495 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002496 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002497 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002498 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002500 if (ni == NULL)
2501 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 if (!quiet)
2503 EMSGN(_(e_listidx), lp->ll_n2);
2504 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002505 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002506 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002507 }
2508
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2510 if (lp->ll_n1 < 0)
2511 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2512 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002513 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002514 if (!quiet)
2515 EMSGN(_(e_listidx), lp->ll_n2);
2516 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002517 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002518 }
2519
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002521 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 }
2523
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 return p;
2525}
2526
2527/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002528 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 */
2530 static void
2531clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002532 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533{
2534 vim_free(lp->ll_exp_name);
2535 vim_free(lp->ll_newkey);
2536}
2537
2538/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002539 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002541 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 */
2543 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002544set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002545 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002547 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002549 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002550{
2551 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002552 listitem_T *ni;
2553 listitem_T *ri;
2554 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555
2556 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 cc = *endp;
2561 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002562 if (op != NULL && *op != '=')
2563 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002564 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002565
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002566 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002567 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2568 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002569 {
2570 if (tv_op(&tv, rettv, op) == OK)
2571 set_var(lp->ll_name, &tv, FALSE);
2572 clear_tv(&tv);
2573 }
2574 }
2575 else
2576 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002578 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002580 else if (tv_check_lock(lp->ll_newkey == NULL
2581 ? lp->ll_tv->v_lock
2582 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2583 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 else if (lp->ll_range)
2585 {
2586 /*
2587 * Assign the List values to the list items.
2588 */
2589 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002590 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002591 if (op != NULL && *op != '=')
2592 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2593 else
2594 {
2595 clear_tv(&lp->ll_li->li_tv);
2596 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2597 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 ri = ri->li_next;
2599 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2600 break;
2601 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002602 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 /* Need to add an empty item. */
2604 ni = listitem_alloc();
2605 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002606 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 ri = NULL;
2608 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002609 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002611 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 ni->li_tv.vval.v_number = 0;
2613 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002614 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 lp->ll_li = lp->ll_li->li_next;
2616 ++lp->ll_n1;
2617 }
2618 if (ri != NULL)
2619 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002620 else if (lp->ll_empty2
2621 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 : lp->ll_n1 != lp->ll_n2)
2623 EMSG(_("E711: List value has not enough items"));
2624 }
2625 else
2626 {
2627 /*
2628 * Assign to a List or Dictionary item.
2629 */
2630 if (lp->ll_newkey != NULL)
2631 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002632 if (op != NULL && *op != '=')
2633 {
2634 EMSG2(_(e_letwrong), op);
2635 return;
2636 }
2637
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002638 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002639 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002640 if (di == NULL)
2641 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002642 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2643 {
2644 vim_free(di);
2645 return;
2646 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002648 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002649 else if (op != NULL && *op != '=')
2650 {
2651 tv_op(lp->ll_tv, rettv, op);
2652 return;
2653 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002654 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 /*
2658 * Assign the value to the variable or list item.
2659 */
2660 if (copy)
2661 copy_tv(rettv, lp->ll_tv);
2662 else
2663 {
2664 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002665 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667 }
2668 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002669}
2670
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002671/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002672 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2673 * Returns OK or FAIL.
2674 */
2675 static int
2676tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002677 typval_T *tv1;
2678 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002679 char_u *op;
2680{
2681 long n;
2682 char_u numbuf[NUMBUFLEN];
2683 char_u *s;
2684
2685 /* Can't do anything with a Funcref or a Dict on the right. */
2686 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2687 {
2688 switch (tv1->v_type)
2689 {
2690 case VAR_DICT:
2691 case VAR_FUNC:
2692 break;
2693
2694 case VAR_LIST:
2695 if (*op != '+' || tv2->v_type != VAR_LIST)
2696 break;
2697 /* List += List */
2698 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2699 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2700 return OK;
2701
2702 case VAR_NUMBER:
2703 case VAR_STRING:
2704 if (tv2->v_type == VAR_LIST)
2705 break;
2706 if (*op == '+' || *op == '-')
2707 {
2708 /* nr += nr or nr -= nr*/
2709 n = get_tv_number(tv1);
2710 if (*op == '+')
2711 n += get_tv_number(tv2);
2712 else
2713 n -= get_tv_number(tv2);
2714 clear_tv(tv1);
2715 tv1->v_type = VAR_NUMBER;
2716 tv1->vval.v_number = n;
2717 }
2718 else
2719 {
2720 /* str .= str */
2721 s = get_tv_string(tv1);
2722 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2723 clear_tv(tv1);
2724 tv1->v_type = VAR_STRING;
2725 tv1->vval.v_string = s;
2726 }
2727 return OK;
2728 }
2729 }
2730
2731 EMSG2(_(e_letwrong), op);
2732 return FAIL;
2733}
2734
2735/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002736 * Add a watcher to a list.
2737 */
2738 static void
2739list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002740 list_T *l;
2741 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002742{
2743 lw->lw_next = l->lv_watch;
2744 l->lv_watch = lw;
2745}
2746
2747/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002748 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002749 * No warning when it isn't found...
2750 */
2751 static void
2752list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002753 list_T *l;
2754 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002755{
Bram Moolenaar33570922005-01-25 22:26:29 +00002756 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002757
2758 lwp = &l->lv_watch;
2759 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2760 {
2761 if (lw == lwrem)
2762 {
2763 *lwp = lw->lw_next;
2764 break;
2765 }
2766 lwp = &lw->lw_next;
2767 }
2768}
2769
2770/*
2771 * Just before removing an item from a list: advance watchers to the next
2772 * item.
2773 */
2774 static void
2775list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002776 list_T *l;
2777 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002778{
Bram Moolenaar33570922005-01-25 22:26:29 +00002779 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002780
2781 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2782 if (lw->lw_item == item)
2783 lw->lw_item = item->li_next;
2784}
2785
2786/*
2787 * Evaluate the expression used in a ":for var in expr" command.
2788 * "arg" points to "var".
2789 * Set "*errp" to TRUE for an error, FALSE otherwise;
2790 * Return a pointer that holds the info. Null when there is an error.
2791 */
2792 void *
2793eval_for_line(arg, errp, nextcmdp, skip)
2794 char_u *arg;
2795 int *errp;
2796 char_u **nextcmdp;
2797 int skip;
2798{
Bram Moolenaar33570922005-01-25 22:26:29 +00002799 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002800 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002801 typval_T tv;
2802 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002803
2804 *errp = TRUE; /* default: there is an error */
2805
Bram Moolenaar33570922005-01-25 22:26:29 +00002806 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002807 if (fi == NULL)
2808 return NULL;
2809
2810 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2811 if (expr == NULL)
2812 return fi;
2813
2814 expr = skipwhite(expr);
2815 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2816 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002817 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002818 return fi;
2819 }
2820
2821 if (skip)
2822 ++emsg_skip;
2823 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2824 {
2825 *errp = FALSE;
2826 if (!skip)
2827 {
2828 l = tv.vval.v_list;
2829 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002830 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002831 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002832 clear_tv(&tv);
2833 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002834 else
2835 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00002836 /* No need to increment the refcount, it's already set for the
2837 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002838 fi->fi_list = l;
2839 list_add_watch(l, &fi->fi_lw);
2840 fi->fi_lw.lw_item = l->lv_first;
2841 }
2842 }
2843 }
2844 if (skip)
2845 --emsg_skip;
2846
2847 return fi;
2848}
2849
2850/*
2851 * Use the first item in a ":for" list. Advance to the next.
2852 * Assign the values to the variable (list). "arg" points to the first one.
2853 * Return TRUE when a valid item was found, FALSE when at end of list or
2854 * something wrong.
2855 */
2856 int
2857next_for_item(fi_void, arg)
2858 void *fi_void;
2859 char_u *arg;
2860{
Bram Moolenaar33570922005-01-25 22:26:29 +00002861 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002862 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002863 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002864
2865 item = fi->fi_lw.lw_item;
2866 if (item == NULL)
2867 result = FALSE;
2868 else
2869 {
2870 fi->fi_lw.lw_item = item->li_next;
2871 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2872 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2873 }
2874 return result;
2875}
2876
2877/*
2878 * Free the structure used to store info used by ":for".
2879 */
2880 void
2881free_for_info(fi_void)
2882 void *fi_void;
2883{
Bram Moolenaar33570922005-01-25 22:26:29 +00002884 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002885
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002886 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002887 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002888 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002889 list_unref(fi->fi_list);
2890 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002891 vim_free(fi);
2892}
2893
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2895
2896 void
2897set_context_for_expression(xp, arg, cmdidx)
2898 expand_T *xp;
2899 char_u *arg;
2900 cmdidx_T cmdidx;
2901{
2902 int got_eq = FALSE;
2903 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002904 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002906 if (cmdidx == CMD_let)
2907 {
2908 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002909 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002910 {
2911 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002912 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002913 {
2914 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002915 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002916 if (vim_iswhite(*p))
2917 break;
2918 }
2919 return;
2920 }
2921 }
2922 else
2923 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2924 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 while ((xp->xp_pattern = vim_strpbrk(arg,
2926 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2927 {
2928 c = *xp->xp_pattern;
2929 if (c == '&')
2930 {
2931 c = xp->xp_pattern[1];
2932 if (c == '&')
2933 {
2934 ++xp->xp_pattern;
2935 xp->xp_context = cmdidx != CMD_let || got_eq
2936 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2937 }
2938 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002939 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002941 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2942 xp->xp_pattern += 2;
2943
2944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 }
2946 else if (c == '$')
2947 {
2948 /* environment variable */
2949 xp->xp_context = EXPAND_ENV_VARS;
2950 }
2951 else if (c == '=')
2952 {
2953 got_eq = TRUE;
2954 xp->xp_context = EXPAND_EXPRESSION;
2955 }
2956 else if (c == '<'
2957 && xp->xp_context == EXPAND_FUNCTIONS
2958 && vim_strchr(xp->xp_pattern, '(') == NULL)
2959 {
2960 /* Function name can start with "<SNR>" */
2961 break;
2962 }
2963 else if (cmdidx != CMD_let || got_eq)
2964 {
2965 if (c == '"') /* string */
2966 {
2967 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2968 if (c == '\\' && xp->xp_pattern[1] != NUL)
2969 ++xp->xp_pattern;
2970 xp->xp_context = EXPAND_NOTHING;
2971 }
2972 else if (c == '\'') /* literal string */
2973 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002974 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2976 /* skip */ ;
2977 xp->xp_context = EXPAND_NOTHING;
2978 }
2979 else if (c == '|')
2980 {
2981 if (xp->xp_pattern[1] == '|')
2982 {
2983 ++xp->xp_pattern;
2984 xp->xp_context = EXPAND_EXPRESSION;
2985 }
2986 else
2987 xp->xp_context = EXPAND_COMMANDS;
2988 }
2989 else
2990 xp->xp_context = EXPAND_EXPRESSION;
2991 }
2992 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002993 /* Doesn't look like something valid, expand as an expression
2994 * anyway. */
2995 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 arg = xp->xp_pattern;
2997 if (*arg != NUL)
2998 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2999 /* skip */ ;
3000 }
3001 xp->xp_pattern = arg;
3002}
3003
3004#endif /* FEAT_CMDL_COMPL */
3005
3006/*
3007 * ":1,25call func(arg1, arg2)" function call.
3008 */
3009 void
3010ex_call(eap)
3011 exarg_T *eap;
3012{
3013 char_u *arg = eap->arg;
3014 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003016 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003018 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 linenr_T lnum;
3020 int doesrange;
3021 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003022 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003024 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
3025 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003026 if (tofree == NULL)
3027 return;
3028
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003029 /* Increase refcount on dictionary, it could get deleted when evaluating
3030 * the arguments. */
3031 if (fudi.fd_dict != NULL)
3032 ++fudi.fd_dict->dv_refcount;
3033
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003034 /* If it is the name of a variable of type VAR_FUNC use its contents. */
3035 len = STRLEN(tofree);
3036 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037
Bram Moolenaar532c7802005-01-27 14:44:31 +00003038 /* Skip white space to allow ":call func ()". Not good, but required for
3039 * backward compatibility. */
3040 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003041 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042
3043 if (*startarg != '(')
3044 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00003045 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 goto end;
3047 }
3048
3049 /*
3050 * When skipping, evaluate the function once, to find the end of the
3051 * arguments.
3052 * When the function takes a range, this is discovered after the first
3053 * call, and the loop is broken.
3054 */
3055 if (eap->skip)
3056 {
3057 ++emsg_skip;
3058 lnum = eap->line2; /* do it once, also with an invalid range */
3059 }
3060 else
3061 lnum = eap->line1;
3062 for ( ; lnum <= eap->line2; ++lnum)
3063 {
3064 if (!eap->skip && eap->addr_count > 0)
3065 {
3066 curwin->w_cursor.lnum = lnum;
3067 curwin->w_cursor.col = 0;
3068 }
3069 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003070 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003071 eap->line1, eap->line2, &doesrange,
3072 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 {
3074 failed = TRUE;
3075 break;
3076 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003077 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 if (doesrange || eap->skip)
3079 break;
3080 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003081 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003082 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003083 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 if (aborting())
3085 break;
3086 }
3087 if (eap->skip)
3088 --emsg_skip;
3089
3090 if (!failed)
3091 {
3092 /* Check for trailing illegal characters and a following command. */
3093 if (!ends_excmd(*arg))
3094 {
3095 emsg_severe = TRUE;
3096 EMSG(_(e_trailing));
3097 }
3098 else
3099 eap->nextcmd = check_nextcmd(arg);
3100 }
3101
3102end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003103 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003104 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105}
3106
3107/*
3108 * ":unlet[!] var1 ... " command.
3109 */
3110 void
3111ex_unlet(eap)
3112 exarg_T *eap;
3113{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003114 ex_unletlock(eap, eap->arg, 0);
3115}
3116
3117/*
3118 * ":lockvar" and ":unlockvar" commands
3119 */
3120 void
3121ex_lockvar(eap)
3122 exarg_T *eap;
3123{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003125 int deep = 2;
3126
3127 if (eap->forceit)
3128 deep = -1;
3129 else if (vim_isdigit(*arg))
3130 {
3131 deep = getdigits(&arg);
3132 arg = skipwhite(arg);
3133 }
3134
3135 ex_unletlock(eap, arg, deep);
3136}
3137
3138/*
3139 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3140 */
3141 static void
3142ex_unletlock(eap, argstart, deep)
3143 exarg_T *eap;
3144 char_u *argstart;
3145 int deep;
3146{
3147 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003150 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
3152 do
3153 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003154 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003155 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3156 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003157 if (lv.ll_name == NULL)
3158 error = TRUE; /* error but continue parsing */
3159 if (name_end == NULL || (!vim_iswhite(*name_end)
3160 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003162 if (name_end != NULL)
3163 {
3164 emsg_severe = TRUE;
3165 EMSG(_(e_trailing));
3166 }
3167 if (!(eap->skip || error))
3168 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 break;
3170 }
3171
3172 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003173 {
3174 if (eap->cmdidx == CMD_unlet)
3175 {
3176 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3177 error = TRUE;
3178 }
3179 else
3180 {
3181 if (do_lock_var(&lv, name_end, deep,
3182 eap->cmdidx == CMD_lockvar) == FAIL)
3183 error = TRUE;
3184 }
3185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003187 if (!eap->skip)
3188 clear_lval(&lv);
3189
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 arg = skipwhite(name_end);
3191 } while (!ends_excmd(*arg));
3192
3193 eap->nextcmd = check_nextcmd(arg);
3194}
3195
Bram Moolenaar8c711452005-01-14 21:53:12 +00003196 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003197do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003198 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003199 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003200 int forceit;
3201{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003202 int ret = OK;
3203 int cc;
3204
3205 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003206 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003207 cc = *name_end;
3208 *name_end = NUL;
3209
3210 /* Normal name or expanded name. */
3211 if (check_changedtick(lp->ll_name))
3212 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003213 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003214 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003215 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003216 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003217 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3218 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003219 else if (lp->ll_range)
3220 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003221 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003222
3223 /* Delete a range of List items. */
3224 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3225 {
3226 li = lp->ll_li->li_next;
3227 listitem_remove(lp->ll_list, lp->ll_li);
3228 lp->ll_li = li;
3229 ++lp->ll_n1;
3230 }
3231 }
3232 else
3233 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003234 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003235 /* unlet a List item. */
3236 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003237 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003238 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003239 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003240 }
3241
3242 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003243}
3244
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245/*
3246 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003247 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 */
3249 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003250do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003252 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253{
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 hashtab_T *ht;
3255 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003256 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257
Bram Moolenaar33570922005-01-25 22:26:29 +00003258 ht = find_var_ht(name, &varname);
3259 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003261 hi = hash_find(ht, varname);
3262 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003263 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003264 if (var_check_ro(HI2DI(hi)->di_flags, name))
3265 return FAIL;
3266 delete_var(ht, hi);
3267 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003270 if (forceit)
3271 return OK;
3272 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 return FAIL;
3274}
3275
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003276/*
3277 * Lock or unlock variable indicated by "lp".
3278 * "deep" is the levels to go (-1 for unlimited);
3279 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3280 */
3281 static int
3282do_lock_var(lp, name_end, deep, lock)
3283 lval_T *lp;
3284 char_u *name_end;
3285 int deep;
3286 int lock;
3287{
3288 int ret = OK;
3289 int cc;
3290 dictitem_T *di;
3291
3292 if (deep == 0) /* nothing to do */
3293 return OK;
3294
3295 if (lp->ll_tv == NULL)
3296 {
3297 cc = *name_end;
3298 *name_end = NUL;
3299
3300 /* Normal name or expanded name. */
3301 if (check_changedtick(lp->ll_name))
3302 ret = FAIL;
3303 else
3304 {
3305 di = find_var(lp->ll_name, NULL);
3306 if (di == NULL)
3307 ret = FAIL;
3308 else
3309 {
3310 if (lock)
3311 di->di_flags |= DI_FLAGS_LOCK;
3312 else
3313 di->di_flags &= ~DI_FLAGS_LOCK;
3314 item_lock(&di->di_tv, deep, lock);
3315 }
3316 }
3317 *name_end = cc;
3318 }
3319 else if (lp->ll_range)
3320 {
3321 listitem_T *li = lp->ll_li;
3322
3323 /* (un)lock a range of List items. */
3324 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3325 {
3326 item_lock(&li->li_tv, deep, lock);
3327 li = li->li_next;
3328 ++lp->ll_n1;
3329 }
3330 }
3331 else if (lp->ll_list != NULL)
3332 /* (un)lock a List item. */
3333 item_lock(&lp->ll_li->li_tv, deep, lock);
3334 else
3335 /* un(lock) a Dictionary item. */
3336 item_lock(&lp->ll_di->di_tv, deep, lock);
3337
3338 return ret;
3339}
3340
3341/*
3342 * Lock or unlock an item. "deep" is nr of levels to go.
3343 */
3344 static void
3345item_lock(tv, deep, lock)
3346 typval_T *tv;
3347 int deep;
3348 int lock;
3349{
3350 static int recurse = 0;
3351 list_T *l;
3352 listitem_T *li;
3353 dict_T *d;
3354 hashitem_T *hi;
3355 int todo;
3356
3357 if (recurse >= DICT_MAXNEST)
3358 {
3359 EMSG(_("E743: variable nested too deep for (un)lock"));
3360 return;
3361 }
3362 if (deep == 0)
3363 return;
3364 ++recurse;
3365
3366 /* lock/unlock the item itself */
3367 if (lock)
3368 tv->v_lock |= VAR_LOCKED;
3369 else
3370 tv->v_lock &= ~VAR_LOCKED;
3371
3372 switch (tv->v_type)
3373 {
3374 case VAR_LIST:
3375 if ((l = tv->vval.v_list) != NULL)
3376 {
3377 if (lock)
3378 l->lv_lock |= VAR_LOCKED;
3379 else
3380 l->lv_lock &= ~VAR_LOCKED;
3381 if (deep < 0 || deep > 1)
3382 /* recursive: lock/unlock the items the List contains */
3383 for (li = l->lv_first; li != NULL; li = li->li_next)
3384 item_lock(&li->li_tv, deep - 1, lock);
3385 }
3386 break;
3387 case VAR_DICT:
3388 if ((d = tv->vval.v_dict) != NULL)
3389 {
3390 if (lock)
3391 d->dv_lock |= VAR_LOCKED;
3392 else
3393 d->dv_lock &= ~VAR_LOCKED;
3394 if (deep < 0 || deep > 1)
3395 {
3396 /* recursive: lock/unlock the items the List contains */
3397 todo = d->dv_hashtab.ht_used;
3398 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3399 {
3400 if (!HASHITEM_EMPTY(hi))
3401 {
3402 --todo;
3403 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3404 }
3405 }
3406 }
3407 }
3408 }
3409 --recurse;
3410}
3411
Bram Moolenaara40058a2005-07-11 22:42:07 +00003412/*
3413 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3414 * it refers to a List or Dictionary that is locked.
3415 */
3416 static int
3417tv_islocked(tv)
3418 typval_T *tv;
3419{
3420 return (tv->v_lock & VAR_LOCKED)
3421 || (tv->v_type == VAR_LIST
3422 && tv->vval.v_list != NULL
3423 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3424 || (tv->v_type == VAR_DICT
3425 && tv->vval.v_dict != NULL
3426 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3427}
3428
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3430/*
3431 * Delete all "menutrans_" variables.
3432 */
3433 void
3434del_menutrans_vars()
3435{
Bram Moolenaar33570922005-01-25 22:26:29 +00003436 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003437 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
Bram Moolenaar33570922005-01-25 22:26:29 +00003439 hash_lock(&globvarht);
3440 todo = globvarht.ht_used;
3441 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003442 {
3443 if (!HASHITEM_EMPTY(hi))
3444 {
3445 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003446 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3447 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003448 }
3449 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003450 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451}
3452#endif
3453
3454#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3455
3456/*
3457 * Local string buffer for the next two functions to store a variable name
3458 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3459 * get_user_var_name().
3460 */
3461
3462static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3463
3464static char_u *varnamebuf = NULL;
3465static int varnamebuflen = 0;
3466
3467/*
3468 * Function to concatenate a prefix and a variable name.
3469 */
3470 static char_u *
3471cat_prefix_varname(prefix, name)
3472 int prefix;
3473 char_u *name;
3474{
3475 int len;
3476
3477 len = (int)STRLEN(name) + 3;
3478 if (len > varnamebuflen)
3479 {
3480 vim_free(varnamebuf);
3481 len += 10; /* some additional space */
3482 varnamebuf = alloc(len);
3483 if (varnamebuf == NULL)
3484 {
3485 varnamebuflen = 0;
3486 return NULL;
3487 }
3488 varnamebuflen = len;
3489 }
3490 *varnamebuf = prefix;
3491 varnamebuf[1] = ':';
3492 STRCPY(varnamebuf + 2, name);
3493 return varnamebuf;
3494}
3495
3496/*
3497 * Function given to ExpandGeneric() to obtain the list of user defined
3498 * (global/buffer/window/built-in) variable names.
3499 */
3500/*ARGSUSED*/
3501 char_u *
3502get_user_var_name(xp, idx)
3503 expand_T *xp;
3504 int idx;
3505{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003506 static long_u gdone;
3507 static long_u bdone;
3508 static long_u wdone;
3509 static int vidx;
3510 static hashitem_T *hi;
3511 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
3513 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003514 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003515
3516 /* Global variables */
3517 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003519 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003520 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003521 else
3522 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003523 while (HASHITEM_EMPTY(hi))
3524 ++hi;
3525 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3526 return cat_prefix_varname('g', hi->hi_key);
3527 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003529
3530 /* b: variables */
3531 ht = &curbuf->b_vars.dv_hashtab;
3532 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003534 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003535 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003536 else
3537 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003538 while (HASHITEM_EMPTY(hi))
3539 ++hi;
3540 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003542 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003544 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 return (char_u *)"b:changedtick";
3546 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003547
3548 /* w: variables */
3549 ht = &curwin->w_vars.dv_hashtab;
3550 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003552 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003553 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003554 else
3555 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003556 while (HASHITEM_EMPTY(hi))
3557 ++hi;
3558 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003560
3561 /* v: variables */
3562 if (vidx < VV_LEN)
3563 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
3565 vim_free(varnamebuf);
3566 varnamebuf = NULL;
3567 varnamebuflen = 0;
3568 return NULL;
3569}
3570
3571#endif /* FEAT_CMDL_COMPL */
3572
3573/*
3574 * types for expressions.
3575 */
3576typedef enum
3577{
3578 TYPE_UNKNOWN = 0
3579 , TYPE_EQUAL /* == */
3580 , TYPE_NEQUAL /* != */
3581 , TYPE_GREATER /* > */
3582 , TYPE_GEQUAL /* >= */
3583 , TYPE_SMALLER /* < */
3584 , TYPE_SEQUAL /* <= */
3585 , TYPE_MATCH /* =~ */
3586 , TYPE_NOMATCH /* !~ */
3587} exptype_T;
3588
3589/*
3590 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003591 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3593 */
3594
3595/*
3596 * Handle zero level expression.
3597 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003598 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 * Return OK or FAIL.
3600 */
3601 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003602eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 char_u **nextcmd;
3606 int evaluate;
3607{
3608 int ret;
3609 char_u *p;
3610
3611 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003612 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 if (ret == FAIL || !ends_excmd(*p))
3614 {
3615 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003616 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 /*
3618 * Report the invalid expression unless the expression evaluation has
3619 * been cancelled due to an aborting error, an interrupt, or an
3620 * exception.
3621 */
3622 if (!aborting())
3623 EMSG2(_(e_invexpr2), arg);
3624 ret = FAIL;
3625 }
3626 if (nextcmd != NULL)
3627 *nextcmd = check_nextcmd(p);
3628
3629 return ret;
3630}
3631
3632/*
3633 * Handle top level expression:
3634 * expr1 ? expr0 : expr0
3635 *
3636 * "arg" must point to the first non-white of the expression.
3637 * "arg" is advanced to the next non-white after the recognized expression.
3638 *
3639 * Return OK or FAIL.
3640 */
3641 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003642eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003644 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 int evaluate;
3646{
3647 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003648 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649
3650 /*
3651 * Get the first variable.
3652 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003653 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 return FAIL;
3655
3656 if ((*arg)[0] == '?')
3657 {
3658 result = FALSE;
3659 if (evaluate)
3660 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003661 int error = FALSE;
3662
3663 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003665 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003666 if (error)
3667 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 }
3669
3670 /*
3671 * Get the second variable.
3672 */
3673 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003674 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 return FAIL;
3676
3677 /*
3678 * Check for the ":".
3679 */
3680 if ((*arg)[0] != ':')
3681 {
3682 EMSG(_("E109: Missing ':' after '?'"));
3683 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003684 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 return FAIL;
3686 }
3687
3688 /*
3689 * Get the third variable.
3690 */
3691 *arg = skipwhite(*arg + 1);
3692 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3693 {
3694 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003695 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 return FAIL;
3697 }
3698 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003699 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 }
3701
3702 return OK;
3703}
3704
3705/*
3706 * Handle first level expression:
3707 * expr2 || expr2 || expr2 logical OR
3708 *
3709 * "arg" must point to the first non-white of the expression.
3710 * "arg" is advanced to the next non-white after the recognized expression.
3711 *
3712 * Return OK or FAIL.
3713 */
3714 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003715eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003717 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 int evaluate;
3719{
Bram Moolenaar33570922005-01-25 22:26:29 +00003720 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 long result;
3722 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003723 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724
3725 /*
3726 * Get the first variable.
3727 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003728 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 return FAIL;
3730
3731 /*
3732 * Repeat until there is no following "||".
3733 */
3734 first = TRUE;
3735 result = FALSE;
3736 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3737 {
3738 if (evaluate && first)
3739 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003740 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003742 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003743 if (error)
3744 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 first = FALSE;
3746 }
3747
3748 /*
3749 * Get the second variable.
3750 */
3751 *arg = skipwhite(*arg + 2);
3752 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3753 return FAIL;
3754
3755 /*
3756 * Compute the result.
3757 */
3758 if (evaluate && !result)
3759 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003760 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003762 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003763 if (error)
3764 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
3766 if (evaluate)
3767 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003768 rettv->v_type = VAR_NUMBER;
3769 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 }
3771 }
3772
3773 return OK;
3774}
3775
3776/*
3777 * Handle second level expression:
3778 * expr3 && expr3 && expr3 logical AND
3779 *
3780 * "arg" must point to the first non-white of the expression.
3781 * "arg" is advanced to the next non-white after the recognized expression.
3782 *
3783 * Return OK or FAIL.
3784 */
3785 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003786eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 int evaluate;
3790{
Bram Moolenaar33570922005-01-25 22:26:29 +00003791 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 long result;
3793 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003794 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795
3796 /*
3797 * Get the first variable.
3798 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003799 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 return FAIL;
3801
3802 /*
3803 * Repeat until there is no following "&&".
3804 */
3805 first = TRUE;
3806 result = TRUE;
3807 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3808 {
3809 if (evaluate && first)
3810 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003811 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003813 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003814 if (error)
3815 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 first = FALSE;
3817 }
3818
3819 /*
3820 * Get the second variable.
3821 */
3822 *arg = skipwhite(*arg + 2);
3823 if (eval4(arg, &var2, evaluate && result) == FAIL)
3824 return FAIL;
3825
3826 /*
3827 * Compute the result.
3828 */
3829 if (evaluate && result)
3830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003831 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003833 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003834 if (error)
3835 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 }
3837 if (evaluate)
3838 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003839 rettv->v_type = VAR_NUMBER;
3840 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 }
3842 }
3843
3844 return OK;
3845}
3846
3847/*
3848 * Handle third level expression:
3849 * var1 == var2
3850 * var1 =~ var2
3851 * var1 != var2
3852 * var1 !~ var2
3853 * var1 > var2
3854 * var1 >= var2
3855 * var1 < var2
3856 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003857 * var1 is var2
3858 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 *
3860 * "arg" must point to the first non-white of the expression.
3861 * "arg" is advanced to the next non-white after the recognized expression.
3862 *
3863 * Return OK or FAIL.
3864 */
3865 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003866eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 int evaluate;
3870{
Bram Moolenaar33570922005-01-25 22:26:29 +00003871 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 char_u *p;
3873 int i;
3874 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003875 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 int len = 2;
3877 long n1, n2;
3878 char_u *s1, *s2;
3879 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3880 regmatch_T regmatch;
3881 int ic;
3882 char_u *save_cpo;
3883
3884 /*
3885 * Get the first variable.
3886 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003887 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 return FAIL;
3889
3890 p = *arg;
3891 switch (p[0])
3892 {
3893 case '=': if (p[1] == '=')
3894 type = TYPE_EQUAL;
3895 else if (p[1] == '~')
3896 type = TYPE_MATCH;
3897 break;
3898 case '!': if (p[1] == '=')
3899 type = TYPE_NEQUAL;
3900 else if (p[1] == '~')
3901 type = TYPE_NOMATCH;
3902 break;
3903 case '>': if (p[1] != '=')
3904 {
3905 type = TYPE_GREATER;
3906 len = 1;
3907 }
3908 else
3909 type = TYPE_GEQUAL;
3910 break;
3911 case '<': if (p[1] != '=')
3912 {
3913 type = TYPE_SMALLER;
3914 len = 1;
3915 }
3916 else
3917 type = TYPE_SEQUAL;
3918 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003919 case 'i': if (p[1] == 's')
3920 {
3921 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3922 len = 5;
3923 if (!vim_isIDc(p[len]))
3924 {
3925 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3926 type_is = TRUE;
3927 }
3928 }
3929 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
3931
3932 /*
3933 * If there is a comparitive operator, use it.
3934 */
3935 if (type != TYPE_UNKNOWN)
3936 {
3937 /* extra question mark appended: ignore case */
3938 if (p[len] == '?')
3939 {
3940 ic = TRUE;
3941 ++len;
3942 }
3943 /* extra '#' appended: match case */
3944 else if (p[len] == '#')
3945 {
3946 ic = FALSE;
3947 ++len;
3948 }
3949 /* nothing appened: use 'ignorecase' */
3950 else
3951 ic = p_ic;
3952
3953 /*
3954 * Get the second variable.
3955 */
3956 *arg = skipwhite(p + len);
3957 if (eval5(arg, &var2, evaluate) == FAIL)
3958 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003959 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 return FAIL;
3961 }
3962
3963 if (evaluate)
3964 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003965 if (type_is && rettv->v_type != var2.v_type)
3966 {
3967 /* For "is" a different type always means FALSE, for "notis"
3968 * it means TRUE. */
3969 n1 = (type == TYPE_NEQUAL);
3970 }
3971 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3972 {
3973 if (type_is)
3974 {
3975 n1 = (rettv->v_type == var2.v_type
3976 && rettv->vval.v_list == var2.vval.v_list);
3977 if (type == TYPE_NEQUAL)
3978 n1 = !n1;
3979 }
3980 else if (rettv->v_type != var2.v_type
3981 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3982 {
3983 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003984 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003985 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003986 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003987 clear_tv(rettv);
3988 clear_tv(&var2);
3989 return FAIL;
3990 }
3991 else
3992 {
3993 /* Compare two Lists for being equal or unequal. */
3994 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3995 if (type == TYPE_NEQUAL)
3996 n1 = !n1;
3997 }
3998 }
3999
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004000 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4001 {
4002 if (type_is)
4003 {
4004 n1 = (rettv->v_type == var2.v_type
4005 && rettv->vval.v_dict == var2.vval.v_dict);
4006 if (type == TYPE_NEQUAL)
4007 n1 = !n1;
4008 }
4009 else if (rettv->v_type != var2.v_type
4010 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4011 {
4012 if (rettv->v_type != var2.v_type)
4013 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4014 else
4015 EMSG(_("E736: Invalid operation for Dictionary"));
4016 clear_tv(rettv);
4017 clear_tv(&var2);
4018 return FAIL;
4019 }
4020 else
4021 {
4022 /* Compare two Dictionaries for being equal or unequal. */
4023 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
4024 if (type == TYPE_NEQUAL)
4025 n1 = !n1;
4026 }
4027 }
4028
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004029 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4030 {
4031 if (rettv->v_type != var2.v_type
4032 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4033 {
4034 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004035 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004036 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004037 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004038 clear_tv(rettv);
4039 clear_tv(&var2);
4040 return FAIL;
4041 }
4042 else
4043 {
4044 /* Compare two Funcrefs for being equal or unequal. */
4045 if (rettv->vval.v_string == NULL
4046 || var2.vval.v_string == NULL)
4047 n1 = FALSE;
4048 else
4049 n1 = STRCMP(rettv->vval.v_string,
4050 var2.vval.v_string) == 0;
4051 if (type == TYPE_NEQUAL)
4052 n1 = !n1;
4053 }
4054 }
4055
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 /*
4057 * If one of the two variables is a number, compare as a number.
4058 * When using "=~" or "!~", always compare as string.
4059 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004060 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4062 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004063 n1 = get_tv_number(rettv);
4064 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 switch (type)
4066 {
4067 case TYPE_EQUAL: n1 = (n1 == n2); break;
4068 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4069 case TYPE_GREATER: n1 = (n1 > n2); break;
4070 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4071 case TYPE_SMALLER: n1 = (n1 < n2); break;
4072 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4073 case TYPE_UNKNOWN:
4074 case TYPE_MATCH:
4075 case TYPE_NOMATCH: break; /* avoid gcc warning */
4076 }
4077 }
4078 else
4079 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004080 s1 = get_tv_string_buf(rettv, buf1);
4081 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4083 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4084 else
4085 i = 0;
4086 n1 = FALSE;
4087 switch (type)
4088 {
4089 case TYPE_EQUAL: n1 = (i == 0); break;
4090 case TYPE_NEQUAL: n1 = (i != 0); break;
4091 case TYPE_GREATER: n1 = (i > 0); break;
4092 case TYPE_GEQUAL: n1 = (i >= 0); break;
4093 case TYPE_SMALLER: n1 = (i < 0); break;
4094 case TYPE_SEQUAL: n1 = (i <= 0); break;
4095
4096 case TYPE_MATCH:
4097 case TYPE_NOMATCH:
4098 /* avoid 'l' flag in 'cpoptions' */
4099 save_cpo = p_cpo;
4100 p_cpo = (char_u *)"";
4101 regmatch.regprog = vim_regcomp(s2,
4102 RE_MAGIC + RE_STRING);
4103 regmatch.rm_ic = ic;
4104 if (regmatch.regprog != NULL)
4105 {
4106 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4107 vim_free(regmatch.regprog);
4108 if (type == TYPE_NOMATCH)
4109 n1 = !n1;
4110 }
4111 p_cpo = save_cpo;
4112 break;
4113
4114 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4115 }
4116 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004117 clear_tv(rettv);
4118 clear_tv(&var2);
4119 rettv->v_type = VAR_NUMBER;
4120 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
4122 }
4123
4124 return OK;
4125}
4126
4127/*
4128 * Handle fourth level expression:
4129 * + number addition
4130 * - number subtraction
4131 * . string concatenation
4132 *
4133 * "arg" must point to the first non-white of the expression.
4134 * "arg" is advanced to the next non-white after the recognized expression.
4135 *
4136 * Return OK or FAIL.
4137 */
4138 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004139eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004141 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 int evaluate;
4143{
Bram Moolenaar33570922005-01-25 22:26:29 +00004144 typval_T var2;
4145 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 int op;
4147 long n1, n2;
4148 char_u *s1, *s2;
4149 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4150 char_u *p;
4151
4152 /*
4153 * Get the first variable.
4154 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004155 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 return FAIL;
4157
4158 /*
4159 * Repeat computing, until no '+', '-' or '.' is following.
4160 */
4161 for (;;)
4162 {
4163 op = **arg;
4164 if (op != '+' && op != '-' && op != '.')
4165 break;
4166
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004167 if (op != '+' || rettv->v_type != VAR_LIST)
4168 {
4169 /* For "list + ...", an illegal use of the first operand as
4170 * a number cannot be determined before evaluating the 2nd
4171 * operand: if this is also a list, all is ok.
4172 * For "something . ...", "something - ..." or "non-list + ...",
4173 * we know that the first operand needs to be a string or number
4174 * without evaluating the 2nd operand. So check before to avoid
4175 * side effects after an error. */
4176 if (evaluate && get_tv_string_chk(rettv) == NULL)
4177 {
4178 clear_tv(rettv);
4179 return FAIL;
4180 }
4181 }
4182
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 /*
4184 * Get the second variable.
4185 */
4186 *arg = skipwhite(*arg + 1);
4187 if (eval6(arg, &var2, evaluate) == FAIL)
4188 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004189 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 return FAIL;
4191 }
4192
4193 if (evaluate)
4194 {
4195 /*
4196 * Compute the result.
4197 */
4198 if (op == '.')
4199 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004200 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4201 s2 = get_tv_string_buf_chk(&var2, buf2);
4202 if (s2 == NULL) /* type error ? */
4203 {
4204 clear_tv(rettv);
4205 clear_tv(&var2);
4206 return FAIL;
4207 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004208 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004209 clear_tv(rettv);
4210 rettv->v_type = VAR_STRING;
4211 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004213 else if (op == '+' && rettv->v_type == VAR_LIST
4214 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004215 {
4216 /* concatenate Lists */
4217 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4218 &var3) == FAIL)
4219 {
4220 clear_tv(rettv);
4221 clear_tv(&var2);
4222 return FAIL;
4223 }
4224 clear_tv(rettv);
4225 *rettv = var3;
4226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 else
4228 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004229 int error = FALSE;
4230
4231 n1 = get_tv_number_chk(rettv, &error);
4232 if (error)
4233 {
4234 /* This can only happen for "list + non-list".
4235 * For "non-list + ..." or "something - ...", we returned
4236 * before evaluating the 2nd operand. */
4237 clear_tv(rettv);
4238 return FAIL;
4239 }
4240 n2 = get_tv_number_chk(&var2, &error);
4241 if (error)
4242 {
4243 clear_tv(rettv);
4244 clear_tv(&var2);
4245 return FAIL;
4246 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 if (op == '+')
4249 n1 = n1 + n2;
4250 else
4251 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004252 rettv->v_type = VAR_NUMBER;
4253 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004255 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 }
4257 }
4258 return OK;
4259}
4260
4261/*
4262 * Handle fifth level expression:
4263 * * number multiplication
4264 * / number division
4265 * % number modulo
4266 *
4267 * "arg" must point to the first non-white of the expression.
4268 * "arg" is advanced to the next non-white after the recognized expression.
4269 *
4270 * Return OK or FAIL.
4271 */
4272 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004273eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 int evaluate;
4277{
Bram Moolenaar33570922005-01-25 22:26:29 +00004278 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 int op;
4280 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004281 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282
4283 /*
4284 * Get the first variable.
4285 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004286 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 return FAIL;
4288
4289 /*
4290 * Repeat computing, until no '*', '/' or '%' is following.
4291 */
4292 for (;;)
4293 {
4294 op = **arg;
4295 if (op != '*' && op != '/' && op != '%')
4296 break;
4297
4298 if (evaluate)
4299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004300 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004301 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004302 if (error)
4303 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 }
4305 else
4306 n1 = 0;
4307
4308 /*
4309 * Get the second variable.
4310 */
4311 *arg = skipwhite(*arg + 1);
4312 if (eval7(arg, &var2, evaluate) == FAIL)
4313 return FAIL;
4314
4315 if (evaluate)
4316 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004317 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004318 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004319 if (error)
4320 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321
4322 /*
4323 * Compute the result.
4324 */
4325 if (op == '*')
4326 n1 = n1 * n2;
4327 else if (op == '/')
4328 {
4329 if (n2 == 0) /* give an error message? */
4330 n1 = 0x7fffffffL;
4331 else
4332 n1 = n1 / n2;
4333 }
4334 else
4335 {
4336 if (n2 == 0) /* give an error message? */
4337 n1 = 0;
4338 else
4339 n1 = n1 % n2;
4340 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004341 rettv->v_type = VAR_NUMBER;
4342 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 }
4344 }
4345
4346 return OK;
4347}
4348
4349/*
4350 * Handle sixth level expression:
4351 * number number constant
4352 * "string" string contstant
4353 * 'string' literal string contstant
4354 * &option-name option value
4355 * @r register contents
4356 * identifier variable value
4357 * function() function call
4358 * $VAR environment variable
4359 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004360 * [expr, expr] List
4361 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 *
4363 * Also handle:
4364 * ! in front logical NOT
4365 * - in front unary minus
4366 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004367 * trailing [] subscript in String or List
4368 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 *
4370 * "arg" must point to the first non-white of the expression.
4371 * "arg" is advanced to the next non-white after the recognized expression.
4372 *
4373 * Return OK or FAIL.
4374 */
4375 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004376eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 int evaluate;
4380{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 long n;
4382 int len;
4383 char_u *s;
4384 int val;
4385 char_u *start_leader, *end_leader;
4386 int ret = OK;
4387 char_u *alias;
4388
4389 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004390 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004391 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004393 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394
4395 /*
4396 * Skip '!' and '-' characters. They are handled later.
4397 */
4398 start_leader = *arg;
4399 while (**arg == '!' || **arg == '-' || **arg == '+')
4400 *arg = skipwhite(*arg + 1);
4401 end_leader = *arg;
4402
4403 switch (**arg)
4404 {
4405 /*
4406 * Number constant.
4407 */
4408 case '0':
4409 case '1':
4410 case '2':
4411 case '3':
4412 case '4':
4413 case '5':
4414 case '6':
4415 case '7':
4416 case '8':
4417 case '9':
4418 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4419 *arg += len;
4420 if (evaluate)
4421 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004422 rettv->v_type = VAR_NUMBER;
4423 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 }
4425 break;
4426
4427 /*
4428 * String constant: "string".
4429 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004430 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 break;
4432
4433 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004434 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004436 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004437 break;
4438
4439 /*
4440 * List: [expr, expr]
4441 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004442 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 break;
4444
4445 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004446 * Dictionary: {key: val, key: val}
4447 */
4448 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4449 break;
4450
4451 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004452 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004454 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 break;
4456
4457 /*
4458 * Environment variable: $VAR.
4459 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004460 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 break;
4462
4463 /*
4464 * Register contents: @r.
4465 */
4466 case '@': ++*arg;
4467 if (evaluate)
4468 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004469 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004470 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 }
4472 if (**arg != NUL)
4473 ++*arg;
4474 break;
4475
4476 /*
4477 * nested expression: (expression).
4478 */
4479 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004480 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 if (**arg == ')')
4482 ++*arg;
4483 else if (ret == OK)
4484 {
4485 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004486 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 ret = FAIL;
4488 }
4489 break;
4490
Bram Moolenaar8c711452005-01-14 21:53:12 +00004491 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 break;
4493 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004494
4495 if (ret == NOTDONE)
4496 {
4497 /*
4498 * Must be a variable or function name.
4499 * Can also be a curly-braces kind of name: {expr}.
4500 */
4501 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004502 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004503 if (alias != NULL)
4504 s = alias;
4505
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004506 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004507 ret = FAIL;
4508 else
4509 {
4510 if (**arg == '(') /* recursive! */
4511 {
4512 /* If "s" is the name of a variable of type VAR_FUNC
4513 * use its contents. */
4514 s = deref_func_name(s, &len);
4515
4516 /* Invoke the function. */
4517 ret = get_func_tv(s, len, rettv, arg,
4518 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004519 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004520 /* Stop the expression evaluation when immediately
4521 * aborting on error, or when an interrupt occurred or
4522 * an exception was thrown but not caught. */
4523 if (aborting())
4524 {
4525 if (ret == OK)
4526 clear_tv(rettv);
4527 ret = FAIL;
4528 }
4529 }
4530 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004531 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004532 else
4533 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004534 }
4535
4536 if (alias != NULL)
4537 vim_free(alias);
4538 }
4539
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 *arg = skipwhite(*arg);
4541
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004542 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4543 * expr(expr). */
4544 if (ret == OK)
4545 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546
4547 /*
4548 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4549 */
4550 if (ret == OK && evaluate && end_leader > start_leader)
4551 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004552 int error = FALSE;
4553
4554 val = get_tv_number_chk(rettv, &error);
4555 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004557 clear_tv(rettv);
4558 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004560 else
4561 {
4562 while (end_leader > start_leader)
4563 {
4564 --end_leader;
4565 if (*end_leader == '!')
4566 val = !val;
4567 else if (*end_leader == '-')
4568 val = -val;
4569 }
4570 clear_tv(rettv);
4571 rettv->v_type = VAR_NUMBER;
4572 rettv->vval.v_number = val;
4573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 }
4575
4576 return ret;
4577}
4578
4579/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004580 * Evaluate an "[expr]" or "[expr:expr]" index.
4581 * "*arg" points to the '['.
4582 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4583 */
4584 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004585eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004586 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004587 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004588 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004589 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004590{
4591 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004592 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004593 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004594 long len = -1;
4595 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004596 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004597 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004598
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004599 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004600 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004601 if (verbose)
4602 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004603 return FAIL;
4604 }
4605
Bram Moolenaar8c711452005-01-14 21:53:12 +00004606 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004607 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004608 /*
4609 * dict.name
4610 */
4611 key = *arg + 1;
4612 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4613 ;
4614 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004615 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004616 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004617 }
4618 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004619 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004620 /*
4621 * something[idx]
4622 *
4623 * Get the (first) variable from inside the [].
4624 */
4625 *arg = skipwhite(*arg + 1);
4626 if (**arg == ':')
4627 empty1 = TRUE;
4628 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4629 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004630 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4631 {
4632 /* not a number or string */
4633 clear_tv(&var1);
4634 return FAIL;
4635 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004636
4637 /*
4638 * Get the second variable from inside the [:].
4639 */
4640 if (**arg == ':')
4641 {
4642 range = TRUE;
4643 *arg = skipwhite(*arg + 1);
4644 if (**arg == ']')
4645 empty2 = TRUE;
4646 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4647 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004648 if (!empty1)
4649 clear_tv(&var1);
4650 return FAIL;
4651 }
4652 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4653 {
4654 /* not a number or string */
4655 if (!empty1)
4656 clear_tv(&var1);
4657 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004658 return FAIL;
4659 }
4660 }
4661
4662 /* Check for the ']'. */
4663 if (**arg != ']')
4664 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004665 if (verbose)
4666 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004667 clear_tv(&var1);
4668 if (range)
4669 clear_tv(&var2);
4670 return FAIL;
4671 }
4672 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004673 }
4674
4675 if (evaluate)
4676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677 n1 = 0;
4678 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004679 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004680 n1 = get_tv_number(&var1);
4681 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004682 }
4683 if (range)
4684 {
4685 if (empty2)
4686 n2 = -1;
4687 else
4688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004689 n2 = get_tv_number(&var2);
4690 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004691 }
4692 }
4693
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004694 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004695 {
4696 case VAR_NUMBER:
4697 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004698 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004699 len = (long)STRLEN(s);
4700 if (range)
4701 {
4702 /* The resulting variable is a substring. If the indexes
4703 * are out of range the result is empty. */
4704 if (n1 < 0)
4705 {
4706 n1 = len + n1;
4707 if (n1 < 0)
4708 n1 = 0;
4709 }
4710 if (n2 < 0)
4711 n2 = len + n2;
4712 else if (n2 >= len)
4713 n2 = len;
4714 if (n1 >= len || n2 < 0 || n1 > n2)
4715 s = NULL;
4716 else
4717 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4718 }
4719 else
4720 {
4721 /* The resulting variable is a string of a single
4722 * character. If the index is too big or negative the
4723 * result is empty. */
4724 if (n1 >= len || n1 < 0)
4725 s = NULL;
4726 else
4727 s = vim_strnsave(s + n1, 1);
4728 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004729 clear_tv(rettv);
4730 rettv->v_type = VAR_STRING;
4731 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004732 break;
4733
4734 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004735 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004736 if (n1 < 0)
4737 n1 = len + n1;
4738 if (!empty1 && (n1 < 0 || n1 >= len))
4739 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004740 if (verbose)
4741 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004742 return FAIL;
4743 }
4744 if (range)
4745 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004746 list_T *l;
4747 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004748
4749 if (n2 < 0)
4750 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004751 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004752 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004753 if (verbose)
4754 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004755 return FAIL;
4756 }
4757 l = list_alloc();
4758 if (l == NULL)
4759 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004760 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004761 n1 <= n2; ++n1)
4762 {
4763 if (list_append_tv(l, &item->li_tv) == FAIL)
4764 {
4765 list_free(l);
4766 return FAIL;
4767 }
4768 item = item->li_next;
4769 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004770 clear_tv(rettv);
4771 rettv->v_type = VAR_LIST;
4772 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004773 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004774 }
4775 else
4776 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004777 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004778 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004779 clear_tv(rettv);
4780 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004781 }
4782 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004783
4784 case VAR_DICT:
4785 if (range)
4786 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004787 if (verbose)
4788 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004789 if (len == -1)
4790 clear_tv(&var1);
4791 return FAIL;
4792 }
4793 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004794 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004795
4796 if (len == -1)
4797 {
4798 key = get_tv_string(&var1);
4799 if (*key == NUL)
4800 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004801 if (verbose)
4802 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004803 clear_tv(&var1);
4804 return FAIL;
4805 }
4806 }
4807
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004808 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004809
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004810 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004811 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004812 if (len == -1)
4813 clear_tv(&var1);
4814 if (item == NULL)
4815 return FAIL;
4816
4817 copy_tv(&item->di_tv, &var1);
4818 clear_tv(rettv);
4819 *rettv = var1;
4820 }
4821 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004822 }
4823 }
4824
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004825 return OK;
4826}
4827
4828/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 * Get an option value.
4830 * "arg" points to the '&' or '+' before the option name.
4831 * "arg" is advanced to character after the option name.
4832 * Return OK or FAIL.
4833 */
4834 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004835get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004837 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 int evaluate;
4839{
4840 char_u *option_end;
4841 long numval;
4842 char_u *stringval;
4843 int opt_type;
4844 int c;
4845 int working = (**arg == '+'); /* has("+option") */
4846 int ret = OK;
4847 int opt_flags;
4848
4849 /*
4850 * Isolate the option name and find its value.
4851 */
4852 option_end = find_option_end(arg, &opt_flags);
4853 if (option_end == NULL)
4854 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004855 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 EMSG2(_("E112: Option name missing: %s"), *arg);
4857 return FAIL;
4858 }
4859
4860 if (!evaluate)
4861 {
4862 *arg = option_end;
4863 return OK;
4864 }
4865
4866 c = *option_end;
4867 *option_end = NUL;
4868 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004869 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870
4871 if (opt_type == -3) /* invalid name */
4872 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004873 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 EMSG2(_("E113: Unknown option: %s"), *arg);
4875 ret = FAIL;
4876 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004877 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
4879 if (opt_type == -2) /* hidden string option */
4880 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004881 rettv->v_type = VAR_STRING;
4882 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 }
4884 else if (opt_type == -1) /* hidden number option */
4885 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004886 rettv->v_type = VAR_NUMBER;
4887 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889 else if (opt_type == 1) /* number option */
4890 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004891 rettv->v_type = VAR_NUMBER;
4892 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 }
4894 else /* string option */
4895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004896 rettv->v_type = VAR_STRING;
4897 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 }
4899 }
4900 else if (working && (opt_type == -2 || opt_type == -1))
4901 ret = FAIL;
4902
4903 *option_end = c; /* put back for error messages */
4904 *arg = option_end;
4905
4906 return ret;
4907}
4908
4909/*
4910 * Allocate a variable for a string constant.
4911 * Return OK or FAIL.
4912 */
4913 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 int evaluate;
4918{
4919 char_u *p;
4920 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 int extra = 0;
4922
4923 /*
4924 * Find the end of the string, skipping backslashed characters.
4925 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004926 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 {
4928 if (*p == '\\' && p[1] != NUL)
4929 {
4930 ++p;
4931 /* A "\<x>" form occupies at least 4 characters, and produces up
4932 * to 6 characters: reserve space for 2 extra */
4933 if (*p == '<')
4934 extra += 2;
4935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
4937
4938 if (*p != '"')
4939 {
4940 EMSG2(_("E114: Missing quote: %s"), *arg);
4941 return FAIL;
4942 }
4943
4944 /* If only parsing, set *arg and return here */
4945 if (!evaluate)
4946 {
4947 *arg = p + 1;
4948 return OK;
4949 }
4950
4951 /*
4952 * Copy the string into allocated memory, handling backslashed
4953 * characters.
4954 */
4955 name = alloc((unsigned)(p - *arg + extra));
4956 if (name == NULL)
4957 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004958 rettv->v_type = VAR_STRING;
4959 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960
Bram Moolenaar8c711452005-01-14 21:53:12 +00004961 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 {
4963 if (*p == '\\')
4964 {
4965 switch (*++p)
4966 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004967 case 'b': *name++ = BS; ++p; break;
4968 case 'e': *name++ = ESC; ++p; break;
4969 case 'f': *name++ = FF; ++p; break;
4970 case 'n': *name++ = NL; ++p; break;
4971 case 'r': *name++ = CAR; ++p; break;
4972 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
4974 case 'X': /* hex: "\x1", "\x12" */
4975 case 'x':
4976 case 'u': /* Unicode: "\u0023" */
4977 case 'U':
4978 if (vim_isxdigit(p[1]))
4979 {
4980 int n, nr;
4981 int c = toupper(*p);
4982
4983 if (c == 'X')
4984 n = 2;
4985 else
4986 n = 4;
4987 nr = 0;
4988 while (--n >= 0 && vim_isxdigit(p[1]))
4989 {
4990 ++p;
4991 nr = (nr << 4) + hex2nr(*p);
4992 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004993 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994#ifdef FEAT_MBYTE
4995 /* For "\u" store the number according to
4996 * 'encoding'. */
4997 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004998 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 else
5000#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005001 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 break;
5004
5005 /* octal: "\1", "\12", "\123" */
5006 case '0':
5007 case '1':
5008 case '2':
5009 case '3':
5010 case '4':
5011 case '5':
5012 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005013 case '7': *name = *p++ - '0';
5014 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005016 *name = (*name << 3) + *p++ - '0';
5017 if (*p >= '0' && *p <= '7')
5018 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005020 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 break;
5022
5023 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005024 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 if (extra != 0)
5026 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005027 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 break;
5029 }
5030 /* FALLTHROUGH */
5031
Bram Moolenaar8c711452005-01-14 21:53:12 +00005032 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 break;
5034 }
5035 }
5036 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005037 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 *arg = p + 1;
5042
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 return OK;
5044}
5045
5046/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005047 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 * Return OK or FAIL.
5049 */
5050 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005051get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 int evaluate;
5055{
5056 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005057 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005058 int reduce = 0;
5059
5060 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005061 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005062 */
5063 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5064 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005065 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005066 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005068 break;
5069 ++reduce;
5070 ++p;
5071 }
5072 }
5073
Bram Moolenaar8c711452005-01-14 21:53:12 +00005074 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005075 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005076 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005077 return FAIL;
5078 }
5079
Bram Moolenaar8c711452005-01-14 21:53:12 +00005080 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005081 if (!evaluate)
5082 {
5083 *arg = p + 1;
5084 return OK;
5085 }
5086
5087 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005088 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005089 */
5090 str = alloc((unsigned)((p - *arg) - reduce));
5091 if (str == NULL)
5092 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005093 rettv->v_type = VAR_STRING;
5094 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005095
Bram Moolenaar8c711452005-01-14 21:53:12 +00005096 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005097 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005098 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005099 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005100 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005101 break;
5102 ++p;
5103 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005104 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005105 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005106 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005107 *arg = p + 1;
5108
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005109 return OK;
5110}
5111
5112/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005113 * Allocate a variable for a List and fill it from "*arg".
5114 * Return OK or FAIL.
5115 */
5116 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005118 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005119 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005120 int evaluate;
5121{
Bram Moolenaar33570922005-01-25 22:26:29 +00005122 list_T *l = NULL;
5123 typval_T tv;
5124 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005125
5126 if (evaluate)
5127 {
5128 l = list_alloc();
5129 if (l == NULL)
5130 return FAIL;
5131 }
5132
5133 *arg = skipwhite(*arg + 1);
5134 while (**arg != ']' && **arg != NUL)
5135 {
5136 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5137 goto failret;
5138 if (evaluate)
5139 {
5140 item = listitem_alloc();
5141 if (item != NULL)
5142 {
5143 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005144 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005145 list_append(l, item);
5146 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005147 else
5148 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005149 }
5150
5151 if (**arg == ']')
5152 break;
5153 if (**arg != ',')
5154 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005156 goto failret;
5157 }
5158 *arg = skipwhite(*arg + 1);
5159 }
5160
5161 if (**arg != ']')
5162 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005164failret:
5165 if (evaluate)
5166 list_free(l);
5167 return FAIL;
5168 }
5169
5170 *arg = skipwhite(*arg + 1);
5171 if (evaluate)
5172 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005173 rettv->v_type = VAR_LIST;
5174 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005175 ++l->lv_refcount;
5176 }
5177
5178 return OK;
5179}
5180
5181/*
5182 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005183 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005184 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005185 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005186list_alloc()
5187{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005188 list_T *l;
5189
5190 l = (list_T *)alloc_clear(sizeof(list_T));
5191 if (l != NULL)
5192 {
5193 /* Prepend the list to the list of lists for garbage collection. */
5194 if (first_list != NULL)
5195 first_list->lv_used_prev = l;
5196 l->lv_used_prev = NULL;
5197 l->lv_used_next = first_list;
5198 first_list = l;
5199 }
5200 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005201}
5202
5203/*
5204 * Unreference a list: decrement the reference count and free it when it
5205 * becomes zero.
5206 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005207 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005208list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005209 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005210{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005211 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5212 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005213}
5214
5215/*
5216 * Free a list, including all items it points to.
5217 * Ignores the reference count.
5218 */
5219 static void
5220list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005221 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005222{
Bram Moolenaar33570922005-01-25 22:26:29 +00005223 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005224
Bram Moolenaard9fba312005-06-26 22:34:35 +00005225 /* Avoid that recursive reference to the list frees us again. */
5226 l->lv_refcount = DEL_REFCOUNT;
5227
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005228 /* Remove the list from the list of lists for garbage collection. */
5229 if (l->lv_used_prev == NULL)
5230 first_list = l->lv_used_next;
5231 else
5232 l->lv_used_prev->lv_used_next = l->lv_used_next;
5233 if (l->lv_used_next != NULL)
5234 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5235
Bram Moolenaard9fba312005-06-26 22:34:35 +00005236 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005237 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005238 /* Remove the item before deleting it. */
5239 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005240 listitem_free(item);
5241 }
5242 vim_free(l);
5243}
5244
5245/*
5246 * Allocate a list item.
5247 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005248 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005249listitem_alloc()
5250{
Bram Moolenaar33570922005-01-25 22:26:29 +00005251 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005252}
5253
5254/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005255 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256 */
5257 static void
5258listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005259 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005261 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 vim_free(item);
5263}
5264
5265/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005266 * Remove a list item from a List and free it. Also clears the value.
5267 */
5268 static void
5269listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005270 list_T *l;
5271 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005272{
5273 list_remove(l, item, item);
5274 listitem_free(item);
5275}
5276
5277/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 * Get the number of items in a list.
5279 */
5280 static long
5281list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005282 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005283{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005284 if (l == NULL)
5285 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005286 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287}
5288
5289/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005290 * Return TRUE when two lists have exactly the same values.
5291 */
5292 static int
5293list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005294 list_T *l1;
5295 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005296 int ic; /* ignore case for strings */
5297{
Bram Moolenaar33570922005-01-25 22:26:29 +00005298 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005299
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005300 if (list_len(l1) != list_len(l2))
5301 return FALSE;
5302
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005303 for (item1 = l1->lv_first, item2 = l2->lv_first;
5304 item1 != NULL && item2 != NULL;
5305 item1 = item1->li_next, item2 = item2->li_next)
5306 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5307 return FALSE;
5308 return item1 == NULL && item2 == NULL;
5309}
5310
5311/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005312 * Return TRUE when two dictionaries have exactly the same key/values.
5313 */
5314 static int
5315dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005316 dict_T *d1;
5317 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005318 int ic; /* ignore case for strings */
5319{
Bram Moolenaar33570922005-01-25 22:26:29 +00005320 hashitem_T *hi;
5321 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005322 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005323
5324 if (dict_len(d1) != dict_len(d2))
5325 return FALSE;
5326
Bram Moolenaar33570922005-01-25 22:26:29 +00005327 todo = d1->dv_hashtab.ht_used;
5328 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005329 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005330 if (!HASHITEM_EMPTY(hi))
5331 {
5332 item2 = dict_find(d2, hi->hi_key, -1);
5333 if (item2 == NULL)
5334 return FALSE;
5335 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5336 return FALSE;
5337 --todo;
5338 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005339 }
5340 return TRUE;
5341}
5342
5343/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005344 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005345 * Compares the items just like "==" would compare them, but strings and
5346 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005347 */
5348 static int
5349tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005350 typval_T *tv1;
5351 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005352 int ic; /* ignore case */
5353{
5354 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005355 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005356
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005357 if (tv1->v_type != tv2->v_type)
5358 return FALSE;
5359
5360 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005361 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005362 case VAR_LIST:
5363 /* recursive! */
5364 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5365
5366 case VAR_DICT:
5367 /* recursive! */
5368 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5369
5370 case VAR_FUNC:
5371 return (tv1->vval.v_string != NULL
5372 && tv2->vval.v_string != NULL
5373 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5374
5375 case VAR_NUMBER:
5376 return tv1->vval.v_number == tv2->vval.v_number;
5377
5378 case VAR_STRING:
5379 s1 = get_tv_string_buf(tv1, buf1);
5380 s2 = get_tv_string_buf(tv2, buf2);
5381 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005382 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005383
5384 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005385 return TRUE;
5386}
5387
5388/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389 * Locate item with index "n" in list "l" and return it.
5390 * A negative index is counted from the end; -1 is the last item.
5391 * Returns NULL when "n" is out of range.
5392 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005393 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005394list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005395 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 long n;
5397{
Bram Moolenaar33570922005-01-25 22:26:29 +00005398 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 long idx;
5400
5401 if (l == NULL)
5402 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005403
5404 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005406 n = l->lv_len + n;
5407
5408 /* Check for index out of range. */
5409 if (n < 0 || n >= l->lv_len)
5410 return NULL;
5411
5412 /* When there is a cached index may start search from there. */
5413 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005415 if (n < l->lv_idx / 2)
5416 {
5417 /* closest to the start of the list */
5418 item = l->lv_first;
5419 idx = 0;
5420 }
5421 else if (n > (l->lv_idx + l->lv_len) / 2)
5422 {
5423 /* closest to the end of the list */
5424 item = l->lv_last;
5425 idx = l->lv_len - 1;
5426 }
5427 else
5428 {
5429 /* closest to the cached index */
5430 item = l->lv_idx_item;
5431 idx = l->lv_idx;
5432 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 }
5434 else
5435 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005436 if (n < l->lv_len / 2)
5437 {
5438 /* closest to the start of the list */
5439 item = l->lv_first;
5440 idx = 0;
5441 }
5442 else
5443 {
5444 /* closest to the end of the list */
5445 item = l->lv_last;
5446 idx = l->lv_len - 1;
5447 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005449
5450 while (n > idx)
5451 {
5452 /* search forward */
5453 item = item->li_next;
5454 ++idx;
5455 }
5456 while (n < idx)
5457 {
5458 /* search backward */
5459 item = item->li_prev;
5460 --idx;
5461 }
5462
5463 /* cache the used index */
5464 l->lv_idx = idx;
5465 l->lv_idx_item = item;
5466
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 return item;
5468}
5469
5470/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005471 * Locate "item" list "l" and return its index.
5472 * Returns -1 when "item" is not in the list.
5473 */
5474 static long
5475list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005476 list_T *l;
5477 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005478{
5479 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005480 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005481
5482 if (l == NULL)
5483 return -1;
5484 idx = 0;
5485 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5486 ++idx;
5487 if (li == NULL)
5488 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005489 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005490}
5491
5492/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 * Append item "item" to the end of list "l".
5494 */
5495 static void
5496list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005497 list_T *l;
5498 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005499{
5500 if (l->lv_last == NULL)
5501 {
5502 /* empty list */
5503 l->lv_first = item;
5504 l->lv_last = item;
5505 item->li_prev = NULL;
5506 }
5507 else
5508 {
5509 l->lv_last->li_next = item;
5510 item->li_prev = l->lv_last;
5511 l->lv_last = item;
5512 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005513 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005514 item->li_next = NULL;
5515}
5516
5517/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005518 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005519 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005520 */
5521 static int
5522list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005523 list_T *l;
5524 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005525{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005526 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005527
Bram Moolenaar05159a02005-02-26 23:04:13 +00005528 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005529 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005530 copy_tv(tv, &li->li_tv);
5531 list_append(l, li);
5532 return OK;
5533}
5534
5535/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005536 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005537 * Return FAIL when out of memory.
5538 */
5539 int
5540list_append_dict(list, dict)
5541 list_T *list;
5542 dict_T *dict;
5543{
5544 listitem_T *li = listitem_alloc();
5545
5546 if (li == NULL)
5547 return FAIL;
5548 li->li_tv.v_type = VAR_DICT;
5549 li->li_tv.v_lock = 0;
5550 li->li_tv.vval.v_dict = dict;
5551 list_append(list, li);
5552 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553 return OK;
5554}
5555
5556/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005557 * Make a copy of "str" and append it as an item to list "l".
5558 * Returns FAIL when out of memory.
5559 */
5560 static int
5561list_append_string(l, str)
5562 list_T *l;
5563 char_u *str;
5564{
5565 listitem_T *li = listitem_alloc();
5566
5567 if (li == NULL)
5568 return FAIL;
5569 list_append(l, li);
5570 li->li_tv.v_type = VAR_STRING;
5571 li->li_tv.v_lock = 0;
5572 if ((li->li_tv.vval.v_string = vim_strsave(str)) == NULL)
5573 return FAIL;
5574 return OK;
5575}
5576
5577/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005578 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005579 * If "item" is NULL append at the end.
5580 * Return FAIL when out of memory.
5581 */
5582 static int
5583list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005584 list_T *l;
5585 typval_T *tv;
5586 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005587{
Bram Moolenaar33570922005-01-25 22:26:29 +00005588 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005589
5590 if (ni == NULL)
5591 return FAIL;
5592 copy_tv(tv, &ni->li_tv);
5593 if (item == NULL)
5594 /* Append new item at end of list. */
5595 list_append(l, ni);
5596 else
5597 {
5598 /* Insert new item before existing item. */
5599 ni->li_prev = item->li_prev;
5600 ni->li_next = item;
5601 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005602 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005603 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005604 ++l->lv_idx;
5605 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005606 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005607 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005608 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005609 l->lv_idx_item = NULL;
5610 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005611 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005612 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005613 }
5614 return OK;
5615}
5616
5617/*
5618 * Extend "l1" with "l2".
5619 * If "bef" is NULL append at the end, otherwise insert before this item.
5620 * Returns FAIL when out of memory.
5621 */
5622 static int
5623list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005624 list_T *l1;
5625 list_T *l2;
5626 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005627{
Bram Moolenaar33570922005-01-25 22:26:29 +00005628 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005629
5630 for (item = l2->lv_first; item != NULL; item = item->li_next)
5631 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5632 return FAIL;
5633 return OK;
5634}
5635
5636/*
5637 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5638 * Return FAIL when out of memory.
5639 */
5640 static int
5641list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005642 list_T *l1;
5643 list_T *l2;
5644 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005645{
Bram Moolenaar33570922005-01-25 22:26:29 +00005646 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005647
5648 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005649 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005650 if (l == NULL)
5651 return FAIL;
5652 tv->v_type = VAR_LIST;
5653 tv->vval.v_list = l;
5654
5655 /* append all items from the second list */
5656 return list_extend(l, l2, NULL);
5657}
5658
5659/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005660 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005661 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005662 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005663 * Returns NULL when out of memory.
5664 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005665 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005666list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005667 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005668 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005669 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005670{
Bram Moolenaar33570922005-01-25 22:26:29 +00005671 list_T *copy;
5672 listitem_T *item;
5673 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005674
5675 if (orig == NULL)
5676 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005677
5678 copy = list_alloc();
5679 if (copy != NULL)
5680 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005681 if (copyID != 0)
5682 {
5683 /* Do this before adding the items, because one of the items may
5684 * refer back to this list. */
5685 orig->lv_copyID = copyID;
5686 orig->lv_copylist = copy;
5687 }
5688 for (item = orig->lv_first; item != NULL && !got_int;
5689 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005690 {
5691 ni = listitem_alloc();
5692 if (ni == NULL)
5693 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005694 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005695 {
5696 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5697 {
5698 vim_free(ni);
5699 break;
5700 }
5701 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005702 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005703 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005704 list_append(copy, ni);
5705 }
5706 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005707 if (item != NULL)
5708 {
5709 list_unref(copy);
5710 copy = NULL;
5711 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005712 }
5713
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005714 return copy;
5715}
5716
5717/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005718 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005719 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005720 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005721 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005722list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005723 list_T *l;
5724 listitem_T *item;
5725 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005726{
Bram Moolenaar33570922005-01-25 22:26:29 +00005727 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005728
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005729 /* notify watchers */
5730 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005731 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005732 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005733 list_fix_watch(l, ip);
5734 if (ip == item2)
5735 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005736 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005737
5738 if (item2->li_next == NULL)
5739 l->lv_last = item->li_prev;
5740 else
5741 item2->li_next->li_prev = item->li_prev;
5742 if (item->li_prev == NULL)
5743 l->lv_first = item2->li_next;
5744 else
5745 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005746 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005747}
5748
5749/*
5750 * Return an allocated string with the string representation of a list.
5751 * May return NULL.
5752 */
5753 static char_u *
5754list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005755 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005756{
5757 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005758
5759 if (tv->vval.v_list == NULL)
5760 return NULL;
5761 ga_init2(&ga, (int)sizeof(char), 80);
5762 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005763 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5764 {
5765 vim_free(ga.ga_data);
5766 return NULL;
5767 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005768 ga_append(&ga, ']');
5769 ga_append(&ga, NUL);
5770 return (char_u *)ga.ga_data;
5771}
5772
5773/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005774 * Join list "l" into a string in "*gap", using separator "sep".
5775 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005776 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005777 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005778 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005779list_join(gap, l, sep, echo)
5780 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005781 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005782 char_u *sep;
5783 int echo;
5784{
5785 int first = TRUE;
5786 char_u *tofree;
5787 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005788 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005789 char_u *s;
5790
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005791 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005792 {
5793 if (first)
5794 first = FALSE;
5795 else
5796 ga_concat(gap, sep);
5797
5798 if (echo)
5799 s = echo_string(&item->li_tv, &tofree, numbuf);
5800 else
5801 s = tv2string(&item->li_tv, &tofree, numbuf);
5802 if (s != NULL)
5803 ga_concat(gap, s);
5804 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005805 if (s == NULL)
5806 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005807 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005808 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005809}
5810
5811/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005812 * Garbage collection for lists and dictionaries.
5813 *
5814 * We use reference counts to be able to free most items right away when they
5815 * are no longer used. But for composite items it's possible that it becomes
5816 * unused while the reference count is > 0: When there is a recursive
5817 * reference. Example:
5818 * :let l = [1, 2, 3]
5819 * :let d = {9: l}
5820 * :let l[1] = d
5821 *
5822 * Since this is quite unusual we handle this with garbage collection: every
5823 * once in a while find out which lists and dicts are not referenced from any
5824 * variable.
5825 *
5826 * Here is a good reference text about garbage collection (refers to Python
5827 * but it applies to all reference-counting mechanisms):
5828 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005829 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005830
5831/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005832 * Do garbage collection for lists and dicts.
5833 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005834 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005835 int
5836garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005837{
5838 dict_T *dd;
5839 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005840 int copyID = ++current_copyID;
5841 buf_T *buf;
5842 win_T *wp;
5843 int i;
5844 funccall_T *fc;
5845 int did_free = FALSE;
5846
5847 /*
5848 * 1. Go through all accessible variables and mark all lists and dicts
5849 * with copyID.
5850 */
5851 /* script-local variables */
5852 for (i = 1; i <= ga_scripts.ga_len; ++i)
5853 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5854
5855 /* buffer-local variables */
5856 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5857 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5858
5859 /* window-local variables */
5860 FOR_ALL_WINDOWS(wp)
5861 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5862
5863 /* global variables */
5864 set_ref_in_ht(&globvarht, copyID);
5865
5866 /* function-local variables */
5867 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5868 {
5869 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5870 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5871 }
5872
5873 /*
5874 * 2. Go through the list of dicts and free items without the copyID.
5875 */
5876 for (dd = first_dict; dd != NULL; )
5877 if (dd->dv_copyID != copyID)
5878 {
5879 dict_free(dd);
5880 did_free = TRUE;
5881
5882 /* restart, next dict may also have been freed */
5883 dd = first_dict;
5884 }
5885 else
5886 dd = dd->dv_used_next;
5887
5888 /*
5889 * 3. Go through the list of lists and free items without the copyID.
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005890 * But don't free a list that has a watcher (used in a for loop), these
5891 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005892 */
5893 for (ll = first_list; ll != NULL; )
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005894 if (ll->lv_copyID != copyID && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005895 {
5896 list_free(ll);
5897 did_free = TRUE;
5898
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00005899 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005900 ll = first_list;
5901 }
5902 else
5903 ll = ll->lv_used_next;
5904
5905 return did_free;
5906}
5907
5908/*
5909 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5910 */
5911 static void
5912set_ref_in_ht(ht, copyID)
5913 hashtab_T *ht;
5914 int copyID;
5915{
5916 int todo;
5917 hashitem_T *hi;
5918
5919 todo = ht->ht_used;
5920 for (hi = ht->ht_array; todo > 0; ++hi)
5921 if (!HASHITEM_EMPTY(hi))
5922 {
5923 --todo;
5924 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5925 }
5926}
5927
5928/*
5929 * Mark all lists and dicts referenced through list "l" with "copyID".
5930 */
5931 static void
5932set_ref_in_list(l, copyID)
5933 list_T *l;
5934 int copyID;
5935{
5936 listitem_T *li;
5937
5938 for (li = l->lv_first; li != NULL; li = li->li_next)
5939 set_ref_in_item(&li->li_tv, copyID);
5940}
5941
5942/*
5943 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5944 */
5945 static void
5946set_ref_in_item(tv, copyID)
5947 typval_T *tv;
5948 int copyID;
5949{
5950 dict_T *dd;
5951 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005952
5953 switch (tv->v_type)
5954 {
5955 case VAR_DICT:
5956 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005957 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005958 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005959 /* Didn't see this dict yet. */
5960 dd->dv_copyID = copyID;
5961 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005962 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005963 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005964
5965 case VAR_LIST:
5966 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005967 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005968 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005969 /* Didn't see this list yet. */
5970 ll->lv_copyID = copyID;
5971 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005972 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005973 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005974 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005975 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005976}
5977
5978/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005979 * Allocate an empty header for a dictionary.
5980 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005981 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005982dict_alloc()
5983{
Bram Moolenaar33570922005-01-25 22:26:29 +00005984 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005985
Bram Moolenaar33570922005-01-25 22:26:29 +00005986 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005987 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005988 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005989 /* Add the list to the hashtable for garbage collection. */
5990 if (first_dict != NULL)
5991 first_dict->dv_used_prev = d;
5992 d->dv_used_next = first_dict;
5993 d->dv_used_prev = NULL;
5994
Bram Moolenaar33570922005-01-25 22:26:29 +00005995 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005996 d->dv_lock = 0;
5997 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005998 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005999 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006000 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006001}
6002
6003/*
6004 * Unreference a Dictionary: decrement the reference count and free it when it
6005 * becomes zero.
6006 */
6007 static void
6008dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006009 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006010{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006011 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
6012 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006013}
6014
6015/*
6016 * Free a Dictionary, including all items it contains.
6017 * Ignores the reference count.
6018 */
6019 static void
6020dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006021 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006022{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006023 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006024 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006025 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006026
Bram Moolenaard9fba312005-06-26 22:34:35 +00006027 /* Avoid that recursive reference to the dict frees us again. */
6028 d->dv_refcount = DEL_REFCOUNT;
6029
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006030 /* Remove the dict from the list of dicts for garbage collection. */
6031 if (d->dv_used_prev == NULL)
6032 first_dict = d->dv_used_next;
6033 else
6034 d->dv_used_prev->dv_used_next = d->dv_used_next;
6035 if (d->dv_used_next != NULL)
6036 d->dv_used_next->dv_used_prev = d->dv_used_prev;
6037
6038 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006039 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00006040 todo = d->dv_hashtab.ht_used;
6041 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006042 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006043 if (!HASHITEM_EMPTY(hi))
6044 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006045 /* Remove the item before deleting it, just in case there is
6046 * something recursive causing trouble. */
6047 di = HI2DI(hi);
6048 hash_remove(&d->dv_hashtab, hi);
6049 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006050 --todo;
6051 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006052 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006053 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006054 vim_free(d);
6055}
6056
6057/*
6058 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006059 * The "key" is copied to the new item.
6060 * Note that the value of the item "di_tv" still needs to be initialized!
6061 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006062 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006063 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006064dictitem_alloc(key)
6065 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006066{
Bram Moolenaar33570922005-01-25 22:26:29 +00006067 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006068
Bram Moolenaar33570922005-01-25 22:26:29 +00006069 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006070 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006071 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006072 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006073 di->di_flags = 0;
6074 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006075 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006076}
6077
6078/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006079 * Make a copy of a Dictionary item.
6080 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006081 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006082dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006083 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006084{
Bram Moolenaar33570922005-01-25 22:26:29 +00006085 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006086
Bram Moolenaar33570922005-01-25 22:26:29 +00006087 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006088 if (di != NULL)
6089 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006090 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006091 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006092 copy_tv(&org->di_tv, &di->di_tv);
6093 }
6094 return di;
6095}
6096
6097/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006098 * Remove item "item" from Dictionary "dict" and free it.
6099 */
6100 static void
6101dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006102 dict_T *dict;
6103 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006104{
Bram Moolenaar33570922005-01-25 22:26:29 +00006105 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006106
Bram Moolenaar33570922005-01-25 22:26:29 +00006107 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006108 if (HASHITEM_EMPTY(hi))
6109 EMSG2(_(e_intern2), "dictitem_remove()");
6110 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006111 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006112 dictitem_free(item);
6113}
6114
6115/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006116 * Free a dict item. Also clears the value.
6117 */
6118 static void
6119dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006120 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006121{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006122 clear_tv(&item->di_tv);
6123 vim_free(item);
6124}
6125
6126/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006127 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6128 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006129 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006130 * Returns NULL when out of memory.
6131 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006132 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006133dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006134 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006135 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006136 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006137{
Bram Moolenaar33570922005-01-25 22:26:29 +00006138 dict_T *copy;
6139 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006140 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006141 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006142
6143 if (orig == NULL)
6144 return NULL;
6145
6146 copy = dict_alloc();
6147 if (copy != NULL)
6148 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006149 if (copyID != 0)
6150 {
6151 orig->dv_copyID = copyID;
6152 orig->dv_copydict = copy;
6153 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006154 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006155 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006156 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006157 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006158 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006159 --todo;
6160
6161 di = dictitem_alloc(hi->hi_key);
6162 if (di == NULL)
6163 break;
6164 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006165 {
6166 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6167 copyID) == FAIL)
6168 {
6169 vim_free(di);
6170 break;
6171 }
6172 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006173 else
6174 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6175 if (dict_add(copy, di) == FAIL)
6176 {
6177 dictitem_free(di);
6178 break;
6179 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006180 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006181 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006182
Bram Moolenaare9a41262005-01-15 22:18:47 +00006183 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006184 if (todo > 0)
6185 {
6186 dict_unref(copy);
6187 copy = NULL;
6188 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006189 }
6190
6191 return copy;
6192}
6193
6194/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006195 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006196 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006197 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006198 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006199dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006200 dict_T *d;
6201 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006202{
Bram Moolenaar33570922005-01-25 22:26:29 +00006203 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006204}
6205
Bram Moolenaar8c711452005-01-14 21:53:12 +00006206/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006207 * Add a number or string entry to dictionary "d".
6208 * When "str" is NULL use number "nr", otherwise use "str".
6209 * Returns FAIL when out of memory and when key already exists.
6210 */
6211 int
6212dict_add_nr_str(d, key, nr, str)
6213 dict_T *d;
6214 char *key;
6215 long nr;
6216 char_u *str;
6217{
6218 dictitem_T *item;
6219
6220 item = dictitem_alloc((char_u *)key);
6221 if (item == NULL)
6222 return FAIL;
6223 item->di_tv.v_lock = 0;
6224 if (str == NULL)
6225 {
6226 item->di_tv.v_type = VAR_NUMBER;
6227 item->di_tv.vval.v_number = nr;
6228 }
6229 else
6230 {
6231 item->di_tv.v_type = VAR_STRING;
6232 item->di_tv.vval.v_string = vim_strsave(str);
6233 }
6234 if (dict_add(d, item) == FAIL)
6235 {
6236 dictitem_free(item);
6237 return FAIL;
6238 }
6239 return OK;
6240}
6241
6242/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006243 * Get the number of items in a Dictionary.
6244 */
6245 static long
6246dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006247 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006248{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006249 if (d == NULL)
6250 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006251 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006252}
6253
6254/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006255 * Find item "key[len]" in Dictionary "d".
6256 * If "len" is negative use strlen(key).
6257 * Returns NULL when not found.
6258 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006259 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006260dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006261 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006262 char_u *key;
6263 int len;
6264{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006265#define AKEYLEN 200
6266 char_u buf[AKEYLEN];
6267 char_u *akey;
6268 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006269 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006270
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006271 if (len < 0)
6272 akey = key;
6273 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006274 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006275 tofree = akey = vim_strnsave(key, len);
6276 if (akey == NULL)
6277 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006278 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006279 else
6280 {
6281 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006282 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006283 akey = buf;
6284 }
6285
Bram Moolenaar33570922005-01-25 22:26:29 +00006286 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006287 vim_free(tofree);
6288 if (HASHITEM_EMPTY(hi))
6289 return NULL;
6290 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006291}
6292
6293/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006294 * Get a string item from a dictionary in allocated memory.
6295 * Returns NULL if the entry doesn't exist or out of memory.
6296 */
6297 char_u *
6298get_dict_string(d, key)
6299 dict_T *d;
6300 char_u *key;
6301{
6302 dictitem_T *di;
6303
6304 di = dict_find(d, key, -1);
6305 if (di == NULL)
6306 return NULL;
6307 return vim_strsave(get_tv_string(&di->di_tv));
6308}
6309
6310/*
6311 * Get a number item from a dictionary.
6312 * Returns 0 if the entry doesn't exist or out of memory.
6313 */
6314 long
6315get_dict_number(d, key)
6316 dict_T *d;
6317 char_u *key;
6318{
6319 dictitem_T *di;
6320
6321 di = dict_find(d, key, -1);
6322 if (di == NULL)
6323 return 0;
6324 return get_tv_number(&di->di_tv);
6325}
6326
6327/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006328 * Return an allocated string with the string representation of a Dictionary.
6329 * May return NULL.
6330 */
6331 static char_u *
6332dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006333 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006334{
6335 garray_T ga;
6336 int first = TRUE;
6337 char_u *tofree;
6338 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006339 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006340 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006341 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006342 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006343
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006344 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006345 return NULL;
6346 ga_init2(&ga, (int)sizeof(char), 80);
6347 ga_append(&ga, '{');
6348
Bram Moolenaar33570922005-01-25 22:26:29 +00006349 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006350 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006351 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006352 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006353 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006354 --todo;
6355
6356 if (first)
6357 first = FALSE;
6358 else
6359 ga_concat(&ga, (char_u *)", ");
6360
6361 tofree = string_quote(hi->hi_key, FALSE);
6362 if (tofree != NULL)
6363 {
6364 ga_concat(&ga, tofree);
6365 vim_free(tofree);
6366 }
6367 ga_concat(&ga, (char_u *)": ");
6368 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6369 if (s != NULL)
6370 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006371 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006372 if (s == NULL)
6373 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006374 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006375 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006376 if (todo > 0)
6377 {
6378 vim_free(ga.ga_data);
6379 return NULL;
6380 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006381
6382 ga_append(&ga, '}');
6383 ga_append(&ga, NUL);
6384 return (char_u *)ga.ga_data;
6385}
6386
6387/*
6388 * Allocate a variable for a Dictionary and fill it from "*arg".
6389 * Return OK or FAIL. Returns NOTDONE for {expr}.
6390 */
6391 static int
6392get_dict_tv(arg, rettv, evaluate)
6393 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006395 int evaluate;
6396{
Bram Moolenaar33570922005-01-25 22:26:29 +00006397 dict_T *d = NULL;
6398 typval_T tvkey;
6399 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006400 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006401 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006402 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006403 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006404
6405 /*
6406 * First check if it's not a curly-braces thing: {expr}.
6407 * Must do this without evaluating, otherwise a function may be called
6408 * twice. Unfortunately this means we need to call eval1() twice for the
6409 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006410 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006411 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006412 if (*start != '}')
6413 {
6414 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6415 return FAIL;
6416 if (*start == '}')
6417 return NOTDONE;
6418 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006419
6420 if (evaluate)
6421 {
6422 d = dict_alloc();
6423 if (d == NULL)
6424 return FAIL;
6425 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006426 tvkey.v_type = VAR_UNKNOWN;
6427 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006428
6429 *arg = skipwhite(*arg + 1);
6430 while (**arg != '}' && **arg != NUL)
6431 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006432 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006433 goto failret;
6434 if (**arg != ':')
6435 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006436 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006437 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006438 goto failret;
6439 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006440 key = get_tv_string_buf_chk(&tvkey, buf);
6441 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006443 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6444 if (key != NULL)
6445 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006446 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006447 goto failret;
6448 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006449
6450 *arg = skipwhite(*arg + 1);
6451 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6452 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006453 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006454 goto failret;
6455 }
6456 if (evaluate)
6457 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006458 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006459 if (item != NULL)
6460 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006461 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006462 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006463 clear_tv(&tv);
6464 goto failret;
6465 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006466 item = dictitem_alloc(key);
6467 clear_tv(&tvkey);
6468 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006469 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006470 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006471 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006472 if (dict_add(d, item) == FAIL)
6473 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006474 }
6475 }
6476
6477 if (**arg == '}')
6478 break;
6479 if (**arg != ',')
6480 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006481 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006482 goto failret;
6483 }
6484 *arg = skipwhite(*arg + 1);
6485 }
6486
6487 if (**arg != '}')
6488 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006489 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006490failret:
6491 if (evaluate)
6492 dict_free(d);
6493 return FAIL;
6494 }
6495
6496 *arg = skipwhite(*arg + 1);
6497 if (evaluate)
6498 {
6499 rettv->v_type = VAR_DICT;
6500 rettv->vval.v_dict = d;
6501 ++d->dv_refcount;
6502 }
6503
6504 return OK;
6505}
6506
Bram Moolenaar8c711452005-01-14 21:53:12 +00006507/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006508 * Return a string with the string representation of a variable.
6509 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006510 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006511 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006512 * May return NULL;
6513 */
6514 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006515echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006516 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006517 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006518 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006519{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006520 static int recurse = 0;
6521 char_u *r = NULL;
6522
Bram Moolenaar33570922005-01-25 22:26:29 +00006523 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006524 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006525 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006526 *tofree = NULL;
6527 return NULL;
6528 }
6529 ++recurse;
6530
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006531 switch (tv->v_type)
6532 {
6533 case VAR_FUNC:
6534 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006535 r = tv->vval.v_string;
6536 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006537 case VAR_LIST:
6538 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006539 r = *tofree;
6540 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006541 case VAR_DICT:
6542 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006543 r = *tofree;
6544 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006545 case VAR_STRING:
6546 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006547 *tofree = NULL;
6548 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006549 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006550 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006551 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006552 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006553 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006554
6555 --recurse;
6556 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006557}
6558
6559/*
6560 * Return a string with the string representation of a variable.
6561 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6562 * "numbuf" is used for a number.
6563 * Puts quotes around strings, so that they can be parsed back by eval().
6564 * May return NULL;
6565 */
6566 static char_u *
6567tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006568 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006569 char_u **tofree;
6570 char_u *numbuf;
6571{
6572 switch (tv->v_type)
6573 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006574 case VAR_FUNC:
6575 *tofree = string_quote(tv->vval.v_string, TRUE);
6576 return *tofree;
6577 case VAR_STRING:
6578 *tofree = string_quote(tv->vval.v_string, FALSE);
6579 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006580 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006581 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006582 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006583 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006584 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006585 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006587 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006588}
6589
6590/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006591 * Return string "str" in ' quotes, doubling ' characters.
6592 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006593 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006594 */
6595 static char_u *
6596string_quote(str, function)
6597 char_u *str;
6598 int function;
6599{
Bram Moolenaar33570922005-01-25 22:26:29 +00006600 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006601 char_u *p, *r, *s;
6602
Bram Moolenaar33570922005-01-25 22:26:29 +00006603 len = (function ? 13 : 3);
6604 if (str != NULL)
6605 {
6606 len += STRLEN(str);
6607 for (p = str; *p != NUL; mb_ptr_adv(p))
6608 if (*p == '\'')
6609 ++len;
6610 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006611 s = r = alloc(len);
6612 if (r != NULL)
6613 {
6614 if (function)
6615 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006616 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006617 r += 10;
6618 }
6619 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006620 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006621 if (str != NULL)
6622 for (p = str; *p != NUL; )
6623 {
6624 if (*p == '\'')
6625 *r++ = '\'';
6626 MB_COPY_CHAR(p, r);
6627 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006628 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006629 if (function)
6630 *r++ = ')';
6631 *r++ = NUL;
6632 }
6633 return s;
6634}
6635
6636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 * Get the value of an environment variable.
6638 * "arg" is pointing to the '$'. It is advanced to after the name.
6639 * If the environment variable was not set, silently assume it is empty.
6640 * Always return OK.
6641 */
6642 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006643get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 int evaluate;
6647{
6648 char_u *string = NULL;
6649 int len;
6650 int cc;
6651 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006652 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653
6654 ++*arg;
6655 name = *arg;
6656 len = get_env_len(arg);
6657 if (evaluate)
6658 {
6659 if (len != 0)
6660 {
6661 cc = name[len];
6662 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006663 /* first try vim_getenv(), fast for normal environment vars */
6664 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006666 {
6667 if (!mustfree)
6668 string = vim_strsave(string);
6669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 else
6671 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006672 if (mustfree)
6673 vim_free(string);
6674
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 /* next try expanding things like $VIM and ${HOME} */
6676 string = expand_env_save(name - 1);
6677 if (string != NULL && *string == '$')
6678 {
6679 vim_free(string);
6680 string = NULL;
6681 }
6682 }
6683 name[len] = cc;
6684 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006685 rettv->v_type = VAR_STRING;
6686 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 }
6688
6689 return OK;
6690}
6691
6692/*
6693 * Array with names and number of arguments of all internal functions
6694 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6695 */
6696static struct fst
6697{
6698 char *f_name; /* function name */
6699 char f_min_argc; /* minimal number of arguments */
6700 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006701 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006702 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703} functions[] =
6704{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006705 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 {"append", 2, 2, f_append},
6707 {"argc", 0, 0, f_argc},
6708 {"argidx", 0, 0, f_argidx},
6709 {"argv", 1, 1, f_argv},
6710 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006711 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 {"bufexists", 1, 1, f_bufexists},
6713 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6714 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6715 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6716 {"buflisted", 1, 1, f_buflisted},
6717 {"bufloaded", 1, 1, f_bufloaded},
6718 {"bufname", 1, 1, f_bufname},
6719 {"bufnr", 1, 1, f_bufnr},
6720 {"bufwinnr", 1, 1, f_bufwinnr},
6721 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006722 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006723 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 {"char2nr", 1, 1, f_char2nr},
6725 {"cindent", 1, 1, f_cindent},
6726 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00006727#if defined(FEAT_INS_EXPAND)
6728 {"complete_add", 1, 1, f_complete_add},
6729 {"complete_check", 0, 0, f_complete_check},
6730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006732 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006733 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 {"cscope_connection",0,3, f_cscope_connection},
6735 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006736 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 {"delete", 1, 1, f_delete},
6738 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006739 {"diff_filler", 1, 1, f_diff_filler},
6740 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006741 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006743 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 {"eventhandler", 0, 0, f_eventhandler},
6745 {"executable", 1, 1, f_executable},
6746 {"exists", 1, 1, f_exists},
6747 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006748 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6750 {"filereadable", 1, 1, f_filereadable},
6751 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006752 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006753 {"finddir", 1, 3, f_finddir},
6754 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 {"fnamemodify", 2, 2, f_fnamemodify},
6756 {"foldclosed", 1, 1, f_foldclosed},
6757 {"foldclosedend", 1, 1, f_foldclosedend},
6758 {"foldlevel", 1, 1, f_foldlevel},
6759 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006760 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006762 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006763 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006764 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00006765 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 {"getbufvar", 2, 2, f_getbufvar},
6767 {"getchar", 0, 1, f_getchar},
6768 {"getcharmod", 0, 0, f_getcharmod},
6769 {"getcmdline", 0, 0, f_getcmdline},
6770 {"getcmdpos", 0, 0, f_getcmdpos},
6771 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006772 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006773 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 {"getfsize", 1, 1, f_getfsize},
6775 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006776 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006777 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006778 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006779 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 {"getregtype", 0, 1, f_getregtype},
6781 {"getwinposx", 0, 0, f_getwinposx},
6782 {"getwinposy", 0, 0, f_getwinposy},
6783 {"getwinvar", 2, 2, f_getwinvar},
6784 {"glob", 1, 1, f_glob},
6785 {"globpath", 2, 2, f_globpath},
6786 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006787 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 {"hasmapto", 1, 2, f_hasmapto},
6789 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6790 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6791 {"histadd", 2, 2, f_histadd},
6792 {"histdel", 1, 2, f_histdel},
6793 {"histget", 1, 2, f_histget},
6794 {"histnr", 1, 1, f_histnr},
6795 {"hlID", 1, 1, f_hlID},
6796 {"hlexists", 1, 1, f_hlexists},
6797 {"hostname", 0, 0, f_hostname},
6798 {"iconv", 3, 3, f_iconv},
6799 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006800 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 {"input", 1, 2, f_input},
6802 {"inputdialog", 1, 3, f_inputdialog},
6803 {"inputrestore", 0, 0, f_inputrestore},
6804 {"inputsave", 0, 0, f_inputsave},
6805 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006806 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006808 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006809 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006810 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006811 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006813 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 {"libcall", 3, 3, f_libcall},
6815 {"libcallnr", 3, 3, f_libcallnr},
6816 {"line", 1, 1, f_line},
6817 {"line2byte", 1, 1, f_line2byte},
6818 {"lispindent", 1, 1, f_lispindent},
6819 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006820 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 {"maparg", 1, 2, f_maparg},
6822 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006823 {"match", 2, 4, f_match},
6824 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006825 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006826 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006827 {"max", 1, 1, f_max},
6828 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006829#ifdef vim_mkdir
6830 {"mkdir", 1, 3, f_mkdir},
6831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 {"mode", 0, 0, f_mode},
6833 {"nextnonblank", 1, 1, f_nextnonblank},
6834 {"nr2char", 1, 1, f_nr2char},
6835 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00006836 {"printf", 2, 19, f_printf},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006837 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006838 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 {"remote_expr", 2, 3, f_remote_expr},
6840 {"remote_foreground", 1, 1, f_remote_foreground},
6841 {"remote_peek", 1, 2, f_remote_peek},
6842 {"remote_read", 1, 1, f_remote_read},
6843 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006844 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006846 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006848 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 {"search", 1, 2, f_search},
Bram Moolenaardd2436f2005-09-05 22:14:46 +00006850 {"searchdecl", 1, 2, f_searchdecl},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 {"searchpair", 3, 5, f_searchpair},
6852 {"server2client", 2, 2, f_server2client},
6853 {"serverlist", 0, 0, f_serverlist},
6854 {"setbufvar", 3, 3, f_setbufvar},
6855 {"setcmdpos", 1, 1, f_setcmdpos},
6856 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006857 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 {"setreg", 2, 3, f_setreg},
6859 {"setwinvar", 3, 3, f_setwinvar},
6860 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006861 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006862 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006863 {"spellbadword", 0, 0, f_spellbadword},
6864 {"spellsuggest", 1, 2, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006865 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866#ifdef HAVE_STRFTIME
6867 {"strftime", 1, 2, f_strftime},
6868#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006869 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006870 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 {"strlen", 1, 1, f_strlen},
6872 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006873 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 {"strtrans", 1, 1, f_strtrans},
6875 {"submatch", 1, 1, f_submatch},
6876 {"substitute", 4, 4, f_substitute},
6877 {"synID", 3, 3, f_synID},
6878 {"synIDattr", 2, 3, f_synIDattr},
6879 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006880 {"system", 1, 2, f_system},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006881 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006882 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00006884 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 {"tolower", 1, 1, f_tolower},
6886 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006887 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006889 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 {"virtcol", 1, 1, f_virtcol},
6891 {"visualmode", 0, 1, f_visualmode},
6892 {"winbufnr", 1, 1, f_winbufnr},
6893 {"wincol", 0, 0, f_wincol},
6894 {"winheight", 1, 1, f_winheight},
6895 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006896 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 {"winrestcmd", 0, 0, f_winrestcmd},
6898 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006899 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900};
6901
6902#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6903
6904/*
6905 * Function given to ExpandGeneric() to obtain the list of internal
6906 * or user defined function names.
6907 */
6908 char_u *
6909get_function_name(xp, idx)
6910 expand_T *xp;
6911 int idx;
6912{
6913 static int intidx = -1;
6914 char_u *name;
6915
6916 if (idx == 0)
6917 intidx = -1;
6918 if (intidx < 0)
6919 {
6920 name = get_user_func_name(xp, idx);
6921 if (name != NULL)
6922 return name;
6923 }
6924 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6925 {
6926 STRCPY(IObuff, functions[intidx].f_name);
6927 STRCAT(IObuff, "(");
6928 if (functions[intidx].f_max_argc == 0)
6929 STRCAT(IObuff, ")");
6930 return IObuff;
6931 }
6932
6933 return NULL;
6934}
6935
6936/*
6937 * Function given to ExpandGeneric() to obtain the list of internal or
6938 * user defined variable or function names.
6939 */
6940/*ARGSUSED*/
6941 char_u *
6942get_expr_name(xp, idx)
6943 expand_T *xp;
6944 int idx;
6945{
6946 static int intidx = -1;
6947 char_u *name;
6948
6949 if (idx == 0)
6950 intidx = -1;
6951 if (intidx < 0)
6952 {
6953 name = get_function_name(xp, idx);
6954 if (name != NULL)
6955 return name;
6956 }
6957 return get_user_var_name(xp, ++intidx);
6958}
6959
6960#endif /* FEAT_CMDL_COMPL */
6961
6962/*
6963 * Find internal function in table above.
6964 * Return index, or -1 if not found
6965 */
6966 static int
6967find_internal_func(name)
6968 char_u *name; /* name of the function */
6969{
6970 int first = 0;
6971 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6972 int cmp;
6973 int x;
6974
6975 /*
6976 * Find the function name in the table. Binary search.
6977 */
6978 while (first <= last)
6979 {
6980 x = first + ((unsigned)(last - first) >> 1);
6981 cmp = STRCMP(name, functions[x].f_name);
6982 if (cmp < 0)
6983 last = x - 1;
6984 else if (cmp > 0)
6985 first = x + 1;
6986 else
6987 return x;
6988 }
6989 return -1;
6990}
6991
6992/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006993 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6994 * name it contains, otherwise return "name".
6995 */
6996 static char_u *
6997deref_func_name(name, lenp)
6998 char_u *name;
6999 int *lenp;
7000{
Bram Moolenaar33570922005-01-25 22:26:29 +00007001 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007002 int cc;
7003
7004 cc = name[*lenp];
7005 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00007006 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007007 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00007008 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007009 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007010 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007011 {
7012 *lenp = 0;
7013 return (char_u *)""; /* just in case */
7014 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007015 *lenp = STRLEN(v->di_tv.vval.v_string);
7016 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007017 }
7018
7019 return name;
7020}
7021
7022/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 * Allocate a variable for the result of a function.
7024 * Return OK or FAIL.
7025 */
7026 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00007027get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
7028 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 char_u *name; /* name of the function */
7030 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007031 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 char_u **arg; /* argument, pointing to the '(' */
7033 linenr_T firstline; /* first line of range */
7034 linenr_T lastline; /* last line of range */
7035 int *doesrange; /* return: function handled range */
7036 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007037 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038{
7039 char_u *argp;
7040 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00007041 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 int argcount = 0; /* number of arguments found */
7043
7044 /*
7045 * Get the arguments.
7046 */
7047 argp = *arg;
7048 while (argcount < MAX_FUNC_ARGS)
7049 {
7050 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
7051 if (*argp == ')' || *argp == ',' || *argp == NUL)
7052 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
7054 {
7055 ret = FAIL;
7056 break;
7057 }
7058 ++argcount;
7059 if (*argp != ',')
7060 break;
7061 }
7062 if (*argp == ')')
7063 ++argp;
7064 else
7065 ret = FAIL;
7066
7067 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007068 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007069 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00007071 {
7072 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007073 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007074 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007075 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00007076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077
7078 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007079 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080
7081 *arg = skipwhite(argp);
7082 return ret;
7083}
7084
7085
7086/*
7087 * Call a function with its resolved parameters
7088 * Return OK or FAIL.
7089 */
7090 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007091call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007092 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 char_u *name; /* name of the function */
7094 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007095 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007097 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 linenr_T firstline; /* first line of range */
7099 linenr_T lastline; /* last line of range */
7100 int *doesrange; /* return: function handled range */
7101 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007102 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103{
7104 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105#define ERROR_UNKNOWN 0
7106#define ERROR_TOOMANY 1
7107#define ERROR_TOOFEW 2
7108#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007109#define ERROR_DICT 4
7110#define ERROR_NONE 5
7111#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 int error = ERROR_NONE;
7113 int i;
7114 int llen;
7115 ufunc_T *fp;
7116 int cc;
7117#define FLEN_FIXED 40
7118 char_u fname_buf[FLEN_FIXED + 1];
7119 char_u *fname;
7120
7121 /*
7122 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7123 * Change <SNR>123_name() to K_SNR 123_name().
7124 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7125 */
7126 cc = name[len];
7127 name[len] = NUL;
7128 llen = eval_fname_script(name);
7129 if (llen > 0)
7130 {
7131 fname_buf[0] = K_SPECIAL;
7132 fname_buf[1] = KS_EXTRA;
7133 fname_buf[2] = (int)KE_SNR;
7134 i = 3;
7135 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7136 {
7137 if (current_SID <= 0)
7138 error = ERROR_SCRIPT;
7139 else
7140 {
7141 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7142 i = (int)STRLEN(fname_buf);
7143 }
7144 }
7145 if (i + STRLEN(name + llen) < FLEN_FIXED)
7146 {
7147 STRCPY(fname_buf + i, name + llen);
7148 fname = fname_buf;
7149 }
7150 else
7151 {
7152 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7153 if (fname == NULL)
7154 error = ERROR_OTHER;
7155 else
7156 {
7157 mch_memmove(fname, fname_buf, (size_t)i);
7158 STRCPY(fname + i, name + llen);
7159 }
7160 }
7161 }
7162 else
7163 fname = name;
7164
7165 *doesrange = FALSE;
7166
7167
7168 /* execute the function if no errors detected and executing */
7169 if (evaluate && error == ERROR_NONE)
7170 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007171 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 error = ERROR_UNKNOWN;
7173
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007174 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 {
7176 /*
7177 * User defined function.
7178 */
7179 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007180
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007182 /* Trigger FuncUndefined event, may load the function. */
7183 if (fp == NULL
7184 && apply_autocmds(EVENT_FUNCUNDEFINED,
7185 fname, fname, TRUE, NULL)
7186 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007188 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 fp = find_func(fname);
7190 }
7191#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007192 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007193 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007194 {
7195 /* loaded a package, search for the function again */
7196 fp = find_func(fname);
7197 }
7198
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 if (fp != NULL)
7200 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007201 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007203 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007205 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007207 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007208 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 else
7210 {
7211 /*
7212 * Call the user function.
7213 * Save and restore search patterns, script variables and
7214 * redo buffer.
7215 */
7216 save_search_patterns();
7217 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007218 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007219 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007220 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007221 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7222 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7223 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007224 /* Function was unreferenced while being used, free it
7225 * now. */
7226 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 restoreRedobuff();
7228 restore_search_patterns();
7229 error = ERROR_NONE;
7230 }
7231 }
7232 }
7233 else
7234 {
7235 /*
7236 * Find the function name in the table, call its implementation.
7237 */
7238 i = find_internal_func(fname);
7239 if (i >= 0)
7240 {
7241 if (argcount < functions[i].f_min_argc)
7242 error = ERROR_TOOFEW;
7243 else if (argcount > functions[i].f_max_argc)
7244 error = ERROR_TOOMANY;
7245 else
7246 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007247 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007248 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 error = ERROR_NONE;
7250 }
7251 }
7252 }
7253 /*
7254 * The function call (or "FuncUndefined" autocommand sequence) might
7255 * have been aborted by an error, an interrupt, or an explicitly thrown
7256 * exception that has not been caught so far. This situation can be
7257 * tested for by calling aborting(). For an error in an internal
7258 * function or for the "E132" error in call_user_func(), however, the
7259 * throw point at which the "force_abort" flag (temporarily reset by
7260 * emsg()) is normally updated has not been reached yet. We need to
7261 * update that flag first to make aborting() reliable.
7262 */
7263 update_force_abort();
7264 }
7265 if (error == ERROR_NONE)
7266 ret = OK;
7267
7268 /*
7269 * Report an error unless the argument evaluation or function call has been
7270 * cancelled due to an aborting error, an interrupt, or an exception.
7271 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272 if (!aborting())
7273 {
7274 switch (error)
7275 {
7276 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007277 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007278 break;
7279 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007280 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007281 break;
7282 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007283 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007284 name);
7285 break;
7286 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007287 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288 name);
7289 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007290 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007291 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007292 name);
7293 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007294 }
7295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296
7297 name[len] = cc;
7298 if (fname != name && fname != fname_buf)
7299 vim_free(fname);
7300
7301 return ret;
7302}
7303
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007304/*
7305 * Give an error message with a function name. Handle <SNR> things.
7306 */
7307 static void
7308emsg_funcname(msg, name)
7309 char *msg;
7310 char_u *name;
7311{
7312 char_u *p;
7313
7314 if (*name == K_SPECIAL)
7315 p = concat_str((char_u *)"<SNR>", name + 3);
7316 else
7317 p = name;
7318 EMSG2(_(msg), p);
7319 if (p != name)
7320 vim_free(p);
7321}
7322
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323/*********************************************
7324 * Implementation of the built-in functions
7325 */
7326
7327/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007328 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 */
7330 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007331f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 typval_T *argvars;
7333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334{
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007337 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007338 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007340 if ((l = argvars[0].vval.v_list) != NULL
7341 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7342 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007343 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007344 }
7345 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007346 EMSG(_(e_listreq));
7347}
7348
7349/*
7350 * "append(lnum, string/list)" function
7351 */
7352 static void
7353f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007354 typval_T *argvars;
7355 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007356{
7357 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007358 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 list_T *l = NULL;
7360 listitem_T *li = NULL;
7361 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007362 long added = 0;
7363
Bram Moolenaar0d660222005-01-07 21:51:51 +00007364 lnum = get_tv_lnum(argvars);
7365 if (lnum >= 0
7366 && lnum <= curbuf->b_ml.ml_line_count
7367 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007368 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007369 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007370 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007371 l = argvars[1].vval.v_list;
7372 if (l == NULL)
7373 return;
7374 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007375 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007376 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007377 for (;;)
7378 {
7379 if (l == NULL)
7380 tv = &argvars[1]; /* append a string */
7381 else if (li == NULL)
7382 break; /* end of list */
7383 else
7384 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007385 line = get_tv_string_chk(tv);
7386 if (line == NULL) /* type error */
7387 {
7388 rettv->vval.v_number = 1; /* Failed */
7389 break;
7390 }
7391 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007392 ++added;
7393 if (l == NULL)
7394 break;
7395 li = li->li_next;
7396 }
7397
7398 appended_lines_mark(lnum, added);
7399 if (curwin->w_cursor.lnum > lnum)
7400 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007402 else
7403 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404}
7405
7406/*
7407 * "argc()" function
7408 */
7409/* ARGSUSED */
7410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007411f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007412 typval_T *argvars;
7413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007415 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416}
7417
7418/*
7419 * "argidx()" function
7420 */
7421/* ARGSUSED */
7422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007423f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007424 typval_T *argvars;
7425 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007427 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428}
7429
7430/*
7431 * "argv(nr)" function
7432 */
7433 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007434f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007435 typval_T *argvars;
7436 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437{
7438 int idx;
7439
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007440 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007442 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007444 rettv->vval.v_string = NULL;
7445 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446}
7447
7448/*
7449 * "browse(save, title, initdir, default)" function
7450 */
7451/* ARGSUSED */
7452 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007453f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007454 typval_T *argvars;
7455 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456{
7457#ifdef FEAT_BROWSE
7458 int save;
7459 char_u *title;
7460 char_u *initdir;
7461 char_u *defname;
7462 char_u buf[NUMBUFLEN];
7463 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007464 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007466 save = get_tv_number_chk(&argvars[0], &error);
7467 title = get_tv_string_chk(&argvars[1]);
7468 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7469 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007471 if (error || title == NULL || initdir == NULL || defname == NULL)
7472 rettv->vval.v_string = NULL;
7473 else
7474 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007475 do_browse(save ? BROWSE_SAVE : 0,
7476 title, defname, NULL, initdir, NULL, curbuf);
7477#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007478 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007479#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007480 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007481}
7482
7483/*
7484 * "browsedir(title, initdir)" function
7485 */
7486/* ARGSUSED */
7487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007488f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007489 typval_T *argvars;
7490 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007491{
7492#ifdef FEAT_BROWSE
7493 char_u *title;
7494 char_u *initdir;
7495 char_u buf[NUMBUFLEN];
7496
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007497 title = get_tv_string_chk(&argvars[0]);
7498 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007499
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007500 if (title == NULL || initdir == NULL)
7501 rettv->vval.v_string = NULL;
7502 else
7503 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007504 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007506 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007508 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509}
7510
Bram Moolenaar33570922005-01-25 22:26:29 +00007511static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007512
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513/*
7514 * Find a buffer by number or exact name.
7515 */
7516 static buf_T *
7517find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007518 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519{
7520 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007522 if (avar->v_type == VAR_NUMBER)
7523 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007524 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007526 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007527 if (buf == NULL)
7528 {
7529 /* No full path name match, try a match with a URL or a "nofile"
7530 * buffer, these don't use the full path. */
7531 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7532 if (buf->b_fname != NULL
7533 && (path_with_url(buf->b_fname)
7534#ifdef FEAT_QUICKFIX
7535 || bt_nofile(buf)
7536#endif
7537 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007538 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007539 break;
7540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 }
7542 return buf;
7543}
7544
7545/*
7546 * "bufexists(expr)" function
7547 */
7548 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007549f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007550 typval_T *argvars;
7551 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007553 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554}
7555
7556/*
7557 * "buflisted(expr)" function
7558 */
7559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007560f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007561 typval_T *argvars;
7562 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563{
7564 buf_T *buf;
7565
7566 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007567 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568}
7569
7570/*
7571 * "bufloaded(expr)" function
7572 */
7573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007574f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007575 typval_T *argvars;
7576 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577{
7578 buf_T *buf;
7579
7580 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007581 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582}
7583
Bram Moolenaar33570922005-01-25 22:26:29 +00007584static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007585
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586/*
7587 * Get buffer by number or pattern.
7588 */
7589 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007590get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007593 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 int save_magic;
7595 char_u *save_cpo;
7596 buf_T *buf;
7597
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007598 if (tv->v_type == VAR_NUMBER)
7599 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007600 if (tv->v_type != VAR_STRING)
7601 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 if (name == NULL || *name == NUL)
7603 return curbuf;
7604 if (name[0] == '$' && name[1] == NUL)
7605 return lastbuf;
7606
7607 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7608 save_magic = p_magic;
7609 p_magic = TRUE;
7610 save_cpo = p_cpo;
7611 p_cpo = (char_u *)"";
7612
7613 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7614 TRUE, FALSE));
7615
7616 p_magic = save_magic;
7617 p_cpo = save_cpo;
7618
7619 /* If not found, try expanding the name, like done for bufexists(). */
7620 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007621 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622
7623 return buf;
7624}
7625
7626/*
7627 * "bufname(expr)" function
7628 */
7629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007630f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007631 typval_T *argvars;
7632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633{
7634 buf_T *buf;
7635
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007636 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007638 buf = get_buf_tv(&argvars[0]);
7639 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007641 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007643 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 --emsg_off;
7645}
7646
7647/*
7648 * "bufnr(expr)" function
7649 */
7650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007651f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007652 typval_T *argvars;
7653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654{
7655 buf_T *buf;
7656
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007657 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007659 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007661 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007663 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 --emsg_off;
7665}
7666
7667/*
7668 * "bufwinnr(nr)" function
7669 */
7670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007671f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007672 typval_T *argvars;
7673 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674{
7675#ifdef FEAT_WINDOWS
7676 win_T *wp;
7677 int winnr = 0;
7678#endif
7679 buf_T *buf;
7680
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007681 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007683 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684#ifdef FEAT_WINDOWS
7685 for (wp = firstwin; wp; wp = wp->w_next)
7686 {
7687 ++winnr;
7688 if (wp->w_buffer == buf)
7689 break;
7690 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007691 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007693 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694#endif
7695 --emsg_off;
7696}
7697
7698/*
7699 * "byte2line(byte)" function
7700 */
7701/*ARGSUSED*/
7702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007703f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007704 typval_T *argvars;
7705 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706{
7707#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007708 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709#else
7710 long boff = 0;
7711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007712 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007714 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007716 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 (linenr_T)0, &boff);
7718#endif
7719}
7720
7721/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007722 * "byteidx()" function
7723 */
7724/*ARGSUSED*/
7725 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007726f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007727 typval_T *argvars;
7728 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007729{
7730#ifdef FEAT_MBYTE
7731 char_u *t;
7732#endif
7733 char_u *str;
7734 long idx;
7735
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007736 str = get_tv_string_chk(&argvars[0]);
7737 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007738 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007739 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007740 return;
7741
7742#ifdef FEAT_MBYTE
7743 t = str;
7744 for ( ; idx > 0; idx--)
7745 {
7746 if (*t == NUL) /* EOL reached */
7747 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007748 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007749 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007750 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007751#else
7752 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007753 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007754#endif
7755}
7756
7757/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007758 * "call(func, arglist)" function
7759 */
7760 static void
7761f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007762 typval_T *argvars;
7763 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007764{
7765 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007766 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007767 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007768 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007769 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007770 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007771
7772 rettv->vval.v_number = 0;
7773 if (argvars[1].v_type != VAR_LIST)
7774 {
7775 EMSG(_(e_listreq));
7776 return;
7777 }
7778 if (argvars[1].vval.v_list == NULL)
7779 return;
7780
7781 if (argvars[0].v_type == VAR_FUNC)
7782 func = argvars[0].vval.v_string;
7783 else
7784 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007785 if (*func == NUL)
7786 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007787
Bram Moolenaare9a41262005-01-15 22:18:47 +00007788 if (argvars[2].v_type != VAR_UNKNOWN)
7789 {
7790 if (argvars[2].v_type != VAR_DICT)
7791 {
7792 EMSG(_(e_dictreq));
7793 return;
7794 }
7795 selfdict = argvars[2].vval.v_dict;
7796 }
7797
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007798 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7799 item = item->li_next)
7800 {
7801 if (argc == MAX_FUNC_ARGS)
7802 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007803 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007804 break;
7805 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007806 /* Make a copy of each argument. This is needed to be able to set
7807 * v_lock to VAR_FIXED in the copy without changing the original list.
7808 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007809 copy_tv(&item->li_tv, &argv[argc++]);
7810 }
7811
7812 if (item == NULL)
7813 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007814 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7815 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007816
7817 /* Free the arguments. */
7818 while (argc > 0)
7819 clear_tv(&argv[--argc]);
7820}
7821
7822/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 * "char2nr(string)" function
7824 */
7825 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007826f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007827 typval_T *argvars;
7828 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829{
7830#ifdef FEAT_MBYTE
7831 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007832 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 else
7834#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007835 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836}
7837
7838/*
7839 * "cindent(lnum)" function
7840 */
7841 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007842f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007843 typval_T *argvars;
7844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845{
7846#ifdef FEAT_CINDENT
7847 pos_T pos;
7848 linenr_T lnum;
7849
7850 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007851 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7853 {
7854 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007855 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 curwin->w_cursor = pos;
7857 }
7858 else
7859#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007860 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861}
7862
7863/*
7864 * "col(string)" function
7865 */
7866 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007867f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007868 typval_T *argvars;
7869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870{
7871 colnr_T col = 0;
7872 pos_T *fp;
7873
7874 fp = var2fpos(&argvars[0], FALSE);
7875 if (fp != NULL)
7876 {
7877 if (fp->col == MAXCOL)
7878 {
7879 /* '> can be MAXCOL, get the length of the line then */
7880 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7881 col = STRLEN(ml_get(fp->lnum)) + 1;
7882 else
7883 col = MAXCOL;
7884 }
7885 else
7886 {
7887 col = fp->col + 1;
7888#ifdef FEAT_VIRTUALEDIT
7889 /* col(".") when the cursor is on the NUL at the end of the line
7890 * because of "coladd" can be seen as an extra column. */
7891 if (virtual_active() && fp == &curwin->w_cursor)
7892 {
7893 char_u *p = ml_get_cursor();
7894
7895 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7896 curwin->w_virtcol - curwin->w_cursor.coladd))
7897 {
7898# ifdef FEAT_MBYTE
7899 int l;
7900
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007901 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 col += l;
7903# else
7904 if (*p != NUL && p[1] == NUL)
7905 ++col;
7906# endif
7907 }
7908 }
7909#endif
7910 }
7911 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007912 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913}
7914
Bram Moolenaar572cb562005-08-05 21:35:02 +00007915#if defined(FEAT_INS_EXPAND)
7916/*
7917 * "complete_add()" function
7918 */
7919/*ARGSUSED*/
7920 static void
7921f_complete_add(argvars, rettv)
7922 typval_T *argvars;
7923 typval_T *rettv;
7924{
7925 char_u *s;
7926
7927 s = get_tv_string_chk(&argvars[0]);
7928 if (s != NULL)
7929 rettv->vval.v_number = ins_compl_add(s, -1, NULL, FORWARD, 0);
7930}
7931
7932/*
7933 * "complete_check()" function
7934 */
7935/*ARGSUSED*/
7936 static void
7937f_complete_check(argvars, rettv)
7938 typval_T *argvars;
7939 typval_T *rettv;
7940{
7941 int saved = RedrawingDisabled;
7942
7943 RedrawingDisabled = 0;
7944 ins_compl_check_keys(0);
7945 rettv->vval.v_number = compl_interrupted;
7946 RedrawingDisabled = saved;
7947}
7948#endif
7949
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950/*
7951 * "confirm(message, buttons[, default [, type]])" function
7952 */
7953/*ARGSUSED*/
7954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007955f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007956 typval_T *argvars;
7957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958{
7959#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7960 char_u *message;
7961 char_u *buttons = NULL;
7962 char_u buf[NUMBUFLEN];
7963 char_u buf2[NUMBUFLEN];
7964 int def = 1;
7965 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007966 char_u *typestr;
7967 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007969 message = get_tv_string_chk(&argvars[0]);
7970 if (message == NULL)
7971 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007972 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007974 buttons = get_tv_string_buf_chk(&argvars[1], buf);
7975 if (buttons == NULL)
7976 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007977 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007979 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007980 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007982 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
7983 if (typestr == NULL)
7984 error = TRUE;
7985 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007987 switch (TOUPPER_ASC(*typestr))
7988 {
7989 case 'E': type = VIM_ERROR; break;
7990 case 'Q': type = VIM_QUESTION; break;
7991 case 'I': type = VIM_INFO; break;
7992 case 'W': type = VIM_WARNING; break;
7993 case 'G': type = VIM_GENERIC; break;
7994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 }
7996 }
7997 }
7998 }
7999
8000 if (buttons == NULL || *buttons == NUL)
8001 buttons = (char_u *)_("&Ok");
8002
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008003 if (error)
8004 rettv->vval.v_number = 0;
8005 else
8006 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 def, NULL);
8008#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008009 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010#endif
8011}
8012
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008013/*
8014 * "copy()" function
8015 */
8016 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008017f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008018 typval_T *argvars;
8019 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008020{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008021 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008022}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023
8024/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008025 * "count()" function
8026 */
8027 static void
8028f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008029 typval_T *argvars;
8030 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008031{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008032 long n = 0;
8033 int ic = FALSE;
8034
Bram Moolenaare9a41262005-01-15 22:18:47 +00008035 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008036 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008037 listitem_T *li;
8038 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008039 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008040
Bram Moolenaare9a41262005-01-15 22:18:47 +00008041 if ((l = argvars[0].vval.v_list) != NULL)
8042 {
8043 li = l->lv_first;
8044 if (argvars[2].v_type != VAR_UNKNOWN)
8045 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008046 int error = FALSE;
8047
8048 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008049 if (argvars[3].v_type != VAR_UNKNOWN)
8050 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008051 idx = get_tv_number_chk(&argvars[3], &error);
8052 if (!error)
8053 {
8054 li = list_find(l, idx);
8055 if (li == NULL)
8056 EMSGN(_(e_listidx), idx);
8057 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008058 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008059 if (error)
8060 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008061 }
8062
8063 for ( ; li != NULL; li = li->li_next)
8064 if (tv_equal(&li->li_tv, &argvars[1], ic))
8065 ++n;
8066 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008067 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008068 else if (argvars[0].v_type == VAR_DICT)
8069 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008070 int todo;
8071 dict_T *d;
8072 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008073
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008074 if ((d = argvars[0].vval.v_dict) != NULL)
8075 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008076 int error = FALSE;
8077
Bram Moolenaare9a41262005-01-15 22:18:47 +00008078 if (argvars[2].v_type != VAR_UNKNOWN)
8079 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008080 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008081 if (argvars[3].v_type != VAR_UNKNOWN)
8082 EMSG(_(e_invarg));
8083 }
8084
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008085 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00008086 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008087 {
8088 if (!HASHITEM_EMPTY(hi))
8089 {
8090 --todo;
8091 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
8092 ++n;
8093 }
8094 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008095 }
8096 }
8097 else
8098 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008099 rettv->vval.v_number = n;
8100}
8101
8102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
8104 *
8105 * Checks the existence of a cscope connection.
8106 */
8107/*ARGSUSED*/
8108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008109f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008110 typval_T *argvars;
8111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112{
8113#ifdef FEAT_CSCOPE
8114 int num = 0;
8115 char_u *dbpath = NULL;
8116 char_u *prepend = NULL;
8117 char_u buf[NUMBUFLEN];
8118
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008119 if (argvars[0].v_type != VAR_UNKNOWN
8120 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008122 num = (int)get_tv_number(&argvars[0]);
8123 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008124 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008125 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 }
8127
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008128 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008130 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131#endif
8132}
8133
8134/*
8135 * "cursor(lnum, col)" function
8136 *
8137 * Moves the cursor to the specified line and column
8138 */
8139/*ARGSUSED*/
8140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008141f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008142 typval_T *argvars;
8143 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144{
8145 long line, col;
8146
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008147 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008148 col = get_tv_number_chk(&argvars[1], NULL);
8149 if (line < 0 || col < 0)
8150 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 if (line > 0)
8152 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 if (col > 0)
8154 curwin->w_cursor.col = col - 1;
8155#ifdef FEAT_VIRTUALEDIT
8156 curwin->w_cursor.coladd = 0;
8157#endif
8158
8159 /* Make sure the cursor is in a valid position. */
8160 check_cursor();
8161#ifdef FEAT_MBYTE
8162 /* Correct cursor for multi-byte character. */
8163 if (has_mbyte)
8164 mb_adjust_cursor();
8165#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008166
8167 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168}
8169
8170/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008171 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 */
8173 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008174f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008175 typval_T *argvars;
8176 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008178 int noref = 0;
8179
8180 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008181 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008182 if (noref < 0 || noref > 1)
8183 EMSG(_(e_invarg));
8184 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008185 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186}
8187
8188/*
8189 * "delete()" function
8190 */
8191 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008192f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008193 typval_T *argvars;
8194 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195{
8196 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008197 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008199 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200}
8201
8202/*
8203 * "did_filetype()" function
8204 */
8205/*ARGSUSED*/
8206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008207f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008208 typval_T *argvars;
8209 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210{
8211#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008212 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008214 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215#endif
8216}
8217
8218/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008219 * "diff_filler()" function
8220 */
8221/*ARGSUSED*/
8222 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008223f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008224 typval_T *argvars;
8225 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008226{
8227#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008228 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008229#endif
8230}
8231
8232/*
8233 * "diff_hlID()" function
8234 */
8235/*ARGSUSED*/
8236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008237f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008238 typval_T *argvars;
8239 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008240{
8241#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008242 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008243 static linenr_T prev_lnum = 0;
8244 static int changedtick = 0;
8245 static int fnum = 0;
8246 static int change_start = 0;
8247 static int change_end = 0;
8248 static enum hlf_value hlID = 0;
8249 int filler_lines;
8250 int col;
8251
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008252 if (lnum < 0) /* ignore type error in {lnum} arg */
8253 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008254 if (lnum != prev_lnum
8255 || changedtick != curbuf->b_changedtick
8256 || fnum != curbuf->b_fnum)
8257 {
8258 /* New line, buffer, change: need to get the values. */
8259 filler_lines = diff_check(curwin, lnum);
8260 if (filler_lines < 0)
8261 {
8262 if (filler_lines == -1)
8263 {
8264 change_start = MAXCOL;
8265 change_end = -1;
8266 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8267 hlID = HLF_ADD; /* added line */
8268 else
8269 hlID = HLF_CHD; /* changed line */
8270 }
8271 else
8272 hlID = HLF_ADD; /* added line */
8273 }
8274 else
8275 hlID = (enum hlf_value)0;
8276 prev_lnum = lnum;
8277 changedtick = curbuf->b_changedtick;
8278 fnum = curbuf->b_fnum;
8279 }
8280
8281 if (hlID == HLF_CHD || hlID == HLF_TXD)
8282 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008283 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008284 if (col >= change_start && col <= change_end)
8285 hlID = HLF_TXD; /* changed text */
8286 else
8287 hlID = HLF_CHD; /* changed line */
8288 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008289 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008290#endif
8291}
8292
8293/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008294 * "empty({expr})" function
8295 */
8296 static void
8297f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008298 typval_T *argvars;
8299 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008300{
8301 int n;
8302
8303 switch (argvars[0].v_type)
8304 {
8305 case VAR_STRING:
8306 case VAR_FUNC:
8307 n = argvars[0].vval.v_string == NULL
8308 || *argvars[0].vval.v_string == NUL;
8309 break;
8310 case VAR_NUMBER:
8311 n = argvars[0].vval.v_number == 0;
8312 break;
8313 case VAR_LIST:
8314 n = argvars[0].vval.v_list == NULL
8315 || argvars[0].vval.v_list->lv_first == NULL;
8316 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008317 case VAR_DICT:
8318 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008319 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008320 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008321 default:
8322 EMSG2(_(e_intern2), "f_empty()");
8323 n = 0;
8324 }
8325
8326 rettv->vval.v_number = n;
8327}
8328
8329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 * "escape({string}, {chars})" function
8331 */
8332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008333f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008334 typval_T *argvars;
8335 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336{
8337 char_u buf[NUMBUFLEN];
8338
Bram Moolenaar758711c2005-02-02 23:11:38 +00008339 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8340 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008341 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342}
8343
8344/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008345 * "eval()" function
8346 */
8347/*ARGSUSED*/
8348 static void
8349f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008350 typval_T *argvars;
8351 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008352{
8353 char_u *s;
8354
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008355 s = get_tv_string_chk(&argvars[0]);
8356 if (s != NULL)
8357 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008358
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008359 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8360 {
8361 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008362 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008363 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008364 else if (*s != NUL)
8365 EMSG(_(e_trailing));
8366}
8367
8368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 * "eventhandler()" function
8370 */
8371/*ARGSUSED*/
8372 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008373f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008374 typval_T *argvars;
8375 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008377 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378}
8379
8380/*
8381 * "executable()" function
8382 */
8383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008384f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008385 typval_T *argvars;
8386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008388 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389}
8390
8391/*
8392 * "exists()" function
8393 */
8394 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008395f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008396 typval_T *argvars;
8397 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398{
8399 char_u *p;
8400 char_u *name;
8401 int n = FALSE;
8402 int len = 0;
8403
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008404 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 if (*p == '$') /* environment variable */
8406 {
8407 /* first try "normal" environment variables (fast) */
8408 if (mch_getenv(p + 1) != NULL)
8409 n = TRUE;
8410 else
8411 {
8412 /* try expanding things like $VIM and ${HOME} */
8413 p = expand_env_save(p);
8414 if (p != NULL && *p != '$')
8415 n = TRUE;
8416 vim_free(p);
8417 }
8418 }
8419 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008420 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 else if (*p == '*') /* internal or user defined function */
8422 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008423 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 }
8425 else if (*p == ':')
8426 {
8427 n = cmd_exists(p + 1);
8428 }
8429 else if (*p == '#')
8430 {
8431#ifdef FEAT_AUTOCMD
8432 name = p + 1;
8433 p = vim_strchr(name, '#');
8434 if (p != NULL)
8435 n = au_exists(name, p, p + 1);
8436 else
8437 n = au_exists(name, name + STRLEN(name), NULL);
8438#endif
8439 }
8440 else /* internal variable */
8441 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008442 char_u *tofree;
8443 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008445 /* get_name_len() takes care of expanding curly braces */
8446 name = p;
8447 len = get_name_len(&p, &tofree, TRUE, FALSE);
8448 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008450 if (tofree != NULL)
8451 name = tofree;
8452 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8453 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008455 /* handle d.key, l[idx], f(expr) */
8456 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8457 if (n)
8458 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 }
8460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008462 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 }
8464
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008465 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466}
8467
8468/*
8469 * "expand()" function
8470 */
8471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008472f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008473 typval_T *argvars;
8474 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475{
8476 char_u *s;
8477 int len;
8478 char_u *errormsg;
8479 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8480 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008481 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008483 rettv->v_type = VAR_STRING;
8484 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 if (*s == '%' || *s == '#' || *s == '<')
8486 {
8487 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008488 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 --emsg_off;
8490 }
8491 else
8492 {
8493 /* When the optional second argument is non-zero, don't remove matches
8494 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008495 if (argvars[1].v_type != VAR_UNKNOWN
8496 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008498 if (!error)
8499 {
8500 ExpandInit(&xpc);
8501 xpc.xp_context = EXPAND_FILES;
8502 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8503 ExpandCleanup(&xpc);
8504 }
8505 else
8506 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 }
8508}
8509
8510/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008511 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008512 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008513 */
8514 static void
8515f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008516 typval_T *argvars;
8517 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008518{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008519 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008520 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008521 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008522 list_T *l1, *l2;
8523 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008524 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008525 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008526
Bram Moolenaare9a41262005-01-15 22:18:47 +00008527 l1 = argvars[0].vval.v_list;
8528 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008529 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8530 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008531 {
8532 if (argvars[2].v_type != VAR_UNKNOWN)
8533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008534 before = get_tv_number_chk(&argvars[2], &error);
8535 if (error)
8536 return; /* type error; errmsg already given */
8537
Bram Moolenaar758711c2005-02-02 23:11:38 +00008538 if (before == l1->lv_len)
8539 item = NULL;
8540 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008541 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008542 item = list_find(l1, before);
8543 if (item == NULL)
8544 {
8545 EMSGN(_(e_listidx), before);
8546 return;
8547 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008548 }
8549 }
8550 else
8551 item = NULL;
8552 list_extend(l1, l2, item);
8553
Bram Moolenaare9a41262005-01-15 22:18:47 +00008554 copy_tv(&argvars[0], rettv);
8555 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008556 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008557 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8558 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008559 dict_T *d1, *d2;
8560 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008561 char_u *action;
8562 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008563 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008564 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008565
8566 d1 = argvars[0].vval.v_dict;
8567 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008568 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8569 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008570 {
8571 /* Check the third argument. */
8572 if (argvars[2].v_type != VAR_UNKNOWN)
8573 {
8574 static char *(av[]) = {"keep", "force", "error"};
8575
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008576 action = get_tv_string_chk(&argvars[2]);
8577 if (action == NULL)
8578 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008579 for (i = 0; i < 3; ++i)
8580 if (STRCMP(action, av[i]) == 0)
8581 break;
8582 if (i == 3)
8583 {
8584 EMSGN(_(e_invarg2), action);
8585 return;
8586 }
8587 }
8588 else
8589 action = (char_u *)"force";
8590
8591 /* Go over all entries in the second dict and add them to the
8592 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008593 todo = d2->dv_hashtab.ht_used;
8594 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008595 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008596 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008597 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008598 --todo;
8599 di1 = dict_find(d1, hi2->hi_key, -1);
8600 if (di1 == NULL)
8601 {
8602 di1 = dictitem_copy(HI2DI(hi2));
8603 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8604 dictitem_free(di1);
8605 }
8606 else if (*action == 'e')
8607 {
8608 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8609 break;
8610 }
8611 else if (*action == 'f')
8612 {
8613 clear_tv(&di1->di_tv);
8614 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8615 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008616 }
8617 }
8618
Bram Moolenaare9a41262005-01-15 22:18:47 +00008619 copy_tv(&argvars[0], rettv);
8620 }
8621 }
8622 else
8623 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008624}
8625
8626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 * "filereadable()" function
8628 */
8629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008630f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008631 typval_T *argvars;
8632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633{
8634 FILE *fd;
8635 char_u *p;
8636 int n;
8637
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008638 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8640 {
8641 n = TRUE;
8642 fclose(fd);
8643 }
8644 else
8645 n = FALSE;
8646
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008647 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648}
8649
8650/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008651 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 * rights to write into.
8653 */
8654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008655f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008656 typval_T *argvars;
8657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008659 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660}
8661
Bram Moolenaar33570922005-01-25 22:26:29 +00008662static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008663
8664 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008665findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008666 typval_T *argvars;
8667 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008668 int dir;
8669{
8670#ifdef FEAT_SEARCHPATH
8671 char_u *fname;
8672 char_u *fresult = NULL;
8673 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8674 char_u *p;
8675 char_u pathbuf[NUMBUFLEN];
8676 int count = 1;
8677 int first = TRUE;
8678
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008679 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008680
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008681 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008682 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008683 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8684 if (p == NULL)
8685 count = -1; /* error */
8686 else
8687 {
8688 if (*p != NUL)
8689 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008690
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008691 if (argvars[2].v_type != VAR_UNKNOWN)
8692 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8693 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008694 }
8695
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008696 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008698 do
8699 {
8700 vim_free(fresult);
8701 fresult = find_file_in_path_option(first ? fname : NULL,
8702 first ? (int)STRLEN(fname) : 0,
8703 0, first, path, dir, NULL);
8704 first = FALSE;
8705 } while (--count > 0 && fresult != NULL);
8706 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008707
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008708 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008709#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008710 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008711#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008712 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008713}
8714
Bram Moolenaar33570922005-01-25 22:26:29 +00008715static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8716static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008717
8718/*
8719 * Implementation of map() and filter().
8720 */
8721 static void
8722filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008723 typval_T *argvars;
8724 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008725 int map;
8726{
8727 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008728 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008729 listitem_T *li, *nli;
8730 list_T *l = NULL;
8731 dictitem_T *di;
8732 hashtab_T *ht;
8733 hashitem_T *hi;
8734 dict_T *d = NULL;
8735 typval_T save_val;
8736 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008737 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008738 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008739 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8740
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008741
8742 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008743 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008744 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008745 if ((l = argvars[0].vval.v_list) == NULL
8746 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008747 return;
8748 }
8749 else if (argvars[0].v_type == VAR_DICT)
8750 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008751 if ((d = argvars[0].vval.v_dict) == NULL
8752 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008753 return;
8754 }
8755 else
8756 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008757 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008758 return;
8759 }
8760
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008761 expr = get_tv_string_buf_chk(&argvars[1], buf);
8762 /* On type errors, the preceding call has already displayed an error
8763 * message. Avoid a misleading error message for an empty string that
8764 * was not passed as argument. */
8765 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008766 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008767 prepare_vimvar(VV_VAL, &save_val);
8768 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008769
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008770 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008771 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008772 prepare_vimvar(VV_KEY, &save_key);
8773 vimvars[VV_KEY].vv_type = VAR_STRING;
8774
8775 ht = &d->dv_hashtab;
8776 hash_lock(ht);
8777 todo = ht->ht_used;
8778 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008780 if (!HASHITEM_EMPTY(hi))
8781 {
8782 --todo;
8783 di = HI2DI(hi);
8784 if (tv_check_lock(di->di_tv.v_lock, msg))
8785 break;
8786 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8787 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8788 break;
8789 if (!map && rem)
8790 dictitem_remove(d, di);
8791 clear_tv(&vimvars[VV_KEY].vv_tv);
8792 }
8793 }
8794 hash_unlock(ht);
8795
8796 restore_vimvar(VV_KEY, &save_key);
8797 }
8798 else
8799 {
8800 for (li = l->lv_first; li != NULL; li = nli)
8801 {
8802 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008803 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008804 nli = li->li_next;
8805 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008806 break;
8807 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008808 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008809 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008810 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008811
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008812 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008813 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008814
8815 copy_tv(&argvars[0], rettv);
8816}
8817
8818 static int
8819filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008820 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008821 char_u *expr;
8822 int map;
8823 int *remp;
8824{
Bram Moolenaar33570922005-01-25 22:26:29 +00008825 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008826 char_u *s;
8827
Bram Moolenaar33570922005-01-25 22:26:29 +00008828 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008829 s = expr;
8830 if (eval1(&s, &rettv, TRUE) == FAIL)
8831 return FAIL;
8832 if (*s != NUL) /* check for trailing chars after expr */
8833 {
8834 EMSG2(_(e_invexpr2), s);
8835 return FAIL;
8836 }
8837 if (map)
8838 {
8839 /* map(): replace the list item value */
8840 clear_tv(tv);
8841 *tv = rettv;
8842 }
8843 else
8844 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008845 int error = FALSE;
8846
Bram Moolenaare9a41262005-01-15 22:18:47 +00008847 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008848 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008849 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008850 /* On type error, nothing has been removed; return FAIL to stop the
8851 * loop. The error message was given by get_tv_number_chk(). */
8852 if (error)
8853 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008854 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008855 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008856 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008857}
8858
8859/*
8860 * "filter()" function
8861 */
8862 static void
8863f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008864 typval_T *argvars;
8865 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008866{
8867 filter_map(argvars, rettv, FALSE);
8868}
8869
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008870/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008871 * "finddir({fname}[, {path}[, {count}]])" function
8872 */
8873 static void
8874f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008875 typval_T *argvars;
8876 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008877{
8878 findfilendir(argvars, rettv, TRUE);
8879}
8880
8881/*
8882 * "findfile({fname}[, {path}[, {count}]])" function
8883 */
8884 static void
8885f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008886 typval_T *argvars;
8887 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008888{
8889 findfilendir(argvars, rettv, FALSE);
8890}
8891
8892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 * "fnamemodify({fname}, {mods})" function
8894 */
8895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008896f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008897 typval_T *argvars;
8898 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899{
8900 char_u *fname;
8901 char_u *mods;
8902 int usedlen = 0;
8903 int len;
8904 char_u *fbuf = NULL;
8905 char_u buf[NUMBUFLEN];
8906
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008907 fname = get_tv_string_chk(&argvars[0]);
8908 mods = get_tv_string_buf_chk(&argvars[1], buf);
8909 if (fname == NULL || mods == NULL)
8910 fname = NULL;
8911 else
8912 {
8913 len = (int)STRLEN(fname);
8914 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008917 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008919 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008921 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 vim_free(fbuf);
8923}
8924
Bram Moolenaar33570922005-01-25 22:26:29 +00008925static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926
8927/*
8928 * "foldclosed()" function
8929 */
8930 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008931foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008932 typval_T *argvars;
8933 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 int end;
8935{
8936#ifdef FEAT_FOLDING
8937 linenr_T lnum;
8938 linenr_T first, last;
8939
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008940 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8942 {
8943 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8944 {
8945 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008946 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008948 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 return;
8950 }
8951 }
8952#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008953 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954}
8955
8956/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008957 * "foldclosed()" function
8958 */
8959 static void
8960f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008961 typval_T *argvars;
8962 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008963{
8964 foldclosed_both(argvars, rettv, FALSE);
8965}
8966
8967/*
8968 * "foldclosedend()" function
8969 */
8970 static void
8971f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008972 typval_T *argvars;
8973 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008974{
8975 foldclosed_both(argvars, rettv, TRUE);
8976}
8977
8978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 * "foldlevel()" function
8980 */
8981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008982f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008983 typval_T *argvars;
8984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985{
8986#ifdef FEAT_FOLDING
8987 linenr_T lnum;
8988
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008989 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008991 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 else
8993#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008994 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995}
8996
8997/*
8998 * "foldtext()" function
8999 */
9000/*ARGSUSED*/
9001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009002f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009003 typval_T *argvars;
9004 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005{
9006#ifdef FEAT_FOLDING
9007 linenr_T lnum;
9008 char_u *s;
9009 char_u *r;
9010 int len;
9011 char *txt;
9012#endif
9013
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009014 rettv->v_type = VAR_STRING;
9015 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00009017 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
9018 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
9019 <= curbuf->b_ml.ml_line_count
9020 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 {
9022 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009023 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
9024 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 {
9026 if (!linewhite(lnum))
9027 break;
9028 ++lnum;
9029 }
9030
9031 /* Find interesting text in this line. */
9032 s = skipwhite(ml_get(lnum));
9033 /* skip C comment-start */
9034 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009037 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00009038 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009039 {
9040 s = skipwhite(ml_get(lnum + 1));
9041 if (*s == '*')
9042 s = skipwhite(s + 1);
9043 }
9044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 txt = _("+-%s%3ld lines: ");
9046 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009047 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 + 20 /* for %3ld */
9049 + STRLEN(s))); /* concatenated */
9050 if (r != NULL)
9051 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009052 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
9053 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
9054 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 len = (int)STRLEN(r);
9056 STRCAT(r, s);
9057 /* remove 'foldmarker' and 'commentstring' */
9058 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009059 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 }
9061 }
9062#endif
9063}
9064
9065/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009066 * "foldtextresult(lnum)" function
9067 */
9068/*ARGSUSED*/
9069 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009071 typval_T *argvars;
9072 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009073{
9074#ifdef FEAT_FOLDING
9075 linenr_T lnum;
9076 char_u *text;
9077 char_u buf[51];
9078 foldinfo_T foldinfo;
9079 int fold_count;
9080#endif
9081
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009082 rettv->v_type = VAR_STRING;
9083 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009084#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009085 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009086 /* treat illegal types and illegal string values for {lnum} the same */
9087 if (lnum < 0)
9088 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009089 fold_count = foldedCount(curwin, lnum, &foldinfo);
9090 if (fold_count > 0)
9091 {
9092 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
9093 &foldinfo, buf);
9094 if (text == buf)
9095 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009096 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009097 }
9098#endif
9099}
9100
9101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 * "foreground()" function
9103 */
9104/*ARGSUSED*/
9105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009106f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009107 typval_T *argvars;
9108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111#ifdef FEAT_GUI
9112 if (gui.in_use)
9113 gui_mch_set_foreground();
9114#else
9115# ifdef WIN32
9116 win32_set_foreground();
9117# endif
9118#endif
9119}
9120
9121/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009122 * "function()" function
9123 */
9124/*ARGSUSED*/
9125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009126f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009127 typval_T *argvars;
9128 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009129{
9130 char_u *s;
9131
Bram Moolenaara7043832005-01-21 11:56:39 +00009132 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009133 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009134 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009135 EMSG2(_(e_invarg2), s);
9136 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009137 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009138 else
9139 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009140 rettv->vval.v_string = vim_strsave(s);
9141 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009142 }
9143}
9144
9145/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009146 * "garbagecollect()" function
9147 */
9148/*ARGSUSED*/
9149 static void
9150f_garbagecollect(argvars, rettv)
9151 typval_T *argvars;
9152 typval_T *rettv;
9153{
9154 garbage_collect();
9155}
9156
9157/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009158 * "get()" function
9159 */
9160 static void
9161f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009162 typval_T *argvars;
9163 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009164{
Bram Moolenaar33570922005-01-25 22:26:29 +00009165 listitem_T *li;
9166 list_T *l;
9167 dictitem_T *di;
9168 dict_T *d;
9169 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009170
Bram Moolenaare9a41262005-01-15 22:18:47 +00009171 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009172 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009173 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009174 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009175 int error = FALSE;
9176
9177 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9178 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009179 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009180 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009181 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009182 else if (argvars[0].v_type == VAR_DICT)
9183 {
9184 if ((d = argvars[0].vval.v_dict) != NULL)
9185 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009186 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009187 if (di != NULL)
9188 tv = &di->di_tv;
9189 }
9190 }
9191 else
9192 EMSG2(_(e_listdictarg), "get()");
9193
9194 if (tv == NULL)
9195 {
9196 if (argvars[2].v_type == VAR_UNKNOWN)
9197 rettv->vval.v_number = 0;
9198 else
9199 copy_tv(&argvars[2], rettv);
9200 }
9201 else
9202 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009203}
9204
Bram Moolenaar342337a2005-07-21 21:11:17 +00009205static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009206
9207/*
9208 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +00009209 * Return a range (from start to end) of lines in rettv from the specified
9210 * buffer.
9211 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009212 */
9213 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +00009214get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009215 buf_T *buf;
9216 linenr_T start;
9217 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009218 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009219 typval_T *rettv;
9220{
9221 char_u *p;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009222 list_T *l = NULL;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009223
Bram Moolenaar342337a2005-07-21 21:11:17 +00009224 if (retlist)
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009225 {
Bram Moolenaar342337a2005-07-21 21:11:17 +00009226 l = list_alloc();
9227 if (l == NULL)
9228 return;
9229
9230 rettv->vval.v_list = l;
9231 rettv->v_type = VAR_LIST;
9232 ++l->lv_refcount;
9233 }
9234 else
9235 rettv->vval.v_number = 0;
9236
9237 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
9238 return;
9239
9240 if (!retlist)
9241 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009242 if (start >= 1 && start <= buf->b_ml.ml_line_count)
9243 p = ml_get_buf(buf, start, FALSE);
9244 else
9245 p = (char_u *)"";
9246
9247 rettv->v_type = VAR_STRING;
9248 rettv->vval.v_string = vim_strsave(p);
9249 }
9250 else
9251 {
9252 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009253 return;
9254
9255 if (start < 1)
9256 start = 1;
9257 if (end > buf->b_ml.ml_line_count)
9258 end = buf->b_ml.ml_line_count;
9259 while (start <= end)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00009260 if (list_append_string(l, ml_get_buf(buf, start++, FALSE)) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009261 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009262 }
9263}
9264
9265/*
9266 * "getbufline()" function
9267 */
9268 static void
9269f_getbufline(argvars, rettv)
9270 typval_T *argvars;
9271 typval_T *rettv;
9272{
9273 linenr_T lnum;
9274 linenr_T end;
9275 buf_T *buf;
9276
9277 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9278 ++emsg_off;
9279 buf = get_buf_tv(&argvars[0]);
9280 --emsg_off;
9281
Bram Moolenaar661b1822005-07-28 22:36:45 +00009282 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009283 if (argvars[2].v_type == VAR_UNKNOWN)
9284 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009285 else
Bram Moolenaar661b1822005-07-28 22:36:45 +00009286 end = get_tv_lnum_buf(&argvars[2], buf);
9287
Bram Moolenaar342337a2005-07-21 21:11:17 +00009288 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009289}
9290
Bram Moolenaar0d660222005-01-07 21:51:51 +00009291/*
9292 * "getbufvar()" function
9293 */
9294 static void
9295f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009296 typval_T *argvars;
9297 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009298{
9299 buf_T *buf;
9300 buf_T *save_curbuf;
9301 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009302 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009304 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9305 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009306 ++emsg_off;
9307 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009308
9309 rettv->v_type = VAR_STRING;
9310 rettv->vval.v_string = NULL;
9311
9312 if (buf != NULL && varname != NULL)
9313 {
9314 if (*varname == '&') /* buffer-local-option */
9315 {
9316 /* set curbuf to be our buf, temporarily */
9317 save_curbuf = curbuf;
9318 curbuf = buf;
9319
9320 get_option_tv(&varname, rettv, TRUE);
9321
9322 /* restore previous notion of curbuf */
9323 curbuf = save_curbuf;
9324 }
9325 else
9326 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009327 if (*varname == NUL)
9328 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9329 * scope prefix before the NUL byte is required by
9330 * find_var_in_ht(). */
9331 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009332 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009333 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009334 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009335 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009336 }
9337 }
9338
9339 --emsg_off;
9340}
9341
9342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 * "getchar()" function
9344 */
9345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009346f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009347 typval_T *argvars;
9348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349{
9350 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009351 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352
9353 ++no_mapping;
9354 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009355 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 /* getchar(): blocking wait. */
9357 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009358 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 /* getchar(1): only check if char avail */
9360 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009361 else if (error || vpeekc() == NUL)
9362 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 n = 0;
9364 else
9365 /* getchar(0) and char avail: return char */
9366 n = safe_vgetc();
9367 --no_mapping;
9368 --allow_keys;
9369
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009370 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371 if (IS_SPECIAL(n) || mod_mask != 0)
9372 {
9373 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9374 int i = 0;
9375
9376 /* Turn a special key into three bytes, plus modifier. */
9377 if (mod_mask != 0)
9378 {
9379 temp[i++] = K_SPECIAL;
9380 temp[i++] = KS_MODIFIER;
9381 temp[i++] = mod_mask;
9382 }
9383 if (IS_SPECIAL(n))
9384 {
9385 temp[i++] = K_SPECIAL;
9386 temp[i++] = K_SECOND(n);
9387 temp[i++] = K_THIRD(n);
9388 }
9389#ifdef FEAT_MBYTE
9390 else if (has_mbyte)
9391 i += (*mb_char2bytes)(n, temp + i);
9392#endif
9393 else
9394 temp[i++] = n;
9395 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009396 rettv->v_type = VAR_STRING;
9397 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 }
9399}
9400
9401/*
9402 * "getcharmod()" function
9403 */
9404/*ARGSUSED*/
9405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009406f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009407 typval_T *argvars;
9408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009410 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411}
9412
9413/*
9414 * "getcmdline()" function
9415 */
9416/*ARGSUSED*/
9417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009418f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009419 typval_T *argvars;
9420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009422 rettv->v_type = VAR_STRING;
9423 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424}
9425
9426/*
9427 * "getcmdpos()" function
9428 */
9429/*ARGSUSED*/
9430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009431f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009432 typval_T *argvars;
9433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009434{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009435 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436}
9437
9438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 * "getcwd()" function
9440 */
9441/*ARGSUSED*/
9442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009443f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009444 typval_T *argvars;
9445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009446{
9447 char_u cwd[MAXPATHL];
9448
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009449 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009451 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 else
9453 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009454 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar342337a2005-07-21 21:11:17 +00009456 if (rettv->vval.v_string != NULL)
9457 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458#endif
9459 }
9460}
9461
9462/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009463 * "getfontname()" function
9464 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009465/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009466 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009467f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009468 typval_T *argvars;
9469 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009470{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009471 rettv->v_type = VAR_STRING;
9472 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009473#ifdef FEAT_GUI
9474 if (gui.in_use)
9475 {
9476 GuiFont font;
9477 char_u *name = NULL;
9478
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009479 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009480 {
9481 /* Get the "Normal" font. Either the name saved by
9482 * hl_set_font_name() or from the font ID. */
9483 font = gui.norm_font;
9484 name = hl_get_font_name();
9485 }
9486 else
9487 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009488 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009489 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9490 return;
9491 font = gui_mch_get_font(name, FALSE);
9492 if (font == NOFONT)
9493 return; /* Invalid font name, return empty string. */
9494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009495 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009496 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009497 gui_mch_free_font(font);
9498 }
9499#endif
9500}
9501
9502/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009503 * "getfperm({fname})" function
9504 */
9505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009506f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009507 typval_T *argvars;
9508 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009509{
9510 char_u *fname;
9511 struct stat st;
9512 char_u *perm = NULL;
9513 char_u flags[] = "rwx";
9514 int i;
9515
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009516 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009517
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009518 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009519 if (mch_stat((char *)fname, &st) >= 0)
9520 {
9521 perm = vim_strsave((char_u *)"---------");
9522 if (perm != NULL)
9523 {
9524 for (i = 0; i < 9; i++)
9525 {
9526 if (st.st_mode & (1 << (8 - i)))
9527 perm[i] = flags[i % 3];
9528 }
9529 }
9530 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009531 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009532}
9533
9534/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 * "getfsize({fname})" function
9536 */
9537 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009538f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009539 typval_T *argvars;
9540 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541{
9542 char_u *fname;
9543 struct stat st;
9544
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009545 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009547 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548
9549 if (mch_stat((char *)fname, &st) >= 0)
9550 {
9551 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009552 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009554 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 }
9556 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558}
9559
9560/*
9561 * "getftime({fname})" function
9562 */
9563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009564f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009565 typval_T *argvars;
9566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567{
9568 char_u *fname;
9569 struct stat st;
9570
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009571 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572
9573 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009574 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009576 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577}
9578
9579/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009580 * "getftype({fname})" function
9581 */
9582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009583f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009584 typval_T *argvars;
9585 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009586{
9587 char_u *fname;
9588 struct stat st;
9589 char_u *type = NULL;
9590 char *t;
9591
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009592 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009593
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009594 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009595 if (mch_lstat((char *)fname, &st) >= 0)
9596 {
9597#ifdef S_ISREG
9598 if (S_ISREG(st.st_mode))
9599 t = "file";
9600 else if (S_ISDIR(st.st_mode))
9601 t = "dir";
9602# ifdef S_ISLNK
9603 else if (S_ISLNK(st.st_mode))
9604 t = "link";
9605# endif
9606# ifdef S_ISBLK
9607 else if (S_ISBLK(st.st_mode))
9608 t = "bdev";
9609# endif
9610# ifdef S_ISCHR
9611 else if (S_ISCHR(st.st_mode))
9612 t = "cdev";
9613# endif
9614# ifdef S_ISFIFO
9615 else if (S_ISFIFO(st.st_mode))
9616 t = "fifo";
9617# endif
9618# ifdef S_ISSOCK
9619 else if (S_ISSOCK(st.st_mode))
9620 t = "fifo";
9621# endif
9622 else
9623 t = "other";
9624#else
9625# ifdef S_IFMT
9626 switch (st.st_mode & S_IFMT)
9627 {
9628 case S_IFREG: t = "file"; break;
9629 case S_IFDIR: t = "dir"; break;
9630# ifdef S_IFLNK
9631 case S_IFLNK: t = "link"; break;
9632# endif
9633# ifdef S_IFBLK
9634 case S_IFBLK: t = "bdev"; break;
9635# endif
9636# ifdef S_IFCHR
9637 case S_IFCHR: t = "cdev"; break;
9638# endif
9639# ifdef S_IFIFO
9640 case S_IFIFO: t = "fifo"; break;
9641# endif
9642# ifdef S_IFSOCK
9643 case S_IFSOCK: t = "socket"; break;
9644# endif
9645 default: t = "other";
9646 }
9647# else
9648 if (mch_isdir(fname))
9649 t = "dir";
9650 else
9651 t = "file";
9652# endif
9653#endif
9654 type = vim_strsave((char_u *)t);
9655 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009656 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009657}
9658
9659/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009660 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +00009661 */
9662 static void
9663f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009664 typval_T *argvars;
9665 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009666{
9667 linenr_T lnum;
9668 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009669 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009670
9671 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009672 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +00009673 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009674 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +00009675 retlist = FALSE;
9676 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009677 else
Bram Moolenaar342337a2005-07-21 21:11:17 +00009678 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009679 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +00009680 retlist = TRUE;
9681 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +00009682
Bram Moolenaar342337a2005-07-21 21:11:17 +00009683 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009684}
9685
9686/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009687 * "getqflist()" function
9688 */
9689/*ARGSUSED*/
9690 static void
9691f_getqflist(argvars, rettv)
9692 typval_T *argvars;
9693 typval_T *rettv;
9694{
9695#ifdef FEAT_QUICKFIX
9696 list_T *l;
9697#endif
9698
9699 rettv->vval.v_number = FALSE;
9700#ifdef FEAT_QUICKFIX
9701 l = list_alloc();
9702 if (l != NULL)
9703 {
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00009704 rettv->vval.v_list = l;
9705 rettv->v_type = VAR_LIST;
9706 ++l->lv_refcount;
9707 (void)get_errorlist(l);
Bram Moolenaar2641f772005-03-25 21:58:17 +00009708 }
9709#endif
9710}
9711
9712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 * "getreg()" function
9714 */
9715 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009716f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009717 typval_T *argvars;
9718 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719{
9720 char_u *strregname;
9721 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009722 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009723 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009725 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009726 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009727 strregname = get_tv_string_chk(&argvars[0]);
9728 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009729 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009730 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009733 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 regname = (strregname == NULL ? '"' : *strregname);
9735 if (regname == 0)
9736 regname = '"';
9737
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009738 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009739 rettv->vval.v_string = error ? NULL :
9740 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741}
9742
9743/*
9744 * "getregtype()" function
9745 */
9746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009747f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009748 typval_T *argvars;
9749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750{
9751 char_u *strregname;
9752 int regname;
9753 char_u buf[NUMBUFLEN + 2];
9754 long reglen = 0;
9755
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009756 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009757 {
9758 strregname = get_tv_string_chk(&argvars[0]);
9759 if (strregname == NULL) /* type error; errmsg already given */
9760 {
9761 rettv->v_type = VAR_STRING;
9762 rettv->vval.v_string = NULL;
9763 return;
9764 }
9765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 else
9767 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009768 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769
9770 regname = (strregname == NULL ? '"' : *strregname);
9771 if (regname == 0)
9772 regname = '"';
9773
9774 buf[0] = NUL;
9775 buf[1] = NUL;
9776 switch (get_reg_type(regname, &reglen))
9777 {
9778 case MLINE: buf[0] = 'V'; break;
9779 case MCHAR: buf[0] = 'v'; break;
9780#ifdef FEAT_VISUAL
9781 case MBLOCK:
9782 buf[0] = Ctrl_V;
9783 sprintf((char *)buf + 1, "%ld", reglen + 1);
9784 break;
9785#endif
9786 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009787 rettv->v_type = VAR_STRING;
9788 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789}
9790
9791/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 * "getwinposx()" function
9793 */
9794/*ARGSUSED*/
9795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009796f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009797 typval_T *argvars;
9798 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009800 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801#ifdef FEAT_GUI
9802 if (gui.in_use)
9803 {
9804 int x, y;
9805
9806 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009807 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 }
9809#endif
9810}
9811
9812/*
9813 * "getwinposy()" function
9814 */
9815/*ARGSUSED*/
9816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009817f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009818 typval_T *argvars;
9819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009821 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822#ifdef FEAT_GUI
9823 if (gui.in_use)
9824 {
9825 int x, y;
9826
9827 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009828 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009829 }
9830#endif
9831}
9832
Bram Moolenaara40058a2005-07-11 22:42:07 +00009833static win_T *find_win_by_nr __ARGS((typval_T *vp));
9834
9835 static win_T *
9836find_win_by_nr(vp)
9837 typval_T *vp;
9838{
9839#ifdef FEAT_WINDOWS
9840 win_T *wp;
9841#endif
9842 int nr;
9843
9844 nr = get_tv_number_chk(vp, NULL);
9845
9846#ifdef FEAT_WINDOWS
9847 if (nr < 0)
9848 return NULL;
9849 if (nr == 0)
9850 return curwin;
9851
9852 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9853 if (--nr <= 0)
9854 break;
9855 return wp;
9856#else
9857 if (nr == 0 || nr == 1)
9858 return curwin;
9859 return NULL;
9860#endif
9861}
9862
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863/*
9864 * "getwinvar()" function
9865 */
9866 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009867f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009868 typval_T *argvars;
9869 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009870{
9871 win_T *win, *oldcurwin;
9872 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009873 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009876 varname = get_tv_string_chk(&argvars[1]);
9877 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009879 rettv->v_type = VAR_STRING;
9880 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881
9882 if (win != NULL && varname != NULL)
9883 {
9884 if (*varname == '&') /* window-local-option */
9885 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009886 /* Set curwin to be our win, temporarily. Also set curbuf, so
9887 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 oldcurwin = curwin;
9889 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009890 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009892 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009893
9894 /* restore previous notion of curwin */
9895 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009896 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 }
9898 else
9899 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009900 if (*varname == NUL)
9901 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9902 * scope prefix before the NUL byte is required by
9903 * find_var_in_ht(). */
9904 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009906 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009908 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 }
9910 }
9911
9912 --emsg_off;
9913}
9914
9915/*
9916 * "glob()" function
9917 */
9918 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009919f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009920 typval_T *argvars;
9921 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922{
9923 expand_T xpc;
9924
9925 ExpandInit(&xpc);
9926 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009927 rettv->v_type = VAR_STRING;
9928 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9930 ExpandCleanup(&xpc);
9931}
9932
9933/*
9934 * "globpath()" function
9935 */
9936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009937f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009938 typval_T *argvars;
9939 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009940{
9941 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009942 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009945 if (file == NULL)
9946 rettv->vval.v_string = NULL;
9947 else
9948 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949}
9950
9951/*
9952 * "has()" function
9953 */
9954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009955f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009956 typval_T *argvars;
9957 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958{
9959 int i;
9960 char_u *name;
9961 int n = FALSE;
9962 static char *(has_list[]) =
9963 {
9964#ifdef AMIGA
9965 "amiga",
9966# ifdef FEAT_ARP
9967 "arp",
9968# endif
9969#endif
9970#ifdef __BEOS__
9971 "beos",
9972#endif
9973#ifdef MSDOS
9974# ifdef DJGPP
9975 "dos32",
9976# else
9977 "dos16",
9978# endif
9979#endif
9980#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9981 "mac",
9982#endif
9983#if defined(MACOS_X_UNIX)
9984 "macunix",
9985#endif
9986#ifdef OS2
9987 "os2",
9988#endif
9989#ifdef __QNX__
9990 "qnx",
9991#endif
9992#ifdef RISCOS
9993 "riscos",
9994#endif
9995#ifdef UNIX
9996 "unix",
9997#endif
9998#ifdef VMS
9999 "vms",
10000#endif
10001#ifdef WIN16
10002 "win16",
10003#endif
10004#ifdef WIN32
10005 "win32",
10006#endif
10007#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
10008 "win32unix",
10009#endif
10010#ifdef WIN64
10011 "win64",
10012#endif
10013#ifdef EBCDIC
10014 "ebcdic",
10015#endif
10016#ifndef CASE_INSENSITIVE_FILENAME
10017 "fname_case",
10018#endif
10019#ifdef FEAT_ARABIC
10020 "arabic",
10021#endif
10022#ifdef FEAT_AUTOCMD
10023 "autocmd",
10024#endif
10025#ifdef FEAT_BEVAL
10026 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000010027# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
10028 "balloon_multiline",
10029# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010030#endif
10031#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
10032 "builtin_terms",
10033# ifdef ALL_BUILTIN_TCAPS
10034 "all_builtin_terms",
10035# endif
10036#endif
10037#ifdef FEAT_BYTEOFF
10038 "byte_offset",
10039#endif
10040#ifdef FEAT_CINDENT
10041 "cindent",
10042#endif
10043#ifdef FEAT_CLIENTSERVER
10044 "clientserver",
10045#endif
10046#ifdef FEAT_CLIPBOARD
10047 "clipboard",
10048#endif
10049#ifdef FEAT_CMDL_COMPL
10050 "cmdline_compl",
10051#endif
10052#ifdef FEAT_CMDHIST
10053 "cmdline_hist",
10054#endif
10055#ifdef FEAT_COMMENTS
10056 "comments",
10057#endif
10058#ifdef FEAT_CRYPT
10059 "cryptv",
10060#endif
10061#ifdef FEAT_CSCOPE
10062 "cscope",
10063#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000010064#ifdef CURSOR_SHAPE
10065 "cursorshape",
10066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067#ifdef DEBUG
10068 "debug",
10069#endif
10070#ifdef FEAT_CON_DIALOG
10071 "dialog_con",
10072#endif
10073#ifdef FEAT_GUI_DIALOG
10074 "dialog_gui",
10075#endif
10076#ifdef FEAT_DIFF
10077 "diff",
10078#endif
10079#ifdef FEAT_DIGRAPHS
10080 "digraphs",
10081#endif
10082#ifdef FEAT_DND
10083 "dnd",
10084#endif
10085#ifdef FEAT_EMACS_TAGS
10086 "emacs_tags",
10087#endif
10088 "eval", /* always present, of course! */
10089#ifdef FEAT_EX_EXTRA
10090 "ex_extra",
10091#endif
10092#ifdef FEAT_SEARCH_EXTRA
10093 "extra_search",
10094#endif
10095#ifdef FEAT_FKMAP
10096 "farsi",
10097#endif
10098#ifdef FEAT_SEARCHPATH
10099 "file_in_path",
10100#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000010101#if defined(UNIX) && !defined(USE_SYSTEM)
10102 "filterpipe",
10103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104#ifdef FEAT_FIND_ID
10105 "find_in_path",
10106#endif
10107#ifdef FEAT_FOLDING
10108 "folding",
10109#endif
10110#ifdef FEAT_FOOTER
10111 "footer",
10112#endif
10113#if !defined(USE_SYSTEM) && defined(UNIX)
10114 "fork",
10115#endif
10116#ifdef FEAT_GETTEXT
10117 "gettext",
10118#endif
10119#ifdef FEAT_GUI
10120 "gui",
10121#endif
10122#ifdef FEAT_GUI_ATHENA
10123# ifdef FEAT_GUI_NEXTAW
10124 "gui_neXtaw",
10125# else
10126 "gui_athena",
10127# endif
10128#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +000010129#ifdef FEAT_GUI_KDE
10130 "gui_kde",
10131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132#ifdef FEAT_GUI_GTK
10133 "gui_gtk",
10134# ifdef HAVE_GTK2
10135 "gui_gtk2",
10136# endif
10137#endif
10138#ifdef FEAT_GUI_MAC
10139 "gui_mac",
10140#endif
10141#ifdef FEAT_GUI_MOTIF
10142 "gui_motif",
10143#endif
10144#ifdef FEAT_GUI_PHOTON
10145 "gui_photon",
10146#endif
10147#ifdef FEAT_GUI_W16
10148 "gui_win16",
10149#endif
10150#ifdef FEAT_GUI_W32
10151 "gui_win32",
10152#endif
10153#ifdef FEAT_HANGULIN
10154 "hangul_input",
10155#endif
10156#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
10157 "iconv",
10158#endif
10159#ifdef FEAT_INS_EXPAND
10160 "insert_expand",
10161#endif
10162#ifdef FEAT_JUMPLIST
10163 "jumplist",
10164#endif
10165#ifdef FEAT_KEYMAP
10166 "keymap",
10167#endif
10168#ifdef FEAT_LANGMAP
10169 "langmap",
10170#endif
10171#ifdef FEAT_LIBCALL
10172 "libcall",
10173#endif
10174#ifdef FEAT_LINEBREAK
10175 "linebreak",
10176#endif
10177#ifdef FEAT_LISP
10178 "lispindent",
10179#endif
10180#ifdef FEAT_LISTCMDS
10181 "listcmds",
10182#endif
10183#ifdef FEAT_LOCALMAP
10184 "localmap",
10185#endif
10186#ifdef FEAT_MENU
10187 "menu",
10188#endif
10189#ifdef FEAT_SESSION
10190 "mksession",
10191#endif
10192#ifdef FEAT_MODIFY_FNAME
10193 "modify_fname",
10194#endif
10195#ifdef FEAT_MOUSE
10196 "mouse",
10197#endif
10198#ifdef FEAT_MOUSESHAPE
10199 "mouseshape",
10200#endif
10201#if defined(UNIX) || defined(VMS)
10202# ifdef FEAT_MOUSE_DEC
10203 "mouse_dec",
10204# endif
10205# ifdef FEAT_MOUSE_GPM
10206 "mouse_gpm",
10207# endif
10208# ifdef FEAT_MOUSE_JSB
10209 "mouse_jsbterm",
10210# endif
10211# ifdef FEAT_MOUSE_NET
10212 "mouse_netterm",
10213# endif
10214# ifdef FEAT_MOUSE_PTERM
10215 "mouse_pterm",
10216# endif
10217# ifdef FEAT_MOUSE_XTERM
10218 "mouse_xterm",
10219# endif
10220#endif
10221#ifdef FEAT_MBYTE
10222 "multi_byte",
10223#endif
10224#ifdef FEAT_MBYTE_IME
10225 "multi_byte_ime",
10226#endif
10227#ifdef FEAT_MULTI_LANG
10228 "multi_lang",
10229#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010230#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010231#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010232 "mzscheme",
10233#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235#ifdef FEAT_OLE
10236 "ole",
10237#endif
10238#ifdef FEAT_OSFILETYPE
10239 "osfiletype",
10240#endif
10241#ifdef FEAT_PATH_EXTRA
10242 "path_extra",
10243#endif
10244#ifdef FEAT_PERL
10245#ifndef DYNAMIC_PERL
10246 "perl",
10247#endif
10248#endif
10249#ifdef FEAT_PYTHON
10250#ifndef DYNAMIC_PYTHON
10251 "python",
10252#endif
10253#endif
10254#ifdef FEAT_POSTSCRIPT
10255 "postscript",
10256#endif
10257#ifdef FEAT_PRINTER
10258 "printer",
10259#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010260#ifdef FEAT_PROFILE
10261 "profile",
10262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263#ifdef FEAT_QUICKFIX
10264 "quickfix",
10265#endif
10266#ifdef FEAT_RIGHTLEFT
10267 "rightleft",
10268#endif
10269#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10270 "ruby",
10271#endif
10272#ifdef FEAT_SCROLLBIND
10273 "scrollbind",
10274#endif
10275#ifdef FEAT_CMDL_INFO
10276 "showcmd",
10277 "cmdline_info",
10278#endif
10279#ifdef FEAT_SIGNS
10280 "signs",
10281#endif
10282#ifdef FEAT_SMARTINDENT
10283 "smartindent",
10284#endif
10285#ifdef FEAT_SNIFF
10286 "sniff",
10287#endif
10288#ifdef FEAT_STL_OPT
10289 "statusline",
10290#endif
10291#ifdef FEAT_SUN_WORKSHOP
10292 "sun_workshop",
10293#endif
10294#ifdef FEAT_NETBEANS_INTG
10295 "netbeans_intg",
10296#endif
10297#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010298 "spell",
10299#endif
10300#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 "syntax",
10302#endif
10303#if defined(USE_SYSTEM) || !defined(UNIX)
10304 "system",
10305#endif
10306#ifdef FEAT_TAG_BINS
10307 "tag_binary",
10308#endif
10309#ifdef FEAT_TAG_OLDSTATIC
10310 "tag_old_static",
10311#endif
10312#ifdef FEAT_TAG_ANYWHITE
10313 "tag_any_white",
10314#endif
10315#ifdef FEAT_TCL
10316# ifndef DYNAMIC_TCL
10317 "tcl",
10318# endif
10319#endif
10320#ifdef TERMINFO
10321 "terminfo",
10322#endif
10323#ifdef FEAT_TERMRESPONSE
10324 "termresponse",
10325#endif
10326#ifdef FEAT_TEXTOBJ
10327 "textobjects",
10328#endif
10329#ifdef HAVE_TGETENT
10330 "tgetent",
10331#endif
10332#ifdef FEAT_TITLE
10333 "title",
10334#endif
10335#ifdef FEAT_TOOLBAR
10336 "toolbar",
10337#endif
10338#ifdef FEAT_USR_CMDS
10339 "user-commands", /* was accidentally included in 5.4 */
10340 "user_commands",
10341#endif
10342#ifdef FEAT_VIMINFO
10343 "viminfo",
10344#endif
10345#ifdef FEAT_VERTSPLIT
10346 "vertsplit",
10347#endif
10348#ifdef FEAT_VIRTUALEDIT
10349 "virtualedit",
10350#endif
10351#ifdef FEAT_VISUAL
10352 "visual",
10353#endif
10354#ifdef FEAT_VISUALEXTRA
10355 "visualextra",
10356#endif
10357#ifdef FEAT_VREPLACE
10358 "vreplace",
10359#endif
10360#ifdef FEAT_WILDIGN
10361 "wildignore",
10362#endif
10363#ifdef FEAT_WILDMENU
10364 "wildmenu",
10365#endif
10366#ifdef FEAT_WINDOWS
10367 "windows",
10368#endif
10369#ifdef FEAT_WAK
10370 "winaltkeys",
10371#endif
10372#ifdef FEAT_WRITEBACKUP
10373 "writebackup",
10374#endif
10375#ifdef FEAT_XIM
10376 "xim",
10377#endif
10378#ifdef FEAT_XFONTSET
10379 "xfontset",
10380#endif
10381#ifdef USE_XSMP
10382 "xsmp",
10383#endif
10384#ifdef USE_XSMP_INTERACT
10385 "xsmp_interact",
10386#endif
10387#ifdef FEAT_XCLIPBOARD
10388 "xterm_clipboard",
10389#endif
10390#ifdef FEAT_XTERM_SAVE
10391 "xterm_save",
10392#endif
10393#if defined(UNIX) && defined(FEAT_X11)
10394 "X11",
10395#endif
10396 NULL
10397 };
10398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010399 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 for (i = 0; has_list[i] != NULL; ++i)
10401 if (STRICMP(name, has_list[i]) == 0)
10402 {
10403 n = TRUE;
10404 break;
10405 }
10406
10407 if (n == FALSE)
10408 {
10409 if (STRNICMP(name, "patch", 5) == 0)
10410 n = has_patch(atoi((char *)name + 5));
10411 else if (STRICMP(name, "vim_starting") == 0)
10412 n = (starting != 0);
Bram Moolenaar342337a2005-07-21 21:11:17 +000010413#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
10414 else if (STRICMP(name, "balloon_multiline") == 0)
10415 n = multiline_balloon_available();
10416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417#ifdef DYNAMIC_TCL
10418 else if (STRICMP(name, "tcl") == 0)
10419 n = tcl_enabled(FALSE);
10420#endif
10421#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10422 else if (STRICMP(name, "iconv") == 0)
10423 n = iconv_enabled(FALSE);
10424#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010425#ifdef DYNAMIC_MZSCHEME
10426 else if (STRICMP(name, "mzscheme") == 0)
10427 n = mzscheme_enabled(FALSE);
10428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429#ifdef DYNAMIC_RUBY
10430 else if (STRICMP(name, "ruby") == 0)
10431 n = ruby_enabled(FALSE);
10432#endif
10433#ifdef DYNAMIC_PYTHON
10434 else if (STRICMP(name, "python") == 0)
10435 n = python_enabled(FALSE);
10436#endif
10437#ifdef DYNAMIC_PERL
10438 else if (STRICMP(name, "perl") == 0)
10439 n = perl_enabled(FALSE);
10440#endif
10441#ifdef FEAT_GUI
10442 else if (STRICMP(name, "gui_running") == 0)
10443 n = (gui.in_use || gui.starting);
10444# ifdef FEAT_GUI_W32
10445 else if (STRICMP(name, "gui_win32s") == 0)
10446 n = gui_is_win32s();
10447# endif
10448# ifdef FEAT_BROWSE
10449 else if (STRICMP(name, "browse") == 0)
10450 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10451# endif
10452#endif
10453#ifdef FEAT_SYN_HL
10454 else if (STRICMP(name, "syntax_items") == 0)
10455 n = syntax_present(curbuf);
10456#endif
10457#if defined(WIN3264)
10458 else if (STRICMP(name, "win95") == 0)
10459 n = mch_windows95();
10460#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010461#ifdef FEAT_NETBEANS_INTG
10462 else if (STRICMP(name, "netbeans_enabled") == 0)
10463 n = usingNetbeans;
10464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465 }
10466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010467 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468}
10469
10470/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010471 * "has_key()" function
10472 */
10473 static void
10474f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010475 typval_T *argvars;
10476 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010477{
10478 rettv->vval.v_number = 0;
10479 if (argvars[0].v_type != VAR_DICT)
10480 {
10481 EMSG(_(e_dictreq));
10482 return;
10483 }
10484 if (argvars[0].vval.v_dict == NULL)
10485 return;
10486
10487 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010488 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010489}
10490
10491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492 * "hasmapto()" function
10493 */
10494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010495f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010496 typval_T *argvars;
10497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498{
10499 char_u *name;
10500 char_u *mode;
10501 char_u buf[NUMBUFLEN];
10502
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010503 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010504 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 mode = (char_u *)"nvo";
10506 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010507 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508
10509 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010510 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010512 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513}
10514
10515/*
10516 * "histadd()" function
10517 */
10518/*ARGSUSED*/
10519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010520f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010521 typval_T *argvars;
10522 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523{
10524#ifdef FEAT_CMDHIST
10525 int histype;
10526 char_u *str;
10527 char_u buf[NUMBUFLEN];
10528#endif
10529
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010530 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531 if (check_restricted() || check_secure())
10532 return;
10533#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010534 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10535 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010536 if (histype >= 0)
10537 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010538 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 if (*str != NUL)
10540 {
10541 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010542 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 return;
10544 }
10545 }
10546#endif
10547}
10548
10549/*
10550 * "histdel()" function
10551 */
10552/*ARGSUSED*/
10553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010554f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010555 typval_T *argvars;
10556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010557{
10558#ifdef FEAT_CMDHIST
10559 int n;
10560 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010561 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010563 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10564 if (str == NULL)
10565 n = 0;
10566 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010568 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010569 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010571 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010572 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573 else
10574 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010575 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010576 get_tv_string_buf(&argvars[1], buf));
10577 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010578#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010579 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580#endif
10581}
10582
10583/*
10584 * "histget()" function
10585 */
10586/*ARGSUSED*/
10587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010588f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010589 typval_T *argvars;
10590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591{
10592#ifdef FEAT_CMDHIST
10593 int type;
10594 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010595 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010597 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10598 if (str == NULL)
10599 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010600 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010601 {
10602 type = get_histtype(str);
10603 if (argvars[1].v_type == VAR_UNKNOWN)
10604 idx = get_history_idx(type);
10605 else
10606 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10607 /* -1 on type error */
10608 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010611 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010613 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614}
10615
10616/*
10617 * "histnr()" function
10618 */
10619/*ARGSUSED*/
10620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010621f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010622 typval_T *argvars;
10623 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624{
10625 int i;
10626
10627#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010628 char_u *history = get_tv_string_chk(&argvars[0]);
10629
10630 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 if (i >= HIST_CMD && i < HIST_COUNT)
10632 i = get_history_idx(i);
10633 else
10634#endif
10635 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010636 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637}
10638
10639/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 * "highlightID(name)" function
10641 */
10642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010643f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010644 typval_T *argvars;
10645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010646{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010647 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648}
10649
10650/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010651 * "highlight_exists()" function
10652 */
10653 static void
10654f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010655 typval_T *argvars;
10656 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010657{
10658 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10659}
10660
10661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662 * "hostname()" function
10663 */
10664/*ARGSUSED*/
10665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010666f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010667 typval_T *argvars;
10668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669{
10670 char_u hostname[256];
10671
10672 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010673 rettv->v_type = VAR_STRING;
10674 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675}
10676
10677/*
10678 * iconv() function
10679 */
10680/*ARGSUSED*/
10681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010682f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010683 typval_T *argvars;
10684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685{
10686#ifdef FEAT_MBYTE
10687 char_u buf1[NUMBUFLEN];
10688 char_u buf2[NUMBUFLEN];
10689 char_u *from, *to, *str;
10690 vimconv_T vimconv;
10691#endif
10692
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010693 rettv->v_type = VAR_STRING;
10694 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695
10696#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010697 str = get_tv_string(&argvars[0]);
10698 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10699 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010700 vimconv.vc_type = CONV_NONE;
10701 convert_setup(&vimconv, from, to);
10702
10703 /* If the encodings are equal, no conversion needed. */
10704 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010705 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010707 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708
10709 convert_setup(&vimconv, NULL, NULL);
10710 vim_free(from);
10711 vim_free(to);
10712#endif
10713}
10714
10715/*
10716 * "indent()" function
10717 */
10718 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010719f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010720 typval_T *argvars;
10721 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722{
10723 linenr_T lnum;
10724
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010725 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010727 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010729 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730}
10731
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010732/*
10733 * "index()" function
10734 */
10735 static void
10736f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010737 typval_T *argvars;
10738 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010739{
Bram Moolenaar33570922005-01-25 22:26:29 +000010740 list_T *l;
10741 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010742 long idx = 0;
10743 int ic = FALSE;
10744
10745 rettv->vval.v_number = -1;
10746 if (argvars[0].v_type != VAR_LIST)
10747 {
10748 EMSG(_(e_listreq));
10749 return;
10750 }
10751 l = argvars[0].vval.v_list;
10752 if (l != NULL)
10753 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010754 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010755 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010756 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010757 int error = FALSE;
10758
Bram Moolenaar758711c2005-02-02 23:11:38 +000010759 /* Start at specified item. Use the cached index that list_find()
10760 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010761 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010762 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010763 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010764 ic = get_tv_number_chk(&argvars[3], &error);
10765 if (error)
10766 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010767 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010768
Bram Moolenaar758711c2005-02-02 23:11:38 +000010769 for ( ; item != NULL; item = item->li_next, ++idx)
10770 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010771 {
10772 rettv->vval.v_number = idx;
10773 break;
10774 }
10775 }
10776}
10777
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778static int inputsecret_flag = 0;
10779
10780/*
10781 * "input()" function
10782 * Also handles inputsecret() when inputsecret is set.
10783 */
10784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010785f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010786 typval_T *argvars;
10787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010789 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010790 char_u *p = NULL;
10791 int c;
10792 char_u buf[NUMBUFLEN];
10793 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010794 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010796 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797
10798#ifdef NO_CONSOLE_INPUT
10799 /* While starting up, there is no place to enter text. */
10800 if (no_console_input())
10801 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010802 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803 return;
10804 }
10805#endif
10806
10807 cmd_silent = FALSE; /* Want to see the prompt. */
10808 if (prompt != NULL)
10809 {
10810 /* Only the part of the message after the last NL is considered as
10811 * prompt for the command line */
10812 p = vim_strrchr(prompt, '\n');
10813 if (p == NULL)
10814 p = prompt;
10815 else
10816 {
10817 ++p;
10818 c = *p;
10819 *p = NUL;
10820 msg_start();
10821 msg_clr_eos();
10822 msg_puts_attr(prompt, echo_attr);
10823 msg_didout = FALSE;
10824 msg_starthere();
10825 *p = c;
10826 }
10827 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010829 if (argvars[1].v_type != VAR_UNKNOWN)
10830 {
10831 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10832 if (defstr != NULL)
10833 stuffReadbuffSpec(defstr);
10834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010836 if (defstr != NULL)
10837 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
10839
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010840 /* since the user typed this, no need to wait for return */
10841 need_wait_return = FALSE;
10842 msg_didout = FALSE;
10843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010844 cmd_silent = cmd_silent_save;
10845}
10846
10847/*
10848 * "inputdialog()" function
10849 */
10850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010851f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010852 typval_T *argvars;
10853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854{
10855#if defined(FEAT_GUI_TEXTDIALOG)
10856 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10857 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10858 {
10859 char_u *message;
10860 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010861 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010863 message = get_tv_string_chk(&argvars[0]);
10864 if (argvars[1].v_type != VAR_UNKNOWN
10865 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000010866 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 else
10868 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010869 if (message != NULL && defstr != NULL
10870 && do_dialog(VIM_QUESTION, NULL, message,
10871 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010872 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 else
10874 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010875 if (message != NULL && defstr != NULL
10876 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010877 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010878 rettv->vval.v_string = vim_strsave(
10879 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010881 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010883 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010884 }
10885 else
10886#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010887 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888}
10889
10890static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10891
10892/*
10893 * "inputrestore()" function
10894 */
10895/*ARGSUSED*/
10896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010897f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010898 typval_T *argvars;
10899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900{
10901 if (ga_userinput.ga_len > 0)
10902 {
10903 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10905 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010906 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907 }
10908 else if (p_verbose > 1)
10909 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000010910 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010911 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 }
10913}
10914
10915/*
10916 * "inputsave()" function
10917 */
10918/*ARGSUSED*/
10919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010920f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010921 typval_T *argvars;
10922 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923{
10924 /* Add an entry to the stack of typehead storage. */
10925 if (ga_grow(&ga_userinput, 1) == OK)
10926 {
10927 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10928 + ga_userinput.ga_len);
10929 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010930 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 }
10932 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010933 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934}
10935
10936/*
10937 * "inputsecret()" function
10938 */
10939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010940f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010941 typval_T *argvars;
10942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943{
10944 ++cmdline_star;
10945 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010946 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 --cmdline_star;
10948 --inputsecret_flag;
10949}
10950
10951/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010952 * "insert()" function
10953 */
10954 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010955f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010956 typval_T *argvars;
10957 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010958{
10959 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010960 listitem_T *item;
10961 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010962 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010963
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010964 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010965 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010966 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010967 else if ((l = argvars[0].vval.v_list) != NULL
10968 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010969 {
10970 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010971 before = get_tv_number_chk(&argvars[2], &error);
10972 if (error)
10973 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010974
Bram Moolenaar758711c2005-02-02 23:11:38 +000010975 if (before == l->lv_len)
10976 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010977 else
10978 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010979 item = list_find(l, before);
10980 if (item == NULL)
10981 {
10982 EMSGN(_(e_listidx), before);
10983 l = NULL;
10984 }
10985 }
10986 if (l != NULL)
10987 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010988 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010989 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010990 }
10991 }
10992}
10993
10994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995 * "isdirectory()" function
10996 */
10997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010998f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010999 typval_T *argvars;
11000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011002 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003}
11004
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011005/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011006 * "islocked()" function
11007 */
11008 static void
11009f_islocked(argvars, rettv)
11010 typval_T *argvars;
11011 typval_T *rettv;
11012{
11013 lval_T lv;
11014 char_u *end;
11015 dictitem_T *di;
11016
11017 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000011018 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
11019 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011020 if (end != NULL && lv.ll_name != NULL)
11021 {
11022 if (*end != NUL)
11023 EMSG(_(e_trailing));
11024 else
11025 {
11026 if (lv.ll_tv == NULL)
11027 {
11028 if (check_changedtick(lv.ll_name))
11029 rettv->vval.v_number = 1; /* always locked */
11030 else
11031 {
11032 di = find_var(lv.ll_name, NULL);
11033 if (di != NULL)
11034 {
11035 /* Consider a variable locked when:
11036 * 1. the variable itself is locked
11037 * 2. the value of the variable is locked.
11038 * 3. the List or Dict value is locked.
11039 */
11040 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
11041 || tv_islocked(&di->di_tv));
11042 }
11043 }
11044 }
11045 else if (lv.ll_range)
11046 EMSG(_("E745: Range not allowed"));
11047 else if (lv.ll_newkey != NULL)
11048 EMSG2(_(e_dictkey), lv.ll_newkey);
11049 else if (lv.ll_list != NULL)
11050 /* List item. */
11051 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
11052 else
11053 /* Dictionary item. */
11054 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
11055 }
11056 }
11057
11058 clear_lval(&lv);
11059}
11060
Bram Moolenaar33570922005-01-25 22:26:29 +000011061static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011062
11063/*
11064 * Turn a dict into a list:
11065 * "what" == 0: list of keys
11066 * "what" == 1: list of values
11067 * "what" == 2: list of items
11068 */
11069 static void
11070dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000011071 typval_T *argvars;
11072 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011073 int what;
11074{
Bram Moolenaar33570922005-01-25 22:26:29 +000011075 list_T *l;
11076 list_T *l2;
11077 dictitem_T *di;
11078 hashitem_T *hi;
11079 listitem_T *li;
11080 listitem_T *li2;
11081 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011082 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011083
11084 rettv->vval.v_number = 0;
11085 if (argvars[0].v_type != VAR_DICT)
11086 {
11087 EMSG(_(e_dictreq));
11088 return;
11089 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011090 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011091 return;
11092
11093 l = list_alloc();
11094 if (l == NULL)
11095 return;
11096 rettv->v_type = VAR_LIST;
11097 rettv->vval.v_list = l;
11098 ++l->lv_refcount;
11099
Bram Moolenaar33570922005-01-25 22:26:29 +000011100 todo = d->dv_hashtab.ht_used;
11101 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011102 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011103 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011104 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011105 --todo;
11106 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011107
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011108 li = listitem_alloc();
11109 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011110 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011111 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011112
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011113 if (what == 0)
11114 {
11115 /* keys() */
11116 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011117 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011118 li->li_tv.vval.v_string = vim_strsave(di->di_key);
11119 }
11120 else if (what == 1)
11121 {
11122 /* values() */
11123 copy_tv(&di->di_tv, &li->li_tv);
11124 }
11125 else
11126 {
11127 /* items() */
11128 l2 = list_alloc();
11129 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011130 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011131 li->li_tv.vval.v_list = l2;
11132 if (l2 == NULL)
11133 break;
11134 ++l2->lv_refcount;
11135
11136 li2 = listitem_alloc();
11137 if (li2 == NULL)
11138 break;
11139 list_append(l2, li2);
11140 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011141 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011142 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
11143
11144 li2 = listitem_alloc();
11145 if (li2 == NULL)
11146 break;
11147 list_append(l2, li2);
11148 copy_tv(&di->di_tv, &li2->li_tv);
11149 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011150 }
11151 }
11152}
11153
11154/*
11155 * "items(dict)" function
11156 */
11157 static void
11158f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011159 typval_T *argvars;
11160 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011161{
11162 dict_list(argvars, rettv, 2);
11163}
11164
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011166 * "join()" function
11167 */
11168 static void
11169f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011170 typval_T *argvars;
11171 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011172{
11173 garray_T ga;
11174 char_u *sep;
11175
11176 rettv->vval.v_number = 0;
11177 if (argvars[0].v_type != VAR_LIST)
11178 {
11179 EMSG(_(e_listreq));
11180 return;
11181 }
11182 if (argvars[0].vval.v_list == NULL)
11183 return;
11184 if (argvars[1].v_type == VAR_UNKNOWN)
11185 sep = (char_u *)" ";
11186 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011187 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011188
11189 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011190
11191 if (sep != NULL)
11192 {
11193 ga_init2(&ga, (int)sizeof(char), 80);
11194 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
11195 ga_append(&ga, NUL);
11196 rettv->vval.v_string = (char_u *)ga.ga_data;
11197 }
11198 else
11199 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011200}
11201
11202/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011203 * "keys()" function
11204 */
11205 static void
11206f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011207 typval_T *argvars;
11208 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011209{
11210 dict_list(argvars, rettv, 0);
11211}
11212
11213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214 * "last_buffer_nr()" function.
11215 */
11216/*ARGSUSED*/
11217 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011219 typval_T *argvars;
11220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221{
11222 int n = 0;
11223 buf_T *buf;
11224
11225 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11226 if (n < buf->b_fnum)
11227 n = buf->b_fnum;
11228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011229 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011230}
11231
11232/*
11233 * "len()" function
11234 */
11235 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011236f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011237 typval_T *argvars;
11238 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011239{
11240 switch (argvars[0].v_type)
11241 {
11242 case VAR_STRING:
11243 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011244 rettv->vval.v_number = (varnumber_T)STRLEN(
11245 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011246 break;
11247 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011248 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011249 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011250 case VAR_DICT:
11251 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11252 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011253 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011254 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011255 break;
11256 }
11257}
11258
Bram Moolenaar33570922005-01-25 22:26:29 +000011259static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011260
11261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011262libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011263 typval_T *argvars;
11264 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011265 int type;
11266{
11267#ifdef FEAT_LIBCALL
11268 char_u *string_in;
11269 char_u **string_result;
11270 int nr_result;
11271#endif
11272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011274 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011275 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011276 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011277 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011278
11279 if (check_restricted() || check_secure())
11280 return;
11281
11282#ifdef FEAT_LIBCALL
11283 /* The first two args must be strings, otherwise its meaningless */
11284 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11285 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011286 string_in = NULL;
11287 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011288 string_in = argvars[2].vval.v_string;
11289 if (type == VAR_NUMBER)
11290 string_result = NULL;
11291 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011292 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011293 if (mch_libcall(argvars[0].vval.v_string,
11294 argvars[1].vval.v_string,
11295 string_in,
11296 argvars[2].vval.v_number,
11297 string_result,
11298 &nr_result) == OK
11299 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011300 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011301 }
11302#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303}
11304
11305/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011306 * "libcall()" function
11307 */
11308 static void
11309f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011310 typval_T *argvars;
11311 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011312{
11313 libcall_common(argvars, rettv, VAR_STRING);
11314}
11315
11316/*
11317 * "libcallnr()" function
11318 */
11319 static void
11320f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011321 typval_T *argvars;
11322 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011323{
11324 libcall_common(argvars, rettv, VAR_NUMBER);
11325}
11326
11327/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328 * "line(string)" function
11329 */
11330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011331f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011332 typval_T *argvars;
11333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334{
11335 linenr_T lnum = 0;
11336 pos_T *fp;
11337
11338 fp = var2fpos(&argvars[0], TRUE);
11339 if (fp != NULL)
11340 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011341 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342}
11343
11344/*
11345 * "line2byte(lnum)" function
11346 */
11347/*ARGSUSED*/
11348 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011349f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011350 typval_T *argvars;
11351 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352{
11353#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011354 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355#else
11356 linenr_T lnum;
11357
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011358 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011360 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011362 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11363 if (rettv->vval.v_number >= 0)
11364 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365#endif
11366}
11367
11368/*
11369 * "lispindent(lnum)" function
11370 */
11371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011372f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011373 typval_T *argvars;
11374 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375{
11376#ifdef FEAT_LISP
11377 pos_T pos;
11378 linenr_T lnum;
11379
11380 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011381 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11383 {
11384 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011385 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386 curwin->w_cursor = pos;
11387 }
11388 else
11389#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391}
11392
11393/*
11394 * "localtime()" function
11395 */
11396/*ARGSUSED*/
11397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011398f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011399 typval_T *argvars;
11400 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011402 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403}
11404
Bram Moolenaar33570922005-01-25 22:26:29 +000011405static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406
11407 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011408get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011409 typval_T *argvars;
11410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411 int exact;
11412{
11413 char_u *keys;
11414 char_u *which;
11415 char_u buf[NUMBUFLEN];
11416 char_u *keys_buf = NULL;
11417 char_u *rhs;
11418 int mode;
11419 garray_T ga;
11420
11421 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011422 rettv->v_type = VAR_STRING;
11423 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011425 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011426 if (*keys == NUL)
11427 return;
11428
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011429 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011430 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011431 else
11432 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011433 if (which == NULL)
11434 return;
11435
Bram Moolenaar071d4272004-06-13 20:20:40 +000011436 mode = get_map_mode(&which, 0);
11437
11438 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011439 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011440 vim_free(keys_buf);
11441 if (rhs != NULL)
11442 {
11443 ga_init(&ga);
11444 ga.ga_itemsize = 1;
11445 ga.ga_growsize = 40;
11446
11447 while (*rhs != NUL)
11448 ga_concat(&ga, str2special(&rhs, FALSE));
11449
11450 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011451 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452 }
11453}
11454
11455/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011456 * "map()" function
11457 */
11458 static void
11459f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011460 typval_T *argvars;
11461 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011462{
11463 filter_map(argvars, rettv, TRUE);
11464}
11465
11466/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011467 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468 */
11469 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011470f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011471 typval_T *argvars;
11472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011474 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475}
11476
11477/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011478 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479 */
11480 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011481f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011482 typval_T *argvars;
11483 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011485 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486}
11487
Bram Moolenaar33570922005-01-25 22:26:29 +000011488static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489
11490 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011491find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011492 typval_T *argvars;
11493 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494 int type;
11495{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011496 char_u *str = NULL;
11497 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498 char_u *pat;
11499 regmatch_T regmatch;
11500 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011501 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011502 char_u *save_cpo;
11503 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011504 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011505 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011506 list_T *l = NULL;
11507 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011508 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011509 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510
11511 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11512 save_cpo = p_cpo;
11513 p_cpo = (char_u *)"";
11514
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011515 rettv->vval.v_number = -1;
11516 if (type == 3)
11517 {
11518 /* return empty list when there are no matches */
11519 if ((rettv->vval.v_list = list_alloc()) == NULL)
11520 goto theend;
11521 rettv->v_type = VAR_LIST;
11522 ++rettv->vval.v_list->lv_refcount;
11523 }
11524 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011526 rettv->v_type = VAR_STRING;
11527 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011529
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011530 if (argvars[0].v_type == VAR_LIST)
11531 {
11532 if ((l = argvars[0].vval.v_list) == NULL)
11533 goto theend;
11534 li = l->lv_first;
11535 }
11536 else
11537 expr = str = get_tv_string(&argvars[0]);
11538
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011539 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11540 if (pat == NULL)
11541 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011542
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011543 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011545 int error = FALSE;
11546
11547 start = get_tv_number_chk(&argvars[2], &error);
11548 if (error)
11549 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011550 if (l != NULL)
11551 {
11552 li = list_find(l, start);
11553 if (li == NULL)
11554 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011555 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011556 }
11557 else
11558 {
11559 if (start < 0)
11560 start = 0;
11561 if (start > (long)STRLEN(str))
11562 goto theend;
11563 str += start;
11564 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011565
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011566 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011567 nth = get_tv_number_chk(&argvars[3], &error);
11568 if (error)
11569 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011570 }
11571
11572 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11573 if (regmatch.regprog != NULL)
11574 {
11575 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011576
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011577 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011578 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011579 if (l != NULL)
11580 {
11581 if (li == NULL)
11582 {
11583 match = FALSE;
11584 break;
11585 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011586 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011587 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011588 if (str == NULL)
11589 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011590 }
11591
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011592 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011593
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011594 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011595 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011596 if (l == NULL && !match)
11597 break;
11598
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011599 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011600 if (l != NULL)
11601 {
11602 li = li->li_next;
11603 ++idx;
11604 }
11605 else
11606 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011607#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000011608 str = regmatch.startp[0] + (*mb_ptr2len)(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011609#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011610 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011611#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011612 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011613 }
11614
11615 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011616 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011617 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011618 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011619 int i;
11620
11621 /* return list with matched string and submatches */
11622 for (i = 0; i < NSUBEXP; ++i)
11623 {
11624 if (regmatch.endp[i] == NULL)
11625 break;
11626 li = listitem_alloc();
11627 if (li == NULL)
11628 break;
11629 li->li_tv.v_type = VAR_STRING;
11630 li->li_tv.v_lock = 0;
11631 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
11632 (int)(regmatch.endp[i] - regmatch.startp[i]));
11633 list_append(rettv->vval.v_list, li);
11634 }
11635 }
11636 else if (type == 2)
11637 {
11638 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011639 if (l != NULL)
11640 copy_tv(&li->li_tv, rettv);
11641 else
11642 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011643 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011644 }
11645 else if (l != NULL)
11646 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011647 else
11648 {
11649 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011650 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011651 (varnumber_T)(regmatch.startp[0] - str);
11652 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011653 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011655 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011656 }
11657 }
11658 vim_free(regmatch.regprog);
11659 }
11660
11661theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011662 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011663 p_cpo = save_cpo;
11664}
11665
11666/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011667 * "match()" function
11668 */
11669 static void
11670f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011671 typval_T *argvars;
11672 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011673{
11674 find_some_match(argvars, rettv, 1);
11675}
11676
11677/*
11678 * "matchend()" function
11679 */
11680 static void
11681f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011682 typval_T *argvars;
11683 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011684{
11685 find_some_match(argvars, rettv, 0);
11686}
11687
11688/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011689 * "matchlist()" function
11690 */
11691 static void
11692f_matchlist(argvars, rettv)
11693 typval_T *argvars;
11694 typval_T *rettv;
11695{
11696 find_some_match(argvars, rettv, 3);
11697}
11698
11699/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011700 * "matchstr()" function
11701 */
11702 static void
11703f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011704 typval_T *argvars;
11705 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011706{
11707 find_some_match(argvars, rettv, 2);
11708}
11709
Bram Moolenaar33570922005-01-25 22:26:29 +000011710static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011711
11712 static void
11713max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011714 typval_T *argvars;
11715 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011716 int domax;
11717{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011718 long n = 0;
11719 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011720 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011721
11722 if (argvars[0].v_type == VAR_LIST)
11723 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011724 list_T *l;
11725 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011726
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011727 l = argvars[0].vval.v_list;
11728 if (l != NULL)
11729 {
11730 li = l->lv_first;
11731 if (li != NULL)
11732 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011733 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011734 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011735 {
11736 li = li->li_next;
11737 if (li == NULL)
11738 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011739 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011740 if (domax ? i > n : i < n)
11741 n = i;
11742 }
11743 }
11744 }
11745 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011746 else if (argvars[0].v_type == VAR_DICT)
11747 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011748 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011749 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011750 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011751 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011752
11753 d = argvars[0].vval.v_dict;
11754 if (d != NULL)
11755 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011756 todo = d->dv_hashtab.ht_used;
11757 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011758 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011759 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011760 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011761 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011762 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011763 if (first)
11764 {
11765 n = i;
11766 first = FALSE;
11767 }
11768 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011769 n = i;
11770 }
11771 }
11772 }
11773 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011774 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011775 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011776 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011777}
11778
11779/*
11780 * "max()" function
11781 */
11782 static void
11783f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011784 typval_T *argvars;
11785 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011786{
11787 max_min(argvars, rettv, TRUE);
11788}
11789
11790/*
11791 * "min()" function
11792 */
11793 static void
11794f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011795 typval_T *argvars;
11796 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011797{
11798 max_min(argvars, rettv, FALSE);
11799}
11800
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011801static int mkdir_recurse __ARGS((char_u *dir, int prot));
11802
11803/*
11804 * Create the directory in which "dir" is located, and higher levels when
11805 * needed.
11806 */
11807 static int
11808mkdir_recurse(dir, prot)
11809 char_u *dir;
11810 int prot;
11811{
11812 char_u *p;
11813 char_u *updir;
11814 int r = FAIL;
11815
11816 /* Get end of directory name in "dir".
11817 * We're done when it's "/" or "c:/". */
11818 p = gettail_sep(dir);
11819 if (p <= get_past_head(dir))
11820 return OK;
11821
11822 /* If the directory exists we're done. Otherwise: create it.*/
11823 updir = vim_strnsave(dir, (int)(p - dir));
11824 if (updir == NULL)
11825 return FAIL;
11826 if (mch_isdir(updir))
11827 r = OK;
11828 else if (mkdir_recurse(updir, prot) == OK)
11829 r = vim_mkdir_emsg(updir, prot);
11830 vim_free(updir);
11831 return r;
11832}
11833
11834#ifdef vim_mkdir
11835/*
11836 * "mkdir()" function
11837 */
11838 static void
11839f_mkdir(argvars, rettv)
11840 typval_T *argvars;
11841 typval_T *rettv;
11842{
11843 char_u *dir;
11844 char_u buf[NUMBUFLEN];
11845 int prot = 0755;
11846
11847 rettv->vval.v_number = FAIL;
11848 if (check_restricted() || check_secure())
11849 return;
11850
11851 dir = get_tv_string_buf(&argvars[0], buf);
11852 if (argvars[1].v_type != VAR_UNKNOWN)
11853 {
11854 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011855 prot = get_tv_number_chk(&argvars[2], NULL);
11856 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011857 mkdir_recurse(dir, prot);
11858 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011859 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011860}
11861#endif
11862
Bram Moolenaar0d660222005-01-07 21:51:51 +000011863/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864 * "mode()" function
11865 */
11866/*ARGSUSED*/
11867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011868f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011869 typval_T *argvars;
11870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011871{
11872 char_u buf[2];
11873
11874#ifdef FEAT_VISUAL
11875 if (VIsual_active)
11876 {
11877 if (VIsual_select)
11878 buf[0] = VIsual_mode + 's' - 'v';
11879 else
11880 buf[0] = VIsual_mode;
11881 }
11882 else
11883#endif
11884 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11885 buf[0] = 'r';
11886 else if (State & INSERT)
11887 {
11888 if (State & REPLACE_FLAG)
11889 buf[0] = 'R';
11890 else
11891 buf[0] = 'i';
11892 }
11893 else if (State & CMDLINE)
11894 buf[0] = 'c';
11895 else
11896 buf[0] = 'n';
11897
11898 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011899 rettv->vval.v_string = vim_strsave(buf);
11900 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011901}
11902
11903/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011904 * "nextnonblank()" function
11905 */
11906 static void
11907f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011908 typval_T *argvars;
11909 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011910{
11911 linenr_T lnum;
11912
11913 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11914 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011915 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011916 {
11917 lnum = 0;
11918 break;
11919 }
11920 if (*skipwhite(ml_get(lnum)) != NUL)
11921 break;
11922 }
11923 rettv->vval.v_number = lnum;
11924}
11925
11926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011927 * "nr2char()" function
11928 */
11929 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011930f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011931 typval_T *argvars;
11932 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011933{
11934 char_u buf[NUMBUFLEN];
11935
11936#ifdef FEAT_MBYTE
11937 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011938 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011939 else
11940#endif
11941 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011942 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943 buf[1] = NUL;
11944 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011945 rettv->v_type = VAR_STRING;
11946 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011947}
11948
11949/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011950 * "prevnonblank()" function
11951 */
11952 static void
11953f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011954 typval_T *argvars;
11955 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011956{
11957 linenr_T lnum;
11958
11959 lnum = get_tv_lnum(argvars);
11960 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11961 lnum = 0;
11962 else
11963 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11964 --lnum;
11965 rettv->vval.v_number = lnum;
11966}
11967
Bram Moolenaara6c840d2005-08-22 22:59:46 +000011968#ifdef HAVE_STDARG_H
11969/* This dummy va_list is here because:
11970 * - passing a NULL pointer doesn't work when va_list isn't a pointer
11971 * - locally in the function results in a "used before set" warning
11972 * - using va_start() to initialize it gives "function with fixed args" error */
11973static va_list ap;
11974#endif
11975
Bram Moolenaar8c711452005-01-14 21:53:12 +000011976/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000011977 * "printf()" function
11978 */
11979 static void
11980f_printf(argvars, rettv)
11981 typval_T *argvars;
11982 typval_T *rettv;
11983{
11984 rettv->v_type = VAR_STRING;
11985 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000011986#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000011987 {
11988 char_u buf[NUMBUFLEN];
11989 int len;
11990 char_u *s;
11991 int saved_did_emsg = did_emsg;
11992 char *fmt;
11993
11994 /* Get the required length, allocate the buffer and do it for real. */
11995 did_emsg = FALSE;
11996 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000011997 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000011998 if (!did_emsg)
11999 {
12000 s = alloc(len + 1);
12001 if (s != NULL)
12002 {
12003 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000012004 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000012005 }
12006 }
12007 did_emsg |= saved_did_emsg;
12008 }
12009#endif
12010}
12011
12012/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012013 * "range()" function
12014 */
12015 static void
12016f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012017 typval_T *argvars;
12018 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012019{
12020 long start;
12021 long end;
12022 long stride = 1;
12023 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012024 list_T *l;
12025 listitem_T *li;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012026 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012028 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012029 if (argvars[1].v_type == VAR_UNKNOWN)
12030 {
12031 end = start - 1;
12032 start = 0;
12033 }
12034 else
12035 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012036 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012037 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012038 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000012039 }
12040
12041 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012042 if (error)
12043 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000012044 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012045 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000012046 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012047 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000012048 else
12049 {
12050 l = list_alloc();
12051 if (l != NULL)
12052 {
12053 rettv->v_type = VAR_LIST;
12054 rettv->vval.v_list = l;
12055 ++l->lv_refcount;
12056
12057 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
12058 {
12059 li = listitem_alloc();
12060 if (li == NULL)
12061 break;
12062 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012063 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012064 li->li_tv.vval.v_number = i;
12065 list_append(l, li);
12066 }
12067 }
12068 }
12069}
12070
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012071/*
12072 * "readfile()" function
12073 */
12074 static void
12075f_readfile(argvars, rettv)
12076 typval_T *argvars;
12077 typval_T *rettv;
12078{
12079 int binary = FALSE;
12080 char_u *fname;
12081 FILE *fd;
12082 list_T *l;
12083 listitem_T *li;
12084#define FREAD_SIZE 200 /* optimized for text lines */
12085 char_u buf[FREAD_SIZE];
12086 int readlen; /* size of last fread() */
12087 int buflen; /* nr of valid chars in buf[] */
12088 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
12089 int tolist; /* first byte in buf[] still to be put in list */
12090 int chop; /* how many CR to chop off */
12091 char_u *prev = NULL; /* previously read bytes, if any */
12092 int prevlen = 0; /* length of "prev" if not NULL */
12093 char_u *s;
12094 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012095 long maxline = MAXLNUM;
12096 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012097
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012098 if (argvars[1].v_type != VAR_UNKNOWN)
12099 {
12100 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
12101 binary = TRUE;
12102 if (argvars[2].v_type != VAR_UNKNOWN)
12103 maxline = get_tv_number(&argvars[2]);
12104 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012105
12106 l = list_alloc();
12107 if (l == NULL)
12108 return;
12109 rettv->v_type = VAR_LIST;
12110 rettv->vval.v_list = l;
12111 l->lv_refcount = 1;
12112
12113 /* Always open the file in binary mode, library functions have a mind of
12114 * their own about CR-LF conversion. */
12115 fname = get_tv_string(&argvars[0]);
12116 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
12117 {
12118 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
12119 return;
12120 }
12121
12122 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012123 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012124 {
12125 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
12126 buflen = filtd + readlen;
12127 tolist = 0;
12128 for ( ; filtd < buflen || readlen <= 0; ++filtd)
12129 {
12130 if (buf[filtd] == '\n' || readlen <= 0)
12131 {
12132 /* Only when in binary mode add an empty list item when the
12133 * last line ends in a '\n'. */
12134 if (!binary && readlen == 0 && filtd == 0)
12135 break;
12136
12137 /* Found end-of-line or end-of-file: add a text line to the
12138 * list. */
12139 chop = 0;
12140 if (!binary)
12141 while (filtd - chop - 1 >= tolist
12142 && buf[filtd - chop - 1] == '\r')
12143 ++chop;
12144 len = filtd - tolist - chop;
12145 if (prev == NULL)
12146 s = vim_strnsave(buf + tolist, len);
12147 else
12148 {
12149 s = alloc((unsigned)(prevlen + len + 1));
12150 if (s != NULL)
12151 {
12152 mch_memmove(s, prev, prevlen);
12153 vim_free(prev);
12154 prev = NULL;
12155 mch_memmove(s + prevlen, buf + tolist, len);
12156 s[prevlen + len] = NUL;
12157 }
12158 }
12159 tolist = filtd + 1;
12160
12161 li = listitem_alloc();
12162 if (li == NULL)
12163 {
12164 vim_free(s);
12165 break;
12166 }
12167 li->li_tv.v_type = VAR_STRING;
12168 li->li_tv.v_lock = 0;
12169 li->li_tv.vval.v_string = s;
12170 list_append(l, li);
12171
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012172 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012173 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012174 if (readlen <= 0)
12175 break;
12176 }
12177 else if (buf[filtd] == NUL)
12178 buf[filtd] = '\n';
12179 }
12180 if (readlen <= 0)
12181 break;
12182
12183 if (tolist == 0)
12184 {
12185 /* "buf" is full, need to move text to an allocated buffer */
12186 if (prev == NULL)
12187 {
12188 prev = vim_strnsave(buf, buflen);
12189 prevlen = buflen;
12190 }
12191 else
12192 {
12193 s = alloc((unsigned)(prevlen + buflen));
12194 if (s != NULL)
12195 {
12196 mch_memmove(s, prev, prevlen);
12197 mch_memmove(s + prevlen, buf, buflen);
12198 vim_free(prev);
12199 prev = s;
12200 prevlen += buflen;
12201 }
12202 }
12203 filtd = 0;
12204 }
12205 else
12206 {
12207 mch_memmove(buf, buf + tolist, buflen - tolist);
12208 filtd -= tolist;
12209 }
12210 }
12211
Bram Moolenaarb982ca52005-03-28 21:02:15 +000012212 /*
12213 * For a negative line count use only the lines at the end of the file,
12214 * free the rest.
12215 */
12216 if (maxline < 0)
12217 while (cnt > -maxline)
12218 {
12219 listitem_remove(l, l->lv_first);
12220 --cnt;
12221 }
12222
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012223 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000012224 fclose(fd);
12225}
12226
12227
Bram Moolenaar0d660222005-01-07 21:51:51 +000012228#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12229static void make_connection __ARGS((void));
12230static int check_connection __ARGS((void));
12231
12232 static void
12233make_connection()
12234{
12235 if (X_DISPLAY == NULL
12236# ifdef FEAT_GUI
12237 && !gui.in_use
12238# endif
12239 )
12240 {
12241 x_force_connect = TRUE;
12242 setup_term_clip();
12243 x_force_connect = FALSE;
12244 }
12245}
12246
12247 static int
12248check_connection()
12249{
12250 make_connection();
12251 if (X_DISPLAY == NULL)
12252 {
12253 EMSG(_("E240: No connection to Vim server"));
12254 return FAIL;
12255 }
12256 return OK;
12257}
12258#endif
12259
12260#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012261static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012262
12263 static void
12264remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012265 typval_T *argvars;
12266 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012267 int expr;
12268{
12269 char_u *server_name;
12270 char_u *keys;
12271 char_u *r = NULL;
12272 char_u buf[NUMBUFLEN];
12273# ifdef WIN32
12274 HWND w;
12275# else
12276 Window w;
12277# endif
12278
12279 if (check_restricted() || check_secure())
12280 return;
12281
12282# ifdef FEAT_X11
12283 if (check_connection() == FAIL)
12284 return;
12285# endif
12286
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012287 server_name = get_tv_string_chk(&argvars[0]);
12288 if (server_name == NULL)
12289 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012290 keys = get_tv_string_buf(&argvars[1], buf);
12291# ifdef WIN32
12292 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12293# else
12294 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12295 < 0)
12296# endif
12297 {
12298 if (r != NULL)
12299 EMSG(r); /* sending worked but evaluation failed */
12300 else
12301 EMSG2(_("E241: Unable to send to %s"), server_name);
12302 return;
12303 }
12304
12305 rettv->vval.v_string = r;
12306
12307 if (argvars[2].v_type != VAR_UNKNOWN)
12308 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012309 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012310 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012311 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012312
12313 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012314 v.di_tv.v_type = VAR_STRING;
12315 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012316 idvar = get_tv_string_chk(&argvars[2]);
12317 if (idvar != NULL)
12318 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012319 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012320 }
12321}
12322#endif
12323
12324/*
12325 * "remote_expr()" function
12326 */
12327/*ARGSUSED*/
12328 static void
12329f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012330 typval_T *argvars;
12331 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012332{
12333 rettv->v_type = VAR_STRING;
12334 rettv->vval.v_string = NULL;
12335#ifdef FEAT_CLIENTSERVER
12336 remote_common(argvars, rettv, TRUE);
12337#endif
12338}
12339
12340/*
12341 * "remote_foreground()" function
12342 */
12343/*ARGSUSED*/
12344 static void
12345f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012346 typval_T *argvars;
12347 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012348{
12349 rettv->vval.v_number = 0;
12350#ifdef FEAT_CLIENTSERVER
12351# ifdef WIN32
12352 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012353 {
12354 char_u *server_name = get_tv_string_chk(&argvars[0]);
12355
12356 if (server_name != NULL)
12357 serverForeground(server_name);
12358 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012359# else
12360 /* Send a foreground() expression to the server. */
12361 argvars[1].v_type = VAR_STRING;
12362 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12363 argvars[2].v_type = VAR_UNKNOWN;
12364 remote_common(argvars, rettv, TRUE);
12365 vim_free(argvars[1].vval.v_string);
12366# endif
12367#endif
12368}
12369
12370/*ARGSUSED*/
12371 static void
12372f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012373 typval_T *argvars;
12374 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012375{
12376#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012377 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012378 char_u *s = NULL;
12379# ifdef WIN32
12380 int n = 0;
12381# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012382 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012383
12384 if (check_restricted() || check_secure())
12385 {
12386 rettv->vval.v_number = -1;
12387 return;
12388 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012389 serverid = get_tv_string_chk(&argvars[0]);
12390 if (serverid == NULL)
12391 {
12392 rettv->vval.v_number = -1;
12393 return; /* type error; errmsg already given */
12394 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012395# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012396 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012397 if (n == 0)
12398 rettv->vval.v_number = -1;
12399 else
12400 {
12401 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12402 rettv->vval.v_number = (s != NULL);
12403 }
12404# else
12405 rettv->vval.v_number = 0;
12406 if (check_connection() == FAIL)
12407 return;
12408
12409 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012410 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012411# endif
12412
12413 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12414 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012415 char_u *retvar;
12416
Bram Moolenaar33570922005-01-25 22:26:29 +000012417 v.di_tv.v_type = VAR_STRING;
12418 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012419 retvar = get_tv_string_chk(&argvars[1]);
12420 if (retvar != NULL)
12421 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012422 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012423 }
12424#else
12425 rettv->vval.v_number = -1;
12426#endif
12427}
12428
12429/*ARGSUSED*/
12430 static void
12431f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012432 typval_T *argvars;
12433 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012434{
12435 char_u *r = NULL;
12436
12437#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012438 char_u *serverid = get_tv_string_chk(&argvars[0]);
12439
12440 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012441 {
12442# ifdef WIN32
12443 /* The server's HWND is encoded in the 'id' parameter */
12444 int n = 0;
12445
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012446 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012447 if (n != 0)
12448 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12449 if (r == NULL)
12450# else
12451 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012452 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012453# endif
12454 EMSG(_("E277: Unable to read a server reply"));
12455 }
12456#endif
12457 rettv->v_type = VAR_STRING;
12458 rettv->vval.v_string = r;
12459}
12460
12461/*
12462 * "remote_send()" function
12463 */
12464/*ARGSUSED*/
12465 static void
12466f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012467 typval_T *argvars;
12468 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012469{
12470 rettv->v_type = VAR_STRING;
12471 rettv->vval.v_string = NULL;
12472#ifdef FEAT_CLIENTSERVER
12473 remote_common(argvars, rettv, FALSE);
12474#endif
12475}
12476
12477/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012478 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012479 */
12480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012481f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012482 typval_T *argvars;
12483 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012484{
Bram Moolenaar33570922005-01-25 22:26:29 +000012485 list_T *l;
12486 listitem_T *item, *item2;
12487 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012488 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012489 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012490 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012491 dict_T *d;
12492 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012493
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012494 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012495 if (argvars[0].v_type == VAR_DICT)
12496 {
12497 if (argvars[2].v_type != VAR_UNKNOWN)
12498 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012499 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012500 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012502 key = get_tv_string_chk(&argvars[1]);
12503 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012504 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012505 di = dict_find(d, key, -1);
12506 if (di == NULL)
12507 EMSG2(_(e_dictkey), key);
12508 else
12509 {
12510 *rettv = di->di_tv;
12511 init_tv(&di->di_tv);
12512 dictitem_remove(d, di);
12513 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012514 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012515 }
12516 }
12517 else if (argvars[0].v_type != VAR_LIST)
12518 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012519 else if ((l = argvars[0].vval.v_list) != NULL
12520 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012522 int error = FALSE;
12523
12524 idx = get_tv_number_chk(&argvars[1], &error);
12525 if (error)
12526 ; /* type error: do nothing, errmsg already given */
12527 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012528 EMSGN(_(e_listidx), idx);
12529 else
12530 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012531 if (argvars[2].v_type == VAR_UNKNOWN)
12532 {
12533 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012534 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012535 *rettv = item->li_tv;
12536 vim_free(item);
12537 }
12538 else
12539 {
12540 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012541 end = get_tv_number_chk(&argvars[2], &error);
12542 if (error)
12543 ; /* type error: do nothing */
12544 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012545 EMSGN(_(e_listidx), end);
12546 else
12547 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012548 int cnt = 0;
12549
12550 for (li = item; li != NULL; li = li->li_next)
12551 {
12552 ++cnt;
12553 if (li == item2)
12554 break;
12555 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012556 if (li == NULL) /* didn't find "item2" after "item" */
12557 EMSG(_(e_invrange));
12558 else
12559 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012560 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012561 l = list_alloc();
12562 if (l != NULL)
12563 {
12564 rettv->v_type = VAR_LIST;
12565 rettv->vval.v_list = l;
12566 l->lv_first = item;
12567 l->lv_last = item2;
12568 l->lv_refcount = 1;
12569 item->li_prev = NULL;
12570 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012571 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012572 }
12573 }
12574 }
12575 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012576 }
12577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012578}
12579
12580/*
12581 * "rename({from}, {to})" function
12582 */
12583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012584f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012585 typval_T *argvars;
12586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012587{
12588 char_u buf[NUMBUFLEN];
12589
12590 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012591 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012593 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12594 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595}
12596
12597/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012598 * "repeat()" function
12599 */
12600/*ARGSUSED*/
12601 static void
12602f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012603 typval_T *argvars;
12604 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012605{
12606 char_u *p;
12607 int n;
12608 int slen;
12609 int len;
12610 char_u *r;
12611 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012612 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012613
12614 n = get_tv_number(&argvars[1]);
12615 if (argvars[0].v_type == VAR_LIST)
12616 {
12617 l = list_alloc();
12618 if (l != NULL && argvars[0].vval.v_list != NULL)
12619 {
12620 l->lv_refcount = 1;
12621 while (n-- > 0)
12622 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12623 break;
12624 }
12625 rettv->v_type = VAR_LIST;
12626 rettv->vval.v_list = l;
12627 }
12628 else
12629 {
12630 p = get_tv_string(&argvars[0]);
12631 rettv->v_type = VAR_STRING;
12632 rettv->vval.v_string = NULL;
12633
12634 slen = (int)STRLEN(p);
12635 len = slen * n;
12636 if (len <= 0)
12637 return;
12638
12639 r = alloc(len + 1);
12640 if (r != NULL)
12641 {
12642 for (i = 0; i < n; i++)
12643 mch_memmove(r + i * slen, p, (size_t)slen);
12644 r[len] = NUL;
12645 }
12646
12647 rettv->vval.v_string = r;
12648 }
12649}
12650
12651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652 * "resolve()" function
12653 */
12654 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012655f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012656 typval_T *argvars;
12657 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012658{
12659 char_u *p;
12660
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012661 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662#ifdef FEAT_SHORTCUT
12663 {
12664 char_u *v = NULL;
12665
12666 v = mch_resolve_shortcut(p);
12667 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012668 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012670 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012671 }
12672#else
12673# ifdef HAVE_READLINK
12674 {
12675 char_u buf[MAXPATHL + 1];
12676 char_u *cpy;
12677 int len;
12678 char_u *remain = NULL;
12679 char_u *q;
12680 int is_relative_to_current = FALSE;
12681 int has_trailing_pathsep = FALSE;
12682 int limit = 100;
12683
12684 p = vim_strsave(p);
12685
12686 if (p[0] == '.' && (vim_ispathsep(p[1])
12687 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12688 is_relative_to_current = TRUE;
12689
12690 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012691 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012692 has_trailing_pathsep = TRUE;
12693
12694 q = getnextcomp(p);
12695 if (*q != NUL)
12696 {
12697 /* Separate the first path component in "p", and keep the
12698 * remainder (beginning with the path separator). */
12699 remain = vim_strsave(q - 1);
12700 q[-1] = NUL;
12701 }
12702
12703 for (;;)
12704 {
12705 for (;;)
12706 {
12707 len = readlink((char *)p, (char *)buf, MAXPATHL);
12708 if (len <= 0)
12709 break;
12710 buf[len] = NUL;
12711
12712 if (limit-- == 0)
12713 {
12714 vim_free(p);
12715 vim_free(remain);
12716 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012717 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718 goto fail;
12719 }
12720
12721 /* Ensure that the result will have a trailing path separator
12722 * if the argument has one. */
12723 if (remain == NULL && has_trailing_pathsep)
12724 add_pathsep(buf);
12725
12726 /* Separate the first path component in the link value and
12727 * concatenate the remainders. */
12728 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12729 if (*q != NUL)
12730 {
12731 if (remain == NULL)
12732 remain = vim_strsave(q - 1);
12733 else
12734 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012735 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736 if (cpy != NULL)
12737 {
12738 STRCAT(cpy, remain);
12739 vim_free(remain);
12740 remain = cpy;
12741 }
12742 }
12743 q[-1] = NUL;
12744 }
12745
12746 q = gettail(p);
12747 if (q > p && *q == NUL)
12748 {
12749 /* Ignore trailing path separator. */
12750 q[-1] = NUL;
12751 q = gettail(p);
12752 }
12753 if (q > p && !mch_isFullName(buf))
12754 {
12755 /* symlink is relative to directory of argument */
12756 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12757 if (cpy != NULL)
12758 {
12759 STRCPY(cpy, p);
12760 STRCPY(gettail(cpy), buf);
12761 vim_free(p);
12762 p = cpy;
12763 }
12764 }
12765 else
12766 {
12767 vim_free(p);
12768 p = vim_strsave(buf);
12769 }
12770 }
12771
12772 if (remain == NULL)
12773 break;
12774
12775 /* Append the first path component of "remain" to "p". */
12776 q = getnextcomp(remain + 1);
12777 len = q - remain - (*q != NUL);
12778 cpy = vim_strnsave(p, STRLEN(p) + len);
12779 if (cpy != NULL)
12780 {
12781 STRNCAT(cpy, remain, len);
12782 vim_free(p);
12783 p = cpy;
12784 }
12785 /* Shorten "remain". */
12786 if (*q != NUL)
12787 STRCPY(remain, q - 1);
12788 else
12789 {
12790 vim_free(remain);
12791 remain = NULL;
12792 }
12793 }
12794
12795 /* If the result is a relative path name, make it explicitly relative to
12796 * the current directory if and only if the argument had this form. */
12797 if (!vim_ispathsep(*p))
12798 {
12799 if (is_relative_to_current
12800 && *p != NUL
12801 && !(p[0] == '.'
12802 && (p[1] == NUL
12803 || vim_ispathsep(p[1])
12804 || (p[1] == '.'
12805 && (p[2] == NUL
12806 || vim_ispathsep(p[2]))))))
12807 {
12808 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012809 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810 if (cpy != NULL)
12811 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812 vim_free(p);
12813 p = cpy;
12814 }
12815 }
12816 else if (!is_relative_to_current)
12817 {
12818 /* Strip leading "./". */
12819 q = p;
12820 while (q[0] == '.' && vim_ispathsep(q[1]))
12821 q += 2;
12822 if (q > p)
12823 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12824 }
12825 }
12826
12827 /* Ensure that the result will have no trailing path separator
12828 * if the argument had none. But keep "/" or "//". */
12829 if (!has_trailing_pathsep)
12830 {
12831 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012832 if (after_pathsep(p, q))
12833 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834 }
12835
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012836 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837 }
12838# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012839 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012840# endif
12841#endif
12842
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012843 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844
12845#ifdef HAVE_READLINK
12846fail:
12847#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012848 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849}
12850
12851/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012852 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012853 */
12854 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012855f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012856 typval_T *argvars;
12857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858{
Bram Moolenaar33570922005-01-25 22:26:29 +000012859 list_T *l;
12860 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861
Bram Moolenaar0d660222005-01-07 21:51:51 +000012862 rettv->vval.v_number = 0;
12863 if (argvars[0].v_type != VAR_LIST)
12864 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012865 else if ((l = argvars[0].vval.v_list) != NULL
12866 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012867 {
12868 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012869 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012870 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012871 while (li != NULL)
12872 {
12873 ni = li->li_prev;
12874 list_append(l, li);
12875 li = ni;
12876 }
12877 rettv->vval.v_list = l;
12878 rettv->v_type = VAR_LIST;
12879 ++l->lv_refcount;
12880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881}
12882
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012883#define SP_NOMOVE 1 /* don't move cursor */
12884#define SP_REPEAT 2 /* repeat to find outer pair */
12885#define SP_RETCOUNT 4 /* return matchcount */
Bram Moolenaar231334e2005-07-25 20:46:57 +000012886#define SP_SETPCMARK 8 /* set previous context mark */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012887
Bram Moolenaar33570922005-01-25 22:26:29 +000012888static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012889
12890/*
12891 * Get flags for a search function.
12892 * Possibly sets "p_ws".
12893 * Returns BACKWARD, FORWARD or zero (for an error).
12894 */
12895 static int
12896get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012897 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012898 int *flagsp;
12899{
12900 int dir = FORWARD;
12901 char_u *flags;
12902 char_u nbuf[NUMBUFLEN];
12903 int mask;
12904
12905 if (varp->v_type != VAR_UNKNOWN)
12906 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012907 flags = get_tv_string_buf_chk(varp, nbuf);
12908 if (flags == NULL)
12909 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012910 while (*flags != NUL)
12911 {
12912 switch (*flags)
12913 {
12914 case 'b': dir = BACKWARD; break;
12915 case 'w': p_ws = TRUE; break;
12916 case 'W': p_ws = FALSE; break;
12917 default: mask = 0;
12918 if (flagsp != NULL)
12919 switch (*flags)
12920 {
12921 case 'n': mask = SP_NOMOVE; break;
12922 case 'r': mask = SP_REPEAT; break;
12923 case 'm': mask = SP_RETCOUNT; break;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012924 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012925 }
12926 if (mask == 0)
12927 {
12928 EMSG2(_(e_invarg2), flags);
12929 dir = 0;
12930 }
12931 else
12932 *flagsp |= mask;
12933 }
12934 if (dir == 0)
12935 break;
12936 ++flags;
12937 }
12938 }
12939 return dir;
12940}
12941
Bram Moolenaar071d4272004-06-13 20:20:40 +000012942/*
12943 * "search()" function
12944 */
12945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012946f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012947 typval_T *argvars;
12948 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949{
12950 char_u *pat;
12951 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012952 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012953 int save_p_ws = p_ws;
12954 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012955 int flags = 0;
12956
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012957 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012958
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012959 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012960 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12961 if (dir == 0)
12962 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012963 /*
12964 * This function accepts only SP_NOMOVE and SP_SETPCMARK flags.
12965 * Check to make sure only those flags are set.
12966 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
12967 * flags cannot be set. Check for that condition also.
12968 */
12969 if (((flags & ~(SP_NOMOVE | SP_SETPCMARK)) != 0) ||
12970 ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012971 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012972 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012973 goto theend;
12974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012975
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012976 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012977 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12978 SEARCH_KEEP, RE_SEARCH) != FAIL)
12979 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012980 rettv->vval.v_number = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000012981 if (flags & SP_SETPCMARK)
12982 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983 curwin->w_cursor = pos;
12984 /* "/$" will put the cursor after the end of the line, may need to
12985 * correct that here */
12986 check_cursor();
12987 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012988
12989 /* If 'n' flag is used: restore cursor position. */
12990 if (flags & SP_NOMOVE)
12991 curwin->w_cursor = save_cursor;
12992theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993 p_ws = save_p_ws;
12994}
12995
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000012997 * "searchdecl()" function
12998 */
12999 static void
13000f_searchdecl(argvars, rettv)
13001 typval_T *argvars;
13002 typval_T *rettv;
13003{
13004 int locally = 1;
13005 int error = FALSE;
13006 char_u *name;
13007
13008 rettv->vval.v_number = 1; /* default: FAIL */
13009
13010 name = get_tv_string_chk(&argvars[0]);
13011 if (argvars[1].v_type != VAR_UNKNOWN)
13012 locally = get_tv_number_chk(&argvars[1], &error) == 0;
13013 if (!error && name != NULL)
13014 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
13015 locally, SEARCH_KEEP) == FAIL;
13016}
13017
13018/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019 * "searchpair()" function
13020 */
13021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013022f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013023 typval_T *argvars;
13024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025{
13026 char_u *spat, *mpat, *epat;
13027 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013028 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013029 int dir;
13030 int flags = 0;
13031 char_u nbuf1[NUMBUFLEN];
13032 char_u nbuf2[NUMBUFLEN];
13033 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013035 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013038 spat = get_tv_string_chk(&argvars[0]);
13039 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
13040 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
13041 if (spat == NULL || mpat == NULL || epat == NULL)
13042 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044 /* Handle the optional fourth argument: flags */
13045 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013046 if (dir == 0)
13047 goto theend;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013048 /*
13049 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
13050 */
13051 if ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK))
13052 {
13053 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
13054 goto theend;
13055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056
13057 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013058 if (argvars[3].v_type == VAR_UNKNOWN
13059 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060 skip = (char_u *)"";
13061 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013062 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
13063 if (skip == NULL)
13064 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013066 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
13067
13068theend:
13069 p_ws = save_p_ws;
13070}
13071
13072/*
13073 * Search for a start/middle/end thing.
13074 * Used by searchpair(), see its documentation for the details.
13075 * Returns 0 or -1 for no match,
13076 */
13077 long
13078do_searchpair(spat, mpat, epat, dir, skip, flags)
13079 char_u *spat; /* start pattern */
13080 char_u *mpat; /* middle pattern */
13081 char_u *epat; /* end pattern */
13082 int dir; /* BACKWARD or FORWARD */
13083 char_u *skip; /* skip expression */
13084 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
13085{
13086 char_u *save_cpo;
13087 char_u *pat, *pat2 = NULL, *pat3 = NULL;
13088 long retval = 0;
13089 pos_T pos;
13090 pos_T firstpos;
13091 pos_T foundpos;
13092 pos_T save_cursor;
13093 pos_T save_pos;
13094 int n;
13095 int r;
13096 int nest = 1;
13097 int err;
13098
13099 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13100 save_cpo = p_cpo;
13101 p_cpo = (char_u *)"";
13102
13103 /* Make two search patterns: start/end (pat2, for in nested pairs) and
13104 * start/middle/end (pat3, for the top pair). */
13105 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
13106 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
13107 if (pat2 == NULL || pat3 == NULL)
13108 goto theend;
13109 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
13110 if (*mpat == NUL)
13111 STRCPY(pat3, pat2);
13112 else
13113 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
13114 spat, epat, mpat);
13115
Bram Moolenaar071d4272004-06-13 20:20:40 +000013116 save_cursor = curwin->w_cursor;
13117 pos = curwin->w_cursor;
13118 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013119 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120 pat = pat3;
13121 for (;;)
13122 {
13123 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
13124 SEARCH_KEEP, RE_SEARCH);
13125 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
13126 /* didn't find it or found the first match again: FAIL */
13127 break;
13128
13129 if (firstpos.lnum == 0)
13130 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000013131 if (equalpos(pos, foundpos))
13132 {
13133 /* Found the same position again. Can happen with a pattern that
13134 * has "\zs" at the end and searching backwards. Advance one
13135 * character and try again. */
13136 if (dir == BACKWARD)
13137 decl(&pos);
13138 else
13139 incl(&pos);
13140 }
13141 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142
13143 /* If the skip pattern matches, ignore this match. */
13144 if (*skip != NUL)
13145 {
13146 save_pos = curwin->w_cursor;
13147 curwin->w_cursor = pos;
13148 r = eval_to_bool(skip, &err, NULL, FALSE);
13149 curwin->w_cursor = save_pos;
13150 if (err)
13151 {
13152 /* Evaluating {skip} caused an error, break here. */
13153 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013154 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155 break;
13156 }
13157 if (r)
13158 continue;
13159 }
13160
13161 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
13162 {
13163 /* Found end when searching backwards or start when searching
13164 * forward: nested pair. */
13165 ++nest;
13166 pat = pat2; /* nested, don't search for middle */
13167 }
13168 else
13169 {
13170 /* Found end when searching forward or start when searching
13171 * backward: end of (nested) pair; or found middle in outer pair. */
13172 if (--nest == 1)
13173 pat = pat3; /* outer level, search for middle */
13174 }
13175
13176 if (nest == 0)
13177 {
13178 /* Found the match: return matchcount or line number. */
13179 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013180 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013182 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000013183 if (flags & SP_SETPCMARK)
13184 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013185 curwin->w_cursor = pos;
13186 if (!(flags & SP_REPEAT))
13187 break;
13188 nest = 1; /* search for next unmatched */
13189 }
13190 }
13191
13192 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013193 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194 curwin->w_cursor = save_cursor;
13195
13196theend:
13197 vim_free(pat2);
13198 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000013200
13201 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202}
13203
Bram Moolenaar0d660222005-01-07 21:51:51 +000013204/*ARGSUSED*/
13205 static void
13206f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013207 typval_T *argvars;
13208 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013210#ifdef FEAT_CLIENTSERVER
13211 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013212 char_u *server = get_tv_string_chk(&argvars[0]);
13213 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214
Bram Moolenaar0d660222005-01-07 21:51:51 +000013215 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013216 if (server == NULL || reply == NULL)
13217 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013218 if (check_restricted() || check_secure())
13219 return;
13220# ifdef FEAT_X11
13221 if (check_connection() == FAIL)
13222 return;
13223# endif
13224
13225 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013226 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013227 EMSG(_("E258: Unable to send to client"));
13228 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013230 rettv->vval.v_number = 0;
13231#else
13232 rettv->vval.v_number = -1;
13233#endif
13234}
13235
13236/*ARGSUSED*/
13237 static void
13238f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013239 typval_T *argvars;
13240 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013241{
13242 char_u *r = NULL;
13243
13244#ifdef FEAT_CLIENTSERVER
13245# ifdef WIN32
13246 r = serverGetVimNames();
13247# else
13248 make_connection();
13249 if (X_DISPLAY != NULL)
13250 r = serverGetVimNames(X_DISPLAY);
13251# endif
13252#endif
13253 rettv->v_type = VAR_STRING;
13254 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255}
13256
13257/*
13258 * "setbufvar()" function
13259 */
13260/*ARGSUSED*/
13261 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013262f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013263 typval_T *argvars;
13264 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265{
13266 buf_T *buf;
13267#ifdef FEAT_AUTOCMD
13268 aco_save_T aco;
13269#else
13270 buf_T *save_curbuf;
13271#endif
13272 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013273 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274 char_u nbuf[NUMBUFLEN];
13275
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013276 rettv->vval.v_number = 0;
13277
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278 if (check_restricted() || check_secure())
13279 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013280 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13281 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013282 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013283 varp = &argvars[2];
13284
13285 if (buf != NULL && varname != NULL && varp != NULL)
13286 {
13287 /* set curbuf to be our buf, temporarily */
13288#ifdef FEAT_AUTOCMD
13289 aucmd_prepbuf(&aco, buf);
13290#else
13291 save_curbuf = curbuf;
13292 curbuf = buf;
13293#endif
13294
13295 if (*varname == '&')
13296 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013297 long numval;
13298 char_u *strval;
13299 int error = FALSE;
13300
Bram Moolenaar071d4272004-06-13 20:20:40 +000013301 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013302 numval = get_tv_number_chk(varp, &error);
13303 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013304 if (!error && strval != NULL)
13305 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013306 }
13307 else
13308 {
13309 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13310 if (bufvarname != NULL)
13311 {
13312 STRCPY(bufvarname, "b:");
13313 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013314 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013315 vim_free(bufvarname);
13316 }
13317 }
13318
13319 /* reset notion of buffer */
13320#ifdef FEAT_AUTOCMD
13321 aucmd_restbuf(&aco);
13322#else
13323 curbuf = save_curbuf;
13324#endif
13325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013326}
13327
13328/*
13329 * "setcmdpos()" function
13330 */
13331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013332f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013333 typval_T *argvars;
13334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013335{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013336 int pos = (int)get_tv_number(&argvars[0]) - 1;
13337
13338 if (pos >= 0)
13339 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340}
13341
13342/*
13343 * "setline()" function
13344 */
13345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013346f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013347 typval_T *argvars;
13348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349{
13350 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013351 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013352 list_T *l = NULL;
13353 listitem_T *li = NULL;
13354 long added = 0;
13355 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013357 lnum = get_tv_lnum(&argvars[0]);
13358 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013360 l = argvars[1].vval.v_list;
13361 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013363 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013364 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013365
13366 rettv->vval.v_number = 0; /* OK */
13367 for (;;)
13368 {
13369 if (l != NULL)
13370 {
13371 /* list argument, get next string */
13372 if (li == NULL)
13373 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013374 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013375 li = li->li_next;
13376 }
13377
13378 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013379 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013380 break;
13381 if (lnum <= curbuf->b_ml.ml_line_count)
13382 {
13383 /* existing line, replace it */
13384 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13385 {
13386 changed_bytes(lnum, 0);
13387 check_cursor_col();
13388 rettv->vval.v_number = 0; /* OK */
13389 }
13390 }
13391 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13392 {
13393 /* lnum is one past the last line, append the line */
13394 ++added;
13395 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13396 rettv->vval.v_number = 0; /* OK */
13397 }
13398
13399 if (l == NULL) /* only one string argument */
13400 break;
13401 ++lnum;
13402 }
13403
13404 if (added > 0)
13405 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406}
13407
13408/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013409 * "setqflist()" function
13410 */
13411/*ARGSUSED*/
13412 static void
13413f_setqflist(argvars, rettv)
13414 typval_T *argvars;
13415 typval_T *rettv;
13416{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013417 char_u *act;
13418 int action = ' ';
13419
Bram Moolenaar2641f772005-03-25 21:58:17 +000013420 rettv->vval.v_number = -1;
13421
13422#ifdef FEAT_QUICKFIX
13423 if (argvars[0].v_type != VAR_LIST)
13424 EMSG(_(e_listreq));
13425 else
13426 {
13427 list_T *l = argvars[0].vval.v_list;
13428
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013429 if (argvars[1].v_type == VAR_STRING)
13430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013431 act = get_tv_string_chk(&argvars[1]);
13432 if (act == NULL)
13433 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013434 if (*act == 'a' || *act == 'r')
13435 action = *act;
13436 }
13437
13438 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013439 rettv->vval.v_number = 0;
13440 }
13441#endif
13442}
13443
13444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445 * "setreg()" function
13446 */
13447 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013448f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013449 typval_T *argvars;
13450 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451{
13452 int regname;
13453 char_u *strregname;
13454 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013455 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456 int append;
13457 char_u yank_type;
13458 long block_len;
13459
13460 block_len = -1;
13461 yank_type = MAUTO;
13462 append = FALSE;
13463
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013464 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013465 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013467 if (strregname == NULL)
13468 return; /* type error; errmsg already given */
13469 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470 if (regname == 0 || regname == '@')
13471 regname = '"';
13472 else if (regname == '=')
13473 return;
13474
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013475 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013477 stropt = get_tv_string_chk(&argvars[2]);
13478 if (stropt == NULL)
13479 return; /* type error */
13480 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481 switch (*stropt)
13482 {
13483 case 'a': case 'A': /* append */
13484 append = TRUE;
13485 break;
13486 case 'v': case 'c': /* character-wise selection */
13487 yank_type = MCHAR;
13488 break;
13489 case 'V': case 'l': /* line-wise selection */
13490 yank_type = MLINE;
13491 break;
13492#ifdef FEAT_VISUAL
13493 case 'b': case Ctrl_V: /* block-wise selection */
13494 yank_type = MBLOCK;
13495 if (VIM_ISDIGIT(stropt[1]))
13496 {
13497 ++stropt;
13498 block_len = getdigits(&stropt) - 1;
13499 --stropt;
13500 }
13501 break;
13502#endif
13503 }
13504 }
13505
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013506 strval = get_tv_string_chk(&argvars[1]);
13507 if (strval != NULL)
13508 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013510 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511}
13512
13513
13514/*
13515 * "setwinvar(expr)" function
13516 */
13517/*ARGSUSED*/
13518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013519f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013520 typval_T *argvars;
13521 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013522{
13523 win_T *win;
13524#ifdef FEAT_WINDOWS
13525 win_T *save_curwin;
13526#endif
13527 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013528 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529 char_u nbuf[NUMBUFLEN];
13530
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013531 rettv->vval.v_number = 0;
13532
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533 if (check_restricted() || check_secure())
13534 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013536 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013537 varp = &argvars[2];
13538
13539 if (win != NULL && varname != NULL && varp != NULL)
13540 {
13541#ifdef FEAT_WINDOWS
13542 /* set curwin to be our win, temporarily */
13543 save_curwin = curwin;
13544 curwin = win;
13545 curbuf = curwin->w_buffer;
13546#endif
13547
13548 if (*varname == '&')
13549 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013550 long numval;
13551 char_u *strval;
13552 int error = FALSE;
13553
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013555 numval = get_tv_number_chk(varp, &error);
13556 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013557 if (!error && strval != NULL)
13558 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559 }
13560 else
13561 {
13562 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13563 if (winvarname != NULL)
13564 {
13565 STRCPY(winvarname, "w:");
13566 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013567 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568 vim_free(winvarname);
13569 }
13570 }
13571
13572#ifdef FEAT_WINDOWS
13573 /* Restore current window, if it's still valid (autocomands can make
13574 * it invalid). */
13575 if (win_valid(save_curwin))
13576 {
13577 curwin = save_curwin;
13578 curbuf = curwin->w_buffer;
13579 }
13580#endif
13581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582}
13583
13584/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013585 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013586 */
13587 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013588f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013589 typval_T *argvars;
13590 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013592 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593
Bram Moolenaar0d660222005-01-07 21:51:51 +000013594 p = get_tv_string(&argvars[0]);
13595 rettv->vval.v_string = vim_strsave(p);
13596 simplify_filename(rettv->vval.v_string); /* simplify in place */
13597 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598}
13599
Bram Moolenaar0d660222005-01-07 21:51:51 +000013600static int
13601#ifdef __BORLANDC__
13602 _RTLENTRYF
13603#endif
13604 item_compare __ARGS((const void *s1, const void *s2));
13605static int
13606#ifdef __BORLANDC__
13607 _RTLENTRYF
13608#endif
13609 item_compare2 __ARGS((const void *s1, const void *s2));
13610
13611static int item_compare_ic;
13612static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013613static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013614#define ITEM_COMPARE_FAIL 999
13615
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013617 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013618 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013619 static int
13620#ifdef __BORLANDC__
13621_RTLENTRYF
13622#endif
13623item_compare(s1, s2)
13624 const void *s1;
13625 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013627 char_u *p1, *p2;
13628 char_u *tofree1, *tofree2;
13629 int res;
13630 char_u numbuf1[NUMBUFLEN];
13631 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632
Bram Moolenaar33570922005-01-25 22:26:29 +000013633 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13634 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013635 if (item_compare_ic)
13636 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013638 res = STRCMP(p1, p2);
13639 vim_free(tofree1);
13640 vim_free(tofree2);
13641 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642}
13643
13644 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013645#ifdef __BORLANDC__
13646_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013648item_compare2(s1, s2)
13649 const void *s1;
13650 const void *s2;
13651{
13652 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013653 typval_T rettv;
13654 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013655 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013657 /* shortcut after failure in previous call; compare all items equal */
13658 if (item_compare_func_err)
13659 return 0;
13660
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013661 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13662 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013663 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13664 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013665
13666 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13667 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013668 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013669 clear_tv(&argv[0]);
13670 clear_tv(&argv[1]);
13671
13672 if (res == FAIL)
13673 res = ITEM_COMPARE_FAIL;
13674 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013675 /* return value has wrong type */
13676 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13677 if (item_compare_func_err)
13678 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013679 clear_tv(&rettv);
13680 return res;
13681}
13682
13683/*
13684 * "sort({list})" function
13685 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013687f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013688 typval_T *argvars;
13689 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690{
Bram Moolenaar33570922005-01-25 22:26:29 +000013691 list_T *l;
13692 listitem_T *li;
13693 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013694 long len;
13695 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013696
Bram Moolenaar0d660222005-01-07 21:51:51 +000013697 rettv->vval.v_number = 0;
13698 if (argvars[0].v_type != VAR_LIST)
13699 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700 else
13701 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013702 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013703 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013704 return;
13705 rettv->vval.v_list = l;
13706 rettv->v_type = VAR_LIST;
13707 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708
Bram Moolenaar0d660222005-01-07 21:51:51 +000013709 len = list_len(l);
13710 if (len <= 1)
13711 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712
Bram Moolenaar0d660222005-01-07 21:51:51 +000013713 item_compare_ic = FALSE;
13714 item_compare_func = NULL;
13715 if (argvars[1].v_type != VAR_UNKNOWN)
13716 {
13717 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013718 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013719 else
13720 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013721 int error = FALSE;
13722
13723 i = get_tv_number_chk(&argvars[1], &error);
13724 if (error)
13725 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013726 if (i == 1)
13727 item_compare_ic = TRUE;
13728 else
13729 item_compare_func = get_tv_string(&argvars[1]);
13730 }
13731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732
Bram Moolenaar0d660222005-01-07 21:51:51 +000013733 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013734 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013735 if (ptrs == NULL)
13736 return;
13737 i = 0;
13738 for (li = l->lv_first; li != NULL; li = li->li_next)
13739 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013740
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013741 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013742 /* test the compare function */
13743 if (item_compare_func != NULL
13744 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13745 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013746 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013748 {
13749 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013750 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013751 item_compare_func == NULL ? item_compare : item_compare2);
13752
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013753 if (!item_compare_func_err)
13754 {
13755 /* Clear the List and append the items in the sorted order. */
13756 l->lv_first = l->lv_last = NULL;
13757 l->lv_len = 0;
13758 for (i = 0; i < len; ++i)
13759 list_append(l, ptrs[i]);
13760 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013761 }
13762
13763 vim_free(ptrs);
13764 }
13765}
13766
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013767/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013768 * "soundfold({word})" function
13769 */
13770 static void
13771f_soundfold(argvars, rettv)
13772 typval_T *argvars;
13773 typval_T *rettv;
13774{
13775 char_u *s;
13776
13777 rettv->v_type = VAR_STRING;
13778 s = get_tv_string(&argvars[0]);
13779#ifdef FEAT_SYN_HL
13780 rettv->vval.v_string = eval_soundfold(s);
13781#else
13782 rettv->vval.v_string = vim_strsave(s);
13783#endif
13784}
13785
13786/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013787 * "spellbadword()" function
13788 */
13789/* ARGSUSED */
13790 static void
13791f_spellbadword(argvars, rettv)
13792 typval_T *argvars;
13793 typval_T *rettv;
13794{
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013795 int len;
13796
13797 rettv->vval.v_string = NULL;
13798 rettv->v_type = VAR_STRING;
13799
13800#ifdef FEAT_SYN_HL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +000013801 /* Find the start and length of the badly spelled word. */
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000013802 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, NULL);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +000013803 if (len != 0)
13804 rettv->vval.v_string = vim_strnsave(ml_get_cursor(), len);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013805#endif
13806}
13807
13808/*
13809 * "spellsuggest()" function
13810 */
13811 static void
13812f_spellsuggest(argvars, rettv)
13813 typval_T *argvars;
13814 typval_T *rettv;
13815{
13816 char_u *str;
13817 int maxcount;
13818 garray_T ga;
13819 list_T *l;
13820 listitem_T *li;
13821 int i;
13822
13823 l = list_alloc();
13824 if (l == NULL)
13825 return;
13826 rettv->v_type = VAR_LIST;
13827 rettv->vval.v_list = l;
13828 ++l->lv_refcount;
13829
13830#ifdef FEAT_SYN_HL
13831 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13832 {
13833 str = get_tv_string(&argvars[0]);
13834 if (argvars[1].v_type != VAR_UNKNOWN)
13835 {
13836 maxcount = get_tv_number(&argvars[1]);
13837 if (maxcount <= 0)
13838 return;
13839 }
13840 else
13841 maxcount = 25;
13842
Bram Moolenaar8c45cdf2005-08-11 20:11:38 +000013843 spell_suggest_list(&ga, str, maxcount, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013844
13845 for (i = 0; i < ga.ga_len; ++i)
13846 {
13847 str = ((char_u **)ga.ga_data)[i];
13848
13849 li = listitem_alloc();
13850 if (li == NULL)
13851 vim_free(str);
13852 else
13853 {
13854 li->li_tv.v_type = VAR_STRING;
13855 li->li_tv.v_lock = 0;
13856 li->li_tv.vval.v_string = str;
13857 list_append(l, li);
13858 }
13859 }
13860 ga_clear(&ga);
13861 }
13862#endif
13863}
13864
Bram Moolenaar0d660222005-01-07 21:51:51 +000013865 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013866f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013867 typval_T *argvars;
13868 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013869{
13870 char_u *str;
13871 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013872 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013873 regmatch_T regmatch;
13874 char_u patbuf[NUMBUFLEN];
13875 char_u *save_cpo;
13876 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000013877 listitem_T *ni;
13878 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013879 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013880 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013881 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013882
13883 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13884 save_cpo = p_cpo;
13885 p_cpo = (char_u *)"";
13886
13887 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013888 if (argvars[1].v_type != VAR_UNKNOWN)
13889 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013890 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13891 if (pat == NULL)
13892 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013893 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013894 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013895 }
13896 if (pat == NULL || *pat == NUL)
13897 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000013898
13899 l = list_alloc();
13900 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013901 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013902 rettv->v_type = VAR_LIST;
13903 rettv->vval.v_list = l;
13904 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013905 if (typeerr)
13906 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907
Bram Moolenaar0d660222005-01-07 21:51:51 +000013908 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13909 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013910 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013911 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013912 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013913 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013914 if (*str == NUL)
13915 match = FALSE; /* empty item at the end */
13916 else
13917 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013918 if (match)
13919 end = regmatch.startp[0];
13920 else
13921 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013922 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
13923 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013924 {
13925 ni = listitem_alloc();
13926 if (ni == NULL)
13927 break;
13928 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013929 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013930 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
13931 list_append(l, ni);
13932 }
13933 if (!match)
13934 break;
13935 /* Advance to just after the match. */
13936 if (regmatch.endp[0] > str)
13937 col = 0;
13938 else
13939 {
13940 /* Don't get stuck at the same match. */
13941#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000013942 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013943#else
13944 col = 1;
13945#endif
13946 }
13947 str = regmatch.endp[0];
13948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949
Bram Moolenaar0d660222005-01-07 21:51:51 +000013950 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952
Bram Moolenaar0d660222005-01-07 21:51:51 +000013953 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013954}
13955
13956#ifdef HAVE_STRFTIME
13957/*
13958 * "strftime({format}[, {time}])" function
13959 */
13960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013961f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013962 typval_T *argvars;
13963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013964{
13965 char_u result_buf[256];
13966 struct tm *curtime;
13967 time_t seconds;
13968 char_u *p;
13969
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013970 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013972 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013973 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 seconds = time(NULL);
13975 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013976 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977 curtime = localtime(&seconds);
13978 /* MSVC returns NULL for an invalid value of seconds. */
13979 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013980 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981 else
13982 {
13983# ifdef FEAT_MBYTE
13984 vimconv_T conv;
13985 char_u *enc;
13986
13987 conv.vc_type = CONV_NONE;
13988 enc = enc_locale();
13989 convert_setup(&conv, p_enc, enc);
13990 if (conv.vc_type != CONV_NONE)
13991 p = string_convert(&conv, p, NULL);
13992# endif
13993 if (p != NULL)
13994 (void)strftime((char *)result_buf, sizeof(result_buf),
13995 (char *)p, curtime);
13996 else
13997 result_buf[0] = NUL;
13998
13999# ifdef FEAT_MBYTE
14000 if (conv.vc_type != CONV_NONE)
14001 vim_free(p);
14002 convert_setup(&conv, enc, p_enc);
14003 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014004 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005 else
14006# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014007 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008
14009# ifdef FEAT_MBYTE
14010 /* Release conversion descriptors */
14011 convert_setup(&conv, NULL, NULL);
14012 vim_free(enc);
14013# endif
14014 }
14015}
14016#endif
14017
14018/*
14019 * "stridx()" function
14020 */
14021 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014022f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014023 typval_T *argvars;
14024 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014025{
14026 char_u buf[NUMBUFLEN];
14027 char_u *needle;
14028 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000014029 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000014031 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014032
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014033 needle = get_tv_string_chk(&argvars[1]);
14034 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000014035 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014036 if (needle == NULL || haystack == NULL)
14037 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038
Bram Moolenaar33570922005-01-25 22:26:29 +000014039 if (argvars[2].v_type != VAR_UNKNOWN)
14040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014041 int error = FALSE;
14042
14043 start_idx = get_tv_number_chk(&argvars[2], &error);
14044 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000014045 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014046 if (start_idx >= 0)
14047 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000014048 }
14049
14050 pos = (char_u *)strstr((char *)haystack, (char *)needle);
14051 if (pos != NULL)
14052 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053}
14054
14055/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014056 * "string()" function
14057 */
14058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014059f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014060 typval_T *argvars;
14061 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014062{
14063 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014064 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014065
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014066 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014067 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014068 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014069 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070}
14071
14072/*
14073 * "strlen()" function
14074 */
14075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014076f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014077 typval_T *argvars;
14078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014080 rettv->vval.v_number = (varnumber_T)(STRLEN(
14081 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014082}
14083
14084/*
14085 * "strpart()" function
14086 */
14087 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014088f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014089 typval_T *argvars;
14090 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091{
14092 char_u *p;
14093 int n;
14094 int len;
14095 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014096 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014098 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099 slen = (int)STRLEN(p);
14100
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014101 n = get_tv_number_chk(&argvars[1], &error);
14102 if (error)
14103 len = 0;
14104 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014105 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106 else
14107 len = slen - n; /* default len: all bytes that are available. */
14108
14109 /*
14110 * Only return the overlap between the specified part and the actual
14111 * string.
14112 */
14113 if (n < 0)
14114 {
14115 len += n;
14116 n = 0;
14117 }
14118 else if (n > slen)
14119 n = slen;
14120 if (len < 0)
14121 len = 0;
14122 else if (n + len > slen)
14123 len = slen - n;
14124
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014125 rettv->v_type = VAR_STRING;
14126 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127}
14128
14129/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014130 * "strridx()" function
14131 */
14132 static void
14133f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014134 typval_T *argvars;
14135 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014136{
14137 char_u buf[NUMBUFLEN];
14138 char_u *needle;
14139 char_u *haystack;
14140 char_u *rest;
14141 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014142 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014143
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014144 needle = get_tv_string_chk(&argvars[1]);
14145 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014146 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014147
14148 rettv->vval.v_number = -1;
14149 if (needle == NULL || haystack == NULL)
14150 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014151 if (argvars[2].v_type != VAR_UNKNOWN)
14152 {
14153 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014154 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000014155 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014156 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014157 }
14158 else
14159 end_idx = haystack_len;
14160
Bram Moolenaar0d660222005-01-07 21:51:51 +000014161 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000014162 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014163 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000014164 lastmatch = haystack + end_idx;
14165 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014166 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000014167 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000014168 for (rest = haystack; *rest != '\0'; ++rest)
14169 {
14170 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000014171 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014172 break;
14173 lastmatch = rest;
14174 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000014175 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000014176
14177 if (lastmatch == NULL)
14178 rettv->vval.v_number = -1;
14179 else
14180 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
14181}
14182
14183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184 * "strtrans()" function
14185 */
14186 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014187f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014188 typval_T *argvars;
14189 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014191 rettv->v_type = VAR_STRING;
14192 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193}
14194
14195/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014196 * "submatch()" function
14197 */
14198 static void
14199f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014200 typval_T *argvars;
14201 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014202{
14203 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014204 rettv->vval.v_string =
14205 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000014206}
14207
14208/*
14209 * "substitute()" function
14210 */
14211 static void
14212f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014213 typval_T *argvars;
14214 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014215{
14216 char_u patbuf[NUMBUFLEN];
14217 char_u subbuf[NUMBUFLEN];
14218 char_u flagsbuf[NUMBUFLEN];
14219
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014220 char_u *str = get_tv_string_chk(&argvars[0]);
14221 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
14222 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
14223 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
14224
Bram Moolenaar0d660222005-01-07 21:51:51 +000014225 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014226 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
14227 rettv->vval.v_string = NULL;
14228 else
14229 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000014230}
14231
14232/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014233 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234 */
14235/*ARGSUSED*/
14236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014237f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014238 typval_T *argvars;
14239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240{
14241 int id = 0;
14242#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014243 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014244 long col;
14245 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000014246 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014248 lnum = get_tv_lnum(argvars); /* -1 on type error */
14249 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
14250 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014251
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014252 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000014253 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +000014254 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014255#endif
14256
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014257 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258}
14259
14260/*
14261 * "synIDattr(id, what [, mode])" function
14262 */
14263/*ARGSUSED*/
14264 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014265f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014266 typval_T *argvars;
14267 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268{
14269 char_u *p = NULL;
14270#ifdef FEAT_SYN_HL
14271 int id;
14272 char_u *what;
14273 char_u *mode;
14274 char_u modebuf[NUMBUFLEN];
14275 int modec;
14276
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014277 id = get_tv_number(&argvars[0]);
14278 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014279 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014281 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282 modec = TOLOWER_ASC(mode[0]);
14283 if (modec != 't' && modec != 'c'
14284#ifdef FEAT_GUI
14285 && modec != 'g'
14286#endif
14287 )
14288 modec = 0; /* replace invalid with current */
14289 }
14290 else
14291 {
14292#ifdef FEAT_GUI
14293 if (gui.in_use)
14294 modec = 'g';
14295 else
14296#endif
14297 if (t_colors > 1)
14298 modec = 'c';
14299 else
14300 modec = 't';
14301 }
14302
14303
14304 switch (TOLOWER_ASC(what[0]))
14305 {
14306 case 'b':
14307 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14308 p = highlight_color(id, what, modec);
14309 else /* bold */
14310 p = highlight_has_attr(id, HL_BOLD, modec);
14311 break;
14312
14313 case 'f': /* fg[#] */
14314 p = highlight_color(id, what, modec);
14315 break;
14316
14317 case 'i':
14318 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14319 p = highlight_has_attr(id, HL_INVERSE, modec);
14320 else /* italic */
14321 p = highlight_has_attr(id, HL_ITALIC, modec);
14322 break;
14323
14324 case 'n': /* name */
14325 p = get_highlight_name(NULL, id - 1);
14326 break;
14327
14328 case 'r': /* reverse */
14329 p = highlight_has_attr(id, HL_INVERSE, modec);
14330 break;
14331
14332 case 's': /* standout */
14333 p = highlight_has_attr(id, HL_STANDOUT, modec);
14334 break;
14335
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014336 case 'u':
14337 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14338 /* underline */
14339 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14340 else
14341 /* undercurl */
14342 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343 break;
14344 }
14345
14346 if (p != NULL)
14347 p = vim_strsave(p);
14348#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014349 rettv->v_type = VAR_STRING;
14350 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351}
14352
14353/*
14354 * "synIDtrans(id)" function
14355 */
14356/*ARGSUSED*/
14357 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014358f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014359 typval_T *argvars;
14360 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014361{
14362 int id;
14363
14364#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014365 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366
14367 if (id > 0)
14368 id = syn_get_final_id(id);
14369 else
14370#endif
14371 id = 0;
14372
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014373 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374}
14375
14376/*
14377 * "system()" function
14378 */
14379 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014380f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014381 typval_T *argvars;
14382 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014384 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014386 char_u *infile = NULL;
14387 char_u buf[NUMBUFLEN];
14388 int err = FALSE;
14389 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014391 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014392 {
14393 /*
14394 * Write the string to a temp file, to be used for input of the shell
14395 * command.
14396 */
14397 if ((infile = vim_tempname('i')) == NULL)
14398 {
14399 EMSG(_(e_notmp));
14400 return;
14401 }
14402
14403 fd = mch_fopen((char *)infile, WRITEBIN);
14404 if (fd == NULL)
14405 {
14406 EMSG2(_(e_notopen), infile);
14407 goto done;
14408 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014409 p = get_tv_string_buf_chk(&argvars[1], buf);
14410 if (p == NULL)
14411 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014412 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14413 err = TRUE;
14414 if (fclose(fd) != 0)
14415 err = TRUE;
14416 if (err)
14417 {
14418 EMSG(_("E677: Error writing temp file"));
14419 goto done;
14420 }
14421 }
14422
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014423 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014424
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425#ifdef USE_CR
14426 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014427 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014428 {
14429 char_u *s;
14430
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014431 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432 {
14433 if (*s == CAR)
14434 *s = NL;
14435 }
14436 }
14437#else
14438# ifdef USE_CRNL
14439 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014440 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441 {
14442 char_u *s, *d;
14443
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014444 d = res;
14445 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446 {
14447 if (s[0] == CAR && s[1] == NL)
14448 ++s;
14449 *d++ = *s;
14450 }
14451 *d = NUL;
14452 }
14453# endif
14454#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014455
14456done:
14457 if (infile != NULL)
14458 {
14459 mch_remove(infile);
14460 vim_free(infile);
14461 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014462 rettv->v_type = VAR_STRING;
14463 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464}
14465
14466/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000014467 * "tagfiles()" function
14468 */
14469/*ARGSUSED*/
14470 static void
14471f_tagfiles(argvars, rettv)
14472 typval_T *argvars;
14473 typval_T *rettv;
14474{
14475 char_u fname[MAXPATHL + 1];
14476 list_T *l;
14477
14478 l = list_alloc();
14479 if (l == NULL)
14480 {
14481 rettv->vval.v_number = 0;
14482 return;
14483 }
14484 rettv->vval.v_list = l;
14485 rettv->v_type = VAR_LIST;
14486 ++l->lv_refcount;
14487
14488 get_tagfname(TRUE, NULL);
14489 for (;;)
14490 if (get_tagfname(FALSE, fname) == FAIL
14491 || list_append_string(l, fname) == FAIL)
14492 break;
14493}
14494
14495/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014496 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014497 */
14498 static void
14499f_taglist(argvars, rettv)
14500 typval_T *argvars;
14501 typval_T *rettv;
14502{
14503 char_u *tag_pattern;
14504 list_T *l;
14505
14506 tag_pattern = get_tv_string(&argvars[0]);
14507
14508 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014509 if (*tag_pattern == NUL)
14510 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014511
14512 l = list_alloc();
14513 if (l != NULL)
14514 {
14515 if (get_tags(l, tag_pattern) != FAIL)
14516 {
14517 rettv->vval.v_list = l;
14518 rettv->v_type = VAR_LIST;
14519 ++l->lv_refcount;
14520 }
14521 else
14522 list_free(l);
14523 }
14524}
14525
14526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014527 * "tempname()" function
14528 */
14529/*ARGSUSED*/
14530 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014531f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014532 typval_T *argvars;
14533 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014534{
14535 static int x = 'A';
14536
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014537 rettv->v_type = VAR_STRING;
14538 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539
14540 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14541 * names. Skip 'I' and 'O', they are used for shell redirection. */
14542 do
14543 {
14544 if (x == 'Z')
14545 x = '0';
14546 else if (x == '9')
14547 x = 'A';
14548 else
14549 {
14550#ifdef EBCDIC
14551 if (x == 'I')
14552 x = 'J';
14553 else if (x == 'R')
14554 x = 'S';
14555 else
14556#endif
14557 ++x;
14558 }
14559 } while (x == 'I' || x == 'O');
14560}
14561
14562/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000014563 * "test(list)" function: Just checking the walls...
14564 */
14565/*ARGSUSED*/
14566 static void
14567f_test(argvars, rettv)
14568 typval_T *argvars;
14569 typval_T *rettv;
14570{
14571 /* Used for unit testing. Change the code below to your liking. */
14572#if 0
14573 listitem_T *li;
14574 list_T *l;
14575 char_u *bad, *good;
14576
14577 if (argvars[0].v_type != VAR_LIST)
14578 return;
14579 l = argvars[0].vval.v_list;
14580 if (l == NULL)
14581 return;
14582 li = l->lv_first;
14583 if (li == NULL)
14584 return;
14585 bad = get_tv_string(&li->li_tv);
14586 li = li->li_next;
14587 if (li == NULL)
14588 return;
14589 good = get_tv_string(&li->li_tv);
14590 rettv->vval.v_number = test_edit_score(bad, good);
14591#endif
14592}
14593
14594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595 * "tolower(string)" function
14596 */
14597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014598f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014599 typval_T *argvars;
14600 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014601{
14602 char_u *p;
14603
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014604 p = vim_strsave(get_tv_string(&argvars[0]));
14605 rettv->v_type = VAR_STRING;
14606 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014607
14608 if (p != NULL)
14609 while (*p != NUL)
14610 {
14611#ifdef FEAT_MBYTE
14612 int l;
14613
14614 if (enc_utf8)
14615 {
14616 int c, lc;
14617
14618 c = utf_ptr2char(p);
14619 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014620 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014621 /* TODO: reallocate string when byte count changes. */
14622 if (utf_char2len(lc) == l)
14623 utf_char2bytes(lc, p);
14624 p += l;
14625 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014626 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014627 p += l; /* skip multi-byte character */
14628 else
14629#endif
14630 {
14631 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14632 ++p;
14633 }
14634 }
14635}
14636
14637/*
14638 * "toupper(string)" function
14639 */
14640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014641f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014642 typval_T *argvars;
14643 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014644{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014645 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014646 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014647}
14648
14649/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014650 * "tr(string, fromstr, tostr)" function
14651 */
14652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014653f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014654 typval_T *argvars;
14655 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014656{
14657 char_u *instr;
14658 char_u *fromstr;
14659 char_u *tostr;
14660 char_u *p;
14661#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000014662 int inlen;
14663 int fromlen;
14664 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014665 int idx;
14666 char_u *cpstr;
14667 int cplen;
14668 int first = TRUE;
14669#endif
14670 char_u buf[NUMBUFLEN];
14671 char_u buf2[NUMBUFLEN];
14672 garray_T ga;
14673
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014674 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014675 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14676 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014677
14678 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014679 rettv->v_type = VAR_STRING;
14680 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014681 if (fromstr == NULL || tostr == NULL)
14682 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014683 ga_init2(&ga, (int)sizeof(char), 80);
14684
14685#ifdef FEAT_MBYTE
14686 if (!has_mbyte)
14687#endif
14688 /* not multi-byte: fromstr and tostr must be the same length */
14689 if (STRLEN(fromstr) != STRLEN(tostr))
14690 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014691#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014692error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014693#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014694 EMSG2(_(e_invarg2), fromstr);
14695 ga_clear(&ga);
14696 return;
14697 }
14698
14699 /* fromstr and tostr have to contain the same number of chars */
14700 while (*instr != NUL)
14701 {
14702#ifdef FEAT_MBYTE
14703 if (has_mbyte)
14704 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014705 inlen = (*mb_ptr2len)(instr);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014706 cpstr = instr;
14707 cplen = inlen;
14708 idx = 0;
14709 for (p = fromstr; *p != NUL; p += fromlen)
14710 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014711 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014712 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14713 {
14714 for (p = tostr; *p != NUL; p += tolen)
14715 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014716 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014717 if (idx-- == 0)
14718 {
14719 cplen = tolen;
14720 cpstr = p;
14721 break;
14722 }
14723 }
14724 if (*p == NUL) /* tostr is shorter than fromstr */
14725 goto error;
14726 break;
14727 }
14728 ++idx;
14729 }
14730
14731 if (first && cpstr == instr)
14732 {
14733 /* Check that fromstr and tostr have the same number of
14734 * (multi-byte) characters. Done only once when a character
14735 * of instr doesn't appear in fromstr. */
14736 first = FALSE;
14737 for (p = tostr; *p != NUL; p += tolen)
14738 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000014739 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014740 --idx;
14741 }
14742 if (idx != 0)
14743 goto error;
14744 }
14745
14746 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014747 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014748 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014749
14750 instr += inlen;
14751 }
14752 else
14753#endif
14754 {
14755 /* When not using multi-byte chars we can do it faster. */
14756 p = vim_strchr(fromstr, *instr);
14757 if (p != NULL)
14758 ga_append(&ga, tostr[p - fromstr]);
14759 else
14760 ga_append(&ga, *instr);
14761 ++instr;
14762 }
14763 }
14764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014765 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014766}
14767
14768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769 * "type(expr)" function
14770 */
14771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014772f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014773 typval_T *argvars;
14774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014776 int n;
14777
14778 switch (argvars[0].v_type)
14779 {
14780 case VAR_NUMBER: n = 0; break;
14781 case VAR_STRING: n = 1; break;
14782 case VAR_FUNC: n = 2; break;
14783 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014784 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014785 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14786 }
14787 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014788}
14789
14790/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014791 * "values(dict)" function
14792 */
14793 static void
14794f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014795 typval_T *argvars;
14796 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014797{
14798 dict_list(argvars, rettv, 1);
14799}
14800
14801/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014802 * "virtcol(string)" function
14803 */
14804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014805f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014806 typval_T *argvars;
14807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014808{
14809 colnr_T vcol = 0;
14810 pos_T *fp;
14811
14812 fp = var2fpos(&argvars[0], FALSE);
14813 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14814 {
14815 getvvcol(curwin, fp, NULL, NULL, &vcol);
14816 ++vcol;
14817 }
14818
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014819 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820}
14821
14822/*
14823 * "visualmode()" function
14824 */
14825/*ARGSUSED*/
14826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014827f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014828 typval_T *argvars;
14829 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014830{
14831#ifdef FEAT_VISUAL
14832 char_u str[2];
14833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014834 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835 str[0] = curbuf->b_visual_mode_eval;
14836 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014837 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014838
14839 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014840 if ((argvars[0].v_type == VAR_NUMBER
14841 && argvars[0].vval.v_number != 0)
14842 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014843 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844 curbuf->b_visual_mode_eval = NUL;
14845#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014846 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847#endif
14848}
14849
14850/*
14851 * "winbufnr(nr)" function
14852 */
14853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014854f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014855 typval_T *argvars;
14856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014857{
14858 win_T *wp;
14859
14860 wp = find_win_by_nr(&argvars[0]);
14861 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014862 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014863 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014864 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014865}
14866
14867/*
14868 * "wincol()" function
14869 */
14870/*ARGSUSED*/
14871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014872f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014873 typval_T *argvars;
14874 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014875{
14876 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014877 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014878}
14879
14880/*
14881 * "winheight(nr)" function
14882 */
14883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014884f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014885 typval_T *argvars;
14886 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014887{
14888 win_T *wp;
14889
14890 wp = find_win_by_nr(&argvars[0]);
14891 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014892 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014893 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014894 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014895}
14896
14897/*
14898 * "winline()" function
14899 */
14900/*ARGSUSED*/
14901 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014902f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014903 typval_T *argvars;
14904 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014905{
14906 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014907 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908}
14909
14910/*
14911 * "winnr()" function
14912 */
14913/* ARGSUSED */
14914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014915f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014916 typval_T *argvars;
14917 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014918{
14919 int nr = 1;
14920#ifdef FEAT_WINDOWS
14921 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014922 win_T *twin = curwin;
14923 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014924
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014925 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014926 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014927 arg = get_tv_string_chk(&argvars[0]);
14928 if (arg == NULL)
14929 nr = 0; /* type error; errmsg already given */
14930 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014931 twin = lastwin;
14932 else if (STRCMP(arg, "#") == 0)
14933 {
14934 twin = prevwin;
14935 if (prevwin == NULL)
14936 nr = 0;
14937 }
14938 else
14939 {
14940 EMSG2(_(e_invexpr2), arg);
14941 nr = 0;
14942 }
14943 }
14944
14945 if (nr > 0)
14946 for (wp = firstwin; wp != twin; wp = wp->w_next)
14947 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014948#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014949 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014950}
14951
14952/*
14953 * "winrestcmd()" function
14954 */
14955/* ARGSUSED */
14956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014957f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014958 typval_T *argvars;
14959 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960{
14961#ifdef FEAT_WINDOWS
14962 win_T *wp;
14963 int winnr = 1;
14964 garray_T ga;
14965 char_u buf[50];
14966
14967 ga_init2(&ga, (int)sizeof(char), 70);
14968 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14969 {
14970 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14971 ga_concat(&ga, buf);
14972# ifdef FEAT_VERTSPLIT
14973 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14974 ga_concat(&ga, buf);
14975# endif
14976 ++winnr;
14977 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000014978 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014979
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014980 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014981#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014982 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014984 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985}
14986
14987/*
14988 * "winwidth(nr)" function
14989 */
14990 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014991f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014992 typval_T *argvars;
14993 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014994{
14995 win_T *wp;
14996
14997 wp = find_win_by_nr(&argvars[0]);
14998 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014999 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015000 else
15001#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015002 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015003#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015004 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015005#endif
15006}
15007
Bram Moolenaar071d4272004-06-13 20:20:40 +000015008/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015009 * "writefile()" function
15010 */
15011 static void
15012f_writefile(argvars, rettv)
15013 typval_T *argvars;
15014 typval_T *rettv;
15015{
15016 int binary = FALSE;
15017 char_u *fname;
15018 FILE *fd;
15019 listitem_T *li;
15020 char_u *s;
15021 int ret = 0;
15022 int c;
15023
15024 if (argvars[0].v_type != VAR_LIST)
15025 {
15026 EMSG2(_(e_listarg), "writefile()");
15027 return;
15028 }
15029 if (argvars[0].vval.v_list == NULL)
15030 return;
15031
15032 if (argvars[2].v_type != VAR_UNKNOWN
15033 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
15034 binary = TRUE;
15035
15036 /* Always open the file in binary mode, library functions have a mind of
15037 * their own about CR-LF conversion. */
15038 fname = get_tv_string(&argvars[1]);
15039 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
15040 {
15041 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
15042 ret = -1;
15043 }
15044 else
15045 {
15046 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
15047 li = li->li_next)
15048 {
15049 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
15050 {
15051 if (*s == '\n')
15052 c = putc(NUL, fd);
15053 else
15054 c = putc(*s, fd);
15055 if (c == EOF)
15056 {
15057 ret = -1;
15058 break;
15059 }
15060 }
15061 if (!binary || li->li_next != NULL)
15062 if (putc('\n', fd) == EOF)
15063 {
15064 ret = -1;
15065 break;
15066 }
15067 if (ret < 0)
15068 {
15069 EMSG(_(e_write));
15070 break;
15071 }
15072 }
15073 fclose(fd);
15074 }
15075
15076 rettv->vval.v_number = ret;
15077}
15078
15079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015080 * Translate a String variable into a position.
15081 */
15082 static pos_T *
15083var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000015084 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015085 int lnum; /* TRUE when $ is last line */
15086{
15087 char_u *name;
15088 static pos_T pos;
15089 pos_T *pp;
15090
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015091 name = get_tv_string_chk(varp);
15092 if (name == NULL)
15093 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015094 if (name[0] == '.') /* cursor */
15095 return &curwin->w_cursor;
15096 if (name[0] == '\'') /* mark */
15097 {
15098 pp = getmark(name[1], FALSE);
15099 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
15100 return NULL;
15101 return pp;
15102 }
15103 if (name[0] == '$') /* last column or line */
15104 {
15105 if (lnum)
15106 {
15107 pos.lnum = curbuf->b_ml.ml_line_count;
15108 pos.col = 0;
15109 }
15110 else
15111 {
15112 pos.lnum = curwin->w_cursor.lnum;
15113 pos.col = (colnr_T)STRLEN(ml_get_curline());
15114 }
15115 return &pos;
15116 }
15117 return NULL;
15118}
15119
15120/*
15121 * Get the length of an environment variable name.
15122 * Advance "arg" to the first character after the name.
15123 * Return 0 for error.
15124 */
15125 static int
15126get_env_len(arg)
15127 char_u **arg;
15128{
15129 char_u *p;
15130 int len;
15131
15132 for (p = *arg; vim_isIDc(*p); ++p)
15133 ;
15134 if (p == *arg) /* no name found */
15135 return 0;
15136
15137 len = (int)(p - *arg);
15138 *arg = p;
15139 return len;
15140}
15141
15142/*
15143 * Get the length of the name of a function or internal variable.
15144 * "arg" is advanced to the first non-white character after the name.
15145 * Return 0 if something is wrong.
15146 */
15147 static int
15148get_id_len(arg)
15149 char_u **arg;
15150{
15151 char_u *p;
15152 int len;
15153
15154 /* Find the end of the name. */
15155 for (p = *arg; eval_isnamec(*p); ++p)
15156 ;
15157 if (p == *arg) /* no name found */
15158 return 0;
15159
15160 len = (int)(p - *arg);
15161 *arg = skipwhite(p);
15162
15163 return len;
15164}
15165
15166/*
Bram Moolenaara7043832005-01-21 11:56:39 +000015167 * Get the length of the name of a variable or function.
15168 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015169 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015170 * Return -1 if curly braces expansion failed.
15171 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015172 * If the name contains 'magic' {}'s, expand them and return the
15173 * expanded name in an allocated string via 'alias' - caller must free.
15174 */
15175 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015176get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015177 char_u **arg;
15178 char_u **alias;
15179 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015180 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015181{
15182 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015183 char_u *p;
15184 char_u *expr_start;
15185 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186
15187 *alias = NULL; /* default to no alias */
15188
15189 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
15190 && (*arg)[2] == (int)KE_SNR)
15191 {
15192 /* hard coded <SNR>, already translated */
15193 *arg += 3;
15194 return get_id_len(arg) + 3;
15195 }
15196 len = eval_fname_script(*arg);
15197 if (len > 0)
15198 {
15199 /* literal "<SID>", "s:" or "<SNR>" */
15200 *arg += len;
15201 }
15202
Bram Moolenaar071d4272004-06-13 20:20:40 +000015203 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015204 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015205 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015206 p = find_name_end(*arg, &expr_start, &expr_end,
15207 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015208 if (expr_start != NULL)
15209 {
15210 char_u *temp_string;
15211
15212 if (!evaluate)
15213 {
15214 len += (int)(p - *arg);
15215 *arg = skipwhite(p);
15216 return len;
15217 }
15218
15219 /*
15220 * Include any <SID> etc in the expanded string:
15221 * Thus the -len here.
15222 */
15223 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
15224 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015225 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015226 *alias = temp_string;
15227 *arg = skipwhite(p);
15228 return (int)STRLEN(temp_string);
15229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015230
15231 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015232 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015233 EMSG2(_(e_invexpr2), *arg);
15234
15235 return len;
15236}
15237
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015238/*
15239 * Find the end of a variable or function name, taking care of magic braces.
15240 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
15241 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015242 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015243 * Return a pointer to just after the name. Equal to "arg" if there is no
15244 * valid name.
15245 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015246 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015247find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015248 char_u *arg;
15249 char_u **expr_start;
15250 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015251 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015252{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015253 int mb_nest = 0;
15254 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015255 char_u *p;
15256
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015257 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015258 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015259 *expr_start = NULL;
15260 *expr_end = NULL;
15261 }
15262
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015263 /* Quick check for valid starting character. */
15264 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
15265 return arg;
15266
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015267 for (p = arg; *p != NUL
15268 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015269 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015270 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015271 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000015272 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015273 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000015274 if (*p == '\'')
15275 {
15276 /* skip over 'string' to avoid counting [ and ] inside it. */
15277 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
15278 ;
15279 if (*p == NUL)
15280 break;
15281 }
15282 else if (*p == '"')
15283 {
15284 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
15285 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
15286 if (*p == '\\' && p[1] != NUL)
15287 ++p;
15288 if (*p == NUL)
15289 break;
15290 }
15291
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015292 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015294 if (*p == '[')
15295 ++br_nest;
15296 else if (*p == ']')
15297 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015298 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000015299
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015300 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015301 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015302 if (*p == '{')
15303 {
15304 mb_nest++;
15305 if (expr_start != NULL && *expr_start == NULL)
15306 *expr_start = p;
15307 }
15308 else if (*p == '}')
15309 {
15310 mb_nest--;
15311 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
15312 *expr_end = p;
15313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015315 }
15316
15317 return p;
15318}
15319
15320/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015321 * Expands out the 'magic' {}'s in a variable/function name.
15322 * Note that this can call itself recursively, to deal with
15323 * constructs like foo{bar}{baz}{bam}
15324 * The four pointer arguments point to "foo{expre}ss{ion}bar"
15325 * "in_start" ^
15326 * "expr_start" ^
15327 * "expr_end" ^
15328 * "in_end" ^
15329 *
15330 * Returns a new allocated string, which the caller must free.
15331 * Returns NULL for failure.
15332 */
15333 static char_u *
15334make_expanded_name(in_start, expr_start, expr_end, in_end)
15335 char_u *in_start;
15336 char_u *expr_start;
15337 char_u *expr_end;
15338 char_u *in_end;
15339{
15340 char_u c1;
15341 char_u *retval = NULL;
15342 char_u *temp_result;
15343 char_u *nextcmd = NULL;
15344
15345 if (expr_end == NULL || in_end == NULL)
15346 return NULL;
15347 *expr_start = NUL;
15348 *expr_end = NUL;
15349 c1 = *in_end;
15350 *in_end = NUL;
15351
15352 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15353 if (temp_result != NULL && nextcmd == NULL)
15354 {
15355 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15356 + (in_end - expr_end) + 1));
15357 if (retval != NULL)
15358 {
15359 STRCPY(retval, in_start);
15360 STRCAT(retval, temp_result);
15361 STRCAT(retval, expr_end + 1);
15362 }
15363 }
15364 vim_free(temp_result);
15365
15366 *in_end = c1; /* put char back for error messages */
15367 *expr_start = '{';
15368 *expr_end = '}';
15369
15370 if (retval != NULL)
15371 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015372 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015373 if (expr_start != NULL)
15374 {
15375 /* Further expansion! */
15376 temp_result = make_expanded_name(retval, expr_start,
15377 expr_end, temp_result);
15378 vim_free(retval);
15379 retval = temp_result;
15380 }
15381 }
15382
15383 return retval;
15384}
15385
15386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015387 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015388 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015389 */
15390 static int
15391eval_isnamec(c)
15392 int c;
15393{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015394 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15395}
15396
15397/*
15398 * Return TRUE if character "c" can be used as the first character in a
15399 * variable or function name (excluding '{' and '}').
15400 */
15401 static int
15402eval_isnamec1(c)
15403 int c;
15404{
15405 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406}
15407
15408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015409 * Set number v: variable to "val".
15410 */
15411 void
15412set_vim_var_nr(idx, val)
15413 int idx;
15414 long val;
15415{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015416 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015417}
15418
15419/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015420 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015421 */
15422 long
15423get_vim_var_nr(idx)
15424 int idx;
15425{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015426 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015427}
15428
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015429#if defined(FEAT_AUTOCMD) || defined(PROTO)
15430/*
15431 * Get string v: variable value. Uses a static buffer, can only be used once.
15432 */
15433 char_u *
15434get_vim_var_str(idx)
15435 int idx;
15436{
15437 return get_tv_string(&vimvars[idx].vv_tv);
15438}
15439#endif
15440
Bram Moolenaar071d4272004-06-13 20:20:40 +000015441/*
15442 * Set v:count, v:count1 and v:prevcount.
15443 */
15444 void
15445set_vcount(count, count1)
15446 long count;
15447 long count1;
15448{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015449 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15450 vimvars[VV_COUNT].vv_nr = count;
15451 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015452}
15453
15454/*
15455 * Set string v: variable to a copy of "val".
15456 */
15457 void
15458set_vim_var_string(idx, val, len)
15459 int idx;
15460 char_u *val;
15461 int len; /* length of "val" to use or -1 (whole string) */
15462{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015463 /* Need to do this (at least) once, since we can't initialize a union.
15464 * Will always be invoked when "v:progname" is set. */
15465 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15466
Bram Moolenaare9a41262005-01-15 22:18:47 +000015467 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015468 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015469 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015470 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015471 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015473 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015474}
15475
15476/*
15477 * Set v:register if needed.
15478 */
15479 void
15480set_reg_var(c)
15481 int c;
15482{
15483 char_u regname;
15484
15485 if (c == 0 || c == ' ')
15486 regname = '"';
15487 else
15488 regname = c;
15489 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015490 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015491 set_vim_var_string(VV_REG, &regname, 1);
15492}
15493
15494/*
15495 * Get or set v:exception. If "oldval" == NULL, return the current value.
15496 * Otherwise, restore the value to "oldval" and return NULL.
15497 * Must always be called in pairs to save and restore v:exception! Does not
15498 * take care of memory allocations.
15499 */
15500 char_u *
15501v_exception(oldval)
15502 char_u *oldval;
15503{
15504 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015505 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506
Bram Moolenaare9a41262005-01-15 22:18:47 +000015507 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508 return NULL;
15509}
15510
15511/*
15512 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15513 * Otherwise, restore the value to "oldval" and return NULL.
15514 * Must always be called in pairs to save and restore v:throwpoint! Does not
15515 * take care of memory allocations.
15516 */
15517 char_u *
15518v_throwpoint(oldval)
15519 char_u *oldval;
15520{
15521 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015522 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523
Bram Moolenaare9a41262005-01-15 22:18:47 +000015524 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525 return NULL;
15526}
15527
15528#if defined(FEAT_AUTOCMD) || defined(PROTO)
15529/*
15530 * Set v:cmdarg.
15531 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15532 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15533 * Must always be called in pairs!
15534 */
15535 char_u *
15536set_cmdarg(eap, oldarg)
15537 exarg_T *eap;
15538 char_u *oldarg;
15539{
15540 char_u *oldval;
15541 char_u *newval;
15542 unsigned len;
15543
Bram Moolenaare9a41262005-01-15 22:18:47 +000015544 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015545 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015547 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015548 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015549 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550 }
15551
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015552 if (eap->force_bin == FORCE_BIN)
15553 len = 6;
15554 else if (eap->force_bin == FORCE_NOBIN)
15555 len = 8;
15556 else
15557 len = 0;
15558 if (eap->force_ff != 0)
15559 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15560# ifdef FEAT_MBYTE
15561 if (eap->force_enc != 0)
15562 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
15563# endif
15564
15565 newval = alloc(len + 1);
15566 if (newval == NULL)
15567 return NULL;
15568
15569 if (eap->force_bin == FORCE_BIN)
15570 sprintf((char *)newval, " ++bin");
15571 else if (eap->force_bin == FORCE_NOBIN)
15572 sprintf((char *)newval, " ++nobin");
15573 else
15574 *newval = NUL;
15575 if (eap->force_ff != 0)
15576 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15577 eap->cmd + eap->force_ff);
15578# ifdef FEAT_MBYTE
15579 if (eap->force_enc != 0)
15580 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15581 eap->cmd + eap->force_enc);
15582# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015583 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015584 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585}
15586#endif
15587
15588/*
15589 * Get the value of internal variable "name".
15590 * Return OK or FAIL.
15591 */
15592 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015593get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015594 char_u *name;
15595 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015596 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015597 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598{
15599 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015600 typval_T *tv = NULL;
15601 typval_T atv;
15602 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015604
15605 /* truncate the name, so that we can use strcmp() */
15606 cc = name[len];
15607 name[len] = NUL;
15608
15609 /*
15610 * Check for "b:changedtick".
15611 */
15612 if (STRCMP(name, "b:changedtick") == 0)
15613 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015614 atv.v_type = VAR_NUMBER;
15615 atv.vval.v_number = curbuf->b_changedtick;
15616 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617 }
15618
15619 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015620 * Check for user-defined variables.
15621 */
15622 else
15623 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015624 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015625 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015626 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627 }
15628
Bram Moolenaare9a41262005-01-15 22:18:47 +000015629 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015630 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015631 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015632 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633 ret = FAIL;
15634 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015635 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015636 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637
15638 name[len] = cc;
15639
15640 return ret;
15641}
15642
15643/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015644 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15645 * Also handle function call with Funcref variable: func(expr)
15646 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15647 */
15648 static int
15649handle_subscript(arg, rettv, evaluate, verbose)
15650 char_u **arg;
15651 typval_T *rettv;
15652 int evaluate; /* do more than finding the end */
15653 int verbose; /* give error messages */
15654{
15655 int ret = OK;
15656 dict_T *selfdict = NULL;
15657 char_u *s;
15658 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015659 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015660
15661 while (ret == OK
15662 && (**arg == '['
15663 || (**arg == '.' && rettv->v_type == VAR_DICT)
15664 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15665 && !vim_iswhite(*(*arg - 1)))
15666 {
15667 if (**arg == '(')
15668 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015669 /* need to copy the funcref so that we can clear rettv */
15670 functv = *rettv;
15671 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015672
15673 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015674 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015675 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015676 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15677 &len, evaluate, selfdict);
15678
15679 /* Clear the funcref afterwards, so that deleting it while
15680 * evaluating the arguments is possible (see test55). */
15681 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015682
15683 /* Stop the expression evaluation when immediately aborting on
15684 * error, or when an interrupt occurred or an exception was thrown
15685 * but not caught. */
15686 if (aborting())
15687 {
15688 if (ret == OK)
15689 clear_tv(rettv);
15690 ret = FAIL;
15691 }
15692 dict_unref(selfdict);
15693 selfdict = NULL;
15694 }
15695 else /* **arg == '[' || **arg == '.' */
15696 {
15697 dict_unref(selfdict);
15698 if (rettv->v_type == VAR_DICT)
15699 {
15700 selfdict = rettv->vval.v_dict;
15701 if (selfdict != NULL)
15702 ++selfdict->dv_refcount;
15703 }
15704 else
15705 selfdict = NULL;
15706 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15707 {
15708 clear_tv(rettv);
15709 ret = FAIL;
15710 }
15711 }
15712 }
15713 dict_unref(selfdict);
15714 return ret;
15715}
15716
15717/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015718 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15719 * value).
15720 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015721 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015722alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015723{
Bram Moolenaar33570922005-01-25 22:26:29 +000015724 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015725}
15726
15727/*
15728 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015729 * The string "s" must have been allocated, it is consumed.
15730 * Return NULL for out of memory, the variable otherwise.
15731 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015732 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015733alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734 char_u *s;
15735{
Bram Moolenaar33570922005-01-25 22:26:29 +000015736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015738 rettv = alloc_tv();
15739 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015740 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015741 rettv->v_type = VAR_STRING;
15742 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015743 }
15744 else
15745 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015746 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747}
15748
15749/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015750 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015751 */
15752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015753free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015754 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015755{
15756 if (varp != NULL)
15757 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015758 switch (varp->v_type)
15759 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015760 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015761 func_unref(varp->vval.v_string);
15762 /*FALLTHROUGH*/
15763 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015764 vim_free(varp->vval.v_string);
15765 break;
15766 case VAR_LIST:
15767 list_unref(varp->vval.v_list);
15768 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015769 case VAR_DICT:
15770 dict_unref(varp->vval.v_dict);
15771 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015772 case VAR_NUMBER:
15773 case VAR_UNKNOWN:
15774 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015775 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015776 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015777 break;
15778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779 vim_free(varp);
15780 }
15781}
15782
15783/*
15784 * Free the memory for a variable value and set the value to NULL or 0.
15785 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000015786 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015787clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015788 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789{
15790 if (varp != NULL)
15791 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015792 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015793 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015794 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015795 func_unref(varp->vval.v_string);
15796 /*FALLTHROUGH*/
15797 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015798 vim_free(varp->vval.v_string);
15799 varp->vval.v_string = NULL;
15800 break;
15801 case VAR_LIST:
15802 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015803 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015804 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015805 case VAR_DICT:
15806 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015807 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015808 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015809 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015810 varp->vval.v_number = 0;
15811 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015812 case VAR_UNKNOWN:
15813 break;
15814 default:
15815 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015817 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015818 }
15819}
15820
15821/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015822 * Set the value of a variable to NULL without freeing items.
15823 */
15824 static void
15825init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015826 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015827{
15828 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015829 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015830}
15831
15832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833 * Get the number value of a variable.
15834 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015835 * For incompatible types, return 0.
15836 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15837 * caller of incompatible types: it sets *denote to TRUE if "denote"
15838 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839 */
15840 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015841get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015842 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015843{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015844 int error = FALSE;
15845
15846 return get_tv_number_chk(varp, &error); /* return 0L on error */
15847}
15848
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015849 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015850get_tv_number_chk(varp, denote)
15851 typval_T *varp;
15852 int *denote;
15853{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015854 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015856 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015857 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015858 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015859 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015860 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015861 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015862 break;
15863 case VAR_STRING:
15864 if (varp->vval.v_string != NULL)
15865 vim_str2nr(varp->vval.v_string, NULL, NULL,
15866 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015867 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015868 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015869 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015870 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015871 case VAR_DICT:
15872 EMSG(_("E728: Using a Dictionary as a number"));
15873 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015874 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015875 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015876 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015877 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015878 if (denote == NULL) /* useful for values that must be unsigned */
15879 n = -1;
15880 else
15881 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015882 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015883}
15884
15885/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000015886 * Get the lnum from the first argument.
15887 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015888 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015889 */
15890 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015891get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000015892 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015893{
Bram Moolenaar33570922005-01-25 22:26:29 +000015894 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015895 linenr_T lnum;
15896
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015897 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015898 if (lnum == 0) /* no valid number, try using line() */
15899 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015900 rettv.v_type = VAR_NUMBER;
15901 f_line(argvars, &rettv);
15902 lnum = rettv.vval.v_number;
15903 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904 }
15905 return lnum;
15906}
15907
15908/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000015909 * Get the lnum from the first argument.
15910 * Also accepts "$", then "buf" is used.
15911 * Returns 0 on error.
15912 */
15913 static linenr_T
15914get_tv_lnum_buf(argvars, buf)
15915 typval_T *argvars;
15916 buf_T *buf;
15917{
15918 if (argvars[0].v_type == VAR_STRING
15919 && argvars[0].vval.v_string != NULL
15920 && argvars[0].vval.v_string[0] == '$'
15921 && buf != NULL)
15922 return buf->b_ml.ml_line_count;
15923 return get_tv_number_chk(&argvars[0], NULL);
15924}
15925
15926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015927 * Get the string value of a variable.
15928 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000015929 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
15930 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015931 * If the String variable has never been set, return an empty string.
15932 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015933 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
15934 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015935 */
15936 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015937get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015938 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015939{
15940 static char_u mybuf[NUMBUFLEN];
15941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015942 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015943}
15944
15945 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015946get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000015947 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015948 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015949{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015950 char_u *res = get_tv_string_buf_chk(varp, buf);
15951
15952 return res != NULL ? res : (char_u *)"";
15953}
15954
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015955 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015956get_tv_string_chk(varp)
15957 typval_T *varp;
15958{
15959 static char_u mybuf[NUMBUFLEN];
15960
15961 return get_tv_string_buf_chk(varp, mybuf);
15962}
15963
15964 static char_u *
15965get_tv_string_buf_chk(varp, buf)
15966 typval_T *varp;
15967 char_u *buf;
15968{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015969 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015970 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015971 case VAR_NUMBER:
15972 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
15973 return buf;
15974 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015975 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015976 break;
15977 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015978 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015979 break;
15980 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015981 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015982 break;
15983 case VAR_STRING:
15984 if (varp->vval.v_string != NULL)
15985 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015986 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015987 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015988 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015989 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015991 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992}
15993
15994/*
15995 * Find variable "name" in the list of variables.
15996 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015997 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015998 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000015999 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016000 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016001 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016002find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016003 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016004 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016005{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016006 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016007 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016008
Bram Moolenaara7043832005-01-21 11:56:39 +000016009 ht = find_var_ht(name, &varname);
16010 if (htp != NULL)
16011 *htp = ht;
16012 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016014 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016015}
16016
16017/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016018 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000016019 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016020 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016021 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016022find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000016023 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000016024 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016025 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000016026{
Bram Moolenaar33570922005-01-25 22:26:29 +000016027 hashitem_T *hi;
16028
16029 if (*varname == NUL)
16030 {
16031 /* Must be something like "s:", otherwise "ht" would be NULL. */
16032 switch (varname[-2])
16033 {
16034 case 's': return &SCRIPT_SV(current_SID).sv_var;
16035 case 'g': return &globvars_var;
16036 case 'v': return &vimvars_var;
16037 case 'b': return &curbuf->b_bufvar;
16038 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016039 case 'l': return current_funccal == NULL
16040 ? NULL : &current_funccal->l_vars_var;
16041 case 'a': return current_funccal == NULL
16042 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000016043 }
16044 return NULL;
16045 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016046
16047 hi = hash_find(ht, varname);
16048 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016049 {
16050 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016051 * worked find the variable again. Don't auto-load a script if it was
16052 * loaded already, otherwise it would be loaded every time when
16053 * checking if a function name is a Funcref variable. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016054 if (ht == &globvarht && !writing
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000016055 && script_autoload(varname, FALSE) && !aborting())
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016056 hi = hash_find(ht, varname);
16057 if (HASHITEM_EMPTY(hi))
16058 return NULL;
16059 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016060 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016061}
16062
16063/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016064 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000016065 * Set "varname" to the start of name without ':'.
16066 */
Bram Moolenaar33570922005-01-25 22:26:29 +000016067 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000016068find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016069 char_u *name;
16070 char_u **varname;
16071{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016072 hashitem_T *hi;
16073
Bram Moolenaar071d4272004-06-13 20:20:40 +000016074 if (name[1] != ':')
16075 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016076 /* The name must not start with a colon or #. */
16077 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016078 return NULL;
16079 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000016080
16081 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000016082 hi = hash_find(&compat_hashtab, name);
16083 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000016084 return &compat_hashtab;
16085
Bram Moolenaar071d4272004-06-13 20:20:40 +000016086 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016087 return &globvarht; /* global variable */
16088 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016089 }
16090 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016091 if (*name == 'g') /* global variable */
16092 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016093 /* There must be no ':' or '#' in the rest of the name, unless g: is used
16094 */
16095 if (vim_strchr(name + 2, ':') != NULL
16096 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016097 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016099 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016100 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000016101 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000016102 if (*name == 'v') /* v: variable */
16103 return &vimvarht;
16104 if (*name == 'a' && current_funccal != NULL) /* function argument */
16105 return &current_funccal->l_avars.dv_hashtab;
16106 if (*name == 'l' && current_funccal != NULL) /* local function variable */
16107 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016108 if (*name == 's' /* script variable */
16109 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
16110 return &SCRIPT_VARS(current_SID);
16111 return NULL;
16112}
16113
16114/*
16115 * Get the string value of a (global/local) variable.
16116 * Returns NULL when it doesn't exist.
16117 */
16118 char_u *
16119get_var_value(name)
16120 char_u *name;
16121{
Bram Moolenaar33570922005-01-25 22:26:29 +000016122 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016123
Bram Moolenaara7043832005-01-21 11:56:39 +000016124 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016125 if (v == NULL)
16126 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016127 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016128}
16129
16130/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016131 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000016132 * sourcing this script and when executing functions defined in the script.
16133 */
16134 void
16135new_script_vars(id)
16136 scid_T id;
16137{
Bram Moolenaara7043832005-01-21 11:56:39 +000016138 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000016139 hashtab_T *ht;
16140 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000016141
Bram Moolenaar071d4272004-06-13 20:20:40 +000016142 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
16143 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016144 /* Re-allocating ga_data means that an ht_array pointing to
16145 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000016146 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000016147 for (i = 1; i <= ga_scripts.ga_len; ++i)
16148 {
16149 ht = &SCRIPT_VARS(i);
16150 if (ht->ht_mask == HT_INIT_SIZE - 1)
16151 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000016152 sv = &SCRIPT_SV(i);
16153 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000016154 }
16155
Bram Moolenaar071d4272004-06-13 20:20:40 +000016156 while (ga_scripts.ga_len < id)
16157 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016158 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
16159 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016161 }
16162 }
16163}
16164
16165/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016166 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
16167 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016168 */
16169 void
Bram Moolenaar33570922005-01-25 22:26:29 +000016170init_var_dict(dict, dict_var)
16171 dict_T *dict;
16172 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016173{
Bram Moolenaar33570922005-01-25 22:26:29 +000016174 hash_init(&dict->dv_hashtab);
16175 dict->dv_refcount = 99999;
16176 dict_var->di_tv.vval.v_dict = dict;
16177 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016178 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016179 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16180 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016181}
16182
16183/*
16184 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000016185 * Frees all allocated variables and the value they contain.
16186 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187 */
16188 void
Bram Moolenaara7043832005-01-21 11:56:39 +000016189vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000016190 hashtab_T *ht;
16191{
16192 vars_clear_ext(ht, TRUE);
16193}
16194
16195/*
16196 * Like vars_clear(), but only free the value if "free_val" is TRUE.
16197 */
16198 static void
16199vars_clear_ext(ht, free_val)
16200 hashtab_T *ht;
16201 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202{
Bram Moolenaara7043832005-01-21 11:56:39 +000016203 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016204 hashitem_T *hi;
16205 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206
Bram Moolenaar33570922005-01-25 22:26:29 +000016207 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000016208 todo = ht->ht_used;
16209 for (hi = ht->ht_array; todo > 0; ++hi)
16210 {
16211 if (!HASHITEM_EMPTY(hi))
16212 {
16213 --todo;
16214
Bram Moolenaar33570922005-01-25 22:26:29 +000016215 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000016216 * ht_array might change then. hash_clear() takes care of it
16217 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016218 v = HI2DI(hi);
16219 if (free_val)
16220 clear_tv(&v->di_tv);
16221 if ((v->di_flags & DI_FLAGS_FIX) == 0)
16222 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000016223 }
16224 }
16225 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016226 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227}
16228
Bram Moolenaara7043832005-01-21 11:56:39 +000016229/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016230 * Delete a variable from hashtab "ht" at item "hi".
16231 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000016232 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000016234delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000016235 hashtab_T *ht;
16236 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237{
Bram Moolenaar33570922005-01-25 22:26:29 +000016238 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000016239
16240 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000016241 clear_tv(&di->di_tv);
16242 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243}
16244
16245/*
16246 * List the value of one internal variable.
16247 */
16248 static void
16249list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000016250 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251 char_u *prefix;
16252{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016253 char_u *tofree;
16254 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016255 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016256
Bram Moolenaar33570922005-01-25 22:26:29 +000016257 s = echo_string(&v->di_tv, &tofree, numbuf);
16258 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016259 s == NULL ? (char_u *)"" : s);
16260 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261}
16262
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263 static void
16264list_one_var_a(prefix, name, type, string)
16265 char_u *prefix;
16266 char_u *name;
16267 int type;
16268 char_u *string;
16269{
16270 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
16271 if (name != NULL) /* "a:" vars don't have a name stored */
16272 msg_puts(name);
16273 msg_putchar(' ');
16274 msg_advance(22);
16275 if (type == VAR_NUMBER)
16276 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016277 else if (type == VAR_FUNC)
16278 msg_putchar('*');
16279 else if (type == VAR_LIST)
16280 {
16281 msg_putchar('[');
16282 if (*string == '[')
16283 ++string;
16284 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016285 else if (type == VAR_DICT)
16286 {
16287 msg_putchar('{');
16288 if (*string == '{')
16289 ++string;
16290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291 else
16292 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016293
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016295
16296 if (type == VAR_FUNC)
16297 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298}
16299
16300/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016301 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302 * If the variable already exists, the value is updated.
16303 * Otherwise the variable is created.
16304 */
16305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016306set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000016308 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016309 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310{
Bram Moolenaar33570922005-01-25 22:26:29 +000016311 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016313 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000016314 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016315
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016316 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016317 {
16318 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
16319 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
16320 ? name[2] : name[0]))
16321 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016322 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016323 return;
16324 }
16325 if (function_exists(name))
16326 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000016327 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016328 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016329 return;
16330 }
16331 }
16332
Bram Moolenaara7043832005-01-21 11:56:39 +000016333 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016334 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000016335 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016336 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000016337 return;
16338 }
16339
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016340 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016341 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016342 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016343 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016344 if (var_check_ro(v->di_flags, name)
16345 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000016346 return;
16347 if (v->di_tv.v_type != tv->v_type
16348 && !((v->di_tv.v_type == VAR_STRING
16349 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016350 && (tv->v_type == VAR_STRING
16351 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016352 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000016353 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016354 return;
16355 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016356
16357 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000016358 * Handle setting internal v: variables separately: we don't change
16359 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000016360 */
16361 if (ht == &vimvarht)
16362 {
16363 if (v->di_tv.v_type == VAR_STRING)
16364 {
16365 vim_free(v->di_tv.vval.v_string);
16366 if (copy || tv->v_type != VAR_STRING)
16367 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16368 else
16369 {
16370 /* Take over the string to avoid an extra alloc/free. */
16371 v->di_tv.vval.v_string = tv->vval.v_string;
16372 tv->vval.v_string = NULL;
16373 }
16374 }
16375 else if (v->di_tv.v_type != VAR_NUMBER)
16376 EMSG2(_(e_intern2), "set_var()");
16377 else
16378 v->di_tv.vval.v_number = get_tv_number(tv);
16379 return;
16380 }
16381
16382 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383 }
16384 else /* add a new variable */
16385 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016386 /* Make sure the variable name is valid. */
16387 for (p = varname; *p != NUL; ++p)
16388 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)))
16389 {
16390 EMSG2(_(e_illvar), varname);
16391 return;
16392 }
16393
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016394 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16395 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016396 if (v == NULL)
16397 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016398 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016399 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016400 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016401 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016402 return;
16403 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016404 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016406
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016407 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016408 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016409 else
16410 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016411 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016412 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016413 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415}
16416
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016417/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016418 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16419 * Also give an error message.
16420 */
16421 static int
16422var_check_ro(flags, name)
16423 int flags;
16424 char_u *name;
16425{
16426 if (flags & DI_FLAGS_RO)
16427 {
16428 EMSG2(_(e_readonlyvar), name);
16429 return TRUE;
16430 }
16431 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16432 {
16433 EMSG2(_(e_readonlysbx), name);
16434 return TRUE;
16435 }
16436 return FALSE;
16437}
16438
16439/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016440 * Return TRUE if typeval "tv" is set to be locked (immutable).
16441 * Also give an error message, using "name".
16442 */
16443 static int
16444tv_check_lock(lock, name)
16445 int lock;
16446 char_u *name;
16447{
16448 if (lock & VAR_LOCKED)
16449 {
16450 EMSG2(_("E741: Value is locked: %s"),
16451 name == NULL ? (char_u *)_("Unknown") : name);
16452 return TRUE;
16453 }
16454 if (lock & VAR_FIXED)
16455 {
16456 EMSG2(_("E742: Cannot change value of %s"),
16457 name == NULL ? (char_u *)_("Unknown") : name);
16458 return TRUE;
16459 }
16460 return FALSE;
16461}
16462
16463/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016464 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016465 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016466 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016469copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016470 typval_T *from;
16471 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016472{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016473 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016474 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016475 switch (from->v_type)
16476 {
16477 case VAR_NUMBER:
16478 to->vval.v_number = from->vval.v_number;
16479 break;
16480 case VAR_STRING:
16481 case VAR_FUNC:
16482 if (from->vval.v_string == NULL)
16483 to->vval.v_string = NULL;
16484 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016485 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016486 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016487 if (from->v_type == VAR_FUNC)
16488 func_ref(to->vval.v_string);
16489 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016490 break;
16491 case VAR_LIST:
16492 if (from->vval.v_list == NULL)
16493 to->vval.v_list = NULL;
16494 else
16495 {
16496 to->vval.v_list = from->vval.v_list;
16497 ++to->vval.v_list->lv_refcount;
16498 }
16499 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016500 case VAR_DICT:
16501 if (from->vval.v_dict == NULL)
16502 to->vval.v_dict = NULL;
16503 else
16504 {
16505 to->vval.v_dict = from->vval.v_dict;
16506 ++to->vval.v_dict->dv_refcount;
16507 }
16508 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016509 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016510 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016511 break;
16512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016513}
16514
16515/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016516 * Make a copy of an item.
16517 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016518 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16519 * reference to an already copied list/dict can be used.
16520 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016521 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016522 static int
16523item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016524 typval_T *from;
16525 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016526 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016527 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016528{
16529 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016530 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016531
Bram Moolenaar33570922005-01-25 22:26:29 +000016532 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016533 {
16534 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016535 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016536 }
16537 ++recurse;
16538
16539 switch (from->v_type)
16540 {
16541 case VAR_NUMBER:
16542 case VAR_STRING:
16543 case VAR_FUNC:
16544 copy_tv(from, to);
16545 break;
16546 case VAR_LIST:
16547 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016548 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016549 if (from->vval.v_list == NULL)
16550 to->vval.v_list = NULL;
16551 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16552 {
16553 /* use the copy made earlier */
16554 to->vval.v_list = from->vval.v_list->lv_copylist;
16555 ++to->vval.v_list->lv_refcount;
16556 }
16557 else
16558 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16559 if (to->vval.v_list == NULL)
16560 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016561 break;
16562 case VAR_DICT:
16563 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016564 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016565 if (from->vval.v_dict == NULL)
16566 to->vval.v_dict = NULL;
16567 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16568 {
16569 /* use the copy made earlier */
16570 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16571 ++to->vval.v_dict->dv_refcount;
16572 }
16573 else
16574 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16575 if (to->vval.v_dict == NULL)
16576 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016577 break;
16578 default:
16579 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016580 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016581 }
16582 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016583 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016584}
16585
16586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587 * ":echo expr1 ..." print each argument separated with a space, add a
16588 * newline at the end.
16589 * ":echon expr1 ..." print each argument plain.
16590 */
16591 void
16592ex_echo(eap)
16593 exarg_T *eap;
16594{
16595 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016596 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016597 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016598 char_u *p;
16599 int needclr = TRUE;
16600 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016601 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016602
16603 if (eap->skip)
16604 ++emsg_skip;
16605 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16606 {
16607 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016608 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016609 {
16610 /*
16611 * Report the invalid expression unless the expression evaluation
16612 * has been cancelled due to an aborting error, an interrupt, or an
16613 * exception.
16614 */
16615 if (!aborting())
16616 EMSG2(_(e_invexpr2), p);
16617 break;
16618 }
16619 if (!eap->skip)
16620 {
16621 if (atstart)
16622 {
16623 atstart = FALSE;
16624 /* Call msg_start() after eval1(), evaluating the expression
16625 * may cause a message to appear. */
16626 if (eap->cmdidx == CMD_echo)
16627 msg_start();
16628 }
16629 else if (eap->cmdidx == CMD_echo)
16630 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016631 p = echo_string(&rettv, &tofree, numbuf);
16632 if (p != NULL)
16633 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016635 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016636 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016637 if (*p != TAB && needclr)
16638 {
16639 /* remove any text still there from the command */
16640 msg_clr_eos();
16641 needclr = FALSE;
16642 }
16643 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644 }
16645 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016646 {
16647#ifdef FEAT_MBYTE
16648 if (has_mbyte)
16649 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000016650 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016651
16652 (void)msg_outtrans_len_attr(p, i, echo_attr);
16653 p += i - 1;
16654 }
16655 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016657 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016659 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016660 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016661 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016662 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016663 arg = skipwhite(arg);
16664 }
16665 eap->nextcmd = check_nextcmd(arg);
16666
16667 if (eap->skip)
16668 --emsg_skip;
16669 else
16670 {
16671 /* remove text that may still be there from the command */
16672 if (needclr)
16673 msg_clr_eos();
16674 if (eap->cmdidx == CMD_echo)
16675 msg_end();
16676 }
16677}
16678
16679/*
16680 * ":echohl {name}".
16681 */
16682 void
16683ex_echohl(eap)
16684 exarg_T *eap;
16685{
16686 int id;
16687
16688 id = syn_name2id(eap->arg);
16689 if (id == 0)
16690 echo_attr = 0;
16691 else
16692 echo_attr = syn_id2attr(id);
16693}
16694
16695/*
16696 * ":execute expr1 ..." execute the result of an expression.
16697 * ":echomsg expr1 ..." Print a message
16698 * ":echoerr expr1 ..." Print an error
16699 * Each gets spaces around each argument and a newline at the end for
16700 * echo commands
16701 */
16702 void
16703ex_execute(eap)
16704 exarg_T *eap;
16705{
16706 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016707 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016708 int ret = OK;
16709 char_u *p;
16710 garray_T ga;
16711 int len;
16712 int save_did_emsg;
16713
16714 ga_init2(&ga, 1, 80);
16715
16716 if (eap->skip)
16717 ++emsg_skip;
16718 while (*arg != NUL && *arg != '|' && *arg != '\n')
16719 {
16720 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016721 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016722 {
16723 /*
16724 * Report the invalid expression unless the expression evaluation
16725 * has been cancelled due to an aborting error, an interrupt, or an
16726 * exception.
16727 */
16728 if (!aborting())
16729 EMSG2(_(e_invexpr2), p);
16730 ret = FAIL;
16731 break;
16732 }
16733
16734 if (!eap->skip)
16735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016736 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016737 len = (int)STRLEN(p);
16738 if (ga_grow(&ga, len + 2) == FAIL)
16739 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016740 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016741 ret = FAIL;
16742 break;
16743 }
16744 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016746 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016747 ga.ga_len += len;
16748 }
16749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016750 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016751 arg = skipwhite(arg);
16752 }
16753
16754 if (ret != FAIL && ga.ga_data != NULL)
16755 {
16756 if (eap->cmdidx == CMD_echomsg)
16757 MSG_ATTR(ga.ga_data, echo_attr);
16758 else if (eap->cmdidx == CMD_echoerr)
16759 {
16760 /* We don't want to abort following commands, restore did_emsg. */
16761 save_did_emsg = did_emsg;
16762 EMSG((char_u *)ga.ga_data);
16763 if (!force_abort)
16764 did_emsg = save_did_emsg;
16765 }
16766 else if (eap->cmdidx == CMD_execute)
16767 do_cmdline((char_u *)ga.ga_data,
16768 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16769 }
16770
16771 ga_clear(&ga);
16772
16773 if (eap->skip)
16774 --emsg_skip;
16775
16776 eap->nextcmd = check_nextcmd(arg);
16777}
16778
16779/*
16780 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16781 * "arg" points to the "&" or '+' when called, to "option" when returning.
16782 * Returns NULL when no option name found. Otherwise pointer to the char
16783 * after the option name.
16784 */
16785 static char_u *
16786find_option_end(arg, opt_flags)
16787 char_u **arg;
16788 int *opt_flags;
16789{
16790 char_u *p = *arg;
16791
16792 ++p;
16793 if (*p == 'g' && p[1] == ':')
16794 {
16795 *opt_flags = OPT_GLOBAL;
16796 p += 2;
16797 }
16798 else if (*p == 'l' && p[1] == ':')
16799 {
16800 *opt_flags = OPT_LOCAL;
16801 p += 2;
16802 }
16803 else
16804 *opt_flags = 0;
16805
16806 if (!ASCII_ISALPHA(*p))
16807 return NULL;
16808 *arg = p;
16809
16810 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16811 p += 4; /* termcap option */
16812 else
16813 while (ASCII_ISALPHA(*p))
16814 ++p;
16815 return p;
16816}
16817
16818/*
16819 * ":function"
16820 */
16821 void
16822ex_function(eap)
16823 exarg_T *eap;
16824{
16825 char_u *theline;
16826 int j;
16827 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016828 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016829 char_u *name = NULL;
16830 char_u *p;
16831 char_u *arg;
16832 garray_T newargs;
16833 garray_T newlines;
16834 int varargs = FALSE;
16835 int mustend = FALSE;
16836 int flags = 0;
16837 ufunc_T *fp;
16838 int indent;
16839 int nesting;
16840 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016841 dictitem_T *v;
16842 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016843 static int func_nr = 0; /* number for nameless function */
16844 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016845 hashtab_T *ht;
16846 int todo;
16847 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016848
16849 /*
16850 * ":function" without argument: list functions.
16851 */
16852 if (ends_excmd(*eap->arg))
16853 {
16854 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016855 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000016856 todo = func_hashtab.ht_used;
16857 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016858 {
16859 if (!HASHITEM_EMPTY(hi))
16860 {
16861 --todo;
16862 fp = HI2UF(hi);
16863 if (!isdigit(*fp->uf_name))
16864 list_func_head(fp, FALSE);
16865 }
16866 }
16867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016868 eap->nextcmd = check_nextcmd(eap->arg);
16869 return;
16870 }
16871
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016872 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016873 * ":function /pat": list functions matching pattern.
16874 */
16875 if (*eap->arg == '/')
16876 {
16877 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
16878 if (!eap->skip)
16879 {
16880 regmatch_T regmatch;
16881
16882 c = *p;
16883 *p = NUL;
16884 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
16885 *p = c;
16886 if (regmatch.regprog != NULL)
16887 {
16888 regmatch.rm_ic = p_ic;
16889
16890 todo = func_hashtab.ht_used;
16891 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
16892 {
16893 if (!HASHITEM_EMPTY(hi))
16894 {
16895 --todo;
16896 fp = HI2UF(hi);
16897 if (!isdigit(*fp->uf_name)
16898 && vim_regexec(&regmatch, fp->uf_name, 0))
16899 list_func_head(fp, FALSE);
16900 }
16901 }
16902 }
16903 }
16904 if (*p == '/')
16905 ++p;
16906 eap->nextcmd = check_nextcmd(p);
16907 return;
16908 }
16909
16910 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016911 * Get the function name. There are these situations:
16912 * func normal function name
16913 * "name" == func, "fudi.fd_dict" == NULL
16914 * dict.func new dictionary entry
16915 * "name" == NULL, "fudi.fd_dict" set,
16916 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
16917 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016918 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016919 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16920 * dict.func existing dict entry that's not a Funcref
16921 * "name" == NULL, "fudi.fd_dict" set,
16922 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16923 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016924 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016925 name = trans_function_name(&p, eap->skip, 0, &fudi);
16926 paren = (vim_strchr(p, '(') != NULL);
16927 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016928 {
16929 /*
16930 * Return on an invalid expression in braces, unless the expression
16931 * evaluation has been cancelled due to an aborting error, an
16932 * interrupt, or an exception.
16933 */
16934 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016935 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016936 if (!eap->skip && fudi.fd_newkey != NULL)
16937 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016938 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016939 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941 else
16942 eap->skip = TRUE;
16943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016944 /* An error in a function call during evaluation of an expression in magic
16945 * braces should not cause the function not to be defined. */
16946 saved_did_emsg = did_emsg;
16947 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948
16949 /*
16950 * ":function func" with only function name: list function.
16951 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016952 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016953 {
16954 if (!ends_excmd(*skipwhite(p)))
16955 {
16956 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016957 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958 }
16959 eap->nextcmd = check_nextcmd(p);
16960 if (eap->nextcmd != NULL)
16961 *p = NUL;
16962 if (!eap->skip && !got_int)
16963 {
16964 fp = find_func(name);
16965 if (fp != NULL)
16966 {
16967 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016968 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969 {
16970 msg_putchar('\n');
16971 msg_outnum((long)(j + 1));
16972 if (j < 9)
16973 msg_putchar(' ');
16974 if (j < 99)
16975 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016976 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977 out_flush(); /* show a line at a time */
16978 ui_breakcheck();
16979 }
16980 if (!got_int)
16981 {
16982 msg_putchar('\n');
16983 msg_puts((char_u *)" endfunction");
16984 }
16985 }
16986 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016987 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016988 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016989 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990 }
16991
16992 /*
16993 * ":function name(arg1, arg2)" Define function.
16994 */
16995 p = skipwhite(p);
16996 if (*p != '(')
16997 {
16998 if (!eap->skip)
16999 {
17000 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017001 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017002 }
17003 /* attempt to continue by skipping some text */
17004 if (vim_strchr(p, '(') != NULL)
17005 p = vim_strchr(p, '(');
17006 }
17007 p = skipwhite(p + 1);
17008
17009 ga_init2(&newargs, (int)sizeof(char_u *), 3);
17010 ga_init2(&newlines, (int)sizeof(char_u *), 3);
17011
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017012 if (!eap->skip)
17013 {
17014 /* Check the name of the function. */
17015 if (name != NULL)
17016 arg = name;
17017 else
17018 arg = fudi.fd_newkey;
17019 if (arg != NULL)
17020 {
17021 if (*arg == K_SPECIAL)
17022 j = 3;
17023 else
17024 j = 0;
17025 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
17026 : eval_isnamec(arg[j])))
17027 ++j;
17028 if (arg[j] != NUL)
17029 emsg_funcname(_(e_invarg2), arg);
17030 }
17031 }
17032
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033 /*
17034 * Isolate the arguments: "arg1, arg2, ...)"
17035 */
17036 while (*p != ')')
17037 {
17038 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
17039 {
17040 varargs = TRUE;
17041 p += 3;
17042 mustend = TRUE;
17043 }
17044 else
17045 {
17046 arg = p;
17047 while (ASCII_ISALNUM(*p) || *p == '_')
17048 ++p;
17049 if (arg == p || isdigit(*arg)
17050 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
17051 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
17052 {
17053 if (!eap->skip)
17054 EMSG2(_("E125: Illegal argument: %s"), arg);
17055 break;
17056 }
17057 if (ga_grow(&newargs, 1) == FAIL)
17058 goto erret;
17059 c = *p;
17060 *p = NUL;
17061 arg = vim_strsave(arg);
17062 if (arg == NULL)
17063 goto erret;
17064 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
17065 *p = c;
17066 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067 if (*p == ',')
17068 ++p;
17069 else
17070 mustend = TRUE;
17071 }
17072 p = skipwhite(p);
17073 if (mustend && *p != ')')
17074 {
17075 if (!eap->skip)
17076 EMSG2(_(e_invarg2), eap->arg);
17077 break;
17078 }
17079 }
17080 ++p; /* skip the ')' */
17081
Bram Moolenaare9a41262005-01-15 22:18:47 +000017082 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017083 for (;;)
17084 {
17085 p = skipwhite(p);
17086 if (STRNCMP(p, "range", 5) == 0)
17087 {
17088 flags |= FC_RANGE;
17089 p += 5;
17090 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017091 else if (STRNCMP(p, "dict", 4) == 0)
17092 {
17093 flags |= FC_DICT;
17094 p += 4;
17095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017096 else if (STRNCMP(p, "abort", 5) == 0)
17097 {
17098 flags |= FC_ABORT;
17099 p += 5;
17100 }
17101 else
17102 break;
17103 }
17104
17105 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
17106 EMSG(_(e_trailing));
17107
17108 /*
17109 * Read the body of the function, until ":endfunction" is found.
17110 */
17111 if (KeyTyped)
17112 {
17113 /* Check if the function already exists, don't let the user type the
17114 * whole function before telling him it doesn't work! For a script we
17115 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017116 if (!eap->skip && !eap->forceit)
17117 {
17118 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
17119 EMSG(_(e_funcdict));
17120 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017121 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017124 if (!eap->skip && did_emsg)
17125 goto erret;
17126
Bram Moolenaar071d4272004-06-13 20:20:40 +000017127 msg_putchar('\n'); /* don't overwrite the function name */
17128 cmdline_row = msg_row;
17129 }
17130
17131 indent = 2;
17132 nesting = 0;
17133 for (;;)
17134 {
17135 msg_scroll = TRUE;
17136 need_wait_return = FALSE;
17137 if (eap->getline == NULL)
17138 theline = getcmdline(':', 0L, indent);
17139 else
17140 theline = eap->getline(':', eap->cookie, indent);
17141 if (KeyTyped)
17142 lines_left = Rows - 1;
17143 if (theline == NULL)
17144 {
17145 EMSG(_("E126: Missing :endfunction"));
17146 goto erret;
17147 }
17148
17149 if (skip_until != NULL)
17150 {
17151 /* between ":append" and "." and between ":python <<EOF" and "EOF"
17152 * don't check for ":endfunc". */
17153 if (STRCMP(theline, skip_until) == 0)
17154 {
17155 vim_free(skip_until);
17156 skip_until = NULL;
17157 }
17158 }
17159 else
17160 {
17161 /* skip ':' and blanks*/
17162 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
17163 ;
17164
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017165 /* Check for "endfunction". */
17166 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167 {
17168 vim_free(theline);
17169 break;
17170 }
17171
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017172 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000017173 * at "end". */
17174 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
17175 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017176 else if (STRNCMP(p, "if", 2) == 0
17177 || STRNCMP(p, "wh", 2) == 0
17178 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000017179 || STRNCMP(p, "try", 3) == 0)
17180 indent += 2;
17181
17182 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017183 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017184 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017185 if (*p == '!')
17186 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 p += eval_fname_script(p);
17188 if (ASCII_ISALPHA(*p))
17189 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017190 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017191 if (*skipwhite(p) == '(')
17192 {
17193 ++nesting;
17194 indent += 2;
17195 }
17196 }
17197 }
17198
17199 /* Check for ":append" or ":insert". */
17200 p = skip_range(p, NULL);
17201 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
17202 || (p[0] == 'i'
17203 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
17204 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
17205 skip_until = vim_strsave((char_u *)".");
17206
17207 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
17208 arg = skipwhite(skiptowhite(p));
17209 if (arg[0] == '<' && arg[1] =='<'
17210 && ((p[0] == 'p' && p[1] == 'y'
17211 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
17212 || (p[0] == 'p' && p[1] == 'e'
17213 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
17214 || (p[0] == 't' && p[1] == 'c'
17215 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
17216 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
17217 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000017218 || (p[0] == 'm' && p[1] == 'z'
17219 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000017220 ))
17221 {
17222 /* ":python <<" continues until a dot, like ":append" */
17223 p = skipwhite(arg + 2);
17224 if (*p == NUL)
17225 skip_until = vim_strsave((char_u *)".");
17226 else
17227 skip_until = vim_strsave(p);
17228 }
17229 }
17230
17231 /* Add the line to the function. */
17232 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017233 {
17234 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017235 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017236 }
17237
17238 /* Copy the line to newly allocated memory. get_one_sourceline()
17239 * allocates 250 bytes per line, this saves 80% on average. The cost
17240 * is an extra alloc/free. */
17241 p = vim_strsave(theline);
17242 if (p != NULL)
17243 {
17244 vim_free(theline);
17245 theline = p;
17246 }
17247
Bram Moolenaar071d4272004-06-13 20:20:40 +000017248 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
17249 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017250 }
17251
17252 /* Don't define the function when skipping commands or when an error was
17253 * detected. */
17254 if (eap->skip || did_emsg)
17255 goto erret;
17256
17257 /*
17258 * If there are no errors, add the function
17259 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017260 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017261 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017262 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000017263 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017264 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017265 emsg_funcname("E707: Function name conflicts with variable: %s",
17266 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017267 goto erret;
17268 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017269
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017270 fp = find_func(name);
17271 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017272 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017273 if (!eap->forceit)
17274 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017275 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017276 goto erret;
17277 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017278 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017279 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017280 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017281 name);
17282 goto erret;
17283 }
17284 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017285 ga_clear_strings(&(fp->uf_args));
17286 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017287 vim_free(name);
17288 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290 }
17291 else
17292 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017293 char numbuf[20];
17294
17295 fp = NULL;
17296 if (fudi.fd_newkey == NULL && !eap->forceit)
17297 {
17298 EMSG(_(e_funcdict));
17299 goto erret;
17300 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000017301 if (fudi.fd_di == NULL)
17302 {
17303 /* Can't add a function to a locked dictionary */
17304 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
17305 goto erret;
17306 }
17307 /* Can't change an existing function if it is locked */
17308 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
17309 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017310
17311 /* Give the function a sequential number. Can only be used with a
17312 * Funcref! */
17313 vim_free(name);
17314 sprintf(numbuf, "%d", ++func_nr);
17315 name = vim_strsave((char_u *)numbuf);
17316 if (name == NULL)
17317 goto erret;
17318 }
17319
17320 if (fp == NULL)
17321 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017322 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017323 {
17324 int slen, plen;
17325 char_u *scriptname;
17326
17327 /* Check that the autoload name matches the script name. */
17328 j = FAIL;
17329 if (sourcing_name != NULL)
17330 {
17331 scriptname = autoload_name(name);
17332 if (scriptname != NULL)
17333 {
17334 p = vim_strchr(scriptname, '/');
17335 plen = STRLEN(p);
17336 slen = STRLEN(sourcing_name);
17337 if (slen > plen && fnamecmp(p,
17338 sourcing_name + slen - plen) == 0)
17339 j = OK;
17340 vim_free(scriptname);
17341 }
17342 }
17343 if (j == FAIL)
17344 {
17345 EMSG2(_("E746: Function name does not match script file name: %s"), name);
17346 goto erret;
17347 }
17348 }
17349
17350 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351 if (fp == NULL)
17352 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017353
17354 if (fudi.fd_dict != NULL)
17355 {
17356 if (fudi.fd_di == NULL)
17357 {
17358 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017359 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017360 if (fudi.fd_di == NULL)
17361 {
17362 vim_free(fp);
17363 goto erret;
17364 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017365 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
17366 {
17367 vim_free(fudi.fd_di);
17368 goto erret;
17369 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017370 }
17371 else
17372 /* overwrite existing dict entry */
17373 clear_tv(&fudi.fd_di->di_tv);
17374 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017375 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017376 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017377 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017378 }
17379
Bram Moolenaar071d4272004-06-13 20:20:40 +000017380 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017381 STRCPY(fp->uf_name, name);
17382 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017383 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017384 fp->uf_args = newargs;
17385 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017386#ifdef FEAT_PROFILE
17387 fp->uf_tml_count = NULL;
17388 fp->uf_tml_total = NULL;
17389 fp->uf_tml_self = NULL;
17390 fp->uf_profiling = FALSE;
17391 if (prof_def_func())
17392 func_do_profile(fp);
17393#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017394 fp->uf_varargs = varargs;
17395 fp->uf_flags = flags;
17396 fp->uf_calls = 0;
17397 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017398 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017399
17400erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401 ga_clear_strings(&newargs);
17402 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017403ret_free:
17404 vim_free(skip_until);
17405 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408}
17409
17410/*
17411 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017412 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017414 * flags:
17415 * TFN_INT: internal function name OK
17416 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017417 * Advances "pp" to just after the function name (if no error).
17418 */
17419 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017420trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017421 char_u **pp;
17422 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017423 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017424 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017426 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017427 char_u *start;
17428 char_u *end;
17429 int lead;
17430 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017432 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017433
17434 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017435 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017437
17438 /* Check for hard coded <SNR>: already translated function ID (from a user
17439 * command). */
17440 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17441 && (*pp)[2] == (int)KE_SNR)
17442 {
17443 *pp += 3;
17444 len = get_id_len(pp) + 3;
17445 return vim_strnsave(start, len);
17446 }
17447
17448 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17449 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017451 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017453
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017454 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17455 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017456 if (end == start)
17457 {
17458 if (!skip)
17459 EMSG(_("E129: Function name required"));
17460 goto theend;
17461 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017462 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017463 {
17464 /*
17465 * Report an invalid expression in braces, unless the expression
17466 * evaluation has been cancelled due to an aborting error, an
17467 * interrupt, or an exception.
17468 */
17469 if (!aborting())
17470 {
17471 if (end != NULL)
17472 EMSG2(_(e_invarg2), start);
17473 }
17474 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017475 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017476 goto theend;
17477 }
17478
17479 if (lv.ll_tv != NULL)
17480 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017481 if (fdp != NULL)
17482 {
17483 fdp->fd_dict = lv.ll_dict;
17484 fdp->fd_newkey = lv.ll_newkey;
17485 lv.ll_newkey = NULL;
17486 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017487 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017488 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17489 {
17490 name = vim_strsave(lv.ll_tv->vval.v_string);
17491 *pp = end;
17492 }
17493 else
17494 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017495 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17496 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017497 EMSG(_(e_funcref));
17498 else
17499 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017500 name = NULL;
17501 }
17502 goto theend;
17503 }
17504
17505 if (lv.ll_name == NULL)
17506 {
17507 /* Error found, but continue after the function name. */
17508 *pp = end;
17509 goto theend;
17510 }
17511
17512 if (lv.ll_exp_name != NULL)
17513 len = STRLEN(lv.ll_exp_name);
17514 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017515 {
17516 if (lead == 2) /* skip over "s:" */
17517 lv.ll_name += 2;
17518 len = (int)(end - lv.ll_name);
17519 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017520
17521 /*
17522 * Copy the function name to allocated memory.
17523 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17524 * Accept <SNR>123_name() outside a script.
17525 */
17526 if (skip)
17527 lead = 0; /* do nothing */
17528 else if (lead > 0)
17529 {
17530 lead = 3;
17531 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17532 {
17533 if (current_SID <= 0)
17534 {
17535 EMSG(_(e_usingsid));
17536 goto theend;
17537 }
17538 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17539 lead += (int)STRLEN(sid_buf);
17540 }
17541 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017542 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017543 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017544 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017545 goto theend;
17546 }
17547 name = alloc((unsigned)(len + lead + 1));
17548 if (name != NULL)
17549 {
17550 if (lead > 0)
17551 {
17552 name[0] = K_SPECIAL;
17553 name[1] = KS_EXTRA;
17554 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017555 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017556 STRCPY(name + 3, sid_buf);
17557 }
17558 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17559 name[len + lead] = NUL;
17560 }
17561 *pp = end;
17562
17563theend:
17564 clear_lval(&lv);
17565 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566}
17567
17568/*
17569 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17570 * Return 2 if "p" starts with "s:".
17571 * Return 0 otherwise.
17572 */
17573 static int
17574eval_fname_script(p)
17575 char_u *p;
17576{
17577 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17578 || STRNICMP(p + 1, "SNR>", 4) == 0))
17579 return 5;
17580 if (p[0] == 's' && p[1] == ':')
17581 return 2;
17582 return 0;
17583}
17584
17585/*
17586 * Return TRUE if "p" starts with "<SID>" or "s:".
17587 * Only works if eval_fname_script() returned non-zero for "p"!
17588 */
17589 static int
17590eval_fname_sid(p)
17591 char_u *p;
17592{
17593 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17594}
17595
17596/*
17597 * List the head of the function: "name(arg1, arg2)".
17598 */
17599 static void
17600list_func_head(fp, indent)
17601 ufunc_T *fp;
17602 int indent;
17603{
17604 int j;
17605
17606 msg_start();
17607 if (indent)
17608 MSG_PUTS(" ");
17609 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017610 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017611 {
17612 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017613 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614 }
17615 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017616 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017618 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619 {
17620 if (j)
17621 MSG_PUTS(", ");
17622 msg_puts(FUNCARG(fp, j));
17623 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017624 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625 {
17626 if (j)
17627 MSG_PUTS(", ");
17628 MSG_PUTS("...");
17629 }
17630 msg_putchar(')');
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000017631 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000017632#ifdef FEAT_EVAL
17633 if (p_verbose > 0)
17634 last_set_msg(fp->uf_script_ID);
17635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636}
17637
17638/*
17639 * Find a function by name, return pointer to it in ufuncs.
17640 * Return NULL for unknown function.
17641 */
17642 static ufunc_T *
17643find_func(name)
17644 char_u *name;
17645{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017646 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017648 hi = hash_find(&func_hashtab, name);
17649 if (!HASHITEM_EMPTY(hi))
17650 return HI2UF(hi);
17651 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652}
17653
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017654#if defined(EXITFREE) || defined(PROTO)
17655 void
17656free_all_functions()
17657{
17658 hashitem_T *hi;
17659
17660 /* Need to start all over every time, because func_free() may change the
17661 * hash table. */
17662 while (func_hashtab.ht_used > 0)
17663 for (hi = func_hashtab.ht_array; ; ++hi)
17664 if (!HASHITEM_EMPTY(hi))
17665 {
17666 func_free(HI2UF(hi));
17667 break;
17668 }
17669}
17670#endif
17671
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017672/*
17673 * Return TRUE if a function "name" exists.
17674 */
17675 static int
17676function_exists(name)
17677 char_u *name;
17678{
17679 char_u *p = name;
17680 int n = FALSE;
17681
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017682 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017683 if (p != NULL)
17684 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017685 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017686 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017687 else
17688 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017689 vim_free(p);
17690 }
17691 return n;
17692}
17693
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017694/*
17695 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017696 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017697 */
17698 static int
17699builtin_function(name)
17700 char_u *name;
17701{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017702 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17703 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017704}
17705
Bram Moolenaar05159a02005-02-26 23:04:13 +000017706#if defined(FEAT_PROFILE) || defined(PROTO)
17707/*
17708 * Start profiling function "fp".
17709 */
17710 static void
17711func_do_profile(fp)
17712 ufunc_T *fp;
17713{
17714 fp->uf_tm_count = 0;
17715 profile_zero(&fp->uf_tm_self);
17716 profile_zero(&fp->uf_tm_total);
17717 if (fp->uf_tml_count == NULL)
17718 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17719 (sizeof(int) * fp->uf_lines.ga_len));
17720 if (fp->uf_tml_total == NULL)
17721 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17722 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17723 if (fp->uf_tml_self == NULL)
17724 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17725 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17726 fp->uf_tml_idx = -1;
17727 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17728 || fp->uf_tml_self == NULL)
17729 return; /* out of memory */
17730
17731 fp->uf_profiling = TRUE;
17732}
17733
17734/*
17735 * Dump the profiling results for all functions in file "fd".
17736 */
17737 void
17738func_dump_profile(fd)
17739 FILE *fd;
17740{
17741 hashitem_T *hi;
17742 int todo;
17743 ufunc_T *fp;
17744 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017745 ufunc_T **sorttab;
17746 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017747
17748 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017749 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17750
Bram Moolenaar05159a02005-02-26 23:04:13 +000017751 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17752 {
17753 if (!HASHITEM_EMPTY(hi))
17754 {
17755 --todo;
17756 fp = HI2UF(hi);
17757 if (fp->uf_profiling)
17758 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017759 if (sorttab != NULL)
17760 sorttab[st_len++] = fp;
17761
Bram Moolenaar05159a02005-02-26 23:04:13 +000017762 if (fp->uf_name[0] == K_SPECIAL)
17763 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17764 else
17765 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17766 if (fp->uf_tm_count == 1)
17767 fprintf(fd, "Called 1 time\n");
17768 else
17769 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17770 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17771 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17772 fprintf(fd, "\n");
17773 fprintf(fd, "count total (s) self (s)\n");
17774
17775 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17776 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017777 prof_func_line(fd, fp->uf_tml_count[i],
17778 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017779 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17780 }
17781 fprintf(fd, "\n");
17782 }
17783 }
17784 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017785
17786 if (sorttab != NULL && st_len > 0)
17787 {
17788 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17789 prof_total_cmp);
17790 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17791 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17792 prof_self_cmp);
17793 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17794 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017795}
Bram Moolenaar73830342005-02-28 22:48:19 +000017796
17797 static void
17798prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17799 FILE *fd;
17800 ufunc_T **sorttab;
17801 int st_len;
17802 char *title;
17803 int prefer_self; /* when equal print only self time */
17804{
17805 int i;
17806 ufunc_T *fp;
17807
17808 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17809 fprintf(fd, "count total (s) self (s) function\n");
17810 for (i = 0; i < 20 && i < st_len; ++i)
17811 {
17812 fp = sorttab[i];
17813 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17814 prefer_self);
17815 if (fp->uf_name[0] == K_SPECIAL)
17816 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17817 else
17818 fprintf(fd, " %s()\n", fp->uf_name);
17819 }
17820 fprintf(fd, "\n");
17821}
17822
17823/*
17824 * Print the count and times for one function or function line.
17825 */
17826 static void
17827prof_func_line(fd, count, total, self, prefer_self)
17828 FILE *fd;
17829 int count;
17830 proftime_T *total;
17831 proftime_T *self;
17832 int prefer_self; /* when equal print only self time */
17833{
17834 if (count > 0)
17835 {
17836 fprintf(fd, "%5d ", count);
17837 if (prefer_self && profile_equal(total, self))
17838 fprintf(fd, " ");
17839 else
17840 fprintf(fd, "%s ", profile_msg(total));
17841 if (!prefer_self && profile_equal(total, self))
17842 fprintf(fd, " ");
17843 else
17844 fprintf(fd, "%s ", profile_msg(self));
17845 }
17846 else
17847 fprintf(fd, " ");
17848}
17849
17850/*
17851 * Compare function for total time sorting.
17852 */
17853 static int
17854#ifdef __BORLANDC__
17855_RTLENTRYF
17856#endif
17857prof_total_cmp(s1, s2)
17858 const void *s1;
17859 const void *s2;
17860{
17861 ufunc_T *p1, *p2;
17862
17863 p1 = *(ufunc_T **)s1;
17864 p2 = *(ufunc_T **)s2;
17865 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
17866}
17867
17868/*
17869 * Compare function for self time sorting.
17870 */
17871 static int
17872#ifdef __BORLANDC__
17873_RTLENTRYF
17874#endif
17875prof_self_cmp(s1, s2)
17876 const void *s1;
17877 const void *s2;
17878{
17879 ufunc_T *p1, *p2;
17880
17881 p1 = *(ufunc_T **)s1;
17882 p2 = *(ufunc_T **)s2;
17883 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
17884}
17885
Bram Moolenaar05159a02005-02-26 23:04:13 +000017886#endif
17887
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017888/* The names of packages that once were loaded is remembered. */
17889static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
17890
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017891/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017892 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017893 * Return TRUE if a package was loaded.
17894 */
17895 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017896script_autoload(name, reload)
17897 char_u *name;
17898 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017899{
17900 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017901 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017902 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017903 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017904
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017905 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017906 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017907 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017908 return FALSE;
17909
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017910 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017911
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017912 /* Find the name in the list of previously loaded package names. Skip
17913 * "autoload/", it's always the same. */
17914 for (i = 0; i < ga_loaded.ga_len; ++i)
17915 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
17916 break;
17917 if (!reload && i < ga_loaded.ga_len)
17918 ret = FALSE; /* was loaded already */
17919 else
17920 {
17921 /* Remember the name if it wasn't loaded already. */
17922 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
17923 {
17924 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
17925 tofree = NULL;
17926 }
17927
17928 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000017929 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000017930 ret = TRUE;
17931 }
17932
17933 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017934 return ret;
17935}
17936
17937/*
17938 * Return the autoload script name for a function or variable name.
17939 * Returns NULL when out of memory.
17940 */
17941 static char_u *
17942autoload_name(name)
17943 char_u *name;
17944{
17945 char_u *p;
17946 char_u *scriptname;
17947
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017948 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017949 scriptname = alloc((unsigned)(STRLEN(name) + 14));
17950 if (scriptname == NULL)
17951 return FALSE;
17952 STRCPY(scriptname, "autoload/");
17953 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017954 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017955 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017956 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017957 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017958 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017959}
17960
Bram Moolenaar071d4272004-06-13 20:20:40 +000017961#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
17962
17963/*
17964 * Function given to ExpandGeneric() to obtain the list of user defined
17965 * function names.
17966 */
17967 char_u *
17968get_user_func_name(xp, idx)
17969 expand_T *xp;
17970 int idx;
17971{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017972 static long_u done;
17973 static hashitem_T *hi;
17974 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975
17976 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017977 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017978 done = 0;
17979 hi = func_hashtab.ht_array;
17980 }
17981 if (done < func_hashtab.ht_used)
17982 {
17983 if (done++ > 0)
17984 ++hi;
17985 while (HASHITEM_EMPTY(hi))
17986 ++hi;
17987 fp = HI2UF(hi);
17988
17989 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
17990 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991
17992 cat_func_name(IObuff, fp);
17993 if (xp->xp_context != EXPAND_USER_FUNC)
17994 {
17995 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017996 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997 STRCAT(IObuff, ")");
17998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017999 return IObuff;
18000 }
18001 return NULL;
18002}
18003
18004#endif /* FEAT_CMDL_COMPL */
18005
18006/*
18007 * Copy the function name of "fp" to buffer "buf".
18008 * "buf" must be able to hold the function name plus three bytes.
18009 * Takes care of script-local function names.
18010 */
18011 static void
18012cat_func_name(buf, fp)
18013 char_u *buf;
18014 ufunc_T *fp;
18015{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018016 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018017 {
18018 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018019 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020 }
18021 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018022 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023}
18024
18025/*
18026 * ":delfunction {name}"
18027 */
18028 void
18029ex_delfunction(eap)
18030 exarg_T *eap;
18031{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018032 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018033 char_u *p;
18034 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000018035 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036
18037 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018038 name = trans_function_name(&p, eap->skip, 0, &fudi);
18039 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018040 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018041 {
18042 if (fudi.fd_dict != NULL && !eap->skip)
18043 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046 if (!ends_excmd(*skipwhite(p)))
18047 {
18048 vim_free(name);
18049 EMSG(_(e_trailing));
18050 return;
18051 }
18052 eap->nextcmd = check_nextcmd(p);
18053 if (eap->nextcmd != NULL)
18054 *p = NUL;
18055
18056 if (!eap->skip)
18057 fp = find_func(name);
18058 vim_free(name);
18059
18060 if (!eap->skip)
18061 {
18062 if (fp == NULL)
18063 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018064 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018065 return;
18066 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018067 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018068 {
18069 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
18070 return;
18071 }
18072
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018073 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018075 /* Delete the dict item that refers to the function, it will
18076 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000018077 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018078 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018079 else
18080 func_free(fp);
18081 }
18082}
18083
18084/*
18085 * Free a function and remove it from the list of functions.
18086 */
18087 static void
18088func_free(fp)
18089 ufunc_T *fp;
18090{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018091 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018092
18093 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018094 ga_clear_strings(&(fp->uf_args));
18095 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000018096#ifdef FEAT_PROFILE
18097 vim_free(fp->uf_tml_count);
18098 vim_free(fp->uf_tml_total);
18099 vim_free(fp->uf_tml_self);
18100#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018101
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018102 /* remove the function from the function hashtable */
18103 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
18104 if (HASHITEM_EMPTY(hi))
18105 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018106 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018107 hash_remove(&func_hashtab, hi);
18108
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018109 vim_free(fp);
18110}
18111
18112/*
18113 * Unreference a Function: decrement the reference count and free it when it
18114 * becomes zero. Only for numbered functions.
18115 */
18116 static void
18117func_unref(name)
18118 char_u *name;
18119{
18120 ufunc_T *fp;
18121
18122 if (name != NULL && isdigit(*name))
18123 {
18124 fp = find_func(name);
18125 if (fp == NULL)
18126 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018127 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018128 {
18129 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018130 * when "uf_calls" becomes zero. */
18131 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000018132 func_free(fp);
18133 }
18134 }
18135}
18136
18137/*
18138 * Count a reference to a Function.
18139 */
18140 static void
18141func_ref(name)
18142 char_u *name;
18143{
18144 ufunc_T *fp;
18145
18146 if (name != NULL && isdigit(*name))
18147 {
18148 fp = find_func(name);
18149 if (fp == NULL)
18150 EMSG2(_(e_intern2), "func_ref()");
18151 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018152 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153 }
18154}
18155
18156/*
18157 * Call a user function.
18158 */
18159 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000018160call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161 ufunc_T *fp; /* pointer to function */
18162 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000018163 typval_T *argvars; /* arguments */
18164 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018165 linenr_T firstline; /* first line of range */
18166 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000018167 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168{
Bram Moolenaar33570922005-01-25 22:26:29 +000018169 char_u *save_sourcing_name;
18170 linenr_T save_sourcing_lnum;
18171 scid_T save_current_SID;
18172 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000018173 int save_did_emsg;
18174 static int depth = 0;
18175 dictitem_T *v;
18176 int fixvar_idx = 0; /* index in fixvar[] */
18177 int i;
18178 int ai;
18179 char_u numbuf[NUMBUFLEN];
18180 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018181#ifdef FEAT_PROFILE
18182 proftime_T wait_start;
18183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018184
18185 /* If depth of calling is getting too high, don't execute the function */
18186 if (depth >= p_mfd)
18187 {
18188 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018189 rettv->v_type = VAR_NUMBER;
18190 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018191 return;
18192 }
18193 ++depth;
18194
18195 line_breakcheck(); /* check for CTRL-C hit */
18196
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018197 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000018198 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018199 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018200 fc.rettv = rettv;
18201 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018202 fc.linenr = 0;
18203 fc.returned = FALSE;
18204 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018205 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018206 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207 fc.dbg_tick = debug_tick;
18208
Bram Moolenaar33570922005-01-25 22:26:29 +000018209 /*
18210 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
18211 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
18212 * each argument variable and saves a lot of time.
18213 */
18214 /*
18215 * Init l: variables.
18216 */
18217 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000018218 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000018219 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018220 /* Set l:self to "selfdict". */
18221 v = &fc.fixvar[fixvar_idx++].var;
18222 STRCPY(v->di_key, "self");
18223 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
18224 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
18225 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018226 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000018227 v->di_tv.vval.v_dict = selfdict;
18228 ++selfdict->dv_refcount;
18229 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000018230
Bram Moolenaar33570922005-01-25 22:26:29 +000018231 /*
18232 * Init a: variables.
18233 * Set a:0 to "argcount".
18234 * Set a:000 to a list with room for the "..." arguments.
18235 */
18236 init_var_dict(&fc.l_avars, &fc.l_avars_var);
18237 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018238 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000018239 v = &fc.fixvar[fixvar_idx++].var;
18240 STRCPY(v->di_key, "000");
18241 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18242 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18243 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018244 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018245 v->di_tv.vval.v_list = &fc.l_varlist;
18246 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
18247 fc.l_varlist.lv_refcount = 99999;
18248
18249 /*
18250 * Set a:firstline to "firstline" and a:lastline to "lastline".
18251 * Set a:name to named arguments.
18252 * Set a:N to the "..." arguments.
18253 */
18254 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
18255 (varnumber_T)firstline);
18256 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
18257 (varnumber_T)lastline);
18258 for (i = 0; i < argcount; ++i)
18259 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018260 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000018261 if (ai < 0)
18262 /* named argument a:name */
18263 name = FUNCARG(fp, i);
18264 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000018265 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018266 /* "..." argument a:1, a:2, etc. */
18267 sprintf((char *)numbuf, "%d", ai + 1);
18268 name = numbuf;
18269 }
18270 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
18271 {
18272 v = &fc.fixvar[fixvar_idx++].var;
18273 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18274 }
18275 else
18276 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018277 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
18278 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000018279 if (v == NULL)
18280 break;
18281 v->di_flags = DI_FLAGS_RO;
18282 }
18283 STRCPY(v->di_key, name);
18284 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
18285
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018286 /* Note: the values are copied directly to avoid alloc/free.
18287 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018288 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018289 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018290
18291 if (ai >= 0 && ai < MAX_FUNC_ARGS)
18292 {
18293 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
18294 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018295 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000018296 }
18297 }
18298
Bram Moolenaar071d4272004-06-13 20:20:40 +000018299 /* Don't redraw while executing the function. */
18300 ++RedrawingDisabled;
18301 save_sourcing_name = sourcing_name;
18302 save_sourcing_lnum = sourcing_lnum;
18303 sourcing_lnum = 1;
18304 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018305 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018306 if (sourcing_name != NULL)
18307 {
18308 if (save_sourcing_name != NULL
18309 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
18310 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
18311 else
18312 STRCPY(sourcing_name, "function ");
18313 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
18314
18315 if (p_verbose >= 12)
18316 {
18317 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018318 verbose_enter_scroll();
18319
Bram Moolenaar555b2802005-05-19 21:08:39 +000018320 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321 if (p_verbose >= 14)
18322 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000018324 char_u numbuf[NUMBUFLEN];
18325 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326
18327 msg_puts((char_u *)"(");
18328 for (i = 0; i < argcount; ++i)
18329 {
18330 if (i > 0)
18331 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018332 if (argvars[i].v_type == VAR_NUMBER)
18333 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018334 else
18335 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018336 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018337 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018338 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018339 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018340 }
18341 }
18342 msg_puts((char_u *)")");
18343 }
18344 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018345
18346 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018347 --no_wait_return;
18348 }
18349 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018350#ifdef FEAT_PROFILE
18351 if (do_profiling)
18352 {
18353 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
18354 func_do_profile(fp);
18355 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018356 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018357 {
18358 ++fp->uf_tm_count;
18359 profile_start(&fp->uf_tm_start);
18360 profile_zero(&fp->uf_tm_children);
18361 }
18362 script_prof_save(&wait_start);
18363 }
18364#endif
18365
Bram Moolenaar071d4272004-06-13 20:20:40 +000018366 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018367 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018368 save_did_emsg = did_emsg;
18369 did_emsg = FALSE;
18370
18371 /* call do_cmdline() to execute the lines */
18372 do_cmdline(NULL, get_func_line, (void *)&fc,
18373 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
18374
18375 --RedrawingDisabled;
18376
18377 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018378 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018380 clear_tv(rettv);
18381 rettv->v_type = VAR_NUMBER;
18382 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383 }
18384
Bram Moolenaar05159a02005-02-26 23:04:13 +000018385#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018386 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000018387 {
18388 profile_end(&fp->uf_tm_start);
18389 profile_sub_wait(&wait_start, &fp->uf_tm_start);
18390 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
18391 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
18392 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018393 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000018394 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018395 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
18396 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000018397 }
18398 }
18399#endif
18400
Bram Moolenaar071d4272004-06-13 20:20:40 +000018401 /* when being verbose, mention the return value */
18402 if (p_verbose >= 12)
18403 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018404 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018405 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018406
Bram Moolenaar071d4272004-06-13 20:20:40 +000018407 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000018408 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018409 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000018410 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
18411 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018412 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000018413 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000018414 char_u buf[MSG_BUF_LEN];
18415 char_u numbuf[NUMBUFLEN];
18416 char_u *tofree;
18417
Bram Moolenaar555b2802005-05-19 21:08:39 +000018418 /* The value may be very long. Skip the middle part, so that we
18419 * have some idea how it starts and ends. smsg() would always
18420 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000018421 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000018422 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000018423 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000018424 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018425 }
18426 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018427
18428 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018429 --no_wait_return;
18430 }
18431
18432 vim_free(sourcing_name);
18433 sourcing_name = save_sourcing_name;
18434 sourcing_lnum = save_sourcing_lnum;
18435 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018436#ifdef FEAT_PROFILE
18437 if (do_profiling)
18438 script_prof_restore(&wait_start);
18439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440
18441 if (p_verbose >= 12 && sourcing_name != NULL)
18442 {
18443 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018444 verbose_enter_scroll();
18445
Bram Moolenaar555b2802005-05-19 21:08:39 +000018446 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018448
18449 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450 --no_wait_return;
18451 }
18452
18453 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018454 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018455
Bram Moolenaar33570922005-01-25 22:26:29 +000018456 /* The a: variables typevals were not alloced, only free the allocated
18457 * variables. */
18458 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18459
18460 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018461 --depth;
18462}
18463
18464/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018465 * Add a number variable "name" to dict "dp" with value "nr".
18466 */
18467 static void
18468add_nr_var(dp, v, name, nr)
18469 dict_T *dp;
18470 dictitem_T *v;
18471 char *name;
18472 varnumber_T nr;
18473{
18474 STRCPY(v->di_key, name);
18475 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18476 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18477 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018478 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018479 v->di_tv.vval.v_number = nr;
18480}
18481
18482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018483 * ":return [expr]"
18484 */
18485 void
18486ex_return(eap)
18487 exarg_T *eap;
18488{
18489 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018490 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018491 int returning = FALSE;
18492
18493 if (current_funccal == NULL)
18494 {
18495 EMSG(_("E133: :return not inside a function"));
18496 return;
18497 }
18498
18499 if (eap->skip)
18500 ++emsg_skip;
18501
18502 eap->nextcmd = NULL;
18503 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018504 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018505 {
18506 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018507 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018508 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018509 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018510 }
18511 /* It's safer to return also on error. */
18512 else if (!eap->skip)
18513 {
18514 /*
18515 * Return unless the expression evaluation has been cancelled due to an
18516 * aborting error, an interrupt, or an exception.
18517 */
18518 if (!aborting())
18519 returning = do_return(eap, FALSE, TRUE, NULL);
18520 }
18521
18522 /* When skipping or the return gets pending, advance to the next command
18523 * in this line (!returning). Otherwise, ignore the rest of the line.
18524 * Following lines will be ignored by get_func_line(). */
18525 if (returning)
18526 eap->nextcmd = NULL;
18527 else if (eap->nextcmd == NULL) /* no argument */
18528 eap->nextcmd = check_nextcmd(arg);
18529
18530 if (eap->skip)
18531 --emsg_skip;
18532}
18533
18534/*
18535 * Return from a function. Possibly makes the return pending. Also called
18536 * for a pending return at the ":endtry" or after returning from an extra
18537 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018538 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018539 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018540 * FALSE when the return gets pending.
18541 */
18542 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018543do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544 exarg_T *eap;
18545 int reanimate;
18546 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018547 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018548{
18549 int idx;
18550 struct condstack *cstack = eap->cstack;
18551
18552 if (reanimate)
18553 /* Undo the return. */
18554 current_funccal->returned = FALSE;
18555
18556 /*
18557 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18558 * not in its finally clause (which then is to be executed next) is found.
18559 * In this case, make the ":return" pending for execution at the ":endtry".
18560 * Otherwise, return normally.
18561 */
18562 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18563 if (idx >= 0)
18564 {
18565 cstack->cs_pending[idx] = CSTP_RETURN;
18566
18567 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018568 /* A pending return again gets pending. "rettv" points to an
18569 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018571 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572 else
18573 {
18574 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018575 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018577 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018579 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580 {
18581 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018582 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018583 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584 else
18585 EMSG(_(e_outofmem));
18586 }
18587 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018588 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018589
18590 if (reanimate)
18591 {
18592 /* The pending return value could be overwritten by a ":return"
18593 * without argument in a finally clause; reset the default
18594 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018595 current_funccal->rettv->v_type = VAR_NUMBER;
18596 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018597 }
18598 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018599 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018600 }
18601 else
18602 {
18603 current_funccal->returned = TRUE;
18604
18605 /* If the return is carried out now, store the return value. For
18606 * a return immediately after reanimation, the value is already
18607 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018608 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018610 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018611 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018613 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018614 }
18615 }
18616
18617 return idx < 0;
18618}
18619
18620/*
18621 * Free the variable with a pending return value.
18622 */
18623 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018624discard_pending_return(rettv)
18625 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626{
Bram Moolenaar33570922005-01-25 22:26:29 +000018627 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628}
18629
18630/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018631 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632 * is an allocated string. Used by report_pending() for verbose messages.
18633 */
18634 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018635get_return_cmd(rettv)
18636 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018638 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018639 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018640 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018642 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018643 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018644 if (s == NULL)
18645 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018646
18647 STRCPY(IObuff, ":return ");
18648 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18649 if (STRLEN(s) + 8 >= IOSIZE)
18650 STRCPY(IObuff + IOSIZE - 4, "...");
18651 vim_free(tofree);
18652 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653}
18654
18655/*
18656 * Get next function line.
18657 * Called by do_cmdline() to get the next line.
18658 * Returns allocated string, or NULL for end of function.
18659 */
18660/* ARGSUSED */
18661 char_u *
18662get_func_line(c, cookie, indent)
18663 int c; /* not used */
18664 void *cookie;
18665 int indent; /* not used */
18666{
Bram Moolenaar33570922005-01-25 22:26:29 +000018667 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018668 ufunc_T *fp = fcp->func;
18669 char_u *retval;
18670 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671
18672 /* If breakpoints have been added/deleted need to check for it. */
18673 if (fcp->dbg_tick != debug_tick)
18674 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018675 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018676 sourcing_lnum);
18677 fcp->dbg_tick = debug_tick;
18678 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018679#ifdef FEAT_PROFILE
18680 if (do_profiling)
18681 func_line_end(cookie);
18682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683
Bram Moolenaar05159a02005-02-26 23:04:13 +000018684 gap = &fp->uf_lines;
18685 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686 retval = NULL;
18687 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18688 retval = NULL;
18689 else
18690 {
18691 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18692 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018693#ifdef FEAT_PROFILE
18694 if (do_profiling)
18695 func_line_start(cookie);
18696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018697 }
18698
18699 /* Did we encounter a breakpoint? */
18700 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18701 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018702 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018704 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018705 sourcing_lnum);
18706 fcp->dbg_tick = debug_tick;
18707 }
18708
18709 return retval;
18710}
18711
Bram Moolenaar05159a02005-02-26 23:04:13 +000018712#if defined(FEAT_PROFILE) || defined(PROTO)
18713/*
18714 * Called when starting to read a function line.
18715 * "sourcing_lnum" must be correct!
18716 * When skipping lines it may not actually be executed, but we won't find out
18717 * until later and we need to store the time now.
18718 */
18719 void
18720func_line_start(cookie)
18721 void *cookie;
18722{
18723 funccall_T *fcp = (funccall_T *)cookie;
18724 ufunc_T *fp = fcp->func;
18725
18726 if (fp->uf_profiling && sourcing_lnum >= 1
18727 && sourcing_lnum <= fp->uf_lines.ga_len)
18728 {
18729 fp->uf_tml_idx = sourcing_lnum - 1;
18730 fp->uf_tml_execed = FALSE;
18731 profile_start(&fp->uf_tml_start);
18732 profile_zero(&fp->uf_tml_children);
18733 profile_get_wait(&fp->uf_tml_wait);
18734 }
18735}
18736
18737/*
18738 * Called when actually executing a function line.
18739 */
18740 void
18741func_line_exec(cookie)
18742 void *cookie;
18743{
18744 funccall_T *fcp = (funccall_T *)cookie;
18745 ufunc_T *fp = fcp->func;
18746
18747 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18748 fp->uf_tml_execed = TRUE;
18749}
18750
18751/*
18752 * Called when done with a function line.
18753 */
18754 void
18755func_line_end(cookie)
18756 void *cookie;
18757{
18758 funccall_T *fcp = (funccall_T *)cookie;
18759 ufunc_T *fp = fcp->func;
18760
18761 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18762 {
18763 if (fp->uf_tml_execed)
18764 {
18765 ++fp->uf_tml_count[fp->uf_tml_idx];
18766 profile_end(&fp->uf_tml_start);
18767 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18768 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18769 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18770 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18771 }
18772 fp->uf_tml_idx = -1;
18773 }
18774}
18775#endif
18776
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777/*
18778 * Return TRUE if the currently active function should be ended, because a
18779 * return was encountered or an error occured. Used inside a ":while".
18780 */
18781 int
18782func_has_ended(cookie)
18783 void *cookie;
18784{
Bram Moolenaar33570922005-01-25 22:26:29 +000018785 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786
18787 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18788 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018789 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790 || fcp->returned);
18791}
18792
18793/*
18794 * return TRUE if cookie indicates a function which "abort"s on errors.
18795 */
18796 int
18797func_has_abort(cookie)
18798 void *cookie;
18799{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018800 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801}
18802
18803#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18804typedef enum
18805{
18806 VAR_FLAVOUR_DEFAULT,
18807 VAR_FLAVOUR_SESSION,
18808 VAR_FLAVOUR_VIMINFO
18809} var_flavour_T;
18810
18811static var_flavour_T var_flavour __ARGS((char_u *varname));
18812
18813 static var_flavour_T
18814var_flavour(varname)
18815 char_u *varname;
18816{
18817 char_u *p = varname;
18818
18819 if (ASCII_ISUPPER(*p))
18820 {
18821 while (*(++p))
18822 if (ASCII_ISLOWER(*p))
18823 return VAR_FLAVOUR_SESSION;
18824 return VAR_FLAVOUR_VIMINFO;
18825 }
18826 else
18827 return VAR_FLAVOUR_DEFAULT;
18828}
18829#endif
18830
18831#if defined(FEAT_VIMINFO) || defined(PROTO)
18832/*
18833 * Restore global vars that start with a capital from the viminfo file
18834 */
18835 int
18836read_viminfo_varlist(virp, writing)
18837 vir_T *virp;
18838 int writing;
18839{
18840 char_u *tab;
18841 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018842 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843
18844 if (!writing && (find_viminfo_parameter('!') != NULL))
18845 {
18846 tab = vim_strchr(virp->vir_line + 1, '\t');
18847 if (tab != NULL)
18848 {
18849 *tab++ = '\0'; /* isolate the variable name */
18850 if (*tab == 'S') /* string var */
18851 is_string = TRUE;
18852
18853 tab = vim_strchr(tab, '\t');
18854 if (tab != NULL)
18855 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018856 if (is_string)
18857 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018858 tv.v_type = VAR_STRING;
18859 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018860 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018861 }
18862 else
18863 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018864 tv.v_type = VAR_NUMBER;
18865 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018867 set_var(virp->vir_line + 1, &tv, FALSE);
18868 if (is_string)
18869 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870 }
18871 }
18872 }
18873
18874 return viminfo_readline(virp);
18875}
18876
18877/*
18878 * Write global vars that start with a capital to the viminfo file
18879 */
18880 void
18881write_viminfo_varlist(fp)
18882 FILE *fp;
18883{
Bram Moolenaar33570922005-01-25 22:26:29 +000018884 hashitem_T *hi;
18885 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018886 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018887 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018888 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018889 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018890 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018891
18892 if (find_viminfo_parameter('!') == NULL)
18893 return;
18894
18895 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000018896
Bram Moolenaar33570922005-01-25 22:26:29 +000018897 todo = globvarht.ht_used;
18898 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018900 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018901 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018902 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018903 this_var = HI2DI(hi);
18904 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018905 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018906 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000018907 {
18908 case VAR_STRING: s = "STR"; break;
18909 case VAR_NUMBER: s = "NUM"; break;
18910 default: continue;
18911 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018912 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018913 p = echo_string(&this_var->di_tv, &tofree, numbuf);
18914 if (p != NULL)
18915 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000018916 vim_free(tofree);
18917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918 }
18919 }
18920}
18921#endif
18922
18923#if defined(FEAT_SESSION) || defined(PROTO)
18924 int
18925store_session_globals(fd)
18926 FILE *fd;
18927{
Bram Moolenaar33570922005-01-25 22:26:29 +000018928 hashitem_T *hi;
18929 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018930 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931 char_u *p, *t;
18932
Bram Moolenaar33570922005-01-25 22:26:29 +000018933 todo = globvarht.ht_used;
18934 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018936 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018938 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018939 this_var = HI2DI(hi);
18940 if ((this_var->di_tv.v_type == VAR_NUMBER
18941 || this_var->di_tv.v_type == VAR_STRING)
18942 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018943 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018944 /* Escape special characters with a backslash. Turn a LF and
18945 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018946 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000018947 (char_u *)"\\\"\n\r");
18948 if (p == NULL) /* out of memory */
18949 break;
18950 for (t = p; *t != NUL; ++t)
18951 if (*t == '\n')
18952 *t = 'n';
18953 else if (*t == '\r')
18954 *t = 'r';
18955 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000018956 this_var->di_key,
18957 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18958 : ' ',
18959 p,
18960 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18961 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000018962 || put_eol(fd) == FAIL)
18963 {
18964 vim_free(p);
18965 return FAIL;
18966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018967 vim_free(p);
18968 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018969 }
18970 }
18971 return OK;
18972}
18973#endif
18974
Bram Moolenaar661b1822005-07-28 22:36:45 +000018975/*
18976 * Display script name where an item was last set.
18977 * Should only be invoked when 'verbose' is non-zero.
18978 */
18979 void
18980last_set_msg(scriptID)
18981 scid_T scriptID;
18982{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018983 char_u *p;
18984
Bram Moolenaar661b1822005-07-28 22:36:45 +000018985 if (scriptID != 0)
18986 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000018987 p = home_replace_save(NULL, get_scriptname(scriptID));
18988 if (p != NULL)
18989 {
18990 verbose_enter();
18991 MSG_PUTS(_("\n\tLast set from "));
18992 MSG_PUTS(p);
18993 vim_free(p);
18994 verbose_leave();
18995 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000018996 }
18997}
18998
Bram Moolenaar071d4272004-06-13 20:20:40 +000018999#endif /* FEAT_EVAL */
19000
19001#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
19002
19003
19004#ifdef WIN3264
19005/*
19006 * Functions for ":8" filename modifier: get 8.3 version of a filename.
19007 */
19008static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19009static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
19010static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
19011
19012/*
19013 * Get the short pathname of a file.
19014 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
19015 */
19016 static int
19017get_short_pathname(fnamep, bufp, fnamelen)
19018 char_u **fnamep;
19019 char_u **bufp;
19020 int *fnamelen;
19021{
19022 int l,len;
19023 char_u *newbuf;
19024
19025 len = *fnamelen;
19026
19027 l = GetShortPathName(*fnamep, *fnamep, len);
19028 if (l > len - 1)
19029 {
19030 /* If that doesn't work (not enough space), then save the string
19031 * and try again with a new buffer big enough
19032 */
19033 newbuf = vim_strnsave(*fnamep, l);
19034 if (newbuf == NULL)
19035 return 0;
19036
19037 vim_free(*bufp);
19038 *fnamep = *bufp = newbuf;
19039
19040 l = GetShortPathName(*fnamep,*fnamep,l+1);
19041
19042 /* Really should always succeed, as the buffer is big enough */
19043 }
19044
19045 *fnamelen = l;
19046 return 1;
19047}
19048
19049/*
19050 * Create a short path name. Returns the length of the buffer it needs.
19051 * Doesn't copy over the end of the buffer passed in.
19052 */
19053 static int
19054shortpath_for_invalid_fname(fname, bufp, fnamelen)
19055 char_u **fname;
19056 char_u **bufp;
19057 int *fnamelen;
19058{
19059 char_u *s, *p, *pbuf2, *pbuf3;
19060 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000019061 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019062
19063 /* Make a copy */
19064 len2 = *fnamelen;
19065 pbuf2 = vim_strnsave(*fname, len2);
19066 pbuf3 = NULL;
19067
19068 s = pbuf2 + len2 - 1; /* Find the end */
19069 slen = 1;
19070 plen = len2;
19071
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019072 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073 {
19074 --s;
19075 ++slen;
19076 --plen;
19077 }
19078
19079 do
19080 {
19081 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019082 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083 {
19084 --s;
19085 ++slen;
19086 --plen;
19087 }
19088 if (s <= pbuf2)
19089 break;
19090
19091 /* Remeber the character that is about to be blatted */
19092 ch = *s;
19093 *s = 0; /* get_short_pathname requires a null-terminated string */
19094
19095 /* Try it in situ */
19096 p = pbuf2;
19097 if (!get_short_pathname(&p, &pbuf3, &plen))
19098 {
19099 vim_free(pbuf2);
19100 return -1;
19101 }
19102 *s = ch; /* Preserve the string */
19103 } while (plen == 0);
19104
19105 if (plen > 0)
19106 {
19107 /* Remeber the length of the new string. */
19108 *fnamelen = len = plen + slen;
19109 vim_free(*bufp);
19110 if (len > len2)
19111 {
19112 /* If there's not enough space in the currently allocated string,
19113 * then copy it to a buffer big enough.
19114 */
19115 *fname= *bufp = vim_strnsave(p, len);
19116 if (*fname == NULL)
19117 return -1;
19118 }
19119 else
19120 {
19121 /* Transfer pbuf2 to being the main buffer (it's big enough) */
19122 *fname = *bufp = pbuf2;
19123 if (p != pbuf2)
19124 strncpy(*fname, p, plen);
19125 pbuf2 = NULL;
19126 }
19127 /* Concat the next bit */
19128 strncpy(*fname + plen, s, slen);
19129 (*fname)[len] = '\0';
19130 }
19131 vim_free(pbuf3);
19132 vim_free(pbuf2);
19133 return 0;
19134}
19135
19136/*
19137 * Get a pathname for a partial path.
19138 */
19139 static int
19140shortpath_for_partial(fnamep, bufp, fnamelen)
19141 char_u **fnamep;
19142 char_u **bufp;
19143 int *fnamelen;
19144{
19145 int sepcount, len, tflen;
19146 char_u *p;
19147 char_u *pbuf, *tfname;
19148 int hasTilde;
19149
19150 /* Count up the path seperators from the RHS.. so we know which part
19151 * of the path to return.
19152 */
19153 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019154 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155 if (vim_ispathsep(*p))
19156 ++sepcount;
19157
19158 /* Need full path first (use expand_env() to remove a "~/") */
19159 hasTilde = (**fnamep == '~');
19160 if (hasTilde)
19161 pbuf = tfname = expand_env_save(*fnamep);
19162 else
19163 pbuf = tfname = FullName_save(*fnamep, FALSE);
19164
19165 len = tflen = STRLEN(tfname);
19166
19167 if (!get_short_pathname(&tfname, &pbuf, &len))
19168 return -1;
19169
19170 if (len == 0)
19171 {
19172 /* Don't have a valid filename, so shorten the rest of the
19173 * path if we can. This CAN give us invalid 8.3 filenames, but
19174 * there's not a lot of point in guessing what it might be.
19175 */
19176 len = tflen;
19177 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
19178 return -1;
19179 }
19180
19181 /* Count the paths backward to find the beginning of the desired string. */
19182 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019183 {
19184#ifdef FEAT_MBYTE
19185 if (has_mbyte)
19186 p -= mb_head_off(tfname, p);
19187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188 if (vim_ispathsep(*p))
19189 {
19190 if (sepcount == 0 || (hasTilde && sepcount == 1))
19191 break;
19192 else
19193 sepcount --;
19194 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019196 if (hasTilde)
19197 {
19198 --p;
19199 if (p >= tfname)
19200 *p = '~';
19201 else
19202 return -1;
19203 }
19204 else
19205 ++p;
19206
19207 /* Copy in the string - p indexes into tfname - allocated at pbuf */
19208 vim_free(*bufp);
19209 *fnamelen = (int)STRLEN(p);
19210 *bufp = pbuf;
19211 *fnamep = p;
19212
19213 return 0;
19214}
19215#endif /* WIN3264 */
19216
19217/*
19218 * Adjust a filename, according to a string of modifiers.
19219 * *fnamep must be NUL terminated when called. When returning, the length is
19220 * determined by *fnamelen.
19221 * Returns valid flags.
19222 * When there is an error, *fnamep is set to NULL.
19223 */
19224 int
19225modify_fname(src, usedlen, fnamep, bufp, fnamelen)
19226 char_u *src; /* string with modifiers */
19227 int *usedlen; /* characters after src that are used */
19228 char_u **fnamep; /* file name so far */
19229 char_u **bufp; /* buffer for allocated file name or NULL */
19230 int *fnamelen; /* length of fnamep */
19231{
19232 int valid = 0;
19233 char_u *tail;
19234 char_u *s, *p, *pbuf;
19235 char_u dirname[MAXPATHL];
19236 int c;
19237 int has_fullname = 0;
19238#ifdef WIN3264
19239 int has_shortname = 0;
19240#endif
19241
19242repeat:
19243 /* ":p" - full path/file_name */
19244 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
19245 {
19246 has_fullname = 1;
19247
19248 valid |= VALID_PATH;
19249 *usedlen += 2;
19250
19251 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
19252 if ((*fnamep)[0] == '~'
19253#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
19254 && ((*fnamep)[1] == '/'
19255# ifdef BACKSLASH_IN_FILENAME
19256 || (*fnamep)[1] == '\\'
19257# endif
19258 || (*fnamep)[1] == NUL)
19259
19260#endif
19261 )
19262 {
19263 *fnamep = expand_env_save(*fnamep);
19264 vim_free(*bufp); /* free any allocated file name */
19265 *bufp = *fnamep;
19266 if (*fnamep == NULL)
19267 return -1;
19268 }
19269
19270 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019271 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272 {
19273 if (vim_ispathsep(*p)
19274 && p[1] == '.'
19275 && (p[2] == NUL
19276 || vim_ispathsep(p[2])
19277 || (p[2] == '.'
19278 && (p[3] == NUL || vim_ispathsep(p[3])))))
19279 break;
19280 }
19281
19282 /* FullName_save() is slow, don't use it when not needed. */
19283 if (*p != NUL || !vim_isAbsName(*fnamep))
19284 {
19285 *fnamep = FullName_save(*fnamep, *p != NUL);
19286 vim_free(*bufp); /* free any allocated file name */
19287 *bufp = *fnamep;
19288 if (*fnamep == NULL)
19289 return -1;
19290 }
19291
19292 /* Append a path separator to a directory. */
19293 if (mch_isdir(*fnamep))
19294 {
19295 /* Make room for one or two extra characters. */
19296 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
19297 vim_free(*bufp); /* free any allocated file name */
19298 *bufp = *fnamep;
19299 if (*fnamep == NULL)
19300 return -1;
19301 add_pathsep(*fnamep);
19302 }
19303 }
19304
19305 /* ":." - path relative to the current directory */
19306 /* ":~" - path relative to the home directory */
19307 /* ":8" - shortname path - postponed till after */
19308 while (src[*usedlen] == ':'
19309 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
19310 {
19311 *usedlen += 2;
19312 if (c == '8')
19313 {
19314#ifdef WIN3264
19315 has_shortname = 1; /* Postpone this. */
19316#endif
19317 continue;
19318 }
19319 pbuf = NULL;
19320 /* Need full path first (use expand_env() to remove a "~/") */
19321 if (!has_fullname)
19322 {
19323 if (c == '.' && **fnamep == '~')
19324 p = pbuf = expand_env_save(*fnamep);
19325 else
19326 p = pbuf = FullName_save(*fnamep, FALSE);
19327 }
19328 else
19329 p = *fnamep;
19330
19331 has_fullname = 0;
19332
19333 if (p != NULL)
19334 {
19335 if (c == '.')
19336 {
19337 mch_dirname(dirname, MAXPATHL);
19338 s = shorten_fname(p, dirname);
19339 if (s != NULL)
19340 {
19341 *fnamep = s;
19342 if (pbuf != NULL)
19343 {
19344 vim_free(*bufp); /* free any allocated file name */
19345 *bufp = pbuf;
19346 pbuf = NULL;
19347 }
19348 }
19349 }
19350 else
19351 {
19352 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
19353 /* Only replace it when it starts with '~' */
19354 if (*dirname == '~')
19355 {
19356 s = vim_strsave(dirname);
19357 if (s != NULL)
19358 {
19359 *fnamep = s;
19360 vim_free(*bufp);
19361 *bufp = s;
19362 }
19363 }
19364 }
19365 vim_free(pbuf);
19366 }
19367 }
19368
19369 tail = gettail(*fnamep);
19370 *fnamelen = (int)STRLEN(*fnamep);
19371
19372 /* ":h" - head, remove "/file_name", can be repeated */
19373 /* Don't remove the first "/" or "c:\" */
19374 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
19375 {
19376 valid |= VALID_HEAD;
19377 *usedlen += 2;
19378 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019379 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380 --tail;
19381 *fnamelen = (int)(tail - *fnamep);
19382#ifdef VMS
19383 if (*fnamelen > 0)
19384 *fnamelen += 1; /* the path separator is part of the path */
19385#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000019386 while (tail > s && !after_pathsep(s, tail))
19387 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019388 }
19389
19390 /* ":8" - shortname */
19391 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
19392 {
19393 *usedlen += 2;
19394#ifdef WIN3264
19395 has_shortname = 1;
19396#endif
19397 }
19398
19399#ifdef WIN3264
19400 /* Check shortname after we have done 'heads' and before we do 'tails'
19401 */
19402 if (has_shortname)
19403 {
19404 pbuf = NULL;
19405 /* Copy the string if it is shortened by :h */
19406 if (*fnamelen < (int)STRLEN(*fnamep))
19407 {
19408 p = vim_strnsave(*fnamep, *fnamelen);
19409 if (p == 0)
19410 return -1;
19411 vim_free(*bufp);
19412 *bufp = *fnamep = p;
19413 }
19414
19415 /* Split into two implementations - makes it easier. First is where
19416 * there isn't a full name already, second is where there is.
19417 */
19418 if (!has_fullname && !vim_isAbsName(*fnamep))
19419 {
19420 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
19421 return -1;
19422 }
19423 else
19424 {
19425 int l;
19426
19427 /* Simple case, already have the full-name
19428 * Nearly always shorter, so try first time. */
19429 l = *fnamelen;
19430 if (!get_short_pathname(fnamep, bufp, &l))
19431 return -1;
19432
19433 if (l == 0)
19434 {
19435 /* Couldn't find the filename.. search the paths.
19436 */
19437 l = *fnamelen;
19438 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
19439 return -1;
19440 }
19441 *fnamelen = l;
19442 }
19443 }
19444#endif /* WIN3264 */
19445
19446 /* ":t" - tail, just the basename */
19447 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
19448 {
19449 *usedlen += 2;
19450 *fnamelen -= (int)(tail - *fnamep);
19451 *fnamep = tail;
19452 }
19453
19454 /* ":e" - extension, can be repeated */
19455 /* ":r" - root, without extension, can be repeated */
19456 while (src[*usedlen] == ':'
19457 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19458 {
19459 /* find a '.' in the tail:
19460 * - for second :e: before the current fname
19461 * - otherwise: The last '.'
19462 */
19463 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19464 s = *fnamep - 2;
19465 else
19466 s = *fnamep + *fnamelen - 1;
19467 for ( ; s > tail; --s)
19468 if (s[0] == '.')
19469 break;
19470 if (src[*usedlen + 1] == 'e') /* :e */
19471 {
19472 if (s > tail)
19473 {
19474 *fnamelen += (int)(*fnamep - (s + 1));
19475 *fnamep = s + 1;
19476#ifdef VMS
19477 /* cut version from the extension */
19478 s = *fnamep + *fnamelen - 1;
19479 for ( ; s > *fnamep; --s)
19480 if (s[0] == ';')
19481 break;
19482 if (s > *fnamep)
19483 *fnamelen = s - *fnamep;
19484#endif
19485 }
19486 else if (*fnamep <= tail)
19487 *fnamelen = 0;
19488 }
19489 else /* :r */
19490 {
19491 if (s > tail) /* remove one extension */
19492 *fnamelen = (int)(s - *fnamep);
19493 }
19494 *usedlen += 2;
19495 }
19496
19497 /* ":s?pat?foo?" - substitute */
19498 /* ":gs?pat?foo?" - global substitute */
19499 if (src[*usedlen] == ':'
19500 && (src[*usedlen + 1] == 's'
19501 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19502 {
19503 char_u *str;
19504 char_u *pat;
19505 char_u *sub;
19506 int sep;
19507 char_u *flags;
19508 int didit = FALSE;
19509
19510 flags = (char_u *)"";
19511 s = src + *usedlen + 2;
19512 if (src[*usedlen + 1] == 'g')
19513 {
19514 flags = (char_u *)"g";
19515 ++s;
19516 }
19517
19518 sep = *s++;
19519 if (sep)
19520 {
19521 /* find end of pattern */
19522 p = vim_strchr(s, sep);
19523 if (p != NULL)
19524 {
19525 pat = vim_strnsave(s, (int)(p - s));
19526 if (pat != NULL)
19527 {
19528 s = p + 1;
19529 /* find end of substitution */
19530 p = vim_strchr(s, sep);
19531 if (p != NULL)
19532 {
19533 sub = vim_strnsave(s, (int)(p - s));
19534 str = vim_strnsave(*fnamep, *fnamelen);
19535 if (sub != NULL && str != NULL)
19536 {
19537 *usedlen = (int)(p + 1 - src);
19538 s = do_string_sub(str, pat, sub, flags);
19539 if (s != NULL)
19540 {
19541 *fnamep = s;
19542 *fnamelen = (int)STRLEN(s);
19543 vim_free(*bufp);
19544 *bufp = s;
19545 didit = TRUE;
19546 }
19547 }
19548 vim_free(sub);
19549 vim_free(str);
19550 }
19551 vim_free(pat);
19552 }
19553 }
19554 /* after using ":s", repeat all the modifiers */
19555 if (didit)
19556 goto repeat;
19557 }
19558 }
19559
19560 return valid;
19561}
19562
19563/*
19564 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19565 * "flags" can be "g" to do a global substitute.
19566 * Returns an allocated string, NULL for error.
19567 */
19568 char_u *
19569do_string_sub(str, pat, sub, flags)
19570 char_u *str;
19571 char_u *pat;
19572 char_u *sub;
19573 char_u *flags;
19574{
19575 int sublen;
19576 regmatch_T regmatch;
19577 int i;
19578 int do_all;
19579 char_u *tail;
19580 garray_T ga;
19581 char_u *ret;
19582 char_u *save_cpo;
19583
19584 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19585 save_cpo = p_cpo;
19586 p_cpo = (char_u *)"";
19587
19588 ga_init2(&ga, 1, 200);
19589
19590 do_all = (flags[0] == 'g');
19591
19592 regmatch.rm_ic = p_ic;
19593 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19594 if (regmatch.regprog != NULL)
19595 {
19596 tail = str;
19597 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19598 {
19599 /*
19600 * Get some space for a temporary buffer to do the substitution
19601 * into. It will contain:
19602 * - The text up to where the match is.
19603 * - The substituted text.
19604 * - The text after the match.
19605 */
19606 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19607 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19608 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19609 {
19610 ga_clear(&ga);
19611 break;
19612 }
19613
19614 /* copy the text up to where the match is */
19615 i = (int)(regmatch.startp[0] - tail);
19616 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19617 /* add the substituted text */
19618 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19619 + ga.ga_len + i, TRUE, TRUE, FALSE);
19620 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 /* avoid getting stuck on a match with an empty string */
19622 if (tail == regmatch.endp[0])
19623 {
19624 if (*tail == NUL)
19625 break;
19626 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19627 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628 }
19629 else
19630 {
19631 tail = regmatch.endp[0];
19632 if (*tail == NUL)
19633 break;
19634 }
19635 if (!do_all)
19636 break;
19637 }
19638
19639 if (ga.ga_data != NULL)
19640 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19641
19642 vim_free(regmatch.regprog);
19643 }
19644
19645 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19646 ga_clear(&ga);
19647 p_cpo = save_cpo;
19648
19649 return ret;
19650}
19651
19652#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */