blob: a6190364a949b910edcd9d0696cc775351eb0d3c [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));
412static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
413static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
414static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000415static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000416static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
417static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000418static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000419static void set_ref_in_ht __ARGS((hashtab_T *ht, int copyID));
420static void set_ref_in_list __ARGS((list_T *l, int copyID));
421static void set_ref_in_item __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000422static void dict_unref __ARGS((dict_T *d));
423static void dict_free __ARGS((dict_T *d));
424static dictitem_T *dictitem_alloc __ARGS((char_u *key));
425static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
426static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
427static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000428static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000429static int dict_add __ARGS((dict_T *d, dictitem_T *item));
430static long dict_len __ARGS((dict_T *d));
431static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
432static char_u *dict2string __ARGS((typval_T *tv));
433static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar33570922005-01-25 22:26:29 +0000434static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
435static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
436static char_u *string_quote __ARGS((char_u *str, int function));
437static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
438static int find_internal_func __ARGS((char_u *name));
439static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
440static 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));
441static 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 +0000442static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000443
444static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
457static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
458static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
468static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
483static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000494static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000495static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000508static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000509static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000536static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
539static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
540static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000553static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000554static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
555static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
556static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000557#ifdef vim_mkdir
558static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
559#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000560static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
561static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000565static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000583static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000584static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000588static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000589static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
590static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000591static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
592#ifdef HAVE_STRFTIME
593static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
594#endif
595static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
603static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
604static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
605static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
606static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000607static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
614static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
615static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
616static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
617static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
622static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000623static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000624
Bram Moolenaar33570922005-01-25 22:26:29 +0000625static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
626static int get_env_len __ARGS((char_u **arg));
627static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000628static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000629static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
630#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
631#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
632 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000633static 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 +0000634static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000635static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000636static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
637static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000638static typval_T *alloc_tv __ARGS((void));
639static typval_T *alloc_string_tv __ARGS((char_u *string));
640static void free_tv __ARGS((typval_T *varp));
641static void clear_tv __ARGS((typval_T *varp));
642static void init_tv __ARGS((typval_T *varp));
643static long get_tv_number __ARGS((typval_T *varp));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000644static long get_tv_number_chk __ARGS((typval_T *varp, int *denote));
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
646static char_u *get_tv_string __ARGS((typval_T *varp));
647static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000648static char_u *get_tv_string_chk __ARGS((typval_T *varp));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000649static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000650static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000651static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000652static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
653static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
654static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
655static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
656static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
657static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
658static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000659static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000660static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000661static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000662static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
663static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
664static int eval_fname_script __ARGS((char_u *p));
665static int eval_fname_sid __ARGS((char_u *p));
666static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000667static ufunc_T *find_func __ARGS((char_u *name));
668static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000669static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000670#ifdef FEAT_PROFILE
671static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000672static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
673static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
674static int
675# ifdef __BORLANDC__
676 _RTLENTRYF
677# endif
678 prof_total_cmp __ARGS((const void *s1, const void *s2));
679static int
680# ifdef __BORLANDC__
681 _RTLENTRYF
682# endif
683 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000684#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000685static int script_autoload __ARGS((char_u *name));
686static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000687static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000688static void func_free __ARGS((ufunc_T *fp));
689static void func_unref __ARGS((char_u *name));
690static void func_ref __ARGS((char_u *name));
691static 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));
692static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
693
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000694/* Character used as separated in autoload function/variable names. */
695#define AUTOLOAD_CHAR '#'
696
Bram Moolenaar33570922005-01-25 22:26:29 +0000697/*
698 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000699 */
700 void
701eval_init()
702{
Bram Moolenaar33570922005-01-25 22:26:29 +0000703 int i;
704 struct vimvar *p;
705
706 init_var_dict(&globvardict, &globvars_var);
707 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000708 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000709 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000710
711 for (i = 0; i < VV_LEN; ++i)
712 {
713 p = &vimvars[i];
714 STRCPY(p->vv_di.di_key, p->vv_name);
715 if (p->vv_flags & VV_RO)
716 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
717 else if (p->vv_flags & VV_RO_SBX)
718 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
719 else
720 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000721
722 /* add to v: scope dict, unless the value is not always available */
723 if (p->vv_type != VAR_UNKNOWN)
724 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000725 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000726 /* add to compat scope dict */
727 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000728 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000729}
730
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000731#if defined(EXITFREE) || defined(PROTO)
732 void
733eval_clear()
734{
735 int i;
736 struct vimvar *p;
737
738 for (i = 0; i < VV_LEN; ++i)
739 {
740 p = &vimvars[i];
741 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000742 {
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000743 vim_free(p->vv_di.di_tv.vval.v_string);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000744 p->vv_di.di_tv.vval.v_string = NULL;
745 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000746 }
747 hash_clear(&vimvarht);
748 hash_clear(&compat_hashtab);
749
750 /* script-local variables */
751 for (i = 1; i <= ga_scripts.ga_len; ++i)
752 vars_clear(&SCRIPT_VARS(i));
753 ga_clear(&ga_scripts);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000754 free_scriptnames();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000755
756 /* global variables */
757 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000758
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000759 /* functions */
Bram Moolenaard9fba312005-06-26 22:34:35 +0000760 free_all_functions();
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000761 hash_clear(&func_hashtab);
762
763 /* unreferenced lists and dicts */
764 (void)garbage_collect();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000765}
766#endif
767
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 * Return the name of the executed function.
770 */
771 char_u *
772func_name(cookie)
773 void *cookie;
774{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000775 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776}
777
778/*
779 * Return the address holding the next breakpoint line for a funccall cookie.
780 */
781 linenr_T *
782func_breakpoint(cookie)
783 void *cookie;
784{
Bram Moolenaar33570922005-01-25 22:26:29 +0000785 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786}
787
788/*
789 * Return the address holding the debug tick for a funccall cookie.
790 */
791 int *
792func_dbg_tick(cookie)
793 void *cookie;
794{
Bram Moolenaar33570922005-01-25 22:26:29 +0000795 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796}
797
798/*
799 * Return the nesting level for a funccall cookie.
800 */
801 int
802func_level(cookie)
803 void *cookie;
804{
Bram Moolenaar33570922005-01-25 22:26:29 +0000805 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806}
807
808/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000809funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810
811/*
812 * Return TRUE when a function was ended by a ":return" command.
813 */
814 int
815current_func_returned()
816{
817 return current_funccal->returned;
818}
819
820
821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 * Set an internal variable to a string value. Creates the variable if it does
823 * not already exist.
824 */
825 void
826set_internal_string_var(name, value)
827 char_u *name;
828 char_u *value;
829{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000830 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000831 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
833 val = vim_strsave(value);
834 if (val != NULL)
835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000836 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000837 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000839 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000840 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 }
842 }
843}
844
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000845static lval_T *redir_lval = NULL;
846static char_u *redir_endp = NULL;
847static char_u *redir_varname = NULL;
848
849/*
850 * Start recording command output to a variable
851 * Returns OK if successfully completed the setup. FAIL otherwise.
852 */
853 int
854var_redir_start(name, append)
855 char_u *name;
856 int append; /* append to an existing variable */
857{
858 int save_emsg;
859 int err;
860 typval_T tv;
861
862 /* Make sure a valid variable name is specified */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000863 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000864 {
865 EMSG(_(e_invarg));
866 return FAIL;
867 }
868
869 redir_varname = vim_strsave(name);
870 if (redir_varname == NULL)
871 return FAIL;
872
873 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
874 if (redir_lval == NULL)
875 {
876 var_redir_stop();
877 return FAIL;
878 }
879
880 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000881 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
882 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000883 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
884 {
885 if (redir_endp != NULL && *redir_endp != NUL)
886 /* Trailing characters are present after the variable name */
887 EMSG(_(e_trailing));
888 else
889 EMSG(_(e_invarg));
890 var_redir_stop();
891 return FAIL;
892 }
893
894 /* check if we can write to the variable: set it to or append an empty
895 * string */
896 save_emsg = did_emsg;
897 did_emsg = FALSE;
898 tv.v_type = VAR_STRING;
899 tv.vval.v_string = (char_u *)"";
900 if (append)
901 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
902 else
903 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
904 err = did_emsg;
905 did_emsg += save_emsg;
906 if (err)
907 {
908 var_redir_stop();
909 return FAIL;
910 }
911 if (redir_lval->ll_newkey != NULL)
912 {
913 /* Dictionary item was created, don't do it again. */
914 vim_free(redir_lval->ll_newkey);
915 redir_lval->ll_newkey = NULL;
916 }
917
918 return OK;
919}
920
921/*
922 * Append "value[len]" to the variable set by var_redir_start().
923 */
924 void
925var_redir_str(value, len)
926 char_u *value;
927 int len;
928{
929 char_u *val;
930 typval_T tv;
931 int save_emsg;
932 int err;
933
934 if (redir_lval == NULL)
935 return;
936
937 if (len == -1)
938 /* Append the entire string */
939 val = vim_strsave(value);
940 else
941 /* Append only the specified number of characters */
942 val = vim_strnsave(value, len);
943 if (val == NULL)
944 return;
945
946 tv.v_type = VAR_STRING;
947 tv.vval.v_string = val;
948
949 save_emsg = did_emsg;
950 did_emsg = FALSE;
951 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
952 err = did_emsg;
953 did_emsg += save_emsg;
954 if (err)
955 var_redir_stop();
956
957 vim_free(tv.vval.v_string);
958}
959
960/*
961 * Stop redirecting command output to a variable.
962 */
963 void
964var_redir_stop()
965{
966 if (redir_lval != NULL)
967 {
968 clear_lval(redir_lval);
969 vim_free(redir_lval);
970 redir_lval = NULL;
971 }
972 vim_free(redir_varname);
973 redir_varname = NULL;
974}
975
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976# if defined(FEAT_MBYTE) || defined(PROTO)
977 int
978eval_charconvert(enc_from, enc_to, fname_from, fname_to)
979 char_u *enc_from;
980 char_u *enc_to;
981 char_u *fname_from;
982 char_u *fname_to;
983{
984 int err = FALSE;
985
986 set_vim_var_string(VV_CC_FROM, enc_from, -1);
987 set_vim_var_string(VV_CC_TO, enc_to, -1);
988 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
989 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
990 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
991 err = TRUE;
992 set_vim_var_string(VV_CC_FROM, NULL, -1);
993 set_vim_var_string(VV_CC_TO, NULL, -1);
994 set_vim_var_string(VV_FNAME_IN, NULL, -1);
995 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
996
997 if (err)
998 return FAIL;
999 return OK;
1000}
1001# endif
1002
1003# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1004 int
1005eval_printexpr(fname, args)
1006 char_u *fname;
1007 char_u *args;
1008{
1009 int err = FALSE;
1010
1011 set_vim_var_string(VV_FNAME_IN, fname, -1);
1012 set_vim_var_string(VV_CMDARG, args, -1);
1013 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1014 err = TRUE;
1015 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1016 set_vim_var_string(VV_CMDARG, NULL, -1);
1017
1018 if (err)
1019 {
1020 mch_remove(fname);
1021 return FAIL;
1022 }
1023 return OK;
1024}
1025# endif
1026
1027# if defined(FEAT_DIFF) || defined(PROTO)
1028 void
1029eval_diff(origfile, newfile, outfile)
1030 char_u *origfile;
1031 char_u *newfile;
1032 char_u *outfile;
1033{
1034 int err = FALSE;
1035
1036 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1037 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1038 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1039 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1040 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1041 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1042 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1043}
1044
1045 void
1046eval_patch(origfile, difffile, outfile)
1047 char_u *origfile;
1048 char_u *difffile;
1049 char_u *outfile;
1050{
1051 int err;
1052
1053 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1054 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1055 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1056 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1057 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1058 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1059 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1060}
1061# endif
1062
1063/*
1064 * Top level evaluation function, returning a boolean.
1065 * Sets "error" to TRUE if there was an error.
1066 * Return TRUE or FALSE.
1067 */
1068 int
1069eval_to_bool(arg, error, nextcmd, skip)
1070 char_u *arg;
1071 int *error;
1072 char_u **nextcmd;
1073 int skip; /* only parse, don't execute */
1074{
Bram Moolenaar33570922005-01-25 22:26:29 +00001075 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 int retval = FALSE;
1077
1078 if (skip)
1079 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001080 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 else
1083 {
1084 *error = FALSE;
1085 if (!skip)
1086 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001087 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001088 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090 }
1091 if (skip)
1092 --emsg_skip;
1093
1094 return retval;
1095}
1096
1097/*
1098 * Top level evaluation function, returning a string. If "skip" is TRUE,
1099 * only parsing to "nextcmd" is done, without reporting errors. Return
1100 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1101 */
1102 char_u *
1103eval_to_string_skip(arg, nextcmd, skip)
1104 char_u *arg;
1105 char_u **nextcmd;
1106 int skip; /* only parse, don't execute */
1107{
Bram Moolenaar33570922005-01-25 22:26:29 +00001108 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 char_u *retval;
1110
1111 if (skip)
1112 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001113 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 retval = NULL;
1115 else
1116 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001117 retval = vim_strsave(get_tv_string(&tv));
1118 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 }
1120 if (skip)
1121 --emsg_skip;
1122
1123 return retval;
1124}
1125
1126/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001127 * Skip over an expression at "*pp".
1128 * Return FAIL for an error, OK otherwise.
1129 */
1130 int
1131skip_expr(pp)
1132 char_u **pp;
1133{
Bram Moolenaar33570922005-01-25 22:26:29 +00001134 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001135
1136 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001137 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001138}
1139
1140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 * Top level evaluation function, returning a string.
1142 * Return pointer to allocated memory, or NULL for failure.
1143 */
1144 char_u *
1145eval_to_string(arg, nextcmd)
1146 char_u *arg;
1147 char_u **nextcmd;
1148{
Bram Moolenaar33570922005-01-25 22:26:29 +00001149 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 char_u *retval;
1151
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001152 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 retval = NULL;
1154 else
1155 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001156 retval = vim_strsave(get_tv_string(&tv));
1157 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 }
1159
1160 return retval;
1161}
1162
1163/*
1164 * Call eval_to_string() with "sandbox" set and not using local variables.
1165 */
1166 char_u *
1167eval_to_string_safe(arg, nextcmd)
1168 char_u *arg;
1169 char_u **nextcmd;
1170{
1171 char_u *retval;
1172 void *save_funccalp;
1173
1174 save_funccalp = save_funccal();
1175 ++sandbox;
1176 retval = eval_to_string(arg, nextcmd);
1177 --sandbox;
1178 restore_funccal(save_funccalp);
1179 return retval;
1180}
1181
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182/*
1183 * Top level evaluation function, returning a number.
1184 * Evaluates "expr" silently.
1185 * Returns -1 for an error.
1186 */
1187 int
1188eval_to_number(expr)
1189 char_u *expr;
1190{
Bram Moolenaar33570922005-01-25 22:26:29 +00001191 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001193 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194
1195 ++emsg_off;
1196
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001197 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 retval = -1;
1199 else
1200 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001201 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001202 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 }
1204 --emsg_off;
1205
1206 return retval;
1207}
1208
Bram Moolenaara40058a2005-07-11 22:42:07 +00001209/*
1210 * Prepare v: variable "idx" to be used.
1211 * Save the current typeval in "save_tv".
1212 * When not used yet add the variable to the v: hashtable.
1213 */
1214 static void
1215prepare_vimvar(idx, save_tv)
1216 int idx;
1217 typval_T *save_tv;
1218{
1219 *save_tv = vimvars[idx].vv_tv;
1220 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1221 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1222}
1223
1224/*
1225 * Restore v: variable "idx" to typeval "save_tv".
1226 * When no longer defined, remove the variable from the v: hashtable.
1227 */
1228 static void
1229restore_vimvar(idx, save_tv)
1230 int idx;
1231 typval_T *save_tv;
1232{
1233 hashitem_T *hi;
1234
1235 clear_tv(&vimvars[idx].vv_tv);
1236 vimvars[idx].vv_tv = *save_tv;
1237 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1238 {
1239 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1240 if (HASHITEM_EMPTY(hi))
1241 EMSG2(_(e_intern2), "restore_vimvar()");
1242 else
1243 hash_remove(&vimvarht, hi);
1244 }
1245}
1246
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001247#if defined(FEAT_SYN_HL) || defined(PROTO)
1248/*
1249 * Evaluate an expression to a list with suggestions.
1250 * For the "expr:" part of 'spellsuggest'.
1251 */
1252 list_T *
1253eval_spell_expr(badword, expr)
1254 char_u *badword;
1255 char_u *expr;
1256{
1257 typval_T save_val;
1258 typval_T rettv;
1259 list_T *list = NULL;
1260 char_u *p = skipwhite(expr);
1261
1262 /* Set "v:val" to the bad word. */
1263 prepare_vimvar(VV_VAL, &save_val);
1264 vimvars[VV_VAL].vv_type = VAR_STRING;
1265 vimvars[VV_VAL].vv_str = badword;
1266 if (p_verbose == 0)
1267 ++emsg_off;
1268
1269 if (eval1(&p, &rettv, TRUE) == OK)
1270 {
1271 if (rettv.v_type != VAR_LIST)
1272 clear_tv(&rettv);
1273 else
1274 list = rettv.vval.v_list;
1275 }
1276
1277 if (p_verbose == 0)
1278 --emsg_off;
1279 vimvars[VV_VAL].vv_str = NULL;
1280 restore_vimvar(VV_VAL, &save_val);
1281
1282 return list;
1283}
1284
1285/*
1286 * "list" is supposed to contain two items: a word and a number. Return the
1287 * word in "pp" and the number as the return value.
1288 * Return -1 if anything isn't right.
1289 * Used to get the good word and score from the eval_spell_expr() result.
1290 */
1291 int
1292get_spellword(list, pp)
1293 list_T *list;
1294 char_u **pp;
1295{
1296 listitem_T *li;
1297
1298 li = list->lv_first;
1299 if (li == NULL)
1300 return -1;
1301 *pp = get_tv_string(&li->li_tv);
1302
1303 li = li->li_next;
1304 if (li == NULL)
1305 return -1;
1306 return get_tv_number(&li->li_tv);
1307}
1308#endif
1309
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
1311/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001312 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 * Uses argv[argc] for the function arguments.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001314 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001316 static int
1317call_vim_function(func, argc, argv, safe, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 char_u *func;
1319 int argc;
1320 char_u **argv;
1321 int safe; /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323{
Bram Moolenaar33570922005-01-25 22:26:29 +00001324 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 long n;
1326 int len;
1327 int i;
1328 int doesrange;
1329 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001330 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331
Bram Moolenaar33570922005-01-25 22:26:29 +00001332 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001334 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335
1336 for (i = 0; i < argc; i++)
1337 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001338 /* Pass a NULL or empty argument as an empty string */
1339 if (argv[i] == NULL || *argv[i] == NUL)
1340 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001341 argvars[i].v_type = VAR_STRING;
1342 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001343 continue;
1344 }
1345
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 /* Recognize a number argument, the others must be strings. */
1347 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
1348 if (len != 0 && len == (int)STRLEN(argv[i]))
1349 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001350 argvars[i].v_type = VAR_NUMBER;
1351 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
1353 else
1354 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001355 argvars[i].v_type = VAR_STRING;
1356 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 }
1358 }
1359
1360 if (safe)
1361 {
1362 save_funccalp = save_funccal();
1363 ++sandbox;
1364 }
1365
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001366 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1367 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001369 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 if (safe)
1371 {
1372 --sandbox;
1373 restore_funccal(save_funccalp);
1374 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001375 vim_free(argvars);
1376
1377 if (ret == FAIL)
1378 clear_tv(rettv);
1379
1380 return ret;
1381}
1382
1383/*
1384 * Call some vimL function and return the result as a string
1385 * Uses argv[argc] for the function arguments.
1386 */
1387 void *
1388call_func_retstr(func, argc, argv, safe)
1389 char_u *func;
1390 int argc;
1391 char_u **argv;
1392 int safe; /* use the sandbox */
1393{
1394 typval_T rettv;
1395 char_u *retval = NULL;
1396
1397 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1398 return NULL;
1399
1400 retval = vim_strsave(get_tv_string(&rettv));
1401
1402 clear_tv(&rettv);
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 return retval;
1405}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001406
1407/*
1408 * Call some vimL function and return the result as a list
1409 * Uses argv[argc] for the function arguments.
1410 */
1411 void *
1412call_func_retlist(func, argc, argv, safe)
1413 char_u *func;
1414 int argc;
1415 char_u **argv;
1416 int safe; /* use the sandbox */
1417{
1418 typval_T rettv;
1419
1420 if (call_vim_function(func, argc, argv, safe, &rettv) == FAIL)
1421 return NULL;
1422
1423 if (rettv.v_type != VAR_LIST)
1424 {
1425 clear_tv(&rettv);
1426 return NULL;
1427 }
1428
1429 return rettv.vval.v_list;
1430}
1431
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432#endif
1433
1434/*
1435 * Save the current function call pointer, and set it to NULL.
1436 * Used when executing autocommands and for ":source".
1437 */
1438 void *
1439save_funccal()
1440{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001441 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 current_funccal = NULL;
1444 return (void *)fc;
1445}
1446
1447 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001448restore_funccal(vfc)
1449 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001451 funccall_T *fc = (funccall_T *)vfc;
1452
1453 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454}
1455
Bram Moolenaar05159a02005-02-26 23:04:13 +00001456#if defined(FEAT_PROFILE) || defined(PROTO)
1457/*
1458 * Prepare profiling for entering a child or something else that is not
1459 * counted for the script/function itself.
1460 * Should always be called in pair with prof_child_exit().
1461 */
1462 void
1463prof_child_enter(tm)
1464 proftime_T *tm; /* place to store waittime */
1465{
1466 funccall_T *fc = current_funccal;
1467
1468 if (fc != NULL && fc->func->uf_profiling)
1469 profile_start(&fc->prof_child);
1470 script_prof_save(tm);
1471}
1472
1473/*
1474 * Take care of time spent in a child.
1475 * Should always be called after prof_child_enter().
1476 */
1477 void
1478prof_child_exit(tm)
1479 proftime_T *tm; /* where waittime was stored */
1480{
1481 funccall_T *fc = current_funccal;
1482
1483 if (fc != NULL && fc->func->uf_profiling)
1484 {
1485 profile_end(&fc->prof_child);
1486 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1487 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1488 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1489 }
1490 script_prof_restore(tm);
1491}
1492#endif
1493
1494
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495#ifdef FEAT_FOLDING
1496/*
1497 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1498 * it in "*cp". Doesn't give error messages.
1499 */
1500 int
1501eval_foldexpr(arg, cp)
1502 char_u *arg;
1503 int *cp;
1504{
Bram Moolenaar33570922005-01-25 22:26:29 +00001505 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 int retval;
1507 char_u *s;
1508
1509 ++emsg_off;
1510 ++sandbox;
1511 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001512 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 retval = 0;
1514 else
1515 {
1516 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001517 if (tv.v_type == VAR_NUMBER)
1518 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001519 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 retval = 0;
1521 else
1522 {
1523 /* If the result is a string, check if there is a non-digit before
1524 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001525 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if (!VIM_ISDIGIT(*s) && *s != '-')
1527 *cp = *s++;
1528 retval = atol((char *)s);
1529 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001530 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 }
1532 --emsg_off;
1533 --sandbox;
1534
1535 return retval;
1536}
1537#endif
1538
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001540 * ":let" list all variable values
1541 * ":let var1 var2" list variable values
1542 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001543 * ":let var += expr" assignment command.
1544 * ":let var -= expr" assignment command.
1545 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001546 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 */
1548 void
1549ex_let(eap)
1550 exarg_T *eap;
1551{
1552 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001553 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001554 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001556 int var_count = 0;
1557 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001558 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001560 expr = skip_var_list(arg, &var_count, &semicolon);
1561 if (expr == NULL)
1562 return;
1563 expr = vim_strchr(expr, '=');
1564 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001566 /*
1567 * ":let" without "=": list variables
1568 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001569 if (*arg == '[')
1570 EMSG(_(e_invarg));
1571 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001572 /* ":let var1 var2" */
1573 arg = list_arg_vars(eap, arg);
1574 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001575 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001576 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001577 list_glob_vars();
1578 list_buf_vars();
1579 list_win_vars();
1580 list_vim_vars();
1581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 eap->nextcmd = check_nextcmd(arg);
1583 }
1584 else
1585 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001586 op[0] = '=';
1587 op[1] = NUL;
1588 if (expr > arg)
1589 {
1590 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1591 op[0] = expr[-1]; /* +=, -= or .= */
1592 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001593 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001594
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 if (eap->skip)
1596 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001597 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (eap->skip)
1599 {
1600 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001601 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 --emsg_skip;
1603 }
1604 else if (i != FAIL)
1605 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001606 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001607 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001608 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 }
1610 }
1611}
1612
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001613/*
1614 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1615 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001616 * When "nextchars" is not NULL it points to a string with characters that
1617 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1618 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001619 * Returns OK or FAIL;
1620 */
1621 static int
1622ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1623 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001624 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001625 int copy; /* copy values from "tv", don't move */
1626 int semicolon; /* from skip_var_list() */
1627 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001628 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001629{
1630 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001631 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001632 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001633 listitem_T *item;
1634 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001635
1636 if (*arg != '[')
1637 {
1638 /*
1639 * ":let var = expr" or ":for var in list"
1640 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001641 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001642 return FAIL;
1643 return OK;
1644 }
1645
1646 /*
1647 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1648 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001649 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001650 {
1651 EMSG(_(e_listreq));
1652 return FAIL;
1653 }
1654
1655 i = list_len(l);
1656 if (semicolon == 0 && var_count < i)
1657 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001658 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001659 return FAIL;
1660 }
1661 if (var_count - semicolon > i)
1662 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001663 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001664 return FAIL;
1665 }
1666
1667 item = l->lv_first;
1668 while (*arg != ']')
1669 {
1670 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001671 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001672 item = item->li_next;
1673 if (arg == NULL)
1674 return FAIL;
1675
1676 arg = skipwhite(arg);
1677 if (*arg == ';')
1678 {
1679 /* Put the rest of the list (may be empty) in the var after ';'.
1680 * Create a new list for this. */
1681 l = list_alloc();
1682 if (l == NULL)
1683 return FAIL;
1684 while (item != NULL)
1685 {
1686 list_append_tv(l, &item->li_tv);
1687 item = item->li_next;
1688 }
1689
1690 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001691 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001692 ltv.vval.v_list = l;
1693 l->lv_refcount = 1;
1694
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001695 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1696 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001697 clear_tv(&ltv);
1698 if (arg == NULL)
1699 return FAIL;
1700 break;
1701 }
1702 else if (*arg != ',' && *arg != ']')
1703 {
1704 EMSG2(_(e_intern2), "ex_let_vars()");
1705 return FAIL;
1706 }
1707 }
1708
1709 return OK;
1710}
1711
1712/*
1713 * Skip over assignable variable "var" or list of variables "[var, var]".
1714 * Used for ":let varvar = expr" and ":for varvar in expr".
1715 * For "[var, var]" increment "*var_count" for each variable.
1716 * for "[var, var; var]" set "semicolon".
1717 * Return NULL for an error.
1718 */
1719 static char_u *
1720skip_var_list(arg, var_count, semicolon)
1721 char_u *arg;
1722 int *var_count;
1723 int *semicolon;
1724{
1725 char_u *p, *s;
1726
1727 if (*arg == '[')
1728 {
1729 /* "[var, var]": find the matching ']'. */
1730 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001731 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001732 {
1733 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1734 s = skip_var_one(p);
1735 if (s == p)
1736 {
1737 EMSG2(_(e_invarg2), p);
1738 return NULL;
1739 }
1740 ++*var_count;
1741
1742 p = skipwhite(s);
1743 if (*p == ']')
1744 break;
1745 else if (*p == ';')
1746 {
1747 if (*semicolon == 1)
1748 {
1749 EMSG(_("Double ; in list of variables"));
1750 return NULL;
1751 }
1752 *semicolon = 1;
1753 }
1754 else if (*p != ',')
1755 {
1756 EMSG2(_(e_invarg2), p);
1757 return NULL;
1758 }
1759 }
1760 return p + 1;
1761 }
1762 else
1763 return skip_var_one(arg);
1764}
1765
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001766/*
Bram Moolenaar92124a32005-06-17 22:03:40 +00001767 * Skip one (assignable) variable name, includig @r, $VAR, &option, d.key,
1768 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001769 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001770 static char_u *
1771skip_var_one(arg)
1772 char_u *arg;
1773{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001774 if (*arg == '@' && arg[1] != NUL)
1775 return arg + 2;
1776 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1777 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001778}
1779
Bram Moolenaara7043832005-01-21 11:56:39 +00001780/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001781 * List variables for hashtab "ht" with prefix "prefix".
1782 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001783 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001784 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001785list_hashtable_vars(ht, prefix, empty)
1786 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001787 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001788 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001789{
Bram Moolenaar33570922005-01-25 22:26:29 +00001790 hashitem_T *hi;
1791 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001792 int todo;
1793
1794 todo = ht->ht_used;
1795 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1796 {
1797 if (!HASHITEM_EMPTY(hi))
1798 {
1799 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001800 di = HI2DI(hi);
1801 if (empty || di->di_tv.v_type != VAR_STRING
1802 || di->di_tv.vval.v_string != NULL)
1803 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001804 }
1805 }
1806}
1807
1808/*
1809 * List global variables.
1810 */
1811 static void
1812list_glob_vars()
1813{
Bram Moolenaar33570922005-01-25 22:26:29 +00001814 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001815}
1816
1817/*
1818 * List buffer variables.
1819 */
1820 static void
1821list_buf_vars()
1822{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001823 char_u numbuf[NUMBUFLEN];
1824
Bram Moolenaar33570922005-01-25 22:26:29 +00001825 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001826
1827 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1828 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001829}
1830
1831/*
1832 * List window variables.
1833 */
1834 static void
1835list_win_vars()
1836{
Bram Moolenaar33570922005-01-25 22:26:29 +00001837 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001838}
1839
1840/*
1841 * List Vim variables.
1842 */
1843 static void
1844list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001845{
Bram Moolenaar33570922005-01-25 22:26:29 +00001846 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001847}
1848
1849/*
1850 * List variables in "arg".
1851 */
1852 static char_u *
1853list_arg_vars(eap, arg)
1854 exarg_T *eap;
1855 char_u *arg;
1856{
1857 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001858 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001860 char_u *name_start;
1861 char_u *arg_subsc;
1862 char_u *tofree;
1863 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864
1865 while (!ends_excmd(*arg) && !got_int)
1866 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001867 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001868 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001869 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001870 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1871 {
1872 emsg_severe = TRUE;
1873 EMSG(_(e_trailing));
1874 break;
1875 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001876 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001877 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001879 /* get_name_len() takes care of expanding curly braces */
1880 name_start = name = arg;
1881 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1882 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001884 /* This is mainly to keep test 49 working: when expanding
1885 * curly braces fails overrule the exception error message. */
1886 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001888 emsg_severe = TRUE;
1889 EMSG2(_(e_invarg2), arg);
1890 break;
1891 }
1892 error = TRUE;
1893 }
1894 else
1895 {
1896 if (tofree != NULL)
1897 name = tofree;
1898 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001899 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001900 else
1901 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001902 /* handle d.key, l[idx], f(expr) */
1903 arg_subsc = arg;
1904 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001905 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001907 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001908 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001909 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001910 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001911 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001912 case 'g': list_glob_vars(); break;
1913 case 'b': list_buf_vars(); break;
1914 case 'w': list_win_vars(); break;
1915 case 'v': list_vim_vars(); break;
1916 default:
1917 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001918 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001919 }
1920 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001921 {
1922 char_u numbuf[NUMBUFLEN];
1923 char_u *tf;
1924 int c;
1925 char_u *s;
1926
1927 s = echo_string(&tv, &tf, numbuf);
1928 c = *arg;
1929 *arg = NUL;
1930 list_one_var_a((char_u *)"",
1931 arg == arg_subsc ? name : name_start,
1932 tv.v_type, s == NULL ? (char_u *)"" : s);
1933 *arg = c;
1934 vim_free(tf);
1935 }
1936 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001937 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001938 }
1939 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001940
1941 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001942 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001943
1944 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945 }
1946
1947 return arg;
1948}
1949
1950/*
1951 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1952 * Returns a pointer to the char just after the var name.
1953 * Returns NULL if there is an error.
1954 */
1955 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001956ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001957 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001958 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001959 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001961 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962{
1963 int c1;
1964 char_u *name;
1965 char_u *p;
1966 char_u *arg_end = NULL;
1967 int len;
1968 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001969 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001970
1971 /*
1972 * ":let $VAR = expr": Set environment variable.
1973 */
1974 if (*arg == '$')
1975 {
1976 /* Find the end of the name. */
1977 ++arg;
1978 name = arg;
1979 len = get_env_len(&arg);
1980 if (len == 0)
1981 EMSG2(_(e_invarg2), name - 1);
1982 else
1983 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001984 if (op != NULL && (*op == '+' || *op == '-'))
1985 EMSG2(_(e_letwrong), op);
1986 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001987 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001988 EMSG(_(e_letunexp));
1989 else
1990 {
1991 c1 = name[len];
1992 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001993 p = get_tv_string_chk(tv);
1994 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001995 {
1996 int mustfree = FALSE;
1997 char_u *s = vim_getenv(name, &mustfree);
1998
1999 if (s != NULL)
2000 {
2001 p = tofree = concat_str(s, p);
2002 if (mustfree)
2003 vim_free(s);
2004 }
2005 }
2006 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002007 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002008 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002009 if (STRICMP(name, "HOME") == 0)
2010 init_homedir();
2011 else if (didset_vim && STRICMP(name, "VIM") == 0)
2012 didset_vim = FALSE;
2013 else if (didset_vimruntime
2014 && STRICMP(name, "VIMRUNTIME") == 0)
2015 didset_vimruntime = FALSE;
2016 arg_end = arg;
2017 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002018 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002019 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002020 }
2021 }
2022 }
2023
2024 /*
2025 * ":let &option = expr": Set option value.
2026 * ":let &l:option = expr": Set local option value.
2027 * ":let &g:option = expr": Set global option value.
2028 */
2029 else if (*arg == '&')
2030 {
2031 /* Find the end of the name. */
2032 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002033 if (p == NULL || (endchars != NULL
2034 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002035 EMSG(_(e_letunexp));
2036 else
2037 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002038 long n;
2039 int opt_type;
2040 long numval;
2041 char_u *stringval = NULL;
2042 char_u *s;
2043
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002044 c1 = *p;
2045 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002046
2047 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002048 s = get_tv_string_chk(tv); /* != NULL if number or string */
2049 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002050 {
2051 opt_type = get_option_value(arg, &numval,
2052 &stringval, opt_flags);
2053 if ((opt_type == 1 && *op == '.')
2054 || (opt_type == 0 && *op != '.'))
2055 EMSG2(_(e_letwrong), op);
2056 else
2057 {
2058 if (opt_type == 1) /* number */
2059 {
2060 if (*op == '+')
2061 n = numval + n;
2062 else
2063 n = numval - n;
2064 }
2065 else if (opt_type == 0 && stringval != NULL) /* string */
2066 {
2067 s = concat_str(stringval, s);
2068 vim_free(stringval);
2069 stringval = s;
2070 }
2071 }
2072 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002073 if (s != NULL)
2074 {
2075 set_option_value(arg, n, s, opt_flags);
2076 arg_end = p;
2077 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002078 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002079 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002080 }
2081 }
2082
2083 /*
2084 * ":let @r = expr": Set register contents.
2085 */
2086 else if (*arg == '@')
2087 {
2088 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002089 if (op != NULL && (*op == '+' || *op == '-'))
2090 EMSG2(_(e_letwrong), op);
2091 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002092 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002093 EMSG(_(e_letunexp));
2094 else
2095 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002096 char_u *tofree = NULL;
2097 char_u *s;
2098
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002099 p = get_tv_string_chk(tv);
2100 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002101 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002102 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002103 if (s != NULL)
2104 {
2105 p = tofree = concat_str(s, p);
2106 vim_free(s);
2107 }
2108 }
2109 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002110 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002111 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002112 arg_end = arg + 1;
2113 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002114 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002115 }
2116 }
2117
2118 /*
2119 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002120 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002121 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002122 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002123 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002124 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002125
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002126 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002127 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002128 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002129 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2130 EMSG(_(e_letunexp));
2131 else
2132 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002133 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002134 arg_end = p;
2135 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002136 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002137 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002138 }
2139
2140 else
2141 EMSG2(_(e_invarg2), arg);
2142
2143 return arg_end;
2144}
2145
2146/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002147 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2148 */
2149 static int
2150check_changedtick(arg)
2151 char_u *arg;
2152{
2153 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2154 {
2155 EMSG2(_(e_readonlyvar), arg);
2156 return TRUE;
2157 }
2158 return FALSE;
2159}
2160
2161/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002162 * Get an lval: variable, Dict item or List item that can be assigned a value
2163 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2164 * "name.key", "name.key[expr]" etc.
2165 * Indexing only works if "name" is an existing List or Dictionary.
2166 * "name" points to the start of the name.
2167 * If "rettv" is not NULL it points to the value to be assigned.
2168 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2169 * wrong; must end in space or cmd separator.
2170 *
2171 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002172 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002173 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002174 */
2175 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002176get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002177 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002178 typval_T *rettv;
2179 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002180 int unlet;
2181 int skip;
2182 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002183 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002184{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002185 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002186 char_u *expr_start, *expr_end;
2187 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002188 dictitem_T *v;
2189 typval_T var1;
2190 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002191 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002192 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002193 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002194 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002195 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002197 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002198 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002199
2200 if (skip)
2201 {
2202 /* When skipping just find the end of the name. */
2203 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002204 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002205 }
2206
2207 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002208 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002209 if (expr_start != NULL)
2210 {
2211 /* Don't expand the name when we already know there is an error. */
2212 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2213 && *p != '[' && *p != '.')
2214 {
2215 EMSG(_(e_trailing));
2216 return NULL;
2217 }
2218
2219 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2220 if (lp->ll_exp_name == NULL)
2221 {
2222 /* Report an invalid expression in braces, unless the
2223 * expression evaluation has been cancelled due to an
2224 * aborting error, an interrupt, or an exception. */
2225 if (!aborting() && !quiet)
2226 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002227 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002228 EMSG2(_(e_invarg2), name);
2229 return NULL;
2230 }
2231 }
2232 lp->ll_name = lp->ll_exp_name;
2233 }
2234 else
2235 lp->ll_name = name;
2236
2237 /* Without [idx] or .key we are done. */
2238 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2239 return p;
2240
2241 cc = *p;
2242 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002243 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002244 if (v == NULL && !quiet)
2245 EMSG2(_(e_undefvar), lp->ll_name);
2246 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 if (v == NULL)
2248 return NULL;
2249
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002250 /*
2251 * Loop until no more [idx] or .key is following.
2252 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002253 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002254 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002256 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2257 && !(lp->ll_tv->v_type == VAR_DICT
2258 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002260 if (!quiet)
2261 EMSG(_("E689: Can only index a List or Dictionary"));
2262 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002264 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002266 if (!quiet)
2267 EMSG(_("E708: [:] must come last"));
2268 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002269 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002270
Bram Moolenaar8c711452005-01-14 21:53:12 +00002271 len = -1;
2272 if (*p == '.')
2273 {
2274 key = p + 1;
2275 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2276 ;
2277 if (len == 0)
2278 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002279 if (!quiet)
2280 EMSG(_(e_emptykey));
2281 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002282 }
2283 p = key + len;
2284 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002285 else
2286 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002287 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002288 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002289 if (*p == ':')
2290 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002291 else
2292 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002293 empty1 = FALSE;
2294 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002295 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002296 if (get_tv_string_chk(&var1) == NULL)
2297 {
2298 /* not a number or string */
2299 clear_tv(&var1);
2300 return NULL;
2301 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002302 }
2303
2304 /* Optionally get the second index [ :expr]. */
2305 if (*p == ':')
2306 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002307 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002308 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002309 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002310 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002311 if (!empty1)
2312 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002313 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002314 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002315 if (rettv != NULL && (rettv->v_type != VAR_LIST
2316 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002317 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002318 if (!quiet)
2319 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002320 if (!empty1)
2321 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002322 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002323 }
2324 p = skipwhite(p + 1);
2325 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002326 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002327 else
2328 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002329 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002330 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2331 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002332 if (!empty1)
2333 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002334 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002335 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002336 if (get_tv_string_chk(&var2) == NULL)
2337 {
2338 /* not a number or string */
2339 if (!empty1)
2340 clear_tv(&var1);
2341 clear_tv(&var2);
2342 return NULL;
2343 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002344 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002345 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002346 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002347 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002348 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002349
Bram Moolenaar8c711452005-01-14 21:53:12 +00002350 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002351 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002352 if (!quiet)
2353 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002354 if (!empty1)
2355 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002356 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002357 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002358 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002359 }
2360
2361 /* Skip to past ']'. */
2362 ++p;
2363 }
2364
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002365 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002366 {
2367 if (len == -1)
2368 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002369 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002370 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002371 if (*key == NUL)
2372 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002373 if (!quiet)
2374 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002375 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002376 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002377 }
2378 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002379 lp->ll_list = NULL;
2380 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002381 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002382 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002383 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002384 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002385 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002386 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002387 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002389 if (len == -1)
2390 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002391 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002392 }
2393 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002394 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002395 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002396 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002397 if (len == -1)
2398 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002399 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002400 p = NULL;
2401 break;
2402 }
2403 if (len == -1)
2404 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002405 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002406 }
2407 else
2408 {
2409 /*
2410 * Get the number and item for the only or first index of the List.
2411 */
2412 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002414 else
2415 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002416 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002417 clear_tv(&var1);
2418 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002419 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002420 lp->ll_list = lp->ll_tv->vval.v_list;
2421 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2422 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002423 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002424 if (!quiet)
2425 EMSGN(_(e_listidx), lp->ll_n1);
2426 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002427 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002428 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002429 }
2430
2431 /*
2432 * May need to find the item or absolute index for the second
2433 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002434 * When no index given: "lp->ll_empty2" is TRUE.
2435 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002436 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002437 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002439 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002440 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002441 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002442 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002443 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002444 if (ni == NULL)
2445 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 if (!quiet)
2447 EMSGN(_(e_listidx), lp->ll_n2);
2448 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002449 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002450 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002451 }
2452
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002453 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2454 if (lp->ll_n1 < 0)
2455 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2456 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002457 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002458 if (!quiet)
2459 EMSGN(_(e_listidx), lp->ll_n2);
2460 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002461 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002462 }
2463
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002464 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002465 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002466 }
2467
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002468 return p;
2469}
2470
2471/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002472 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 */
2474 static void
2475clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002476 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477{
2478 vim_free(lp->ll_exp_name);
2479 vim_free(lp->ll_newkey);
2480}
2481
2482/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002483 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002485 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 */
2487 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002489 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002490 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002491 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002492 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002493 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002494{
2495 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002496 listitem_T *ni;
2497 listitem_T *ri;
2498 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002499
2500 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002501 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002502 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002503 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002504 cc = *endp;
2505 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002506 if (op != NULL && *op != '=')
2507 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002508 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002509
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002510 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002511 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2512 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002513 {
2514 if (tv_op(&tv, rettv, op) == OK)
2515 set_var(lp->ll_name, &tv, FALSE);
2516 clear_tv(&tv);
2517 }
2518 }
2519 else
2520 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002521 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002524 else if (tv_check_lock(lp->ll_newkey == NULL
2525 ? lp->ll_tv->v_lock
2526 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2527 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 else if (lp->ll_range)
2529 {
2530 /*
2531 * Assign the List values to the list items.
2532 */
2533 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002534 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002535 if (op != NULL && *op != '=')
2536 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2537 else
2538 {
2539 clear_tv(&lp->ll_li->li_tv);
2540 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2541 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002542 ri = ri->li_next;
2543 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2544 break;
2545 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002546 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002547 /* Need to add an empty item. */
2548 ni = listitem_alloc();
2549 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002550 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 ri = NULL;
2552 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002553 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002555 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 ni->li_tv.vval.v_number = 0;
2557 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002558 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002559 lp->ll_li = lp->ll_li->li_next;
2560 ++lp->ll_n1;
2561 }
2562 if (ri != NULL)
2563 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002564 else if (lp->ll_empty2
2565 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002566 : lp->ll_n1 != lp->ll_n2)
2567 EMSG(_("E711: List value has not enough items"));
2568 }
2569 else
2570 {
2571 /*
2572 * Assign to a List or Dictionary item.
2573 */
2574 if (lp->ll_newkey != NULL)
2575 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002576 if (op != NULL && *op != '=')
2577 {
2578 EMSG2(_(e_letwrong), op);
2579 return;
2580 }
2581
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002582 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002583 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002584 if (di == NULL)
2585 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002586 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2587 {
2588 vim_free(di);
2589 return;
2590 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002592 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002593 else if (op != NULL && *op != '=')
2594 {
2595 tv_op(lp->ll_tv, rettv, op);
2596 return;
2597 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002598 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002600
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 /*
2602 * Assign the value to the variable or list item.
2603 */
2604 if (copy)
2605 copy_tv(rettv, lp->ll_tv);
2606 else
2607 {
2608 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002609 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002611 }
2612 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002613}
2614
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002615/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002616 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2617 * Returns OK or FAIL.
2618 */
2619 static int
2620tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002621 typval_T *tv1;
2622 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002623 char_u *op;
2624{
2625 long n;
2626 char_u numbuf[NUMBUFLEN];
2627 char_u *s;
2628
2629 /* Can't do anything with a Funcref or a Dict on the right. */
2630 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2631 {
2632 switch (tv1->v_type)
2633 {
2634 case VAR_DICT:
2635 case VAR_FUNC:
2636 break;
2637
2638 case VAR_LIST:
2639 if (*op != '+' || tv2->v_type != VAR_LIST)
2640 break;
2641 /* List += List */
2642 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2643 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2644 return OK;
2645
2646 case VAR_NUMBER:
2647 case VAR_STRING:
2648 if (tv2->v_type == VAR_LIST)
2649 break;
2650 if (*op == '+' || *op == '-')
2651 {
2652 /* nr += nr or nr -= nr*/
2653 n = get_tv_number(tv1);
2654 if (*op == '+')
2655 n += get_tv_number(tv2);
2656 else
2657 n -= get_tv_number(tv2);
2658 clear_tv(tv1);
2659 tv1->v_type = VAR_NUMBER;
2660 tv1->vval.v_number = n;
2661 }
2662 else
2663 {
2664 /* str .= str */
2665 s = get_tv_string(tv1);
2666 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2667 clear_tv(tv1);
2668 tv1->v_type = VAR_STRING;
2669 tv1->vval.v_string = s;
2670 }
2671 return OK;
2672 }
2673 }
2674
2675 EMSG2(_(e_letwrong), op);
2676 return FAIL;
2677}
2678
2679/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002680 * Add a watcher to a list.
2681 */
2682 static void
2683list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002684 list_T *l;
2685 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002686{
2687 lw->lw_next = l->lv_watch;
2688 l->lv_watch = lw;
2689}
2690
2691/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002692 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002693 * No warning when it isn't found...
2694 */
2695 static void
2696list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002697 list_T *l;
2698 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002699{
Bram Moolenaar33570922005-01-25 22:26:29 +00002700 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002701
2702 lwp = &l->lv_watch;
2703 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2704 {
2705 if (lw == lwrem)
2706 {
2707 *lwp = lw->lw_next;
2708 break;
2709 }
2710 lwp = &lw->lw_next;
2711 }
2712}
2713
2714/*
2715 * Just before removing an item from a list: advance watchers to the next
2716 * item.
2717 */
2718 static void
2719list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002720 list_T *l;
2721 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002722{
Bram Moolenaar33570922005-01-25 22:26:29 +00002723 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002724
2725 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2726 if (lw->lw_item == item)
2727 lw->lw_item = item->li_next;
2728}
2729
2730/*
2731 * Evaluate the expression used in a ":for var in expr" command.
2732 * "arg" points to "var".
2733 * Set "*errp" to TRUE for an error, FALSE otherwise;
2734 * Return a pointer that holds the info. Null when there is an error.
2735 */
2736 void *
2737eval_for_line(arg, errp, nextcmdp, skip)
2738 char_u *arg;
2739 int *errp;
2740 char_u **nextcmdp;
2741 int skip;
2742{
Bram Moolenaar33570922005-01-25 22:26:29 +00002743 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002744 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002745 typval_T tv;
2746 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002747
2748 *errp = TRUE; /* default: there is an error */
2749
Bram Moolenaar33570922005-01-25 22:26:29 +00002750 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002751 if (fi == NULL)
2752 return NULL;
2753
2754 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2755 if (expr == NULL)
2756 return fi;
2757
2758 expr = skipwhite(expr);
2759 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2760 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002761 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002762 return fi;
2763 }
2764
2765 if (skip)
2766 ++emsg_skip;
2767 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2768 {
2769 *errp = FALSE;
2770 if (!skip)
2771 {
2772 l = tv.vval.v_list;
2773 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002774 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002775 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002776 clear_tv(&tv);
2777 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002778 else
2779 {
2780 fi->fi_list = l;
2781 list_add_watch(l, &fi->fi_lw);
2782 fi->fi_lw.lw_item = l->lv_first;
2783 }
2784 }
2785 }
2786 if (skip)
2787 --emsg_skip;
2788
2789 return fi;
2790}
2791
2792/*
2793 * Use the first item in a ":for" list. Advance to the next.
2794 * Assign the values to the variable (list). "arg" points to the first one.
2795 * Return TRUE when a valid item was found, FALSE when at end of list or
2796 * something wrong.
2797 */
2798 int
2799next_for_item(fi_void, arg)
2800 void *fi_void;
2801 char_u *arg;
2802{
Bram Moolenaar33570922005-01-25 22:26:29 +00002803 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002804 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002805 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002806
2807 item = fi->fi_lw.lw_item;
2808 if (item == NULL)
2809 result = FALSE;
2810 else
2811 {
2812 fi->fi_lw.lw_item = item->li_next;
2813 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2814 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2815 }
2816 return result;
2817}
2818
2819/*
2820 * Free the structure used to store info used by ":for".
2821 */
2822 void
2823free_for_info(fi_void)
2824 void *fi_void;
2825{
Bram Moolenaar33570922005-01-25 22:26:29 +00002826 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002827
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002828 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002829 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002830 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002831 list_unref(fi->fi_list);
2832 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002833 vim_free(fi);
2834}
2835
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2837
2838 void
2839set_context_for_expression(xp, arg, cmdidx)
2840 expand_T *xp;
2841 char_u *arg;
2842 cmdidx_T cmdidx;
2843{
2844 int got_eq = FALSE;
2845 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002846 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002848 if (cmdidx == CMD_let)
2849 {
2850 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002851 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002852 {
2853 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002854 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002855 {
2856 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002857 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002858 if (vim_iswhite(*p))
2859 break;
2860 }
2861 return;
2862 }
2863 }
2864 else
2865 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2866 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 while ((xp->xp_pattern = vim_strpbrk(arg,
2868 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2869 {
2870 c = *xp->xp_pattern;
2871 if (c == '&')
2872 {
2873 c = xp->xp_pattern[1];
2874 if (c == '&')
2875 {
2876 ++xp->xp_pattern;
2877 xp->xp_context = cmdidx != CMD_let || got_eq
2878 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2879 }
2880 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002883 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2884 xp->xp_pattern += 2;
2885
2886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 }
2888 else if (c == '$')
2889 {
2890 /* environment variable */
2891 xp->xp_context = EXPAND_ENV_VARS;
2892 }
2893 else if (c == '=')
2894 {
2895 got_eq = TRUE;
2896 xp->xp_context = EXPAND_EXPRESSION;
2897 }
2898 else if (c == '<'
2899 && xp->xp_context == EXPAND_FUNCTIONS
2900 && vim_strchr(xp->xp_pattern, '(') == NULL)
2901 {
2902 /* Function name can start with "<SNR>" */
2903 break;
2904 }
2905 else if (cmdidx != CMD_let || got_eq)
2906 {
2907 if (c == '"') /* string */
2908 {
2909 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2910 if (c == '\\' && xp->xp_pattern[1] != NUL)
2911 ++xp->xp_pattern;
2912 xp->xp_context = EXPAND_NOTHING;
2913 }
2914 else if (c == '\'') /* literal string */
2915 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002916 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2918 /* skip */ ;
2919 xp->xp_context = EXPAND_NOTHING;
2920 }
2921 else if (c == '|')
2922 {
2923 if (xp->xp_pattern[1] == '|')
2924 {
2925 ++xp->xp_pattern;
2926 xp->xp_context = EXPAND_EXPRESSION;
2927 }
2928 else
2929 xp->xp_context = EXPAND_COMMANDS;
2930 }
2931 else
2932 xp->xp_context = EXPAND_EXPRESSION;
2933 }
2934 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002935 /* Doesn't look like something valid, expand as an expression
2936 * anyway. */
2937 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 arg = xp->xp_pattern;
2939 if (*arg != NUL)
2940 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2941 /* skip */ ;
2942 }
2943 xp->xp_pattern = arg;
2944}
2945
2946#endif /* FEAT_CMDL_COMPL */
2947
2948/*
2949 * ":1,25call func(arg1, arg2)" function call.
2950 */
2951 void
2952ex_call(eap)
2953 exarg_T *eap;
2954{
2955 char_u *arg = eap->arg;
2956 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002958 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002960 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 linenr_T lnum;
2962 int doesrange;
2963 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002964 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002966 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2967 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002968 if (tofree == NULL)
2969 return;
2970
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002971 /* Increase refcount on dictionary, it could get deleted when evaluating
2972 * the arguments. */
2973 if (fudi.fd_dict != NULL)
2974 ++fudi.fd_dict->dv_refcount;
2975
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002976 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2977 len = STRLEN(tofree);
2978 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979
Bram Moolenaar532c7802005-01-27 14:44:31 +00002980 /* Skip white space to allow ":call func ()". Not good, but required for
2981 * backward compatibility. */
2982 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002983 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984
2985 if (*startarg != '(')
2986 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00002987 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 goto end;
2989 }
2990
2991 /*
2992 * When skipping, evaluate the function once, to find the end of the
2993 * arguments.
2994 * When the function takes a range, this is discovered after the first
2995 * call, and the loop is broken.
2996 */
2997 if (eap->skip)
2998 {
2999 ++emsg_skip;
3000 lnum = eap->line2; /* do it once, also with an invalid range */
3001 }
3002 else
3003 lnum = eap->line1;
3004 for ( ; lnum <= eap->line2; ++lnum)
3005 {
3006 if (!eap->skip && eap->addr_count > 0)
3007 {
3008 curwin->w_cursor.lnum = lnum;
3009 curwin->w_cursor.col = 0;
3010 }
3011 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003012 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003013 eap->line1, eap->line2, &doesrange,
3014 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 {
3016 failed = TRUE;
3017 break;
3018 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003019 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 if (doesrange || eap->skip)
3021 break;
3022 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003023 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003024 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003025 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 if (aborting())
3027 break;
3028 }
3029 if (eap->skip)
3030 --emsg_skip;
3031
3032 if (!failed)
3033 {
3034 /* Check for trailing illegal characters and a following command. */
3035 if (!ends_excmd(*arg))
3036 {
3037 emsg_severe = TRUE;
3038 EMSG(_(e_trailing));
3039 }
3040 else
3041 eap->nextcmd = check_nextcmd(arg);
3042 }
3043
3044end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003045 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003046 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047}
3048
3049/*
3050 * ":unlet[!] var1 ... " command.
3051 */
3052 void
3053ex_unlet(eap)
3054 exarg_T *eap;
3055{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003056 ex_unletlock(eap, eap->arg, 0);
3057}
3058
3059/*
3060 * ":lockvar" and ":unlockvar" commands
3061 */
3062 void
3063ex_lockvar(eap)
3064 exarg_T *eap;
3065{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003067 int deep = 2;
3068
3069 if (eap->forceit)
3070 deep = -1;
3071 else if (vim_isdigit(*arg))
3072 {
3073 deep = getdigits(&arg);
3074 arg = skipwhite(arg);
3075 }
3076
3077 ex_unletlock(eap, arg, deep);
3078}
3079
3080/*
3081 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3082 */
3083 static void
3084ex_unletlock(eap, argstart, deep)
3085 exarg_T *eap;
3086 char_u *argstart;
3087 int deep;
3088{
3089 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003092 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093
3094 do
3095 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003096 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003097 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3098 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003099 if (lv.ll_name == NULL)
3100 error = TRUE; /* error but continue parsing */
3101 if (name_end == NULL || (!vim_iswhite(*name_end)
3102 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003104 if (name_end != NULL)
3105 {
3106 emsg_severe = TRUE;
3107 EMSG(_(e_trailing));
3108 }
3109 if (!(eap->skip || error))
3110 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 break;
3112 }
3113
3114 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003115 {
3116 if (eap->cmdidx == CMD_unlet)
3117 {
3118 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3119 error = TRUE;
3120 }
3121 else
3122 {
3123 if (do_lock_var(&lv, name_end, deep,
3124 eap->cmdidx == CMD_lockvar) == FAIL)
3125 error = TRUE;
3126 }
3127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003129 if (!eap->skip)
3130 clear_lval(&lv);
3131
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 arg = skipwhite(name_end);
3133 } while (!ends_excmd(*arg));
3134
3135 eap->nextcmd = check_nextcmd(arg);
3136}
3137
Bram Moolenaar8c711452005-01-14 21:53:12 +00003138 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003139do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003140 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003141 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003142 int forceit;
3143{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003144 int ret = OK;
3145 int cc;
3146
3147 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003148 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003149 cc = *name_end;
3150 *name_end = NUL;
3151
3152 /* Normal name or expanded name. */
3153 if (check_changedtick(lp->ll_name))
3154 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003155 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003156 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003157 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003158 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003159 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3160 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003161 else if (lp->ll_range)
3162 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003163 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003164
3165 /* Delete a range of List items. */
3166 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3167 {
3168 li = lp->ll_li->li_next;
3169 listitem_remove(lp->ll_list, lp->ll_li);
3170 lp->ll_li = li;
3171 ++lp->ll_n1;
3172 }
3173 }
3174 else
3175 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003176 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003177 /* unlet a List item. */
3178 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003179 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003180 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003181 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003182 }
3183
3184 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003185}
3186
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187/*
3188 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003189 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 */
3191 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003192do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003194 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195{
Bram Moolenaar33570922005-01-25 22:26:29 +00003196 hashtab_T *ht;
3197 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003198 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199
Bram Moolenaar33570922005-01-25 22:26:29 +00003200 ht = find_var_ht(name, &varname);
3201 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003203 hi = hash_find(ht, varname);
3204 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003205 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003206 if (var_check_ro(HI2DI(hi)->di_flags, name))
3207 return FAIL;
3208 delete_var(ht, hi);
3209 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003212 if (forceit)
3213 return OK;
3214 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 return FAIL;
3216}
3217
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003218/*
3219 * Lock or unlock variable indicated by "lp".
3220 * "deep" is the levels to go (-1 for unlimited);
3221 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3222 */
3223 static int
3224do_lock_var(lp, name_end, deep, lock)
3225 lval_T *lp;
3226 char_u *name_end;
3227 int deep;
3228 int lock;
3229{
3230 int ret = OK;
3231 int cc;
3232 dictitem_T *di;
3233
3234 if (deep == 0) /* nothing to do */
3235 return OK;
3236
3237 if (lp->ll_tv == NULL)
3238 {
3239 cc = *name_end;
3240 *name_end = NUL;
3241
3242 /* Normal name or expanded name. */
3243 if (check_changedtick(lp->ll_name))
3244 ret = FAIL;
3245 else
3246 {
3247 di = find_var(lp->ll_name, NULL);
3248 if (di == NULL)
3249 ret = FAIL;
3250 else
3251 {
3252 if (lock)
3253 di->di_flags |= DI_FLAGS_LOCK;
3254 else
3255 di->di_flags &= ~DI_FLAGS_LOCK;
3256 item_lock(&di->di_tv, deep, lock);
3257 }
3258 }
3259 *name_end = cc;
3260 }
3261 else if (lp->ll_range)
3262 {
3263 listitem_T *li = lp->ll_li;
3264
3265 /* (un)lock a range of List items. */
3266 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3267 {
3268 item_lock(&li->li_tv, deep, lock);
3269 li = li->li_next;
3270 ++lp->ll_n1;
3271 }
3272 }
3273 else if (lp->ll_list != NULL)
3274 /* (un)lock a List item. */
3275 item_lock(&lp->ll_li->li_tv, deep, lock);
3276 else
3277 /* un(lock) a Dictionary item. */
3278 item_lock(&lp->ll_di->di_tv, deep, lock);
3279
3280 return ret;
3281}
3282
3283/*
3284 * Lock or unlock an item. "deep" is nr of levels to go.
3285 */
3286 static void
3287item_lock(tv, deep, lock)
3288 typval_T *tv;
3289 int deep;
3290 int lock;
3291{
3292 static int recurse = 0;
3293 list_T *l;
3294 listitem_T *li;
3295 dict_T *d;
3296 hashitem_T *hi;
3297 int todo;
3298
3299 if (recurse >= DICT_MAXNEST)
3300 {
3301 EMSG(_("E743: variable nested too deep for (un)lock"));
3302 return;
3303 }
3304 if (deep == 0)
3305 return;
3306 ++recurse;
3307
3308 /* lock/unlock the item itself */
3309 if (lock)
3310 tv->v_lock |= VAR_LOCKED;
3311 else
3312 tv->v_lock &= ~VAR_LOCKED;
3313
3314 switch (tv->v_type)
3315 {
3316 case VAR_LIST:
3317 if ((l = tv->vval.v_list) != NULL)
3318 {
3319 if (lock)
3320 l->lv_lock |= VAR_LOCKED;
3321 else
3322 l->lv_lock &= ~VAR_LOCKED;
3323 if (deep < 0 || deep > 1)
3324 /* recursive: lock/unlock the items the List contains */
3325 for (li = l->lv_first; li != NULL; li = li->li_next)
3326 item_lock(&li->li_tv, deep - 1, lock);
3327 }
3328 break;
3329 case VAR_DICT:
3330 if ((d = tv->vval.v_dict) != NULL)
3331 {
3332 if (lock)
3333 d->dv_lock |= VAR_LOCKED;
3334 else
3335 d->dv_lock &= ~VAR_LOCKED;
3336 if (deep < 0 || deep > 1)
3337 {
3338 /* recursive: lock/unlock the items the List contains */
3339 todo = d->dv_hashtab.ht_used;
3340 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3341 {
3342 if (!HASHITEM_EMPTY(hi))
3343 {
3344 --todo;
3345 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3346 }
3347 }
3348 }
3349 }
3350 }
3351 --recurse;
3352}
3353
Bram Moolenaara40058a2005-07-11 22:42:07 +00003354/*
3355 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
3356 * it refers to a List or Dictionary that is locked.
3357 */
3358 static int
3359tv_islocked(tv)
3360 typval_T *tv;
3361{
3362 return (tv->v_lock & VAR_LOCKED)
3363 || (tv->v_type == VAR_LIST
3364 && tv->vval.v_list != NULL
3365 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3366 || (tv->v_type == VAR_DICT
3367 && tv->vval.v_dict != NULL
3368 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3369}
3370
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3372/*
3373 * Delete all "menutrans_" variables.
3374 */
3375 void
3376del_menutrans_vars()
3377{
Bram Moolenaar33570922005-01-25 22:26:29 +00003378 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003379 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
Bram Moolenaar33570922005-01-25 22:26:29 +00003381 hash_lock(&globvarht);
3382 todo = globvarht.ht_used;
3383 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003384 {
3385 if (!HASHITEM_EMPTY(hi))
3386 {
3387 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003388 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3389 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003390 }
3391 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003392 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393}
3394#endif
3395
3396#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3397
3398/*
3399 * Local string buffer for the next two functions to store a variable name
3400 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3401 * get_user_var_name().
3402 */
3403
3404static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3405
3406static char_u *varnamebuf = NULL;
3407static int varnamebuflen = 0;
3408
3409/*
3410 * Function to concatenate a prefix and a variable name.
3411 */
3412 static char_u *
3413cat_prefix_varname(prefix, name)
3414 int prefix;
3415 char_u *name;
3416{
3417 int len;
3418
3419 len = (int)STRLEN(name) + 3;
3420 if (len > varnamebuflen)
3421 {
3422 vim_free(varnamebuf);
3423 len += 10; /* some additional space */
3424 varnamebuf = alloc(len);
3425 if (varnamebuf == NULL)
3426 {
3427 varnamebuflen = 0;
3428 return NULL;
3429 }
3430 varnamebuflen = len;
3431 }
3432 *varnamebuf = prefix;
3433 varnamebuf[1] = ':';
3434 STRCPY(varnamebuf + 2, name);
3435 return varnamebuf;
3436}
3437
3438/*
3439 * Function given to ExpandGeneric() to obtain the list of user defined
3440 * (global/buffer/window/built-in) variable names.
3441 */
3442/*ARGSUSED*/
3443 char_u *
3444get_user_var_name(xp, idx)
3445 expand_T *xp;
3446 int idx;
3447{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003448 static long_u gdone;
3449 static long_u bdone;
3450 static long_u wdone;
3451 static int vidx;
3452 static hashitem_T *hi;
3453 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454
3455 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00003456 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00003457
3458 /* Global variables */
3459 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003461 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003462 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003463 else
3464 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003465 while (HASHITEM_EMPTY(hi))
3466 ++hi;
3467 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3468 return cat_prefix_varname('g', hi->hi_key);
3469 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003471
3472 /* b: variables */
3473 ht = &curbuf->b_vars.dv_hashtab;
3474 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003476 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003477 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003478 else
3479 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003480 while (HASHITEM_EMPTY(hi))
3481 ++hi;
3482 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003484 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003486 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 return (char_u *)"b:changedtick";
3488 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003489
3490 /* w: variables */
3491 ht = &curwin->w_vars.dv_hashtab;
3492 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003494 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003495 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003496 else
3497 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003498 while (HASHITEM_EMPTY(hi))
3499 ++hi;
3500 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003502
3503 /* v: variables */
3504 if (vidx < VV_LEN)
3505 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
3507 vim_free(varnamebuf);
3508 varnamebuf = NULL;
3509 varnamebuflen = 0;
3510 return NULL;
3511}
3512
3513#endif /* FEAT_CMDL_COMPL */
3514
3515/*
3516 * types for expressions.
3517 */
3518typedef enum
3519{
3520 TYPE_UNKNOWN = 0
3521 , TYPE_EQUAL /* == */
3522 , TYPE_NEQUAL /* != */
3523 , TYPE_GREATER /* > */
3524 , TYPE_GEQUAL /* >= */
3525 , TYPE_SMALLER /* < */
3526 , TYPE_SEQUAL /* <= */
3527 , TYPE_MATCH /* =~ */
3528 , TYPE_NOMATCH /* !~ */
3529} exptype_T;
3530
3531/*
3532 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003533 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3535 */
3536
3537/*
3538 * Handle zero level expression.
3539 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003540 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 * Return OK or FAIL.
3542 */
3543 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003544eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003546 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 char_u **nextcmd;
3548 int evaluate;
3549{
3550 int ret;
3551 char_u *p;
3552
3553 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003554 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 if (ret == FAIL || !ends_excmd(*p))
3556 {
3557 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003558 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 /*
3560 * Report the invalid expression unless the expression evaluation has
3561 * been cancelled due to an aborting error, an interrupt, or an
3562 * exception.
3563 */
3564 if (!aborting())
3565 EMSG2(_(e_invexpr2), arg);
3566 ret = FAIL;
3567 }
3568 if (nextcmd != NULL)
3569 *nextcmd = check_nextcmd(p);
3570
3571 return ret;
3572}
3573
3574/*
3575 * Handle top level expression:
3576 * expr1 ? expr0 : expr0
3577 *
3578 * "arg" must point to the first non-white of the expression.
3579 * "arg" is advanced to the next non-white after the recognized expression.
3580 *
3581 * Return OK or FAIL.
3582 */
3583 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003584eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 int evaluate;
3588{
3589 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003590 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591
3592 /*
3593 * Get the first variable.
3594 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003595 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 return FAIL;
3597
3598 if ((*arg)[0] == '?')
3599 {
3600 result = FALSE;
3601 if (evaluate)
3602 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003603 int error = FALSE;
3604
3605 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003607 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003608 if (error)
3609 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 }
3611
3612 /*
3613 * Get the second variable.
3614 */
3615 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003616 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 return FAIL;
3618
3619 /*
3620 * Check for the ":".
3621 */
3622 if ((*arg)[0] != ':')
3623 {
3624 EMSG(_("E109: Missing ':' after '?'"));
3625 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003626 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 return FAIL;
3628 }
3629
3630 /*
3631 * Get the third variable.
3632 */
3633 *arg = skipwhite(*arg + 1);
3634 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3635 {
3636 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003637 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 return FAIL;
3639 }
3640 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003641 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 }
3643
3644 return OK;
3645}
3646
3647/*
3648 * Handle first level expression:
3649 * expr2 || expr2 || expr2 logical OR
3650 *
3651 * "arg" must point to the first non-white of the expression.
3652 * "arg" is advanced to the next non-white after the recognized expression.
3653 *
3654 * Return OK or FAIL.
3655 */
3656 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003657eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003659 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 int evaluate;
3661{
Bram Moolenaar33570922005-01-25 22:26:29 +00003662 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 long result;
3664 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003665 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666
3667 /*
3668 * Get the first variable.
3669 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003670 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 return FAIL;
3672
3673 /*
3674 * Repeat until there is no following "||".
3675 */
3676 first = TRUE;
3677 result = FALSE;
3678 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3679 {
3680 if (evaluate && first)
3681 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003682 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003684 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003685 if (error)
3686 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 first = FALSE;
3688 }
3689
3690 /*
3691 * Get the second variable.
3692 */
3693 *arg = skipwhite(*arg + 2);
3694 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3695 return FAIL;
3696
3697 /*
3698 * Compute the result.
3699 */
3700 if (evaluate && !result)
3701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003702 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003704 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003705 if (error)
3706 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 }
3708 if (evaluate)
3709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003710 rettv->v_type = VAR_NUMBER;
3711 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 }
3713 }
3714
3715 return OK;
3716}
3717
3718/*
3719 * Handle second level expression:
3720 * expr3 && expr3 && expr3 logical AND
3721 *
3722 * "arg" must point to the first non-white of the expression.
3723 * "arg" is advanced to the next non-white after the recognized expression.
3724 *
3725 * Return OK or FAIL.
3726 */
3727 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003728eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003730 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 int evaluate;
3732{
Bram Moolenaar33570922005-01-25 22:26:29 +00003733 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 long result;
3735 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003736 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737
3738 /*
3739 * Get the first variable.
3740 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003741 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 return FAIL;
3743
3744 /*
3745 * Repeat until there is no following "&&".
3746 */
3747 first = TRUE;
3748 result = TRUE;
3749 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3750 {
3751 if (evaluate && first)
3752 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003753 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003755 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003756 if (error)
3757 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 first = FALSE;
3759 }
3760
3761 /*
3762 * Get the second variable.
3763 */
3764 *arg = skipwhite(*arg + 2);
3765 if (eval4(arg, &var2, evaluate && result) == FAIL)
3766 return FAIL;
3767
3768 /*
3769 * Compute the result.
3770 */
3771 if (evaluate && result)
3772 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003773 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003775 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003776 if (error)
3777 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 }
3779 if (evaluate)
3780 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003781 rettv->v_type = VAR_NUMBER;
3782 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784 }
3785
3786 return OK;
3787}
3788
3789/*
3790 * Handle third level expression:
3791 * var1 == var2
3792 * var1 =~ var2
3793 * var1 != var2
3794 * var1 !~ var2
3795 * var1 > var2
3796 * var1 >= var2
3797 * var1 < var2
3798 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003799 * var1 is var2
3800 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 *
3802 * "arg" must point to the first non-white of the expression.
3803 * "arg" is advanced to the next non-white after the recognized expression.
3804 *
3805 * Return OK or FAIL.
3806 */
3807 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003808eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003810 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 int evaluate;
3812{
Bram Moolenaar33570922005-01-25 22:26:29 +00003813 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 char_u *p;
3815 int i;
3816 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003817 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 int len = 2;
3819 long n1, n2;
3820 char_u *s1, *s2;
3821 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3822 regmatch_T regmatch;
3823 int ic;
3824 char_u *save_cpo;
3825
3826 /*
3827 * Get the first variable.
3828 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003829 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 return FAIL;
3831
3832 p = *arg;
3833 switch (p[0])
3834 {
3835 case '=': if (p[1] == '=')
3836 type = TYPE_EQUAL;
3837 else if (p[1] == '~')
3838 type = TYPE_MATCH;
3839 break;
3840 case '!': if (p[1] == '=')
3841 type = TYPE_NEQUAL;
3842 else if (p[1] == '~')
3843 type = TYPE_NOMATCH;
3844 break;
3845 case '>': if (p[1] != '=')
3846 {
3847 type = TYPE_GREATER;
3848 len = 1;
3849 }
3850 else
3851 type = TYPE_GEQUAL;
3852 break;
3853 case '<': if (p[1] != '=')
3854 {
3855 type = TYPE_SMALLER;
3856 len = 1;
3857 }
3858 else
3859 type = TYPE_SEQUAL;
3860 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003861 case 'i': if (p[1] == 's')
3862 {
3863 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3864 len = 5;
3865 if (!vim_isIDc(p[len]))
3866 {
3867 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3868 type_is = TRUE;
3869 }
3870 }
3871 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
3873
3874 /*
3875 * If there is a comparitive operator, use it.
3876 */
3877 if (type != TYPE_UNKNOWN)
3878 {
3879 /* extra question mark appended: ignore case */
3880 if (p[len] == '?')
3881 {
3882 ic = TRUE;
3883 ++len;
3884 }
3885 /* extra '#' appended: match case */
3886 else if (p[len] == '#')
3887 {
3888 ic = FALSE;
3889 ++len;
3890 }
3891 /* nothing appened: use 'ignorecase' */
3892 else
3893 ic = p_ic;
3894
3895 /*
3896 * Get the second variable.
3897 */
3898 *arg = skipwhite(p + len);
3899 if (eval5(arg, &var2, evaluate) == FAIL)
3900 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003901 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 return FAIL;
3903 }
3904
3905 if (evaluate)
3906 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003907 if (type_is && rettv->v_type != var2.v_type)
3908 {
3909 /* For "is" a different type always means FALSE, for "notis"
3910 * it means TRUE. */
3911 n1 = (type == TYPE_NEQUAL);
3912 }
3913 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3914 {
3915 if (type_is)
3916 {
3917 n1 = (rettv->v_type == var2.v_type
3918 && rettv->vval.v_list == var2.vval.v_list);
3919 if (type == TYPE_NEQUAL)
3920 n1 = !n1;
3921 }
3922 else if (rettv->v_type != var2.v_type
3923 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3924 {
3925 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003926 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003927 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003928 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003929 clear_tv(rettv);
3930 clear_tv(&var2);
3931 return FAIL;
3932 }
3933 else
3934 {
3935 /* Compare two Lists for being equal or unequal. */
3936 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3937 if (type == TYPE_NEQUAL)
3938 n1 = !n1;
3939 }
3940 }
3941
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003942 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3943 {
3944 if (type_is)
3945 {
3946 n1 = (rettv->v_type == var2.v_type
3947 && rettv->vval.v_dict == var2.vval.v_dict);
3948 if (type == TYPE_NEQUAL)
3949 n1 = !n1;
3950 }
3951 else if (rettv->v_type != var2.v_type
3952 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3953 {
3954 if (rettv->v_type != var2.v_type)
3955 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3956 else
3957 EMSG(_("E736: Invalid operation for Dictionary"));
3958 clear_tv(rettv);
3959 clear_tv(&var2);
3960 return FAIL;
3961 }
3962 else
3963 {
3964 /* Compare two Dictionaries for being equal or unequal. */
3965 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3966 if (type == TYPE_NEQUAL)
3967 n1 = !n1;
3968 }
3969 }
3970
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003971 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3972 {
3973 if (rettv->v_type != var2.v_type
3974 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3975 {
3976 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003977 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003978 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003979 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003980 clear_tv(rettv);
3981 clear_tv(&var2);
3982 return FAIL;
3983 }
3984 else
3985 {
3986 /* Compare two Funcrefs for being equal or unequal. */
3987 if (rettv->vval.v_string == NULL
3988 || var2.vval.v_string == NULL)
3989 n1 = FALSE;
3990 else
3991 n1 = STRCMP(rettv->vval.v_string,
3992 var2.vval.v_string) == 0;
3993 if (type == TYPE_NEQUAL)
3994 n1 = !n1;
3995 }
3996 }
3997
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 /*
3999 * If one of the two variables is a number, compare as a number.
4000 * When using "=~" or "!~", always compare as string.
4001 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004002 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4004 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004005 n1 = get_tv_number(rettv);
4006 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 switch (type)
4008 {
4009 case TYPE_EQUAL: n1 = (n1 == n2); break;
4010 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4011 case TYPE_GREATER: n1 = (n1 > n2); break;
4012 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4013 case TYPE_SMALLER: n1 = (n1 < n2); break;
4014 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4015 case TYPE_UNKNOWN:
4016 case TYPE_MATCH:
4017 case TYPE_NOMATCH: break; /* avoid gcc warning */
4018 }
4019 }
4020 else
4021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004022 s1 = get_tv_string_buf(rettv, buf1);
4023 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4025 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4026 else
4027 i = 0;
4028 n1 = FALSE;
4029 switch (type)
4030 {
4031 case TYPE_EQUAL: n1 = (i == 0); break;
4032 case TYPE_NEQUAL: n1 = (i != 0); break;
4033 case TYPE_GREATER: n1 = (i > 0); break;
4034 case TYPE_GEQUAL: n1 = (i >= 0); break;
4035 case TYPE_SMALLER: n1 = (i < 0); break;
4036 case TYPE_SEQUAL: n1 = (i <= 0); break;
4037
4038 case TYPE_MATCH:
4039 case TYPE_NOMATCH:
4040 /* avoid 'l' flag in 'cpoptions' */
4041 save_cpo = p_cpo;
4042 p_cpo = (char_u *)"";
4043 regmatch.regprog = vim_regcomp(s2,
4044 RE_MAGIC + RE_STRING);
4045 regmatch.rm_ic = ic;
4046 if (regmatch.regprog != NULL)
4047 {
4048 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
4049 vim_free(regmatch.regprog);
4050 if (type == TYPE_NOMATCH)
4051 n1 = !n1;
4052 }
4053 p_cpo = save_cpo;
4054 break;
4055
4056 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4057 }
4058 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004059 clear_tv(rettv);
4060 clear_tv(&var2);
4061 rettv->v_type = VAR_NUMBER;
4062 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 }
4064 }
4065
4066 return OK;
4067}
4068
4069/*
4070 * Handle fourth level expression:
4071 * + number addition
4072 * - number subtraction
4073 * . string concatenation
4074 *
4075 * "arg" must point to the first non-white of the expression.
4076 * "arg" is advanced to the next non-white after the recognized expression.
4077 *
4078 * Return OK or FAIL.
4079 */
4080 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 int evaluate;
4085{
Bram Moolenaar33570922005-01-25 22:26:29 +00004086 typval_T var2;
4087 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 int op;
4089 long n1, n2;
4090 char_u *s1, *s2;
4091 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4092 char_u *p;
4093
4094 /*
4095 * Get the first variable.
4096 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004097 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 return FAIL;
4099
4100 /*
4101 * Repeat computing, until no '+', '-' or '.' is following.
4102 */
4103 for (;;)
4104 {
4105 op = **arg;
4106 if (op != '+' && op != '-' && op != '.')
4107 break;
4108
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004109 if (op != '+' || rettv->v_type != VAR_LIST)
4110 {
4111 /* For "list + ...", an illegal use of the first operand as
4112 * a number cannot be determined before evaluating the 2nd
4113 * operand: if this is also a list, all is ok.
4114 * For "something . ...", "something - ..." or "non-list + ...",
4115 * we know that the first operand needs to be a string or number
4116 * without evaluating the 2nd operand. So check before to avoid
4117 * side effects after an error. */
4118 if (evaluate && get_tv_string_chk(rettv) == NULL)
4119 {
4120 clear_tv(rettv);
4121 return FAIL;
4122 }
4123 }
4124
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 /*
4126 * Get the second variable.
4127 */
4128 *arg = skipwhite(*arg + 1);
4129 if (eval6(arg, &var2, evaluate) == FAIL)
4130 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004131 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 return FAIL;
4133 }
4134
4135 if (evaluate)
4136 {
4137 /*
4138 * Compute the result.
4139 */
4140 if (op == '.')
4141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004142 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4143 s2 = get_tv_string_buf_chk(&var2, buf2);
4144 if (s2 == NULL) /* type error ? */
4145 {
4146 clear_tv(rettv);
4147 clear_tv(&var2);
4148 return FAIL;
4149 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004150 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151 clear_tv(rettv);
4152 rettv->v_type = VAR_STRING;
4153 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004155 else if (op == '+' && rettv->v_type == VAR_LIST
4156 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004157 {
4158 /* concatenate Lists */
4159 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4160 &var3) == FAIL)
4161 {
4162 clear_tv(rettv);
4163 clear_tv(&var2);
4164 return FAIL;
4165 }
4166 clear_tv(rettv);
4167 *rettv = var3;
4168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 else
4170 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004171 int error = FALSE;
4172
4173 n1 = get_tv_number_chk(rettv, &error);
4174 if (error)
4175 {
4176 /* This can only happen for "list + non-list".
4177 * For "non-list + ..." or "something - ...", we returned
4178 * before evaluating the 2nd operand. */
4179 clear_tv(rettv);
4180 return FAIL;
4181 }
4182 n2 = get_tv_number_chk(&var2, &error);
4183 if (error)
4184 {
4185 clear_tv(rettv);
4186 clear_tv(&var2);
4187 return FAIL;
4188 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004189 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 if (op == '+')
4191 n1 = n1 + n2;
4192 else
4193 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004194 rettv->v_type = VAR_NUMBER;
4195 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
4199 }
4200 return OK;
4201}
4202
4203/*
4204 * Handle fifth level expression:
4205 * * number multiplication
4206 * / number division
4207 * % number modulo
4208 *
4209 * "arg" must point to the first non-white of the expression.
4210 * "arg" is advanced to the next non-white after the recognized expression.
4211 *
4212 * Return OK or FAIL.
4213 */
4214 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004217 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 int evaluate;
4219{
Bram Moolenaar33570922005-01-25 22:26:29 +00004220 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 int op;
4222 long n1, n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004223 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224
4225 /*
4226 * Get the first variable.
4227 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004228 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 return FAIL;
4230
4231 /*
4232 * Repeat computing, until no '*', '/' or '%' is following.
4233 */
4234 for (;;)
4235 {
4236 op = **arg;
4237 if (op != '*' && op != '/' && op != '%')
4238 break;
4239
4240 if (evaluate)
4241 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004242 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004243 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004244 if (error)
4245 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 }
4247 else
4248 n1 = 0;
4249
4250 /*
4251 * Get the second variable.
4252 */
4253 *arg = skipwhite(*arg + 1);
4254 if (eval7(arg, &var2, evaluate) == FAIL)
4255 return FAIL;
4256
4257 if (evaluate)
4258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 n2 = get_tv_number_chk(&var2, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261 if (error)
4262 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
4264 /*
4265 * Compute the result.
4266 */
4267 if (op == '*')
4268 n1 = n1 * n2;
4269 else if (op == '/')
4270 {
4271 if (n2 == 0) /* give an error message? */
4272 n1 = 0x7fffffffL;
4273 else
4274 n1 = n1 / n2;
4275 }
4276 else
4277 {
4278 if (n2 == 0) /* give an error message? */
4279 n1 = 0;
4280 else
4281 n1 = n1 % n2;
4282 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004283 rettv->v_type = VAR_NUMBER;
4284 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 }
4287
4288 return OK;
4289}
4290
4291/*
4292 * Handle sixth level expression:
4293 * number number constant
4294 * "string" string contstant
4295 * 'string' literal string contstant
4296 * &option-name option value
4297 * @r register contents
4298 * identifier variable value
4299 * function() function call
4300 * $VAR environment variable
4301 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004302 * [expr, expr] List
4303 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 *
4305 * Also handle:
4306 * ! in front logical NOT
4307 * - in front unary minus
4308 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004309 * trailing [] subscript in String or List
4310 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 *
4312 * "arg" must point to the first non-white of the expression.
4313 * "arg" is advanced to the next non-white after the recognized expression.
4314 *
4315 * Return OK or FAIL.
4316 */
4317 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004318eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 int evaluate;
4322{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 long n;
4324 int len;
4325 char_u *s;
4326 int val;
4327 char_u *start_leader, *end_leader;
4328 int ret = OK;
4329 char_u *alias;
4330
4331 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004332 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004333 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004335 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336
4337 /*
4338 * Skip '!' and '-' characters. They are handled later.
4339 */
4340 start_leader = *arg;
4341 while (**arg == '!' || **arg == '-' || **arg == '+')
4342 *arg = skipwhite(*arg + 1);
4343 end_leader = *arg;
4344
4345 switch (**arg)
4346 {
4347 /*
4348 * Number constant.
4349 */
4350 case '0':
4351 case '1':
4352 case '2':
4353 case '3':
4354 case '4':
4355 case '5':
4356 case '6':
4357 case '7':
4358 case '8':
4359 case '9':
4360 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
4361 *arg += len;
4362 if (evaluate)
4363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004364 rettv->v_type = VAR_NUMBER;
4365 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 }
4367 break;
4368
4369 /*
4370 * String constant: "string".
4371 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004372 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 break;
4374
4375 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004376 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004378 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004379 break;
4380
4381 /*
4382 * List: [expr, expr]
4383 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004384 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 break;
4386
4387 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004388 * Dictionary: {key: val, key: val}
4389 */
4390 case '{': ret = get_dict_tv(arg, rettv, evaluate);
4391 break;
4392
4393 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004394 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004396 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 break;
4398
4399 /*
4400 * Environment variable: $VAR.
4401 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004402 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 break;
4404
4405 /*
4406 * Register contents: @r.
4407 */
4408 case '@': ++*arg;
4409 if (evaluate)
4410 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004411 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00004412 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
4414 if (**arg != NUL)
4415 ++*arg;
4416 break;
4417
4418 /*
4419 * nested expression: (expression).
4420 */
4421 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004422 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 if (**arg == ')')
4424 ++*arg;
4425 else if (ret == OK)
4426 {
4427 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004428 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 ret = FAIL;
4430 }
4431 break;
4432
Bram Moolenaar8c711452005-01-14 21:53:12 +00004433 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 break;
4435 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004436
4437 if (ret == NOTDONE)
4438 {
4439 /*
4440 * Must be a variable or function name.
4441 * Can also be a curly-braces kind of name: {expr}.
4442 */
4443 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004444 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004445 if (alias != NULL)
4446 s = alias;
4447
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004448 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004449 ret = FAIL;
4450 else
4451 {
4452 if (**arg == '(') /* recursive! */
4453 {
4454 /* If "s" is the name of a variable of type VAR_FUNC
4455 * use its contents. */
4456 s = deref_func_name(s, &len);
4457
4458 /* Invoke the function. */
4459 ret = get_func_tv(s, len, rettv, arg,
4460 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00004461 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004462 /* Stop the expression evaluation when immediately
4463 * aborting on error, or when an interrupt occurred or
4464 * an exception was thrown but not caught. */
4465 if (aborting())
4466 {
4467 if (ret == OK)
4468 clear_tv(rettv);
4469 ret = FAIL;
4470 }
4471 }
4472 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004473 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004474 else
4475 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004476 }
4477
4478 if (alias != NULL)
4479 vim_free(alias);
4480 }
4481
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 *arg = skipwhite(*arg);
4483
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004484 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4485 * expr(expr). */
4486 if (ret == OK)
4487 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488
4489 /*
4490 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4491 */
4492 if (ret == OK && evaluate && end_leader > start_leader)
4493 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004494 int error = FALSE;
4495
4496 val = get_tv_number_chk(rettv, &error);
4497 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004499 clear_tv(rettv);
4500 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004502 else
4503 {
4504 while (end_leader > start_leader)
4505 {
4506 --end_leader;
4507 if (*end_leader == '!')
4508 val = !val;
4509 else if (*end_leader == '-')
4510 val = -val;
4511 }
4512 clear_tv(rettv);
4513 rettv->v_type = VAR_NUMBER;
4514 rettv->vval.v_number = val;
4515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 }
4517
4518 return ret;
4519}
4520
4521/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004522 * Evaluate an "[expr]" or "[expr:expr]" index.
4523 * "*arg" points to the '['.
4524 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4525 */
4526 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004527eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004528 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004529 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004530 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004531 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004532{
4533 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004534 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004535 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004536 long len = -1;
4537 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004538 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004539 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004540
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004541 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004542 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004543 if (verbose)
4544 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004545 return FAIL;
4546 }
4547
Bram Moolenaar8c711452005-01-14 21:53:12 +00004548 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004549 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004550 /*
4551 * dict.name
4552 */
4553 key = *arg + 1;
4554 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4555 ;
4556 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004557 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004558 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004559 }
4560 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004561 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004562 /*
4563 * something[idx]
4564 *
4565 * Get the (first) variable from inside the [].
4566 */
4567 *arg = skipwhite(*arg + 1);
4568 if (**arg == ':')
4569 empty1 = TRUE;
4570 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4571 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004572 else if (evaluate && get_tv_string_chk(&var1) == NULL)
4573 {
4574 /* not a number or string */
4575 clear_tv(&var1);
4576 return FAIL;
4577 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004578
4579 /*
4580 * Get the second variable from inside the [:].
4581 */
4582 if (**arg == ':')
4583 {
4584 range = TRUE;
4585 *arg = skipwhite(*arg + 1);
4586 if (**arg == ']')
4587 empty2 = TRUE;
4588 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4589 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004590 if (!empty1)
4591 clear_tv(&var1);
4592 return FAIL;
4593 }
4594 else if (evaluate && get_tv_string_chk(&var2) == NULL)
4595 {
4596 /* not a number or string */
4597 if (!empty1)
4598 clear_tv(&var1);
4599 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004600 return FAIL;
4601 }
4602 }
4603
4604 /* Check for the ']'. */
4605 if (**arg != ']')
4606 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004607 if (verbose)
4608 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004609 clear_tv(&var1);
4610 if (range)
4611 clear_tv(&var2);
4612 return FAIL;
4613 }
4614 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004615 }
4616
4617 if (evaluate)
4618 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004619 n1 = 0;
4620 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004621 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004622 n1 = get_tv_number(&var1);
4623 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004624 }
4625 if (range)
4626 {
4627 if (empty2)
4628 n2 = -1;
4629 else
4630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004631 n2 = get_tv_number(&var2);
4632 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004633 }
4634 }
4635
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004636 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004637 {
4638 case VAR_NUMBER:
4639 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004640 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004641 len = (long)STRLEN(s);
4642 if (range)
4643 {
4644 /* The resulting variable is a substring. If the indexes
4645 * are out of range the result is empty. */
4646 if (n1 < 0)
4647 {
4648 n1 = len + n1;
4649 if (n1 < 0)
4650 n1 = 0;
4651 }
4652 if (n2 < 0)
4653 n2 = len + n2;
4654 else if (n2 >= len)
4655 n2 = len;
4656 if (n1 >= len || n2 < 0 || n1 > n2)
4657 s = NULL;
4658 else
4659 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4660 }
4661 else
4662 {
4663 /* The resulting variable is a string of a single
4664 * character. If the index is too big or negative the
4665 * result is empty. */
4666 if (n1 >= len || n1 < 0)
4667 s = NULL;
4668 else
4669 s = vim_strnsave(s + n1, 1);
4670 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004671 clear_tv(rettv);
4672 rettv->v_type = VAR_STRING;
4673 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004674 break;
4675
4676 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004677 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004678 if (n1 < 0)
4679 n1 = len + n1;
4680 if (!empty1 && (n1 < 0 || n1 >= len))
4681 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004682 if (verbose)
4683 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004684 return FAIL;
4685 }
4686 if (range)
4687 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004688 list_T *l;
4689 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004690
4691 if (n2 < 0)
4692 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004693 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004694 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004695 if (verbose)
4696 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004697 return FAIL;
4698 }
4699 l = list_alloc();
4700 if (l == NULL)
4701 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004702 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004703 n1 <= n2; ++n1)
4704 {
4705 if (list_append_tv(l, &item->li_tv) == FAIL)
4706 {
4707 list_free(l);
4708 return FAIL;
4709 }
4710 item = item->li_next;
4711 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004712 clear_tv(rettv);
4713 rettv->v_type = VAR_LIST;
4714 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004715 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004716 }
4717 else
4718 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004719 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004720 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004721 clear_tv(rettv);
4722 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004723 }
4724 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004725
4726 case VAR_DICT:
4727 if (range)
4728 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004729 if (verbose)
4730 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004731 if (len == -1)
4732 clear_tv(&var1);
4733 return FAIL;
4734 }
4735 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004736 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004737
4738 if (len == -1)
4739 {
4740 key = get_tv_string(&var1);
4741 if (*key == NUL)
4742 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004743 if (verbose)
4744 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004745 clear_tv(&var1);
4746 return FAIL;
4747 }
4748 }
4749
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004750 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004751
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004752 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004753 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004754 if (len == -1)
4755 clear_tv(&var1);
4756 if (item == NULL)
4757 return FAIL;
4758
4759 copy_tv(&item->di_tv, &var1);
4760 clear_tv(rettv);
4761 *rettv = var1;
4762 }
4763 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004764 }
4765 }
4766
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004767 return OK;
4768}
4769
4770/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 * Get an option value.
4772 * "arg" points to the '&' or '+' before the option name.
4773 * "arg" is advanced to character after the option name.
4774 * Return OK or FAIL.
4775 */
4776 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004777get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004779 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 int evaluate;
4781{
4782 char_u *option_end;
4783 long numval;
4784 char_u *stringval;
4785 int opt_type;
4786 int c;
4787 int working = (**arg == '+'); /* has("+option") */
4788 int ret = OK;
4789 int opt_flags;
4790
4791 /*
4792 * Isolate the option name and find its value.
4793 */
4794 option_end = find_option_end(arg, &opt_flags);
4795 if (option_end == NULL)
4796 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004797 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 EMSG2(_("E112: Option name missing: %s"), *arg);
4799 return FAIL;
4800 }
4801
4802 if (!evaluate)
4803 {
4804 *arg = option_end;
4805 return OK;
4806 }
4807
4808 c = *option_end;
4809 *option_end = NUL;
4810 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004811 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
4813 if (opt_type == -3) /* invalid name */
4814 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004815 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 EMSG2(_("E113: Unknown option: %s"), *arg);
4817 ret = FAIL;
4818 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004819 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 {
4821 if (opt_type == -2) /* hidden string option */
4822 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004823 rettv->v_type = VAR_STRING;
4824 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 }
4826 else if (opt_type == -1) /* hidden number option */
4827 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004828 rettv->v_type = VAR_NUMBER;
4829 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 }
4831 else if (opt_type == 1) /* number option */
4832 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004833 rettv->v_type = VAR_NUMBER;
4834 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 }
4836 else /* string option */
4837 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004838 rettv->v_type = VAR_STRING;
4839 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 }
4841 }
4842 else if (working && (opt_type == -2 || opt_type == -1))
4843 ret = FAIL;
4844
4845 *option_end = c; /* put back for error messages */
4846 *arg = option_end;
4847
4848 return ret;
4849}
4850
4851/*
4852 * Allocate a variable for a string constant.
4853 * Return OK or FAIL.
4854 */
4855 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004856get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 int evaluate;
4860{
4861 char_u *p;
4862 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 int extra = 0;
4864
4865 /*
4866 * Find the end of the string, skipping backslashed characters.
4867 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004868 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 {
4870 if (*p == '\\' && p[1] != NUL)
4871 {
4872 ++p;
4873 /* A "\<x>" form occupies at least 4 characters, and produces up
4874 * to 6 characters: reserve space for 2 extra */
4875 if (*p == '<')
4876 extra += 2;
4877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
4879
4880 if (*p != '"')
4881 {
4882 EMSG2(_("E114: Missing quote: %s"), *arg);
4883 return FAIL;
4884 }
4885
4886 /* If only parsing, set *arg and return here */
4887 if (!evaluate)
4888 {
4889 *arg = p + 1;
4890 return OK;
4891 }
4892
4893 /*
4894 * Copy the string into allocated memory, handling backslashed
4895 * characters.
4896 */
4897 name = alloc((unsigned)(p - *arg + extra));
4898 if (name == NULL)
4899 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004900 rettv->v_type = VAR_STRING;
4901 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902
Bram Moolenaar8c711452005-01-14 21:53:12 +00004903 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 {
4905 if (*p == '\\')
4906 {
4907 switch (*++p)
4908 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004909 case 'b': *name++ = BS; ++p; break;
4910 case 'e': *name++ = ESC; ++p; break;
4911 case 'f': *name++ = FF; ++p; break;
4912 case 'n': *name++ = NL; ++p; break;
4913 case 'r': *name++ = CAR; ++p; break;
4914 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915
4916 case 'X': /* hex: "\x1", "\x12" */
4917 case 'x':
4918 case 'u': /* Unicode: "\u0023" */
4919 case 'U':
4920 if (vim_isxdigit(p[1]))
4921 {
4922 int n, nr;
4923 int c = toupper(*p);
4924
4925 if (c == 'X')
4926 n = 2;
4927 else
4928 n = 4;
4929 nr = 0;
4930 while (--n >= 0 && vim_isxdigit(p[1]))
4931 {
4932 ++p;
4933 nr = (nr << 4) + hex2nr(*p);
4934 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004935 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936#ifdef FEAT_MBYTE
4937 /* For "\u" store the number according to
4938 * 'encoding'. */
4939 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004940 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 else
4942#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004943 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 break;
4946
4947 /* octal: "\1", "\12", "\123" */
4948 case '0':
4949 case '1':
4950 case '2':
4951 case '3':
4952 case '4':
4953 case '5':
4954 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004955 case '7': *name = *p++ - '0';
4956 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004958 *name = (*name << 3) + *p++ - '0';
4959 if (*p >= '0' && *p <= '7')
4960 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004962 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 break;
4964
4965 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004966 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 if (extra != 0)
4968 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004969 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 break;
4971 }
4972 /* FALLTHROUGH */
4973
Bram Moolenaar8c711452005-01-14 21:53:12 +00004974 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 break;
4976 }
4977 }
4978 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004979 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004982 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 *arg = p + 1;
4984
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 return OK;
4986}
4987
4988/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004989 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 * Return OK or FAIL.
4991 */
4992 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004993get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 int evaluate;
4997{
4998 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004999 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005000 int reduce = 0;
5001
5002 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005003 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005004 */
5005 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5006 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005007 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005008 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005009 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005010 break;
5011 ++reduce;
5012 ++p;
5013 }
5014 }
5015
Bram Moolenaar8c711452005-01-14 21:53:12 +00005016 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005017 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005018 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005019 return FAIL;
5020 }
5021
Bram Moolenaar8c711452005-01-14 21:53:12 +00005022 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005023 if (!evaluate)
5024 {
5025 *arg = p + 1;
5026 return OK;
5027 }
5028
5029 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005030 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005031 */
5032 str = alloc((unsigned)((p - *arg) - reduce));
5033 if (str == NULL)
5034 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005035 rettv->v_type = VAR_STRING;
5036 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005037
Bram Moolenaar8c711452005-01-14 21:53:12 +00005038 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005039 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005041 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005042 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005043 break;
5044 ++p;
5045 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005047 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005048 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005049 *arg = p + 1;
5050
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005051 return OK;
5052}
5053
5054/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005055 * Allocate a variable for a List and fill it from "*arg".
5056 * Return OK or FAIL.
5057 */
5058 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005059get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005060 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005061 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005062 int evaluate;
5063{
Bram Moolenaar33570922005-01-25 22:26:29 +00005064 list_T *l = NULL;
5065 typval_T tv;
5066 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005067
5068 if (evaluate)
5069 {
5070 l = list_alloc();
5071 if (l == NULL)
5072 return FAIL;
5073 }
5074
5075 *arg = skipwhite(*arg + 1);
5076 while (**arg != ']' && **arg != NUL)
5077 {
5078 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5079 goto failret;
5080 if (evaluate)
5081 {
5082 item = listitem_alloc();
5083 if (item != NULL)
5084 {
5085 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005086 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005087 list_append(l, item);
5088 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005089 else
5090 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005091 }
5092
5093 if (**arg == ']')
5094 break;
5095 if (**arg != ',')
5096 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005097 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005098 goto failret;
5099 }
5100 *arg = skipwhite(*arg + 1);
5101 }
5102
5103 if (**arg != ']')
5104 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005105 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005106failret:
5107 if (evaluate)
5108 list_free(l);
5109 return FAIL;
5110 }
5111
5112 *arg = skipwhite(*arg + 1);
5113 if (evaluate)
5114 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005115 rettv->v_type = VAR_LIST;
5116 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005117 ++l->lv_refcount;
5118 }
5119
5120 return OK;
5121}
5122
5123/*
5124 * Allocate an empty header for a list.
5125 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005126 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005127list_alloc()
5128{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005129 list_T *l;
5130
5131 l = (list_T *)alloc_clear(sizeof(list_T));
5132 if (l != NULL)
5133 {
5134 /* Prepend the list to the list of lists for garbage collection. */
5135 if (first_list != NULL)
5136 first_list->lv_used_prev = l;
5137 l->lv_used_prev = NULL;
5138 l->lv_used_next = first_list;
5139 first_list = l;
5140 }
5141 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005142}
5143
5144/*
5145 * Unreference a list: decrement the reference count and free it when it
5146 * becomes zero.
5147 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005148 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005149list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005150 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005151{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005152 if (l != NULL && l->lv_refcount != DEL_REFCOUNT && --l->lv_refcount <= 0)
5153 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005154}
5155
5156/*
5157 * Free a list, including all items it points to.
5158 * Ignores the reference count.
5159 */
5160 static void
5161list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005162 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005163{
Bram Moolenaar33570922005-01-25 22:26:29 +00005164 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005165
Bram Moolenaard9fba312005-06-26 22:34:35 +00005166 /* Avoid that recursive reference to the list frees us again. */
5167 l->lv_refcount = DEL_REFCOUNT;
5168
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005169 /* Remove the list from the list of lists for garbage collection. */
5170 if (l->lv_used_prev == NULL)
5171 first_list = l->lv_used_next;
5172 else
5173 l->lv_used_prev->lv_used_next = l->lv_used_next;
5174 if (l->lv_used_next != NULL)
5175 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5176
Bram Moolenaard9fba312005-06-26 22:34:35 +00005177 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005178 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005179 /* Remove the item before deleting it. */
5180 l->lv_first = item->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005181 listitem_free(item);
5182 }
5183 vim_free(l);
5184}
5185
5186/*
5187 * Allocate a list item.
5188 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005189 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005190listitem_alloc()
5191{
Bram Moolenaar33570922005-01-25 22:26:29 +00005192 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005193}
5194
5195/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005196 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005197 */
5198 static void
5199listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005200 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005201{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005202 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005203 vim_free(item);
5204}
5205
5206/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005207 * Remove a list item from a List and free it. Also clears the value.
5208 */
5209 static void
5210listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005211 list_T *l;
5212 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005213{
5214 list_remove(l, item, item);
5215 listitem_free(item);
5216}
5217
5218/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005219 * Get the number of items in a list.
5220 */
5221 static long
5222list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005223 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005224{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005225 if (l == NULL)
5226 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005227 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005228}
5229
5230/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005231 * Return TRUE when two lists have exactly the same values.
5232 */
5233 static int
5234list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005235 list_T *l1;
5236 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005237 int ic; /* ignore case for strings */
5238{
Bram Moolenaar33570922005-01-25 22:26:29 +00005239 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005240
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005241 if (list_len(l1) != list_len(l2))
5242 return FALSE;
5243
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005244 for (item1 = l1->lv_first, item2 = l2->lv_first;
5245 item1 != NULL && item2 != NULL;
5246 item1 = item1->li_next, item2 = item2->li_next)
5247 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
5248 return FALSE;
5249 return item1 == NULL && item2 == NULL;
5250}
5251
5252/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005253 * Return TRUE when two dictionaries have exactly the same key/values.
5254 */
5255 static int
5256dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005257 dict_T *d1;
5258 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005259 int ic; /* ignore case for strings */
5260{
Bram Moolenaar33570922005-01-25 22:26:29 +00005261 hashitem_T *hi;
5262 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005263 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005264
5265 if (dict_len(d1) != dict_len(d2))
5266 return FALSE;
5267
Bram Moolenaar33570922005-01-25 22:26:29 +00005268 todo = d1->dv_hashtab.ht_used;
5269 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005270 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005271 if (!HASHITEM_EMPTY(hi))
5272 {
5273 item2 = dict_find(d2, hi->hi_key, -1);
5274 if (item2 == NULL)
5275 return FALSE;
5276 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
5277 return FALSE;
5278 --todo;
5279 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005280 }
5281 return TRUE;
5282}
5283
5284/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005285 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005286 * Compares the items just like "==" would compare them, but strings and
5287 * numbers are different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005288 */
5289 static int
5290tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00005291 typval_T *tv1;
5292 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005293 int ic; /* ignore case */
5294{
5295 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005296 char_u *s1, *s2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005297
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005298 if (tv1->v_type != tv2->v_type)
5299 return FALSE;
5300
5301 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005302 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005303 case VAR_LIST:
5304 /* recursive! */
5305 return list_equal(tv1->vval.v_list, tv2->vval.v_list, ic);
5306
5307 case VAR_DICT:
5308 /* recursive! */
5309 return dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic);
5310
5311 case VAR_FUNC:
5312 return (tv1->vval.v_string != NULL
5313 && tv2->vval.v_string != NULL
5314 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
5315
5316 case VAR_NUMBER:
5317 return tv1->vval.v_number == tv2->vval.v_number;
5318
5319 case VAR_STRING:
5320 s1 = get_tv_string_buf(tv1, buf1);
5321 s2 = get_tv_string_buf(tv2, buf2);
5322 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005323 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005324
5325 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005326 return TRUE;
5327}
5328
5329/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005330 * Locate item with index "n" in list "l" and return it.
5331 * A negative index is counted from the end; -1 is the last item.
5332 * Returns NULL when "n" is out of range.
5333 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005334 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005335list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00005336 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005337 long n;
5338{
Bram Moolenaar33570922005-01-25 22:26:29 +00005339 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005340 long idx;
5341
5342 if (l == NULL)
5343 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005344
5345 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005346 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005347 n = l->lv_len + n;
5348
5349 /* Check for index out of range. */
5350 if (n < 0 || n >= l->lv_len)
5351 return NULL;
5352
5353 /* When there is a cached index may start search from there. */
5354 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005356 if (n < l->lv_idx / 2)
5357 {
5358 /* closest to the start of the list */
5359 item = l->lv_first;
5360 idx = 0;
5361 }
5362 else if (n > (l->lv_idx + l->lv_len) / 2)
5363 {
5364 /* closest to the end of the list */
5365 item = l->lv_last;
5366 idx = l->lv_len - 1;
5367 }
5368 else
5369 {
5370 /* closest to the cached index */
5371 item = l->lv_idx_item;
5372 idx = l->lv_idx;
5373 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 }
5375 else
5376 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005377 if (n < l->lv_len / 2)
5378 {
5379 /* closest to the start of the list */
5380 item = l->lv_first;
5381 idx = 0;
5382 }
5383 else
5384 {
5385 /* closest to the end of the list */
5386 item = l->lv_last;
5387 idx = l->lv_len - 1;
5388 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005390
5391 while (n > idx)
5392 {
5393 /* search forward */
5394 item = item->li_next;
5395 ++idx;
5396 }
5397 while (n < idx)
5398 {
5399 /* search backward */
5400 item = item->li_prev;
5401 --idx;
5402 }
5403
5404 /* cache the used index */
5405 l->lv_idx = idx;
5406 l->lv_idx_item = item;
5407
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 return item;
5409}
5410
5411/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005412 * Locate "item" list "l" and return its index.
5413 * Returns -1 when "item" is not in the list.
5414 */
5415 static long
5416list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005417 list_T *l;
5418 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005419{
5420 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00005421 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005422
5423 if (l == NULL)
5424 return -1;
5425 idx = 0;
5426 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
5427 ++idx;
5428 if (li == NULL)
5429 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005430 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005431}
5432
5433/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005434 * Append item "item" to the end of list "l".
5435 */
5436 static void
5437list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005438 list_T *l;
5439 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005440{
5441 if (l->lv_last == NULL)
5442 {
5443 /* empty list */
5444 l->lv_first = item;
5445 l->lv_last = item;
5446 item->li_prev = NULL;
5447 }
5448 else
5449 {
5450 l->lv_last->li_next = item;
5451 item->li_prev = l->lv_last;
5452 l->lv_last = item;
5453 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00005454 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 item->li_next = NULL;
5456}
5457
5458/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005459 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005460 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 */
5462 static int
5463list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005464 list_T *l;
5465 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005466{
Bram Moolenaar05159a02005-02-26 23:04:13 +00005467 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468
Bram Moolenaar05159a02005-02-26 23:04:13 +00005469 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00005471 copy_tv(tv, &li->li_tv);
5472 list_append(l, li);
5473 return OK;
5474}
5475
5476/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00005477 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00005478 * Return FAIL when out of memory.
5479 */
5480 int
5481list_append_dict(list, dict)
5482 list_T *list;
5483 dict_T *dict;
5484{
5485 listitem_T *li = listitem_alloc();
5486
5487 if (li == NULL)
5488 return FAIL;
5489 li->li_tv.v_type = VAR_DICT;
5490 li->li_tv.v_lock = 0;
5491 li->li_tv.vval.v_dict = dict;
5492 list_append(list, li);
5493 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 return OK;
5495}
5496
5497/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005498 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005499 * If "item" is NULL append at the end.
5500 * Return FAIL when out of memory.
5501 */
5502 static int
5503list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005504 list_T *l;
5505 typval_T *tv;
5506 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005507{
Bram Moolenaar33570922005-01-25 22:26:29 +00005508 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005509
5510 if (ni == NULL)
5511 return FAIL;
5512 copy_tv(tv, &ni->li_tv);
5513 if (item == NULL)
5514 /* Append new item at end of list. */
5515 list_append(l, ni);
5516 else
5517 {
5518 /* Insert new item before existing item. */
5519 ni->li_prev = item->li_prev;
5520 ni->li_next = item;
5521 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00005522 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005523 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005524 ++l->lv_idx;
5525 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005526 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00005527 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005528 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005529 l->lv_idx_item = NULL;
5530 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005531 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005532 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005533 }
5534 return OK;
5535}
5536
5537/*
5538 * Extend "l1" with "l2".
5539 * If "bef" is NULL append at the end, otherwise insert before this item.
5540 * Returns FAIL when out of memory.
5541 */
5542 static int
5543list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00005544 list_T *l1;
5545 list_T *l2;
5546 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005547{
Bram Moolenaar33570922005-01-25 22:26:29 +00005548 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005549
5550 for (item = l2->lv_first; item != NULL; item = item->li_next)
5551 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
5552 return FAIL;
5553 return OK;
5554}
5555
5556/*
5557 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
5558 * Return FAIL when out of memory.
5559 */
5560 static int
5561list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005562 list_T *l1;
5563 list_T *l2;
5564 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005565{
Bram Moolenaar33570922005-01-25 22:26:29 +00005566 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005567
5568 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005569 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005570 if (l == NULL)
5571 return FAIL;
5572 tv->v_type = VAR_LIST;
5573 tv->vval.v_list = l;
5574
5575 /* append all items from the second list */
5576 return list_extend(l, l2, NULL);
5577}
5578
5579/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005580 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005582 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005583 * Returns NULL when out of memory.
5584 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005585 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005586list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005587 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005588 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005589 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005590{
Bram Moolenaar33570922005-01-25 22:26:29 +00005591 list_T *copy;
5592 listitem_T *item;
5593 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005594
5595 if (orig == NULL)
5596 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005597
5598 copy = list_alloc();
5599 if (copy != NULL)
5600 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005601 if (copyID != 0)
5602 {
5603 /* Do this before adding the items, because one of the items may
5604 * refer back to this list. */
5605 orig->lv_copyID = copyID;
5606 orig->lv_copylist = copy;
5607 }
5608 for (item = orig->lv_first; item != NULL && !got_int;
5609 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005610 {
5611 ni = listitem_alloc();
5612 if (ni == NULL)
5613 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005614 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005615 {
5616 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5617 {
5618 vim_free(ni);
5619 break;
5620 }
5621 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005622 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005623 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005624 list_append(copy, ni);
5625 }
5626 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005627 if (item != NULL)
5628 {
5629 list_unref(copy);
5630 copy = NULL;
5631 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005632 }
5633
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005634 return copy;
5635}
5636
5637/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005638 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005639 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005640 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005641 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005642list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005643 list_T *l;
5644 listitem_T *item;
5645 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005646{
Bram Moolenaar33570922005-01-25 22:26:29 +00005647 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005648
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005649 /* notify watchers */
5650 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005651 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005652 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005653 list_fix_watch(l, ip);
5654 if (ip == item2)
5655 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005656 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005657
5658 if (item2->li_next == NULL)
5659 l->lv_last = item->li_prev;
5660 else
5661 item2->li_next->li_prev = item->li_prev;
5662 if (item->li_prev == NULL)
5663 l->lv_first = item2->li_next;
5664 else
5665 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005666 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005667}
5668
5669/*
5670 * Return an allocated string with the string representation of a list.
5671 * May return NULL.
5672 */
5673 static char_u *
5674list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005675 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005676{
5677 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005678
5679 if (tv->vval.v_list == NULL)
5680 return NULL;
5681 ga_init2(&ga, (int)sizeof(char), 80);
5682 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005683 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5684 {
5685 vim_free(ga.ga_data);
5686 return NULL;
5687 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005688 ga_append(&ga, ']');
5689 ga_append(&ga, NUL);
5690 return (char_u *)ga.ga_data;
5691}
5692
5693/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005694 * Join list "l" into a string in "*gap", using separator "sep".
5695 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005696 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005697 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005698 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005699list_join(gap, l, sep, echo)
5700 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005701 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005702 char_u *sep;
5703 int echo;
5704{
5705 int first = TRUE;
5706 char_u *tofree;
5707 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005708 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005709 char_u *s;
5710
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005711 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005712 {
5713 if (first)
5714 first = FALSE;
5715 else
5716 ga_concat(gap, sep);
5717
5718 if (echo)
5719 s = echo_string(&item->li_tv, &tofree, numbuf);
5720 else
5721 s = tv2string(&item->li_tv, &tofree, numbuf);
5722 if (s != NULL)
5723 ga_concat(gap, s);
5724 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005725 if (s == NULL)
5726 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005727 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005728 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005729}
5730
5731/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005732 * Garbage collection for lists and dictionaries.
5733 *
5734 * We use reference counts to be able to free most items right away when they
5735 * are no longer used. But for composite items it's possible that it becomes
5736 * unused while the reference count is > 0: When there is a recursive
5737 * reference. Example:
5738 * :let l = [1, 2, 3]
5739 * :let d = {9: l}
5740 * :let l[1] = d
5741 *
5742 * Since this is quite unusual we handle this with garbage collection: every
5743 * once in a while find out which lists and dicts are not referenced from any
5744 * variable.
5745 *
5746 * Here is a good reference text about garbage collection (refers to Python
5747 * but it applies to all reference-counting mechanisms):
5748 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005749 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005750
5751/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005752 * Do garbage collection for lists and dicts.
5753 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005754 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005755 int
5756garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00005757{
5758 dict_T *dd;
5759 list_T *ll;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005760 int copyID = ++current_copyID;
5761 buf_T *buf;
5762 win_T *wp;
5763 int i;
5764 funccall_T *fc;
5765 int did_free = FALSE;
5766
5767 /*
5768 * 1. Go through all accessible variables and mark all lists and dicts
5769 * with copyID.
5770 */
5771 /* script-local variables */
5772 for (i = 1; i <= ga_scripts.ga_len; ++i)
5773 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
5774
5775 /* buffer-local variables */
5776 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5777 set_ref_in_ht(&buf->b_vars.dv_hashtab, copyID);
5778
5779 /* window-local variables */
5780 FOR_ALL_WINDOWS(wp)
5781 set_ref_in_ht(&wp->w_vars.dv_hashtab, copyID);
5782
5783 /* global variables */
5784 set_ref_in_ht(&globvarht, copyID);
5785
5786 /* function-local variables */
5787 for (fc = current_funccal; fc != NULL; fc = fc->caller)
5788 {
5789 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
5790 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
5791 }
5792
5793 /*
5794 * 2. Go through the list of dicts and free items without the copyID.
5795 */
5796 for (dd = first_dict; dd != NULL; )
5797 if (dd->dv_copyID != copyID)
5798 {
5799 dict_free(dd);
5800 did_free = TRUE;
5801
5802 /* restart, next dict may also have been freed */
5803 dd = first_dict;
5804 }
5805 else
5806 dd = dd->dv_used_next;
5807
5808 /*
5809 * 3. Go through the list of lists and free items without the copyID.
5810 */
5811 for (ll = first_list; ll != NULL; )
5812 if (ll->lv_copyID != copyID)
5813 {
5814 list_free(ll);
5815 did_free = TRUE;
5816
5817 /* restart, next dict may also have been freed */
5818 ll = first_list;
5819 }
5820 else
5821 ll = ll->lv_used_next;
5822
5823 return did_free;
5824}
5825
5826/*
5827 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
5828 */
5829 static void
5830set_ref_in_ht(ht, copyID)
5831 hashtab_T *ht;
5832 int copyID;
5833{
5834 int todo;
5835 hashitem_T *hi;
5836
5837 todo = ht->ht_used;
5838 for (hi = ht->ht_array; todo > 0; ++hi)
5839 if (!HASHITEM_EMPTY(hi))
5840 {
5841 --todo;
5842 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
5843 }
5844}
5845
5846/*
5847 * Mark all lists and dicts referenced through list "l" with "copyID".
5848 */
5849 static void
5850set_ref_in_list(l, copyID)
5851 list_T *l;
5852 int copyID;
5853{
5854 listitem_T *li;
5855
5856 for (li = l->lv_first; li != NULL; li = li->li_next)
5857 set_ref_in_item(&li->li_tv, copyID);
5858}
5859
5860/*
5861 * Mark all lists and dicts referenced through typval "tv" with "copyID".
5862 */
5863 static void
5864set_ref_in_item(tv, copyID)
5865 typval_T *tv;
5866 int copyID;
5867{
5868 dict_T *dd;
5869 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005870
5871 switch (tv->v_type)
5872 {
5873 case VAR_DICT:
5874 dd = tv->vval.v_dict;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005875 if (dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005876 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005877 /* Didn't see this dict yet. */
5878 dd->dv_copyID = copyID;
5879 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005880 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005881 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005882
5883 case VAR_LIST:
5884 ll = tv->vval.v_list;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005885 if (ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005886 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005887 /* Didn't see this list yet. */
5888 ll->lv_copyID = copyID;
5889 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00005890 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005891 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005892 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005893 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005894}
5895
5896/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 * Allocate an empty header for a dictionary.
5898 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00005899 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005900dict_alloc()
5901{
Bram Moolenaar33570922005-01-25 22:26:29 +00005902 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005903
Bram Moolenaar33570922005-01-25 22:26:29 +00005904 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005905 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005906 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005907 /* Add the list to the hashtable for garbage collection. */
5908 if (first_dict != NULL)
5909 first_dict->dv_used_prev = d;
5910 d->dv_used_next = first_dict;
5911 d->dv_used_prev = NULL;
5912
Bram Moolenaar33570922005-01-25 22:26:29 +00005913 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005914 d->dv_lock = 0;
5915 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005916 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005917 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005918 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005919}
5920
5921/*
5922 * Unreference a Dictionary: decrement the reference count and free it when it
5923 * becomes zero.
5924 */
5925 static void
5926dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005927 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005928{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005929 if (d != NULL && d->dv_refcount != DEL_REFCOUNT && --d->dv_refcount <= 0)
5930 dict_free(d);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005931}
5932
5933/*
5934 * Free a Dictionary, including all items it contains.
5935 * Ignores the reference count.
5936 */
5937 static void
5938dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005939 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005940{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005941 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005942 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005943 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005944
Bram Moolenaard9fba312005-06-26 22:34:35 +00005945 /* Avoid that recursive reference to the dict frees us again. */
5946 d->dv_refcount = DEL_REFCOUNT;
5947
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005948 /* Remove the dict from the list of dicts for garbage collection. */
5949 if (d->dv_used_prev == NULL)
5950 first_dict = d->dv_used_next;
5951 else
5952 d->dv_used_prev->dv_used_next = d->dv_used_next;
5953 if (d->dv_used_next != NULL)
5954 d->dv_used_next->dv_used_prev = d->dv_used_prev;
5955
5956 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005957 hash_lock(&d->dv_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +00005958 todo = d->dv_hashtab.ht_used;
5959 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005960 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005961 if (!HASHITEM_EMPTY(hi))
5962 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005963 /* Remove the item before deleting it, just in case there is
5964 * something recursive causing trouble. */
5965 di = HI2DI(hi);
5966 hash_remove(&d->dv_hashtab, hi);
5967 dictitem_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005968 --todo;
5969 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005970 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005971 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005972 vim_free(d);
5973}
5974
5975/*
5976 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005977 * The "key" is copied to the new item.
5978 * Note that the value of the item "di_tv" still needs to be initialized!
5979 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005980 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005981 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005982dictitem_alloc(key)
5983 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005984{
Bram Moolenaar33570922005-01-25 22:26:29 +00005985 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005986
Bram Moolenaar33570922005-01-25 22:26:29 +00005987 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005988 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005989 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005990 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005991 di->di_flags = 0;
5992 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005993 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005994}
5995
5996/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005997 * Make a copy of a Dictionary item.
5998 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005999 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00006000dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00006001 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006002{
Bram Moolenaar33570922005-01-25 22:26:29 +00006003 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006004
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006006 if (di != NULL)
6007 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006008 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006009 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006010 copy_tv(&org->di_tv, &di->di_tv);
6011 }
6012 return di;
6013}
6014
6015/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006016 * Remove item "item" from Dictionary "dict" and free it.
6017 */
6018 static void
6019dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006020 dict_T *dict;
6021 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006022{
Bram Moolenaar33570922005-01-25 22:26:29 +00006023 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006024
Bram Moolenaar33570922005-01-25 22:26:29 +00006025 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006026 if (HASHITEM_EMPTY(hi))
6027 EMSG2(_(e_intern2), "dictitem_remove()");
6028 else
Bram Moolenaar33570922005-01-25 22:26:29 +00006029 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006030 dictitem_free(item);
6031}
6032
6033/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006034 * Free a dict item. Also clears the value.
6035 */
6036 static void
6037dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006038 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006039{
Bram Moolenaar8c711452005-01-14 21:53:12 +00006040 clear_tv(&item->di_tv);
6041 vim_free(item);
6042}
6043
6044/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006045 * Make a copy of dict "d". Shallow if "deep" is FALSE.
6046 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006047 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00006048 * Returns NULL when out of memory.
6049 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006050 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006051dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006052 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006053 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006054 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006055{
Bram Moolenaar33570922005-01-25 22:26:29 +00006056 dict_T *copy;
6057 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006058 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00006059 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006060
6061 if (orig == NULL)
6062 return NULL;
6063
6064 copy = dict_alloc();
6065 if (copy != NULL)
6066 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006067 if (copyID != 0)
6068 {
6069 orig->dv_copyID = copyID;
6070 orig->dv_copydict = copy;
6071 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006072 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006073 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006074 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006075 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00006076 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006077 --todo;
6078
6079 di = dictitem_alloc(hi->hi_key);
6080 if (di == NULL)
6081 break;
6082 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006083 {
6084 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
6085 copyID) == FAIL)
6086 {
6087 vim_free(di);
6088 break;
6089 }
6090 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006091 else
6092 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
6093 if (dict_add(copy, di) == FAIL)
6094 {
6095 dictitem_free(di);
6096 break;
6097 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006098 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006099 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006100
Bram Moolenaare9a41262005-01-15 22:18:47 +00006101 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006102 if (todo > 0)
6103 {
6104 dict_unref(copy);
6105 copy = NULL;
6106 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006107 }
6108
6109 return copy;
6110}
6111
6112/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006113 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006114 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006115 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006116 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00006117dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006118 dict_T *d;
6119 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006120{
Bram Moolenaar33570922005-01-25 22:26:29 +00006121 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006122}
6123
Bram Moolenaar8c711452005-01-14 21:53:12 +00006124/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00006125 * Add a number or string entry to dictionary "d".
6126 * When "str" is NULL use number "nr", otherwise use "str".
6127 * Returns FAIL when out of memory and when key already exists.
6128 */
6129 int
6130dict_add_nr_str(d, key, nr, str)
6131 dict_T *d;
6132 char *key;
6133 long nr;
6134 char_u *str;
6135{
6136 dictitem_T *item;
6137
6138 item = dictitem_alloc((char_u *)key);
6139 if (item == NULL)
6140 return FAIL;
6141 item->di_tv.v_lock = 0;
6142 if (str == NULL)
6143 {
6144 item->di_tv.v_type = VAR_NUMBER;
6145 item->di_tv.vval.v_number = nr;
6146 }
6147 else
6148 {
6149 item->di_tv.v_type = VAR_STRING;
6150 item->di_tv.vval.v_string = vim_strsave(str);
6151 }
6152 if (dict_add(d, item) == FAIL)
6153 {
6154 dictitem_free(item);
6155 return FAIL;
6156 }
6157 return OK;
6158}
6159
6160/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006161 * Get the number of items in a Dictionary.
6162 */
6163 static long
6164dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00006165 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006166{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006167 if (d == NULL)
6168 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00006169 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006170}
6171
6172/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006173 * Find item "key[len]" in Dictionary "d".
6174 * If "len" is negative use strlen(key).
6175 * Returns NULL when not found.
6176 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006177 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006178dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00006179 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006180 char_u *key;
6181 int len;
6182{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006183#define AKEYLEN 200
6184 char_u buf[AKEYLEN];
6185 char_u *akey;
6186 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006187 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006188
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006189 if (len < 0)
6190 akey = key;
6191 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006192 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006193 tofree = akey = vim_strnsave(key, len);
6194 if (akey == NULL)
6195 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006196 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006197 else
6198 {
6199 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00006200 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006201 akey = buf;
6202 }
6203
Bram Moolenaar33570922005-01-25 22:26:29 +00006204 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006205 vim_free(tofree);
6206 if (HASHITEM_EMPTY(hi))
6207 return NULL;
6208 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006209}
6210
6211/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006212 * Get a string item from a dictionary in allocated memory.
6213 * Returns NULL if the entry doesn't exist or out of memory.
6214 */
6215 char_u *
6216get_dict_string(d, key)
6217 dict_T *d;
6218 char_u *key;
6219{
6220 dictitem_T *di;
6221
6222 di = dict_find(d, key, -1);
6223 if (di == NULL)
6224 return NULL;
6225 return vim_strsave(get_tv_string(&di->di_tv));
6226}
6227
6228/*
6229 * Get a number item from a dictionary.
6230 * Returns 0 if the entry doesn't exist or out of memory.
6231 */
6232 long
6233get_dict_number(d, key)
6234 dict_T *d;
6235 char_u *key;
6236{
6237 dictitem_T *di;
6238
6239 di = dict_find(d, key, -1);
6240 if (di == NULL)
6241 return 0;
6242 return get_tv_number(&di->di_tv);
6243}
6244
6245/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006246 * Return an allocated string with the string representation of a Dictionary.
6247 * May return NULL.
6248 */
6249 static char_u *
6250dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006251 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006252{
6253 garray_T ga;
6254 int first = TRUE;
6255 char_u *tofree;
6256 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00006257 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006258 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00006259 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006260 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006261
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006262 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006263 return NULL;
6264 ga_init2(&ga, (int)sizeof(char), 80);
6265 ga_append(&ga, '{');
6266
Bram Moolenaar33570922005-01-25 22:26:29 +00006267 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006268 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006269 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006270 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00006271 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006272 --todo;
6273
6274 if (first)
6275 first = FALSE;
6276 else
6277 ga_concat(&ga, (char_u *)", ");
6278
6279 tofree = string_quote(hi->hi_key, FALSE);
6280 if (tofree != NULL)
6281 {
6282 ga_concat(&ga, tofree);
6283 vim_free(tofree);
6284 }
6285 ga_concat(&ga, (char_u *)": ");
6286 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
6287 if (s != NULL)
6288 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006289 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006290 if (s == NULL)
6291 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006292 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006293 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006294 if (todo > 0)
6295 {
6296 vim_free(ga.ga_data);
6297 return NULL;
6298 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006299
6300 ga_append(&ga, '}');
6301 ga_append(&ga, NUL);
6302 return (char_u *)ga.ga_data;
6303}
6304
6305/*
6306 * Allocate a variable for a Dictionary and fill it from "*arg".
6307 * Return OK or FAIL. Returns NOTDONE for {expr}.
6308 */
6309 static int
6310get_dict_tv(arg, rettv, evaluate)
6311 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006312 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006313 int evaluate;
6314{
Bram Moolenaar33570922005-01-25 22:26:29 +00006315 dict_T *d = NULL;
6316 typval_T tvkey;
6317 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006318 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00006319 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006320 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006321 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00006322
6323 /*
6324 * First check if it's not a curly-braces thing: {expr}.
6325 * Must do this without evaluating, otherwise a function may be called
6326 * twice. Unfortunately this means we need to call eval1() twice for the
6327 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006328 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006329 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006330 if (*start != '}')
6331 {
6332 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
6333 return FAIL;
6334 if (*start == '}')
6335 return NOTDONE;
6336 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006337
6338 if (evaluate)
6339 {
6340 d = dict_alloc();
6341 if (d == NULL)
6342 return FAIL;
6343 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006344 tvkey.v_type = VAR_UNKNOWN;
6345 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006346
6347 *arg = skipwhite(*arg + 1);
6348 while (**arg != '}' && **arg != NUL)
6349 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006350 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006351 goto failret;
6352 if (**arg != ':')
6353 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006354 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006355 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006356 goto failret;
6357 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006358 key = get_tv_string_buf_chk(&tvkey, buf);
6359 if (key == NULL || *key == NUL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006360 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006361 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
6362 if (key != NULL)
6363 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006364 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006365 goto failret;
6366 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006367
6368 *arg = skipwhite(*arg + 1);
6369 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
6370 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006371 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006372 goto failret;
6373 }
6374 if (evaluate)
6375 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006376 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006377 if (item != NULL)
6378 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00006379 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006380 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006381 clear_tv(&tv);
6382 goto failret;
6383 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006384 item = dictitem_alloc(key);
6385 clear_tv(&tvkey);
6386 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00006387 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006388 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006389 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006390 if (dict_add(d, item) == FAIL)
6391 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006392 }
6393 }
6394
6395 if (**arg == '}')
6396 break;
6397 if (**arg != ',')
6398 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006399 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006400 goto failret;
6401 }
6402 *arg = skipwhite(*arg + 1);
6403 }
6404
6405 if (**arg != '}')
6406 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006407 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006408failret:
6409 if (evaluate)
6410 dict_free(d);
6411 return FAIL;
6412 }
6413
6414 *arg = skipwhite(*arg + 1);
6415 if (evaluate)
6416 {
6417 rettv->v_type = VAR_DICT;
6418 rettv->vval.v_dict = d;
6419 ++d->dv_refcount;
6420 }
6421
6422 return OK;
6423}
6424
Bram Moolenaar8c711452005-01-14 21:53:12 +00006425/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426 * Return a string with the string representation of a variable.
6427 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006428 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006429 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006430 * May return NULL;
6431 */
6432 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006433echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006434 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006435 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006436 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006437{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006438 static int recurse = 0;
6439 char_u *r = NULL;
6440
Bram Moolenaar33570922005-01-25 22:26:29 +00006441 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006442 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006443 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00006444 *tofree = NULL;
6445 return NULL;
6446 }
6447 ++recurse;
6448
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006449 switch (tv->v_type)
6450 {
6451 case VAR_FUNC:
6452 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006453 r = tv->vval.v_string;
6454 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006455 case VAR_LIST:
6456 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006457 r = *tofree;
6458 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006459 case VAR_DICT:
6460 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006461 r = *tofree;
6462 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006463 case VAR_STRING:
6464 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006465 *tofree = NULL;
6466 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006467 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006468 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006469 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00006470 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006471 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006472
6473 --recurse;
6474 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006475}
6476
6477/*
6478 * Return a string with the string representation of a variable.
6479 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6480 * "numbuf" is used for a number.
6481 * Puts quotes around strings, so that they can be parsed back by eval().
6482 * May return NULL;
6483 */
6484 static char_u *
6485tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00006486 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006487 char_u **tofree;
6488 char_u *numbuf;
6489{
6490 switch (tv->v_type)
6491 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006492 case VAR_FUNC:
6493 *tofree = string_quote(tv->vval.v_string, TRUE);
6494 return *tofree;
6495 case VAR_STRING:
6496 *tofree = string_quote(tv->vval.v_string, FALSE);
6497 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006498 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006499 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00006500 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006501 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006502 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006503 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006504 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006505 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006506}
6507
6508/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006509 * Return string "str" in ' quotes, doubling ' characters.
6510 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006511 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006512 */
6513 static char_u *
6514string_quote(str, function)
6515 char_u *str;
6516 int function;
6517{
Bram Moolenaar33570922005-01-25 22:26:29 +00006518 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006519 char_u *p, *r, *s;
6520
Bram Moolenaar33570922005-01-25 22:26:29 +00006521 len = (function ? 13 : 3);
6522 if (str != NULL)
6523 {
6524 len += STRLEN(str);
6525 for (p = str; *p != NUL; mb_ptr_adv(p))
6526 if (*p == '\'')
6527 ++len;
6528 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006529 s = r = alloc(len);
6530 if (r != NULL)
6531 {
6532 if (function)
6533 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006534 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006535 r += 10;
6536 }
6537 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006538 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006539 if (str != NULL)
6540 for (p = str; *p != NUL; )
6541 {
6542 if (*p == '\'')
6543 *r++ = '\'';
6544 MB_COPY_CHAR(p, r);
6545 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006546 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006547 if (function)
6548 *r++ = ')';
6549 *r++ = NUL;
6550 }
6551 return s;
6552}
6553
6554/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 * Get the value of an environment variable.
6556 * "arg" is pointing to the '$'. It is advanced to after the name.
6557 * If the environment variable was not set, silently assume it is empty.
6558 * Always return OK.
6559 */
6560 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006561get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 int evaluate;
6565{
6566 char_u *string = NULL;
6567 int len;
6568 int cc;
6569 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006570 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571
6572 ++*arg;
6573 name = *arg;
6574 len = get_env_len(arg);
6575 if (evaluate)
6576 {
6577 if (len != 0)
6578 {
6579 cc = name[len];
6580 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006581 /* first try vim_getenv(), fast for normal environment vars */
6582 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006584 {
6585 if (!mustfree)
6586 string = vim_strsave(string);
6587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 else
6589 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00006590 if (mustfree)
6591 vim_free(string);
6592
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 /* next try expanding things like $VIM and ${HOME} */
6594 string = expand_env_save(name - 1);
6595 if (string != NULL && *string == '$')
6596 {
6597 vim_free(string);
6598 string = NULL;
6599 }
6600 }
6601 name[len] = cc;
6602 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006603 rettv->v_type = VAR_STRING;
6604 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 }
6606
6607 return OK;
6608}
6609
6610/*
6611 * Array with names and number of arguments of all internal functions
6612 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
6613 */
6614static struct fst
6615{
6616 char *f_name; /* function name */
6617 char f_min_argc; /* minimal number of arguments */
6618 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00006619 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006620 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621} functions[] =
6622{
Bram Moolenaar0d660222005-01-07 21:51:51 +00006623 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 {"append", 2, 2, f_append},
6625 {"argc", 0, 0, f_argc},
6626 {"argidx", 0, 0, f_argidx},
6627 {"argv", 1, 1, f_argv},
6628 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006629 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 {"bufexists", 1, 1, f_bufexists},
6631 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
6632 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
6633 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
6634 {"buflisted", 1, 1, f_buflisted},
6635 {"bufloaded", 1, 1, f_bufloaded},
6636 {"bufname", 1, 1, f_bufname},
6637 {"bufnr", 1, 1, f_bufnr},
6638 {"bufwinnr", 1, 1, f_bufwinnr},
6639 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006640 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006641 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 {"char2nr", 1, 1, f_char2nr},
6643 {"cindent", 1, 1, f_cindent},
6644 {"col", 1, 1, f_col},
6645 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006646 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006647 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 {"cscope_connection",0,3, f_cscope_connection},
6649 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006650 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651 {"delete", 1, 1, f_delete},
6652 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00006653 {"diff_filler", 1, 1, f_diff_filler},
6654 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006655 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006657 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 {"eventhandler", 0, 0, f_eventhandler},
6659 {"executable", 1, 1, f_executable},
6660 {"exists", 1, 1, f_exists},
6661 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006662 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
6664 {"filereadable", 1, 1, f_filereadable},
6665 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006666 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006667 {"finddir", 1, 3, f_finddir},
6668 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 {"fnamemodify", 2, 2, f_fnamemodify},
6670 {"foldclosed", 1, 1, f_foldclosed},
6671 {"foldclosedend", 1, 1, f_foldclosedend},
6672 {"foldlevel", 1, 1, f_foldlevel},
6673 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006674 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006676 {"function", 1, 1, f_function},
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006677 {"garbagecollect", 0, 0, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006678 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 {"getbufvar", 2, 2, f_getbufvar},
6680 {"getchar", 0, 1, f_getchar},
6681 {"getcharmod", 0, 0, f_getcharmod},
6682 {"getcmdline", 0, 0, f_getcmdline},
6683 {"getcmdpos", 0, 0, f_getcmdpos},
6684 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006685 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006686 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 {"getfsize", 1, 1, f_getfsize},
6688 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006689 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006690 {"getline", 1, 2, f_getline},
Bram Moolenaar2641f772005-03-25 21:58:17 +00006691 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006692 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 {"getregtype", 0, 1, f_getregtype},
6694 {"getwinposx", 0, 0, f_getwinposx},
6695 {"getwinposy", 0, 0, f_getwinposy},
6696 {"getwinvar", 2, 2, f_getwinvar},
6697 {"glob", 1, 1, f_glob},
6698 {"globpath", 2, 2, f_globpath},
6699 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00006700 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 {"hasmapto", 1, 2, f_hasmapto},
6702 {"highlightID", 1, 1, f_hlID}, /* obsolete */
6703 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
6704 {"histadd", 2, 2, f_histadd},
6705 {"histdel", 1, 2, f_histdel},
6706 {"histget", 1, 2, f_histget},
6707 {"histnr", 1, 1, f_histnr},
6708 {"hlID", 1, 1, f_hlID},
6709 {"hlexists", 1, 1, f_hlexists},
6710 {"hostname", 0, 0, f_hostname},
6711 {"iconv", 3, 3, f_iconv},
6712 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006713 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 {"input", 1, 2, f_input},
6715 {"inputdialog", 1, 3, f_inputdialog},
6716 {"inputrestore", 0, 0, f_inputrestore},
6717 {"inputsave", 0, 0, f_inputsave},
6718 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006719 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006721 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006722 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006723 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006724 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006726 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 {"libcall", 3, 3, f_libcall},
6728 {"libcallnr", 3, 3, f_libcallnr},
6729 {"line", 1, 1, f_line},
6730 {"line2byte", 1, 1, f_line2byte},
6731 {"lispindent", 1, 1, f_lispindent},
6732 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006733 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734 {"maparg", 1, 2, f_maparg},
6735 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006736 {"match", 2, 4, f_match},
6737 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006738 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006739 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006740 {"max", 1, 1, f_max},
6741 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006742#ifdef vim_mkdir
6743 {"mkdir", 1, 3, f_mkdir},
6744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 {"mode", 0, 0, f_mode},
6746 {"nextnonblank", 1, 1, f_nextnonblank},
6747 {"nr2char", 1, 1, f_nr2char},
6748 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006749 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006750 {"readfile", 1, 3, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 {"remote_expr", 2, 3, f_remote_expr},
6752 {"remote_foreground", 1, 1, f_remote_foreground},
6753 {"remote_peek", 1, 2, f_remote_peek},
6754 {"remote_read", 1, 1, f_remote_read},
6755 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006756 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006758 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006760 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 {"search", 1, 2, f_search},
6762 {"searchpair", 3, 5, f_searchpair},
6763 {"server2client", 2, 2, f_server2client},
6764 {"serverlist", 0, 0, f_serverlist},
6765 {"setbufvar", 3, 3, f_setbufvar},
6766 {"setcmdpos", 1, 1, f_setcmdpos},
6767 {"setline", 2, 2, f_setline},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00006768 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 {"setreg", 2, 3, f_setreg},
6770 {"setwinvar", 3, 3, f_setwinvar},
6771 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00006772 {"sort", 1, 2, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006773 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaard857f0e2005-06-21 22:37:39 +00006774 {"spellbadword", 0, 0, f_spellbadword},
6775 {"spellsuggest", 1, 2, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00006776 {"split", 1, 3, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777#ifdef HAVE_STRFTIME
6778 {"strftime", 1, 2, f_strftime},
6779#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00006780 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006781 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 {"strlen", 1, 1, f_strlen},
6783 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00006784 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 {"strtrans", 1, 1, f_strtrans},
6786 {"submatch", 1, 1, f_submatch},
6787 {"substitute", 4, 4, f_substitute},
6788 {"synID", 3, 3, f_synID},
6789 {"synIDattr", 2, 3, f_synIDattr},
6790 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006791 {"system", 1, 2, f_system},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006792 {"taglist", 1, 1, f_taglist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 {"tempname", 0, 0, f_tempname},
6794 {"tolower", 1, 1, f_tolower},
6795 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00006796 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00006798 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 {"virtcol", 1, 1, f_virtcol},
6800 {"visualmode", 0, 1, f_visualmode},
6801 {"winbufnr", 1, 1, f_winbufnr},
6802 {"wincol", 0, 0, f_wincol},
6803 {"winheight", 1, 1, f_winheight},
6804 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006805 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 {"winrestcmd", 0, 0, f_winrestcmd},
6807 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006808 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809};
6810
6811#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6812
6813/*
6814 * Function given to ExpandGeneric() to obtain the list of internal
6815 * or user defined function names.
6816 */
6817 char_u *
6818get_function_name(xp, idx)
6819 expand_T *xp;
6820 int idx;
6821{
6822 static int intidx = -1;
6823 char_u *name;
6824
6825 if (idx == 0)
6826 intidx = -1;
6827 if (intidx < 0)
6828 {
6829 name = get_user_func_name(xp, idx);
6830 if (name != NULL)
6831 return name;
6832 }
6833 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
6834 {
6835 STRCPY(IObuff, functions[intidx].f_name);
6836 STRCAT(IObuff, "(");
6837 if (functions[intidx].f_max_argc == 0)
6838 STRCAT(IObuff, ")");
6839 return IObuff;
6840 }
6841
6842 return NULL;
6843}
6844
6845/*
6846 * Function given to ExpandGeneric() to obtain the list of internal or
6847 * user defined variable or function names.
6848 */
6849/*ARGSUSED*/
6850 char_u *
6851get_expr_name(xp, idx)
6852 expand_T *xp;
6853 int idx;
6854{
6855 static int intidx = -1;
6856 char_u *name;
6857
6858 if (idx == 0)
6859 intidx = -1;
6860 if (intidx < 0)
6861 {
6862 name = get_function_name(xp, idx);
6863 if (name != NULL)
6864 return name;
6865 }
6866 return get_user_var_name(xp, ++intidx);
6867}
6868
6869#endif /* FEAT_CMDL_COMPL */
6870
6871/*
6872 * Find internal function in table above.
6873 * Return index, or -1 if not found
6874 */
6875 static int
6876find_internal_func(name)
6877 char_u *name; /* name of the function */
6878{
6879 int first = 0;
6880 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6881 int cmp;
6882 int x;
6883
6884 /*
6885 * Find the function name in the table. Binary search.
6886 */
6887 while (first <= last)
6888 {
6889 x = first + ((unsigned)(last - first) >> 1);
6890 cmp = STRCMP(name, functions[x].f_name);
6891 if (cmp < 0)
6892 last = x - 1;
6893 else if (cmp > 0)
6894 first = x + 1;
6895 else
6896 return x;
6897 }
6898 return -1;
6899}
6900
6901/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006902 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6903 * name it contains, otherwise return "name".
6904 */
6905 static char_u *
6906deref_func_name(name, lenp)
6907 char_u *name;
6908 int *lenp;
6909{
Bram Moolenaar33570922005-01-25 22:26:29 +00006910 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006911 int cc;
6912
6913 cc = name[*lenp];
6914 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006915 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006916 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006917 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006918 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006919 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006920 {
6921 *lenp = 0;
6922 return (char_u *)""; /* just in case */
6923 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006924 *lenp = STRLEN(v->di_tv.vval.v_string);
6925 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006926 }
6927
6928 return name;
6929}
6930
6931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 * Allocate a variable for the result of a function.
6933 * Return OK or FAIL.
6934 */
6935 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006936get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6937 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 char_u *name; /* name of the function */
6939 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006940 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 char_u **arg; /* argument, pointing to the '(' */
6942 linenr_T firstline; /* first line of range */
6943 linenr_T lastline; /* last line of range */
6944 int *doesrange; /* return: function handled range */
6945 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006946 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947{
6948 char_u *argp;
6949 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006950 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 int argcount = 0; /* number of arguments found */
6952
6953 /*
6954 * Get the arguments.
6955 */
6956 argp = *arg;
6957 while (argcount < MAX_FUNC_ARGS)
6958 {
6959 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6960 if (*argp == ')' || *argp == ',' || *argp == NUL)
6961 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6963 {
6964 ret = FAIL;
6965 break;
6966 }
6967 ++argcount;
6968 if (*argp != ',')
6969 break;
6970 }
6971 if (*argp == ')')
6972 ++argp;
6973 else
6974 ret = FAIL;
6975
6976 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006977 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006978 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00006980 {
6981 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006982 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006983 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006984 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986
6987 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006988 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989
6990 *arg = skipwhite(argp);
6991 return ret;
6992}
6993
6994
6995/*
6996 * Call a function with its resolved parameters
6997 * Return OK or FAIL.
6998 */
6999 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007000call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007001 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 char_u *name; /* name of the function */
7003 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007004 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00007006 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 linenr_T firstline; /* first line of range */
7008 linenr_T lastline; /* last line of range */
7009 int *doesrange; /* return: function handled range */
7010 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00007011 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012{
7013 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014#define ERROR_UNKNOWN 0
7015#define ERROR_TOOMANY 1
7016#define ERROR_TOOFEW 2
7017#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00007018#define ERROR_DICT 4
7019#define ERROR_NONE 5
7020#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 int error = ERROR_NONE;
7022 int i;
7023 int llen;
7024 ufunc_T *fp;
7025 int cc;
7026#define FLEN_FIXED 40
7027 char_u fname_buf[FLEN_FIXED + 1];
7028 char_u *fname;
7029
7030 /*
7031 * In a script change <SID>name() and s:name() to K_SNR 123_name().
7032 * Change <SNR>123_name() to K_SNR 123_name().
7033 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
7034 */
7035 cc = name[len];
7036 name[len] = NUL;
7037 llen = eval_fname_script(name);
7038 if (llen > 0)
7039 {
7040 fname_buf[0] = K_SPECIAL;
7041 fname_buf[1] = KS_EXTRA;
7042 fname_buf[2] = (int)KE_SNR;
7043 i = 3;
7044 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
7045 {
7046 if (current_SID <= 0)
7047 error = ERROR_SCRIPT;
7048 else
7049 {
7050 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
7051 i = (int)STRLEN(fname_buf);
7052 }
7053 }
7054 if (i + STRLEN(name + llen) < FLEN_FIXED)
7055 {
7056 STRCPY(fname_buf + i, name + llen);
7057 fname = fname_buf;
7058 }
7059 else
7060 {
7061 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
7062 if (fname == NULL)
7063 error = ERROR_OTHER;
7064 else
7065 {
7066 mch_memmove(fname, fname_buf, (size_t)i);
7067 STRCPY(fname + i, name + llen);
7068 }
7069 }
7070 }
7071 else
7072 fname = name;
7073
7074 *doesrange = FALSE;
7075
7076
7077 /* execute the function if no errors detected and executing */
7078 if (evaluate && error == ERROR_NONE)
7079 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007080 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 error = ERROR_UNKNOWN;
7082
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007083 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 {
7085 /*
7086 * User defined function.
7087 */
7088 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007089
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007091 /* Trigger FuncUndefined event, may load the function. */
7092 if (fp == NULL
7093 && apply_autocmds(EVENT_FUNCUNDEFINED,
7094 fname, fname, TRUE, NULL)
7095 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007097 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 fp = find_func(fname);
7099 }
7100#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007101 /* Try loading a package. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007102 if (fp == NULL && script_autoload(fname) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00007103 {
7104 /* loaded a package, search for the function again */
7105 fp = find_func(fname);
7106 }
7107
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 if (fp != NULL)
7109 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007110 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007112 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007114 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007116 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007117 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 else
7119 {
7120 /*
7121 * Call the user function.
7122 * Save and restore search patterns, script variables and
7123 * redo buffer.
7124 */
7125 save_search_patterns();
7126 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007127 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007128 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007129 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007130 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
7131 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
7132 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007133 /* Function was unreferenced while being used, free it
7134 * now. */
7135 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 restoreRedobuff();
7137 restore_search_patterns();
7138 error = ERROR_NONE;
7139 }
7140 }
7141 }
7142 else
7143 {
7144 /*
7145 * Find the function name in the table, call its implementation.
7146 */
7147 i = find_internal_func(fname);
7148 if (i >= 0)
7149 {
7150 if (argcount < functions[i].f_min_argc)
7151 error = ERROR_TOOFEW;
7152 else if (argcount > functions[i].f_max_argc)
7153 error = ERROR_TOOMANY;
7154 else
7155 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007156 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007157 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 error = ERROR_NONE;
7159 }
7160 }
7161 }
7162 /*
7163 * The function call (or "FuncUndefined" autocommand sequence) might
7164 * have been aborted by an error, an interrupt, or an explicitly thrown
7165 * exception that has not been caught so far. This situation can be
7166 * tested for by calling aborting(). For an error in an internal
7167 * function or for the "E132" error in call_user_func(), however, the
7168 * throw point at which the "force_abort" flag (temporarily reset by
7169 * emsg()) is normally updated has not been reached yet. We need to
7170 * update that flag first to make aborting() reliable.
7171 */
7172 update_force_abort();
7173 }
7174 if (error == ERROR_NONE)
7175 ret = OK;
7176
7177 /*
7178 * Report an error unless the argument evaluation or function call has been
7179 * cancelled due to an aborting error, an interrupt, or an exception.
7180 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007181 if (!aborting())
7182 {
7183 switch (error)
7184 {
7185 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007186 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007187 break;
7188 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007189 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007190 break;
7191 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007192 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007193 name);
7194 break;
7195 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007196 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00007197 name);
7198 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007199 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007200 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00007201 name);
7202 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007203 }
7204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205
7206 name[len] = cc;
7207 if (fname != name && fname != fname_buf)
7208 vim_free(fname);
7209
7210 return ret;
7211}
7212
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007213/*
7214 * Give an error message with a function name. Handle <SNR> things.
7215 */
7216 static void
7217emsg_funcname(msg, name)
7218 char *msg;
7219 char_u *name;
7220{
7221 char_u *p;
7222
7223 if (*name == K_SPECIAL)
7224 p = concat_str((char_u *)"<SNR>", name + 3);
7225 else
7226 p = name;
7227 EMSG2(_(msg), p);
7228 if (p != name)
7229 vim_free(p);
7230}
7231
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232/*********************************************
7233 * Implementation of the built-in functions
7234 */
7235
7236/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007237 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 */
7239 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007240f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007241 typval_T *argvars;
7242 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243{
Bram Moolenaar33570922005-01-25 22:26:29 +00007244 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007246 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007247 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007249 if ((l = argvars[0].vval.v_list) != NULL
7250 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
7251 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007252 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007253 }
7254 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00007255 EMSG(_(e_listreq));
7256}
7257
7258/*
7259 * "append(lnum, string/list)" function
7260 */
7261 static void
7262f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007263 typval_T *argvars;
7264 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007265{
7266 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007267 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00007268 list_T *l = NULL;
7269 listitem_T *li = NULL;
7270 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007271 long added = 0;
7272
Bram Moolenaar0d660222005-01-07 21:51:51 +00007273 lnum = get_tv_lnum(argvars);
7274 if (lnum >= 0
7275 && lnum <= curbuf->b_ml.ml_line_count
7276 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007277 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007278 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007279 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00007280 l = argvars[1].vval.v_list;
7281 if (l == NULL)
7282 return;
7283 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007284 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007285 rettv->vval.v_number = 0; /* Default: Success */
Bram Moolenaar0d660222005-01-07 21:51:51 +00007286 for (;;)
7287 {
7288 if (l == NULL)
7289 tv = &argvars[1]; /* append a string */
7290 else if (li == NULL)
7291 break; /* end of list */
7292 else
7293 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007294 line = get_tv_string_chk(tv);
7295 if (line == NULL) /* type error */
7296 {
7297 rettv->vval.v_number = 1; /* Failed */
7298 break;
7299 }
7300 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00007301 ++added;
7302 if (l == NULL)
7303 break;
7304 li = li->li_next;
7305 }
7306
7307 appended_lines_mark(lnum, added);
7308 if (curwin->w_cursor.lnum > lnum)
7309 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007311 else
7312 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313}
7314
7315/*
7316 * "argc()" function
7317 */
7318/* ARGSUSED */
7319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007320f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 typval_T *argvars;
7322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007324 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325}
7326
7327/*
7328 * "argidx()" function
7329 */
7330/* ARGSUSED */
7331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007332f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007333 typval_T *argvars;
7334 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007336 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337}
7338
7339/*
7340 * "argv(nr)" function
7341 */
7342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007343f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007344 typval_T *argvars;
7345 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346{
7347 int idx;
7348
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007349 idx = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007351 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007353 rettv->vval.v_string = NULL;
7354 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355}
7356
7357/*
7358 * "browse(save, title, initdir, default)" function
7359 */
7360/* ARGSUSED */
7361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007362f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007363 typval_T *argvars;
7364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365{
7366#ifdef FEAT_BROWSE
7367 int save;
7368 char_u *title;
7369 char_u *initdir;
7370 char_u *defname;
7371 char_u buf[NUMBUFLEN];
7372 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007373 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007375 save = get_tv_number_chk(&argvars[0], &error);
7376 title = get_tv_string_chk(&argvars[1]);
7377 initdir = get_tv_string_buf_chk(&argvars[2], buf);
7378 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007380 if (error || title == NULL || initdir == NULL || defname == NULL)
7381 rettv->vval.v_string = NULL;
7382 else
7383 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007384 do_browse(save ? BROWSE_SAVE : 0,
7385 title, defname, NULL, initdir, NULL, curbuf);
7386#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007387 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007388#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007389 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007390}
7391
7392/*
7393 * "browsedir(title, initdir)" function
7394 */
7395/* ARGSUSED */
7396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007397f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007398 typval_T *argvars;
7399 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007400{
7401#ifdef FEAT_BROWSE
7402 char_u *title;
7403 char_u *initdir;
7404 char_u buf[NUMBUFLEN];
7405
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007406 title = get_tv_string_chk(&argvars[0]);
7407 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007408
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007409 if (title == NULL || initdir == NULL)
7410 rettv->vval.v_string = NULL;
7411 else
7412 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007413 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007415 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007417 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418}
7419
Bram Moolenaar33570922005-01-25 22:26:29 +00007420static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007421
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422/*
7423 * Find a buffer by number or exact name.
7424 */
7425 static buf_T *
7426find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00007427 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
7429 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007431 if (avar->v_type == VAR_NUMBER)
7432 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007433 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007435 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007436 if (buf == NULL)
7437 {
7438 /* No full path name match, try a match with a URL or a "nofile"
7439 * buffer, these don't use the full path. */
7440 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7441 if (buf->b_fname != NULL
7442 && (path_with_url(buf->b_fname)
7443#ifdef FEAT_QUICKFIX
7444 || bt_nofile(buf)
7445#endif
7446 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007447 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00007448 break;
7449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 }
7451 return buf;
7452}
7453
7454/*
7455 * "bufexists(expr)" function
7456 */
7457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007458f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007459 typval_T *argvars;
7460 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007462 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463}
7464
7465/*
7466 * "buflisted(expr)" function
7467 */
7468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007469f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007470 typval_T *argvars;
7471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472{
7473 buf_T *buf;
7474
7475 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007476 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477}
7478
7479/*
7480 * "bufloaded(expr)" function
7481 */
7482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007483f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007484 typval_T *argvars;
7485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486{
7487 buf_T *buf;
7488
7489 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007490 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491}
7492
Bram Moolenaar33570922005-01-25 22:26:29 +00007493static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00007494
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495/*
7496 * Get buffer by number or pattern.
7497 */
7498 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007499get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007500 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007502 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 int save_magic;
7504 char_u *save_cpo;
7505 buf_T *buf;
7506
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007507 if (tv->v_type == VAR_NUMBER)
7508 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00007509 if (tv->v_type != VAR_STRING)
7510 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 if (name == NULL || *name == NUL)
7512 return curbuf;
7513 if (name[0] == '$' && name[1] == NUL)
7514 return lastbuf;
7515
7516 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
7517 save_magic = p_magic;
7518 p_magic = TRUE;
7519 save_cpo = p_cpo;
7520 p_cpo = (char_u *)"";
7521
7522 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
7523 TRUE, FALSE));
7524
7525 p_magic = save_magic;
7526 p_cpo = save_cpo;
7527
7528 /* If not found, try expanding the name, like done for bufexists(). */
7529 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007530 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531
7532 return buf;
7533}
7534
7535/*
7536 * "bufname(expr)" function
7537 */
7538 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007539f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007540 typval_T *argvars;
7541 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542{
7543 buf_T *buf;
7544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007545 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007547 buf = get_buf_tv(&argvars[0]);
7548 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007550 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007552 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 --emsg_off;
7554}
7555
7556/*
7557 * "bufnr(expr)" function
7558 */
7559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007560f_bufnr(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
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007566 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007568 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007570 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007572 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 --emsg_off;
7574}
7575
7576/*
7577 * "bufwinnr(nr)" function
7578 */
7579 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007580f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007581 typval_T *argvars;
7582 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583{
7584#ifdef FEAT_WINDOWS
7585 win_T *wp;
7586 int winnr = 0;
7587#endif
7588 buf_T *buf;
7589
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007590 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007592 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593#ifdef FEAT_WINDOWS
7594 for (wp = firstwin; wp; wp = wp->w_next)
7595 {
7596 ++winnr;
7597 if (wp->w_buffer == buf)
7598 break;
7599 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007600 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007602 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603#endif
7604 --emsg_off;
7605}
7606
7607/*
7608 * "byte2line(byte)" function
7609 */
7610/*ARGSUSED*/
7611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007612f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007613 typval_T *argvars;
7614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615{
7616#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007617 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618#else
7619 long boff = 0;
7620
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007621 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007623 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007625 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 (linenr_T)0, &boff);
7627#endif
7628}
7629
7630/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007631 * "byteidx()" function
7632 */
7633/*ARGSUSED*/
7634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007635f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007636 typval_T *argvars;
7637 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007638{
7639#ifdef FEAT_MBYTE
7640 char_u *t;
7641#endif
7642 char_u *str;
7643 long idx;
7644
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007645 str = get_tv_string_chk(&argvars[0]);
7646 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007647 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007648 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007649 return;
7650
7651#ifdef FEAT_MBYTE
7652 t = str;
7653 for ( ; idx > 0; idx--)
7654 {
7655 if (*t == NUL) /* EOL reached */
7656 return;
7657 t += mb_ptr2len_check(t);
7658 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007659 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007660#else
7661 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007662 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007663#endif
7664}
7665
7666/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007667 * "call(func, arglist)" function
7668 */
7669 static void
7670f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007671 typval_T *argvars;
7672 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007673{
7674 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00007675 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007676 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007677 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007678 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00007679 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007680
7681 rettv->vval.v_number = 0;
7682 if (argvars[1].v_type != VAR_LIST)
7683 {
7684 EMSG(_(e_listreq));
7685 return;
7686 }
7687 if (argvars[1].vval.v_list == NULL)
7688 return;
7689
7690 if (argvars[0].v_type == VAR_FUNC)
7691 func = argvars[0].vval.v_string;
7692 else
7693 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007694 if (*func == NUL)
7695 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007696
Bram Moolenaare9a41262005-01-15 22:18:47 +00007697 if (argvars[2].v_type != VAR_UNKNOWN)
7698 {
7699 if (argvars[2].v_type != VAR_DICT)
7700 {
7701 EMSG(_(e_dictreq));
7702 return;
7703 }
7704 selfdict = argvars[2].vval.v_dict;
7705 }
7706
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007707 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
7708 item = item->li_next)
7709 {
7710 if (argc == MAX_FUNC_ARGS)
7711 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007712 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007713 break;
7714 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007715 /* Make a copy of each argument. This is needed to be able to set
7716 * v_lock to VAR_FIXED in the copy without changing the original list.
7717 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007718 copy_tv(&item->li_tv, &argv[argc++]);
7719 }
7720
7721 if (item == NULL)
7722 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00007723 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
7724 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007725
7726 /* Free the arguments. */
7727 while (argc > 0)
7728 clear_tv(&argv[--argc]);
7729}
7730
7731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 * "char2nr(string)" function
7733 */
7734 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007735f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007736 typval_T *argvars;
7737 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738{
7739#ifdef FEAT_MBYTE
7740 if (has_mbyte)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007741 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 else
7743#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007744 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745}
7746
7747/*
7748 * "cindent(lnum)" function
7749 */
7750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007751f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007752 typval_T *argvars;
7753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754{
7755#ifdef FEAT_CINDENT
7756 pos_T pos;
7757 linenr_T lnum;
7758
7759 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007760 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7762 {
7763 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007764 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 curwin->w_cursor = pos;
7766 }
7767 else
7768#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007769 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770}
7771
7772/*
7773 * "col(string)" function
7774 */
7775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007776f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007777 typval_T *argvars;
7778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779{
7780 colnr_T col = 0;
7781 pos_T *fp;
7782
7783 fp = var2fpos(&argvars[0], FALSE);
7784 if (fp != NULL)
7785 {
7786 if (fp->col == MAXCOL)
7787 {
7788 /* '> can be MAXCOL, get the length of the line then */
7789 if (fp->lnum <= curbuf->b_ml.ml_line_count)
7790 col = STRLEN(ml_get(fp->lnum)) + 1;
7791 else
7792 col = MAXCOL;
7793 }
7794 else
7795 {
7796 col = fp->col + 1;
7797#ifdef FEAT_VIRTUALEDIT
7798 /* col(".") when the cursor is on the NUL at the end of the line
7799 * because of "coladd" can be seen as an extra column. */
7800 if (virtual_active() && fp == &curwin->w_cursor)
7801 {
7802 char_u *p = ml_get_cursor();
7803
7804 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
7805 curwin->w_virtcol - curwin->w_cursor.coladd))
7806 {
7807# ifdef FEAT_MBYTE
7808 int l;
7809
7810 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
7811 col += l;
7812# else
7813 if (*p != NUL && p[1] == NUL)
7814 ++col;
7815# endif
7816 }
7817 }
7818#endif
7819 }
7820 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007821 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822}
7823
7824/*
7825 * "confirm(message, buttons[, default [, type]])" function
7826 */
7827/*ARGSUSED*/
7828 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007829f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007830 typval_T *argvars;
7831 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832{
7833#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
7834 char_u *message;
7835 char_u *buttons = NULL;
7836 char_u buf[NUMBUFLEN];
7837 char_u buf2[NUMBUFLEN];
7838 int def = 1;
7839 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007840 char_u *typestr;
7841 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007843 message = get_tv_string_chk(&argvars[0]);
7844 if (message == NULL)
7845 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007846 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007848 buttons = get_tv_string_buf_chk(&argvars[1], buf);
7849 if (buttons == NULL)
7850 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007851 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007853 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007854 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007856 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
7857 if (typestr == NULL)
7858 error = TRUE;
7859 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007861 switch (TOUPPER_ASC(*typestr))
7862 {
7863 case 'E': type = VIM_ERROR; break;
7864 case 'Q': type = VIM_QUESTION; break;
7865 case 'I': type = VIM_INFO; break;
7866 case 'W': type = VIM_WARNING; break;
7867 case 'G': type = VIM_GENERIC; break;
7868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 }
7870 }
7871 }
7872 }
7873
7874 if (buttons == NULL || *buttons == NUL)
7875 buttons = (char_u *)_("&Ok");
7876
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007877 if (error)
7878 rettv->vval.v_number = 0;
7879 else
7880 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 def, NULL);
7882#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007883 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884#endif
7885}
7886
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007887/*
7888 * "copy()" function
7889 */
7890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007891f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007892 typval_T *argvars;
7893 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007894{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007895 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007896}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897
7898/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007899 * "count()" function
7900 */
7901 static void
7902f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007903 typval_T *argvars;
7904 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007905{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007906 long n = 0;
7907 int ic = FALSE;
7908
Bram Moolenaare9a41262005-01-15 22:18:47 +00007909 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007910 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007911 listitem_T *li;
7912 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007913 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007914
Bram Moolenaare9a41262005-01-15 22:18:47 +00007915 if ((l = argvars[0].vval.v_list) != NULL)
7916 {
7917 li = l->lv_first;
7918 if (argvars[2].v_type != VAR_UNKNOWN)
7919 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007920 int error = FALSE;
7921
7922 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007923 if (argvars[3].v_type != VAR_UNKNOWN)
7924 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007925 idx = get_tv_number_chk(&argvars[3], &error);
7926 if (!error)
7927 {
7928 li = list_find(l, idx);
7929 if (li == NULL)
7930 EMSGN(_(e_listidx), idx);
7931 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007932 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007933 if (error)
7934 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007935 }
7936
7937 for ( ; li != NULL; li = li->li_next)
7938 if (tv_equal(&li->li_tv, &argvars[1], ic))
7939 ++n;
7940 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007941 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007942 else if (argvars[0].v_type == VAR_DICT)
7943 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007944 int todo;
7945 dict_T *d;
7946 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007947
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007948 if ((d = argvars[0].vval.v_dict) != NULL)
7949 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007950 int error = FALSE;
7951
Bram Moolenaare9a41262005-01-15 22:18:47 +00007952 if (argvars[2].v_type != VAR_UNKNOWN)
7953 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007954 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007955 if (argvars[3].v_type != VAR_UNKNOWN)
7956 EMSG(_(e_invarg));
7957 }
7958
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007959 todo = error ? 0 : d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007960 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007961 {
7962 if (!HASHITEM_EMPTY(hi))
7963 {
7964 --todo;
7965 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7966 ++n;
7967 }
7968 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007969 }
7970 }
7971 else
7972 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007973 rettv->vval.v_number = n;
7974}
7975
7976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
7978 *
7979 * Checks the existence of a cscope connection.
7980 */
7981/*ARGSUSED*/
7982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007983f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007984 typval_T *argvars;
7985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986{
7987#ifdef FEAT_CSCOPE
7988 int num = 0;
7989 char_u *dbpath = NULL;
7990 char_u *prepend = NULL;
7991 char_u buf[NUMBUFLEN];
7992
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007993 if (argvars[0].v_type != VAR_UNKNOWN
7994 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007996 num = (int)get_tv_number(&argvars[0]);
7997 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007998 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007999 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 }
8001
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008002 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008004 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005#endif
8006}
8007
8008/*
8009 * "cursor(lnum, col)" function
8010 *
8011 * Moves the cursor to the specified line and column
8012 */
8013/*ARGSUSED*/
8014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008015f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008016 typval_T *argvars;
8017 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018{
8019 long line, col;
8020
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008021 line = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008022 col = get_tv_number_chk(&argvars[1], NULL);
8023 if (line < 0 || col < 0)
8024 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 if (line > 0)
8026 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 if (col > 0)
8028 curwin->w_cursor.col = col - 1;
8029#ifdef FEAT_VIRTUALEDIT
8030 curwin->w_cursor.coladd = 0;
8031#endif
8032
8033 /* Make sure the cursor is in a valid position. */
8034 check_cursor();
8035#ifdef FEAT_MBYTE
8036 /* Correct cursor for multi-byte character. */
8037 if (has_mbyte)
8038 mb_adjust_cursor();
8039#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00008040
8041 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042}
8043
8044/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008045 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 */
8047 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008048f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008049 typval_T *argvars;
8050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008052 int noref = 0;
8053
8054 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008055 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008056 if (noref < 0 || noref > 1)
8057 EMSG(_(e_invarg));
8058 else
Bram Moolenaard9fba312005-06-26 22:34:35 +00008059 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++current_copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060}
8061
8062/*
8063 * "delete()" function
8064 */
8065 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008066f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008067 typval_T *argvars;
8068 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069{
8070 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008071 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008073 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074}
8075
8076/*
8077 * "did_filetype()" function
8078 */
8079/*ARGSUSED*/
8080 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008081f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008082 typval_T *argvars;
8083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084{
8085#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008086 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008088 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089#endif
8090}
8091
8092/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00008093 * "diff_filler()" function
8094 */
8095/*ARGSUSED*/
8096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008097f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008098 typval_T *argvars;
8099 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008100{
8101#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008102 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00008103#endif
8104}
8105
8106/*
8107 * "diff_hlID()" function
8108 */
8109/*ARGSUSED*/
8110 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008111f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008112 typval_T *argvars;
8113 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008114{
8115#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008116 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00008117 static linenr_T prev_lnum = 0;
8118 static int changedtick = 0;
8119 static int fnum = 0;
8120 static int change_start = 0;
8121 static int change_end = 0;
8122 static enum hlf_value hlID = 0;
8123 int filler_lines;
8124 int col;
8125
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008126 if (lnum < 0) /* ignore type error in {lnum} arg */
8127 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008128 if (lnum != prev_lnum
8129 || changedtick != curbuf->b_changedtick
8130 || fnum != curbuf->b_fnum)
8131 {
8132 /* New line, buffer, change: need to get the values. */
8133 filler_lines = diff_check(curwin, lnum);
8134 if (filler_lines < 0)
8135 {
8136 if (filler_lines == -1)
8137 {
8138 change_start = MAXCOL;
8139 change_end = -1;
8140 if (diff_find_change(curwin, lnum, &change_start, &change_end))
8141 hlID = HLF_ADD; /* added line */
8142 else
8143 hlID = HLF_CHD; /* changed line */
8144 }
8145 else
8146 hlID = HLF_ADD; /* added line */
8147 }
8148 else
8149 hlID = (enum hlf_value)0;
8150 prev_lnum = lnum;
8151 changedtick = curbuf->b_changedtick;
8152 fnum = curbuf->b_fnum;
8153 }
8154
8155 if (hlID == HLF_CHD || hlID == HLF_TXD)
8156 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008157 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00008158 if (col >= change_start && col <= change_end)
8159 hlID = HLF_TXD; /* changed text */
8160 else
8161 hlID = HLF_CHD; /* changed line */
8162 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008163 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00008164#endif
8165}
8166
8167/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008168 * "empty({expr})" function
8169 */
8170 static void
8171f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008172 typval_T *argvars;
8173 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008174{
8175 int n;
8176
8177 switch (argvars[0].v_type)
8178 {
8179 case VAR_STRING:
8180 case VAR_FUNC:
8181 n = argvars[0].vval.v_string == NULL
8182 || *argvars[0].vval.v_string == NUL;
8183 break;
8184 case VAR_NUMBER:
8185 n = argvars[0].vval.v_number == 0;
8186 break;
8187 case VAR_LIST:
8188 n = argvars[0].vval.v_list == NULL
8189 || argvars[0].vval.v_list->lv_first == NULL;
8190 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008191 case VAR_DICT:
8192 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00008193 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008194 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008195 default:
8196 EMSG2(_(e_intern2), "f_empty()");
8197 n = 0;
8198 }
8199
8200 rettv->vval.v_number = n;
8201}
8202
8203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 * "escape({string}, {chars})" function
8205 */
8206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008207f_escape(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 char_u buf[NUMBUFLEN];
8212
Bram Moolenaar758711c2005-02-02 23:11:38 +00008213 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
8214 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008215 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216}
8217
8218/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008219 * "eval()" function
8220 */
8221/*ARGSUSED*/
8222 static void
8223f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008224 typval_T *argvars;
8225 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008226{
8227 char_u *s;
8228
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008229 s = get_tv_string_chk(&argvars[0]);
8230 if (s != NULL)
8231 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008232
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008233 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
8234 {
8235 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008236 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008237 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008238 else if (*s != NUL)
8239 EMSG(_(e_trailing));
8240}
8241
8242/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 * "eventhandler()" function
8244 */
8245/*ARGSUSED*/
8246 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008247f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008248 typval_T *argvars;
8249 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008251 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252}
8253
8254/*
8255 * "executable()" function
8256 */
8257 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008258f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008259 typval_T *argvars;
8260 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008262 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263}
8264
8265/*
8266 * "exists()" function
8267 */
8268 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008269f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008270 typval_T *argvars;
8271 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272{
8273 char_u *p;
8274 char_u *name;
8275 int n = FALSE;
8276 int len = 0;
8277
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008278 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 if (*p == '$') /* environment variable */
8280 {
8281 /* first try "normal" environment variables (fast) */
8282 if (mch_getenv(p + 1) != NULL)
8283 n = TRUE;
8284 else
8285 {
8286 /* try expanding things like $VIM and ${HOME} */
8287 p = expand_env_save(p);
8288 if (p != NULL && *p != '$')
8289 n = TRUE;
8290 vim_free(p);
8291 }
8292 }
8293 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008294 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 else if (*p == '*') /* internal or user defined function */
8296 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008297 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 }
8299 else if (*p == ':')
8300 {
8301 n = cmd_exists(p + 1);
8302 }
8303 else if (*p == '#')
8304 {
8305#ifdef FEAT_AUTOCMD
8306 name = p + 1;
8307 p = vim_strchr(name, '#');
8308 if (p != NULL)
8309 n = au_exists(name, p, p + 1);
8310 else
8311 n = au_exists(name, name + STRLEN(name), NULL);
8312#endif
8313 }
8314 else /* internal variable */
8315 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008316 char_u *tofree;
8317 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008319 /* get_name_len() takes care of expanding curly braces */
8320 name = p;
8321 len = get_name_len(&p, &tofree, TRUE, FALSE);
8322 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008324 if (tofree != NULL)
8325 name = tofree;
8326 n = (get_var_tv(name, len, &tv, FALSE) == OK);
8327 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008329 /* handle d.key, l[idx], f(expr) */
8330 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
8331 if (n)
8332 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 }
8334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008336 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 }
8338
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008339 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340}
8341
8342/*
8343 * "expand()" function
8344 */
8345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008346f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008347 typval_T *argvars;
8348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349{
8350 char_u *s;
8351 int len;
8352 char_u *errormsg;
8353 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
8354 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008355 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008357 rettv->v_type = VAR_STRING;
8358 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 if (*s == '%' || *s == '#' || *s == '<')
8360 {
8361 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008362 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 --emsg_off;
8364 }
8365 else
8366 {
8367 /* When the optional second argument is non-zero, don't remove matches
8368 * for 'suffixes' and 'wildignore' */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008369 if (argvars[1].v_type != VAR_UNKNOWN
8370 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 flags |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008372 if (!error)
8373 {
8374 ExpandInit(&xpc);
8375 xpc.xp_context = EXPAND_FILES;
8376 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
8377 ExpandCleanup(&xpc);
8378 }
8379 else
8380 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 }
8382}
8383
8384/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008385 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00008386 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008387 */
8388 static void
8389f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008390 typval_T *argvars;
8391 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008392{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008393 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008394 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008395 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008396 list_T *l1, *l2;
8397 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008398 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008399 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008400
Bram Moolenaare9a41262005-01-15 22:18:47 +00008401 l1 = argvars[0].vval.v_list;
8402 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008403 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
8404 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008405 {
8406 if (argvars[2].v_type != VAR_UNKNOWN)
8407 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008408 before = get_tv_number_chk(&argvars[2], &error);
8409 if (error)
8410 return; /* type error; errmsg already given */
8411
Bram Moolenaar758711c2005-02-02 23:11:38 +00008412 if (before == l1->lv_len)
8413 item = NULL;
8414 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008415 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00008416 item = list_find(l1, before);
8417 if (item == NULL)
8418 {
8419 EMSGN(_(e_listidx), before);
8420 return;
8421 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008422 }
8423 }
8424 else
8425 item = NULL;
8426 list_extend(l1, l2, item);
8427
Bram Moolenaare9a41262005-01-15 22:18:47 +00008428 copy_tv(&argvars[0], rettv);
8429 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008430 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008431 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
8432 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008433 dict_T *d1, *d2;
8434 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008435 char_u *action;
8436 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00008437 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008438 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008439
8440 d1 = argvars[0].vval.v_dict;
8441 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008442 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
8443 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008444 {
8445 /* Check the third argument. */
8446 if (argvars[2].v_type != VAR_UNKNOWN)
8447 {
8448 static char *(av[]) = {"keep", "force", "error"};
8449
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008450 action = get_tv_string_chk(&argvars[2]);
8451 if (action == NULL)
8452 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008453 for (i = 0; i < 3; ++i)
8454 if (STRCMP(action, av[i]) == 0)
8455 break;
8456 if (i == 3)
8457 {
8458 EMSGN(_(e_invarg2), action);
8459 return;
8460 }
8461 }
8462 else
8463 action = (char_u *)"force";
8464
8465 /* Go over all entries in the second dict and add them to the
8466 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00008467 todo = d2->dv_hashtab.ht_used;
8468 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008469 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008470 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008471 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008472 --todo;
8473 di1 = dict_find(d1, hi2->hi_key, -1);
8474 if (di1 == NULL)
8475 {
8476 di1 = dictitem_copy(HI2DI(hi2));
8477 if (di1 != NULL && dict_add(d1, di1) == FAIL)
8478 dictitem_free(di1);
8479 }
8480 else if (*action == 'e')
8481 {
8482 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
8483 break;
8484 }
8485 else if (*action == 'f')
8486 {
8487 clear_tv(&di1->di_tv);
8488 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
8489 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008490 }
8491 }
8492
Bram Moolenaare9a41262005-01-15 22:18:47 +00008493 copy_tv(&argvars[0], rettv);
8494 }
8495 }
8496 else
8497 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008498}
8499
8500/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 * "filereadable()" function
8502 */
8503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008504f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008505 typval_T *argvars;
8506 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507{
8508 FILE *fd;
8509 char_u *p;
8510 int n;
8511
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008512 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
8514 {
8515 n = TRUE;
8516 fclose(fd);
8517 }
8518 else
8519 n = FALSE;
8520
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008521 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522}
8523
8524/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008525 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 * rights to write into.
8527 */
8528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008529f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008530 typval_T *argvars;
8531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +00008533 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534}
8535
Bram Moolenaar33570922005-01-25 22:26:29 +00008536static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008537
8538 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008539findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00008540 typval_T *argvars;
8541 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008542 int dir;
8543{
8544#ifdef FEAT_SEARCHPATH
8545 char_u *fname;
8546 char_u *fresult = NULL;
8547 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
8548 char_u *p;
8549 char_u pathbuf[NUMBUFLEN];
8550 int count = 1;
8551 int first = TRUE;
8552
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008553 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008554
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008555 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008556 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008557 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
8558 if (p == NULL)
8559 count = -1; /* error */
8560 else
8561 {
8562 if (*p != NUL)
8563 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008564
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008565 if (argvars[2].v_type != VAR_UNKNOWN)
8566 count = get_tv_number_chk(&argvars[2], NULL); /* -1: error */
8567 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008568 }
8569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008570 if (*fname != NUL && count >= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008572 do
8573 {
8574 vim_free(fresult);
8575 fresult = find_file_in_path_option(first ? fname : NULL,
8576 first ? (int)STRLEN(fname) : 0,
8577 0, first, path, dir, NULL);
8578 first = FALSE;
8579 } while (--count > 0 && fresult != NULL);
8580 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008581
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008582 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008583#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008584 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008585#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008586 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008587}
8588
Bram Moolenaar33570922005-01-25 22:26:29 +00008589static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
8590static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008591
8592/*
8593 * Implementation of map() and filter().
8594 */
8595 static void
8596filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00008597 typval_T *argvars;
8598 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008599 int map;
8600{
8601 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00008602 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00008603 listitem_T *li, *nli;
8604 list_T *l = NULL;
8605 dictitem_T *di;
8606 hashtab_T *ht;
8607 hashitem_T *hi;
8608 dict_T *d = NULL;
8609 typval_T save_val;
8610 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008611 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008612 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008613 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
8614
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008615
8616 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008617 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008618 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008619 if ((l = argvars[0].vval.v_list) == NULL
8620 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008621 return;
8622 }
8623 else if (argvars[0].v_type == VAR_DICT)
8624 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008625 if ((d = argvars[0].vval.v_dict) == NULL
8626 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00008627 return;
8628 }
8629 else
8630 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008631 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008632 return;
8633 }
8634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008635 expr = get_tv_string_buf_chk(&argvars[1], buf);
8636 /* On type errors, the preceding call has already displayed an error
8637 * message. Avoid a misleading error message for an empty string that
8638 * was not passed as argument. */
8639 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008640 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008641 prepare_vimvar(VV_VAL, &save_val);
8642 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008643
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008644 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008645 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008646 prepare_vimvar(VV_KEY, &save_key);
8647 vimvars[VV_KEY].vv_type = VAR_STRING;
8648
8649 ht = &d->dv_hashtab;
8650 hash_lock(ht);
8651 todo = ht->ht_used;
8652 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008653 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008654 if (!HASHITEM_EMPTY(hi))
8655 {
8656 --todo;
8657 di = HI2DI(hi);
8658 if (tv_check_lock(di->di_tv.v_lock, msg))
8659 break;
8660 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
8661 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
8662 break;
8663 if (!map && rem)
8664 dictitem_remove(d, di);
8665 clear_tv(&vimvars[VV_KEY].vv_tv);
8666 }
8667 }
8668 hash_unlock(ht);
8669
8670 restore_vimvar(VV_KEY, &save_key);
8671 }
8672 else
8673 {
8674 for (li = l->lv_first; li != NULL; li = nli)
8675 {
8676 if (tv_check_lock(li->li_tv.v_lock, msg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008677 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008678 nli = li->li_next;
8679 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008680 break;
8681 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008682 listitem_remove(l, li);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008683 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008684 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008685
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008686 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008687 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008688
8689 copy_tv(&argvars[0], rettv);
8690}
8691
8692 static int
8693filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00008694 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008695 char_u *expr;
8696 int map;
8697 int *remp;
8698{
Bram Moolenaar33570922005-01-25 22:26:29 +00008699 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008700 char_u *s;
8701
Bram Moolenaar33570922005-01-25 22:26:29 +00008702 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008703 s = expr;
8704 if (eval1(&s, &rettv, TRUE) == FAIL)
8705 return FAIL;
8706 if (*s != NUL) /* check for trailing chars after expr */
8707 {
8708 EMSG2(_(e_invexpr2), s);
8709 return FAIL;
8710 }
8711 if (map)
8712 {
8713 /* map(): replace the list item value */
8714 clear_tv(tv);
8715 *tv = rettv;
8716 }
8717 else
8718 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008719 int error = FALSE;
8720
Bram Moolenaare9a41262005-01-15 22:18:47 +00008721 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008722 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008723 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008724 /* On type error, nothing has been removed; return FAIL to stop the
8725 * loop. The error message was given by get_tv_number_chk(). */
8726 if (error)
8727 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008728 }
Bram Moolenaar33570922005-01-25 22:26:29 +00008729 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008730 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008731}
8732
8733/*
8734 * "filter()" function
8735 */
8736 static void
8737f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008738 typval_T *argvars;
8739 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008740{
8741 filter_map(argvars, rettv, FALSE);
8742}
8743
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008744/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008745 * "finddir({fname}[, {path}[, {count}]])" function
8746 */
8747 static void
8748f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008749 typval_T *argvars;
8750 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008751{
8752 findfilendir(argvars, rettv, TRUE);
8753}
8754
8755/*
8756 * "findfile({fname}[, {path}[, {count}]])" function
8757 */
8758 static void
8759f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008760 typval_T *argvars;
8761 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008762{
8763 findfilendir(argvars, rettv, FALSE);
8764}
8765
8766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 * "fnamemodify({fname}, {mods})" function
8768 */
8769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008770f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008771 typval_T *argvars;
8772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773{
8774 char_u *fname;
8775 char_u *mods;
8776 int usedlen = 0;
8777 int len;
8778 char_u *fbuf = NULL;
8779 char_u buf[NUMBUFLEN];
8780
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008781 fname = get_tv_string_chk(&argvars[0]);
8782 mods = get_tv_string_buf_chk(&argvars[1], buf);
8783 if (fname == NULL || mods == NULL)
8784 fname = NULL;
8785 else
8786 {
8787 len = (int)STRLEN(fname);
8788 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
8789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008791 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008793 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008795 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 vim_free(fbuf);
8797}
8798
Bram Moolenaar33570922005-01-25 22:26:29 +00008799static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800
8801/*
8802 * "foldclosed()" function
8803 */
8804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008805foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00008806 typval_T *argvars;
8807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 int end;
8809{
8810#ifdef FEAT_FOLDING
8811 linenr_T lnum;
8812 linenr_T first, last;
8813
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008814 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8816 {
8817 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
8818 {
8819 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008820 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008822 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 return;
8824 }
8825 }
8826#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008827 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828}
8829
8830/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008831 * "foldclosed()" function
8832 */
8833 static void
8834f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008835 typval_T *argvars;
8836 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008837{
8838 foldclosed_both(argvars, rettv, FALSE);
8839}
8840
8841/*
8842 * "foldclosedend()" function
8843 */
8844 static void
8845f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008846 typval_T *argvars;
8847 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008848{
8849 foldclosed_both(argvars, rettv, TRUE);
8850}
8851
8852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 * "foldlevel()" function
8854 */
8855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008856f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008857 typval_T *argvars;
8858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859{
8860#ifdef FEAT_FOLDING
8861 linenr_T lnum;
8862
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008863 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008865 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 else
8867#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008868 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869}
8870
8871/*
8872 * "foldtext()" function
8873 */
8874/*ARGSUSED*/
8875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008876f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008877 typval_T *argvars;
8878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879{
8880#ifdef FEAT_FOLDING
8881 linenr_T lnum;
8882 char_u *s;
8883 char_u *r;
8884 int len;
8885 char *txt;
8886#endif
8887
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008888 rettv->v_type = VAR_STRING;
8889 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00008891 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
8892 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
8893 <= curbuf->b_ml.ml_line_count
8894 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 {
8896 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008897 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8898 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 {
8900 if (!linewhite(lnum))
8901 break;
8902 ++lnum;
8903 }
8904
8905 /* Find interesting text in this line. */
8906 s = skipwhite(ml_get(lnum));
8907 /* skip C comment-start */
8908 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008909 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008911 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008912 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008913 {
8914 s = skipwhite(ml_get(lnum + 1));
8915 if (*s == '*')
8916 s = skipwhite(s + 1);
8917 }
8918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 txt = _("+-%s%3ld lines: ");
8920 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008921 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 + 20 /* for %3ld */
8923 + STRLEN(s))); /* concatenated */
8924 if (r != NULL)
8925 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008926 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8927 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8928 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 len = (int)STRLEN(r);
8930 STRCAT(r, s);
8931 /* remove 'foldmarker' and 'commentstring' */
8932 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008933 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 }
8935 }
8936#endif
8937}
8938
8939/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008940 * "foldtextresult(lnum)" function
8941 */
8942/*ARGSUSED*/
8943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008944f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008945 typval_T *argvars;
8946 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008947{
8948#ifdef FEAT_FOLDING
8949 linenr_T lnum;
8950 char_u *text;
8951 char_u buf[51];
8952 foldinfo_T foldinfo;
8953 int fold_count;
8954#endif
8955
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008956 rettv->v_type = VAR_STRING;
8957 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008958#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008959 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008960 /* treat illegal types and illegal string values for {lnum} the same */
8961 if (lnum < 0)
8962 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008963 fold_count = foldedCount(curwin, lnum, &foldinfo);
8964 if (fold_count > 0)
8965 {
8966 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8967 &foldinfo, buf);
8968 if (text == buf)
8969 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008970 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008971 }
8972#endif
8973}
8974
8975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 * "foreground()" function
8977 */
8978/*ARGSUSED*/
8979 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008980f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008981 typval_T *argvars;
8982 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008984 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985#ifdef FEAT_GUI
8986 if (gui.in_use)
8987 gui_mch_set_foreground();
8988#else
8989# ifdef WIN32
8990 win32_set_foreground();
8991# endif
8992#endif
8993}
8994
8995/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008996 * "function()" function
8997 */
8998/*ARGSUSED*/
8999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009000f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009001 typval_T *argvars;
9002 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009003{
9004 char_u *s;
9005
Bram Moolenaara7043832005-01-21 11:56:39 +00009006 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009007 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009008 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009009 EMSG2(_(e_invarg2), s);
9010 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009011 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009012 else
9013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009014 rettv->vval.v_string = vim_strsave(s);
9015 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009016 }
9017}
9018
9019/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00009020 * "garbagecollect()" function
9021 */
9022/*ARGSUSED*/
9023 static void
9024f_garbagecollect(argvars, rettv)
9025 typval_T *argvars;
9026 typval_T *rettv;
9027{
9028 garbage_collect();
9029}
9030
9031/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009032 * "get()" function
9033 */
9034 static void
9035f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009036 typval_T *argvars;
9037 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009038{
Bram Moolenaar33570922005-01-25 22:26:29 +00009039 listitem_T *li;
9040 list_T *l;
9041 dictitem_T *di;
9042 dict_T *d;
9043 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009044
Bram Moolenaare9a41262005-01-15 22:18:47 +00009045 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009046 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00009047 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009048 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009049 int error = FALSE;
9050
9051 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
9052 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00009053 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009054 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009055 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009056 else if (argvars[0].v_type == VAR_DICT)
9057 {
9058 if ((d = argvars[0].vval.v_dict) != NULL)
9059 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009060 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009061 if (di != NULL)
9062 tv = &di->di_tv;
9063 }
9064 }
9065 else
9066 EMSG2(_(e_listdictarg), "get()");
9067
9068 if (tv == NULL)
9069 {
9070 if (argvars[2].v_type == VAR_UNKNOWN)
9071 rettv->vval.v_number = 0;
9072 else
9073 copy_tv(&argvars[2], rettv);
9074 }
9075 else
9076 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009077}
9078
9079/*
9080 * "getbufvar()" function
9081 */
9082 static void
9083f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009084 typval_T *argvars;
9085 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009086{
9087 buf_T *buf;
9088 buf_T *save_curbuf;
9089 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009090 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009091
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009092 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
9093 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009094 ++emsg_off;
9095 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009096
9097 rettv->v_type = VAR_STRING;
9098 rettv->vval.v_string = NULL;
9099
9100 if (buf != NULL && varname != NULL)
9101 {
9102 if (*varname == '&') /* buffer-local-option */
9103 {
9104 /* set curbuf to be our buf, temporarily */
9105 save_curbuf = curbuf;
9106 curbuf = buf;
9107
9108 get_option_tv(&varname, rettv, TRUE);
9109
9110 /* restore previous notion of curbuf */
9111 curbuf = save_curbuf;
9112 }
9113 else
9114 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009115 if (*varname == NUL)
9116 /* let getbufvar({nr}, "") return the "b:" dictionary. The
9117 * scope prefix before the NUL byte is required by
9118 * find_var_in_ht(). */
9119 varname = (char_u *)"b:" + 2;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009120 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009121 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009122 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009123 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009124 }
9125 }
9126
9127 --emsg_off;
9128}
9129
9130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 * "getchar()" function
9132 */
9133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009134f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009135 typval_T *argvars;
9136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137{
9138 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009139 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140
9141 ++no_mapping;
9142 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009143 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 /* getchar(): blocking wait. */
9145 n = safe_vgetc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009146 else if (get_tv_number_chk(&argvars[0], &error) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 /* getchar(1): only check if char avail */
9148 n = vpeekc();
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009149 else if (error || vpeekc() == NUL)
9150 /* illegal argument or getchar(0) and no char avail: return zero */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 n = 0;
9152 else
9153 /* getchar(0) and char avail: return char */
9154 n = safe_vgetc();
9155 --no_mapping;
9156 --allow_keys;
9157
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009158 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 if (IS_SPECIAL(n) || mod_mask != 0)
9160 {
9161 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
9162 int i = 0;
9163
9164 /* Turn a special key into three bytes, plus modifier. */
9165 if (mod_mask != 0)
9166 {
9167 temp[i++] = K_SPECIAL;
9168 temp[i++] = KS_MODIFIER;
9169 temp[i++] = mod_mask;
9170 }
9171 if (IS_SPECIAL(n))
9172 {
9173 temp[i++] = K_SPECIAL;
9174 temp[i++] = K_SECOND(n);
9175 temp[i++] = K_THIRD(n);
9176 }
9177#ifdef FEAT_MBYTE
9178 else if (has_mbyte)
9179 i += (*mb_char2bytes)(n, temp + i);
9180#endif
9181 else
9182 temp[i++] = n;
9183 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184 rettv->v_type = VAR_STRING;
9185 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186 }
9187}
9188
9189/*
9190 * "getcharmod()" function
9191 */
9192/*ARGSUSED*/
9193 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009194f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009195 typval_T *argvars;
9196 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009198 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199}
9200
9201/*
9202 * "getcmdline()" function
9203 */
9204/*ARGSUSED*/
9205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009206f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009207 typval_T *argvars;
9208 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009210 rettv->v_type = VAR_STRING;
9211 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212}
9213
9214/*
9215 * "getcmdpos()" function
9216 */
9217/*ARGSUSED*/
9218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009219f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009220 typval_T *argvars;
9221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009223 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009224}
9225
9226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 * "getcwd()" function
9228 */
9229/*ARGSUSED*/
9230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009231f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009232 typval_T *argvars;
9233 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234{
9235 char_u cwd[MAXPATHL];
9236
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009237 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009239 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 else
9241 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009242 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009244 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245#endif
9246 }
9247}
9248
9249/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009250 * "getfontname()" function
9251 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009252/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009254f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009255 typval_T *argvars;
9256 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009257{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009258 rettv->v_type = VAR_STRING;
9259 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009260#ifdef FEAT_GUI
9261 if (gui.in_use)
9262 {
9263 GuiFont font;
9264 char_u *name = NULL;
9265
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009266 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009267 {
9268 /* Get the "Normal" font. Either the name saved by
9269 * hl_set_font_name() or from the font ID. */
9270 font = gui.norm_font;
9271 name = hl_get_font_name();
9272 }
9273 else
9274 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009275 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009276 if (STRCMP(name, "*") == 0) /* don't use font dialog */
9277 return;
9278 font = gui_mch_get_font(name, FALSE);
9279 if (font == NOFONT)
9280 return; /* Invalid font name, return empty string. */
9281 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009282 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009283 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00009284 gui_mch_free_font(font);
9285 }
9286#endif
9287}
9288
9289/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009290 * "getfperm({fname})" function
9291 */
9292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009293f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009294 typval_T *argvars;
9295 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009296{
9297 char_u *fname;
9298 struct stat st;
9299 char_u *perm = NULL;
9300 char_u flags[] = "rwx";
9301 int i;
9302
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009303 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009304
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009305 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009306 if (mch_stat((char *)fname, &st) >= 0)
9307 {
9308 perm = vim_strsave((char_u *)"---------");
9309 if (perm != NULL)
9310 {
9311 for (i = 0; i < 9; i++)
9312 {
9313 if (st.st_mode & (1 << (8 - i)))
9314 perm[i] = flags[i % 3];
9315 }
9316 }
9317 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009318 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009319}
9320
9321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322 * "getfsize({fname})" function
9323 */
9324 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009325f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009326 typval_T *argvars;
9327 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328{
9329 char_u *fname;
9330 struct stat st;
9331
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009332 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009334 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335
9336 if (mch_stat((char *)fname, &st) >= 0)
9337 {
9338 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009339 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009341 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 }
9343 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009344 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009345}
9346
9347/*
9348 * "getftime({fname})" function
9349 */
9350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009351f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009352 typval_T *argvars;
9353 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354{
9355 char_u *fname;
9356 struct stat st;
9357
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009358 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359
9360 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009361 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009363 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364}
9365
9366/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009367 * "getftype({fname})" function
9368 */
9369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009370f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009371 typval_T *argvars;
9372 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009373{
9374 char_u *fname;
9375 struct stat st;
9376 char_u *type = NULL;
9377 char *t;
9378
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009379 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009380
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009381 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009382 if (mch_lstat((char *)fname, &st) >= 0)
9383 {
9384#ifdef S_ISREG
9385 if (S_ISREG(st.st_mode))
9386 t = "file";
9387 else if (S_ISDIR(st.st_mode))
9388 t = "dir";
9389# ifdef S_ISLNK
9390 else if (S_ISLNK(st.st_mode))
9391 t = "link";
9392# endif
9393# ifdef S_ISBLK
9394 else if (S_ISBLK(st.st_mode))
9395 t = "bdev";
9396# endif
9397# ifdef S_ISCHR
9398 else if (S_ISCHR(st.st_mode))
9399 t = "cdev";
9400# endif
9401# ifdef S_ISFIFO
9402 else if (S_ISFIFO(st.st_mode))
9403 t = "fifo";
9404# endif
9405# ifdef S_ISSOCK
9406 else if (S_ISSOCK(st.st_mode))
9407 t = "fifo";
9408# endif
9409 else
9410 t = "other";
9411#else
9412# ifdef S_IFMT
9413 switch (st.st_mode & S_IFMT)
9414 {
9415 case S_IFREG: t = "file"; break;
9416 case S_IFDIR: t = "dir"; break;
9417# ifdef S_IFLNK
9418 case S_IFLNK: t = "link"; break;
9419# endif
9420# ifdef S_IFBLK
9421 case S_IFBLK: t = "bdev"; break;
9422# endif
9423# ifdef S_IFCHR
9424 case S_IFCHR: t = "cdev"; break;
9425# endif
9426# ifdef S_IFIFO
9427 case S_IFIFO: t = "fifo"; break;
9428# endif
9429# ifdef S_IFSOCK
9430 case S_IFSOCK: t = "socket"; break;
9431# endif
9432 default: t = "other";
9433 }
9434# else
9435 if (mch_isdir(fname))
9436 t = "dir";
9437 else
9438 t = "file";
9439# endif
9440#endif
9441 type = vim_strsave((char_u *)t);
9442 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009443 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009444}
9445
9446/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009447 * "getline(lnum)" function
9448 */
9449 static void
9450f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009451 typval_T *argvars;
9452 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009453{
9454 linenr_T lnum;
9455 linenr_T end;
9456 char_u *p;
Bram Moolenaar33570922005-01-25 22:26:29 +00009457 list_T *l;
9458 listitem_T *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009459
9460 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009461 if (lnum < 0)
9462 rettv->vval.v_number = 0; /* failure; error message already given */
9463 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009464 {
9465 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9466 p = ml_get(lnum);
9467 else
9468 p = (char_u *)"";
9469
9470 rettv->v_type = VAR_STRING;
9471 rettv->vval.v_string = vim_strsave(p);
9472 }
9473 else
9474 {
9475 end = get_tv_lnum(&argvars[1]);
9476 if (end < lnum)
9477 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009478 if (end >= 0) /* else: error message already given */
9479 EMSG(_(e_invrange));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009480 rettv->vval.v_number = 0;
9481 }
9482 else
9483 {
9484 l = list_alloc();
9485 if (l != NULL)
9486 {
9487 if (lnum < 1)
9488 lnum = 1;
9489 if (end > curbuf->b_ml.ml_line_count)
9490 end = curbuf->b_ml.ml_line_count;
9491 while (lnum <= end)
9492 {
9493 li = listitem_alloc();
9494 if (li == NULL)
9495 break;
9496 list_append(l, li);
9497 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009498 li->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009499 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
9500 }
9501 rettv->vval.v_list = l;
9502 rettv->v_type = VAR_LIST;
9503 ++l->lv_refcount;
9504 }
9505 }
9506 }
9507}
9508
9509/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00009510 * "getqflist()" function
9511 */
9512/*ARGSUSED*/
9513 static void
9514f_getqflist(argvars, rettv)
9515 typval_T *argvars;
9516 typval_T *rettv;
9517{
9518#ifdef FEAT_QUICKFIX
9519 list_T *l;
9520#endif
9521
9522 rettv->vval.v_number = FALSE;
9523#ifdef FEAT_QUICKFIX
9524 l = list_alloc();
9525 if (l != NULL)
9526 {
9527 if (get_errorlist(l) != FAIL)
9528 {
9529 rettv->vval.v_list = l;
9530 rettv->v_type = VAR_LIST;
9531 ++l->lv_refcount;
9532 }
9533 else
9534 list_free(l);
9535 }
9536#endif
9537}
9538
9539/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 * "getreg()" function
9541 */
9542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009543f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009544 typval_T *argvars;
9545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546{
9547 char_u *strregname;
9548 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009549 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009550 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009552 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009553 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009554 strregname = get_tv_string_chk(&argvars[0]);
9555 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009556 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009557 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00009558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00009560 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561 regname = (strregname == NULL ? '"' : *strregname);
9562 if (regname == 0)
9563 regname = '"';
9564
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009565 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009566 rettv->vval.v_string = error ? NULL :
9567 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009568}
9569
9570/*
9571 * "getregtype()" function
9572 */
9573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009574f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009575 typval_T *argvars;
9576 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009577{
9578 char_u *strregname;
9579 int regname;
9580 char_u buf[NUMBUFLEN + 2];
9581 long reglen = 0;
9582
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009583 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009584 {
9585 strregname = get_tv_string_chk(&argvars[0]);
9586 if (strregname == NULL) /* type error; errmsg already given */
9587 {
9588 rettv->v_type = VAR_STRING;
9589 rettv->vval.v_string = NULL;
9590 return;
9591 }
9592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593 else
9594 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00009595 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596
9597 regname = (strregname == NULL ? '"' : *strregname);
9598 if (regname == 0)
9599 regname = '"';
9600
9601 buf[0] = NUL;
9602 buf[1] = NUL;
9603 switch (get_reg_type(regname, &reglen))
9604 {
9605 case MLINE: buf[0] = 'V'; break;
9606 case MCHAR: buf[0] = 'v'; break;
9607#ifdef FEAT_VISUAL
9608 case MBLOCK:
9609 buf[0] = Ctrl_V;
9610 sprintf((char *)buf + 1, "%ld", reglen + 1);
9611 break;
9612#endif
9613 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009614 rettv->v_type = VAR_STRING;
9615 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616}
9617
9618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 * "getwinposx()" function
9620 */
9621/*ARGSUSED*/
9622 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009623f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009624 typval_T *argvars;
9625 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009627 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628#ifdef FEAT_GUI
9629 if (gui.in_use)
9630 {
9631 int x, y;
9632
9633 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009634 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 }
9636#endif
9637}
9638
9639/*
9640 * "getwinposy()" function
9641 */
9642/*ARGSUSED*/
9643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009644f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009645 typval_T *argvars;
9646 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009648 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649#ifdef FEAT_GUI
9650 if (gui.in_use)
9651 {
9652 int x, y;
9653
9654 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 }
9657#endif
9658}
9659
Bram Moolenaara40058a2005-07-11 22:42:07 +00009660static win_T *find_win_by_nr __ARGS((typval_T *vp));
9661
9662 static win_T *
9663find_win_by_nr(vp)
9664 typval_T *vp;
9665{
9666#ifdef FEAT_WINDOWS
9667 win_T *wp;
9668#endif
9669 int nr;
9670
9671 nr = get_tv_number_chk(vp, NULL);
9672
9673#ifdef FEAT_WINDOWS
9674 if (nr < 0)
9675 return NULL;
9676 if (nr == 0)
9677 return curwin;
9678
9679 for (wp = firstwin; wp != NULL; wp = wp->w_next)
9680 if (--nr <= 0)
9681 break;
9682 return wp;
9683#else
9684 if (nr == 0 || nr == 1)
9685 return curwin;
9686 return NULL;
9687#endif
9688}
9689
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690/*
9691 * "getwinvar()" function
9692 */
9693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009694f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009695 typval_T *argvars;
9696 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697{
9698 win_T *win, *oldcurwin;
9699 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00009700 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009703 varname = get_tv_string_chk(&argvars[1]);
9704 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706 rettv->v_type = VAR_STRING;
9707 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708
9709 if (win != NULL && varname != NULL)
9710 {
9711 if (*varname == '&') /* window-local-option */
9712 {
Bram Moolenaarc0761132005-03-18 20:30:32 +00009713 /* Set curwin to be our win, temporarily. Also set curbuf, so
9714 * that we can get buffer-local options. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 oldcurwin = curwin;
9716 curwin = win;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009717 curbuf = win->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009719 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720
9721 /* restore previous notion of curwin */
9722 curwin = oldcurwin;
Bram Moolenaarc0761132005-03-18 20:30:32 +00009723 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 }
9725 else
9726 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009727 if (*varname == NUL)
9728 /* let getwinvar({nr}, "") return the "w:" dictionary. The
9729 * scope prefix before the NUL byte is required by
9730 * find_var_in_ht(). */
9731 varname = (char_u *)"w:" + 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732 /* look up the variable */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009733 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00009735 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 }
9737 }
9738
9739 --emsg_off;
9740}
9741
9742/*
9743 * "glob()" function
9744 */
9745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009746f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009747 typval_T *argvars;
9748 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749{
9750 expand_T xpc;
9751
9752 ExpandInit(&xpc);
9753 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009754 rettv->v_type = VAR_STRING;
9755 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
9757 ExpandCleanup(&xpc);
9758}
9759
9760/*
9761 * "globpath()" function
9762 */
9763 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009764f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009765 typval_T *argvars;
9766 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767{
9768 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009769 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009771 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009772 if (file == NULL)
9773 rettv->vval.v_string = NULL;
9774 else
9775 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776}
9777
9778/*
9779 * "has()" function
9780 */
9781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009782f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009783 typval_T *argvars;
9784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785{
9786 int i;
9787 char_u *name;
9788 int n = FALSE;
9789 static char *(has_list[]) =
9790 {
9791#ifdef AMIGA
9792 "amiga",
9793# ifdef FEAT_ARP
9794 "arp",
9795# endif
9796#endif
9797#ifdef __BEOS__
9798 "beos",
9799#endif
9800#ifdef MSDOS
9801# ifdef DJGPP
9802 "dos32",
9803# else
9804 "dos16",
9805# endif
9806#endif
9807#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
9808 "mac",
9809#endif
9810#if defined(MACOS_X_UNIX)
9811 "macunix",
9812#endif
9813#ifdef OS2
9814 "os2",
9815#endif
9816#ifdef __QNX__
9817 "qnx",
9818#endif
9819#ifdef RISCOS
9820 "riscos",
9821#endif
9822#ifdef UNIX
9823 "unix",
9824#endif
9825#ifdef VMS
9826 "vms",
9827#endif
9828#ifdef WIN16
9829 "win16",
9830#endif
9831#ifdef WIN32
9832 "win32",
9833#endif
9834#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
9835 "win32unix",
9836#endif
9837#ifdef WIN64
9838 "win64",
9839#endif
9840#ifdef EBCDIC
9841 "ebcdic",
9842#endif
9843#ifndef CASE_INSENSITIVE_FILENAME
9844 "fname_case",
9845#endif
9846#ifdef FEAT_ARABIC
9847 "arabic",
9848#endif
9849#ifdef FEAT_AUTOCMD
9850 "autocmd",
9851#endif
9852#ifdef FEAT_BEVAL
9853 "balloon_eval",
9854#endif
9855#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
9856 "builtin_terms",
9857# ifdef ALL_BUILTIN_TCAPS
9858 "all_builtin_terms",
9859# endif
9860#endif
9861#ifdef FEAT_BYTEOFF
9862 "byte_offset",
9863#endif
9864#ifdef FEAT_CINDENT
9865 "cindent",
9866#endif
9867#ifdef FEAT_CLIENTSERVER
9868 "clientserver",
9869#endif
9870#ifdef FEAT_CLIPBOARD
9871 "clipboard",
9872#endif
9873#ifdef FEAT_CMDL_COMPL
9874 "cmdline_compl",
9875#endif
9876#ifdef FEAT_CMDHIST
9877 "cmdline_hist",
9878#endif
9879#ifdef FEAT_COMMENTS
9880 "comments",
9881#endif
9882#ifdef FEAT_CRYPT
9883 "cryptv",
9884#endif
9885#ifdef FEAT_CSCOPE
9886 "cscope",
9887#endif
9888#ifdef DEBUG
9889 "debug",
9890#endif
9891#ifdef FEAT_CON_DIALOG
9892 "dialog_con",
9893#endif
9894#ifdef FEAT_GUI_DIALOG
9895 "dialog_gui",
9896#endif
9897#ifdef FEAT_DIFF
9898 "diff",
9899#endif
9900#ifdef FEAT_DIGRAPHS
9901 "digraphs",
9902#endif
9903#ifdef FEAT_DND
9904 "dnd",
9905#endif
9906#ifdef FEAT_EMACS_TAGS
9907 "emacs_tags",
9908#endif
9909 "eval", /* always present, of course! */
9910#ifdef FEAT_EX_EXTRA
9911 "ex_extra",
9912#endif
9913#ifdef FEAT_SEARCH_EXTRA
9914 "extra_search",
9915#endif
9916#ifdef FEAT_FKMAP
9917 "farsi",
9918#endif
9919#ifdef FEAT_SEARCHPATH
9920 "file_in_path",
9921#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00009922#if defined(UNIX) && !defined(USE_SYSTEM)
9923 "filterpipe",
9924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925#ifdef FEAT_FIND_ID
9926 "find_in_path",
9927#endif
9928#ifdef FEAT_FOLDING
9929 "folding",
9930#endif
9931#ifdef FEAT_FOOTER
9932 "footer",
9933#endif
9934#if !defined(USE_SYSTEM) && defined(UNIX)
9935 "fork",
9936#endif
9937#ifdef FEAT_GETTEXT
9938 "gettext",
9939#endif
9940#ifdef FEAT_GUI
9941 "gui",
9942#endif
9943#ifdef FEAT_GUI_ATHENA
9944# ifdef FEAT_GUI_NEXTAW
9945 "gui_neXtaw",
9946# else
9947 "gui_athena",
9948# endif
9949#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00009950#ifdef FEAT_GUI_KDE
9951 "gui_kde",
9952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953#ifdef FEAT_GUI_GTK
9954 "gui_gtk",
9955# ifdef HAVE_GTK2
9956 "gui_gtk2",
9957# endif
9958#endif
9959#ifdef FEAT_GUI_MAC
9960 "gui_mac",
9961#endif
9962#ifdef FEAT_GUI_MOTIF
9963 "gui_motif",
9964#endif
9965#ifdef FEAT_GUI_PHOTON
9966 "gui_photon",
9967#endif
9968#ifdef FEAT_GUI_W16
9969 "gui_win16",
9970#endif
9971#ifdef FEAT_GUI_W32
9972 "gui_win32",
9973#endif
9974#ifdef FEAT_HANGULIN
9975 "hangul_input",
9976#endif
9977#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
9978 "iconv",
9979#endif
9980#ifdef FEAT_INS_EXPAND
9981 "insert_expand",
9982#endif
9983#ifdef FEAT_JUMPLIST
9984 "jumplist",
9985#endif
9986#ifdef FEAT_KEYMAP
9987 "keymap",
9988#endif
9989#ifdef FEAT_LANGMAP
9990 "langmap",
9991#endif
9992#ifdef FEAT_LIBCALL
9993 "libcall",
9994#endif
9995#ifdef FEAT_LINEBREAK
9996 "linebreak",
9997#endif
9998#ifdef FEAT_LISP
9999 "lispindent",
10000#endif
10001#ifdef FEAT_LISTCMDS
10002 "listcmds",
10003#endif
10004#ifdef FEAT_LOCALMAP
10005 "localmap",
10006#endif
10007#ifdef FEAT_MENU
10008 "menu",
10009#endif
10010#ifdef FEAT_SESSION
10011 "mksession",
10012#endif
10013#ifdef FEAT_MODIFY_FNAME
10014 "modify_fname",
10015#endif
10016#ifdef FEAT_MOUSE
10017 "mouse",
10018#endif
10019#ifdef FEAT_MOUSESHAPE
10020 "mouseshape",
10021#endif
10022#if defined(UNIX) || defined(VMS)
10023# ifdef FEAT_MOUSE_DEC
10024 "mouse_dec",
10025# endif
10026# ifdef FEAT_MOUSE_GPM
10027 "mouse_gpm",
10028# endif
10029# ifdef FEAT_MOUSE_JSB
10030 "mouse_jsbterm",
10031# endif
10032# ifdef FEAT_MOUSE_NET
10033 "mouse_netterm",
10034# endif
10035# ifdef FEAT_MOUSE_PTERM
10036 "mouse_pterm",
10037# endif
10038# ifdef FEAT_MOUSE_XTERM
10039 "mouse_xterm",
10040# endif
10041#endif
10042#ifdef FEAT_MBYTE
10043 "multi_byte",
10044#endif
10045#ifdef FEAT_MBYTE_IME
10046 "multi_byte_ime",
10047#endif
10048#ifdef FEAT_MULTI_LANG
10049 "multi_lang",
10050#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010051#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000010052#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000010053 "mzscheme",
10054#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010056#ifdef FEAT_OLE
10057 "ole",
10058#endif
10059#ifdef FEAT_OSFILETYPE
10060 "osfiletype",
10061#endif
10062#ifdef FEAT_PATH_EXTRA
10063 "path_extra",
10064#endif
10065#ifdef FEAT_PERL
10066#ifndef DYNAMIC_PERL
10067 "perl",
10068#endif
10069#endif
10070#ifdef FEAT_PYTHON
10071#ifndef DYNAMIC_PYTHON
10072 "python",
10073#endif
10074#endif
10075#ifdef FEAT_POSTSCRIPT
10076 "postscript",
10077#endif
10078#ifdef FEAT_PRINTER
10079 "printer",
10080#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000010081#ifdef FEAT_PROFILE
10082 "profile",
10083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084#ifdef FEAT_QUICKFIX
10085 "quickfix",
10086#endif
10087#ifdef FEAT_RIGHTLEFT
10088 "rightleft",
10089#endif
10090#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
10091 "ruby",
10092#endif
10093#ifdef FEAT_SCROLLBIND
10094 "scrollbind",
10095#endif
10096#ifdef FEAT_CMDL_INFO
10097 "showcmd",
10098 "cmdline_info",
10099#endif
10100#ifdef FEAT_SIGNS
10101 "signs",
10102#endif
10103#ifdef FEAT_SMARTINDENT
10104 "smartindent",
10105#endif
10106#ifdef FEAT_SNIFF
10107 "sniff",
10108#endif
10109#ifdef FEAT_STL_OPT
10110 "statusline",
10111#endif
10112#ifdef FEAT_SUN_WORKSHOP
10113 "sun_workshop",
10114#endif
10115#ifdef FEAT_NETBEANS_INTG
10116 "netbeans_intg",
10117#endif
10118#ifdef FEAT_SYN_HL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010119 "spell",
10120#endif
10121#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 "syntax",
10123#endif
10124#if defined(USE_SYSTEM) || !defined(UNIX)
10125 "system",
10126#endif
10127#ifdef FEAT_TAG_BINS
10128 "tag_binary",
10129#endif
10130#ifdef FEAT_TAG_OLDSTATIC
10131 "tag_old_static",
10132#endif
10133#ifdef FEAT_TAG_ANYWHITE
10134 "tag_any_white",
10135#endif
10136#ifdef FEAT_TCL
10137# ifndef DYNAMIC_TCL
10138 "tcl",
10139# endif
10140#endif
10141#ifdef TERMINFO
10142 "terminfo",
10143#endif
10144#ifdef FEAT_TERMRESPONSE
10145 "termresponse",
10146#endif
10147#ifdef FEAT_TEXTOBJ
10148 "textobjects",
10149#endif
10150#ifdef HAVE_TGETENT
10151 "tgetent",
10152#endif
10153#ifdef FEAT_TITLE
10154 "title",
10155#endif
10156#ifdef FEAT_TOOLBAR
10157 "toolbar",
10158#endif
10159#ifdef FEAT_USR_CMDS
10160 "user-commands", /* was accidentally included in 5.4 */
10161 "user_commands",
10162#endif
10163#ifdef FEAT_VIMINFO
10164 "viminfo",
10165#endif
10166#ifdef FEAT_VERTSPLIT
10167 "vertsplit",
10168#endif
10169#ifdef FEAT_VIRTUALEDIT
10170 "virtualedit",
10171#endif
10172#ifdef FEAT_VISUAL
10173 "visual",
10174#endif
10175#ifdef FEAT_VISUALEXTRA
10176 "visualextra",
10177#endif
10178#ifdef FEAT_VREPLACE
10179 "vreplace",
10180#endif
10181#ifdef FEAT_WILDIGN
10182 "wildignore",
10183#endif
10184#ifdef FEAT_WILDMENU
10185 "wildmenu",
10186#endif
10187#ifdef FEAT_WINDOWS
10188 "windows",
10189#endif
10190#ifdef FEAT_WAK
10191 "winaltkeys",
10192#endif
10193#ifdef FEAT_WRITEBACKUP
10194 "writebackup",
10195#endif
10196#ifdef FEAT_XIM
10197 "xim",
10198#endif
10199#ifdef FEAT_XFONTSET
10200 "xfontset",
10201#endif
10202#ifdef USE_XSMP
10203 "xsmp",
10204#endif
10205#ifdef USE_XSMP_INTERACT
10206 "xsmp_interact",
10207#endif
10208#ifdef FEAT_XCLIPBOARD
10209 "xterm_clipboard",
10210#endif
10211#ifdef FEAT_XTERM_SAVE
10212 "xterm_save",
10213#endif
10214#if defined(UNIX) && defined(FEAT_X11)
10215 "X11",
10216#endif
10217 NULL
10218 };
10219
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010220 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010221 for (i = 0; has_list[i] != NULL; ++i)
10222 if (STRICMP(name, has_list[i]) == 0)
10223 {
10224 n = TRUE;
10225 break;
10226 }
10227
10228 if (n == FALSE)
10229 {
10230 if (STRNICMP(name, "patch", 5) == 0)
10231 n = has_patch(atoi((char *)name + 5));
10232 else if (STRICMP(name, "vim_starting") == 0)
10233 n = (starting != 0);
10234#ifdef DYNAMIC_TCL
10235 else if (STRICMP(name, "tcl") == 0)
10236 n = tcl_enabled(FALSE);
10237#endif
10238#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
10239 else if (STRICMP(name, "iconv") == 0)
10240 n = iconv_enabled(FALSE);
10241#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000010242#ifdef DYNAMIC_MZSCHEME
10243 else if (STRICMP(name, "mzscheme") == 0)
10244 n = mzscheme_enabled(FALSE);
10245#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246#ifdef DYNAMIC_RUBY
10247 else if (STRICMP(name, "ruby") == 0)
10248 n = ruby_enabled(FALSE);
10249#endif
10250#ifdef DYNAMIC_PYTHON
10251 else if (STRICMP(name, "python") == 0)
10252 n = python_enabled(FALSE);
10253#endif
10254#ifdef DYNAMIC_PERL
10255 else if (STRICMP(name, "perl") == 0)
10256 n = perl_enabled(FALSE);
10257#endif
10258#ifdef FEAT_GUI
10259 else if (STRICMP(name, "gui_running") == 0)
10260 n = (gui.in_use || gui.starting);
10261# ifdef FEAT_GUI_W32
10262 else if (STRICMP(name, "gui_win32s") == 0)
10263 n = gui_is_win32s();
10264# endif
10265# ifdef FEAT_BROWSE
10266 else if (STRICMP(name, "browse") == 0)
10267 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
10268# endif
10269#endif
10270#ifdef FEAT_SYN_HL
10271 else if (STRICMP(name, "syntax_items") == 0)
10272 n = syntax_present(curbuf);
10273#endif
10274#if defined(WIN3264)
10275 else if (STRICMP(name, "win95") == 0)
10276 n = mch_windows95();
10277#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000010278#ifdef FEAT_NETBEANS_INTG
10279 else if (STRICMP(name, "netbeans_enabled") == 0)
10280 n = usingNetbeans;
10281#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 }
10283
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010284 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285}
10286
10287/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000010288 * "has_key()" function
10289 */
10290 static void
10291f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010292 typval_T *argvars;
10293 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010294{
10295 rettv->vval.v_number = 0;
10296 if (argvars[0].v_type != VAR_DICT)
10297 {
10298 EMSG(_(e_dictreq));
10299 return;
10300 }
10301 if (argvars[0].vval.v_dict == NULL)
10302 return;
10303
10304 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010305 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010306}
10307
10308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309 * "hasmapto()" function
10310 */
10311 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010312f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010313 typval_T *argvars;
10314 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315{
10316 char_u *name;
10317 char_u *mode;
10318 char_u buf[NUMBUFLEN];
10319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010320 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010321 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 mode = (char_u *)"nvo";
10323 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010324 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325
10326 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010327 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010329 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330}
10331
10332/*
10333 * "histadd()" function
10334 */
10335/*ARGSUSED*/
10336 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010337f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010338 typval_T *argvars;
10339 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340{
10341#ifdef FEAT_CMDHIST
10342 int histype;
10343 char_u *str;
10344 char_u buf[NUMBUFLEN];
10345#endif
10346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010347 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 if (check_restricted() || check_secure())
10349 return;
10350#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010351 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10352 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 if (histype >= 0)
10354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010355 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 if (*str != NUL)
10357 {
10358 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010359 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 return;
10361 }
10362 }
10363#endif
10364}
10365
10366/*
10367 * "histdel()" function
10368 */
10369/*ARGSUSED*/
10370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010371f_histdel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010372 typval_T *argvars;
10373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374{
10375#ifdef FEAT_CMDHIST
10376 int n;
10377 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010378 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010380 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10381 if (str == NULL)
10382 n = 0;
10383 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010384 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010385 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010386 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010388 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010389 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 else
10391 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010392 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010393 get_tv_string_buf(&argvars[1], buf));
10394 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010396 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010397#endif
10398}
10399
10400/*
10401 * "histget()" function
10402 */
10403/*ARGSUSED*/
10404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010405f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010406 typval_T *argvars;
10407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408{
10409#ifdef FEAT_CMDHIST
10410 int type;
10411 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010412 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010414 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
10415 if (str == NULL)
10416 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010418 {
10419 type = get_histtype(str);
10420 if (argvars[1].v_type == VAR_UNKNOWN)
10421 idx = get_history_idx(type);
10422 else
10423 idx = (int)get_tv_number_chk(&argvars[1], NULL);
10424 /* -1 on type error */
10425 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
10426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010428 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010430 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010431}
10432
10433/*
10434 * "histnr()" function
10435 */
10436/*ARGSUSED*/
10437 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010438f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010439 typval_T *argvars;
10440 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441{
10442 int i;
10443
10444#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010445 char_u *history = get_tv_string_chk(&argvars[0]);
10446
10447 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448 if (i >= HIST_CMD && i < HIST_COUNT)
10449 i = get_history_idx(i);
10450 else
10451#endif
10452 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010453 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454}
10455
10456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457 * "highlightID(name)" function
10458 */
10459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010460f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010461 typval_T *argvars;
10462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010464 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010465}
10466
10467/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010468 * "highlight_exists()" function
10469 */
10470 static void
10471f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010472 typval_T *argvars;
10473 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010474{
10475 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
10476}
10477
10478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479 * "hostname()" function
10480 */
10481/*ARGSUSED*/
10482 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010483f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010484 typval_T *argvars;
10485 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486{
10487 char_u hostname[256];
10488
10489 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010490 rettv->v_type = VAR_STRING;
10491 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492}
10493
10494/*
10495 * iconv() function
10496 */
10497/*ARGSUSED*/
10498 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010499f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010500 typval_T *argvars;
10501 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010502{
10503#ifdef FEAT_MBYTE
10504 char_u buf1[NUMBUFLEN];
10505 char_u buf2[NUMBUFLEN];
10506 char_u *from, *to, *str;
10507 vimconv_T vimconv;
10508#endif
10509
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010510 rettv->v_type = VAR_STRING;
10511 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512
10513#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010514 str = get_tv_string(&argvars[0]);
10515 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
10516 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517 vimconv.vc_type = CONV_NONE;
10518 convert_setup(&vimconv, from, to);
10519
10520 /* If the encodings are equal, no conversion needed. */
10521 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010522 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010524 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525
10526 convert_setup(&vimconv, NULL, NULL);
10527 vim_free(from);
10528 vim_free(to);
10529#endif
10530}
10531
10532/*
10533 * "indent()" function
10534 */
10535 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010536f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010537 typval_T *argvars;
10538 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539{
10540 linenr_T lnum;
10541
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010542 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010543 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010544 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010546 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547}
10548
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010549/*
10550 * "index()" function
10551 */
10552 static void
10553f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010554 typval_T *argvars;
10555 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010556{
Bram Moolenaar33570922005-01-25 22:26:29 +000010557 list_T *l;
10558 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010559 long idx = 0;
10560 int ic = FALSE;
10561
10562 rettv->vval.v_number = -1;
10563 if (argvars[0].v_type != VAR_LIST)
10564 {
10565 EMSG(_(e_listreq));
10566 return;
10567 }
10568 l = argvars[0].vval.v_list;
10569 if (l != NULL)
10570 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010571 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010572 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010573 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010574 int error = FALSE;
10575
Bram Moolenaar758711c2005-02-02 23:11:38 +000010576 /* Start at specified item. Use the cached index that list_find()
10577 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010578 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000010579 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010580 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010581 ic = get_tv_number_chk(&argvars[3], &error);
10582 if (error)
10583 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010584 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010585
Bram Moolenaar758711c2005-02-02 23:11:38 +000010586 for ( ; item != NULL; item = item->li_next, ++idx)
10587 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010588 {
10589 rettv->vval.v_number = idx;
10590 break;
10591 }
10592 }
10593}
10594
Bram Moolenaar071d4272004-06-13 20:20:40 +000010595static int inputsecret_flag = 0;
10596
10597/*
10598 * "input()" function
10599 * Also handles inputsecret() when inputsecret is set.
10600 */
10601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010602f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010603 typval_T *argvars;
10604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010606 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010607 char_u *p = NULL;
10608 int c;
10609 char_u buf[NUMBUFLEN];
10610 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010611 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010613 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614
10615#ifdef NO_CONSOLE_INPUT
10616 /* While starting up, there is no place to enter text. */
10617 if (no_console_input())
10618 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010619 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620 return;
10621 }
10622#endif
10623
10624 cmd_silent = FALSE; /* Want to see the prompt. */
10625 if (prompt != NULL)
10626 {
10627 /* Only the part of the message after the last NL is considered as
10628 * prompt for the command line */
10629 p = vim_strrchr(prompt, '\n');
10630 if (p == NULL)
10631 p = prompt;
10632 else
10633 {
10634 ++p;
10635 c = *p;
10636 *p = NUL;
10637 msg_start();
10638 msg_clr_eos();
10639 msg_puts_attr(prompt, echo_attr);
10640 msg_didout = FALSE;
10641 msg_starthere();
10642 *p = c;
10643 }
10644 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010646 if (argvars[1].v_type != VAR_UNKNOWN)
10647 {
10648 defstr = get_tv_string_buf_chk(&argvars[1], buf);
10649 if (defstr != NULL)
10650 stuffReadbuffSpec(defstr);
10651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010653 if (defstr != NULL)
10654 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
10656
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010657 /* since the user typed this, no need to wait for return */
10658 need_wait_return = FALSE;
10659 msg_didout = FALSE;
10660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661 cmd_silent = cmd_silent_save;
10662}
10663
10664/*
10665 * "inputdialog()" function
10666 */
10667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010668f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010669 typval_T *argvars;
10670 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671{
10672#if defined(FEAT_GUI_TEXTDIALOG)
10673 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
10674 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
10675 {
10676 char_u *message;
10677 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010678 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010680 message = get_tv_string_chk(&argvars[0]);
10681 if (argvars[1].v_type != VAR_UNKNOWN
10682 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000010683 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010684 else
10685 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010686 if (message != NULL && defstr != NULL
10687 && do_dialog(VIM_QUESTION, NULL, message,
10688 (char_u *)_("&OK\n&Cancel"), 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010689 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010690 else
10691 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010692 if (message != NULL && defstr != NULL
10693 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010694 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010695 rettv->vval.v_string = vim_strsave(
10696 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010698 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010700 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701 }
10702 else
10703#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010704 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705}
10706
10707static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
10708
10709/*
10710 * "inputrestore()" function
10711 */
10712/*ARGSUSED*/
10713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010714f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010715 typval_T *argvars;
10716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717{
10718 if (ga_userinput.ga_len > 0)
10719 {
10720 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
10722 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010723 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 }
10725 else if (p_verbose > 1)
10726 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000010727 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010728 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 }
10730}
10731
10732/*
10733 * "inputsave()" function
10734 */
10735/*ARGSUSED*/
10736 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010737f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010738 typval_T *argvars;
10739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740{
10741 /* Add an entry to the stack of typehead storage. */
10742 if (ga_grow(&ga_userinput, 1) == OK)
10743 {
10744 save_typeahead((tasave_T *)(ga_userinput.ga_data)
10745 + ga_userinput.ga_len);
10746 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010747 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010748 }
10749 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010750 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751}
10752
10753/*
10754 * "inputsecret()" function
10755 */
10756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010757f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010758 typval_T *argvars;
10759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010760{
10761 ++cmdline_star;
10762 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010763 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 --cmdline_star;
10765 --inputsecret_flag;
10766}
10767
10768/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010769 * "insert()" function
10770 */
10771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010772f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010773 typval_T *argvars;
10774 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010775{
10776 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010777 listitem_T *item;
10778 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010779 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010780
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010781 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010782 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010783 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010784 else if ((l = argvars[0].vval.v_list) != NULL
10785 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010786 {
10787 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010788 before = get_tv_number_chk(&argvars[2], &error);
10789 if (error)
10790 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010791
Bram Moolenaar758711c2005-02-02 23:11:38 +000010792 if (before == l->lv_len)
10793 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010794 else
10795 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010796 item = list_find(l, before);
10797 if (item == NULL)
10798 {
10799 EMSGN(_(e_listidx), before);
10800 l = NULL;
10801 }
10802 }
10803 if (l != NULL)
10804 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010805 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010806 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010807 }
10808 }
10809}
10810
10811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812 * "isdirectory()" function
10813 */
10814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010815f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010816 typval_T *argvars;
10817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010819 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820}
10821
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010822/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010823 * "islocked()" function
10824 */
10825 static void
10826f_islocked(argvars, rettv)
10827 typval_T *argvars;
10828 typval_T *rettv;
10829{
10830 lval_T lv;
10831 char_u *end;
10832 dictitem_T *di;
10833
10834 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000010835 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
10836 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010837 if (end != NULL && lv.ll_name != NULL)
10838 {
10839 if (*end != NUL)
10840 EMSG(_(e_trailing));
10841 else
10842 {
10843 if (lv.ll_tv == NULL)
10844 {
10845 if (check_changedtick(lv.ll_name))
10846 rettv->vval.v_number = 1; /* always locked */
10847 else
10848 {
10849 di = find_var(lv.ll_name, NULL);
10850 if (di != NULL)
10851 {
10852 /* Consider a variable locked when:
10853 * 1. the variable itself is locked
10854 * 2. the value of the variable is locked.
10855 * 3. the List or Dict value is locked.
10856 */
10857 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
10858 || tv_islocked(&di->di_tv));
10859 }
10860 }
10861 }
10862 else if (lv.ll_range)
10863 EMSG(_("E745: Range not allowed"));
10864 else if (lv.ll_newkey != NULL)
10865 EMSG2(_(e_dictkey), lv.ll_newkey);
10866 else if (lv.ll_list != NULL)
10867 /* List item. */
10868 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
10869 else
10870 /* Dictionary item. */
10871 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
10872 }
10873 }
10874
10875 clear_lval(&lv);
10876}
10877
Bram Moolenaar33570922005-01-25 22:26:29 +000010878static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010879
10880/*
10881 * Turn a dict into a list:
10882 * "what" == 0: list of keys
10883 * "what" == 1: list of values
10884 * "what" == 2: list of items
10885 */
10886 static void
10887dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000010888 typval_T *argvars;
10889 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010890 int what;
10891{
Bram Moolenaar33570922005-01-25 22:26:29 +000010892 list_T *l;
10893 list_T *l2;
10894 dictitem_T *di;
10895 hashitem_T *hi;
10896 listitem_T *li;
10897 listitem_T *li2;
10898 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010899 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010900
10901 rettv->vval.v_number = 0;
10902 if (argvars[0].v_type != VAR_DICT)
10903 {
10904 EMSG(_(e_dictreq));
10905 return;
10906 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010907 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010908 return;
10909
10910 l = list_alloc();
10911 if (l == NULL)
10912 return;
10913 rettv->v_type = VAR_LIST;
10914 rettv->vval.v_list = l;
10915 ++l->lv_refcount;
10916
Bram Moolenaar33570922005-01-25 22:26:29 +000010917 todo = d->dv_hashtab.ht_used;
10918 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010919 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010920 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000010921 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010922 --todo;
10923 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010924
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010925 li = listitem_alloc();
10926 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000010927 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010928 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000010929
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010930 if (what == 0)
10931 {
10932 /* keys() */
10933 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010934 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010935 li->li_tv.vval.v_string = vim_strsave(di->di_key);
10936 }
10937 else if (what == 1)
10938 {
10939 /* values() */
10940 copy_tv(&di->di_tv, &li->li_tv);
10941 }
10942 else
10943 {
10944 /* items() */
10945 l2 = list_alloc();
10946 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010947 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010948 li->li_tv.vval.v_list = l2;
10949 if (l2 == NULL)
10950 break;
10951 ++l2->lv_refcount;
10952
10953 li2 = listitem_alloc();
10954 if (li2 == NULL)
10955 break;
10956 list_append(l2, li2);
10957 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010958 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010959 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
10960
10961 li2 = listitem_alloc();
10962 if (li2 == NULL)
10963 break;
10964 list_append(l2, li2);
10965 copy_tv(&di->di_tv, &li2->li_tv);
10966 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000010967 }
10968 }
10969}
10970
10971/*
10972 * "items(dict)" function
10973 */
10974 static void
10975f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010976 typval_T *argvars;
10977 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010978{
10979 dict_list(argvars, rettv, 2);
10980}
10981
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010983 * "join()" function
10984 */
10985 static void
10986f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010987 typval_T *argvars;
10988 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010989{
10990 garray_T ga;
10991 char_u *sep;
10992
10993 rettv->vval.v_number = 0;
10994 if (argvars[0].v_type != VAR_LIST)
10995 {
10996 EMSG(_(e_listreq));
10997 return;
10998 }
10999 if (argvars[0].vval.v_list == NULL)
11000 return;
11001 if (argvars[1].v_type == VAR_UNKNOWN)
11002 sep = (char_u *)" ";
11003 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011004 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011005
11006 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011007
11008 if (sep != NULL)
11009 {
11010 ga_init2(&ga, (int)sizeof(char), 80);
11011 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
11012 ga_append(&ga, NUL);
11013 rettv->vval.v_string = (char_u *)ga.ga_data;
11014 }
11015 else
11016 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011017}
11018
11019/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011020 * "keys()" function
11021 */
11022 static void
11023f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011024 typval_T *argvars;
11025 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011026{
11027 dict_list(argvars, rettv, 0);
11028}
11029
11030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 * "last_buffer_nr()" function.
11032 */
11033/*ARGSUSED*/
11034 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011035f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011036 typval_T *argvars;
11037 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038{
11039 int n = 0;
11040 buf_T *buf;
11041
11042 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
11043 if (n < buf->b_fnum)
11044 n = buf->b_fnum;
11045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011046 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011047}
11048
11049/*
11050 * "len()" function
11051 */
11052 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011053f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011054 typval_T *argvars;
11055 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011056{
11057 switch (argvars[0].v_type)
11058 {
11059 case VAR_STRING:
11060 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011061 rettv->vval.v_number = (varnumber_T)STRLEN(
11062 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011063 break;
11064 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011065 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011066 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011067 case VAR_DICT:
11068 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
11069 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011070 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011071 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011072 break;
11073 }
11074}
11075
Bram Moolenaar33570922005-01-25 22:26:29 +000011076static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011077
11078 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011079libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011080 typval_T *argvars;
11081 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011082 int type;
11083{
11084#ifdef FEAT_LIBCALL
11085 char_u *string_in;
11086 char_u **string_result;
11087 int nr_result;
11088#endif
11089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011090 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011091 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011092 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011093 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011094 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011095
11096 if (check_restricted() || check_secure())
11097 return;
11098
11099#ifdef FEAT_LIBCALL
11100 /* The first two args must be strings, otherwise its meaningless */
11101 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
11102 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011103 string_in = NULL;
11104 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011105 string_in = argvars[2].vval.v_string;
11106 if (type == VAR_NUMBER)
11107 string_result = NULL;
11108 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011109 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011110 if (mch_libcall(argvars[0].vval.v_string,
11111 argvars[1].vval.v_string,
11112 string_in,
11113 argvars[2].vval.v_number,
11114 string_result,
11115 &nr_result) == OK
11116 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011117 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011118 }
11119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011120}
11121
11122/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011123 * "libcall()" function
11124 */
11125 static void
11126f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011127 typval_T *argvars;
11128 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011129{
11130 libcall_common(argvars, rettv, VAR_STRING);
11131}
11132
11133/*
11134 * "libcallnr()" function
11135 */
11136 static void
11137f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011138 typval_T *argvars;
11139 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011140{
11141 libcall_common(argvars, rettv, VAR_NUMBER);
11142}
11143
11144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011145 * "line(string)" function
11146 */
11147 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011148f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011149 typval_T *argvars;
11150 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151{
11152 linenr_T lnum = 0;
11153 pos_T *fp;
11154
11155 fp = var2fpos(&argvars[0], TRUE);
11156 if (fp != NULL)
11157 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011158 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011159}
11160
11161/*
11162 * "line2byte(lnum)" function
11163 */
11164/*ARGSUSED*/
11165 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011166f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011167 typval_T *argvars;
11168 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169{
11170#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011171 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172#else
11173 linenr_T lnum;
11174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011175 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011177 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011178 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011179 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
11180 if (rettv->vval.v_number >= 0)
11181 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011182#endif
11183}
11184
11185/*
11186 * "lispindent(lnum)" function
11187 */
11188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011189f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011190 typval_T *argvars;
11191 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011192{
11193#ifdef FEAT_LISP
11194 pos_T pos;
11195 linenr_T lnum;
11196
11197 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011198 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11200 {
11201 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011202 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011203 curwin->w_cursor = pos;
11204 }
11205 else
11206#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011207 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011208}
11209
11210/*
11211 * "localtime()" function
11212 */
11213/*ARGSUSED*/
11214 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011215f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011216 typval_T *argvars;
11217 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011218{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011219 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220}
11221
Bram Moolenaar33570922005-01-25 22:26:29 +000011222static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223
11224 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011225get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000011226 typval_T *argvars;
11227 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 int exact;
11229{
11230 char_u *keys;
11231 char_u *which;
11232 char_u buf[NUMBUFLEN];
11233 char_u *keys_buf = NULL;
11234 char_u *rhs;
11235 int mode;
11236 garray_T ga;
11237
11238 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011239 rettv->v_type = VAR_STRING;
11240 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011242 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243 if (*keys == NUL)
11244 return;
11245
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011246 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011247 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 else
11249 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011250 if (which == NULL)
11251 return;
11252
Bram Moolenaar071d4272004-06-13 20:20:40 +000011253 mode = get_map_mode(&which, 0);
11254
11255 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
Bram Moolenaarf4630b62005-05-20 21:31:17 +000011256 rhs = check_map(keys, mode, exact, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 vim_free(keys_buf);
11258 if (rhs != NULL)
11259 {
11260 ga_init(&ga);
11261 ga.ga_itemsize = 1;
11262 ga.ga_growsize = 40;
11263
11264 while (*rhs != NUL)
11265 ga_concat(&ga, str2special(&rhs, FALSE));
11266
11267 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011268 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269 }
11270}
11271
11272/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011273 * "map()" function
11274 */
11275 static void
11276f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011277 typval_T *argvars;
11278 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011279{
11280 filter_map(argvars, rettv, TRUE);
11281}
11282
11283/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011284 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011285 */
11286 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011287f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011288 typval_T *argvars;
11289 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011290{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011291 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292}
11293
11294/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011295 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011296 */
11297 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011298f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011299 typval_T *argvars;
11300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011301{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011302 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011303}
11304
Bram Moolenaar33570922005-01-25 22:26:29 +000011305static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011306
11307 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011308find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000011309 typval_T *argvars;
11310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011311 int type;
11312{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011313 char_u *str = NULL;
11314 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315 char_u *pat;
11316 regmatch_T regmatch;
11317 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011318 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 char_u *save_cpo;
11320 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011321 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011322 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000011323 list_T *l = NULL;
11324 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011325 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011326 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327
11328 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11329 save_cpo = p_cpo;
11330 p_cpo = (char_u *)"";
11331
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011332 rettv->vval.v_number = -1;
11333 if (type == 3)
11334 {
11335 /* return empty list when there are no matches */
11336 if ((rettv->vval.v_list = list_alloc()) == NULL)
11337 goto theend;
11338 rettv->v_type = VAR_LIST;
11339 ++rettv->vval.v_list->lv_refcount;
11340 }
11341 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011343 rettv->v_type = VAR_STRING;
11344 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011347 if (argvars[0].v_type == VAR_LIST)
11348 {
11349 if ((l = argvars[0].vval.v_list) == NULL)
11350 goto theend;
11351 li = l->lv_first;
11352 }
11353 else
11354 expr = str = get_tv_string(&argvars[0]);
11355
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011356 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
11357 if (pat == NULL)
11358 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011359
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011360 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011362 int error = FALSE;
11363
11364 start = get_tv_number_chk(&argvars[2], &error);
11365 if (error)
11366 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011367 if (l != NULL)
11368 {
11369 li = list_find(l, start);
11370 if (li == NULL)
11371 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011372 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011373 }
11374 else
11375 {
11376 if (start < 0)
11377 start = 0;
11378 if (start > (long)STRLEN(str))
11379 goto theend;
11380 str += start;
11381 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011382
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011383 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011384 nth = get_tv_number_chk(&argvars[3], &error);
11385 if (error)
11386 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387 }
11388
11389 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
11390 if (regmatch.regprog != NULL)
11391 {
11392 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011393
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011394 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011395 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011396 if (l != NULL)
11397 {
11398 if (li == NULL)
11399 {
11400 match = FALSE;
11401 break;
11402 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011403 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011404 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011405 if (str == NULL)
11406 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011407 }
11408
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011409 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011410
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011411 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011412 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011413 if (l == NULL && !match)
11414 break;
11415
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011416 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011417 if (l != NULL)
11418 {
11419 li = li->li_next;
11420 ++idx;
11421 }
11422 else
11423 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011424#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011425 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011426#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011427 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011428#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011429 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011430 }
11431
11432 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011434 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011435 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011436 int i;
11437
11438 /* return list with matched string and submatches */
11439 for (i = 0; i < NSUBEXP; ++i)
11440 {
11441 if (regmatch.endp[i] == NULL)
11442 break;
11443 li = listitem_alloc();
11444 if (li == NULL)
11445 break;
11446 li->li_tv.v_type = VAR_STRING;
11447 li->li_tv.v_lock = 0;
11448 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
11449 (int)(regmatch.endp[i] - regmatch.startp[i]));
11450 list_append(rettv->vval.v_list, li);
11451 }
11452 }
11453 else if (type == 2)
11454 {
11455 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011456 if (l != NULL)
11457 copy_tv(&li->li_tv, rettv);
11458 else
11459 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000011460 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011461 }
11462 else if (l != NULL)
11463 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464 else
11465 {
11466 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011467 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468 (varnumber_T)(regmatch.startp[0] - str);
11469 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011470 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473 }
11474 }
11475 vim_free(regmatch.regprog);
11476 }
11477
11478theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011479 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 p_cpo = save_cpo;
11481}
11482
11483/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011484 * "match()" function
11485 */
11486 static void
11487f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011488 typval_T *argvars;
11489 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011490{
11491 find_some_match(argvars, rettv, 1);
11492}
11493
11494/*
11495 * "matchend()" function
11496 */
11497 static void
11498f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011499 typval_T *argvars;
11500 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011501{
11502 find_some_match(argvars, rettv, 0);
11503}
11504
11505/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011506 * "matchlist()" function
11507 */
11508 static void
11509f_matchlist(argvars, rettv)
11510 typval_T *argvars;
11511 typval_T *rettv;
11512{
11513 find_some_match(argvars, rettv, 3);
11514}
11515
11516/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011517 * "matchstr()" function
11518 */
11519 static void
11520f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011521 typval_T *argvars;
11522 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011523{
11524 find_some_match(argvars, rettv, 2);
11525}
11526
Bram Moolenaar33570922005-01-25 22:26:29 +000011527static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011528
11529 static void
11530max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000011531 typval_T *argvars;
11532 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011533 int domax;
11534{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011535 long n = 0;
11536 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011537 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011538
11539 if (argvars[0].v_type == VAR_LIST)
11540 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011541 list_T *l;
11542 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011543
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011544 l = argvars[0].vval.v_list;
11545 if (l != NULL)
11546 {
11547 li = l->lv_first;
11548 if (li != NULL)
11549 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011550 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000011551 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011552 {
11553 li = li->li_next;
11554 if (li == NULL)
11555 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011556 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011557 if (domax ? i > n : i < n)
11558 n = i;
11559 }
11560 }
11561 }
11562 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011563 else if (argvars[0].v_type == VAR_DICT)
11564 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011565 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011566 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000011567 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011568 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011569
11570 d = argvars[0].vval.v_dict;
11571 if (d != NULL)
11572 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011573 todo = d->dv_hashtab.ht_used;
11574 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011575 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011576 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011577 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011578 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011579 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011580 if (first)
11581 {
11582 n = i;
11583 first = FALSE;
11584 }
11585 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011586 n = i;
11587 }
11588 }
11589 }
11590 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011591 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000011592 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011593 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011594}
11595
11596/*
11597 * "max()" function
11598 */
11599 static void
11600f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011601 typval_T *argvars;
11602 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011603{
11604 max_min(argvars, rettv, TRUE);
11605}
11606
11607/*
11608 * "min()" function
11609 */
11610 static void
11611f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011612 typval_T *argvars;
11613 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011614{
11615 max_min(argvars, rettv, FALSE);
11616}
11617
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011618static int mkdir_recurse __ARGS((char_u *dir, int prot));
11619
11620/*
11621 * Create the directory in which "dir" is located, and higher levels when
11622 * needed.
11623 */
11624 static int
11625mkdir_recurse(dir, prot)
11626 char_u *dir;
11627 int prot;
11628{
11629 char_u *p;
11630 char_u *updir;
11631 int r = FAIL;
11632
11633 /* Get end of directory name in "dir".
11634 * We're done when it's "/" or "c:/". */
11635 p = gettail_sep(dir);
11636 if (p <= get_past_head(dir))
11637 return OK;
11638
11639 /* If the directory exists we're done. Otherwise: create it.*/
11640 updir = vim_strnsave(dir, (int)(p - dir));
11641 if (updir == NULL)
11642 return FAIL;
11643 if (mch_isdir(updir))
11644 r = OK;
11645 else if (mkdir_recurse(updir, prot) == OK)
11646 r = vim_mkdir_emsg(updir, prot);
11647 vim_free(updir);
11648 return r;
11649}
11650
11651#ifdef vim_mkdir
11652/*
11653 * "mkdir()" function
11654 */
11655 static void
11656f_mkdir(argvars, rettv)
11657 typval_T *argvars;
11658 typval_T *rettv;
11659{
11660 char_u *dir;
11661 char_u buf[NUMBUFLEN];
11662 int prot = 0755;
11663
11664 rettv->vval.v_number = FAIL;
11665 if (check_restricted() || check_secure())
11666 return;
11667
11668 dir = get_tv_string_buf(&argvars[0], buf);
11669 if (argvars[1].v_type != VAR_UNKNOWN)
11670 {
11671 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011672 prot = get_tv_number_chk(&argvars[2], NULL);
11673 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011674 mkdir_recurse(dir, prot);
11675 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011676 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011677}
11678#endif
11679
Bram Moolenaar0d660222005-01-07 21:51:51 +000011680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 * "mode()" function
11682 */
11683/*ARGSUSED*/
11684 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011685f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011686 typval_T *argvars;
11687 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011688{
11689 char_u buf[2];
11690
11691#ifdef FEAT_VISUAL
11692 if (VIsual_active)
11693 {
11694 if (VIsual_select)
11695 buf[0] = VIsual_mode + 's' - 'v';
11696 else
11697 buf[0] = VIsual_mode;
11698 }
11699 else
11700#endif
11701 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
11702 buf[0] = 'r';
11703 else if (State & INSERT)
11704 {
11705 if (State & REPLACE_FLAG)
11706 buf[0] = 'R';
11707 else
11708 buf[0] = 'i';
11709 }
11710 else if (State & CMDLINE)
11711 buf[0] = 'c';
11712 else
11713 buf[0] = 'n';
11714
11715 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011716 rettv->vval.v_string = vim_strsave(buf);
11717 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011718}
11719
11720/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011721 * "nextnonblank()" function
11722 */
11723 static void
11724f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011725 typval_T *argvars;
11726 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011727{
11728 linenr_T lnum;
11729
11730 for (lnum = get_tv_lnum(argvars); ; ++lnum)
11731 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011732 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011733 {
11734 lnum = 0;
11735 break;
11736 }
11737 if (*skipwhite(ml_get(lnum)) != NUL)
11738 break;
11739 }
11740 rettv->vval.v_number = lnum;
11741}
11742
11743/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011744 * "nr2char()" function
11745 */
11746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011747f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011748 typval_T *argvars;
11749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750{
11751 char_u buf[NUMBUFLEN];
11752
11753#ifdef FEAT_MBYTE
11754 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011755 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756 else
11757#endif
11758 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011759 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760 buf[1] = NUL;
11761 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011762 rettv->v_type = VAR_STRING;
11763 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011764}
11765
11766/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011767 * "prevnonblank()" function
11768 */
11769 static void
11770f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011771 typval_T *argvars;
11772 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011773{
11774 linenr_T lnum;
11775
11776 lnum = get_tv_lnum(argvars);
11777 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
11778 lnum = 0;
11779 else
11780 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
11781 --lnum;
11782 rettv->vval.v_number = lnum;
11783}
11784
Bram Moolenaar8c711452005-01-14 21:53:12 +000011785/*
11786 * "range()" function
11787 */
11788 static void
11789f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011790 typval_T *argvars;
11791 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011792{
11793 long start;
11794 long end;
11795 long stride = 1;
11796 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011797 list_T *l;
11798 listitem_T *li;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011799 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011800
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011801 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011802 if (argvars[1].v_type == VAR_UNKNOWN)
11803 {
11804 end = start - 1;
11805 start = 0;
11806 }
11807 else
11808 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011809 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011810 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011811 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011812 }
11813
11814 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011815 if (error)
11816 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000011817 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011818 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000011819 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011820 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000011821 else
11822 {
11823 l = list_alloc();
11824 if (l != NULL)
11825 {
11826 rettv->v_type = VAR_LIST;
11827 rettv->vval.v_list = l;
11828 ++l->lv_refcount;
11829
11830 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
11831 {
11832 li = listitem_alloc();
11833 if (li == NULL)
11834 break;
11835 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011836 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011837 li->li_tv.vval.v_number = i;
11838 list_append(l, li);
11839 }
11840 }
11841 }
11842}
11843
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011844/*
11845 * "readfile()" function
11846 */
11847 static void
11848f_readfile(argvars, rettv)
11849 typval_T *argvars;
11850 typval_T *rettv;
11851{
11852 int binary = FALSE;
11853 char_u *fname;
11854 FILE *fd;
11855 list_T *l;
11856 listitem_T *li;
11857#define FREAD_SIZE 200 /* optimized for text lines */
11858 char_u buf[FREAD_SIZE];
11859 int readlen; /* size of last fread() */
11860 int buflen; /* nr of valid chars in buf[] */
11861 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
11862 int tolist; /* first byte in buf[] still to be put in list */
11863 int chop; /* how many CR to chop off */
11864 char_u *prev = NULL; /* previously read bytes, if any */
11865 int prevlen = 0; /* length of "prev" if not NULL */
11866 char_u *s;
11867 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011868 long maxline = MAXLNUM;
11869 long cnt = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011870
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011871 if (argvars[1].v_type != VAR_UNKNOWN)
11872 {
11873 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
11874 binary = TRUE;
11875 if (argvars[2].v_type != VAR_UNKNOWN)
11876 maxline = get_tv_number(&argvars[2]);
11877 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011878
11879 l = list_alloc();
11880 if (l == NULL)
11881 return;
11882 rettv->v_type = VAR_LIST;
11883 rettv->vval.v_list = l;
11884 l->lv_refcount = 1;
11885
11886 /* Always open the file in binary mode, library functions have a mind of
11887 * their own about CR-LF conversion. */
11888 fname = get_tv_string(&argvars[0]);
11889 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
11890 {
11891 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
11892 return;
11893 }
11894
11895 filtd = 0;
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011896 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011897 {
11898 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
11899 buflen = filtd + readlen;
11900 tolist = 0;
11901 for ( ; filtd < buflen || readlen <= 0; ++filtd)
11902 {
11903 if (buf[filtd] == '\n' || readlen <= 0)
11904 {
11905 /* Only when in binary mode add an empty list item when the
11906 * last line ends in a '\n'. */
11907 if (!binary && readlen == 0 && filtd == 0)
11908 break;
11909
11910 /* Found end-of-line or end-of-file: add a text line to the
11911 * list. */
11912 chop = 0;
11913 if (!binary)
11914 while (filtd - chop - 1 >= tolist
11915 && buf[filtd - chop - 1] == '\r')
11916 ++chop;
11917 len = filtd - tolist - chop;
11918 if (prev == NULL)
11919 s = vim_strnsave(buf + tolist, len);
11920 else
11921 {
11922 s = alloc((unsigned)(prevlen + len + 1));
11923 if (s != NULL)
11924 {
11925 mch_memmove(s, prev, prevlen);
11926 vim_free(prev);
11927 prev = NULL;
11928 mch_memmove(s + prevlen, buf + tolist, len);
11929 s[prevlen + len] = NUL;
11930 }
11931 }
11932 tolist = filtd + 1;
11933
11934 li = listitem_alloc();
11935 if (li == NULL)
11936 {
11937 vim_free(s);
11938 break;
11939 }
11940 li->li_tv.v_type = VAR_STRING;
11941 li->li_tv.v_lock = 0;
11942 li->li_tv.vval.v_string = s;
11943 list_append(l, li);
11944
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011945 if (++cnt >= maxline && maxline >= 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011946 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011947 if (readlen <= 0)
11948 break;
11949 }
11950 else if (buf[filtd] == NUL)
11951 buf[filtd] = '\n';
11952 }
11953 if (readlen <= 0)
11954 break;
11955
11956 if (tolist == 0)
11957 {
11958 /* "buf" is full, need to move text to an allocated buffer */
11959 if (prev == NULL)
11960 {
11961 prev = vim_strnsave(buf, buflen);
11962 prevlen = buflen;
11963 }
11964 else
11965 {
11966 s = alloc((unsigned)(prevlen + buflen));
11967 if (s != NULL)
11968 {
11969 mch_memmove(s, prev, prevlen);
11970 mch_memmove(s + prevlen, buf, buflen);
11971 vim_free(prev);
11972 prev = s;
11973 prevlen += buflen;
11974 }
11975 }
11976 filtd = 0;
11977 }
11978 else
11979 {
11980 mch_memmove(buf, buf + tolist, buflen - tolist);
11981 filtd -= tolist;
11982 }
11983 }
11984
Bram Moolenaarb982ca52005-03-28 21:02:15 +000011985 /*
11986 * For a negative line count use only the lines at the end of the file,
11987 * free the rest.
11988 */
11989 if (maxline < 0)
11990 while (cnt > -maxline)
11991 {
11992 listitem_remove(l, l->lv_first);
11993 --cnt;
11994 }
11995
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000011996 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011997 fclose(fd);
11998}
11999
12000
Bram Moolenaar0d660222005-01-07 21:51:51 +000012001#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
12002static void make_connection __ARGS((void));
12003static int check_connection __ARGS((void));
12004
12005 static void
12006make_connection()
12007{
12008 if (X_DISPLAY == NULL
12009# ifdef FEAT_GUI
12010 && !gui.in_use
12011# endif
12012 )
12013 {
12014 x_force_connect = TRUE;
12015 setup_term_clip();
12016 x_force_connect = FALSE;
12017 }
12018}
12019
12020 static int
12021check_connection()
12022{
12023 make_connection();
12024 if (X_DISPLAY == NULL)
12025 {
12026 EMSG(_("E240: No connection to Vim server"));
12027 return FAIL;
12028 }
12029 return OK;
12030}
12031#endif
12032
12033#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012034static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012035
12036 static void
12037remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000012038 typval_T *argvars;
12039 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012040 int expr;
12041{
12042 char_u *server_name;
12043 char_u *keys;
12044 char_u *r = NULL;
12045 char_u buf[NUMBUFLEN];
12046# ifdef WIN32
12047 HWND w;
12048# else
12049 Window w;
12050# endif
12051
12052 if (check_restricted() || check_secure())
12053 return;
12054
12055# ifdef FEAT_X11
12056 if (check_connection() == FAIL)
12057 return;
12058# endif
12059
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012060 server_name = get_tv_string_chk(&argvars[0]);
12061 if (server_name == NULL)
12062 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012063 keys = get_tv_string_buf(&argvars[1], buf);
12064# ifdef WIN32
12065 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
12066# else
12067 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
12068 < 0)
12069# endif
12070 {
12071 if (r != NULL)
12072 EMSG(r); /* sending worked but evaluation failed */
12073 else
12074 EMSG2(_("E241: Unable to send to %s"), server_name);
12075 return;
12076 }
12077
12078 rettv->vval.v_string = r;
12079
12080 if (argvars[2].v_type != VAR_UNKNOWN)
12081 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012082 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000012083 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012084 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012085
12086 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000012087 v.di_tv.v_type = VAR_STRING;
12088 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012089 idvar = get_tv_string_chk(&argvars[2]);
12090 if (idvar != NULL)
12091 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012092 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012093 }
12094}
12095#endif
12096
12097/*
12098 * "remote_expr()" function
12099 */
12100/*ARGSUSED*/
12101 static void
12102f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012103 typval_T *argvars;
12104 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012105{
12106 rettv->v_type = VAR_STRING;
12107 rettv->vval.v_string = NULL;
12108#ifdef FEAT_CLIENTSERVER
12109 remote_common(argvars, rettv, TRUE);
12110#endif
12111}
12112
12113/*
12114 * "remote_foreground()" function
12115 */
12116/*ARGSUSED*/
12117 static void
12118f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012119 typval_T *argvars;
12120 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012121{
12122 rettv->vval.v_number = 0;
12123#ifdef FEAT_CLIENTSERVER
12124# ifdef WIN32
12125 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012126 {
12127 char_u *server_name = get_tv_string_chk(&argvars[0]);
12128
12129 if (server_name != NULL)
12130 serverForeground(server_name);
12131 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012132# else
12133 /* Send a foreground() expression to the server. */
12134 argvars[1].v_type = VAR_STRING;
12135 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
12136 argvars[2].v_type = VAR_UNKNOWN;
12137 remote_common(argvars, rettv, TRUE);
12138 vim_free(argvars[1].vval.v_string);
12139# endif
12140#endif
12141}
12142
12143/*ARGSUSED*/
12144 static void
12145f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012146 typval_T *argvars;
12147 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012148{
12149#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000012150 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012151 char_u *s = NULL;
12152# ifdef WIN32
12153 int n = 0;
12154# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012155 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012156
12157 if (check_restricted() || check_secure())
12158 {
12159 rettv->vval.v_number = -1;
12160 return;
12161 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012162 serverid = get_tv_string_chk(&argvars[0]);
12163 if (serverid == NULL)
12164 {
12165 rettv->vval.v_number = -1;
12166 return; /* type error; errmsg already given */
12167 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012168# ifdef WIN32
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012169 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012170 if (n == 0)
12171 rettv->vval.v_number = -1;
12172 else
12173 {
12174 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
12175 rettv->vval.v_number = (s != NULL);
12176 }
12177# else
12178 rettv->vval.v_number = 0;
12179 if (check_connection() == FAIL)
12180 return;
12181
12182 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012183 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012184# endif
12185
12186 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
12187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012188 char_u *retvar;
12189
Bram Moolenaar33570922005-01-25 22:26:29 +000012190 v.di_tv.v_type = VAR_STRING;
12191 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012192 retvar = get_tv_string_chk(&argvars[1]);
12193 if (retvar != NULL)
12194 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000012195 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012196 }
12197#else
12198 rettv->vval.v_number = -1;
12199#endif
12200}
12201
12202/*ARGSUSED*/
12203 static void
12204f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012205 typval_T *argvars;
12206 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012207{
12208 char_u *r = NULL;
12209
12210#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012211 char_u *serverid = get_tv_string_chk(&argvars[0]);
12212
12213 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000012214 {
12215# ifdef WIN32
12216 /* The server's HWND is encoded in the 'id' parameter */
12217 int n = 0;
12218
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012219 sscanf(serverid, "%x", &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012220 if (n != 0)
12221 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
12222 if (r == NULL)
12223# else
12224 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012225 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012226# endif
12227 EMSG(_("E277: Unable to read a server reply"));
12228 }
12229#endif
12230 rettv->v_type = VAR_STRING;
12231 rettv->vval.v_string = r;
12232}
12233
12234/*
12235 * "remote_send()" function
12236 */
12237/*ARGSUSED*/
12238 static void
12239f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012240 typval_T *argvars;
12241 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012242{
12243 rettv->v_type = VAR_STRING;
12244 rettv->vval.v_string = NULL;
12245#ifdef FEAT_CLIENTSERVER
12246 remote_common(argvars, rettv, FALSE);
12247#endif
12248}
12249
12250/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000012251 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012252 */
12253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012254f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012255 typval_T *argvars;
12256 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012257{
Bram Moolenaar33570922005-01-25 22:26:29 +000012258 list_T *l;
12259 listitem_T *item, *item2;
12260 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012261 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012262 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012263 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000012264 dict_T *d;
12265 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012266
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012267 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012268 if (argvars[0].v_type == VAR_DICT)
12269 {
12270 if (argvars[2].v_type != VAR_UNKNOWN)
12271 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012272 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000012273 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000012274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012275 key = get_tv_string_chk(&argvars[1]);
12276 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012278 di = dict_find(d, key, -1);
12279 if (di == NULL)
12280 EMSG2(_(e_dictkey), key);
12281 else
12282 {
12283 *rettv = di->di_tv;
12284 init_tv(&di->di_tv);
12285 dictitem_remove(d, di);
12286 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012287 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012288 }
12289 }
12290 else if (argvars[0].v_type != VAR_LIST)
12291 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012292 else if ((l = argvars[0].vval.v_list) != NULL
12293 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012295 int error = FALSE;
12296
12297 idx = get_tv_number_chk(&argvars[1], &error);
12298 if (error)
12299 ; /* type error: do nothing, errmsg already given */
12300 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012301 EMSGN(_(e_listidx), idx);
12302 else
12303 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012304 if (argvars[2].v_type == VAR_UNKNOWN)
12305 {
12306 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012307 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012308 *rettv = item->li_tv;
12309 vim_free(item);
12310 }
12311 else
12312 {
12313 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012314 end = get_tv_number_chk(&argvars[2], &error);
12315 if (error)
12316 ; /* type error: do nothing */
12317 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012318 EMSGN(_(e_listidx), end);
12319 else
12320 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012321 int cnt = 0;
12322
12323 for (li = item; li != NULL; li = li->li_next)
12324 {
12325 ++cnt;
12326 if (li == item2)
12327 break;
12328 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012329 if (li == NULL) /* didn't find "item2" after "item" */
12330 EMSG(_(e_invrange));
12331 else
12332 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000012333 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012334 l = list_alloc();
12335 if (l != NULL)
12336 {
12337 rettv->v_type = VAR_LIST;
12338 rettv->vval.v_list = l;
12339 l->lv_first = item;
12340 l->lv_last = item2;
12341 l->lv_refcount = 1;
12342 item->li_prev = NULL;
12343 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012344 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012345 }
12346 }
12347 }
12348 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012349 }
12350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012351}
12352
12353/*
12354 * "rename({from}, {to})" function
12355 */
12356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012357f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012358 typval_T *argvars;
12359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360{
12361 char_u buf[NUMBUFLEN];
12362
12363 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012364 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012366 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
12367 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012368}
12369
12370/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012371 * "repeat()" function
12372 */
12373/*ARGSUSED*/
12374 static void
12375f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012376 typval_T *argvars;
12377 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012378{
12379 char_u *p;
12380 int n;
12381 int slen;
12382 int len;
12383 char_u *r;
12384 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000012385 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012386
12387 n = get_tv_number(&argvars[1]);
12388 if (argvars[0].v_type == VAR_LIST)
12389 {
12390 l = list_alloc();
12391 if (l != NULL && argvars[0].vval.v_list != NULL)
12392 {
12393 l->lv_refcount = 1;
12394 while (n-- > 0)
12395 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
12396 break;
12397 }
12398 rettv->v_type = VAR_LIST;
12399 rettv->vval.v_list = l;
12400 }
12401 else
12402 {
12403 p = get_tv_string(&argvars[0]);
12404 rettv->v_type = VAR_STRING;
12405 rettv->vval.v_string = NULL;
12406
12407 slen = (int)STRLEN(p);
12408 len = slen * n;
12409 if (len <= 0)
12410 return;
12411
12412 r = alloc(len + 1);
12413 if (r != NULL)
12414 {
12415 for (i = 0; i < n; i++)
12416 mch_memmove(r + i * slen, p, (size_t)slen);
12417 r[len] = NUL;
12418 }
12419
12420 rettv->vval.v_string = r;
12421 }
12422}
12423
12424/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012425 * "resolve()" function
12426 */
12427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012428f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012429 typval_T *argvars;
12430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431{
12432 char_u *p;
12433
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012434 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435#ifdef FEAT_SHORTCUT
12436 {
12437 char_u *v = NULL;
12438
12439 v = mch_resolve_shortcut(p);
12440 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012441 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012443 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444 }
12445#else
12446# ifdef HAVE_READLINK
12447 {
12448 char_u buf[MAXPATHL + 1];
12449 char_u *cpy;
12450 int len;
12451 char_u *remain = NULL;
12452 char_u *q;
12453 int is_relative_to_current = FALSE;
12454 int has_trailing_pathsep = FALSE;
12455 int limit = 100;
12456
12457 p = vim_strsave(p);
12458
12459 if (p[0] == '.' && (vim_ispathsep(p[1])
12460 || (p[1] == '.' && (vim_ispathsep(p[2])))))
12461 is_relative_to_current = TRUE;
12462
12463 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012464 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012465 has_trailing_pathsep = TRUE;
12466
12467 q = getnextcomp(p);
12468 if (*q != NUL)
12469 {
12470 /* Separate the first path component in "p", and keep the
12471 * remainder (beginning with the path separator). */
12472 remain = vim_strsave(q - 1);
12473 q[-1] = NUL;
12474 }
12475
12476 for (;;)
12477 {
12478 for (;;)
12479 {
12480 len = readlink((char *)p, (char *)buf, MAXPATHL);
12481 if (len <= 0)
12482 break;
12483 buf[len] = NUL;
12484
12485 if (limit-- == 0)
12486 {
12487 vim_free(p);
12488 vim_free(remain);
12489 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012490 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491 goto fail;
12492 }
12493
12494 /* Ensure that the result will have a trailing path separator
12495 * if the argument has one. */
12496 if (remain == NULL && has_trailing_pathsep)
12497 add_pathsep(buf);
12498
12499 /* Separate the first path component in the link value and
12500 * concatenate the remainders. */
12501 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
12502 if (*q != NUL)
12503 {
12504 if (remain == NULL)
12505 remain = vim_strsave(q - 1);
12506 else
12507 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012508 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509 if (cpy != NULL)
12510 {
12511 STRCAT(cpy, remain);
12512 vim_free(remain);
12513 remain = cpy;
12514 }
12515 }
12516 q[-1] = NUL;
12517 }
12518
12519 q = gettail(p);
12520 if (q > p && *q == NUL)
12521 {
12522 /* Ignore trailing path separator. */
12523 q[-1] = NUL;
12524 q = gettail(p);
12525 }
12526 if (q > p && !mch_isFullName(buf))
12527 {
12528 /* symlink is relative to directory of argument */
12529 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
12530 if (cpy != NULL)
12531 {
12532 STRCPY(cpy, p);
12533 STRCPY(gettail(cpy), buf);
12534 vim_free(p);
12535 p = cpy;
12536 }
12537 }
12538 else
12539 {
12540 vim_free(p);
12541 p = vim_strsave(buf);
12542 }
12543 }
12544
12545 if (remain == NULL)
12546 break;
12547
12548 /* Append the first path component of "remain" to "p". */
12549 q = getnextcomp(remain + 1);
12550 len = q - remain - (*q != NUL);
12551 cpy = vim_strnsave(p, STRLEN(p) + len);
12552 if (cpy != NULL)
12553 {
12554 STRNCAT(cpy, remain, len);
12555 vim_free(p);
12556 p = cpy;
12557 }
12558 /* Shorten "remain". */
12559 if (*q != NUL)
12560 STRCPY(remain, q - 1);
12561 else
12562 {
12563 vim_free(remain);
12564 remain = NULL;
12565 }
12566 }
12567
12568 /* If the result is a relative path name, make it explicitly relative to
12569 * the current directory if and only if the argument had this form. */
12570 if (!vim_ispathsep(*p))
12571 {
12572 if (is_relative_to_current
12573 && *p != NUL
12574 && !(p[0] == '.'
12575 && (p[1] == NUL
12576 || vim_ispathsep(p[1])
12577 || (p[1] == '.'
12578 && (p[2] == NUL
12579 || vim_ispathsep(p[2]))))))
12580 {
12581 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000012582 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583 if (cpy != NULL)
12584 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585 vim_free(p);
12586 p = cpy;
12587 }
12588 }
12589 else if (!is_relative_to_current)
12590 {
12591 /* Strip leading "./". */
12592 q = p;
12593 while (q[0] == '.' && vim_ispathsep(q[1]))
12594 q += 2;
12595 if (q > p)
12596 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
12597 }
12598 }
12599
12600 /* Ensure that the result will have no trailing path separator
12601 * if the argument had none. But keep "/" or "//". */
12602 if (!has_trailing_pathsep)
12603 {
12604 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000012605 if (after_pathsep(p, q))
12606 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012607 }
12608
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012609 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012610 }
12611# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012612 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613# endif
12614#endif
12615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012616 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617
12618#ifdef HAVE_READLINK
12619fail:
12620#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012621 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012622}
12623
12624/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012625 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626 */
12627 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012628f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012629 typval_T *argvars;
12630 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631{
Bram Moolenaar33570922005-01-25 22:26:29 +000012632 list_T *l;
12633 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634
Bram Moolenaar0d660222005-01-07 21:51:51 +000012635 rettv->vval.v_number = 0;
12636 if (argvars[0].v_type != VAR_LIST)
12637 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012638 else if ((l = argvars[0].vval.v_list) != NULL
12639 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012640 {
12641 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000012642 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012643 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012644 while (li != NULL)
12645 {
12646 ni = li->li_prev;
12647 list_append(l, li);
12648 li = ni;
12649 }
12650 rettv->vval.v_list = l;
12651 rettv->v_type = VAR_LIST;
12652 ++l->lv_refcount;
12653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654}
12655
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012656#define SP_NOMOVE 1 /* don't move cursor */
12657#define SP_REPEAT 2 /* repeat to find outer pair */
12658#define SP_RETCOUNT 4 /* return matchcount */
12659
Bram Moolenaar33570922005-01-25 22:26:29 +000012660static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012661
12662/*
12663 * Get flags for a search function.
12664 * Possibly sets "p_ws".
12665 * Returns BACKWARD, FORWARD or zero (for an error).
12666 */
12667 static int
12668get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000012669 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012670 int *flagsp;
12671{
12672 int dir = FORWARD;
12673 char_u *flags;
12674 char_u nbuf[NUMBUFLEN];
12675 int mask;
12676
12677 if (varp->v_type != VAR_UNKNOWN)
12678 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012679 flags = get_tv_string_buf_chk(varp, nbuf);
12680 if (flags == NULL)
12681 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012682 while (*flags != NUL)
12683 {
12684 switch (*flags)
12685 {
12686 case 'b': dir = BACKWARD; break;
12687 case 'w': p_ws = TRUE; break;
12688 case 'W': p_ws = FALSE; break;
12689 default: mask = 0;
12690 if (flagsp != NULL)
12691 switch (*flags)
12692 {
12693 case 'n': mask = SP_NOMOVE; break;
12694 case 'r': mask = SP_REPEAT; break;
12695 case 'm': mask = SP_RETCOUNT; break;
12696 }
12697 if (mask == 0)
12698 {
12699 EMSG2(_(e_invarg2), flags);
12700 dir = 0;
12701 }
12702 else
12703 *flagsp |= mask;
12704 }
12705 if (dir == 0)
12706 break;
12707 ++flags;
12708 }
12709 }
12710 return dir;
12711}
12712
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713/*
12714 * "search()" function
12715 */
12716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012717f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012718 typval_T *argvars;
12719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720{
12721 char_u *pat;
12722 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012723 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724 int save_p_ws = p_ws;
12725 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012726 int flags = 0;
12727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012728 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012730 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012731 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
12732 if (dir == 0)
12733 goto theend;
12734 if ((flags & ~SP_NOMOVE) != 0)
12735 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012736 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012737 goto theend;
12738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012740 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
12742 SEARCH_KEEP, RE_SEARCH) != FAIL)
12743 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012744 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745 curwin->w_cursor = pos;
12746 /* "/$" will put the cursor after the end of the line, may need to
12747 * correct that here */
12748 check_cursor();
12749 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012750
12751 /* If 'n' flag is used: restore cursor position. */
12752 if (flags & SP_NOMOVE)
12753 curwin->w_cursor = save_cursor;
12754theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755 p_ws = save_p_ws;
12756}
12757
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758/*
12759 * "searchpair()" function
12760 */
12761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012763 typval_T *argvars;
12764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765{
12766 char_u *spat, *mpat, *epat;
12767 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769 int dir;
12770 int flags = 0;
12771 char_u nbuf1[NUMBUFLEN];
12772 char_u nbuf2[NUMBUFLEN];
12773 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012775 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012778 spat = get_tv_string_chk(&argvars[0]);
12779 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
12780 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
12781 if (spat == NULL || mpat == NULL || epat == NULL)
12782 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012783
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784 /* Handle the optional fourth argument: flags */
12785 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012786 if (dir == 0)
12787 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788
12789 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012790 if (argvars[3].v_type == VAR_UNKNOWN
12791 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 skip = (char_u *)"";
12793 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012794 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
12795 if (skip == NULL)
12796 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012798 rettv->vval.v_number = do_searchpair(spat, mpat, epat, dir, skip, flags);
12799
12800theend:
12801 p_ws = save_p_ws;
12802}
12803
12804/*
12805 * Search for a start/middle/end thing.
12806 * Used by searchpair(), see its documentation for the details.
12807 * Returns 0 or -1 for no match,
12808 */
12809 long
12810do_searchpair(spat, mpat, epat, dir, skip, flags)
12811 char_u *spat; /* start pattern */
12812 char_u *mpat; /* middle pattern */
12813 char_u *epat; /* end pattern */
12814 int dir; /* BACKWARD or FORWARD */
12815 char_u *skip; /* skip expression */
12816 int flags; /* SP_RETCOUNT, SP_REPEAT, SP_NOMOVE */
12817{
12818 char_u *save_cpo;
12819 char_u *pat, *pat2 = NULL, *pat3 = NULL;
12820 long retval = 0;
12821 pos_T pos;
12822 pos_T firstpos;
12823 pos_T foundpos;
12824 pos_T save_cursor;
12825 pos_T save_pos;
12826 int n;
12827 int r;
12828 int nest = 1;
12829 int err;
12830
12831 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12832 save_cpo = p_cpo;
12833 p_cpo = (char_u *)"";
12834
12835 /* Make two search patterns: start/end (pat2, for in nested pairs) and
12836 * start/middle/end (pat3, for the top pair). */
12837 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
12838 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
12839 if (pat2 == NULL || pat3 == NULL)
12840 goto theend;
12841 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
12842 if (*mpat == NUL)
12843 STRCPY(pat3, pat2);
12844 else
12845 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
12846 spat, epat, mpat);
12847
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 save_cursor = curwin->w_cursor;
12849 pos = curwin->w_cursor;
12850 firstpos.lnum = 0;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012851 foundpos.lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 pat = pat3;
12853 for (;;)
12854 {
12855 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
12856 SEARCH_KEEP, RE_SEARCH);
12857 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
12858 /* didn't find it or found the first match again: FAIL */
12859 break;
12860
12861 if (firstpos.lnum == 0)
12862 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000012863 if (equalpos(pos, foundpos))
12864 {
12865 /* Found the same position again. Can happen with a pattern that
12866 * has "\zs" at the end and searching backwards. Advance one
12867 * character and try again. */
12868 if (dir == BACKWARD)
12869 decl(&pos);
12870 else
12871 incl(&pos);
12872 }
12873 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874
12875 /* If the skip pattern matches, ignore this match. */
12876 if (*skip != NUL)
12877 {
12878 save_pos = curwin->w_cursor;
12879 curwin->w_cursor = pos;
12880 r = eval_to_bool(skip, &err, NULL, FALSE);
12881 curwin->w_cursor = save_pos;
12882 if (err)
12883 {
12884 /* Evaluating {skip} caused an error, break here. */
12885 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012886 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887 break;
12888 }
12889 if (r)
12890 continue;
12891 }
12892
12893 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
12894 {
12895 /* Found end when searching backwards or start when searching
12896 * forward: nested pair. */
12897 ++nest;
12898 pat = pat2; /* nested, don't search for middle */
12899 }
12900 else
12901 {
12902 /* Found end when searching forward or start when searching
12903 * backward: end of (nested) pair; or found middle in outer pair. */
12904 if (--nest == 1)
12905 pat = pat3; /* outer level, search for middle */
12906 }
12907
12908 if (nest == 0)
12909 {
12910 /* Found the match: return matchcount or line number. */
12911 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012912 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012914 retval = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012915 curwin->w_cursor = pos;
12916 if (!(flags & SP_REPEAT))
12917 break;
12918 nest = 1; /* search for next unmatched */
12919 }
12920 }
12921
12922 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012923 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924 curwin->w_cursor = save_cursor;
12925
12926theend:
12927 vim_free(pat2);
12928 vim_free(pat3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929 p_cpo = save_cpo;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000012930
12931 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932}
12933
Bram Moolenaar0d660222005-01-07 21:51:51 +000012934/*ARGSUSED*/
12935 static void
12936f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012937 typval_T *argvars;
12938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012939{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012940#ifdef FEAT_CLIENTSERVER
12941 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012942 char_u *server = get_tv_string_chk(&argvars[0]);
12943 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012944
Bram Moolenaar0d660222005-01-07 21:51:51 +000012945 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012946 if (server == NULL || reply == NULL)
12947 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012948 if (check_restricted() || check_secure())
12949 return;
12950# ifdef FEAT_X11
12951 if (check_connection() == FAIL)
12952 return;
12953# endif
12954
12955 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012957 EMSG(_("E258: Unable to send to client"));
12958 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012959 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012960 rettv->vval.v_number = 0;
12961#else
12962 rettv->vval.v_number = -1;
12963#endif
12964}
12965
12966/*ARGSUSED*/
12967 static void
12968f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012969 typval_T *argvars;
12970 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012971{
12972 char_u *r = NULL;
12973
12974#ifdef FEAT_CLIENTSERVER
12975# ifdef WIN32
12976 r = serverGetVimNames();
12977# else
12978 make_connection();
12979 if (X_DISPLAY != NULL)
12980 r = serverGetVimNames(X_DISPLAY);
12981# endif
12982#endif
12983 rettv->v_type = VAR_STRING;
12984 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012985}
12986
12987/*
12988 * "setbufvar()" function
12989 */
12990/*ARGSUSED*/
12991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012992f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012993 typval_T *argvars;
12994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995{
12996 buf_T *buf;
12997#ifdef FEAT_AUTOCMD
12998 aco_save_T aco;
12999#else
13000 buf_T *save_curbuf;
13001#endif
13002 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013003 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004 char_u nbuf[NUMBUFLEN];
13005
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013006 rettv->vval.v_number = 0;
13007
Bram Moolenaar071d4272004-06-13 20:20:40 +000013008 if (check_restricted() || check_secure())
13009 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013010 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
13011 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013012 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013013 varp = &argvars[2];
13014
13015 if (buf != NULL && varname != NULL && varp != NULL)
13016 {
13017 /* set curbuf to be our buf, temporarily */
13018#ifdef FEAT_AUTOCMD
13019 aucmd_prepbuf(&aco, buf);
13020#else
13021 save_curbuf = curbuf;
13022 curbuf = buf;
13023#endif
13024
13025 if (*varname == '&')
13026 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013027 long numval;
13028 char_u *strval;
13029 int error = FALSE;
13030
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013032 numval = get_tv_number_chk(varp, &error);
13033 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013034 if (!error && strval != NULL)
13035 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013036 }
13037 else
13038 {
13039 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
13040 if (bufvarname != NULL)
13041 {
13042 STRCPY(bufvarname, "b:");
13043 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013044 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045 vim_free(bufvarname);
13046 }
13047 }
13048
13049 /* reset notion of buffer */
13050#ifdef FEAT_AUTOCMD
13051 aucmd_restbuf(&aco);
13052#else
13053 curbuf = save_curbuf;
13054#endif
13055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013056}
13057
13058/*
13059 * "setcmdpos()" function
13060 */
13061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013062f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013063 typval_T *argvars;
13064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013065{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013066 int pos = (int)get_tv_number(&argvars[0]) - 1;
13067
13068 if (pos >= 0)
13069 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070}
13071
13072/*
13073 * "setline()" function
13074 */
13075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013076f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013077 typval_T *argvars;
13078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079{
13080 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000013081 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013082 list_T *l = NULL;
13083 listitem_T *li = NULL;
13084 long added = 0;
13085 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013086
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013087 lnum = get_tv_lnum(&argvars[0]);
13088 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013089 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013090 l = argvars[1].vval.v_list;
13091 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013092 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013093 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013094 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013095
13096 rettv->vval.v_number = 0; /* OK */
13097 for (;;)
13098 {
13099 if (l != NULL)
13100 {
13101 /* list argument, get next string */
13102 if (li == NULL)
13103 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013104 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013105 li = li->li_next;
13106 }
13107
13108 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013109 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013110 break;
13111 if (lnum <= curbuf->b_ml.ml_line_count)
13112 {
13113 /* existing line, replace it */
13114 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
13115 {
13116 changed_bytes(lnum, 0);
13117 check_cursor_col();
13118 rettv->vval.v_number = 0; /* OK */
13119 }
13120 }
13121 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
13122 {
13123 /* lnum is one past the last line, append the line */
13124 ++added;
13125 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
13126 rettv->vval.v_number = 0; /* OK */
13127 }
13128
13129 if (l == NULL) /* only one string argument */
13130 break;
13131 ++lnum;
13132 }
13133
13134 if (added > 0)
13135 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013136}
13137
13138/*
Bram Moolenaar2641f772005-03-25 21:58:17 +000013139 * "setqflist()" function
13140 */
13141/*ARGSUSED*/
13142 static void
13143f_setqflist(argvars, rettv)
13144 typval_T *argvars;
13145 typval_T *rettv;
13146{
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013147 char_u *act;
13148 int action = ' ';
13149
Bram Moolenaar2641f772005-03-25 21:58:17 +000013150 rettv->vval.v_number = -1;
13151
13152#ifdef FEAT_QUICKFIX
13153 if (argvars[0].v_type != VAR_LIST)
13154 EMSG(_(e_listreq));
13155 else
13156 {
13157 list_T *l = argvars[0].vval.v_list;
13158
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013159 if (argvars[1].v_type == VAR_STRING)
13160 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013161 act = get_tv_string_chk(&argvars[1]);
13162 if (act == NULL)
13163 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000013164 if (*act == 'a' || *act == 'r')
13165 action = *act;
13166 }
13167
13168 if (l != NULL && set_errorlist(l, action) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013169 rettv->vval.v_number = 0;
13170 }
13171#endif
13172}
13173
13174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175 * "setreg()" function
13176 */
13177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013178f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013179 typval_T *argvars;
13180 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181{
13182 int regname;
13183 char_u *strregname;
13184 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013185 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186 int append;
13187 char_u yank_type;
13188 long block_len;
13189
13190 block_len = -1;
13191 yank_type = MAUTO;
13192 append = FALSE;
13193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013194 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013195 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013197 if (strregname == NULL)
13198 return; /* type error; errmsg already given */
13199 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013200 if (regname == 0 || regname == '@')
13201 regname = '"';
13202 else if (regname == '=')
13203 return;
13204
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013205 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013206 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013207 stropt = get_tv_string_chk(&argvars[2]);
13208 if (stropt == NULL)
13209 return; /* type error */
13210 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211 switch (*stropt)
13212 {
13213 case 'a': case 'A': /* append */
13214 append = TRUE;
13215 break;
13216 case 'v': case 'c': /* character-wise selection */
13217 yank_type = MCHAR;
13218 break;
13219 case 'V': case 'l': /* line-wise selection */
13220 yank_type = MLINE;
13221 break;
13222#ifdef FEAT_VISUAL
13223 case 'b': case Ctrl_V: /* block-wise selection */
13224 yank_type = MBLOCK;
13225 if (VIM_ISDIGIT(stropt[1]))
13226 {
13227 ++stropt;
13228 block_len = getdigits(&stropt) - 1;
13229 --stropt;
13230 }
13231 break;
13232#endif
13233 }
13234 }
13235
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013236 strval = get_tv_string_chk(&argvars[1]);
13237 if (strval != NULL)
13238 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013239 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013240 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013241}
13242
13243
13244/*
13245 * "setwinvar(expr)" function
13246 */
13247/*ARGSUSED*/
13248 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013249f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013250 typval_T *argvars;
13251 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013252{
13253 win_T *win;
13254#ifdef FEAT_WINDOWS
13255 win_T *save_curwin;
13256#endif
13257 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013258 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259 char_u nbuf[NUMBUFLEN];
13260
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013261 rettv->vval.v_number = 0;
13262
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263 if (check_restricted() || check_secure())
13264 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265 win = find_win_by_nr(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013266 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013267 varp = &argvars[2];
13268
13269 if (win != NULL && varname != NULL && varp != NULL)
13270 {
13271#ifdef FEAT_WINDOWS
13272 /* set curwin to be our win, temporarily */
13273 save_curwin = curwin;
13274 curwin = win;
13275 curbuf = curwin->w_buffer;
13276#endif
13277
13278 if (*varname == '&')
13279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013280 long numval;
13281 char_u *strval;
13282 int error = FALSE;
13283
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013285 numval = get_tv_number_chk(varp, &error);
13286 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013287 if (!error && strval != NULL)
13288 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289 }
13290 else
13291 {
13292 winvarname = alloc((unsigned)STRLEN(varname) + 3);
13293 if (winvarname != NULL)
13294 {
13295 STRCPY(winvarname, "w:");
13296 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000013297 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013298 vim_free(winvarname);
13299 }
13300 }
13301
13302#ifdef FEAT_WINDOWS
13303 /* Restore current window, if it's still valid (autocomands can make
13304 * it invalid). */
13305 if (win_valid(save_curwin))
13306 {
13307 curwin = save_curwin;
13308 curbuf = curwin->w_buffer;
13309 }
13310#endif
13311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013312}
13313
13314/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013315 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316 */
13317 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013318f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013319 typval_T *argvars;
13320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013322 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323
Bram Moolenaar0d660222005-01-07 21:51:51 +000013324 p = get_tv_string(&argvars[0]);
13325 rettv->vval.v_string = vim_strsave(p);
13326 simplify_filename(rettv->vval.v_string); /* simplify in place */
13327 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013328}
13329
Bram Moolenaar0d660222005-01-07 21:51:51 +000013330static int
13331#ifdef __BORLANDC__
13332 _RTLENTRYF
13333#endif
13334 item_compare __ARGS((const void *s1, const void *s2));
13335static int
13336#ifdef __BORLANDC__
13337 _RTLENTRYF
13338#endif
13339 item_compare2 __ARGS((const void *s1, const void *s2));
13340
13341static int item_compare_ic;
13342static char_u *item_compare_func;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013343static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013344#define ITEM_COMPARE_FAIL 999
13345
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013347 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013349 static int
13350#ifdef __BORLANDC__
13351_RTLENTRYF
13352#endif
13353item_compare(s1, s2)
13354 const void *s1;
13355 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013356{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013357 char_u *p1, *p2;
13358 char_u *tofree1, *tofree2;
13359 int res;
13360 char_u numbuf1[NUMBUFLEN];
13361 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013362
Bram Moolenaar33570922005-01-25 22:26:29 +000013363 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
13364 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013365 if (item_compare_ic)
13366 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013368 res = STRCMP(p1, p2);
13369 vim_free(tofree1);
13370 vim_free(tofree2);
13371 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372}
13373
13374 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000013375#ifdef __BORLANDC__
13376_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000013378item_compare2(s1, s2)
13379 const void *s1;
13380 const void *s2;
13381{
13382 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000013383 typval_T rettv;
13384 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000013385 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013386
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013387 /* shortcut after failure in previous call; compare all items equal */
13388 if (item_compare_func_err)
13389 return 0;
13390
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013391 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
13392 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013393 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
13394 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013395
13396 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
13397 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000013398 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013399 clear_tv(&argv[0]);
13400 clear_tv(&argv[1]);
13401
13402 if (res == FAIL)
13403 res = ITEM_COMPARE_FAIL;
13404 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013405 /* return value has wrong type */
13406 res = get_tv_number_chk(&rettv, &item_compare_func_err);
13407 if (item_compare_func_err)
13408 res = ITEM_COMPARE_FAIL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013409 clear_tv(&rettv);
13410 return res;
13411}
13412
13413/*
13414 * "sort({list})" function
13415 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013417f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013418 typval_T *argvars;
13419 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013420{
Bram Moolenaar33570922005-01-25 22:26:29 +000013421 list_T *l;
13422 listitem_T *li;
13423 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013424 long len;
13425 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426
Bram Moolenaar0d660222005-01-07 21:51:51 +000013427 rettv->vval.v_number = 0;
13428 if (argvars[0].v_type != VAR_LIST)
13429 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 else
13431 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013432 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013433 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013434 return;
13435 rettv->vval.v_list = l;
13436 rettv->v_type = VAR_LIST;
13437 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438
Bram Moolenaar0d660222005-01-07 21:51:51 +000013439 len = list_len(l);
13440 if (len <= 1)
13441 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442
Bram Moolenaar0d660222005-01-07 21:51:51 +000013443 item_compare_ic = FALSE;
13444 item_compare_func = NULL;
13445 if (argvars[1].v_type != VAR_UNKNOWN)
13446 {
13447 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013448 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013449 else
13450 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013451 int error = FALSE;
13452
13453 i = get_tv_number_chk(&argvars[1], &error);
13454 if (error)
13455 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000013456 if (i == 1)
13457 item_compare_ic = TRUE;
13458 else
13459 item_compare_func = get_tv_string(&argvars[1]);
13460 }
13461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462
Bram Moolenaar0d660222005-01-07 21:51:51 +000013463 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013464 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013465 if (ptrs == NULL)
13466 return;
13467 i = 0;
13468 for (li = l->lv_first; li != NULL; li = li->li_next)
13469 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013471 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013472 /* test the compare function */
13473 if (item_compare_func != NULL
13474 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
13475 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013476 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013477 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000013478 {
13479 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000013480 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000013481 item_compare_func == NULL ? item_compare : item_compare2);
13482
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013483 if (!item_compare_func_err)
13484 {
13485 /* Clear the List and append the items in the sorted order. */
13486 l->lv_first = l->lv_last = NULL;
13487 l->lv_len = 0;
13488 for (i = 0; i < len; ++i)
13489 list_append(l, ptrs[i]);
13490 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013491 }
13492
13493 vim_free(ptrs);
13494 }
13495}
13496
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013497/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000013498 * "soundfold({word})" function
13499 */
13500 static void
13501f_soundfold(argvars, rettv)
13502 typval_T *argvars;
13503 typval_T *rettv;
13504{
13505 char_u *s;
13506
13507 rettv->v_type = VAR_STRING;
13508 s = get_tv_string(&argvars[0]);
13509#ifdef FEAT_SYN_HL
13510 rettv->vval.v_string = eval_soundfold(s);
13511#else
13512 rettv->vval.v_string = vim_strsave(s);
13513#endif
13514}
13515
13516/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013517 * "spellbadword()" function
13518 */
13519/* ARGSUSED */
13520 static void
13521f_spellbadword(argvars, rettv)
13522 typval_T *argvars;
13523 typval_T *rettv;
13524{
13525 int attr;
13526 char_u *ptr;
13527 int len;
13528
13529 rettv->vval.v_string = NULL;
13530 rettv->v_type = VAR_STRING;
13531
13532#ifdef FEAT_SYN_HL
13533 /* Find the start of the badly spelled word. */
13534 if (spell_move_to(FORWARD, TRUE, TRUE) == FAIL)
13535 return;
13536
13537 /* Get the length of the word and copy it. */
13538 ptr = ml_get_cursor();
Bram Moolenaar51ac12f2005-07-02 23:21:11 +000013539 len = spell_check(curwin, ptr, &attr, NULL);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000013540 rettv->vval.v_string = vim_strnsave(ptr, len);
13541#endif
13542}
13543
13544/*
13545 * "spellsuggest()" function
13546 */
13547 static void
13548f_spellsuggest(argvars, rettv)
13549 typval_T *argvars;
13550 typval_T *rettv;
13551{
13552 char_u *str;
13553 int maxcount;
13554 garray_T ga;
13555 list_T *l;
13556 listitem_T *li;
13557 int i;
13558
13559 l = list_alloc();
13560 if (l == NULL)
13561 return;
13562 rettv->v_type = VAR_LIST;
13563 rettv->vval.v_list = l;
13564 ++l->lv_refcount;
13565
13566#ifdef FEAT_SYN_HL
13567 if (curwin->w_p_spell && *curbuf->b_p_spl != NUL)
13568 {
13569 str = get_tv_string(&argvars[0]);
13570 if (argvars[1].v_type != VAR_UNKNOWN)
13571 {
13572 maxcount = get_tv_number(&argvars[1]);
13573 if (maxcount <= 0)
13574 return;
13575 }
13576 else
13577 maxcount = 25;
13578
13579 spell_suggest_list(&ga, str, maxcount);
13580
13581 for (i = 0; i < ga.ga_len; ++i)
13582 {
13583 str = ((char_u **)ga.ga_data)[i];
13584
13585 li = listitem_alloc();
13586 if (li == NULL)
13587 vim_free(str);
13588 else
13589 {
13590 li->li_tv.v_type = VAR_STRING;
13591 li->li_tv.v_lock = 0;
13592 li->li_tv.vval.v_string = str;
13593 list_append(l, li);
13594 }
13595 }
13596 ga_clear(&ga);
13597 }
13598#endif
13599}
13600
Bram Moolenaar0d660222005-01-07 21:51:51 +000013601 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013602f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013603 typval_T *argvars;
13604 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013605{
13606 char_u *str;
13607 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013608 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013609 regmatch_T regmatch;
13610 char_u patbuf[NUMBUFLEN];
13611 char_u *save_cpo;
13612 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000013613 listitem_T *ni;
13614 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013615 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013616 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013617 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013618
13619 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13620 save_cpo = p_cpo;
13621 p_cpo = (char_u *)"";
13622
13623 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013624 if (argvars[1].v_type != VAR_UNKNOWN)
13625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013626 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13627 if (pat == NULL)
13628 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013629 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013630 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013631 }
13632 if (pat == NULL || *pat == NUL)
13633 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000013634
13635 l = list_alloc();
13636 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013638 rettv->v_type = VAR_LIST;
13639 rettv->vval.v_list = l;
13640 ++l->lv_refcount;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013641 if (typeerr)
13642 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643
Bram Moolenaar0d660222005-01-07 21:51:51 +000013644 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13645 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013647 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013648 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013649 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013650 if (*str == NUL)
13651 match = FALSE; /* empty item at the end */
13652 else
13653 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013654 if (match)
13655 end = regmatch.startp[0];
13656 else
13657 end = str + STRLEN(str);
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013658 if (keepempty || end > str || (l->lv_len > 0 && *str != NUL
13659 && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000013660 {
13661 ni = listitem_alloc();
13662 if (ni == NULL)
13663 break;
13664 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013665 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013666 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
13667 list_append(l, ni);
13668 }
13669 if (!match)
13670 break;
13671 /* Advance to just after the match. */
13672 if (regmatch.endp[0] > str)
13673 col = 0;
13674 else
13675 {
13676 /* Don't get stuck at the same match. */
13677#ifdef FEAT_MBYTE
13678 col = mb_ptr2len_check(regmatch.endp[0]);
13679#else
13680 col = 1;
13681#endif
13682 }
13683 str = regmatch.endp[0];
13684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013685
Bram Moolenaar0d660222005-01-07 21:51:51 +000013686 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013688
Bram Moolenaar0d660222005-01-07 21:51:51 +000013689 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013690}
13691
13692#ifdef HAVE_STRFTIME
13693/*
13694 * "strftime({format}[, {time}])" function
13695 */
13696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013697f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013698 typval_T *argvars;
13699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013700{
13701 char_u result_buf[256];
13702 struct tm *curtime;
13703 time_t seconds;
13704 char_u *p;
13705
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013706 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013708 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013709 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710 seconds = time(NULL);
13711 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013712 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713 curtime = localtime(&seconds);
13714 /* MSVC returns NULL for an invalid value of seconds. */
13715 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013716 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717 else
13718 {
13719# ifdef FEAT_MBYTE
13720 vimconv_T conv;
13721 char_u *enc;
13722
13723 conv.vc_type = CONV_NONE;
13724 enc = enc_locale();
13725 convert_setup(&conv, p_enc, enc);
13726 if (conv.vc_type != CONV_NONE)
13727 p = string_convert(&conv, p, NULL);
13728# endif
13729 if (p != NULL)
13730 (void)strftime((char *)result_buf, sizeof(result_buf),
13731 (char *)p, curtime);
13732 else
13733 result_buf[0] = NUL;
13734
13735# ifdef FEAT_MBYTE
13736 if (conv.vc_type != CONV_NONE)
13737 vim_free(p);
13738 convert_setup(&conv, enc, p_enc);
13739 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013740 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741 else
13742# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013743 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013744
13745# ifdef FEAT_MBYTE
13746 /* Release conversion descriptors */
13747 convert_setup(&conv, NULL, NULL);
13748 vim_free(enc);
13749# endif
13750 }
13751}
13752#endif
13753
13754/*
13755 * "stridx()" function
13756 */
13757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013758f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013759 typval_T *argvars;
13760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013761{
13762 char_u buf[NUMBUFLEN];
13763 char_u *needle;
13764 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000013765 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000013767 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013769 needle = get_tv_string_chk(&argvars[1]);
13770 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000013771 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013772 if (needle == NULL || haystack == NULL)
13773 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774
Bram Moolenaar33570922005-01-25 22:26:29 +000013775 if (argvars[2].v_type != VAR_UNKNOWN)
13776 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013777 int error = FALSE;
13778
13779 start_idx = get_tv_number_chk(&argvars[2], &error);
13780 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000013781 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013782 if (start_idx >= 0)
13783 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000013784 }
13785
13786 pos = (char_u *)strstr((char *)haystack, (char *)needle);
13787 if (pos != NULL)
13788 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013789}
13790
13791/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013792 * "string()" function
13793 */
13794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013795f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013796 typval_T *argvars;
13797 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013798{
13799 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013800 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013802 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013803 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013804 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013805 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806}
13807
13808/*
13809 * "strlen()" function
13810 */
13811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013812f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013813 typval_T *argvars;
13814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013816 rettv->vval.v_number = (varnumber_T)(STRLEN(
13817 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818}
13819
13820/*
13821 * "strpart()" function
13822 */
13823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013824f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013825 typval_T *argvars;
13826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827{
13828 char_u *p;
13829 int n;
13830 int len;
13831 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013832 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013834 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835 slen = (int)STRLEN(p);
13836
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013837 n = get_tv_number_chk(&argvars[1], &error);
13838 if (error)
13839 len = 0;
13840 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013841 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013842 else
13843 len = slen - n; /* default len: all bytes that are available. */
13844
13845 /*
13846 * Only return the overlap between the specified part and the actual
13847 * string.
13848 */
13849 if (n < 0)
13850 {
13851 len += n;
13852 n = 0;
13853 }
13854 else if (n > slen)
13855 n = slen;
13856 if (len < 0)
13857 len = 0;
13858 else if (n + len > slen)
13859 len = slen - n;
13860
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013861 rettv->v_type = VAR_STRING;
13862 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863}
13864
13865/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013866 * "strridx()" function
13867 */
13868 static void
13869f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013870 typval_T *argvars;
13871 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013872{
13873 char_u buf[NUMBUFLEN];
13874 char_u *needle;
13875 char_u *haystack;
13876 char_u *rest;
13877 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000013878 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013879
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013880 needle = get_tv_string_chk(&argvars[1]);
13881 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013882 haystack_len = STRLEN(haystack);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013883
13884 rettv->vval.v_number = -1;
13885 if (needle == NULL || haystack == NULL)
13886 return; /* type error; errmsg already given */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013887 if (argvars[2].v_type != VAR_UNKNOWN)
13888 {
13889 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013890 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000013891 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013892 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013893 }
13894 else
13895 end_idx = haystack_len;
13896
Bram Moolenaar0d660222005-01-07 21:51:51 +000013897 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000013898 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013899 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000013900 lastmatch = haystack + end_idx;
13901 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013902 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000013903 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013904 for (rest = haystack; *rest != '\0'; ++rest)
13905 {
13906 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000013907 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013908 break;
13909 lastmatch = rest;
13910 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000013911 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013912
13913 if (lastmatch == NULL)
13914 rettv->vval.v_number = -1;
13915 else
13916 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
13917}
13918
13919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920 * "strtrans()" function
13921 */
13922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013923f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013924 typval_T *argvars;
13925 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013926{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013927 rettv->v_type = VAR_STRING;
13928 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013929}
13930
13931/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013932 * "submatch()" function
13933 */
13934 static void
13935f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013936 typval_T *argvars;
13937 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013938{
13939 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013940 rettv->vval.v_string =
13941 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000013942}
13943
13944/*
13945 * "substitute()" function
13946 */
13947 static void
13948f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013949 typval_T *argvars;
13950 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013951{
13952 char_u patbuf[NUMBUFLEN];
13953 char_u subbuf[NUMBUFLEN];
13954 char_u flagsbuf[NUMBUFLEN];
13955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013956 char_u *str = get_tv_string_chk(&argvars[0]);
13957 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13958 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
13959 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
13960
Bram Moolenaar0d660222005-01-07 21:51:51 +000013961 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013962 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
13963 rettv->vval.v_string = NULL;
13964 else
13965 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013966}
13967
13968/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013969 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970 */
13971/*ARGSUSED*/
13972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013973f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013974 typval_T *argvars;
13975 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013976{
13977 int id = 0;
13978#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013979 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980 long col;
13981 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000013982 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013984 lnum = get_tv_lnum(argvars); /* -1 on type error */
13985 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
13986 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013988 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000013989 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
13990 id = syn_get_id(lnum, (colnr_T)col, trans, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013991#endif
13992
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013993 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013994}
13995
13996/*
13997 * "synIDattr(id, what [, mode])" function
13998 */
13999/*ARGSUSED*/
14000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014001f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014002 typval_T *argvars;
14003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004{
14005 char_u *p = NULL;
14006#ifdef FEAT_SYN_HL
14007 int id;
14008 char_u *what;
14009 char_u *mode;
14010 char_u modebuf[NUMBUFLEN];
14011 int modec;
14012
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014013 id = get_tv_number(&argvars[0]);
14014 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014015 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014016 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014017 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018 modec = TOLOWER_ASC(mode[0]);
14019 if (modec != 't' && modec != 'c'
14020#ifdef FEAT_GUI
14021 && modec != 'g'
14022#endif
14023 )
14024 modec = 0; /* replace invalid with current */
14025 }
14026 else
14027 {
14028#ifdef FEAT_GUI
14029 if (gui.in_use)
14030 modec = 'g';
14031 else
14032#endif
14033 if (t_colors > 1)
14034 modec = 'c';
14035 else
14036 modec = 't';
14037 }
14038
14039
14040 switch (TOLOWER_ASC(what[0]))
14041 {
14042 case 'b':
14043 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
14044 p = highlight_color(id, what, modec);
14045 else /* bold */
14046 p = highlight_has_attr(id, HL_BOLD, modec);
14047 break;
14048
14049 case 'f': /* fg[#] */
14050 p = highlight_color(id, what, modec);
14051 break;
14052
14053 case 'i':
14054 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
14055 p = highlight_has_attr(id, HL_INVERSE, modec);
14056 else /* italic */
14057 p = highlight_has_attr(id, HL_ITALIC, modec);
14058 break;
14059
14060 case 'n': /* name */
14061 p = get_highlight_name(NULL, id - 1);
14062 break;
14063
14064 case 'r': /* reverse */
14065 p = highlight_has_attr(id, HL_INVERSE, modec);
14066 break;
14067
14068 case 's': /* standout */
14069 p = highlight_has_attr(id, HL_STANDOUT, modec);
14070 break;
14071
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000014072 case 'u':
14073 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
14074 /* underline */
14075 p = highlight_has_attr(id, HL_UNDERLINE, modec);
14076 else
14077 /* undercurl */
14078 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079 break;
14080 }
14081
14082 if (p != NULL)
14083 p = vim_strsave(p);
14084#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014085 rettv->v_type = VAR_STRING;
14086 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087}
14088
14089/*
14090 * "synIDtrans(id)" function
14091 */
14092/*ARGSUSED*/
14093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014094f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014095 typval_T *argvars;
14096 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097{
14098 int id;
14099
14100#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014101 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102
14103 if (id > 0)
14104 id = syn_get_final_id(id);
14105 else
14106#endif
14107 id = 0;
14108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014109 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014110}
14111
14112/*
14113 * "system()" function
14114 */
14115 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014116f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014117 typval_T *argvars;
14118 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014120 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014122 char_u *infile = NULL;
14123 char_u buf[NUMBUFLEN];
14124 int err = FALSE;
14125 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014127 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014128 {
14129 /*
14130 * Write the string to a temp file, to be used for input of the shell
14131 * command.
14132 */
14133 if ((infile = vim_tempname('i')) == NULL)
14134 {
14135 EMSG(_(e_notmp));
14136 return;
14137 }
14138
14139 fd = mch_fopen((char *)infile, WRITEBIN);
14140 if (fd == NULL)
14141 {
14142 EMSG2(_(e_notopen), infile);
14143 goto done;
14144 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014145 p = get_tv_string_buf_chk(&argvars[1], buf);
14146 if (p == NULL)
14147 goto done; /* type error; errmsg already given */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014148 if (fwrite(p, STRLEN(p), 1, fd) != 1)
14149 err = TRUE;
14150 if (fclose(fd) != 0)
14151 err = TRUE;
14152 if (err)
14153 {
14154 EMSG(_("E677: Error writing temp file"));
14155 goto done;
14156 }
14157 }
14158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014159 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014160
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161#ifdef USE_CR
14162 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014163 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164 {
14165 char_u *s;
14166
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014167 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168 {
14169 if (*s == CAR)
14170 *s = NL;
14171 }
14172 }
14173#else
14174# ifdef USE_CRNL
14175 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014176 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177 {
14178 char_u *s, *d;
14179
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014180 d = res;
14181 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182 {
14183 if (s[0] == CAR && s[1] == NL)
14184 ++s;
14185 *d++ = *s;
14186 }
14187 *d = NUL;
14188 }
14189# endif
14190#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000014191
14192done:
14193 if (infile != NULL)
14194 {
14195 mch_remove(infile);
14196 vim_free(infile);
14197 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014198 rettv->v_type = VAR_STRING;
14199 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200}
14201
14202/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000014203 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014204 */
14205 static void
14206f_taglist(argvars, rettv)
14207 typval_T *argvars;
14208 typval_T *rettv;
14209{
14210 char_u *tag_pattern;
14211 list_T *l;
14212
14213 tag_pattern = get_tv_string(&argvars[0]);
14214
14215 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014216 if (*tag_pattern == NUL)
14217 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000014218
14219 l = list_alloc();
14220 if (l != NULL)
14221 {
14222 if (get_tags(l, tag_pattern) != FAIL)
14223 {
14224 rettv->vval.v_list = l;
14225 rettv->v_type = VAR_LIST;
14226 ++l->lv_refcount;
14227 }
14228 else
14229 list_free(l);
14230 }
14231}
14232
14233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234 * "tempname()" function
14235 */
14236/*ARGSUSED*/
14237 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014238f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014239 typval_T *argvars;
14240 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014241{
14242 static int x = 'A';
14243
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014244 rettv->v_type = VAR_STRING;
14245 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246
14247 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
14248 * names. Skip 'I' and 'O', they are used for shell redirection. */
14249 do
14250 {
14251 if (x == 'Z')
14252 x = '0';
14253 else if (x == '9')
14254 x = 'A';
14255 else
14256 {
14257#ifdef EBCDIC
14258 if (x == 'I')
14259 x = 'J';
14260 else if (x == 'R')
14261 x = 'S';
14262 else
14263#endif
14264 ++x;
14265 }
14266 } while (x == 'I' || x == 'O');
14267}
14268
14269/*
14270 * "tolower(string)" function
14271 */
14272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014273f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014274 typval_T *argvars;
14275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014276{
14277 char_u *p;
14278
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014279 p = vim_strsave(get_tv_string(&argvars[0]));
14280 rettv->v_type = VAR_STRING;
14281 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014282
14283 if (p != NULL)
14284 while (*p != NUL)
14285 {
14286#ifdef FEAT_MBYTE
14287 int l;
14288
14289 if (enc_utf8)
14290 {
14291 int c, lc;
14292
14293 c = utf_ptr2char(p);
14294 lc = utf_tolower(c);
14295 l = utf_ptr2len_check(p);
14296 /* TODO: reallocate string when byte count changes. */
14297 if (utf_char2len(lc) == l)
14298 utf_char2bytes(lc, p);
14299 p += l;
14300 }
14301 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
14302 p += l; /* skip multi-byte character */
14303 else
14304#endif
14305 {
14306 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
14307 ++p;
14308 }
14309 }
14310}
14311
14312/*
14313 * "toupper(string)" function
14314 */
14315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014316f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014317 typval_T *argvars;
14318 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014320 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014321 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322}
14323
14324/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000014325 * "tr(string, fromstr, tostr)" function
14326 */
14327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014328f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014329 typval_T *argvars;
14330 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014331{
14332 char_u *instr;
14333 char_u *fromstr;
14334 char_u *tostr;
14335 char_u *p;
14336#ifdef FEAT_MBYTE
14337 int inlen;
14338 int fromlen;
14339 int tolen;
14340 int idx;
14341 char_u *cpstr;
14342 int cplen;
14343 int first = TRUE;
14344#endif
14345 char_u buf[NUMBUFLEN];
14346 char_u buf2[NUMBUFLEN];
14347 garray_T ga;
14348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014349 instr = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014350 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
14351 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014352
14353 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014354 rettv->v_type = VAR_STRING;
14355 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014356 if (fromstr == NULL || tostr == NULL)
14357 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000014358 ga_init2(&ga, (int)sizeof(char), 80);
14359
14360#ifdef FEAT_MBYTE
14361 if (!has_mbyte)
14362#endif
14363 /* not multi-byte: fromstr and tostr must be the same length */
14364 if (STRLEN(fromstr) != STRLEN(tostr))
14365 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014366#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000014367error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014368#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000014369 EMSG2(_(e_invarg2), fromstr);
14370 ga_clear(&ga);
14371 return;
14372 }
14373
14374 /* fromstr and tostr have to contain the same number of chars */
14375 while (*instr != NUL)
14376 {
14377#ifdef FEAT_MBYTE
14378 if (has_mbyte)
14379 {
14380 inlen = mb_ptr2len_check(instr);
14381 cpstr = instr;
14382 cplen = inlen;
14383 idx = 0;
14384 for (p = fromstr; *p != NUL; p += fromlen)
14385 {
14386 fromlen = mb_ptr2len_check(p);
14387 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
14388 {
14389 for (p = tostr; *p != NUL; p += tolen)
14390 {
14391 tolen = mb_ptr2len_check(p);
14392 if (idx-- == 0)
14393 {
14394 cplen = tolen;
14395 cpstr = p;
14396 break;
14397 }
14398 }
14399 if (*p == NUL) /* tostr is shorter than fromstr */
14400 goto error;
14401 break;
14402 }
14403 ++idx;
14404 }
14405
14406 if (first && cpstr == instr)
14407 {
14408 /* Check that fromstr and tostr have the same number of
14409 * (multi-byte) characters. Done only once when a character
14410 * of instr doesn't appear in fromstr. */
14411 first = FALSE;
14412 for (p = tostr; *p != NUL; p += tolen)
14413 {
14414 tolen = mb_ptr2len_check(p);
14415 --idx;
14416 }
14417 if (idx != 0)
14418 goto error;
14419 }
14420
14421 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000014422 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000014423 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014424
14425 instr += inlen;
14426 }
14427 else
14428#endif
14429 {
14430 /* When not using multi-byte chars we can do it faster. */
14431 p = vim_strchr(fromstr, *instr);
14432 if (p != NULL)
14433 ga_append(&ga, tostr[p - fromstr]);
14434 else
14435 ga_append(&ga, *instr);
14436 ++instr;
14437 }
14438 }
14439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014440 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000014441}
14442
14443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444 * "type(expr)" function
14445 */
14446 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014447f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014448 typval_T *argvars;
14449 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014450{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014451 int n;
14452
14453 switch (argvars[0].v_type)
14454 {
14455 case VAR_NUMBER: n = 0; break;
14456 case VAR_STRING: n = 1; break;
14457 case VAR_FUNC: n = 2; break;
14458 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000014459 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014460 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
14461 }
14462 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463}
14464
14465/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014466 * "values(dict)" function
14467 */
14468 static void
14469f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014470 typval_T *argvars;
14471 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014472{
14473 dict_list(argvars, rettv, 1);
14474}
14475
14476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 * "virtcol(string)" function
14478 */
14479 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014480f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014481 typval_T *argvars;
14482 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483{
14484 colnr_T vcol = 0;
14485 pos_T *fp;
14486
14487 fp = var2fpos(&argvars[0], FALSE);
14488 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
14489 {
14490 getvvcol(curwin, fp, NULL, NULL, &vcol);
14491 ++vcol;
14492 }
14493
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014494 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495}
14496
14497/*
14498 * "visualmode()" function
14499 */
14500/*ARGSUSED*/
14501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014502f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014503 typval_T *argvars;
14504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505{
14506#ifdef FEAT_VISUAL
14507 char_u str[2];
14508
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014509 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510 str[0] = curbuf->b_visual_mode_eval;
14511 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014512 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513
14514 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014515 if ((argvars[0].v_type == VAR_NUMBER
14516 && argvars[0].vval.v_number != 0)
14517 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014518 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519 curbuf->b_visual_mode_eval = NUL;
14520#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014521 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522#endif
14523}
14524
14525/*
14526 * "winbufnr(nr)" function
14527 */
14528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014529f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014530 typval_T *argvars;
14531 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014532{
14533 win_T *wp;
14534
14535 wp = find_win_by_nr(&argvars[0]);
14536 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014537 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014539 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014540}
14541
14542/*
14543 * "wincol()" function
14544 */
14545/*ARGSUSED*/
14546 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014547f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014548 typval_T *argvars;
14549 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014550{
14551 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014552 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014553}
14554
14555/*
14556 * "winheight(nr)" function
14557 */
14558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014559f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014560 typval_T *argvars;
14561 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562{
14563 win_T *wp;
14564
14565 wp = find_win_by_nr(&argvars[0]);
14566 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014567 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014569 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014570}
14571
14572/*
14573 * "winline()" function
14574 */
14575/*ARGSUSED*/
14576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014577f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014578 typval_T *argvars;
14579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580{
14581 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014582 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014583}
14584
14585/*
14586 * "winnr()" function
14587 */
14588/* ARGSUSED */
14589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014590f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014591 typval_T *argvars;
14592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593{
14594 int nr = 1;
14595#ifdef FEAT_WINDOWS
14596 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014597 win_T *twin = curwin;
14598 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014599
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014600 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014601 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014602 arg = get_tv_string_chk(&argvars[0]);
14603 if (arg == NULL)
14604 nr = 0; /* type error; errmsg already given */
14605 else if (STRCMP(arg, "$") == 0)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000014606 twin = lastwin;
14607 else if (STRCMP(arg, "#") == 0)
14608 {
14609 twin = prevwin;
14610 if (prevwin == NULL)
14611 nr = 0;
14612 }
14613 else
14614 {
14615 EMSG2(_(e_invexpr2), arg);
14616 nr = 0;
14617 }
14618 }
14619
14620 if (nr > 0)
14621 for (wp = firstwin; wp != twin; wp = wp->w_next)
14622 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014623#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014624 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014625}
14626
14627/*
14628 * "winrestcmd()" function
14629 */
14630/* ARGSUSED */
14631 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014632f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014633 typval_T *argvars;
14634 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014635{
14636#ifdef FEAT_WINDOWS
14637 win_T *wp;
14638 int winnr = 1;
14639 garray_T ga;
14640 char_u buf[50];
14641
14642 ga_init2(&ga, (int)sizeof(char), 70);
14643 for (wp = firstwin; wp != NULL; wp = wp->w_next)
14644 {
14645 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
14646 ga_concat(&ga, buf);
14647# ifdef FEAT_VERTSPLIT
14648 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
14649 ga_concat(&ga, buf);
14650# endif
14651 ++winnr;
14652 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000014653 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014654
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014655 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014656#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014657 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014658#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014659 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660}
14661
14662/*
14663 * "winwidth(nr)" function
14664 */
14665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014666f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014667 typval_T *argvars;
14668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669{
14670 win_T *wp;
14671
14672 wp = find_win_by_nr(&argvars[0]);
14673 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014674 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014675 else
14676#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014677 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014678#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014679 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680#endif
14681}
14682
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014684 * "writefile()" function
14685 */
14686 static void
14687f_writefile(argvars, rettv)
14688 typval_T *argvars;
14689 typval_T *rettv;
14690{
14691 int binary = FALSE;
14692 char_u *fname;
14693 FILE *fd;
14694 listitem_T *li;
14695 char_u *s;
14696 int ret = 0;
14697 int c;
14698
14699 if (argvars[0].v_type != VAR_LIST)
14700 {
14701 EMSG2(_(e_listarg), "writefile()");
14702 return;
14703 }
14704 if (argvars[0].vval.v_list == NULL)
14705 return;
14706
14707 if (argvars[2].v_type != VAR_UNKNOWN
14708 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
14709 binary = TRUE;
14710
14711 /* Always open the file in binary mode, library functions have a mind of
14712 * their own about CR-LF conversion. */
14713 fname = get_tv_string(&argvars[1]);
14714 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
14715 {
14716 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
14717 ret = -1;
14718 }
14719 else
14720 {
14721 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
14722 li = li->li_next)
14723 {
14724 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
14725 {
14726 if (*s == '\n')
14727 c = putc(NUL, fd);
14728 else
14729 c = putc(*s, fd);
14730 if (c == EOF)
14731 {
14732 ret = -1;
14733 break;
14734 }
14735 }
14736 if (!binary || li->li_next != NULL)
14737 if (putc('\n', fd) == EOF)
14738 {
14739 ret = -1;
14740 break;
14741 }
14742 if (ret < 0)
14743 {
14744 EMSG(_(e_write));
14745 break;
14746 }
14747 }
14748 fclose(fd);
14749 }
14750
14751 rettv->vval.v_number = ret;
14752}
14753
14754/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014755 * Translate a String variable into a position.
14756 */
14757 static pos_T *
14758var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000014759 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760 int lnum; /* TRUE when $ is last line */
14761{
14762 char_u *name;
14763 static pos_T pos;
14764 pos_T *pp;
14765
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014766 name = get_tv_string_chk(varp);
14767 if (name == NULL)
14768 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014769 if (name[0] == '.') /* cursor */
14770 return &curwin->w_cursor;
14771 if (name[0] == '\'') /* mark */
14772 {
14773 pp = getmark(name[1], FALSE);
14774 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
14775 return NULL;
14776 return pp;
14777 }
14778 if (name[0] == '$') /* last column or line */
14779 {
14780 if (lnum)
14781 {
14782 pos.lnum = curbuf->b_ml.ml_line_count;
14783 pos.col = 0;
14784 }
14785 else
14786 {
14787 pos.lnum = curwin->w_cursor.lnum;
14788 pos.col = (colnr_T)STRLEN(ml_get_curline());
14789 }
14790 return &pos;
14791 }
14792 return NULL;
14793}
14794
14795/*
14796 * Get the length of an environment variable name.
14797 * Advance "arg" to the first character after the name.
14798 * Return 0 for error.
14799 */
14800 static int
14801get_env_len(arg)
14802 char_u **arg;
14803{
14804 char_u *p;
14805 int len;
14806
14807 for (p = *arg; vim_isIDc(*p); ++p)
14808 ;
14809 if (p == *arg) /* no name found */
14810 return 0;
14811
14812 len = (int)(p - *arg);
14813 *arg = p;
14814 return len;
14815}
14816
14817/*
14818 * Get the length of the name of a function or internal variable.
14819 * "arg" is advanced to the first non-white character after the name.
14820 * Return 0 if something is wrong.
14821 */
14822 static int
14823get_id_len(arg)
14824 char_u **arg;
14825{
14826 char_u *p;
14827 int len;
14828
14829 /* Find the end of the name. */
14830 for (p = *arg; eval_isnamec(*p); ++p)
14831 ;
14832 if (p == *arg) /* no name found */
14833 return 0;
14834
14835 len = (int)(p - *arg);
14836 *arg = skipwhite(p);
14837
14838 return len;
14839}
14840
14841/*
Bram Moolenaara7043832005-01-21 11:56:39 +000014842 * Get the length of the name of a variable or function.
14843 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014845 * Return -1 if curly braces expansion failed.
14846 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847 * If the name contains 'magic' {}'s, expand them and return the
14848 * expanded name in an allocated string via 'alias' - caller must free.
14849 */
14850 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014851get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014852 char_u **arg;
14853 char_u **alias;
14854 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014855 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014856{
14857 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014858 char_u *p;
14859 char_u *expr_start;
14860 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014861
14862 *alias = NULL; /* default to no alias */
14863
14864 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
14865 && (*arg)[2] == (int)KE_SNR)
14866 {
14867 /* hard coded <SNR>, already translated */
14868 *arg += 3;
14869 return get_id_len(arg) + 3;
14870 }
14871 len = eval_fname_script(*arg);
14872 if (len > 0)
14873 {
14874 /* literal "<SID>", "s:" or "<SNR>" */
14875 *arg += len;
14876 }
14877
Bram Moolenaar071d4272004-06-13 20:20:40 +000014878 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014879 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014880 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014881 p = find_name_end(*arg, &expr_start, &expr_end,
14882 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014883 if (expr_start != NULL)
14884 {
14885 char_u *temp_string;
14886
14887 if (!evaluate)
14888 {
14889 len += (int)(p - *arg);
14890 *arg = skipwhite(p);
14891 return len;
14892 }
14893
14894 /*
14895 * Include any <SID> etc in the expanded string:
14896 * Thus the -len here.
14897 */
14898 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
14899 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014900 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014901 *alias = temp_string;
14902 *arg = skipwhite(p);
14903 return (int)STRLEN(temp_string);
14904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014905
14906 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014907 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014908 EMSG2(_(e_invexpr2), *arg);
14909
14910 return len;
14911}
14912
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014913/*
14914 * Find the end of a variable or function name, taking care of magic braces.
14915 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
14916 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014917 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014918 * Return a pointer to just after the name. Equal to "arg" if there is no
14919 * valid name.
14920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014921 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014922find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014923 char_u *arg;
14924 char_u **expr_start;
14925 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014926 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014927{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014928 int mb_nest = 0;
14929 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014930 char_u *p;
14931
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014932 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014933 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014934 *expr_start = NULL;
14935 *expr_end = NULL;
14936 }
14937
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014938 /* Quick check for valid starting character. */
14939 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
14940 return arg;
14941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014942 for (p = arg; *p != NUL
14943 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014944 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000014945 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014946 || mb_nest != 0
14947 || br_nest != 0); ++p)
14948 {
14949 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014950 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014951 if (*p == '[')
14952 ++br_nest;
14953 else if (*p == ']')
14954 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014955 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014956 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014957 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014958 if (*p == '{')
14959 {
14960 mb_nest++;
14961 if (expr_start != NULL && *expr_start == NULL)
14962 *expr_start = p;
14963 }
14964 else if (*p == '}')
14965 {
14966 mb_nest--;
14967 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
14968 *expr_end = p;
14969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014971 }
14972
14973 return p;
14974}
14975
14976/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014977 * Expands out the 'magic' {}'s in a variable/function name.
14978 * Note that this can call itself recursively, to deal with
14979 * constructs like foo{bar}{baz}{bam}
14980 * The four pointer arguments point to "foo{expre}ss{ion}bar"
14981 * "in_start" ^
14982 * "expr_start" ^
14983 * "expr_end" ^
14984 * "in_end" ^
14985 *
14986 * Returns a new allocated string, which the caller must free.
14987 * Returns NULL for failure.
14988 */
14989 static char_u *
14990make_expanded_name(in_start, expr_start, expr_end, in_end)
14991 char_u *in_start;
14992 char_u *expr_start;
14993 char_u *expr_end;
14994 char_u *in_end;
14995{
14996 char_u c1;
14997 char_u *retval = NULL;
14998 char_u *temp_result;
14999 char_u *nextcmd = NULL;
15000
15001 if (expr_end == NULL || in_end == NULL)
15002 return NULL;
15003 *expr_start = NUL;
15004 *expr_end = NUL;
15005 c1 = *in_end;
15006 *in_end = NUL;
15007
15008 temp_result = eval_to_string(expr_start + 1, &nextcmd);
15009 if (temp_result != NULL && nextcmd == NULL)
15010 {
15011 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
15012 + (in_end - expr_end) + 1));
15013 if (retval != NULL)
15014 {
15015 STRCPY(retval, in_start);
15016 STRCAT(retval, temp_result);
15017 STRCAT(retval, expr_end + 1);
15018 }
15019 }
15020 vim_free(temp_result);
15021
15022 *in_end = c1; /* put char back for error messages */
15023 *expr_start = '{';
15024 *expr_end = '}';
15025
15026 if (retval != NULL)
15027 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015028 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015029 if (expr_start != NULL)
15030 {
15031 /* Further expansion! */
15032 temp_result = make_expanded_name(retval, expr_start,
15033 expr_end, temp_result);
15034 vim_free(retval);
15035 retval = temp_result;
15036 }
15037 }
15038
15039 return retval;
15040}
15041
15042/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000015044 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015045 */
15046 static int
15047eval_isnamec(c)
15048 int c;
15049{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015050 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
15051}
15052
15053/*
15054 * Return TRUE if character "c" can be used as the first character in a
15055 * variable or function name (excluding '{' and '}').
15056 */
15057 static int
15058eval_isnamec1(c)
15059 int c;
15060{
15061 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000015062}
15063
15064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015065 * Set number v: variable to "val".
15066 */
15067 void
15068set_vim_var_nr(idx, val)
15069 int idx;
15070 long val;
15071{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015072 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015073}
15074
15075/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015076 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015077 */
15078 long
15079get_vim_var_nr(idx)
15080 int idx;
15081{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015082 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015083}
15084
Bram Moolenaar19a09a12005-03-04 23:39:37 +000015085#if defined(FEAT_AUTOCMD) || defined(PROTO)
15086/*
15087 * Get string v: variable value. Uses a static buffer, can only be used once.
15088 */
15089 char_u *
15090get_vim_var_str(idx)
15091 int idx;
15092{
15093 return get_tv_string(&vimvars[idx].vv_tv);
15094}
15095#endif
15096
Bram Moolenaar071d4272004-06-13 20:20:40 +000015097/*
15098 * Set v:count, v:count1 and v:prevcount.
15099 */
15100 void
15101set_vcount(count, count1)
15102 long count;
15103 long count1;
15104{
Bram Moolenaare9a41262005-01-15 22:18:47 +000015105 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
15106 vimvars[VV_COUNT].vv_nr = count;
15107 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108}
15109
15110/*
15111 * Set string v: variable to a copy of "val".
15112 */
15113 void
15114set_vim_var_string(idx, val, len)
15115 int idx;
15116 char_u *val;
15117 int len; /* length of "val" to use or -1 (whole string) */
15118{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015119 /* Need to do this (at least) once, since we can't initialize a union.
15120 * Will always be invoked when "v:progname" is set. */
15121 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
15122
Bram Moolenaare9a41262005-01-15 22:18:47 +000015123 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015124 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015125 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015127 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015128 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015129 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015130}
15131
15132/*
15133 * Set v:register if needed.
15134 */
15135 void
15136set_reg_var(c)
15137 int c;
15138{
15139 char_u regname;
15140
15141 if (c == 0 || c == ' ')
15142 regname = '"';
15143 else
15144 regname = c;
15145 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000015146 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147 set_vim_var_string(VV_REG, &regname, 1);
15148}
15149
15150/*
15151 * Get or set v:exception. If "oldval" == NULL, return the current value.
15152 * Otherwise, restore the value to "oldval" and return NULL.
15153 * Must always be called in pairs to save and restore v:exception! Does not
15154 * take care of memory allocations.
15155 */
15156 char_u *
15157v_exception(oldval)
15158 char_u *oldval;
15159{
15160 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015161 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162
Bram Moolenaare9a41262005-01-15 22:18:47 +000015163 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015164 return NULL;
15165}
15166
15167/*
15168 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
15169 * Otherwise, restore the value to "oldval" and return NULL.
15170 * Must always be called in pairs to save and restore v:throwpoint! Does not
15171 * take care of memory allocations.
15172 */
15173 char_u *
15174v_throwpoint(oldval)
15175 char_u *oldval;
15176{
15177 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015178 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015179
Bram Moolenaare9a41262005-01-15 22:18:47 +000015180 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015181 return NULL;
15182}
15183
15184#if defined(FEAT_AUTOCMD) || defined(PROTO)
15185/*
15186 * Set v:cmdarg.
15187 * If "eap" != NULL, use "eap" to generate the value and return the old value.
15188 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
15189 * Must always be called in pairs!
15190 */
15191 char_u *
15192set_cmdarg(eap, oldarg)
15193 exarg_T *eap;
15194 char_u *oldarg;
15195{
15196 char_u *oldval;
15197 char_u *newval;
15198 unsigned len;
15199
Bram Moolenaare9a41262005-01-15 22:18:47 +000015200 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015201 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015202 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015203 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000015204 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015205 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206 }
15207
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015208 if (eap->force_bin == FORCE_BIN)
15209 len = 6;
15210 else if (eap->force_bin == FORCE_NOBIN)
15211 len = 8;
15212 else
15213 len = 0;
15214 if (eap->force_ff != 0)
15215 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
15216# ifdef FEAT_MBYTE
15217 if (eap->force_enc != 0)
15218 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
15219# endif
15220
15221 newval = alloc(len + 1);
15222 if (newval == NULL)
15223 return NULL;
15224
15225 if (eap->force_bin == FORCE_BIN)
15226 sprintf((char *)newval, " ++bin");
15227 else if (eap->force_bin == FORCE_NOBIN)
15228 sprintf((char *)newval, " ++nobin");
15229 else
15230 *newval = NUL;
15231 if (eap->force_ff != 0)
15232 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
15233 eap->cmd + eap->force_ff);
15234# ifdef FEAT_MBYTE
15235 if (eap->force_enc != 0)
15236 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
15237 eap->cmd + eap->force_enc);
15238# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000015239 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000015240 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241}
15242#endif
15243
15244/*
15245 * Get the value of internal variable "name".
15246 * Return OK or FAIL.
15247 */
15248 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015249get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015250 char_u *name;
15251 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000015252 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015253 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015254{
15255 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000015256 typval_T *tv = NULL;
15257 typval_T atv;
15258 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015259 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015260
15261 /* truncate the name, so that we can use strcmp() */
15262 cc = name[len];
15263 name[len] = NUL;
15264
15265 /*
15266 * Check for "b:changedtick".
15267 */
15268 if (STRCMP(name, "b:changedtick") == 0)
15269 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000015270 atv.v_type = VAR_NUMBER;
15271 atv.vval.v_number = curbuf->b_changedtick;
15272 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273 }
15274
15275 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015276 * Check for user-defined variables.
15277 */
15278 else
15279 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015280 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015281 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015282 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015283 }
15284
Bram Moolenaare9a41262005-01-15 22:18:47 +000015285 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015286 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015287 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015288 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015289 ret = FAIL;
15290 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015291 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015292 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015293
15294 name[len] = cc;
15295
15296 return ret;
15297}
15298
15299/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015300 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
15301 * Also handle function call with Funcref variable: func(expr)
15302 * Can all be combined: dict.func(expr)[idx]['func'](expr)
15303 */
15304 static int
15305handle_subscript(arg, rettv, evaluate, verbose)
15306 char_u **arg;
15307 typval_T *rettv;
15308 int evaluate; /* do more than finding the end */
15309 int verbose; /* give error messages */
15310{
15311 int ret = OK;
15312 dict_T *selfdict = NULL;
15313 char_u *s;
15314 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000015315 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015316
15317 while (ret == OK
15318 && (**arg == '['
15319 || (**arg == '.' && rettv->v_type == VAR_DICT)
15320 || (**arg == '(' && rettv->v_type == VAR_FUNC))
15321 && !vim_iswhite(*(*arg - 1)))
15322 {
15323 if (**arg == '(')
15324 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000015325 /* need to copy the funcref so that we can clear rettv */
15326 functv = *rettv;
15327 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015328
15329 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000015330 s = functv.vval.v_string;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015331 ret = get_func_tv(s, STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000015332 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
15333 &len, evaluate, selfdict);
15334
15335 /* Clear the funcref afterwards, so that deleting it while
15336 * evaluating the arguments is possible (see test55). */
15337 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015338
15339 /* Stop the expression evaluation when immediately aborting on
15340 * error, or when an interrupt occurred or an exception was thrown
15341 * but not caught. */
15342 if (aborting())
15343 {
15344 if (ret == OK)
15345 clear_tv(rettv);
15346 ret = FAIL;
15347 }
15348 dict_unref(selfdict);
15349 selfdict = NULL;
15350 }
15351 else /* **arg == '[' || **arg == '.' */
15352 {
15353 dict_unref(selfdict);
15354 if (rettv->v_type == VAR_DICT)
15355 {
15356 selfdict = rettv->vval.v_dict;
15357 if (selfdict != NULL)
15358 ++selfdict->dv_refcount;
15359 }
15360 else
15361 selfdict = NULL;
15362 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
15363 {
15364 clear_tv(rettv);
15365 ret = FAIL;
15366 }
15367 }
15368 }
15369 dict_unref(selfdict);
15370 return ret;
15371}
15372
15373/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015374 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
15375 * value).
15376 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015377 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015378alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015379{
Bram Moolenaar33570922005-01-25 22:26:29 +000015380 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015381}
15382
15383/*
15384 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015385 * The string "s" must have been allocated, it is consumed.
15386 * Return NULL for out of memory, the variable otherwise.
15387 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015388 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015389alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015390 char_u *s;
15391{
Bram Moolenaar33570922005-01-25 22:26:29 +000015392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015394 rettv = alloc_tv();
15395 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015396 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015397 rettv->v_type = VAR_STRING;
15398 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015399 }
15400 else
15401 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015402 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403}
15404
15405/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015406 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407 */
15408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015409free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015410 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411{
15412 if (varp != NULL)
15413 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015414 switch (varp->v_type)
15415 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015416 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015417 func_unref(varp->vval.v_string);
15418 /*FALLTHROUGH*/
15419 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015420 vim_free(varp->vval.v_string);
15421 break;
15422 case VAR_LIST:
15423 list_unref(varp->vval.v_list);
15424 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015425 case VAR_DICT:
15426 dict_unref(varp->vval.v_dict);
15427 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015428 case VAR_NUMBER:
15429 case VAR_UNKNOWN:
15430 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015431 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015432 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015433 break;
15434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015435 vim_free(varp);
15436 }
15437}
15438
15439/*
15440 * Free the memory for a variable value and set the value to NULL or 0.
15441 */
15442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015443clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015444 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445{
15446 if (varp != NULL)
15447 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015448 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015449 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015450 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015451 func_unref(varp->vval.v_string);
15452 /*FALLTHROUGH*/
15453 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015454 vim_free(varp->vval.v_string);
15455 varp->vval.v_string = NULL;
15456 break;
15457 case VAR_LIST:
15458 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015459 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015460 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015461 case VAR_DICT:
15462 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015463 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015464 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015465 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015466 varp->vval.v_number = 0;
15467 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015468 case VAR_UNKNOWN:
15469 break;
15470 default:
15471 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015473 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015474 }
15475}
15476
15477/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015478 * Set the value of a variable to NULL without freeing items.
15479 */
15480 static void
15481init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015482 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015483{
15484 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015485 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015486}
15487
15488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015489 * Get the number value of a variable.
15490 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015491 * For incompatible types, return 0.
15492 * get_tv_number_chk() is similar to get_tv_number(), but informs the
15493 * caller of incompatible types: it sets *denote to TRUE if "denote"
15494 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015495 */
15496 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015497get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015498 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015499{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015500 int error = FALSE;
15501
15502 return get_tv_number_chk(varp, &error); /* return 0L on error */
15503}
15504
15505 static long
15506get_tv_number_chk(varp, denote)
15507 typval_T *varp;
15508 int *denote;
15509{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015510 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015512 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015514 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015515 return (long)(varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015516 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015517 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015518 break;
15519 case VAR_STRING:
15520 if (varp->vval.v_string != NULL)
15521 vim_str2nr(varp->vval.v_string, NULL, NULL,
15522 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015523 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015524 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000015525 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015526 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015527 case VAR_DICT:
15528 EMSG(_("E728: Using a Dictionary as a number"));
15529 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015530 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015531 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015532 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015534 if (denote == NULL) /* useful for values that must be unsigned */
15535 n = -1;
15536 else
15537 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015538 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015539}
15540
15541/*
15542 * Get the lnum from the first argument. Also accepts ".", "$", etc.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015543 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544 */
15545 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015546get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000015547 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548{
Bram Moolenaar33570922005-01-25 22:26:29 +000015549 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550 linenr_T lnum;
15551
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015552 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553 if (lnum == 0) /* no valid number, try using line() */
15554 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015555 rettv.v_type = VAR_NUMBER;
15556 f_line(argvars, &rettv);
15557 lnum = rettv.vval.v_number;
15558 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559 }
15560 return lnum;
15561}
15562
15563/*
15564 * Get the string value of a variable.
15565 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000015566 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
15567 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568 * If the String variable has never been set, return an empty string.
15569 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015570 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
15571 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015572 */
15573 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015574get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015575 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015576{
15577 static char_u mybuf[NUMBUFLEN];
15578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015579 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580}
15581
15582 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015583get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000015584 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015585 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015587 char_u *res = get_tv_string_buf_chk(varp, buf);
15588
15589 return res != NULL ? res : (char_u *)"";
15590}
15591
15592 static char_u *
15593get_tv_string_chk(varp)
15594 typval_T *varp;
15595{
15596 static char_u mybuf[NUMBUFLEN];
15597
15598 return get_tv_string_buf_chk(varp, mybuf);
15599}
15600
15601 static char_u *
15602get_tv_string_buf_chk(varp, buf)
15603 typval_T *varp;
15604 char_u *buf;
15605{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015606 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015608 case VAR_NUMBER:
15609 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
15610 return buf;
15611 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015612 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015613 break;
15614 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015615 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000015616 break;
15617 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015618 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015619 break;
15620 case VAR_STRING:
15621 if (varp->vval.v_string != NULL)
15622 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015623 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015624 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015625 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015626 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015628 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629}
15630
15631/*
15632 * Find variable "name" in the list of variables.
15633 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015634 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015635 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000015636 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015637 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015638 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015639find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015640 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015641 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015642{
Bram Moolenaar071d4272004-06-13 20:20:40 +000015643 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015644 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015645
Bram Moolenaara7043832005-01-21 11:56:39 +000015646 ht = find_var_ht(name, &varname);
15647 if (htp != NULL)
15648 *htp = ht;
15649 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015650 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015651 return find_var_in_ht(ht, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652}
15653
15654/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015655 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000015656 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015658 static dictitem_T *
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015659find_var_in_ht(ht, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000015660 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000015661 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015662 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000015663{
Bram Moolenaar33570922005-01-25 22:26:29 +000015664 hashitem_T *hi;
15665
15666 if (*varname == NUL)
15667 {
15668 /* Must be something like "s:", otherwise "ht" would be NULL. */
15669 switch (varname[-2])
15670 {
15671 case 's': return &SCRIPT_SV(current_SID).sv_var;
15672 case 'g': return &globvars_var;
15673 case 'v': return &vimvars_var;
15674 case 'b': return &curbuf->b_bufvar;
15675 case 'w': return &curwin->w_winvar;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015676 case 'l': return current_funccal == NULL
15677 ? NULL : &current_funccal->l_vars_var;
15678 case 'a': return current_funccal == NULL
15679 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000015680 }
15681 return NULL;
15682 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015683
15684 hi = hash_find(ht, varname);
15685 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015686 {
15687 /* For global variables we may try auto-loading the script. If it
15688 * worked find the variable again. */
15689 if (ht == &globvarht && !writing
15690 && script_autoload(varname) && !aborting())
15691 hi = hash_find(ht, varname);
15692 if (HASHITEM_EMPTY(hi))
15693 return NULL;
15694 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015695 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015696}
15697
15698/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015699 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000015700 * Set "varname" to the start of name without ':'.
15701 */
Bram Moolenaar33570922005-01-25 22:26:29 +000015702 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000015703find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015704 char_u *name;
15705 char_u **varname;
15706{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015707 hashitem_T *hi;
15708
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709 if (name[1] != ':')
15710 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015711 /* The name must not start with a colon or #. */
15712 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713 return NULL;
15714 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000015715
15716 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000015717 hi = hash_find(&compat_hashtab, name);
15718 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000015719 return &compat_hashtab;
15720
Bram Moolenaar071d4272004-06-13 20:20:40 +000015721 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015722 return &globvarht; /* global variable */
15723 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724 }
15725 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015726 if (*name == 'g') /* global variable */
15727 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000015728 /* There must be no ':' or '#' in the rest of the name, unless g: is used
15729 */
15730 if (vim_strchr(name + 2, ':') != NULL
15731 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015732 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015734 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000015736 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000015737 if (*name == 'v') /* v: variable */
15738 return &vimvarht;
15739 if (*name == 'a' && current_funccal != NULL) /* function argument */
15740 return &current_funccal->l_avars.dv_hashtab;
15741 if (*name == 'l' && current_funccal != NULL) /* local function variable */
15742 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015743 if (*name == 's' /* script variable */
15744 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
15745 return &SCRIPT_VARS(current_SID);
15746 return NULL;
15747}
15748
15749/*
15750 * Get the string value of a (global/local) variable.
15751 * Returns NULL when it doesn't exist.
15752 */
15753 char_u *
15754get_var_value(name)
15755 char_u *name;
15756{
Bram Moolenaar33570922005-01-25 22:26:29 +000015757 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758
Bram Moolenaara7043832005-01-21 11:56:39 +000015759 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 if (v == NULL)
15761 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000015762 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763}
15764
15765/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015766 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767 * sourcing this script and when executing functions defined in the script.
15768 */
15769 void
15770new_script_vars(id)
15771 scid_T id;
15772{
Bram Moolenaara7043832005-01-21 11:56:39 +000015773 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000015774 hashtab_T *ht;
15775 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000015776
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
15778 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015779 /* Re-allocating ga_data means that an ht_array pointing to
15780 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000015781 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000015782 for (i = 1; i <= ga_scripts.ga_len; ++i)
15783 {
15784 ht = &SCRIPT_VARS(i);
15785 if (ht->ht_mask == HT_INIT_SIZE - 1)
15786 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000015787 sv = &SCRIPT_SV(i);
15788 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000015789 }
15790
Bram Moolenaar071d4272004-06-13 20:20:40 +000015791 while (ga_scripts.ga_len < id)
15792 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015793 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
15794 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015795 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015796 }
15797 }
15798}
15799
15800/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015801 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
15802 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803 */
15804 void
Bram Moolenaar33570922005-01-25 22:26:29 +000015805init_var_dict(dict, dict_var)
15806 dict_T *dict;
15807 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015808{
Bram Moolenaar33570922005-01-25 22:26:29 +000015809 hash_init(&dict->dv_hashtab);
15810 dict->dv_refcount = 99999;
15811 dict_var->di_tv.vval.v_dict = dict;
15812 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015813 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015814 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15815 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015816}
15817
15818/*
15819 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000015820 * Frees all allocated variables and the value they contain.
15821 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822 */
15823 void
Bram Moolenaara7043832005-01-21 11:56:39 +000015824vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000015825 hashtab_T *ht;
15826{
15827 vars_clear_ext(ht, TRUE);
15828}
15829
15830/*
15831 * Like vars_clear(), but only free the value if "free_val" is TRUE.
15832 */
15833 static void
15834vars_clear_ext(ht, free_val)
15835 hashtab_T *ht;
15836 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015837{
Bram Moolenaara7043832005-01-21 11:56:39 +000015838 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000015839 hashitem_T *hi;
15840 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015841
Bram Moolenaar33570922005-01-25 22:26:29 +000015842 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000015843 todo = ht->ht_used;
15844 for (hi = ht->ht_array; todo > 0; ++hi)
15845 {
15846 if (!HASHITEM_EMPTY(hi))
15847 {
15848 --todo;
15849
Bram Moolenaar33570922005-01-25 22:26:29 +000015850 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000015851 * ht_array might change then. hash_clear() takes care of it
15852 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000015853 v = HI2DI(hi);
15854 if (free_val)
15855 clear_tv(&v->di_tv);
15856 if ((v->di_flags & DI_FLAGS_FIX) == 0)
15857 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000015858 }
15859 }
15860 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015861 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862}
15863
Bram Moolenaara7043832005-01-21 11:56:39 +000015864/*
Bram Moolenaar33570922005-01-25 22:26:29 +000015865 * Delete a variable from hashtab "ht" at item "hi".
15866 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000015867 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015868 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000015869delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000015870 hashtab_T *ht;
15871 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015872{
Bram Moolenaar33570922005-01-25 22:26:29 +000015873 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000015874
15875 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000015876 clear_tv(&di->di_tv);
15877 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015878}
15879
15880/*
15881 * List the value of one internal variable.
15882 */
15883 static void
15884list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000015885 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015886 char_u *prefix;
15887{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015888 char_u *tofree;
15889 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015890 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015891
Bram Moolenaar33570922005-01-25 22:26:29 +000015892 s = echo_string(&v->di_tv, &tofree, numbuf);
15893 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015894 s == NULL ? (char_u *)"" : s);
15895 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015896}
15897
Bram Moolenaar071d4272004-06-13 20:20:40 +000015898 static void
15899list_one_var_a(prefix, name, type, string)
15900 char_u *prefix;
15901 char_u *name;
15902 int type;
15903 char_u *string;
15904{
15905 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
15906 if (name != NULL) /* "a:" vars don't have a name stored */
15907 msg_puts(name);
15908 msg_putchar(' ');
15909 msg_advance(22);
15910 if (type == VAR_NUMBER)
15911 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015912 else if (type == VAR_FUNC)
15913 msg_putchar('*');
15914 else if (type == VAR_LIST)
15915 {
15916 msg_putchar('[');
15917 if (*string == '[')
15918 ++string;
15919 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015920 else if (type == VAR_DICT)
15921 {
15922 msg_putchar('{');
15923 if (*string == '{')
15924 ++string;
15925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015926 else
15927 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015928
Bram Moolenaar071d4272004-06-13 20:20:40 +000015929 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015930
15931 if (type == VAR_FUNC)
15932 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000015933}
15934
15935/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015936 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000015937 * If the variable already exists, the value is updated.
15938 * Otherwise the variable is created.
15939 */
15940 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015941set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015942 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015943 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015944 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015945{
Bram Moolenaar33570922005-01-25 22:26:29 +000015946 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015947 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000015948 hashtab_T *ht;
Bram Moolenaar92124a32005-06-17 22:03:40 +000015949 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015950
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015951 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015952 {
15953 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
15954 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
15955 ? name[2] : name[0]))
15956 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015957 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015958 return;
15959 }
15960 if (function_exists(name))
15961 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000015962 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015963 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015964 return;
15965 }
15966 }
15967
Bram Moolenaara7043832005-01-21 11:56:39 +000015968 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000015969 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000015970 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000015971 EMSG2(_(e_illvar), name);
Bram Moolenaara7043832005-01-21 11:56:39 +000015972 return;
15973 }
15974
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015975 v = find_var_in_ht(ht, varname, TRUE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015976 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015977 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015978 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015979 if (var_check_ro(v->di_flags, name)
15980 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000015981 return;
15982 if (v->di_tv.v_type != tv->v_type
15983 && !((v->di_tv.v_type == VAR_STRING
15984 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015985 && (tv->v_type == VAR_STRING
15986 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015987 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015988 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015989 return;
15990 }
Bram Moolenaar33570922005-01-25 22:26:29 +000015991
15992 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000015993 * Handle setting internal v: variables separately: we don't change
15994 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000015995 */
15996 if (ht == &vimvarht)
15997 {
15998 if (v->di_tv.v_type == VAR_STRING)
15999 {
16000 vim_free(v->di_tv.vval.v_string);
16001 if (copy || tv->v_type != VAR_STRING)
16002 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
16003 else
16004 {
16005 /* Take over the string to avoid an extra alloc/free. */
16006 v->di_tv.vval.v_string = tv->vval.v_string;
16007 tv->vval.v_string = NULL;
16008 }
16009 }
16010 else if (v->di_tv.v_type != VAR_NUMBER)
16011 EMSG2(_(e_intern2), "set_var()");
16012 else
16013 v->di_tv.vval.v_number = get_tv_number(tv);
16014 return;
16015 }
16016
16017 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016018 }
16019 else /* add a new variable */
16020 {
Bram Moolenaar92124a32005-06-17 22:03:40 +000016021 /* Make sure the variable name is valid. */
16022 for (p = varname; *p != NUL; ++p)
16023 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p)))
16024 {
16025 EMSG2(_(e_illvar), varname);
16026 return;
16027 }
16028
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016029 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
16030 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000016031 if (v == NULL)
16032 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000016033 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000016034 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016035 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016036 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037 return;
16038 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016039 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016040 }
Bram Moolenaara7043832005-01-21 11:56:39 +000016041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016042 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000016043 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016044 else
16045 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016046 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016047 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016048 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016050}
16051
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016052/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016053 * Return TRUE if di_flags "flags" indicate read-only variable "name".
16054 * Also give an error message.
16055 */
16056 static int
16057var_check_ro(flags, name)
16058 int flags;
16059 char_u *name;
16060{
16061 if (flags & DI_FLAGS_RO)
16062 {
16063 EMSG2(_(e_readonlyvar), name);
16064 return TRUE;
16065 }
16066 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
16067 {
16068 EMSG2(_(e_readonlysbx), name);
16069 return TRUE;
16070 }
16071 return FALSE;
16072}
16073
16074/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016075 * Return TRUE if typeval "tv" is set to be locked (immutable).
16076 * Also give an error message, using "name".
16077 */
16078 static int
16079tv_check_lock(lock, name)
16080 int lock;
16081 char_u *name;
16082{
16083 if (lock & VAR_LOCKED)
16084 {
16085 EMSG2(_("E741: Value is locked: %s"),
16086 name == NULL ? (char_u *)_("Unknown") : name);
16087 return TRUE;
16088 }
16089 if (lock & VAR_FIXED)
16090 {
16091 EMSG2(_("E742: Cannot change value of %s"),
16092 name == NULL ? (char_u *)_("Unknown") : name);
16093 return TRUE;
16094 }
16095 return FALSE;
16096}
16097
16098/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016099 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016100 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016101 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016104copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000016105 typval_T *from;
16106 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016107{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016108 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016109 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016110 switch (from->v_type)
16111 {
16112 case VAR_NUMBER:
16113 to->vval.v_number = from->vval.v_number;
16114 break;
16115 case VAR_STRING:
16116 case VAR_FUNC:
16117 if (from->vval.v_string == NULL)
16118 to->vval.v_string = NULL;
16119 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016120 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016121 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016122 if (from->v_type == VAR_FUNC)
16123 func_ref(to->vval.v_string);
16124 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016125 break;
16126 case VAR_LIST:
16127 if (from->vval.v_list == NULL)
16128 to->vval.v_list = NULL;
16129 else
16130 {
16131 to->vval.v_list = from->vval.v_list;
16132 ++to->vval.v_list->lv_refcount;
16133 }
16134 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016135 case VAR_DICT:
16136 if (from->vval.v_dict == NULL)
16137 to->vval.v_dict = NULL;
16138 else
16139 {
16140 to->vval.v_dict = from->vval.v_dict;
16141 ++to->vval.v_dict->dv_refcount;
16142 }
16143 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016144 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016145 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016146 break;
16147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148}
16149
16150/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000016151 * Make a copy of an item.
16152 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016153 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
16154 * reference to an already copied list/dict can be used.
16155 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000016156 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016157 static int
16158item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000016159 typval_T *from;
16160 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016161 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016162 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016163{
16164 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016165 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016166
Bram Moolenaar33570922005-01-25 22:26:29 +000016167 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016168 {
16169 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016170 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016171 }
16172 ++recurse;
16173
16174 switch (from->v_type)
16175 {
16176 case VAR_NUMBER:
16177 case VAR_STRING:
16178 case VAR_FUNC:
16179 copy_tv(from, to);
16180 break;
16181 case VAR_LIST:
16182 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016183 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016184 if (from->vval.v_list == NULL)
16185 to->vval.v_list = NULL;
16186 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
16187 {
16188 /* use the copy made earlier */
16189 to->vval.v_list = from->vval.v_list->lv_copylist;
16190 ++to->vval.v_list->lv_refcount;
16191 }
16192 else
16193 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
16194 if (to->vval.v_list == NULL)
16195 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016196 break;
16197 case VAR_DICT:
16198 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016199 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016200 if (from->vval.v_dict == NULL)
16201 to->vval.v_dict = NULL;
16202 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
16203 {
16204 /* use the copy made earlier */
16205 to->vval.v_dict = from->vval.v_dict->dv_copydict;
16206 ++to->vval.v_dict->dv_refcount;
16207 }
16208 else
16209 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
16210 if (to->vval.v_dict == NULL)
16211 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016212 break;
16213 default:
16214 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016215 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016216 }
16217 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016218 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016219}
16220
16221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222 * ":echo expr1 ..." print each argument separated with a space, add a
16223 * newline at the end.
16224 * ":echon expr1 ..." print each argument plain.
16225 */
16226 void
16227ex_echo(eap)
16228 exarg_T *eap;
16229{
16230 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016231 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016232 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233 char_u *p;
16234 int needclr = TRUE;
16235 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016236 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016237
16238 if (eap->skip)
16239 ++emsg_skip;
16240 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
16241 {
16242 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016243 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244 {
16245 /*
16246 * Report the invalid expression unless the expression evaluation
16247 * has been cancelled due to an aborting error, an interrupt, or an
16248 * exception.
16249 */
16250 if (!aborting())
16251 EMSG2(_(e_invexpr2), p);
16252 break;
16253 }
16254 if (!eap->skip)
16255 {
16256 if (atstart)
16257 {
16258 atstart = FALSE;
16259 /* Call msg_start() after eval1(), evaluating the expression
16260 * may cause a message to appear. */
16261 if (eap->cmdidx == CMD_echo)
16262 msg_start();
16263 }
16264 else if (eap->cmdidx == CMD_echo)
16265 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016266 p = echo_string(&rettv, &tofree, numbuf);
16267 if (p != NULL)
16268 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016270 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016271 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016272 if (*p != TAB && needclr)
16273 {
16274 /* remove any text still there from the command */
16275 msg_clr_eos();
16276 needclr = FALSE;
16277 }
16278 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016279 }
16280 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016281 {
16282#ifdef FEAT_MBYTE
16283 if (has_mbyte)
16284 {
16285 int i = (*mb_ptr2len_check)(p);
16286
16287 (void)msg_outtrans_len_attr(p, i, echo_attr);
16288 p += i - 1;
16289 }
16290 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016291#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016292 (void)msg_outtrans_len_attr(p, 1, echo_attr);
16293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016294 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016295 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016297 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298 arg = skipwhite(arg);
16299 }
16300 eap->nextcmd = check_nextcmd(arg);
16301
16302 if (eap->skip)
16303 --emsg_skip;
16304 else
16305 {
16306 /* remove text that may still be there from the command */
16307 if (needclr)
16308 msg_clr_eos();
16309 if (eap->cmdidx == CMD_echo)
16310 msg_end();
16311 }
16312}
16313
16314/*
16315 * ":echohl {name}".
16316 */
16317 void
16318ex_echohl(eap)
16319 exarg_T *eap;
16320{
16321 int id;
16322
16323 id = syn_name2id(eap->arg);
16324 if (id == 0)
16325 echo_attr = 0;
16326 else
16327 echo_attr = syn_id2attr(id);
16328}
16329
16330/*
16331 * ":execute expr1 ..." execute the result of an expression.
16332 * ":echomsg expr1 ..." Print a message
16333 * ":echoerr expr1 ..." Print an error
16334 * Each gets spaces around each argument and a newline at the end for
16335 * echo commands
16336 */
16337 void
16338ex_execute(eap)
16339 exarg_T *eap;
16340{
16341 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016342 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016343 int ret = OK;
16344 char_u *p;
16345 garray_T ga;
16346 int len;
16347 int save_did_emsg;
16348
16349 ga_init2(&ga, 1, 80);
16350
16351 if (eap->skip)
16352 ++emsg_skip;
16353 while (*arg != NUL && *arg != '|' && *arg != '\n')
16354 {
16355 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016356 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016357 {
16358 /*
16359 * Report the invalid expression unless the expression evaluation
16360 * has been cancelled due to an aborting error, an interrupt, or an
16361 * exception.
16362 */
16363 if (!aborting())
16364 EMSG2(_(e_invexpr2), p);
16365 ret = FAIL;
16366 break;
16367 }
16368
16369 if (!eap->skip)
16370 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016371 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016372 len = (int)STRLEN(p);
16373 if (ga_grow(&ga, len + 2) == FAIL)
16374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016375 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376 ret = FAIL;
16377 break;
16378 }
16379 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382 ga.ga_len += len;
16383 }
16384
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016385 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016386 arg = skipwhite(arg);
16387 }
16388
16389 if (ret != FAIL && ga.ga_data != NULL)
16390 {
16391 if (eap->cmdidx == CMD_echomsg)
16392 MSG_ATTR(ga.ga_data, echo_attr);
16393 else if (eap->cmdidx == CMD_echoerr)
16394 {
16395 /* We don't want to abort following commands, restore did_emsg. */
16396 save_did_emsg = did_emsg;
16397 EMSG((char_u *)ga.ga_data);
16398 if (!force_abort)
16399 did_emsg = save_did_emsg;
16400 }
16401 else if (eap->cmdidx == CMD_execute)
16402 do_cmdline((char_u *)ga.ga_data,
16403 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
16404 }
16405
16406 ga_clear(&ga);
16407
16408 if (eap->skip)
16409 --emsg_skip;
16410
16411 eap->nextcmd = check_nextcmd(arg);
16412}
16413
16414/*
16415 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
16416 * "arg" points to the "&" or '+' when called, to "option" when returning.
16417 * Returns NULL when no option name found. Otherwise pointer to the char
16418 * after the option name.
16419 */
16420 static char_u *
16421find_option_end(arg, opt_flags)
16422 char_u **arg;
16423 int *opt_flags;
16424{
16425 char_u *p = *arg;
16426
16427 ++p;
16428 if (*p == 'g' && p[1] == ':')
16429 {
16430 *opt_flags = OPT_GLOBAL;
16431 p += 2;
16432 }
16433 else if (*p == 'l' && p[1] == ':')
16434 {
16435 *opt_flags = OPT_LOCAL;
16436 p += 2;
16437 }
16438 else
16439 *opt_flags = 0;
16440
16441 if (!ASCII_ISALPHA(*p))
16442 return NULL;
16443 *arg = p;
16444
16445 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
16446 p += 4; /* termcap option */
16447 else
16448 while (ASCII_ISALPHA(*p))
16449 ++p;
16450 return p;
16451}
16452
16453/*
16454 * ":function"
16455 */
16456 void
16457ex_function(eap)
16458 exarg_T *eap;
16459{
16460 char_u *theline;
16461 int j;
16462 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016463 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016464 char_u *name = NULL;
16465 char_u *p;
16466 char_u *arg;
16467 garray_T newargs;
16468 garray_T newlines;
16469 int varargs = FALSE;
16470 int mustend = FALSE;
16471 int flags = 0;
16472 ufunc_T *fp;
16473 int indent;
16474 int nesting;
16475 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000016476 dictitem_T *v;
16477 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016478 static int func_nr = 0; /* number for nameless function */
16479 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016480 hashtab_T *ht;
16481 int todo;
16482 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016483
16484 /*
16485 * ":function" without argument: list functions.
16486 */
16487 if (ends_excmd(*eap->arg))
16488 {
16489 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016490 {
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000016491 todo = func_hashtab.ht_used;
16492 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016493 {
16494 if (!HASHITEM_EMPTY(hi))
16495 {
16496 --todo;
16497 fp = HI2UF(hi);
16498 if (!isdigit(*fp->uf_name))
16499 list_func_head(fp, FALSE);
16500 }
16501 }
16502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016503 eap->nextcmd = check_nextcmd(eap->arg);
16504 return;
16505 }
16506
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016507 /*
16508 * Get the function name. There are these situations:
16509 * func normal function name
16510 * "name" == func, "fudi.fd_dict" == NULL
16511 * dict.func new dictionary entry
16512 * "name" == NULL, "fudi.fd_dict" set,
16513 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
16514 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016515 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016516 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16517 * dict.func existing dict entry that's not a Funcref
16518 * "name" == NULL, "fudi.fd_dict" set,
16519 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
16520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016521 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016522 name = trans_function_name(&p, eap->skip, 0, &fudi);
16523 paren = (vim_strchr(p, '(') != NULL);
16524 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016525 {
16526 /*
16527 * Return on an invalid expression in braces, unless the expression
16528 * evaluation has been cancelled due to an aborting error, an
16529 * interrupt, or an exception.
16530 */
16531 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016532 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016533 if (!eap->skip && fudi.fd_newkey != NULL)
16534 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016535 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016538 else
16539 eap->skip = TRUE;
16540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016541 /* An error in a function call during evaluation of an expression in magic
16542 * braces should not cause the function not to be defined. */
16543 saved_did_emsg = did_emsg;
16544 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016545
16546 /*
16547 * ":function func" with only function name: list function.
16548 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016549 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016550 {
16551 if (!ends_excmd(*skipwhite(p)))
16552 {
16553 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016554 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016555 }
16556 eap->nextcmd = check_nextcmd(p);
16557 if (eap->nextcmd != NULL)
16558 *p = NUL;
16559 if (!eap->skip && !got_int)
16560 {
16561 fp = find_func(name);
16562 if (fp != NULL)
16563 {
16564 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016565 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016566 {
16567 msg_putchar('\n');
16568 msg_outnum((long)(j + 1));
16569 if (j < 9)
16570 msg_putchar(' ');
16571 if (j < 99)
16572 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016573 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016574 out_flush(); /* show a line at a time */
16575 ui_breakcheck();
16576 }
16577 if (!got_int)
16578 {
16579 msg_putchar('\n');
16580 msg_puts((char_u *)" endfunction");
16581 }
16582 }
16583 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016584 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016585 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016586 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016587 }
16588
16589 /*
16590 * ":function name(arg1, arg2)" Define function.
16591 */
16592 p = skipwhite(p);
16593 if (*p != '(')
16594 {
16595 if (!eap->skip)
16596 {
16597 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016598 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016599 }
16600 /* attempt to continue by skipping some text */
16601 if (vim_strchr(p, '(') != NULL)
16602 p = vim_strchr(p, '(');
16603 }
16604 p = skipwhite(p + 1);
16605
16606 ga_init2(&newargs, (int)sizeof(char_u *), 3);
16607 ga_init2(&newlines, (int)sizeof(char_u *), 3);
16608
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016609 if (!eap->skip)
16610 {
16611 /* Check the name of the function. */
16612 if (name != NULL)
16613 arg = name;
16614 else
16615 arg = fudi.fd_newkey;
16616 if (arg != NULL)
16617 {
16618 if (*arg == K_SPECIAL)
16619 j = 3;
16620 else
16621 j = 0;
16622 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
16623 : eval_isnamec(arg[j])))
16624 ++j;
16625 if (arg[j] != NUL)
16626 emsg_funcname(_(e_invarg2), arg);
16627 }
16628 }
16629
Bram Moolenaar071d4272004-06-13 20:20:40 +000016630 /*
16631 * Isolate the arguments: "arg1, arg2, ...)"
16632 */
16633 while (*p != ')')
16634 {
16635 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
16636 {
16637 varargs = TRUE;
16638 p += 3;
16639 mustend = TRUE;
16640 }
16641 else
16642 {
16643 arg = p;
16644 while (ASCII_ISALNUM(*p) || *p == '_')
16645 ++p;
16646 if (arg == p || isdigit(*arg)
16647 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
16648 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
16649 {
16650 if (!eap->skip)
16651 EMSG2(_("E125: Illegal argument: %s"), arg);
16652 break;
16653 }
16654 if (ga_grow(&newargs, 1) == FAIL)
16655 goto erret;
16656 c = *p;
16657 *p = NUL;
16658 arg = vim_strsave(arg);
16659 if (arg == NULL)
16660 goto erret;
16661 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
16662 *p = c;
16663 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016664 if (*p == ',')
16665 ++p;
16666 else
16667 mustend = TRUE;
16668 }
16669 p = skipwhite(p);
16670 if (mustend && *p != ')')
16671 {
16672 if (!eap->skip)
16673 EMSG2(_(e_invarg2), eap->arg);
16674 break;
16675 }
16676 }
16677 ++p; /* skip the ')' */
16678
Bram Moolenaare9a41262005-01-15 22:18:47 +000016679 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680 for (;;)
16681 {
16682 p = skipwhite(p);
16683 if (STRNCMP(p, "range", 5) == 0)
16684 {
16685 flags |= FC_RANGE;
16686 p += 5;
16687 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016688 else if (STRNCMP(p, "dict", 4) == 0)
16689 {
16690 flags |= FC_DICT;
16691 p += 4;
16692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016693 else if (STRNCMP(p, "abort", 5) == 0)
16694 {
16695 flags |= FC_ABORT;
16696 p += 5;
16697 }
16698 else
16699 break;
16700 }
16701
16702 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
16703 EMSG(_(e_trailing));
16704
16705 /*
16706 * Read the body of the function, until ":endfunction" is found.
16707 */
16708 if (KeyTyped)
16709 {
16710 /* Check if the function already exists, don't let the user type the
16711 * whole function before telling him it doesn't work! For a script we
16712 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016713 if (!eap->skip && !eap->forceit)
16714 {
16715 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
16716 EMSG(_(e_funcdict));
16717 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016718 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720
Bram Moolenaard857f0e2005-06-21 22:37:39 +000016721 if (!eap->skip && did_emsg)
16722 goto erret;
16723
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724 msg_putchar('\n'); /* don't overwrite the function name */
16725 cmdline_row = msg_row;
16726 }
16727
16728 indent = 2;
16729 nesting = 0;
16730 for (;;)
16731 {
16732 msg_scroll = TRUE;
16733 need_wait_return = FALSE;
16734 if (eap->getline == NULL)
16735 theline = getcmdline(':', 0L, indent);
16736 else
16737 theline = eap->getline(':', eap->cookie, indent);
16738 if (KeyTyped)
16739 lines_left = Rows - 1;
16740 if (theline == NULL)
16741 {
16742 EMSG(_("E126: Missing :endfunction"));
16743 goto erret;
16744 }
16745
16746 if (skip_until != NULL)
16747 {
16748 /* between ":append" and "." and between ":python <<EOF" and "EOF"
16749 * don't check for ":endfunc". */
16750 if (STRCMP(theline, skip_until) == 0)
16751 {
16752 vim_free(skip_until);
16753 skip_until = NULL;
16754 }
16755 }
16756 else
16757 {
16758 /* skip ':' and blanks*/
16759 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
16760 ;
16761
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016762 /* Check for "endfunction". */
16763 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016764 {
16765 vim_free(theline);
16766 break;
16767 }
16768
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016769 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000016770 * at "end". */
16771 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
16772 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016773 else if (STRNCMP(p, "if", 2) == 0
16774 || STRNCMP(p, "wh", 2) == 0
16775 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000016776 || STRNCMP(p, "try", 3) == 0)
16777 indent += 2;
16778
16779 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016780 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000016782 if (*p == '!')
16783 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784 p += eval_fname_script(p);
16785 if (ASCII_ISALPHA(*p))
16786 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016787 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788 if (*skipwhite(p) == '(')
16789 {
16790 ++nesting;
16791 indent += 2;
16792 }
16793 }
16794 }
16795
16796 /* Check for ":append" or ":insert". */
16797 p = skip_range(p, NULL);
16798 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
16799 || (p[0] == 'i'
16800 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
16801 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
16802 skip_until = vim_strsave((char_u *)".");
16803
16804 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
16805 arg = skipwhite(skiptowhite(p));
16806 if (arg[0] == '<' && arg[1] =='<'
16807 && ((p[0] == 'p' && p[1] == 'y'
16808 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
16809 || (p[0] == 'p' && p[1] == 'e'
16810 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
16811 || (p[0] == 't' && p[1] == 'c'
16812 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
16813 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
16814 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000016815 || (p[0] == 'm' && p[1] == 'z'
16816 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817 ))
16818 {
16819 /* ":python <<" continues until a dot, like ":append" */
16820 p = skipwhite(arg + 2);
16821 if (*p == NUL)
16822 skip_until = vim_strsave((char_u *)".");
16823 else
16824 skip_until = vim_strsave(p);
16825 }
16826 }
16827
16828 /* Add the line to the function. */
16829 if (ga_grow(&newlines, 1) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016830 {
16831 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016832 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000016833 }
16834
16835 /* Copy the line to newly allocated memory. get_one_sourceline()
16836 * allocates 250 bytes per line, this saves 80% on average. The cost
16837 * is an extra alloc/free. */
16838 p = vim_strsave(theline);
16839 if (p != NULL)
16840 {
16841 vim_free(theline);
16842 theline = p;
16843 }
16844
Bram Moolenaar071d4272004-06-13 20:20:40 +000016845 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
16846 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016847 }
16848
16849 /* Don't define the function when skipping commands or when an error was
16850 * detected. */
16851 if (eap->skip || did_emsg)
16852 goto erret;
16853
16854 /*
16855 * If there are no errors, add the function
16856 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016857 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016858 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016859 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000016860 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016861 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016862 emsg_funcname("E707: Function name conflicts with variable: %s",
16863 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016864 goto erret;
16865 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016866
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016867 fp = find_func(name);
16868 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016869 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016870 if (!eap->forceit)
16871 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016872 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016873 goto erret;
16874 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016875 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016876 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016877 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016878 name);
16879 goto erret;
16880 }
16881 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016882 ga_clear_strings(&(fp->uf_args));
16883 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016884 vim_free(name);
16885 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887 }
16888 else
16889 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016890 char numbuf[20];
16891
16892 fp = NULL;
16893 if (fudi.fd_newkey == NULL && !eap->forceit)
16894 {
16895 EMSG(_(e_funcdict));
16896 goto erret;
16897 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000016898 if (fudi.fd_di == NULL)
16899 {
16900 /* Can't add a function to a locked dictionary */
16901 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
16902 goto erret;
16903 }
16904 /* Can't change an existing function if it is locked */
16905 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
16906 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016907
16908 /* Give the function a sequential number. Can only be used with a
16909 * Funcref! */
16910 vim_free(name);
16911 sprintf(numbuf, "%d", ++func_nr);
16912 name = vim_strsave((char_u *)numbuf);
16913 if (name == NULL)
16914 goto erret;
16915 }
16916
16917 if (fp == NULL)
16918 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000016919 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016920 {
16921 int slen, plen;
16922 char_u *scriptname;
16923
16924 /* Check that the autoload name matches the script name. */
16925 j = FAIL;
16926 if (sourcing_name != NULL)
16927 {
16928 scriptname = autoload_name(name);
16929 if (scriptname != NULL)
16930 {
16931 p = vim_strchr(scriptname, '/');
16932 plen = STRLEN(p);
16933 slen = STRLEN(sourcing_name);
16934 if (slen > plen && fnamecmp(p,
16935 sourcing_name + slen - plen) == 0)
16936 j = OK;
16937 vim_free(scriptname);
16938 }
16939 }
16940 if (j == FAIL)
16941 {
16942 EMSG2(_("E746: Function name does not match script file name: %s"), name);
16943 goto erret;
16944 }
16945 }
16946
16947 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948 if (fp == NULL)
16949 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016950
16951 if (fudi.fd_dict != NULL)
16952 {
16953 if (fudi.fd_di == NULL)
16954 {
16955 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016956 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016957 if (fudi.fd_di == NULL)
16958 {
16959 vim_free(fp);
16960 goto erret;
16961 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016962 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
16963 {
16964 vim_free(fudi.fd_di);
16965 goto erret;
16966 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016967 }
16968 else
16969 /* overwrite existing dict entry */
16970 clear_tv(&fudi.fd_di->di_tv);
16971 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016972 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016973 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016974 fp->uf_refcount = 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016975 }
16976
Bram Moolenaar071d4272004-06-13 20:20:40 +000016977 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016978 STRCPY(fp->uf_name, name);
16979 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016980 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016981 fp->uf_args = newargs;
16982 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000016983#ifdef FEAT_PROFILE
16984 fp->uf_tml_count = NULL;
16985 fp->uf_tml_total = NULL;
16986 fp->uf_tml_self = NULL;
16987 fp->uf_profiling = FALSE;
16988 if (prof_def_func())
16989 func_do_profile(fp);
16990#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016991 fp->uf_varargs = varargs;
16992 fp->uf_flags = flags;
16993 fp->uf_calls = 0;
16994 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016995 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016996
16997erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998 ga_clear_strings(&newargs);
16999 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017000ret_free:
17001 vim_free(skip_until);
17002 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005}
17006
17007/*
17008 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000017009 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017010 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017011 * flags:
17012 * TFN_INT: internal function name OK
17013 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000017014 * Advances "pp" to just after the function name (if no error).
17015 */
17016 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017017trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018 char_u **pp;
17019 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017020 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000017021 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017023 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017024 char_u *start;
17025 char_u *end;
17026 int lead;
17027 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017028 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017029 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017030
17031 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000017032 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000017034
17035 /* Check for hard coded <SNR>: already translated function ID (from a user
17036 * command). */
17037 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
17038 && (*pp)[2] == (int)KE_SNR)
17039 {
17040 *pp += 3;
17041 len = get_id_len(pp) + 3;
17042 return vim_strnsave(start, len);
17043 }
17044
17045 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
17046 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000017048 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017049 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017050
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017051 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
17052 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017053 if (end == start)
17054 {
17055 if (!skip)
17056 EMSG(_("E129: Function name required"));
17057 goto theend;
17058 }
Bram Moolenaara7043832005-01-21 11:56:39 +000017059 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017060 {
17061 /*
17062 * Report an invalid expression in braces, unless the expression
17063 * evaluation has been cancelled due to an aborting error, an
17064 * interrupt, or an exception.
17065 */
17066 if (!aborting())
17067 {
17068 if (end != NULL)
17069 EMSG2(_(e_invarg2), start);
17070 }
17071 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017072 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017073 goto theend;
17074 }
17075
17076 if (lv.ll_tv != NULL)
17077 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017078 if (fdp != NULL)
17079 {
17080 fdp->fd_dict = lv.ll_dict;
17081 fdp->fd_newkey = lv.ll_newkey;
17082 lv.ll_newkey = NULL;
17083 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017084 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017085 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
17086 {
17087 name = vim_strsave(lv.ll_tv->vval.v_string);
17088 *pp = end;
17089 }
17090 else
17091 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017092 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
17093 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017094 EMSG(_(e_funcref));
17095 else
17096 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017097 name = NULL;
17098 }
17099 goto theend;
17100 }
17101
17102 if (lv.ll_name == NULL)
17103 {
17104 /* Error found, but continue after the function name. */
17105 *pp = end;
17106 goto theend;
17107 }
17108
17109 if (lv.ll_exp_name != NULL)
17110 len = STRLEN(lv.ll_exp_name);
17111 else
Bram Moolenaara7043832005-01-21 11:56:39 +000017112 {
17113 if (lead == 2) /* skip over "s:" */
17114 lv.ll_name += 2;
17115 len = (int)(end - lv.ll_name);
17116 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017117
17118 /*
17119 * Copy the function name to allocated memory.
17120 * Accept <SID>name() inside a script, translate into <SNR>123_name().
17121 * Accept <SNR>123_name() outside a script.
17122 */
17123 if (skip)
17124 lead = 0; /* do nothing */
17125 else if (lead > 0)
17126 {
17127 lead = 3;
17128 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
17129 {
17130 if (current_SID <= 0)
17131 {
17132 EMSG(_(e_usingsid));
17133 goto theend;
17134 }
17135 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
17136 lead += (int)STRLEN(sid_buf);
17137 }
17138 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017139 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017140 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017141 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017142 goto theend;
17143 }
17144 name = alloc((unsigned)(len + lead + 1));
17145 if (name != NULL)
17146 {
17147 if (lead > 0)
17148 {
17149 name[0] = K_SPECIAL;
17150 name[1] = KS_EXTRA;
17151 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000017152 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000017153 STRCPY(name + 3, sid_buf);
17154 }
17155 mch_memmove(name + lead, lv.ll_name, (size_t)len);
17156 name[len + lead] = NUL;
17157 }
17158 *pp = end;
17159
17160theend:
17161 clear_lval(&lv);
17162 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017163}
17164
17165/*
17166 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
17167 * Return 2 if "p" starts with "s:".
17168 * Return 0 otherwise.
17169 */
17170 static int
17171eval_fname_script(p)
17172 char_u *p;
17173{
17174 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
17175 || STRNICMP(p + 1, "SNR>", 4) == 0))
17176 return 5;
17177 if (p[0] == 's' && p[1] == ':')
17178 return 2;
17179 return 0;
17180}
17181
17182/*
17183 * Return TRUE if "p" starts with "<SID>" or "s:".
17184 * Only works if eval_fname_script() returned non-zero for "p"!
17185 */
17186 static int
17187eval_fname_sid(p)
17188 char_u *p;
17189{
17190 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
17191}
17192
17193/*
17194 * List the head of the function: "name(arg1, arg2)".
17195 */
17196 static void
17197list_func_head(fp, indent)
17198 ufunc_T *fp;
17199 int indent;
17200{
17201 int j;
17202
17203 msg_start();
17204 if (indent)
17205 MSG_PUTS(" ");
17206 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017207 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017208 {
17209 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017210 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017211 }
17212 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017213 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017214 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017215 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017216 {
17217 if (j)
17218 MSG_PUTS(", ");
17219 msg_puts(FUNCARG(fp, j));
17220 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017221 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017222 {
17223 if (j)
17224 MSG_PUTS(", ");
17225 MSG_PUTS("...");
17226 }
17227 msg_putchar(')');
17228}
17229
17230/*
17231 * Find a function by name, return pointer to it in ufuncs.
17232 * Return NULL for unknown function.
17233 */
17234 static ufunc_T *
17235find_func(name)
17236 char_u *name;
17237{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017238 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017239
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017240 hi = hash_find(&func_hashtab, name);
17241 if (!HASHITEM_EMPTY(hi))
17242 return HI2UF(hi);
17243 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244}
17245
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000017246#if defined(EXITFREE) || defined(PROTO)
17247 void
17248free_all_functions()
17249{
17250 hashitem_T *hi;
17251
17252 /* Need to start all over every time, because func_free() may change the
17253 * hash table. */
17254 while (func_hashtab.ht_used > 0)
17255 for (hi = func_hashtab.ht_array; ; ++hi)
17256 if (!HASHITEM_EMPTY(hi))
17257 {
17258 func_free(HI2UF(hi));
17259 break;
17260 }
17261}
17262#endif
17263
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017264/*
17265 * Return TRUE if a function "name" exists.
17266 */
17267 static int
17268function_exists(name)
17269 char_u *name;
17270{
17271 char_u *p = name;
17272 int n = FALSE;
17273
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017274 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017275 if (p != NULL)
17276 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017277 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017278 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017279 else
17280 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017281 vim_free(p);
17282 }
17283 return n;
17284}
17285
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017286/*
17287 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017288 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017289 */
17290 static int
17291builtin_function(name)
17292 char_u *name;
17293{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017294 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
17295 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017296}
17297
Bram Moolenaar05159a02005-02-26 23:04:13 +000017298#if defined(FEAT_PROFILE) || defined(PROTO)
17299/*
17300 * Start profiling function "fp".
17301 */
17302 static void
17303func_do_profile(fp)
17304 ufunc_T *fp;
17305{
17306 fp->uf_tm_count = 0;
17307 profile_zero(&fp->uf_tm_self);
17308 profile_zero(&fp->uf_tm_total);
17309 if (fp->uf_tml_count == NULL)
17310 fp->uf_tml_count = (int *)alloc_clear((unsigned)
17311 (sizeof(int) * fp->uf_lines.ga_len));
17312 if (fp->uf_tml_total == NULL)
17313 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
17314 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17315 if (fp->uf_tml_self == NULL)
17316 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
17317 (sizeof(proftime_T) * fp->uf_lines.ga_len));
17318 fp->uf_tml_idx = -1;
17319 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
17320 || fp->uf_tml_self == NULL)
17321 return; /* out of memory */
17322
17323 fp->uf_profiling = TRUE;
17324}
17325
17326/*
17327 * Dump the profiling results for all functions in file "fd".
17328 */
17329 void
17330func_dump_profile(fd)
17331 FILE *fd;
17332{
17333 hashitem_T *hi;
17334 int todo;
17335 ufunc_T *fp;
17336 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000017337 ufunc_T **sorttab;
17338 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017339
17340 todo = func_hashtab.ht_used;
Bram Moolenaar73830342005-02-28 22:48:19 +000017341 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
17342
Bram Moolenaar05159a02005-02-26 23:04:13 +000017343 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
17344 {
17345 if (!HASHITEM_EMPTY(hi))
17346 {
17347 --todo;
17348 fp = HI2UF(hi);
17349 if (fp->uf_profiling)
17350 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017351 if (sorttab != NULL)
17352 sorttab[st_len++] = fp;
17353
Bram Moolenaar05159a02005-02-26 23:04:13 +000017354 if (fp->uf_name[0] == K_SPECIAL)
17355 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
17356 else
17357 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
17358 if (fp->uf_tm_count == 1)
17359 fprintf(fd, "Called 1 time\n");
17360 else
17361 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
17362 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
17363 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
17364 fprintf(fd, "\n");
17365 fprintf(fd, "count total (s) self (s)\n");
17366
17367 for (i = 0; i < fp->uf_lines.ga_len; ++i)
17368 {
Bram Moolenaar73830342005-02-28 22:48:19 +000017369 prof_func_line(fd, fp->uf_tml_count[i],
17370 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017371 fprintf(fd, "%s\n", FUNCLINE(fp, i));
17372 }
17373 fprintf(fd, "\n");
17374 }
17375 }
17376 }
Bram Moolenaar73830342005-02-28 22:48:19 +000017377
17378 if (sorttab != NULL && st_len > 0)
17379 {
17380 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17381 prof_total_cmp);
17382 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
17383 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
17384 prof_self_cmp);
17385 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
17386 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017387}
Bram Moolenaar73830342005-02-28 22:48:19 +000017388
17389 static void
17390prof_sort_list(fd, sorttab, st_len, title, prefer_self)
17391 FILE *fd;
17392 ufunc_T **sorttab;
17393 int st_len;
17394 char *title;
17395 int prefer_self; /* when equal print only self time */
17396{
17397 int i;
17398 ufunc_T *fp;
17399
17400 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
17401 fprintf(fd, "count total (s) self (s) function\n");
17402 for (i = 0; i < 20 && i < st_len; ++i)
17403 {
17404 fp = sorttab[i];
17405 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
17406 prefer_self);
17407 if (fp->uf_name[0] == K_SPECIAL)
17408 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
17409 else
17410 fprintf(fd, " %s()\n", fp->uf_name);
17411 }
17412 fprintf(fd, "\n");
17413}
17414
17415/*
17416 * Print the count and times for one function or function line.
17417 */
17418 static void
17419prof_func_line(fd, count, total, self, prefer_self)
17420 FILE *fd;
17421 int count;
17422 proftime_T *total;
17423 proftime_T *self;
17424 int prefer_self; /* when equal print only self time */
17425{
17426 if (count > 0)
17427 {
17428 fprintf(fd, "%5d ", count);
17429 if (prefer_self && profile_equal(total, self))
17430 fprintf(fd, " ");
17431 else
17432 fprintf(fd, "%s ", profile_msg(total));
17433 if (!prefer_self && profile_equal(total, self))
17434 fprintf(fd, " ");
17435 else
17436 fprintf(fd, "%s ", profile_msg(self));
17437 }
17438 else
17439 fprintf(fd, " ");
17440}
17441
17442/*
17443 * Compare function for total time sorting.
17444 */
17445 static int
17446#ifdef __BORLANDC__
17447_RTLENTRYF
17448#endif
17449prof_total_cmp(s1, s2)
17450 const void *s1;
17451 const void *s2;
17452{
17453 ufunc_T *p1, *p2;
17454
17455 p1 = *(ufunc_T **)s1;
17456 p2 = *(ufunc_T **)s2;
17457 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
17458}
17459
17460/*
17461 * Compare function for self time sorting.
17462 */
17463 static int
17464#ifdef __BORLANDC__
17465_RTLENTRYF
17466#endif
17467prof_self_cmp(s1, s2)
17468 const void *s1;
17469 const void *s2;
17470{
17471 ufunc_T *p1, *p2;
17472
17473 p1 = *(ufunc_T **)s1;
17474 p2 = *(ufunc_T **)s2;
17475 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
17476}
17477
Bram Moolenaar05159a02005-02-26 23:04:13 +000017478#endif
17479
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017480/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017481 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017482 * Return TRUE if a package was loaded.
17483 */
17484 static int
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017485script_autoload(name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017486 char_u *name;
17487{
17488 char_u *p;
17489 char_u *scriptname;
17490 int ret = FALSE;
17491
17492 /* If there is no colon after name[1] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017493 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017494 if (p == NULL || p <= name + 2)
17495 return FALSE;
17496
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017497 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
17498 scriptname = autoload_name(name);
17499 if (cmd_runtime(scriptname, FALSE) == OK)
17500 ret = TRUE;
17501
17502 vim_free(scriptname);
17503 return ret;
17504}
17505
17506/*
17507 * Return the autoload script name for a function or variable name.
17508 * Returns NULL when out of memory.
17509 */
17510 static char_u *
17511autoload_name(name)
17512 char_u *name;
17513{
17514 char_u *p;
17515 char_u *scriptname;
17516
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017517 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017518 scriptname = alloc((unsigned)(STRLEN(name) + 14));
17519 if (scriptname == NULL)
17520 return FALSE;
17521 STRCPY(scriptname, "autoload/");
17522 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017523 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017524 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017525 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017526 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017527 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000017528}
17529
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
17531
17532/*
17533 * Function given to ExpandGeneric() to obtain the list of user defined
17534 * function names.
17535 */
17536 char_u *
17537get_user_func_name(xp, idx)
17538 expand_T *xp;
17539 int idx;
17540{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017541 static long_u done;
17542 static hashitem_T *hi;
17543 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017544
17545 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017547 done = 0;
17548 hi = func_hashtab.ht_array;
17549 }
17550 if (done < func_hashtab.ht_used)
17551 {
17552 if (done++ > 0)
17553 ++hi;
17554 while (HASHITEM_EMPTY(hi))
17555 ++hi;
17556 fp = HI2UF(hi);
17557
17558 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
17559 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560
17561 cat_func_name(IObuff, fp);
17562 if (xp->xp_context != EXPAND_USER_FUNC)
17563 {
17564 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017565 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017566 STRCAT(IObuff, ")");
17567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017568 return IObuff;
17569 }
17570 return NULL;
17571}
17572
17573#endif /* FEAT_CMDL_COMPL */
17574
17575/*
17576 * Copy the function name of "fp" to buffer "buf".
17577 * "buf" must be able to hold the function name plus three bytes.
17578 * Takes care of script-local function names.
17579 */
17580 static void
17581cat_func_name(buf, fp)
17582 char_u *buf;
17583 ufunc_T *fp;
17584{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017585 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586 {
17587 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017588 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589 }
17590 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017591 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017592}
17593
17594/*
17595 * ":delfunction {name}"
17596 */
17597 void
17598ex_delfunction(eap)
17599 exarg_T *eap;
17600{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017601 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602 char_u *p;
17603 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000017604 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605
17606 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017607 name = trans_function_name(&p, eap->skip, 0, &fudi);
17608 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017610 {
17611 if (fudi.fd_dict != NULL && !eap->skip)
17612 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017613 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017615 if (!ends_excmd(*skipwhite(p)))
17616 {
17617 vim_free(name);
17618 EMSG(_(e_trailing));
17619 return;
17620 }
17621 eap->nextcmd = check_nextcmd(p);
17622 if (eap->nextcmd != NULL)
17623 *p = NUL;
17624
17625 if (!eap->skip)
17626 fp = find_func(name);
17627 vim_free(name);
17628
17629 if (!eap->skip)
17630 {
17631 if (fp == NULL)
17632 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000017633 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017634 return;
17635 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017636 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637 {
17638 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
17639 return;
17640 }
17641
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017642 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017644 /* Delete the dict item that refers to the function, it will
17645 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017646 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017648 else
17649 func_free(fp);
17650 }
17651}
17652
17653/*
17654 * Free a function and remove it from the list of functions.
17655 */
17656 static void
17657func_free(fp)
17658 ufunc_T *fp;
17659{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017660 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017661
17662 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017663 ga_clear_strings(&(fp->uf_args));
17664 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000017665#ifdef FEAT_PROFILE
17666 vim_free(fp->uf_tml_count);
17667 vim_free(fp->uf_tml_total);
17668 vim_free(fp->uf_tml_self);
17669#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017670
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017671 /* remove the function from the function hashtable */
17672 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
17673 if (HASHITEM_EMPTY(hi))
17674 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017675 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017676 hash_remove(&func_hashtab, hi);
17677
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017678 vim_free(fp);
17679}
17680
17681/*
17682 * Unreference a Function: decrement the reference count and free it when it
17683 * becomes zero. Only for numbered functions.
17684 */
17685 static void
17686func_unref(name)
17687 char_u *name;
17688{
17689 ufunc_T *fp;
17690
17691 if (name != NULL && isdigit(*name))
17692 {
17693 fp = find_func(name);
17694 if (fp == NULL)
17695 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017696 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017697 {
17698 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017699 * when "uf_calls" becomes zero. */
17700 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017701 func_free(fp);
17702 }
17703 }
17704}
17705
17706/*
17707 * Count a reference to a Function.
17708 */
17709 static void
17710func_ref(name)
17711 char_u *name;
17712{
17713 ufunc_T *fp;
17714
17715 if (name != NULL && isdigit(*name))
17716 {
17717 fp = find_func(name);
17718 if (fp == NULL)
17719 EMSG2(_(e_intern2), "func_ref()");
17720 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017721 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017722 }
17723}
17724
17725/*
17726 * Call a user function.
17727 */
17728 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000017729call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017730 ufunc_T *fp; /* pointer to function */
17731 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000017732 typval_T *argvars; /* arguments */
17733 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734 linenr_T firstline; /* first line of range */
17735 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000017736 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737{
Bram Moolenaar33570922005-01-25 22:26:29 +000017738 char_u *save_sourcing_name;
17739 linenr_T save_sourcing_lnum;
17740 scid_T save_current_SID;
17741 funccall_T fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000017742 int save_did_emsg;
17743 static int depth = 0;
17744 dictitem_T *v;
17745 int fixvar_idx = 0; /* index in fixvar[] */
17746 int i;
17747 int ai;
17748 char_u numbuf[NUMBUFLEN];
17749 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000017750#ifdef FEAT_PROFILE
17751 proftime_T wait_start;
17752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017753
17754 /* If depth of calling is getting too high, don't execute the function */
17755 if (depth >= p_mfd)
17756 {
17757 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017758 rettv->v_type = VAR_NUMBER;
17759 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760 return;
17761 }
17762 ++depth;
17763
17764 line_breakcheck(); /* check for CTRL-C hit */
17765
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017766 fc.caller = current_funccal;
Bram Moolenaar33570922005-01-25 22:26:29 +000017767 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017768 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017769 fc.rettv = rettv;
17770 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771 fc.linenr = 0;
17772 fc.returned = FALSE;
17773 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774 /* Check if this function has a breakpoint. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017775 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776 fc.dbg_tick = debug_tick;
17777
Bram Moolenaar33570922005-01-25 22:26:29 +000017778 /*
17779 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
17780 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
17781 * each argument variable and saves a lot of time.
17782 */
17783 /*
17784 * Init l: variables.
17785 */
17786 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000017787 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000017788 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017789 /* Set l:self to "selfdict". */
17790 v = &fc.fixvar[fixvar_idx++].var;
17791 STRCPY(v->di_key, "self");
17792 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
17793 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
17794 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017795 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000017796 v->di_tv.vval.v_dict = selfdict;
17797 ++selfdict->dv_refcount;
17798 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000017799
Bram Moolenaar33570922005-01-25 22:26:29 +000017800 /*
17801 * Init a: variables.
17802 * Set a:0 to "argcount".
17803 * Set a:000 to a list with room for the "..." arguments.
17804 */
17805 init_var_dict(&fc.l_avars, &fc.l_avars_var);
17806 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017807 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar33570922005-01-25 22:26:29 +000017808 v = &fc.fixvar[fixvar_idx++].var;
17809 STRCPY(v->di_key, "000");
17810 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17811 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17812 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017813 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017814 v->di_tv.vval.v_list = &fc.l_varlist;
17815 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
17816 fc.l_varlist.lv_refcount = 99999;
17817
17818 /*
17819 * Set a:firstline to "firstline" and a:lastline to "lastline".
17820 * Set a:name to named arguments.
17821 * Set a:N to the "..." arguments.
17822 */
17823 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
17824 (varnumber_T)firstline);
17825 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
17826 (varnumber_T)lastline);
17827 for (i = 0; i < argcount; ++i)
17828 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017829 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000017830 if (ai < 0)
17831 /* named argument a:name */
17832 name = FUNCARG(fp, i);
17833 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000017834 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017835 /* "..." argument a:1, a:2, etc. */
17836 sprintf((char *)numbuf, "%d", ai + 1);
17837 name = numbuf;
17838 }
17839 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
17840 {
17841 v = &fc.fixvar[fixvar_idx++].var;
17842 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
17843 }
17844 else
17845 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017846 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
17847 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000017848 if (v == NULL)
17849 break;
17850 v->di_flags = DI_FLAGS_RO;
17851 }
17852 STRCPY(v->di_key, name);
17853 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
17854
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017855 /* Note: the values are copied directly to avoid alloc/free.
17856 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017857 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017858 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017859
17860 if (ai >= 0 && ai < MAX_FUNC_ARGS)
17861 {
17862 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
17863 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017864 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000017865 }
17866 }
17867
Bram Moolenaar071d4272004-06-13 20:20:40 +000017868 /* Don't redraw while executing the function. */
17869 ++RedrawingDisabled;
17870 save_sourcing_name = sourcing_name;
17871 save_sourcing_lnum = sourcing_lnum;
17872 sourcing_lnum = 1;
17873 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017874 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875 if (sourcing_name != NULL)
17876 {
17877 if (save_sourcing_name != NULL
17878 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
17879 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
17880 else
17881 STRCPY(sourcing_name, "function ");
17882 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
17883
17884 if (p_verbose >= 12)
17885 {
17886 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017887 verbose_enter_scroll();
17888
Bram Moolenaar555b2802005-05-19 21:08:39 +000017889 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017890 if (p_verbose >= 14)
17891 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000017893 char_u numbuf[NUMBUFLEN];
17894 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017895
17896 msg_puts((char_u *)"(");
17897 for (i = 0; i < argcount; ++i)
17898 {
17899 if (i > 0)
17900 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017901 if (argvars[i].v_type == VAR_NUMBER)
17902 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903 else
17904 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017905 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017906 buf, MSG_BUF_CLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017907 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017908 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017909 }
17910 }
17911 msg_puts((char_u *)")");
17912 }
17913 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017914
17915 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916 --no_wait_return;
17917 }
17918 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000017919#ifdef FEAT_PROFILE
17920 if (do_profiling)
17921 {
17922 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
17923 func_do_profile(fp);
17924 if (fp->uf_profiling
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017925 || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000017926 {
17927 ++fp->uf_tm_count;
17928 profile_start(&fp->uf_tm_start);
17929 profile_zero(&fp->uf_tm_children);
17930 }
17931 script_prof_save(&wait_start);
17932 }
17933#endif
17934
Bram Moolenaar071d4272004-06-13 20:20:40 +000017935 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017936 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017937 save_did_emsg = did_emsg;
17938 did_emsg = FALSE;
17939
17940 /* call do_cmdline() to execute the lines */
17941 do_cmdline(NULL, get_func_line, (void *)&fc,
17942 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
17943
17944 --RedrawingDisabled;
17945
17946 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000017947 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017949 clear_tv(rettv);
17950 rettv->v_type = VAR_NUMBER;
17951 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017952 }
17953
Bram Moolenaar05159a02005-02-26 23:04:13 +000017954#ifdef FEAT_PROFILE
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017955 if (fp->uf_profiling || (fc.caller != NULL && &fc.caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000017956 {
17957 profile_end(&fp->uf_tm_start);
17958 profile_sub_wait(&wait_start, &fp->uf_tm_start);
17959 profile_add(&fp->uf_tm_total, &fp->uf_tm_start);
17960 profile_add(&fp->uf_tm_self, &fp->uf_tm_start);
17961 profile_sub(&fp->uf_tm_self, &fp->uf_tm_children);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017962 if (fc.caller != NULL && &fc.caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017963 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000017964 profile_add(&fc.caller->func->uf_tm_children, &fp->uf_tm_start);
17965 profile_add(&fc.caller->func->uf_tml_children, &fp->uf_tm_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017966 }
17967 }
17968#endif
17969
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970 /* when being verbose, mention the return value */
17971 if (p_verbose >= 12)
17972 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017974 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017975
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000017977 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017978 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000017979 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
17980 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017981 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000017982 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017983 char_u buf[MSG_BUF_LEN];
17984 char_u numbuf[NUMBUFLEN];
17985 char_u *tofree;
17986
Bram Moolenaar555b2802005-05-19 21:08:39 +000017987 /* The value may be very long. Skip the middle part, so that we
17988 * have some idea how it starts and ends. smsg() would always
17989 * truncate it at the end. */
Bram Moolenaar758711c2005-02-02 23:11:38 +000017990 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000017991 buf, MSG_BUF_CLEN);
Bram Moolenaar555b2802005-05-19 21:08:39 +000017992 smsg((char_u *)_("%s returning %s"), sourcing_name, buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000017993 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017994 }
17995 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000017996
17997 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017998 --no_wait_return;
17999 }
18000
18001 vim_free(sourcing_name);
18002 sourcing_name = save_sourcing_name;
18003 sourcing_lnum = save_sourcing_lnum;
18004 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018005#ifdef FEAT_PROFILE
18006 if (do_profiling)
18007 script_prof_restore(&wait_start);
18008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009
18010 if (p_verbose >= 12 && sourcing_name != NULL)
18011 {
18012 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018013 verbose_enter_scroll();
18014
Bram Moolenaar555b2802005-05-19 21:08:39 +000018015 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018016 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000018017
18018 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018019 --no_wait_return;
18020 }
18021
18022 did_emsg |= save_did_emsg;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000018023 current_funccal = fc.caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024
Bram Moolenaar33570922005-01-25 22:26:29 +000018025 /* The a: variables typevals were not alloced, only free the allocated
18026 * variables. */
18027 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
18028
18029 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030 --depth;
18031}
18032
18033/*
Bram Moolenaar33570922005-01-25 22:26:29 +000018034 * Add a number variable "name" to dict "dp" with value "nr".
18035 */
18036 static void
18037add_nr_var(dp, v, name, nr)
18038 dict_T *dp;
18039 dictitem_T *v;
18040 char *name;
18041 varnumber_T nr;
18042{
18043 STRCPY(v->di_key, name);
18044 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
18045 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
18046 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018047 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018048 v->di_tv.vval.v_number = nr;
18049}
18050
18051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052 * ":return [expr]"
18053 */
18054 void
18055ex_return(eap)
18056 exarg_T *eap;
18057{
18058 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000018059 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018060 int returning = FALSE;
18061
18062 if (current_funccal == NULL)
18063 {
18064 EMSG(_("E133: :return not inside a function"));
18065 return;
18066 }
18067
18068 if (eap->skip)
18069 ++emsg_skip;
18070
18071 eap->nextcmd = NULL;
18072 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018073 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018074 {
18075 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018076 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018078 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 }
18080 /* It's safer to return also on error. */
18081 else if (!eap->skip)
18082 {
18083 /*
18084 * Return unless the expression evaluation has been cancelled due to an
18085 * aborting error, an interrupt, or an exception.
18086 */
18087 if (!aborting())
18088 returning = do_return(eap, FALSE, TRUE, NULL);
18089 }
18090
18091 /* When skipping or the return gets pending, advance to the next command
18092 * in this line (!returning). Otherwise, ignore the rest of the line.
18093 * Following lines will be ignored by get_func_line(). */
18094 if (returning)
18095 eap->nextcmd = NULL;
18096 else if (eap->nextcmd == NULL) /* no argument */
18097 eap->nextcmd = check_nextcmd(arg);
18098
18099 if (eap->skip)
18100 --emsg_skip;
18101}
18102
18103/*
18104 * Return from a function. Possibly makes the return pending. Also called
18105 * for a pending return at the ":endtry" or after returning from an extra
18106 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000018107 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018108 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018109 * FALSE when the return gets pending.
18110 */
18111 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018112do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 exarg_T *eap;
18114 int reanimate;
18115 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018116 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117{
18118 int idx;
18119 struct condstack *cstack = eap->cstack;
18120
18121 if (reanimate)
18122 /* Undo the return. */
18123 current_funccal->returned = FALSE;
18124
18125 /*
18126 * Cleanup (and inactivate) conditionals, but stop when a try conditional
18127 * not in its finally clause (which then is to be executed next) is found.
18128 * In this case, make the ":return" pending for execution at the ":endtry".
18129 * Otherwise, return normally.
18130 */
18131 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
18132 if (idx >= 0)
18133 {
18134 cstack->cs_pending[idx] = CSTP_RETURN;
18135
18136 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018137 /* A pending return again gets pending. "rettv" points to an
18138 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018140 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018141 else
18142 {
18143 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018144 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018145 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018146 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018148 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149 {
18150 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018151 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018152 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153 else
18154 EMSG(_(e_outofmem));
18155 }
18156 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018157 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158
18159 if (reanimate)
18160 {
18161 /* The pending return value could be overwritten by a ":return"
18162 * without argument in a finally clause; reset the default
18163 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018164 current_funccal->rettv->v_type = VAR_NUMBER;
18165 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166 }
18167 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018168 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169 }
18170 else
18171 {
18172 current_funccal->returned = TRUE;
18173
18174 /* If the return is carried out now, store the return value. For
18175 * a return immediately after reanimation, the value is already
18176 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018177 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018178 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018179 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000018180 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018182 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018183 }
18184 }
18185
18186 return idx < 0;
18187}
18188
18189/*
18190 * Free the variable with a pending return value.
18191 */
18192 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018193discard_pending_return(rettv)
18194 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195{
Bram Moolenaar33570922005-01-25 22:26:29 +000018196 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018197}
18198
18199/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018200 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000018201 * is an allocated string. Used by report_pending() for verbose messages.
18202 */
18203 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018204get_return_cmd(rettv)
18205 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018206{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018207 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018208 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018209 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018210
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018211 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000018212 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018213 if (s == NULL)
18214 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018215
18216 STRCPY(IObuff, ":return ");
18217 STRNCPY(IObuff + 8, s, IOSIZE - 8);
18218 if (STRLEN(s) + 8 >= IOSIZE)
18219 STRCPY(IObuff + IOSIZE - 4, "...");
18220 vim_free(tofree);
18221 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018222}
18223
18224/*
18225 * Get next function line.
18226 * Called by do_cmdline() to get the next line.
18227 * Returns allocated string, or NULL for end of function.
18228 */
18229/* ARGSUSED */
18230 char_u *
18231get_func_line(c, cookie, indent)
18232 int c; /* not used */
18233 void *cookie;
18234 int indent; /* not used */
18235{
Bram Moolenaar33570922005-01-25 22:26:29 +000018236 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018237 ufunc_T *fp = fcp->func;
18238 char_u *retval;
18239 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240
18241 /* If breakpoints have been added/deleted need to check for it. */
18242 if (fcp->dbg_tick != debug_tick)
18243 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018244 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018245 sourcing_lnum);
18246 fcp->dbg_tick = debug_tick;
18247 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000018248#ifdef FEAT_PROFILE
18249 if (do_profiling)
18250 func_line_end(cookie);
18251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252
Bram Moolenaar05159a02005-02-26 23:04:13 +000018253 gap = &fp->uf_lines;
18254 if ((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255 retval = NULL;
18256 else if (fcp->returned || fcp->linenr >= gap->ga_len)
18257 retval = NULL;
18258 else
18259 {
18260 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
18261 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000018262#ifdef FEAT_PROFILE
18263 if (do_profiling)
18264 func_line_start(cookie);
18265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266 }
18267
18268 /* Did we encounter a breakpoint? */
18269 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
18270 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000018271 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018272 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000018273 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274 sourcing_lnum);
18275 fcp->dbg_tick = debug_tick;
18276 }
18277
18278 return retval;
18279}
18280
Bram Moolenaar05159a02005-02-26 23:04:13 +000018281#if defined(FEAT_PROFILE) || defined(PROTO)
18282/*
18283 * Called when starting to read a function line.
18284 * "sourcing_lnum" must be correct!
18285 * When skipping lines it may not actually be executed, but we won't find out
18286 * until later and we need to store the time now.
18287 */
18288 void
18289func_line_start(cookie)
18290 void *cookie;
18291{
18292 funccall_T *fcp = (funccall_T *)cookie;
18293 ufunc_T *fp = fcp->func;
18294
18295 if (fp->uf_profiling && sourcing_lnum >= 1
18296 && sourcing_lnum <= fp->uf_lines.ga_len)
18297 {
18298 fp->uf_tml_idx = sourcing_lnum - 1;
18299 fp->uf_tml_execed = FALSE;
18300 profile_start(&fp->uf_tml_start);
18301 profile_zero(&fp->uf_tml_children);
18302 profile_get_wait(&fp->uf_tml_wait);
18303 }
18304}
18305
18306/*
18307 * Called when actually executing a function line.
18308 */
18309 void
18310func_line_exec(cookie)
18311 void *cookie;
18312{
18313 funccall_T *fcp = (funccall_T *)cookie;
18314 ufunc_T *fp = fcp->func;
18315
18316 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18317 fp->uf_tml_execed = TRUE;
18318}
18319
18320/*
18321 * Called when done with a function line.
18322 */
18323 void
18324func_line_end(cookie)
18325 void *cookie;
18326{
18327 funccall_T *fcp = (funccall_T *)cookie;
18328 ufunc_T *fp = fcp->func;
18329
18330 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
18331 {
18332 if (fp->uf_tml_execed)
18333 {
18334 ++fp->uf_tml_count[fp->uf_tml_idx];
18335 profile_end(&fp->uf_tml_start);
18336 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
18337 profile_add(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start);
18338 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
18339 profile_sub(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_children);
18340 }
18341 fp->uf_tml_idx = -1;
18342 }
18343}
18344#endif
18345
Bram Moolenaar071d4272004-06-13 20:20:40 +000018346/*
18347 * Return TRUE if the currently active function should be ended, because a
18348 * return was encountered or an error occured. Used inside a ":while".
18349 */
18350 int
18351func_has_ended(cookie)
18352 void *cookie;
18353{
Bram Moolenaar33570922005-01-25 22:26:29 +000018354 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018355
18356 /* Ignore the "abort" flag if the abortion behavior has been changed due to
18357 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018358 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359 || fcp->returned);
18360}
18361
18362/*
18363 * return TRUE if cookie indicates a function which "abort"s on errors.
18364 */
18365 int
18366func_has_abort(cookie)
18367 void *cookie;
18368{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000018369 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018370}
18371
18372#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
18373typedef enum
18374{
18375 VAR_FLAVOUR_DEFAULT,
18376 VAR_FLAVOUR_SESSION,
18377 VAR_FLAVOUR_VIMINFO
18378} var_flavour_T;
18379
18380static var_flavour_T var_flavour __ARGS((char_u *varname));
18381
18382 static var_flavour_T
18383var_flavour(varname)
18384 char_u *varname;
18385{
18386 char_u *p = varname;
18387
18388 if (ASCII_ISUPPER(*p))
18389 {
18390 while (*(++p))
18391 if (ASCII_ISLOWER(*p))
18392 return VAR_FLAVOUR_SESSION;
18393 return VAR_FLAVOUR_VIMINFO;
18394 }
18395 else
18396 return VAR_FLAVOUR_DEFAULT;
18397}
18398#endif
18399
18400#if defined(FEAT_VIMINFO) || defined(PROTO)
18401/*
18402 * Restore global vars that start with a capital from the viminfo file
18403 */
18404 int
18405read_viminfo_varlist(virp, writing)
18406 vir_T *virp;
18407 int writing;
18408{
18409 char_u *tab;
18410 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000018411 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018412
18413 if (!writing && (find_viminfo_parameter('!') != NULL))
18414 {
18415 tab = vim_strchr(virp->vir_line + 1, '\t');
18416 if (tab != NULL)
18417 {
18418 *tab++ = '\0'; /* isolate the variable name */
18419 if (*tab == 'S') /* string var */
18420 is_string = TRUE;
18421
18422 tab = vim_strchr(tab, '\t');
18423 if (tab != NULL)
18424 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000018425 if (is_string)
18426 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018427 tv.v_type = VAR_STRING;
18428 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018429 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018430 }
18431 else
18432 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018433 tv.v_type = VAR_NUMBER;
18434 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018436 set_var(virp->vir_line + 1, &tv, FALSE);
18437 if (is_string)
18438 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018439 }
18440 }
18441 }
18442
18443 return viminfo_readline(virp);
18444}
18445
18446/*
18447 * Write global vars that start with a capital to the viminfo file
18448 */
18449 void
18450write_viminfo_varlist(fp)
18451 FILE *fp;
18452{
Bram Moolenaar33570922005-01-25 22:26:29 +000018453 hashitem_T *hi;
18454 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018455 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018456 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018457 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018458 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000018459 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018460
18461 if (find_viminfo_parameter('!') == NULL)
18462 return;
18463
18464 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000018465
Bram Moolenaar33570922005-01-25 22:26:29 +000018466 todo = globvarht.ht_used;
18467 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018468 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018469 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018471 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018472 this_var = HI2DI(hi);
18473 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018474 {
Bram Moolenaar33570922005-01-25 22:26:29 +000018475 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000018476 {
18477 case VAR_STRING: s = "STR"; break;
18478 case VAR_NUMBER: s = "NUM"; break;
18479 default: continue;
18480 }
Bram Moolenaar33570922005-01-25 22:26:29 +000018481 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000018482 p = echo_string(&this_var->di_tv, &tofree, numbuf);
18483 if (p != NULL)
18484 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000018485 vim_free(tofree);
18486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018487 }
18488 }
18489}
18490#endif
18491
18492#if defined(FEAT_SESSION) || defined(PROTO)
18493 int
18494store_session_globals(fd)
18495 FILE *fd;
18496{
Bram Moolenaar33570922005-01-25 22:26:29 +000018497 hashitem_T *hi;
18498 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000018499 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018500 char_u *p, *t;
18501
Bram Moolenaar33570922005-01-25 22:26:29 +000018502 todo = globvarht.ht_used;
18503 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018504 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018505 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018506 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018507 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000018508 this_var = HI2DI(hi);
18509 if ((this_var->di_tv.v_type == VAR_NUMBER
18510 || this_var->di_tv.v_type == VAR_STRING)
18511 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000018512 {
Bram Moolenaara7043832005-01-21 11:56:39 +000018513 /* Escape special characters with a backslash. Turn a LF and
18514 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000018515 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000018516 (char_u *)"\\\"\n\r");
18517 if (p == NULL) /* out of memory */
18518 break;
18519 for (t = p; *t != NUL; ++t)
18520 if (*t == '\n')
18521 *t = 'n';
18522 else if (*t == '\r')
18523 *t = 'r';
18524 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000018525 this_var->di_key,
18526 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18527 : ' ',
18528 p,
18529 (this_var->di_tv.v_type == VAR_STRING) ? '"'
18530 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000018531 || put_eol(fd) == FAIL)
18532 {
18533 vim_free(p);
18534 return FAIL;
18535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018536 vim_free(p);
18537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018538 }
18539 }
18540 return OK;
18541}
18542#endif
18543
18544#endif /* FEAT_EVAL */
18545
18546#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
18547
18548
18549#ifdef WIN3264
18550/*
18551 * Functions for ":8" filename modifier: get 8.3 version of a filename.
18552 */
18553static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18554static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
18555static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
18556
18557/*
18558 * Get the short pathname of a file.
18559 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
18560 */
18561 static int
18562get_short_pathname(fnamep, bufp, fnamelen)
18563 char_u **fnamep;
18564 char_u **bufp;
18565 int *fnamelen;
18566{
18567 int l,len;
18568 char_u *newbuf;
18569
18570 len = *fnamelen;
18571
18572 l = GetShortPathName(*fnamep, *fnamep, len);
18573 if (l > len - 1)
18574 {
18575 /* If that doesn't work (not enough space), then save the string
18576 * and try again with a new buffer big enough
18577 */
18578 newbuf = vim_strnsave(*fnamep, l);
18579 if (newbuf == NULL)
18580 return 0;
18581
18582 vim_free(*bufp);
18583 *fnamep = *bufp = newbuf;
18584
18585 l = GetShortPathName(*fnamep,*fnamep,l+1);
18586
18587 /* Really should always succeed, as the buffer is big enough */
18588 }
18589
18590 *fnamelen = l;
18591 return 1;
18592}
18593
18594/*
18595 * Create a short path name. Returns the length of the buffer it needs.
18596 * Doesn't copy over the end of the buffer passed in.
18597 */
18598 static int
18599shortpath_for_invalid_fname(fname, bufp, fnamelen)
18600 char_u **fname;
18601 char_u **bufp;
18602 int *fnamelen;
18603{
18604 char_u *s, *p, *pbuf2, *pbuf3;
18605 char_u ch;
Bram Moolenaar75c50c42005-06-04 22:06:24 +000018606 int len, len2, plen, slen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018607
18608 /* Make a copy */
18609 len2 = *fnamelen;
18610 pbuf2 = vim_strnsave(*fname, len2);
18611 pbuf3 = NULL;
18612
18613 s = pbuf2 + len2 - 1; /* Find the end */
18614 slen = 1;
18615 plen = len2;
18616
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018617 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018618 {
18619 --s;
18620 ++slen;
18621 --plen;
18622 }
18623
18624 do
18625 {
18626 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018627 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628 {
18629 --s;
18630 ++slen;
18631 --plen;
18632 }
18633 if (s <= pbuf2)
18634 break;
18635
18636 /* Remeber the character that is about to be blatted */
18637 ch = *s;
18638 *s = 0; /* get_short_pathname requires a null-terminated string */
18639
18640 /* Try it in situ */
18641 p = pbuf2;
18642 if (!get_short_pathname(&p, &pbuf3, &plen))
18643 {
18644 vim_free(pbuf2);
18645 return -1;
18646 }
18647 *s = ch; /* Preserve the string */
18648 } while (plen == 0);
18649
18650 if (plen > 0)
18651 {
18652 /* Remeber the length of the new string. */
18653 *fnamelen = len = plen + slen;
18654 vim_free(*bufp);
18655 if (len > len2)
18656 {
18657 /* If there's not enough space in the currently allocated string,
18658 * then copy it to a buffer big enough.
18659 */
18660 *fname= *bufp = vim_strnsave(p, len);
18661 if (*fname == NULL)
18662 return -1;
18663 }
18664 else
18665 {
18666 /* Transfer pbuf2 to being the main buffer (it's big enough) */
18667 *fname = *bufp = pbuf2;
18668 if (p != pbuf2)
18669 strncpy(*fname, p, plen);
18670 pbuf2 = NULL;
18671 }
18672 /* Concat the next bit */
18673 strncpy(*fname + plen, s, slen);
18674 (*fname)[len] = '\0';
18675 }
18676 vim_free(pbuf3);
18677 vim_free(pbuf2);
18678 return 0;
18679}
18680
18681/*
18682 * Get a pathname for a partial path.
18683 */
18684 static int
18685shortpath_for_partial(fnamep, bufp, fnamelen)
18686 char_u **fnamep;
18687 char_u **bufp;
18688 int *fnamelen;
18689{
18690 int sepcount, len, tflen;
18691 char_u *p;
18692 char_u *pbuf, *tfname;
18693 int hasTilde;
18694
18695 /* Count up the path seperators from the RHS.. so we know which part
18696 * of the path to return.
18697 */
18698 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018699 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018700 if (vim_ispathsep(*p))
18701 ++sepcount;
18702
18703 /* Need full path first (use expand_env() to remove a "~/") */
18704 hasTilde = (**fnamep == '~');
18705 if (hasTilde)
18706 pbuf = tfname = expand_env_save(*fnamep);
18707 else
18708 pbuf = tfname = FullName_save(*fnamep, FALSE);
18709
18710 len = tflen = STRLEN(tfname);
18711
18712 if (!get_short_pathname(&tfname, &pbuf, &len))
18713 return -1;
18714
18715 if (len == 0)
18716 {
18717 /* Don't have a valid filename, so shorten the rest of the
18718 * path if we can. This CAN give us invalid 8.3 filenames, but
18719 * there's not a lot of point in guessing what it might be.
18720 */
18721 len = tflen;
18722 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
18723 return -1;
18724 }
18725
18726 /* Count the paths backward to find the beginning of the desired string. */
18727 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018728 {
18729#ifdef FEAT_MBYTE
18730 if (has_mbyte)
18731 p -= mb_head_off(tfname, p);
18732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733 if (vim_ispathsep(*p))
18734 {
18735 if (sepcount == 0 || (hasTilde && sepcount == 1))
18736 break;
18737 else
18738 sepcount --;
18739 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741 if (hasTilde)
18742 {
18743 --p;
18744 if (p >= tfname)
18745 *p = '~';
18746 else
18747 return -1;
18748 }
18749 else
18750 ++p;
18751
18752 /* Copy in the string - p indexes into tfname - allocated at pbuf */
18753 vim_free(*bufp);
18754 *fnamelen = (int)STRLEN(p);
18755 *bufp = pbuf;
18756 *fnamep = p;
18757
18758 return 0;
18759}
18760#endif /* WIN3264 */
18761
18762/*
18763 * Adjust a filename, according to a string of modifiers.
18764 * *fnamep must be NUL terminated when called. When returning, the length is
18765 * determined by *fnamelen.
18766 * Returns valid flags.
18767 * When there is an error, *fnamep is set to NULL.
18768 */
18769 int
18770modify_fname(src, usedlen, fnamep, bufp, fnamelen)
18771 char_u *src; /* string with modifiers */
18772 int *usedlen; /* characters after src that are used */
18773 char_u **fnamep; /* file name so far */
18774 char_u **bufp; /* buffer for allocated file name or NULL */
18775 int *fnamelen; /* length of fnamep */
18776{
18777 int valid = 0;
18778 char_u *tail;
18779 char_u *s, *p, *pbuf;
18780 char_u dirname[MAXPATHL];
18781 int c;
18782 int has_fullname = 0;
18783#ifdef WIN3264
18784 int has_shortname = 0;
18785#endif
18786
18787repeat:
18788 /* ":p" - full path/file_name */
18789 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
18790 {
18791 has_fullname = 1;
18792
18793 valid |= VALID_PATH;
18794 *usedlen += 2;
18795
18796 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
18797 if ((*fnamep)[0] == '~'
18798#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
18799 && ((*fnamep)[1] == '/'
18800# ifdef BACKSLASH_IN_FILENAME
18801 || (*fnamep)[1] == '\\'
18802# endif
18803 || (*fnamep)[1] == NUL)
18804
18805#endif
18806 )
18807 {
18808 *fnamep = expand_env_save(*fnamep);
18809 vim_free(*bufp); /* free any allocated file name */
18810 *bufp = *fnamep;
18811 if (*fnamep == NULL)
18812 return -1;
18813 }
18814
18815 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018816 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018817 {
18818 if (vim_ispathsep(*p)
18819 && p[1] == '.'
18820 && (p[2] == NUL
18821 || vim_ispathsep(p[2])
18822 || (p[2] == '.'
18823 && (p[3] == NUL || vim_ispathsep(p[3])))))
18824 break;
18825 }
18826
18827 /* FullName_save() is slow, don't use it when not needed. */
18828 if (*p != NUL || !vim_isAbsName(*fnamep))
18829 {
18830 *fnamep = FullName_save(*fnamep, *p != NUL);
18831 vim_free(*bufp); /* free any allocated file name */
18832 *bufp = *fnamep;
18833 if (*fnamep == NULL)
18834 return -1;
18835 }
18836
18837 /* Append a path separator to a directory. */
18838 if (mch_isdir(*fnamep))
18839 {
18840 /* Make room for one or two extra characters. */
18841 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
18842 vim_free(*bufp); /* free any allocated file name */
18843 *bufp = *fnamep;
18844 if (*fnamep == NULL)
18845 return -1;
18846 add_pathsep(*fnamep);
18847 }
18848 }
18849
18850 /* ":." - path relative to the current directory */
18851 /* ":~" - path relative to the home directory */
18852 /* ":8" - shortname path - postponed till after */
18853 while (src[*usedlen] == ':'
18854 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
18855 {
18856 *usedlen += 2;
18857 if (c == '8')
18858 {
18859#ifdef WIN3264
18860 has_shortname = 1; /* Postpone this. */
18861#endif
18862 continue;
18863 }
18864 pbuf = NULL;
18865 /* Need full path first (use expand_env() to remove a "~/") */
18866 if (!has_fullname)
18867 {
18868 if (c == '.' && **fnamep == '~')
18869 p = pbuf = expand_env_save(*fnamep);
18870 else
18871 p = pbuf = FullName_save(*fnamep, FALSE);
18872 }
18873 else
18874 p = *fnamep;
18875
18876 has_fullname = 0;
18877
18878 if (p != NULL)
18879 {
18880 if (c == '.')
18881 {
18882 mch_dirname(dirname, MAXPATHL);
18883 s = shorten_fname(p, dirname);
18884 if (s != NULL)
18885 {
18886 *fnamep = s;
18887 if (pbuf != NULL)
18888 {
18889 vim_free(*bufp); /* free any allocated file name */
18890 *bufp = pbuf;
18891 pbuf = NULL;
18892 }
18893 }
18894 }
18895 else
18896 {
18897 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
18898 /* Only replace it when it starts with '~' */
18899 if (*dirname == '~')
18900 {
18901 s = vim_strsave(dirname);
18902 if (s != NULL)
18903 {
18904 *fnamep = s;
18905 vim_free(*bufp);
18906 *bufp = s;
18907 }
18908 }
18909 }
18910 vim_free(pbuf);
18911 }
18912 }
18913
18914 tail = gettail(*fnamep);
18915 *fnamelen = (int)STRLEN(*fnamep);
18916
18917 /* ":h" - head, remove "/file_name", can be repeated */
18918 /* Don't remove the first "/" or "c:\" */
18919 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
18920 {
18921 valid |= VALID_HEAD;
18922 *usedlen += 2;
18923 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018924 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018925 --tail;
18926 *fnamelen = (int)(tail - *fnamep);
18927#ifdef VMS
18928 if (*fnamelen > 0)
18929 *fnamelen += 1; /* the path separator is part of the path */
18930#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000018931 while (tail > s && !after_pathsep(s, tail))
18932 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933 }
18934
18935 /* ":8" - shortname */
18936 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
18937 {
18938 *usedlen += 2;
18939#ifdef WIN3264
18940 has_shortname = 1;
18941#endif
18942 }
18943
18944#ifdef WIN3264
18945 /* Check shortname after we have done 'heads' and before we do 'tails'
18946 */
18947 if (has_shortname)
18948 {
18949 pbuf = NULL;
18950 /* Copy the string if it is shortened by :h */
18951 if (*fnamelen < (int)STRLEN(*fnamep))
18952 {
18953 p = vim_strnsave(*fnamep, *fnamelen);
18954 if (p == 0)
18955 return -1;
18956 vim_free(*bufp);
18957 *bufp = *fnamep = p;
18958 }
18959
18960 /* Split into two implementations - makes it easier. First is where
18961 * there isn't a full name already, second is where there is.
18962 */
18963 if (!has_fullname && !vim_isAbsName(*fnamep))
18964 {
18965 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
18966 return -1;
18967 }
18968 else
18969 {
18970 int l;
18971
18972 /* Simple case, already have the full-name
18973 * Nearly always shorter, so try first time. */
18974 l = *fnamelen;
18975 if (!get_short_pathname(fnamep, bufp, &l))
18976 return -1;
18977
18978 if (l == 0)
18979 {
18980 /* Couldn't find the filename.. search the paths.
18981 */
18982 l = *fnamelen;
18983 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
18984 return -1;
18985 }
18986 *fnamelen = l;
18987 }
18988 }
18989#endif /* WIN3264 */
18990
18991 /* ":t" - tail, just the basename */
18992 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
18993 {
18994 *usedlen += 2;
18995 *fnamelen -= (int)(tail - *fnamep);
18996 *fnamep = tail;
18997 }
18998
18999 /* ":e" - extension, can be repeated */
19000 /* ":r" - root, without extension, can be repeated */
19001 while (src[*usedlen] == ':'
19002 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
19003 {
19004 /* find a '.' in the tail:
19005 * - for second :e: before the current fname
19006 * - otherwise: The last '.'
19007 */
19008 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
19009 s = *fnamep - 2;
19010 else
19011 s = *fnamep + *fnamelen - 1;
19012 for ( ; s > tail; --s)
19013 if (s[0] == '.')
19014 break;
19015 if (src[*usedlen + 1] == 'e') /* :e */
19016 {
19017 if (s > tail)
19018 {
19019 *fnamelen += (int)(*fnamep - (s + 1));
19020 *fnamep = s + 1;
19021#ifdef VMS
19022 /* cut version from the extension */
19023 s = *fnamep + *fnamelen - 1;
19024 for ( ; s > *fnamep; --s)
19025 if (s[0] == ';')
19026 break;
19027 if (s > *fnamep)
19028 *fnamelen = s - *fnamep;
19029#endif
19030 }
19031 else if (*fnamep <= tail)
19032 *fnamelen = 0;
19033 }
19034 else /* :r */
19035 {
19036 if (s > tail) /* remove one extension */
19037 *fnamelen = (int)(s - *fnamep);
19038 }
19039 *usedlen += 2;
19040 }
19041
19042 /* ":s?pat?foo?" - substitute */
19043 /* ":gs?pat?foo?" - global substitute */
19044 if (src[*usedlen] == ':'
19045 && (src[*usedlen + 1] == 's'
19046 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
19047 {
19048 char_u *str;
19049 char_u *pat;
19050 char_u *sub;
19051 int sep;
19052 char_u *flags;
19053 int didit = FALSE;
19054
19055 flags = (char_u *)"";
19056 s = src + *usedlen + 2;
19057 if (src[*usedlen + 1] == 'g')
19058 {
19059 flags = (char_u *)"g";
19060 ++s;
19061 }
19062
19063 sep = *s++;
19064 if (sep)
19065 {
19066 /* find end of pattern */
19067 p = vim_strchr(s, sep);
19068 if (p != NULL)
19069 {
19070 pat = vim_strnsave(s, (int)(p - s));
19071 if (pat != NULL)
19072 {
19073 s = p + 1;
19074 /* find end of substitution */
19075 p = vim_strchr(s, sep);
19076 if (p != NULL)
19077 {
19078 sub = vim_strnsave(s, (int)(p - s));
19079 str = vim_strnsave(*fnamep, *fnamelen);
19080 if (sub != NULL && str != NULL)
19081 {
19082 *usedlen = (int)(p + 1 - src);
19083 s = do_string_sub(str, pat, sub, flags);
19084 if (s != NULL)
19085 {
19086 *fnamep = s;
19087 *fnamelen = (int)STRLEN(s);
19088 vim_free(*bufp);
19089 *bufp = s;
19090 didit = TRUE;
19091 }
19092 }
19093 vim_free(sub);
19094 vim_free(str);
19095 }
19096 vim_free(pat);
19097 }
19098 }
19099 /* after using ":s", repeat all the modifiers */
19100 if (didit)
19101 goto repeat;
19102 }
19103 }
19104
19105 return valid;
19106}
19107
19108/*
19109 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
19110 * "flags" can be "g" to do a global substitute.
19111 * Returns an allocated string, NULL for error.
19112 */
19113 char_u *
19114do_string_sub(str, pat, sub, flags)
19115 char_u *str;
19116 char_u *pat;
19117 char_u *sub;
19118 char_u *flags;
19119{
19120 int sublen;
19121 regmatch_T regmatch;
19122 int i;
19123 int do_all;
19124 char_u *tail;
19125 garray_T ga;
19126 char_u *ret;
19127 char_u *save_cpo;
19128
19129 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
19130 save_cpo = p_cpo;
19131 p_cpo = (char_u *)"";
19132
19133 ga_init2(&ga, 1, 200);
19134
19135 do_all = (flags[0] == 'g');
19136
19137 regmatch.rm_ic = p_ic;
19138 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19139 if (regmatch.regprog != NULL)
19140 {
19141 tail = str;
19142 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
19143 {
19144 /*
19145 * Get some space for a temporary buffer to do the substitution
19146 * into. It will contain:
19147 * - The text up to where the match is.
19148 * - The substituted text.
19149 * - The text after the match.
19150 */
19151 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
19152 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
19153 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
19154 {
19155 ga_clear(&ga);
19156 break;
19157 }
19158
19159 /* copy the text up to where the match is */
19160 i = (int)(regmatch.startp[0] - tail);
19161 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
19162 /* add the substituted text */
19163 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
19164 + ga.ga_len + i, TRUE, TRUE, FALSE);
19165 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019166 /* avoid getting stuck on a match with an empty string */
19167 if (tail == regmatch.endp[0])
19168 {
19169 if (*tail == NUL)
19170 break;
19171 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
19172 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019173 }
19174 else
19175 {
19176 tail = regmatch.endp[0];
19177 if (*tail == NUL)
19178 break;
19179 }
19180 if (!do_all)
19181 break;
19182 }
19183
19184 if (ga.ga_data != NULL)
19185 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
19186
19187 vim_free(regmatch.regprog);
19188 }
19189
19190 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
19191 ga_clear(&ga);
19192 p_cpo = save_cpo;
19193
19194 return ret;
19195}
19196
19197#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */