blob: c6ec0da0e13c6e3a81def4186d101b24ba378ec0 [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 Moolenaare9a41262005-01-15 22:18:47 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionaary");
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 Moolenaar49cd9572005-01-03 21:06:01 +0000109
110/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000111 * All user-defined global variables are stored in dictionary "globvardict".
112 * "globvars_var" is the variable that is used for "g:".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000114static dict_T globvardict;
115static dictitem_T globvars_var;
116#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117
118/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000119 * Old Vim variables such as "v:version" are also available without the "v:".
120 * Also in functions. We need a special hashtable for them.
121 */
122hashtab_T compat_hashtab;
123
124/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000125 * Array to hold the hashtab with variables local to each sourced script.
126 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000128typedef struct
129{
130 dictitem_T sv_var;
131 dict_T sv_dict;
132} scriptvar_T;
133
134static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T), 4, NULL};
135#define SCRIPT_SV(id) (((scriptvar_T *)ga_scripts.ga_data)[(id) - 1])
136#define SCRIPT_VARS(id) (SCRIPT_SV(id).sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137
138static int echo_attr = 0; /* attributes used for ":echo" */
139
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000140/* Values for trans_function_name() argument: */
141#define TFN_INT 1 /* internal function name OK */
142#define TFN_QUIET 2 /* no error messages */
143
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144/*
145 * Structure to hold info for a user function.
146 */
147typedef struct ufunc ufunc_T;
148
149struct ufunc
150{
151 ufunc_T *next; /* next function in list */
152 char_u *name; /* name of function; can start with <SNR>123_
153 (<SNR> is K_SPECIAL KS_EXTRA KE_SNR) */
154 int varargs; /* variable nr of arguments */
155 int flags;
156 int calls; /* nr of active calls */
157 garray_T args; /* arguments */
158 garray_T lines; /* function lines */
159 scid_T script_ID; /* ID of script where function was defined,
160 used for s: variables */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161 int refcount; /* for numbered function: reference count */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162};
163
164/* function flags */
165#define FC_ABORT 1 /* abort function on error */
166#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000167#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168
169/*
170 * All user-defined functions are found in the forward-linked function list.
171 * The first function is pointed at by firstfunc.
172 */
173ufunc_T *firstfunc = NULL;
174
175#define FUNCARG(fp, j) ((char_u **)(fp->args.ga_data))[j]
176#define FUNCLINE(fp, j) ((char_u **)(fp->lines.ga_data))[j]
177
Bram Moolenaar33570922005-01-25 22:26:29 +0000178#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
179#define VAR_SHORT_LEN 20 /* short variable name length */
180#define FIXVAR_CNT 12 /* number of fixed variables */
181
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar33570922005-01-25 22:26:29 +0000183typedef struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184{
185 ufunc_T *func; /* function being called */
186 int linenr; /* next line to be executed */
187 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000188 struct /* fixed variables for arguments */
189 {
190 dictitem_T var; /* variable (without room for name) */
191 char_u room[VAR_SHORT_LEN]; /* room for the name */
192 } fixvar[FIXVAR_CNT];
193 dict_T l_vars; /* l: local function variables */
194 dictitem_T l_vars_var; /* variable for l: scope */
195 dict_T l_avars; /* a: argument variables */
196 dictitem_T l_avars_var; /* variable for a: scope */
197 list_T l_varlist; /* list for a:000 */
198 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
199 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 linenr_T breakpoint; /* next line with breakpoint or zero */
201 int dbg_tick; /* debug_tick when breakpoint was set */
202 int level; /* top nesting level of executed function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000203} funccall_T;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204
205/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000206 * Info used by a ":for" loop.
207 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000208typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000209{
210 int fi_semicolon; /* TRUE if ending in '; var]' */
211 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000212 listwatch_T fi_lw; /* keep an eye on the item used. */
213 list_T *fi_list; /* list being used */
214} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000215
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000216/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000217 * Struct used by trans_function_name()
218 */
219typedef struct
220{
Bram Moolenaar33570922005-01-25 22:26:29 +0000221 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000222 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000223 dictitem_T *fd_di; /* Dictionary item used */
224} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000225
Bram Moolenaara7043832005-01-21 11:56:39 +0000226
227/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000228 * Array to hold the value of v: variables.
229 * The value is in a dictitem, so that it can also be used in the v: scope.
230 * The reason to use this table anyway is for very quick access to the
231 * variables with the VV_ defines.
232 */
233#include "version.h"
234
235/* values for vv_flags: */
236#define VV_COMPAT 1 /* compatible, also used without "v:" */
237#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000238#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239
240#define VV_NAME(s, t) s, sizeof(s) - 1, {{t}}, {0}
241
242static struct vimvar
243{
244 char *vv_name; /* name of variable, without v: */
245 int vv_len; /* length of name */
246 dictitem_T vv_di; /* value and name for key */
247 char vv_filler[16]; /* space for LONGEST name below!!! */
248 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
249} vimvars[VV_LEN] =
250{
251 /*
252 * The order here must match the VV_ defines in vim.h!
253 * Initializing a union does not work, leave tv.vval empty to get zero's.
254 */
255 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
256 {VV_NAME("count1", VAR_NUMBER), VV_RO},
257 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
258 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
259 {VV_NAME("warningmsg", VAR_STRING), 0},
260 {VV_NAME("statusmsg", VAR_STRING), 0},
261 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
262 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
263 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
264 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
265 {VV_NAME("termresponse", VAR_STRING), VV_RO},
266 {VV_NAME("fname", VAR_STRING), VV_RO},
267 {VV_NAME("lang", VAR_STRING), VV_RO},
268 {VV_NAME("lc_time", VAR_STRING), VV_RO},
269 {VV_NAME("ctype", VAR_STRING), VV_RO},
270 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
271 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
272 {VV_NAME("fname_in", VAR_STRING), VV_RO},
273 {VV_NAME("fname_out", VAR_STRING), VV_RO},
274 {VV_NAME("fname_new", VAR_STRING), VV_RO},
275 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
276 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
277 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
278 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
279 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
280 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
281 {VV_NAME("progname", VAR_STRING), VV_RO},
282 {VV_NAME("servername", VAR_STRING), VV_RO},
283 {VV_NAME("dying", VAR_NUMBER), VV_RO},
284 {VV_NAME("exception", VAR_STRING), VV_RO},
285 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
286 {VV_NAME("register", VAR_STRING), VV_RO},
287 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
288 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000289 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
290 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000291};
292
293/* shorthand */
294#define vv_type vv_di.di_tv.v_type
295#define vv_nr vv_di.di_tv.vval.v_number
296#define vv_str vv_di.di_tv.vval.v_string
297#define vv_tv vv_di.di_tv
298
299/*
300 * The v: variables are stored in dictionary "vimvardict".
301 * "vimvars_var" is the variable that is used for the "l:" scope.
302 */
303static dict_T vimvardict;
304static dictitem_T vimvars_var;
305#define vimvarht vimvardict.dv_hashtab
306
307static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
308static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
309static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
310static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
311static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
312static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
313static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
314static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000315static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000316static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
317static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
318static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
319static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
320static list_T *list_alloc __ARGS((void));
321static void list_unref __ARGS((list_T *l));
322static void list_free __ARGS((list_T *l));
323static listitem_T *listitem_alloc __ARGS((void));
324static void listitem_free __ARGS((listitem_T *item));
325static void listitem_remove __ARGS((list_T *l, listitem_T *item));
326static long list_len __ARGS((list_T *l));
327static int list_equal __ARGS((list_T *l1, list_T *l2, int ic));
328static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic));
329static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic));
330static int string_isa_number __ARGS((char_u *s));
331static listitem_T *list_find __ARGS((list_T *l, long n));
332static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar33570922005-01-25 22:26:29 +0000333static void list_append __ARGS((list_T *l, listitem_T *item));
334static int list_append_tv __ARGS((list_T *l, typval_T *tv));
335static int list_insert_tv __ARGS((list_T *l, typval_T *tv, listitem_T *item));
336static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
337static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000338static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000339static void list_remove __ARGS((list_T *l, listitem_T *item, listitem_T *item2));
340static char_u *list2string __ARGS((typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000341static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo));
Bram Moolenaar33570922005-01-25 22:26:29 +0000342
343static dict_T *dict_alloc __ARGS((void));
344static void dict_unref __ARGS((dict_T *d));
345static void dict_free __ARGS((dict_T *d));
346static dictitem_T *dictitem_alloc __ARGS((char_u *key));
347static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
348static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
349static void dictitem_free __ARGS((dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000350static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000351static int dict_add __ARGS((dict_T *d, dictitem_T *item));
352static long dict_len __ARGS((dict_T *d));
353static dictitem_T *dict_find __ARGS((dict_T *d, char_u *key, int len));
354static char_u *dict2string __ARGS((typval_T *tv));
355static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
356
357static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
358static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf));
359static char_u *string_quote __ARGS((char_u *str, int function));
360static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
361static int find_internal_func __ARGS((char_u *name));
362static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
363static 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));
364static 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 +0000365static void emsg_funcname __ARGS((char *msg, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000366
367static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
368static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
369static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
370static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
371static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
372static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
373static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
374static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
375static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
376static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
377static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
378static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
379static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
380static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
381static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
382static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
383static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
384static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
385static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
386static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
387static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
388static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
389static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
390static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
391static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
392static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
393static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
394static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
395static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
396static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
397static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
398static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
399static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
400static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
401static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
402static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
403static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
404static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
405static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
406static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
407static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
408static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
409static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
410static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
411static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
412static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
413static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
414static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
415static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
416static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
417static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
418static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
419static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
420static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
421static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
422static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
423static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
424static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
425static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
426static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
427static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
428static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
429static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
430static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
431static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
432static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
433static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
434static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
435static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
436static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
437static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
438static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
439static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
440static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
441static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
442static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
443static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
444static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
445static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
446static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
447static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
448static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
449static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
450static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
451static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
452static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
453static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
454static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
455static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
456static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000457static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000458static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
459static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
463static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
464static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
465static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
466static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
467static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
468static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000474static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000475static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
479static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
480static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
481static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
482static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000483static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000484static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
485static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
487static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
488static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
489static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
491static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
494static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
496static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
497static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
498static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
506#ifdef HAVE_STRFTIME
507static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
508#endif
509static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
514static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
515static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
517static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
526static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
528static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
529static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000536static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000537
538static win_T *find_win_by_nr __ARGS((typval_T *vp));
539static pos_T *var2fpos __ARGS((typval_T *varp, int lnum));
540static int get_env_len __ARGS((char_u **arg));
541static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000542static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000543static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int incl_br));
544static int eval_isnamec __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000545static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
546static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000547static typval_T *alloc_tv __ARGS((void));
548static typval_T *alloc_string_tv __ARGS((char_u *string));
549static void free_tv __ARGS((typval_T *varp));
550static void clear_tv __ARGS((typval_T *varp));
551static void init_tv __ARGS((typval_T *varp));
552static long get_tv_number __ARGS((typval_T *varp));
553static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
554static char_u *get_tv_string __ARGS((typval_T *varp));
555static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
556static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
557static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, char_u *varname));
558static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
559static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
560static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
561static void list_one_var __ARGS((dictitem_T *v, char_u *prefix));
562static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
563static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
564static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000565static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000566static void copy_tv __ARGS((typval_T *from, typval_T *to));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000567static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000568static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
569static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
570static int eval_fname_script __ARGS((char_u *p));
571static int eval_fname_sid __ARGS((char_u *p));
572static void list_func_head __ARGS((ufunc_T *fp, int indent));
573static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
574static ufunc_T *find_func __ARGS((char_u *name));
575static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000576static int builtin_function __ARGS((char_u *name));
577static int func_autoload __ARGS((char_u *name));
Bram Moolenaar33570922005-01-25 22:26:29 +0000578static void func_free __ARGS((ufunc_T *fp));
579static void func_unref __ARGS((char_u *name));
580static void func_ref __ARGS((char_u *name));
581static 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));
582static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
583
584static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
585
586static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
587static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
588static char_u *skip_var_one __ARGS((char_u *arg));
589static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty));
590static void list_glob_vars __ARGS((void));
591static void list_buf_vars __ARGS((void));
592static void list_win_vars __ARGS((void));
593static void list_vim_vars __ARGS((void));
594static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
595static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
596static int check_changedtick __ARGS((char_u *arg));
597static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet));
598static void clear_lval __ARGS((lval_T *lp));
599static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
600static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
601static void list_add_watch __ARGS((list_T *l, listwatch_T *lw));
602static void list_rem_watch __ARGS((list_T *l, listwatch_T *lwrem));
603static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000604static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
Bram Moolenaar33570922005-01-25 22:26:29 +0000605static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000606static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
607static void item_lock __ARGS((typval_T *tv, int deep, int lock));
Bram Moolenaar33570922005-01-25 22:26:29 +0000608
609/*
610 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000611 */
612 void
613eval_init()
614{
Bram Moolenaar33570922005-01-25 22:26:29 +0000615 int i;
616 struct vimvar *p;
617
618 init_var_dict(&globvardict, &globvars_var);
619 init_var_dict(&vimvardict, &vimvars_var);
Bram Moolenaar532c7802005-01-27 14:44:31 +0000620 hash_init(&compat_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000621
622 for (i = 0; i < VV_LEN; ++i)
623 {
624 p = &vimvars[i];
625 STRCPY(p->vv_di.di_key, p->vv_name);
626 if (p->vv_flags & VV_RO)
627 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
628 else if (p->vv_flags & VV_RO_SBX)
629 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
630 else
631 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000632
633 /* add to v: scope dict, unless the value is not always available */
634 if (p->vv_type != VAR_UNKNOWN)
635 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000636 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000637 /* add to compat scope dict */
638 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000639 }
Bram Moolenaara7043832005-01-21 11:56:39 +0000640}
641
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 * Return the name of the executed function.
644 */
645 char_u *
646func_name(cookie)
647 void *cookie;
648{
Bram Moolenaar33570922005-01-25 22:26:29 +0000649 return ((funccall_T *)cookie)->func->name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650}
651
652/*
653 * Return the address holding the next breakpoint line for a funccall cookie.
654 */
655 linenr_T *
656func_breakpoint(cookie)
657 void *cookie;
658{
Bram Moolenaar33570922005-01-25 22:26:29 +0000659 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660}
661
662/*
663 * Return the address holding the debug tick for a funccall cookie.
664 */
665 int *
666func_dbg_tick(cookie)
667 void *cookie;
668{
Bram Moolenaar33570922005-01-25 22:26:29 +0000669 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670}
671
672/*
673 * Return the nesting level for a funccall cookie.
674 */
675 int
676func_level(cookie)
677 void *cookie;
678{
Bram Moolenaar33570922005-01-25 22:26:29 +0000679 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680}
681
682/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000683funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684
685/*
686 * Return TRUE when a function was ended by a ":return" command.
687 */
688 int
689current_func_returned()
690{
691 return current_funccal->returned;
692}
693
694
695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 * Set an internal variable to a string value. Creates the variable if it does
697 * not already exist.
698 */
699 void
700set_internal_string_var(name, value)
701 char_u *name;
702 char_u *value;
703{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000704 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000705 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706
707 val = vim_strsave(value);
708 if (val != NULL)
709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000710 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000711 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000713 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000714 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 }
716 }
717}
718
719# if defined(FEAT_MBYTE) || defined(PROTO)
720 int
721eval_charconvert(enc_from, enc_to, fname_from, fname_to)
722 char_u *enc_from;
723 char_u *enc_to;
724 char_u *fname_from;
725 char_u *fname_to;
726{
727 int err = FALSE;
728
729 set_vim_var_string(VV_CC_FROM, enc_from, -1);
730 set_vim_var_string(VV_CC_TO, enc_to, -1);
731 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
732 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
733 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
734 err = TRUE;
735 set_vim_var_string(VV_CC_FROM, NULL, -1);
736 set_vim_var_string(VV_CC_TO, NULL, -1);
737 set_vim_var_string(VV_FNAME_IN, NULL, -1);
738 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
739
740 if (err)
741 return FAIL;
742 return OK;
743}
744# endif
745
746# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
747 int
748eval_printexpr(fname, args)
749 char_u *fname;
750 char_u *args;
751{
752 int err = FALSE;
753
754 set_vim_var_string(VV_FNAME_IN, fname, -1);
755 set_vim_var_string(VV_CMDARG, args, -1);
756 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
757 err = TRUE;
758 set_vim_var_string(VV_FNAME_IN, NULL, -1);
759 set_vim_var_string(VV_CMDARG, NULL, -1);
760
761 if (err)
762 {
763 mch_remove(fname);
764 return FAIL;
765 }
766 return OK;
767}
768# endif
769
770# if defined(FEAT_DIFF) || defined(PROTO)
771 void
772eval_diff(origfile, newfile, outfile)
773 char_u *origfile;
774 char_u *newfile;
775 char_u *outfile;
776{
777 int err = FALSE;
778
779 set_vim_var_string(VV_FNAME_IN, origfile, -1);
780 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
781 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
782 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
783 set_vim_var_string(VV_FNAME_IN, NULL, -1);
784 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
785 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
786}
787
788 void
789eval_patch(origfile, difffile, outfile)
790 char_u *origfile;
791 char_u *difffile;
792 char_u *outfile;
793{
794 int err;
795
796 set_vim_var_string(VV_FNAME_IN, origfile, -1);
797 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
798 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
799 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
800 set_vim_var_string(VV_FNAME_IN, NULL, -1);
801 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
802 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
803}
804# endif
805
806/*
807 * Top level evaluation function, returning a boolean.
808 * Sets "error" to TRUE if there was an error.
809 * Return TRUE or FALSE.
810 */
811 int
812eval_to_bool(arg, error, nextcmd, skip)
813 char_u *arg;
814 int *error;
815 char_u **nextcmd;
816 int skip; /* only parse, don't execute */
817{
Bram Moolenaar33570922005-01-25 22:26:29 +0000818 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 int retval = FALSE;
820
821 if (skip)
822 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000823 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 else
826 {
827 *error = FALSE;
828 if (!skip)
829 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000830 retval = (get_tv_number(&tv) != 0);
831 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 }
833 }
834 if (skip)
835 --emsg_skip;
836
837 return retval;
838}
839
840/*
841 * Top level evaluation function, returning a string. If "skip" is TRUE,
842 * only parsing to "nextcmd" is done, without reporting errors. Return
843 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
844 */
845 char_u *
846eval_to_string_skip(arg, nextcmd, skip)
847 char_u *arg;
848 char_u **nextcmd;
849 int skip; /* only parse, don't execute */
850{
Bram Moolenaar33570922005-01-25 22:26:29 +0000851 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 char_u *retval;
853
854 if (skip)
855 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000856 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 retval = NULL;
858 else
859 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000860 retval = vim_strsave(get_tv_string(&tv));
861 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 }
863 if (skip)
864 --emsg_skip;
865
866 return retval;
867}
868
869/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000870 * Skip over an expression at "*pp".
871 * Return FAIL for an error, OK otherwise.
872 */
873 int
874skip_expr(pp)
875 char_u **pp;
876{
Bram Moolenaar33570922005-01-25 22:26:29 +0000877 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000878
879 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000880 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000881}
882
883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 * Top level evaluation function, returning a string.
885 * Return pointer to allocated memory, or NULL for failure.
886 */
887 char_u *
888eval_to_string(arg, nextcmd)
889 char_u *arg;
890 char_u **nextcmd;
891{
Bram Moolenaar33570922005-01-25 22:26:29 +0000892 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 char_u *retval;
894
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000895 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 retval = NULL;
897 else
898 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000899 retval = vim_strsave(get_tv_string(&tv));
900 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 }
902
903 return retval;
904}
905
906/*
907 * Call eval_to_string() with "sandbox" set and not using local variables.
908 */
909 char_u *
910eval_to_string_safe(arg, nextcmd)
911 char_u *arg;
912 char_u **nextcmd;
913{
914 char_u *retval;
915 void *save_funccalp;
916
917 save_funccalp = save_funccal();
918 ++sandbox;
919 retval = eval_to_string(arg, nextcmd);
920 --sandbox;
921 restore_funccal(save_funccalp);
922 return retval;
923}
924
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925/*
926 * Top level evaluation function, returning a number.
927 * Evaluates "expr" silently.
928 * Returns -1 for an error.
929 */
930 int
931eval_to_number(expr)
932 char_u *expr;
933{
Bram Moolenaar33570922005-01-25 22:26:29 +0000934 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000936 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937
938 ++emsg_off;
939
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000940 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 retval = -1;
942 else
943 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000944 retval = get_tv_number(&rettv);
945 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
947 --emsg_off;
948
949 return retval;
950}
951
952#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
953/*
954 * Call some vimL function and return the result as a string
955 * Uses argv[argc] for the function arguments.
956 */
957 char_u *
958call_vim_function(func, argc, argv, safe)
959 char_u *func;
960 int argc;
961 char_u **argv;
962 int safe; /* use the sandbox */
963{
964 char_u *retval = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 typval_T rettv;
966 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 long n;
968 int len;
969 int i;
970 int doesrange;
971 void *save_funccalp = NULL;
972
Bram Moolenaar33570922005-01-25 22:26:29 +0000973 argvars = (typval_T *)alloc((unsigned)(argc * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 if (argvars == NULL)
975 return NULL;
976
977 for (i = 0; i < argc; i++)
978 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000979 /* Pass a NULL or empty argument as an empty string */
980 if (argv[i] == NULL || *argv[i] == NUL)
981 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000982 argvars[i].v_type = VAR_STRING;
983 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000984 continue;
985 }
986
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 /* Recognize a number argument, the others must be strings. */
988 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
989 if (len != 0 && len == (int)STRLEN(argv[i]))
990 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000991 argvars[i].v_type = VAR_NUMBER;
992 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 }
994 else
995 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000996 argvars[i].v_type = VAR_STRING;
997 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 }
999 }
1000
1001 if (safe)
1002 {
1003 save_funccalp = save_funccal();
1004 ++sandbox;
1005 }
1006
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001007 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1008 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00001010 &doesrange, TRUE, NULL) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001011 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001013 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 vim_free(argvars);
1015
1016 if (safe)
1017 {
1018 --sandbox;
1019 restore_funccal(save_funccalp);
1020 }
1021 return retval;
1022}
1023#endif
1024
1025/*
1026 * Save the current function call pointer, and set it to NULL.
1027 * Used when executing autocommands and for ":source".
1028 */
1029 void *
1030save_funccal()
1031{
Bram Moolenaar33570922005-01-25 22:26:29 +00001032 funccall_T *fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033
1034 fc = current_funccal;
1035 current_funccal = NULL;
1036 return (void *)fc;
1037}
1038
1039 void
1040restore_funccal(fc)
1041 void *fc;
1042{
Bram Moolenaar33570922005-01-25 22:26:29 +00001043 current_funccal = (funccall_T *)fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044}
1045
1046#ifdef FEAT_FOLDING
1047/*
1048 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1049 * it in "*cp". Doesn't give error messages.
1050 */
1051 int
1052eval_foldexpr(arg, cp)
1053 char_u *arg;
1054 int *cp;
1055{
Bram Moolenaar33570922005-01-25 22:26:29 +00001056 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 int retval;
1058 char_u *s;
1059
1060 ++emsg_off;
1061 ++sandbox;
1062 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001063 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 retval = 0;
1065 else
1066 {
1067 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001068 if (tv.v_type == VAR_NUMBER)
1069 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001070 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 retval = 0;
1072 else
1073 {
1074 /* If the result is a string, check if there is a non-digit before
1075 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001076 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if (!VIM_ISDIGIT(*s) && *s != '-')
1078 *cp = *s++;
1079 retval = atol((char *)s);
1080 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001081 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 }
1083 --emsg_off;
1084 --sandbox;
1085
1086 return retval;
1087}
1088#endif
1089
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001091 * ":let" list all variable values
1092 * ":let var1 var2" list variable values
1093 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001094 * ":let var += expr" assignment command.
1095 * ":let var -= expr" assignment command.
1096 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001097 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 */
1099 void
1100ex_let(eap)
1101 exarg_T *eap;
1102{
1103 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001104 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001105 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001107 int var_count = 0;
1108 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001109 char_u op[2];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001111 expr = skip_var_list(arg, &var_count, &semicolon);
1112 if (expr == NULL)
1113 return;
1114 expr = vim_strchr(expr, '=');
1115 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001117 /*
1118 * ":let" without "=": list variables
1119 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001120 if (*arg == '[')
1121 EMSG(_(e_invarg));
1122 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001123 /* ":let var1 var2" */
1124 arg = list_arg_vars(eap, arg);
1125 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001126 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001127 /* ":let" */
Bram Moolenaara7043832005-01-21 11:56:39 +00001128 list_glob_vars();
1129 list_buf_vars();
1130 list_win_vars();
1131 list_vim_vars();
1132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 eap->nextcmd = check_nextcmd(arg);
1134 }
1135 else
1136 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001137 op[0] = '=';
1138 op[1] = NUL;
1139 if (expr > arg)
1140 {
1141 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1142 op[0] = expr[-1]; /* +=, -= or .= */
1143 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001144 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001145
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 if (eap->skip)
1147 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001148 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 if (eap->skip)
1150 {
1151 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001152 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 --emsg_skip;
1154 }
1155 else if (i != FAIL)
1156 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001157 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001158 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001159 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 }
1161 }
1162}
1163
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001164/*
1165 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1166 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001167 * When "nextchars" is not NULL it points to a string with characters that
1168 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1169 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001170 * Returns OK or FAIL;
1171 */
1172 static int
1173ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1174 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001175 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001176 int copy; /* copy values from "tv", don't move */
1177 int semicolon; /* from skip_var_list() */
1178 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001179 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001180{
1181 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001182 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001183 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001184 listitem_T *item;
1185 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001186
1187 if (*arg != '[')
1188 {
1189 /*
1190 * ":let var = expr" or ":for var in list"
1191 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001192 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001193 return FAIL;
1194 return OK;
1195 }
1196
1197 /*
1198 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1199 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001200 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001201 {
1202 EMSG(_(e_listreq));
1203 return FAIL;
1204 }
1205
1206 i = list_len(l);
1207 if (semicolon == 0 && var_count < i)
1208 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001209 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001210 return FAIL;
1211 }
1212 if (var_count - semicolon > i)
1213 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001214 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001215 return FAIL;
1216 }
1217
1218 item = l->lv_first;
1219 while (*arg != ']')
1220 {
1221 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001222 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001223 item = item->li_next;
1224 if (arg == NULL)
1225 return FAIL;
1226
1227 arg = skipwhite(arg);
1228 if (*arg == ';')
1229 {
1230 /* Put the rest of the list (may be empty) in the var after ';'.
1231 * Create a new list for this. */
1232 l = list_alloc();
1233 if (l == NULL)
1234 return FAIL;
1235 while (item != NULL)
1236 {
1237 list_append_tv(l, &item->li_tv);
1238 item = item->li_next;
1239 }
1240
1241 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001242 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001243 ltv.vval.v_list = l;
1244 l->lv_refcount = 1;
1245
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001246 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1247 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001248 clear_tv(&ltv);
1249 if (arg == NULL)
1250 return FAIL;
1251 break;
1252 }
1253 else if (*arg != ',' && *arg != ']')
1254 {
1255 EMSG2(_(e_intern2), "ex_let_vars()");
1256 return FAIL;
1257 }
1258 }
1259
1260 return OK;
1261}
1262
1263/*
1264 * Skip over assignable variable "var" or list of variables "[var, var]".
1265 * Used for ":let varvar = expr" and ":for varvar in expr".
1266 * For "[var, var]" increment "*var_count" for each variable.
1267 * for "[var, var; var]" set "semicolon".
1268 * Return NULL for an error.
1269 */
1270 static char_u *
1271skip_var_list(arg, var_count, semicolon)
1272 char_u *arg;
1273 int *var_count;
1274 int *semicolon;
1275{
1276 char_u *p, *s;
1277
1278 if (*arg == '[')
1279 {
1280 /* "[var, var]": find the matching ']'. */
1281 p = arg;
1282 while (1)
1283 {
1284 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1285 s = skip_var_one(p);
1286 if (s == p)
1287 {
1288 EMSG2(_(e_invarg2), p);
1289 return NULL;
1290 }
1291 ++*var_count;
1292
1293 p = skipwhite(s);
1294 if (*p == ']')
1295 break;
1296 else if (*p == ';')
1297 {
1298 if (*semicolon == 1)
1299 {
1300 EMSG(_("Double ; in list of variables"));
1301 return NULL;
1302 }
1303 *semicolon = 1;
1304 }
1305 else if (*p != ',')
1306 {
1307 EMSG2(_(e_invarg2), p);
1308 return NULL;
1309 }
1310 }
1311 return p + 1;
1312 }
1313 else
1314 return skip_var_one(arg);
1315}
1316
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001317/*
1318 * Skip one (assignable) variable name, includig $VAR, d.key, l[idx].
1319 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001320 static char_u *
1321skip_var_one(arg)
1322 char_u *arg;
1323{
1324 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1325 ++arg;
1326 return find_name_end(arg, NULL, NULL, TRUE);
1327}
1328
Bram Moolenaara7043832005-01-21 11:56:39 +00001329/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001330 * List variables for hashtab "ht" with prefix "prefix".
1331 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001332 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001333 static void
Bram Moolenaar33570922005-01-25 22:26:29 +00001334list_hashtable_vars(ht, prefix, empty)
1335 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00001336 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00001337 int empty;
Bram Moolenaara7043832005-01-21 11:56:39 +00001338{
Bram Moolenaar33570922005-01-25 22:26:29 +00001339 hashitem_T *hi;
1340 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001341 int todo;
1342
1343 todo = ht->ht_used;
1344 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1345 {
1346 if (!HASHITEM_EMPTY(hi))
1347 {
1348 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001349 di = HI2DI(hi);
1350 if (empty || di->di_tv.v_type != VAR_STRING
1351 || di->di_tv.vval.v_string != NULL)
1352 list_one_var(di, prefix);
Bram Moolenaara7043832005-01-21 11:56:39 +00001353 }
1354 }
1355}
1356
1357/*
1358 * List global variables.
1359 */
1360 static void
1361list_glob_vars()
1362{
Bram Moolenaar33570922005-01-25 22:26:29 +00001363 list_hashtable_vars(&globvarht, (char_u *)"", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001364}
1365
1366/*
1367 * List buffer variables.
1368 */
1369 static void
1370list_buf_vars()
1371{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001372 char_u numbuf[NUMBUFLEN];
1373
Bram Moolenaar33570922005-01-25 22:26:29 +00001374 list_hashtable_vars(&curbuf->b_vars.dv_hashtab, (char_u *)"b:", TRUE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001375
1376 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
1377 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER, numbuf);
Bram Moolenaara7043832005-01-21 11:56:39 +00001378}
1379
1380/*
1381 * List window variables.
1382 */
1383 static void
1384list_win_vars()
1385{
Bram Moolenaar33570922005-01-25 22:26:29 +00001386 list_hashtable_vars(&curwin->w_vars.dv_hashtab, (char_u *)"w:", TRUE);
Bram Moolenaara7043832005-01-21 11:56:39 +00001387}
1388
1389/*
1390 * List Vim variables.
1391 */
1392 static void
1393list_vim_vars()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001394{
Bram Moolenaar33570922005-01-25 22:26:29 +00001395 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001396}
1397
1398/*
1399 * List variables in "arg".
1400 */
1401 static char_u *
1402list_arg_vars(eap, arg)
1403 exarg_T *eap;
1404 char_u *arg;
1405{
1406 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001407 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001408 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001409 char_u *name_start;
1410 char_u *arg_subsc;
1411 char_u *tofree;
1412 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001413
1414 while (!ends_excmd(*arg) && !got_int)
1415 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001416 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001417 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001418 arg = find_name_end(arg, NULL, NULL, TRUE);
1419 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
1420 {
1421 emsg_severe = TRUE;
1422 EMSG(_(e_trailing));
1423 break;
1424 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001425 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001426 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001427 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001428 /* get_name_len() takes care of expanding curly braces */
1429 name_start = name = arg;
1430 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1431 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001433 /* This is mainly to keep test 49 working: when expanding
1434 * curly braces fails overrule the exception error message. */
1435 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001436 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001437 emsg_severe = TRUE;
1438 EMSG2(_(e_invarg2), arg);
1439 break;
1440 }
1441 error = TRUE;
1442 }
1443 else
1444 {
1445 if (tofree != NULL)
1446 name = tofree;
1447 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001448 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001449 else
1450 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001451 /* handle d.key, l[idx], f(expr) */
1452 arg_subsc = arg;
1453 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001454 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001455 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001456 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001457 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001458 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001459 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001460 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001461 case 'g': list_glob_vars(); break;
1462 case 'b': list_buf_vars(); break;
1463 case 'w': list_win_vars(); break;
1464 case 'v': list_vim_vars(); break;
1465 default:
1466 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001467 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001468 }
1469 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001470 {
1471 char_u numbuf[NUMBUFLEN];
1472 char_u *tf;
1473 int c;
1474 char_u *s;
1475
1476 s = echo_string(&tv, &tf, numbuf);
1477 c = *arg;
1478 *arg = NUL;
1479 list_one_var_a((char_u *)"",
1480 arg == arg_subsc ? name : name_start,
1481 tv.v_type, s == NULL ? (char_u *)"" : s);
1482 *arg = c;
1483 vim_free(tf);
1484 }
1485 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001486 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001487 }
1488 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001489
1490 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001491 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001492
1493 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001494 }
1495
1496 return arg;
1497}
1498
1499/*
1500 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1501 * Returns a pointer to the char just after the var name.
1502 * Returns NULL if there is an error.
1503 */
1504 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001505ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001506 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00001507 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001508 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001509 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001510 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001511{
1512 int c1;
1513 char_u *name;
1514 char_u *p;
1515 char_u *arg_end = NULL;
1516 int len;
1517 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001518 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001519
1520 /*
1521 * ":let $VAR = expr": Set environment variable.
1522 */
1523 if (*arg == '$')
1524 {
1525 /* Find the end of the name. */
1526 ++arg;
1527 name = arg;
1528 len = get_env_len(&arg);
1529 if (len == 0)
1530 EMSG2(_(e_invarg2), name - 1);
1531 else
1532 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001533 if (op != NULL && (*op == '+' || *op == '-'))
1534 EMSG2(_(e_letwrong), op);
1535 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001536 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001537 EMSG(_(e_letunexp));
1538 else
1539 {
1540 c1 = name[len];
1541 name[len] = NUL;
1542 p = get_tv_string(tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001543 if (op != NULL && *op == '.')
1544 {
1545 int mustfree = FALSE;
1546 char_u *s = vim_getenv(name, &mustfree);
1547
1548 if (s != NULL)
1549 {
1550 p = tofree = concat_str(s, p);
1551 if (mustfree)
1552 vim_free(s);
1553 }
1554 }
1555 if (p != NULL)
1556 vim_setenv(name, p);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001557 if (STRICMP(name, "HOME") == 0)
1558 init_homedir();
1559 else if (didset_vim && STRICMP(name, "VIM") == 0)
1560 didset_vim = FALSE;
1561 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1562 didset_vimruntime = FALSE;
1563 name[len] = c1;
1564 arg_end = arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001565 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001566 }
1567 }
1568 }
1569
1570 /*
1571 * ":let &option = expr": Set option value.
1572 * ":let &l:option = expr": Set local option value.
1573 * ":let &g:option = expr": Set global option value.
1574 */
1575 else if (*arg == '&')
1576 {
1577 /* Find the end of the name. */
1578 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001579 if (p == NULL || (endchars != NULL
1580 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001581 EMSG(_(e_letunexp));
1582 else
1583 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001584 long n;
1585 int opt_type;
1586 long numval;
1587 char_u *stringval = NULL;
1588 char_u *s;
1589
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001590 c1 = *p;
1591 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001592
1593 n = get_tv_number(tv);
1594 s = get_tv_string(tv);
1595 if (op != NULL && *op != '=')
1596 {
1597 opt_type = get_option_value(arg, &numval,
1598 &stringval, opt_flags);
1599 if ((opt_type == 1 && *op == '.')
1600 || (opt_type == 0 && *op != '.'))
1601 EMSG2(_(e_letwrong), op);
1602 else
1603 {
1604 if (opt_type == 1) /* number */
1605 {
1606 if (*op == '+')
1607 n = numval + n;
1608 else
1609 n = numval - n;
1610 }
1611 else if (opt_type == 0 && stringval != NULL) /* string */
1612 {
1613 s = concat_str(stringval, s);
1614 vim_free(stringval);
1615 stringval = s;
1616 }
1617 }
1618 }
1619 set_option_value(arg, n, s, opt_flags);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001620 *p = c1;
1621 arg_end = p;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001622 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001623 }
1624 }
1625
1626 /*
1627 * ":let @r = expr": Set register contents.
1628 */
1629 else if (*arg == '@')
1630 {
1631 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001632 if (op != NULL && (*op == '+' || *op == '-'))
1633 EMSG2(_(e_letwrong), op);
1634 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001635 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001636 EMSG(_(e_letunexp));
1637 else
1638 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001639 char_u *tofree = NULL;
1640 char_u *s;
1641
1642 p = get_tv_string(tv);
1643 if (op != NULL && *op == '.')
1644 {
1645 s = get_reg_contents(*arg == '@' ? '"' : *arg, FALSE);
1646 if (s != NULL)
1647 {
1648 p = tofree = concat_str(s, p);
1649 vim_free(s);
1650 }
1651 }
1652 if (p != NULL)
1653 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001654 arg_end = arg + 1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001655 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001656 }
1657 }
1658
1659 /*
1660 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001661 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001662 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00001663 else if ((eval_isnamec(*arg) && !VIM_ISDIGIT(*arg)) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001664 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001665 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001666
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001667 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE);
1668 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001669 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001670 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1671 EMSG(_(e_letunexp));
1672 else
1673 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001674 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001675 arg_end = p;
1676 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001677 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001678 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001679 }
1680
1681 else
1682 EMSG2(_(e_invarg2), arg);
1683
1684 return arg_end;
1685}
1686
1687/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001688 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1689 */
1690 static int
1691check_changedtick(arg)
1692 char_u *arg;
1693{
1694 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1695 {
1696 EMSG2(_(e_readonlyvar), arg);
1697 return TRUE;
1698 }
1699 return FALSE;
1700}
1701
1702/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001703 * Get an lval: variable, Dict item or List item that can be assigned a value
1704 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1705 * "name.key", "name.key[expr]" etc.
1706 * Indexing only works if "name" is an existing List or Dictionary.
1707 * "name" points to the start of the name.
1708 * If "rettv" is not NULL it points to the value to be assigned.
1709 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1710 * wrong; must end in space or cmd separator.
1711 *
1712 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001713 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001714 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001715 */
1716 static char_u *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001717get_lval(name, rettv, lp, unlet, skip, quiet)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001718 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00001719 typval_T *rettv;
1720 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001721 int unlet;
1722 int skip;
1723 int quiet; /* don't give error messages */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001724{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001725 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001726 char_u *expr_start, *expr_end;
1727 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001728 dictitem_T *v;
1729 typval_T var1;
1730 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001731 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001732 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001733 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001734 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001735 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001736
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001737 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001738 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001739
1740 if (skip)
1741 {
1742 /* When skipping just find the end of the name. */
1743 lp->ll_name = name;
1744 return find_name_end(name, NULL, NULL, TRUE);
1745 }
1746
1747 /* Find the end of the name. */
1748 p = find_name_end(name, &expr_start, &expr_end, FALSE);
1749 if (expr_start != NULL)
1750 {
1751 /* Don't expand the name when we already know there is an error. */
1752 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
1753 && *p != '[' && *p != '.')
1754 {
1755 EMSG(_(e_trailing));
1756 return NULL;
1757 }
1758
1759 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1760 if (lp->ll_exp_name == NULL)
1761 {
1762 /* Report an invalid expression in braces, unless the
1763 * expression evaluation has been cancelled due to an
1764 * aborting error, an interrupt, or an exception. */
1765 if (!aborting() && !quiet)
1766 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001767 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001768 EMSG2(_(e_invarg2), name);
1769 return NULL;
1770 }
1771 }
1772 lp->ll_name = lp->ll_exp_name;
1773 }
1774 else
1775 lp->ll_name = name;
1776
1777 /* Without [idx] or .key we are done. */
1778 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1779 return p;
1780
1781 cc = *p;
1782 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00001783 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001784 if (v == NULL && !quiet)
1785 EMSG2(_(e_undefvar), lp->ll_name);
1786 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001787 if (v == NULL)
1788 return NULL;
1789
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001790 /*
1791 * Loop until no more [idx] or .key is following.
1792 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001793 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001794 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001796 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1797 && !(lp->ll_tv->v_type == VAR_DICT
1798 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001799 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001800 if (!quiet)
1801 EMSG(_("E689: Can only index a List or Dictionary"));
1802 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001803 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001804 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001805 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001806 if (!quiet)
1807 EMSG(_("E708: [:] must come last"));
1808 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001809 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001810
Bram Moolenaar8c711452005-01-14 21:53:12 +00001811 len = -1;
1812 if (*p == '.')
1813 {
1814 key = p + 1;
1815 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1816 ;
1817 if (len == 0)
1818 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001819 if (!quiet)
1820 EMSG(_(e_emptykey));
1821 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001822 }
1823 p = key + len;
1824 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001825 else
1826 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001827 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001828 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001829 if (*p == ':')
1830 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001831 else
1832 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001833 empty1 = FALSE;
1834 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001835 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001836 }
1837
1838 /* Optionally get the second index [ :expr]. */
1839 if (*p == ':')
1840 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001841 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001842 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001843 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001844 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001845 if (!empty1)
1846 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001847 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001848 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001849 if (rettv != NULL && (rettv->v_type != VAR_LIST
1850 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001851 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001852 if (!quiet)
1853 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001854 if (!empty1)
1855 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001856 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001857 }
1858 p = skipwhite(p + 1);
1859 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001860 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001861 else
1862 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001863 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001864 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1865 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001866 if (!empty1)
1867 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001868 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001869 }
1870 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001871 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001872 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001873 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001874 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001875
Bram Moolenaar8c711452005-01-14 21:53:12 +00001876 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001877 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001878 if (!quiet)
1879 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001880 if (!empty1)
1881 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001882 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001883 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001884 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001885 }
1886
1887 /* Skip to past ']'. */
1888 ++p;
1889 }
1890
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001891 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001892 {
1893 if (len == -1)
1894 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001895 /* "[key]": get key from "var1" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001896 key = get_tv_string(&var1);
1897 if (*key == NUL)
1898 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001899 if (!quiet)
1900 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001901 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001902 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001903 }
1904 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001905 lp->ll_list = NULL;
1906 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001907 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001908 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001909 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001910 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001911 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001912 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001913 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001914 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001915 if (len == -1)
1916 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001917 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001918 }
1919 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001920 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001921 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001922 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001923 if (len == -1)
1924 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001925 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001926 p = NULL;
1927 break;
1928 }
1929 if (len == -1)
1930 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001931 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001932 }
1933 else
1934 {
1935 /*
1936 * Get the number and item for the only or first index of the List.
1937 */
1938 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001939 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001940 else
1941 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001942 lp->ll_n1 = get_tv_number(&var1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001943 clear_tv(&var1);
1944 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001945 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001946 lp->ll_list = lp->ll_tv->vval.v_list;
1947 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1948 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001949 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001950 if (!quiet)
1951 EMSGN(_(e_listidx), lp->ll_n1);
1952 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001953 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001954 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001955 }
1956
1957 /*
1958 * May need to find the item or absolute index for the second
1959 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001960 * When no index given: "lp->ll_empty2" is TRUE.
1961 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001962 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001963 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001964 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001965 lp->ll_n2 = get_tv_number(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001966 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001967 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001968 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001969 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001970 if (ni == NULL)
1971 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001972 if (!quiet)
1973 EMSGN(_(e_listidx), lp->ll_n2);
1974 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001975 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001976 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001977 }
1978
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001979 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
1980 if (lp->ll_n1 < 0)
1981 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1982 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001983 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001984 if (!quiet)
1985 EMSGN(_(e_listidx), lp->ll_n2);
1986 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001987 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001988 }
1989
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001990 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001991 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001992 }
1993
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001994 return p;
1995}
1996
1997/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001998 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001999 */
2000 static void
2001clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002002 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002003{
2004 vim_free(lp->ll_exp_name);
2005 vim_free(lp->ll_newkey);
2006}
2007
2008/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002009 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002010 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002011 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002012 */
2013 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002014set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002015 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002016 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002017 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002018 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002019 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002020{
2021 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002022 listitem_T *ni;
2023 listitem_T *ri;
2024 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002025
2026 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002027 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002028 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002029 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002030 cc = *endp;
2031 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002032 if (op != NULL && *op != '=')
2033 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002034 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002035
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002036 /* handle +=, -= and .= */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002037 if (get_var_tv(lp->ll_name, STRLEN(lp->ll_name),
2038 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002039 {
2040 if (tv_op(&tv, rettv, op) == OK)
2041 set_var(lp->ll_name, &tv, FALSE);
2042 clear_tv(&tv);
2043 }
2044 }
2045 else
2046 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002047 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002048 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002049 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002050 else if (tv_check_lock(lp->ll_newkey == NULL
2051 ? lp->ll_tv->v_lock
2052 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2053 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002054 else if (lp->ll_range)
2055 {
2056 /*
2057 * Assign the List values to the list items.
2058 */
2059 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002060 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002061 if (op != NULL && *op != '=')
2062 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2063 else
2064 {
2065 clear_tv(&lp->ll_li->li_tv);
2066 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2067 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002068 ri = ri->li_next;
2069 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2070 break;
2071 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002072 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002073 /* Need to add an empty item. */
2074 ni = listitem_alloc();
2075 if (ni == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002076 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002077 ri = NULL;
2078 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002079 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002080 ni->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002081 ni->li_tv.v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002082 ni->li_tv.vval.v_number = 0;
2083 list_append(lp->ll_list, ni);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002084 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002085 lp->ll_li = lp->ll_li->li_next;
2086 ++lp->ll_n1;
2087 }
2088 if (ri != NULL)
2089 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002090 else if (lp->ll_empty2
2091 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002092 : lp->ll_n1 != lp->ll_n2)
2093 EMSG(_("E711: List value has not enough items"));
2094 }
2095 else
2096 {
2097 /*
2098 * Assign to a List or Dictionary item.
2099 */
2100 if (lp->ll_newkey != NULL)
2101 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002102 if (op != NULL && *op != '=')
2103 {
2104 EMSG2(_(e_letwrong), op);
2105 return;
2106 }
2107
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002108 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002109 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002110 if (di == NULL)
2111 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002112 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2113 {
2114 vim_free(di);
2115 return;
2116 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002117 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002118 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002119 else if (op != NULL && *op != '=')
2120 {
2121 tv_op(lp->ll_tv, rettv, op);
2122 return;
2123 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002124 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002125 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002126
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002127 /*
2128 * Assign the value to the variable or list item.
2129 */
2130 if (copy)
2131 copy_tv(rettv, lp->ll_tv);
2132 else
2133 {
2134 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002135 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002136 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002137 }
2138 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002139}
2140
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002141/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002142 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
2143 * Returns OK or FAIL.
2144 */
2145 static int
2146tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002147 typval_T *tv1;
2148 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002149 char_u *op;
2150{
2151 long n;
2152 char_u numbuf[NUMBUFLEN];
2153 char_u *s;
2154
2155 /* Can't do anything with a Funcref or a Dict on the right. */
2156 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
2157 {
2158 switch (tv1->v_type)
2159 {
2160 case VAR_DICT:
2161 case VAR_FUNC:
2162 break;
2163
2164 case VAR_LIST:
2165 if (*op != '+' || tv2->v_type != VAR_LIST)
2166 break;
2167 /* List += List */
2168 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2169 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2170 return OK;
2171
2172 case VAR_NUMBER:
2173 case VAR_STRING:
2174 if (tv2->v_type == VAR_LIST)
2175 break;
2176 if (*op == '+' || *op == '-')
2177 {
2178 /* nr += nr or nr -= nr*/
2179 n = get_tv_number(tv1);
2180 if (*op == '+')
2181 n += get_tv_number(tv2);
2182 else
2183 n -= get_tv_number(tv2);
2184 clear_tv(tv1);
2185 tv1->v_type = VAR_NUMBER;
2186 tv1->vval.v_number = n;
2187 }
2188 else
2189 {
2190 /* str .= str */
2191 s = get_tv_string(tv1);
2192 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
2193 clear_tv(tv1);
2194 tv1->v_type = VAR_STRING;
2195 tv1->vval.v_string = s;
2196 }
2197 return OK;
2198 }
2199 }
2200
2201 EMSG2(_(e_letwrong), op);
2202 return FAIL;
2203}
2204
2205/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002206 * Add a watcher to a list.
2207 */
2208 static void
2209list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00002210 list_T *l;
2211 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002212{
2213 lw->lw_next = l->lv_watch;
2214 l->lv_watch = lw;
2215}
2216
2217/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00002218 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002219 * No warning when it isn't found...
2220 */
2221 static void
2222list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00002223 list_T *l;
2224 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002225{
Bram Moolenaar33570922005-01-25 22:26:29 +00002226 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002227
2228 lwp = &l->lv_watch;
2229 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2230 {
2231 if (lw == lwrem)
2232 {
2233 *lwp = lw->lw_next;
2234 break;
2235 }
2236 lwp = &lw->lw_next;
2237 }
2238}
2239
2240/*
2241 * Just before removing an item from a list: advance watchers to the next
2242 * item.
2243 */
2244 static void
2245list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00002246 list_T *l;
2247 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002248{
Bram Moolenaar33570922005-01-25 22:26:29 +00002249 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002250
2251 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
2252 if (lw->lw_item == item)
2253 lw->lw_item = item->li_next;
2254}
2255
2256/*
2257 * Evaluate the expression used in a ":for var in expr" command.
2258 * "arg" points to "var".
2259 * Set "*errp" to TRUE for an error, FALSE otherwise;
2260 * Return a pointer that holds the info. Null when there is an error.
2261 */
2262 void *
2263eval_for_line(arg, errp, nextcmdp, skip)
2264 char_u *arg;
2265 int *errp;
2266 char_u **nextcmdp;
2267 int skip;
2268{
Bram Moolenaar33570922005-01-25 22:26:29 +00002269 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002270 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002271 typval_T tv;
2272 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002273
2274 *errp = TRUE; /* default: there is an error */
2275
Bram Moolenaar33570922005-01-25 22:26:29 +00002276 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002277 if (fi == NULL)
2278 return NULL;
2279
2280 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2281 if (expr == NULL)
2282 return fi;
2283
2284 expr = skipwhite(expr);
2285 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2286 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002287 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002288 return fi;
2289 }
2290
2291 if (skip)
2292 ++emsg_skip;
2293 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2294 {
2295 *errp = FALSE;
2296 if (!skip)
2297 {
2298 l = tv.vval.v_list;
2299 if (tv.v_type != VAR_LIST || l == NULL)
2300 EMSG(_(e_listreq));
2301 else
2302 {
2303 fi->fi_list = l;
2304 list_add_watch(l, &fi->fi_lw);
2305 fi->fi_lw.lw_item = l->lv_first;
2306 }
2307 }
2308 }
2309 if (skip)
2310 --emsg_skip;
2311
2312 return fi;
2313}
2314
2315/*
2316 * Use the first item in a ":for" list. Advance to the next.
2317 * Assign the values to the variable (list). "arg" points to the first one.
2318 * Return TRUE when a valid item was found, FALSE when at end of list or
2319 * something wrong.
2320 */
2321 int
2322next_for_item(fi_void, arg)
2323 void *fi_void;
2324 char_u *arg;
2325{
Bram Moolenaar33570922005-01-25 22:26:29 +00002326 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002327 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002328 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002329
2330 item = fi->fi_lw.lw_item;
2331 if (item == NULL)
2332 result = FALSE;
2333 else
2334 {
2335 fi->fi_lw.lw_item = item->li_next;
2336 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2337 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2338 }
2339 return result;
2340}
2341
2342/*
2343 * Free the structure used to store info used by ":for".
2344 */
2345 void
2346free_for_info(fi_void)
2347 void *fi_void;
2348{
Bram Moolenaar33570922005-01-25 22:26:29 +00002349 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002350
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002351 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002352 list_rem_watch(fi->fi_list, &fi->fi_lw);
2353 vim_free(fi);
2354}
2355
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2357
2358 void
2359set_context_for_expression(xp, arg, cmdidx)
2360 expand_T *xp;
2361 char_u *arg;
2362 cmdidx_T cmdidx;
2363{
2364 int got_eq = FALSE;
2365 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002366 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002368 if (cmdidx == CMD_let)
2369 {
2370 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002371 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002372 {
2373 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002374 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002375 {
2376 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00002377 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002378 if (vim_iswhite(*p))
2379 break;
2380 }
2381 return;
2382 }
2383 }
2384 else
2385 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2386 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 while ((xp->xp_pattern = vim_strpbrk(arg,
2388 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2389 {
2390 c = *xp->xp_pattern;
2391 if (c == '&')
2392 {
2393 c = xp->xp_pattern[1];
2394 if (c == '&')
2395 {
2396 ++xp->xp_pattern;
2397 xp->xp_context = cmdidx != CMD_let || got_eq
2398 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2399 }
2400 else if (c != ' ')
2401 xp->xp_context = EXPAND_SETTINGS;
2402 }
2403 else if (c == '$')
2404 {
2405 /* environment variable */
2406 xp->xp_context = EXPAND_ENV_VARS;
2407 }
2408 else if (c == '=')
2409 {
2410 got_eq = TRUE;
2411 xp->xp_context = EXPAND_EXPRESSION;
2412 }
2413 else if (c == '<'
2414 && xp->xp_context == EXPAND_FUNCTIONS
2415 && vim_strchr(xp->xp_pattern, '(') == NULL)
2416 {
2417 /* Function name can start with "<SNR>" */
2418 break;
2419 }
2420 else if (cmdidx != CMD_let || got_eq)
2421 {
2422 if (c == '"') /* string */
2423 {
2424 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2425 if (c == '\\' && xp->xp_pattern[1] != NUL)
2426 ++xp->xp_pattern;
2427 xp->xp_context = EXPAND_NOTHING;
2428 }
2429 else if (c == '\'') /* literal string */
2430 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002431 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2433 /* skip */ ;
2434 xp->xp_context = EXPAND_NOTHING;
2435 }
2436 else if (c == '|')
2437 {
2438 if (xp->xp_pattern[1] == '|')
2439 {
2440 ++xp->xp_pattern;
2441 xp->xp_context = EXPAND_EXPRESSION;
2442 }
2443 else
2444 xp->xp_context = EXPAND_COMMANDS;
2445 }
2446 else
2447 xp->xp_context = EXPAND_EXPRESSION;
2448 }
2449 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002450 /* Doesn't look like something valid, expand as an expression
2451 * anyway. */
2452 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 arg = xp->xp_pattern;
2454 if (*arg != NUL)
2455 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2456 /* skip */ ;
2457 }
2458 xp->xp_pattern = arg;
2459}
2460
2461#endif /* FEAT_CMDL_COMPL */
2462
2463/*
2464 * ":1,25call func(arg1, arg2)" function call.
2465 */
2466 void
2467ex_call(eap)
2468 exarg_T *eap;
2469{
2470 char_u *arg = eap->arg;
2471 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002473 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002475 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 linenr_T lnum;
2477 int doesrange;
2478 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002479 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002481 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
2482 vim_free(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 if (tofree == NULL)
2484 return;
2485
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002486 /* Increase refcount on dictionary, it could get deleted when evaluating
2487 * the arguments. */
2488 if (fudi.fd_dict != NULL)
2489 ++fudi.fd_dict->dv_refcount;
2490
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002491 /* If it is the name of a variable of type VAR_FUNC use its contents. */
2492 len = STRLEN(tofree);
2493 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494
Bram Moolenaar532c7802005-01-27 14:44:31 +00002495 /* Skip white space to allow ":call func ()". Not good, but required for
2496 * backward compatibility. */
2497 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002498 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499
2500 if (*startarg != '(')
2501 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00002502 EMSG2(_("E107: Missing braces: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 goto end;
2504 }
2505
2506 /*
2507 * When skipping, evaluate the function once, to find the end of the
2508 * arguments.
2509 * When the function takes a range, this is discovered after the first
2510 * call, and the loop is broken.
2511 */
2512 if (eap->skip)
2513 {
2514 ++emsg_skip;
2515 lnum = eap->line2; /* do it once, also with an invalid range */
2516 }
2517 else
2518 lnum = eap->line1;
2519 for ( ; lnum <= eap->line2; ++lnum)
2520 {
2521 if (!eap->skip && eap->addr_count > 0)
2522 {
2523 curwin->w_cursor.lnum = lnum;
2524 curwin->w_cursor.col = 0;
2525 }
2526 arg = startarg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002527 if (get_func_tv(name, STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002528 eap->line1, eap->line2, &doesrange,
2529 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 {
2531 failed = TRUE;
2532 break;
2533 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 if (doesrange || eap->skip)
2536 break;
2537 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002538 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002539 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002540 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 if (aborting())
2542 break;
2543 }
2544 if (eap->skip)
2545 --emsg_skip;
2546
2547 if (!failed)
2548 {
2549 /* Check for trailing illegal characters and a following command. */
2550 if (!ends_excmd(*arg))
2551 {
2552 emsg_severe = TRUE;
2553 EMSG(_(e_trailing));
2554 }
2555 else
2556 eap->nextcmd = check_nextcmd(arg);
2557 }
2558
2559end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002560 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002561 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562}
2563
2564/*
2565 * ":unlet[!] var1 ... " command.
2566 */
2567 void
2568ex_unlet(eap)
2569 exarg_T *eap;
2570{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002571 ex_unletlock(eap, eap->arg, 0);
2572}
2573
2574/*
2575 * ":lockvar" and ":unlockvar" commands
2576 */
2577 void
2578ex_lockvar(eap)
2579 exarg_T *eap;
2580{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002582 int deep = 2;
2583
2584 if (eap->forceit)
2585 deep = -1;
2586 else if (vim_isdigit(*arg))
2587 {
2588 deep = getdigits(&arg);
2589 arg = skipwhite(arg);
2590 }
2591
2592 ex_unletlock(eap, arg, deep);
2593}
2594
2595/*
2596 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2597 */
2598 static void
2599ex_unletlock(eap, argstart, deep)
2600 exarg_T *eap;
2601 char_u *argstart;
2602 int deep;
2603{
2604 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002607 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608
2609 do
2610 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 /* Parse the name and find the end. */
2612 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE);
2613 if (lv.ll_name == NULL)
2614 error = TRUE; /* error but continue parsing */
2615 if (name_end == NULL || (!vim_iswhite(*name_end)
2616 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 if (name_end != NULL)
2619 {
2620 emsg_severe = TRUE;
2621 EMSG(_(e_trailing));
2622 }
2623 if (!(eap->skip || error))
2624 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 break;
2626 }
2627
2628 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002629 {
2630 if (eap->cmdidx == CMD_unlet)
2631 {
2632 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2633 error = TRUE;
2634 }
2635 else
2636 {
2637 if (do_lock_var(&lv, name_end, deep,
2638 eap->cmdidx == CMD_lockvar) == FAIL)
2639 error = TRUE;
2640 }
2641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002643 if (!eap->skip)
2644 clear_lval(&lv);
2645
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 arg = skipwhite(name_end);
2647 } while (!ends_excmd(*arg));
2648
2649 eap->nextcmd = check_nextcmd(arg);
2650}
2651
Bram Moolenaar8c711452005-01-14 21:53:12 +00002652 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00002654 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002656 int forceit;
2657{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 int ret = OK;
2659 int cc;
2660
2661 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002662 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 cc = *name_end;
2664 *name_end = NUL;
2665
2666 /* Normal name or expanded name. */
2667 if (check_changedtick(lp->ll_name))
2668 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002669 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002673 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
2674 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 else if (lp->ll_range)
2676 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002677 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678
2679 /* Delete a range of List items. */
2680 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2681 {
2682 li = lp->ll_li->li_next;
2683 listitem_remove(lp->ll_list, lp->ll_li);
2684 lp->ll_li = li;
2685 ++lp->ll_n1;
2686 }
2687 }
2688 else
2689 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002690 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 /* unlet a List item. */
2692 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002693 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002695 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002696 }
2697
2698 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699}
2700
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701/*
2702 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002703 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 */
2705 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002706do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002708 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709{
Bram Moolenaar33570922005-01-25 22:26:29 +00002710 hashtab_T *ht;
2711 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002712 char_u *varname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
Bram Moolenaar33570922005-01-25 22:26:29 +00002714 ht = find_var_ht(name, &varname);
2715 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002717 hi = hash_find(ht, varname);
2718 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00002719 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002720 if (var_check_ro(HI2DI(hi)->di_flags, name))
2721 return FAIL;
2722 delete_var(ht, hi);
2723 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00002724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002726 if (forceit)
2727 return OK;
2728 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 return FAIL;
2730}
2731
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002732/*
2733 * Lock or unlock variable indicated by "lp".
2734 * "deep" is the levels to go (-1 for unlimited);
2735 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2736 */
2737 static int
2738do_lock_var(lp, name_end, deep, lock)
2739 lval_T *lp;
2740 char_u *name_end;
2741 int deep;
2742 int lock;
2743{
2744 int ret = OK;
2745 int cc;
2746 dictitem_T *di;
2747
2748 if (deep == 0) /* nothing to do */
2749 return OK;
2750
2751 if (lp->ll_tv == NULL)
2752 {
2753 cc = *name_end;
2754 *name_end = NUL;
2755
2756 /* Normal name or expanded name. */
2757 if (check_changedtick(lp->ll_name))
2758 ret = FAIL;
2759 else
2760 {
2761 di = find_var(lp->ll_name, NULL);
2762 if (di == NULL)
2763 ret = FAIL;
2764 else
2765 {
2766 if (lock)
2767 di->di_flags |= DI_FLAGS_LOCK;
2768 else
2769 di->di_flags &= ~DI_FLAGS_LOCK;
2770 item_lock(&di->di_tv, deep, lock);
2771 }
2772 }
2773 *name_end = cc;
2774 }
2775 else if (lp->ll_range)
2776 {
2777 listitem_T *li = lp->ll_li;
2778
2779 /* (un)lock a range of List items. */
2780 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2781 {
2782 item_lock(&li->li_tv, deep, lock);
2783 li = li->li_next;
2784 ++lp->ll_n1;
2785 }
2786 }
2787 else if (lp->ll_list != NULL)
2788 /* (un)lock a List item. */
2789 item_lock(&lp->ll_li->li_tv, deep, lock);
2790 else
2791 /* un(lock) a Dictionary item. */
2792 item_lock(&lp->ll_di->di_tv, deep, lock);
2793
2794 return ret;
2795}
2796
2797/*
2798 * Lock or unlock an item. "deep" is nr of levels to go.
2799 */
2800 static void
2801item_lock(tv, deep, lock)
2802 typval_T *tv;
2803 int deep;
2804 int lock;
2805{
2806 static int recurse = 0;
2807 list_T *l;
2808 listitem_T *li;
2809 dict_T *d;
2810 hashitem_T *hi;
2811 int todo;
2812
2813 if (recurse >= DICT_MAXNEST)
2814 {
2815 EMSG(_("E743: variable nested too deep for (un)lock"));
2816 return;
2817 }
2818 if (deep == 0)
2819 return;
2820 ++recurse;
2821
2822 /* lock/unlock the item itself */
2823 if (lock)
2824 tv->v_lock |= VAR_LOCKED;
2825 else
2826 tv->v_lock &= ~VAR_LOCKED;
2827
2828 switch (tv->v_type)
2829 {
2830 case VAR_LIST:
2831 if ((l = tv->vval.v_list) != NULL)
2832 {
2833 if (lock)
2834 l->lv_lock |= VAR_LOCKED;
2835 else
2836 l->lv_lock &= ~VAR_LOCKED;
2837 if (deep < 0 || deep > 1)
2838 /* recursive: lock/unlock the items the List contains */
2839 for (li = l->lv_first; li != NULL; li = li->li_next)
2840 item_lock(&li->li_tv, deep - 1, lock);
2841 }
2842 break;
2843 case VAR_DICT:
2844 if ((d = tv->vval.v_dict) != NULL)
2845 {
2846 if (lock)
2847 d->dv_lock |= VAR_LOCKED;
2848 else
2849 d->dv_lock &= ~VAR_LOCKED;
2850 if (deep < 0 || deep > 1)
2851 {
2852 /* recursive: lock/unlock the items the List contains */
2853 todo = d->dv_hashtab.ht_used;
2854 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2855 {
2856 if (!HASHITEM_EMPTY(hi))
2857 {
2858 --todo;
2859 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
2860 }
2861 }
2862 }
2863 }
2864 }
2865 --recurse;
2866}
2867
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2869/*
2870 * Delete all "menutrans_" variables.
2871 */
2872 void
2873del_menutrans_vars()
2874{
Bram Moolenaar33570922005-01-25 22:26:29 +00002875 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002876 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877
Bram Moolenaar33570922005-01-25 22:26:29 +00002878 hash_lock(&globvarht);
2879 todo = globvarht.ht_used;
2880 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00002881 {
2882 if (!HASHITEM_EMPTY(hi))
2883 {
2884 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002885 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2886 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00002887 }
2888 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002889 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890}
2891#endif
2892
2893#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2894
2895/*
2896 * Local string buffer for the next two functions to store a variable name
2897 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2898 * get_user_var_name().
2899 */
2900
2901static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
2902
2903static char_u *varnamebuf = NULL;
2904static int varnamebuflen = 0;
2905
2906/*
2907 * Function to concatenate a prefix and a variable name.
2908 */
2909 static char_u *
2910cat_prefix_varname(prefix, name)
2911 int prefix;
2912 char_u *name;
2913{
2914 int len;
2915
2916 len = (int)STRLEN(name) + 3;
2917 if (len > varnamebuflen)
2918 {
2919 vim_free(varnamebuf);
2920 len += 10; /* some additional space */
2921 varnamebuf = alloc(len);
2922 if (varnamebuf == NULL)
2923 {
2924 varnamebuflen = 0;
2925 return NULL;
2926 }
2927 varnamebuflen = len;
2928 }
2929 *varnamebuf = prefix;
2930 varnamebuf[1] = ':';
2931 STRCPY(varnamebuf + 2, name);
2932 return varnamebuf;
2933}
2934
2935/*
2936 * Function given to ExpandGeneric() to obtain the list of user defined
2937 * (global/buffer/window/built-in) variable names.
2938 */
2939/*ARGSUSED*/
2940 char_u *
2941get_user_var_name(xp, idx)
2942 expand_T *xp;
2943 int idx;
2944{
Bram Moolenaar532c7802005-01-27 14:44:31 +00002945 static long_u gdone;
2946 static long_u bdone;
2947 static long_u wdone;
2948 static int vidx;
2949 static hashitem_T *hi;
2950 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951
2952 if (idx == 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00002953 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00002954
2955 /* Global variables */
2956 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 {
Bram Moolenaara7043832005-01-21 11:56:39 +00002958 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00002959 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00002960 else
2961 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002962 while (HASHITEM_EMPTY(hi))
2963 ++hi;
2964 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2965 return cat_prefix_varname('g', hi->hi_key);
2966 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002968
2969 /* b: variables */
2970 ht = &curbuf->b_vars.dv_hashtab;
2971 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 {
Bram Moolenaara7043832005-01-21 11:56:39 +00002973 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00002974 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00002975 else
2976 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002977 while (HASHITEM_EMPTY(hi))
2978 ++hi;
2979 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002981 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 {
Bram Moolenaara7043832005-01-21 11:56:39 +00002983 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 return (char_u *)"b:changedtick";
2985 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002986
2987 /* w: variables */
2988 ht = &curwin->w_vars.dv_hashtab;
2989 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00002991 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00002992 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00002993 else
2994 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00002995 while (HASHITEM_EMPTY(hi))
2996 ++hi;
2997 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 }
Bram Moolenaar33570922005-01-25 22:26:29 +00002999
3000 /* v: variables */
3001 if (vidx < VV_LEN)
3002 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003
3004 vim_free(varnamebuf);
3005 varnamebuf = NULL;
3006 varnamebuflen = 0;
3007 return NULL;
3008}
3009
3010#endif /* FEAT_CMDL_COMPL */
3011
3012/*
3013 * types for expressions.
3014 */
3015typedef enum
3016{
3017 TYPE_UNKNOWN = 0
3018 , TYPE_EQUAL /* == */
3019 , TYPE_NEQUAL /* != */
3020 , TYPE_GREATER /* > */
3021 , TYPE_GEQUAL /* >= */
3022 , TYPE_SMALLER /* < */
3023 , TYPE_SEQUAL /* <= */
3024 , TYPE_MATCH /* =~ */
3025 , TYPE_NOMATCH /* !~ */
3026} exptype_T;
3027
3028/*
3029 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003030 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3032 */
3033
3034/*
3035 * Handle zero level expression.
3036 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003037 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 * Return OK or FAIL.
3039 */
3040 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003041eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003043 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 char_u **nextcmd;
3045 int evaluate;
3046{
3047 int ret;
3048 char_u *p;
3049
3050 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003051 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 if (ret == FAIL || !ends_excmd(*p))
3053 {
3054 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003055 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 /*
3057 * Report the invalid expression unless the expression evaluation has
3058 * been cancelled due to an aborting error, an interrupt, or an
3059 * exception.
3060 */
3061 if (!aborting())
3062 EMSG2(_(e_invexpr2), arg);
3063 ret = FAIL;
3064 }
3065 if (nextcmd != NULL)
3066 *nextcmd = check_nextcmd(p);
3067
3068 return ret;
3069}
3070
3071/*
3072 * Handle top level expression:
3073 * expr1 ? expr0 : expr0
3074 *
3075 * "arg" must point to the first non-white of the expression.
3076 * "arg" is advanced to the next non-white after the recognized expression.
3077 *
3078 * Return OK or FAIL.
3079 */
3080 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003081eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003083 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 int evaluate;
3085{
3086 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003087 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
3089 /*
3090 * Get the first variable.
3091 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003092 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 return FAIL;
3094
3095 if ((*arg)[0] == '?')
3096 {
3097 result = FALSE;
3098 if (evaluate)
3099 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003100 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003102 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 }
3104
3105 /*
3106 * Get the second variable.
3107 */
3108 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003109 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 return FAIL;
3111
3112 /*
3113 * Check for the ":".
3114 */
3115 if ((*arg)[0] != ':')
3116 {
3117 EMSG(_("E109: Missing ':' after '?'"));
3118 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003119 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 return FAIL;
3121 }
3122
3123 /*
3124 * Get the third variable.
3125 */
3126 *arg = skipwhite(*arg + 1);
3127 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3128 {
3129 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003130 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 return FAIL;
3132 }
3133 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003134 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 }
3136
3137 return OK;
3138}
3139
3140/*
3141 * Handle first level expression:
3142 * expr2 || expr2 || expr2 logical OR
3143 *
3144 * "arg" must point to the first non-white of the expression.
3145 * "arg" is advanced to the next non-white after the recognized expression.
3146 *
3147 * Return OK or FAIL.
3148 */
3149 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003150eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003152 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 int evaluate;
3154{
Bram Moolenaar33570922005-01-25 22:26:29 +00003155 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 long result;
3157 int first;
3158
3159 /*
3160 * Get the first variable.
3161 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003162 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 return FAIL;
3164
3165 /*
3166 * Repeat until there is no following "||".
3167 */
3168 first = TRUE;
3169 result = FALSE;
3170 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3171 {
3172 if (evaluate && first)
3173 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003174 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003176 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 first = FALSE;
3178 }
3179
3180 /*
3181 * Get the second variable.
3182 */
3183 *arg = skipwhite(*arg + 2);
3184 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3185 return FAIL;
3186
3187 /*
3188 * Compute the result.
3189 */
3190 if (evaluate && !result)
3191 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003192 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003194 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 }
3196 if (evaluate)
3197 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003198 rettv->v_type = VAR_NUMBER;
3199 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 }
3201 }
3202
3203 return OK;
3204}
3205
3206/*
3207 * Handle second level expression:
3208 * expr3 && expr3 && expr3 logical AND
3209 *
3210 * "arg" must point to the first non-white of the expression.
3211 * "arg" is advanced to the next non-white after the recognized expression.
3212 *
3213 * Return OK or FAIL.
3214 */
3215 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003216eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003218 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 int evaluate;
3220{
Bram Moolenaar33570922005-01-25 22:26:29 +00003221 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 long result;
3223 int first;
3224
3225 /*
3226 * Get the first variable.
3227 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003228 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 return FAIL;
3230
3231 /*
3232 * Repeat until there is no following "&&".
3233 */
3234 first = TRUE;
3235 result = TRUE;
3236 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3237 {
3238 if (evaluate && first)
3239 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003240 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003242 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 first = FALSE;
3244 }
3245
3246 /*
3247 * Get the second variable.
3248 */
3249 *arg = skipwhite(*arg + 2);
3250 if (eval4(arg, &var2, evaluate && result) == FAIL)
3251 return FAIL;
3252
3253 /*
3254 * Compute the result.
3255 */
3256 if (evaluate && result)
3257 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003258 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003260 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 }
3262 if (evaluate)
3263 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003264 rettv->v_type = VAR_NUMBER;
3265 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 }
3267 }
3268
3269 return OK;
3270}
3271
3272/*
3273 * Handle third level expression:
3274 * var1 == var2
3275 * var1 =~ var2
3276 * var1 != var2
3277 * var1 !~ var2
3278 * var1 > var2
3279 * var1 >= var2
3280 * var1 < var2
3281 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003282 * var1 is var2
3283 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 *
3285 * "arg" must point to the first non-white of the expression.
3286 * "arg" is advanced to the next non-white after the recognized expression.
3287 *
3288 * Return OK or FAIL.
3289 */
3290 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003291eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003293 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 int evaluate;
3295{
Bram Moolenaar33570922005-01-25 22:26:29 +00003296 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 char_u *p;
3298 int i;
3299 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003300 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 int len = 2;
3302 long n1, n2;
3303 char_u *s1, *s2;
3304 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3305 regmatch_T regmatch;
3306 int ic;
3307 char_u *save_cpo;
3308
3309 /*
3310 * Get the first variable.
3311 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003312 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 return FAIL;
3314
3315 p = *arg;
3316 switch (p[0])
3317 {
3318 case '=': if (p[1] == '=')
3319 type = TYPE_EQUAL;
3320 else if (p[1] == '~')
3321 type = TYPE_MATCH;
3322 break;
3323 case '!': if (p[1] == '=')
3324 type = TYPE_NEQUAL;
3325 else if (p[1] == '~')
3326 type = TYPE_NOMATCH;
3327 break;
3328 case '>': if (p[1] != '=')
3329 {
3330 type = TYPE_GREATER;
3331 len = 1;
3332 }
3333 else
3334 type = TYPE_GEQUAL;
3335 break;
3336 case '<': if (p[1] != '=')
3337 {
3338 type = TYPE_SMALLER;
3339 len = 1;
3340 }
3341 else
3342 type = TYPE_SEQUAL;
3343 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003344 case 'i': if (p[1] == 's')
3345 {
3346 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3347 len = 5;
3348 if (!vim_isIDc(p[len]))
3349 {
3350 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3351 type_is = TRUE;
3352 }
3353 }
3354 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 }
3356
3357 /*
3358 * If there is a comparitive operator, use it.
3359 */
3360 if (type != TYPE_UNKNOWN)
3361 {
3362 /* extra question mark appended: ignore case */
3363 if (p[len] == '?')
3364 {
3365 ic = TRUE;
3366 ++len;
3367 }
3368 /* extra '#' appended: match case */
3369 else if (p[len] == '#')
3370 {
3371 ic = FALSE;
3372 ++len;
3373 }
3374 /* nothing appened: use 'ignorecase' */
3375 else
3376 ic = p_ic;
3377
3378 /*
3379 * Get the second variable.
3380 */
3381 *arg = skipwhite(p + len);
3382 if (eval5(arg, &var2, evaluate) == FAIL)
3383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003384 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 return FAIL;
3386 }
3387
3388 if (evaluate)
3389 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003390 if (type_is && rettv->v_type != var2.v_type)
3391 {
3392 /* For "is" a different type always means FALSE, for "notis"
3393 * it means TRUE. */
3394 n1 = (type == TYPE_NEQUAL);
3395 }
3396 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
3397 {
3398 if (type_is)
3399 {
3400 n1 = (rettv->v_type == var2.v_type
3401 && rettv->vval.v_list == var2.vval.v_list);
3402 if (type == TYPE_NEQUAL)
3403 n1 = !n1;
3404 }
3405 else if (rettv->v_type != var2.v_type
3406 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3407 {
3408 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003409 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003410 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003411 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003412 clear_tv(rettv);
3413 clear_tv(&var2);
3414 return FAIL;
3415 }
3416 else
3417 {
3418 /* Compare two Lists for being equal or unequal. */
3419 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
3420 if (type == TYPE_NEQUAL)
3421 n1 = !n1;
3422 }
3423 }
3424
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003425 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
3426 {
3427 if (type_is)
3428 {
3429 n1 = (rettv->v_type == var2.v_type
3430 && rettv->vval.v_dict == var2.vval.v_dict);
3431 if (type == TYPE_NEQUAL)
3432 n1 = !n1;
3433 }
3434 else if (rettv->v_type != var2.v_type
3435 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3436 {
3437 if (rettv->v_type != var2.v_type)
3438 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
3439 else
3440 EMSG(_("E736: Invalid operation for Dictionary"));
3441 clear_tv(rettv);
3442 clear_tv(&var2);
3443 return FAIL;
3444 }
3445 else
3446 {
3447 /* Compare two Dictionaries for being equal or unequal. */
3448 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict, ic);
3449 if (type == TYPE_NEQUAL)
3450 n1 = !n1;
3451 }
3452 }
3453
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003454 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
3455 {
3456 if (rettv->v_type != var2.v_type
3457 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
3458 {
3459 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003460 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003461 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003462 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003463 clear_tv(rettv);
3464 clear_tv(&var2);
3465 return FAIL;
3466 }
3467 else
3468 {
3469 /* Compare two Funcrefs for being equal or unequal. */
3470 if (rettv->vval.v_string == NULL
3471 || var2.vval.v_string == NULL)
3472 n1 = FALSE;
3473 else
3474 n1 = STRCMP(rettv->vval.v_string,
3475 var2.vval.v_string) == 0;
3476 if (type == TYPE_NEQUAL)
3477 n1 = !n1;
3478 }
3479 }
3480
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 /*
3482 * If one of the two variables is a number, compare as a number.
3483 * When using "=~" or "!~", always compare as string.
3484 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003485 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 && type != TYPE_MATCH && type != TYPE_NOMATCH)
3487 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003488 n1 = get_tv_number(rettv);
3489 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 switch (type)
3491 {
3492 case TYPE_EQUAL: n1 = (n1 == n2); break;
3493 case TYPE_NEQUAL: n1 = (n1 != n2); break;
3494 case TYPE_GREATER: n1 = (n1 > n2); break;
3495 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
3496 case TYPE_SMALLER: n1 = (n1 < n2); break;
3497 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
3498 case TYPE_UNKNOWN:
3499 case TYPE_MATCH:
3500 case TYPE_NOMATCH: break; /* avoid gcc warning */
3501 }
3502 }
3503 else
3504 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003505 s1 = get_tv_string_buf(rettv, buf1);
3506 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
3508 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
3509 else
3510 i = 0;
3511 n1 = FALSE;
3512 switch (type)
3513 {
3514 case TYPE_EQUAL: n1 = (i == 0); break;
3515 case TYPE_NEQUAL: n1 = (i != 0); break;
3516 case TYPE_GREATER: n1 = (i > 0); break;
3517 case TYPE_GEQUAL: n1 = (i >= 0); break;
3518 case TYPE_SMALLER: n1 = (i < 0); break;
3519 case TYPE_SEQUAL: n1 = (i <= 0); break;
3520
3521 case TYPE_MATCH:
3522 case TYPE_NOMATCH:
3523 /* avoid 'l' flag in 'cpoptions' */
3524 save_cpo = p_cpo;
3525 p_cpo = (char_u *)"";
3526 regmatch.regprog = vim_regcomp(s2,
3527 RE_MAGIC + RE_STRING);
3528 regmatch.rm_ic = ic;
3529 if (regmatch.regprog != NULL)
3530 {
3531 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3532 vim_free(regmatch.regprog);
3533 if (type == TYPE_NOMATCH)
3534 n1 = !n1;
3535 }
3536 p_cpo = save_cpo;
3537 break;
3538
3539 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3540 }
3541 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003542 clear_tv(rettv);
3543 clear_tv(&var2);
3544 rettv->v_type = VAR_NUMBER;
3545 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 }
3547 }
3548
3549 return OK;
3550}
3551
3552/*
3553 * Handle fourth level expression:
3554 * + number addition
3555 * - number subtraction
3556 * . string concatenation
3557 *
3558 * "arg" must point to the first non-white of the expression.
3559 * "arg" is advanced to the next non-white after the recognized expression.
3560 *
3561 * Return OK or FAIL.
3562 */
3563 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003564eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 int evaluate;
3568{
Bram Moolenaar33570922005-01-25 22:26:29 +00003569 typval_T var2;
3570 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 int op;
3572 long n1, n2;
3573 char_u *s1, *s2;
3574 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3575 char_u *p;
3576
3577 /*
3578 * Get the first variable.
3579 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003580 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 return FAIL;
3582
3583 /*
3584 * Repeat computing, until no '+', '-' or '.' is following.
3585 */
3586 for (;;)
3587 {
3588 op = **arg;
3589 if (op != '+' && op != '-' && op != '.')
3590 break;
3591
3592 /*
3593 * Get the second variable.
3594 */
3595 *arg = skipwhite(*arg + 1);
3596 if (eval6(arg, &var2, evaluate) == FAIL)
3597 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003598 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 return FAIL;
3600 }
3601
3602 if (evaluate)
3603 {
3604 /*
3605 * Compute the result.
3606 */
3607 if (op == '.')
3608 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003609 s1 = get_tv_string_buf(rettv, buf1);
3610 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003611 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003612 clear_tv(rettv);
3613 rettv->v_type = VAR_STRING;
3614 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003616 else if (op == '+' && rettv->v_type == VAR_LIST
3617 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003618 {
3619 /* concatenate Lists */
3620 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3621 &var3) == FAIL)
3622 {
3623 clear_tv(rettv);
3624 clear_tv(&var2);
3625 return FAIL;
3626 }
3627 clear_tv(rettv);
3628 *rettv = var3;
3629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 else
3631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003632 n1 = get_tv_number(rettv);
3633 n2 = get_tv_number(&var2);
3634 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 if (op == '+')
3636 n1 = n1 + n2;
3637 else
3638 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003639 rettv->v_type = VAR_NUMBER;
3640 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003642 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 }
3644 }
3645 return OK;
3646}
3647
3648/*
3649 * Handle fifth level expression:
3650 * * number multiplication
3651 * / number division
3652 * % number modulo
3653 *
3654 * "arg" must point to the first non-white of the expression.
3655 * "arg" is advanced to the next non-white after the recognized expression.
3656 *
3657 * Return OK or FAIL.
3658 */
3659 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003660eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003662 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 int evaluate;
3664{
Bram Moolenaar33570922005-01-25 22:26:29 +00003665 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 int op;
3667 long n1, n2;
3668
3669 /*
3670 * Get the first variable.
3671 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003672 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 return FAIL;
3674
3675 /*
3676 * Repeat computing, until no '*', '/' or '%' is following.
3677 */
3678 for (;;)
3679 {
3680 op = **arg;
3681 if (op != '*' && op != '/' && op != '%')
3682 break;
3683
3684 if (evaluate)
3685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003686 n1 = get_tv_number(rettv);
3687 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 }
3689 else
3690 n1 = 0;
3691
3692 /*
3693 * Get the second variable.
3694 */
3695 *arg = skipwhite(*arg + 1);
3696 if (eval7(arg, &var2, evaluate) == FAIL)
3697 return FAIL;
3698
3699 if (evaluate)
3700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003701 n2 = get_tv_number(&var2);
3702 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703
3704 /*
3705 * Compute the result.
3706 */
3707 if (op == '*')
3708 n1 = n1 * n2;
3709 else if (op == '/')
3710 {
3711 if (n2 == 0) /* give an error message? */
3712 n1 = 0x7fffffffL;
3713 else
3714 n1 = n1 / n2;
3715 }
3716 else
3717 {
3718 if (n2 == 0) /* give an error message? */
3719 n1 = 0;
3720 else
3721 n1 = n1 % n2;
3722 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003723 rettv->v_type = VAR_NUMBER;
3724 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 }
3726 }
3727
3728 return OK;
3729}
3730
3731/*
3732 * Handle sixth level expression:
3733 * number number constant
3734 * "string" string contstant
3735 * 'string' literal string contstant
3736 * &option-name option value
3737 * @r register contents
3738 * identifier variable value
3739 * function() function call
3740 * $VAR environment variable
3741 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003742 * [expr, expr] List
3743 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 *
3745 * Also handle:
3746 * ! in front logical NOT
3747 * - in front unary minus
3748 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003749 * trailing [] subscript in String or List
3750 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 *
3752 * "arg" must point to the first non-white of the expression.
3753 * "arg" is advanced to the next non-white after the recognized expression.
3754 *
3755 * Return OK or FAIL.
3756 */
3757 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003758eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 int evaluate;
3762{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 long n;
3764 int len;
3765 char_u *s;
3766 int val;
3767 char_u *start_leader, *end_leader;
3768 int ret = OK;
3769 char_u *alias;
3770
3771 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003772 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003773 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003775 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776
3777 /*
3778 * Skip '!' and '-' characters. They are handled later.
3779 */
3780 start_leader = *arg;
3781 while (**arg == '!' || **arg == '-' || **arg == '+')
3782 *arg = skipwhite(*arg + 1);
3783 end_leader = *arg;
3784
3785 switch (**arg)
3786 {
3787 /*
3788 * Number constant.
3789 */
3790 case '0':
3791 case '1':
3792 case '2':
3793 case '3':
3794 case '4':
3795 case '5':
3796 case '6':
3797 case '7':
3798 case '8':
3799 case '9':
3800 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
3801 *arg += len;
3802 if (evaluate)
3803 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003804 rettv->v_type = VAR_NUMBER;
3805 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 }
3807 break;
3808
3809 /*
3810 * String constant: "string".
3811 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003812 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 break;
3814
3815 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003816 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003818 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003819 break;
3820
3821 /*
3822 * List: [expr, expr]
3823 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003824 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 break;
3826
3827 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003828 * Dictionary: {key: val, key: val}
3829 */
3830 case '{': ret = get_dict_tv(arg, rettv, evaluate);
3831 break;
3832
3833 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003834 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003836 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 break;
3838
3839 /*
3840 * Environment variable: $VAR.
3841 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003842 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 break;
3844
3845 /*
3846 * Register contents: @r.
3847 */
3848 case '@': ++*arg;
3849 if (evaluate)
3850 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003851 rettv->v_type = VAR_STRING;
3852 rettv->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 }
3854 if (**arg != NUL)
3855 ++*arg;
3856 break;
3857
3858 /*
3859 * nested expression: (expression).
3860 */
3861 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003862 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 if (**arg == ')')
3864 ++*arg;
3865 else if (ret == OK)
3866 {
3867 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003868 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 ret = FAIL;
3870 }
3871 break;
3872
Bram Moolenaar8c711452005-01-14 21:53:12 +00003873 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 break;
3875 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003876
3877 if (ret == NOTDONE)
3878 {
3879 /*
3880 * Must be a variable or function name.
3881 * Can also be a curly-braces kind of name: {expr}.
3882 */
3883 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003884 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003885 if (alias != NULL)
3886 s = alias;
3887
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003888 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003889 ret = FAIL;
3890 else
3891 {
3892 if (**arg == '(') /* recursive! */
3893 {
3894 /* If "s" is the name of a variable of type VAR_FUNC
3895 * use its contents. */
3896 s = deref_func_name(s, &len);
3897
3898 /* Invoke the function. */
3899 ret = get_func_tv(s, len, rettv, arg,
3900 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00003901 &len, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003902 /* Stop the expression evaluation when immediately
3903 * aborting on error, or when an interrupt occurred or
3904 * an exception was thrown but not caught. */
3905 if (aborting())
3906 {
3907 if (ret == OK)
3908 clear_tv(rettv);
3909 ret = FAIL;
3910 }
3911 }
3912 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003913 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003914 else
3915 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003916 }
3917
3918 if (alias != NULL)
3919 vim_free(alias);
3920 }
3921
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 *arg = skipwhite(*arg);
3923
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003924 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
3925 * expr(expr). */
3926 if (ret == OK)
3927 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928
3929 /*
3930 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3931 */
3932 if (ret == OK && evaluate && end_leader > start_leader)
3933 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003934 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 while (end_leader > start_leader)
3936 {
3937 --end_leader;
3938 if (*end_leader == '!')
3939 val = !val;
3940 else if (*end_leader == '-')
3941 val = -val;
3942 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003943 clear_tv(rettv);
3944 rettv->v_type = VAR_NUMBER;
3945 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
3947
3948 return ret;
3949}
3950
3951/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003952 * Evaluate an "[expr]" or "[expr:expr]" index.
3953 * "*arg" points to the '['.
3954 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3955 */
3956 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003957eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003958 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00003959 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003960 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003961 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003962{
3963 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003964 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003965 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003966 long len = -1;
3967 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003968 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003969 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003970
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003971 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003972 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003973 if (verbose)
3974 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003975 return FAIL;
3976 }
3977
Bram Moolenaar8c711452005-01-14 21:53:12 +00003978 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003979 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003980 /*
3981 * dict.name
3982 */
3983 key = *arg + 1;
3984 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3985 ;
3986 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003987 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003988 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003989 }
3990 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003991 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003992 /*
3993 * something[idx]
3994 *
3995 * Get the (first) variable from inside the [].
3996 */
3997 *arg = skipwhite(*arg + 1);
3998 if (**arg == ':')
3999 empty1 = TRUE;
4000 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4001 return FAIL;
4002
4003 /*
4004 * Get the second variable from inside the [:].
4005 */
4006 if (**arg == ':')
4007 {
4008 range = TRUE;
4009 *arg = skipwhite(*arg + 1);
4010 if (**arg == ']')
4011 empty2 = TRUE;
4012 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4013 {
4014 clear_tv(&var1);
4015 return FAIL;
4016 }
4017 }
4018
4019 /* Check for the ']'. */
4020 if (**arg != ']')
4021 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004022 if (verbose)
4023 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004024 clear_tv(&var1);
4025 if (range)
4026 clear_tv(&var2);
4027 return FAIL;
4028 }
4029 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004030 }
4031
4032 if (evaluate)
4033 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004034 n1 = 0;
4035 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004036 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004037 n1 = get_tv_number(&var1);
4038 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004039 }
4040 if (range)
4041 {
4042 if (empty2)
4043 n2 = -1;
4044 else
4045 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004046 n2 = get_tv_number(&var2);
4047 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004048 }
4049 }
4050
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004051 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004052 {
4053 case VAR_NUMBER:
4054 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004055 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004056 len = (long)STRLEN(s);
4057 if (range)
4058 {
4059 /* The resulting variable is a substring. If the indexes
4060 * are out of range the result is empty. */
4061 if (n1 < 0)
4062 {
4063 n1 = len + n1;
4064 if (n1 < 0)
4065 n1 = 0;
4066 }
4067 if (n2 < 0)
4068 n2 = len + n2;
4069 else if (n2 >= len)
4070 n2 = len;
4071 if (n1 >= len || n2 < 0 || n1 > n2)
4072 s = NULL;
4073 else
4074 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4075 }
4076 else
4077 {
4078 /* The resulting variable is a string of a single
4079 * character. If the index is too big or negative the
4080 * result is empty. */
4081 if (n1 >= len || n1 < 0)
4082 s = NULL;
4083 else
4084 s = vim_strnsave(s + n1, 1);
4085 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004086 clear_tv(rettv);
4087 rettv->v_type = VAR_STRING;
4088 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004089 break;
4090
4091 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004092 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004093 if (n1 < 0)
4094 n1 = len + n1;
4095 if (!empty1 && (n1 < 0 || n1 >= len))
4096 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004097 if (verbose)
4098 EMSGN(_(e_listidx), n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004099 return FAIL;
4100 }
4101 if (range)
4102 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004103 list_T *l;
4104 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004105
4106 if (n2 < 0)
4107 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004108 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004109 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004110 if (verbose)
4111 EMSGN(_(e_listidx), n2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004112 return FAIL;
4113 }
4114 l = list_alloc();
4115 if (l == NULL)
4116 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004117 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004118 n1 <= n2; ++n1)
4119 {
4120 if (list_append_tv(l, &item->li_tv) == FAIL)
4121 {
4122 list_free(l);
4123 return FAIL;
4124 }
4125 item = item->li_next;
4126 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127 clear_tv(rettv);
4128 rettv->v_type = VAR_LIST;
4129 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00004130 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004131 }
4132 else
4133 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004134 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004135 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004136 clear_tv(rettv);
4137 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004138 }
4139 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004140
4141 case VAR_DICT:
4142 if (range)
4143 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004144 if (verbose)
4145 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004146 if (len == -1)
4147 clear_tv(&var1);
4148 return FAIL;
4149 }
4150 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004151 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004152
4153 if (len == -1)
4154 {
4155 key = get_tv_string(&var1);
4156 if (*key == NUL)
4157 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004158 if (verbose)
4159 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004160 clear_tv(&var1);
4161 return FAIL;
4162 }
4163 }
4164
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004165 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004166
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004167 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004168 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004169 if (len == -1)
4170 clear_tv(&var1);
4171 if (item == NULL)
4172 return FAIL;
4173
4174 copy_tv(&item->di_tv, &var1);
4175 clear_tv(rettv);
4176 *rettv = var1;
4177 }
4178 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004179 }
4180 }
4181
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004182 return OK;
4183}
4184
4185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 * Get an option value.
4187 * "arg" points to the '&' or '+' before the option name.
4188 * "arg" is advanced to character after the option name.
4189 * Return OK or FAIL.
4190 */
4191 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004194 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 int evaluate;
4196{
4197 char_u *option_end;
4198 long numval;
4199 char_u *stringval;
4200 int opt_type;
4201 int c;
4202 int working = (**arg == '+'); /* has("+option") */
4203 int ret = OK;
4204 int opt_flags;
4205
4206 /*
4207 * Isolate the option name and find its value.
4208 */
4209 option_end = find_option_end(arg, &opt_flags);
4210 if (option_end == NULL)
4211 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004212 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 EMSG2(_("E112: Option name missing: %s"), *arg);
4214 return FAIL;
4215 }
4216
4217 if (!evaluate)
4218 {
4219 *arg = option_end;
4220 return OK;
4221 }
4222
4223 c = *option_end;
4224 *option_end = NUL;
4225 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
4228 if (opt_type == -3) /* invalid name */
4229 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 EMSG2(_("E113: Unknown option: %s"), *arg);
4232 ret = FAIL;
4233 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 {
4236 if (opt_type == -2) /* hidden string option */
4237 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004238 rettv->v_type = VAR_STRING;
4239 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 }
4241 else if (opt_type == -1) /* hidden number option */
4242 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004243 rettv->v_type = VAR_NUMBER;
4244 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 }
4246 else if (opt_type == 1) /* number option */
4247 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004248 rettv->v_type = VAR_NUMBER;
4249 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 }
4251 else /* string option */
4252 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004253 rettv->v_type = VAR_STRING;
4254 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 }
4256 }
4257 else if (working && (opt_type == -2 || opt_type == -1))
4258 ret = FAIL;
4259
4260 *option_end = c; /* put back for error messages */
4261 *arg = option_end;
4262
4263 return ret;
4264}
4265
4266/*
4267 * Allocate a variable for a string constant.
4268 * Return OK or FAIL.
4269 */
4270 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004271get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 int evaluate;
4275{
4276 char_u *p;
4277 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 int extra = 0;
4279
4280 /*
4281 * Find the end of the string, skipping backslashed characters.
4282 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004283 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 {
4285 if (*p == '\\' && p[1] != NUL)
4286 {
4287 ++p;
4288 /* A "\<x>" form occupies at least 4 characters, and produces up
4289 * to 6 characters: reserve space for 2 extra */
4290 if (*p == '<')
4291 extra += 2;
4292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 }
4294
4295 if (*p != '"')
4296 {
4297 EMSG2(_("E114: Missing quote: %s"), *arg);
4298 return FAIL;
4299 }
4300
4301 /* If only parsing, set *arg and return here */
4302 if (!evaluate)
4303 {
4304 *arg = p + 1;
4305 return OK;
4306 }
4307
4308 /*
4309 * Copy the string into allocated memory, handling backslashed
4310 * characters.
4311 */
4312 name = alloc((unsigned)(p - *arg + extra));
4313 if (name == NULL)
4314 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004315 rettv->v_type = VAR_STRING;
4316 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317
Bram Moolenaar8c711452005-01-14 21:53:12 +00004318 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 {
4320 if (*p == '\\')
4321 {
4322 switch (*++p)
4323 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004324 case 'b': *name++ = BS; ++p; break;
4325 case 'e': *name++ = ESC; ++p; break;
4326 case 'f': *name++ = FF; ++p; break;
4327 case 'n': *name++ = NL; ++p; break;
4328 case 'r': *name++ = CAR; ++p; break;
4329 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330
4331 case 'X': /* hex: "\x1", "\x12" */
4332 case 'x':
4333 case 'u': /* Unicode: "\u0023" */
4334 case 'U':
4335 if (vim_isxdigit(p[1]))
4336 {
4337 int n, nr;
4338 int c = toupper(*p);
4339
4340 if (c == 'X')
4341 n = 2;
4342 else
4343 n = 4;
4344 nr = 0;
4345 while (--n >= 0 && vim_isxdigit(p[1]))
4346 {
4347 ++p;
4348 nr = (nr << 4) + hex2nr(*p);
4349 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004350 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351#ifdef FEAT_MBYTE
4352 /* For "\u" store the number according to
4353 * 'encoding'. */
4354 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00004355 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 else
4357#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00004358 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 break;
4361
4362 /* octal: "\1", "\12", "\123" */
4363 case '0':
4364 case '1':
4365 case '2':
4366 case '3':
4367 case '4':
4368 case '5':
4369 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00004370 case '7': *name = *p++ - '0';
4371 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004373 *name = (*name << 3) + *p++ - '0';
4374 if (*p >= '0' && *p <= '7')
4375 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004377 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 break;
4379
4380 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00004381 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 if (extra != 0)
4383 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004384 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 break;
4386 }
4387 /* FALLTHROUGH */
4388
Bram Moolenaar8c711452005-01-14 21:53:12 +00004389 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 break;
4391 }
4392 }
4393 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004394 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004397 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 *arg = p + 1;
4399
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 return OK;
4401}
4402
4403/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004404 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 * Return OK or FAIL.
4406 */
4407 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004408get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004410 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 int evaluate;
4412{
4413 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004414 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004415 int reduce = 0;
4416
4417 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004418 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004419 */
4420 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
4421 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004422 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004423 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004424 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004425 break;
4426 ++reduce;
4427 ++p;
4428 }
4429 }
4430
Bram Moolenaar8c711452005-01-14 21:53:12 +00004431 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004432 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004433 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004434 return FAIL;
4435 }
4436
Bram Moolenaar8c711452005-01-14 21:53:12 +00004437 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004438 if (!evaluate)
4439 {
4440 *arg = p + 1;
4441 return OK;
4442 }
4443
4444 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004445 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004446 */
4447 str = alloc((unsigned)((p - *arg) - reduce));
4448 if (str == NULL)
4449 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004450 rettv->v_type = VAR_STRING;
4451 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004452
Bram Moolenaar8c711452005-01-14 21:53:12 +00004453 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004454 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004455 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004456 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004457 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004458 break;
4459 ++p;
4460 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004461 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004462 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004463 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004464 *arg = p + 1;
4465
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004466 return OK;
4467}
4468
4469/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004470 * Allocate a variable for a List and fill it from "*arg".
4471 * Return OK or FAIL.
4472 */
4473 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004474get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004475 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004476 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004477 int evaluate;
4478{
Bram Moolenaar33570922005-01-25 22:26:29 +00004479 list_T *l = NULL;
4480 typval_T tv;
4481 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004482
4483 if (evaluate)
4484 {
4485 l = list_alloc();
4486 if (l == NULL)
4487 return FAIL;
4488 }
4489
4490 *arg = skipwhite(*arg + 1);
4491 while (**arg != ']' && **arg != NUL)
4492 {
4493 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4494 goto failret;
4495 if (evaluate)
4496 {
4497 item = listitem_alloc();
4498 if (item != NULL)
4499 {
4500 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004501 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004502 list_append(l, item);
4503 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004504 else
4505 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004506 }
4507
4508 if (**arg == ']')
4509 break;
4510 if (**arg != ',')
4511 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004512 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004513 goto failret;
4514 }
4515 *arg = skipwhite(*arg + 1);
4516 }
4517
4518 if (**arg != ']')
4519 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004520 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004521failret:
4522 if (evaluate)
4523 list_free(l);
4524 return FAIL;
4525 }
4526
4527 *arg = skipwhite(*arg + 1);
4528 if (evaluate)
4529 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004530 rettv->v_type = VAR_LIST;
4531 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004532 ++l->lv_refcount;
4533 }
4534
4535 return OK;
4536}
4537
4538/*
4539 * Allocate an empty header for a list.
4540 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004541 static list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004542list_alloc()
4543{
Bram Moolenaar33570922005-01-25 22:26:29 +00004544 return (list_T *)alloc_clear(sizeof(list_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004545}
4546
4547/*
4548 * Unreference a list: decrement the reference count and free it when it
4549 * becomes zero.
4550 */
4551 static void
4552list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004553 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004554{
4555 if (l != NULL && --l->lv_refcount <= 0)
4556 list_free(l);
4557}
4558
4559/*
4560 * Free a list, including all items it points to.
4561 * Ignores the reference count.
4562 */
4563 static void
4564list_free(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004565 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004566{
Bram Moolenaar33570922005-01-25 22:26:29 +00004567 listitem_T *item;
4568 listitem_T *next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004569
4570 for (item = l->lv_first; item != NULL; item = next)
4571 {
4572 next = item->li_next;
4573 listitem_free(item);
4574 }
4575 vim_free(l);
4576}
4577
4578/*
4579 * Allocate a list item.
4580 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004581 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004582listitem_alloc()
4583{
Bram Moolenaar33570922005-01-25 22:26:29 +00004584 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004585}
4586
4587/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004588 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004589 */
4590 static void
4591listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004592 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004593{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004594 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004595 vim_free(item);
4596}
4597
4598/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004599 * Remove a list item from a List and free it. Also clears the value.
4600 */
4601 static void
4602listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004603 list_T *l;
4604 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00004605{
4606 list_remove(l, item, item);
4607 listitem_free(item);
4608}
4609
4610/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004611 * Get the number of items in a list.
4612 */
4613 static long
4614list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00004615 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004616{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004617 if (l == NULL)
4618 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004619 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004620}
4621
4622/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004623 * Return TRUE when two lists have exactly the same values.
4624 */
4625 static int
4626list_equal(l1, l2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004627 list_T *l1;
4628 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004629 int ic; /* ignore case for strings */
4630{
Bram Moolenaar33570922005-01-25 22:26:29 +00004631 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004632
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004633 if (list_len(l1) != list_len(l2))
4634 return FALSE;
4635
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004636 for (item1 = l1->lv_first, item2 = l2->lv_first;
4637 item1 != NULL && item2 != NULL;
4638 item1 = item1->li_next, item2 = item2->li_next)
4639 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
4640 return FALSE;
4641 return item1 == NULL && item2 == NULL;
4642}
4643
4644/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004645 * Return TRUE when two dictionaries have exactly the same key/values.
4646 */
4647 static int
4648dict_equal(d1, d2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004649 dict_T *d1;
4650 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004651 int ic; /* ignore case for strings */
4652{
Bram Moolenaar33570922005-01-25 22:26:29 +00004653 hashitem_T *hi;
4654 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004655 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004656
4657 if (dict_len(d1) != dict_len(d2))
4658 return FALSE;
4659
Bram Moolenaar33570922005-01-25 22:26:29 +00004660 todo = d1->dv_hashtab.ht_used;
4661 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004662 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004663 if (!HASHITEM_EMPTY(hi))
4664 {
4665 item2 = dict_find(d2, hi->hi_key, -1);
4666 if (item2 == NULL)
4667 return FALSE;
4668 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic))
4669 return FALSE;
4670 --todo;
4671 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004672 }
4673 return TRUE;
4674}
4675
4676/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004677 * Return TRUE if "tv1" and "tv2" have the same value.
4678 * Compares the items just like "==" would compare them.
4679 */
4680 static int
4681tv_equal(tv1, tv2, ic)
Bram Moolenaar33570922005-01-25 22:26:29 +00004682 typval_T *tv1;
4683 typval_T *tv2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004684 int ic; /* ignore case */
4685{
4686 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4687
4688 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
4689 {
4690 /* recursive! */
4691 if (tv1->v_type != tv2->v_type
4692 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
4693 return FALSE;
4694 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004695 else if (tv1->v_type == VAR_DICT || tv2->v_type == VAR_DICT)
4696 {
4697 /* recursive! */
4698 if (tv1->v_type != tv2->v_type
4699 || !dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic))
4700 return FALSE;
4701 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004702 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
4703 {
4704 if (tv1->v_type != tv2->v_type
4705 || tv1->vval.v_string == NULL
4706 || tv2->vval.v_string == NULL
4707 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
4708 return FALSE;
4709 }
4710 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
4711 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004712 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
4713 * Don't consider "4x" to be equal to 4. */
4714 if ((tv1->v_type == VAR_STRING
4715 && !string_isa_number(tv1->vval.v_string))
4716 || (tv2->v_type == VAR_STRING
4717 && !string_isa_number(tv2->vval.v_string)))
4718 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004719 if (get_tv_number(tv1) != get_tv_number(tv2))
4720 return FALSE;
4721 }
4722 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
4723 get_tv_string_buf(tv2, buf2)) != 0)
4724 return FALSE;
4725 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
4726 get_tv_string_buf(tv2, buf2)) != 0)
4727 return FALSE;
4728 return TRUE;
4729}
4730
4731/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004732 * Return TRUE if "tv" is a number without other non-white characters.
4733 */
4734 static int
4735string_isa_number(s)
4736 char_u *s;
4737{
4738 int len;
4739
4740 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
4741 return len > 0 && *skipwhite(s + len) == NUL;
4742}
4743
4744/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004745 * Locate item with index "n" in list "l" and return it.
4746 * A negative index is counted from the end; -1 is the last item.
4747 * Returns NULL when "n" is out of range.
4748 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004749 static listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004750list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00004751 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004752 long n;
4753{
Bram Moolenaar33570922005-01-25 22:26:29 +00004754 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004755 long idx;
4756
4757 if (l == NULL)
4758 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004759
4760 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004761 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00004762 n = l->lv_len + n;
4763
4764 /* Check for index out of range. */
4765 if (n < 0 || n >= l->lv_len)
4766 return NULL;
4767
4768 /* When there is a cached index may start search from there. */
4769 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004770 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00004771 if (n < l->lv_idx / 2)
4772 {
4773 /* closest to the start of the list */
4774 item = l->lv_first;
4775 idx = 0;
4776 }
4777 else if (n > (l->lv_idx + l->lv_len) / 2)
4778 {
4779 /* closest to the end of the list */
4780 item = l->lv_last;
4781 idx = l->lv_len - 1;
4782 }
4783 else
4784 {
4785 /* closest to the cached index */
4786 item = l->lv_idx_item;
4787 idx = l->lv_idx;
4788 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004789 }
4790 else
4791 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00004792 if (n < l->lv_len / 2)
4793 {
4794 /* closest to the start of the list */
4795 item = l->lv_first;
4796 idx = 0;
4797 }
4798 else
4799 {
4800 /* closest to the end of the list */
4801 item = l->lv_last;
4802 idx = l->lv_len - 1;
4803 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004804 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00004805
4806 while (n > idx)
4807 {
4808 /* search forward */
4809 item = item->li_next;
4810 ++idx;
4811 }
4812 while (n < idx)
4813 {
4814 /* search backward */
4815 item = item->li_prev;
4816 --idx;
4817 }
4818
4819 /* cache the used index */
4820 l->lv_idx = idx;
4821 l->lv_idx_item = item;
4822
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004823 return item;
4824}
4825
4826/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004827 * Locate "item" list "l" and return its index.
4828 * Returns -1 when "item" is not in the list.
4829 */
4830 static long
4831list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004832 list_T *l;
4833 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004834{
4835 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00004836 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004837
4838 if (l == NULL)
4839 return -1;
4840 idx = 0;
4841 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
4842 ++idx;
4843 if (li == NULL)
4844 return -1;
4845 return idx;;
4846}
4847
4848/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004849 * Append item "item" to the end of list "l".
4850 */
4851 static void
4852list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004853 list_T *l;
4854 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004855{
4856 if (l->lv_last == NULL)
4857 {
4858 /* empty list */
4859 l->lv_first = item;
4860 l->lv_last = item;
4861 item->li_prev = NULL;
4862 }
4863 else
4864 {
4865 l->lv_last->li_next = item;
4866 item->li_prev = l->lv_last;
4867 l->lv_last = item;
4868 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00004869 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004870 item->li_next = NULL;
4871}
4872
4873/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004874 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004875 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004876 */
4877 static int
4878list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00004879 list_T *l;
4880 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004881{
Bram Moolenaar33570922005-01-25 22:26:29 +00004882 listitem_T *ni = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004883
4884 if (ni == NULL)
4885 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004886 copy_tv(tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004887 list_append(l, ni);
4888 return OK;
4889}
4890
4891/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004892 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004893 * If "item" is NULL append at the end.
4894 * Return FAIL when out of memory.
4895 */
4896 static int
4897list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00004898 list_T *l;
4899 typval_T *tv;
4900 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004901{
Bram Moolenaar33570922005-01-25 22:26:29 +00004902 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004903
4904 if (ni == NULL)
4905 return FAIL;
4906 copy_tv(tv, &ni->li_tv);
4907 if (item == NULL)
4908 /* Append new item at end of list. */
4909 list_append(l, ni);
4910 else
4911 {
4912 /* Insert new item before existing item. */
4913 ni->li_prev = item->li_prev;
4914 ni->li_next = item;
4915 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00004916 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004917 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004918 ++l->lv_idx;
4919 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004920 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00004921 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004922 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004923 l->lv_idx_item = NULL;
4924 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004925 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00004926 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004927 }
4928 return OK;
4929}
4930
4931/*
4932 * Extend "l1" with "l2".
4933 * If "bef" is NULL append at the end, otherwise insert before this item.
4934 * Returns FAIL when out of memory.
4935 */
4936 static int
4937list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00004938 list_T *l1;
4939 list_T *l2;
4940 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004941{
Bram Moolenaar33570922005-01-25 22:26:29 +00004942 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004943
4944 for (item = l2->lv_first; item != NULL; item = item->li_next)
4945 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
4946 return FAIL;
4947 return OK;
4948}
4949
4950/*
4951 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
4952 * Return FAIL when out of memory.
4953 */
4954 static int
4955list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00004956 list_T *l1;
4957 list_T *l2;
4958 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004959{
Bram Moolenaar33570922005-01-25 22:26:29 +00004960 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004961
4962 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004963 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004964 if (l == NULL)
4965 return FAIL;
4966 tv->v_type = VAR_LIST;
4967 tv->vval.v_list = l;
4968
4969 /* append all items from the second list */
4970 return list_extend(l, l2, NULL);
4971}
4972
4973/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004974 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004975 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004976 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004977 * Returns NULL when out of memory.
4978 */
Bram Moolenaar33570922005-01-25 22:26:29 +00004979 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004980list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00004981 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004982 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004983 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004984{
Bram Moolenaar33570922005-01-25 22:26:29 +00004985 list_T *copy;
4986 listitem_T *item;
4987 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004988
4989 if (orig == NULL)
4990 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004991
4992 copy = list_alloc();
4993 if (copy != NULL)
4994 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00004995 if (copyID != 0)
4996 {
4997 /* Do this before adding the items, because one of the items may
4998 * refer back to this list. */
4999 orig->lv_copyID = copyID;
5000 orig->lv_copylist = copy;
5001 }
5002 for (item = orig->lv_first; item != NULL && !got_int;
5003 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005004 {
5005 ni = listitem_alloc();
5006 if (ni == NULL)
5007 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005008 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005009 {
5010 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
5011 {
5012 vim_free(ni);
5013 break;
5014 }
5015 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005016 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005017 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005018 list_append(copy, ni);
5019 }
5020 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005021 if (item != NULL)
5022 {
5023 list_unref(copy);
5024 copy = NULL;
5025 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005026 }
5027
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005028 return copy;
5029}
5030
5031/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005032 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005033 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005034 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005035 static void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005036list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00005037 list_T *l;
5038 listitem_T *item;
5039 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005040{
Bram Moolenaar33570922005-01-25 22:26:29 +00005041 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005042
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005043 /* notify watchers */
5044 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005045 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00005046 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005047 list_fix_watch(l, ip);
5048 if (ip == item2)
5049 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005050 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005051
5052 if (item2->li_next == NULL)
5053 l->lv_last = item->li_prev;
5054 else
5055 item2->li_next->li_prev = item->li_prev;
5056 if (item->li_prev == NULL)
5057 l->lv_first = item2->li_next;
5058 else
5059 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005060 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005061}
5062
5063/*
5064 * Return an allocated string with the string representation of a list.
5065 * May return NULL.
5066 */
5067 static char_u *
5068list2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005069 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005070{
5071 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005072
5073 if (tv->vval.v_list == NULL)
5074 return NULL;
5075 ga_init2(&ga, (int)sizeof(char), 80);
5076 ga_append(&ga, '[');
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005077 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE) == FAIL)
5078 {
5079 vim_free(ga.ga_data);
5080 return NULL;
5081 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005082 ga_append(&ga, ']');
5083 ga_append(&ga, NUL);
5084 return (char_u *)ga.ga_data;
5085}
5086
5087/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005088 * Join list "l" into a string in "*gap", using separator "sep".
5089 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005090 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005091 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005092 static int
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005093list_join(gap, l, sep, echo)
5094 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00005095 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005096 char_u *sep;
5097 int echo;
5098{
5099 int first = TRUE;
5100 char_u *tofree;
5101 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005102 listitem_T *item;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005103 char_u *s;
5104
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005105 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005106 {
5107 if (first)
5108 first = FALSE;
5109 else
5110 ga_concat(gap, sep);
5111
5112 if (echo)
5113 s = echo_string(&item->li_tv, &tofree, numbuf);
5114 else
5115 s = tv2string(&item->li_tv, &tofree, numbuf);
5116 if (s != NULL)
5117 ga_concat(gap, s);
5118 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005119 if (s == NULL)
5120 return FAIL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005121 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005122 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005123}
5124
5125/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005126 * Allocate an empty header for a dictionary.
5127 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005128 static dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00005129dict_alloc()
5130{
Bram Moolenaar33570922005-01-25 22:26:29 +00005131 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005132
Bram Moolenaar33570922005-01-25 22:26:29 +00005133 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005134 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005135 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005136 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005137 d->dv_lock = 0;
5138 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005139 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005140 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005141 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142}
5143
5144/*
5145 * Unreference a Dictionary: decrement the reference count and free it when it
5146 * becomes zero.
5147 */
5148 static void
5149dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005150 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005151{
5152 if (d != NULL && --d->dv_refcount <= 0)
5153 dict_free(d);
5154}
5155
5156/*
5157 * Free a Dictionary, including all items it contains.
5158 * Ignores the reference count.
5159 */
5160 static void
5161dict_free(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005162 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005164 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005165 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005167 /* Careful: we free the dictitems while they still appear in the
Bram Moolenaar33570922005-01-25 22:26:29 +00005168 * hashtab. Must not try to resize the hashtab! */
5169 todo = d->dv_hashtab.ht_used;
5170 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005171 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005172 if (!HASHITEM_EMPTY(hi))
5173 {
5174 dictitem_free(HI2DI(hi));
5175 --todo;
5176 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005178 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005179 vim_free(d);
5180}
5181
5182/*
5183 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005184 * The "key" is copied to the new item.
5185 * Note that the value of the item "di_tv" still needs to be initialized!
5186 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005187 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005188 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005189dictitem_alloc(key)
5190 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005191{
Bram Moolenaar33570922005-01-25 22:26:29 +00005192 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005193
Bram Moolenaar33570922005-01-25 22:26:29 +00005194 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005195 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005196 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005197 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005198 di->di_flags = 0;
5199 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005200 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005201}
5202
5203/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005204 * Make a copy of a Dictionary item.
5205 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005206 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00005207dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00005208 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005209{
Bram Moolenaar33570922005-01-25 22:26:29 +00005210 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005211
Bram Moolenaar33570922005-01-25 22:26:29 +00005212 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005213 if (di != NULL)
5214 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005215 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005216 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005217 copy_tv(&org->di_tv, &di->di_tv);
5218 }
5219 return di;
5220}
5221
5222/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005223 * Remove item "item" from Dictionary "dict" and free it.
5224 */
5225 static void
5226dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005227 dict_T *dict;
5228 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005229{
Bram Moolenaar33570922005-01-25 22:26:29 +00005230 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005231
Bram Moolenaar33570922005-01-25 22:26:29 +00005232 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005233 if (HASHITEM_EMPTY(hi))
5234 EMSG2(_(e_intern2), "dictitem_remove()");
5235 else
Bram Moolenaar33570922005-01-25 22:26:29 +00005236 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005237 dictitem_free(item);
5238}
5239
5240/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005241 * Free a dict item. Also clears the value.
5242 */
5243 static void
5244dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005245 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005246{
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247 clear_tv(&item->di_tv);
5248 vim_free(item);
5249}
5250
5251/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005252 * Make a copy of dict "d". Shallow if "deep" is FALSE.
5253 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005254 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00005255 * Returns NULL when out of memory.
5256 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005257 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005258dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00005259 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005260 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005261 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005262{
Bram Moolenaar33570922005-01-25 22:26:29 +00005263 dict_T *copy;
5264 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005265 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00005266 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005267
5268 if (orig == NULL)
5269 return NULL;
5270
5271 copy = dict_alloc();
5272 if (copy != NULL)
5273 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005274 if (copyID != 0)
5275 {
5276 orig->dv_copyID = copyID;
5277 orig->dv_copydict = copy;
5278 }
Bram Moolenaar33570922005-01-25 22:26:29 +00005279 todo = orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005280 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005281 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005282 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00005283 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005284 --todo;
5285
5286 di = dictitem_alloc(hi->hi_key);
5287 if (di == NULL)
5288 break;
5289 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005290 {
5291 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
5292 copyID) == FAIL)
5293 {
5294 vim_free(di);
5295 break;
5296 }
5297 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005298 else
5299 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
5300 if (dict_add(copy, di) == FAIL)
5301 {
5302 dictitem_free(di);
5303 break;
5304 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005305 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005306 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005307
Bram Moolenaare9a41262005-01-15 22:18:47 +00005308 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005309 if (todo > 0)
5310 {
5311 dict_unref(copy);
5312 copy = NULL;
5313 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005314 }
5315
5316 return copy;
5317}
5318
5319/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005320 * Add item "item" to Dictionary "d".
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005321 * Returns FAIL when out of memory and when key already existed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005322 */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005323 static int
Bram Moolenaar8c711452005-01-14 21:53:12 +00005324dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005325 dict_T *d;
5326 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005327{
Bram Moolenaar33570922005-01-25 22:26:29 +00005328 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005329}
5330
Bram Moolenaar8c711452005-01-14 21:53:12 +00005331/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005332 * Get the number of items in a Dictionary.
5333 */
5334 static long
5335dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00005336 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005337{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005338 if (d == NULL)
5339 return 0L;
Bram Moolenaar33570922005-01-25 22:26:29 +00005340 return d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005341}
5342
5343/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005344 * Find item "key[len]" in Dictionary "d".
5345 * If "len" is negative use strlen(key).
5346 * Returns NULL when not found.
5347 */
Bram Moolenaar33570922005-01-25 22:26:29 +00005348 static dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005349dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00005350 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005351 char_u *key;
5352 int len;
5353{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005354#define AKEYLEN 200
5355 char_u buf[AKEYLEN];
5356 char_u *akey;
5357 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00005358 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005359
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005360 if (len < 0)
5361 akey = key;
5362 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005363 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005364 tofree = akey = vim_strnsave(key, len);
5365 if (akey == NULL)
5366 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005367 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005368 else
5369 {
5370 /* Avoid a malloc/free by using buf[]. */
5371 STRNCPY(buf, key, len);
5372 buf[len] = NUL;
5373 akey = buf;
5374 }
5375
Bram Moolenaar33570922005-01-25 22:26:29 +00005376 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005377 vim_free(tofree);
5378 if (HASHITEM_EMPTY(hi))
5379 return NULL;
5380 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005381}
5382
5383/*
5384 * Return an allocated string with the string representation of a Dictionary.
5385 * May return NULL.
5386 */
5387 static char_u *
5388dict2string(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00005389 typval_T *tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005390{
5391 garray_T ga;
5392 int first = TRUE;
5393 char_u *tofree;
5394 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00005395 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005396 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00005397 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005398 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005399
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005400 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005401 return NULL;
5402 ga_init2(&ga, (int)sizeof(char), 80);
5403 ga_append(&ga, '{');
5404
Bram Moolenaar33570922005-01-25 22:26:29 +00005405 todo = d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005406 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005407 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005408 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00005409 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005410 --todo;
5411
5412 if (first)
5413 first = FALSE;
5414 else
5415 ga_concat(&ga, (char_u *)", ");
5416
5417 tofree = string_quote(hi->hi_key, FALSE);
5418 if (tofree != NULL)
5419 {
5420 ga_concat(&ga, tofree);
5421 vim_free(tofree);
5422 }
5423 ga_concat(&ga, (char_u *)": ");
5424 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf);
5425 if (s != NULL)
5426 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005427 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005428 if (s == NULL)
5429 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005430 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005431 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005432 if (todo > 0)
5433 {
5434 vim_free(ga.ga_data);
5435 return NULL;
5436 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005437
5438 ga_append(&ga, '}');
5439 ga_append(&ga, NUL);
5440 return (char_u *)ga.ga_data;
5441}
5442
5443/*
5444 * Allocate a variable for a Dictionary and fill it from "*arg".
5445 * Return OK or FAIL. Returns NOTDONE for {expr}.
5446 */
5447 static int
5448get_dict_tv(arg, rettv, evaluate)
5449 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005450 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005451 int evaluate;
5452{
Bram Moolenaar33570922005-01-25 22:26:29 +00005453 dict_T *d = NULL;
5454 typval_T tvkey;
5455 typval_T tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005456 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +00005457 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005458 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005459 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00005460
5461 /*
5462 * First check if it's not a curly-braces thing: {expr}.
5463 * Must do this without evaluating, otherwise a function may be called
5464 * twice. Unfortunately this means we need to call eval1() twice for the
5465 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005466 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005467 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005468 if (*start != '}')
5469 {
5470 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
5471 return FAIL;
5472 if (*start == '}')
5473 return NOTDONE;
5474 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005475
5476 if (evaluate)
5477 {
5478 d = dict_alloc();
5479 if (d == NULL)
5480 return FAIL;
5481 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005482 tvkey.v_type = VAR_UNKNOWN;
5483 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005484
5485 *arg = skipwhite(*arg + 1);
5486 while (**arg != '}' && **arg != NUL)
5487 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005488 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005489 goto failret;
5490 if (**arg != ':')
5491 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005492 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005493 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005494 goto failret;
5495 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005496 key = get_tv_string_buf(&tvkey, buf);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005497 if (*key == NUL)
5498 {
5499 EMSG(_(e_emptykey));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005500 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005501 goto failret;
5502 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005503
5504 *arg = skipwhite(*arg + 1);
5505 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5506 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005507 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005508 goto failret;
5509 }
5510 if (evaluate)
5511 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005512 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005513 if (item != NULL)
5514 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005515 EMSG(_("E721: Duplicate key in Dictionary"));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005516 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005517 clear_tv(&tv);
5518 goto failret;
5519 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005520 item = dictitem_alloc(key);
5521 clear_tv(&tvkey);
5522 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005523 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005524 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005525 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005526 if (dict_add(d, item) == FAIL)
5527 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005528 }
5529 }
5530
5531 if (**arg == '}')
5532 break;
5533 if (**arg != ',')
5534 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005535 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005536 goto failret;
5537 }
5538 *arg = skipwhite(*arg + 1);
5539 }
5540
5541 if (**arg != '}')
5542 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005543 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005544failret:
5545 if (evaluate)
5546 dict_free(d);
5547 return FAIL;
5548 }
5549
5550 *arg = skipwhite(*arg + 1);
5551 if (evaluate)
5552 {
5553 rettv->v_type = VAR_DICT;
5554 rettv->vval.v_dict = d;
5555 ++d->dv_refcount;
5556 }
5557
5558 return OK;
5559}
5560
Bram Moolenaar8c711452005-01-14 21:53:12 +00005561/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005562 * Return a string with the string representation of a variable.
5563 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005564 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005565 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005566 * May return NULL;
5567 */
5568 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005569echo_string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005570 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005572 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005573{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005574 static int recurse = 0;
5575 char_u *r = NULL;
5576
Bram Moolenaar33570922005-01-25 22:26:29 +00005577 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005578 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005579 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00005580 *tofree = NULL;
5581 return NULL;
5582 }
5583 ++recurse;
5584
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005585 switch (tv->v_type)
5586 {
5587 case VAR_FUNC:
5588 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005589 r = tv->vval.v_string;
5590 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005591 case VAR_LIST:
5592 *tofree = list2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005593 r = *tofree;
5594 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005595 case VAR_DICT:
5596 *tofree = dict2string(tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00005597 r = *tofree;
5598 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005599 case VAR_STRING:
5600 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005601 *tofree = NULL;
5602 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005603 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005604 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005605 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00005606 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005607 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005608
5609 --recurse;
5610 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005611}
5612
5613/*
5614 * Return a string with the string representation of a variable.
5615 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5616 * "numbuf" is used for a number.
5617 * Puts quotes around strings, so that they can be parsed back by eval().
5618 * May return NULL;
5619 */
5620 static char_u *
5621tv2string(tv, tofree, numbuf)
Bram Moolenaar33570922005-01-25 22:26:29 +00005622 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005623 char_u **tofree;
5624 char_u *numbuf;
5625{
5626 switch (tv->v_type)
5627 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005628 case VAR_FUNC:
5629 *tofree = string_quote(tv->vval.v_string, TRUE);
5630 return *tofree;
5631 case VAR_STRING:
5632 *tofree = string_quote(tv->vval.v_string, FALSE);
5633 return *tofree;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005634 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005635 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00005636 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005637 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005638 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005639 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005640 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005641 return echo_string(tv, tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005642}
5643
5644/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005645 * Return string "str" in ' quotes, doubling ' characters.
5646 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00005647 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005648 */
5649 static char_u *
5650string_quote(str, function)
5651 char_u *str;
5652 int function;
5653{
Bram Moolenaar33570922005-01-25 22:26:29 +00005654 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005655 char_u *p, *r, *s;
5656
Bram Moolenaar33570922005-01-25 22:26:29 +00005657 len = (function ? 13 : 3);
5658 if (str != NULL)
5659 {
5660 len += STRLEN(str);
5661 for (p = str; *p != NUL; mb_ptr_adv(p))
5662 if (*p == '\'')
5663 ++len;
5664 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005665 s = r = alloc(len);
5666 if (r != NULL)
5667 {
5668 if (function)
5669 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005670 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005671 r += 10;
5672 }
5673 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005674 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00005675 if (str != NULL)
5676 for (p = str; *p != NUL; )
5677 {
5678 if (*p == '\'')
5679 *r++ = '\'';
5680 MB_COPY_CHAR(p, r);
5681 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005683 if (function)
5684 *r++ = ')';
5685 *r++ = NUL;
5686 }
5687 return s;
5688}
5689
5690/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 * Get the value of an environment variable.
5692 * "arg" is pointing to the '$'. It is advanced to after the name.
5693 * If the environment variable was not set, silently assume it is empty.
5694 * Always return OK.
5695 */
5696 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005697get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005699 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 int evaluate;
5701{
5702 char_u *string = NULL;
5703 int len;
5704 int cc;
5705 char_u *name;
5706
5707 ++*arg;
5708 name = *arg;
5709 len = get_env_len(arg);
5710 if (evaluate)
5711 {
5712 if (len != 0)
5713 {
5714 cc = name[len];
5715 name[len] = NUL;
5716 /* first try mch_getenv(), fast for normal environment vars */
5717 string = mch_getenv(name);
5718 if (string != NULL && *string != NUL)
5719 string = vim_strsave(string);
5720 else
5721 {
5722 /* next try expanding things like $VIM and ${HOME} */
5723 string = expand_env_save(name - 1);
5724 if (string != NULL && *string == '$')
5725 {
5726 vim_free(string);
5727 string = NULL;
5728 }
5729 }
5730 name[len] = cc;
5731 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005732 rettv->v_type = VAR_STRING;
5733 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 }
5735
5736 return OK;
5737}
5738
5739/*
5740 * Array with names and number of arguments of all internal functions
5741 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
5742 */
5743static struct fst
5744{
5745 char *f_name; /* function name */
5746 char f_min_argc; /* minimal number of arguments */
5747 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00005748 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005749 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750} functions[] =
5751{
Bram Moolenaar0d660222005-01-07 21:51:51 +00005752 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {"append", 2, 2, f_append},
5754 {"argc", 0, 0, f_argc},
5755 {"argidx", 0, 0, f_argidx},
5756 {"argv", 1, 1, f_argv},
5757 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005758 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 {"bufexists", 1, 1, f_bufexists},
5760 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
5761 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
5762 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
5763 {"buflisted", 1, 1, f_buflisted},
5764 {"bufloaded", 1, 1, f_bufloaded},
5765 {"bufname", 1, 1, f_bufname},
5766 {"bufnr", 1, 1, f_bufnr},
5767 {"bufwinnr", 1, 1, f_bufwinnr},
5768 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005769 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00005770 {"call", 2, 3, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 {"char2nr", 1, 1, f_char2nr},
5772 {"cindent", 1, 1, f_cindent},
5773 {"col", 1, 1, f_col},
5774 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005775 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005776 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 {"cscope_connection",0,3, f_cscope_connection},
5778 {"cursor", 2, 2, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005779 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 {"delete", 1, 1, f_delete},
5781 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00005782 {"diff_filler", 1, 1, f_diff_filler},
5783 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00005784 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005786 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 {"eventhandler", 0, 0, f_eventhandler},
5788 {"executable", 1, 1, f_executable},
5789 {"exists", 1, 1, f_exists},
5790 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005791 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
5793 {"filereadable", 1, 1, f_filereadable},
5794 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005795 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005796 {"finddir", 1, 3, f_finddir},
5797 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 {"fnamemodify", 2, 2, f_fnamemodify},
5799 {"foldclosed", 1, 1, f_foldclosed},
5800 {"foldclosedend", 1, 1, f_foldclosedend},
5801 {"foldlevel", 1, 1, f_foldlevel},
5802 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005803 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005805 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005806 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 {"getbufvar", 2, 2, f_getbufvar},
5808 {"getchar", 0, 1, f_getchar},
5809 {"getcharmod", 0, 0, f_getcharmod},
5810 {"getcmdline", 0, 0, f_getcmdline},
5811 {"getcmdpos", 0, 0, f_getcmdpos},
5812 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005813 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005814 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 {"getfsize", 1, 1, f_getfsize},
5816 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005817 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005818 {"getline", 1, 2, f_getline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 {"getreg", 0, 1, f_getreg},
5820 {"getregtype", 0, 1, f_getregtype},
5821 {"getwinposx", 0, 0, f_getwinposx},
5822 {"getwinposy", 0, 0, f_getwinposy},
5823 {"getwinvar", 2, 2, f_getwinvar},
5824 {"glob", 1, 1, f_glob},
5825 {"globpath", 2, 2, f_globpath},
5826 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00005827 {"has_key", 2, 2, f_has_key},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 {"hasmapto", 1, 2, f_hasmapto},
5829 {"highlightID", 1, 1, f_hlID}, /* obsolete */
5830 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
5831 {"histadd", 2, 2, f_histadd},
5832 {"histdel", 1, 2, f_histdel},
5833 {"histget", 1, 2, f_histget},
5834 {"histnr", 1, 1, f_histnr},
5835 {"hlID", 1, 1, f_hlID},
5836 {"hlexists", 1, 1, f_hlexists},
5837 {"hostname", 0, 0, f_hostname},
5838 {"iconv", 3, 3, f_iconv},
5839 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005840 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 {"input", 1, 2, f_input},
5842 {"inputdialog", 1, 3, f_inputdialog},
5843 {"inputrestore", 0, 0, f_inputrestore},
5844 {"inputsave", 0, 0, f_inputsave},
5845 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005846 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005848 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005853 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 {"libcall", 3, 3, f_libcall},
5855 {"libcallnr", 3, 3, f_libcallnr},
5856 {"line", 1, 1, f_line},
5857 {"line2byte", 1, 1, f_line2byte},
5858 {"lispindent", 1, 1, f_lispindent},
5859 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005860 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 {"maparg", 1, 2, f_maparg},
5862 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005863 {"match", 2, 4, f_match},
5864 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005865 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005866 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005867 {"max", 1, 1, f_max},
5868 {"min", 1, 1, f_min},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 {"mode", 0, 0, f_mode},
5870 {"nextnonblank", 1, 1, f_nextnonblank},
5871 {"nr2char", 1, 1, f_nr2char},
5872 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 {"range", 1, 3, f_range},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005874 {"readfile", 1, 2, f_readfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 {"remote_expr", 2, 3, f_remote_expr},
5876 {"remote_foreground", 1, 1, f_remote_foreground},
5877 {"remote_peek", 1, 2, f_remote_peek},
5878 {"remote_read", 1, 1, f_remote_read},
5879 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005880 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005882 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005884 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 {"search", 1, 2, f_search},
5886 {"searchpair", 3, 5, f_searchpair},
5887 {"server2client", 2, 2, f_server2client},
5888 {"serverlist", 0, 0, f_serverlist},
5889 {"setbufvar", 3, 3, f_setbufvar},
5890 {"setcmdpos", 1, 1, f_setcmdpos},
5891 {"setline", 2, 2, f_setline},
5892 {"setreg", 2, 3, f_setreg},
5893 {"setwinvar", 3, 3, f_setwinvar},
5894 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005895 {"sort", 1, 2, f_sort},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896 {"split", 1, 2, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897#ifdef HAVE_STRFTIME
5898 {"strftime", 1, 2, f_strftime},
5899#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00005900 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005901 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 {"strlen", 1, 1, f_strlen},
5903 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00005904 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 {"strtrans", 1, 1, f_strtrans},
5906 {"submatch", 1, 1, f_submatch},
5907 {"substitute", 4, 4, f_substitute},
5908 {"synID", 3, 3, f_synID},
5909 {"synIDattr", 2, 3, f_synIDattr},
5910 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005911 {"system", 1, 2, f_system},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 {"tempname", 0, 0, f_tempname},
5913 {"tolower", 1, 1, f_tolower},
5914 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00005915 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005917 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 {"virtcol", 1, 1, f_virtcol},
5919 {"visualmode", 0, 1, f_visualmode},
5920 {"winbufnr", 1, 1, f_winbufnr},
5921 {"wincol", 0, 0, f_wincol},
5922 {"winheight", 1, 1, f_winheight},
5923 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005924 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {"winrestcmd", 0, 0, f_winrestcmd},
5926 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005927 {"writefile", 2, 3, f_writefile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928};
5929
5930#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5931
5932/*
5933 * Function given to ExpandGeneric() to obtain the list of internal
5934 * or user defined function names.
5935 */
5936 char_u *
5937get_function_name(xp, idx)
5938 expand_T *xp;
5939 int idx;
5940{
5941 static int intidx = -1;
5942 char_u *name;
5943
5944 if (idx == 0)
5945 intidx = -1;
5946 if (intidx < 0)
5947 {
5948 name = get_user_func_name(xp, idx);
5949 if (name != NULL)
5950 return name;
5951 }
5952 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
5953 {
5954 STRCPY(IObuff, functions[intidx].f_name);
5955 STRCAT(IObuff, "(");
5956 if (functions[intidx].f_max_argc == 0)
5957 STRCAT(IObuff, ")");
5958 return IObuff;
5959 }
5960
5961 return NULL;
5962}
5963
5964/*
5965 * Function given to ExpandGeneric() to obtain the list of internal or
5966 * user defined variable or function names.
5967 */
5968/*ARGSUSED*/
5969 char_u *
5970get_expr_name(xp, idx)
5971 expand_T *xp;
5972 int idx;
5973{
5974 static int intidx = -1;
5975 char_u *name;
5976
5977 if (idx == 0)
5978 intidx = -1;
5979 if (intidx < 0)
5980 {
5981 name = get_function_name(xp, idx);
5982 if (name != NULL)
5983 return name;
5984 }
5985 return get_user_var_name(xp, ++intidx);
5986}
5987
5988#endif /* FEAT_CMDL_COMPL */
5989
5990/*
5991 * Find internal function in table above.
5992 * Return index, or -1 if not found
5993 */
5994 static int
5995find_internal_func(name)
5996 char_u *name; /* name of the function */
5997{
5998 int first = 0;
5999 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
6000 int cmp;
6001 int x;
6002
6003 /*
6004 * Find the function name in the table. Binary search.
6005 */
6006 while (first <= last)
6007 {
6008 x = first + ((unsigned)(last - first) >> 1);
6009 cmp = STRCMP(name, functions[x].f_name);
6010 if (cmp < 0)
6011 last = x - 1;
6012 else if (cmp > 0)
6013 first = x + 1;
6014 else
6015 return x;
6016 }
6017 return -1;
6018}
6019
6020/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
6022 * name it contains, otherwise return "name".
6023 */
6024 static char_u *
6025deref_func_name(name, lenp)
6026 char_u *name;
6027 int *lenp;
6028{
Bram Moolenaar33570922005-01-25 22:26:29 +00006029 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 int cc;
6031
6032 cc = name[*lenp];
6033 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00006034 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00006036 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006037 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006038 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039 {
6040 *lenp = 0;
6041 return (char_u *)""; /* just in case */
6042 }
Bram Moolenaar33570922005-01-25 22:26:29 +00006043 *lenp = STRLEN(v->di_tv.vval.v_string);
6044 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006045 }
6046
6047 return name;
6048}
6049
6050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 * Allocate a variable for the result of a function.
6052 * Return OK or FAIL.
6053 */
6054 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00006055get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
6056 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 char_u *name; /* name of the function */
6058 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006059 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 char_u **arg; /* argument, pointing to the '(' */
6061 linenr_T firstline; /* first line of range */
6062 linenr_T lastline; /* last line of range */
6063 int *doesrange; /* return: function handled range */
6064 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006065 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066{
6067 char_u *argp;
6068 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006069 typval_T argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 int argcount = 0; /* number of arguments found */
6071
6072 /*
6073 * Get the arguments.
6074 */
6075 argp = *arg;
6076 while (argcount < MAX_FUNC_ARGS)
6077 {
6078 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
6079 if (*argp == ')' || *argp == ',' || *argp == NUL)
6080 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
6082 {
6083 ret = FAIL;
6084 break;
6085 }
6086 ++argcount;
6087 if (*argp != ',')
6088 break;
6089 }
6090 if (*argp == ')')
6091 ++argp;
6092 else
6093 ret = FAIL;
6094
6095 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006096 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006097 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00006099 {
6100 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006101 emsg_funcname("E740: Too many arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006102 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006103 emsg_funcname("E116: Invalid arguments for function %s", name);
Bram Moolenaar33570922005-01-25 22:26:29 +00006104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105
6106 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006107 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108
6109 *arg = skipwhite(argp);
6110 return ret;
6111}
6112
6113
6114/*
6115 * Call a function with its resolved parameters
6116 * Return OK or FAIL.
6117 */
6118 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006119call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006120 doesrange, evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 char_u *name; /* name of the function */
6122 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006123 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 int argcount; /* number of "argvars" */
Bram Moolenaar33570922005-01-25 22:26:29 +00006125 typval_T *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 linenr_T firstline; /* first line of range */
6127 linenr_T lastline; /* last line of range */
6128 int *doesrange; /* return: function handled range */
6129 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00006130 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131{
6132 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133#define ERROR_UNKNOWN 0
6134#define ERROR_TOOMANY 1
6135#define ERROR_TOOFEW 2
6136#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00006137#define ERROR_DICT 4
6138#define ERROR_NONE 5
6139#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 int error = ERROR_NONE;
6141 int i;
6142 int llen;
6143 ufunc_T *fp;
6144 int cc;
6145#define FLEN_FIXED 40
6146 char_u fname_buf[FLEN_FIXED + 1];
6147 char_u *fname;
6148
6149 /*
6150 * In a script change <SID>name() and s:name() to K_SNR 123_name().
6151 * Change <SNR>123_name() to K_SNR 123_name().
6152 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
6153 */
6154 cc = name[len];
6155 name[len] = NUL;
6156 llen = eval_fname_script(name);
6157 if (llen > 0)
6158 {
6159 fname_buf[0] = K_SPECIAL;
6160 fname_buf[1] = KS_EXTRA;
6161 fname_buf[2] = (int)KE_SNR;
6162 i = 3;
6163 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
6164 {
6165 if (current_SID <= 0)
6166 error = ERROR_SCRIPT;
6167 else
6168 {
6169 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
6170 i = (int)STRLEN(fname_buf);
6171 }
6172 }
6173 if (i + STRLEN(name + llen) < FLEN_FIXED)
6174 {
6175 STRCPY(fname_buf + i, name + llen);
6176 fname = fname_buf;
6177 }
6178 else
6179 {
6180 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
6181 if (fname == NULL)
6182 error = ERROR_OTHER;
6183 else
6184 {
6185 mch_memmove(fname, fname_buf, (size_t)i);
6186 STRCPY(fname + i, name + llen);
6187 }
6188 }
6189 }
6190 else
6191 fname = name;
6192
6193 *doesrange = FALSE;
6194
6195
6196 /* execute the function if no errors detected and executing */
6197 if (evaluate && error == ERROR_NONE)
6198 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006199 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 error = ERROR_UNKNOWN;
6201
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006202 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 {
6204 /*
6205 * User defined function.
6206 */
6207 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006208
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006210 /* Trigger FuncUndefined event, may load the function. */
6211 if (fp == NULL
6212 && apply_autocmds(EVENT_FUNCUNDEFINED,
6213 fname, fname, TRUE, NULL)
6214 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006216 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 fp = find_func(fname);
6218 }
6219#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006220 /* Try loading a package. */
6221 if (fp == NULL && func_autoload(fname) && !aborting())
6222 {
6223 /* loaded a package, search for the function again */
6224 fp = find_func(fname);
6225 }
6226
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 if (fp != NULL)
6228 {
6229 if (fp->flags & FC_RANGE)
6230 *doesrange = TRUE;
6231 if (argcount < fp->args.ga_len)
6232 error = ERROR_TOOFEW;
6233 else if (!fp->varargs && argcount > fp->args.ga_len)
6234 error = ERROR_TOOMANY;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006235 else if ((fp->flags & FC_DICT) && selfdict == NULL)
6236 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 else
6238 {
6239 /*
6240 * Call the user function.
6241 * Save and restore search patterns, script variables and
6242 * redo buffer.
6243 */
6244 save_search_patterns();
6245 saveRedobuff();
6246 ++fp->calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006247 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006248 firstline, lastline,
6249 (fp->flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006250 if (--fp->calls <= 0 && isdigit(*fp->name)
6251 && fp->refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006252 /* Function was unreferenced while being used, free it
6253 * now. */
6254 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 restoreRedobuff();
6256 restore_search_patterns();
6257 error = ERROR_NONE;
6258 }
6259 }
6260 }
6261 else
6262 {
6263 /*
6264 * Find the function name in the table, call its implementation.
6265 */
6266 i = find_internal_func(fname);
6267 if (i >= 0)
6268 {
6269 if (argcount < functions[i].f_min_argc)
6270 error = ERROR_TOOFEW;
6271 else if (argcount > functions[i].f_max_argc)
6272 error = ERROR_TOOMANY;
6273 else
6274 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006275 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006276 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 error = ERROR_NONE;
6278 }
6279 }
6280 }
6281 /*
6282 * The function call (or "FuncUndefined" autocommand sequence) might
6283 * have been aborted by an error, an interrupt, or an explicitly thrown
6284 * exception that has not been caught so far. This situation can be
6285 * tested for by calling aborting(). For an error in an internal
6286 * function or for the "E132" error in call_user_func(), however, the
6287 * throw point at which the "force_abort" flag (temporarily reset by
6288 * emsg()) is normally updated has not been reached yet. We need to
6289 * update that flag first to make aborting() reliable.
6290 */
6291 update_force_abort();
6292 }
6293 if (error == ERROR_NONE)
6294 ret = OK;
6295
6296 /*
6297 * Report an error unless the argument evaluation or function call has been
6298 * cancelled due to an aborting error, an interrupt, or an exception.
6299 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00006300 if (!aborting())
6301 {
6302 switch (error)
6303 {
6304 case ERROR_UNKNOWN:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006305 emsg_funcname("E117: Unknown function: %s", name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006306 break;
6307 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006308 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00006309 break;
6310 case ERROR_TOOFEW:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006311 emsg_funcname("E119: Not enough arguments for function: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006312 name);
6313 break;
6314 case ERROR_SCRIPT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006315 emsg_funcname("E120: Using <SID> not in a script context: %s",
Bram Moolenaar8c711452005-01-14 21:53:12 +00006316 name);
6317 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006318 case ERROR_DICT:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006319 emsg_funcname("E725: Calling dict function without Dictionary: %s",
Bram Moolenaare9a41262005-01-15 22:18:47 +00006320 name);
6321 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006322 }
6323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324
6325 name[len] = cc;
6326 if (fname != name && fname != fname_buf)
6327 vim_free(fname);
6328
6329 return ret;
6330}
6331
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006332/*
6333 * Give an error message with a function name. Handle <SNR> things.
6334 */
6335 static void
6336emsg_funcname(msg, name)
6337 char *msg;
6338 char_u *name;
6339{
6340 char_u *p;
6341
6342 if (*name == K_SPECIAL)
6343 p = concat_str((char_u *)"<SNR>", name + 3);
6344 else
6345 p = name;
6346 EMSG2(_(msg), p);
6347 if (p != name)
6348 vim_free(p);
6349}
6350
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351/*********************************************
6352 * Implementation of the built-in functions
6353 */
6354
6355/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006356 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 */
6358 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00006359f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006360 typval_T *argvars;
6361 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362{
Bram Moolenaar33570922005-01-25 22:26:29 +00006363 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006365 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006366 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006368 if ((l = argvars[0].vval.v_list) != NULL
6369 && !tv_check_lock(l->lv_lock, (char_u *)"add()")
6370 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006371 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006372 }
6373 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00006374 EMSG(_(e_listreq));
6375}
6376
6377/*
6378 * "append(lnum, string/list)" function
6379 */
6380 static void
6381f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006382 typval_T *argvars;
6383 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006384{
6385 long lnum;
Bram Moolenaar33570922005-01-25 22:26:29 +00006386 list_T *l = NULL;
6387 listitem_T *li = NULL;
6388 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00006389 long added = 0;
6390
6391 rettv->vval.v_number = 1; /* Default: Failed */
6392 lnum = get_tv_lnum(argvars);
6393 if (lnum >= 0
6394 && lnum <= curbuf->b_ml.ml_line_count
6395 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006396 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006397 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006398 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00006399 l = argvars[1].vval.v_list;
6400 if (l == NULL)
6401 return;
6402 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006403 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00006404 for (;;)
6405 {
6406 if (l == NULL)
6407 tv = &argvars[1]; /* append a string */
6408 else if (li == NULL)
6409 break; /* end of list */
6410 else
6411 tv = &li->li_tv; /* append item from list */
6412 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
6413 ++added;
6414 if (l == NULL)
6415 break;
6416 li = li->li_next;
6417 }
6418
6419 appended_lines_mark(lnum, added);
6420 if (curwin->w_cursor.lnum > lnum)
6421 curwin->w_cursor.lnum += added;
6422 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 }
6424}
6425
6426/*
6427 * "argc()" function
6428 */
6429/* ARGSUSED */
6430 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006431f_argc(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006432 typval_T *argvars;
6433 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006435 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436}
6437
6438/*
6439 * "argidx()" function
6440 */
6441/* ARGSUSED */
6442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006443f_argidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006444 typval_T *argvars;
6445 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006447 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448}
6449
6450/*
6451 * "argv(nr)" function
6452 */
6453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006454f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006455 typval_T *argvars;
6456 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457{
6458 int idx;
6459
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006460 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006462 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006464 rettv->vval.v_string = NULL;
6465 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466}
6467
6468/*
6469 * "browse(save, title, initdir, default)" function
6470 */
6471/* ARGSUSED */
6472 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006473f_browse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006474 typval_T *argvars;
6475 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476{
6477#ifdef FEAT_BROWSE
6478 int save;
6479 char_u *title;
6480 char_u *initdir;
6481 char_u *defname;
6482 char_u buf[NUMBUFLEN];
6483 char_u buf2[NUMBUFLEN];
6484
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006485 save = get_tv_number(&argvars[0]);
6486 title = get_tv_string(&argvars[1]);
6487 initdir = get_tv_string_buf(&argvars[2], buf);
6488 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006490 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006491 do_browse(save ? BROWSE_SAVE : 0,
6492 title, defname, NULL, initdir, NULL, curbuf);
6493#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006494 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006495#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006496 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006497}
6498
6499/*
6500 * "browsedir(title, initdir)" function
6501 */
6502/* ARGSUSED */
6503 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006504f_browsedir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006505 typval_T *argvars;
6506 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006507{
6508#ifdef FEAT_BROWSE
6509 char_u *title;
6510 char_u *initdir;
6511 char_u buf[NUMBUFLEN];
6512
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006513 title = get_tv_string(&argvars[0]);
6514 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006515
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006516 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006517 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006519 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006521 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522}
6523
Bram Moolenaar33570922005-01-25 22:26:29 +00006524static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006525
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526/*
6527 * Find a buffer by number or exact name.
6528 */
6529 static buf_T *
6530find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00006531 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532{
6533 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006535 if (avar->v_type == VAR_NUMBER)
6536 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00006537 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006539 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006540 if (buf == NULL)
6541 {
6542 /* No full path name match, try a match with a URL or a "nofile"
6543 * buffer, these don't use the full path. */
6544 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
6545 if (buf->b_fname != NULL
6546 && (path_with_url(buf->b_fname)
6547#ifdef FEAT_QUICKFIX
6548 || bt_nofile(buf)
6549#endif
6550 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006551 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00006552 break;
6553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 }
6555 return buf;
6556}
6557
6558/*
6559 * "bufexists(expr)" function
6560 */
6561 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006562f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006563 typval_T *argvars;
6564 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006566 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567}
6568
6569/*
6570 * "buflisted(expr)" function
6571 */
6572 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006573f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006574 typval_T *argvars;
6575 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576{
6577 buf_T *buf;
6578
6579 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006580 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581}
6582
6583/*
6584 * "bufloaded(expr)" function
6585 */
6586 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006587f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006588 typval_T *argvars;
6589 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590{
6591 buf_T *buf;
6592
6593 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006594 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595}
6596
Bram Moolenaar33570922005-01-25 22:26:29 +00006597static buf_T *get_buf_tv __ARGS((typval_T *tv));
Bram Moolenaar0d660222005-01-07 21:51:51 +00006598
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599/*
6600 * Get buffer by number or pattern.
6601 */
6602 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006603get_buf_tv(tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006604 typval_T *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006606 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 int save_magic;
6608 char_u *save_cpo;
6609 buf_T *buf;
6610
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006611 if (tv->v_type == VAR_NUMBER)
6612 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00006613 if (tv->v_type != VAR_STRING)
6614 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 if (name == NULL || *name == NUL)
6616 return curbuf;
6617 if (name[0] == '$' && name[1] == NUL)
6618 return lastbuf;
6619
6620 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
6621 save_magic = p_magic;
6622 p_magic = TRUE;
6623 save_cpo = p_cpo;
6624 p_cpo = (char_u *)"";
6625
6626 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
6627 TRUE, FALSE));
6628
6629 p_magic = save_magic;
6630 p_cpo = save_cpo;
6631
6632 /* If not found, try expanding the name, like done for bufexists(). */
6633 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006634 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635
6636 return buf;
6637}
6638
6639/*
6640 * "bufname(expr)" function
6641 */
6642 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006643f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006644 typval_T *argvars;
6645 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646{
6647 buf_T *buf;
6648
6649 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006650 buf = get_buf_tv(&argvars[0]);
6651 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006653 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006655 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 --emsg_off;
6657}
6658
6659/*
6660 * "bufnr(expr)" function
6661 */
6662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006663f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006664 typval_T *argvars;
6665 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666{
6667 buf_T *buf;
6668
6669 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006670 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006672 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006674 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 --emsg_off;
6676}
6677
6678/*
6679 * "bufwinnr(nr)" function
6680 */
6681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006682f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006683 typval_T *argvars;
6684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685{
6686#ifdef FEAT_WINDOWS
6687 win_T *wp;
6688 int winnr = 0;
6689#endif
6690 buf_T *buf;
6691
6692 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006693 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694#ifdef FEAT_WINDOWS
6695 for (wp = firstwin; wp; wp = wp->w_next)
6696 {
6697 ++winnr;
6698 if (wp->w_buffer == buf)
6699 break;
6700 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006701 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006703 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704#endif
6705 --emsg_off;
6706}
6707
6708/*
6709 * "byte2line(byte)" function
6710 */
6711/*ARGSUSED*/
6712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006713f_byte2line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006714 typval_T *argvars;
6715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716{
6717#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006718 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719#else
6720 long boff = 0;
6721
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006722 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006724 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006726 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 (linenr_T)0, &boff);
6728#endif
6729}
6730
6731/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006732 * "byteidx()" function
6733 */
6734/*ARGSUSED*/
6735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006736f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006737 typval_T *argvars;
6738 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006739{
6740#ifdef FEAT_MBYTE
6741 char_u *t;
6742#endif
6743 char_u *str;
6744 long idx;
6745
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006746 str = get_tv_string(&argvars[0]);
6747 idx = get_tv_number(&argvars[1]);
6748 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006749 if (idx < 0)
6750 return;
6751
6752#ifdef FEAT_MBYTE
6753 t = str;
6754 for ( ; idx > 0; idx--)
6755 {
6756 if (*t == NUL) /* EOL reached */
6757 return;
6758 t += mb_ptr2len_check(t);
6759 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006760 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006761#else
6762 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006763 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00006764#endif
6765}
6766
6767/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006768 * "call(func, arglist)" function
6769 */
6770 static void
6771f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006772 typval_T *argvars;
6773 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006774{
6775 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00006776 typval_T argv[MAX_FUNC_ARGS];
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006777 int argc = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006778 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006779 int dummy;
Bram Moolenaar33570922005-01-25 22:26:29 +00006780 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006781
6782 rettv->vval.v_number = 0;
6783 if (argvars[1].v_type != VAR_LIST)
6784 {
6785 EMSG(_(e_listreq));
6786 return;
6787 }
6788 if (argvars[1].vval.v_list == NULL)
6789 return;
6790
6791 if (argvars[0].v_type == VAR_FUNC)
6792 func = argvars[0].vval.v_string;
6793 else
6794 func = get_tv_string(&argvars[0]);
6795
Bram Moolenaare9a41262005-01-15 22:18:47 +00006796 if (argvars[2].v_type != VAR_UNKNOWN)
6797 {
6798 if (argvars[2].v_type != VAR_DICT)
6799 {
6800 EMSG(_(e_dictreq));
6801 return;
6802 }
6803 selfdict = argvars[2].vval.v_dict;
6804 }
6805
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006806 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
6807 item = item->li_next)
6808 {
6809 if (argc == MAX_FUNC_ARGS)
6810 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006811 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006812 break;
6813 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006814 /* Make a copy of each argument. This is needed to be able to set
6815 * v_lock to VAR_FIXED in the copy without changing the original list.
6816 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006817 copy_tv(&item->li_tv, &argv[argc++]);
6818 }
6819
6820 if (item == NULL)
6821 (void)call_func(func, STRLEN(func), rettv, argc, argv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00006822 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
6823 &dummy, TRUE, selfdict);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006824
6825 /* Free the arguments. */
6826 while (argc > 0)
6827 clear_tv(&argv[--argc]);
6828}
6829
6830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 * "char2nr(string)" function
6832 */
6833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006834f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006835 typval_T *argvars;
6836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837{
6838#ifdef FEAT_MBYTE
6839 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006840 rettv->vval.v_number =
6841 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 else
6843#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006844 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845}
6846
6847/*
6848 * "cindent(lnum)" function
6849 */
6850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006851f_cindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006852 typval_T *argvars;
6853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854{
6855#ifdef FEAT_CINDENT
6856 pos_T pos;
6857 linenr_T lnum;
6858
6859 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006860 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6862 {
6863 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006864 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 curwin->w_cursor = pos;
6866 }
6867 else
6868#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006869 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870}
6871
6872/*
6873 * "col(string)" function
6874 */
6875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006876f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006877 typval_T *argvars;
6878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879{
6880 colnr_T col = 0;
6881 pos_T *fp;
6882
6883 fp = var2fpos(&argvars[0], FALSE);
6884 if (fp != NULL)
6885 {
6886 if (fp->col == MAXCOL)
6887 {
6888 /* '> can be MAXCOL, get the length of the line then */
6889 if (fp->lnum <= curbuf->b_ml.ml_line_count)
6890 col = STRLEN(ml_get(fp->lnum)) + 1;
6891 else
6892 col = MAXCOL;
6893 }
6894 else
6895 {
6896 col = fp->col + 1;
6897#ifdef FEAT_VIRTUALEDIT
6898 /* col(".") when the cursor is on the NUL at the end of the line
6899 * because of "coladd" can be seen as an extra column. */
6900 if (virtual_active() && fp == &curwin->w_cursor)
6901 {
6902 char_u *p = ml_get_cursor();
6903
6904 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
6905 curwin->w_virtcol - curwin->w_cursor.coladd))
6906 {
6907# ifdef FEAT_MBYTE
6908 int l;
6909
6910 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
6911 col += l;
6912# else
6913 if (*p != NUL && p[1] == NUL)
6914 ++col;
6915# endif
6916 }
6917 }
6918#endif
6919 }
6920 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006921 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922}
6923
6924/*
6925 * "confirm(message, buttons[, default [, type]])" function
6926 */
6927/*ARGSUSED*/
6928 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006929f_confirm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006930 typval_T *argvars;
6931 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932{
6933#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6934 char_u *message;
6935 char_u *buttons = NULL;
6936 char_u buf[NUMBUFLEN];
6937 char_u buf2[NUMBUFLEN];
6938 int def = 1;
6939 int type = VIM_GENERIC;
6940 int c;
6941
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006942 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006943 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006945 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006946 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006948 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006949 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 {
Bram Moolenaara7043832005-01-21 11:56:39 +00006951 /* avoid that TOUPPER_ASC calls get_tv_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006952 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 switch (TOUPPER_ASC(c))
6954 {
6955 case 'E': type = VIM_ERROR; break;
6956 case 'Q': type = VIM_QUESTION; break;
6957 case 'I': type = VIM_INFO; break;
6958 case 'W': type = VIM_WARNING; break;
6959 case 'G': type = VIM_GENERIC; break;
6960 }
6961 }
6962 }
6963 }
6964
6965 if (buttons == NULL || *buttons == NUL)
6966 buttons = (char_u *)_("&Ok");
6967
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006968 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 def, NULL);
6970#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006971 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972#endif
6973}
6974
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006975/*
6976 * "copy()" function
6977 */
6978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006979f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006980 typval_T *argvars;
6981 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006982{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006983 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006984}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985
6986/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006987 * "count()" function
6988 */
6989 static void
6990f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006991 typval_T *argvars;
6992 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006993{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006994 long n = 0;
6995 int ic = FALSE;
6996
Bram Moolenaare9a41262005-01-15 22:18:47 +00006997 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006998 {
Bram Moolenaar33570922005-01-25 22:26:29 +00006999 listitem_T *li;
7000 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007001 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007002
Bram Moolenaare9a41262005-01-15 22:18:47 +00007003 if ((l = argvars[0].vval.v_list) != NULL)
7004 {
7005 li = l->lv_first;
7006 if (argvars[2].v_type != VAR_UNKNOWN)
7007 {
7008 ic = get_tv_number(&argvars[2]);
7009 if (argvars[3].v_type != VAR_UNKNOWN)
7010 {
7011 idx = get_tv_number(&argvars[3]);
7012 li = list_find(l, idx);
7013 if (li == NULL)
7014 EMSGN(_(e_listidx), idx);
7015 }
7016 }
7017
7018 for ( ; li != NULL; li = li->li_next)
7019 if (tv_equal(&li->li_tv, &argvars[1], ic))
7020 ++n;
7021 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007022 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007023 else if (argvars[0].v_type == VAR_DICT)
7024 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007025 int todo;
7026 dict_T *d;
7027 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007028
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007029 if ((d = argvars[0].vval.v_dict) != NULL)
7030 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00007031 if (argvars[2].v_type != VAR_UNKNOWN)
7032 {
7033 ic = get_tv_number(&argvars[2]);
7034 if (argvars[3].v_type != VAR_UNKNOWN)
7035 EMSG(_(e_invarg));
7036 }
7037
Bram Moolenaar33570922005-01-25 22:26:29 +00007038 todo = d->dv_hashtab.ht_used;
7039 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007040 {
7041 if (!HASHITEM_EMPTY(hi))
7042 {
7043 --todo;
7044 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic))
7045 ++n;
7046 }
7047 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007048 }
7049 }
7050 else
7051 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007052 rettv->vval.v_number = n;
7053}
7054
7055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
7057 *
7058 * Checks the existence of a cscope connection.
7059 */
7060/*ARGSUSED*/
7061 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007062f_cscope_connection(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007063 typval_T *argvars;
7064 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065{
7066#ifdef FEAT_CSCOPE
7067 int num = 0;
7068 char_u *dbpath = NULL;
7069 char_u *prepend = NULL;
7070 char_u buf[NUMBUFLEN];
7071
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007072 if (argvars[0].v_type != VAR_UNKNOWN
7073 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007075 num = (int)get_tv_number(&argvars[0]);
7076 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007077 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007078 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 }
7080
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007081 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007083 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084#endif
7085}
7086
7087/*
7088 * "cursor(lnum, col)" function
7089 *
7090 * Moves the cursor to the specified line and column
7091 */
7092/*ARGSUSED*/
7093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007094f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007095 typval_T *argvars;
7096 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097{
7098 long line, col;
7099
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007100 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 if (line > 0)
7102 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007103 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 if (col > 0)
7105 curwin->w_cursor.col = col - 1;
7106#ifdef FEAT_VIRTUALEDIT
7107 curwin->w_cursor.coladd = 0;
7108#endif
7109
7110 /* Make sure the cursor is in a valid position. */
7111 check_cursor();
7112#ifdef FEAT_MBYTE
7113 /* Correct cursor for multi-byte character. */
7114 if (has_mbyte)
7115 mb_adjust_cursor();
7116#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007117
7118 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119}
7120
7121/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007122 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 */
7124 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007125f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007126 typval_T *argvars;
7127 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007129 static int copyID = 0;
7130 int noref = 0;
7131
7132 if (argvars[1].v_type != VAR_UNKNOWN)
7133 noref = get_tv_number(&argvars[1]);
7134 if (noref < 0 || noref > 1)
7135 EMSG(_(e_invarg));
7136 else
7137 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? ++copyID : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138}
7139
7140/*
7141 * "delete()" function
7142 */
7143 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007144f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 typval_T *argvars;
7146 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147{
7148 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007149 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007151 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152}
7153
7154/*
7155 * "did_filetype()" function
7156 */
7157/*ARGSUSED*/
7158 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007159f_did_filetype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007160 typval_T *argvars;
7161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162{
7163#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007164 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007166 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167#endif
7168}
7169
7170/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00007171 * "diff_filler()" function
7172 */
7173/*ARGSUSED*/
7174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007175f_diff_filler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007176 typval_T *argvars;
7177 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007178{
7179#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007180 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00007181#endif
7182}
7183
7184/*
7185 * "diff_hlID()" function
7186 */
7187/*ARGSUSED*/
7188 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007189f_diff_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007190 typval_T *argvars;
7191 typval_T *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007192{
7193#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007194 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00007195 static linenr_T prev_lnum = 0;
7196 static int changedtick = 0;
7197 static int fnum = 0;
7198 static int change_start = 0;
7199 static int change_end = 0;
7200 static enum hlf_value hlID = 0;
7201 int filler_lines;
7202 int col;
7203
7204 if (lnum != prev_lnum
7205 || changedtick != curbuf->b_changedtick
7206 || fnum != curbuf->b_fnum)
7207 {
7208 /* New line, buffer, change: need to get the values. */
7209 filler_lines = diff_check(curwin, lnum);
7210 if (filler_lines < 0)
7211 {
7212 if (filler_lines == -1)
7213 {
7214 change_start = MAXCOL;
7215 change_end = -1;
7216 if (diff_find_change(curwin, lnum, &change_start, &change_end))
7217 hlID = HLF_ADD; /* added line */
7218 else
7219 hlID = HLF_CHD; /* changed line */
7220 }
7221 else
7222 hlID = HLF_ADD; /* added line */
7223 }
7224 else
7225 hlID = (enum hlf_value)0;
7226 prev_lnum = lnum;
7227 changedtick = curbuf->b_changedtick;
7228 fnum = curbuf->b_fnum;
7229 }
7230
7231 if (hlID == HLF_CHD || hlID == HLF_TXD)
7232 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007233 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007234 if (col >= change_start && col <= change_end)
7235 hlID = HLF_TXD; /* changed text */
7236 else
7237 hlID = HLF_CHD; /* changed line */
7238 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007239 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00007240#endif
7241}
7242
7243/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007244 * "empty({expr})" function
7245 */
7246 static void
7247f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007248 typval_T *argvars;
7249 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007250{
7251 int n;
7252
7253 switch (argvars[0].v_type)
7254 {
7255 case VAR_STRING:
7256 case VAR_FUNC:
7257 n = argvars[0].vval.v_string == NULL
7258 || *argvars[0].vval.v_string == NUL;
7259 break;
7260 case VAR_NUMBER:
7261 n = argvars[0].vval.v_number == 0;
7262 break;
7263 case VAR_LIST:
7264 n = argvars[0].vval.v_list == NULL
7265 || argvars[0].vval.v_list->lv_first == NULL;
7266 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007267 case VAR_DICT:
7268 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00007269 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007270 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007271 default:
7272 EMSG2(_(e_intern2), "f_empty()");
7273 n = 0;
7274 }
7275
7276 rettv->vval.v_number = n;
7277}
7278
7279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 * "escape({string}, {chars})" function
7281 */
7282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007283f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007284 typval_T *argvars;
7285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286{
7287 char_u buf[NUMBUFLEN];
7288
Bram Moolenaar758711c2005-02-02 23:11:38 +00007289 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
7290 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007291 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292}
7293
7294/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007295 * "eval()" function
7296 */
7297/*ARGSUSED*/
7298 static void
7299f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007300 typval_T *argvars;
7301 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007302{
7303 char_u *s;
7304
7305 s = get_tv_string(&argvars[0]);
7306 s = skipwhite(s);
7307
7308 if (eval1(&s, rettv, TRUE) == FAIL)
7309 rettv->vval.v_number = 0;
7310 else if (*s != NUL)
7311 EMSG(_(e_trailing));
7312}
7313
7314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 * "eventhandler()" function
7316 */
7317/*ARGSUSED*/
7318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007319f_eventhandler(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007320 typval_T *argvars;
7321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007323 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324}
7325
7326/*
7327 * "executable()" function
7328 */
7329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007330f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 typval_T *argvars;
7332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007334 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335}
7336
7337/*
7338 * "exists()" function
7339 */
7340 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007341f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007342 typval_T *argvars;
7343 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344{
7345 char_u *p;
7346 char_u *name;
7347 int n = FALSE;
7348 int len = 0;
7349
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007350 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 if (*p == '$') /* environment variable */
7352 {
7353 /* first try "normal" environment variables (fast) */
7354 if (mch_getenv(p + 1) != NULL)
7355 n = TRUE;
7356 else
7357 {
7358 /* try expanding things like $VIM and ${HOME} */
7359 p = expand_env_save(p);
7360 if (p != NULL && *p != '$')
7361 n = TRUE;
7362 vim_free(p);
7363 }
7364 }
7365 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007366 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 else if (*p == '*') /* internal or user defined function */
7368 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007369 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 }
7371 else if (*p == ':')
7372 {
7373 n = cmd_exists(p + 1);
7374 }
7375 else if (*p == '#')
7376 {
7377#ifdef FEAT_AUTOCMD
7378 name = p + 1;
7379 p = vim_strchr(name, '#');
7380 if (p != NULL)
7381 n = au_exists(name, p, p + 1);
7382 else
7383 n = au_exists(name, name + STRLEN(name), NULL);
7384#endif
7385 }
7386 else /* internal variable */
7387 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007388 char_u *tofree;
7389 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007391 /* get_name_len() takes care of expanding curly braces */
7392 name = p;
7393 len = get_name_len(&p, &tofree, TRUE, FALSE);
7394 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007396 if (tofree != NULL)
7397 name = tofree;
7398 n = (get_var_tv(name, len, &tv, FALSE) == OK);
7399 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007401 /* handle d.key, l[idx], f(expr) */
7402 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
7403 if (n)
7404 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 }
7406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007408 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 }
7410
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007411 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412}
7413
7414/*
7415 * "expand()" function
7416 */
7417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007418f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 typval_T *argvars;
7420 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421{
7422 char_u *s;
7423 int len;
7424 char_u *errormsg;
7425 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
7426 expand_T xpc;
7427
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007428 rettv->v_type = VAR_STRING;
7429 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 if (*s == '%' || *s == '#' || *s == '<')
7431 {
7432 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007433 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 --emsg_off;
7435 }
7436 else
7437 {
7438 /* When the optional second argument is non-zero, don't remove matches
7439 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007440 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 flags |= WILD_KEEP_ALL;
7442 ExpandInit(&xpc);
7443 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007444 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 ExpandCleanup(&xpc);
7446 }
7447}
7448
7449/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007450 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +00007451 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007452 */
7453 static void
7454f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007455 typval_T *argvars;
7456 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007457{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007458 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007459 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007460 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007461 list_T *l1, *l2;
7462 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007463 long before;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007464
Bram Moolenaare9a41262005-01-15 22:18:47 +00007465 l1 = argvars[0].vval.v_list;
7466 l2 = argvars[1].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007467 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)"extend()")
7468 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007469 {
7470 if (argvars[2].v_type != VAR_UNKNOWN)
7471 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007472 before = get_tv_number(&argvars[2]);
7473 if (before == l1->lv_len)
7474 item = NULL;
7475 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00007476 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00007477 item = list_find(l1, before);
7478 if (item == NULL)
7479 {
7480 EMSGN(_(e_listidx), before);
7481 return;
7482 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007483 }
7484 }
7485 else
7486 item = NULL;
7487 list_extend(l1, l2, item);
7488
7489 ++l1->lv_refcount;
7490 copy_tv(&argvars[0], rettv);
7491 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007492 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007493 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
7494 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007495 dict_T *d1, *d2;
7496 dictitem_T *di1;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007497 char_u *action;
7498 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007499 hashitem_T *hi2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007500 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007501
7502 d1 = argvars[0].vval.v_dict;
7503 d2 = argvars[1].vval.v_dict;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007504 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)"extend()")
7505 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007506 {
7507 /* Check the third argument. */
7508 if (argvars[2].v_type != VAR_UNKNOWN)
7509 {
7510 static char *(av[]) = {"keep", "force", "error"};
7511
7512 action = get_tv_string(&argvars[2]);
7513 for (i = 0; i < 3; ++i)
7514 if (STRCMP(action, av[i]) == 0)
7515 break;
7516 if (i == 3)
7517 {
7518 EMSGN(_(e_invarg2), action);
7519 return;
7520 }
7521 }
7522 else
7523 action = (char_u *)"force";
7524
7525 /* Go over all entries in the second dict and add them to the
7526 * first dict. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007527 todo = d2->dv_hashtab.ht_used;
7528 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007529 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007530 if (!HASHITEM_EMPTY(hi2))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007531 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532 --todo;
7533 di1 = dict_find(d1, hi2->hi_key, -1);
7534 if (di1 == NULL)
7535 {
7536 di1 = dictitem_copy(HI2DI(hi2));
7537 if (di1 != NULL && dict_add(d1, di1) == FAIL)
7538 dictitem_free(di1);
7539 }
7540 else if (*action == 'e')
7541 {
7542 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
7543 break;
7544 }
7545 else if (*action == 'f')
7546 {
7547 clear_tv(&di1->di_tv);
7548 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
7549 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007550 }
7551 }
7552
7553 ++d1->dv_refcount;
7554 copy_tv(&argvars[0], rettv);
7555 }
7556 }
7557 else
7558 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007559}
7560
7561/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 * "filereadable()" function
7563 */
7564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007565f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007566 typval_T *argvars;
7567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568{
7569 FILE *fd;
7570 char_u *p;
7571 int n;
7572
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007573 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
7575 {
7576 n = TRUE;
7577 fclose(fd);
7578 }
7579 else
7580 n = FALSE;
7581
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007582 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583}
7584
7585/*
7586 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
7587 * rights to write into.
7588 */
7589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007590f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 typval_T *argvars;
7592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593{
7594 char_u *p;
7595 int retval = 0;
7596#if defined(UNIX) || defined(VMS)
7597 int perm = 0;
7598#endif
7599
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007600 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601#if defined(UNIX) || defined(VMS)
7602 perm = mch_getperm(p);
7603#endif
7604#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
7605 if (
7606# ifdef WIN3264
7607 mch_writable(p) &&
7608# else
7609# if defined(UNIX) || defined(VMS)
7610 (perm & 0222) &&
7611# endif
7612# endif
7613 mch_access((char *)p, W_OK) == 0
7614 )
7615#endif
7616 {
7617 ++retval;
7618 if (mch_isdir(p))
7619 ++retval;
7620 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007621 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622}
7623
Bram Moolenaar33570922005-01-25 22:26:29 +00007624static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007625
7626 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007627findfilendir(argvars, rettv, dir)
Bram Moolenaar33570922005-01-25 22:26:29 +00007628 typval_T *argvars;
7629 typval_T *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007630 int dir;
7631{
7632#ifdef FEAT_SEARCHPATH
7633 char_u *fname;
7634 char_u *fresult = NULL;
7635 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
7636 char_u *p;
7637 char_u pathbuf[NUMBUFLEN];
7638 int count = 1;
7639 int first = TRUE;
7640
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007641 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007642
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007643 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007644 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007645 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007646 if (*p != NUL)
7647 path = p;
7648
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007649 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007650 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007651 }
7652
7653 do
7654 {
7655 vim_free(fresult);
7656 fresult = find_file_in_path_option(first ? fname : NULL,
7657 first ? (int)STRLEN(fname) : 0,
7658 0, first, path, dir, NULL);
7659 first = FALSE;
7660 } while (--count > 0 && fresult != NULL);
7661
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007662 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007663#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007664 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007665#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007666 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007667}
7668
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007669static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
7670static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaar33570922005-01-25 22:26:29 +00007671static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
7672static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007673
7674/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007675 * Prepare v: variable "idx" to be used.
7676 * Save the current typeval in "save_tv".
7677 * When not used yet add the variable to the v: hashtable.
7678 */
7679 static void
7680prepare_vimvar(idx, save_tv)
7681 int idx;
7682 typval_T *save_tv;
7683{
7684 *save_tv = vimvars[idx].vv_tv;
7685 if (vimvars[idx].vv_type == VAR_UNKNOWN)
7686 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
7687}
7688
7689/*
7690 * Restore v: variable "idx" to typeval "save_tv".
7691 * When no longer defined, remove the variable from the v: hashtable.
7692 */
7693 static void
7694restore_vimvar(idx, save_tv)
7695 int idx;
7696 typval_T *save_tv;
7697{
7698 hashitem_T *hi;
7699
7700 clear_tv(&vimvars[idx].vv_tv);
7701 vimvars[idx].vv_tv = *save_tv;
7702 if (vimvars[idx].vv_type == VAR_UNKNOWN)
7703 {
7704 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
7705 if (HASHITEM_EMPTY(hi))
7706 EMSG2(_(e_intern2), "restore_vimvar()");
7707 else
7708 hash_remove(&vimvarht, hi);
7709 }
7710}
7711
7712/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007713 * Implementation of map() and filter().
7714 */
7715 static void
7716filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +00007717 typval_T *argvars;
7718 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007719 int map;
7720{
7721 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +00007722 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00007723 listitem_T *li, *nli;
7724 list_T *l = NULL;
7725 dictitem_T *di;
7726 hashtab_T *ht;
7727 hashitem_T *hi;
7728 dict_T *d = NULL;
7729 typval_T save_val;
7730 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007731 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007732 int todo;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007733 char_u *msg = map ? (char_u *)"map()" : (char_u *)"filter()";
7734
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007735
7736 rettv->vval.v_number = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007737 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007738 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007739 if ((l = argvars[0].vval.v_list) == NULL
7740 || (map && tv_check_lock(l->lv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007741 return;
7742 }
7743 else if (argvars[0].v_type == VAR_DICT)
7744 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007745 if ((d = argvars[0].vval.v_dict) == NULL
7746 || (map && tv_check_lock(d->dv_lock, msg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007747 return;
7748 }
7749 else
7750 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007751 EMSG2(_(e_listdictarg), msg);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007752 return;
7753 }
7754
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007755 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007756 expr = skipwhite(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007757
7758 if (argvars[0].v_type == VAR_DICT)
7759 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007760 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar33570922005-01-25 22:26:29 +00007761 vimvars[VV_KEY].vv_type = VAR_STRING;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007762
Bram Moolenaar33570922005-01-25 22:26:29 +00007763 ht = &d->dv_hashtab;
Bram Moolenaara7043832005-01-21 11:56:39 +00007764 hash_lock(ht);
7765 todo = ht->ht_used;
7766 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007767 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007768 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007769 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007770 --todo;
7771 di = HI2DI(hi);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007772 if (tv_check_lock(di->di_tv.v_lock, msg))
7773 break;
Bram Moolenaar33570922005-01-25 22:26:29 +00007774 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007775 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL)
7776 break;
7777 if (!map && rem)
7778 dictitem_remove(d, di);
Bram Moolenaar33570922005-01-25 22:26:29 +00007779 clear_tv(&vimvars[VV_KEY].vv_tv);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007780 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007781 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007782 hash_unlock(ht);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007783
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007784 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007785 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007786 else
7787 {
7788 for (li = l->lv_first; li != NULL; li = nli)
7789 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007790 if (tv_check_lock(li->li_tv.v_lock, msg))
7791 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007792 nli = li->li_next;
7793 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL)
7794 break;
7795 if (!map && rem)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007796 listitem_remove(l, li);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007797 }
7798 }
7799
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007800 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007801
7802 copy_tv(&argvars[0], rettv);
7803}
7804
7805 static int
7806filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +00007807 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007808 char_u *expr;
7809 int map;
7810 int *remp;
7811{
Bram Moolenaar33570922005-01-25 22:26:29 +00007812 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007813 char_u *s;
7814
Bram Moolenaar33570922005-01-25 22:26:29 +00007815 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007816 s = expr;
7817 if (eval1(&s, &rettv, TRUE) == FAIL)
7818 return FAIL;
7819 if (*s != NUL) /* check for trailing chars after expr */
7820 {
7821 EMSG2(_(e_invexpr2), s);
7822 return FAIL;
7823 }
7824 if (map)
7825 {
7826 /* map(): replace the list item value */
7827 clear_tv(tv);
7828 *tv = rettv;
7829 }
7830 else
7831 {
7832 /* filter(): when expr is zero remove the item */
7833 *remp = (get_tv_number(&rettv) == 0);
7834 clear_tv(&rettv);
7835 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007836 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +00007837 return OK;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007838}
7839
7840/*
7841 * "filter()" function
7842 */
7843 static void
7844f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007845 typval_T *argvars;
7846 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007847{
7848 filter_map(argvars, rettv, FALSE);
7849}
7850
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007851/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007852 * "finddir({fname}[, {path}[, {count}]])" function
7853 */
7854 static void
7855f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007856 typval_T *argvars;
7857 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007858{
7859 findfilendir(argvars, rettv, TRUE);
7860}
7861
7862/*
7863 * "findfile({fname}[, {path}[, {count}]])" function
7864 */
7865 static void
7866f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007867 typval_T *argvars;
7868 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007869{
7870 findfilendir(argvars, rettv, FALSE);
7871}
7872
7873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 * "fnamemodify({fname}, {mods})" function
7875 */
7876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007877f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007878 typval_T *argvars;
7879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880{
7881 char_u *fname;
7882 char_u *mods;
7883 int usedlen = 0;
7884 int len;
7885 char_u *fbuf = NULL;
7886 char_u buf[NUMBUFLEN];
7887
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007888 fname = get_tv_string(&argvars[0]);
7889 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 len = (int)STRLEN(fname);
7891
7892 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
7893
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007894 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007896 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007898 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 vim_free(fbuf);
7900}
7901
Bram Moolenaar33570922005-01-25 22:26:29 +00007902static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903
7904/*
7905 * "foldclosed()" function
7906 */
7907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007908foldclosed_both(argvars, rettv, end)
Bram Moolenaar33570922005-01-25 22:26:29 +00007909 typval_T *argvars;
7910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 int end;
7912{
7913#ifdef FEAT_FOLDING
7914 linenr_T lnum;
7915 linenr_T first, last;
7916
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007917 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7919 {
7920 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
7921 {
7922 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007923 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007925 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 return;
7927 }
7928 }
7929#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007930 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931}
7932
7933/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007934 * "foldclosed()" function
7935 */
7936 static void
7937f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007938 typval_T *argvars;
7939 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007940{
7941 foldclosed_both(argvars, rettv, FALSE);
7942}
7943
7944/*
7945 * "foldclosedend()" function
7946 */
7947 static void
7948f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007949 typval_T *argvars;
7950 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00007951{
7952 foldclosed_both(argvars, rettv, TRUE);
7953}
7954
7955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 * "foldlevel()" function
7957 */
7958 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007959f_foldlevel(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007960 typval_T *argvars;
7961 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962{
7963#ifdef FEAT_FOLDING
7964 linenr_T lnum;
7965
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007966 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007968 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 else
7970#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007971 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972}
7973
7974/*
7975 * "foldtext()" function
7976 */
7977/*ARGSUSED*/
7978 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007979f_foldtext(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00007980 typval_T *argvars;
7981 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982{
7983#ifdef FEAT_FOLDING
7984 linenr_T lnum;
7985 char_u *s;
7986 char_u *r;
7987 int len;
7988 char *txt;
7989#endif
7990
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007991 rettv->v_type = VAR_STRING;
7992 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +00007994 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
7995 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
7996 <= curbuf->b_ml.ml_line_count
7997 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 {
7999 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008000 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
8001 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 {
8003 if (!linewhite(lnum))
8004 break;
8005 ++lnum;
8006 }
8007
8008 /* Find interesting text in this line. */
8009 s = skipwhite(ml_get(lnum));
8010 /* skip C comment-start */
8011 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008012 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008014 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +00008015 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008016 {
8017 s = skipwhite(ml_get(lnum + 1));
8018 if (*s == '*')
8019 s = skipwhite(s + 1);
8020 }
8021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 txt = _("+-%s%3ld lines: ");
8023 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008024 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 + 20 /* for %3ld */
8026 + STRLEN(s))); /* concatenated */
8027 if (r != NULL)
8028 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008029 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
8030 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
8031 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 len = (int)STRLEN(r);
8033 STRCAT(r, s);
8034 /* remove 'foldmarker' and 'commentstring' */
8035 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008036 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 }
8038 }
8039#endif
8040}
8041
8042/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008043 * "foldtextresult(lnum)" function
8044 */
8045/*ARGSUSED*/
8046 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008047f_foldtextresult(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008048 typval_T *argvars;
8049 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008050{
8051#ifdef FEAT_FOLDING
8052 linenr_T lnum;
8053 char_u *text;
8054 char_u buf[51];
8055 foldinfo_T foldinfo;
8056 int fold_count;
8057#endif
8058
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008059 rettv->v_type = VAR_STRING;
8060 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008061#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008062 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008063 fold_count = foldedCount(curwin, lnum, &foldinfo);
8064 if (fold_count > 0)
8065 {
8066 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
8067 &foldinfo, buf);
8068 if (text == buf)
8069 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008070 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008071 }
8072#endif
8073}
8074
8075/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 * "foreground()" function
8077 */
8078/*ARGSUSED*/
8079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008080f_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008081 typval_T *argvars;
8082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008084 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085#ifdef FEAT_GUI
8086 if (gui.in_use)
8087 gui_mch_set_foreground();
8088#else
8089# ifdef WIN32
8090 win32_set_foreground();
8091# endif
8092#endif
8093}
8094
8095/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008096 * "function()" function
8097 */
8098/*ARGSUSED*/
8099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008100f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008101 typval_T *argvars;
8102 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008103{
8104 char_u *s;
8105
Bram Moolenaara7043832005-01-21 11:56:39 +00008106 rettv->vval.v_number = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008107 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008108 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008109 EMSG2(_(e_invarg2), s);
8110 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008111 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008112 else
8113 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008114 rettv->vval.v_string = vim_strsave(s);
8115 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008116 }
8117}
8118
8119/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008120 * "get()" function
8121 */
8122 static void
8123f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008124 typval_T *argvars;
8125 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008126{
Bram Moolenaar33570922005-01-25 22:26:29 +00008127 listitem_T *li;
8128 list_T *l;
8129 dictitem_T *di;
8130 dict_T *d;
8131 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008132
Bram Moolenaare9a41262005-01-15 22:18:47 +00008133 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008134 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008135 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008136 {
Bram Moolenaare9a41262005-01-15 22:18:47 +00008137 li = list_find(l, get_tv_number(&argvars[1]));
8138 if (li != NULL)
8139 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008140 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008141 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00008142 else if (argvars[0].v_type == VAR_DICT)
8143 {
8144 if ((d = argvars[0].vval.v_dict) != NULL)
8145 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00008146 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +00008147 if (di != NULL)
8148 tv = &di->di_tv;
8149 }
8150 }
8151 else
8152 EMSG2(_(e_listdictarg), "get()");
8153
8154 if (tv == NULL)
8155 {
8156 if (argvars[2].v_type == VAR_UNKNOWN)
8157 rettv->vval.v_number = 0;
8158 else
8159 copy_tv(&argvars[2], rettv);
8160 }
8161 else
8162 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008163}
8164
8165/*
8166 * "getbufvar()" function
8167 */
8168 static void
8169f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008170 typval_T *argvars;
8171 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008172{
8173 buf_T *buf;
8174 buf_T *save_curbuf;
8175 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008176 dictitem_T *v;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008177
8178 ++emsg_off;
8179 buf = get_buf_tv(&argvars[0]);
8180 varname = get_tv_string(&argvars[1]);
8181
8182 rettv->v_type = VAR_STRING;
8183 rettv->vval.v_string = NULL;
8184
8185 if (buf != NULL && varname != NULL)
8186 {
8187 if (*varname == '&') /* buffer-local-option */
8188 {
8189 /* set curbuf to be our buf, temporarily */
8190 save_curbuf = curbuf;
8191 curbuf = buf;
8192
8193 get_option_tv(&varname, rettv, TRUE);
8194
8195 /* restore previous notion of curbuf */
8196 curbuf = save_curbuf;
8197 }
8198 else
8199 {
8200 /* look up the variable */
Bram Moolenaar33570922005-01-25 22:26:29 +00008201 v = find_var_in_ht(&buf->b_vars.dv_hashtab, varname);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008202 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008203 copy_tv(&v->di_tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008204 }
8205 }
8206
8207 --emsg_off;
8208}
8209
8210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 * "getchar()" function
8212 */
8213 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008214f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008215 typval_T *argvars;
8216 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217{
8218 varnumber_T n;
8219
8220 ++no_mapping;
8221 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008222 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 /* getchar(): blocking wait. */
8224 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008225 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 /* getchar(1): only check if char avail */
8227 n = vpeekc();
8228 else if (vpeekc() == NUL)
8229 /* getchar(0) and no char avail: return zero */
8230 n = 0;
8231 else
8232 /* getchar(0) and char avail: return char */
8233 n = safe_vgetc();
8234 --no_mapping;
8235 --allow_keys;
8236
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008237 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 if (IS_SPECIAL(n) || mod_mask != 0)
8239 {
8240 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
8241 int i = 0;
8242
8243 /* Turn a special key into three bytes, plus modifier. */
8244 if (mod_mask != 0)
8245 {
8246 temp[i++] = K_SPECIAL;
8247 temp[i++] = KS_MODIFIER;
8248 temp[i++] = mod_mask;
8249 }
8250 if (IS_SPECIAL(n))
8251 {
8252 temp[i++] = K_SPECIAL;
8253 temp[i++] = K_SECOND(n);
8254 temp[i++] = K_THIRD(n);
8255 }
8256#ifdef FEAT_MBYTE
8257 else if (has_mbyte)
8258 i += (*mb_char2bytes)(n, temp + i);
8259#endif
8260 else
8261 temp[i++] = n;
8262 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008263 rettv->v_type = VAR_STRING;
8264 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 }
8266}
8267
8268/*
8269 * "getcharmod()" function
8270 */
8271/*ARGSUSED*/
8272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008273f_getcharmod(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008274 typval_T *argvars;
8275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008277 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278}
8279
8280/*
8281 * "getcmdline()" function
8282 */
8283/*ARGSUSED*/
8284 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008285f_getcmdline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008286 typval_T *argvars;
8287 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008289 rettv->v_type = VAR_STRING;
8290 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291}
8292
8293/*
8294 * "getcmdpos()" function
8295 */
8296/*ARGSUSED*/
8297 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008298f_getcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008299 typval_T *argvars;
8300 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008302 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303}
8304
8305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 * "getcwd()" function
8307 */
8308/*ARGSUSED*/
8309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008310f_getcwd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008311 typval_T *argvars;
8312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313{
8314 char_u cwd[MAXPATHL];
8315
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008316 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008318 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 else
8320 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008321 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008323 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324#endif
8325 }
8326}
8327
8328/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008329 * "getfontname()" function
8330 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008331/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008333f_getfontname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008334 typval_T *argvars;
8335 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008336{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008337 rettv->v_type = VAR_STRING;
8338 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008339#ifdef FEAT_GUI
8340 if (gui.in_use)
8341 {
8342 GuiFont font;
8343 char_u *name = NULL;
8344
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008345 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008346 {
8347 /* Get the "Normal" font. Either the name saved by
8348 * hl_set_font_name() or from the font ID. */
8349 font = gui.norm_font;
8350 name = hl_get_font_name();
8351 }
8352 else
8353 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008354 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008355 if (STRCMP(name, "*") == 0) /* don't use font dialog */
8356 return;
8357 font = gui_mch_get_font(name, FALSE);
8358 if (font == NOFONT)
8359 return; /* Invalid font name, return empty string. */
8360 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008361 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008362 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008363 gui_mch_free_font(font);
8364 }
8365#endif
8366}
8367
8368/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008369 * "getfperm({fname})" function
8370 */
8371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008372f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008373 typval_T *argvars;
8374 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008375{
8376 char_u *fname;
8377 struct stat st;
8378 char_u *perm = NULL;
8379 char_u flags[] = "rwx";
8380 int i;
8381
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008382 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008383
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008384 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008385 if (mch_stat((char *)fname, &st) >= 0)
8386 {
8387 perm = vim_strsave((char_u *)"---------");
8388 if (perm != NULL)
8389 {
8390 for (i = 0; i < 9; i++)
8391 {
8392 if (st.st_mode & (1 << (8 - i)))
8393 perm[i] = flags[i % 3];
8394 }
8395 }
8396 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008397 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008398}
8399
8400/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 * "getfsize({fname})" function
8402 */
8403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008404f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008405 typval_T *argvars;
8406 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407{
8408 char_u *fname;
8409 struct stat st;
8410
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008411 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008413 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414
8415 if (mch_stat((char *)fname, &st) >= 0)
8416 {
8417 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008418 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008420 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 }
8422 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008423 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424}
8425
8426/*
8427 * "getftime({fname})" function
8428 */
8429 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008430f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008431 typval_T *argvars;
8432 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433{
8434 char_u *fname;
8435 struct stat st;
8436
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008437 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438
8439 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008440 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008442 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443}
8444
8445/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008446 * "getftype({fname})" function
8447 */
8448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008449f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008450 typval_T *argvars;
8451 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008452{
8453 char_u *fname;
8454 struct stat st;
8455 char_u *type = NULL;
8456 char *t;
8457
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008458 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008459
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008460 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008461 if (mch_lstat((char *)fname, &st) >= 0)
8462 {
8463#ifdef S_ISREG
8464 if (S_ISREG(st.st_mode))
8465 t = "file";
8466 else if (S_ISDIR(st.st_mode))
8467 t = "dir";
8468# ifdef S_ISLNK
8469 else if (S_ISLNK(st.st_mode))
8470 t = "link";
8471# endif
8472# ifdef S_ISBLK
8473 else if (S_ISBLK(st.st_mode))
8474 t = "bdev";
8475# endif
8476# ifdef S_ISCHR
8477 else if (S_ISCHR(st.st_mode))
8478 t = "cdev";
8479# endif
8480# ifdef S_ISFIFO
8481 else if (S_ISFIFO(st.st_mode))
8482 t = "fifo";
8483# endif
8484# ifdef S_ISSOCK
8485 else if (S_ISSOCK(st.st_mode))
8486 t = "fifo";
8487# endif
8488 else
8489 t = "other";
8490#else
8491# ifdef S_IFMT
8492 switch (st.st_mode & S_IFMT)
8493 {
8494 case S_IFREG: t = "file"; break;
8495 case S_IFDIR: t = "dir"; break;
8496# ifdef S_IFLNK
8497 case S_IFLNK: t = "link"; break;
8498# endif
8499# ifdef S_IFBLK
8500 case S_IFBLK: t = "bdev"; break;
8501# endif
8502# ifdef S_IFCHR
8503 case S_IFCHR: t = "cdev"; break;
8504# endif
8505# ifdef S_IFIFO
8506 case S_IFIFO: t = "fifo"; break;
8507# endif
8508# ifdef S_IFSOCK
8509 case S_IFSOCK: t = "socket"; break;
8510# endif
8511 default: t = "other";
8512 }
8513# else
8514 if (mch_isdir(fname))
8515 t = "dir";
8516 else
8517 t = "file";
8518# endif
8519#endif
8520 type = vim_strsave((char_u *)t);
8521 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008522 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008523}
8524
8525/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008526 * "getline(lnum)" function
8527 */
8528 static void
8529f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008530 typval_T *argvars;
8531 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008532{
8533 linenr_T lnum;
8534 linenr_T end;
8535 char_u *p;
Bram Moolenaar33570922005-01-25 22:26:29 +00008536 list_T *l;
8537 listitem_T *li;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008538
8539 lnum = get_tv_lnum(argvars);
8540
8541 if (argvars[1].v_type == VAR_UNKNOWN)
8542 {
8543 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8544 p = ml_get(lnum);
8545 else
8546 p = (char_u *)"";
8547
8548 rettv->v_type = VAR_STRING;
8549 rettv->vval.v_string = vim_strsave(p);
8550 }
8551 else
8552 {
8553 end = get_tv_lnum(&argvars[1]);
8554 if (end < lnum)
8555 {
8556 EMSG(_(e_invrange));
8557 rettv->vval.v_number = 0;
8558 }
8559 else
8560 {
8561 l = list_alloc();
8562 if (l != NULL)
8563 {
8564 if (lnum < 1)
8565 lnum = 1;
8566 if (end > curbuf->b_ml.ml_line_count)
8567 end = curbuf->b_ml.ml_line_count;
8568 while (lnum <= end)
8569 {
8570 li = listitem_alloc();
8571 if (li == NULL)
8572 break;
8573 list_append(l, li);
8574 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008575 li->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008576 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
8577 }
8578 rettv->vval.v_list = l;
8579 rettv->v_type = VAR_LIST;
8580 ++l->lv_refcount;
8581 }
8582 }
8583 }
8584}
8585
8586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 * "getreg()" function
8588 */
8589 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008590f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008591 typval_T *argvars;
8592 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593{
8594 char_u *strregname;
8595 int regname;
8596
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008597 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008598 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00008600 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 regname = (strregname == NULL ? '"' : *strregname);
8602 if (regname == 0)
8603 regname = '"';
8604
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008605 rettv->v_type = VAR_STRING;
8606 rettv->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607}
8608
8609/*
8610 * "getregtype()" function
8611 */
8612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008613f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008614 typval_T *argvars;
8615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616{
8617 char_u *strregname;
8618 int regname;
8619 char_u buf[NUMBUFLEN + 2];
8620 long reglen = 0;
8621
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008622 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008623 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 else
8625 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +00008626 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627
8628 regname = (strregname == NULL ? '"' : *strregname);
8629 if (regname == 0)
8630 regname = '"';
8631
8632 buf[0] = NUL;
8633 buf[1] = NUL;
8634 switch (get_reg_type(regname, &reglen))
8635 {
8636 case MLINE: buf[0] = 'V'; break;
8637 case MCHAR: buf[0] = 'v'; break;
8638#ifdef FEAT_VISUAL
8639 case MBLOCK:
8640 buf[0] = Ctrl_V;
8641 sprintf((char *)buf + 1, "%ld", reglen + 1);
8642 break;
8643#endif
8644 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008645 rettv->v_type = VAR_STRING;
8646 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647}
8648
8649/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650 * "getwinposx()" function
8651 */
8652/*ARGSUSED*/
8653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008654f_getwinposx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008655 typval_T *argvars;
8656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008658 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659#ifdef FEAT_GUI
8660 if (gui.in_use)
8661 {
8662 int x, y;
8663
8664 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008665 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 }
8667#endif
8668}
8669
8670/*
8671 * "getwinposy()" function
8672 */
8673/*ARGSUSED*/
8674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008675f_getwinposy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008676 typval_T *argvars;
8677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008679 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680#ifdef FEAT_GUI
8681 if (gui.in_use)
8682 {
8683 int x, y;
8684
8685 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008686 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 }
8688#endif
8689}
8690
8691/*
8692 * "getwinvar()" function
8693 */
8694 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008695f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008696 typval_T *argvars;
8697 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698{
8699 win_T *win, *oldcurwin;
8700 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00008701 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702
8703 ++emsg_off;
8704 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008705 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008707 rettv->v_type = VAR_STRING;
8708 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709
8710 if (win != NULL && varname != NULL)
8711 {
8712 if (*varname == '&') /* window-local-option */
8713 {
8714 /* set curwin to be our win, temporarily */
8715 oldcurwin = curwin;
8716 curwin = win;
8717
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008718 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719
8720 /* restore previous notion of curwin */
8721 curwin = oldcurwin;
8722 }
8723 else
8724 {
8725 /* look up the variable */
Bram Moolenaar33570922005-01-25 22:26:29 +00008726 v = find_var_in_ht(&win->w_vars.dv_hashtab, varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00008728 copy_tv(&v->di_tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 }
8730 }
8731
8732 --emsg_off;
8733}
8734
8735/*
8736 * "glob()" function
8737 */
8738 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008739f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008740 typval_T *argvars;
8741 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742{
8743 expand_T xpc;
8744
8745 ExpandInit(&xpc);
8746 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008747 rettv->v_type = VAR_STRING;
8748 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
8750 ExpandCleanup(&xpc);
8751}
8752
8753/*
8754 * "globpath()" function
8755 */
8756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008757f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008758 typval_T *argvars;
8759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760{
8761 char_u buf1[NUMBUFLEN];
8762
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008763 rettv->v_type = VAR_STRING;
8764 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
8765 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766}
8767
8768/*
8769 * "has()" function
8770 */
8771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008772f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008773 typval_T *argvars;
8774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775{
8776 int i;
8777 char_u *name;
8778 int n = FALSE;
8779 static char *(has_list[]) =
8780 {
8781#ifdef AMIGA
8782 "amiga",
8783# ifdef FEAT_ARP
8784 "arp",
8785# endif
8786#endif
8787#ifdef __BEOS__
8788 "beos",
8789#endif
8790#ifdef MSDOS
8791# ifdef DJGPP
8792 "dos32",
8793# else
8794 "dos16",
8795# endif
8796#endif
8797#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
8798 "mac",
8799#endif
8800#if defined(MACOS_X_UNIX)
8801 "macunix",
8802#endif
8803#ifdef OS2
8804 "os2",
8805#endif
8806#ifdef __QNX__
8807 "qnx",
8808#endif
8809#ifdef RISCOS
8810 "riscos",
8811#endif
8812#ifdef UNIX
8813 "unix",
8814#endif
8815#ifdef VMS
8816 "vms",
8817#endif
8818#ifdef WIN16
8819 "win16",
8820#endif
8821#ifdef WIN32
8822 "win32",
8823#endif
8824#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
8825 "win32unix",
8826#endif
8827#ifdef WIN64
8828 "win64",
8829#endif
8830#ifdef EBCDIC
8831 "ebcdic",
8832#endif
8833#ifndef CASE_INSENSITIVE_FILENAME
8834 "fname_case",
8835#endif
8836#ifdef FEAT_ARABIC
8837 "arabic",
8838#endif
8839#ifdef FEAT_AUTOCMD
8840 "autocmd",
8841#endif
8842#ifdef FEAT_BEVAL
8843 "balloon_eval",
8844#endif
8845#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
8846 "builtin_terms",
8847# ifdef ALL_BUILTIN_TCAPS
8848 "all_builtin_terms",
8849# endif
8850#endif
8851#ifdef FEAT_BYTEOFF
8852 "byte_offset",
8853#endif
8854#ifdef FEAT_CINDENT
8855 "cindent",
8856#endif
8857#ifdef FEAT_CLIENTSERVER
8858 "clientserver",
8859#endif
8860#ifdef FEAT_CLIPBOARD
8861 "clipboard",
8862#endif
8863#ifdef FEAT_CMDL_COMPL
8864 "cmdline_compl",
8865#endif
8866#ifdef FEAT_CMDHIST
8867 "cmdline_hist",
8868#endif
8869#ifdef FEAT_COMMENTS
8870 "comments",
8871#endif
8872#ifdef FEAT_CRYPT
8873 "cryptv",
8874#endif
8875#ifdef FEAT_CSCOPE
8876 "cscope",
8877#endif
8878#ifdef DEBUG
8879 "debug",
8880#endif
8881#ifdef FEAT_CON_DIALOG
8882 "dialog_con",
8883#endif
8884#ifdef FEAT_GUI_DIALOG
8885 "dialog_gui",
8886#endif
8887#ifdef FEAT_DIFF
8888 "diff",
8889#endif
8890#ifdef FEAT_DIGRAPHS
8891 "digraphs",
8892#endif
8893#ifdef FEAT_DND
8894 "dnd",
8895#endif
8896#ifdef FEAT_EMACS_TAGS
8897 "emacs_tags",
8898#endif
8899 "eval", /* always present, of course! */
8900#ifdef FEAT_EX_EXTRA
8901 "ex_extra",
8902#endif
8903#ifdef FEAT_SEARCH_EXTRA
8904 "extra_search",
8905#endif
8906#ifdef FEAT_FKMAP
8907 "farsi",
8908#endif
8909#ifdef FEAT_SEARCHPATH
8910 "file_in_path",
8911#endif
8912#ifdef FEAT_FIND_ID
8913 "find_in_path",
8914#endif
8915#ifdef FEAT_FOLDING
8916 "folding",
8917#endif
8918#ifdef FEAT_FOOTER
8919 "footer",
8920#endif
8921#if !defined(USE_SYSTEM) && defined(UNIX)
8922 "fork",
8923#endif
8924#ifdef FEAT_GETTEXT
8925 "gettext",
8926#endif
8927#ifdef FEAT_GUI
8928 "gui",
8929#endif
8930#ifdef FEAT_GUI_ATHENA
8931# ifdef FEAT_GUI_NEXTAW
8932 "gui_neXtaw",
8933# else
8934 "gui_athena",
8935# endif
8936#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00008937#ifdef FEAT_GUI_KDE
8938 "gui_kde",
8939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940#ifdef FEAT_GUI_GTK
8941 "gui_gtk",
8942# ifdef HAVE_GTK2
8943 "gui_gtk2",
8944# endif
8945#endif
8946#ifdef FEAT_GUI_MAC
8947 "gui_mac",
8948#endif
8949#ifdef FEAT_GUI_MOTIF
8950 "gui_motif",
8951#endif
8952#ifdef FEAT_GUI_PHOTON
8953 "gui_photon",
8954#endif
8955#ifdef FEAT_GUI_W16
8956 "gui_win16",
8957#endif
8958#ifdef FEAT_GUI_W32
8959 "gui_win32",
8960#endif
8961#ifdef FEAT_HANGULIN
8962 "hangul_input",
8963#endif
8964#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
8965 "iconv",
8966#endif
8967#ifdef FEAT_INS_EXPAND
8968 "insert_expand",
8969#endif
8970#ifdef FEAT_JUMPLIST
8971 "jumplist",
8972#endif
8973#ifdef FEAT_KEYMAP
8974 "keymap",
8975#endif
8976#ifdef FEAT_LANGMAP
8977 "langmap",
8978#endif
8979#ifdef FEAT_LIBCALL
8980 "libcall",
8981#endif
8982#ifdef FEAT_LINEBREAK
8983 "linebreak",
8984#endif
8985#ifdef FEAT_LISP
8986 "lispindent",
8987#endif
8988#ifdef FEAT_LISTCMDS
8989 "listcmds",
8990#endif
8991#ifdef FEAT_LOCALMAP
8992 "localmap",
8993#endif
8994#ifdef FEAT_MENU
8995 "menu",
8996#endif
8997#ifdef FEAT_SESSION
8998 "mksession",
8999#endif
9000#ifdef FEAT_MODIFY_FNAME
9001 "modify_fname",
9002#endif
9003#ifdef FEAT_MOUSE
9004 "mouse",
9005#endif
9006#ifdef FEAT_MOUSESHAPE
9007 "mouseshape",
9008#endif
9009#if defined(UNIX) || defined(VMS)
9010# ifdef FEAT_MOUSE_DEC
9011 "mouse_dec",
9012# endif
9013# ifdef FEAT_MOUSE_GPM
9014 "mouse_gpm",
9015# endif
9016# ifdef FEAT_MOUSE_JSB
9017 "mouse_jsbterm",
9018# endif
9019# ifdef FEAT_MOUSE_NET
9020 "mouse_netterm",
9021# endif
9022# ifdef FEAT_MOUSE_PTERM
9023 "mouse_pterm",
9024# endif
9025# ifdef FEAT_MOUSE_XTERM
9026 "mouse_xterm",
9027# endif
9028#endif
9029#ifdef FEAT_MBYTE
9030 "multi_byte",
9031#endif
9032#ifdef FEAT_MBYTE_IME
9033 "multi_byte_ime",
9034#endif
9035#ifdef FEAT_MULTI_LANG
9036 "multi_lang",
9037#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009038#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +00009039#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +00009040 "mzscheme",
9041#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043#ifdef FEAT_OLE
9044 "ole",
9045#endif
9046#ifdef FEAT_OSFILETYPE
9047 "osfiletype",
9048#endif
9049#ifdef FEAT_PATH_EXTRA
9050 "path_extra",
9051#endif
9052#ifdef FEAT_PERL
9053#ifndef DYNAMIC_PERL
9054 "perl",
9055#endif
9056#endif
9057#ifdef FEAT_PYTHON
9058#ifndef DYNAMIC_PYTHON
9059 "python",
9060#endif
9061#endif
9062#ifdef FEAT_POSTSCRIPT
9063 "postscript",
9064#endif
9065#ifdef FEAT_PRINTER
9066 "printer",
9067#endif
9068#ifdef FEAT_QUICKFIX
9069 "quickfix",
9070#endif
9071#ifdef FEAT_RIGHTLEFT
9072 "rightleft",
9073#endif
9074#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
9075 "ruby",
9076#endif
9077#ifdef FEAT_SCROLLBIND
9078 "scrollbind",
9079#endif
9080#ifdef FEAT_CMDL_INFO
9081 "showcmd",
9082 "cmdline_info",
9083#endif
9084#ifdef FEAT_SIGNS
9085 "signs",
9086#endif
9087#ifdef FEAT_SMARTINDENT
9088 "smartindent",
9089#endif
9090#ifdef FEAT_SNIFF
9091 "sniff",
9092#endif
9093#ifdef FEAT_STL_OPT
9094 "statusline",
9095#endif
9096#ifdef FEAT_SUN_WORKSHOP
9097 "sun_workshop",
9098#endif
9099#ifdef FEAT_NETBEANS_INTG
9100 "netbeans_intg",
9101#endif
9102#ifdef FEAT_SYN_HL
9103 "syntax",
9104#endif
9105#if defined(USE_SYSTEM) || !defined(UNIX)
9106 "system",
9107#endif
9108#ifdef FEAT_TAG_BINS
9109 "tag_binary",
9110#endif
9111#ifdef FEAT_TAG_OLDSTATIC
9112 "tag_old_static",
9113#endif
9114#ifdef FEAT_TAG_ANYWHITE
9115 "tag_any_white",
9116#endif
9117#ifdef FEAT_TCL
9118# ifndef DYNAMIC_TCL
9119 "tcl",
9120# endif
9121#endif
9122#ifdef TERMINFO
9123 "terminfo",
9124#endif
9125#ifdef FEAT_TERMRESPONSE
9126 "termresponse",
9127#endif
9128#ifdef FEAT_TEXTOBJ
9129 "textobjects",
9130#endif
9131#ifdef HAVE_TGETENT
9132 "tgetent",
9133#endif
9134#ifdef FEAT_TITLE
9135 "title",
9136#endif
9137#ifdef FEAT_TOOLBAR
9138 "toolbar",
9139#endif
9140#ifdef FEAT_USR_CMDS
9141 "user-commands", /* was accidentally included in 5.4 */
9142 "user_commands",
9143#endif
9144#ifdef FEAT_VIMINFO
9145 "viminfo",
9146#endif
9147#ifdef FEAT_VERTSPLIT
9148 "vertsplit",
9149#endif
9150#ifdef FEAT_VIRTUALEDIT
9151 "virtualedit",
9152#endif
9153#ifdef FEAT_VISUAL
9154 "visual",
9155#endif
9156#ifdef FEAT_VISUALEXTRA
9157 "visualextra",
9158#endif
9159#ifdef FEAT_VREPLACE
9160 "vreplace",
9161#endif
9162#ifdef FEAT_WILDIGN
9163 "wildignore",
9164#endif
9165#ifdef FEAT_WILDMENU
9166 "wildmenu",
9167#endif
9168#ifdef FEAT_WINDOWS
9169 "windows",
9170#endif
9171#ifdef FEAT_WAK
9172 "winaltkeys",
9173#endif
9174#ifdef FEAT_WRITEBACKUP
9175 "writebackup",
9176#endif
9177#ifdef FEAT_XIM
9178 "xim",
9179#endif
9180#ifdef FEAT_XFONTSET
9181 "xfontset",
9182#endif
9183#ifdef USE_XSMP
9184 "xsmp",
9185#endif
9186#ifdef USE_XSMP_INTERACT
9187 "xsmp_interact",
9188#endif
9189#ifdef FEAT_XCLIPBOARD
9190 "xterm_clipboard",
9191#endif
9192#ifdef FEAT_XTERM_SAVE
9193 "xterm_save",
9194#endif
9195#if defined(UNIX) && defined(FEAT_X11)
9196 "X11",
9197#endif
9198 NULL
9199 };
9200
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009201 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 for (i = 0; has_list[i] != NULL; ++i)
9203 if (STRICMP(name, has_list[i]) == 0)
9204 {
9205 n = TRUE;
9206 break;
9207 }
9208
9209 if (n == FALSE)
9210 {
9211 if (STRNICMP(name, "patch", 5) == 0)
9212 n = has_patch(atoi((char *)name + 5));
9213 else if (STRICMP(name, "vim_starting") == 0)
9214 n = (starting != 0);
9215#ifdef DYNAMIC_TCL
9216 else if (STRICMP(name, "tcl") == 0)
9217 n = tcl_enabled(FALSE);
9218#endif
9219#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
9220 else if (STRICMP(name, "iconv") == 0)
9221 n = iconv_enabled(FALSE);
9222#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00009223#ifdef DYNAMIC_MZSCHEME
9224 else if (STRICMP(name, "mzscheme") == 0)
9225 n = mzscheme_enabled(FALSE);
9226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227#ifdef DYNAMIC_RUBY
9228 else if (STRICMP(name, "ruby") == 0)
9229 n = ruby_enabled(FALSE);
9230#endif
9231#ifdef DYNAMIC_PYTHON
9232 else if (STRICMP(name, "python") == 0)
9233 n = python_enabled(FALSE);
9234#endif
9235#ifdef DYNAMIC_PERL
9236 else if (STRICMP(name, "perl") == 0)
9237 n = perl_enabled(FALSE);
9238#endif
9239#ifdef FEAT_GUI
9240 else if (STRICMP(name, "gui_running") == 0)
9241 n = (gui.in_use || gui.starting);
9242# ifdef FEAT_GUI_W32
9243 else if (STRICMP(name, "gui_win32s") == 0)
9244 n = gui_is_win32s();
9245# endif
9246# ifdef FEAT_BROWSE
9247 else if (STRICMP(name, "browse") == 0)
9248 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
9249# endif
9250#endif
9251#ifdef FEAT_SYN_HL
9252 else if (STRICMP(name, "syntax_items") == 0)
9253 n = syntax_present(curbuf);
9254#endif
9255#if defined(WIN3264)
9256 else if (STRICMP(name, "win95") == 0)
9257 n = mch_windows95();
9258#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00009259#ifdef FEAT_NETBEANS_INTG
9260 else if (STRICMP(name, "netbeans_enabled") == 0)
9261 n = usingNetbeans;
9262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 }
9264
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009265 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266}
9267
9268/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00009269 * "has_key()" function
9270 */
9271 static void
9272f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009273 typval_T *argvars;
9274 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009275{
9276 rettv->vval.v_number = 0;
9277 if (argvars[0].v_type != VAR_DICT)
9278 {
9279 EMSG(_(e_dictreq));
9280 return;
9281 }
9282 if (argvars[0].vval.v_dict == NULL)
9283 return;
9284
9285 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009286 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009287}
9288
9289/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 * "hasmapto()" function
9291 */
9292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009293f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009294 typval_T *argvars;
9295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296{
9297 char_u *name;
9298 char_u *mode;
9299 char_u buf[NUMBUFLEN];
9300
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009301 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009302 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 mode = (char_u *)"nvo";
9304 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009305 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306
9307 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009308 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009310 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311}
9312
9313/*
9314 * "histadd()" function
9315 */
9316/*ARGSUSED*/
9317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009318f_histadd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009319 typval_T *argvars;
9320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321{
9322#ifdef FEAT_CMDHIST
9323 int histype;
9324 char_u *str;
9325 char_u buf[NUMBUFLEN];
9326#endif
9327
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009328 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 if (check_restricted() || check_secure())
9330 return;
9331#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009332 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 if (histype >= 0)
9334 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009335 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 if (*str != NUL)
9337 {
9338 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009339 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340 return;
9341 }
9342 }
9343#endif
9344}
9345
9346/*
9347 * "histdel()" function
9348 */
9349/*ARGSUSED*/
9350 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009351f_histdel(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#ifdef FEAT_CMDHIST
9356 int n;
9357 char_u buf[NUMBUFLEN];
9358
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009359 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009361 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009362 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009364 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
9365 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 else
9367 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009368 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
9369 get_tv_string_buf(&argvars[1], buf));
9370 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373#endif
9374}
9375
9376/*
9377 * "histget()" function
9378 */
9379/*ARGSUSED*/
9380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009381f_histget(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009382 typval_T *argvars;
9383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384{
9385#ifdef FEAT_CMDHIST
9386 int type;
9387 int idx;
9388
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009389 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009390 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 idx = get_history_idx(type);
9392 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009393 idx = (int)get_tv_number(&argvars[1]);
9394 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009396 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009398 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399}
9400
9401/*
9402 * "histnr()" function
9403 */
9404/*ARGSUSED*/
9405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009406f_histnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009407 typval_T *argvars;
9408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409{
9410 int i;
9411
9412#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009413 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 if (i >= HIST_CMD && i < HIST_COUNT)
9415 i = get_history_idx(i);
9416 else
9417#endif
9418 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009419 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420}
9421
9422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 * "highlightID(name)" function
9424 */
9425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009426f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009427 typval_T *argvars;
9428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009430 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431}
9432
9433/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009434 * "highlight_exists()" function
9435 */
9436 static void
9437f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009438 typval_T *argvars;
9439 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009440{
9441 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
9442}
9443
9444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 * "hostname()" function
9446 */
9447/*ARGSUSED*/
9448 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009449f_hostname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009450 typval_T *argvars;
9451 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452{
9453 char_u hostname[256];
9454
9455 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009456 rettv->v_type = VAR_STRING;
9457 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458}
9459
9460/*
9461 * iconv() function
9462 */
9463/*ARGSUSED*/
9464 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009465f_iconv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009466 typval_T *argvars;
9467 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468{
9469#ifdef FEAT_MBYTE
9470 char_u buf1[NUMBUFLEN];
9471 char_u buf2[NUMBUFLEN];
9472 char_u *from, *to, *str;
9473 vimconv_T vimconv;
9474#endif
9475
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009476 rettv->v_type = VAR_STRING;
9477 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478
9479#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009480 str = get_tv_string(&argvars[0]);
9481 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
9482 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009483 vimconv.vc_type = CONV_NONE;
9484 convert_setup(&vimconv, from, to);
9485
9486 /* If the encodings are equal, no conversion needed. */
9487 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009488 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009489 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009490 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491
9492 convert_setup(&vimconv, NULL, NULL);
9493 vim_free(from);
9494 vim_free(to);
9495#endif
9496}
9497
9498/*
9499 * "indent()" function
9500 */
9501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009502f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009503 typval_T *argvars;
9504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505{
9506 linenr_T lnum;
9507
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009508 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009510 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009512 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513}
9514
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009515/*
9516 * "index()" function
9517 */
9518 static void
9519f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009520 typval_T *argvars;
9521 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009522{
Bram Moolenaar33570922005-01-25 22:26:29 +00009523 list_T *l;
9524 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009525 long idx = 0;
9526 int ic = FALSE;
9527
9528 rettv->vval.v_number = -1;
9529 if (argvars[0].v_type != VAR_LIST)
9530 {
9531 EMSG(_(e_listreq));
9532 return;
9533 }
9534 l = argvars[0].vval.v_list;
9535 if (l != NULL)
9536 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009537 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009538 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009539 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009540 /* Start at specified item. Use the cached index that list_find()
9541 * sets, so that a negative number also works. */
9542 item = list_find(l, get_tv_number(&argvars[2]));
9543 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009544 if (argvars[3].v_type != VAR_UNKNOWN)
9545 ic = get_tv_number(&argvars[3]);
9546 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009547
Bram Moolenaar758711c2005-02-02 23:11:38 +00009548 for ( ; item != NULL; item = item->li_next, ++idx)
9549 if (tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009550 {
9551 rettv->vval.v_number = idx;
9552 break;
9553 }
9554 }
9555}
9556
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557static int inputsecret_flag = 0;
9558
9559/*
9560 * "input()" function
9561 * Also handles inputsecret() when inputsecret is set.
9562 */
9563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009564f_input(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009565 typval_T *argvars;
9566 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009568 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 char_u *p = NULL;
9570 int c;
9571 char_u buf[NUMBUFLEN];
9572 int cmd_silent_save = cmd_silent;
9573
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009574 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575
9576#ifdef NO_CONSOLE_INPUT
9577 /* While starting up, there is no place to enter text. */
9578 if (no_console_input())
9579 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009580 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009581 return;
9582 }
9583#endif
9584
9585 cmd_silent = FALSE; /* Want to see the prompt. */
9586 if (prompt != NULL)
9587 {
9588 /* Only the part of the message after the last NL is considered as
9589 * prompt for the command line */
9590 p = vim_strrchr(prompt, '\n');
9591 if (p == NULL)
9592 p = prompt;
9593 else
9594 {
9595 ++p;
9596 c = *p;
9597 *p = NUL;
9598 msg_start();
9599 msg_clr_eos();
9600 msg_puts_attr(prompt, echo_attr);
9601 msg_didout = FALSE;
9602 msg_starthere();
9603 *p = c;
9604 }
9605 cmdline_row = msg_row;
9606 }
9607
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009608 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009609 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009611 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
9613
9614 /* since the user typed this, no need to wait for return */
9615 need_wait_return = FALSE;
9616 msg_didout = FALSE;
9617 cmd_silent = cmd_silent_save;
9618}
9619
9620/*
9621 * "inputdialog()" function
9622 */
9623 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009624f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009625 typval_T *argvars;
9626 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627{
9628#if defined(FEAT_GUI_TEXTDIALOG)
9629 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
9630 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
9631 {
9632 char_u *message;
9633 char_u buf[NUMBUFLEN];
9634
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009635 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009636 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009638 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009639 IObuff[IOSIZE - 1] = NUL;
9640 }
9641 else
9642 IObuff[0] = NUL;
9643 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
9644 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009645 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 else
9647 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009648 if (argvars[1].v_type != VAR_UNKNOWN
9649 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009650 rettv->vval.v_string = vim_strsave(
9651 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009653 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009655 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656 }
9657 else
9658#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009659 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660}
9661
9662static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
9663
9664/*
9665 * "inputrestore()" function
9666 */
9667/*ARGSUSED*/
9668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669f_inputrestore(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009670 typval_T *argvars;
9671 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672{
9673 if (ga_userinput.ga_len > 0)
9674 {
9675 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009676 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
9677 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009678 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009679 }
9680 else if (p_verbose > 1)
9681 {
9682 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 }
9685}
9686
9687/*
9688 * "inputsave()" function
9689 */
9690/*ARGSUSED*/
9691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009692f_inputsave(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009693 typval_T *argvars;
9694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695{
9696 /* Add an entry to the stack of typehead storage. */
9697 if (ga_grow(&ga_userinput, 1) == OK)
9698 {
9699 save_typeahead((tasave_T *)(ga_userinput.ga_data)
9700 + ga_userinput.ga_len);
9701 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009702 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 }
9704 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706}
9707
9708/*
9709 * "inputsecret()" function
9710 */
9711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009712f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009713 typval_T *argvars;
9714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715{
9716 ++cmdline_star;
9717 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009718 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 --cmdline_star;
9720 --inputsecret_flag;
9721}
9722
9723/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009724 * "insert()" function
9725 */
9726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009727f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009728 typval_T *argvars;
9729 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009730{
9731 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00009732 listitem_T *item;
9733 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009734
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009735 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009736 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009737 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009738 else if ((l = argvars[0].vval.v_list) != NULL
9739 && !tv_check_lock(l->lv_lock, (char_u *)"insert()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009740 {
9741 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009742 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009743
Bram Moolenaar758711c2005-02-02 23:11:38 +00009744 if (before == l->lv_len)
9745 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009746 else
9747 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00009748 item = list_find(l, before);
9749 if (item == NULL)
9750 {
9751 EMSGN(_(e_listidx), before);
9752 l = NULL;
9753 }
9754 }
9755 if (l != NULL)
9756 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009757 list_insert_tv(l, &argvars[1], item);
9758 ++l->lv_refcount;
9759 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009760 }
9761 }
9762}
9763
9764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765 * "isdirectory()" function
9766 */
9767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009768f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009769 typval_T *argvars;
9770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009772 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773}
9774
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009775static int tv_islocked __ARGS((typval_T *tv));
9776
9777/*
9778 * Return TRUE if typeval "tv" is locked: Either tha value is locked itself or
9779 * it refers to a List or Dictionary that is locked.
9780 */
9781 static int
9782tv_islocked(tv)
9783 typval_T *tv;
9784{
9785 return (tv->v_lock & VAR_LOCKED)
9786 || (tv->v_type == VAR_LIST
9787 && tv->vval.v_list != NULL
9788 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
9789 || (tv->v_type == VAR_DICT
9790 && tv->vval.v_dict != NULL
9791 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
9792}
9793
9794/*
9795 * "islocked()" function
9796 */
9797 static void
9798f_islocked(argvars, rettv)
9799 typval_T *argvars;
9800 typval_T *rettv;
9801{
9802 lval_T lv;
9803 char_u *end;
9804 dictitem_T *di;
9805
9806 rettv->vval.v_number = -1;
9807 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE);
9808 if (end != NULL && lv.ll_name != NULL)
9809 {
9810 if (*end != NUL)
9811 EMSG(_(e_trailing));
9812 else
9813 {
9814 if (lv.ll_tv == NULL)
9815 {
9816 if (check_changedtick(lv.ll_name))
9817 rettv->vval.v_number = 1; /* always locked */
9818 else
9819 {
9820 di = find_var(lv.ll_name, NULL);
9821 if (di != NULL)
9822 {
9823 /* Consider a variable locked when:
9824 * 1. the variable itself is locked
9825 * 2. the value of the variable is locked.
9826 * 3. the List or Dict value is locked.
9827 */
9828 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
9829 || tv_islocked(&di->di_tv));
9830 }
9831 }
9832 }
9833 else if (lv.ll_range)
9834 EMSG(_("E745: Range not allowed"));
9835 else if (lv.ll_newkey != NULL)
9836 EMSG2(_(e_dictkey), lv.ll_newkey);
9837 else if (lv.ll_list != NULL)
9838 /* List item. */
9839 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
9840 else
9841 /* Dictionary item. */
9842 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
9843 }
9844 }
9845
9846 clear_lval(&lv);
9847}
9848
Bram Moolenaar33570922005-01-25 22:26:29 +00009849static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +00009850
9851/*
9852 * Turn a dict into a list:
9853 * "what" == 0: list of keys
9854 * "what" == 1: list of values
9855 * "what" == 2: list of items
9856 */
9857 static void
9858dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +00009859 typval_T *argvars;
9860 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009861 int what;
9862{
Bram Moolenaar33570922005-01-25 22:26:29 +00009863 list_T *l;
9864 list_T *l2;
9865 dictitem_T *di;
9866 hashitem_T *hi;
9867 listitem_T *li;
9868 listitem_T *li2;
9869 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009870 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009871
9872 rettv->vval.v_number = 0;
9873 if (argvars[0].v_type != VAR_DICT)
9874 {
9875 EMSG(_(e_dictreq));
9876 return;
9877 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009878 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00009879 return;
9880
9881 l = list_alloc();
9882 if (l == NULL)
9883 return;
9884 rettv->v_type = VAR_LIST;
9885 rettv->vval.v_list = l;
9886 ++l->lv_refcount;
9887
Bram Moolenaar33570922005-01-25 22:26:29 +00009888 todo = d->dv_hashtab.ht_used;
9889 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00009890 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009891 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00009892 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009893 --todo;
9894 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009895
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009896 li = listitem_alloc();
9897 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00009898 break;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009899 list_append(l, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009900
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009901 if (what == 0)
9902 {
9903 /* keys() */
9904 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009905 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009906 li->li_tv.vval.v_string = vim_strsave(di->di_key);
9907 }
9908 else if (what == 1)
9909 {
9910 /* values() */
9911 copy_tv(&di->di_tv, &li->li_tv);
9912 }
9913 else
9914 {
9915 /* items() */
9916 l2 = list_alloc();
9917 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009918 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009919 li->li_tv.vval.v_list = l2;
9920 if (l2 == NULL)
9921 break;
9922 ++l2->lv_refcount;
9923
9924 li2 = listitem_alloc();
9925 if (li2 == NULL)
9926 break;
9927 list_append(l2, li2);
9928 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009929 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009930 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
9931
9932 li2 = listitem_alloc();
9933 if (li2 == NULL)
9934 break;
9935 list_append(l2, li2);
9936 copy_tv(&di->di_tv, &li2->li_tv);
9937 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00009938 }
9939 }
9940}
9941
9942/*
9943 * "items(dict)" function
9944 */
9945 static void
9946f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009947 typval_T *argvars;
9948 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009949{
9950 dict_list(argvars, rettv, 2);
9951}
9952
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009954 * "join()" function
9955 */
9956 static void
9957f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009958 typval_T *argvars;
9959 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009960{
9961 garray_T ga;
9962 char_u *sep;
9963
9964 rettv->vval.v_number = 0;
9965 if (argvars[0].v_type != VAR_LIST)
9966 {
9967 EMSG(_(e_listreq));
9968 return;
9969 }
9970 if (argvars[0].vval.v_list == NULL)
9971 return;
9972 if (argvars[1].v_type == VAR_UNKNOWN)
9973 sep = (char_u *)" ";
9974 else
9975 sep = get_tv_string(&argvars[1]);
9976
9977 ga_init2(&ga, (int)sizeof(char), 80);
9978 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
9979 ga_append(&ga, NUL);
9980
9981 rettv->v_type = VAR_STRING;
9982 rettv->vval.v_string = (char_u *)ga.ga_data;
9983}
9984
9985/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00009986 * "keys()" function
9987 */
9988 static void
9989f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009990 typval_T *argvars;
9991 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009992{
9993 dict_list(argvars, rettv, 0);
9994}
9995
9996/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997 * "last_buffer_nr()" function.
9998 */
9999/*ARGSUSED*/
10000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010001f_last_buffer_nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010002 typval_T *argvars;
10003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004{
10005 int n = 0;
10006 buf_T *buf;
10007
10008 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
10009 if (n < buf->b_fnum)
10010 n = buf->b_fnum;
10011
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010012 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010013}
10014
10015/*
10016 * "len()" function
10017 */
10018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010019f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010020 typval_T *argvars;
10021 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010022{
10023 switch (argvars[0].v_type)
10024 {
10025 case VAR_STRING:
10026 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010027 rettv->vval.v_number = (varnumber_T)STRLEN(
10028 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010029 break;
10030 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010031 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010032 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010033 case VAR_DICT:
10034 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
10035 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010036 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010037 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010038 break;
10039 }
10040}
10041
Bram Moolenaar33570922005-01-25 22:26:29 +000010042static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010043
10044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010045libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010046 typval_T *argvars;
10047 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010048 int type;
10049{
10050#ifdef FEAT_LIBCALL
10051 char_u *string_in;
10052 char_u **string_result;
10053 int nr_result;
10054#endif
10055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010056 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010057 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010058 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010059 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010060 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010061
10062 if (check_restricted() || check_secure())
10063 return;
10064
10065#ifdef FEAT_LIBCALL
10066 /* The first two args must be strings, otherwise its meaningless */
10067 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
10068 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010069 string_in = NULL;
10070 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010071 string_in = argvars[2].vval.v_string;
10072 if (type == VAR_NUMBER)
10073 string_result = NULL;
10074 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010075 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010076 if (mch_libcall(argvars[0].vval.v_string,
10077 argvars[1].vval.v_string,
10078 string_in,
10079 argvars[2].vval.v_number,
10080 string_result,
10081 &nr_result) == OK
10082 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010083 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010084 }
10085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086}
10087
10088/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010089 * "libcall()" function
10090 */
10091 static void
10092f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010093 typval_T *argvars;
10094 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010095{
10096 libcall_common(argvars, rettv, VAR_STRING);
10097}
10098
10099/*
10100 * "libcallnr()" function
10101 */
10102 static void
10103f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010104 typval_T *argvars;
10105 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010106{
10107 libcall_common(argvars, rettv, VAR_NUMBER);
10108}
10109
10110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111 * "line(string)" function
10112 */
10113 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010114f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010115 typval_T *argvars;
10116 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117{
10118 linenr_T lnum = 0;
10119 pos_T *fp;
10120
10121 fp = var2fpos(&argvars[0], TRUE);
10122 if (fp != NULL)
10123 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010124 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010125}
10126
10127/*
10128 * "line2byte(lnum)" function
10129 */
10130/*ARGSUSED*/
10131 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010132f_line2byte(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010133 typval_T *argvars;
10134 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135{
10136#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010137 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138#else
10139 linenr_T lnum;
10140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010141 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010143 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010145 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
10146 if (rettv->vval.v_number >= 0)
10147 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148#endif
10149}
10150
10151/*
10152 * "lispindent(lnum)" function
10153 */
10154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010155f_lispindent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010156 typval_T *argvars;
10157 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158{
10159#ifdef FEAT_LISP
10160 pos_T pos;
10161 linenr_T lnum;
10162
10163 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010164 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10166 {
10167 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010168 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 curwin->w_cursor = pos;
10170 }
10171 else
10172#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010173 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174}
10175
10176/*
10177 * "localtime()" function
10178 */
10179/*ARGSUSED*/
10180 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010181f_localtime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010182 typval_T *argvars;
10183 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010185 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186}
10187
Bram Moolenaar33570922005-01-25 22:26:29 +000010188static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189
10190 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010191get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000010192 typval_T *argvars;
10193 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 int exact;
10195{
10196 char_u *keys;
10197 char_u *which;
10198 char_u buf[NUMBUFLEN];
10199 char_u *keys_buf = NULL;
10200 char_u *rhs;
10201 int mode;
10202 garray_T ga;
10203
10204 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010205 rettv->v_type = VAR_STRING;
10206 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010208 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 if (*keys == NUL)
10210 return;
10211
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010212 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010213 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 else
10215 which = (char_u *)"";
10216 mode = get_map_mode(&which, 0);
10217
10218 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
10219 rhs = check_map(keys, mode, exact);
10220 vim_free(keys_buf);
10221 if (rhs != NULL)
10222 {
10223 ga_init(&ga);
10224 ga.ga_itemsize = 1;
10225 ga.ga_growsize = 40;
10226
10227 while (*rhs != NUL)
10228 ga_concat(&ga, str2special(&rhs, FALSE));
10229
10230 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010231 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010232 }
10233}
10234
10235/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010236 * "map()" function
10237 */
10238 static void
10239f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010240 typval_T *argvars;
10241 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010242{
10243 filter_map(argvars, rettv, TRUE);
10244}
10245
10246/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010247 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 */
10249 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010250f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010251 typval_T *argvars;
10252 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010254 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010255}
10256
10257/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010258 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259 */
10260 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010261f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010262 typval_T *argvars;
10263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010265 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266}
10267
Bram Moolenaar33570922005-01-25 22:26:29 +000010268static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269
10270 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010271find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000010272 typval_T *argvars;
10273 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 int type;
10275{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010276 char_u *str = NULL;
10277 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278 char_u *pat;
10279 regmatch_T regmatch;
10280 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010281 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010282 char_u *save_cpo;
10283 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010284 long nth = 1;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010285 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000010286 list_T *l = NULL;
10287 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010288 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010289 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290
10291 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10292 save_cpo = p_cpo;
10293 p_cpo = (char_u *)"";
10294
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010295 rettv->vval.v_number = -1;
10296 if (type == 3)
10297 {
10298 /* return empty list when there are no matches */
10299 if ((rettv->vval.v_list = list_alloc()) == NULL)
10300 goto theend;
10301 rettv->v_type = VAR_LIST;
10302 ++rettv->vval.v_list->lv_refcount;
10303 }
10304 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010306 rettv->v_type = VAR_STRING;
10307 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010310 if (argvars[0].v_type == VAR_LIST)
10311 {
10312 if ((l = argvars[0].vval.v_list) == NULL)
10313 goto theend;
10314 li = l->lv_first;
10315 }
10316 else
10317 expr = str = get_tv_string(&argvars[0]);
10318
10319 pat = get_tv_string_buf(&argvars[1], patbuf);
10320
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010321 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010322 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010323 start = get_tv_number(&argvars[2]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010324 if (l != NULL)
10325 {
10326 li = list_find(l, start);
10327 if (li == NULL)
10328 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000010329 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010330 }
10331 else
10332 {
10333 if (start < 0)
10334 start = 0;
10335 if (start > (long)STRLEN(str))
10336 goto theend;
10337 str += start;
10338 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010339
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010340 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010341 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 }
10343
10344 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10345 if (regmatch.regprog != NULL)
10346 {
10347 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010348
10349 while (1)
10350 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010351 if (l != NULL)
10352 {
10353 if (li == NULL)
10354 {
10355 match = FALSE;
10356 break;
10357 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010358 vim_free(tofree);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010359 str = echo_string(&li->li_tv, &tofree, strbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010360 if (str == NULL)
10361 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010362 }
10363
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010364 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010365
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010366 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010367 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010368 if (l == NULL && !match)
10369 break;
10370
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010371 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010372 if (l != NULL)
10373 {
10374 li = li->li_next;
10375 ++idx;
10376 }
10377 else
10378 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010379#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010380 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010381#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010382 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010383#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010384 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010385 }
10386
10387 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010388 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010389 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010390 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010391 int i;
10392
10393 /* return list with matched string and submatches */
10394 for (i = 0; i < NSUBEXP; ++i)
10395 {
10396 if (regmatch.endp[i] == NULL)
10397 break;
10398 li = listitem_alloc();
10399 if (li == NULL)
10400 break;
10401 li->li_tv.v_type = VAR_STRING;
10402 li->li_tv.v_lock = 0;
10403 li->li_tv.vval.v_string = vim_strnsave(regmatch.startp[i],
10404 (int)(regmatch.endp[i] - regmatch.startp[i]));
10405 list_append(rettv->vval.v_list, li);
10406 }
10407 }
10408 else if (type == 2)
10409 {
10410 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010411 if (l != NULL)
10412 copy_tv(&li->li_tv, rettv);
10413 else
10414 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010416 }
10417 else if (l != NULL)
10418 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419 else
10420 {
10421 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010422 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423 (varnumber_T)(regmatch.startp[0] - str);
10424 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010425 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000010426 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010427 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 }
10429 }
10430 vim_free(regmatch.regprog);
10431 }
10432
10433theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010434 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435 p_cpo = save_cpo;
10436}
10437
10438/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010439 * "match()" function
10440 */
10441 static void
10442f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010443 typval_T *argvars;
10444 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010445{
10446 find_some_match(argvars, rettv, 1);
10447}
10448
10449/*
10450 * "matchend()" function
10451 */
10452 static void
10453f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010454 typval_T *argvars;
10455 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010456{
10457 find_some_match(argvars, rettv, 0);
10458}
10459
10460/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010461 * "matchlist()" function
10462 */
10463 static void
10464f_matchlist(argvars, rettv)
10465 typval_T *argvars;
10466 typval_T *rettv;
10467{
10468 find_some_match(argvars, rettv, 3);
10469}
10470
10471/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010472 * "matchstr()" function
10473 */
10474 static void
10475f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010476 typval_T *argvars;
10477 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010478{
10479 find_some_match(argvars, rettv, 2);
10480}
10481
Bram Moolenaar33570922005-01-25 22:26:29 +000010482static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010483
10484 static void
10485max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000010486 typval_T *argvars;
10487 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010488 int domax;
10489{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010490 long n = 0;
10491 long i;
10492
10493 if (argvars[0].v_type == VAR_LIST)
10494 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010495 list_T *l;
10496 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010497
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010498 l = argvars[0].vval.v_list;
10499 if (l != NULL)
10500 {
10501 li = l->lv_first;
10502 if (li != NULL)
10503 {
10504 n = get_tv_number(&li->li_tv);
10505 while (1)
10506 {
10507 li = li->li_next;
10508 if (li == NULL)
10509 break;
10510 i = get_tv_number(&li->li_tv);
10511 if (domax ? i > n : i < n)
10512 n = i;
10513 }
10514 }
10515 }
10516 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010517 else if (argvars[0].v_type == VAR_DICT)
10518 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010519 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010520 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000010521 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010522 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010523
10524 d = argvars[0].vval.v_dict;
10525 if (d != NULL)
10526 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010527 todo = d->dv_hashtab.ht_used;
10528 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010529 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010530 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010531 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010532 --todo;
10533 i = get_tv_number(&HI2DI(hi)->di_tv);
10534 if (first)
10535 {
10536 n = i;
10537 first = FALSE;
10538 }
10539 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010540 n = i;
10541 }
10542 }
10543 }
10544 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010545 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000010546 EMSG(_(e_listdictarg));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010547 rettv->vval.v_number = n;
10548}
10549
10550/*
10551 * "max()" function
10552 */
10553 static void
10554f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010555 typval_T *argvars;
10556 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010557{
10558 max_min(argvars, rettv, TRUE);
10559}
10560
10561/*
10562 * "min()" function
10563 */
10564 static void
10565f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010566 typval_T *argvars;
10567 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010568{
10569 max_min(argvars, rettv, FALSE);
10570}
10571
Bram Moolenaar0d660222005-01-07 21:51:51 +000010572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573 * "mode()" function
10574 */
10575/*ARGSUSED*/
10576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010577f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010578 typval_T *argvars;
10579 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580{
10581 char_u buf[2];
10582
10583#ifdef FEAT_VISUAL
10584 if (VIsual_active)
10585 {
10586 if (VIsual_select)
10587 buf[0] = VIsual_mode + 's' - 'v';
10588 else
10589 buf[0] = VIsual_mode;
10590 }
10591 else
10592#endif
10593 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
10594 buf[0] = 'r';
10595 else if (State & INSERT)
10596 {
10597 if (State & REPLACE_FLAG)
10598 buf[0] = 'R';
10599 else
10600 buf[0] = 'i';
10601 }
10602 else if (State & CMDLINE)
10603 buf[0] = 'c';
10604 else
10605 buf[0] = 'n';
10606
10607 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010608 rettv->vval.v_string = vim_strsave(buf);
10609 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610}
10611
10612/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010613 * "nextnonblank()" function
10614 */
10615 static void
10616f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010617 typval_T *argvars;
10618 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010619{
10620 linenr_T lnum;
10621
10622 for (lnum = get_tv_lnum(argvars); ; ++lnum)
10623 {
10624 if (lnum > curbuf->b_ml.ml_line_count)
10625 {
10626 lnum = 0;
10627 break;
10628 }
10629 if (*skipwhite(ml_get(lnum)) != NUL)
10630 break;
10631 }
10632 rettv->vval.v_number = lnum;
10633}
10634
10635/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 * "nr2char()" function
10637 */
10638 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010639f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010640 typval_T *argvars;
10641 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642{
10643 char_u buf[NUMBUFLEN];
10644
10645#ifdef FEAT_MBYTE
10646 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010647 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 else
10649#endif
10650 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010651 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 buf[1] = NUL;
10653 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010654 rettv->v_type = VAR_STRING;
10655 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010656}
10657
10658/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010659 * "prevnonblank()" function
10660 */
10661 static void
10662f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010663 typval_T *argvars;
10664 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010665{
10666 linenr_T lnum;
10667
10668 lnum = get_tv_lnum(argvars);
10669 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10670 lnum = 0;
10671 else
10672 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
10673 --lnum;
10674 rettv->vval.v_number = lnum;
10675}
10676
Bram Moolenaar8c711452005-01-14 21:53:12 +000010677/*
10678 * "range()" function
10679 */
10680 static void
10681f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010682 typval_T *argvars;
10683 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010684{
10685 long start;
10686 long end;
10687 long stride = 1;
10688 long i;
Bram Moolenaar33570922005-01-25 22:26:29 +000010689 list_T *l;
10690 listitem_T *li;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010691
10692 start = get_tv_number(&argvars[0]);
10693 if (argvars[1].v_type == VAR_UNKNOWN)
10694 {
10695 end = start - 1;
10696 start = 0;
10697 }
10698 else
10699 {
10700 end = get_tv_number(&argvars[1]);
10701 if (argvars[2].v_type != VAR_UNKNOWN)
10702 stride = get_tv_number(&argvars[2]);
10703 }
10704
10705 rettv->vval.v_number = 0;
10706 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010707 EMSG(_("E726: Stride is zero"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010708 else if (stride > 0 ? end < start : end > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000010709 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000010710 else
10711 {
10712 l = list_alloc();
10713 if (l != NULL)
10714 {
10715 rettv->v_type = VAR_LIST;
10716 rettv->vval.v_list = l;
10717 ++l->lv_refcount;
10718
10719 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
10720 {
10721 li = listitem_alloc();
10722 if (li == NULL)
10723 break;
10724 li->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010725 li->li_tv.v_lock = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000010726 li->li_tv.vval.v_number = i;
10727 list_append(l, li);
10728 }
10729 }
10730 }
10731}
10732
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010733/*
10734 * "readfile()" function
10735 */
10736 static void
10737f_readfile(argvars, rettv)
10738 typval_T *argvars;
10739 typval_T *rettv;
10740{
10741 int binary = FALSE;
10742 char_u *fname;
10743 FILE *fd;
10744 list_T *l;
10745 listitem_T *li;
10746#define FREAD_SIZE 200 /* optimized for text lines */
10747 char_u buf[FREAD_SIZE];
10748 int readlen; /* size of last fread() */
10749 int buflen; /* nr of valid chars in buf[] */
10750 int filtd; /* how much in buf[] was NUL -> '\n' filtered */
10751 int tolist; /* first byte in buf[] still to be put in list */
10752 int chop; /* how many CR to chop off */
10753 char_u *prev = NULL; /* previously read bytes, if any */
10754 int prevlen = 0; /* length of "prev" if not NULL */
10755 char_u *s;
10756 int len;
10757
10758 if (argvars[1].v_type != VAR_UNKNOWN
10759 && STRCMP(get_tv_string(&argvars[1]), "b") == 0)
10760 binary = TRUE;
10761
10762 l = list_alloc();
10763 if (l == NULL)
10764 return;
10765 rettv->v_type = VAR_LIST;
10766 rettv->vval.v_list = l;
10767 l->lv_refcount = 1;
10768
10769 /* Always open the file in binary mode, library functions have a mind of
10770 * their own about CR-LF conversion. */
10771 fname = get_tv_string(&argvars[0]);
10772 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
10773 {
10774 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
10775 return;
10776 }
10777
10778 filtd = 0;
10779 for (;;)
10780 {
10781 readlen = fread(buf + filtd, 1, FREAD_SIZE - filtd, fd);
10782 buflen = filtd + readlen;
10783 tolist = 0;
10784 for ( ; filtd < buflen || readlen <= 0; ++filtd)
10785 {
10786 if (buf[filtd] == '\n' || readlen <= 0)
10787 {
10788 /* Only when in binary mode add an empty list item when the
10789 * last line ends in a '\n'. */
10790 if (!binary && readlen == 0 && filtd == 0)
10791 break;
10792
10793 /* Found end-of-line or end-of-file: add a text line to the
10794 * list. */
10795 chop = 0;
10796 if (!binary)
10797 while (filtd - chop - 1 >= tolist
10798 && buf[filtd - chop - 1] == '\r')
10799 ++chop;
10800 len = filtd - tolist - chop;
10801 if (prev == NULL)
10802 s = vim_strnsave(buf + tolist, len);
10803 else
10804 {
10805 s = alloc((unsigned)(prevlen + len + 1));
10806 if (s != NULL)
10807 {
10808 mch_memmove(s, prev, prevlen);
10809 vim_free(prev);
10810 prev = NULL;
10811 mch_memmove(s + prevlen, buf + tolist, len);
10812 s[prevlen + len] = NUL;
10813 }
10814 }
10815 tolist = filtd + 1;
10816
10817 li = listitem_alloc();
10818 if (li == NULL)
10819 {
10820 vim_free(s);
10821 break;
10822 }
10823 li->li_tv.v_type = VAR_STRING;
10824 li->li_tv.v_lock = 0;
10825 li->li_tv.vval.v_string = s;
10826 list_append(l, li);
10827
10828 if (readlen <= 0)
10829 break;
10830 }
10831 else if (buf[filtd] == NUL)
10832 buf[filtd] = '\n';
10833 }
10834 if (readlen <= 0)
10835 break;
10836
10837 if (tolist == 0)
10838 {
10839 /* "buf" is full, need to move text to an allocated buffer */
10840 if (prev == NULL)
10841 {
10842 prev = vim_strnsave(buf, buflen);
10843 prevlen = buflen;
10844 }
10845 else
10846 {
10847 s = alloc((unsigned)(prevlen + buflen));
10848 if (s != NULL)
10849 {
10850 mch_memmove(s, prev, prevlen);
10851 mch_memmove(s + prevlen, buf, buflen);
10852 vim_free(prev);
10853 prev = s;
10854 prevlen += buflen;
10855 }
10856 }
10857 filtd = 0;
10858 }
10859 else
10860 {
10861 mch_memmove(buf, buf + tolist, buflen - tolist);
10862 filtd -= tolist;
10863 }
10864 }
10865
10866 fclose(fd);
10867}
10868
10869
Bram Moolenaar0d660222005-01-07 21:51:51 +000010870#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
10871static void make_connection __ARGS((void));
10872static int check_connection __ARGS((void));
10873
10874 static void
10875make_connection()
10876{
10877 if (X_DISPLAY == NULL
10878# ifdef FEAT_GUI
10879 && !gui.in_use
10880# endif
10881 )
10882 {
10883 x_force_connect = TRUE;
10884 setup_term_clip();
10885 x_force_connect = FALSE;
10886 }
10887}
10888
10889 static int
10890check_connection()
10891{
10892 make_connection();
10893 if (X_DISPLAY == NULL)
10894 {
10895 EMSG(_("E240: No connection to Vim server"));
10896 return FAIL;
10897 }
10898 return OK;
10899}
10900#endif
10901
10902#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000010903static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000010904
10905 static void
10906remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000010907 typval_T *argvars;
10908 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010909 int expr;
10910{
10911 char_u *server_name;
10912 char_u *keys;
10913 char_u *r = NULL;
10914 char_u buf[NUMBUFLEN];
10915# ifdef WIN32
10916 HWND w;
10917# else
10918 Window w;
10919# endif
10920
10921 if (check_restricted() || check_secure())
10922 return;
10923
10924# ifdef FEAT_X11
10925 if (check_connection() == FAIL)
10926 return;
10927# endif
10928
10929 server_name = get_tv_string(&argvars[0]);
10930 keys = get_tv_string_buf(&argvars[1], buf);
10931# ifdef WIN32
10932 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
10933# else
10934 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
10935 < 0)
10936# endif
10937 {
10938 if (r != NULL)
10939 EMSG(r); /* sending worked but evaluation failed */
10940 else
10941 EMSG2(_("E241: Unable to send to %s"), server_name);
10942 return;
10943 }
10944
10945 rettv->vval.v_string = r;
10946
10947 if (argvars[2].v_type != VAR_UNKNOWN)
10948 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010949 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010950 char_u str[30];
10951
10952 sprintf((char *)str, "0x%x", (unsigned int)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000010953 v.di_tv.v_type = VAR_STRING;
10954 v.di_tv.vval.v_string = vim_strsave(str);
10955 set_var(get_tv_string(&argvars[2]), &v.di_tv, FALSE);
10956 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010957 }
10958}
10959#endif
10960
10961/*
10962 * "remote_expr()" function
10963 */
10964/*ARGSUSED*/
10965 static void
10966f_remote_expr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010967 typval_T *argvars;
10968 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010969{
10970 rettv->v_type = VAR_STRING;
10971 rettv->vval.v_string = NULL;
10972#ifdef FEAT_CLIENTSERVER
10973 remote_common(argvars, rettv, TRUE);
10974#endif
10975}
10976
10977/*
10978 * "remote_foreground()" function
10979 */
10980/*ARGSUSED*/
10981 static void
10982f_remote_foreground(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010983 typval_T *argvars;
10984 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010985{
10986 rettv->vval.v_number = 0;
10987#ifdef FEAT_CLIENTSERVER
10988# ifdef WIN32
10989 /* On Win32 it's done in this application. */
10990 serverForeground(get_tv_string(&argvars[0]));
10991# else
10992 /* Send a foreground() expression to the server. */
10993 argvars[1].v_type = VAR_STRING;
10994 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
10995 argvars[2].v_type = VAR_UNKNOWN;
10996 remote_common(argvars, rettv, TRUE);
10997 vim_free(argvars[1].vval.v_string);
10998# endif
10999#endif
11000}
11001
11002/*ARGSUSED*/
11003 static void
11004f_remote_peek(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011005 typval_T *argvars;
11006 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011007{
11008#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000011009 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011010 char_u *s = NULL;
11011# ifdef WIN32
11012 int n = 0;
11013# endif
11014
11015 if (check_restricted() || check_secure())
11016 {
11017 rettv->vval.v_number = -1;
11018 return;
11019 }
11020# ifdef WIN32
11021 sscanf(get_tv_string(&argvars[0]), "%x", &n);
11022 if (n == 0)
11023 rettv->vval.v_number = -1;
11024 else
11025 {
11026 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
11027 rettv->vval.v_number = (s != NULL);
11028 }
11029# else
11030 rettv->vval.v_number = 0;
11031 if (check_connection() == FAIL)
11032 return;
11033
11034 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
11035 serverStrToWin(get_tv_string(&argvars[0])), &s);
11036# endif
11037
11038 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
11039 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011040 v.di_tv.v_type = VAR_STRING;
11041 v.di_tv.vval.v_string = vim_strsave(s);
11042 set_var(get_tv_string(&argvars[1]), &v.di_tv, FALSE);
11043 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011044 }
11045#else
11046 rettv->vval.v_number = -1;
11047#endif
11048}
11049
11050/*ARGSUSED*/
11051 static void
11052f_remote_read(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011053 typval_T *argvars;
11054 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011055{
11056 char_u *r = NULL;
11057
11058#ifdef FEAT_CLIENTSERVER
11059 if (!check_restricted() && !check_secure())
11060 {
11061# ifdef WIN32
11062 /* The server's HWND is encoded in the 'id' parameter */
11063 int n = 0;
11064
11065 sscanf(get_tv_string(&argvars[0]), "%x", &n);
11066 if (n != 0)
11067 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
11068 if (r == NULL)
11069# else
11070 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
11071 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
11072# endif
11073 EMSG(_("E277: Unable to read a server reply"));
11074 }
11075#endif
11076 rettv->v_type = VAR_STRING;
11077 rettv->vval.v_string = r;
11078}
11079
11080/*
11081 * "remote_send()" function
11082 */
11083/*ARGSUSED*/
11084 static void
11085f_remote_send(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011086 typval_T *argvars;
11087 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011088{
11089 rettv->v_type = VAR_STRING;
11090 rettv->vval.v_string = NULL;
11091#ifdef FEAT_CLIENTSERVER
11092 remote_common(argvars, rettv, FALSE);
11093#endif
11094}
11095
11096/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011097 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011098 */
11099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011100f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011101 typval_T *argvars;
11102 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011103{
Bram Moolenaar33570922005-01-25 22:26:29 +000011104 list_T *l;
11105 listitem_T *item, *item2;
11106 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011107 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011108 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011109 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000011110 dict_T *d;
11111 dictitem_T *di;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011112
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011113 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011114 if (argvars[0].v_type == VAR_DICT)
11115 {
11116 if (argvars[2].v_type != VAR_UNKNOWN)
11117 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011118 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar758711c2005-02-02 23:11:38 +000011119 && !tv_check_lock(d->dv_lock, (char_u *)"remove()"))
Bram Moolenaar8c711452005-01-14 21:53:12 +000011120 {
11121 key = get_tv_string(&argvars[1]);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011122 di = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +000011123 if (di == NULL)
11124 EMSG2(_(e_dictkey), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011125 else
11126 {
11127 *rettv = di->di_tv;
11128 init_tv(&di->di_tv);
11129 dictitem_remove(d, di);
11130 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000011131 }
11132 }
11133 else if (argvars[0].v_type != VAR_LIST)
11134 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011135 else if ((l = argvars[0].vval.v_list) != NULL
11136 && !tv_check_lock(l->lv_lock, (char_u *)"remove()"))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011137 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011138 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011139 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011140 if (item == NULL)
11141 EMSGN(_(e_listidx), idx);
11142 else
11143 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011144 if (argvars[2].v_type == VAR_UNKNOWN)
11145 {
11146 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011147 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011148 *rettv = item->li_tv;
11149 vim_free(item);
11150 }
11151 else
11152 {
11153 /* Remove range of items, return list with values. */
11154 end = get_tv_number(&argvars[2]);
11155 item2 = list_find(l, end);
11156 if (item2 == NULL)
11157 EMSGN(_(e_listidx), end);
11158 else
11159 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011160 int cnt = 0;
11161
11162 for (li = item; li != NULL; li = li->li_next)
11163 {
11164 ++cnt;
11165 if (li == item2)
11166 break;
11167 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011168 if (li == NULL) /* didn't find "item2" after "item" */
11169 EMSG(_(e_invrange));
11170 else
11171 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000011172 list_remove(l, item, item2);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011173 l = list_alloc();
11174 if (l != NULL)
11175 {
11176 rettv->v_type = VAR_LIST;
11177 rettv->vval.v_list = l;
11178 l->lv_first = item;
11179 l->lv_last = item2;
11180 l->lv_refcount = 1;
11181 item->li_prev = NULL;
11182 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011183 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011184 }
11185 }
11186 }
11187 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011188 }
11189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011190}
11191
11192/*
11193 * "rename({from}, {to})" function
11194 */
11195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011196f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011197 typval_T *argvars;
11198 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011199{
11200 char_u buf[NUMBUFLEN];
11201
11202 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011203 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011205 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
11206 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207}
11208
11209/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011210 * "repeat()" function
11211 */
11212/*ARGSUSED*/
11213 static void
11214f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011215 typval_T *argvars;
11216 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011217{
11218 char_u *p;
11219 int n;
11220 int slen;
11221 int len;
11222 char_u *r;
11223 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000011224 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011225
11226 n = get_tv_number(&argvars[1]);
11227 if (argvars[0].v_type == VAR_LIST)
11228 {
11229 l = list_alloc();
11230 if (l != NULL && argvars[0].vval.v_list != NULL)
11231 {
11232 l->lv_refcount = 1;
11233 while (n-- > 0)
11234 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
11235 break;
11236 }
11237 rettv->v_type = VAR_LIST;
11238 rettv->vval.v_list = l;
11239 }
11240 else
11241 {
11242 p = get_tv_string(&argvars[0]);
11243 rettv->v_type = VAR_STRING;
11244 rettv->vval.v_string = NULL;
11245
11246 slen = (int)STRLEN(p);
11247 len = slen * n;
11248 if (len <= 0)
11249 return;
11250
11251 r = alloc(len + 1);
11252 if (r != NULL)
11253 {
11254 for (i = 0; i < n; i++)
11255 mch_memmove(r + i * slen, p, (size_t)slen);
11256 r[len] = NUL;
11257 }
11258
11259 rettv->vval.v_string = r;
11260 }
11261}
11262
11263/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011264 * "resolve()" function
11265 */
11266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011267f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011268 typval_T *argvars;
11269 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011270{
11271 char_u *p;
11272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011274#ifdef FEAT_SHORTCUT
11275 {
11276 char_u *v = NULL;
11277
11278 v = mch_resolve_shortcut(p);
11279 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011280 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011282 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 }
11284#else
11285# ifdef HAVE_READLINK
11286 {
11287 char_u buf[MAXPATHL + 1];
11288 char_u *cpy;
11289 int len;
11290 char_u *remain = NULL;
11291 char_u *q;
11292 int is_relative_to_current = FALSE;
11293 int has_trailing_pathsep = FALSE;
11294 int limit = 100;
11295
11296 p = vim_strsave(p);
11297
11298 if (p[0] == '.' && (vim_ispathsep(p[1])
11299 || (p[1] == '.' && (vim_ispathsep(p[2])))))
11300 is_relative_to_current = TRUE;
11301
11302 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011303 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 has_trailing_pathsep = TRUE;
11305
11306 q = getnextcomp(p);
11307 if (*q != NUL)
11308 {
11309 /* Separate the first path component in "p", and keep the
11310 * remainder (beginning with the path separator). */
11311 remain = vim_strsave(q - 1);
11312 q[-1] = NUL;
11313 }
11314
11315 for (;;)
11316 {
11317 for (;;)
11318 {
11319 len = readlink((char *)p, (char *)buf, MAXPATHL);
11320 if (len <= 0)
11321 break;
11322 buf[len] = NUL;
11323
11324 if (limit-- == 0)
11325 {
11326 vim_free(p);
11327 vim_free(remain);
11328 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011329 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330 goto fail;
11331 }
11332
11333 /* Ensure that the result will have a trailing path separator
11334 * if the argument has one. */
11335 if (remain == NULL && has_trailing_pathsep)
11336 add_pathsep(buf);
11337
11338 /* Separate the first path component in the link value and
11339 * concatenate the remainders. */
11340 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
11341 if (*q != NUL)
11342 {
11343 if (remain == NULL)
11344 remain = vim_strsave(q - 1);
11345 else
11346 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011347 cpy = vim_strnsave(q-1, STRLEN(q-1) + STRLEN(remain));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348 if (cpy != NULL)
11349 {
11350 STRCAT(cpy, remain);
11351 vim_free(remain);
11352 remain = cpy;
11353 }
11354 }
11355 q[-1] = NUL;
11356 }
11357
11358 q = gettail(p);
11359 if (q > p && *q == NUL)
11360 {
11361 /* Ignore trailing path separator. */
11362 q[-1] = NUL;
11363 q = gettail(p);
11364 }
11365 if (q > p && !mch_isFullName(buf))
11366 {
11367 /* symlink is relative to directory of argument */
11368 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
11369 if (cpy != NULL)
11370 {
11371 STRCPY(cpy, p);
11372 STRCPY(gettail(cpy), buf);
11373 vim_free(p);
11374 p = cpy;
11375 }
11376 }
11377 else
11378 {
11379 vim_free(p);
11380 p = vim_strsave(buf);
11381 }
11382 }
11383
11384 if (remain == NULL)
11385 break;
11386
11387 /* Append the first path component of "remain" to "p". */
11388 q = getnextcomp(remain + 1);
11389 len = q - remain - (*q != NUL);
11390 cpy = vim_strnsave(p, STRLEN(p) + len);
11391 if (cpy != NULL)
11392 {
11393 STRNCAT(cpy, remain, len);
11394 vim_free(p);
11395 p = cpy;
11396 }
11397 /* Shorten "remain". */
11398 if (*q != NUL)
11399 STRCPY(remain, q - 1);
11400 else
11401 {
11402 vim_free(remain);
11403 remain = NULL;
11404 }
11405 }
11406
11407 /* If the result is a relative path name, make it explicitly relative to
11408 * the current directory if and only if the argument had this form. */
11409 if (!vim_ispathsep(*p))
11410 {
11411 if (is_relative_to_current
11412 && *p != NUL
11413 && !(p[0] == '.'
11414 && (p[1] == NUL
11415 || vim_ispathsep(p[1])
11416 || (p[1] == '.'
11417 && (p[2] == NUL
11418 || vim_ispathsep(p[2]))))))
11419 {
11420 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000011421 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422 if (cpy != NULL)
11423 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424 vim_free(p);
11425 p = cpy;
11426 }
11427 }
11428 else if (!is_relative_to_current)
11429 {
11430 /* Strip leading "./". */
11431 q = p;
11432 while (q[0] == '.' && vim_ispathsep(q[1]))
11433 q += 2;
11434 if (q > p)
11435 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
11436 }
11437 }
11438
11439 /* Ensure that the result will have no trailing path separator
11440 * if the argument had none. But keep "/" or "//". */
11441 if (!has_trailing_pathsep)
11442 {
11443 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000011444 if (after_pathsep(p, q))
11445 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446 }
11447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449 }
11450# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011451 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452# endif
11453#endif
11454
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011455 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011456
11457#ifdef HAVE_READLINK
11458fail:
11459#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011460 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011461}
11462
11463/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011464 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 */
11466 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000011467f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011468 typval_T *argvars;
11469 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470{
Bram Moolenaar33570922005-01-25 22:26:29 +000011471 list_T *l;
11472 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011473
Bram Moolenaar0d660222005-01-07 21:51:51 +000011474 rettv->vval.v_number = 0;
11475 if (argvars[0].v_type != VAR_LIST)
11476 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011477 else if ((l = argvars[0].vval.v_list) != NULL
11478 && !tv_check_lock(l->lv_lock, (char_u *)"reverse()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000011479 {
11480 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011481 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000011482 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011483 while (li != NULL)
11484 {
11485 ni = li->li_prev;
11486 list_append(l, li);
11487 li = ni;
11488 }
11489 rettv->vval.v_list = l;
11490 rettv->v_type = VAR_LIST;
11491 ++l->lv_refcount;
11492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011493}
11494
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011495#define SP_NOMOVE 1 /* don't move cursor */
11496#define SP_REPEAT 2 /* repeat to find outer pair */
11497#define SP_RETCOUNT 4 /* return matchcount */
11498
Bram Moolenaar33570922005-01-25 22:26:29 +000011499static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000011500
11501/*
11502 * Get flags for a search function.
11503 * Possibly sets "p_ws".
11504 * Returns BACKWARD, FORWARD or zero (for an error).
11505 */
11506 static int
11507get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000011508 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011509 int *flagsp;
11510{
11511 int dir = FORWARD;
11512 char_u *flags;
11513 char_u nbuf[NUMBUFLEN];
11514 int mask;
11515
11516 if (varp->v_type != VAR_UNKNOWN)
11517 {
11518 flags = get_tv_string_buf(varp, nbuf);
11519 while (*flags != NUL)
11520 {
11521 switch (*flags)
11522 {
11523 case 'b': dir = BACKWARD; break;
11524 case 'w': p_ws = TRUE; break;
11525 case 'W': p_ws = FALSE; break;
11526 default: mask = 0;
11527 if (flagsp != NULL)
11528 switch (*flags)
11529 {
11530 case 'n': mask = SP_NOMOVE; break;
11531 case 'r': mask = SP_REPEAT; break;
11532 case 'm': mask = SP_RETCOUNT; break;
11533 }
11534 if (mask == 0)
11535 {
11536 EMSG2(_(e_invarg2), flags);
11537 dir = 0;
11538 }
11539 else
11540 *flagsp |= mask;
11541 }
11542 if (dir == 0)
11543 break;
11544 ++flags;
11545 }
11546 }
11547 return dir;
11548}
11549
Bram Moolenaar071d4272004-06-13 20:20:40 +000011550/*
11551 * "search()" function
11552 */
11553 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011554f_search(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011555 typval_T *argvars;
11556 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557{
11558 char_u *pat;
11559 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011560 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011561 int save_p_ws = p_ws;
11562 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011563 int flags = 0;
11564
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011565 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011567 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011568 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
11569 if (dir == 0)
11570 goto theend;
11571 if ((flags & ~SP_NOMOVE) != 0)
11572 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011573 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011574 goto theend;
11575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011576
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011577 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
11579 SEARCH_KEEP, RE_SEARCH) != FAIL)
11580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011581 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011582 curwin->w_cursor = pos;
11583 /* "/$" will put the cursor after the end of the line, may need to
11584 * correct that here */
11585 check_cursor();
11586 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011587
11588 /* If 'n' flag is used: restore cursor position. */
11589 if (flags & SP_NOMOVE)
11590 curwin->w_cursor = save_cursor;
11591theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592 p_ws = save_p_ws;
11593}
11594
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595/*
11596 * "searchpair()" function
11597 */
11598 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011599f_searchpair(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011600 typval_T *argvars;
11601 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011602{
11603 char_u *spat, *mpat, *epat;
11604 char_u *skip;
11605 char_u *pat, *pat2, *pat3;
11606 pos_T pos;
11607 pos_T firstpos;
11608 pos_T save_cursor;
11609 pos_T save_pos;
11610 int save_p_ws = p_ws;
11611 char_u *save_cpo;
11612 int dir;
11613 int flags = 0;
11614 char_u nbuf1[NUMBUFLEN];
11615 char_u nbuf2[NUMBUFLEN];
11616 char_u nbuf3[NUMBUFLEN];
11617 int n;
11618 int r;
11619 int nest = 1;
11620 int err;
11621
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011622 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623
11624 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
11625 save_cpo = p_cpo;
11626 p_cpo = (char_u *)"";
11627
11628 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011629 spat = get_tv_string(&argvars[0]);
11630 mpat = get_tv_string_buf(&argvars[1], nbuf1);
11631 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011632
11633 /* Make two search patterns: start/end (pat2, for in nested pairs) and
11634 * start/middle/end (pat3, for the top pair). */
11635 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
11636 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
11637 if (pat2 == NULL || pat3 == NULL)
11638 goto theend;
11639 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
11640 if (*mpat == NUL)
11641 STRCPY(pat3, pat2);
11642 else
11643 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
11644 spat, epat, mpat);
11645
11646 /* Handle the optional fourth argument: flags */
11647 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011648 if (dir == 0)
11649 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650
11651 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011652 if (argvars[3].v_type == VAR_UNKNOWN
11653 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654 skip = (char_u *)"";
11655 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011656 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011657
11658 save_cursor = curwin->w_cursor;
11659 pos = curwin->w_cursor;
11660 firstpos.lnum = 0;
11661 pat = pat3;
11662 for (;;)
11663 {
11664 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
11665 SEARCH_KEEP, RE_SEARCH);
11666 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
11667 /* didn't find it or found the first match again: FAIL */
11668 break;
11669
11670 if (firstpos.lnum == 0)
11671 firstpos = pos;
11672
11673 /* If the skip pattern matches, ignore this match. */
11674 if (*skip != NUL)
11675 {
11676 save_pos = curwin->w_cursor;
11677 curwin->w_cursor = pos;
11678 r = eval_to_bool(skip, &err, NULL, FALSE);
11679 curwin->w_cursor = save_pos;
11680 if (err)
11681 {
11682 /* Evaluating {skip} caused an error, break here. */
11683 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011684 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685 break;
11686 }
11687 if (r)
11688 continue;
11689 }
11690
11691 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
11692 {
11693 /* Found end when searching backwards or start when searching
11694 * forward: nested pair. */
11695 ++nest;
11696 pat = pat2; /* nested, don't search for middle */
11697 }
11698 else
11699 {
11700 /* Found end when searching forward or start when searching
11701 * backward: end of (nested) pair; or found middle in outer pair. */
11702 if (--nest == 1)
11703 pat = pat3; /* outer level, search for middle */
11704 }
11705
11706 if (nest == 0)
11707 {
11708 /* Found the match: return matchcount or line number. */
11709 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011710 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011711 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011712 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011713 curwin->w_cursor = pos;
11714 if (!(flags & SP_REPEAT))
11715 break;
11716 nest = 1; /* search for next unmatched */
11717 }
11718 }
11719
11720 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011721 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011722 curwin->w_cursor = save_cursor;
11723
11724theend:
11725 vim_free(pat2);
11726 vim_free(pat3);
11727 p_ws = save_p_ws;
11728 p_cpo = save_cpo;
11729}
11730
Bram Moolenaar0d660222005-01-07 21:51:51 +000011731/*ARGSUSED*/
11732 static void
11733f_server2client(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011734 typval_T *argvars;
11735 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011736{
Bram Moolenaar0d660222005-01-07 21:51:51 +000011737#ifdef FEAT_CLIENTSERVER
11738 char_u buf[NUMBUFLEN];
11739 char_u *server = get_tv_string(&argvars[0]);
11740 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741
Bram Moolenaar0d660222005-01-07 21:51:51 +000011742 rettv->vval.v_number = -1;
11743 if (check_restricted() || check_secure())
11744 return;
11745# ifdef FEAT_X11
11746 if (check_connection() == FAIL)
11747 return;
11748# endif
11749
11750 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011752 EMSG(_("E258: Unable to send to client"));
11753 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011755 rettv->vval.v_number = 0;
11756#else
11757 rettv->vval.v_number = -1;
11758#endif
11759}
11760
11761/*ARGSUSED*/
11762 static void
11763f_serverlist(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011764 typval_T *argvars;
11765 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011766{
11767 char_u *r = NULL;
11768
11769#ifdef FEAT_CLIENTSERVER
11770# ifdef WIN32
11771 r = serverGetVimNames();
11772# else
11773 make_connection();
11774 if (X_DISPLAY != NULL)
11775 r = serverGetVimNames(X_DISPLAY);
11776# endif
11777#endif
11778 rettv->v_type = VAR_STRING;
11779 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780}
11781
11782/*
11783 * "setbufvar()" function
11784 */
11785/*ARGSUSED*/
11786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011787f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011788 typval_T *argvars;
11789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790{
11791 buf_T *buf;
11792#ifdef FEAT_AUTOCMD
11793 aco_save_T aco;
11794#else
11795 buf_T *save_curbuf;
11796#endif
11797 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011798 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011799 char_u nbuf[NUMBUFLEN];
11800
11801 if (check_restricted() || check_secure())
11802 return;
11803 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011804 buf = get_buf_tv(&argvars[0]);
11805 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806 varp = &argvars[2];
11807
11808 if (buf != NULL && varname != NULL && varp != NULL)
11809 {
11810 /* set curbuf to be our buf, temporarily */
11811#ifdef FEAT_AUTOCMD
11812 aucmd_prepbuf(&aco, buf);
11813#else
11814 save_curbuf = curbuf;
11815 curbuf = buf;
11816#endif
11817
11818 if (*varname == '&')
11819 {
11820 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011821 set_option_value(varname, get_tv_number(varp),
11822 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823 }
11824 else
11825 {
11826 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
11827 if (bufvarname != NULL)
11828 {
11829 STRCPY(bufvarname, "b:");
11830 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011831 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832 vim_free(bufvarname);
11833 }
11834 }
11835
11836 /* reset notion of buffer */
11837#ifdef FEAT_AUTOCMD
11838 aucmd_restbuf(&aco);
11839#else
11840 curbuf = save_curbuf;
11841#endif
11842 }
11843 --emsg_off;
11844}
11845
11846/*
11847 * "setcmdpos()" function
11848 */
11849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011850f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011851 typval_T *argvars;
11852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011854 rettv->vval.v_number = set_cmdline_pos(
11855 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856}
11857
11858/*
11859 * "setline()" function
11860 */
11861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011862f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011863 typval_T *argvars;
11864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865{
11866 linenr_T lnum;
11867 char_u *line;
11868
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011869 lnum = get_tv_lnum(argvars);
11870 line = get_tv_string(&argvars[1]);
11871 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872
11873 if (lnum >= 1
11874 && lnum <= curbuf->b_ml.ml_line_count
11875 && u_savesub(lnum) == OK
11876 && ml_replace(lnum, line, TRUE) == OK)
11877 {
11878 changed_bytes(lnum, 0);
11879 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011880 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011881 }
11882}
11883
11884/*
11885 * "setreg()" function
11886 */
11887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011888f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011889 typval_T *argvars;
11890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891{
11892 int regname;
11893 char_u *strregname;
11894 char_u *stropt;
11895 int append;
11896 char_u yank_type;
11897 long block_len;
11898
11899 block_len = -1;
11900 yank_type = MAUTO;
11901 append = FALSE;
11902
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011903 strregname = get_tv_string(argvars);
11904 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905
11906 regname = (strregname == NULL ? '"' : *strregname);
11907 if (regname == 0 || regname == '@')
11908 regname = '"';
11909 else if (regname == '=')
11910 return;
11911
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011912 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011913 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011914 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011915 switch (*stropt)
11916 {
11917 case 'a': case 'A': /* append */
11918 append = TRUE;
11919 break;
11920 case 'v': case 'c': /* character-wise selection */
11921 yank_type = MCHAR;
11922 break;
11923 case 'V': case 'l': /* line-wise selection */
11924 yank_type = MLINE;
11925 break;
11926#ifdef FEAT_VISUAL
11927 case 'b': case Ctrl_V: /* block-wise selection */
11928 yank_type = MBLOCK;
11929 if (VIM_ISDIGIT(stropt[1]))
11930 {
11931 ++stropt;
11932 block_len = getdigits(&stropt) - 1;
11933 --stropt;
11934 }
11935 break;
11936#endif
11937 }
11938 }
11939
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011940 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000011941 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011942 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943}
11944
11945
11946/*
11947 * "setwinvar(expr)" function
11948 */
11949/*ARGSUSED*/
11950 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011951f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011952 typval_T *argvars;
11953 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011954{
11955 win_T *win;
11956#ifdef FEAT_WINDOWS
11957 win_T *save_curwin;
11958#endif
11959 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011960 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961 char_u nbuf[NUMBUFLEN];
11962
11963 if (check_restricted() || check_secure())
11964 return;
11965 ++emsg_off;
11966 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011967 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968 varp = &argvars[2];
11969
11970 if (win != NULL && varname != NULL && varp != NULL)
11971 {
11972#ifdef FEAT_WINDOWS
11973 /* set curwin to be our win, temporarily */
11974 save_curwin = curwin;
11975 curwin = win;
11976 curbuf = curwin->w_buffer;
11977#endif
11978
11979 if (*varname == '&')
11980 {
11981 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011982 set_option_value(varname, get_tv_number(varp),
11983 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984 }
11985 else
11986 {
11987 winvarname = alloc((unsigned)STRLEN(varname) + 3);
11988 if (winvarname != NULL)
11989 {
11990 STRCPY(winvarname, "w:");
11991 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011992 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993 vim_free(winvarname);
11994 }
11995 }
11996
11997#ifdef FEAT_WINDOWS
11998 /* Restore current window, if it's still valid (autocomands can make
11999 * it invalid). */
12000 if (win_valid(save_curwin))
12001 {
12002 curwin = save_curwin;
12003 curbuf = curwin->w_buffer;
12004 }
12005#endif
12006 }
12007 --emsg_off;
12008}
12009
12010/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012011 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000012012 */
12013 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012014f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012015 typval_T *argvars;
12016 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012017{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012018 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012019
Bram Moolenaar0d660222005-01-07 21:51:51 +000012020 p = get_tv_string(&argvars[0]);
12021 rettv->vval.v_string = vim_strsave(p);
12022 simplify_filename(rettv->vval.v_string); /* simplify in place */
12023 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024}
12025
Bram Moolenaar0d660222005-01-07 21:51:51 +000012026static int
12027#ifdef __BORLANDC__
12028 _RTLENTRYF
12029#endif
12030 item_compare __ARGS((const void *s1, const void *s2));
12031static int
12032#ifdef __BORLANDC__
12033 _RTLENTRYF
12034#endif
12035 item_compare2 __ARGS((const void *s1, const void *s2));
12036
12037static int item_compare_ic;
12038static char_u *item_compare_func;
12039#define ITEM_COMPARE_FAIL 999
12040
Bram Moolenaar071d4272004-06-13 20:20:40 +000012041/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012042 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012043 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000012044 static int
12045#ifdef __BORLANDC__
12046_RTLENTRYF
12047#endif
12048item_compare(s1, s2)
12049 const void *s1;
12050 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051{
Bram Moolenaar0d660222005-01-07 21:51:51 +000012052 char_u *p1, *p2;
12053 char_u *tofree1, *tofree2;
12054 int res;
12055 char_u numbuf1[NUMBUFLEN];
12056 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057
Bram Moolenaar33570922005-01-25 22:26:29 +000012058 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1);
12059 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012060 if (item_compare_ic)
12061 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012062 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012063 res = STRCMP(p1, p2);
12064 vim_free(tofree1);
12065 vim_free(tofree2);
12066 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012067}
12068
12069 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000012070#ifdef __BORLANDC__
12071_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000012073item_compare2(s1, s2)
12074 const void *s1;
12075 const void *s2;
12076{
12077 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000012078 typval_T rettv;
12079 typval_T argv[2];
Bram Moolenaar0d660222005-01-07 21:51:51 +000012080 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012081
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012082 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
12083 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012084 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
12085 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012086
12087 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
12088 res = call_func(item_compare_func, STRLEN(item_compare_func),
Bram Moolenaare9a41262005-01-15 22:18:47 +000012089 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012090 clear_tv(&argv[0]);
12091 clear_tv(&argv[1]);
12092
12093 if (res == FAIL)
12094 res = ITEM_COMPARE_FAIL;
12095 else
12096 res = get_tv_number(&rettv);
12097 clear_tv(&rettv);
12098 return res;
12099}
12100
12101/*
12102 * "sort({list})" function
12103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000012105f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012106 typval_T *argvars;
12107 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108{
Bram Moolenaar33570922005-01-25 22:26:29 +000012109 list_T *l;
12110 listitem_T *li;
12111 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012112 long len;
12113 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114
Bram Moolenaar0d660222005-01-07 21:51:51 +000012115 rettv->vval.v_number = 0;
12116 if (argvars[0].v_type != VAR_LIST)
12117 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000012118 else
12119 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012120 l = argvars[0].vval.v_list;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012121 if (l == NULL || tv_check_lock(l->lv_lock, (char_u *)"sort()"))
Bram Moolenaar0d660222005-01-07 21:51:51 +000012122 return;
12123 rettv->vval.v_list = l;
12124 rettv->v_type = VAR_LIST;
12125 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126
Bram Moolenaar0d660222005-01-07 21:51:51 +000012127 len = list_len(l);
12128 if (len <= 1)
12129 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012130
Bram Moolenaar0d660222005-01-07 21:51:51 +000012131 item_compare_ic = FALSE;
12132 item_compare_func = NULL;
12133 if (argvars[1].v_type != VAR_UNKNOWN)
12134 {
12135 if (argvars[1].v_type == VAR_FUNC)
12136 item_compare_func = argvars[0].vval.v_string;
12137 else
12138 {
12139 i = get_tv_number(&argvars[1]);
12140 if (i == 1)
12141 item_compare_ic = TRUE;
12142 else
12143 item_compare_func = get_tv_string(&argvars[1]);
12144 }
12145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012146
Bram Moolenaar0d660222005-01-07 21:51:51 +000012147 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012148 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000012149 if (ptrs == NULL)
12150 return;
12151 i = 0;
12152 for (li = l->lv_first; li != NULL; li = li->li_next)
12153 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012154
Bram Moolenaar0d660222005-01-07 21:51:51 +000012155 /* test the compare function */
12156 if (item_compare_func != NULL
12157 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
12158 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012159 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012160 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000012161 {
12162 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000012163 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000012164 item_compare_func == NULL ? item_compare : item_compare2);
12165
12166 /* Clear the List and append the items in the sorted order. */
12167 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012168 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012169 for (i = 0; i < len; ++i)
12170 list_append(l, ptrs[i]);
12171 }
12172
12173 vim_free(ptrs);
12174 }
12175}
12176
12177 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012178f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012179 typval_T *argvars;
12180 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012181{
12182 char_u *str;
12183 char_u *end;
12184 char_u *pat;
12185 regmatch_T regmatch;
12186 char_u patbuf[NUMBUFLEN];
12187 char_u *save_cpo;
12188 int match;
Bram Moolenaar33570922005-01-25 22:26:29 +000012189 listitem_T *ni;
12190 list_T *l;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012191 colnr_T col = 0;
12192
12193 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
12194 save_cpo = p_cpo;
12195 p_cpo = (char_u *)"";
12196
12197 str = get_tv_string(&argvars[0]);
12198 if (argvars[1].v_type == VAR_UNKNOWN)
12199 pat = (char_u *)"[\\x01- ]\\+";
12200 else
12201 pat = get_tv_string_buf(&argvars[1], patbuf);
12202
12203 l = list_alloc();
12204 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012205 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012206 rettv->v_type = VAR_LIST;
12207 rettv->vval.v_list = l;
12208 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012209
Bram Moolenaar0d660222005-01-07 21:51:51 +000012210 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
12211 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012212 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012213 regmatch.rm_ic = FALSE;
12214 while (*str != NUL)
12215 {
12216 match = vim_regexec_nl(&regmatch, str, col);
12217 if (match)
12218 end = regmatch.startp[0];
12219 else
12220 end = str + STRLEN(str);
12221 if (end > str)
12222 {
12223 ni = listitem_alloc();
12224 if (ni == NULL)
12225 break;
12226 ni->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012227 ni->li_tv.v_lock = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012228 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
12229 list_append(l, ni);
12230 }
12231 if (!match)
12232 break;
12233 /* Advance to just after the match. */
12234 if (regmatch.endp[0] > str)
12235 col = 0;
12236 else
12237 {
12238 /* Don't get stuck at the same match. */
12239#ifdef FEAT_MBYTE
12240 col = mb_ptr2len_check(regmatch.endp[0]);
12241#else
12242 col = 1;
12243#endif
12244 }
12245 str = regmatch.endp[0];
12246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247
Bram Moolenaar0d660222005-01-07 21:51:51 +000012248 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012250
Bram Moolenaar0d660222005-01-07 21:51:51 +000012251 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252}
12253
12254#ifdef HAVE_STRFTIME
12255/*
12256 * "strftime({format}[, {time}])" function
12257 */
12258 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012259f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012260 typval_T *argvars;
12261 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262{
12263 char_u result_buf[256];
12264 struct tm *curtime;
12265 time_t seconds;
12266 char_u *p;
12267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012268 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012270 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012271 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272 seconds = time(NULL);
12273 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012274 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012275 curtime = localtime(&seconds);
12276 /* MSVC returns NULL for an invalid value of seconds. */
12277 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012278 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012279 else
12280 {
12281# ifdef FEAT_MBYTE
12282 vimconv_T conv;
12283 char_u *enc;
12284
12285 conv.vc_type = CONV_NONE;
12286 enc = enc_locale();
12287 convert_setup(&conv, p_enc, enc);
12288 if (conv.vc_type != CONV_NONE)
12289 p = string_convert(&conv, p, NULL);
12290# endif
12291 if (p != NULL)
12292 (void)strftime((char *)result_buf, sizeof(result_buf),
12293 (char *)p, curtime);
12294 else
12295 result_buf[0] = NUL;
12296
12297# ifdef FEAT_MBYTE
12298 if (conv.vc_type != CONV_NONE)
12299 vim_free(p);
12300 convert_setup(&conv, enc, p_enc);
12301 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012302 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303 else
12304# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012305 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306
12307# ifdef FEAT_MBYTE
12308 /* Release conversion descriptors */
12309 convert_setup(&conv, NULL, NULL);
12310 vim_free(enc);
12311# endif
12312 }
12313}
12314#endif
12315
12316/*
12317 * "stridx()" function
12318 */
12319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012320f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012321 typval_T *argvars;
12322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323{
12324 char_u buf[NUMBUFLEN];
12325 char_u *needle;
12326 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000012327 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012328 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000012329 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012330
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012331 needle = get_tv_string(&argvars[1]);
Bram Moolenaar33570922005-01-25 22:26:29 +000012332 save_haystack = haystack = get_tv_string_buf(&argvars[0], buf);
12333 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334
Bram Moolenaar33570922005-01-25 22:26:29 +000012335 if (argvars[2].v_type != VAR_UNKNOWN)
12336 {
12337 start_idx = get_tv_number(&argvars[2]);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012338 if (start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000012339 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012340 if (start_idx >= 0)
12341 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000012342 }
12343
12344 pos = (char_u *)strstr((char *)haystack, (char *)needle);
12345 if (pos != NULL)
12346 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012347}
12348
12349/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012350 * "string()" function
12351 */
12352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012353f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012354 typval_T *argvars;
12355 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012356{
12357 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012358 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012359
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012360 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012361 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012362 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012363 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364}
12365
12366/*
12367 * "strlen()" function
12368 */
12369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012370f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012371 typval_T *argvars;
12372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012373{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012374 rettv->vval.v_number = (varnumber_T)(STRLEN(
12375 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012376}
12377
12378/*
12379 * "strpart()" function
12380 */
12381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012382f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012383 typval_T *argvars;
12384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385{
12386 char_u *p;
12387 int n;
12388 int len;
12389 int slen;
12390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012391 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392 slen = (int)STRLEN(p);
12393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012394 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012395 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012396 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397 else
12398 len = slen - n; /* default len: all bytes that are available. */
12399
12400 /*
12401 * Only return the overlap between the specified part and the actual
12402 * string.
12403 */
12404 if (n < 0)
12405 {
12406 len += n;
12407 n = 0;
12408 }
12409 else if (n > slen)
12410 n = slen;
12411 if (len < 0)
12412 len = 0;
12413 else if (n + len > slen)
12414 len = slen - n;
12415
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012416 rettv->v_type = VAR_STRING;
12417 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012418}
12419
12420/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012421 * "strridx()" function
12422 */
12423 static void
12424f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012425 typval_T *argvars;
12426 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012427{
12428 char_u buf[NUMBUFLEN];
12429 char_u *needle;
12430 char_u *haystack;
12431 char_u *rest;
12432 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000012433 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012434
12435 needle = get_tv_string(&argvars[1]);
12436 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012437 haystack_len = STRLEN(haystack);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012438 if (*needle == NUL)
12439 /* Empty string matches past the end. */
Bram Moolenaar532c7802005-01-27 14:44:31 +000012440 lastmatch = haystack + haystack_len;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012441 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000012442 {
12443 if (argvars[2].v_type != VAR_UNKNOWN)
12444 {
12445 /* Third argument: upper limit for index */
12446 end_idx = get_tv_number(&argvars[2]);
12447 if (end_idx < 0)
12448 {
12449 /* can never find a match */
12450 rettv->vval.v_number = -1;
12451 return;
12452 }
12453 }
12454 else
12455 end_idx = haystack_len;
12456
Bram Moolenaar0d660222005-01-07 21:51:51 +000012457 for (rest = haystack; *rest != '\0'; ++rest)
12458 {
12459 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000012460 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012461 break;
12462 lastmatch = rest;
12463 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000012464 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012465
12466 if (lastmatch == NULL)
12467 rettv->vval.v_number = -1;
12468 else
12469 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
12470}
12471
12472/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012473 * "strtrans()" function
12474 */
12475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012476f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012477 typval_T *argvars;
12478 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012480 rettv->v_type = VAR_STRING;
12481 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482}
12483
12484/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012485 * "submatch()" function
12486 */
12487 static void
12488f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012489 typval_T *argvars;
12490 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012491{
12492 rettv->v_type = VAR_STRING;
12493 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
12494}
12495
12496/*
12497 * "substitute()" function
12498 */
12499 static void
12500f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012501 typval_T *argvars;
12502 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012503{
12504 char_u patbuf[NUMBUFLEN];
12505 char_u subbuf[NUMBUFLEN];
12506 char_u flagsbuf[NUMBUFLEN];
12507
12508 rettv->v_type = VAR_STRING;
12509 rettv->vval.v_string = do_string_sub(
12510 get_tv_string(&argvars[0]),
12511 get_tv_string_buf(&argvars[1], patbuf),
12512 get_tv_string_buf(&argvars[2], subbuf),
12513 get_tv_string_buf(&argvars[3], flagsbuf));
12514}
12515
12516/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012517 * "synID(line, col, trans)" function
12518 */
12519/*ARGSUSED*/
12520 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012521f_synID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012522 typval_T *argvars;
12523 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524{
12525 int id = 0;
12526#ifdef FEAT_SYN_HL
12527 long line;
12528 long col;
12529 int trans;
12530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012531 line = get_tv_lnum(argvars);
12532 col = get_tv_number(&argvars[1]) - 1;
12533 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534
12535 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
12536 && col >= 0 && col < (long)STRLEN(ml_get(line)))
12537 id = syn_get_id(line, col, trans);
12538#endif
12539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012540 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012541}
12542
12543/*
12544 * "synIDattr(id, what [, mode])" function
12545 */
12546/*ARGSUSED*/
12547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012548f_synIDattr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012549 typval_T *argvars;
12550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551{
12552 char_u *p = NULL;
12553#ifdef FEAT_SYN_HL
12554 int id;
12555 char_u *what;
12556 char_u *mode;
12557 char_u modebuf[NUMBUFLEN];
12558 int modec;
12559
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012560 id = get_tv_number(&argvars[0]);
12561 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012562 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012564 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565 modec = TOLOWER_ASC(mode[0]);
12566 if (modec != 't' && modec != 'c'
12567#ifdef FEAT_GUI
12568 && modec != 'g'
12569#endif
12570 )
12571 modec = 0; /* replace invalid with current */
12572 }
12573 else
12574 {
12575#ifdef FEAT_GUI
12576 if (gui.in_use)
12577 modec = 'g';
12578 else
12579#endif
12580 if (t_colors > 1)
12581 modec = 'c';
12582 else
12583 modec = 't';
12584 }
12585
12586
12587 switch (TOLOWER_ASC(what[0]))
12588 {
12589 case 'b':
12590 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
12591 p = highlight_color(id, what, modec);
12592 else /* bold */
12593 p = highlight_has_attr(id, HL_BOLD, modec);
12594 break;
12595
12596 case 'f': /* fg[#] */
12597 p = highlight_color(id, what, modec);
12598 break;
12599
12600 case 'i':
12601 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
12602 p = highlight_has_attr(id, HL_INVERSE, modec);
12603 else /* italic */
12604 p = highlight_has_attr(id, HL_ITALIC, modec);
12605 break;
12606
12607 case 'n': /* name */
12608 p = get_highlight_name(NULL, id - 1);
12609 break;
12610
12611 case 'r': /* reverse */
12612 p = highlight_has_attr(id, HL_INVERSE, modec);
12613 break;
12614
12615 case 's': /* standout */
12616 p = highlight_has_attr(id, HL_STANDOUT, modec);
12617 break;
12618
12619 case 'u': /* underline */
12620 p = highlight_has_attr(id, HL_UNDERLINE, modec);
12621 break;
12622 }
12623
12624 if (p != NULL)
12625 p = vim_strsave(p);
12626#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012627 rettv->v_type = VAR_STRING;
12628 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012629}
12630
12631/*
12632 * "synIDtrans(id)" function
12633 */
12634/*ARGSUSED*/
12635 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012636f_synIDtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012637 typval_T *argvars;
12638 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639{
12640 int id;
12641
12642#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012643 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012644
12645 if (id > 0)
12646 id = syn_get_final_id(id);
12647 else
12648#endif
12649 id = 0;
12650
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012651 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652}
12653
12654/*
12655 * "system()" function
12656 */
12657 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012658f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012659 typval_T *argvars;
12660 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012661{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012662 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012663 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012664 char_u *infile = NULL;
12665 char_u buf[NUMBUFLEN];
12666 int err = FALSE;
12667 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012669 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012670 {
12671 /*
12672 * Write the string to a temp file, to be used for input of the shell
12673 * command.
12674 */
12675 if ((infile = vim_tempname('i')) == NULL)
12676 {
12677 EMSG(_(e_notmp));
12678 return;
12679 }
12680
12681 fd = mch_fopen((char *)infile, WRITEBIN);
12682 if (fd == NULL)
12683 {
12684 EMSG2(_(e_notopen), infile);
12685 goto done;
12686 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012687 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012688 if (fwrite(p, STRLEN(p), 1, fd) != 1)
12689 err = TRUE;
12690 if (fclose(fd) != 0)
12691 err = TRUE;
12692 if (err)
12693 {
12694 EMSG(_("E677: Error writing temp file"));
12695 goto done;
12696 }
12697 }
12698
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012699 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012700
Bram Moolenaar071d4272004-06-13 20:20:40 +000012701#ifdef USE_CR
12702 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012703 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704 {
12705 char_u *s;
12706
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012707 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708 {
12709 if (*s == CAR)
12710 *s = NL;
12711 }
12712 }
12713#else
12714# ifdef USE_CRNL
12715 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012716 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717 {
12718 char_u *s, *d;
12719
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012720 d = res;
12721 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722 {
12723 if (s[0] == CAR && s[1] == NL)
12724 ++s;
12725 *d++ = *s;
12726 }
12727 *d = NUL;
12728 }
12729# endif
12730#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000012731
12732done:
12733 if (infile != NULL)
12734 {
12735 mch_remove(infile);
12736 vim_free(infile);
12737 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012738 rettv->v_type = VAR_STRING;
12739 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740}
12741
12742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743 * "tempname()" function
12744 */
12745/*ARGSUSED*/
12746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012747f_tempname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012748 typval_T *argvars;
12749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750{
12751 static int x = 'A';
12752
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012753 rettv->v_type = VAR_STRING;
12754 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012755
12756 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
12757 * names. Skip 'I' and 'O', they are used for shell redirection. */
12758 do
12759 {
12760 if (x == 'Z')
12761 x = '0';
12762 else if (x == '9')
12763 x = 'A';
12764 else
12765 {
12766#ifdef EBCDIC
12767 if (x == 'I')
12768 x = 'J';
12769 else if (x == 'R')
12770 x = 'S';
12771 else
12772#endif
12773 ++x;
12774 }
12775 } while (x == 'I' || x == 'O');
12776}
12777
12778/*
12779 * "tolower(string)" function
12780 */
12781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012782f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012783 typval_T *argvars;
12784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785{
12786 char_u *p;
12787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012788 p = vim_strsave(get_tv_string(&argvars[0]));
12789 rettv->v_type = VAR_STRING;
12790 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791
12792 if (p != NULL)
12793 while (*p != NUL)
12794 {
12795#ifdef FEAT_MBYTE
12796 int l;
12797
12798 if (enc_utf8)
12799 {
12800 int c, lc;
12801
12802 c = utf_ptr2char(p);
12803 lc = utf_tolower(c);
12804 l = utf_ptr2len_check(p);
12805 /* TODO: reallocate string when byte count changes. */
12806 if (utf_char2len(lc) == l)
12807 utf_char2bytes(lc, p);
12808 p += l;
12809 }
12810 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
12811 p += l; /* skip multi-byte character */
12812 else
12813#endif
12814 {
12815 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
12816 ++p;
12817 }
12818 }
12819}
12820
12821/*
12822 * "toupper(string)" function
12823 */
12824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012825f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012826 typval_T *argvars;
12827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828{
12829 char_u *p;
12830
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012831 p = vim_strsave(get_tv_string(&argvars[0]));
12832 rettv->v_type = VAR_STRING;
12833 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834
12835 if (p != NULL)
12836 while (*p != NUL)
12837 {
12838#ifdef FEAT_MBYTE
12839 int l;
12840
12841 if (enc_utf8)
12842 {
12843 int c, uc;
12844
12845 c = utf_ptr2char(p);
12846 uc = utf_toupper(c);
12847 l = utf_ptr2len_check(p);
12848 /* TODO: reallocate string when byte count changes. */
12849 if (utf_char2len(uc) == l)
12850 utf_char2bytes(uc, p);
12851 p += l;
12852 }
12853 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
12854 p += l; /* skip multi-byte character */
12855 else
12856#endif
12857 {
12858 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
12859 p++;
12860 }
12861 }
12862}
12863
12864/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000012865 * "tr(string, fromstr, tostr)" function
12866 */
12867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012868f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012869 typval_T *argvars;
12870 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012871{
12872 char_u *instr;
12873 char_u *fromstr;
12874 char_u *tostr;
12875 char_u *p;
12876#ifdef FEAT_MBYTE
12877 int inlen;
12878 int fromlen;
12879 int tolen;
12880 int idx;
12881 char_u *cpstr;
12882 int cplen;
12883 int first = TRUE;
12884#endif
12885 char_u buf[NUMBUFLEN];
12886 char_u buf2[NUMBUFLEN];
12887 garray_T ga;
12888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012889 instr = get_tv_string(&argvars[0]);
12890 fromstr = get_tv_string_buf(&argvars[1], buf);
12891 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000012892
12893 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012894 rettv->v_type = VAR_STRING;
12895 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012896 ga_init2(&ga, (int)sizeof(char), 80);
12897
12898#ifdef FEAT_MBYTE
12899 if (!has_mbyte)
12900#endif
12901 /* not multi-byte: fromstr and tostr must be the same length */
12902 if (STRLEN(fromstr) != STRLEN(tostr))
12903 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012904#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000012905error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012906#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000012907 EMSG2(_(e_invarg2), fromstr);
12908 ga_clear(&ga);
12909 return;
12910 }
12911
12912 /* fromstr and tostr have to contain the same number of chars */
12913 while (*instr != NUL)
12914 {
12915#ifdef FEAT_MBYTE
12916 if (has_mbyte)
12917 {
12918 inlen = mb_ptr2len_check(instr);
12919 cpstr = instr;
12920 cplen = inlen;
12921 idx = 0;
12922 for (p = fromstr; *p != NUL; p += fromlen)
12923 {
12924 fromlen = mb_ptr2len_check(p);
12925 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
12926 {
12927 for (p = tostr; *p != NUL; p += tolen)
12928 {
12929 tolen = mb_ptr2len_check(p);
12930 if (idx-- == 0)
12931 {
12932 cplen = tolen;
12933 cpstr = p;
12934 break;
12935 }
12936 }
12937 if (*p == NUL) /* tostr is shorter than fromstr */
12938 goto error;
12939 break;
12940 }
12941 ++idx;
12942 }
12943
12944 if (first && cpstr == instr)
12945 {
12946 /* Check that fromstr and tostr have the same number of
12947 * (multi-byte) characters. Done only once when a character
12948 * of instr doesn't appear in fromstr. */
12949 first = FALSE;
12950 for (p = tostr; *p != NUL; p += tolen)
12951 {
12952 tolen = mb_ptr2len_check(p);
12953 --idx;
12954 }
12955 if (idx != 0)
12956 goto error;
12957 }
12958
12959 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000012960 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000012961 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012962
12963 instr += inlen;
12964 }
12965 else
12966#endif
12967 {
12968 /* When not using multi-byte chars we can do it faster. */
12969 p = vim_strchr(fromstr, *instr);
12970 if (p != NULL)
12971 ga_append(&ga, tostr[p - fromstr]);
12972 else
12973 ga_append(&ga, *instr);
12974 ++instr;
12975 }
12976 }
12977
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012978 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000012979}
12980
12981/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982 * "type(expr)" function
12983 */
12984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012985f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012986 typval_T *argvars;
12987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012989 int n;
12990
12991 switch (argvars[0].v_type)
12992 {
12993 case VAR_NUMBER: n = 0; break;
12994 case VAR_STRING: n = 1; break;
12995 case VAR_FUNC: n = 2; break;
12996 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000012997 case VAR_DICT: n = 4; break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000012998 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
12999 }
13000 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013001}
13002
13003/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013004 * "values(dict)" function
13005 */
13006 static void
13007f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013008 typval_T *argvars;
13009 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013010{
13011 dict_list(argvars, rettv, 1);
13012}
13013
13014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013015 * "virtcol(string)" function
13016 */
13017 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013018f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013019 typval_T *argvars;
13020 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013021{
13022 colnr_T vcol = 0;
13023 pos_T *fp;
13024
13025 fp = var2fpos(&argvars[0], FALSE);
13026 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
13027 {
13028 getvvcol(curwin, fp, NULL, NULL, &vcol);
13029 ++vcol;
13030 }
13031
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013032 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033}
13034
13035/*
13036 * "visualmode()" function
13037 */
13038/*ARGSUSED*/
13039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013040f_visualmode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013041 typval_T *argvars;
13042 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043{
13044#ifdef FEAT_VISUAL
13045 char_u str[2];
13046
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013047 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048 str[0] = curbuf->b_visual_mode_eval;
13049 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013050 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013051
13052 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013053 if ((argvars[0].v_type == VAR_NUMBER
13054 && argvars[0].vval.v_number != 0)
13055 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013056 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057 curbuf->b_visual_mode_eval = NUL;
13058#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013059 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060#endif
13061}
13062
13063/*
13064 * "winbufnr(nr)" function
13065 */
13066 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013067f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013068 typval_T *argvars;
13069 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013070{
13071 win_T *wp;
13072
13073 wp = find_win_by_nr(&argvars[0]);
13074 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013075 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013076 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013077 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013078}
13079
13080/*
13081 * "wincol()" function
13082 */
13083/*ARGSUSED*/
13084 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013085f_wincol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013086 typval_T *argvars;
13087 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013088{
13089 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013090 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013091}
13092
13093/*
13094 * "winheight(nr)" function
13095 */
13096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013097f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013098 typval_T *argvars;
13099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013100{
13101 win_T *wp;
13102
13103 wp = find_win_by_nr(&argvars[0]);
13104 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013105 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013107 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108}
13109
13110/*
13111 * "winline()" function
13112 */
13113/*ARGSUSED*/
13114 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013115f_winline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013116 typval_T *argvars;
13117 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118{
13119 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013120 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013121}
13122
13123/*
13124 * "winnr()" function
13125 */
13126/* ARGSUSED */
13127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013128f_winnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013129 typval_T *argvars;
13130 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013131{
13132 int nr = 1;
13133#ifdef FEAT_WINDOWS
13134 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013135 win_T *twin = curwin;
13136 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013138 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013139 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013140 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013141 if (STRCMP(arg, "$") == 0)
13142 twin = lastwin;
13143 else if (STRCMP(arg, "#") == 0)
13144 {
13145 twin = prevwin;
13146 if (prevwin == NULL)
13147 nr = 0;
13148 }
13149 else
13150 {
13151 EMSG2(_(e_invexpr2), arg);
13152 nr = 0;
13153 }
13154 }
13155
13156 if (nr > 0)
13157 for (wp = firstwin; wp != twin; wp = wp->w_next)
13158 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013160 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013161}
13162
13163/*
13164 * "winrestcmd()" function
13165 */
13166/* ARGSUSED */
13167 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013168f_winrestcmd(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013169 typval_T *argvars;
13170 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171{
13172#ifdef FEAT_WINDOWS
13173 win_T *wp;
13174 int winnr = 1;
13175 garray_T ga;
13176 char_u buf[50];
13177
13178 ga_init2(&ga, (int)sizeof(char), 70);
13179 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13180 {
13181 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
13182 ga_concat(&ga, buf);
13183# ifdef FEAT_VERTSPLIT
13184 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
13185 ga_concat(&ga, buf);
13186# endif
13187 ++winnr;
13188 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000013189 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013191 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013193 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013195 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196}
13197
13198/*
13199 * "winwidth(nr)" function
13200 */
13201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013202f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013203 typval_T *argvars;
13204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205{
13206 win_T *wp;
13207
13208 wp = find_win_by_nr(&argvars[0]);
13209 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013210 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013211 else
13212#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013213 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013215 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216#endif
13217}
13218
13219 static win_T *
13220find_win_by_nr(vp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013221 typval_T *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222{
13223#ifdef FEAT_WINDOWS
13224 win_T *wp;
13225#endif
13226 int nr;
13227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013228 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229
13230#ifdef FEAT_WINDOWS
13231 if (nr == 0)
13232 return curwin;
13233
13234 for (wp = firstwin; wp != NULL; wp = wp->w_next)
13235 if (--nr <= 0)
13236 break;
13237 return wp;
13238#else
13239 if (nr == 0 || nr == 1)
13240 return curwin;
13241 return NULL;
13242#endif
13243}
13244
13245/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013246 * "writefile()" function
13247 */
13248 static void
13249f_writefile(argvars, rettv)
13250 typval_T *argvars;
13251 typval_T *rettv;
13252{
13253 int binary = FALSE;
13254 char_u *fname;
13255 FILE *fd;
13256 listitem_T *li;
13257 char_u *s;
13258 int ret = 0;
13259 int c;
13260
13261 if (argvars[0].v_type != VAR_LIST)
13262 {
13263 EMSG2(_(e_listarg), "writefile()");
13264 return;
13265 }
13266 if (argvars[0].vval.v_list == NULL)
13267 return;
13268
13269 if (argvars[2].v_type != VAR_UNKNOWN
13270 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
13271 binary = TRUE;
13272
13273 /* Always open the file in binary mode, library functions have a mind of
13274 * their own about CR-LF conversion. */
13275 fname = get_tv_string(&argvars[1]);
13276 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
13277 {
13278 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
13279 ret = -1;
13280 }
13281 else
13282 {
13283 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
13284 li = li->li_next)
13285 {
13286 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
13287 {
13288 if (*s == '\n')
13289 c = putc(NUL, fd);
13290 else
13291 c = putc(*s, fd);
13292 if (c == EOF)
13293 {
13294 ret = -1;
13295 break;
13296 }
13297 }
13298 if (!binary || li->li_next != NULL)
13299 if (putc('\n', fd) == EOF)
13300 {
13301 ret = -1;
13302 break;
13303 }
13304 if (ret < 0)
13305 {
13306 EMSG(_(e_write));
13307 break;
13308 }
13309 }
13310 fclose(fd);
13311 }
13312
13313 rettv->vval.v_number = ret;
13314}
13315
13316/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013317 * Translate a String variable into a position.
13318 */
13319 static pos_T *
13320var2fpos(varp, lnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000013321 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322 int lnum; /* TRUE when $ is last line */
13323{
13324 char_u *name;
13325 static pos_T pos;
13326 pos_T *pp;
13327
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013328 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329 if (name[0] == '.') /* cursor */
13330 return &curwin->w_cursor;
13331 if (name[0] == '\'') /* mark */
13332 {
13333 pp = getmark(name[1], FALSE);
13334 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
13335 return NULL;
13336 return pp;
13337 }
13338 if (name[0] == '$') /* last column or line */
13339 {
13340 if (lnum)
13341 {
13342 pos.lnum = curbuf->b_ml.ml_line_count;
13343 pos.col = 0;
13344 }
13345 else
13346 {
13347 pos.lnum = curwin->w_cursor.lnum;
13348 pos.col = (colnr_T)STRLEN(ml_get_curline());
13349 }
13350 return &pos;
13351 }
13352 return NULL;
13353}
13354
13355/*
13356 * Get the length of an environment variable name.
13357 * Advance "arg" to the first character after the name.
13358 * Return 0 for error.
13359 */
13360 static int
13361get_env_len(arg)
13362 char_u **arg;
13363{
13364 char_u *p;
13365 int len;
13366
13367 for (p = *arg; vim_isIDc(*p); ++p)
13368 ;
13369 if (p == *arg) /* no name found */
13370 return 0;
13371
13372 len = (int)(p - *arg);
13373 *arg = p;
13374 return len;
13375}
13376
13377/*
13378 * Get the length of the name of a function or internal variable.
13379 * "arg" is advanced to the first non-white character after the name.
13380 * Return 0 if something is wrong.
13381 */
13382 static int
13383get_id_len(arg)
13384 char_u **arg;
13385{
13386 char_u *p;
13387 int len;
13388
13389 /* Find the end of the name. */
13390 for (p = *arg; eval_isnamec(*p); ++p)
13391 ;
13392 if (p == *arg) /* no name found */
13393 return 0;
13394
13395 len = (int)(p - *arg);
13396 *arg = skipwhite(p);
13397
13398 return len;
13399}
13400
13401/*
Bram Moolenaara7043832005-01-21 11:56:39 +000013402 * Get the length of the name of a variable or function.
13403 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013405 * Return -1 if curly braces expansion failed.
13406 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407 * If the name contains 'magic' {}'s, expand them and return the
13408 * expanded name in an allocated string via 'alias' - caller must free.
13409 */
13410 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013411get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412 char_u **arg;
13413 char_u **alias;
13414 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013415 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416{
13417 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013418 char_u *p;
13419 char_u *expr_start;
13420 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013421
13422 *alias = NULL; /* default to no alias */
13423
13424 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
13425 && (*arg)[2] == (int)KE_SNR)
13426 {
13427 /* hard coded <SNR>, already translated */
13428 *arg += 3;
13429 return get_id_len(arg) + 3;
13430 }
13431 len = eval_fname_script(*arg);
13432 if (len > 0)
13433 {
13434 /* literal "<SID>", "s:" or "<SNR>" */
13435 *arg += len;
13436 }
13437
Bram Moolenaar071d4272004-06-13 20:20:40 +000013438 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013439 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013441 p = find_name_end(*arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 if (expr_start != NULL)
13443 {
13444 char_u *temp_string;
13445
13446 if (!evaluate)
13447 {
13448 len += (int)(p - *arg);
13449 *arg = skipwhite(p);
13450 return len;
13451 }
13452
13453 /*
13454 * Include any <SID> etc in the expanded string:
13455 * Thus the -len here.
13456 */
13457 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
13458 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013459 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460 *alias = temp_string;
13461 *arg = skipwhite(p);
13462 return (int)STRLEN(temp_string);
13463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464
13465 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013466 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467 EMSG2(_(e_invexpr2), *arg);
13468
13469 return len;
13470}
13471
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013472/*
13473 * Find the end of a variable or function name, taking care of magic braces.
13474 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
13475 * start and end of the first magic braces item.
13476 * Return a pointer to just after the name. Equal to "arg" if there is no
13477 * valid name.
13478 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013480find_name_end(arg, expr_start, expr_end, incl_br)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481 char_u *arg;
13482 char_u **expr_start;
13483 char_u **expr_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013484 int incl_br; /* Include [] indexes and .name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013486 int mb_nest = 0;
13487 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488 char_u *p;
13489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013490 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013492 *expr_start = NULL;
13493 *expr_end = NULL;
13494 }
13495
13496 for (p = arg; *p != NUL
13497 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013498 || *p == '{'
Bram Moolenaar8c711452005-01-14 21:53:12 +000013499 || (incl_br && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013500 || mb_nest != 0
13501 || br_nest != 0); ++p)
13502 {
13503 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013505 if (*p == '[')
13506 ++br_nest;
13507 else if (*p == ']')
13508 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013509 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013510 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013512 if (*p == '{')
13513 {
13514 mb_nest++;
13515 if (expr_start != NULL && *expr_start == NULL)
13516 *expr_start = p;
13517 }
13518 else if (*p == '}')
13519 {
13520 mb_nest--;
13521 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
13522 *expr_end = p;
13523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525 }
13526
13527 return p;
13528}
13529
13530/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013531 * Expands out the 'magic' {}'s in a variable/function name.
13532 * Note that this can call itself recursively, to deal with
13533 * constructs like foo{bar}{baz}{bam}
13534 * The four pointer arguments point to "foo{expre}ss{ion}bar"
13535 * "in_start" ^
13536 * "expr_start" ^
13537 * "expr_end" ^
13538 * "in_end" ^
13539 *
13540 * Returns a new allocated string, which the caller must free.
13541 * Returns NULL for failure.
13542 */
13543 static char_u *
13544make_expanded_name(in_start, expr_start, expr_end, in_end)
13545 char_u *in_start;
13546 char_u *expr_start;
13547 char_u *expr_end;
13548 char_u *in_end;
13549{
13550 char_u c1;
13551 char_u *retval = NULL;
13552 char_u *temp_result;
13553 char_u *nextcmd = NULL;
13554
13555 if (expr_end == NULL || in_end == NULL)
13556 return NULL;
13557 *expr_start = NUL;
13558 *expr_end = NUL;
13559 c1 = *in_end;
13560 *in_end = NUL;
13561
13562 temp_result = eval_to_string(expr_start + 1, &nextcmd);
13563 if (temp_result != NULL && nextcmd == NULL)
13564 {
13565 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
13566 + (in_end - expr_end) + 1));
13567 if (retval != NULL)
13568 {
13569 STRCPY(retval, in_start);
13570 STRCAT(retval, temp_result);
13571 STRCAT(retval, expr_end + 1);
13572 }
13573 }
13574 vim_free(temp_result);
13575
13576 *in_end = c1; /* put char back for error messages */
13577 *expr_start = '{';
13578 *expr_end = '}';
13579
13580 if (retval != NULL)
13581 {
13582 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
13583 if (expr_start != NULL)
13584 {
13585 /* Further expansion! */
13586 temp_result = make_expanded_name(retval, expr_start,
13587 expr_end, temp_result);
13588 vim_free(retval);
13589 retval = temp_result;
13590 }
13591 }
13592
13593 return retval;
13594}
13595
13596/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013597 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000013598 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599 */
13600 static int
13601eval_isnamec(c)
13602 int c;
13603{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013604 return (ASCII_ISALNUM(c) || c == '_' || c == ':');
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605}
13606
13607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608 * Set number v: variable to "val".
13609 */
13610 void
13611set_vim_var_nr(idx, val)
13612 int idx;
13613 long val;
13614{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013615 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616}
13617
13618/*
13619 * Get number v: variable value;
13620 */
13621 long
13622get_vim_var_nr(idx)
13623 int idx;
13624{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013625 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626}
13627
13628/*
13629 * Set v:count, v:count1 and v:prevcount.
13630 */
13631 void
13632set_vcount(count, count1)
13633 long count;
13634 long count1;
13635{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013636 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
13637 vimvars[VV_COUNT].vv_nr = count;
13638 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013639}
13640
13641/*
13642 * Set string v: variable to a copy of "val".
13643 */
13644 void
13645set_vim_var_string(idx, val, len)
13646 int idx;
13647 char_u *val;
13648 int len; /* length of "val" to use or -1 (whole string) */
13649{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013650 /* Need to do this (at least) once, since we can't initialize a union.
13651 * Will always be invoked when "v:progname" is set. */
13652 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
13653
Bram Moolenaare9a41262005-01-15 22:18:47 +000013654 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013655 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013656 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013658 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013660 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661}
13662
13663/*
13664 * Set v:register if needed.
13665 */
13666 void
13667set_reg_var(c)
13668 int c;
13669{
13670 char_u regname;
13671
13672 if (c == 0 || c == ' ')
13673 regname = '"';
13674 else
13675 regname = c;
13676 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013677 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678 set_vim_var_string(VV_REG, &regname, 1);
13679}
13680
13681/*
13682 * Get or set v:exception. If "oldval" == NULL, return the current value.
13683 * Otherwise, restore the value to "oldval" and return NULL.
13684 * Must always be called in pairs to save and restore v:exception! Does not
13685 * take care of memory allocations.
13686 */
13687 char_u *
13688v_exception(oldval)
13689 char_u *oldval;
13690{
13691 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013692 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693
Bram Moolenaare9a41262005-01-15 22:18:47 +000013694 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695 return NULL;
13696}
13697
13698/*
13699 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
13700 * Otherwise, restore the value to "oldval" and return NULL.
13701 * Must always be called in pairs to save and restore v:throwpoint! Does not
13702 * take care of memory allocations.
13703 */
13704 char_u *
13705v_throwpoint(oldval)
13706 char_u *oldval;
13707{
13708 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013709 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710
Bram Moolenaare9a41262005-01-15 22:18:47 +000013711 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712 return NULL;
13713}
13714
13715#if defined(FEAT_AUTOCMD) || defined(PROTO)
13716/*
13717 * Set v:cmdarg.
13718 * If "eap" != NULL, use "eap" to generate the value and return the old value.
13719 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
13720 * Must always be called in pairs!
13721 */
13722 char_u *
13723set_cmdarg(eap, oldarg)
13724 exarg_T *eap;
13725 char_u *oldarg;
13726{
13727 char_u *oldval;
13728 char_u *newval;
13729 unsigned len;
13730
Bram Moolenaare9a41262005-01-15 22:18:47 +000013731 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013732 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013734 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000013735 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013736 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013737 }
13738
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013739 if (eap->force_bin == FORCE_BIN)
13740 len = 6;
13741 else if (eap->force_bin == FORCE_NOBIN)
13742 len = 8;
13743 else
13744 len = 0;
13745 if (eap->force_ff != 0)
13746 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
13747# ifdef FEAT_MBYTE
13748 if (eap->force_enc != 0)
13749 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
13750# endif
13751
13752 newval = alloc(len + 1);
13753 if (newval == NULL)
13754 return NULL;
13755
13756 if (eap->force_bin == FORCE_BIN)
13757 sprintf((char *)newval, " ++bin");
13758 else if (eap->force_bin == FORCE_NOBIN)
13759 sprintf((char *)newval, " ++nobin");
13760 else
13761 *newval = NUL;
13762 if (eap->force_ff != 0)
13763 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
13764 eap->cmd + eap->force_ff);
13765# ifdef FEAT_MBYTE
13766 if (eap->force_enc != 0)
13767 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
13768 eap->cmd + eap->force_enc);
13769# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000013770 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000013771 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772}
13773#endif
13774
13775/*
13776 * Get the value of internal variable "name".
13777 * Return OK or FAIL.
13778 */
13779 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013780get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013781 char_u *name;
13782 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000013783 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013784 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013785{
13786 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000013787 typval_T *tv = NULL;
13788 typval_T atv;
13789 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013790 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013791
13792 /* truncate the name, so that we can use strcmp() */
13793 cc = name[len];
13794 name[len] = NUL;
13795
13796 /*
13797 * Check for "b:changedtick".
13798 */
13799 if (STRCMP(name, "b:changedtick") == 0)
13800 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000013801 atv.v_type = VAR_NUMBER;
13802 atv.vval.v_number = curbuf->b_changedtick;
13803 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804 }
13805
13806 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807 * Check for user-defined variables.
13808 */
13809 else
13810 {
Bram Moolenaara7043832005-01-21 11:56:39 +000013811 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000013813 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013814 }
13815
Bram Moolenaare9a41262005-01-15 22:18:47 +000013816 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013817 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013818 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013819 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 ret = FAIL;
13821 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013822 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013823 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824
13825 name[len] = cc;
13826
13827 return ret;
13828}
13829
13830/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013831 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
13832 * Also handle function call with Funcref variable: func(expr)
13833 * Can all be combined: dict.func(expr)[idx]['func'](expr)
13834 */
13835 static int
13836handle_subscript(arg, rettv, evaluate, verbose)
13837 char_u **arg;
13838 typval_T *rettv;
13839 int evaluate; /* do more than finding the end */
13840 int verbose; /* give error messages */
13841{
13842 int ret = OK;
13843 dict_T *selfdict = NULL;
13844 char_u *s;
13845 int len;
13846
13847 while (ret == OK
13848 && (**arg == '['
13849 || (**arg == '.' && rettv->v_type == VAR_DICT)
13850 || (**arg == '(' && rettv->v_type == VAR_FUNC))
13851 && !vim_iswhite(*(*arg - 1)))
13852 {
13853 if (**arg == '(')
13854 {
13855 s = rettv->vval.v_string;
13856
13857 /* Invoke the function. Recursive! */
13858 ret = get_func_tv(s, STRLEN(s), rettv, arg,
13859 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
13860 &len, evaluate, selfdict);
13861
13862 /* Stop the expression evaluation when immediately aborting on
13863 * error, or when an interrupt occurred or an exception was thrown
13864 * but not caught. */
13865 if (aborting())
13866 {
13867 if (ret == OK)
13868 clear_tv(rettv);
13869 ret = FAIL;
13870 }
13871 dict_unref(selfdict);
13872 selfdict = NULL;
13873 }
13874 else /* **arg == '[' || **arg == '.' */
13875 {
13876 dict_unref(selfdict);
13877 if (rettv->v_type == VAR_DICT)
13878 {
13879 selfdict = rettv->vval.v_dict;
13880 if (selfdict != NULL)
13881 ++selfdict->dv_refcount;
13882 }
13883 else
13884 selfdict = NULL;
13885 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
13886 {
13887 clear_tv(rettv);
13888 ret = FAIL;
13889 }
13890 }
13891 }
13892 dict_unref(selfdict);
13893 return ret;
13894}
13895
13896/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013897 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
13898 * value).
13899 */
Bram Moolenaar33570922005-01-25 22:26:29 +000013900 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013901alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013902{
Bram Moolenaar33570922005-01-25 22:26:29 +000013903 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013904}
13905
13906/*
13907 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013908 * The string "s" must have been allocated, it is consumed.
13909 * Return NULL for out of memory, the variable otherwise.
13910 */
Bram Moolenaar33570922005-01-25 22:26:29 +000013911 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013912alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013913 char_u *s;
13914{
Bram Moolenaar33570922005-01-25 22:26:29 +000013915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013916
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013917 rettv = alloc_tv();
13918 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013920 rettv->v_type = VAR_STRING;
13921 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013922 }
13923 else
13924 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013925 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013926}
13927
13928/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013929 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930 */
13931 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013932free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013933 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934{
13935 if (varp != NULL)
13936 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013937 switch (varp->v_type)
13938 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013939 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013940 func_unref(varp->vval.v_string);
13941 /*FALLTHROUGH*/
13942 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013943 vim_free(varp->vval.v_string);
13944 break;
13945 case VAR_LIST:
13946 list_unref(varp->vval.v_list);
13947 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013948 case VAR_DICT:
13949 dict_unref(varp->vval.v_dict);
13950 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013951 case VAR_NUMBER:
13952 case VAR_UNKNOWN:
13953 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013954 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000013955 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013956 break;
13957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 vim_free(varp);
13959 }
13960}
13961
13962/*
13963 * Free the memory for a variable value and set the value to NULL or 0.
13964 */
13965 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013966clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000013967 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968{
13969 if (varp != NULL)
13970 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013971 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013973 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000013974 func_unref(varp->vval.v_string);
13975 /*FALLTHROUGH*/
13976 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013977 vim_free(varp->vval.v_string);
13978 varp->vval.v_string = NULL;
13979 break;
13980 case VAR_LIST:
13981 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013982 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013983 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013984 case VAR_DICT:
13985 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013986 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013987 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013988 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013989 varp->vval.v_number = 0;
13990 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013991 case VAR_UNKNOWN:
13992 break;
13993 default:
13994 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013996 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997 }
13998}
13999
14000/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014001 * Set the value of a variable to NULL without freeing items.
14002 */
14003 static void
14004init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014005 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014006{
14007 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014008 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014009}
14010
14011/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012 * Get the number value of a variable.
14013 * If it is a String variable, uses vim_str2nr().
14014 */
14015 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014016get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014017 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014019 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014021 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014023 case VAR_NUMBER:
14024 n = (long)(varp->vval.v_number);
14025 break;
14026 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014027 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014028 break;
14029 case VAR_STRING:
14030 if (varp->vval.v_string != NULL)
14031 vim_str2nr(varp->vval.v_string, NULL, NULL,
14032 TRUE, TRUE, &n, NULL);
14033 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014034 case VAR_LIST:
Bram Moolenaar758711c2005-02-02 23:11:38 +000014035 EMSG(_("E745: Using a List as a number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014036 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014037 case VAR_DICT:
14038 EMSG(_("E728: Using a Dictionary as a number"));
14039 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014040 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014041 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014042 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014043 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014044 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045}
14046
14047/*
14048 * Get the lnum from the first argument. Also accepts ".", "$", etc.
14049 */
14050 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014051get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000014052 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053{
Bram Moolenaar33570922005-01-25 22:26:29 +000014054 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055 linenr_T lnum;
14056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014057 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058 if (lnum == 0) /* no valid number, try using line() */
14059 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014060 rettv.v_type = VAR_NUMBER;
14061 f_line(argvars, &rettv);
14062 lnum = rettv.vval.v_number;
14063 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064 }
14065 return lnum;
14066}
14067
14068/*
14069 * Get the string value of a variable.
14070 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000014071 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
14072 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014073 * If the String variable has never been set, return an empty string.
14074 * Never returns NULL;
14075 */
14076 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014077get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000014078 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079{
14080 static char_u mybuf[NUMBUFLEN];
14081
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014082 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083}
14084
14085 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014086get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000014087 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014088 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014090 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014092 case VAR_NUMBER:
14093 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
14094 return buf;
14095 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014096 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014097 break;
14098 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014099 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014100 break;
14101 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014102 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014103 break;
14104 case VAR_STRING:
14105 if (varp->vval.v_string != NULL)
14106 return varp->vval.v_string;
14107 break;
14108 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014109 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014110 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014111 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014112 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113}
14114
14115/*
14116 * Find variable "name" in the list of variables.
14117 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014118 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014119 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000014120 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014122 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014123find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014125 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126{
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014128 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129
Bram Moolenaara7043832005-01-21 11:56:39 +000014130 ht = find_var_ht(name, &varname);
14131 if (htp != NULL)
14132 *htp = ht;
14133 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134 return NULL;
Bram Moolenaara7043832005-01-21 11:56:39 +000014135 return find_var_in_ht(ht, varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014136}
14137
14138/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014139 * Find variable "varname" in hashtab "ht".
Bram Moolenaara7043832005-01-21 11:56:39 +000014140 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014142 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014143find_var_in_ht(ht, varname)
Bram Moolenaar33570922005-01-25 22:26:29 +000014144 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +000014145 char_u *varname;
14146{
Bram Moolenaar33570922005-01-25 22:26:29 +000014147 hashitem_T *hi;
14148
14149 if (*varname == NUL)
14150 {
14151 /* Must be something like "s:", otherwise "ht" would be NULL. */
14152 switch (varname[-2])
14153 {
14154 case 's': return &SCRIPT_SV(current_SID).sv_var;
14155 case 'g': return &globvars_var;
14156 case 'v': return &vimvars_var;
14157 case 'b': return &curbuf->b_bufvar;
14158 case 'w': return &curwin->w_winvar;
14159 case 'l': return &current_funccal->l_vars_var;
14160 case 'a': return &current_funccal->l_avars_var;
14161 }
14162 return NULL;
14163 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014164
14165 hi = hash_find(ht, varname);
14166 if (HASHITEM_EMPTY(hi))
14167 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000014168 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014169}
14170
14171/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014172 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000014173 * Set "varname" to the start of name without ':'.
14174 */
Bram Moolenaar33570922005-01-25 22:26:29 +000014175 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000014176find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177 char_u *name;
14178 char_u **varname;
14179{
14180 if (name[1] != ':')
14181 {
14182 /* If not "x:name" there must not be any ":" in the name. */
14183 if (vim_strchr(name, ':') != NULL)
14184 return NULL;
14185 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000014186
14187 /* "version" is "v:version" in all scopes */
14188 if (!HASHITEM_EMPTY(hash_find(&compat_hashtab, name)))
14189 return &compat_hashtab;
14190
Bram Moolenaar071d4272004-06-13 20:20:40 +000014191 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000014192 return &globvarht; /* global variable */
14193 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014194 }
14195 *varname = name + 2;
14196 if (*name == 'b') /* buffer variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014197 return &curbuf->b_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198 if (*name == 'w') /* window variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014199 return &curwin->w_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200 if (*name == 'g') /* global variable */
Bram Moolenaar33570922005-01-25 22:26:29 +000014201 return &globvarht;
14202 if (*name == 'v') /* v: variable */
14203 return &vimvarht;
14204 if (*name == 'a' && current_funccal != NULL) /* function argument */
14205 return &current_funccal->l_avars.dv_hashtab;
14206 if (*name == 'l' && current_funccal != NULL) /* local function variable */
14207 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208 if (*name == 's' /* script variable */
14209 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
14210 return &SCRIPT_VARS(current_SID);
14211 return NULL;
14212}
14213
14214/*
14215 * Get the string value of a (global/local) variable.
14216 * Returns NULL when it doesn't exist.
14217 */
14218 char_u *
14219get_var_value(name)
14220 char_u *name;
14221{
Bram Moolenaar33570922005-01-25 22:26:29 +000014222 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223
Bram Moolenaara7043832005-01-21 11:56:39 +000014224 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225 if (v == NULL)
14226 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000014227 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228}
14229
14230/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014231 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232 * sourcing this script and when executing functions defined in the script.
14233 */
14234 void
14235new_script_vars(id)
14236 scid_T id;
14237{
Bram Moolenaara7043832005-01-21 11:56:39 +000014238 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000014239 hashtab_T *ht;
14240 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000014241
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
14243 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014244 /* Re-allocating ga_data means that an ht_array pointing to
14245 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000014246 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000014247 for (i = 1; i <= ga_scripts.ga_len; ++i)
14248 {
14249 ht = &SCRIPT_VARS(i);
14250 if (ht->ht_mask == HT_INIT_SIZE - 1)
14251 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar33570922005-01-25 22:26:29 +000014252 sv = &SCRIPT_SV(i);
14253 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000014254 }
14255
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256 while (ga_scripts.ga_len < id)
14257 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014258 sv = &SCRIPT_SV(ga_scripts.ga_len + 1);
14259 init_var_dict(&sv->sv_dict, &sv->sv_var);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014260 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261 }
14262 }
14263}
14264
14265/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014266 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
14267 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268 */
14269 void
Bram Moolenaar33570922005-01-25 22:26:29 +000014270init_var_dict(dict, dict_var)
14271 dict_T *dict;
14272 dictitem_T *dict_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273{
Bram Moolenaar33570922005-01-25 22:26:29 +000014274 hash_init(&dict->dv_hashtab);
14275 dict->dv_refcount = 99999;
14276 dict_var->di_tv.vval.v_dict = dict;
14277 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014278 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000014279 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
14280 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014281}
14282
14283/*
14284 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000014285 * Frees all allocated variables and the value they contain.
14286 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287 */
14288 void
Bram Moolenaara7043832005-01-21 11:56:39 +000014289vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000014290 hashtab_T *ht;
14291{
14292 vars_clear_ext(ht, TRUE);
14293}
14294
14295/*
14296 * Like vars_clear(), but only free the value if "free_val" is TRUE.
14297 */
14298 static void
14299vars_clear_ext(ht, free_val)
14300 hashtab_T *ht;
14301 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014302{
Bram Moolenaara7043832005-01-21 11:56:39 +000014303 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000014304 hashitem_T *hi;
14305 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014306
Bram Moolenaar33570922005-01-25 22:26:29 +000014307 hash_lock(ht);
Bram Moolenaara7043832005-01-21 11:56:39 +000014308 todo = ht->ht_used;
14309 for (hi = ht->ht_array; todo > 0; ++hi)
14310 {
14311 if (!HASHITEM_EMPTY(hi))
14312 {
14313 --todo;
14314
Bram Moolenaar33570922005-01-25 22:26:29 +000014315 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000014316 * ht_array might change then. hash_clear() takes care of it
14317 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000014318 v = HI2DI(hi);
14319 if (free_val)
14320 clear_tv(&v->di_tv);
14321 if ((v->di_flags & DI_FLAGS_FIX) == 0)
14322 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000014323 }
14324 }
14325 hash_clear(ht);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326}
14327
Bram Moolenaara7043832005-01-21 11:56:39 +000014328/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014329 * Delete a variable from hashtab "ht" at item "hi".
14330 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000014331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000014333delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000014334 hashtab_T *ht;
14335 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336{
Bram Moolenaar33570922005-01-25 22:26:29 +000014337 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000014338
14339 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000014340 clear_tv(&di->di_tv);
14341 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014342}
14343
14344/*
14345 * List the value of one internal variable.
14346 */
14347 static void
14348list_one_var(v, prefix)
Bram Moolenaar33570922005-01-25 22:26:29 +000014349 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350 char_u *prefix;
14351{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014352 char_u *tofree;
14353 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014354 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014355
Bram Moolenaar33570922005-01-25 22:26:29 +000014356 s = echo_string(&v->di_tv, &tofree, numbuf);
14357 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014358 s == NULL ? (char_u *)"" : s);
14359 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360}
14361
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362 static void
14363list_one_var_a(prefix, name, type, string)
14364 char_u *prefix;
14365 char_u *name;
14366 int type;
14367 char_u *string;
14368{
14369 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
14370 if (name != NULL) /* "a:" vars don't have a name stored */
14371 msg_puts(name);
14372 msg_putchar(' ');
14373 msg_advance(22);
14374 if (type == VAR_NUMBER)
14375 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014376 else if (type == VAR_FUNC)
14377 msg_putchar('*');
14378 else if (type == VAR_LIST)
14379 {
14380 msg_putchar('[');
14381 if (*string == '[')
14382 ++string;
14383 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014384 else if (type == VAR_DICT)
14385 {
14386 msg_putchar('{');
14387 if (*string == '{')
14388 ++string;
14389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390 else
14391 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014392
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014394
14395 if (type == VAR_FUNC)
14396 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397}
14398
14399/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014400 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401 * If the variable already exists, the value is updated.
14402 * Otherwise the variable is created.
14403 */
14404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014405set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000014407 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014408 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409{
Bram Moolenaar33570922005-01-25 22:26:29 +000014410 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000014412 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014413
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014414 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014415 {
14416 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
14417 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
14418 ? name[2] : name[0]))
14419 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014420 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014421 return;
14422 }
14423 if (function_exists(name))
14424 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014425 EMSG2(_("705: Variable name conflicts with existing function: %s"),
14426 name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014427 return;
14428 }
14429 }
14430
Bram Moolenaara7043832005-01-21 11:56:39 +000014431 ht = find_var_ht(name, &varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000014432 if (ht == NULL || *varname == NUL)
Bram Moolenaara7043832005-01-21 11:56:39 +000014433 {
14434 EMSG2(_("E461: Illegal variable name: %s"), name);
14435 return;
14436 }
14437
14438 v = find_var_in_ht(ht, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000014439 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014441 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014442 if (var_check_ro(v->di_flags, name)
14443 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000014444 return;
14445 if (v->di_tv.v_type != tv->v_type
14446 && !((v->di_tv.v_type == VAR_STRING
14447 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014448 && (tv->v_type == VAR_STRING
14449 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014450 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014451 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014452 return;
14453 }
Bram Moolenaar33570922005-01-25 22:26:29 +000014454
14455 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000014456 * Handle setting internal v: variables separately: we don't change
14457 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000014458 */
14459 if (ht == &vimvarht)
14460 {
14461 if (v->di_tv.v_type == VAR_STRING)
14462 {
14463 vim_free(v->di_tv.vval.v_string);
14464 if (copy || tv->v_type != VAR_STRING)
14465 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
14466 else
14467 {
14468 /* Take over the string to avoid an extra alloc/free. */
14469 v->di_tv.vval.v_string = tv->vval.v_string;
14470 tv->vval.v_string = NULL;
14471 }
14472 }
14473 else if (v->di_tv.v_type != VAR_NUMBER)
14474 EMSG2(_(e_intern2), "set_var()");
14475 else
14476 v->di_tv.vval.v_number = get_tv_number(tv);
14477 return;
14478 }
14479
14480 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 }
14482 else /* add a new variable */
14483 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014484 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
14485 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000014486 if (v == NULL)
14487 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000014488 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000014489 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490 {
Bram Moolenaara7043832005-01-21 11:56:39 +000014491 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492 return;
14493 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014494 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495 }
Bram Moolenaara7043832005-01-21 11:56:39 +000014496
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014497 if (copy || tv->v_type == VAR_NUMBER)
Bram Moolenaar33570922005-01-25 22:26:29 +000014498 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014499 else
14500 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014501 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014502 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014503 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000014504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014505}
14506
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014507/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014508 * Return TRUE if di_flags "flags" indicate read-only variable "name".
14509 * Also give an error message.
14510 */
14511 static int
14512var_check_ro(flags, name)
14513 int flags;
14514 char_u *name;
14515{
14516 if (flags & DI_FLAGS_RO)
14517 {
14518 EMSG2(_(e_readonlyvar), name);
14519 return TRUE;
14520 }
14521 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
14522 {
14523 EMSG2(_(e_readonlysbx), name);
14524 return TRUE;
14525 }
14526 return FALSE;
14527}
14528
14529/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014530 * Return TRUE if typeval "tv" is set to be locked (immutable).
14531 * Also give an error message, using "name".
14532 */
14533 static int
14534tv_check_lock(lock, name)
14535 int lock;
14536 char_u *name;
14537{
14538 if (lock & VAR_LOCKED)
14539 {
14540 EMSG2(_("E741: Value is locked: %s"),
14541 name == NULL ? (char_u *)_("Unknown") : name);
14542 return TRUE;
14543 }
14544 if (lock & VAR_FIXED)
14545 {
14546 EMSG2(_("E742: Cannot change value of %s"),
14547 name == NULL ? (char_u *)_("Unknown") : name);
14548 return TRUE;
14549 }
14550 return FALSE;
14551}
14552
14553/*
Bram Moolenaar33570922005-01-25 22:26:29 +000014554 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014555 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000014556 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014559copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000014560 typval_T *from;
14561 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014563 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014564 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014565 switch (from->v_type)
14566 {
14567 case VAR_NUMBER:
14568 to->vval.v_number = from->vval.v_number;
14569 break;
14570 case VAR_STRING:
14571 case VAR_FUNC:
14572 if (from->vval.v_string == NULL)
14573 to->vval.v_string = NULL;
14574 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014575 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014576 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014577 if (from->v_type == VAR_FUNC)
14578 func_ref(to->vval.v_string);
14579 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014580 break;
14581 case VAR_LIST:
14582 if (from->vval.v_list == NULL)
14583 to->vval.v_list = NULL;
14584 else
14585 {
14586 to->vval.v_list = from->vval.v_list;
14587 ++to->vval.v_list->lv_refcount;
14588 }
14589 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014590 case VAR_DICT:
14591 if (from->vval.v_dict == NULL)
14592 to->vval.v_dict = NULL;
14593 else
14594 {
14595 to->vval.v_dict = from->vval.v_dict;
14596 ++to->vval.v_dict->dv_refcount;
14597 }
14598 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014599 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014600 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014601 break;
14602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603}
14604
14605/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014606 * Make a copy of an item.
14607 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014608 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
14609 * reference to an already copied list/dict can be used.
14610 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000014611 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014612 static int
14613item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000014614 typval_T *from;
14615 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014616 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014617 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014618{
14619 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014620 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014621
Bram Moolenaar33570922005-01-25 22:26:29 +000014622 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014623 {
14624 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014625 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014626 }
14627 ++recurse;
14628
14629 switch (from->v_type)
14630 {
14631 case VAR_NUMBER:
14632 case VAR_STRING:
14633 case VAR_FUNC:
14634 copy_tv(from, to);
14635 break;
14636 case VAR_LIST:
14637 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014638 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014639 if (from->vval.v_list == NULL)
14640 to->vval.v_list = NULL;
14641 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
14642 {
14643 /* use the copy made earlier */
14644 to->vval.v_list = from->vval.v_list->lv_copylist;
14645 ++to->vval.v_list->lv_refcount;
14646 }
14647 else
14648 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
14649 if (to->vval.v_list == NULL)
14650 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014651 break;
14652 case VAR_DICT:
14653 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014654 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014655 if (from->vval.v_dict == NULL)
14656 to->vval.v_dict = NULL;
14657 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
14658 {
14659 /* use the copy made earlier */
14660 to->vval.v_dict = from->vval.v_dict->dv_copydict;
14661 ++to->vval.v_dict->dv_refcount;
14662 }
14663 else
14664 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
14665 if (to->vval.v_dict == NULL)
14666 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014667 break;
14668 default:
14669 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014670 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014671 }
14672 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014673 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014674}
14675
14676/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 * ":echo expr1 ..." print each argument separated with a space, add a
14678 * newline at the end.
14679 * ":echon expr1 ..." print each argument plain.
14680 */
14681 void
14682ex_echo(eap)
14683 exarg_T *eap;
14684{
14685 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000014686 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014687 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014688 char_u *p;
14689 int needclr = TRUE;
14690 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014691 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014692
14693 if (eap->skip)
14694 ++emsg_skip;
14695 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
14696 {
14697 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014698 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014699 {
14700 /*
14701 * Report the invalid expression unless the expression evaluation
14702 * has been cancelled due to an aborting error, an interrupt, or an
14703 * exception.
14704 */
14705 if (!aborting())
14706 EMSG2(_(e_invexpr2), p);
14707 break;
14708 }
14709 if (!eap->skip)
14710 {
14711 if (atstart)
14712 {
14713 atstart = FALSE;
14714 /* Call msg_start() after eval1(), evaluating the expression
14715 * may cause a message to appear. */
14716 if (eap->cmdidx == CMD_echo)
14717 msg_start();
14718 }
14719 else if (eap->cmdidx == CMD_echo)
14720 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014721 p = echo_string(&rettv, &tofree, numbuf);
14722 if (p != NULL)
14723 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014724 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014725 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014726 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014727 if (*p != TAB && needclr)
14728 {
14729 /* remove any text still there from the command */
14730 msg_clr_eos();
14731 needclr = FALSE;
14732 }
14733 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014734 }
14735 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014736 {
14737#ifdef FEAT_MBYTE
14738 if (has_mbyte)
14739 {
14740 int i = (*mb_ptr2len_check)(p);
14741
14742 (void)msg_outtrans_len_attr(p, i, echo_attr);
14743 p += i - 1;
14744 }
14745 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000014747 (void)msg_outtrans_len_attr(p, 1, echo_attr);
14748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014750 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014752 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014753 arg = skipwhite(arg);
14754 }
14755 eap->nextcmd = check_nextcmd(arg);
14756
14757 if (eap->skip)
14758 --emsg_skip;
14759 else
14760 {
14761 /* remove text that may still be there from the command */
14762 if (needclr)
14763 msg_clr_eos();
14764 if (eap->cmdidx == CMD_echo)
14765 msg_end();
14766 }
14767}
14768
14769/*
14770 * ":echohl {name}".
14771 */
14772 void
14773ex_echohl(eap)
14774 exarg_T *eap;
14775{
14776 int id;
14777
14778 id = syn_name2id(eap->arg);
14779 if (id == 0)
14780 echo_attr = 0;
14781 else
14782 echo_attr = syn_id2attr(id);
14783}
14784
14785/*
14786 * ":execute expr1 ..." execute the result of an expression.
14787 * ":echomsg expr1 ..." Print a message
14788 * ":echoerr expr1 ..." Print an error
14789 * Each gets spaces around each argument and a newline at the end for
14790 * echo commands
14791 */
14792 void
14793ex_execute(eap)
14794 exarg_T *eap;
14795{
14796 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000014797 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014798 int ret = OK;
14799 char_u *p;
14800 garray_T ga;
14801 int len;
14802 int save_did_emsg;
14803
14804 ga_init2(&ga, 1, 80);
14805
14806 if (eap->skip)
14807 ++emsg_skip;
14808 while (*arg != NUL && *arg != '|' && *arg != '\n')
14809 {
14810 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014811 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014812 {
14813 /*
14814 * Report the invalid expression unless the expression evaluation
14815 * has been cancelled due to an aborting error, an interrupt, or an
14816 * exception.
14817 */
14818 if (!aborting())
14819 EMSG2(_(e_invexpr2), p);
14820 ret = FAIL;
14821 break;
14822 }
14823
14824 if (!eap->skip)
14825 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014826 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014827 len = (int)STRLEN(p);
14828 if (ga_grow(&ga, len + 2) == FAIL)
14829 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014830 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014831 ret = FAIL;
14832 break;
14833 }
14834 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014835 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014836 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014837 ga.ga_len += len;
14838 }
14839
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014840 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014841 arg = skipwhite(arg);
14842 }
14843
14844 if (ret != FAIL && ga.ga_data != NULL)
14845 {
14846 if (eap->cmdidx == CMD_echomsg)
14847 MSG_ATTR(ga.ga_data, echo_attr);
14848 else if (eap->cmdidx == CMD_echoerr)
14849 {
14850 /* We don't want to abort following commands, restore did_emsg. */
14851 save_did_emsg = did_emsg;
14852 EMSG((char_u *)ga.ga_data);
14853 if (!force_abort)
14854 did_emsg = save_did_emsg;
14855 }
14856 else if (eap->cmdidx == CMD_execute)
14857 do_cmdline((char_u *)ga.ga_data,
14858 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
14859 }
14860
14861 ga_clear(&ga);
14862
14863 if (eap->skip)
14864 --emsg_skip;
14865
14866 eap->nextcmd = check_nextcmd(arg);
14867}
14868
14869/*
14870 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
14871 * "arg" points to the "&" or '+' when called, to "option" when returning.
14872 * Returns NULL when no option name found. Otherwise pointer to the char
14873 * after the option name.
14874 */
14875 static char_u *
14876find_option_end(arg, opt_flags)
14877 char_u **arg;
14878 int *opt_flags;
14879{
14880 char_u *p = *arg;
14881
14882 ++p;
14883 if (*p == 'g' && p[1] == ':')
14884 {
14885 *opt_flags = OPT_GLOBAL;
14886 p += 2;
14887 }
14888 else if (*p == 'l' && p[1] == ':')
14889 {
14890 *opt_flags = OPT_LOCAL;
14891 p += 2;
14892 }
14893 else
14894 *opt_flags = 0;
14895
14896 if (!ASCII_ISALPHA(*p))
14897 return NULL;
14898 *arg = p;
14899
14900 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
14901 p += 4; /* termcap option */
14902 else
14903 while (ASCII_ISALPHA(*p))
14904 ++p;
14905 return p;
14906}
14907
14908/*
14909 * ":function"
14910 */
14911 void
14912ex_function(eap)
14913 exarg_T *eap;
14914{
14915 char_u *theline;
14916 int j;
14917 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014918 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014919 char_u *name = NULL;
14920 char_u *p;
14921 char_u *arg;
14922 garray_T newargs;
14923 garray_T newlines;
14924 int varargs = FALSE;
14925 int mustend = FALSE;
14926 int flags = 0;
14927 ufunc_T *fp;
14928 int indent;
14929 int nesting;
14930 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000014931 dictitem_T *v;
14932 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014933 static int func_nr = 0; /* number for nameless function */
14934 int paren;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014935
14936 /*
14937 * ":function" without argument: list functions.
14938 */
14939 if (ends_excmd(*eap->arg))
14940 {
14941 if (!eap->skip)
14942 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014943 if (!isdigit(*fp->name))
14944 list_func_head(fp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014945 eap->nextcmd = check_nextcmd(eap->arg);
14946 return;
14947 }
14948
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014949 /*
14950 * Get the function name. There are these situations:
14951 * func normal function name
14952 * "name" == func, "fudi.fd_dict" == NULL
14953 * dict.func new dictionary entry
14954 * "name" == NULL, "fudi.fd_dict" set,
14955 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
14956 * dict.func existing dict entry with a Funcref
14957 * "name" == fname, "fudi.fd_dict" set,
14958 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
14959 * dict.func existing dict entry that's not a Funcref
14960 * "name" == NULL, "fudi.fd_dict" set,
14961 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
14962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014963 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014964 name = trans_function_name(&p, eap->skip, 0, &fudi);
14965 paren = (vim_strchr(p, '(') != NULL);
14966 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967 {
14968 /*
14969 * Return on an invalid expression in braces, unless the expression
14970 * evaluation has been cancelled due to an aborting error, an
14971 * interrupt, or an exception.
14972 */
14973 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014974 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014975 if (!eap->skip && fudi.fd_newkey != NULL)
14976 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014977 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014978 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014979 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014980 else
14981 eap->skip = TRUE;
14982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014983 /* An error in a function call during evaluation of an expression in magic
14984 * braces should not cause the function not to be defined. */
14985 saved_did_emsg = did_emsg;
14986 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014987
14988 /*
14989 * ":function func" with only function name: list function.
14990 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014991 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014992 {
14993 if (!ends_excmd(*skipwhite(p)))
14994 {
14995 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014996 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997 }
14998 eap->nextcmd = check_nextcmd(p);
14999 if (eap->nextcmd != NULL)
15000 *p = NUL;
15001 if (!eap->skip && !got_int)
15002 {
15003 fp = find_func(name);
15004 if (fp != NULL)
15005 {
15006 list_func_head(fp, TRUE);
15007 for (j = 0; j < fp->lines.ga_len && !got_int; ++j)
15008 {
15009 msg_putchar('\n');
15010 msg_outnum((long)(j + 1));
15011 if (j < 9)
15012 msg_putchar(' ');
15013 if (j < 99)
15014 msg_putchar(' ');
15015 msg_prt_line(FUNCLINE(fp, j));
15016 out_flush(); /* show a line at a time */
15017 ui_breakcheck();
15018 }
15019 if (!got_int)
15020 {
15021 msg_putchar('\n');
15022 msg_puts((char_u *)" endfunction");
15023 }
15024 }
15025 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015026 emsg_funcname("E123: Undefined function: %s", name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015027 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015028 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029 }
15030
15031 /*
15032 * ":function name(arg1, arg2)" Define function.
15033 */
15034 p = skipwhite(p);
15035 if (*p != '(')
15036 {
15037 if (!eap->skip)
15038 {
15039 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015040 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015041 }
15042 /* attempt to continue by skipping some text */
15043 if (vim_strchr(p, '(') != NULL)
15044 p = vim_strchr(p, '(');
15045 }
15046 p = skipwhite(p + 1);
15047
15048 ga_init2(&newargs, (int)sizeof(char_u *), 3);
15049 ga_init2(&newlines, (int)sizeof(char_u *), 3);
15050
15051 /*
15052 * Isolate the arguments: "arg1, arg2, ...)"
15053 */
15054 while (*p != ')')
15055 {
15056 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
15057 {
15058 varargs = TRUE;
15059 p += 3;
15060 mustend = TRUE;
15061 }
15062 else
15063 {
15064 arg = p;
15065 while (ASCII_ISALNUM(*p) || *p == '_')
15066 ++p;
15067 if (arg == p || isdigit(*arg)
15068 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
15069 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
15070 {
15071 if (!eap->skip)
15072 EMSG2(_("E125: Illegal argument: %s"), arg);
15073 break;
15074 }
15075 if (ga_grow(&newargs, 1) == FAIL)
15076 goto erret;
15077 c = *p;
15078 *p = NUL;
15079 arg = vim_strsave(arg);
15080 if (arg == NULL)
15081 goto erret;
15082 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
15083 *p = c;
15084 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015085 if (*p == ',')
15086 ++p;
15087 else
15088 mustend = TRUE;
15089 }
15090 p = skipwhite(p);
15091 if (mustend && *p != ')')
15092 {
15093 if (!eap->skip)
15094 EMSG2(_(e_invarg2), eap->arg);
15095 break;
15096 }
15097 }
15098 ++p; /* skip the ')' */
15099
Bram Moolenaare9a41262005-01-15 22:18:47 +000015100 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101 for (;;)
15102 {
15103 p = skipwhite(p);
15104 if (STRNCMP(p, "range", 5) == 0)
15105 {
15106 flags |= FC_RANGE;
15107 p += 5;
15108 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015109 else if (STRNCMP(p, "dict", 4) == 0)
15110 {
15111 flags |= FC_DICT;
15112 p += 4;
15113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015114 else if (STRNCMP(p, "abort", 5) == 0)
15115 {
15116 flags |= FC_ABORT;
15117 p += 5;
15118 }
15119 else
15120 break;
15121 }
15122
15123 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
15124 EMSG(_(e_trailing));
15125
15126 /*
15127 * Read the body of the function, until ":endfunction" is found.
15128 */
15129 if (KeyTyped)
15130 {
15131 /* Check if the function already exists, don't let the user type the
15132 * whole function before telling him it doesn't work! For a script we
15133 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015134 if (!eap->skip && !eap->forceit)
15135 {
15136 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
15137 EMSG(_(e_funcdict));
15138 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015139 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015141
15142 msg_putchar('\n'); /* don't overwrite the function name */
15143 cmdline_row = msg_row;
15144 }
15145
15146 indent = 2;
15147 nesting = 0;
15148 for (;;)
15149 {
15150 msg_scroll = TRUE;
15151 need_wait_return = FALSE;
15152 if (eap->getline == NULL)
15153 theline = getcmdline(':', 0L, indent);
15154 else
15155 theline = eap->getline(':', eap->cookie, indent);
15156 if (KeyTyped)
15157 lines_left = Rows - 1;
15158 if (theline == NULL)
15159 {
15160 EMSG(_("E126: Missing :endfunction"));
15161 goto erret;
15162 }
15163
15164 if (skip_until != NULL)
15165 {
15166 /* between ":append" and "." and between ":python <<EOF" and "EOF"
15167 * don't check for ":endfunc". */
15168 if (STRCMP(theline, skip_until) == 0)
15169 {
15170 vim_free(skip_until);
15171 skip_until = NULL;
15172 }
15173 }
15174 else
15175 {
15176 /* skip ':' and blanks*/
15177 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
15178 ;
15179
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015180 /* Check for "endfunction". */
15181 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182 {
15183 vim_free(theline);
15184 break;
15185 }
15186
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015187 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000015188 * at "end". */
15189 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
15190 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015191 else if (STRNCMP(p, "if", 2) == 0
15192 || STRNCMP(p, "wh", 2) == 0
15193 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194 || STRNCMP(p, "try", 3) == 0)
15195 indent += 2;
15196
15197 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015198 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015199 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015200 if (*p == '!')
15201 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015202 p += eval_fname_script(p);
15203 if (ASCII_ISALPHA(*p))
15204 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015205 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206 if (*skipwhite(p) == '(')
15207 {
15208 ++nesting;
15209 indent += 2;
15210 }
15211 }
15212 }
15213
15214 /* Check for ":append" or ":insert". */
15215 p = skip_range(p, NULL);
15216 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
15217 || (p[0] == 'i'
15218 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
15219 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
15220 skip_until = vim_strsave((char_u *)".");
15221
15222 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
15223 arg = skipwhite(skiptowhite(p));
15224 if (arg[0] == '<' && arg[1] =='<'
15225 && ((p[0] == 'p' && p[1] == 'y'
15226 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
15227 || (p[0] == 'p' && p[1] == 'e'
15228 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
15229 || (p[0] == 't' && p[1] == 'c'
15230 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
15231 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
15232 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000015233 || (p[0] == 'm' && p[1] == 'z'
15234 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000015235 ))
15236 {
15237 /* ":python <<" continues until a dot, like ":append" */
15238 p = skipwhite(arg + 2);
15239 if (*p == NUL)
15240 skip_until = vim_strsave((char_u *)".");
15241 else
15242 skip_until = vim_strsave(p);
15243 }
15244 }
15245
15246 /* Add the line to the function. */
15247 if (ga_grow(&newlines, 1) == FAIL)
15248 goto erret;
15249 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
15250 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015251 }
15252
15253 /* Don't define the function when skipping commands or when an error was
15254 * detected. */
15255 if (eap->skip || did_emsg)
15256 goto erret;
15257
15258 /*
15259 * If there are no errors, add the function
15260 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015261 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015262 {
Bram Moolenaara7043832005-01-21 11:56:39 +000015263 v = find_var(name, NULL);
Bram Moolenaar33570922005-01-25 22:26:29 +000015264 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015265 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015266 emsg_funcname("E707: Function name conflicts with variable: %s",
15267 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015268 goto erret;
15269 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015270
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015271 fp = find_func(name);
15272 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015274 if (!eap->forceit)
15275 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015276 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015277 goto erret;
15278 }
15279 if (fp->calls > 0)
15280 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015281 emsg_funcname("E127: Cannot redefine function %s: It is in use",
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015282 name);
15283 goto erret;
15284 }
15285 /* redefine existing function */
15286 ga_clear_strings(&(fp->args));
15287 ga_clear_strings(&(fp->lines));
15288 vim_free(name);
15289 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015291 }
15292 else
15293 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015294 char numbuf[20];
15295
15296 fp = NULL;
15297 if (fudi.fd_newkey == NULL && !eap->forceit)
15298 {
15299 EMSG(_(e_funcdict));
15300 goto erret;
15301 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000015302 if (fudi.fd_di == NULL)
15303 {
15304 /* Can't add a function to a locked dictionary */
15305 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
15306 goto erret;
15307 }
15308 /* Can't change an existing function if it is locked */
15309 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
15310 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015311
15312 /* Give the function a sequential number. Can only be used with a
15313 * Funcref! */
15314 vim_free(name);
15315 sprintf(numbuf, "%d", ++func_nr);
15316 name = vim_strsave((char_u *)numbuf);
15317 if (name == NULL)
15318 goto erret;
15319 }
15320
15321 if (fp == NULL)
15322 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015323 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T));
15324 if (fp == NULL)
15325 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015326
15327 if (fudi.fd_dict != NULL)
15328 {
15329 if (fudi.fd_di == NULL)
15330 {
15331 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015332 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015333 if (fudi.fd_di == NULL)
15334 {
15335 vim_free(fp);
15336 goto erret;
15337 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015338 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
15339 {
15340 vim_free(fudi.fd_di);
15341 goto erret;
15342 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015343 }
15344 else
15345 /* overwrite existing dict entry */
15346 clear_tv(&fudi.fd_di->di_tv);
15347 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015348 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015349 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
15350 fp->refcount = 1;
15351 }
15352
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353 /* insert the new function in the function list */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015354 fp->name = name;
15355 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015356 fp->next = firstfunc;
15357 firstfunc = fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015358 }
15359 fp->args = newargs;
15360 fp->lines = newlines;
15361 fp->varargs = varargs;
15362 fp->flags = flags;
15363 fp->calls = 0;
15364 fp->script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015365 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366
15367erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368 ga_clear_strings(&newargs);
15369 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015370ret_free:
15371 vim_free(skip_until);
15372 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015373 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015374 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015375}
15376
15377/*
15378 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000015379 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000015380 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015381 * flags:
15382 * TFN_INT: internal function name OK
15383 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000015384 * Advances "pp" to just after the function name (if no error).
15385 */
15386 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015387trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015388 char_u **pp;
15389 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015390 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000015391 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015392{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015393 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394 char_u *start;
15395 char_u *end;
15396 int lead;
15397 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000015399 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015400
15401 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000015402 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000015404
15405 /* Check for hard coded <SNR>: already translated function ID (from a user
15406 * command). */
15407 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
15408 && (*pp)[2] == (int)KE_SNR)
15409 {
15410 *pp += 3;
15411 len = get_id_len(pp) + 3;
15412 return vim_strnsave(start, len);
15413 }
15414
15415 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
15416 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015417 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000015418 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015419 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015420
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015421 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015422 if (end == start)
15423 {
15424 if (!skip)
15425 EMSG(_("E129: Function name required"));
15426 goto theend;
15427 }
Bram Moolenaara7043832005-01-21 11:56:39 +000015428 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015429 {
15430 /*
15431 * Report an invalid expression in braces, unless the expression
15432 * evaluation has been cancelled due to an aborting error, an
15433 * interrupt, or an exception.
15434 */
15435 if (!aborting())
15436 {
15437 if (end != NULL)
15438 EMSG2(_(e_invarg2), start);
15439 }
15440 else
15441 *pp = find_name_end(start, NULL, NULL, TRUE);
15442 goto theend;
15443 }
15444
15445 if (lv.ll_tv != NULL)
15446 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015447 if (fdp != NULL)
15448 {
15449 fdp->fd_dict = lv.ll_dict;
15450 fdp->fd_newkey = lv.ll_newkey;
15451 lv.ll_newkey = NULL;
15452 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015453 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015454 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
15455 {
15456 name = vim_strsave(lv.ll_tv->vval.v_string);
15457 *pp = end;
15458 }
15459 else
15460 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015461 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
15462 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015463 EMSG(_(e_funcref));
15464 else
15465 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015466 name = NULL;
15467 }
15468 goto theend;
15469 }
15470
15471 if (lv.ll_name == NULL)
15472 {
15473 /* Error found, but continue after the function name. */
15474 *pp = end;
15475 goto theend;
15476 }
15477
15478 if (lv.ll_exp_name != NULL)
15479 len = STRLEN(lv.ll_exp_name);
15480 else
Bram Moolenaara7043832005-01-21 11:56:39 +000015481 {
15482 if (lead == 2) /* skip over "s:" */
15483 lv.ll_name += 2;
15484 len = (int)(end - lv.ll_name);
15485 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015486
15487 /*
15488 * Copy the function name to allocated memory.
15489 * Accept <SID>name() inside a script, translate into <SNR>123_name().
15490 * Accept <SNR>123_name() outside a script.
15491 */
15492 if (skip)
15493 lead = 0; /* do nothing */
15494 else if (lead > 0)
15495 {
15496 lead = 3;
15497 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
15498 {
15499 if (current_SID <= 0)
15500 {
15501 EMSG(_(e_usingsid));
15502 goto theend;
15503 }
15504 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
15505 lead += (int)STRLEN(sid_buf);
15506 }
15507 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000015508 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015509 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000015510 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015511 goto theend;
15512 }
15513 name = alloc((unsigned)(len + lead + 1));
15514 if (name != NULL)
15515 {
15516 if (lead > 0)
15517 {
15518 name[0] = K_SPECIAL;
15519 name[1] = KS_EXTRA;
15520 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000015521 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015522 STRCPY(name + 3, sid_buf);
15523 }
15524 mch_memmove(name + lead, lv.ll_name, (size_t)len);
15525 name[len + lead] = NUL;
15526 }
15527 *pp = end;
15528
15529theend:
15530 clear_lval(&lv);
15531 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532}
15533
15534/*
15535 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
15536 * Return 2 if "p" starts with "s:".
15537 * Return 0 otherwise.
15538 */
15539 static int
15540eval_fname_script(p)
15541 char_u *p;
15542{
15543 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
15544 || STRNICMP(p + 1, "SNR>", 4) == 0))
15545 return 5;
15546 if (p[0] == 's' && p[1] == ':')
15547 return 2;
15548 return 0;
15549}
15550
15551/*
15552 * Return TRUE if "p" starts with "<SID>" or "s:".
15553 * Only works if eval_fname_script() returned non-zero for "p"!
15554 */
15555 static int
15556eval_fname_sid(p)
15557 char_u *p;
15558{
15559 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
15560}
15561
15562/*
15563 * List the head of the function: "name(arg1, arg2)".
15564 */
15565 static void
15566list_func_head(fp, indent)
15567 ufunc_T *fp;
15568 int indent;
15569{
15570 int j;
15571
15572 msg_start();
15573 if (indent)
15574 MSG_PUTS(" ");
15575 MSG_PUTS("function ");
15576 if (fp->name[0] == K_SPECIAL)
15577 {
15578 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
15579 msg_puts(fp->name + 3);
15580 }
15581 else
15582 msg_puts(fp->name);
15583 msg_putchar('(');
15584 for (j = 0; j < fp->args.ga_len; ++j)
15585 {
15586 if (j)
15587 MSG_PUTS(", ");
15588 msg_puts(FUNCARG(fp, j));
15589 }
15590 if (fp->varargs)
15591 {
15592 if (j)
15593 MSG_PUTS(", ");
15594 MSG_PUTS("...");
15595 }
15596 msg_putchar(')');
15597}
15598
15599/*
15600 * Find a function by name, return pointer to it in ufuncs.
15601 * Return NULL for unknown function.
15602 */
15603 static ufunc_T *
15604find_func(name)
15605 char_u *name;
15606{
15607 ufunc_T *fp;
15608
15609 for (fp = firstfunc; fp != NULL; fp = fp->next)
15610 if (STRCMP(name, fp->name) == 0)
15611 break;
15612 return fp;
15613}
15614
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015615/*
15616 * Return TRUE if a function "name" exists.
15617 */
15618 static int
15619function_exists(name)
15620 char_u *name;
15621{
15622 char_u *p = name;
15623 int n = FALSE;
15624
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015625 p = trans_function_name(&p, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015626 if (p != NULL)
15627 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000015628 if (builtin_function(p))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015629 n = (find_internal_func(p) >= 0);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000015630 else
15631 n = (find_func(p) != NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015632 vim_free(p);
15633 }
15634 return n;
15635}
15636
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000015637/*
15638 * Return TRUE if "name" looks like a builtin function name: starts with a
15639 * lower case letter and doesn't contain a ':'.
15640 */
15641 static int
15642builtin_function(name)
15643 char_u *name;
15644{
15645 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL;
15646}
15647
15648/*
15649 * If "name" has a package name try autoloading the script.
15650 * Return TRUE if a package was loaded.
15651 */
15652 static int
15653func_autoload(name)
15654 char_u *name;
15655{
15656 char_u *p;
15657 char_u *scriptname;
15658 int ret = FALSE;
15659
15660 /* If there is no colon after name[1] there is no package name. */
15661 p = vim_strchr(name, ':');
15662 if (p == NULL || p <= name + 2)
15663 return FALSE;
15664
15665 /* Get the script file name: replace ':' with '/', append ".vim". */
15666 scriptname = alloc((unsigned)(STRLEN(name) + 14));
15667 if (scriptname == NULL)
15668 return FALSE;
15669 STRCPY(scriptname, "autoload/");
15670 STRCAT(scriptname, name);
15671 *vim_strrchr(scriptname, ':') = NUL;
15672 STRCAT(scriptname, ".vim");
15673 while ((p = vim_strchr(scriptname, ':')) != NULL)
15674 *p = '/';
15675
15676 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
15677 if (cmd_runtime(scriptname, FALSE) == OK)
15678 ret = TRUE;
15679
15680 vim_free(scriptname);
15681 return ret;
15682}
15683
Bram Moolenaar071d4272004-06-13 20:20:40 +000015684#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
15685
15686/*
15687 * Function given to ExpandGeneric() to obtain the list of user defined
15688 * function names.
15689 */
15690 char_u *
15691get_user_func_name(xp, idx)
15692 expand_T *xp;
15693 int idx;
15694{
15695 static ufunc_T *fp = NULL;
15696
15697 if (idx == 0)
15698 fp = firstfunc;
15699 if (fp != NULL)
15700 {
15701 if (STRLEN(fp->name) + 4 >= IOSIZE)
15702 return fp->name; /* prevents overflow */
15703
15704 cat_func_name(IObuff, fp);
15705 if (xp->xp_context != EXPAND_USER_FUNC)
15706 {
15707 STRCAT(IObuff, "(");
15708 if (!fp->varargs && fp->args.ga_len == 0)
15709 STRCAT(IObuff, ")");
15710 }
15711
15712 fp = fp->next;
15713 return IObuff;
15714 }
15715 return NULL;
15716}
15717
15718#endif /* FEAT_CMDL_COMPL */
15719
15720/*
15721 * Copy the function name of "fp" to buffer "buf".
15722 * "buf" must be able to hold the function name plus three bytes.
15723 * Takes care of script-local function names.
15724 */
15725 static void
15726cat_func_name(buf, fp)
15727 char_u *buf;
15728 ufunc_T *fp;
15729{
15730 if (fp->name[0] == K_SPECIAL)
15731 {
15732 STRCPY(buf, "<SNR>");
15733 STRCAT(buf, fp->name + 3);
15734 }
15735 else
15736 STRCPY(buf, fp->name);
15737}
15738
15739/*
15740 * ":delfunction {name}"
15741 */
15742 void
15743ex_delfunction(eap)
15744 exarg_T *eap;
15745{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015746 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 char_u *p;
15748 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000015749 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750
15751 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015752 name = trans_function_name(&p, eap->skip, 0, &fudi);
15753 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015754 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015755 {
15756 if (fudi.fd_dict != NULL && !eap->skip)
15757 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 if (!ends_excmd(*skipwhite(p)))
15761 {
15762 vim_free(name);
15763 EMSG(_(e_trailing));
15764 return;
15765 }
15766 eap->nextcmd = check_nextcmd(p);
15767 if (eap->nextcmd != NULL)
15768 *p = NUL;
15769
15770 if (!eap->skip)
15771 fp = find_func(name);
15772 vim_free(name);
15773
15774 if (!eap->skip)
15775 {
15776 if (fp == NULL)
15777 {
15778 EMSG2(_("E130: Undefined function: %s"), eap->arg);
15779 return;
15780 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015781 if (fp->calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782 {
15783 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
15784 return;
15785 }
15786
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015787 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015788 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015789 /* Delete the dict item that refers to the function, it will
15790 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015791 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015792 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015793 else
15794 func_free(fp);
15795 }
15796}
15797
15798/*
15799 * Free a function and remove it from the list of functions.
15800 */
15801 static void
15802func_free(fp)
15803 ufunc_T *fp;
15804{
15805 ufunc_T *pfp;
15806
15807 /* clear this function */
15808 vim_free(fp->name);
15809 ga_clear_strings(&(fp->args));
15810 ga_clear_strings(&(fp->lines));
15811
15812 /* remove the function from the function list */
15813 if (firstfunc == fp)
15814 firstfunc = fp->next;
15815 else
15816 {
15817 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next)
15818 if (pfp->next == fp)
15819 {
15820 pfp->next = fp->next;
15821 break;
15822 }
15823 }
15824 vim_free(fp);
15825}
15826
15827/*
15828 * Unreference a Function: decrement the reference count and free it when it
15829 * becomes zero. Only for numbered functions.
15830 */
15831 static void
15832func_unref(name)
15833 char_u *name;
15834{
15835 ufunc_T *fp;
15836
15837 if (name != NULL && isdigit(*name))
15838 {
15839 fp = find_func(name);
15840 if (fp == NULL)
15841 EMSG2(_(e_intern2), "func_unref()");
15842 else if (--fp->refcount <= 0)
15843 {
15844 /* Only delete it when it's not being used. Otherwise it's done
15845 * when "calls" becomes zero. */
15846 if (fp->calls == 0)
15847 func_free(fp);
15848 }
15849 }
15850}
15851
15852/*
15853 * Count a reference to a Function.
15854 */
15855 static void
15856func_ref(name)
15857 char_u *name;
15858{
15859 ufunc_T *fp;
15860
15861 if (name != NULL && isdigit(*name))
15862 {
15863 fp = find_func(name);
15864 if (fp == NULL)
15865 EMSG2(_(e_intern2), "func_ref()");
15866 else
15867 ++fp->refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015868 }
15869}
15870
15871/*
15872 * Call a user function.
15873 */
15874 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000015875call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015876 ufunc_T *fp; /* pointer to function */
15877 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000015878 typval_T *argvars; /* arguments */
15879 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015880 linenr_T firstline; /* first line of range */
15881 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000015882 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015883{
Bram Moolenaar33570922005-01-25 22:26:29 +000015884 char_u *save_sourcing_name;
15885 linenr_T save_sourcing_lnum;
15886 scid_T save_current_SID;
15887 funccall_T fc;
15888 funccall_T *save_fcp = current_funccal;
15889 int save_did_emsg;
15890 static int depth = 0;
15891 dictitem_T *v;
15892 int fixvar_idx = 0; /* index in fixvar[] */
15893 int i;
15894 int ai;
15895 char_u numbuf[NUMBUFLEN];
15896 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897
15898 /* If depth of calling is getting too high, don't execute the function */
15899 if (depth >= p_mfd)
15900 {
15901 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015902 rettv->v_type = VAR_NUMBER;
15903 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904 return;
15905 }
15906 ++depth;
15907
15908 line_breakcheck(); /* check for CTRL-C hit */
15909
Bram Moolenaar33570922005-01-25 22:26:29 +000015910 current_funccal = &fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015911 fc.func = fp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015912 fc.rettv = rettv;
15913 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015914 fc.linenr = 0;
15915 fc.returned = FALSE;
15916 fc.level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015917 /* Check if this function has a breakpoint. */
15918 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->name, (linenr_T)0);
15919 fc.dbg_tick = debug_tick;
15920
Bram Moolenaar33570922005-01-25 22:26:29 +000015921 /*
15922 * Note about using fc.fixvar[]: This is an array of FIXVAR_CNT variables
15923 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
15924 * each argument variable and saves a lot of time.
15925 */
15926 /*
15927 * Init l: variables.
15928 */
15929 init_var_dict(&fc.l_vars, &fc.l_vars_var);
Bram Moolenaara7043832005-01-21 11:56:39 +000015930 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015931 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015932 /* Set l:self to "selfdict". */
15933 v = &fc.fixvar[fixvar_idx++].var;
15934 STRCPY(v->di_key, "self");
15935 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
15936 hash_add(&fc.l_vars.dv_hashtab, DI2HIKEY(v));
15937 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015938 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015939 v->di_tv.vval.v_dict = selfdict;
15940 ++selfdict->dv_refcount;
15941 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015942
Bram Moolenaar33570922005-01-25 22:26:29 +000015943 /*
15944 * Init a: variables.
15945 * Set a:0 to "argcount".
15946 * Set a:000 to a list with room for the "..." arguments.
15947 */
15948 init_var_dict(&fc.l_avars, &fc.l_avars_var);
15949 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "0",
15950 (varnumber_T)(argcount - fp->args.ga_len));
15951 v = &fc.fixvar[fixvar_idx++].var;
15952 STRCPY(v->di_key, "000");
15953 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15954 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
15955 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015956 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015957 v->di_tv.vval.v_list = &fc.l_varlist;
15958 vim_memset(&fc.l_varlist, 0, sizeof(list_T));
15959 fc.l_varlist.lv_refcount = 99999;
15960
15961 /*
15962 * Set a:firstline to "firstline" and a:lastline to "lastline".
15963 * Set a:name to named arguments.
15964 * Set a:N to the "..." arguments.
15965 */
15966 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "firstline",
15967 (varnumber_T)firstline);
15968 add_nr_var(&fc.l_avars, &fc.fixvar[fixvar_idx++].var, "lastline",
15969 (varnumber_T)lastline);
15970 for (i = 0; i < argcount; ++i)
15971 {
15972 ai = i - fp->args.ga_len;
15973 if (ai < 0)
15974 /* named argument a:name */
15975 name = FUNCARG(fp, i);
15976 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000015977 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015978 /* "..." argument a:1, a:2, etc. */
15979 sprintf((char *)numbuf, "%d", ai + 1);
15980 name = numbuf;
15981 }
15982 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
15983 {
15984 v = &fc.fixvar[fixvar_idx++].var;
15985 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
15986 }
15987 else
15988 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015989 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
15990 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000015991 if (v == NULL)
15992 break;
15993 v->di_flags = DI_FLAGS_RO;
15994 }
15995 STRCPY(v->di_key, name);
15996 hash_add(&fc.l_avars.dv_hashtab, DI2HIKEY(v));
15997
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015998 /* Note: the values are copied directly to avoid alloc/free.
15999 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016000 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016001 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016002
16003 if (ai >= 0 && ai < MAX_FUNC_ARGS)
16004 {
16005 list_append(&fc.l_varlist, &fc.l_listitems[ai]);
16006 fc.l_listitems[ai].li_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016007 fc.l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016008 }
16009 }
16010
Bram Moolenaar071d4272004-06-13 20:20:40 +000016011 /* Don't redraw while executing the function. */
16012 ++RedrawingDisabled;
16013 save_sourcing_name = sourcing_name;
16014 save_sourcing_lnum = sourcing_lnum;
16015 sourcing_lnum = 1;
16016 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
16017 : STRLEN(save_sourcing_name)) + STRLEN(fp->name) + 13));
16018 if (sourcing_name != NULL)
16019 {
16020 if (save_sourcing_name != NULL
16021 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
16022 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
16023 else
16024 STRCPY(sourcing_name, "function ");
16025 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
16026
16027 if (p_verbose >= 12)
16028 {
16029 ++no_wait_return;
16030 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16031 msg_str((char_u *)_("calling %s"), sourcing_name);
16032 if (p_verbose >= 14)
16033 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016034 char_u buf[MSG_BUF_LEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000016035 char_u numbuf[NUMBUFLEN];
16036 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016037
16038 msg_puts((char_u *)"(");
16039 for (i = 0; i < argcount; ++i)
16040 {
16041 if (i > 0)
16042 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016043 if (argvars[i].v_type == VAR_NUMBER)
16044 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016045 else
16046 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016047 trunc_string(tv2string(&argvars[i], &tofree, numbuf),
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048 buf, MSG_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016049 msg_puts(buf);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016050 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016051 }
16052 }
16053 msg_puts((char_u *)")");
16054 }
16055 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16056 cmdline_row = msg_row;
16057 --no_wait_return;
16058 }
16059 }
16060 save_current_SID = current_SID;
16061 current_SID = fp->script_ID;
16062 save_did_emsg = did_emsg;
16063 did_emsg = FALSE;
16064
16065 /* call do_cmdline() to execute the lines */
16066 do_cmdline(NULL, get_func_line, (void *)&fc,
16067 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
16068
16069 --RedrawingDisabled;
16070
16071 /* when the function was aborted because of an error, return -1 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016072 if ((did_emsg && (fp->flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016073 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016074 clear_tv(rettv);
16075 rettv->v_type = VAR_NUMBER;
16076 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077 }
16078
16079 /* when being verbose, mention the return value */
16080 if (p_verbose >= 12)
16081 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016082 char_u *sn;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016083
16084 ++no_wait_return;
16085 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16086
16087 /* Make sure the output fits in IObuff. */
16088 sn = sourcing_name;
16089 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
16090 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
16091
16092 if (aborting())
16093 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016094 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016095 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016096 (long)fc.rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000016097 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000016098 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016099 char_u buf[MSG_BUF_LEN];
16100 char_u numbuf[NUMBUFLEN];
16101 char_u *tofree;
16102
16103 trunc_string(tv2string(fc.rettv, &tofree, numbuf),
16104 buf, MSG_BUF_LEN);
16105 smsg((char_u *)_("%s returning %s"), sn, buf);
16106 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016107 }
16108 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16109 cmdline_row = msg_row;
16110 --no_wait_return;
16111 }
16112
16113 vim_free(sourcing_name);
16114 sourcing_name = save_sourcing_name;
16115 sourcing_lnum = save_sourcing_lnum;
16116 current_SID = save_current_SID;
16117
16118 if (p_verbose >= 12 && sourcing_name != NULL)
16119 {
16120 ++no_wait_return;
16121 msg_scroll = TRUE; /* always scroll up, don't overwrite */
16122 msg_str((char_u *)_("continuing in %s"), sourcing_name);
16123 msg_puts((char_u *)"\n"); /* don't overwrite this either */
16124 cmdline_row = msg_row;
16125 --no_wait_return;
16126 }
16127
16128 did_emsg |= save_did_emsg;
16129 current_funccal = save_fcp;
16130
Bram Moolenaar33570922005-01-25 22:26:29 +000016131 /* The a: variables typevals were not alloced, only free the allocated
16132 * variables. */
16133 vars_clear_ext(&fc.l_avars.dv_hashtab, FALSE);
16134
16135 vars_clear(&fc.l_vars.dv_hashtab); /* free all l: variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016136 --depth;
16137}
16138
16139/*
Bram Moolenaar33570922005-01-25 22:26:29 +000016140 * Add a number variable "name" to dict "dp" with value "nr".
16141 */
16142 static void
16143add_nr_var(dp, v, name, nr)
16144 dict_T *dp;
16145 dictitem_T *v;
16146 char *name;
16147 varnumber_T nr;
16148{
16149 STRCPY(v->di_key, name);
16150 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
16151 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
16152 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016153 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016154 v->di_tv.vval.v_number = nr;
16155}
16156
16157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016158 * ":return [expr]"
16159 */
16160 void
16161ex_return(eap)
16162 exarg_T *eap;
16163{
16164 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000016165 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016166 int returning = FALSE;
16167
16168 if (current_funccal == NULL)
16169 {
16170 EMSG(_("E133: :return not inside a function"));
16171 return;
16172 }
16173
16174 if (eap->skip)
16175 ++emsg_skip;
16176
16177 eap->nextcmd = NULL;
16178 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016179 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016180 {
16181 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016182 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016183 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016184 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016185 }
16186 /* It's safer to return also on error. */
16187 else if (!eap->skip)
16188 {
16189 /*
16190 * Return unless the expression evaluation has been cancelled due to an
16191 * aborting error, an interrupt, or an exception.
16192 */
16193 if (!aborting())
16194 returning = do_return(eap, FALSE, TRUE, NULL);
16195 }
16196
16197 /* When skipping or the return gets pending, advance to the next command
16198 * in this line (!returning). Otherwise, ignore the rest of the line.
16199 * Following lines will be ignored by get_func_line(). */
16200 if (returning)
16201 eap->nextcmd = NULL;
16202 else if (eap->nextcmd == NULL) /* no argument */
16203 eap->nextcmd = check_nextcmd(arg);
16204
16205 if (eap->skip)
16206 --emsg_skip;
16207}
16208
16209/*
16210 * Return from a function. Possibly makes the return pending. Also called
16211 * for a pending return at the ":endtry" or after returning from an extra
16212 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000016213 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016214 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016215 * FALSE when the return gets pending.
16216 */
16217 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016218do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016219 exarg_T *eap;
16220 int reanimate;
16221 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016222 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016223{
16224 int idx;
16225 struct condstack *cstack = eap->cstack;
16226
16227 if (reanimate)
16228 /* Undo the return. */
16229 current_funccal->returned = FALSE;
16230
16231 /*
16232 * Cleanup (and inactivate) conditionals, but stop when a try conditional
16233 * not in its finally clause (which then is to be executed next) is found.
16234 * In this case, make the ":return" pending for execution at the ":endtry".
16235 * Otherwise, return normally.
16236 */
16237 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
16238 if (idx >= 0)
16239 {
16240 cstack->cs_pending[idx] = CSTP_RETURN;
16241
16242 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016243 /* A pending return again gets pending. "rettv" points to an
16244 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000016245 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016246 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016247 else
16248 {
16249 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016250 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016251 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016252 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016254 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255 {
16256 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016257 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016258 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016259 else
16260 EMSG(_(e_outofmem));
16261 }
16262 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016263 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264
16265 if (reanimate)
16266 {
16267 /* The pending return value could be overwritten by a ":return"
16268 * without argument in a finally clause; reset the default
16269 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016270 current_funccal->rettv->v_type = VAR_NUMBER;
16271 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272 }
16273 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016274 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016275 }
16276 else
16277 {
16278 current_funccal->returned = TRUE;
16279
16280 /* If the return is carried out now, store the return value. For
16281 * a return immediately after reanimation, the value is already
16282 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016283 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016285 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000016286 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016288 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016289 }
16290 }
16291
16292 return idx < 0;
16293}
16294
16295/*
16296 * Free the variable with a pending return value.
16297 */
16298 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016299discard_pending_return(rettv)
16300 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301{
Bram Moolenaar33570922005-01-25 22:26:29 +000016302 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016303}
16304
16305/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016306 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000016307 * is an allocated string. Used by report_pending() for verbose messages.
16308 */
16309 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016310get_return_cmd(rettv)
16311 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016313 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016314 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016315 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016316
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016317 if (rettv != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000016318 s = echo_string((typval_T *)rettv, &tofree, numbuf);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016319 if (s == NULL)
16320 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016321
16322 STRCPY(IObuff, ":return ");
16323 STRNCPY(IObuff + 8, s, IOSIZE - 8);
16324 if (STRLEN(s) + 8 >= IOSIZE)
16325 STRCPY(IObuff + IOSIZE - 4, "...");
16326 vim_free(tofree);
16327 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016328}
16329
16330/*
16331 * Get next function line.
16332 * Called by do_cmdline() to get the next line.
16333 * Returns allocated string, or NULL for end of function.
16334 */
16335/* ARGSUSED */
16336 char_u *
16337get_func_line(c, cookie, indent)
16338 int c; /* not used */
16339 void *cookie;
16340 int indent; /* not used */
16341{
Bram Moolenaar33570922005-01-25 22:26:29 +000016342 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016343 char_u *retval;
16344 garray_T *gap; /* growarray with function lines */
16345
16346 /* If breakpoints have been added/deleted need to check for it. */
16347 if (fcp->dbg_tick != debug_tick)
16348 {
16349 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
16350 sourcing_lnum);
16351 fcp->dbg_tick = debug_tick;
16352 }
16353
16354 gap = &fcp->func->lines;
16355 if ((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
16356 retval = NULL;
16357 else if (fcp->returned || fcp->linenr >= gap->ga_len)
16358 retval = NULL;
16359 else
16360 {
16361 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
16362 sourcing_lnum = fcp->linenr;
16363 }
16364
16365 /* Did we encounter a breakpoint? */
16366 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
16367 {
16368 dbg_breakpoint(fcp->func->name, sourcing_lnum);
16369 /* Find next breakpoint. */
16370 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
16371 sourcing_lnum);
16372 fcp->dbg_tick = debug_tick;
16373 }
16374
16375 return retval;
16376}
16377
16378/*
16379 * Return TRUE if the currently active function should be ended, because a
16380 * return was encountered or an error occured. Used inside a ":while".
16381 */
16382 int
16383func_has_ended(cookie)
16384 void *cookie;
16385{
Bram Moolenaar33570922005-01-25 22:26:29 +000016386 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387
16388 /* Ignore the "abort" flag if the abortion behavior has been changed due to
16389 * an error inside a try conditional. */
16390 return (((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
16391 || fcp->returned);
16392}
16393
16394/*
16395 * return TRUE if cookie indicates a function which "abort"s on errors.
16396 */
16397 int
16398func_has_abort(cookie)
16399 void *cookie;
16400{
Bram Moolenaar33570922005-01-25 22:26:29 +000016401 return ((funccall_T *)cookie)->func->flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016402}
16403
16404#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
16405typedef enum
16406{
16407 VAR_FLAVOUR_DEFAULT,
16408 VAR_FLAVOUR_SESSION,
16409 VAR_FLAVOUR_VIMINFO
16410} var_flavour_T;
16411
16412static var_flavour_T var_flavour __ARGS((char_u *varname));
16413
16414 static var_flavour_T
16415var_flavour(varname)
16416 char_u *varname;
16417{
16418 char_u *p = varname;
16419
16420 if (ASCII_ISUPPER(*p))
16421 {
16422 while (*(++p))
16423 if (ASCII_ISLOWER(*p))
16424 return VAR_FLAVOUR_SESSION;
16425 return VAR_FLAVOUR_VIMINFO;
16426 }
16427 else
16428 return VAR_FLAVOUR_DEFAULT;
16429}
16430#endif
16431
16432#if defined(FEAT_VIMINFO) || defined(PROTO)
16433/*
16434 * Restore global vars that start with a capital from the viminfo file
16435 */
16436 int
16437read_viminfo_varlist(virp, writing)
16438 vir_T *virp;
16439 int writing;
16440{
16441 char_u *tab;
16442 int is_string = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016443 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016444
16445 if (!writing && (find_viminfo_parameter('!') != NULL))
16446 {
16447 tab = vim_strchr(virp->vir_line + 1, '\t');
16448 if (tab != NULL)
16449 {
16450 *tab++ = '\0'; /* isolate the variable name */
16451 if (*tab == 'S') /* string var */
16452 is_string = TRUE;
16453
16454 tab = vim_strchr(tab, '\t');
16455 if (tab != NULL)
16456 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016457 if (is_string)
16458 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000016459 tv.v_type = VAR_STRING;
16460 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016461 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016462 }
16463 else
16464 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000016465 tv.v_type = VAR_NUMBER;
16466 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016467 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000016468 set_var(virp->vir_line + 1, &tv, FALSE);
16469 if (is_string)
16470 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471 }
16472 }
16473 }
16474
16475 return viminfo_readline(virp);
16476}
16477
16478/*
16479 * Write global vars that start with a capital to the viminfo file
16480 */
16481 void
16482write_viminfo_varlist(fp)
16483 FILE *fp;
16484{
Bram Moolenaar33570922005-01-25 22:26:29 +000016485 hashitem_T *hi;
16486 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000016487 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016488 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016489 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016490 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016491 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016492
16493 if (find_viminfo_parameter('!') == NULL)
16494 return;
16495
16496 fprintf(fp, _("\n# global variables:\n"));
Bram Moolenaara7043832005-01-21 11:56:39 +000016497
Bram Moolenaar33570922005-01-25 22:26:29 +000016498 todo = globvarht.ht_used;
16499 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016500 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016501 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016502 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016503 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016504 this_var = HI2DI(hi);
16505 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016506 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016507 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000016508 {
16509 case VAR_STRING: s = "STR"; break;
16510 case VAR_NUMBER: s = "NUM"; break;
16511 default: continue;
16512 }
Bram Moolenaar33570922005-01-25 22:26:29 +000016513 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016514 p = echo_string(&this_var->di_tv, &tofree, numbuf);
16515 if (p != NULL)
16516 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000016517 vim_free(tofree);
16518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016519 }
16520 }
16521}
16522#endif
16523
16524#if defined(FEAT_SESSION) || defined(PROTO)
16525 int
16526store_session_globals(fd)
16527 FILE *fd;
16528{
Bram Moolenaar33570922005-01-25 22:26:29 +000016529 hashitem_T *hi;
16530 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000016531 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016532 char_u *p, *t;
16533
Bram Moolenaar33570922005-01-25 22:26:29 +000016534 todo = globvarht.ht_used;
16535 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016536 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016537 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016538 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016539 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000016540 this_var = HI2DI(hi);
16541 if ((this_var->di_tv.v_type == VAR_NUMBER
16542 || this_var->di_tv.v_type == VAR_STRING)
16543 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000016544 {
Bram Moolenaara7043832005-01-21 11:56:39 +000016545 /* Escape special characters with a backslash. Turn a LF and
16546 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016547 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000016548 (char_u *)"\\\"\n\r");
16549 if (p == NULL) /* out of memory */
16550 break;
16551 for (t = p; *t != NUL; ++t)
16552 if (*t == '\n')
16553 *t = 'n';
16554 else if (*t == '\r')
16555 *t = 'r';
16556 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000016557 this_var->di_key,
16558 (this_var->di_tv.v_type == VAR_STRING) ? '"'
16559 : ' ',
16560 p,
16561 (this_var->di_tv.v_type == VAR_STRING) ? '"'
16562 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000016563 || put_eol(fd) == FAIL)
16564 {
16565 vim_free(p);
16566 return FAIL;
16567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016568 vim_free(p);
16569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016570 }
16571 }
16572 return OK;
16573}
16574#endif
16575
16576#endif /* FEAT_EVAL */
16577
16578#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
16579
16580
16581#ifdef WIN3264
16582/*
16583 * Functions for ":8" filename modifier: get 8.3 version of a filename.
16584 */
16585static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
16586static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
16587static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
16588
16589/*
16590 * Get the short pathname of a file.
16591 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
16592 */
16593 static int
16594get_short_pathname(fnamep, bufp, fnamelen)
16595 char_u **fnamep;
16596 char_u **bufp;
16597 int *fnamelen;
16598{
16599 int l,len;
16600 char_u *newbuf;
16601
16602 len = *fnamelen;
16603
16604 l = GetShortPathName(*fnamep, *fnamep, len);
16605 if (l > len - 1)
16606 {
16607 /* If that doesn't work (not enough space), then save the string
16608 * and try again with a new buffer big enough
16609 */
16610 newbuf = vim_strnsave(*fnamep, l);
16611 if (newbuf == NULL)
16612 return 0;
16613
16614 vim_free(*bufp);
16615 *fnamep = *bufp = newbuf;
16616
16617 l = GetShortPathName(*fnamep,*fnamep,l+1);
16618
16619 /* Really should always succeed, as the buffer is big enough */
16620 }
16621
16622 *fnamelen = l;
16623 return 1;
16624}
16625
16626/*
16627 * Create a short path name. Returns the length of the buffer it needs.
16628 * Doesn't copy over the end of the buffer passed in.
16629 */
16630 static int
16631shortpath_for_invalid_fname(fname, bufp, fnamelen)
16632 char_u **fname;
16633 char_u **bufp;
16634 int *fnamelen;
16635{
16636 char_u *s, *p, *pbuf2, *pbuf3;
16637 char_u ch;
16638 int l,len,len2,plen,slen;
16639
16640 /* Make a copy */
16641 len2 = *fnamelen;
16642 pbuf2 = vim_strnsave(*fname, len2);
16643 pbuf3 = NULL;
16644
16645 s = pbuf2 + len2 - 1; /* Find the end */
16646 slen = 1;
16647 plen = len2;
16648
16649 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016650 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016651 {
16652 --s;
16653 ++slen;
16654 --plen;
16655 }
16656
16657 do
16658 {
16659 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016660 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016661 {
16662 --s;
16663 ++slen;
16664 --plen;
16665 }
16666 if (s <= pbuf2)
16667 break;
16668
16669 /* Remeber the character that is about to be blatted */
16670 ch = *s;
16671 *s = 0; /* get_short_pathname requires a null-terminated string */
16672
16673 /* Try it in situ */
16674 p = pbuf2;
16675 if (!get_short_pathname(&p, &pbuf3, &plen))
16676 {
16677 vim_free(pbuf2);
16678 return -1;
16679 }
16680 *s = ch; /* Preserve the string */
16681 } while (plen == 0);
16682
16683 if (plen > 0)
16684 {
16685 /* Remeber the length of the new string. */
16686 *fnamelen = len = plen + slen;
16687 vim_free(*bufp);
16688 if (len > len2)
16689 {
16690 /* If there's not enough space in the currently allocated string,
16691 * then copy it to a buffer big enough.
16692 */
16693 *fname= *bufp = vim_strnsave(p, len);
16694 if (*fname == NULL)
16695 return -1;
16696 }
16697 else
16698 {
16699 /* Transfer pbuf2 to being the main buffer (it's big enough) */
16700 *fname = *bufp = pbuf2;
16701 if (p != pbuf2)
16702 strncpy(*fname, p, plen);
16703 pbuf2 = NULL;
16704 }
16705 /* Concat the next bit */
16706 strncpy(*fname + plen, s, slen);
16707 (*fname)[len] = '\0';
16708 }
16709 vim_free(pbuf3);
16710 vim_free(pbuf2);
16711 return 0;
16712}
16713
16714/*
16715 * Get a pathname for a partial path.
16716 */
16717 static int
16718shortpath_for_partial(fnamep, bufp, fnamelen)
16719 char_u **fnamep;
16720 char_u **bufp;
16721 int *fnamelen;
16722{
16723 int sepcount, len, tflen;
16724 char_u *p;
16725 char_u *pbuf, *tfname;
16726 int hasTilde;
16727
16728 /* Count up the path seperators from the RHS.. so we know which part
16729 * of the path to return.
16730 */
16731 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016732 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016733 if (vim_ispathsep(*p))
16734 ++sepcount;
16735
16736 /* Need full path first (use expand_env() to remove a "~/") */
16737 hasTilde = (**fnamep == '~');
16738 if (hasTilde)
16739 pbuf = tfname = expand_env_save(*fnamep);
16740 else
16741 pbuf = tfname = FullName_save(*fnamep, FALSE);
16742
16743 len = tflen = STRLEN(tfname);
16744
16745 if (!get_short_pathname(&tfname, &pbuf, &len))
16746 return -1;
16747
16748 if (len == 0)
16749 {
16750 /* Don't have a valid filename, so shorten the rest of the
16751 * path if we can. This CAN give us invalid 8.3 filenames, but
16752 * there's not a lot of point in guessing what it might be.
16753 */
16754 len = tflen;
16755 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
16756 return -1;
16757 }
16758
16759 /* Count the paths backward to find the beginning of the desired string. */
16760 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016761 {
16762#ifdef FEAT_MBYTE
16763 if (has_mbyte)
16764 p -= mb_head_off(tfname, p);
16765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766 if (vim_ispathsep(*p))
16767 {
16768 if (sepcount == 0 || (hasTilde && sepcount == 1))
16769 break;
16770 else
16771 sepcount --;
16772 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774 if (hasTilde)
16775 {
16776 --p;
16777 if (p >= tfname)
16778 *p = '~';
16779 else
16780 return -1;
16781 }
16782 else
16783 ++p;
16784
16785 /* Copy in the string - p indexes into tfname - allocated at pbuf */
16786 vim_free(*bufp);
16787 *fnamelen = (int)STRLEN(p);
16788 *bufp = pbuf;
16789 *fnamep = p;
16790
16791 return 0;
16792}
16793#endif /* WIN3264 */
16794
16795/*
16796 * Adjust a filename, according to a string of modifiers.
16797 * *fnamep must be NUL terminated when called. When returning, the length is
16798 * determined by *fnamelen.
16799 * Returns valid flags.
16800 * When there is an error, *fnamep is set to NULL.
16801 */
16802 int
16803modify_fname(src, usedlen, fnamep, bufp, fnamelen)
16804 char_u *src; /* string with modifiers */
16805 int *usedlen; /* characters after src that are used */
16806 char_u **fnamep; /* file name so far */
16807 char_u **bufp; /* buffer for allocated file name or NULL */
16808 int *fnamelen; /* length of fnamep */
16809{
16810 int valid = 0;
16811 char_u *tail;
16812 char_u *s, *p, *pbuf;
16813 char_u dirname[MAXPATHL];
16814 int c;
16815 int has_fullname = 0;
16816#ifdef WIN3264
16817 int has_shortname = 0;
16818#endif
16819
16820repeat:
16821 /* ":p" - full path/file_name */
16822 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
16823 {
16824 has_fullname = 1;
16825
16826 valid |= VALID_PATH;
16827 *usedlen += 2;
16828
16829 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
16830 if ((*fnamep)[0] == '~'
16831#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
16832 && ((*fnamep)[1] == '/'
16833# ifdef BACKSLASH_IN_FILENAME
16834 || (*fnamep)[1] == '\\'
16835# endif
16836 || (*fnamep)[1] == NUL)
16837
16838#endif
16839 )
16840 {
16841 *fnamep = expand_env_save(*fnamep);
16842 vim_free(*bufp); /* free any allocated file name */
16843 *bufp = *fnamep;
16844 if (*fnamep == NULL)
16845 return -1;
16846 }
16847
16848 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016849 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016850 {
16851 if (vim_ispathsep(*p)
16852 && p[1] == '.'
16853 && (p[2] == NUL
16854 || vim_ispathsep(p[2])
16855 || (p[2] == '.'
16856 && (p[3] == NUL || vim_ispathsep(p[3])))))
16857 break;
16858 }
16859
16860 /* FullName_save() is slow, don't use it when not needed. */
16861 if (*p != NUL || !vim_isAbsName(*fnamep))
16862 {
16863 *fnamep = FullName_save(*fnamep, *p != NUL);
16864 vim_free(*bufp); /* free any allocated file name */
16865 *bufp = *fnamep;
16866 if (*fnamep == NULL)
16867 return -1;
16868 }
16869
16870 /* Append a path separator to a directory. */
16871 if (mch_isdir(*fnamep))
16872 {
16873 /* Make room for one or two extra characters. */
16874 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
16875 vim_free(*bufp); /* free any allocated file name */
16876 *bufp = *fnamep;
16877 if (*fnamep == NULL)
16878 return -1;
16879 add_pathsep(*fnamep);
16880 }
16881 }
16882
16883 /* ":." - path relative to the current directory */
16884 /* ":~" - path relative to the home directory */
16885 /* ":8" - shortname path - postponed till after */
16886 while (src[*usedlen] == ':'
16887 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
16888 {
16889 *usedlen += 2;
16890 if (c == '8')
16891 {
16892#ifdef WIN3264
16893 has_shortname = 1; /* Postpone this. */
16894#endif
16895 continue;
16896 }
16897 pbuf = NULL;
16898 /* Need full path first (use expand_env() to remove a "~/") */
16899 if (!has_fullname)
16900 {
16901 if (c == '.' && **fnamep == '~')
16902 p = pbuf = expand_env_save(*fnamep);
16903 else
16904 p = pbuf = FullName_save(*fnamep, FALSE);
16905 }
16906 else
16907 p = *fnamep;
16908
16909 has_fullname = 0;
16910
16911 if (p != NULL)
16912 {
16913 if (c == '.')
16914 {
16915 mch_dirname(dirname, MAXPATHL);
16916 s = shorten_fname(p, dirname);
16917 if (s != NULL)
16918 {
16919 *fnamep = s;
16920 if (pbuf != NULL)
16921 {
16922 vim_free(*bufp); /* free any allocated file name */
16923 *bufp = pbuf;
16924 pbuf = NULL;
16925 }
16926 }
16927 }
16928 else
16929 {
16930 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
16931 /* Only replace it when it starts with '~' */
16932 if (*dirname == '~')
16933 {
16934 s = vim_strsave(dirname);
16935 if (s != NULL)
16936 {
16937 *fnamep = s;
16938 vim_free(*bufp);
16939 *bufp = s;
16940 }
16941 }
16942 }
16943 vim_free(pbuf);
16944 }
16945 }
16946
16947 tail = gettail(*fnamep);
16948 *fnamelen = (int)STRLEN(*fnamep);
16949
16950 /* ":h" - head, remove "/file_name", can be repeated */
16951 /* Don't remove the first "/" or "c:\" */
16952 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
16953 {
16954 valid |= VALID_HEAD;
16955 *usedlen += 2;
16956 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016957 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958 --tail;
16959 *fnamelen = (int)(tail - *fnamep);
16960#ifdef VMS
16961 if (*fnamelen > 0)
16962 *fnamelen += 1; /* the path separator is part of the path */
16963#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016964 while (tail > s && !after_pathsep(s, tail))
16965 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966 }
16967
16968 /* ":8" - shortname */
16969 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
16970 {
16971 *usedlen += 2;
16972#ifdef WIN3264
16973 has_shortname = 1;
16974#endif
16975 }
16976
16977#ifdef WIN3264
16978 /* Check shortname after we have done 'heads' and before we do 'tails'
16979 */
16980 if (has_shortname)
16981 {
16982 pbuf = NULL;
16983 /* Copy the string if it is shortened by :h */
16984 if (*fnamelen < (int)STRLEN(*fnamep))
16985 {
16986 p = vim_strnsave(*fnamep, *fnamelen);
16987 if (p == 0)
16988 return -1;
16989 vim_free(*bufp);
16990 *bufp = *fnamep = p;
16991 }
16992
16993 /* Split into two implementations - makes it easier. First is where
16994 * there isn't a full name already, second is where there is.
16995 */
16996 if (!has_fullname && !vim_isAbsName(*fnamep))
16997 {
16998 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
16999 return -1;
17000 }
17001 else
17002 {
17003 int l;
17004
17005 /* Simple case, already have the full-name
17006 * Nearly always shorter, so try first time. */
17007 l = *fnamelen;
17008 if (!get_short_pathname(fnamep, bufp, &l))
17009 return -1;
17010
17011 if (l == 0)
17012 {
17013 /* Couldn't find the filename.. search the paths.
17014 */
17015 l = *fnamelen;
17016 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
17017 return -1;
17018 }
17019 *fnamelen = l;
17020 }
17021 }
17022#endif /* WIN3264 */
17023
17024 /* ":t" - tail, just the basename */
17025 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
17026 {
17027 *usedlen += 2;
17028 *fnamelen -= (int)(tail - *fnamep);
17029 *fnamep = tail;
17030 }
17031
17032 /* ":e" - extension, can be repeated */
17033 /* ":r" - root, without extension, can be repeated */
17034 while (src[*usedlen] == ':'
17035 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
17036 {
17037 /* find a '.' in the tail:
17038 * - for second :e: before the current fname
17039 * - otherwise: The last '.'
17040 */
17041 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
17042 s = *fnamep - 2;
17043 else
17044 s = *fnamep + *fnamelen - 1;
17045 for ( ; s > tail; --s)
17046 if (s[0] == '.')
17047 break;
17048 if (src[*usedlen + 1] == 'e') /* :e */
17049 {
17050 if (s > tail)
17051 {
17052 *fnamelen += (int)(*fnamep - (s + 1));
17053 *fnamep = s + 1;
17054#ifdef VMS
17055 /* cut version from the extension */
17056 s = *fnamep + *fnamelen - 1;
17057 for ( ; s > *fnamep; --s)
17058 if (s[0] == ';')
17059 break;
17060 if (s > *fnamep)
17061 *fnamelen = s - *fnamep;
17062#endif
17063 }
17064 else if (*fnamep <= tail)
17065 *fnamelen = 0;
17066 }
17067 else /* :r */
17068 {
17069 if (s > tail) /* remove one extension */
17070 *fnamelen = (int)(s - *fnamep);
17071 }
17072 *usedlen += 2;
17073 }
17074
17075 /* ":s?pat?foo?" - substitute */
17076 /* ":gs?pat?foo?" - global substitute */
17077 if (src[*usedlen] == ':'
17078 && (src[*usedlen + 1] == 's'
17079 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
17080 {
17081 char_u *str;
17082 char_u *pat;
17083 char_u *sub;
17084 int sep;
17085 char_u *flags;
17086 int didit = FALSE;
17087
17088 flags = (char_u *)"";
17089 s = src + *usedlen + 2;
17090 if (src[*usedlen + 1] == 'g')
17091 {
17092 flags = (char_u *)"g";
17093 ++s;
17094 }
17095
17096 sep = *s++;
17097 if (sep)
17098 {
17099 /* find end of pattern */
17100 p = vim_strchr(s, sep);
17101 if (p != NULL)
17102 {
17103 pat = vim_strnsave(s, (int)(p - s));
17104 if (pat != NULL)
17105 {
17106 s = p + 1;
17107 /* find end of substitution */
17108 p = vim_strchr(s, sep);
17109 if (p != NULL)
17110 {
17111 sub = vim_strnsave(s, (int)(p - s));
17112 str = vim_strnsave(*fnamep, *fnamelen);
17113 if (sub != NULL && str != NULL)
17114 {
17115 *usedlen = (int)(p + 1 - src);
17116 s = do_string_sub(str, pat, sub, flags);
17117 if (s != NULL)
17118 {
17119 *fnamep = s;
17120 *fnamelen = (int)STRLEN(s);
17121 vim_free(*bufp);
17122 *bufp = s;
17123 didit = TRUE;
17124 }
17125 }
17126 vim_free(sub);
17127 vim_free(str);
17128 }
17129 vim_free(pat);
17130 }
17131 }
17132 /* after using ":s", repeat all the modifiers */
17133 if (didit)
17134 goto repeat;
17135 }
17136 }
17137
17138 return valid;
17139}
17140
17141/*
17142 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
17143 * "flags" can be "g" to do a global substitute.
17144 * Returns an allocated string, NULL for error.
17145 */
17146 char_u *
17147do_string_sub(str, pat, sub, flags)
17148 char_u *str;
17149 char_u *pat;
17150 char_u *sub;
17151 char_u *flags;
17152{
17153 int sublen;
17154 regmatch_T regmatch;
17155 int i;
17156 int do_all;
17157 char_u *tail;
17158 garray_T ga;
17159 char_u *ret;
17160 char_u *save_cpo;
17161
17162 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
17163 save_cpo = p_cpo;
17164 p_cpo = (char_u *)"";
17165
17166 ga_init2(&ga, 1, 200);
17167
17168 do_all = (flags[0] == 'g');
17169
17170 regmatch.rm_ic = p_ic;
17171 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17172 if (regmatch.regprog != NULL)
17173 {
17174 tail = str;
17175 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
17176 {
17177 /*
17178 * Get some space for a temporary buffer to do the substitution
17179 * into. It will contain:
17180 * - The text up to where the match is.
17181 * - The substituted text.
17182 * - The text after the match.
17183 */
17184 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
17185 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
17186 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
17187 {
17188 ga_clear(&ga);
17189 break;
17190 }
17191
17192 /* copy the text up to where the match is */
17193 i = (int)(regmatch.startp[0] - tail);
17194 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
17195 /* add the substituted text */
17196 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
17197 + ga.ga_len + i, TRUE, TRUE, FALSE);
17198 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017199 /* avoid getting stuck on a match with an empty string */
17200 if (tail == regmatch.endp[0])
17201 {
17202 if (*tail == NUL)
17203 break;
17204 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
17205 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017206 }
17207 else
17208 {
17209 tail = regmatch.endp[0];
17210 if (*tail == NUL)
17211 break;
17212 }
17213 if (!do_all)
17214 break;
17215 }
17216
17217 if (ga.ga_data != NULL)
17218 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
17219
17220 vim_free(regmatch.regprog);
17221 }
17222
17223 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
17224 ga_clear(&ga);
17225 p_cpo = save_cpo;
17226
17227 return ret;
17228}
17229
17230#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */