blob: 100bc80a991ddb28cef9b2934810f25df8dba532 [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
33#if SIZEOF_INT <= 3 /* use long if int is smaller than 32 bits */
34typedef long varnumber_T;
35#else
36typedef int varnumber_T;
37#endif
38
39/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000040 * Structure to hold an internal variable without a name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000041 */
42typedef struct
43{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000044 char v_type; /* see below: VAR_NUMBER, VAR_STRING, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 union
46 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000047 varnumber_T v_number; /* number value */
48 char_u *v_string; /* string value (can be NULL!) */
49 struct listvar_S *v_list; /* list value (can be NULL!) */
Bram Moolenaar8c711452005-01-14 21:53:12 +000050 struct dictvar_S *v_dict; /* dict value (can be NULL!) */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000051 } vval;
52} typeval;
53
54/* Values for "v_type". */
55#define VAR_UNKNOWN 0
56#define VAR_NUMBER 1 /* "v_number" is used */
57#define VAR_STRING 2 /* "v_string" is used */
58#define VAR_FUNC 3 /* "v_string" is function name */
59#define VAR_LIST 4 /* "v_list" is used */
Bram Moolenaar8c711452005-01-14 21:53:12 +000060#define VAR_DICT 5 /* "v_dict" is used */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000061
62/*
63 * Structure to hold an internal variable with a name.
64 * The "tv" must come first, so that this can be used as a "typeval" as well.
65 */
66typedef struct
67{
68 typeval tv; /* type and value of the variable */
69 char_u *v_name; /* name of variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000070} var;
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072typedef var * VAR;
73
74/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000075 * Structure to hold an item of a list: an internal variable without a name.
76 */
77struct listitem_S
78{
79 struct listitem_S *li_next; /* next item in list */
80 struct listitem_S *li_prev; /* previous item in list */
81 typeval li_tv; /* type and value of the variable */
82};
83
84typedef struct listitem_S listitem;
85
86/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000087 * Struct used by those that are using an item in a list.
88 */
89typedef struct listwatch_S
90{
91 listitem *lw_item; /* item being watched */
92 struct listwatch_S *lw_next; /* next watcher */
93} listwatch;
94
95/*
96 * Structure to hold info about a list.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000097 */
98struct listvar_S
99{
100 int lv_refcount; /* reference count */
101 listitem *lv_first; /* first item, NULL if none */
102 listitem *lv_last; /* last item, NULL if none */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000103 listwatch *lv_watch; /* first watcher, NULL if none */
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000104};
105
106typedef struct listvar_S listvar;
107
108#define VAR_LIST_MAXNEST 100 /* maximum nesting of lists */
Bram Moolenaar8c711452005-01-14 21:53:12 +0000109
110/*
111 * Structure to hold an item of a Dictionary.
112 */
113struct dictitem_S
114{
115 struct dictitem_S *di_next; /* next item in list */
116 char_u *di_key; /* key string */
117 typeval di_tv; /* type and value of the variable */
118};
119
120typedef struct dictitem_S dictitem;
121
122/*
123 * Structure to hold info about a Dictionary.
124 */
125struct dictvar_S
126{
127 int dv_refcount; /* reference count */
128 dictitem *dv_first; /* first item, NULL if none */
129};
130
131typedef struct dictvar_S dictvar;
132
133
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000134static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000135static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000136static char *e_undefvar = N_("E121: Undefined variable: %s");
137static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000138static char *e_intern2 = N_("E685: Internal error: %s");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000139static char *e_listarg = N_("E686: Argument of %s must be a List");
140static char *e_listdictarg = N_("E999: Argument of %s must be a List or Dictionaary");
141static char *e_emptykey = N_("E999: Empty key in Dictionary");
142static char *e_listreq = N_("E999: List required");
143static char *e_dictreq = N_("E999: Dictionary required");
144static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
145static char *e_dictkey = N_("E999: key not found in Dictionary: %s");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000146
147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 * All user-defined global variables are stored in "variables".
149 */
150garray_T variables = {0, 0, sizeof(var), 4, NULL};
151
152/*
153 * Array to hold an array with variables local to each sourced script.
154 */
155static garray_T ga_scripts = {0, 0, sizeof(garray_T), 4, NULL};
156#define SCRIPT_VARS(id) (((garray_T *)ga_scripts.ga_data)[(id) - 1])
157
158
159#define VAR_ENTRY(idx) (((VAR)(variables.ga_data))[idx])
160#define VAR_GAP_ENTRY(idx, gap) (((VAR)(gap->ga_data))[idx])
161#define BVAR_ENTRY(idx) (((VAR)(curbuf->b_vars.ga_data))[idx])
162#define WVAR_ENTRY(idx) (((VAR)(curwin->w_vars.ga_data))[idx])
163
164static int echo_attr = 0; /* attributes used for ":echo" */
165
166/*
167 * Structure to hold info for a user function.
168 */
169typedef struct ufunc ufunc_T;
170
171struct ufunc
172{
173 ufunc_T *next; /* next function in list */
174 char_u *name; /* name of function; can start with <SNR>123_
175 (<SNR> is K_SPECIAL KS_EXTRA KE_SNR) */
176 int varargs; /* variable nr of arguments */
177 int flags;
178 int calls; /* nr of active calls */
179 garray_T args; /* arguments */
180 garray_T lines; /* function lines */
181 scid_T script_ID; /* ID of script where function was defined,
182 used for s: variables */
183};
184
185/* function flags */
186#define FC_ABORT 1 /* abort function on error */
187#define FC_RANGE 2 /* function accepts range */
188
189/*
190 * All user-defined functions are found in the forward-linked function list.
191 * The first function is pointed at by firstfunc.
192 */
193ufunc_T *firstfunc = NULL;
194
195#define FUNCARG(fp, j) ((char_u **)(fp->args.ga_data))[j]
196#define FUNCLINE(fp, j) ((char_u **)(fp->lines.ga_data))[j]
197
198/* structure to hold info for a function that is currently being executed. */
199struct funccall
200{
201 ufunc_T *func; /* function being called */
202 int linenr; /* next line to be executed */
203 int returned; /* ":return" used */
204 int argcount; /* nr of arguments */
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000205 typeval *argvars; /* arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 var a0_var; /* "a:0" variable */
207 var firstline; /* "a:firstline" variable */
208 var lastline; /* "a:lastline" variable */
209 garray_T l_vars; /* local function variables */
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000210 typeval *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211 linenr_T breakpoint; /* next line with breakpoint or zero */
212 int dbg_tick; /* debug_tick when breakpoint was set */
213 int level; /* top nesting level of executed function */
214};
215
216/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000217 * Info used by a ":for" loop.
218 */
219typedef struct forinfo_S
220{
221 int fi_semicolon; /* TRUE if ending in '; var]' */
222 int fi_varcount; /* nr of variables in the list */
223 listwatch fi_lw; /* keep an eye on the item used. */
224 listvar *fi_list; /* list being used */
225} forinfo;
226
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000227/* used for map() */
228static typeval amp_tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000229
230/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 * Return the name of the executed function.
232 */
233 char_u *
234func_name(cookie)
235 void *cookie;
236{
237 return ((struct funccall *)cookie)->func->name;
238}
239
240/*
241 * Return the address holding the next breakpoint line for a funccall cookie.
242 */
243 linenr_T *
244func_breakpoint(cookie)
245 void *cookie;
246{
247 return &((struct funccall *)cookie)->breakpoint;
248}
249
250/*
251 * Return the address holding the debug tick for a funccall cookie.
252 */
253 int *
254func_dbg_tick(cookie)
255 void *cookie;
256{
257 return &((struct funccall *)cookie)->dbg_tick;
258}
259
260/*
261 * Return the nesting level for a funccall cookie.
262 */
263 int
264func_level(cookie)
265 void *cookie;
266{
267 return ((struct funccall *)cookie)->level;
268}
269
270/* pointer to funccal for currently active function */
271struct funccall *current_funccal = NULL;
272
273/*
274 * Return TRUE when a function was ended by a ":return" command.
275 */
276 int
277current_func_returned()
278{
279 return current_funccal->returned;
280}
281
282
283/*
284 * Array to hold the value of v: variables.
285 */
286#include "version.h"
287
288/* values for flags: */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
291#define VV_RO_SBX 4 /* read-only in the sandbox*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292
293struct vimvar
294{
295 char *name; /* name of variable, without v: */
296 int len; /* length of name */
297 char_u *val; /* current value (can also be a number!) */
298 char type; /* VAR_NUMBER or VAR_STRING */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000299 char flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300} vimvars[VV_LEN] =
301{ /* The order here must match the VV_ defines in vim.h! */
302 {"count", sizeof("count") - 1, NULL, VAR_NUMBER, VV_COMPAT+VV_RO},
303 {"count1", sizeof("count1") - 1, NULL, VAR_NUMBER, VV_RO},
304 {"prevcount", sizeof("prevcount") - 1, NULL, VAR_NUMBER, VV_RO},
305 {"errmsg", sizeof("errmsg") - 1, NULL, VAR_STRING, VV_COMPAT},
306 {"warningmsg", sizeof("warningmsg") - 1, NULL, VAR_STRING, 0},
307 {"statusmsg", sizeof("statusmsg") - 1, NULL, VAR_STRING, 0},
308 {"shell_error", sizeof("shell_error") - 1, NULL, VAR_NUMBER,
309 VV_COMPAT+VV_RO},
310 {"this_session", sizeof("this_session") - 1, NULL, VAR_STRING, VV_COMPAT},
311 {"version", sizeof("version") - 1, (char_u *)VIM_VERSION_100,
312 VAR_NUMBER, VV_COMPAT+VV_RO},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000313 {"lnum", sizeof("lnum") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 {"termresponse", sizeof("termresponse") - 1, NULL, VAR_STRING, VV_RO},
315 {"fname", sizeof("fname") - 1, NULL, VAR_STRING, VV_RO},
316 {"lang", sizeof("lang") - 1, NULL, VAR_STRING, VV_RO},
317 {"lc_time", sizeof("lc_time") - 1, NULL, VAR_STRING, VV_RO},
318 {"ctype", sizeof("ctype") - 1, NULL, VAR_STRING, VV_RO},
319 {"charconvert_from", sizeof("charconvert_from") - 1, NULL, VAR_STRING, VV_RO},
320 {"charconvert_to", sizeof("charconvert_to") - 1, NULL, VAR_STRING, VV_RO},
321 {"fname_in", sizeof("fname_in") - 1, NULL, VAR_STRING, VV_RO},
322 {"fname_out", sizeof("fname_out") - 1, NULL, VAR_STRING, VV_RO},
323 {"fname_new", sizeof("fname_new") - 1, NULL, VAR_STRING, VV_RO},
324 {"fname_diff", sizeof("fname_diff") - 1, NULL, VAR_STRING, VV_RO},
325 {"cmdarg", sizeof("cmdarg") - 1, NULL, VAR_STRING, VV_RO},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000326 {"foldstart", sizeof("foldstart") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
327 {"foldend", sizeof("foldend") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
328 {"folddashes", sizeof("folddashes") - 1, NULL, VAR_STRING, VV_RO_SBX},
329 {"foldlevel", sizeof("foldlevel") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 {"progname", sizeof("progname") - 1, NULL, VAR_STRING, VV_RO},
331 {"servername", sizeof("servername") - 1, NULL, VAR_STRING, VV_RO},
332 {"dying", sizeof("dying") - 1, NULL, VAR_NUMBER, VV_RO},
333 {"exception", sizeof("exception") - 1, NULL, VAR_STRING, VV_RO},
334 {"throwpoint", sizeof("throwpoint") - 1, NULL, VAR_STRING, VV_RO},
335 {"register", sizeof("register") - 1, NULL, VAR_STRING, VV_RO},
336 {"cmdbang", sizeof("cmdbang") - 1, NULL, VAR_NUMBER, VV_RO},
Bram Moolenaar843ee412004-06-30 16:16:41 +0000337 {"insertmode", sizeof("insertmode") - 1, NULL, VAR_STRING, VV_RO},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338};
339
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000340static int eval0 __ARGS((char_u *arg, typeval *rettv, char_u **nextcmd, int evaluate));
341static int eval1 __ARGS((char_u **arg, typeval *rettv, int evaluate));
342static int eval2 __ARGS((char_u **arg, typeval *rettv, int evaluate));
343static int eval3 __ARGS((char_u **arg, typeval *rettv, int evaluate));
344static int eval4 __ARGS((char_u **arg, typeval *rettv, int evaluate));
345static int eval5 __ARGS((char_u **arg, typeval *rettv, int evaluate));
346static int eval6 __ARGS((char_u **arg, typeval *rettv, int evaluate));
347static int eval7 __ARGS((char_u **arg, typeval *rettv, int evaluate));
348static int eval_index __ARGS((char_u **arg, typeval *rettv, int evaluate));
349static int get_option_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
350static int get_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
351static int get_lit_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
352static int get_list_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000353static listvar *list_alloc __ARGS((void));
354static void list_unref __ARGS((listvar *l));
355static void list_free __ARGS((listvar *l));
356static listitem *listitem_alloc __ARGS((void));
357static void listitem_free __ARGS((listitem *item));
358static long list_len __ARGS((listvar *l));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000359static int list_equal __ARGS((listvar *l1, listvar *l2, int ic));
360static int tv_equal __ARGS((typeval *tv1, typeval *tv2, int ic));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000361static int string_isa_number __ARGS((char_u *s));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000362static listitem *list_find __ARGS((listvar *l, long n));
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000363static long list_idx_of_item __ARGS((listvar *l, listitem *item));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000364static listitem *list_find_ext __ARGS((listvar *l, long *ip));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000365static void list_append __ARGS((listvar *l, listitem *item));
366static int list_append_tv __ARGS((listvar *l, typeval *tv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000367static int list_insert_tv __ARGS((listvar *l, typeval *tv, listitem *item));
368static int list_extend __ARGS((listvar *l1, listvar *l2, listitem *bef));
369static int list_concat __ARGS((listvar *l1, listvar *l2, typeval *tv));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000370static listvar *list_copy __ARGS((listvar *orig, int deep));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000371static void list_getrem __ARGS((listvar *l, listitem *item, listitem *item2));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000372static char_u *list2string __ARGS((typeval *tv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000373static void list_join __ARGS((garray_T *gap, listvar *l, char_u *sep, int echo));
374
Bram Moolenaar8c711452005-01-14 21:53:12 +0000375static dictvar *dict_alloc __ARGS((void));
376static void dict_unref __ARGS((dictvar *d));
377static void dict_free __ARGS((dictvar *d));
378static dictitem *dictitem_alloc __ARGS((void));
379static void dictitem_free __ARGS((dictitem *item));
380static void dict_add __ARGS((dictvar *d, dictitem *item));
381static dictitem *dict_find __ARGS((dictvar *d, char_u *key, int len));
382static char_u *dict2string __ARGS((typeval *tv));
383static int get_dict_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
384
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000385static char_u *echo_string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000386static char_u *tv2string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000387static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000388static int get_env_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000390static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000391static int get_func_tv __ARGS((char_u *name, int len, typeval *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate));
392static int call_func __ARGS((char_u *name, int len, typeval *rettv, int argcount, typeval *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000393
394static void f_add __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000395static void f_append __ARGS((typeval *argvars, typeval *rettv));
396static void f_argc __ARGS((typeval *argvars, typeval *rettv));
397static void f_argidx __ARGS((typeval *argvars, typeval *rettv));
398static void f_argv __ARGS((typeval *argvars, typeval *rettv));
399static void f_browse __ARGS((typeval *argvars, typeval *rettv));
400static void f_browsedir __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000401static void f_bufexists __ARGS((typeval *argvars, typeval *rettv));
402static void f_buflisted __ARGS((typeval *argvars, typeval *rettv));
403static void f_bufloaded __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000404static void f_bufname __ARGS((typeval *argvars, typeval *rettv));
405static void f_bufnr __ARGS((typeval *argvars, typeval *rettv));
406static void f_bufwinnr __ARGS((typeval *argvars, typeval *rettv));
407static void f_byte2line __ARGS((typeval *argvars, typeval *rettv));
408static void f_byteidx __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000409static void f_call __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000410static void f_char2nr __ARGS((typeval *argvars, typeval *rettv));
411static void f_cindent __ARGS((typeval *argvars, typeval *rettv));
412static void f_col __ARGS((typeval *argvars, typeval *rettv));
413static void f_confirm __ARGS((typeval *argvars, typeval *rettv));
414static void f_copy __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000415static void f_count __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000416static void f_cscope_connection __ARGS((typeval *argvars, typeval *rettv));
417static void f_cursor __ARGS((typeval *argsvars, typeval *rettv));
418static void f_deepcopy __ARGS((typeval *argvars, typeval *rettv));
419static void f_delete __ARGS((typeval *argvars, typeval *rettv));
420static void f_did_filetype __ARGS((typeval *argvars, typeval *rettv));
421static void f_diff_filler __ARGS((typeval *argvars, typeval *rettv));
422static void f_diff_hlID __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000423static void f_empty __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000424static void f_escape __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000425static void f_eval __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000426static void f_eventhandler __ARGS((typeval *argvars, typeval *rettv));
427static void f_executable __ARGS((typeval *argvars, typeval *rettv));
428static void f_exists __ARGS((typeval *argvars, typeval *rettv));
429static void f_expand __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000430static void f_extend __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000431static void f_filereadable __ARGS((typeval *argvars, typeval *rettv));
432static void f_filewritable __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000433static void f_filter __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000434static void f_finddir __ARGS((typeval *argvars, typeval *rettv));
435static void f_findfile __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000436static void f_fnamemodify __ARGS((typeval *argvars, typeval *rettv));
437static void f_foldclosed __ARGS((typeval *argvars, typeval *rettv));
438static void f_foldclosedend __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000439static void f_foldlevel __ARGS((typeval *argvars, typeval *rettv));
440static void f_foldtext __ARGS((typeval *argvars, typeval *rettv));
441static void f_foldtextresult __ARGS((typeval *argvars, typeval *rettv));
442static void f_foreground __ARGS((typeval *argvars, typeval *rettv));
443static void f_function __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000444static void f_get __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000445static void f_getbufvar __ARGS((typeval *argvars, typeval *rettv));
446static void f_getchar __ARGS((typeval *argvars, typeval *rettv));
447static void f_getcharmod __ARGS((typeval *argvars, typeval *rettv));
448static void f_getcmdline __ARGS((typeval *argvars, typeval *rettv));
449static void f_getcmdpos __ARGS((typeval *argvars, typeval *rettv));
450static void f_getcwd __ARGS((typeval *argvars, typeval *rettv));
451static void f_getfontname __ARGS((typeval *argvars, typeval *rettv));
452static void f_getfperm __ARGS((typeval *argvars, typeval *rettv));
453static void f_getfsize __ARGS((typeval *argvars, typeval *rettv));
454static void f_getftime __ARGS((typeval *argvars, typeval *rettv));
455static void f_getftype __ARGS((typeval *argvars, typeval *rettv));
456static void f_getline __ARGS((typeval *argvars, typeval *rettv));
457static void f_getreg __ARGS((typeval *argvars, typeval *rettv));
458static void f_getregtype __ARGS((typeval *argvars, typeval *rettv));
459static void f_getwinposx __ARGS((typeval *argvars, typeval *rettv));
460static void f_getwinposy __ARGS((typeval *argvars, typeval *rettv));
461static void f_getwinvar __ARGS((typeval *argvars, typeval *rettv));
462static void f_glob __ARGS((typeval *argvars, typeval *rettv));
463static void f_globpath __ARGS((typeval *argvars, typeval *rettv));
464static void f_has __ARGS((typeval *argvars, typeval *rettv));
465static void f_hasmapto __ARGS((typeval *argvars, typeval *rettv));
466static void f_histadd __ARGS((typeval *argvars, typeval *rettv));
467static void f_histdel __ARGS((typeval *argvars, typeval *rettv));
468static void f_histget __ARGS((typeval *argvars, typeval *rettv));
469static void f_histnr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000470static void f_hlID __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000471static void f_hlexists __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000472static void f_hostname __ARGS((typeval *argvars, typeval *rettv));
473static void f_iconv __ARGS((typeval *argvars, typeval *rettv));
474static void f_indent __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000475static void f_index __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000476static void f_input __ARGS((typeval *argvars, typeval *rettv));
477static void f_inputdialog __ARGS((typeval *argvars, typeval *rettv));
478static void f_inputrestore __ARGS((typeval *argvars, typeval *rettv));
479static void f_inputsave __ARGS((typeval *argvars, typeval *rettv));
480static void f_inputsecret __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000481static void f_insert __ARGS((typeval *argvars, typeval *rettv));
482static void f_isdirectory __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000483static void f_items __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000484static void f_join __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000485static void f_keys __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000486static void f_last_buffer_nr __ARGS((typeval *argvars, typeval *rettv));
487static void f_len __ARGS((typeval *argvars, typeval *rettv));
488static void f_libcall __ARGS((typeval *argvars, typeval *rettv));
489static void f_libcallnr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000490static void f_line __ARGS((typeval *argvars, typeval *rettv));
491static void f_line2byte __ARGS((typeval *argvars, typeval *rettv));
492static void f_lispindent __ARGS((typeval *argvars, typeval *rettv));
493static void f_localtime __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000494static void f_map __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000495static void f_maparg __ARGS((typeval *argvars, typeval *rettv));
496static void f_mapcheck __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000497static void f_match __ARGS((typeval *argvars, typeval *rettv));
498static void f_matchend __ARGS((typeval *argvars, typeval *rettv));
499static void f_matchstr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000500static void f_max __ARGS((typeval *argvars, typeval *rettv));
501static void f_min __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000502static void f_mode __ARGS((typeval *argvars, typeval *rettv));
503static void f_nextnonblank __ARGS((typeval *argvars, typeval *rettv));
504static void f_nr2char __ARGS((typeval *argvars, typeval *rettv));
505static void f_prevnonblank __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000506static void f_range __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000507static void f_remote_expr __ARGS((typeval *argvars, typeval *rettv));
508static void f_remote_foreground __ARGS((typeval *argvars, typeval *rettv));
509static void f_remote_peek __ARGS((typeval *argvars, typeval *rettv));
510static void f_remote_read __ARGS((typeval *argvars, typeval *rettv));
511static void f_remote_send __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000512static void f_remove __ARGS((typeval *argvars, typeval *rettv));
513static void f_rename __ARGS((typeval *argvars, typeval *rettv));
514static void f_repeat __ARGS((typeval *argvars, typeval *rettv));
515static void f_resolve __ARGS((typeval *argvars, typeval *rettv));
516static void f_reverse __ARGS((typeval *argvars, typeval *rettv));
517static void f_search __ARGS((typeval *argvars, typeval *rettv));
518static void f_searchpair __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000519static void f_server2client __ARGS((typeval *argvars, typeval *rettv));
520static void f_serverlist __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000521static void f_setbufvar __ARGS((typeval *argvars, typeval *rettv));
522static void f_setcmdpos __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000523static void f_setline __ARGS((typeval *argvars, typeval *rettv));
524static void f_setreg __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000525static void f_setwinvar __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000526static void f_simplify __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000527static void f_sort __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000528static void f_split __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000529#ifdef HAVE_STRFTIME
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000530static void f_strftime __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000531#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000532static void f_stridx __ARGS((typeval *argvars, typeval *rettv));
533static void f_string __ARGS((typeval *argvars, typeval *rettv));
534static void f_strlen __ARGS((typeval *argvars, typeval *rettv));
535static void f_strpart __ARGS((typeval *argvars, typeval *rettv));
536static void f_strridx __ARGS((typeval *argvars, typeval *rettv));
537static void f_strtrans __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000538static void f_submatch __ARGS((typeval *argvars, typeval *rettv));
539static void f_substitute __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000540static void f_synID __ARGS((typeval *argvars, typeval *rettv));
541static void f_synIDattr __ARGS((typeval *argvars, typeval *rettv));
542static void f_synIDtrans __ARGS((typeval *argvars, typeval *rettv));
543static void f_system __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000544static void f_tempname __ARGS((typeval *argvars, typeval *rettv));
545static void f_tolower __ARGS((typeval *argvars, typeval *rettv));
546static void f_toupper __ARGS((typeval *argvars, typeval *rettv));
547static void f_tr __ARGS((typeval *argvars, typeval *rettv));
548static void f_type __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000549static void f_values __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000550static void f_virtcol __ARGS((typeval *argvars, typeval *rettv));
551static void f_visualmode __ARGS((typeval *argvars, typeval *rettv));
552static void f_winbufnr __ARGS((typeval *argvars, typeval *rettv));
553static void f_wincol __ARGS((typeval *argvars, typeval *rettv));
554static void f_winheight __ARGS((typeval *argvars, typeval *rettv));
555static void f_winline __ARGS((typeval *argvars, typeval *rettv));
556static void f_winnr __ARGS((typeval *argvars, typeval *rettv));
557static void f_winrestcmd __ARGS((typeval *argvars, typeval *rettv));
558static void f_winwidth __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000559
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000560static win_T *find_win_by_nr __ARGS((typeval *vp));
561static pos_T *var2fpos __ARGS((typeval *varp, int lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562static int get_env_len __ARGS((char_u **arg));
563static int get_id_len __ARGS((char_u **arg));
564static int get_func_len __ARGS((char_u **arg, char_u **alias, int evaluate));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000565static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int incl_br));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566static int eval_isnamec __ARGS((int c));
567static int find_vim_var __ARGS((char_u *name, int len));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000568static int get_var_tv __ARGS((char_u *name, int len, typeval *rettv));
569static typeval *alloc_tv __ARGS((void));
570static typeval *alloc_string_tv __ARGS((char_u *string));
571static void free_tv __ARGS((typeval *varp));
572static void clear_tv __ARGS((typeval *varp));
573static void init_tv __ARGS((typeval *varp));
574static long get_tv_number __ARGS((typeval *varp));
575static linenr_T get_tv_lnum __ARGS((typeval *argvars));
576static char_u *get_tv_string __ARGS((typeval *varp));
577static char_u *get_tv_string_buf __ARGS((typeval *varp, char_u *buf));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000578static int get_amp_tv __ARGS((typeval *rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579static VAR find_var __ARGS((char_u *name, int writing));
580static VAR find_var_in_ga __ARGS((garray_T *gap, char_u *varname));
581static garray_T *find_var_ga __ARGS((char_u *name, char_u **varname));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000582static void clear_var __ARGS((VAR v));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583static void list_one_var __ARGS((VAR v, char_u *prefix));
584static void list_vim_var __ARGS((int i));
585static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000586static void set_var __ARGS((char_u *name, typeval *varp, int copy));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000587static void copy_tv __ARGS((typeval *from, typeval *to));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
589static char_u *trans_function_name __ARGS((char_u **pp, int skip, int internal));
590static int eval_fname_script __ARGS((char_u *p));
591static int eval_fname_sid __ARGS((char_u *p));
592static void list_func_head __ARGS((ufunc_T *fp, int indent));
593static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
594static ufunc_T *find_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000595static int function_exists __ARGS((char_u *name));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000596static void call_user_func __ARGS((ufunc_T *fp, int argcount, typeval *argvars, typeval *rettv, linenr_T firstline, linenr_T lastline));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000597
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000598#define get_var_string(p) get_tv_string(&(p)->tv)
599#define get_var_string_buf(p, b) get_tv_string_buf(&(p)->tv, (b))
600#define get_var_number(p) get_tv_number(&((p)->tv))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000603
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000604static int ex_let_vars __ARGS((char_u *arg, typeval *tv, int copy, int semicolon, int var_count, char_u *nextchars));
605static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
606static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000607static void list_all_vars __ARGS((void));
608static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
609static char_u *ex_let_one __ARGS((char_u *arg, typeval *tv, int copy, char_u *endchars));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000610static int check_changedtick __ARGS((char_u *arg));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000611static char_u *set_var_idx __ARGS((char_u *name, char_u *ip, typeval *rettv, int copy, char_u *endchars));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000612static void list_add_watch __ARGS((listvar *l, listwatch *lw));
Bram Moolenaar8c711452005-01-14 21:53:12 +0000613static void list_rem_watch __ARGS((listvar *l, listwatch *lwrem));
614static void list_fix_watch __ARGS((listvar *l, listitem *item));
615static int do_unlet_var __ARGS((char_u *name, int forceit));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
617/*
618 * Set an internal variable to a string value. Creates the variable if it does
619 * not already exist.
620 */
621 void
622set_internal_string_var(name, value)
623 char_u *name;
624 char_u *value;
625{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000626 char_u *val;
627 typeval *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628
629 val = vim_strsave(value);
630 if (val != NULL)
631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000632 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000633 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000635 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000636 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 }
638 }
639}
640
641# if defined(FEAT_MBYTE) || defined(PROTO)
642 int
643eval_charconvert(enc_from, enc_to, fname_from, fname_to)
644 char_u *enc_from;
645 char_u *enc_to;
646 char_u *fname_from;
647 char_u *fname_to;
648{
649 int err = FALSE;
650
651 set_vim_var_string(VV_CC_FROM, enc_from, -1);
652 set_vim_var_string(VV_CC_TO, enc_to, -1);
653 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
654 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
655 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
656 err = TRUE;
657 set_vim_var_string(VV_CC_FROM, NULL, -1);
658 set_vim_var_string(VV_CC_TO, NULL, -1);
659 set_vim_var_string(VV_FNAME_IN, NULL, -1);
660 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
661
662 if (err)
663 return FAIL;
664 return OK;
665}
666# endif
667
668# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
669 int
670eval_printexpr(fname, args)
671 char_u *fname;
672 char_u *args;
673{
674 int err = FALSE;
675
676 set_vim_var_string(VV_FNAME_IN, fname, -1);
677 set_vim_var_string(VV_CMDARG, args, -1);
678 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
679 err = TRUE;
680 set_vim_var_string(VV_FNAME_IN, NULL, -1);
681 set_vim_var_string(VV_CMDARG, NULL, -1);
682
683 if (err)
684 {
685 mch_remove(fname);
686 return FAIL;
687 }
688 return OK;
689}
690# endif
691
692# if defined(FEAT_DIFF) || defined(PROTO)
693 void
694eval_diff(origfile, newfile, outfile)
695 char_u *origfile;
696 char_u *newfile;
697 char_u *outfile;
698{
699 int err = FALSE;
700
701 set_vim_var_string(VV_FNAME_IN, origfile, -1);
702 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
703 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
704 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
705 set_vim_var_string(VV_FNAME_IN, NULL, -1);
706 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
707 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
708}
709
710 void
711eval_patch(origfile, difffile, outfile)
712 char_u *origfile;
713 char_u *difffile;
714 char_u *outfile;
715{
716 int err;
717
718 set_vim_var_string(VV_FNAME_IN, origfile, -1);
719 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
720 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
721 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
722 set_vim_var_string(VV_FNAME_IN, NULL, -1);
723 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
724 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
725}
726# endif
727
728/*
729 * Top level evaluation function, returning a boolean.
730 * Sets "error" to TRUE if there was an error.
731 * Return TRUE or FALSE.
732 */
733 int
734eval_to_bool(arg, error, nextcmd, skip)
735 char_u *arg;
736 int *error;
737 char_u **nextcmd;
738 int skip; /* only parse, don't execute */
739{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000740 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 int retval = FALSE;
742
743 if (skip)
744 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000745 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 else
748 {
749 *error = FALSE;
750 if (!skip)
751 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000752 retval = (get_tv_number(&tv) != 0);
753 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 }
755 }
756 if (skip)
757 --emsg_skip;
758
759 return retval;
760}
761
762/*
763 * Top level evaluation function, returning a string. If "skip" is TRUE,
764 * only parsing to "nextcmd" is done, without reporting errors. Return
765 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
766 */
767 char_u *
768eval_to_string_skip(arg, nextcmd, skip)
769 char_u *arg;
770 char_u **nextcmd;
771 int skip; /* only parse, don't execute */
772{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000773 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 char_u *retval;
775
776 if (skip)
777 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000778 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 retval = NULL;
780 else
781 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000782 retval = vim_strsave(get_tv_string(&tv));
783 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 }
785 if (skip)
786 --emsg_skip;
787
788 return retval;
789}
790
791/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000792 * Skip over an expression at "*pp".
793 * Return FAIL for an error, OK otherwise.
794 */
795 int
796skip_expr(pp)
797 char_u **pp;
798{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000799 typeval rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000800
801 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000802 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000803}
804
805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 * Top level evaluation function, returning a string.
807 * Return pointer to allocated memory, or NULL for failure.
808 */
809 char_u *
810eval_to_string(arg, nextcmd)
811 char_u *arg;
812 char_u **nextcmd;
813{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000814 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 char_u *retval;
816
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000817 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 retval = NULL;
819 else
820 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000821 retval = vim_strsave(get_tv_string(&tv));
822 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 }
824
825 return retval;
826}
827
828/*
829 * Call eval_to_string() with "sandbox" set and not using local variables.
830 */
831 char_u *
832eval_to_string_safe(arg, nextcmd)
833 char_u *arg;
834 char_u **nextcmd;
835{
836 char_u *retval;
837 void *save_funccalp;
838
839 save_funccalp = save_funccal();
840 ++sandbox;
841 retval = eval_to_string(arg, nextcmd);
842 --sandbox;
843 restore_funccal(save_funccalp);
844 return retval;
845}
846
847#if 0 /* not used */
848/*
849 * Top level evaluation function, returning a string.
850 * Advances "arg" to the first non-blank after the evaluated expression.
851 * Return pointer to allocated memory, or NULL for failure.
852 * Doesn't give error messages.
853 */
854 char_u *
855eval_arg_to_string(arg)
856 char_u **arg;
857{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000858 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 char_u *retval;
860 int ret;
861
862 ++emsg_off;
863
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000864 ret = eval1(arg, &rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 if (ret == FAIL)
866 retval = NULL;
867 else
868 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000869 retval = vim_strsave(get_tv_string(&rettv));
870 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 }
872
873 --emsg_off;
874
875 return retval;
876}
877#endif
878
879/*
880 * Top level evaluation function, returning a number.
881 * Evaluates "expr" silently.
882 * Returns -1 for an error.
883 */
884 int
885eval_to_number(expr)
886 char_u *expr;
887{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000888 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000890 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891
892 ++emsg_off;
893
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000894 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 retval = -1;
896 else
897 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000898 retval = get_tv_number(&rettv);
899 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 }
901 --emsg_off;
902
903 return retval;
904}
905
906#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
907/*
908 * Call some vimL function and return the result as a string
909 * Uses argv[argc] for the function arguments.
910 */
911 char_u *
912call_vim_function(func, argc, argv, safe)
913 char_u *func;
914 int argc;
915 char_u **argv;
916 int safe; /* use the sandbox */
917{
918 char_u *retval = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000919 typeval rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000920 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 long n;
922 int len;
923 int i;
924 int doesrange;
925 void *save_funccalp = NULL;
926
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000927 argvars = (typeval *)alloc((unsigned)(argc * sizeof(typeval)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 if (argvars == NULL)
929 return NULL;
930
931 for (i = 0; i < argc; i++)
932 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000933 /* Pass a NULL or empty argument as an empty string */
934 if (argv[i] == NULL || *argv[i] == NUL)
935 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000936 argvars[i].v_type = VAR_STRING;
937 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000938 continue;
939 }
940
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 /* Recognize a number argument, the others must be strings. */
942 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
943 if (len != 0 && len == (int)STRLEN(argv[i]))
944 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000945 argvars[i].v_type = VAR_NUMBER;
946 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 }
948 else
949 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000950 argvars[i].v_type = VAR_STRING;
951 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 }
953 }
954
955 if (safe)
956 {
957 save_funccalp = save_funccal();
958 ++sandbox;
959 }
960
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000961 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
962 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
964 &doesrange, TRUE) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000965 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000967 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 vim_free(argvars);
969
970 if (safe)
971 {
972 --sandbox;
973 restore_funccal(save_funccalp);
974 }
975 return retval;
976}
977#endif
978
979/*
980 * Save the current function call pointer, and set it to NULL.
981 * Used when executing autocommands and for ":source".
982 */
983 void *
984save_funccal()
985{
986 struct funccall *fc;
987
988 fc = current_funccal;
989 current_funccal = NULL;
990 return (void *)fc;
991}
992
993 void
994restore_funccal(fc)
995 void *fc;
996{
997 current_funccal = (struct funccall *)fc;
998}
999
1000#ifdef FEAT_FOLDING
1001/*
1002 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1003 * it in "*cp". Doesn't give error messages.
1004 */
1005 int
1006eval_foldexpr(arg, cp)
1007 char_u *arg;
1008 int *cp;
1009{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001010 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 int retval;
1012 char_u *s;
1013
1014 ++emsg_off;
1015 ++sandbox;
1016 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001017 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 retval = 0;
1019 else
1020 {
1021 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001022 if (tv.v_type == VAR_NUMBER)
1023 retval = tv.vval.v_number;
1024 else if (tv.v_type == VAR_UNKNOWN
1025 || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 retval = 0;
1027 else
1028 {
1029 /* If the result is a string, check if there is a non-digit before
1030 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001031 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 if (!VIM_ISDIGIT(*s) && *s != '-')
1033 *cp = *s++;
1034 retval = atol((char *)s);
1035 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001036 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 }
1038 --emsg_off;
1039 --sandbox;
1040
1041 return retval;
1042}
1043#endif
1044
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045/*
1046 * Expands out the 'magic' {}'s in a variable/function name.
1047 * Note that this can call itself recursively, to deal with
1048 * constructs like foo{bar}{baz}{bam}
1049 * The four pointer arguments point to "foo{expre}ss{ion}bar"
1050 * "in_start" ^
1051 * "expr_start" ^
1052 * "expr_end" ^
1053 * "in_end" ^
1054 *
1055 * Returns a new allocated string, which the caller must free.
1056 * Returns NULL for failure.
1057 */
1058 static char_u *
1059make_expanded_name(in_start, expr_start, expr_end, in_end)
1060 char_u *in_start;
1061 char_u *expr_start;
1062 char_u *expr_end;
1063 char_u *in_end;
1064{
1065 char_u c1;
1066 char_u *retval = NULL;
1067 char_u *temp_result;
1068 char_u *nextcmd = NULL;
1069
1070 if (expr_end == NULL || in_end == NULL)
1071 return NULL;
1072 *expr_start = NUL;
1073 *expr_end = NUL;
1074 c1 = *in_end;
1075 *in_end = NUL;
1076
1077 temp_result = eval_to_string(expr_start + 1, &nextcmd);
1078 if (temp_result != NULL && nextcmd == NULL)
1079 {
1080 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
1081 + (in_end - expr_end) + 1));
1082
1083 if (retval != NULL)
1084 {
1085 STRCPY(retval, in_start);
1086 STRCAT(retval, temp_result);
1087 STRCAT(retval, expr_end + 1);
1088 }
1089 }
1090 vim_free(temp_result);
1091
1092 *in_end = c1; /* put char back for error messages */
1093 *expr_start = '{';
1094 *expr_end = '}';
1095
1096 if (retval != NULL)
1097 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001098 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 if (expr_start != NULL)
1100 {
1101 /* Further expansion! */
1102 temp_result = make_expanded_name(retval, expr_start,
1103 expr_end, temp_result);
1104 vim_free(retval);
1105 retval = temp_result;
1106 }
1107 }
1108
1109 return retval;
1110
1111}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112
1113/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001114 * ":let" list all variable values
1115 * ":let var1 var2" list variable values
1116 * ":let var = expr" assignment command.
1117 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 */
1119 void
1120ex_let(eap)
1121 exarg_T *eap;
1122{
1123 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001124 char_u *expr = NULL;
1125 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001127 int var_count = 0;
1128 int semicolon = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001130 expr = skip_var_list(arg, &var_count, &semicolon);
1131 if (expr == NULL)
1132 return;
1133 expr = vim_strchr(expr, '=');
1134 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001136 if (*arg == '[')
1137 EMSG(_(e_invarg));
1138 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001139 /* ":let var1 var2" */
1140 arg = list_arg_vars(eap, arg);
1141 else if (!eap->skip)
1142 /* ":let" */
1143 list_all_vars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 eap->nextcmd = check_nextcmd(arg);
1145 }
1146 else
1147 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001148 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001149
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 if (eap->skip)
1151 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001152 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 if (eap->skip)
1154 {
1155 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001156 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 --emsg_skip;
1158 }
1159 else if (i != FAIL)
1160 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001161 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1162 (char_u *)"=");
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001163 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 }
1165 }
1166}
1167
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001168/*
1169 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1170 * Handles both "var" with any type and "[var, var; var]" with a list type.
1171 * Returns OK or FAIL;
1172 */
1173 static int
1174ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1175 char_u *arg_start;
1176 typeval *tv;
1177 int copy; /* copy values from "tv", don't move */
1178 int semicolon; /* from skip_var_list() */
1179 int var_count; /* from skip_var_list() */
1180 char_u *nextchars; /* characters that must follow or NULL */
1181{
1182 char_u *arg = arg_start;
1183 listvar *l;
1184 int i;
1185 listitem *item;
1186 typeval ltv;
1187
1188 if (*arg != '[')
1189 {
1190 /*
1191 * ":let var = expr" or ":for var in list"
1192 */
1193 if (ex_let_one(arg, tv, copy, nextchars) == NULL)
1194 return FAIL;
1195 return OK;
1196 }
1197
1198 /*
1199 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1200 */
1201 l = tv->vval.v_list;
1202 if (tv->v_type != VAR_LIST || l == NULL)
1203 {
1204 EMSG(_(e_listreq));
1205 return FAIL;
1206 }
1207
1208 i = list_len(l);
1209 if (semicolon == 0 && var_count < i)
1210 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001211 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001212 return FAIL;
1213 }
1214 if (var_count - semicolon > i)
1215 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001216 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001217 return FAIL;
1218 }
1219
1220 item = l->lv_first;
1221 while (*arg != ']')
1222 {
1223 arg = skipwhite(arg + 1);
1224 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]");
1225 item = item->li_next;
1226 if (arg == NULL)
1227 return FAIL;
1228
1229 arg = skipwhite(arg);
1230 if (*arg == ';')
1231 {
1232 /* Put the rest of the list (may be empty) in the var after ';'.
1233 * Create a new list for this. */
1234 l = list_alloc();
1235 if (l == NULL)
1236 return FAIL;
1237 while (item != NULL)
1238 {
1239 list_append_tv(l, &item->li_tv);
1240 item = item->li_next;
1241 }
1242
1243 ltv.v_type = VAR_LIST;
1244 ltv.vval.v_list = l;
1245 l->lv_refcount = 1;
1246
1247 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, (char_u *)"]");
1248 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
1317 static char_u *
1318skip_var_one(arg)
1319 char_u *arg;
1320{
1321 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1322 ++arg;
1323 return find_name_end(arg, NULL, NULL, TRUE);
1324}
1325
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 static void
1327list_all_vars()
1328{
1329 int i;
1330
1331 /*
1332 * List all variables.
1333 */
1334 for (i = 0; i < variables.ga_len && !got_int; ++i)
1335 if (VAR_ENTRY(i).v_name != NULL)
1336 list_one_var(&VAR_ENTRY(i), (char_u *)"");
1337 for (i = 0; i < curbuf->b_vars.ga_len && !got_int; ++i)
1338 if (BVAR_ENTRY(i).v_name != NULL)
1339 list_one_var(&BVAR_ENTRY(i), (char_u *)"b:");
1340 for (i = 0; i < curwin->w_vars.ga_len && !got_int; ++i)
1341 if (WVAR_ENTRY(i).v_name != NULL)
1342 list_one_var(&WVAR_ENTRY(i), (char_u *)"w:");
1343 for (i = 0; i < VV_LEN && !got_int; ++i)
1344 if (vimvars[i].type == VAR_NUMBER || vimvars[i].val != NULL)
1345 list_vim_var(i);
1346}
1347
1348/*
1349 * List variables in "arg".
1350 */
1351 static char_u *
1352list_arg_vars(eap, arg)
1353 exarg_T *eap;
1354 char_u *arg;
1355{
1356 int error = FALSE;
1357 char_u *temp_string = NULL;
1358 int arg_len;
1359 char_u *expr_start;
1360 char_u *expr_end;
1361 char_u *name_end;
1362 int c1 = 0, c2;
1363 int i;
1364 VAR varp;
1365 char_u *name;
1366
1367 while (!ends_excmd(*arg) && !got_int)
1368 {
1369 /* Find the end of the name. */
1370 name_end = find_name_end(arg, &expr_start, &expr_end, FALSE);
1371
1372 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
1373 {
1374 emsg_severe = TRUE;
1375 EMSG(_(e_trailing));
1376 break;
1377 }
1378 if (!error && !eap->skip)
1379 {
1380 if (expr_start != NULL)
1381 {
1382 temp_string = make_expanded_name(arg, expr_start,
1383 expr_end, name_end);
1384 if (temp_string == NULL)
1385 {
1386 /*
1387 * Report an invalid expression in braces, unless
1388 * the expression evaluation has been cancelled due
1389 * to an aborting error, an interrupt, or an
1390 * exception.
1391 */
1392 if (!aborting())
1393 {
1394 emsg_severe = TRUE;
1395 EMSG2(_(e_invarg2), arg);
1396 break;
1397 }
1398 error = TRUE;
1399 arg = skipwhite(name_end);
1400 continue;
1401 }
1402 arg = temp_string;
1403 arg_len = STRLEN(temp_string);
1404 }
1405 else
1406 {
1407 c1 = *name_end;
1408 *name_end = NUL;
1409 arg_len = (int)(name_end - arg);
1410 }
1411 i = find_vim_var(arg, arg_len);
1412 if (i >= 0)
1413 list_vim_var(i);
1414 else if (STRCMP("b:changedtick", arg) == 0)
1415 {
1416 char_u numbuf[NUMBUFLEN];
1417
1418 sprintf((char *)numbuf, "%ld",
1419 (long)curbuf->b_changedtick);
1420 list_one_var_a((char_u *)"b:", (char_u *)"changedtick",
1421 VAR_NUMBER, numbuf);
1422 }
1423 else
1424 {
1425 varp = find_var(arg, FALSE);
1426 if (varp == NULL)
1427 {
1428 /* Skip further arguments but do continue to
1429 * search for a trailing command. */
1430 EMSG2(_("E106: Unknown variable: \"%s\""), arg);
1431 error = TRUE;
1432 }
1433 else
1434 {
1435 name = vim_strchr(arg, ':');
1436 if (name != NULL)
1437 {
1438 /* "a:" vars have no name stored, use whole arg */
1439 if (arg[0] == 'a' && arg[1] == ':')
1440 c2 = NUL;
1441 else
1442 {
1443 c2 = *++name;
1444 *name = NUL;
1445 }
1446 list_one_var(varp, arg);
1447 if (c2 != NUL)
1448 *name = c2;
1449 }
1450 else
1451 list_one_var(varp, (char_u *)"");
1452 }
1453 }
1454 if (expr_start != NULL)
1455 vim_free(temp_string);
1456 else
1457 *name_end = c1;
1458 }
1459 arg = skipwhite(name_end);
1460 }
1461
1462 return arg;
1463}
1464
1465/*
1466 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1467 * Returns a pointer to the char just after the var name.
1468 * Returns NULL if there is an error.
1469 */
1470 static char_u *
1471ex_let_one(arg, tv, copy, endchars)
1472 char_u *arg; /* points to variable name */
1473 typeval *tv; /* value to assign to variable */
1474 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001475 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001476{
1477 int c1;
1478 char_u *name;
1479 char_u *p;
1480 char_u *arg_end = NULL;
1481 int len;
1482 int opt_flags;
1483
1484 /*
1485 * ":let $VAR = expr": Set environment variable.
1486 */
1487 if (*arg == '$')
1488 {
1489 /* Find the end of the name. */
1490 ++arg;
1491 name = arg;
1492 len = get_env_len(&arg);
1493 if (len == 0)
1494 EMSG2(_(e_invarg2), name - 1);
1495 else
1496 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001497 if (endchars != NULL
1498 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001499 EMSG(_(e_letunexp));
1500 else
1501 {
1502 c1 = name[len];
1503 name[len] = NUL;
1504 p = get_tv_string(tv);
1505 vim_setenv(name, p);
1506 if (STRICMP(name, "HOME") == 0)
1507 init_homedir();
1508 else if (didset_vim && STRICMP(name, "VIM") == 0)
1509 didset_vim = FALSE;
1510 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1511 didset_vimruntime = FALSE;
1512 name[len] = c1;
1513 arg_end = arg;
1514 }
1515 }
1516 }
1517
1518 /*
1519 * ":let &option = expr": Set option value.
1520 * ":let &l:option = expr": Set local option value.
1521 * ":let &g:option = expr": Set global option value.
1522 */
1523 else if (*arg == '&')
1524 {
1525 /* Find the end of the name. */
1526 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001527 if (p == NULL || (endchars != NULL
1528 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001529 EMSG(_(e_letunexp));
1530 else
1531 {
1532 c1 = *p;
1533 *p = NUL;
1534 set_option_value(arg, get_tv_number(tv),
1535 get_tv_string(tv), opt_flags);
1536 *p = c1;
1537 arg_end = p;
1538 }
1539 }
1540
1541 /*
1542 * ":let @r = expr": Set register contents.
1543 */
1544 else if (*arg == '@')
1545 {
1546 ++arg;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001547 if (endchars != NULL
1548 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001549 EMSG(_(e_letunexp));
1550 else
1551 {
1552 write_reg_contents(*arg == '@' ? '"' : *arg,
1553 get_tv_string(tv), -1, FALSE);
1554 arg_end = arg + 1;
1555 }
1556 }
1557
1558 /*
1559 * ":let var = expr": Set internal variable.
1560 */
1561 else if (eval_isnamec(*arg) && !VIM_ISDIGIT(*arg))
1562 {
1563 char_u *exp_name = NULL;
1564 char_u *expr_start, *expr_end;
1565
1566 /* Find the end of the name. */
1567 p = find_name_end(arg, &expr_start, &expr_end, FALSE);
1568 if (expr_start != NULL)
1569 {
1570 exp_name = make_expanded_name(arg, expr_start, expr_end, p);
1571 arg = exp_name;
1572 }
1573
1574 if (arg == NULL)
1575 {
1576 /* Report an invalid expression in braces, unless the
1577 * expression evaluation has been cancelled due to an
1578 * aborting error, an interrupt, or an exception. */
1579 if (!aborting())
1580 EMSG2(_(e_invarg2), arg);
1581 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001582 else if (*p == '[' || *p == '.')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001583 arg_end = set_var_idx(arg, p, tv, copy, endchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001584 else if (endchars != NULL
1585 && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001586 EMSG(_(e_letunexp));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001587 else if (!check_changedtick(arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001588 {
1589 c1 = *p;
1590 *p = NUL;
1591 set_var(arg, tv, copy);
1592 *p = c1;
1593 arg_end = p;
1594 }
1595
1596 vim_free(exp_name);
1597 }
1598
1599 else
1600 EMSG2(_(e_invarg2), arg);
1601
1602 return arg_end;
1603}
1604
1605/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00001606 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
1607 */
1608 static int
1609check_changedtick(arg)
1610 char_u *arg;
1611{
1612 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
1613 {
1614 EMSG2(_(e_readonlyvar), arg);
1615 return TRUE;
1616 }
1617 return FALSE;
1618}
1619
1620/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001621 * Set a variable with an index: "name[expr]", "name[expr:expr]",
Bram Moolenaar8c711452005-01-14 21:53:12 +00001622 * "name[expr][expr]", "name.key", "name.key[expr]" etc.
1623 * Only works if "name" is an existing List or Dictionary.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001624 * "ip" points to the first '['.
1625 * Returns a pointer to just after the last used ']'; NULL for error.
1626 */
1627 static char_u *
1628set_var_idx(name, ip, rettv, copy, endchars)
1629 char_u *name;
1630 char_u *ip;
1631 typeval *rettv;
1632 int copy;
1633 char_u *endchars;
1634{
1635 VAR v;
1636 int c1;
1637 char_u *p;
1638 typeval var1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001639 typeval var2;
1640 int range = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001641 typeval *tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001642 long n1 = 0, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001643 int empty1 = FALSE, empty2 = FALSE;
1644 listitem *li = NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001645 listitem *ni;
1646 listitem *ri;
1647 listvar *l = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001648 dictitem *di;
1649 char_u *key = NULL;
1650 char_u *newkey = NULL;
1651 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001652
1653 c1 = *ip;
1654 *ip = NUL;
1655 v = find_var(name, TRUE);
1656 if (v == NULL)
1657 EMSG2(_(e_undefvar), name);
1658 *ip = c1;
1659 if (v == NULL)
1660 return NULL;
1661
1662 tv = &v->tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001663 for (p = ip; *p == '[' || (*p == '.' && tv->v_type == VAR_DICT); )
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001664 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001665 if (!(tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
1666 && !(tv->v_type == VAR_DICT && tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001667 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001668 EMSG(_("E689: Can only index a List or Dictionary"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001669 p = NULL;
1670 break;
1671 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001672 if (range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001673 {
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001674 EMSG(_("E708: [:] must come last"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001675 p = NULL;
1676 break;
1677 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001678
Bram Moolenaar8c711452005-01-14 21:53:12 +00001679 len = -1;
1680 if (*p == '.')
1681 {
1682 key = p + 1;
1683 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1684 ;
1685 if (len == 0)
1686 {
1687 EMSG(_(e_emptykey));
1688 p = NULL;
1689 break;
1690 }
1691 p = key + len;
1692 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001693 else
1694 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001695 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001696 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001697 if (*p == ':')
1698 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001699 else
1700 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001701 empty1 = FALSE;
1702 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001703 {
1704 p = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001705 break;
1706 }
1707 }
1708
1709 /* Optionally get the second index [ :expr]. */
1710 if (*p == ':')
1711 {
1712 if (tv->v_type == VAR_DICT)
1713 {
1714 EMSG(_("E999: Cannot use [:] with a Dictionary"));
1715 p = NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001716 if (!empty1)
1717 clear_tv(&var1);
1718 break;
1719 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001720 if (rettv->v_type != VAR_LIST || rettv->vval.v_list == NULL)
1721 {
1722 EMSG(_("E709: [:] requires a List value"));
1723 p = NULL;
1724 if (!empty1)
1725 clear_tv(&var1);
1726 break;
1727 }
1728 p = skipwhite(p + 1);
1729 if (*p == ']')
1730 empty2 = TRUE;
1731 else
1732 {
1733 empty2 = FALSE;
1734 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1735 {
1736 p = NULL;
1737 if (!empty1)
1738 clear_tv(&var1);
1739 break;
1740 }
1741 }
1742 range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001743 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001744 else
1745 range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001746
Bram Moolenaar8c711452005-01-14 21:53:12 +00001747 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001748 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001749 EMSG(_(e_missbrac));
1750 if (!empty1)
1751 clear_tv(&var1);
1752 if (range && !empty2)
1753 clear_tv(&var2);
1754 p = NULL;
1755 break;
1756 }
1757
1758 /* Skip to past ']'. */
1759 ++p;
1760 }
1761
1762 if (tv->v_type == VAR_DICT)
1763 {
1764 if (len == -1)
1765 {
1766 key = get_tv_string(&var1);
1767 if (*key == NUL)
1768 {
1769 EMSG(_(e_emptykey));
1770 clear_tv(&var1);
1771 p = NULL;
1772 break;
1773 }
1774 }
1775 di = dict_find(tv->vval.v_dict, key, len);
1776 if (di == NULL)
1777 {
1778 /* Key does not exist in dict: may need toadd it. */
1779 if (*p == '[' || *p == '.')
1780 {
1781 EMSG2(_("E999: Key does not exist in Dictionary: %s"), key);
1782 p = NULL;
1783 if (len == -1)
1784 clear_tv(&var1);
1785 break;
1786 }
1787 if (len == -1)
1788 newkey = vim_strsave(key);
1789 else
1790 newkey = vim_strnsave(key, len);
1791 if (len == -1)
1792 clear_tv(&var1);
1793 if (newkey == NULL)
1794 p = NULL;
1795 break;
1796 }
1797 if (len == -1)
1798 clear_tv(&var1);
1799 tv = &di->di_tv;
1800 }
1801 else
1802 {
1803 /*
1804 * Get the number and item for the only or first index of the List.
1805 */
1806 if (empty1)
1807 n1 = 0;
1808 else
1809 {
1810 n1 = get_tv_number(&var1);
1811 clear_tv(&var1);
1812 }
1813 l = tv->vval.v_list;
1814 li = list_find(l, n1);
1815 if (li == NULL)
1816 {
1817 EMSGN(_(e_listidx), n1);
1818 p = NULL;
1819 if (range && !empty2)
1820 clear_tv(&var2);
1821 break;
1822 }
1823
1824 /*
1825 * May need to find the item or absolute index for the second
1826 * index of a range.
1827 * When no index given: "empty2" is TRUE.
1828 * Otherwise "n2" is set to the second index.
1829 */
1830 if (range && !empty2)
1831 {
1832 n2 = get_tv_number(&var2);
1833 clear_tv(&var2);
1834 if (n2 < 0)
1835 {
1836 ni = list_find(l, n2);
1837 if (ni == NULL)
1838 {
1839 EMSGN(_(e_listidx), n2);
1840 p = NULL;
1841 break;
1842 }
1843 n2 = list_idx_of_item(l, ni);
1844 }
1845
1846 /* Check that n2 isn't before n1. */
1847 if (n1 < 0)
1848 n1 = list_idx_of_item(l, li);
1849 if (n2 < n1)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001850 {
1851 EMSGN(_(e_listidx), n2);
1852 p = NULL;
1853 break;
1854 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001855 }
1856
Bram Moolenaar8c711452005-01-14 21:53:12 +00001857 tv = &li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001858 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 }
1860
1861 if (p != NULL)
1862 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001863 p = skipwhite(p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 if (endchars != NULL && vim_strchr(endchars, *p) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 {
1866 EMSG(_(e_letunexp));
1867 p = NULL;
1868 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001869 else if (range)
1870 {
1871 /*
1872 * Assign the List values to the list items.
1873 */
1874 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
1875 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001876 clear_tv(&li->li_tv);
1877 copy_tv(&ri->li_tv, &li->li_tv);
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001878 ri = ri->li_next;
1879 if (ri == NULL || (!empty2 && n2 == n1))
1880 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001881 if (li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001882 {
1883 /* Need to add an empty item. */
1884 ni = listitem_alloc();
1885 if (ni == NULL)
1886 {
1887 ri = NULL;
1888 break;
1889 }
1890 ni->li_tv.v_type = VAR_NUMBER;
1891 ni->li_tv.vval.v_number = 0;
1892 list_append(l, ni);
1893 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001894 li = li->li_next;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001895 ++n1;
1896 }
1897 if (ri != NULL)
1898 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00001899 else if (empty2 ? li != NULL && li->li_next != NULL : n1 != n2)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001900 EMSG(_("E711: List value has not enough items"));
1901 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 else
1903 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001904 if (newkey != NULL)
1905 {
1906 /* Need to add the item to the dictionary. */
1907 di = dictitem_alloc();
1908 if (di == NULL)
1909 p = NULL;
1910 else
1911 {
1912 di->di_key = newkey;
1913 newkey = NULL;
1914 dict_add(tv->vval.v_dict, di);
1915 tv = &di->di_tv;
1916 }
1917 }
1918 else
1919 clear_tv(tv);
1920
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001921 /*
1922 * Assign the value to the variable or list item.
1923 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001924 if (p != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001926 if (copy)
1927 copy_tv(rettv, tv);
1928 else
1929 {
1930 *tv = *rettv;
1931 init_tv(rettv);
1932 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 }
1934 }
1935 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001936 vim_free(newkey);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001937 return p;
1938}
1939
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001940/*
1941 * Add a watcher to a list.
1942 */
1943 static void
1944list_add_watch(l, lw)
1945 listvar *l;
1946 listwatch *lw;
1947{
1948 lw->lw_next = l->lv_watch;
1949 l->lv_watch = lw;
1950}
1951
1952/*
1953 * Remove a watches from a list.
1954 * No warning when it isn't found...
1955 */
1956 static void
1957list_rem_watch(l, lwrem)
1958 listvar *l;
1959 listwatch *lwrem;
1960{
1961 listwatch *lw, **lwp;
1962
1963 lwp = &l->lv_watch;
1964 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
1965 {
1966 if (lw == lwrem)
1967 {
1968 *lwp = lw->lw_next;
1969 break;
1970 }
1971 lwp = &lw->lw_next;
1972 }
1973}
1974
1975/*
1976 * Just before removing an item from a list: advance watchers to the next
1977 * item.
1978 */
1979 static void
1980list_fix_watch(l, item)
1981 listvar *l;
1982 listitem *item;
1983{
1984 listwatch *lw;
1985
1986 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
1987 if (lw->lw_item == item)
1988 lw->lw_item = item->li_next;
1989}
1990
1991/*
1992 * Evaluate the expression used in a ":for var in expr" command.
1993 * "arg" points to "var".
1994 * Set "*errp" to TRUE for an error, FALSE otherwise;
1995 * Return a pointer that holds the info. Null when there is an error.
1996 */
1997 void *
1998eval_for_line(arg, errp, nextcmdp, skip)
1999 char_u *arg;
2000 int *errp;
2001 char_u **nextcmdp;
2002 int skip;
2003{
2004 forinfo *fi;
2005 char_u *expr;
2006 typeval tv;
2007 listvar *l;
2008
2009 *errp = TRUE; /* default: there is an error */
2010
2011 fi = (forinfo *)alloc_clear(sizeof(forinfo));
2012 if (fi == NULL)
2013 return NULL;
2014
2015 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2016 if (expr == NULL)
2017 return fi;
2018
2019 expr = skipwhite(expr);
2020 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
2021 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002022 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002023 return fi;
2024 }
2025
2026 if (skip)
2027 ++emsg_skip;
2028 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2029 {
2030 *errp = FALSE;
2031 if (!skip)
2032 {
2033 l = tv.vval.v_list;
2034 if (tv.v_type != VAR_LIST || l == NULL)
2035 EMSG(_(e_listreq));
2036 else
2037 {
2038 fi->fi_list = l;
2039 list_add_watch(l, &fi->fi_lw);
2040 fi->fi_lw.lw_item = l->lv_first;
2041 }
2042 }
2043 }
2044 if (skip)
2045 --emsg_skip;
2046
2047 return fi;
2048}
2049
2050/*
2051 * Use the first item in a ":for" list. Advance to the next.
2052 * Assign the values to the variable (list). "arg" points to the first one.
2053 * Return TRUE when a valid item was found, FALSE when at end of list or
2054 * something wrong.
2055 */
2056 int
2057next_for_item(fi_void, arg)
2058 void *fi_void;
2059 char_u *arg;
2060{
2061 forinfo *fi = (forinfo *)fi_void;
2062 int result;
2063 listitem *item;
2064
2065 item = fi->fi_lw.lw_item;
2066 if (item == NULL)
2067 result = FALSE;
2068 else
2069 {
2070 fi->fi_lw.lw_item = item->li_next;
2071 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2072 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2073 }
2074 return result;
2075}
2076
2077/*
2078 * Free the structure used to store info used by ":for".
2079 */
2080 void
2081free_for_info(fi_void)
2082 void *fi_void;
2083{
2084 forinfo *fi = (forinfo *)fi_void;
2085
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002086 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002087 list_rem_watch(fi->fi_list, &fi->fi_lw);
2088 vim_free(fi);
2089}
2090
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2092
2093 void
2094set_context_for_expression(xp, arg, cmdidx)
2095 expand_T *xp;
2096 char_u *arg;
2097 cmdidx_T cmdidx;
2098{
2099 int got_eq = FALSE;
2100 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002101 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002103 if (cmdidx == CMD_let)
2104 {
2105 xp->xp_context = EXPAND_USER_VARS;
2106 if (vim_strchr(arg, '=') == NULL)
2107 {
2108 /* ":let var1 var2 ...": find last space. */
2109 for (p = arg + STRLEN(arg); p > arg; )
2110 {
2111 xp->xp_pattern = p;
2112 p = mb_ptr_back(arg, p);
2113 if (vim_iswhite(*p))
2114 break;
2115 }
2116 return;
2117 }
2118 }
2119 else
2120 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2121 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 while ((xp->xp_pattern = vim_strpbrk(arg,
2123 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2124 {
2125 c = *xp->xp_pattern;
2126 if (c == '&')
2127 {
2128 c = xp->xp_pattern[1];
2129 if (c == '&')
2130 {
2131 ++xp->xp_pattern;
2132 xp->xp_context = cmdidx != CMD_let || got_eq
2133 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2134 }
2135 else if (c != ' ')
2136 xp->xp_context = EXPAND_SETTINGS;
2137 }
2138 else if (c == '$')
2139 {
2140 /* environment variable */
2141 xp->xp_context = EXPAND_ENV_VARS;
2142 }
2143 else if (c == '=')
2144 {
2145 got_eq = TRUE;
2146 xp->xp_context = EXPAND_EXPRESSION;
2147 }
2148 else if (c == '<'
2149 && xp->xp_context == EXPAND_FUNCTIONS
2150 && vim_strchr(xp->xp_pattern, '(') == NULL)
2151 {
2152 /* Function name can start with "<SNR>" */
2153 break;
2154 }
2155 else if (cmdidx != CMD_let || got_eq)
2156 {
2157 if (c == '"') /* string */
2158 {
2159 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2160 if (c == '\\' && xp->xp_pattern[1] != NUL)
2161 ++xp->xp_pattern;
2162 xp->xp_context = EXPAND_NOTHING;
2163 }
2164 else if (c == '\'') /* literal string */
2165 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002166 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2168 /* skip */ ;
2169 xp->xp_context = EXPAND_NOTHING;
2170 }
2171 else if (c == '|')
2172 {
2173 if (xp->xp_pattern[1] == '|')
2174 {
2175 ++xp->xp_pattern;
2176 xp->xp_context = EXPAND_EXPRESSION;
2177 }
2178 else
2179 xp->xp_context = EXPAND_COMMANDS;
2180 }
2181 else
2182 xp->xp_context = EXPAND_EXPRESSION;
2183 }
2184 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002185 /* Doesn't look like something valid, expand as an expression
2186 * anyway. */
2187 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 arg = xp->xp_pattern;
2189 if (*arg != NUL)
2190 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2191 /* skip */ ;
2192 }
2193 xp->xp_pattern = arg;
2194}
2195
2196#endif /* FEAT_CMDL_COMPL */
2197
2198/*
2199 * ":1,25call func(arg1, arg2)" function call.
2200 */
2201 void
2202ex_call(eap)
2203 exarg_T *eap;
2204{
2205 char_u *arg = eap->arg;
2206 char_u *startarg;
2207 char_u *alias;
2208 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002209 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 int len;
2211 linenr_T lnum;
2212 int doesrange;
2213 int failed = FALSE;
2214
2215 name = arg;
2216 len = get_func_len(&arg, &alias, !eap->skip);
2217 if (len == 0)
2218 goto end;
2219 if (alias != NULL)
2220 name = alias;
2221
2222 startarg = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224
2225 if (*startarg != '(')
2226 {
2227 EMSG2(_("E107: Missing braces: %s"), name);
2228 goto end;
2229 }
2230
2231 /*
2232 * When skipping, evaluate the function once, to find the end of the
2233 * arguments.
2234 * When the function takes a range, this is discovered after the first
2235 * call, and the loop is broken.
2236 */
2237 if (eap->skip)
2238 {
2239 ++emsg_skip;
2240 lnum = eap->line2; /* do it once, also with an invalid range */
2241 }
2242 else
2243 lnum = eap->line1;
2244 for ( ; lnum <= eap->line2; ++lnum)
2245 {
2246 if (!eap->skip && eap->addr_count > 0)
2247 {
2248 curwin->w_cursor.lnum = lnum;
2249 curwin->w_cursor.col = 0;
2250 }
2251 arg = startarg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 if (get_func_tv(name, len, &rettv, &arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 eap->line1, eap->line2, &doesrange, !eap->skip) == FAIL)
2254 {
2255 failed = TRUE;
2256 break;
2257 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 if (doesrange || eap->skip)
2260 break;
2261 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002262 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002264 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 if (aborting())
2266 break;
2267 }
2268 if (eap->skip)
2269 --emsg_skip;
2270
2271 if (!failed)
2272 {
2273 /* Check for trailing illegal characters and a following command. */
2274 if (!ends_excmd(*arg))
2275 {
2276 emsg_severe = TRUE;
2277 EMSG(_(e_trailing));
2278 }
2279 else
2280 eap->nextcmd = check_nextcmd(arg);
2281 }
2282
2283end:
2284 if (alias != NULL)
2285 vim_free(alias);
2286}
2287
2288/*
2289 * ":unlet[!] var1 ... " command.
2290 */
2291 void
2292ex_unlet(eap)
2293 exarg_T *eap;
2294{
2295 char_u *arg = eap->arg;
2296 char_u *name_end;
2297 char_u cc;
2298 char_u *expr_start;
2299 char_u *expr_end;
2300 int error = FALSE;
2301
2302 do
2303 {
2304 /* Find the end of the name. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002305 name_end = find_name_end(arg, &expr_start, &expr_end, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306
2307 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
2308 {
2309 emsg_severe = TRUE;
2310 EMSG(_(e_trailing));
2311 break;
2312 }
2313
2314 if (!error && !eap->skip)
2315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 if (expr_start != NULL)
2317 {
2318 char_u *temp_string;
2319
2320 temp_string = make_expanded_name(arg, expr_start,
2321 expr_end, name_end);
2322 if (temp_string == NULL)
2323 {
2324 /*
2325 * Report an invalid expression in braces, unless the
2326 * expression evaluation has been cancelled due to an
2327 * aborting error, an interrupt, or an exception.
2328 */
2329 if (!aborting())
2330 {
2331 emsg_severe = TRUE;
2332 EMSG2(_(e_invarg2), arg);
2333 break;
2334 }
2335 error = TRUE;
2336 }
2337 else
2338 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002339 if (do_unlet_var(temp_string, eap->forceit) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 vim_free(temp_string);
2342 }
2343 }
2344 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
2346 cc = *name_end;
2347 *name_end = NUL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002348 if (do_unlet_var(arg, eap->forceit) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 *name_end = cc;
2351 }
2352 }
2353 arg = skipwhite(name_end);
2354 } while (!ends_excmd(*arg));
2355
2356 eap->nextcmd = check_nextcmd(arg);
2357}
2358
Bram Moolenaar8c711452005-01-14 21:53:12 +00002359 static int
2360do_unlet_var(name, forceit)
2361 char_u *name;
2362 int forceit;
2363{
2364 if (check_changedtick(name))
2365 return FAIL;
2366 if (do_unlet(name) == FAIL && !forceit)
2367 {
2368 EMSG2(_("E108: No such variable: \"%s\""), name);
2369 return FAIL;
2370 }
2371 return OK;
2372}
2373
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374/*
2375 * "unlet" a variable. Return OK if it existed, FAIL if not.
2376 */
2377 int
2378do_unlet(name)
2379 char_u *name;
2380{
2381 VAR v;
2382
2383 v = find_var(name, TRUE);
2384 if (v != NULL)
2385 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002386 clear_var(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 return OK;
2388 }
2389 return FAIL;
2390}
2391
2392#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2393/*
2394 * Delete all "menutrans_" variables.
2395 */
2396 void
2397del_menutrans_vars()
2398{
2399 int i;
2400
2401 for (i = 0; i < variables.ga_len; ++i)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002402 if (VAR_ENTRY(i).v_name != NULL
2403 && STRNCMP(VAR_ENTRY(i).v_name, "menutrans_", 10) == 0)
2404 clear_var(&VAR_ENTRY(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405}
2406#endif
2407
2408#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2409
2410/*
2411 * Local string buffer for the next two functions to store a variable name
2412 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2413 * get_user_var_name().
2414 */
2415
2416static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
2417
2418static char_u *varnamebuf = NULL;
2419static int varnamebuflen = 0;
2420
2421/*
2422 * Function to concatenate a prefix and a variable name.
2423 */
2424 static char_u *
2425cat_prefix_varname(prefix, name)
2426 int prefix;
2427 char_u *name;
2428{
2429 int len;
2430
2431 len = (int)STRLEN(name) + 3;
2432 if (len > varnamebuflen)
2433 {
2434 vim_free(varnamebuf);
2435 len += 10; /* some additional space */
2436 varnamebuf = alloc(len);
2437 if (varnamebuf == NULL)
2438 {
2439 varnamebuflen = 0;
2440 return NULL;
2441 }
2442 varnamebuflen = len;
2443 }
2444 *varnamebuf = prefix;
2445 varnamebuf[1] = ':';
2446 STRCPY(varnamebuf + 2, name);
2447 return varnamebuf;
2448}
2449
2450/*
2451 * Function given to ExpandGeneric() to obtain the list of user defined
2452 * (global/buffer/window/built-in) variable names.
2453 */
2454/*ARGSUSED*/
2455 char_u *
2456get_user_var_name(xp, idx)
2457 expand_T *xp;
2458 int idx;
2459{
2460 static int gidx;
2461 static int bidx;
2462 static int widx;
2463 static int vidx;
2464 char_u *name;
2465
2466 if (idx == 0)
2467 gidx = bidx = widx = vidx = 0;
2468 if (gidx < variables.ga_len) /* Global variables */
2469 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002470 while ((name = VAR_ENTRY(gidx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 && gidx < variables.ga_len)
2472 /* skip */;
2473 if (name != NULL)
2474 {
2475 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2476 return cat_prefix_varname('g', name);
2477 else
2478 return name;
2479 }
2480 }
2481 if (bidx < curbuf->b_vars.ga_len) /* Current buffer variables */
2482 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002483 while ((name = BVAR_ENTRY(bidx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 && bidx < curbuf->b_vars.ga_len)
2485 /* skip */;
2486 if (name != NULL)
2487 return cat_prefix_varname('b', name);
2488 }
2489 if (bidx == curbuf->b_vars.ga_len)
2490 {
2491 ++bidx;
2492 return (char_u *)"b:changedtick";
2493 }
2494 if (widx < curwin->w_vars.ga_len) /* Current window variables */
2495 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002496 while ((name = WVAR_ENTRY(widx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 && widx < curwin->w_vars.ga_len)
2498 /* skip */;
2499 if (name != NULL)
2500 return cat_prefix_varname('w', name);
2501 }
2502 if (vidx < VV_LEN) /* Built-in variables */
2503 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].name);
2504
2505 vim_free(varnamebuf);
2506 varnamebuf = NULL;
2507 varnamebuflen = 0;
2508 return NULL;
2509}
2510
2511#endif /* FEAT_CMDL_COMPL */
2512
2513/*
2514 * types for expressions.
2515 */
2516typedef enum
2517{
2518 TYPE_UNKNOWN = 0
2519 , TYPE_EQUAL /* == */
2520 , TYPE_NEQUAL /* != */
2521 , TYPE_GREATER /* > */
2522 , TYPE_GEQUAL /* >= */
2523 , TYPE_SMALLER /* < */
2524 , TYPE_SEQUAL /* <= */
2525 , TYPE_MATCH /* =~ */
2526 , TYPE_NOMATCH /* !~ */
2527} exptype_T;
2528
2529/*
2530 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2533 */
2534
2535/*
2536 * Handle zero level expression.
2537 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002538 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 * Return OK or FAIL.
2540 */
2541 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002542eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 char_u *arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002544 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 char_u **nextcmd;
2546 int evaluate;
2547{
2548 int ret;
2549 char_u *p;
2550
2551 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002552 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 if (ret == FAIL || !ends_excmd(*p))
2554 {
2555 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002556 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 /*
2558 * Report the invalid expression unless the expression evaluation has
2559 * been cancelled due to an aborting error, an interrupt, or an
2560 * exception.
2561 */
2562 if (!aborting())
2563 EMSG2(_(e_invexpr2), arg);
2564 ret = FAIL;
2565 }
2566 if (nextcmd != NULL)
2567 *nextcmd = check_nextcmd(p);
2568
2569 return ret;
2570}
2571
2572/*
2573 * Handle top level expression:
2574 * expr1 ? expr0 : expr0
2575 *
2576 * "arg" must point to the first non-white of the expression.
2577 * "arg" is advanced to the next non-white after the recognized expression.
2578 *
2579 * Return OK or FAIL.
2580 */
2581 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002582eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 int evaluate;
2586{
2587 int result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002588 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589
2590 /*
2591 * Get the first variable.
2592 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002593 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 return FAIL;
2595
2596 if ((*arg)[0] == '?')
2597 {
2598 result = FALSE;
2599 if (evaluate)
2600 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002601 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002603 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
2605
2606 /*
2607 * Get the second variable.
2608 */
2609 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 return FAIL;
2612
2613 /*
2614 * Check for the ":".
2615 */
2616 if ((*arg)[0] != ':')
2617 {
2618 EMSG(_("E109: Missing ':' after '?'"));
2619 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002620 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 return FAIL;
2622 }
2623
2624 /*
2625 * Get the third variable.
2626 */
2627 *arg = skipwhite(*arg + 1);
2628 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
2629 {
2630 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002631 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 return FAIL;
2633 }
2634 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002635 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 }
2637
2638 return OK;
2639}
2640
2641/*
2642 * Handle first level expression:
2643 * expr2 || expr2 || expr2 logical OR
2644 *
2645 * "arg" must point to the first non-white of the expression.
2646 * "arg" is advanced to the next non-white after the recognized expression.
2647 *
2648 * Return OK or FAIL.
2649 */
2650 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002651eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002653 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 int evaluate;
2655{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002656 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 long result;
2658 int first;
2659
2660 /*
2661 * Get the first variable.
2662 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002663 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 return FAIL;
2665
2666 /*
2667 * Repeat until there is no following "||".
2668 */
2669 first = TRUE;
2670 result = FALSE;
2671 while ((*arg)[0] == '|' && (*arg)[1] == '|')
2672 {
2673 if (evaluate && first)
2674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002675 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002677 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 first = FALSE;
2679 }
2680
2681 /*
2682 * Get the second variable.
2683 */
2684 *arg = skipwhite(*arg + 2);
2685 if (eval3(arg, &var2, evaluate && !result) == FAIL)
2686 return FAIL;
2687
2688 /*
2689 * Compute the result.
2690 */
2691 if (evaluate && !result)
2692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002693 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002695 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 }
2697 if (evaluate)
2698 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002699 rettv->v_type = VAR_NUMBER;
2700 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 }
2702 }
2703
2704 return OK;
2705}
2706
2707/*
2708 * Handle second level expression:
2709 * expr3 && expr3 && expr3 logical AND
2710 *
2711 * "arg" must point to the first non-white of the expression.
2712 * "arg" is advanced to the next non-white after the recognized expression.
2713 *
2714 * Return OK or FAIL.
2715 */
2716 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002717eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002719 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 int evaluate;
2721{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002722 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 long result;
2724 int first;
2725
2726 /*
2727 * Get the first variable.
2728 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002729 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 return FAIL;
2731
2732 /*
2733 * Repeat until there is no following "&&".
2734 */
2735 first = TRUE;
2736 result = TRUE;
2737 while ((*arg)[0] == '&' && (*arg)[1] == '&')
2738 {
2739 if (evaluate && first)
2740 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002741 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002743 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 first = FALSE;
2745 }
2746
2747 /*
2748 * Get the second variable.
2749 */
2750 *arg = skipwhite(*arg + 2);
2751 if (eval4(arg, &var2, evaluate && result) == FAIL)
2752 return FAIL;
2753
2754 /*
2755 * Compute the result.
2756 */
2757 if (evaluate && result)
2758 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002759 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002761 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 }
2763 if (evaluate)
2764 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002765 rettv->v_type = VAR_NUMBER;
2766 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 }
2768 }
2769
2770 return OK;
2771}
2772
2773/*
2774 * Handle third level expression:
2775 * var1 == var2
2776 * var1 =~ var2
2777 * var1 != var2
2778 * var1 !~ var2
2779 * var1 > var2
2780 * var1 >= var2
2781 * var1 < var2
2782 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002783 * var1 is var2
2784 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 *
2786 * "arg" must point to the first non-white of the expression.
2787 * "arg" is advanced to the next non-white after the recognized expression.
2788 *
2789 * Return OK or FAIL.
2790 */
2791 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002792eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002794 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 int evaluate;
2796{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002797 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 char_u *p;
2799 int i;
2800 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002801 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 int len = 2;
2803 long n1, n2;
2804 char_u *s1, *s2;
2805 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2806 regmatch_T regmatch;
2807 int ic;
2808 char_u *save_cpo;
2809
2810 /*
2811 * Get the first variable.
2812 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002813 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 return FAIL;
2815
2816 p = *arg;
2817 switch (p[0])
2818 {
2819 case '=': if (p[1] == '=')
2820 type = TYPE_EQUAL;
2821 else if (p[1] == '~')
2822 type = TYPE_MATCH;
2823 break;
2824 case '!': if (p[1] == '=')
2825 type = TYPE_NEQUAL;
2826 else if (p[1] == '~')
2827 type = TYPE_NOMATCH;
2828 break;
2829 case '>': if (p[1] != '=')
2830 {
2831 type = TYPE_GREATER;
2832 len = 1;
2833 }
2834 else
2835 type = TYPE_GEQUAL;
2836 break;
2837 case '<': if (p[1] != '=')
2838 {
2839 type = TYPE_SMALLER;
2840 len = 1;
2841 }
2842 else
2843 type = TYPE_SEQUAL;
2844 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002845 case 'i': if (p[1] == 's')
2846 {
2847 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2848 len = 5;
2849 if (!vim_isIDc(p[len]))
2850 {
2851 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
2852 type_is = TRUE;
2853 }
2854 }
2855 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 }
2857
2858 /*
2859 * If there is a comparitive operator, use it.
2860 */
2861 if (type != TYPE_UNKNOWN)
2862 {
2863 /* extra question mark appended: ignore case */
2864 if (p[len] == '?')
2865 {
2866 ic = TRUE;
2867 ++len;
2868 }
2869 /* extra '#' appended: match case */
2870 else if (p[len] == '#')
2871 {
2872 ic = FALSE;
2873 ++len;
2874 }
2875 /* nothing appened: use 'ignorecase' */
2876 else
2877 ic = p_ic;
2878
2879 /*
2880 * Get the second variable.
2881 */
2882 *arg = skipwhite(p + len);
2883 if (eval5(arg, &var2, evaluate) == FAIL)
2884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002885 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 return FAIL;
2887 }
2888
2889 if (evaluate)
2890 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002891 if (type_is && rettv->v_type != var2.v_type)
2892 {
2893 /* For "is" a different type always means FALSE, for "notis"
2894 * it means TRUE. */
2895 n1 = (type == TYPE_NEQUAL);
2896 }
2897 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
2898 {
2899 if (type_is)
2900 {
2901 n1 = (rettv->v_type == var2.v_type
2902 && rettv->vval.v_list == var2.vval.v_list);
2903 if (type == TYPE_NEQUAL)
2904 n1 = !n1;
2905 }
2906 else if (rettv->v_type != var2.v_type
2907 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
2908 {
2909 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002910 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002911 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002912 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002913 clear_tv(rettv);
2914 clear_tv(&var2);
2915 return FAIL;
2916 }
2917 else
2918 {
2919 /* Compare two Lists for being equal or unequal. */
2920 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
2921 if (type == TYPE_NEQUAL)
2922 n1 = !n1;
2923 }
2924 }
2925
2926 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
2927 {
2928 if (rettv->v_type != var2.v_type
2929 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
2930 {
2931 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002932 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002933 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002934 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002935 clear_tv(rettv);
2936 clear_tv(&var2);
2937 return FAIL;
2938 }
2939 else
2940 {
2941 /* Compare two Funcrefs for being equal or unequal. */
2942 if (rettv->vval.v_string == NULL
2943 || var2.vval.v_string == NULL)
2944 n1 = FALSE;
2945 else
2946 n1 = STRCMP(rettv->vval.v_string,
2947 var2.vval.v_string) == 0;
2948 if (type == TYPE_NEQUAL)
2949 n1 = !n1;
2950 }
2951 }
2952
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 /*
2954 * If one of the two variables is a number, compare as a number.
2955 * When using "=~" or "!~", always compare as string.
2956 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002957 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 && type != TYPE_MATCH && type != TYPE_NOMATCH)
2959 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002960 n1 = get_tv_number(rettv);
2961 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 switch (type)
2963 {
2964 case TYPE_EQUAL: n1 = (n1 == n2); break;
2965 case TYPE_NEQUAL: n1 = (n1 != n2); break;
2966 case TYPE_GREATER: n1 = (n1 > n2); break;
2967 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
2968 case TYPE_SMALLER: n1 = (n1 < n2); break;
2969 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
2970 case TYPE_UNKNOWN:
2971 case TYPE_MATCH:
2972 case TYPE_NOMATCH: break; /* avoid gcc warning */
2973 }
2974 }
2975 else
2976 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002977 s1 = get_tv_string_buf(rettv, buf1);
2978 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
2980 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
2981 else
2982 i = 0;
2983 n1 = FALSE;
2984 switch (type)
2985 {
2986 case TYPE_EQUAL: n1 = (i == 0); break;
2987 case TYPE_NEQUAL: n1 = (i != 0); break;
2988 case TYPE_GREATER: n1 = (i > 0); break;
2989 case TYPE_GEQUAL: n1 = (i >= 0); break;
2990 case TYPE_SMALLER: n1 = (i < 0); break;
2991 case TYPE_SEQUAL: n1 = (i <= 0); break;
2992
2993 case TYPE_MATCH:
2994 case TYPE_NOMATCH:
2995 /* avoid 'l' flag in 'cpoptions' */
2996 save_cpo = p_cpo;
2997 p_cpo = (char_u *)"";
2998 regmatch.regprog = vim_regcomp(s2,
2999 RE_MAGIC + RE_STRING);
3000 regmatch.rm_ic = ic;
3001 if (regmatch.regprog != NULL)
3002 {
3003 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
3004 vim_free(regmatch.regprog);
3005 if (type == TYPE_NOMATCH)
3006 n1 = !n1;
3007 }
3008 p_cpo = save_cpo;
3009 break;
3010
3011 case TYPE_UNKNOWN: break; /* avoid gcc warning */
3012 }
3013 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003014 clear_tv(rettv);
3015 clear_tv(&var2);
3016 rettv->v_type = VAR_NUMBER;
3017 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 }
3019 }
3020
3021 return OK;
3022}
3023
3024/*
3025 * Handle fourth level expression:
3026 * + number addition
3027 * - number subtraction
3028 * . string concatenation
3029 *
3030 * "arg" must point to the first non-white of the expression.
3031 * "arg" is advanced to the next non-white after the recognized expression.
3032 *
3033 * Return OK or FAIL.
3034 */
3035 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003036eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003038 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 int evaluate;
3040{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003041 typeval var2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003042 typeval var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 int op;
3044 long n1, n2;
3045 char_u *s1, *s2;
3046 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3047 char_u *p;
3048
3049 /*
3050 * Get the first variable.
3051 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003052 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 return FAIL;
3054
3055 /*
3056 * Repeat computing, until no '+', '-' or '.' is following.
3057 */
3058 for (;;)
3059 {
3060 op = **arg;
3061 if (op != '+' && op != '-' && op != '.')
3062 break;
3063
3064 /*
3065 * Get the second variable.
3066 */
3067 *arg = skipwhite(*arg + 1);
3068 if (eval6(arg, &var2, evaluate) == FAIL)
3069 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003070 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 return FAIL;
3072 }
3073
3074 if (evaluate)
3075 {
3076 /*
3077 * Compute the result.
3078 */
3079 if (op == '.')
3080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003081 s1 = get_tv_string_buf(rettv, buf1);
3082 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 op = (int)STRLEN(s1);
3084 p = alloc((unsigned)(op + STRLEN(s2) + 1));
3085 if (p != NULL)
3086 {
3087 STRCPY(p, s1);
3088 STRCPY(p + op, s2);
3089 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003090 clear_tv(rettv);
3091 rettv->v_type = VAR_STRING;
3092 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003094 else if (rettv->v_type == VAR_LIST && var2.v_type == VAR_LIST)
3095 {
3096 /* concatenate Lists */
3097 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3098 &var3) == FAIL)
3099 {
3100 clear_tv(rettv);
3101 clear_tv(&var2);
3102 return FAIL;
3103 }
3104 clear_tv(rettv);
3105 *rettv = var3;
3106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 else
3108 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003109 n1 = get_tv_number(rettv);
3110 n2 = get_tv_number(&var2);
3111 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 if (op == '+')
3113 n1 = n1 + n2;
3114 else
3115 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003116 rettv->v_type = VAR_NUMBER;
3117 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003119 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 }
3121 }
3122 return OK;
3123}
3124
3125/*
3126 * Handle fifth level expression:
3127 * * number multiplication
3128 * / number division
3129 * % number modulo
3130 *
3131 * "arg" must point to the first non-white of the expression.
3132 * "arg" is advanced to the next non-white after the recognized expression.
3133 *
3134 * Return OK or FAIL.
3135 */
3136 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003137eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003139 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 int evaluate;
3141{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003142 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 int op;
3144 long n1, n2;
3145
3146 /*
3147 * Get the first variable.
3148 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003149 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 return FAIL;
3151
3152 /*
3153 * Repeat computing, until no '*', '/' or '%' is following.
3154 */
3155 for (;;)
3156 {
3157 op = **arg;
3158 if (op != '*' && op != '/' && op != '%')
3159 break;
3160
3161 if (evaluate)
3162 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003163 n1 = get_tv_number(rettv);
3164 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 }
3166 else
3167 n1 = 0;
3168
3169 /*
3170 * Get the second variable.
3171 */
3172 *arg = skipwhite(*arg + 1);
3173 if (eval7(arg, &var2, evaluate) == FAIL)
3174 return FAIL;
3175
3176 if (evaluate)
3177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003178 n2 = get_tv_number(&var2);
3179 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
3181 /*
3182 * Compute the result.
3183 */
3184 if (op == '*')
3185 n1 = n1 * n2;
3186 else if (op == '/')
3187 {
3188 if (n2 == 0) /* give an error message? */
3189 n1 = 0x7fffffffL;
3190 else
3191 n1 = n1 / n2;
3192 }
3193 else
3194 {
3195 if (n2 == 0) /* give an error message? */
3196 n1 = 0;
3197 else
3198 n1 = n1 % n2;
3199 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003200 rettv->v_type = VAR_NUMBER;
3201 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 }
3203 }
3204
3205 return OK;
3206}
3207
3208/*
3209 * Handle sixth level expression:
3210 * number number constant
3211 * "string" string contstant
3212 * 'string' literal string contstant
3213 * &option-name option value
3214 * @r register contents
3215 * identifier variable value
3216 * function() function call
3217 * $VAR environment variable
3218 * (expression) nested expression
3219 *
3220 * Also handle:
3221 * ! in front logical NOT
3222 * - in front unary minus
3223 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003224 * trailing [] subscript in String or List
3225 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 *
3227 * "arg" must point to the first non-white of the expression.
3228 * "arg" is advanced to the next non-white after the recognized expression.
3229 *
3230 * Return OK or FAIL.
3231 */
3232 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003233eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003235 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 int evaluate;
3237{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 long n;
3239 int len;
3240 char_u *s;
3241 int val;
3242 char_u *start_leader, *end_leader;
3243 int ret = OK;
3244 char_u *alias;
3245
3246 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003247 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003248 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003250 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
3252 /*
3253 * Skip '!' and '-' characters. They are handled later.
3254 */
3255 start_leader = *arg;
3256 while (**arg == '!' || **arg == '-' || **arg == '+')
3257 *arg = skipwhite(*arg + 1);
3258 end_leader = *arg;
3259
3260 switch (**arg)
3261 {
3262 /*
3263 * Number constant.
3264 */
3265 case '0':
3266 case '1':
3267 case '2':
3268 case '3':
3269 case '4':
3270 case '5':
3271 case '6':
3272 case '7':
3273 case '8':
3274 case '9':
3275 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
3276 *arg += len;
3277 if (evaluate)
3278 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003279 rettv->v_type = VAR_NUMBER;
3280 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 }
3282 break;
3283
3284 /*
3285 * String constant: "string".
3286 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003287 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 break;
3289
3290 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003291 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003293 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003294 break;
3295
3296 /*
3297 * List: [expr, expr]
3298 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003299 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 break;
3301
3302 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003303 * Dictionary: {key: val, key: val}
3304 */
3305 case '{': ret = get_dict_tv(arg, rettv, evaluate);
3306 break;
3307
3308 /*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003309 * Option value: &name or map() item "&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003311 case '&': if (!ASCII_ISALPHA(*(*arg + 1)))
3312 {
3313 *arg = skipwhite(*arg + 1);
3314 if (evaluate)
3315 ret = get_amp_tv(rettv);
3316 }
3317 else
3318 ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 break;
3320
3321 /*
3322 * Environment variable: $VAR.
3323 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003324 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 break;
3326
3327 /*
3328 * Register contents: @r.
3329 */
3330 case '@': ++*arg;
3331 if (evaluate)
3332 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003333 rettv->v_type = VAR_STRING;
3334 rettv->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 }
3336 if (**arg != NUL)
3337 ++*arg;
3338 break;
3339
3340 /*
3341 * nested expression: (expression).
3342 */
3343 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003344 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 if (**arg == ')')
3346 ++*arg;
3347 else if (ret == OK)
3348 {
3349 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003350 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 ret = FAIL;
3352 }
3353 break;
3354
Bram Moolenaar8c711452005-01-14 21:53:12 +00003355 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 break;
3357 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003358
3359 if (ret == NOTDONE)
3360 {
3361 /*
3362 * Must be a variable or function name.
3363 * Can also be a curly-braces kind of name: {expr}.
3364 */
3365 s = *arg;
3366 len = get_func_len(arg, &alias, evaluate);
3367 if (alias != NULL)
3368 s = alias;
3369
3370 if (len == 0)
3371 ret = FAIL;
3372 else
3373 {
3374 if (**arg == '(') /* recursive! */
3375 {
3376 /* If "s" is the name of a variable of type VAR_FUNC
3377 * use its contents. */
3378 s = deref_func_name(s, &len);
3379
3380 /* Invoke the function. */
3381 ret = get_func_tv(s, len, rettv, arg,
3382 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
3383 &len, evaluate);
3384 /* Stop the expression evaluation when immediately
3385 * aborting on error, or when an interrupt occurred or
3386 * an exception was thrown but not caught. */
3387 if (aborting())
3388 {
3389 if (ret == OK)
3390 clear_tv(rettv);
3391 ret = FAIL;
3392 }
3393 }
3394 else if (evaluate)
3395 ret = get_var_tv(s, len, rettv);
3396 }
3397
3398 if (alias != NULL)
3399 vim_free(alias);
3400 }
3401
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 *arg = skipwhite(*arg);
3403
3404 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003405 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00003407 while ((**arg == '[' || (**arg == '.' && rettv->v_type == VAR_DICT))
3408 && !vim_iswhite(*(*arg - 1)) && ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003410 if (eval_index(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003412 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 return FAIL;
3414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 }
3416
3417 /*
3418 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3419 */
3420 if (ret == OK && evaluate && end_leader > start_leader)
3421 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003422 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 while (end_leader > start_leader)
3424 {
3425 --end_leader;
3426 if (*end_leader == '!')
3427 val = !val;
3428 else if (*end_leader == '-')
3429 val = -val;
3430 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003431 clear_tv(rettv);
3432 rettv->v_type = VAR_NUMBER;
3433 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 }
3435
3436 return ret;
3437}
3438
3439/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003440 * Evaluate an "[expr]" or "[expr:expr]" index.
3441 * "*arg" points to the '['.
3442 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3443 */
3444 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003445eval_index(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003446 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003447 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003448 int evaluate;
3449{
3450 int empty1 = FALSE, empty2 = FALSE;
3451 typeval var1, var2;
3452 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003453 long len = -1;
3454 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003455 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003456 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003457
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003458 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003459 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003460 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003461 return FAIL;
3462 }
3463
Bram Moolenaar8c711452005-01-14 21:53:12 +00003464 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003465 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003466 /*
3467 * dict.name
3468 */
3469 key = *arg + 1;
3470 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3471 ;
3472 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003473 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003474 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003475 }
3476 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003477 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003478 /*
3479 * something[idx]
3480 *
3481 * Get the (first) variable from inside the [].
3482 */
3483 *arg = skipwhite(*arg + 1);
3484 if (**arg == ':')
3485 empty1 = TRUE;
3486 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
3487 return FAIL;
3488
3489 /*
3490 * Get the second variable from inside the [:].
3491 */
3492 if (**arg == ':')
3493 {
3494 range = TRUE;
3495 *arg = skipwhite(*arg + 1);
3496 if (**arg == ']')
3497 empty2 = TRUE;
3498 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
3499 {
3500 clear_tv(&var1);
3501 return FAIL;
3502 }
3503 }
3504
3505 /* Check for the ']'. */
3506 if (**arg != ']')
3507 {
3508 EMSG(_(e_missbrac));
3509 clear_tv(&var1);
3510 if (range)
3511 clear_tv(&var2);
3512 return FAIL;
3513 }
3514 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003515 }
3516
3517 if (evaluate)
3518 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003519 n1 = 0;
3520 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003521 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003522 n1 = get_tv_number(&var1);
3523 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003524 }
3525 if (range)
3526 {
3527 if (empty2)
3528 n2 = -1;
3529 else
3530 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003531 n2 = get_tv_number(&var2);
3532 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003533 }
3534 }
3535
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003536 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003537 {
3538 case VAR_NUMBER:
3539 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003540 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003541 len = (long)STRLEN(s);
3542 if (range)
3543 {
3544 /* The resulting variable is a substring. If the indexes
3545 * are out of range the result is empty. */
3546 if (n1 < 0)
3547 {
3548 n1 = len + n1;
3549 if (n1 < 0)
3550 n1 = 0;
3551 }
3552 if (n2 < 0)
3553 n2 = len + n2;
3554 else if (n2 >= len)
3555 n2 = len;
3556 if (n1 >= len || n2 < 0 || n1 > n2)
3557 s = NULL;
3558 else
3559 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3560 }
3561 else
3562 {
3563 /* The resulting variable is a string of a single
3564 * character. If the index is too big or negative the
3565 * result is empty. */
3566 if (n1 >= len || n1 < 0)
3567 s = NULL;
3568 else
3569 s = vim_strnsave(s + n1, 1);
3570 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003571 clear_tv(rettv);
3572 rettv->v_type = VAR_STRING;
3573 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003574 break;
3575
3576 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003577 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003578 if (n1 < 0)
3579 n1 = len + n1;
3580 if (!empty1 && (n1 < 0 || n1 >= len))
3581 {
3582 EMSGN(_(e_listidx), n1);
3583 return FAIL;
3584 }
3585 if (range)
3586 {
3587 listvar *l;
3588 listitem *item;
3589
3590 if (n2 < 0)
3591 n2 = len + n2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003592 if (!empty2 && (n2 < 0 || n2 >= len || n2 + 1 < n1))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003593 {
3594 EMSGN(_(e_listidx), n2);
3595 return FAIL;
3596 }
3597 l = list_alloc();
3598 if (l == NULL)
3599 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003600 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003601 n1 <= n2; ++n1)
3602 {
3603 if (list_append_tv(l, &item->li_tv) == FAIL)
3604 {
3605 list_free(l);
3606 return FAIL;
3607 }
3608 item = item->li_next;
3609 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003610 clear_tv(rettv);
3611 rettv->v_type = VAR_LIST;
3612 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00003613 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003614 }
3615 else
3616 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003617 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003618 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003619 clear_tv(rettv);
3620 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003621 }
3622 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003623
3624 case VAR_DICT:
3625 if (range)
3626 {
3627 EMSG(_("E999: Using range with Dictionary"));
3628 if (len == -1)
3629 clear_tv(&var1);
3630 return FAIL;
3631 }
3632 {
3633 dictitem *item;
3634
3635 if (len == -1)
3636 {
3637 key = get_tv_string(&var1);
3638 if (*key == NUL)
3639 {
3640 EMSG(_("E999: Empty key for Dictionary"));
3641 clear_tv(&var1);
3642 return FAIL;
3643 }
3644 }
3645
3646 item = dict_find(rettv->vval.v_dict, key, (int)len);
3647
3648 if (item == NULL)
3649 EMSG2(_("E999: Key not found in Dictionary: %s"), key);
3650 if (len == -1)
3651 clear_tv(&var1);
3652 if (item == NULL)
3653 return FAIL;
3654
3655 copy_tv(&item->di_tv, &var1);
3656 clear_tv(rettv);
3657 *rettv = var1;
3658 }
3659 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003660 }
3661 }
3662
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003663 return OK;
3664}
3665
3666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 * Get an option value.
3668 * "arg" points to the '&' or '+' before the option name.
3669 * "arg" is advanced to character after the option name.
3670 * Return OK or FAIL.
3671 */
3672 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003673get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003675 typeval *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 int evaluate;
3677{
3678 char_u *option_end;
3679 long numval;
3680 char_u *stringval;
3681 int opt_type;
3682 int c;
3683 int working = (**arg == '+'); /* has("+option") */
3684 int ret = OK;
3685 int opt_flags;
3686
3687 /*
3688 * Isolate the option name and find its value.
3689 */
3690 option_end = find_option_end(arg, &opt_flags);
3691 if (option_end == NULL)
3692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003693 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 EMSG2(_("E112: Option name missing: %s"), *arg);
3695 return FAIL;
3696 }
3697
3698 if (!evaluate)
3699 {
3700 *arg = option_end;
3701 return OK;
3702 }
3703
3704 c = *option_end;
3705 *option_end = NUL;
3706 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003707 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708
3709 if (opt_type == -3) /* invalid name */
3710 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003711 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 EMSG2(_("E113: Unknown option: %s"), *arg);
3713 ret = FAIL;
3714 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003715 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 {
3717 if (opt_type == -2) /* hidden string option */
3718 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003719 rettv->v_type = VAR_STRING;
3720 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 }
3722 else if (opt_type == -1) /* hidden number option */
3723 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003724 rettv->v_type = VAR_NUMBER;
3725 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 }
3727 else if (opt_type == 1) /* number option */
3728 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003729 rettv->v_type = VAR_NUMBER;
3730 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 }
3732 else /* string option */
3733 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003734 rettv->v_type = VAR_STRING;
3735 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737 }
3738 else if (working && (opt_type == -2 || opt_type == -1))
3739 ret = FAIL;
3740
3741 *option_end = c; /* put back for error messages */
3742 *arg = option_end;
3743
3744 return ret;
3745}
3746
3747/*
3748 * Allocate a variable for a string constant.
3749 * Return OK or FAIL.
3750 */
3751 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003752get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003754 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 int evaluate;
3756{
3757 char_u *p;
3758 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 int extra = 0;
3760
3761 /*
3762 * Find the end of the string, skipping backslashed characters.
3763 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003764 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 {
3766 if (*p == '\\' && p[1] != NUL)
3767 {
3768 ++p;
3769 /* A "\<x>" form occupies at least 4 characters, and produces up
3770 * to 6 characters: reserve space for 2 extra */
3771 if (*p == '<')
3772 extra += 2;
3773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
3775
3776 if (*p != '"')
3777 {
3778 EMSG2(_("E114: Missing quote: %s"), *arg);
3779 return FAIL;
3780 }
3781
3782 /* If only parsing, set *arg and return here */
3783 if (!evaluate)
3784 {
3785 *arg = p + 1;
3786 return OK;
3787 }
3788
3789 /*
3790 * Copy the string into allocated memory, handling backslashed
3791 * characters.
3792 */
3793 name = alloc((unsigned)(p - *arg + extra));
3794 if (name == NULL)
3795 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003796 rettv->v_type = VAR_STRING;
3797 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798
Bram Moolenaar8c711452005-01-14 21:53:12 +00003799 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 {
3801 if (*p == '\\')
3802 {
3803 switch (*++p)
3804 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003805 case 'b': *name++ = BS; ++p; break;
3806 case 'e': *name++ = ESC; ++p; break;
3807 case 'f': *name++ = FF; ++p; break;
3808 case 'n': *name++ = NL; ++p; break;
3809 case 'r': *name++ = CAR; ++p; break;
3810 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811
3812 case 'X': /* hex: "\x1", "\x12" */
3813 case 'x':
3814 case 'u': /* Unicode: "\u0023" */
3815 case 'U':
3816 if (vim_isxdigit(p[1]))
3817 {
3818 int n, nr;
3819 int c = toupper(*p);
3820
3821 if (c == 'X')
3822 n = 2;
3823 else
3824 n = 4;
3825 nr = 0;
3826 while (--n >= 0 && vim_isxdigit(p[1]))
3827 {
3828 ++p;
3829 nr = (nr << 4) + hex2nr(*p);
3830 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003831 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832#ifdef FEAT_MBYTE
3833 /* For "\u" store the number according to
3834 * 'encoding'. */
3835 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00003836 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 else
3838#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00003839 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 break;
3842
3843 /* octal: "\1", "\12", "\123" */
3844 case '0':
3845 case '1':
3846 case '2':
3847 case '3':
3848 case '4':
3849 case '5':
3850 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00003851 case '7': *name = *p++ - '0';
3852 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003854 *name = (*name << 3) + *p++ - '0';
3855 if (*p >= '0' && *p <= '7')
3856 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003858 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 break;
3860
3861 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00003862 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 if (extra != 0)
3864 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003865 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 break;
3867 }
3868 /* FALLTHROUGH */
3869
Bram Moolenaar8c711452005-01-14 21:53:12 +00003870 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 break;
3872 }
3873 }
3874 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003875 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003878 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 *arg = p + 1;
3880
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 return OK;
3882}
3883
3884/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003885 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 * Return OK or FAIL.
3887 */
3888 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003889get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003891 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 int evaluate;
3893{
3894 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003895 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003896 int reduce = 0;
3897
3898 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003899 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003900 */
3901 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
3902 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003903 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003904 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003905 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003906 break;
3907 ++reduce;
3908 ++p;
3909 }
3910 }
3911
Bram Moolenaar8c711452005-01-14 21:53:12 +00003912 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003913 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003914 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003915 return FAIL;
3916 }
3917
Bram Moolenaar8c711452005-01-14 21:53:12 +00003918 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003919 if (!evaluate)
3920 {
3921 *arg = p + 1;
3922 return OK;
3923 }
3924
3925 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003926 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003927 */
3928 str = alloc((unsigned)((p - *arg) - reduce));
3929 if (str == NULL)
3930 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003931 rettv->v_type = VAR_STRING;
3932 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003933
Bram Moolenaar8c711452005-01-14 21:53:12 +00003934 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003935 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003936 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003937 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003938 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003939 break;
3940 ++p;
3941 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003942 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003943 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003944 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003945 *arg = p + 1;
3946
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003947 return OK;
3948}
3949
3950/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003951 * Allocate a variable for a List and fill it from "*arg".
3952 * Return OK or FAIL.
3953 */
3954 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003955get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003956 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003957 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003958 int evaluate;
3959{
3960 listvar *l = NULL;
3961 typeval tv;
3962 listitem *item;
3963
3964 if (evaluate)
3965 {
3966 l = list_alloc();
3967 if (l == NULL)
3968 return FAIL;
3969 }
3970
3971 *arg = skipwhite(*arg + 1);
3972 while (**arg != ']' && **arg != NUL)
3973 {
3974 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
3975 goto failret;
3976 if (evaluate)
3977 {
3978 item = listitem_alloc();
3979 if (item != NULL)
3980 {
3981 item->li_tv = tv;
3982 list_append(l, item);
3983 }
3984 }
3985
3986 if (**arg == ']')
3987 break;
3988 if (**arg != ',')
3989 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003990 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003991 goto failret;
3992 }
3993 *arg = skipwhite(*arg + 1);
3994 }
3995
3996 if (**arg != ']')
3997 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003998 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003999failret:
4000 if (evaluate)
4001 list_free(l);
4002 return FAIL;
4003 }
4004
4005 *arg = skipwhite(*arg + 1);
4006 if (evaluate)
4007 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004008 rettv->v_type = VAR_LIST;
4009 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004010 ++l->lv_refcount;
4011 }
4012
4013 return OK;
4014}
4015
4016/*
4017 * Allocate an empty header for a list.
4018 */
4019 static listvar *
4020list_alloc()
4021{
4022 return (listvar *)alloc_clear(sizeof(listvar));
4023}
4024
4025/*
4026 * Unreference a list: decrement the reference count and free it when it
4027 * becomes zero.
4028 */
4029 static void
4030list_unref(l)
4031 listvar *l;
4032{
4033 if (l != NULL && --l->lv_refcount <= 0)
4034 list_free(l);
4035}
4036
4037/*
4038 * Free a list, including all items it points to.
4039 * Ignores the reference count.
4040 */
4041 static void
4042list_free(l)
4043 listvar *l;
4044{
4045 listitem *item;
4046 listitem *next;
4047
4048 for (item = l->lv_first; item != NULL; item = next)
4049 {
4050 next = item->li_next;
4051 listitem_free(item);
4052 }
4053 vim_free(l);
4054}
4055
4056/*
4057 * Allocate a list item.
4058 */
4059 static listitem *
4060listitem_alloc()
4061{
4062 return (listitem *)alloc(sizeof(listitem));
4063}
4064
4065/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00004066 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004067 */
4068 static void
4069listitem_free(item)
4070 listitem *item;
4071{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004072 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004073 vim_free(item);
4074}
4075
4076/*
4077 * Get the number of items in a list.
4078 */
4079 static long
4080list_len(l)
4081 listvar *l;
4082{
4083 listitem *item;
4084 long len = 0;
4085
4086 if (l == NULL)
4087 return 0L;
4088 for (item = l->lv_first; item != NULL; item = item->li_next)
4089 ++len;
4090 return len;
4091}
4092
4093/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004094 * Return TRUE when two lists have exactly the same values.
4095 */
4096 static int
4097list_equal(l1, l2, ic)
4098 listvar *l1;
4099 listvar *l2;
4100 int ic; /* ignore case for strings */
4101{
4102 listitem *item1, *item2;
4103
4104 for (item1 = l1->lv_first, item2 = l2->lv_first;
4105 item1 != NULL && item2 != NULL;
4106 item1 = item1->li_next, item2 = item2->li_next)
4107 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
4108 return FALSE;
4109 return item1 == NULL && item2 == NULL;
4110}
4111
4112/*
4113 * Return TRUE if "tv1" and "tv2" have the same value.
4114 * Compares the items just like "==" would compare them.
4115 */
4116 static int
4117tv_equal(tv1, tv2, ic)
4118 typeval *tv1;
4119 typeval *tv2;
4120 int ic; /* ignore case */
4121{
4122 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4123
4124 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
4125 {
4126 /* recursive! */
4127 if (tv1->v_type != tv2->v_type
4128 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
4129 return FALSE;
4130 }
4131 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
4132 {
4133 if (tv1->v_type != tv2->v_type
4134 || tv1->vval.v_string == NULL
4135 || tv2->vval.v_string == NULL
4136 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
4137 return FALSE;
4138 }
4139 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
4140 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004141 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
4142 * Don't consider "4x" to be equal to 4. */
4143 if ((tv1->v_type == VAR_STRING
4144 && !string_isa_number(tv1->vval.v_string))
4145 || (tv2->v_type == VAR_STRING
4146 && !string_isa_number(tv2->vval.v_string)))
4147 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004148 if (get_tv_number(tv1) != get_tv_number(tv2))
4149 return FALSE;
4150 }
4151 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
4152 get_tv_string_buf(tv2, buf2)) != 0)
4153 return FALSE;
4154 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
4155 get_tv_string_buf(tv2, buf2)) != 0)
4156 return FALSE;
4157 return TRUE;
4158}
4159
4160/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004161 * Return TRUE if "tv" is a number without other non-white characters.
4162 */
4163 static int
4164string_isa_number(s)
4165 char_u *s;
4166{
4167 int len;
4168
4169 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
4170 return len > 0 && *skipwhite(s + len) == NUL;
4171}
4172
4173/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004174 * Locate item with index "n" in list "l" and return it.
4175 * A negative index is counted from the end; -1 is the last item.
4176 * Returns NULL when "n" is out of range.
4177 */
4178 static listitem *
4179list_find(l, n)
4180 listvar *l;
4181 long n;
4182{
4183 listitem *item;
4184 long idx;
4185
4186 if (l == NULL)
4187 return NULL;
4188 if (n < 0)
4189 {
4190 idx = -1; /* search from the end */
4191 for (item = l->lv_last; item != NULL && idx > n; item = item->li_prev)
4192 --idx;
4193 }
4194 else
4195 {
4196 idx = 0; /* search from the start */
4197 for (item = l->lv_first; item != NULL && idx < n; item = item->li_next)
4198 ++idx;
4199 }
4200 if (idx != n)
4201 return NULL;
4202 return item;
4203}
4204
4205/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004206 * Locate "item" list "l" and return its index.
4207 * Returns -1 when "item" is not in the list.
4208 */
4209 static long
4210list_idx_of_item(l, item)
4211 listvar *l;
4212 listitem *item;
4213{
4214 long idx = 0;
4215 listitem *li;
4216
4217 if (l == NULL)
4218 return -1;
4219 idx = 0;
4220 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
4221 ++idx;
4222 if (li == NULL)
4223 return -1;
4224 return idx;;
4225}
4226
4227/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004228 * Like list_find(), but also find an item just past the end.
4229 * "*ip" is the item to find.
4230 * When found "*ip" is set to zero, when not found "*ip" is non-zero.
4231 * Returns NULL when item not found or item is just past the end.
4232 */
4233 static listitem *
4234list_find_ext(l, ip)
4235 listvar *l;
4236 long *ip;
4237{
4238 long n;
4239 listitem *item;
4240
4241 if (*ip < 0)
4242 {
4243 /* Count from the end: -1 is before last item. */
4244 item = l->lv_last;
4245 for (n = *ip + 1; n < 0 && item != NULL; ++n)
4246 item = item->li_prev;
4247 if (item == NULL)
4248 n = 1; /* error! */
4249 }
4250 else
4251 {
4252 item = l->lv_first;
4253 for (n = *ip; n > 0 && item != NULL; --n)
4254 item = item->li_next;
4255 }
4256 *ip = n;
4257 return item;
4258}
4259
4260/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004261 * Append item "item" to the end of list "l".
4262 */
4263 static void
4264list_append(l, item)
4265 listvar *l;
4266 listitem *item;
4267{
4268 if (l->lv_last == NULL)
4269 {
4270 /* empty list */
4271 l->lv_first = item;
4272 l->lv_last = item;
4273 item->li_prev = NULL;
4274 }
4275 else
4276 {
4277 l->lv_last->li_next = item;
4278 item->li_prev = l->lv_last;
4279 l->lv_last = item;
4280 }
4281 item->li_next = NULL;
4282}
4283
4284/*
4285 * Append typeval "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004286 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004287 */
4288 static int
4289list_append_tv(l, tv)
4290 listvar *l;
4291 typeval *tv;
4292{
4293 listitem *ni = listitem_alloc();
4294
4295 if (ni == NULL)
4296 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 copy_tv(tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004298 list_append(l, ni);
4299 return OK;
4300}
4301
4302/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004303 * Insert typeval "tv" in list "l" before "item".
4304 * If "item" is NULL append at the end.
4305 * Return FAIL when out of memory.
4306 */
4307 static int
4308list_insert_tv(l, tv, item)
4309 listvar *l;
4310 typeval *tv;
4311 listitem *item;
4312{
4313 listitem *ni = listitem_alloc();
4314
4315 if (ni == NULL)
4316 return FAIL;
4317 copy_tv(tv, &ni->li_tv);
4318 if (item == NULL)
4319 /* Append new item at end of list. */
4320 list_append(l, ni);
4321 else
4322 {
4323 /* Insert new item before existing item. */
4324 ni->li_prev = item->li_prev;
4325 ni->li_next = item;
4326 if (item->li_prev == NULL)
4327 l->lv_first = ni;
4328 else
4329 item->li_prev->li_next = ni;
4330 item->li_prev = ni;
4331 }
4332 return OK;
4333}
4334
4335/*
4336 * Extend "l1" with "l2".
4337 * If "bef" is NULL append at the end, otherwise insert before this item.
4338 * Returns FAIL when out of memory.
4339 */
4340 static int
4341list_extend(l1, l2, bef)
4342 listvar *l1;
4343 listvar *l2;
4344 listitem *bef;
4345{
4346 listitem *item;
4347
4348 for (item = l2->lv_first; item != NULL; item = item->li_next)
4349 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
4350 return FAIL;
4351 return OK;
4352}
4353
4354/*
4355 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
4356 * Return FAIL when out of memory.
4357 */
4358 static int
4359list_concat(l1, l2, tv)
4360 listvar *l1;
4361 listvar *l2;
4362 typeval *tv;
4363{
4364 listvar *l;
4365
4366 /* make a copy of the first list. */
4367 l = list_copy(l1, FALSE);
4368 if (l == NULL)
4369 return FAIL;
4370 tv->v_type = VAR_LIST;
4371 tv->vval.v_list = l;
4372
4373 /* append all items from the second list */
4374 return list_extend(l, l2, NULL);
4375}
4376
4377/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004378 * Make a copy of list "l". Shallow if "deep" is FALSE.
4379 * The refcount of the new list is set to 1.
4380 * Returns NULL when out of memory.
4381 */
4382 static listvar *
4383list_copy(orig, deep)
4384 listvar *orig;
4385 int deep;
4386{
4387 listvar *copy;
4388 listitem *item;
4389 listitem *ni;
4390 static int recurse = 0;
4391
4392 if (orig == NULL)
4393 return NULL;
4394 if (recurse >= VAR_LIST_MAXNEST)
4395 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004396 EMSG(_("E698: List nested too deep for making a copy"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004397 return NULL;
4398 }
4399 ++recurse;
4400
4401 copy = list_alloc();
4402 if (copy != NULL)
4403 {
4404 for (item = orig->lv_first; item != NULL; item = item->li_next)
4405 {
4406 ni = listitem_alloc();
4407 if (ni == NULL)
4408 break;
4409 if (deep && item->li_tv.v_type == VAR_LIST)
4410 {
4411 ni->li_tv.v_type = VAR_LIST;
4412 ni->li_tv.vval.v_list = list_copy(item->li_tv.vval.v_list,
4413 TRUE);
4414 if (ni->li_tv.vval.v_list == NULL)
4415 {
4416 vim_free(ni);
4417 break;
4418 }
4419 }
4420 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004421 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004422 list_append(copy, ni);
4423 }
4424 ++copy->lv_refcount;
4425 }
4426
4427 --recurse;
4428 return copy;
4429}
4430
4431/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004432 * Remove items "item" to "item2" from list "l".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004433 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004434 static void
4435list_getrem(l, item, item2)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004436 listvar *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004437 listitem *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004438 listitem *item2;
4439{
4440 listitem *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004441
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004442 /* notify watchers */
4443 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004444 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004445 list_fix_watch(l, ip);
4446 if (ip == item2)
4447 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004448 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004449
4450 if (item2->li_next == NULL)
4451 l->lv_last = item->li_prev;
4452 else
4453 item2->li_next->li_prev = item->li_prev;
4454 if (item->li_prev == NULL)
4455 l->lv_first = item2->li_next;
4456 else
4457 item->li_prev->li_next = item2->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004458}
4459
4460/*
4461 * Return an allocated string with the string representation of a list.
4462 * May return NULL.
4463 */
4464 static char_u *
4465list2string(tv)
4466 typeval *tv;
4467{
4468 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004469
4470 if (tv->vval.v_list == NULL)
4471 return NULL;
4472 ga_init2(&ga, (int)sizeof(char), 80);
4473 ga_append(&ga, '[');
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004474 list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004475 ga_append(&ga, ']');
4476 ga_append(&ga, NUL);
4477 return (char_u *)ga.ga_data;
4478}
4479
4480/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004481 * Join list "l" into a string in "*gap", using separator "sep".
4482 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
4483 */
4484 static void
4485list_join(gap, l, sep, echo)
4486 garray_T *gap;
4487 listvar *l;
4488 char_u *sep;
4489 int echo;
4490{
4491 int first = TRUE;
4492 char_u *tofree;
4493 char_u numbuf[NUMBUFLEN];
4494 listitem *item;
4495 char_u *s;
4496
4497 for (item = l->lv_first; item != NULL; item = item->li_next)
4498 {
4499 if (first)
4500 first = FALSE;
4501 else
4502 ga_concat(gap, sep);
4503
4504 if (echo)
4505 s = echo_string(&item->li_tv, &tofree, numbuf);
4506 else
4507 s = tv2string(&item->li_tv, &tofree, numbuf);
4508 if (s != NULL)
4509 ga_concat(gap, s);
4510 vim_free(tofree);
4511 }
4512}
4513
4514/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004515 * Allocate an empty header for a dictionary.
4516 */
4517 static dictvar *
4518dict_alloc()
4519{
4520 return (dictvar *)alloc_clear(sizeof(dictvar));
4521}
4522
4523/*
4524 * Unreference a Dictionary: decrement the reference count and free it when it
4525 * becomes zero.
4526 */
4527 static void
4528dict_unref(d)
4529 dictvar *d;
4530{
4531 if (d != NULL && --d->dv_refcount <= 0)
4532 dict_free(d);
4533}
4534
4535/*
4536 * Free a Dictionary, including all items it contains.
4537 * Ignores the reference count.
4538 */
4539 static void
4540dict_free(d)
4541 dictvar *d;
4542{
4543 dictitem *item;
4544 dictitem *next;
4545
4546 for (item = d->dv_first; item != NULL; item = next)
4547 {
4548 next = item->di_next;
4549 dictitem_free(item);
4550 }
4551 vim_free(d);
4552}
4553
4554/*
4555 * Allocate a Dictionary item.
4556 */
4557 static dictitem *
4558dictitem_alloc()
4559{
4560 return (dictitem *)alloc(sizeof(dictitem));
4561}
4562
4563/*
4564 * Free a dict item. Also clears the value.
4565 */
4566 static void
4567dictitem_free(item)
4568 dictitem *item;
4569{
4570 vim_free(item->di_key);
4571 clear_tv(&item->di_tv);
4572 vim_free(item);
4573}
4574
4575/*
4576 * Add item "item" to Dictionary "d".
4577 */
4578 static void
4579dict_add(d, item)
4580 dictvar *d;
4581 dictitem *item;
4582{
4583 item->di_next = d->dv_first;
4584 d->dv_first = item;
4585}
4586
4587#if 0 /* not currently used */
4588static void dict_set_item __ARGS((dictvar *d, int type, char *key, void *val));
4589
4590/*
4591 * Add an item to Dictionary "d" with type "type", key "key" and value "val".
4592 * If it already exists it is overwritten.
4593 * The key and value are copied to allocated memory.
4594 */
4595 static void
4596dict_set_item(d, type, key, val)
4597 dictvar *d;
4598 int type;
4599 char *key;
4600 void *val;
4601{
4602 dictitem *di;
4603 char_u *dkey;
4604
4605 di = dict_find(d, (char_u *)key, -1);
4606 if (di == NULL)
4607 {
4608 dkey = vim_strsave((char_u *)key);
4609 if (dkey != NULL)
4610 {
4611 di = dictitem_alloc();
4612 if (di == NULL)
4613 vim_free(dkey);
4614 else
4615 di->di_key = dkey;
4616 }
4617 }
4618 else
4619 clear_tv(&di->di_tv);
4620
4621 if (di != NULL)
4622 {
4623 di->di_tv.v_type = type;
4624 switch (type)
4625 {
4626 case VAR_NUMBER:
4627 di->di_tv.vval.v_number = (varnumber_T)val;
4628 break;
4629 case VAR_FUNC:
4630 case VAR_STRING:
4631 di->di_tv.vval.v_string = vim_strsave((char_u *)val);
4632 break;
4633 default:
4634 EMSG2(_(e_intern2), "dict_set_item()");
4635 }
4636 dict_add(d, di);
4637 }
4638}
4639#endif
4640
4641/*
4642 * Find item "key[len]" in Dictionary "d".
4643 * If "len" is negative use strlen(key).
4644 * Returns NULL when not found.
4645 */
4646 static dictitem *
4647dict_find(d, key, len)
4648 dictvar *d;
4649 char_u *key;
4650 int len;
4651{
4652 static dictitem *di;
4653
4654 for (di = d->dv_first; di != NULL; di = di->di_next)
4655 if (len < 0
4656 ? STRCMP(di->di_key, key) == 0
4657 : STRNCMP(di->di_key, key, len) == 0 && di->di_key[len] == NUL)
4658 return di;
4659 return NULL;
4660}
4661
4662/*
4663 * Return an allocated string with the string representation of a Dictionary.
4664 * May return NULL.
4665 */
4666 static char_u *
4667dict2string(tv)
4668 typeval *tv;
4669{
4670 garray_T ga;
4671 int first = TRUE;
4672 char_u *tofree;
4673 char_u numbuf[NUMBUFLEN];
4674 dictitem *item;
4675 char_u *s;
4676
4677 if (tv->vval.v_dict == NULL)
4678 return NULL;
4679 ga_init2(&ga, (int)sizeof(char), 80);
4680 ga_append(&ga, '{');
4681
4682 for (item = tv->vval.v_dict->dv_first; item != NULL; item = item->di_next)
4683 {
4684 if (first)
4685 first = FALSE;
4686 else
4687 ga_concat(&ga, (char_u *)", ");
4688
4689 tofree = string_quote(item->di_key, FALSE);
4690 if (tofree != NULL)
4691 {
4692 ga_concat(&ga, tofree);
4693 vim_free(tofree);
4694 }
4695 ga_concat(&ga, (char_u *)": ");
4696 s = tv2string(&item->di_tv, &tofree, numbuf);
4697 if (s != NULL)
4698 ga_concat(&ga, s);
4699 vim_free(tofree);
4700 }
4701
4702 ga_append(&ga, '}');
4703 ga_append(&ga, NUL);
4704 return (char_u *)ga.ga_data;
4705}
4706
4707/*
4708 * Allocate a variable for a Dictionary and fill it from "*arg".
4709 * Return OK or FAIL. Returns NOTDONE for {expr}.
4710 */
4711 static int
4712get_dict_tv(arg, rettv, evaluate)
4713 char_u **arg;
4714 typeval *rettv;
4715 int evaluate;
4716{
4717 dictvar *d = NULL;
4718 typeval tv;
4719 char_u *key;
4720 dictitem *item;
4721 char_u *start = skipwhite(*arg + 1);
4722
4723 /*
4724 * First check if it's not a curly-braces thing: {expr}.
4725 * Must do this without evaluating, otherwise a function may be called
4726 * twice. Unfortunately this means we need to call eval1() twice for the
4727 * first item.
4728 */
4729 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
4730 return FAIL;
4731 if (*start == '}')
4732 return NOTDONE;
4733
4734 if (evaluate)
4735 {
4736 d = dict_alloc();
4737 if (d == NULL)
4738 return FAIL;
4739 }
4740
4741 *arg = skipwhite(*arg + 1);
4742 while (**arg != '}' && **arg != NUL)
4743 {
4744 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4745 goto failret;
4746 if (**arg != ':')
4747 {
4748 EMSG2(_("E999: Missing colon in Dictionary: %s"), *arg);
4749 clear_tv(&tv);
4750 goto failret;
4751 }
4752 key = get_tv_string(&tv);
4753 if (*key == NUL)
4754 {
4755 EMSG(_(e_emptykey));
4756 clear_tv(&tv);
4757 goto failret;
4758 }
4759 key = vim_strsave(key);
4760 clear_tv(&tv);
4761 if (key == NULL)
4762 goto failret;
4763
4764 *arg = skipwhite(*arg + 1);
4765 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
4766 {
4767 vim_free(key);
4768 goto failret;
4769 }
4770 if (evaluate)
4771 {
4772 item = dict_find(d, key, -1);
4773 if (item != NULL)
4774 {
4775 EMSG(_("E999: Duplicate key in Dictionary"));
4776 vim_free(key);
4777 clear_tv(&tv);
4778 goto failret;
4779 }
4780 item = dictitem_alloc();
4781 if (item == NULL)
4782 vim_free(key);
4783 else
4784 {
4785 item->di_key = key;
4786 item->di_tv = tv;
4787 dict_add(d, item);
4788 }
4789 }
4790
4791 if (**arg == '}')
4792 break;
4793 if (**arg != ',')
4794 {
4795 EMSG2(_("E999: Missing comma in Dictionary: %s"), *arg);
4796 goto failret;
4797 }
4798 *arg = skipwhite(*arg + 1);
4799 }
4800
4801 if (**arg != '}')
4802 {
4803 EMSG2(_("E999: Missing end of Dictionary '}': %s"), *arg);
4804failret:
4805 if (evaluate)
4806 dict_free(d);
4807 return FAIL;
4808 }
4809
4810 *arg = skipwhite(*arg + 1);
4811 if (evaluate)
4812 {
4813 rettv->v_type = VAR_DICT;
4814 rettv->vval.v_dict = d;
4815 ++d->dv_refcount;
4816 }
4817
4818 return OK;
4819}
4820
4821
4822/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004823 * Return a string with the string representation of a variable.
4824 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004825 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004826 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004827 * May return NULL;
4828 */
4829 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004830echo_string(tv, tofree, numbuf)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004831 typeval *tv;
4832 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004833 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004834{
4835 switch (tv->v_type)
4836 {
4837 case VAR_FUNC:
4838 *tofree = NULL;
4839 return tv->vval.v_string;
4840 case VAR_LIST:
4841 *tofree = list2string(tv);
4842 return *tofree;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004843 case VAR_DICT:
4844 *tofree = dict2string(tv);
4845 return *tofree;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004846 case VAR_STRING:
4847 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004848 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004849 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004850 EMSG2(_(e_intern2), "echo_string()");
4851 }
4852 *tofree = NULL;
4853 return get_tv_string_buf(tv, numbuf);
4854}
4855
4856/*
4857 * Return a string with the string representation of a variable.
4858 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4859 * "numbuf" is used for a number.
4860 * Puts quotes around strings, so that they can be parsed back by eval().
4861 * May return NULL;
4862 */
4863 static char_u *
4864tv2string(tv, tofree, numbuf)
4865 typeval *tv;
4866 char_u **tofree;
4867 char_u *numbuf;
4868{
4869 switch (tv->v_type)
4870 {
4871 case VAR_NUMBER:
4872 break;
4873 case VAR_FUNC:
4874 *tofree = string_quote(tv->vval.v_string, TRUE);
4875 return *tofree;
4876 case VAR_STRING:
4877 *tofree = string_quote(tv->vval.v_string, FALSE);
4878 return *tofree;
4879 case VAR_LIST:
4880 *tofree = list2string(tv);
4881 return *tofree;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004882 case VAR_DICT:
4883 *tofree = dict2string(tv);
4884 return *tofree;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004885 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004886 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004887 }
4888 *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004889 return get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004890}
4891
4892/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004893 * Return a string in ' quotes, doubling ' characters.
4894 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004895 */
4896 static char_u *
4897string_quote(str, function)
4898 char_u *str;
4899 int function;
4900{
4901 unsigned len = function ? 13 : 3;
4902 char_u *p, *r, *s;
4903
4904 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar8c711452005-01-14 21:53:12 +00004905 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004906 ++len;
4907 s = r = alloc(len);
4908 if (r != NULL)
4909 {
4910 if (function)
4911 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004912 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004913 r += 10;
4914 }
4915 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004916 *r++ = '\'';
4917 for (p = str; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004918 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004919 if (*p == '\'')
4920 *r++ = '\'';
4921 MB_COPY_CHAR(p, r);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004922 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004923 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004924 if (function)
4925 *r++ = ')';
4926 *r++ = NUL;
4927 }
4928 return s;
4929}
4930
4931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 * Get the value of an environment variable.
4933 * "arg" is pointing to the '$'. It is advanced to after the name.
4934 * If the environment variable was not set, silently assume it is empty.
4935 * Always return OK.
4936 */
4937 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004938get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004940 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 int evaluate;
4942{
4943 char_u *string = NULL;
4944 int len;
4945 int cc;
4946 char_u *name;
4947
4948 ++*arg;
4949 name = *arg;
4950 len = get_env_len(arg);
4951 if (evaluate)
4952 {
4953 if (len != 0)
4954 {
4955 cc = name[len];
4956 name[len] = NUL;
4957 /* first try mch_getenv(), fast for normal environment vars */
4958 string = mch_getenv(name);
4959 if (string != NULL && *string != NUL)
4960 string = vim_strsave(string);
4961 else
4962 {
4963 /* next try expanding things like $VIM and ${HOME} */
4964 string = expand_env_save(name - 1);
4965 if (string != NULL && *string == '$')
4966 {
4967 vim_free(string);
4968 string = NULL;
4969 }
4970 }
4971 name[len] = cc;
4972 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004973 rettv->v_type = VAR_STRING;
4974 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 }
4976
4977 return OK;
4978}
4979
4980/*
4981 * Array with names and number of arguments of all internal functions
4982 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
4983 */
4984static struct fst
4985{
4986 char *f_name; /* function name */
4987 char f_min_argc; /* minimal number of arguments */
4988 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004989 void (*f_func) __ARGS((typeval *args, typeval *rvar));
4990 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991} functions[] =
4992{
Bram Moolenaar0d660222005-01-07 21:51:51 +00004993 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 {"append", 2, 2, f_append},
4995 {"argc", 0, 0, f_argc},
4996 {"argidx", 0, 0, f_argidx},
4997 {"argv", 1, 1, f_argv},
4998 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004999 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {"bufexists", 1, 1, f_bufexists},
5001 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
5002 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
5003 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
5004 {"buflisted", 1, 1, f_buflisted},
5005 {"bufloaded", 1, 1, f_bufloaded},
5006 {"bufname", 1, 1, f_bufname},
5007 {"bufnr", 1, 1, f_bufnr},
5008 {"bufwinnr", 1, 1, f_bufwinnr},
5009 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005010 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005011 {"call", 2, 2, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {"char2nr", 1, 1, f_char2nr},
5013 {"cindent", 1, 1, f_cindent},
5014 {"col", 1, 1, f_col},
5015 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005016 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005017 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 {"cscope_connection",0,3, f_cscope_connection},
5019 {"cursor", 2, 2, f_cursor},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005020 {"deepcopy", 1, 1, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 {"delete", 1, 1, f_delete},
5022 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00005023 {"diff_filler", 1, 1, f_diff_filler},
5024 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00005025 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005027 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 {"eventhandler", 0, 0, f_eventhandler},
5029 {"executable", 1, 1, f_executable},
5030 {"exists", 1, 1, f_exists},
5031 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005032 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
5034 {"filereadable", 1, 1, f_filereadable},
5035 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005036 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005037 {"finddir", 1, 3, f_finddir},
5038 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 {"fnamemodify", 2, 2, f_fnamemodify},
5040 {"foldclosed", 1, 1, f_foldclosed},
5041 {"foldclosedend", 1, 1, f_foldclosedend},
5042 {"foldlevel", 1, 1, f_foldlevel},
5043 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005044 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005046 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005047 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 {"getbufvar", 2, 2, f_getbufvar},
5049 {"getchar", 0, 1, f_getchar},
5050 {"getcharmod", 0, 0, f_getcharmod},
5051 {"getcmdline", 0, 0, f_getcmdline},
5052 {"getcmdpos", 0, 0, f_getcmdpos},
5053 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00005054 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005055 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 {"getfsize", 1, 1, f_getfsize},
5057 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005058 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005059 {"getline", 1, 2, f_getline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 {"getreg", 0, 1, f_getreg},
5061 {"getregtype", 0, 1, f_getregtype},
5062 {"getwinposx", 0, 0, f_getwinposx},
5063 {"getwinposy", 0, 0, f_getwinposy},
5064 {"getwinvar", 2, 2, f_getwinvar},
5065 {"glob", 1, 1, f_glob},
5066 {"globpath", 2, 2, f_globpath},
5067 {"has", 1, 1, f_has},
5068 {"hasmapto", 1, 2, f_hasmapto},
5069 {"highlightID", 1, 1, f_hlID}, /* obsolete */
5070 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
5071 {"histadd", 2, 2, f_histadd},
5072 {"histdel", 1, 2, f_histdel},
5073 {"histget", 1, 2, f_histget},
5074 {"histnr", 1, 1, f_histnr},
5075 {"hlID", 1, 1, f_hlID},
5076 {"hlexists", 1, 1, f_hlexists},
5077 {"hostname", 0, 0, f_hostname},
5078 {"iconv", 3, 3, f_iconv},
5079 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005080 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {"input", 1, 2, f_input},
5082 {"inputdialog", 1, 3, f_inputdialog},
5083 {"inputrestore", 0, 0, f_inputrestore},
5084 {"inputsave", 0, 0, f_inputsave},
5085 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005086 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005088 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005089 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005090 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005092 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 {"libcall", 3, 3, f_libcall},
5094 {"libcallnr", 3, 3, f_libcallnr},
5095 {"line", 1, 1, f_line},
5096 {"line2byte", 1, 1, f_line2byte},
5097 {"lispindent", 1, 1, f_lispindent},
5098 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005099 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {"maparg", 1, 2, f_maparg},
5101 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005102 {"match", 2, 4, f_match},
5103 {"matchend", 2, 4, f_matchend},
5104 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00005105 {"max", 1, 1, f_max},
5106 {"min", 1, 1, f_min},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {"mode", 0, 0, f_mode},
5108 {"nextnonblank", 1, 1, f_nextnonblank},
5109 {"nr2char", 1, 1, f_nr2char},
5110 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005111 {"range", 1, 3, f_range},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {"remote_expr", 2, 3, f_remote_expr},
5113 {"remote_foreground", 1, 1, f_remote_foreground},
5114 {"remote_peek", 1, 2, f_remote_peek},
5115 {"remote_read", 1, 1, f_remote_read},
5116 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005117 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005119 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005121 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 {"search", 1, 2, f_search},
5123 {"searchpair", 3, 5, f_searchpair},
5124 {"server2client", 2, 2, f_server2client},
5125 {"serverlist", 0, 0, f_serverlist},
5126 {"setbufvar", 3, 3, f_setbufvar},
5127 {"setcmdpos", 1, 1, f_setcmdpos},
5128 {"setline", 2, 2, f_setline},
5129 {"setreg", 2, 3, f_setreg},
5130 {"setwinvar", 3, 3, f_setwinvar},
5131 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00005132 {"sort", 1, 2, f_sort},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005133 {"split", 1, 2, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134#ifdef HAVE_STRFTIME
5135 {"strftime", 1, 2, f_strftime},
5136#endif
5137 {"stridx", 2, 2, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005138 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 {"strlen", 1, 1, f_strlen},
5140 {"strpart", 2, 3, f_strpart},
5141 {"strridx", 2, 2, f_strridx},
5142 {"strtrans", 1, 1, f_strtrans},
5143 {"submatch", 1, 1, f_submatch},
5144 {"substitute", 4, 4, f_substitute},
5145 {"synID", 3, 3, f_synID},
5146 {"synIDattr", 2, 3, f_synIDattr},
5147 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005148 {"system", 1, 2, f_system},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 {"tempname", 0, 0, f_tempname},
5150 {"tolower", 1, 1, f_tolower},
5151 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00005152 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 {"type", 1, 1, f_type},
Bram Moolenaar8c711452005-01-14 21:53:12 +00005154 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 {"virtcol", 1, 1, f_virtcol},
5156 {"visualmode", 0, 1, f_visualmode},
5157 {"winbufnr", 1, 1, f_winbufnr},
5158 {"wincol", 0, 0, f_wincol},
5159 {"winheight", 1, 1, f_winheight},
5160 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005161 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 {"winrestcmd", 0, 0, f_winrestcmd},
5163 {"winwidth", 1, 1, f_winwidth},
5164};
5165
5166#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5167
5168/*
5169 * Function given to ExpandGeneric() to obtain the list of internal
5170 * or user defined function names.
5171 */
5172 char_u *
5173get_function_name(xp, idx)
5174 expand_T *xp;
5175 int idx;
5176{
5177 static int intidx = -1;
5178 char_u *name;
5179
5180 if (idx == 0)
5181 intidx = -1;
5182 if (intidx < 0)
5183 {
5184 name = get_user_func_name(xp, idx);
5185 if (name != NULL)
5186 return name;
5187 }
5188 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
5189 {
5190 STRCPY(IObuff, functions[intidx].f_name);
5191 STRCAT(IObuff, "(");
5192 if (functions[intidx].f_max_argc == 0)
5193 STRCAT(IObuff, ")");
5194 return IObuff;
5195 }
5196
5197 return NULL;
5198}
5199
5200/*
5201 * Function given to ExpandGeneric() to obtain the list of internal or
5202 * user defined variable or function names.
5203 */
5204/*ARGSUSED*/
5205 char_u *
5206get_expr_name(xp, idx)
5207 expand_T *xp;
5208 int idx;
5209{
5210 static int intidx = -1;
5211 char_u *name;
5212
5213 if (idx == 0)
5214 intidx = -1;
5215 if (intidx < 0)
5216 {
5217 name = get_function_name(xp, idx);
5218 if (name != NULL)
5219 return name;
5220 }
5221 return get_user_var_name(xp, ++intidx);
5222}
5223
5224#endif /* FEAT_CMDL_COMPL */
5225
5226/*
5227 * Find internal function in table above.
5228 * Return index, or -1 if not found
5229 */
5230 static int
5231find_internal_func(name)
5232 char_u *name; /* name of the function */
5233{
5234 int first = 0;
5235 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
5236 int cmp;
5237 int x;
5238
5239 /*
5240 * Find the function name in the table. Binary search.
5241 */
5242 while (first <= last)
5243 {
5244 x = first + ((unsigned)(last - first) >> 1);
5245 cmp = STRCMP(name, functions[x].f_name);
5246 if (cmp < 0)
5247 last = x - 1;
5248 else if (cmp > 0)
5249 first = x + 1;
5250 else
5251 return x;
5252 }
5253 return -1;
5254}
5255
5256/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
5258 * name it contains, otherwise return "name".
5259 */
5260 static char_u *
5261deref_func_name(name, lenp)
5262 char_u *name;
5263 int *lenp;
5264{
5265 VAR v;
5266 int cc;
5267
5268 cc = name[*lenp];
5269 name[*lenp] = NUL;
5270 v = find_var(name, FALSE);
5271 name[*lenp] = cc;
5272 if (v != NULL && v->tv.v_type == VAR_FUNC)
5273 {
5274 if (v->tv.vval.v_string == NULL)
5275 {
5276 *lenp = 0;
5277 return (char_u *)""; /* just in case */
5278 }
5279 *lenp = STRLEN(v->tv.vval.v_string);
5280 return v->tv.vval.v_string;
5281 }
5282
5283 return name;
5284}
5285
5286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 * Allocate a variable for the result of a function.
5288 * Return OK or FAIL.
5289 */
5290 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005291get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 char_u *name; /* name of the function */
5293 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005294 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 char_u **arg; /* argument, pointing to the '(' */
5296 linenr_T firstline; /* first line of range */
5297 linenr_T lastline; /* last line of range */
5298 int *doesrange; /* return: function handled range */
5299 int evaluate;
5300{
5301 char_u *argp;
5302 int ret = OK;
5303#define MAX_FUNC_ARGS 20
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005304 typeval argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 int argcount = 0; /* number of arguments found */
5306
5307 /*
5308 * Get the arguments.
5309 */
5310 argp = *arg;
5311 while (argcount < MAX_FUNC_ARGS)
5312 {
5313 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
5314 if (*argp == ')' || *argp == ',' || *argp == NUL)
5315 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
5317 {
5318 ret = FAIL;
5319 break;
5320 }
5321 ++argcount;
5322 if (*argp != ',')
5323 break;
5324 }
5325 if (*argp == ')')
5326 ++argp;
5327 else
5328 ret = FAIL;
5329
5330 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005331 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 firstline, lastline, doesrange, evaluate);
5333 else if (!aborting())
5334 EMSG2(_("E116: Invalid arguments for function %s"), name);
5335
5336 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005337 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338
5339 *arg = skipwhite(argp);
5340 return ret;
5341}
5342
5343
5344/*
5345 * Call a function with its resolved parameters
5346 * Return OK or FAIL.
5347 */
5348 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005349call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 doesrange, evaluate)
5351 char_u *name; /* name of the function */
5352 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005353 typeval *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 int argcount; /* number of "argvars" */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005355 typeval *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 linenr_T firstline; /* first line of range */
5357 linenr_T lastline; /* last line of range */
5358 int *doesrange; /* return: function handled range */
5359 int evaluate;
5360{
5361 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362#define ERROR_UNKNOWN 0
5363#define ERROR_TOOMANY 1
5364#define ERROR_TOOFEW 2
5365#define ERROR_SCRIPT 3
5366#define ERROR_NONE 4
5367#define ERROR_OTHER 5
5368 int error = ERROR_NONE;
5369 int i;
5370 int llen;
5371 ufunc_T *fp;
5372 int cc;
5373#define FLEN_FIXED 40
5374 char_u fname_buf[FLEN_FIXED + 1];
5375 char_u *fname;
5376
5377 /*
5378 * In a script change <SID>name() and s:name() to K_SNR 123_name().
5379 * Change <SNR>123_name() to K_SNR 123_name().
5380 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
5381 */
5382 cc = name[len];
5383 name[len] = NUL;
5384 llen = eval_fname_script(name);
5385 if (llen > 0)
5386 {
5387 fname_buf[0] = K_SPECIAL;
5388 fname_buf[1] = KS_EXTRA;
5389 fname_buf[2] = (int)KE_SNR;
5390 i = 3;
5391 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
5392 {
5393 if (current_SID <= 0)
5394 error = ERROR_SCRIPT;
5395 else
5396 {
5397 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
5398 i = (int)STRLEN(fname_buf);
5399 }
5400 }
5401 if (i + STRLEN(name + llen) < FLEN_FIXED)
5402 {
5403 STRCPY(fname_buf + i, name + llen);
5404 fname = fname_buf;
5405 }
5406 else
5407 {
5408 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
5409 if (fname == NULL)
5410 error = ERROR_OTHER;
5411 else
5412 {
5413 mch_memmove(fname, fname_buf, (size_t)i);
5414 STRCPY(fname + i, name + llen);
5415 }
5416 }
5417 }
5418 else
5419 fname = name;
5420
5421 *doesrange = FALSE;
5422
5423
5424 /* execute the function if no errors detected and executing */
5425 if (evaluate && error == ERROR_NONE)
5426 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005427 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 error = ERROR_UNKNOWN;
5429
5430 if (!ASCII_ISLOWER(fname[0]))
5431 {
5432 /*
5433 * User defined function.
5434 */
5435 fp = find_func(fname);
5436#ifdef FEAT_AUTOCMD
5437 if (fp == NULL && apply_autocmds(EVENT_FUNCUNDEFINED,
5438 fname, fname, TRUE, NULL)
5439#ifdef FEAT_EVAL
5440 && !aborting()
5441#endif
5442 )
5443 {
5444 /* executed an autocommand, search for function again */
5445 fp = find_func(fname);
5446 }
5447#endif
5448 if (fp != NULL)
5449 {
5450 if (fp->flags & FC_RANGE)
5451 *doesrange = TRUE;
5452 if (argcount < fp->args.ga_len)
5453 error = ERROR_TOOFEW;
5454 else if (!fp->varargs && argcount > fp->args.ga_len)
5455 error = ERROR_TOOMANY;
5456 else
5457 {
5458 /*
5459 * Call the user function.
5460 * Save and restore search patterns, script variables and
5461 * redo buffer.
5462 */
5463 save_search_patterns();
5464 saveRedobuff();
5465 ++fp->calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005466 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 firstline, lastline);
5468 --fp->calls;
5469 restoreRedobuff();
5470 restore_search_patterns();
5471 error = ERROR_NONE;
5472 }
5473 }
5474 }
5475 else
5476 {
5477 /*
5478 * Find the function name in the table, call its implementation.
5479 */
5480 i = find_internal_func(fname);
5481 if (i >= 0)
5482 {
5483 if (argcount < functions[i].f_min_argc)
5484 error = ERROR_TOOFEW;
5485 else if (argcount > functions[i].f_max_argc)
5486 error = ERROR_TOOMANY;
5487 else
5488 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005490 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 error = ERROR_NONE;
5492 }
5493 }
5494 }
5495 /*
5496 * The function call (or "FuncUndefined" autocommand sequence) might
5497 * have been aborted by an error, an interrupt, or an explicitly thrown
5498 * exception that has not been caught so far. This situation can be
5499 * tested for by calling aborting(). For an error in an internal
5500 * function or for the "E132" error in call_user_func(), however, the
5501 * throw point at which the "force_abort" flag (temporarily reset by
5502 * emsg()) is normally updated has not been reached yet. We need to
5503 * update that flag first to make aborting() reliable.
5504 */
5505 update_force_abort();
5506 }
5507 if (error == ERROR_NONE)
5508 ret = OK;
5509
5510 /*
5511 * Report an error unless the argument evaluation or function call has been
5512 * cancelled due to an aborting error, an interrupt, or an exception.
5513 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005514 if (!aborting())
5515 {
5516 switch (error)
5517 {
5518 case ERROR_UNKNOWN:
5519 EMSG2(_("E117: Unknown function: %s"), name);
5520 break;
5521 case ERROR_TOOMANY:
5522 EMSG2(_(e_toomanyarg), name);
5523 break;
5524 case ERROR_TOOFEW:
5525 EMSG2(_("E119: Not enough arguments for function: %s"),
5526 name);
5527 break;
5528 case ERROR_SCRIPT:
5529 EMSG2(_("E120: Using <SID> not in a script context: %s"),
5530 name);
5531 break;
5532 }
5533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534
5535 name[len] = cc;
5536 if (fname != name && fname != fname_buf)
5537 vim_free(fname);
5538
5539 return ret;
5540}
5541
5542/*********************************************
5543 * Implementation of the built-in functions
5544 */
5545
5546/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00005547 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 */
5549 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00005550f_add(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005551 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005554 listvar *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005556 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005557 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005559 l = argvars[0].vval.v_list;
5560 if (l != NULL && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005562 }
5563 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00005564 EMSG(_(e_listreq));
5565}
5566
5567/*
5568 * "append(lnum, string/list)" function
5569 */
5570 static void
5571f_append(argvars, rettv)
5572 typeval *argvars;
5573 typeval *rettv;
5574{
5575 long lnum;
5576 listvar *l = NULL;
5577 listitem *li = NULL;
5578 typeval *tv;
5579 long added = 0;
5580
5581 rettv->vval.v_number = 1; /* Default: Failed */
5582 lnum = get_tv_lnum(argvars);
5583 if (lnum >= 0
5584 && lnum <= curbuf->b_ml.ml_line_count
5585 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005586 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00005587 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005588 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00005589 l = argvars[1].vval.v_list;
5590 if (l == NULL)
5591 return;
5592 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005593 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00005594 for (;;)
5595 {
5596 if (l == NULL)
5597 tv = &argvars[1]; /* append a string */
5598 else if (li == NULL)
5599 break; /* end of list */
5600 else
5601 tv = &li->li_tv; /* append item from list */
5602 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
5603 ++added;
5604 if (l == NULL)
5605 break;
5606 li = li->li_next;
5607 }
5608
5609 appended_lines_mark(lnum, added);
5610 if (curwin->w_cursor.lnum > lnum)
5611 curwin->w_cursor.lnum += added;
5612 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
5614}
5615
5616/*
5617 * "argc()" function
5618 */
5619/* ARGSUSED */
5620 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005621f_argc(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005622 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005623 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005625 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626}
5627
5628/*
5629 * "argidx()" function
5630 */
5631/* ARGSUSED */
5632 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005633f_argidx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005634 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005635 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005637 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638}
5639
5640/*
5641 * "argv(nr)" function
5642 */
5643 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005644f_argv(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005645 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005646 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647{
5648 int idx;
5649
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005650 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005654 rettv->vval.v_string = NULL;
5655 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656}
5657
5658/*
5659 * "browse(save, title, initdir, default)" function
5660 */
5661/* ARGSUSED */
5662 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005663f_browse(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005664 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005665 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666{
5667#ifdef FEAT_BROWSE
5668 int save;
5669 char_u *title;
5670 char_u *initdir;
5671 char_u *defname;
5672 char_u buf[NUMBUFLEN];
5673 char_u buf2[NUMBUFLEN];
5674
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005675 save = get_tv_number(&argvars[0]);
5676 title = get_tv_string(&argvars[1]);
5677 initdir = get_tv_string_buf(&argvars[2], buf);
5678 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005680 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005681 do_browse(save ? BROWSE_SAVE : 0,
5682 title, defname, NULL, initdir, NULL, curbuf);
5683#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005684 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005685#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005686 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005687}
5688
5689/*
5690 * "browsedir(title, initdir)" function
5691 */
5692/* ARGSUSED */
5693 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005694f_browsedir(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005695 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005696 typeval *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005697{
5698#ifdef FEAT_BROWSE
5699 char_u *title;
5700 char_u *initdir;
5701 char_u buf[NUMBUFLEN];
5702
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005703 title = get_tv_string(&argvars[0]);
5704 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005705
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005706 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005707 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005709 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005711 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712}
5713
Bram Moolenaar0d660222005-01-07 21:51:51 +00005714static buf_T *find_buffer __ARGS((typeval *avar));
5715
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716/*
5717 * Find a buffer by number or exact name.
5718 */
5719 static buf_T *
5720find_buffer(avar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005721 typeval *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722{
5723 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005725 if (avar->v_type == VAR_NUMBER)
5726 buf = buflist_findnr((int)avar->vval.v_number);
5727 else if (avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005729 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005730 if (buf == NULL)
5731 {
5732 /* No full path name match, try a match with a URL or a "nofile"
5733 * buffer, these don't use the full path. */
5734 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5735 if (buf->b_fname != NULL
5736 && (path_with_url(buf->b_fname)
5737#ifdef FEAT_QUICKFIX
5738 || bt_nofile(buf)
5739#endif
5740 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005741 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005742 break;
5743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 }
5745 return buf;
5746}
5747
5748/*
5749 * "bufexists(expr)" function
5750 */
5751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005752f_bufexists(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005753 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005754 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005756 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757}
5758
5759/*
5760 * "buflisted(expr)" function
5761 */
5762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005763f_buflisted(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005764 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005765 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766{
5767 buf_T *buf;
5768
5769 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005770 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771}
5772
5773/*
5774 * "bufloaded(expr)" function
5775 */
5776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005777f_bufloaded(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005778 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005779 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780{
5781 buf_T *buf;
5782
5783 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005784 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785}
5786
Bram Moolenaar0d660222005-01-07 21:51:51 +00005787static buf_T *get_buf_tv __ARGS((typeval *tv));
5788
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789/*
5790 * Get buffer by number or pattern.
5791 */
5792 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005793get_buf_tv(tv)
5794 typeval *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005796 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 int save_magic;
5798 char_u *save_cpo;
5799 buf_T *buf;
5800
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005801 if (tv->v_type == VAR_NUMBER)
5802 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 if (name == NULL || *name == NUL)
5804 return curbuf;
5805 if (name[0] == '$' && name[1] == NUL)
5806 return lastbuf;
5807
5808 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
5809 save_magic = p_magic;
5810 p_magic = TRUE;
5811 save_cpo = p_cpo;
5812 p_cpo = (char_u *)"";
5813
5814 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
5815 TRUE, FALSE));
5816
5817 p_magic = save_magic;
5818 p_cpo = save_cpo;
5819
5820 /* If not found, try expanding the name, like done for bufexists(). */
5821 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005822 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823
5824 return buf;
5825}
5826
5827/*
5828 * "bufname(expr)" function
5829 */
5830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005831f_bufname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005833 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834{
5835 buf_T *buf;
5836
5837 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005838 buf = get_buf_tv(&argvars[0]);
5839 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005841 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005843 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 --emsg_off;
5845}
5846
5847/*
5848 * "bufnr(expr)" function
5849 */
5850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005851f_bufnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005853 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854{
5855 buf_T *buf;
5856
5857 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005858 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005860 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005862 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 --emsg_off;
5864}
5865
5866/*
5867 * "bufwinnr(nr)" function
5868 */
5869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005870f_bufwinnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005871 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005872 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873{
5874#ifdef FEAT_WINDOWS
5875 win_T *wp;
5876 int winnr = 0;
5877#endif
5878 buf_T *buf;
5879
5880 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005881 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882#ifdef FEAT_WINDOWS
5883 for (wp = firstwin; wp; wp = wp->w_next)
5884 {
5885 ++winnr;
5886 if (wp->w_buffer == buf)
5887 break;
5888 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005889 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005891 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892#endif
5893 --emsg_off;
5894}
5895
5896/*
5897 * "byte2line(byte)" function
5898 */
5899/*ARGSUSED*/
5900 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005901f_byte2line(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005902 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005903 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904{
5905#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005906 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907#else
5908 long boff = 0;
5909
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005910 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005912 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005914 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 (linenr_T)0, &boff);
5916#endif
5917}
5918
5919/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005920 * "byteidx()" function
5921 */
5922/*ARGSUSED*/
5923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005924f_byteidx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005925 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005926 typeval *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005927{
5928#ifdef FEAT_MBYTE
5929 char_u *t;
5930#endif
5931 char_u *str;
5932 long idx;
5933
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005934 str = get_tv_string(&argvars[0]);
5935 idx = get_tv_number(&argvars[1]);
5936 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005937 if (idx < 0)
5938 return;
5939
5940#ifdef FEAT_MBYTE
5941 t = str;
5942 for ( ; idx > 0; idx--)
5943 {
5944 if (*t == NUL) /* EOL reached */
5945 return;
5946 t += mb_ptr2len_check(t);
5947 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005948 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005949#else
5950 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005951 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005952#endif
5953}
5954
5955/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005956 * "call(func, arglist)" function
5957 */
5958 static void
5959f_call(argvars, rettv)
5960 typeval *argvars;
5961 typeval *rettv;
5962{
5963 char_u *func;
5964 typeval argv[MAX_FUNC_ARGS];
5965 int argc = 0;
5966 listitem *item;
5967 int dummy;
5968
5969 rettv->vval.v_number = 0;
5970 if (argvars[1].v_type != VAR_LIST)
5971 {
5972 EMSG(_(e_listreq));
5973 return;
5974 }
5975 if (argvars[1].vval.v_list == NULL)
5976 return;
5977
5978 if (argvars[0].v_type == VAR_FUNC)
5979 func = argvars[0].vval.v_string;
5980 else
5981 func = get_tv_string(&argvars[0]);
5982
5983 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
5984 item = item->li_next)
5985 {
5986 if (argc == MAX_FUNC_ARGS)
5987 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00005988 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005989 break;
5990 }
5991 /* Make a copy of each argument (is this really needed?) */
5992 copy_tv(&item->li_tv, &argv[argc++]);
5993 }
5994
5995 if (item == NULL)
5996 (void)call_func(func, STRLEN(func), rettv, argc, argv,
5997 curwin->w_cursor.lnum, curwin->w_cursor.lnum, &dummy, TRUE);
5998
5999 /* Free the arguments. */
6000 while (argc > 0)
6001 clear_tv(&argv[--argc]);
6002}
6003
6004/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 * "char2nr(string)" function
6006 */
6007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006008f_char2nr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006009 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006010 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011{
6012#ifdef FEAT_MBYTE
6013 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006014 rettv->vval.v_number =
6015 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 else
6017#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006018 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019}
6020
6021/*
6022 * "cindent(lnum)" function
6023 */
6024 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006025f_cindent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006027 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028{
6029#ifdef FEAT_CINDENT
6030 pos_T pos;
6031 linenr_T lnum;
6032
6033 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006034 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6036 {
6037 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006038 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 curwin->w_cursor = pos;
6040 }
6041 else
6042#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006043 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044}
6045
6046/*
6047 * "col(string)" function
6048 */
6049 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006050f_col(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006051 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006052 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053{
6054 colnr_T col = 0;
6055 pos_T *fp;
6056
6057 fp = var2fpos(&argvars[0], FALSE);
6058 if (fp != NULL)
6059 {
6060 if (fp->col == MAXCOL)
6061 {
6062 /* '> can be MAXCOL, get the length of the line then */
6063 if (fp->lnum <= curbuf->b_ml.ml_line_count)
6064 col = STRLEN(ml_get(fp->lnum)) + 1;
6065 else
6066 col = MAXCOL;
6067 }
6068 else
6069 {
6070 col = fp->col + 1;
6071#ifdef FEAT_VIRTUALEDIT
6072 /* col(".") when the cursor is on the NUL at the end of the line
6073 * because of "coladd" can be seen as an extra column. */
6074 if (virtual_active() && fp == &curwin->w_cursor)
6075 {
6076 char_u *p = ml_get_cursor();
6077
6078 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
6079 curwin->w_virtcol - curwin->w_cursor.coladd))
6080 {
6081# ifdef FEAT_MBYTE
6082 int l;
6083
6084 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
6085 col += l;
6086# else
6087 if (*p != NUL && p[1] == NUL)
6088 ++col;
6089# endif
6090 }
6091 }
6092#endif
6093 }
6094 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006095 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096}
6097
6098/*
6099 * "confirm(message, buttons[, default [, type]])" function
6100 */
6101/*ARGSUSED*/
6102 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006103f_confirm(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006104 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006105 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106{
6107#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
6108 char_u *message;
6109 char_u *buttons = NULL;
6110 char_u buf[NUMBUFLEN];
6111 char_u buf2[NUMBUFLEN];
6112 int def = 1;
6113 int type = VIM_GENERIC;
6114 int c;
6115
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006116 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006117 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006119 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006120 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006122 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006123 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 {
6125 /* avoid that TOUPPER_ASC calls get_var_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006126 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 switch (TOUPPER_ASC(c))
6128 {
6129 case 'E': type = VIM_ERROR; break;
6130 case 'Q': type = VIM_QUESTION; break;
6131 case 'I': type = VIM_INFO; break;
6132 case 'W': type = VIM_WARNING; break;
6133 case 'G': type = VIM_GENERIC; break;
6134 }
6135 }
6136 }
6137 }
6138
6139 if (buttons == NULL || *buttons == NUL)
6140 buttons = (char_u *)_("&Ok");
6141
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006142 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 def, NULL);
6144#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006145 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146#endif
6147}
6148
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149/*
6150 * "copy()" function
6151 */
6152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006153f_copy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006154 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006155 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006156{
6157 if (argvars[0].v_type == VAR_LIST)
6158 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006159 rettv->v_type = VAR_LIST;
6160 rettv->vval.v_list = list_copy(argvars[0].vval.v_list, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006161 }
6162 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006163 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006164}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165
6166/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006167 * "count()" function
6168 */
6169 static void
6170f_count(argvars, rettv)
6171 typeval *argvars;
6172 typeval *rettv;
6173{
6174 listitem *li;
6175 long n = 0;
6176 int ic = FALSE;
6177
6178 if (argvars[0].v_type != VAR_LIST)
6179 EMSG(_(e_listreq));
6180 else if (argvars[0].vval.v_list != NULL)
6181 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006182 li = argvars[0].vval.v_list->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006183 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006184 {
6185 for (n = get_tv_number(&argvars[2]); n > 0 && li != NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006186 li = li->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006187 --n;
6188 if (argvars[3].v_type != VAR_UNKNOWN)
6189 ic = get_tv_number(&argvars[3]);
6190 }
6191
6192 n = 0;
6193 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006194 if (tv_equal(&li->li_tv, &argvars[1], ic))
6195 ++n;
6196 }
6197 rettv->vval.v_number = n;
6198}
6199
6200/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
6202 *
6203 * Checks the existence of a cscope connection.
6204 */
6205/*ARGSUSED*/
6206 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006207f_cscope_connection(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006208 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006209 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210{
6211#ifdef FEAT_CSCOPE
6212 int num = 0;
6213 char_u *dbpath = NULL;
6214 char_u *prepend = NULL;
6215 char_u buf[NUMBUFLEN];
6216
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006217 if (argvars[0].v_type != VAR_UNKNOWN
6218 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006220 num = (int)get_tv_number(&argvars[0]);
6221 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006222 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006223 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 }
6225
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006226 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006228 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229#endif
6230}
6231
6232/*
6233 * "cursor(lnum, col)" function
6234 *
6235 * Moves the cursor to the specified line and column
6236 */
6237/*ARGSUSED*/
6238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006239f_cursor(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006240 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006241 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242{
6243 long line, col;
6244
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006245 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 if (line > 0)
6247 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006248 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 if (col > 0)
6250 curwin->w_cursor.col = col - 1;
6251#ifdef FEAT_VIRTUALEDIT
6252 curwin->w_cursor.coladd = 0;
6253#endif
6254
6255 /* Make sure the cursor is in a valid position. */
6256 check_cursor();
6257#ifdef FEAT_MBYTE
6258 /* Correct cursor for multi-byte character. */
6259 if (has_mbyte)
6260 mb_adjust_cursor();
6261#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006262
6263 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264}
6265
6266/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006267 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 */
6269 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006270f_deepcopy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006271 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006272 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006274 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006276 rettv->v_type = VAR_LIST;
6277 rettv->vval.v_list = list_copy(argvars[0].vval.v_list, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006279 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006280 copy_tv(&argvars[0], rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281}
6282
6283/*
6284 * "delete()" function
6285 */
6286 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006287f_delete(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006288 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006289 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
6291 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006292 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006294 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295}
6296
6297/*
6298 * "did_filetype()" function
6299 */
6300/*ARGSUSED*/
6301 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006302f_did_filetype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006303 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006304 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305{
6306#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006307 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006309 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310#endif
6311}
6312
6313/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00006314 * "diff_filler()" function
6315 */
6316/*ARGSUSED*/
6317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006318f_diff_filler(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006319 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006320 typeval *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006321{
6322#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006323 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00006324#endif
6325}
6326
6327/*
6328 * "diff_hlID()" function
6329 */
6330/*ARGSUSED*/
6331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006332f_diff_hlID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006333 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006334 typeval *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006335{
6336#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006337 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00006338 static linenr_T prev_lnum = 0;
6339 static int changedtick = 0;
6340 static int fnum = 0;
6341 static int change_start = 0;
6342 static int change_end = 0;
6343 static enum hlf_value hlID = 0;
6344 int filler_lines;
6345 int col;
6346
6347 if (lnum != prev_lnum
6348 || changedtick != curbuf->b_changedtick
6349 || fnum != curbuf->b_fnum)
6350 {
6351 /* New line, buffer, change: need to get the values. */
6352 filler_lines = diff_check(curwin, lnum);
6353 if (filler_lines < 0)
6354 {
6355 if (filler_lines == -1)
6356 {
6357 change_start = MAXCOL;
6358 change_end = -1;
6359 if (diff_find_change(curwin, lnum, &change_start, &change_end))
6360 hlID = HLF_ADD; /* added line */
6361 else
6362 hlID = HLF_CHD; /* changed line */
6363 }
6364 else
6365 hlID = HLF_ADD; /* added line */
6366 }
6367 else
6368 hlID = (enum hlf_value)0;
6369 prev_lnum = lnum;
6370 changedtick = curbuf->b_changedtick;
6371 fnum = curbuf->b_fnum;
6372 }
6373
6374 if (hlID == HLF_CHD || hlID == HLF_TXD)
6375 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006376 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006377 if (col >= change_start && col <= change_end)
6378 hlID = HLF_TXD; /* changed text */
6379 else
6380 hlID = HLF_CHD; /* changed line */
6381 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006382 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00006383#endif
6384}
6385
6386/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006387 * "empty({expr})" function
6388 */
6389 static void
6390f_empty(argvars, rettv)
6391 typeval *argvars;
6392 typeval *rettv;
6393{
6394 int n;
6395
6396 switch (argvars[0].v_type)
6397 {
6398 case VAR_STRING:
6399 case VAR_FUNC:
6400 n = argvars[0].vval.v_string == NULL
6401 || *argvars[0].vval.v_string == NUL;
6402 break;
6403 case VAR_NUMBER:
6404 n = argvars[0].vval.v_number == 0;
6405 break;
6406 case VAR_LIST:
6407 n = argvars[0].vval.v_list == NULL
6408 || argvars[0].vval.v_list->lv_first == NULL;
6409 break;
6410 default:
6411 EMSG2(_(e_intern2), "f_empty()");
6412 n = 0;
6413 }
6414
6415 rettv->vval.v_number = n;
6416}
6417
6418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 * "escape({string}, {chars})" function
6420 */
6421 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006422f_escape(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006424 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425{
6426 char_u buf[NUMBUFLEN];
6427
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006428 rettv->vval.v_string =
6429 vim_strsave_escaped(get_tv_string(&argvars[0]),
6430 get_tv_string_buf(&argvars[1], buf));
6431 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432}
6433
6434/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006435 * "eval()" function
6436 */
6437/*ARGSUSED*/
6438 static void
6439f_eval(argvars, rettv)
6440 typeval *argvars;
6441 typeval *rettv;
6442{
6443 char_u *s;
6444
6445 s = get_tv_string(&argvars[0]);
6446 s = skipwhite(s);
6447
6448 if (eval1(&s, rettv, TRUE) == FAIL)
6449 rettv->vval.v_number = 0;
6450 else if (*s != NUL)
6451 EMSG(_(e_trailing));
6452}
6453
6454/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 * "eventhandler()" function
6456 */
6457/*ARGSUSED*/
6458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006459f_eventhandler(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006460 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006461 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006463 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464}
6465
6466/*
6467 * "executable()" function
6468 */
6469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006470f_executable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006471 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006472 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006474 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475}
6476
6477/*
6478 * "exists()" function
6479 */
6480 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006481f_exists(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006482 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006483 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484{
6485 char_u *p;
6486 char_u *name;
6487 int n = FALSE;
6488 int len = 0;
6489
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006490 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 if (*p == '$') /* environment variable */
6492 {
6493 /* first try "normal" environment variables (fast) */
6494 if (mch_getenv(p + 1) != NULL)
6495 n = TRUE;
6496 else
6497 {
6498 /* try expanding things like $VIM and ${HOME} */
6499 p = expand_env_save(p);
6500 if (p != NULL && *p != '$')
6501 n = TRUE;
6502 vim_free(p);
6503 }
6504 }
6505 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006506 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 else if (*p == '*') /* internal or user defined function */
6508 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006509 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 }
6511 else if (*p == ':')
6512 {
6513 n = cmd_exists(p + 1);
6514 }
6515 else if (*p == '#')
6516 {
6517#ifdef FEAT_AUTOCMD
6518 name = p + 1;
6519 p = vim_strchr(name, '#');
6520 if (p != NULL)
6521 n = au_exists(name, p, p + 1);
6522 else
6523 n = au_exists(name, name + STRLEN(name), NULL);
6524#endif
6525 }
6526 else /* internal variable */
6527 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 char_u *expr_start;
6529 char_u *expr_end;
6530 char_u *temp_string = NULL;
6531 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 name = p;
6533
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 /* Find the end of the name. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006535 s = find_name_end(name, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 if (expr_start != NULL)
6537 {
6538 temp_string = make_expanded_name(name, expr_start, expr_end, s);
6539 if (temp_string != NULL)
6540 {
6541 len = STRLEN(temp_string);
6542 name = temp_string;
6543 }
6544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 if (len == 0)
6546 len = get_id_len(&p);
6547 if (len != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006548 n = (get_var_tv(name, len, NULL) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 vim_free(temp_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 }
6552
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006553 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554}
6555
6556/*
6557 * "expand()" function
6558 */
6559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006560f_expand(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006561 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006562 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563{
6564 char_u *s;
6565 int len;
6566 char_u *errormsg;
6567 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
6568 expand_T xpc;
6569
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006570 rettv->v_type = VAR_STRING;
6571 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 if (*s == '%' || *s == '#' || *s == '<')
6573 {
6574 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006575 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 --emsg_off;
6577 }
6578 else
6579 {
6580 /* When the optional second argument is non-zero, don't remove matches
6581 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006582 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 flags |= WILD_KEEP_ALL;
6584 ExpandInit(&xpc);
6585 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006586 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 ExpandCleanup(&xpc);
6588 }
6589}
6590
6591/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006592 * "extend(list, list [, idx])" function
6593 */
6594 static void
6595f_extend(argvars, rettv)
6596 typeval *argvars;
6597 typeval *rettv;
6598{
6599 long before;
6600 long n;
6601 listitem *item;
6602 listvar *l1, *l2;
6603
6604 rettv->vval.v_number = 0;
6605 if (argvars[0].v_type != VAR_LIST || argvars[1].v_type != VAR_LIST)
6606 {
6607 EMSG(_(e_listreq));
6608 return;
6609 }
6610 l1 = argvars[0].vval.v_list;
6611 l2 = argvars[1].vval.v_list;
6612 if (l1 != NULL && l2 != NULL)
6613 {
6614 if (argvars[2].v_type != VAR_UNKNOWN)
6615 {
6616 n = before = get_tv_number(&argvars[2]);
6617 item = list_find_ext(l1, &n);
6618 if (n != 0)
6619 {
6620 EMSGN(_(e_listidx), before);
6621 return;
6622 }
6623 }
6624 else
6625 item = NULL;
6626 list_extend(l1, l2, item);
6627
6628 ++l1->lv_refcount;
6629 copy_tv(&argvars[0], rettv);
6630 }
6631}
6632
6633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 * "filereadable()" function
6635 */
6636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006637f_filereadable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006639 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640{
6641 FILE *fd;
6642 char_u *p;
6643 int n;
6644
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006645 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
6647 {
6648 n = TRUE;
6649 fclose(fd);
6650 }
6651 else
6652 n = FALSE;
6653
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006654 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655}
6656
6657/*
6658 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
6659 * rights to write into.
6660 */
6661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006662f_filewritable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006663 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006664 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665{
6666 char_u *p;
6667 int retval = 0;
6668#if defined(UNIX) || defined(VMS)
6669 int perm = 0;
6670#endif
6671
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006672 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673#if defined(UNIX) || defined(VMS)
6674 perm = mch_getperm(p);
6675#endif
6676#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
6677 if (
6678# ifdef WIN3264
6679 mch_writable(p) &&
6680# else
6681# if defined(UNIX) || defined(VMS)
6682 (perm & 0222) &&
6683# endif
6684# endif
6685 mch_access((char *)p, W_OK) == 0
6686 )
6687#endif
6688 {
6689 ++retval;
6690 if (mch_isdir(p))
6691 ++retval;
6692 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006693 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694}
6695
Bram Moolenaar0d660222005-01-07 21:51:51 +00006696static void findfilendir __ARGS((typeval *argvars, typeval *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006697
6698 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00006699findfilendir(argvars, rettv, dir)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006700 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006701 typeval *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006702 int dir;
6703{
6704#ifdef FEAT_SEARCHPATH
6705 char_u *fname;
6706 char_u *fresult = NULL;
6707 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
6708 char_u *p;
6709 char_u pathbuf[NUMBUFLEN];
6710 int count = 1;
6711 int first = TRUE;
6712
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006713 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006714
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006715 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006716 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006717 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006718 if (*p != NUL)
6719 path = p;
6720
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006721 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006722 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006723 }
6724
6725 do
6726 {
6727 vim_free(fresult);
6728 fresult = find_file_in_path_option(first ? fname : NULL,
6729 first ? (int)STRLEN(fname) : 0,
6730 0, first, path, dir, NULL);
6731 first = FALSE;
6732 } while (--count > 0 && fresult != NULL);
6733
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006734 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006735#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006736 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006737#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006738 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006739}
6740
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006741static void filter_map __ARGS((typeval *argvars, typeval *rettv, int map));
6742
6743/*
6744 * Implementation of map() and filter().
6745 */
6746 static void
6747filter_map(argvars, rettv, map)
6748 typeval *argvars;
6749 typeval *rettv;
6750 int map;
6751{
6752 char_u buf[NUMBUFLEN];
6753 char_u *expr, *s;
6754 listitem *li, *nli;
6755 listvar *l;
6756
6757 rettv->vval.v_number = 0;
6758 if (argvars[0].v_type != VAR_LIST)
6759 EMSG(_(e_listreq));
6760 else if ((l = argvars[0].vval.v_list) != NULL)
6761 {
6762 expr = skipwhite(get_tv_string_buf(&argvars[1], buf));
6763 for (li = l->lv_first; li != NULL; li = nli)
6764 {
6765 copy_tv(&li->li_tv, &amp_tv);
6766 s = expr;
6767 if (eval1(&s, rettv, TRUE) == FAIL)
6768 break;
6769 if (*s != NUL) /* check for trailing chars after expr */
6770 {
6771 EMSG2(_(e_invexpr2), s);
6772 break;
6773 }
6774 nli = li->li_next;
6775 if (map)
6776 {
6777 /* map(): replace the list item value */
6778 clear_tv(&li->li_tv);
6779 li->li_tv = *rettv;
6780 }
6781 else
6782 {
6783 /* filter(): when expr is zero remove the item */
6784 if (get_tv_number(rettv) == 0)
6785 {
6786 list_getrem(l, li, li);
6787 clear_tv(&li->li_tv);
6788 }
6789 clear_tv(rettv);
6790 }
6791 clear_tv(&amp_tv);
6792 }
6793
6794 clear_tv(&amp_tv);
6795 amp_tv.v_type = VAR_UNKNOWN;
6796
6797 copy_tv(&argvars[0], rettv);
6798 }
6799}
6800
6801/*
6802 * "filter()" function
6803 */
6804 static void
6805f_filter(argvars, rettv)
6806 typeval *argvars;
6807 typeval *rettv;
6808{
6809 filter_map(argvars, rettv, FALSE);
6810}
6811
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006812/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006813 * "finddir({fname}[, {path}[, {count}]])" function
6814 */
6815 static void
6816f_finddir(argvars, rettv)
6817 typeval *argvars;
6818 typeval *rettv;
6819{
6820 findfilendir(argvars, rettv, TRUE);
6821}
6822
6823/*
6824 * "findfile({fname}[, {path}[, {count}]])" function
6825 */
6826 static void
6827f_findfile(argvars, rettv)
6828 typeval *argvars;
6829 typeval *rettv;
6830{
6831 findfilendir(argvars, rettv, FALSE);
6832}
6833
6834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 * "fnamemodify({fname}, {mods})" function
6836 */
6837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006838f_fnamemodify(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006839 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006840 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841{
6842 char_u *fname;
6843 char_u *mods;
6844 int usedlen = 0;
6845 int len;
6846 char_u *fbuf = NULL;
6847 char_u buf[NUMBUFLEN];
6848
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006849 fname = get_tv_string(&argvars[0]);
6850 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 len = (int)STRLEN(fname);
6852
6853 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
6854
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006855 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006857 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006859 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 vim_free(fbuf);
6861}
6862
Bram Moolenaar0d660222005-01-07 21:51:51 +00006863static void foldclosed_both __ARGS((typeval *argvars, typeval *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864
6865/*
6866 * "foldclosed()" function
6867 */
6868 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006869foldclosed_both(argvars, rettv, end)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006870 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006871 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 int end;
6873{
6874#ifdef FEAT_FOLDING
6875 linenr_T lnum;
6876 linenr_T first, last;
6877
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006878 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6880 {
6881 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
6882 {
6883 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006884 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006886 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 return;
6888 }
6889 }
6890#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006891 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892}
6893
6894/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006895 * "foldclosed()" function
6896 */
6897 static void
6898f_foldclosed(argvars, rettv)
6899 typeval *argvars;
6900 typeval *rettv;
6901{
6902 foldclosed_both(argvars, rettv, FALSE);
6903}
6904
6905/*
6906 * "foldclosedend()" function
6907 */
6908 static void
6909f_foldclosedend(argvars, rettv)
6910 typeval *argvars;
6911 typeval *rettv;
6912{
6913 foldclosed_both(argvars, rettv, TRUE);
6914}
6915
6916/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 * "foldlevel()" function
6918 */
6919 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006920f_foldlevel(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006921 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006922 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923{
6924#ifdef FEAT_FOLDING
6925 linenr_T lnum;
6926
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006927 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006929 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 else
6931#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006932 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933}
6934
6935/*
6936 * "foldtext()" function
6937 */
6938/*ARGSUSED*/
6939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006940f_foldtext(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006941 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006942 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943{
6944#ifdef FEAT_FOLDING
6945 linenr_T lnum;
6946 char_u *s;
6947 char_u *r;
6948 int len;
6949 char *txt;
6950#endif
6951
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006952 rettv->v_type = VAR_STRING;
6953 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954#ifdef FEAT_FOLDING
6955 if ((linenr_T)vimvars[VV_FOLDSTART].val > 0
6956 && (linenr_T)vimvars[VV_FOLDEND].val <= curbuf->b_ml.ml_line_count
6957 && vimvars[VV_FOLDDASHES].val != NULL)
6958 {
6959 /* Find first non-empty line in the fold. */
6960 lnum = (linenr_T)vimvars[VV_FOLDSTART].val;
6961 while (lnum < (linenr_T)vimvars[VV_FOLDEND].val)
6962 {
6963 if (!linewhite(lnum))
6964 break;
6965 ++lnum;
6966 }
6967
6968 /* Find interesting text in this line. */
6969 s = skipwhite(ml_get(lnum));
6970 /* skip C comment-start */
6971 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00006972 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00006974 if (*skipwhite(s) == NUL
6975 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].val)
6976 {
6977 s = skipwhite(ml_get(lnum + 1));
6978 if (*s == '*')
6979 s = skipwhite(s + 1);
6980 }
6981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 txt = _("+-%s%3ld lines: ");
6983 r = alloc((unsigned)(STRLEN(txt)
6984 + STRLEN(vimvars[VV_FOLDDASHES].val) /* for %s */
6985 + 20 /* for %3ld */
6986 + STRLEN(s))); /* concatenated */
6987 if (r != NULL)
6988 {
6989 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].val,
6990 (long)((linenr_T)vimvars[VV_FOLDEND].val
6991 - (linenr_T)vimvars[VV_FOLDSTART].val + 1));
6992 len = (int)STRLEN(r);
6993 STRCAT(r, s);
6994 /* remove 'foldmarker' and 'commentstring' */
6995 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006996 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 }
6998 }
6999#endif
7000}
7001
7002/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007003 * "foldtextresult(lnum)" function
7004 */
7005/*ARGSUSED*/
7006 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007007f_foldtextresult(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007008 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007009 typeval *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007010{
7011#ifdef FEAT_FOLDING
7012 linenr_T lnum;
7013 char_u *text;
7014 char_u buf[51];
7015 foldinfo_T foldinfo;
7016 int fold_count;
7017#endif
7018
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007019 rettv->v_type = VAR_STRING;
7020 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007021#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007022 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007023 fold_count = foldedCount(curwin, lnum, &foldinfo);
7024 if (fold_count > 0)
7025 {
7026 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
7027 &foldinfo, buf);
7028 if (text == buf)
7029 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007030 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007031 }
7032#endif
7033}
7034
7035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 * "foreground()" function
7037 */
7038/*ARGSUSED*/
7039 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007040f_foreground(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007041 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007042 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007044 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045#ifdef FEAT_GUI
7046 if (gui.in_use)
7047 gui_mch_set_foreground();
7048#else
7049# ifdef WIN32
7050 win32_set_foreground();
7051# endif
7052#endif
7053}
7054
7055/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007056 * "function()" function
7057 */
7058/*ARGSUSED*/
7059 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007060f_function(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007061 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007062 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007063{
7064 char_u *s;
7065
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007066 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007067 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007068 EMSG2(_(e_invarg2), s);
7069 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007070 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007071 else
7072 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007073 rettv->vval.v_string = vim_strsave(s);
7074 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007075 }
7076}
7077
7078/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007079 * "get()" function
7080 */
7081 static void
7082f_get(argvars, rettv)
7083 typeval *argvars;
7084 typeval *rettv;
7085{
7086 listitem *item;
7087 listvar *l;
7088
7089 if (argvars[0].v_type != VAR_LIST)
7090 EMSG2(_(e_listarg), "get()");
7091 else if ((l = argvars[0].vval.v_list) != NULL)
7092 {
7093 item = list_find(l, get_tv_number(&argvars[1]));
7094 if (item == NULL)
7095 {
7096 if (argvars[2].v_type == VAR_UNKNOWN)
7097 rettv->vval.v_number = 0;
7098 else
7099 copy_tv(&argvars[2], rettv);
7100 }
7101 else
7102 copy_tv(&item->li_tv, rettv);
7103 }
7104}
7105
7106/*
7107 * "getbufvar()" function
7108 */
7109 static void
7110f_getbufvar(argvars, rettv)
7111 typeval *argvars;
7112 typeval *rettv;
7113{
7114 buf_T *buf;
7115 buf_T *save_curbuf;
7116 char_u *varname;
7117 VAR v;
7118
7119 ++emsg_off;
7120 buf = get_buf_tv(&argvars[0]);
7121 varname = get_tv_string(&argvars[1]);
7122
7123 rettv->v_type = VAR_STRING;
7124 rettv->vval.v_string = NULL;
7125
7126 if (buf != NULL && varname != NULL)
7127 {
7128 if (*varname == '&') /* buffer-local-option */
7129 {
7130 /* set curbuf to be our buf, temporarily */
7131 save_curbuf = curbuf;
7132 curbuf = buf;
7133
7134 get_option_tv(&varname, rettv, TRUE);
7135
7136 /* restore previous notion of curbuf */
7137 curbuf = save_curbuf;
7138 }
7139 else
7140 {
7141 /* look up the variable */
7142 v = find_var_in_ga(&buf->b_vars, varname);
7143 if (v != NULL)
7144 copy_tv(&v->tv, rettv);
7145 }
7146 }
7147
7148 --emsg_off;
7149}
7150
7151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 * "getchar()" function
7153 */
7154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007155f_getchar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007156 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007157 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158{
7159 varnumber_T n;
7160
7161 ++no_mapping;
7162 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007163 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 /* getchar(): blocking wait. */
7165 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007166 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 /* getchar(1): only check if char avail */
7168 n = vpeekc();
7169 else if (vpeekc() == NUL)
7170 /* getchar(0) and no char avail: return zero */
7171 n = 0;
7172 else
7173 /* getchar(0) and char avail: return char */
7174 n = safe_vgetc();
7175 --no_mapping;
7176 --allow_keys;
7177
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007178 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 if (IS_SPECIAL(n) || mod_mask != 0)
7180 {
7181 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
7182 int i = 0;
7183
7184 /* Turn a special key into three bytes, plus modifier. */
7185 if (mod_mask != 0)
7186 {
7187 temp[i++] = K_SPECIAL;
7188 temp[i++] = KS_MODIFIER;
7189 temp[i++] = mod_mask;
7190 }
7191 if (IS_SPECIAL(n))
7192 {
7193 temp[i++] = K_SPECIAL;
7194 temp[i++] = K_SECOND(n);
7195 temp[i++] = K_THIRD(n);
7196 }
7197#ifdef FEAT_MBYTE
7198 else if (has_mbyte)
7199 i += (*mb_char2bytes)(n, temp + i);
7200#endif
7201 else
7202 temp[i++] = n;
7203 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007204 rettv->v_type = VAR_STRING;
7205 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206 }
7207}
7208
7209/*
7210 * "getcharmod()" function
7211 */
7212/*ARGSUSED*/
7213 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007214f_getcharmod(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007215 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007216 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007218 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219}
7220
7221/*
7222 * "getcmdline()" function
7223 */
7224/*ARGSUSED*/
7225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007226f_getcmdline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007227 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007228 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007230 rettv->v_type = VAR_STRING;
7231 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232}
7233
7234/*
7235 * "getcmdpos()" function
7236 */
7237/*ARGSUSED*/
7238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007239f_getcmdpos(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007240 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007241 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007243 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244}
7245
7246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 * "getcwd()" function
7248 */
7249/*ARGSUSED*/
7250 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007251f_getcwd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007252 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007253 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254{
7255 char_u cwd[MAXPATHL];
7256
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007257 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007259 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 else
7261 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007262 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007264 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265#endif
7266 }
7267}
7268
7269/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007270 * "getfontname()" function
7271 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007272/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007273 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007274f_getfontname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007275 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007276 typeval *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007277{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007278 rettv->v_type = VAR_STRING;
7279 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007280#ifdef FEAT_GUI
7281 if (gui.in_use)
7282 {
7283 GuiFont font;
7284 char_u *name = NULL;
7285
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007286 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007287 {
7288 /* Get the "Normal" font. Either the name saved by
7289 * hl_set_font_name() or from the font ID. */
7290 font = gui.norm_font;
7291 name = hl_get_font_name();
7292 }
7293 else
7294 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007295 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007296 if (STRCMP(name, "*") == 0) /* don't use font dialog */
7297 return;
7298 font = gui_mch_get_font(name, FALSE);
7299 if (font == NOFONT)
7300 return; /* Invalid font name, return empty string. */
7301 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007302 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007303 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007304 gui_mch_free_font(font);
7305 }
7306#endif
7307}
7308
7309/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007310 * "getfperm({fname})" function
7311 */
7312 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007313f_getfperm(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007314 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007315 typeval *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007316{
7317 char_u *fname;
7318 struct stat st;
7319 char_u *perm = NULL;
7320 char_u flags[] = "rwx";
7321 int i;
7322
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007323 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007324
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007325 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007326 if (mch_stat((char *)fname, &st) >= 0)
7327 {
7328 perm = vim_strsave((char_u *)"---------");
7329 if (perm != NULL)
7330 {
7331 for (i = 0; i < 9; i++)
7332 {
7333 if (st.st_mode & (1 << (8 - i)))
7334 perm[i] = flags[i % 3];
7335 }
7336 }
7337 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007338 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007339}
7340
7341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 * "getfsize({fname})" function
7343 */
7344 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007345f_getfsize(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007346 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007347 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348{
7349 char_u *fname;
7350 struct stat st;
7351
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007352 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007354 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355
7356 if (mch_stat((char *)fname, &st) >= 0)
7357 {
7358 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007359 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007361 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 }
7363 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007364 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365}
7366
7367/*
7368 * "getftime({fname})" function
7369 */
7370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007371f_getftime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007372 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007373 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374{
7375 char_u *fname;
7376 struct stat st;
7377
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007378 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379
7380 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007381 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007383 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384}
7385
7386/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007387 * "getftype({fname})" function
7388 */
7389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007390f_getftype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007391 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007392 typeval *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007393{
7394 char_u *fname;
7395 struct stat st;
7396 char_u *type = NULL;
7397 char *t;
7398
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007399 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007400
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007401 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007402 if (mch_lstat((char *)fname, &st) >= 0)
7403 {
7404#ifdef S_ISREG
7405 if (S_ISREG(st.st_mode))
7406 t = "file";
7407 else if (S_ISDIR(st.st_mode))
7408 t = "dir";
7409# ifdef S_ISLNK
7410 else if (S_ISLNK(st.st_mode))
7411 t = "link";
7412# endif
7413# ifdef S_ISBLK
7414 else if (S_ISBLK(st.st_mode))
7415 t = "bdev";
7416# endif
7417# ifdef S_ISCHR
7418 else if (S_ISCHR(st.st_mode))
7419 t = "cdev";
7420# endif
7421# ifdef S_ISFIFO
7422 else if (S_ISFIFO(st.st_mode))
7423 t = "fifo";
7424# endif
7425# ifdef S_ISSOCK
7426 else if (S_ISSOCK(st.st_mode))
7427 t = "fifo";
7428# endif
7429 else
7430 t = "other";
7431#else
7432# ifdef S_IFMT
7433 switch (st.st_mode & S_IFMT)
7434 {
7435 case S_IFREG: t = "file"; break;
7436 case S_IFDIR: t = "dir"; break;
7437# ifdef S_IFLNK
7438 case S_IFLNK: t = "link"; break;
7439# endif
7440# ifdef S_IFBLK
7441 case S_IFBLK: t = "bdev"; break;
7442# endif
7443# ifdef S_IFCHR
7444 case S_IFCHR: t = "cdev"; break;
7445# endif
7446# ifdef S_IFIFO
7447 case S_IFIFO: t = "fifo"; break;
7448# endif
7449# ifdef S_IFSOCK
7450 case S_IFSOCK: t = "socket"; break;
7451# endif
7452 default: t = "other";
7453 }
7454# else
7455 if (mch_isdir(fname))
7456 t = "dir";
7457 else
7458 t = "file";
7459# endif
7460#endif
7461 type = vim_strsave((char_u *)t);
7462 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007463 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007464}
7465
7466/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007467 * "getline(lnum)" function
7468 */
7469 static void
7470f_getline(argvars, rettv)
7471 typeval *argvars;
7472 typeval *rettv;
7473{
7474 linenr_T lnum;
7475 linenr_T end;
7476 char_u *p;
7477 listvar *l;
7478 listitem *li;
7479
7480 lnum = get_tv_lnum(argvars);
7481
7482 if (argvars[1].v_type == VAR_UNKNOWN)
7483 {
7484 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7485 p = ml_get(lnum);
7486 else
7487 p = (char_u *)"";
7488
7489 rettv->v_type = VAR_STRING;
7490 rettv->vval.v_string = vim_strsave(p);
7491 }
7492 else
7493 {
7494 end = get_tv_lnum(&argvars[1]);
7495 if (end < lnum)
7496 {
7497 EMSG(_(e_invrange));
7498 rettv->vval.v_number = 0;
7499 }
7500 else
7501 {
7502 l = list_alloc();
7503 if (l != NULL)
7504 {
7505 if (lnum < 1)
7506 lnum = 1;
7507 if (end > curbuf->b_ml.ml_line_count)
7508 end = curbuf->b_ml.ml_line_count;
7509 while (lnum <= end)
7510 {
7511 li = listitem_alloc();
7512 if (li == NULL)
7513 break;
7514 list_append(l, li);
7515 li->li_tv.v_type = VAR_STRING;
7516 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
7517 }
7518 rettv->vval.v_list = l;
7519 rettv->v_type = VAR_LIST;
7520 ++l->lv_refcount;
7521 }
7522 }
7523 }
7524}
7525
7526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 * "getreg()" function
7528 */
7529 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007530f_getreg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007531 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007532 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533{
7534 char_u *strregname;
7535 int regname;
7536
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007537 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007538 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 else
7540 strregname = vimvars[VV_REG].val;
7541 regname = (strregname == NULL ? '"' : *strregname);
7542 if (regname == 0)
7543 regname = '"';
7544
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007545 rettv->v_type = VAR_STRING;
7546 rettv->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547}
7548
7549/*
7550 * "getregtype()" function
7551 */
7552 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007553f_getregtype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007554 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007555 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556{
7557 char_u *strregname;
7558 int regname;
7559 char_u buf[NUMBUFLEN + 2];
7560 long reglen = 0;
7561
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007562 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007563 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 else
7565 /* Default to v:register */
7566 strregname = vimvars[VV_REG].val;
7567
7568 regname = (strregname == NULL ? '"' : *strregname);
7569 if (regname == 0)
7570 regname = '"';
7571
7572 buf[0] = NUL;
7573 buf[1] = NUL;
7574 switch (get_reg_type(regname, &reglen))
7575 {
7576 case MLINE: buf[0] = 'V'; break;
7577 case MCHAR: buf[0] = 'v'; break;
7578#ifdef FEAT_VISUAL
7579 case MBLOCK:
7580 buf[0] = Ctrl_V;
7581 sprintf((char *)buf + 1, "%ld", reglen + 1);
7582 break;
7583#endif
7584 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007585 rettv->v_type = VAR_STRING;
7586 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587}
7588
7589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 * "getwinposx()" function
7591 */
7592/*ARGSUSED*/
7593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007594f_getwinposx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007595 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007596 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007598 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599#ifdef FEAT_GUI
7600 if (gui.in_use)
7601 {
7602 int x, y;
7603
7604 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007605 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 }
7607#endif
7608}
7609
7610/*
7611 * "getwinposy()" function
7612 */
7613/*ARGSUSED*/
7614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007615f_getwinposy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007616 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007617 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007619 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620#ifdef FEAT_GUI
7621 if (gui.in_use)
7622 {
7623 int x, y;
7624
7625 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007626 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 }
7628#endif
7629}
7630
7631/*
7632 * "getwinvar()" function
7633 */
7634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007635f_getwinvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007636 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007637 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638{
7639 win_T *win, *oldcurwin;
7640 char_u *varname;
7641 VAR v;
7642
7643 ++emsg_off;
7644 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007645 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007647 rettv->v_type = VAR_STRING;
7648 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649
7650 if (win != NULL && varname != NULL)
7651 {
7652 if (*varname == '&') /* window-local-option */
7653 {
7654 /* set curwin to be our win, temporarily */
7655 oldcurwin = curwin;
7656 curwin = win;
7657
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007658 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659
7660 /* restore previous notion of curwin */
7661 curwin = oldcurwin;
7662 }
7663 else
7664 {
7665 /* look up the variable */
7666 v = find_var_in_ga(&win->w_vars, varname);
7667 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007668 copy_tv(&v->tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 }
7670 }
7671
7672 --emsg_off;
7673}
7674
7675/*
7676 * "glob()" function
7677 */
7678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007679f_glob(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007680 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007681 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682{
7683 expand_T xpc;
7684
7685 ExpandInit(&xpc);
7686 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007687 rettv->v_type = VAR_STRING;
7688 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
7690 ExpandCleanup(&xpc);
7691}
7692
7693/*
7694 * "globpath()" function
7695 */
7696 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007697f_globpath(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007698 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007699 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700{
7701 char_u buf1[NUMBUFLEN];
7702
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007703 rettv->v_type = VAR_STRING;
7704 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
7705 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706}
7707
7708/*
7709 * "has()" function
7710 */
7711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007712f_has(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007713 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007714 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715{
7716 int i;
7717 char_u *name;
7718 int n = FALSE;
7719 static char *(has_list[]) =
7720 {
7721#ifdef AMIGA
7722 "amiga",
7723# ifdef FEAT_ARP
7724 "arp",
7725# endif
7726#endif
7727#ifdef __BEOS__
7728 "beos",
7729#endif
7730#ifdef MSDOS
7731# ifdef DJGPP
7732 "dos32",
7733# else
7734 "dos16",
7735# endif
7736#endif
7737#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
7738 "mac",
7739#endif
7740#if defined(MACOS_X_UNIX)
7741 "macunix",
7742#endif
7743#ifdef OS2
7744 "os2",
7745#endif
7746#ifdef __QNX__
7747 "qnx",
7748#endif
7749#ifdef RISCOS
7750 "riscos",
7751#endif
7752#ifdef UNIX
7753 "unix",
7754#endif
7755#ifdef VMS
7756 "vms",
7757#endif
7758#ifdef WIN16
7759 "win16",
7760#endif
7761#ifdef WIN32
7762 "win32",
7763#endif
7764#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
7765 "win32unix",
7766#endif
7767#ifdef WIN64
7768 "win64",
7769#endif
7770#ifdef EBCDIC
7771 "ebcdic",
7772#endif
7773#ifndef CASE_INSENSITIVE_FILENAME
7774 "fname_case",
7775#endif
7776#ifdef FEAT_ARABIC
7777 "arabic",
7778#endif
7779#ifdef FEAT_AUTOCMD
7780 "autocmd",
7781#endif
7782#ifdef FEAT_BEVAL
7783 "balloon_eval",
7784#endif
7785#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
7786 "builtin_terms",
7787# ifdef ALL_BUILTIN_TCAPS
7788 "all_builtin_terms",
7789# endif
7790#endif
7791#ifdef FEAT_BYTEOFF
7792 "byte_offset",
7793#endif
7794#ifdef FEAT_CINDENT
7795 "cindent",
7796#endif
7797#ifdef FEAT_CLIENTSERVER
7798 "clientserver",
7799#endif
7800#ifdef FEAT_CLIPBOARD
7801 "clipboard",
7802#endif
7803#ifdef FEAT_CMDL_COMPL
7804 "cmdline_compl",
7805#endif
7806#ifdef FEAT_CMDHIST
7807 "cmdline_hist",
7808#endif
7809#ifdef FEAT_COMMENTS
7810 "comments",
7811#endif
7812#ifdef FEAT_CRYPT
7813 "cryptv",
7814#endif
7815#ifdef FEAT_CSCOPE
7816 "cscope",
7817#endif
7818#ifdef DEBUG
7819 "debug",
7820#endif
7821#ifdef FEAT_CON_DIALOG
7822 "dialog_con",
7823#endif
7824#ifdef FEAT_GUI_DIALOG
7825 "dialog_gui",
7826#endif
7827#ifdef FEAT_DIFF
7828 "diff",
7829#endif
7830#ifdef FEAT_DIGRAPHS
7831 "digraphs",
7832#endif
7833#ifdef FEAT_DND
7834 "dnd",
7835#endif
7836#ifdef FEAT_EMACS_TAGS
7837 "emacs_tags",
7838#endif
7839 "eval", /* always present, of course! */
7840#ifdef FEAT_EX_EXTRA
7841 "ex_extra",
7842#endif
7843#ifdef FEAT_SEARCH_EXTRA
7844 "extra_search",
7845#endif
7846#ifdef FEAT_FKMAP
7847 "farsi",
7848#endif
7849#ifdef FEAT_SEARCHPATH
7850 "file_in_path",
7851#endif
7852#ifdef FEAT_FIND_ID
7853 "find_in_path",
7854#endif
7855#ifdef FEAT_FOLDING
7856 "folding",
7857#endif
7858#ifdef FEAT_FOOTER
7859 "footer",
7860#endif
7861#if !defined(USE_SYSTEM) && defined(UNIX)
7862 "fork",
7863#endif
7864#ifdef FEAT_GETTEXT
7865 "gettext",
7866#endif
7867#ifdef FEAT_GUI
7868 "gui",
7869#endif
7870#ifdef FEAT_GUI_ATHENA
7871# ifdef FEAT_GUI_NEXTAW
7872 "gui_neXtaw",
7873# else
7874 "gui_athena",
7875# endif
7876#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007877#ifdef FEAT_GUI_KDE
7878 "gui_kde",
7879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880#ifdef FEAT_GUI_GTK
7881 "gui_gtk",
7882# ifdef HAVE_GTK2
7883 "gui_gtk2",
7884# endif
7885#endif
7886#ifdef FEAT_GUI_MAC
7887 "gui_mac",
7888#endif
7889#ifdef FEAT_GUI_MOTIF
7890 "gui_motif",
7891#endif
7892#ifdef FEAT_GUI_PHOTON
7893 "gui_photon",
7894#endif
7895#ifdef FEAT_GUI_W16
7896 "gui_win16",
7897#endif
7898#ifdef FEAT_GUI_W32
7899 "gui_win32",
7900#endif
7901#ifdef FEAT_HANGULIN
7902 "hangul_input",
7903#endif
7904#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
7905 "iconv",
7906#endif
7907#ifdef FEAT_INS_EXPAND
7908 "insert_expand",
7909#endif
7910#ifdef FEAT_JUMPLIST
7911 "jumplist",
7912#endif
7913#ifdef FEAT_KEYMAP
7914 "keymap",
7915#endif
7916#ifdef FEAT_LANGMAP
7917 "langmap",
7918#endif
7919#ifdef FEAT_LIBCALL
7920 "libcall",
7921#endif
7922#ifdef FEAT_LINEBREAK
7923 "linebreak",
7924#endif
7925#ifdef FEAT_LISP
7926 "lispindent",
7927#endif
7928#ifdef FEAT_LISTCMDS
7929 "listcmds",
7930#endif
7931#ifdef FEAT_LOCALMAP
7932 "localmap",
7933#endif
7934#ifdef FEAT_MENU
7935 "menu",
7936#endif
7937#ifdef FEAT_SESSION
7938 "mksession",
7939#endif
7940#ifdef FEAT_MODIFY_FNAME
7941 "modify_fname",
7942#endif
7943#ifdef FEAT_MOUSE
7944 "mouse",
7945#endif
7946#ifdef FEAT_MOUSESHAPE
7947 "mouseshape",
7948#endif
7949#if defined(UNIX) || defined(VMS)
7950# ifdef FEAT_MOUSE_DEC
7951 "mouse_dec",
7952# endif
7953# ifdef FEAT_MOUSE_GPM
7954 "mouse_gpm",
7955# endif
7956# ifdef FEAT_MOUSE_JSB
7957 "mouse_jsbterm",
7958# endif
7959# ifdef FEAT_MOUSE_NET
7960 "mouse_netterm",
7961# endif
7962# ifdef FEAT_MOUSE_PTERM
7963 "mouse_pterm",
7964# endif
7965# ifdef FEAT_MOUSE_XTERM
7966 "mouse_xterm",
7967# endif
7968#endif
7969#ifdef FEAT_MBYTE
7970 "multi_byte",
7971#endif
7972#ifdef FEAT_MBYTE_IME
7973 "multi_byte_ime",
7974#endif
7975#ifdef FEAT_MULTI_LANG
7976 "multi_lang",
7977#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007978#ifdef FEAT_MZSCHEME
7979 "mzscheme",
7980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981#ifdef FEAT_OLE
7982 "ole",
7983#endif
7984#ifdef FEAT_OSFILETYPE
7985 "osfiletype",
7986#endif
7987#ifdef FEAT_PATH_EXTRA
7988 "path_extra",
7989#endif
7990#ifdef FEAT_PERL
7991#ifndef DYNAMIC_PERL
7992 "perl",
7993#endif
7994#endif
7995#ifdef FEAT_PYTHON
7996#ifndef DYNAMIC_PYTHON
7997 "python",
7998#endif
7999#endif
8000#ifdef FEAT_POSTSCRIPT
8001 "postscript",
8002#endif
8003#ifdef FEAT_PRINTER
8004 "printer",
8005#endif
8006#ifdef FEAT_QUICKFIX
8007 "quickfix",
8008#endif
8009#ifdef FEAT_RIGHTLEFT
8010 "rightleft",
8011#endif
8012#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
8013 "ruby",
8014#endif
8015#ifdef FEAT_SCROLLBIND
8016 "scrollbind",
8017#endif
8018#ifdef FEAT_CMDL_INFO
8019 "showcmd",
8020 "cmdline_info",
8021#endif
8022#ifdef FEAT_SIGNS
8023 "signs",
8024#endif
8025#ifdef FEAT_SMARTINDENT
8026 "smartindent",
8027#endif
8028#ifdef FEAT_SNIFF
8029 "sniff",
8030#endif
8031#ifdef FEAT_STL_OPT
8032 "statusline",
8033#endif
8034#ifdef FEAT_SUN_WORKSHOP
8035 "sun_workshop",
8036#endif
8037#ifdef FEAT_NETBEANS_INTG
8038 "netbeans_intg",
8039#endif
8040#ifdef FEAT_SYN_HL
8041 "syntax",
8042#endif
8043#if defined(USE_SYSTEM) || !defined(UNIX)
8044 "system",
8045#endif
8046#ifdef FEAT_TAG_BINS
8047 "tag_binary",
8048#endif
8049#ifdef FEAT_TAG_OLDSTATIC
8050 "tag_old_static",
8051#endif
8052#ifdef FEAT_TAG_ANYWHITE
8053 "tag_any_white",
8054#endif
8055#ifdef FEAT_TCL
8056# ifndef DYNAMIC_TCL
8057 "tcl",
8058# endif
8059#endif
8060#ifdef TERMINFO
8061 "terminfo",
8062#endif
8063#ifdef FEAT_TERMRESPONSE
8064 "termresponse",
8065#endif
8066#ifdef FEAT_TEXTOBJ
8067 "textobjects",
8068#endif
8069#ifdef HAVE_TGETENT
8070 "tgetent",
8071#endif
8072#ifdef FEAT_TITLE
8073 "title",
8074#endif
8075#ifdef FEAT_TOOLBAR
8076 "toolbar",
8077#endif
8078#ifdef FEAT_USR_CMDS
8079 "user-commands", /* was accidentally included in 5.4 */
8080 "user_commands",
8081#endif
8082#ifdef FEAT_VIMINFO
8083 "viminfo",
8084#endif
8085#ifdef FEAT_VERTSPLIT
8086 "vertsplit",
8087#endif
8088#ifdef FEAT_VIRTUALEDIT
8089 "virtualedit",
8090#endif
8091#ifdef FEAT_VISUAL
8092 "visual",
8093#endif
8094#ifdef FEAT_VISUALEXTRA
8095 "visualextra",
8096#endif
8097#ifdef FEAT_VREPLACE
8098 "vreplace",
8099#endif
8100#ifdef FEAT_WILDIGN
8101 "wildignore",
8102#endif
8103#ifdef FEAT_WILDMENU
8104 "wildmenu",
8105#endif
8106#ifdef FEAT_WINDOWS
8107 "windows",
8108#endif
8109#ifdef FEAT_WAK
8110 "winaltkeys",
8111#endif
8112#ifdef FEAT_WRITEBACKUP
8113 "writebackup",
8114#endif
8115#ifdef FEAT_XIM
8116 "xim",
8117#endif
8118#ifdef FEAT_XFONTSET
8119 "xfontset",
8120#endif
8121#ifdef USE_XSMP
8122 "xsmp",
8123#endif
8124#ifdef USE_XSMP_INTERACT
8125 "xsmp_interact",
8126#endif
8127#ifdef FEAT_XCLIPBOARD
8128 "xterm_clipboard",
8129#endif
8130#ifdef FEAT_XTERM_SAVE
8131 "xterm_save",
8132#endif
8133#if defined(UNIX) && defined(FEAT_X11)
8134 "X11",
8135#endif
8136 NULL
8137 };
8138
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008139 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 for (i = 0; has_list[i] != NULL; ++i)
8141 if (STRICMP(name, has_list[i]) == 0)
8142 {
8143 n = TRUE;
8144 break;
8145 }
8146
8147 if (n == FALSE)
8148 {
8149 if (STRNICMP(name, "patch", 5) == 0)
8150 n = has_patch(atoi((char *)name + 5));
8151 else if (STRICMP(name, "vim_starting") == 0)
8152 n = (starting != 0);
8153#ifdef DYNAMIC_TCL
8154 else if (STRICMP(name, "tcl") == 0)
8155 n = tcl_enabled(FALSE);
8156#endif
8157#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
8158 else if (STRICMP(name, "iconv") == 0)
8159 n = iconv_enabled(FALSE);
8160#endif
8161#ifdef DYNAMIC_RUBY
8162 else if (STRICMP(name, "ruby") == 0)
8163 n = ruby_enabled(FALSE);
8164#endif
8165#ifdef DYNAMIC_PYTHON
8166 else if (STRICMP(name, "python") == 0)
8167 n = python_enabled(FALSE);
8168#endif
8169#ifdef DYNAMIC_PERL
8170 else if (STRICMP(name, "perl") == 0)
8171 n = perl_enabled(FALSE);
8172#endif
8173#ifdef FEAT_GUI
8174 else if (STRICMP(name, "gui_running") == 0)
8175 n = (gui.in_use || gui.starting);
8176# ifdef FEAT_GUI_W32
8177 else if (STRICMP(name, "gui_win32s") == 0)
8178 n = gui_is_win32s();
8179# endif
8180# ifdef FEAT_BROWSE
8181 else if (STRICMP(name, "browse") == 0)
8182 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
8183# endif
8184#endif
8185#ifdef FEAT_SYN_HL
8186 else if (STRICMP(name, "syntax_items") == 0)
8187 n = syntax_present(curbuf);
8188#endif
8189#if defined(WIN3264)
8190 else if (STRICMP(name, "win95") == 0)
8191 n = mch_windows95();
8192#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00008193#ifdef FEAT_NETBEANS_INTG
8194 else if (STRICMP(name, "netbeans_enabled") == 0)
8195 n = usingNetbeans;
8196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 }
8198
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008199 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200}
8201
8202/*
8203 * "hasmapto()" function
8204 */
8205 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008206f_hasmapto(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008207 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008208 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209{
8210 char_u *name;
8211 char_u *mode;
8212 char_u buf[NUMBUFLEN];
8213
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008214 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008215 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 mode = (char_u *)"nvo";
8217 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008218 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219
8220 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008221 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008223 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224}
8225
8226/*
8227 * "histadd()" function
8228 */
8229/*ARGSUSED*/
8230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008231f_histadd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008232 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008233 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234{
8235#ifdef FEAT_CMDHIST
8236 int histype;
8237 char_u *str;
8238 char_u buf[NUMBUFLEN];
8239#endif
8240
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008241 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 if (check_restricted() || check_secure())
8243 return;
8244#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008245 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 if (histype >= 0)
8247 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008248 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 if (*str != NUL)
8250 {
8251 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008252 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 return;
8254 }
8255 }
8256#endif
8257}
8258
8259/*
8260 * "histdel()" function
8261 */
8262/*ARGSUSED*/
8263 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008264f_histdel(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008265 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008266 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267{
8268#ifdef FEAT_CMDHIST
8269 int n;
8270 char_u buf[NUMBUFLEN];
8271
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008272 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008274 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008275 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008277 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
8278 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 else
8280 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008281 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
8282 get_tv_string_buf(&argvars[1], buf));
8283 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008285 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286#endif
8287}
8288
8289/*
8290 * "histget()" function
8291 */
8292/*ARGSUSED*/
8293 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008294f_histget(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008295 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008296 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297{
8298#ifdef FEAT_CMDHIST
8299 int type;
8300 int idx;
8301
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008302 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008303 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 idx = get_history_idx(type);
8305 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008306 idx = (int)get_tv_number(&argvars[1]);
8307 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008309 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008311 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312}
8313
8314/*
8315 * "histnr()" function
8316 */
8317/*ARGSUSED*/
8318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008319f_histnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008320 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008321 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322{
8323 int i;
8324
8325#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008326 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 if (i >= HIST_CMD && i < HIST_COUNT)
8328 i = get_history_idx(i);
8329 else
8330#endif
8331 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008332 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333}
8334
8335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 * "highlightID(name)" function
8337 */
8338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008339f_hlID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008340 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008341 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008343 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344}
8345
8346/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008347 * "highlight_exists()" function
8348 */
8349 static void
8350f_hlexists(argvars, rettv)
8351 typeval *argvars;
8352 typeval *rettv;
8353{
8354 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
8355}
8356
8357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 * "hostname()" function
8359 */
8360/*ARGSUSED*/
8361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008362f_hostname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008363 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008364 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365{
8366 char_u hostname[256];
8367
8368 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008369 rettv->v_type = VAR_STRING;
8370 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371}
8372
8373/*
8374 * iconv() function
8375 */
8376/*ARGSUSED*/
8377 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008378f_iconv(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008379 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008380 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381{
8382#ifdef FEAT_MBYTE
8383 char_u buf1[NUMBUFLEN];
8384 char_u buf2[NUMBUFLEN];
8385 char_u *from, *to, *str;
8386 vimconv_T vimconv;
8387#endif
8388
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008389 rettv->v_type = VAR_STRING;
8390 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391
8392#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008393 str = get_tv_string(&argvars[0]);
8394 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
8395 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 vimconv.vc_type = CONV_NONE;
8397 convert_setup(&vimconv, from, to);
8398
8399 /* If the encodings are equal, no conversion needed. */
8400 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008401 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008403 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404
8405 convert_setup(&vimconv, NULL, NULL);
8406 vim_free(from);
8407 vim_free(to);
8408#endif
8409}
8410
8411/*
8412 * "indent()" function
8413 */
8414 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008415f_indent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008416 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008417 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418{
8419 linenr_T lnum;
8420
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008421 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008423 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008425 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426}
8427
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008428/*
8429 * "index()" function
8430 */
8431 static void
8432f_index(argvars, rettv)
8433 typeval *argvars;
8434 typeval *rettv;
8435{
8436 listvar *l;
8437 listitem *item;
8438 long idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008439 long min_idx = 0;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008440 int ic = FALSE;
8441
8442 rettv->vval.v_number = -1;
8443 if (argvars[0].v_type != VAR_LIST)
8444 {
8445 EMSG(_(e_listreq));
8446 return;
8447 }
8448 l = argvars[0].vval.v_list;
8449 if (l != NULL)
8450 {
8451 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008452 {
8453 min_idx = get_tv_number(&argvars[2]);
8454 if (argvars[3].v_type != VAR_UNKNOWN)
8455 ic = get_tv_number(&argvars[3]);
8456 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008457
8458 for (item = l->lv_first; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008459 if (idx >= min_idx && tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008460 {
8461 rettv->vval.v_number = idx;
8462 break;
8463 }
8464 }
8465}
8466
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467static int inputsecret_flag = 0;
8468
8469/*
8470 * "input()" function
8471 * Also handles inputsecret() when inputsecret is set.
8472 */
8473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008474f_input(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008475 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008476 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008478 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 char_u *p = NULL;
8480 int c;
8481 char_u buf[NUMBUFLEN];
8482 int cmd_silent_save = cmd_silent;
8483
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008484 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485
8486#ifdef NO_CONSOLE_INPUT
8487 /* While starting up, there is no place to enter text. */
8488 if (no_console_input())
8489 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008490 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 return;
8492 }
8493#endif
8494
8495 cmd_silent = FALSE; /* Want to see the prompt. */
8496 if (prompt != NULL)
8497 {
8498 /* Only the part of the message after the last NL is considered as
8499 * prompt for the command line */
8500 p = vim_strrchr(prompt, '\n');
8501 if (p == NULL)
8502 p = prompt;
8503 else
8504 {
8505 ++p;
8506 c = *p;
8507 *p = NUL;
8508 msg_start();
8509 msg_clr_eos();
8510 msg_puts_attr(prompt, echo_attr);
8511 msg_didout = FALSE;
8512 msg_starthere();
8513 *p = c;
8514 }
8515 cmdline_row = msg_row;
8516 }
8517
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008518 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008519 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008521 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
8523
8524 /* since the user typed this, no need to wait for return */
8525 need_wait_return = FALSE;
8526 msg_didout = FALSE;
8527 cmd_silent = cmd_silent_save;
8528}
8529
8530/*
8531 * "inputdialog()" function
8532 */
8533 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008534f_inputdialog(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008535 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008536 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537{
8538#if defined(FEAT_GUI_TEXTDIALOG)
8539 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
8540 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
8541 {
8542 char_u *message;
8543 char_u buf[NUMBUFLEN];
8544
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008545 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008546 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008548 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 IObuff[IOSIZE - 1] = NUL;
8550 }
8551 else
8552 IObuff[0] = NUL;
8553 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
8554 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008555 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 else
8557 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008558 if (argvars[1].v_type != VAR_UNKNOWN
8559 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008560 rettv->vval.v_string = vim_strsave(
8561 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008562 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008563 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008565 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 }
8567 else
8568#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008569 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570}
8571
8572static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
8573
8574/*
8575 * "inputrestore()" function
8576 */
8577/*ARGSUSED*/
8578 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008579f_inputrestore(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008580 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008581 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582{
8583 if (ga_userinput.ga_len > 0)
8584 {
8585 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
8587 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008588 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 }
8590 else if (p_verbose > 1)
8591 {
8592 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008593 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 }
8595}
8596
8597/*
8598 * "inputsave()" function
8599 */
8600/*ARGSUSED*/
8601 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008602f_inputsave(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008603 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008604 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605{
8606 /* Add an entry to the stack of typehead storage. */
8607 if (ga_grow(&ga_userinput, 1) == OK)
8608 {
8609 save_typeahead((tasave_T *)(ga_userinput.ga_data)
8610 + ga_userinput.ga_len);
8611 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008612 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 }
8614 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008615 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616}
8617
8618/*
8619 * "inputsecret()" function
8620 */
8621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008622f_inputsecret(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008623 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008624 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625{
8626 ++cmdline_star;
8627 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008628 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 --cmdline_star;
8630 --inputsecret_flag;
8631}
8632
8633/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008634 * "insert()" function
8635 */
8636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008637f_insert(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008638 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008639 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008640{
8641 long before = 0;
8642 long n;
8643 listitem *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008644 listvar *l;
8645
8646 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008647 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008648 else if ((l = argvars[0].vval.v_list) != NULL)
8649 {
8650 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008651 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008652
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008653 n = before;
8654 item = list_find_ext(l, &n);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008655 if (n > 0)
8656 EMSGN(_(e_listidx), before);
8657 else
8658 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008659 list_insert_tv(l, &argvars[1], item);
8660 ++l->lv_refcount;
8661 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008662 }
8663 }
8664}
8665
8666/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 * "isdirectory()" function
8668 */
8669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008670f_isdirectory(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008671 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008672 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008674 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675}
8676
Bram Moolenaar8c711452005-01-14 21:53:12 +00008677static void dict_list __ARGS((typeval *argvars, typeval *rettv, int what));
8678
8679/*
8680 * Turn a dict into a list:
8681 * "what" == 0: list of keys
8682 * "what" == 1: list of values
8683 * "what" == 2: list of items
8684 */
8685 static void
8686dict_list(argvars, rettv, what)
8687 typeval *argvars;
8688 typeval *rettv;
8689 int what;
8690{
8691 listvar *l;
8692 listvar *l2;
8693 dictitem *di;
8694 listitem *li;
8695 listitem *li2;
8696
8697 rettv->vval.v_number = 0;
8698 if (argvars[0].v_type != VAR_DICT)
8699 {
8700 EMSG(_(e_dictreq));
8701 return;
8702 }
8703 if (argvars[0].vval.v_dict == NULL)
8704 return;
8705
8706 l = list_alloc();
8707 if (l == NULL)
8708 return;
8709 rettv->v_type = VAR_LIST;
8710 rettv->vval.v_list = l;
8711 ++l->lv_refcount;
8712
8713 for (di = argvars[0].vval.v_dict->dv_first; di != NULL; di = di->di_next)
8714 {
8715 li = listitem_alloc();
8716 if (li == NULL)
8717 break;
8718 list_append(l, li);
8719
8720 if (what == 0)
8721 {
8722 /* keys() */
8723 li->li_tv.v_type = VAR_STRING;
8724 li->li_tv.vval.v_string = vim_strsave(di->di_key);
8725 }
8726 else if (what == 1)
8727 {
8728 /* values() */
8729 copy_tv(&di->di_tv, &li->li_tv);
8730 }
8731 else
8732 {
8733 /* items() */
8734 l2 = list_alloc();
8735 li->li_tv.v_type = VAR_LIST;
8736 li->li_tv.vval.v_list = l2;
8737 if (l2 == NULL)
8738 break;
8739 ++l2->lv_refcount;
8740
8741 li2 = listitem_alloc();
8742 if (li2 == NULL)
8743 break;
8744 list_append(l2, li2);
8745 li2->li_tv.v_type = VAR_STRING;
8746 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
8747
8748 li2 = listitem_alloc();
8749 if (li2 == NULL)
8750 break;
8751 list_append(l2, li2);
8752 copy_tv(&di->di_tv, &li2->li_tv);
8753 }
8754 }
8755}
8756
8757/*
8758 * "items(dict)" function
8759 */
8760 static void
8761f_items(argvars, rettv)
8762 typeval *argvars;
8763 typeval *rettv;
8764{
8765 dict_list(argvars, rettv, 2);
8766}
8767
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008769 * "join()" function
8770 */
8771 static void
8772f_join(argvars, rettv)
8773 typeval *argvars;
8774 typeval *rettv;
8775{
8776 garray_T ga;
8777 char_u *sep;
8778
8779 rettv->vval.v_number = 0;
8780 if (argvars[0].v_type != VAR_LIST)
8781 {
8782 EMSG(_(e_listreq));
8783 return;
8784 }
8785 if (argvars[0].vval.v_list == NULL)
8786 return;
8787 if (argvars[1].v_type == VAR_UNKNOWN)
8788 sep = (char_u *)" ";
8789 else
8790 sep = get_tv_string(&argvars[1]);
8791
8792 ga_init2(&ga, (int)sizeof(char), 80);
8793 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
8794 ga_append(&ga, NUL);
8795
8796 rettv->v_type = VAR_STRING;
8797 rettv->vval.v_string = (char_u *)ga.ga_data;
8798}
8799
8800/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00008801 * "keys()" function
8802 */
8803 static void
8804f_keys(argvars, rettv)
8805 typeval *argvars;
8806 typeval *rettv;
8807{
8808 dict_list(argvars, rettv, 0);
8809}
8810
8811/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 * "last_buffer_nr()" function.
8813 */
8814/*ARGSUSED*/
8815 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008816f_last_buffer_nr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008817 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008818 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819{
8820 int n = 0;
8821 buf_T *buf;
8822
8823 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8824 if (n < buf->b_fnum)
8825 n = buf->b_fnum;
8826
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008827 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008828}
8829
8830/*
8831 * "len()" function
8832 */
8833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008834f_len(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008835 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008836 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008837{
8838 switch (argvars[0].v_type)
8839 {
8840 case VAR_STRING:
8841 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008842 rettv->vval.v_number = (varnumber_T)STRLEN(
8843 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008844 break;
8845 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008846 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008847 break;
8848 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008849 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008850 break;
8851 }
8852}
8853
Bram Moolenaar0d660222005-01-07 21:51:51 +00008854static void libcall_common __ARGS((typeval *argvars, typeval *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008855
8856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008857libcall_common(argvars, rettv, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008858 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008859 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008860 int type;
8861{
8862#ifdef FEAT_LIBCALL
8863 char_u *string_in;
8864 char_u **string_result;
8865 int nr_result;
8866#endif
8867
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008868 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008869 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008870 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008871 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008872 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008873
8874 if (check_restricted() || check_secure())
8875 return;
8876
8877#ifdef FEAT_LIBCALL
8878 /* The first two args must be strings, otherwise its meaningless */
8879 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
8880 {
8881 if (argvars[2].v_type == VAR_NUMBER)
8882 string_in = NULL;
8883 else
8884 string_in = argvars[2].vval.v_string;
8885 if (type == VAR_NUMBER)
8886 string_result = NULL;
8887 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008888 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008889 if (mch_libcall(argvars[0].vval.v_string,
8890 argvars[1].vval.v_string,
8891 string_in,
8892 argvars[2].vval.v_number,
8893 string_result,
8894 &nr_result) == OK
8895 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008896 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008897 }
8898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899}
8900
8901/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008902 * "libcall()" function
8903 */
8904 static void
8905f_libcall(argvars, rettv)
8906 typeval *argvars;
8907 typeval *rettv;
8908{
8909 libcall_common(argvars, rettv, VAR_STRING);
8910}
8911
8912/*
8913 * "libcallnr()" function
8914 */
8915 static void
8916f_libcallnr(argvars, rettv)
8917 typeval *argvars;
8918 typeval *rettv;
8919{
8920 libcall_common(argvars, rettv, VAR_NUMBER);
8921}
8922
8923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 * "line(string)" function
8925 */
8926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008927f_line(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008928 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008929 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930{
8931 linenr_T lnum = 0;
8932 pos_T *fp;
8933
8934 fp = var2fpos(&argvars[0], TRUE);
8935 if (fp != NULL)
8936 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008937 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938}
8939
8940/*
8941 * "line2byte(lnum)" function
8942 */
8943/*ARGSUSED*/
8944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008945f_line2byte(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008946 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008947 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948{
8949#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008950 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951#else
8952 linenr_T lnum;
8953
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008954 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008956 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
8959 if (rettv->vval.v_number >= 0)
8960 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961#endif
8962}
8963
8964/*
8965 * "lispindent(lnum)" function
8966 */
8967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008968f_lispindent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008969 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008970 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971{
8972#ifdef FEAT_LISP
8973 pos_T pos;
8974 linenr_T lnum;
8975
8976 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008977 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8979 {
8980 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008981 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 curwin->w_cursor = pos;
8983 }
8984 else
8985#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987}
8988
8989/*
8990 * "localtime()" function
8991 */
8992/*ARGSUSED*/
8993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008994f_localtime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008995 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008996 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999}
9000
Bram Moolenaar0d660222005-01-07 21:51:51 +00009001static void get_maparg __ARGS((typeval *argvars, typeval *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002
9003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009004get_maparg(argvars, rettv, exact)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009005 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009006 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 int exact;
9008{
9009 char_u *keys;
9010 char_u *which;
9011 char_u buf[NUMBUFLEN];
9012 char_u *keys_buf = NULL;
9013 char_u *rhs;
9014 int mode;
9015 garray_T ga;
9016
9017 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009018 rettv->v_type = VAR_STRING;
9019 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009021 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 if (*keys == NUL)
9023 return;
9024
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009025 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009026 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 else
9028 which = (char_u *)"";
9029 mode = get_map_mode(&which, 0);
9030
9031 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
9032 rhs = check_map(keys, mode, exact);
9033 vim_free(keys_buf);
9034 if (rhs != NULL)
9035 {
9036 ga_init(&ga);
9037 ga.ga_itemsize = 1;
9038 ga.ga_growsize = 40;
9039
9040 while (*rhs != NUL)
9041 ga_concat(&ga, str2special(&rhs, FALSE));
9042
9043 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009044 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 }
9046}
9047
9048/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009049 * "map()" function
9050 */
9051 static void
9052f_map(argvars, rettv)
9053 typeval *argvars;
9054 typeval *rettv;
9055{
9056 filter_map(argvars, rettv, TRUE);
9057}
9058
9059/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009060 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 */
9062 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009063f_maparg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009064 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009065 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009067 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068}
9069
9070/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009071 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 */
9073 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009074f_mapcheck(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009075 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009076 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009078 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079}
9080
Bram Moolenaar0d660222005-01-07 21:51:51 +00009081static void find_some_match __ARGS((typeval *argvars, typeval *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082
9083 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009084find_some_match(argvars, rettv, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009085 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009086 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 int type;
9088{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009089 char_u *str = NULL;
9090 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 char_u *pat;
9092 regmatch_T regmatch;
9093 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009094 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 char_u *save_cpo;
9096 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009097 long nth = 1;
9098 int match;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009099 listvar *l = NULL;
9100 listitem *li = NULL;
9101 long idx = 0;
9102 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103
9104 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9105 save_cpo = p_cpo;
9106 p_cpo = (char_u *)"";
9107
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 if (type == 2)
9109 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110 rettv->v_type = VAR_STRING;
9111 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 }
9113 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009114 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009116 if (argvars[0].v_type == VAR_LIST)
9117 {
9118 if ((l = argvars[0].vval.v_list) == NULL)
9119 goto theend;
9120 li = l->lv_first;
9121 }
9122 else
9123 expr = str = get_tv_string(&argvars[0]);
9124
9125 pat = get_tv_string_buf(&argvars[1], patbuf);
9126
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009127 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009129 start = get_tv_number(&argvars[2]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009130 if (l != NULL)
9131 {
9132 li = list_find(l, start);
9133 if (li == NULL)
9134 goto theend;
9135 if (start < 0)
9136 {
9137 listitem *ni;
9138
9139 /* Need to compute the index. */
9140 for (ni = li; ni->li_prev != NULL; ni = ni->li_prev)
9141 ++idx;
9142 }
9143 else
9144 idx = start;
9145 }
9146 else
9147 {
9148 if (start < 0)
9149 start = 0;
9150 if (start > (long)STRLEN(str))
9151 goto theend;
9152 str += start;
9153 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009154
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009155 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009156 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 }
9158
9159 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9160 if (regmatch.regprog != NULL)
9161 {
9162 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009163
9164 while (1)
9165 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009166 if (l != NULL)
9167 {
9168 if (li == NULL)
9169 {
9170 match = FALSE;
9171 break;
9172 }
9173 str = echo_string(&li->li_tv, &tofree, strbuf);
9174 }
9175
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009176 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009177
9178 if (l != NULL)
9179 vim_free(tofree);
9180 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009181 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009182 if (l == NULL && !match)
9183 break;
9184
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009185 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009186 if (l != NULL)
9187 {
9188 li = li->li_next;
9189 ++idx;
9190 }
9191 else
9192 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009193#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009194 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009195#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009196 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009197#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009198 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00009199 }
9200
9201 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 {
9203 if (type == 2)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009204 {
9205 if (l != NULL)
9206 copy_tv(&li->li_tv, rettv);
9207 else
9208 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009210 }
9211 else if (l != NULL)
9212 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 else
9214 {
9215 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009216 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 (varnumber_T)(regmatch.startp[0] - str);
9218 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009219 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009221 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 }
9223 }
9224 vim_free(regmatch.regprog);
9225 }
9226
9227theend:
9228 p_cpo = save_cpo;
9229}
9230
9231/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009232 * "match()" function
9233 */
9234 static void
9235f_match(argvars, rettv)
9236 typeval *argvars;
9237 typeval *rettv;
9238{
9239 find_some_match(argvars, rettv, 1);
9240}
9241
9242/*
9243 * "matchend()" function
9244 */
9245 static void
9246f_matchend(argvars, rettv)
9247 typeval *argvars;
9248 typeval *rettv;
9249{
9250 find_some_match(argvars, rettv, 0);
9251}
9252
9253/*
9254 * "matchstr()" function
9255 */
9256 static void
9257f_matchstr(argvars, rettv)
9258 typeval *argvars;
9259 typeval *rettv;
9260{
9261 find_some_match(argvars, rettv, 2);
9262}
9263
Bram Moolenaar6cc16192005-01-08 21:49:45 +00009264static void max_min __ARGS((typeval *argvars, typeval *rettv, int domax));
9265
9266 static void
9267max_min(argvars, rettv, domax)
9268 typeval *argvars;
9269 typeval *rettv;
9270 int domax;
9271{
9272 listvar *l;
9273 listitem *li;
9274 long n = 0;
9275 long i;
9276
9277 if (argvars[0].v_type == VAR_LIST)
9278 {
9279 l = argvars[0].vval.v_list;
9280 if (l != NULL)
9281 {
9282 li = l->lv_first;
9283 if (li != NULL)
9284 {
9285 n = get_tv_number(&li->li_tv);
9286 while (1)
9287 {
9288 li = li->li_next;
9289 if (li == NULL)
9290 break;
9291 i = get_tv_number(&li->li_tv);
9292 if (domax ? i > n : i < n)
9293 n = i;
9294 }
9295 }
9296 }
9297 }
9298 else
9299 EMSG(_(e_listreq));
9300 rettv->vval.v_number = n;
9301}
9302
9303/*
9304 * "max()" function
9305 */
9306 static void
9307f_max(argvars, rettv)
9308 typeval *argvars;
9309 typeval *rettv;
9310{
9311 max_min(argvars, rettv, TRUE);
9312}
9313
9314/*
9315 * "min()" function
9316 */
9317 static void
9318f_min(argvars, rettv)
9319 typeval *argvars;
9320 typeval *rettv;
9321{
9322 max_min(argvars, rettv, FALSE);
9323}
9324
Bram Moolenaar0d660222005-01-07 21:51:51 +00009325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 * "mode()" function
9327 */
9328/*ARGSUSED*/
9329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009330f_mode(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009331 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009332 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333{
9334 char_u buf[2];
9335
9336#ifdef FEAT_VISUAL
9337 if (VIsual_active)
9338 {
9339 if (VIsual_select)
9340 buf[0] = VIsual_mode + 's' - 'v';
9341 else
9342 buf[0] = VIsual_mode;
9343 }
9344 else
9345#endif
9346 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
9347 buf[0] = 'r';
9348 else if (State & INSERT)
9349 {
9350 if (State & REPLACE_FLAG)
9351 buf[0] = 'R';
9352 else
9353 buf[0] = 'i';
9354 }
9355 else if (State & CMDLINE)
9356 buf[0] = 'c';
9357 else
9358 buf[0] = 'n';
9359
9360 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009361 rettv->vval.v_string = vim_strsave(buf);
9362 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363}
9364
9365/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009366 * "nextnonblank()" function
9367 */
9368 static void
9369f_nextnonblank(argvars, rettv)
9370 typeval *argvars;
9371 typeval *rettv;
9372{
9373 linenr_T lnum;
9374
9375 for (lnum = get_tv_lnum(argvars); ; ++lnum)
9376 {
9377 if (lnum > curbuf->b_ml.ml_line_count)
9378 {
9379 lnum = 0;
9380 break;
9381 }
9382 if (*skipwhite(ml_get(lnum)) != NUL)
9383 break;
9384 }
9385 rettv->vval.v_number = lnum;
9386}
9387
9388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 * "nr2char()" function
9390 */
9391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009392f_nr2char(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009393 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009394 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395{
9396 char_u buf[NUMBUFLEN];
9397
9398#ifdef FEAT_MBYTE
9399 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009400 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 else
9402#endif
9403 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009404 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 buf[1] = NUL;
9406 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009407 rettv->v_type = VAR_STRING;
9408 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009409}
9410
9411/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009412 * "prevnonblank()" function
9413 */
9414 static void
9415f_prevnonblank(argvars, rettv)
9416 typeval *argvars;
9417 typeval *rettv;
9418{
9419 linenr_T lnum;
9420
9421 lnum = get_tv_lnum(argvars);
9422 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9423 lnum = 0;
9424 else
9425 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
9426 --lnum;
9427 rettv->vval.v_number = lnum;
9428}
9429
Bram Moolenaar8c711452005-01-14 21:53:12 +00009430/*
9431 * "range()" function
9432 */
9433 static void
9434f_range(argvars, rettv)
9435 typeval *argvars;
9436 typeval *rettv;
9437{
9438 long start;
9439 long end;
9440 long stride = 1;
9441 long i;
9442 listvar *l;
9443 listitem *li;
9444
9445 start = get_tv_number(&argvars[0]);
9446 if (argvars[1].v_type == VAR_UNKNOWN)
9447 {
9448 end = start - 1;
9449 start = 0;
9450 }
9451 else
9452 {
9453 end = get_tv_number(&argvars[1]);
9454 if (argvars[2].v_type != VAR_UNKNOWN)
9455 stride = get_tv_number(&argvars[2]);
9456 }
9457
9458 rettv->vval.v_number = 0;
9459 if (stride == 0)
9460 EMSG(_("E999: Stride is zero"));
9461 else if (stride > 0 ? end < start : end > start)
9462 EMSG(_("E999: Start past end"));
9463 else
9464 {
9465 l = list_alloc();
9466 if (l != NULL)
9467 {
9468 rettv->v_type = VAR_LIST;
9469 rettv->vval.v_list = l;
9470 ++l->lv_refcount;
9471
9472 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
9473 {
9474 li = listitem_alloc();
9475 if (li == NULL)
9476 break;
9477 li->li_tv.v_type = VAR_NUMBER;
9478 li->li_tv.vval.v_number = i;
9479 list_append(l, li);
9480 }
9481 }
9482 }
9483}
9484
Bram Moolenaar0d660222005-01-07 21:51:51 +00009485#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
9486static void make_connection __ARGS((void));
9487static int check_connection __ARGS((void));
9488
9489 static void
9490make_connection()
9491{
9492 if (X_DISPLAY == NULL
9493# ifdef FEAT_GUI
9494 && !gui.in_use
9495# endif
9496 )
9497 {
9498 x_force_connect = TRUE;
9499 setup_term_clip();
9500 x_force_connect = FALSE;
9501 }
9502}
9503
9504 static int
9505check_connection()
9506{
9507 make_connection();
9508 if (X_DISPLAY == NULL)
9509 {
9510 EMSG(_("E240: No connection to Vim server"));
9511 return FAIL;
9512 }
9513 return OK;
9514}
9515#endif
9516
9517#ifdef FEAT_CLIENTSERVER
9518static void remote_common __ARGS((typeval *argvars, typeval *rettv, int expr));
9519
9520 static void
9521remote_common(argvars, rettv, expr)
9522 typeval *argvars;
9523 typeval *rettv;
9524 int expr;
9525{
9526 char_u *server_name;
9527 char_u *keys;
9528 char_u *r = NULL;
9529 char_u buf[NUMBUFLEN];
9530# ifdef WIN32
9531 HWND w;
9532# else
9533 Window w;
9534# endif
9535
9536 if (check_restricted() || check_secure())
9537 return;
9538
9539# ifdef FEAT_X11
9540 if (check_connection() == FAIL)
9541 return;
9542# endif
9543
9544 server_name = get_tv_string(&argvars[0]);
9545 keys = get_tv_string_buf(&argvars[1], buf);
9546# ifdef WIN32
9547 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
9548# else
9549 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
9550 < 0)
9551# endif
9552 {
9553 if (r != NULL)
9554 EMSG(r); /* sending worked but evaluation failed */
9555 else
9556 EMSG2(_("E241: Unable to send to %s"), server_name);
9557 return;
9558 }
9559
9560 rettv->vval.v_string = r;
9561
9562 if (argvars[2].v_type != VAR_UNKNOWN)
9563 {
9564 var v;
9565 char_u str[30];
9566
9567 sprintf((char *)str, "0x%x", (unsigned int)w);
9568 v.tv.v_type = VAR_STRING;
9569 v.tv.vval.v_string = vim_strsave(str);
9570 set_var(get_tv_string(&argvars[2]), &v.tv, FALSE);
9571 vim_free(v.tv.vval.v_string);
9572 }
9573}
9574#endif
9575
9576/*
9577 * "remote_expr()" function
9578 */
9579/*ARGSUSED*/
9580 static void
9581f_remote_expr(argvars, rettv)
9582 typeval *argvars;
9583 typeval *rettv;
9584{
9585 rettv->v_type = VAR_STRING;
9586 rettv->vval.v_string = NULL;
9587#ifdef FEAT_CLIENTSERVER
9588 remote_common(argvars, rettv, TRUE);
9589#endif
9590}
9591
9592/*
9593 * "remote_foreground()" function
9594 */
9595/*ARGSUSED*/
9596 static void
9597f_remote_foreground(argvars, rettv)
9598 typeval *argvars;
9599 typeval *rettv;
9600{
9601 rettv->vval.v_number = 0;
9602#ifdef FEAT_CLIENTSERVER
9603# ifdef WIN32
9604 /* On Win32 it's done in this application. */
9605 serverForeground(get_tv_string(&argvars[0]));
9606# else
9607 /* Send a foreground() expression to the server. */
9608 argvars[1].v_type = VAR_STRING;
9609 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
9610 argvars[2].v_type = VAR_UNKNOWN;
9611 remote_common(argvars, rettv, TRUE);
9612 vim_free(argvars[1].vval.v_string);
9613# endif
9614#endif
9615}
9616
9617/*ARGSUSED*/
9618 static void
9619f_remote_peek(argvars, rettv)
9620 typeval *argvars;
9621 typeval *rettv;
9622{
9623#ifdef FEAT_CLIENTSERVER
9624 var v;
9625 char_u *s = NULL;
9626# ifdef WIN32
9627 int n = 0;
9628# endif
9629
9630 if (check_restricted() || check_secure())
9631 {
9632 rettv->vval.v_number = -1;
9633 return;
9634 }
9635# ifdef WIN32
9636 sscanf(get_tv_string(&argvars[0]), "%x", &n);
9637 if (n == 0)
9638 rettv->vval.v_number = -1;
9639 else
9640 {
9641 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
9642 rettv->vval.v_number = (s != NULL);
9643 }
9644# else
9645 rettv->vval.v_number = 0;
9646 if (check_connection() == FAIL)
9647 return;
9648
9649 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9650 serverStrToWin(get_tv_string(&argvars[0])), &s);
9651# endif
9652
9653 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9654 {
9655 v.tv.v_type = VAR_STRING;
9656 v.tv.vval.v_string = vim_strsave(s);
9657 set_var(get_tv_string(&argvars[1]), &v.tv, FALSE);
9658 vim_free(v.tv.vval.v_string);
9659 }
9660#else
9661 rettv->vval.v_number = -1;
9662#endif
9663}
9664
9665/*ARGSUSED*/
9666 static void
9667f_remote_read(argvars, rettv)
9668 typeval *argvars;
9669 typeval *rettv;
9670{
9671 char_u *r = NULL;
9672
9673#ifdef FEAT_CLIENTSERVER
9674 if (!check_restricted() && !check_secure())
9675 {
9676# ifdef WIN32
9677 /* The server's HWND is encoded in the 'id' parameter */
9678 int n = 0;
9679
9680 sscanf(get_tv_string(&argvars[0]), "%x", &n);
9681 if (n != 0)
9682 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
9683 if (r == NULL)
9684# else
9685 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
9686 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
9687# endif
9688 EMSG(_("E277: Unable to read a server reply"));
9689 }
9690#endif
9691 rettv->v_type = VAR_STRING;
9692 rettv->vval.v_string = r;
9693}
9694
9695/*
9696 * "remote_send()" function
9697 */
9698/*ARGSUSED*/
9699 static void
9700f_remote_send(argvars, rettv)
9701 typeval *argvars;
9702 typeval *rettv;
9703{
9704 rettv->v_type = VAR_STRING;
9705 rettv->vval.v_string = NULL;
9706#ifdef FEAT_CLIENTSERVER
9707 remote_common(argvars, rettv, FALSE);
9708#endif
9709}
9710
9711/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00009712 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009713 */
9714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009715f_remove(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009716 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009717 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009718{
9719 listvar *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009720 listitem *item, *item2;
9721 listitem *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009722 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009723 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009724 char_u *key;
9725 dictvar *d;
9726 dictitem *di, **pdi;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009727
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009728 rettv->vval.v_number = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009729 if (argvars[0].v_type == VAR_DICT)
9730 {
9731 if (argvars[2].v_type != VAR_UNKNOWN)
9732 EMSG2(_(e_toomanyarg), "remove()");
9733 else if ((d = argvars[0].vval.v_dict) != NULL)
9734 {
9735 key = get_tv_string(&argvars[1]);
9736 pdi = &d->dv_first;
9737 for (di = d->dv_first; di != NULL; pdi = &di->di_next, di = *pdi)
9738 if (STRCMP(di->di_key, key) == 0)
9739 {
9740 *pdi = di->di_next;
9741 dictitem_free(di);
9742 break;
9743 }
9744 if (di == NULL)
9745 EMSG2(_(e_dictkey), key);
9746 }
9747 }
9748 else if (argvars[0].v_type != VAR_LIST)
9749 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009750 else if ((l = argvars[0].vval.v_list) != NULL)
9751 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009752 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009753 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009754 if (item == NULL)
9755 EMSGN(_(e_listidx), idx);
9756 else
9757 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009758 if (argvars[2].v_type == VAR_UNKNOWN)
9759 {
9760 /* Remove one item, return its value. */
9761 list_getrem(l, item, item);
9762 *rettv = item->li_tv;
9763 vim_free(item);
9764 }
9765 else
9766 {
9767 /* Remove range of items, return list with values. */
9768 end = get_tv_number(&argvars[2]);
9769 item2 = list_find(l, end);
9770 if (item2 == NULL)
9771 EMSGN(_(e_listidx), end);
9772 else
9773 {
9774 for (li = item; li != item2 && li != NULL; li = li->li_next)
9775 ;
9776 if (li == NULL) /* didn't find "item2" after "item" */
9777 EMSG(_(e_invrange));
9778 else
9779 {
9780 list_getrem(l, item, item2);
9781 l = list_alloc();
9782 if (l != NULL)
9783 {
9784 rettv->v_type = VAR_LIST;
9785 rettv->vval.v_list = l;
9786 l->lv_first = item;
9787 l->lv_last = item2;
9788 l->lv_refcount = 1;
9789 item->li_prev = NULL;
9790 item2->li_next = NULL;
9791 }
9792 }
9793 }
9794 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009795 }
9796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797}
9798
9799/*
9800 * "rename({from}, {to})" function
9801 */
9802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009803f_rename(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009804 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009805 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806{
9807 char_u buf[NUMBUFLEN];
9808
9809 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009810 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009812 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9813 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814}
9815
9816/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009817 * "repeat()" function
9818 */
9819/*ARGSUSED*/
9820 static void
9821f_repeat(argvars, rettv)
9822 typeval *argvars;
9823 typeval *rettv;
9824{
9825 char_u *p;
9826 int n;
9827 int slen;
9828 int len;
9829 char_u *r;
9830 int i;
9831 listvar *l;
9832
9833 n = get_tv_number(&argvars[1]);
9834 if (argvars[0].v_type == VAR_LIST)
9835 {
9836 l = list_alloc();
9837 if (l != NULL && argvars[0].vval.v_list != NULL)
9838 {
9839 l->lv_refcount = 1;
9840 while (n-- > 0)
9841 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
9842 break;
9843 }
9844 rettv->v_type = VAR_LIST;
9845 rettv->vval.v_list = l;
9846 }
9847 else
9848 {
9849 p = get_tv_string(&argvars[0]);
9850 rettv->v_type = VAR_STRING;
9851 rettv->vval.v_string = NULL;
9852
9853 slen = (int)STRLEN(p);
9854 len = slen * n;
9855 if (len <= 0)
9856 return;
9857
9858 r = alloc(len + 1);
9859 if (r != NULL)
9860 {
9861 for (i = 0; i < n; i++)
9862 mch_memmove(r + i * slen, p, (size_t)slen);
9863 r[len] = NUL;
9864 }
9865
9866 rettv->vval.v_string = r;
9867 }
9868}
9869
9870/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 * "resolve()" function
9872 */
9873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009874f_resolve(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009875 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009876 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877{
9878 char_u *p;
9879
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009880 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881#ifdef FEAT_SHORTCUT
9882 {
9883 char_u *v = NULL;
9884
9885 v = mch_resolve_shortcut(p);
9886 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009887 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009889 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 }
9891#else
9892# ifdef HAVE_READLINK
9893 {
9894 char_u buf[MAXPATHL + 1];
9895 char_u *cpy;
9896 int len;
9897 char_u *remain = NULL;
9898 char_u *q;
9899 int is_relative_to_current = FALSE;
9900 int has_trailing_pathsep = FALSE;
9901 int limit = 100;
9902
9903 p = vim_strsave(p);
9904
9905 if (p[0] == '.' && (vim_ispathsep(p[1])
9906 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9907 is_relative_to_current = TRUE;
9908
9909 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009910 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 has_trailing_pathsep = TRUE;
9912
9913 q = getnextcomp(p);
9914 if (*q != NUL)
9915 {
9916 /* Separate the first path component in "p", and keep the
9917 * remainder (beginning with the path separator). */
9918 remain = vim_strsave(q - 1);
9919 q[-1] = NUL;
9920 }
9921
9922 for (;;)
9923 {
9924 for (;;)
9925 {
9926 len = readlink((char *)p, (char *)buf, MAXPATHL);
9927 if (len <= 0)
9928 break;
9929 buf[len] = NUL;
9930
9931 if (limit-- == 0)
9932 {
9933 vim_free(p);
9934 vim_free(remain);
9935 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009936 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 goto fail;
9938 }
9939
9940 /* Ensure that the result will have a trailing path separator
9941 * if the argument has one. */
9942 if (remain == NULL && has_trailing_pathsep)
9943 add_pathsep(buf);
9944
9945 /* Separate the first path component in the link value and
9946 * concatenate the remainders. */
9947 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9948 if (*q != NUL)
9949 {
9950 if (remain == NULL)
9951 remain = vim_strsave(q - 1);
9952 else
9953 {
9954 cpy = vim_strnsave(q-1, STRLEN(q-1)+STRLEN(remain));
9955 if (cpy != NULL)
9956 {
9957 STRCAT(cpy, remain);
9958 vim_free(remain);
9959 remain = cpy;
9960 }
9961 }
9962 q[-1] = NUL;
9963 }
9964
9965 q = gettail(p);
9966 if (q > p && *q == NUL)
9967 {
9968 /* Ignore trailing path separator. */
9969 q[-1] = NUL;
9970 q = gettail(p);
9971 }
9972 if (q > p && !mch_isFullName(buf))
9973 {
9974 /* symlink is relative to directory of argument */
9975 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9976 if (cpy != NULL)
9977 {
9978 STRCPY(cpy, p);
9979 STRCPY(gettail(cpy), buf);
9980 vim_free(p);
9981 p = cpy;
9982 }
9983 }
9984 else
9985 {
9986 vim_free(p);
9987 p = vim_strsave(buf);
9988 }
9989 }
9990
9991 if (remain == NULL)
9992 break;
9993
9994 /* Append the first path component of "remain" to "p". */
9995 q = getnextcomp(remain + 1);
9996 len = q - remain - (*q != NUL);
9997 cpy = vim_strnsave(p, STRLEN(p) + len);
9998 if (cpy != NULL)
9999 {
10000 STRNCAT(cpy, remain, len);
10001 vim_free(p);
10002 p = cpy;
10003 }
10004 /* Shorten "remain". */
10005 if (*q != NUL)
10006 STRCPY(remain, q - 1);
10007 else
10008 {
10009 vim_free(remain);
10010 remain = NULL;
10011 }
10012 }
10013
10014 /* If the result is a relative path name, make it explicitly relative to
10015 * the current directory if and only if the argument had this form. */
10016 if (!vim_ispathsep(*p))
10017 {
10018 if (is_relative_to_current
10019 && *p != NUL
10020 && !(p[0] == '.'
10021 && (p[1] == NUL
10022 || vim_ispathsep(p[1])
10023 || (p[1] == '.'
10024 && (p[2] == NUL
10025 || vim_ispathsep(p[2]))))))
10026 {
10027 /* Prepend "./". */
10028 cpy = vim_strnsave((char_u *)"./", 2 + STRLEN(p));
10029 if (cpy != NULL)
10030 {
10031 STRCAT(cpy, p);
10032 vim_free(p);
10033 p = cpy;
10034 }
10035 }
10036 else if (!is_relative_to_current)
10037 {
10038 /* Strip leading "./". */
10039 q = p;
10040 while (q[0] == '.' && vim_ispathsep(q[1]))
10041 q += 2;
10042 if (q > p)
10043 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
10044 }
10045 }
10046
10047 /* Ensure that the result will have no trailing path separator
10048 * if the argument had none. But keep "/" or "//". */
10049 if (!has_trailing_pathsep)
10050 {
10051 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010052 if (after_pathsep(p, q))
10053 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 }
10055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010056 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 }
10058# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010059 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060# endif
10061#endif
10062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010063 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064
10065#ifdef HAVE_READLINK
10066fail:
10067#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010068 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069}
10070
10071/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010072 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 */
10074 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010075f_reverse(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010076 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010077 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010079 listvar *l;
10080 listitem *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081
Bram Moolenaar0d660222005-01-07 21:51:51 +000010082 rettv->vval.v_number = 0;
10083 if (argvars[0].v_type != VAR_LIST)
10084 EMSG2(_(e_listarg), "reverse()");
10085 else if ((l = argvars[0].vval.v_list) != NULL)
10086 {
10087 li = l->lv_last;
10088 l->lv_first = l->lv_last = li;
10089 while (li != NULL)
10090 {
10091 ni = li->li_prev;
10092 list_append(l, li);
10093 li = ni;
10094 }
10095 rettv->vval.v_list = l;
10096 rettv->v_type = VAR_LIST;
10097 ++l->lv_refcount;
10098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099}
10100
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010101#define SP_NOMOVE 1 /* don't move cursor */
10102#define SP_REPEAT 2 /* repeat to find outer pair */
10103#define SP_RETCOUNT 4 /* return matchcount */
10104
Bram Moolenaar0d660222005-01-07 21:51:51 +000010105static int get_search_arg __ARGS((typeval *varp, int *flagsp));
10106
10107/*
10108 * Get flags for a search function.
10109 * Possibly sets "p_ws".
10110 * Returns BACKWARD, FORWARD or zero (for an error).
10111 */
10112 static int
10113get_search_arg(varp, flagsp)
10114 typeval *varp;
10115 int *flagsp;
10116{
10117 int dir = FORWARD;
10118 char_u *flags;
10119 char_u nbuf[NUMBUFLEN];
10120 int mask;
10121
10122 if (varp->v_type != VAR_UNKNOWN)
10123 {
10124 flags = get_tv_string_buf(varp, nbuf);
10125 while (*flags != NUL)
10126 {
10127 switch (*flags)
10128 {
10129 case 'b': dir = BACKWARD; break;
10130 case 'w': p_ws = TRUE; break;
10131 case 'W': p_ws = FALSE; break;
10132 default: mask = 0;
10133 if (flagsp != NULL)
10134 switch (*flags)
10135 {
10136 case 'n': mask = SP_NOMOVE; break;
10137 case 'r': mask = SP_REPEAT; break;
10138 case 'm': mask = SP_RETCOUNT; break;
10139 }
10140 if (mask == 0)
10141 {
10142 EMSG2(_(e_invarg2), flags);
10143 dir = 0;
10144 }
10145 else
10146 *flagsp |= mask;
10147 }
10148 if (dir == 0)
10149 break;
10150 ++flags;
10151 }
10152 }
10153 return dir;
10154}
10155
Bram Moolenaar071d4272004-06-13 20:20:40 +000010156/*
10157 * "search()" function
10158 */
10159 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010160f_search(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010161 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010162 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163{
10164 char_u *pat;
10165 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010166 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 int save_p_ws = p_ws;
10168 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010169 int flags = 0;
10170
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010171 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010173 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010174 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
10175 if (dir == 0)
10176 goto theend;
10177 if ((flags & ~SP_NOMOVE) != 0)
10178 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010179 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010180 goto theend;
10181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010183 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
10185 SEARCH_KEEP, RE_SEARCH) != FAIL)
10186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010187 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 curwin->w_cursor = pos;
10189 /* "/$" will put the cursor after the end of the line, may need to
10190 * correct that here */
10191 check_cursor();
10192 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010193
10194 /* If 'n' flag is used: restore cursor position. */
10195 if (flags & SP_NOMOVE)
10196 curwin->w_cursor = save_cursor;
10197theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000010198 p_ws = save_p_ws;
10199}
10200
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201/*
10202 * "searchpair()" function
10203 */
10204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010205f_searchpair(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010206 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010207 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208{
10209 char_u *spat, *mpat, *epat;
10210 char_u *skip;
10211 char_u *pat, *pat2, *pat3;
10212 pos_T pos;
10213 pos_T firstpos;
10214 pos_T save_cursor;
10215 pos_T save_pos;
10216 int save_p_ws = p_ws;
10217 char_u *save_cpo;
10218 int dir;
10219 int flags = 0;
10220 char_u nbuf1[NUMBUFLEN];
10221 char_u nbuf2[NUMBUFLEN];
10222 char_u nbuf3[NUMBUFLEN];
10223 int n;
10224 int r;
10225 int nest = 1;
10226 int err;
10227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010228 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229
10230 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10231 save_cpo = p_cpo;
10232 p_cpo = (char_u *)"";
10233
10234 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010235 spat = get_tv_string(&argvars[0]);
10236 mpat = get_tv_string_buf(&argvars[1], nbuf1);
10237 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010238
10239 /* Make two search patterns: start/end (pat2, for in nested pairs) and
10240 * start/middle/end (pat3, for the top pair). */
10241 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
10242 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
10243 if (pat2 == NULL || pat3 == NULL)
10244 goto theend;
10245 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
10246 if (*mpat == NUL)
10247 STRCPY(pat3, pat2);
10248 else
10249 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
10250 spat, epat, mpat);
10251
10252 /* Handle the optional fourth argument: flags */
10253 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010254 if (dir == 0)
10255 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256
10257 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010258 if (argvars[3].v_type == VAR_UNKNOWN
10259 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010260 skip = (char_u *)"";
10261 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010262 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263
10264 save_cursor = curwin->w_cursor;
10265 pos = curwin->w_cursor;
10266 firstpos.lnum = 0;
10267 pat = pat3;
10268 for (;;)
10269 {
10270 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
10271 SEARCH_KEEP, RE_SEARCH);
10272 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
10273 /* didn't find it or found the first match again: FAIL */
10274 break;
10275
10276 if (firstpos.lnum == 0)
10277 firstpos = pos;
10278
10279 /* If the skip pattern matches, ignore this match. */
10280 if (*skip != NUL)
10281 {
10282 save_pos = curwin->w_cursor;
10283 curwin->w_cursor = pos;
10284 r = eval_to_bool(skip, &err, NULL, FALSE);
10285 curwin->w_cursor = save_pos;
10286 if (err)
10287 {
10288 /* Evaluating {skip} caused an error, break here. */
10289 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010290 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 break;
10292 }
10293 if (r)
10294 continue;
10295 }
10296
10297 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
10298 {
10299 /* Found end when searching backwards or start when searching
10300 * forward: nested pair. */
10301 ++nest;
10302 pat = pat2; /* nested, don't search for middle */
10303 }
10304 else
10305 {
10306 /* Found end when searching forward or start when searching
10307 * backward: end of (nested) pair; or found middle in outer pair. */
10308 if (--nest == 1)
10309 pat = pat3; /* outer level, search for middle */
10310 }
10311
10312 if (nest == 0)
10313 {
10314 /* Found the match: return matchcount or line number. */
10315 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010316 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010317 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010318 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 curwin->w_cursor = pos;
10320 if (!(flags & SP_REPEAT))
10321 break;
10322 nest = 1; /* search for next unmatched */
10323 }
10324 }
10325
10326 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010327 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 curwin->w_cursor = save_cursor;
10329
10330theend:
10331 vim_free(pat2);
10332 vim_free(pat3);
10333 p_ws = save_p_ws;
10334 p_cpo = save_cpo;
10335}
10336
Bram Moolenaar0d660222005-01-07 21:51:51 +000010337/*ARGSUSED*/
10338 static void
10339f_server2client(argvars, rettv)
10340 typeval *argvars;
10341 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010343#ifdef FEAT_CLIENTSERVER
10344 char_u buf[NUMBUFLEN];
10345 char_u *server = get_tv_string(&argvars[0]);
10346 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347
Bram Moolenaar0d660222005-01-07 21:51:51 +000010348 rettv->vval.v_number = -1;
10349 if (check_restricted() || check_secure())
10350 return;
10351# ifdef FEAT_X11
10352 if (check_connection() == FAIL)
10353 return;
10354# endif
10355
10356 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010358 EMSG(_("E258: Unable to send to client"));
10359 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000010361 rettv->vval.v_number = 0;
10362#else
10363 rettv->vval.v_number = -1;
10364#endif
10365}
10366
10367/*ARGSUSED*/
10368 static void
10369f_serverlist(argvars, rettv)
10370 typeval *argvars;
10371 typeval *rettv;
10372{
10373 char_u *r = NULL;
10374
10375#ifdef FEAT_CLIENTSERVER
10376# ifdef WIN32
10377 r = serverGetVimNames();
10378# else
10379 make_connection();
10380 if (X_DISPLAY != NULL)
10381 r = serverGetVimNames(X_DISPLAY);
10382# endif
10383#endif
10384 rettv->v_type = VAR_STRING;
10385 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010386}
10387
10388/*
10389 * "setbufvar()" function
10390 */
10391/*ARGSUSED*/
10392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010393f_setbufvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010394 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010395 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396{
10397 buf_T *buf;
10398#ifdef FEAT_AUTOCMD
10399 aco_save_T aco;
10400#else
10401 buf_T *save_curbuf;
10402#endif
10403 char_u *varname, *bufvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010404 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010405 char_u nbuf[NUMBUFLEN];
10406
10407 if (check_restricted() || check_secure())
10408 return;
10409 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010410 buf = get_buf_tv(&argvars[0]);
10411 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412 varp = &argvars[2];
10413
10414 if (buf != NULL && varname != NULL && varp != NULL)
10415 {
10416 /* set curbuf to be our buf, temporarily */
10417#ifdef FEAT_AUTOCMD
10418 aucmd_prepbuf(&aco, buf);
10419#else
10420 save_curbuf = curbuf;
10421 curbuf = buf;
10422#endif
10423
10424 if (*varname == '&')
10425 {
10426 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010427 set_option_value(varname, get_tv_number(varp),
10428 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429 }
10430 else
10431 {
10432 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
10433 if (bufvarname != NULL)
10434 {
10435 STRCPY(bufvarname, "b:");
10436 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000010437 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010438 vim_free(bufvarname);
10439 }
10440 }
10441
10442 /* reset notion of buffer */
10443#ifdef FEAT_AUTOCMD
10444 aucmd_restbuf(&aco);
10445#else
10446 curbuf = save_curbuf;
10447#endif
10448 }
10449 --emsg_off;
10450}
10451
10452/*
10453 * "setcmdpos()" function
10454 */
10455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010456f_setcmdpos(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010457 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010458 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010460 rettv->vval.v_number = set_cmdline_pos(
10461 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462}
10463
10464/*
10465 * "setline()" function
10466 */
10467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010468f_setline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010469 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010470 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471{
10472 linenr_T lnum;
10473 char_u *line;
10474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010475 lnum = get_tv_lnum(argvars);
10476 line = get_tv_string(&argvars[1]);
10477 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478
10479 if (lnum >= 1
10480 && lnum <= curbuf->b_ml.ml_line_count
10481 && u_savesub(lnum) == OK
10482 && ml_replace(lnum, line, TRUE) == OK)
10483 {
10484 changed_bytes(lnum, 0);
10485 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010486 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487 }
10488}
10489
10490/*
10491 * "setreg()" function
10492 */
10493 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010494f_setreg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010495 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010496 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497{
10498 int regname;
10499 char_u *strregname;
10500 char_u *stropt;
10501 int append;
10502 char_u yank_type;
10503 long block_len;
10504
10505 block_len = -1;
10506 yank_type = MAUTO;
10507 append = FALSE;
10508
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010509 strregname = get_tv_string(argvars);
10510 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010511
10512 regname = (strregname == NULL ? '"' : *strregname);
10513 if (regname == 0 || regname == '@')
10514 regname = '"';
10515 else if (regname == '=')
10516 return;
10517
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010518 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010520 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521 switch (*stropt)
10522 {
10523 case 'a': case 'A': /* append */
10524 append = TRUE;
10525 break;
10526 case 'v': case 'c': /* character-wise selection */
10527 yank_type = MCHAR;
10528 break;
10529 case 'V': case 'l': /* line-wise selection */
10530 yank_type = MLINE;
10531 break;
10532#ifdef FEAT_VISUAL
10533 case 'b': case Ctrl_V: /* block-wise selection */
10534 yank_type = MBLOCK;
10535 if (VIM_ISDIGIT(stropt[1]))
10536 {
10537 ++stropt;
10538 block_len = getdigits(&stropt) - 1;
10539 --stropt;
10540 }
10541 break;
10542#endif
10543 }
10544 }
10545
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010546 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000010547 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010548 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010549}
10550
10551
10552/*
10553 * "setwinvar(expr)" function
10554 */
10555/*ARGSUSED*/
10556 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010557f_setwinvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010558 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010559 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560{
10561 win_T *win;
10562#ifdef FEAT_WINDOWS
10563 win_T *save_curwin;
10564#endif
10565 char_u *varname, *winvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010566 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010567 char_u nbuf[NUMBUFLEN];
10568
10569 if (check_restricted() || check_secure())
10570 return;
10571 ++emsg_off;
10572 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010573 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 varp = &argvars[2];
10575
10576 if (win != NULL && varname != NULL && varp != NULL)
10577 {
10578#ifdef FEAT_WINDOWS
10579 /* set curwin to be our win, temporarily */
10580 save_curwin = curwin;
10581 curwin = win;
10582 curbuf = curwin->w_buffer;
10583#endif
10584
10585 if (*varname == '&')
10586 {
10587 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010588 set_option_value(varname, get_tv_number(varp),
10589 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 }
10591 else
10592 {
10593 winvarname = alloc((unsigned)STRLEN(varname) + 3);
10594 if (winvarname != NULL)
10595 {
10596 STRCPY(winvarname, "w:");
10597 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000010598 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010599 vim_free(winvarname);
10600 }
10601 }
10602
10603#ifdef FEAT_WINDOWS
10604 /* Restore current window, if it's still valid (autocomands can make
10605 * it invalid). */
10606 if (win_valid(save_curwin))
10607 {
10608 curwin = save_curwin;
10609 curbuf = curwin->w_buffer;
10610 }
10611#endif
10612 }
10613 --emsg_off;
10614}
10615
10616/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010617 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 */
10619 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010620f_simplify(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010621 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010622 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010624 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625
Bram Moolenaar0d660222005-01-07 21:51:51 +000010626 p = get_tv_string(&argvars[0]);
10627 rettv->vval.v_string = vim_strsave(p);
10628 simplify_filename(rettv->vval.v_string); /* simplify in place */
10629 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630}
10631
Bram Moolenaar0d660222005-01-07 21:51:51 +000010632static int
10633#ifdef __BORLANDC__
10634 _RTLENTRYF
10635#endif
10636 item_compare __ARGS((const void *s1, const void *s2));
10637static int
10638#ifdef __BORLANDC__
10639 _RTLENTRYF
10640#endif
10641 item_compare2 __ARGS((const void *s1, const void *s2));
10642
10643static int item_compare_ic;
10644static char_u *item_compare_func;
10645#define ITEM_COMPARE_FAIL 999
10646
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010648 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010650 static int
10651#ifdef __BORLANDC__
10652_RTLENTRYF
10653#endif
10654item_compare(s1, s2)
10655 const void *s1;
10656 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010658 char_u *p1, *p2;
10659 char_u *tofree1, *tofree2;
10660 int res;
10661 char_u numbuf1[NUMBUFLEN];
10662 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010663
Bram Moolenaar0d660222005-01-07 21:51:51 +000010664 p1 = tv2string(&(*(listitem **)s1)->li_tv, &tofree1, numbuf1);
10665 p2 = tv2string(&(*(listitem **)s2)->li_tv, &tofree2, numbuf2);
10666 if (item_compare_ic)
10667 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000010669 res = STRCMP(p1, p2);
10670 vim_free(tofree1);
10671 vim_free(tofree2);
10672 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673}
10674
10675 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000010676#ifdef __BORLANDC__
10677_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000010679item_compare2(s1, s2)
10680 const void *s1;
10681 const void *s2;
10682{
10683 int res;
10684 typeval rettv;
10685 typeval argv[2];
10686 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010687
Bram Moolenaar0d660222005-01-07 21:51:51 +000010688 /* copy the values (is this really needed?) */
10689 copy_tv(&(*(listitem **)s1)->li_tv, &argv[0]);
10690 copy_tv(&(*(listitem **)s2)->li_tv, &argv[1]);
10691
10692 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
10693 res = call_func(item_compare_func, STRLEN(item_compare_func),
10694 &rettv, 2, argv, 0L, 0L, &dummy, TRUE);
10695 clear_tv(&argv[0]);
10696 clear_tv(&argv[1]);
10697
10698 if (res == FAIL)
10699 res = ITEM_COMPARE_FAIL;
10700 else
10701 res = get_tv_number(&rettv);
10702 clear_tv(&rettv);
10703 return res;
10704}
10705
10706/*
10707 * "sort({list})" function
10708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010710f_sort(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010711 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010712 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010714 listvar *l;
10715 listitem *li;
10716 listitem **ptrs;
10717 long len;
10718 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719
Bram Moolenaar0d660222005-01-07 21:51:51 +000010720 rettv->vval.v_number = 0;
10721 if (argvars[0].v_type != VAR_LIST)
10722 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010723 else
10724 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010725 l = argvars[0].vval.v_list;
10726 if (l == NULL)
10727 return;
10728 rettv->vval.v_list = l;
10729 rettv->v_type = VAR_LIST;
10730 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731
Bram Moolenaar0d660222005-01-07 21:51:51 +000010732 len = list_len(l);
10733 if (len <= 1)
10734 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010735
Bram Moolenaar0d660222005-01-07 21:51:51 +000010736 item_compare_ic = FALSE;
10737 item_compare_func = NULL;
10738 if (argvars[1].v_type != VAR_UNKNOWN)
10739 {
10740 if (argvars[1].v_type == VAR_FUNC)
10741 item_compare_func = argvars[0].vval.v_string;
10742 else
10743 {
10744 i = get_tv_number(&argvars[1]);
10745 if (i == 1)
10746 item_compare_ic = TRUE;
10747 else
10748 item_compare_func = get_tv_string(&argvars[1]);
10749 }
10750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751
Bram Moolenaar0d660222005-01-07 21:51:51 +000010752 /* Make an array with each entry pointing to an item in the List. */
10753 ptrs = (listitem **)alloc((int)(len * sizeof(listitem *)));
10754 if (ptrs == NULL)
10755 return;
10756 i = 0;
10757 for (li = l->lv_first; li != NULL; li = li->li_next)
10758 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759
Bram Moolenaar0d660222005-01-07 21:51:51 +000010760 /* test the compare function */
10761 if (item_compare_func != NULL
10762 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
10763 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010764 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000010766 {
10767 /* Sort the array with item pointers. */
10768 qsort((void *)ptrs, (size_t)len, sizeof(listitem *),
10769 item_compare_func == NULL ? item_compare : item_compare2);
10770
10771 /* Clear the List and append the items in the sorted order. */
10772 l->lv_first = l->lv_last = NULL;
10773 for (i = 0; i < len; ++i)
10774 list_append(l, ptrs[i]);
10775 }
10776
10777 vim_free(ptrs);
10778 }
10779}
10780
10781 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010782f_split(argvars, rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010783 typeval *argvars;
10784 typeval *rettv;
10785{
10786 char_u *str;
10787 char_u *end;
10788 char_u *pat;
10789 regmatch_T regmatch;
10790 char_u patbuf[NUMBUFLEN];
10791 char_u *save_cpo;
10792 int match;
10793 listitem *ni;
10794 listvar *l;
10795 colnr_T col = 0;
10796
10797 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10798 save_cpo = p_cpo;
10799 p_cpo = (char_u *)"";
10800
10801 str = get_tv_string(&argvars[0]);
10802 if (argvars[1].v_type == VAR_UNKNOWN)
10803 pat = (char_u *)"[\\x01- ]\\+";
10804 else
10805 pat = get_tv_string_buf(&argvars[1], patbuf);
10806
10807 l = list_alloc();
10808 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010810 rettv->v_type = VAR_LIST;
10811 rettv->vval.v_list = l;
10812 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813
Bram Moolenaar0d660222005-01-07 21:51:51 +000010814 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10815 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010817 regmatch.rm_ic = FALSE;
10818 while (*str != NUL)
10819 {
10820 match = vim_regexec_nl(&regmatch, str, col);
10821 if (match)
10822 end = regmatch.startp[0];
10823 else
10824 end = str + STRLEN(str);
10825 if (end > str)
10826 {
10827 ni = listitem_alloc();
10828 if (ni == NULL)
10829 break;
10830 ni->li_tv.v_type = VAR_STRING;
10831 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
10832 list_append(l, ni);
10833 }
10834 if (!match)
10835 break;
10836 /* Advance to just after the match. */
10837 if (regmatch.endp[0] > str)
10838 col = 0;
10839 else
10840 {
10841 /* Don't get stuck at the same match. */
10842#ifdef FEAT_MBYTE
10843 col = mb_ptr2len_check(regmatch.endp[0]);
10844#else
10845 col = 1;
10846#endif
10847 }
10848 str = regmatch.endp[0];
10849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850
Bram Moolenaar0d660222005-01-07 21:51:51 +000010851 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010853
Bram Moolenaar0d660222005-01-07 21:51:51 +000010854 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855}
10856
10857#ifdef HAVE_STRFTIME
10858/*
10859 * "strftime({format}[, {time}])" function
10860 */
10861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010862f_strftime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010863 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010864 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865{
10866 char_u result_buf[256];
10867 struct tm *curtime;
10868 time_t seconds;
10869 char_u *p;
10870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010871 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010873 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010874 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 seconds = time(NULL);
10876 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010877 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 curtime = localtime(&seconds);
10879 /* MSVC returns NULL for an invalid value of seconds. */
10880 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010881 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 else
10883 {
10884# ifdef FEAT_MBYTE
10885 vimconv_T conv;
10886 char_u *enc;
10887
10888 conv.vc_type = CONV_NONE;
10889 enc = enc_locale();
10890 convert_setup(&conv, p_enc, enc);
10891 if (conv.vc_type != CONV_NONE)
10892 p = string_convert(&conv, p, NULL);
10893# endif
10894 if (p != NULL)
10895 (void)strftime((char *)result_buf, sizeof(result_buf),
10896 (char *)p, curtime);
10897 else
10898 result_buf[0] = NUL;
10899
10900# ifdef FEAT_MBYTE
10901 if (conv.vc_type != CONV_NONE)
10902 vim_free(p);
10903 convert_setup(&conv, enc, p_enc);
10904 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010905 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010906 else
10907# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010908 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909
10910# ifdef FEAT_MBYTE
10911 /* Release conversion descriptors */
10912 convert_setup(&conv, NULL, NULL);
10913 vim_free(enc);
10914# endif
10915 }
10916}
10917#endif
10918
10919/*
10920 * "stridx()" function
10921 */
10922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010923f_stridx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010924 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010925 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926{
10927 char_u buf[NUMBUFLEN];
10928 char_u *needle;
10929 char_u *haystack;
10930 char_u *pos;
10931
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010932 needle = get_tv_string(&argvars[1]);
10933 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934 pos = (char_u *)strstr((char *)haystack, (char *)needle);
10935
10936 if (pos == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010937 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010939 rettv->vval.v_number = (varnumber_T) (pos - haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940}
10941
10942/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010943 * "string()" function
10944 */
10945 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010946f_string(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010947 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010948 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010949{
10950 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010951 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010952
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010953 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010954 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010955 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010956 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957}
10958
10959/*
10960 * "strlen()" function
10961 */
10962 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010963f_strlen(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010964 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010965 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010967 rettv->vval.v_number = (varnumber_T)(STRLEN(
10968 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969}
10970
10971/*
10972 * "strpart()" function
10973 */
10974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010975f_strpart(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010976 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010977 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978{
10979 char_u *p;
10980 int n;
10981 int len;
10982 int slen;
10983
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010984 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010985 slen = (int)STRLEN(p);
10986
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010987 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010988 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010989 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 else
10991 len = slen - n; /* default len: all bytes that are available. */
10992
10993 /*
10994 * Only return the overlap between the specified part and the actual
10995 * string.
10996 */
10997 if (n < 0)
10998 {
10999 len += n;
11000 n = 0;
11001 }
11002 else if (n > slen)
11003 n = slen;
11004 if (len < 0)
11005 len = 0;
11006 else if (n + len > slen)
11007 len = slen - n;
11008
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011009 rettv->v_type = VAR_STRING;
11010 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011011}
11012
11013/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011014 * "strridx()" function
11015 */
11016 static void
11017f_strridx(argvars, rettv)
11018 typeval *argvars;
11019 typeval *rettv;
11020{
11021 char_u buf[NUMBUFLEN];
11022 char_u *needle;
11023 char_u *haystack;
11024 char_u *rest;
11025 char_u *lastmatch = NULL;
11026
11027 needle = get_tv_string(&argvars[1]);
11028 haystack = get_tv_string_buf(&argvars[0], buf);
11029 if (*needle == NUL)
11030 /* Empty string matches past the end. */
11031 lastmatch = haystack + STRLEN(haystack);
11032 else
11033 for (rest = haystack; *rest != '\0'; ++rest)
11034 {
11035 rest = (char_u *)strstr((char *)rest, (char *)needle);
11036 if (rest == NULL)
11037 break;
11038 lastmatch = rest;
11039 }
11040
11041 if (lastmatch == NULL)
11042 rettv->vval.v_number = -1;
11043 else
11044 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
11045}
11046
11047/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048 * "strtrans()" function
11049 */
11050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011051f_strtrans(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011052 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011053 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011055 rettv->v_type = VAR_STRING;
11056 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057}
11058
11059/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011060 * "submatch()" function
11061 */
11062 static void
11063f_submatch(argvars, rettv)
11064 typeval *argvars;
11065 typeval *rettv;
11066{
11067 rettv->v_type = VAR_STRING;
11068 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
11069}
11070
11071/*
11072 * "substitute()" function
11073 */
11074 static void
11075f_substitute(argvars, rettv)
11076 typeval *argvars;
11077 typeval *rettv;
11078{
11079 char_u patbuf[NUMBUFLEN];
11080 char_u subbuf[NUMBUFLEN];
11081 char_u flagsbuf[NUMBUFLEN];
11082
11083 rettv->v_type = VAR_STRING;
11084 rettv->vval.v_string = do_string_sub(
11085 get_tv_string(&argvars[0]),
11086 get_tv_string_buf(&argvars[1], patbuf),
11087 get_tv_string_buf(&argvars[2], subbuf),
11088 get_tv_string_buf(&argvars[3], flagsbuf));
11089}
11090
11091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092 * "synID(line, col, trans)" function
11093 */
11094/*ARGSUSED*/
11095 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011096f_synID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011097 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011098 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011099{
11100 int id = 0;
11101#ifdef FEAT_SYN_HL
11102 long line;
11103 long col;
11104 int trans;
11105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011106 line = get_tv_lnum(argvars);
11107 col = get_tv_number(&argvars[1]) - 1;
11108 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109
11110 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
11111 && col >= 0 && col < (long)STRLEN(ml_get(line)))
11112 id = syn_get_id(line, col, trans);
11113#endif
11114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011115 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011116}
11117
11118/*
11119 * "synIDattr(id, what [, mode])" function
11120 */
11121/*ARGSUSED*/
11122 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011123f_synIDattr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011124 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011125 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126{
11127 char_u *p = NULL;
11128#ifdef FEAT_SYN_HL
11129 int id;
11130 char_u *what;
11131 char_u *mode;
11132 char_u modebuf[NUMBUFLEN];
11133 int modec;
11134
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011135 id = get_tv_number(&argvars[0]);
11136 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011137 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011138 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011139 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 modec = TOLOWER_ASC(mode[0]);
11141 if (modec != 't' && modec != 'c'
11142#ifdef FEAT_GUI
11143 && modec != 'g'
11144#endif
11145 )
11146 modec = 0; /* replace invalid with current */
11147 }
11148 else
11149 {
11150#ifdef FEAT_GUI
11151 if (gui.in_use)
11152 modec = 'g';
11153 else
11154#endif
11155 if (t_colors > 1)
11156 modec = 'c';
11157 else
11158 modec = 't';
11159 }
11160
11161
11162 switch (TOLOWER_ASC(what[0]))
11163 {
11164 case 'b':
11165 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
11166 p = highlight_color(id, what, modec);
11167 else /* bold */
11168 p = highlight_has_attr(id, HL_BOLD, modec);
11169 break;
11170
11171 case 'f': /* fg[#] */
11172 p = highlight_color(id, what, modec);
11173 break;
11174
11175 case 'i':
11176 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
11177 p = highlight_has_attr(id, HL_INVERSE, modec);
11178 else /* italic */
11179 p = highlight_has_attr(id, HL_ITALIC, modec);
11180 break;
11181
11182 case 'n': /* name */
11183 p = get_highlight_name(NULL, id - 1);
11184 break;
11185
11186 case 'r': /* reverse */
11187 p = highlight_has_attr(id, HL_INVERSE, modec);
11188 break;
11189
11190 case 's': /* standout */
11191 p = highlight_has_attr(id, HL_STANDOUT, modec);
11192 break;
11193
11194 case 'u': /* underline */
11195 p = highlight_has_attr(id, HL_UNDERLINE, modec);
11196 break;
11197 }
11198
11199 if (p != NULL)
11200 p = vim_strsave(p);
11201#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011202 rettv->v_type = VAR_STRING;
11203 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204}
11205
11206/*
11207 * "synIDtrans(id)" function
11208 */
11209/*ARGSUSED*/
11210 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011211f_synIDtrans(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011212 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011213 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011214{
11215 int id;
11216
11217#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011219
11220 if (id > 0)
11221 id = syn_get_final_id(id);
11222 else
11223#endif
11224 id = 0;
11225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011226 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011227}
11228
11229/*
11230 * "system()" function
11231 */
11232 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011233f_system(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011234 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011235 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011236{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011237 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011238 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011239 char_u *infile = NULL;
11240 char_u buf[NUMBUFLEN];
11241 int err = FALSE;
11242 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011243
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011244 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011245 {
11246 /*
11247 * Write the string to a temp file, to be used for input of the shell
11248 * command.
11249 */
11250 if ((infile = vim_tempname('i')) == NULL)
11251 {
11252 EMSG(_(e_notmp));
11253 return;
11254 }
11255
11256 fd = mch_fopen((char *)infile, WRITEBIN);
11257 if (fd == NULL)
11258 {
11259 EMSG2(_(e_notopen), infile);
11260 goto done;
11261 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011262 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011263 if (fwrite(p, STRLEN(p), 1, fd) != 1)
11264 err = TRUE;
11265 if (fclose(fd) != 0)
11266 err = TRUE;
11267 if (err)
11268 {
11269 EMSG(_("E677: Error writing temp file"));
11270 goto done;
11271 }
11272 }
11273
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011274 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011275
Bram Moolenaar071d4272004-06-13 20:20:40 +000011276#ifdef USE_CR
11277 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011278 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011279 {
11280 char_u *s;
11281
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011282 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 {
11284 if (*s == CAR)
11285 *s = NL;
11286 }
11287 }
11288#else
11289# ifdef USE_CRNL
11290 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011291 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 {
11293 char_u *s, *d;
11294
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011295 d = res;
11296 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297 {
11298 if (s[0] == CAR && s[1] == NL)
11299 ++s;
11300 *d++ = *s;
11301 }
11302 *d = NUL;
11303 }
11304# endif
11305#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000011306
11307done:
11308 if (infile != NULL)
11309 {
11310 mch_remove(infile);
11311 vim_free(infile);
11312 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011313 rettv->v_type = VAR_STRING;
11314 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315}
11316
11317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318 * "tempname()" function
11319 */
11320/*ARGSUSED*/
11321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011322f_tempname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011323 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011324 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325{
11326 static int x = 'A';
11327
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011328 rettv->v_type = VAR_STRING;
11329 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330
11331 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
11332 * names. Skip 'I' and 'O', they are used for shell redirection. */
11333 do
11334 {
11335 if (x == 'Z')
11336 x = '0';
11337 else if (x == '9')
11338 x = 'A';
11339 else
11340 {
11341#ifdef EBCDIC
11342 if (x == 'I')
11343 x = 'J';
11344 else if (x == 'R')
11345 x = 'S';
11346 else
11347#endif
11348 ++x;
11349 }
11350 } while (x == 'I' || x == 'O');
11351}
11352
11353/*
11354 * "tolower(string)" function
11355 */
11356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011357f_tolower(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011358 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011359 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360{
11361 char_u *p;
11362
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011363 p = vim_strsave(get_tv_string(&argvars[0]));
11364 rettv->v_type = VAR_STRING;
11365 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366
11367 if (p != NULL)
11368 while (*p != NUL)
11369 {
11370#ifdef FEAT_MBYTE
11371 int l;
11372
11373 if (enc_utf8)
11374 {
11375 int c, lc;
11376
11377 c = utf_ptr2char(p);
11378 lc = utf_tolower(c);
11379 l = utf_ptr2len_check(p);
11380 /* TODO: reallocate string when byte count changes. */
11381 if (utf_char2len(lc) == l)
11382 utf_char2bytes(lc, p);
11383 p += l;
11384 }
11385 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
11386 p += l; /* skip multi-byte character */
11387 else
11388#endif
11389 {
11390 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
11391 ++p;
11392 }
11393 }
11394}
11395
11396/*
11397 * "toupper(string)" function
11398 */
11399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011400f_toupper(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011401 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011402 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403{
11404 char_u *p;
11405
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011406 p = vim_strsave(get_tv_string(&argvars[0]));
11407 rettv->v_type = VAR_STRING;
11408 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409
11410 if (p != NULL)
11411 while (*p != NUL)
11412 {
11413#ifdef FEAT_MBYTE
11414 int l;
11415
11416 if (enc_utf8)
11417 {
11418 int c, uc;
11419
11420 c = utf_ptr2char(p);
11421 uc = utf_toupper(c);
11422 l = utf_ptr2len_check(p);
11423 /* TODO: reallocate string when byte count changes. */
11424 if (utf_char2len(uc) == l)
11425 utf_char2bytes(uc, p);
11426 p += l;
11427 }
11428 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
11429 p += l; /* skip multi-byte character */
11430 else
11431#endif
11432 {
11433 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
11434 p++;
11435 }
11436 }
11437}
11438
11439/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000011440 * "tr(string, fromstr, tostr)" function
11441 */
11442 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011443f_tr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011444 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445 typeval *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000011446{
11447 char_u *instr;
11448 char_u *fromstr;
11449 char_u *tostr;
11450 char_u *p;
11451#ifdef FEAT_MBYTE
11452 int inlen;
11453 int fromlen;
11454 int tolen;
11455 int idx;
11456 char_u *cpstr;
11457 int cplen;
11458 int first = TRUE;
11459#endif
11460 char_u buf[NUMBUFLEN];
11461 char_u buf2[NUMBUFLEN];
11462 garray_T ga;
11463
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011464 instr = get_tv_string(&argvars[0]);
11465 fromstr = get_tv_string_buf(&argvars[1], buf);
11466 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000011467
11468 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469 rettv->v_type = VAR_STRING;
11470 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000011471 ga_init2(&ga, (int)sizeof(char), 80);
11472
11473#ifdef FEAT_MBYTE
11474 if (!has_mbyte)
11475#endif
11476 /* not multi-byte: fromstr and tostr must be the same length */
11477 if (STRLEN(fromstr) != STRLEN(tostr))
11478 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011479#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000011480error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011481#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000011482 EMSG2(_(e_invarg2), fromstr);
11483 ga_clear(&ga);
11484 return;
11485 }
11486
11487 /* fromstr and tostr have to contain the same number of chars */
11488 while (*instr != NUL)
11489 {
11490#ifdef FEAT_MBYTE
11491 if (has_mbyte)
11492 {
11493 inlen = mb_ptr2len_check(instr);
11494 cpstr = instr;
11495 cplen = inlen;
11496 idx = 0;
11497 for (p = fromstr; *p != NUL; p += fromlen)
11498 {
11499 fromlen = mb_ptr2len_check(p);
11500 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
11501 {
11502 for (p = tostr; *p != NUL; p += tolen)
11503 {
11504 tolen = mb_ptr2len_check(p);
11505 if (idx-- == 0)
11506 {
11507 cplen = tolen;
11508 cpstr = p;
11509 break;
11510 }
11511 }
11512 if (*p == NUL) /* tostr is shorter than fromstr */
11513 goto error;
11514 break;
11515 }
11516 ++idx;
11517 }
11518
11519 if (first && cpstr == instr)
11520 {
11521 /* Check that fromstr and tostr have the same number of
11522 * (multi-byte) characters. Done only once when a character
11523 * of instr doesn't appear in fromstr. */
11524 first = FALSE;
11525 for (p = tostr; *p != NUL; p += tolen)
11526 {
11527 tolen = mb_ptr2len_check(p);
11528 --idx;
11529 }
11530 if (idx != 0)
11531 goto error;
11532 }
11533
11534 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000011535 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000011536 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000011537
11538 instr += inlen;
11539 }
11540 else
11541#endif
11542 {
11543 /* When not using multi-byte chars we can do it faster. */
11544 p = vim_strchr(fromstr, *instr);
11545 if (p != NULL)
11546 ga_append(&ga, tostr[p - fromstr]);
11547 else
11548 ga_append(&ga, *instr);
11549 ++instr;
11550 }
11551 }
11552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011553 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000011554}
11555
11556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011557 * "type(expr)" function
11558 */
11559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011560f_type(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011561 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011562 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011563{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000011564 int n;
11565
11566 switch (argvars[0].v_type)
11567 {
11568 case VAR_NUMBER: n = 0; break;
11569 case VAR_STRING: n = 1; break;
11570 case VAR_FUNC: n = 2; break;
11571 case VAR_LIST: n = 3; break;
11572 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
11573 }
11574 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011575}
11576
11577/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000011578 * "values(dict)" function
11579 */
11580 static void
11581f_values(argvars, rettv)
11582 typeval *argvars;
11583 typeval *rettv;
11584{
11585 dict_list(argvars, rettv, 1);
11586}
11587
11588/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589 * "virtcol(string)" function
11590 */
11591 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011592f_virtcol(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011593 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011594 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011595{
11596 colnr_T vcol = 0;
11597 pos_T *fp;
11598
11599 fp = var2fpos(&argvars[0], FALSE);
11600 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
11601 {
11602 getvvcol(curwin, fp, NULL, NULL, &vcol);
11603 ++vcol;
11604 }
11605
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011606 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607}
11608
11609/*
11610 * "visualmode()" function
11611 */
11612/*ARGSUSED*/
11613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011614f_visualmode(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011615 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011616 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011617{
11618#ifdef FEAT_VISUAL
11619 char_u str[2];
11620
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011621 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011622 str[0] = curbuf->b_visual_mode_eval;
11623 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011624 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625
11626 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011627 if ((argvars[0].v_type == VAR_NUMBER
11628 && argvars[0].vval.v_number != 0)
11629 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011630 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631 curbuf->b_visual_mode_eval = NUL;
11632#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011633 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634#endif
11635}
11636
11637/*
11638 * "winbufnr(nr)" function
11639 */
11640 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011641f_winbufnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011642 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011643 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011644{
11645 win_T *wp;
11646
11647 wp = find_win_by_nr(&argvars[0]);
11648 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011649 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011651 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652}
11653
11654/*
11655 * "wincol()" function
11656 */
11657/*ARGSUSED*/
11658 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011659f_wincol(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011660 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011661 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011662{
11663 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011664 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011665}
11666
11667/*
11668 * "winheight(nr)" function
11669 */
11670 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011671f_winheight(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011672 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011673 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011674{
11675 win_T *wp;
11676
11677 wp = find_win_by_nr(&argvars[0]);
11678 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011679 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011681 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011682}
11683
11684/*
11685 * "winline()" function
11686 */
11687/*ARGSUSED*/
11688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011689f_winline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011690 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011691 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692{
11693 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011694 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011695}
11696
11697/*
11698 * "winnr()" function
11699 */
11700/* ARGSUSED */
11701 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011702f_winnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011703 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011704 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011705{
11706 int nr = 1;
11707#ifdef FEAT_WINDOWS
11708 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011709 win_T *twin = curwin;
11710 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011711
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011712 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011713 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011714 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011715 if (STRCMP(arg, "$") == 0)
11716 twin = lastwin;
11717 else if (STRCMP(arg, "#") == 0)
11718 {
11719 twin = prevwin;
11720 if (prevwin == NULL)
11721 nr = 0;
11722 }
11723 else
11724 {
11725 EMSG2(_(e_invexpr2), arg);
11726 nr = 0;
11727 }
11728 }
11729
11730 if (nr > 0)
11731 for (wp = firstwin; wp != twin; wp = wp->w_next)
11732 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011734 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011735}
11736
11737/*
11738 * "winrestcmd()" function
11739 */
11740/* ARGSUSED */
11741 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011742f_winrestcmd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011743 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011744 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011745{
11746#ifdef FEAT_WINDOWS
11747 win_T *wp;
11748 int winnr = 1;
11749 garray_T ga;
11750 char_u buf[50];
11751
11752 ga_init2(&ga, (int)sizeof(char), 70);
11753 for (wp = firstwin; wp != NULL; wp = wp->w_next)
11754 {
11755 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
11756 ga_concat(&ga, buf);
11757# ifdef FEAT_VERTSPLIT
11758 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
11759 ga_concat(&ga, buf);
11760# endif
11761 ++winnr;
11762 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000011763 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011765 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011767 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011769 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770}
11771
11772/*
11773 * "winwidth(nr)" function
11774 */
11775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011776f_winwidth(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011777 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011778 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011779{
11780 win_T *wp;
11781
11782 wp = find_win_by_nr(&argvars[0]);
11783 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011784 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785 else
11786#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011787 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011789 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011790#endif
11791}
11792
11793 static win_T *
11794find_win_by_nr(vp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011795 typeval *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796{
11797#ifdef FEAT_WINDOWS
11798 win_T *wp;
11799#endif
11800 int nr;
11801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803
11804#ifdef FEAT_WINDOWS
11805 if (nr == 0)
11806 return curwin;
11807
11808 for (wp = firstwin; wp != NULL; wp = wp->w_next)
11809 if (--nr <= 0)
11810 break;
11811 return wp;
11812#else
11813 if (nr == 0 || nr == 1)
11814 return curwin;
11815 return NULL;
11816#endif
11817}
11818
11819/*
11820 * Translate a String variable into a position.
11821 */
11822 static pos_T *
11823var2fpos(varp, lnum)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011824 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825 int lnum; /* TRUE when $ is last line */
11826{
11827 char_u *name;
11828 static pos_T pos;
11829 pos_T *pp;
11830
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011831 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832 if (name[0] == '.') /* cursor */
11833 return &curwin->w_cursor;
11834 if (name[0] == '\'') /* mark */
11835 {
11836 pp = getmark(name[1], FALSE);
11837 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
11838 return NULL;
11839 return pp;
11840 }
11841 if (name[0] == '$') /* last column or line */
11842 {
11843 if (lnum)
11844 {
11845 pos.lnum = curbuf->b_ml.ml_line_count;
11846 pos.col = 0;
11847 }
11848 else
11849 {
11850 pos.lnum = curwin->w_cursor.lnum;
11851 pos.col = (colnr_T)STRLEN(ml_get_curline());
11852 }
11853 return &pos;
11854 }
11855 return NULL;
11856}
11857
11858/*
11859 * Get the length of an environment variable name.
11860 * Advance "arg" to the first character after the name.
11861 * Return 0 for error.
11862 */
11863 static int
11864get_env_len(arg)
11865 char_u **arg;
11866{
11867 char_u *p;
11868 int len;
11869
11870 for (p = *arg; vim_isIDc(*p); ++p)
11871 ;
11872 if (p == *arg) /* no name found */
11873 return 0;
11874
11875 len = (int)(p - *arg);
11876 *arg = p;
11877 return len;
11878}
11879
11880/*
11881 * Get the length of the name of a function or internal variable.
11882 * "arg" is advanced to the first non-white character after the name.
11883 * Return 0 if something is wrong.
11884 */
11885 static int
11886get_id_len(arg)
11887 char_u **arg;
11888{
11889 char_u *p;
11890 int len;
11891
11892 /* Find the end of the name. */
11893 for (p = *arg; eval_isnamec(*p); ++p)
11894 ;
11895 if (p == *arg) /* no name found */
11896 return 0;
11897
11898 len = (int)(p - *arg);
11899 *arg = skipwhite(p);
11900
11901 return len;
11902}
11903
11904/*
11905 * Get the length of the name of a function.
11906 * "arg" is advanced to the first non-white character after the name.
11907 * Return 0 if something is wrong.
11908 * If the name contains 'magic' {}'s, expand them and return the
11909 * expanded name in an allocated string via 'alias' - caller must free.
11910 */
11911 static int
11912get_func_len(arg, alias, evaluate)
11913 char_u **arg;
11914 char_u **alias;
11915 int evaluate;
11916{
11917 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011918 char_u *p;
11919 char_u *expr_start;
11920 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921
11922 *alias = NULL; /* default to no alias */
11923
11924 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
11925 && (*arg)[2] == (int)KE_SNR)
11926 {
11927 /* hard coded <SNR>, already translated */
11928 *arg += 3;
11929 return get_id_len(arg) + 3;
11930 }
11931 len = eval_fname_script(*arg);
11932 if (len > 0)
11933 {
11934 /* literal "<SID>", "s:" or "<SNR>" */
11935 *arg += len;
11936 }
11937
Bram Moolenaar071d4272004-06-13 20:20:40 +000011938 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011939 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011941 p = find_name_end(*arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942 if (expr_start != NULL)
11943 {
11944 char_u *temp_string;
11945
11946 if (!evaluate)
11947 {
11948 len += (int)(p - *arg);
11949 *arg = skipwhite(p);
11950 return len;
11951 }
11952
11953 /*
11954 * Include any <SID> etc in the expanded string:
11955 * Thus the -len here.
11956 */
11957 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
11958 if (temp_string == NULL)
11959 return 0;
11960 *alias = temp_string;
11961 *arg = skipwhite(p);
11962 return (int)STRLEN(temp_string);
11963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011964
11965 len += get_id_len(arg);
11966 if (len == 0)
11967 EMSG2(_(e_invexpr2), *arg);
11968
11969 return len;
11970}
11971
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011972/*
11973 * Find the end of a variable or function name, taking care of magic braces.
11974 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
11975 * start and end of the first magic braces item.
11976 * Return a pointer to just after the name. Equal to "arg" if there is no
11977 * valid name.
11978 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011980find_name_end(arg, expr_start, expr_end, incl_br)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011981 char_u *arg;
11982 char_u **expr_start;
11983 char_u **expr_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000011984 int incl_br; /* Include [] indexes and .name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011986 int mb_nest = 0;
11987 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 char_u *p;
11989
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011990 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011992 *expr_start = NULL;
11993 *expr_end = NULL;
11994 }
11995
11996 for (p = arg; *p != NUL
11997 && (eval_isnamec(*p)
Bram Moolenaar8c711452005-01-14 21:53:12 +000011998 || (incl_br && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011999 || mb_nest != 0
12000 || br_nest != 0); ++p)
12001 {
12002 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012004 if (*p == '[')
12005 ++br_nest;
12006 else if (*p == ']')
12007 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012009 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012011 if (*p == '{')
12012 {
12013 mb_nest++;
12014 if (expr_start != NULL && *expr_start == NULL)
12015 *expr_start = p;
12016 }
12017 else if (*p == '}')
12018 {
12019 mb_nest--;
12020 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
12021 *expr_end = p;
12022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012024 }
12025
12026 return p;
12027}
12028
12029/*
12030 * Return TRUE if character "c" can be used in a variable or function name.
12031 */
12032 static int
12033eval_isnamec(c)
12034 int c;
12035{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012036 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == '{' || c == '}');
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037}
12038
12039/*
12040 * Find a v: variable.
12041 * Return it's index, or -1 if not found.
12042 */
12043 static int
12044find_vim_var(name, len)
12045 char_u *name;
12046 int len; /* length of "name" */
12047{
12048 char_u *vname;
12049 int vlen;
12050 int i;
12051
12052 /*
12053 * Ignore "v:" for old built-in variables, require it for new ones.
12054 */
12055 if (name[0] == 'v' && name[1] == ':')
12056 {
12057 vname = name + 2;
12058 vlen = len - 2;
12059 }
12060 else
12061 {
12062 vname = name;
12063 vlen = len;
12064 }
12065 for (i = 0; i < VV_LEN; ++i)
12066 if (vlen == vimvars[i].len && STRCMP(vname, vimvars[i].name) == 0
12067 && ((vimvars[i].flags & VV_COMPAT) || vname != name))
12068 return i;
12069 return -1;
12070}
12071
12072/*
12073 * Set number v: variable to "val".
12074 */
12075 void
12076set_vim_var_nr(idx, val)
12077 int idx;
12078 long val;
12079{
12080 vimvars[idx].val = (char_u *)val;
12081}
12082
12083/*
12084 * Get number v: variable value;
12085 */
12086 long
12087get_vim_var_nr(idx)
12088 int idx;
12089{
12090 return (long)vimvars[idx].val;
12091}
12092
12093/*
12094 * Set v:count, v:count1 and v:prevcount.
12095 */
12096 void
12097set_vcount(count, count1)
12098 long count;
12099 long count1;
12100{
12101 vimvars[VV_PREVCOUNT].val = vimvars[VV_COUNT].val;
12102 vimvars[VV_COUNT].val = (char_u *)count;
12103 vimvars[VV_COUNT1].val = (char_u *)count1;
12104}
12105
12106/*
12107 * Set string v: variable to a copy of "val".
12108 */
12109 void
12110set_vim_var_string(idx, val, len)
12111 int idx;
12112 char_u *val;
12113 int len; /* length of "val" to use or -1 (whole string) */
12114{
12115 vim_free(vimvars[idx].val);
12116 if (val == NULL)
12117 vimvars[idx].val = NULL;
12118 else if (len == -1)
12119 vimvars[idx].val = vim_strsave(val);
12120 else
12121 vimvars[idx].val = vim_strnsave(val, len);
12122}
12123
12124/*
12125 * Set v:register if needed.
12126 */
12127 void
12128set_reg_var(c)
12129 int c;
12130{
12131 char_u regname;
12132
12133 if (c == 0 || c == ' ')
12134 regname = '"';
12135 else
12136 regname = c;
12137 /* Avoid free/alloc when the value is already right. */
12138 if (vimvars[VV_REG].val == NULL || vimvars[VV_REG].val[0] != c)
12139 set_vim_var_string(VV_REG, &regname, 1);
12140}
12141
12142/*
12143 * Get or set v:exception. If "oldval" == NULL, return the current value.
12144 * Otherwise, restore the value to "oldval" and return NULL.
12145 * Must always be called in pairs to save and restore v:exception! Does not
12146 * take care of memory allocations.
12147 */
12148 char_u *
12149v_exception(oldval)
12150 char_u *oldval;
12151{
12152 if (oldval == NULL)
12153 return vimvars[VV_EXCEPTION].val;
12154
12155 vimvars[VV_EXCEPTION].val = oldval;
12156 return NULL;
12157}
12158
12159/*
12160 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
12161 * Otherwise, restore the value to "oldval" and return NULL.
12162 * Must always be called in pairs to save and restore v:throwpoint! Does not
12163 * take care of memory allocations.
12164 */
12165 char_u *
12166v_throwpoint(oldval)
12167 char_u *oldval;
12168{
12169 if (oldval == NULL)
12170 return vimvars[VV_THROWPOINT].val;
12171
12172 vimvars[VV_THROWPOINT].val = oldval;
12173 return NULL;
12174}
12175
12176#if defined(FEAT_AUTOCMD) || defined(PROTO)
12177/*
12178 * Set v:cmdarg.
12179 * If "eap" != NULL, use "eap" to generate the value and return the old value.
12180 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
12181 * Must always be called in pairs!
12182 */
12183 char_u *
12184set_cmdarg(eap, oldarg)
12185 exarg_T *eap;
12186 char_u *oldarg;
12187{
12188 char_u *oldval;
12189 char_u *newval;
12190 unsigned len;
12191
12192 oldval = vimvars[VV_CMDARG].val;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000012193 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000012195 vim_free(oldval);
12196 vimvars[VV_CMDARG].val = oldarg;
12197 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198 }
12199
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000012200 if (eap->force_bin == FORCE_BIN)
12201 len = 6;
12202 else if (eap->force_bin == FORCE_NOBIN)
12203 len = 8;
12204 else
12205 len = 0;
12206 if (eap->force_ff != 0)
12207 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
12208# ifdef FEAT_MBYTE
12209 if (eap->force_enc != 0)
12210 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
12211# endif
12212
12213 newval = alloc(len + 1);
12214 if (newval == NULL)
12215 return NULL;
12216
12217 if (eap->force_bin == FORCE_BIN)
12218 sprintf((char *)newval, " ++bin");
12219 else if (eap->force_bin == FORCE_NOBIN)
12220 sprintf((char *)newval, " ++nobin");
12221 else
12222 *newval = NUL;
12223 if (eap->force_ff != 0)
12224 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
12225 eap->cmd + eap->force_ff);
12226# ifdef FEAT_MBYTE
12227 if (eap->force_enc != 0)
12228 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
12229 eap->cmd + eap->force_enc);
12230# endif
12231 vimvars[VV_CMDARG].val = newval;
12232 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233}
12234#endif
12235
12236/*
12237 * Get the value of internal variable "name".
12238 * Return OK or FAIL.
12239 */
12240 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012241get_var_tv(name, len, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012242 char_u *name;
12243 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012244 typeval *rettv; /* NULL when only checking existence */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012245{
12246 int ret = OK;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012247 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012248 VAR v;
12249 int cc;
12250 int i;
12251
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012252 tv.v_type = VAR_UNKNOWN;
12253
Bram Moolenaar071d4272004-06-13 20:20:40 +000012254 /* truncate the name, so that we can use strcmp() */
12255 cc = name[len];
12256 name[len] = NUL;
12257
12258 /*
12259 * Check for "b:changedtick".
12260 */
12261 if (STRCMP(name, "b:changedtick") == 0)
12262 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012263 tv.v_type = VAR_NUMBER;
12264 tv.vval.v_number = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265 }
12266
12267 /*
12268 * Check for built-in v: variables.
12269 */
12270 else if ((i = find_vim_var(name, len)) >= 0)
12271 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012272 tv.v_type = vimvars[i].type;
12273 if (tv.v_type == VAR_NUMBER)
12274 tv.vval.v_number = (long)vimvars[i].val;
12275 else
12276 tv.vval.v_string = vimvars[i].val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277 }
12278
12279 /*
12280 * Check for user-defined variables.
12281 */
12282 else
12283 {
12284 v = find_var(name, FALSE);
12285 if (v != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012286 tv = v->tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012287 }
12288
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012289 if (tv.v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012291 if (rettv != NULL)
12292 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012293 ret = FAIL;
12294 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012295 else if (rettv != NULL)
12296 copy_tv(&tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297
12298 name[len] = cc;
12299
12300 return ret;
12301}
12302
12303/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012304 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
12305 * value).
12306 */
12307 static typeval *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012308alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012309{
12310 return (typeval *)alloc_clear((unsigned)sizeof(typeval));
12311}
12312
12313/*
12314 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012315 * The string "s" must have been allocated, it is consumed.
12316 * Return NULL for out of memory, the variable otherwise.
12317 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012318 static typeval *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012319alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320 char_u *s;
12321{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012322 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012324 rettv = alloc_tv();
12325 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012327 rettv->v_type = VAR_STRING;
12328 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012329 }
12330 else
12331 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012332 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333}
12334
12335/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012336 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012337 */
12338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012339free_tv(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012340 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341{
12342 if (varp != NULL)
12343 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012344 switch (varp->v_type)
12345 {
12346 case VAR_STRING:
12347 case VAR_FUNC:
12348 vim_free(varp->vval.v_string);
12349 break;
12350 case VAR_LIST:
12351 list_unref(varp->vval.v_list);
12352 break;
12353 default:
12354 break;
12355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356 vim_free(varp);
12357 }
12358}
12359
12360/*
12361 * Free the memory for a variable value and set the value to NULL or 0.
12362 */
12363 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012364clear_tv(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012365 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366{
12367 if (varp != NULL)
12368 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012369 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012370 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012371 case VAR_STRING:
12372 case VAR_FUNC:
12373 vim_free(varp->vval.v_string);
12374 varp->vval.v_string = NULL;
12375 break;
12376 case VAR_LIST:
12377 list_unref(varp->vval.v_list);
12378 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012379 case VAR_DICT:
12380 dict_unref(varp->vval.v_dict);
12381 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012382 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012383 varp->vval.v_number = 0;
12384 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012385 case VAR_UNKNOWN:
12386 break;
12387 default:
12388 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012390 }
12391}
12392
12393/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012394 * Set the value of a variable to NULL without freeing items.
12395 */
12396 static void
12397init_tv(varp)
12398 typeval *varp;
12399{
12400 if (varp != NULL)
12401 vim_memset(varp, 0, sizeof(typeval));
12402}
12403
12404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405 * Get the number value of a variable.
12406 * If it is a String variable, uses vim_str2nr().
12407 */
12408 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012409get_tv_number(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012410 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012411{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012412 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012414 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012415 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012416 case VAR_NUMBER:
12417 n = (long)(varp->vval.v_number);
12418 break;
12419 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012420 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012421 break;
12422 case VAR_STRING:
12423 if (varp->vval.v_string != NULL)
12424 vim_str2nr(varp->vval.v_string, NULL, NULL,
12425 TRUE, TRUE, &n, NULL);
12426 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012427 case VAR_LIST:
12428 EMSG(_("E703: Using a List as a number"));
12429 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012430 default:
12431 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012432 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012433 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434}
12435
12436/*
12437 * Get the lnum from the first argument. Also accepts ".", "$", etc.
12438 */
12439 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012440get_tv_lnum(argvars)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012441 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012443 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444 linenr_T lnum;
12445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012446 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447 if (lnum == 0) /* no valid number, try using line() */
12448 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012449 rettv.v_type = VAR_NUMBER;
12450 f_line(argvars, &rettv);
12451 lnum = rettv.vval.v_number;
12452 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012453 }
12454 return lnum;
12455}
12456
12457/*
12458 * Get the string value of a variable.
12459 * If it is a Number variable, the number is converted into a string.
12460 * get_var_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
12461 * get_var_string_buf() uses a given buffer.
12462 * If the String variable has never been set, return an empty string.
12463 * Never returns NULL;
12464 */
12465 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012466get_tv_string(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012467 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012468{
12469 static char_u mybuf[NUMBUFLEN];
12470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012471 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012472}
12473
12474 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012475get_tv_string_buf(varp, buf)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012476 typeval *varp;
12477 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012479 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012481 case VAR_NUMBER:
12482 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
12483 return buf;
12484 case VAR_FUNC:
Bram Moolenaar8c711452005-01-14 21:53:12 +000012485 EMSG(_("E999: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012486 break;
12487 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +000012488 EMSG(_("E999: using List as a String"));
12489 break;
12490 case VAR_DICT:
12491 EMSG(_("E999: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012492 break;
12493 case VAR_STRING:
12494 if (varp->vval.v_string != NULL)
12495 return varp->vval.v_string;
12496 break;
12497 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012498 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012499 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012501 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502}
12503
12504/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012505 * Get value for "&", as used in map() and filter().
12506 */
12507 static int
12508get_amp_tv(rettv)
12509 typeval *rettv;
12510{
12511 if (amp_tv.v_type == VAR_UNKNOWN)
12512 {
12513 EMSG(_("E712: Using & outside of map() or filter()"));
12514 return FAIL;
12515 }
12516 copy_tv(&amp_tv, rettv);
12517 return OK;
12518}
12519
12520/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012521 * Find variable "name" in the list of variables.
12522 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012523 * Careful: "a:0" variables don't have a name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012524 */
12525 static VAR
12526find_var(name, writing)
12527 char_u *name;
12528 int writing;
12529{
12530 int i;
12531 char_u *varname;
12532 garray_T *gap;
12533
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534 if (name[0] == 'a' && name[1] == ':')
12535 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012536 /* Function arguments "a:".
12537 * NOTE: We use a typecast, because function arguments don't have a
12538 * name. The caller must not try to access the name! */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012539 if (writing)
12540 {
12541 EMSG2(_(e_readonlyvar), name);
12542 return NULL;
12543 }
12544 name += 2;
12545 if (current_funccal == NULL)
12546 return NULL;
12547 if (VIM_ISDIGIT(*name))
12548 {
12549 i = atol((char *)name);
12550 if (i == 0) /* a:0 */
12551 return &current_funccal->a0_var;
12552 i += current_funccal->func->args.ga_len;
12553 if (i > current_funccal->argcount) /* a:999 */
12554 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012555 return (VAR)&(current_funccal->argvars[i - 1]); /* a:1, a:2, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556 }
12557 if (STRCMP(name, "firstline") == 0)
12558 return &(current_funccal->firstline);
12559 if (STRCMP(name, "lastline") == 0)
12560 return &(current_funccal->lastline);
12561 for (i = 0; i < current_funccal->func->args.ga_len; ++i)
12562 if (STRCMP(name, ((char_u **)
12563 (current_funccal->func->args.ga_data))[i]) == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012564 return (VAR)&(current_funccal->argvars[i]); /* a:name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012565 return NULL;
12566 }
12567
12568 gap = find_var_ga(name, &varname);
12569 if (gap == NULL)
12570 return NULL;
12571 return find_var_in_ga(gap, varname);
12572}
12573
12574 static VAR
12575find_var_in_ga(gap, varname)
12576 garray_T *gap;
12577 char_u *varname;
12578{
12579 int i;
12580
12581 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012582 if (VAR_GAP_ENTRY(i, gap).v_name != NULL
12583 && STRCMP(VAR_GAP_ENTRY(i, gap).v_name, varname) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584 break;
12585 if (i < 0)
12586 return NULL;
12587 return &VAR_GAP_ENTRY(i, gap);
12588}
12589
12590/*
12591 * Find the growarray and start of name without ':' for a variable name.
12592 */
12593 static garray_T *
12594find_var_ga(name, varname)
12595 char_u *name;
12596 char_u **varname;
12597{
12598 if (name[1] != ':')
12599 {
12600 /* If not "x:name" there must not be any ":" in the name. */
12601 if (vim_strchr(name, ':') != NULL)
12602 return NULL;
12603 *varname = name;
12604 if (current_funccal == NULL)
12605 return &variables; /* global variable */
12606 return &current_funccal->l_vars; /* local function variable */
12607 }
12608 *varname = name + 2;
12609 if (*name == 'b') /* buffer variable */
12610 return &curbuf->b_vars;
12611 if (*name == 'w') /* window variable */
12612 return &curwin->w_vars;
12613 if (*name == 'g') /* global variable */
12614 return &variables;
12615 if (*name == 'l' && current_funccal != NULL)/* local function variable */
12616 return &current_funccal->l_vars;
12617 if (*name == 's' /* script variable */
12618 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
12619 return &SCRIPT_VARS(current_SID);
12620 return NULL;
12621}
12622
12623/*
12624 * Get the string value of a (global/local) variable.
12625 * Returns NULL when it doesn't exist.
12626 */
12627 char_u *
12628get_var_value(name)
12629 char_u *name;
12630{
12631 VAR v;
12632
12633 v = find_var(name, FALSE);
12634 if (v == NULL)
12635 return NULL;
12636 return get_var_string(v);
12637}
12638
12639/*
12640 * Allocate a new growarry for a sourced script. It will be used while
12641 * sourcing this script and when executing functions defined in the script.
12642 */
12643 void
12644new_script_vars(id)
12645 scid_T id;
12646{
12647 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
12648 {
12649 while (ga_scripts.ga_len < id)
12650 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012651 vars_init(&SCRIPT_VARS(ga_scripts.ga_len + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012653 }
12654 }
12655}
12656
12657/*
12658 * Initialize internal variables for use.
12659 */
12660 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012661vars_init(gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012662 garray_T *gap;
12663{
12664 ga_init2(gap, (int)sizeof(var), 4);
12665}
12666
12667/*
12668 * Clean up a list of internal variables.
12669 */
12670 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012671vars_clear(gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672 garray_T *gap;
12673{
12674 int i;
12675
12676 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012677 clear_var(&VAR_GAP_ENTRY(i, gap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678 ga_clear(gap);
12679}
12680
12681 static void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012682clear_var(v)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683 VAR v;
12684{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012685 vim_free(v->v_name);
12686 v->v_name = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012687 clear_tv(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688}
12689
12690/*
12691 * List the value of one internal variable.
12692 */
12693 static void
12694list_one_var(v, prefix)
12695 VAR v;
12696 char_u *prefix;
12697{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012698 char_u *tofree;
12699 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012700 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012701
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012702 s = echo_string(&v->tv, &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012703 list_one_var_a(prefix, v->v_name, v->tv.v_type,
12704 s == NULL ? (char_u *)"" : s);
12705 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706}
12707
12708/*
12709 * List the value of one "v:" variable.
12710 */
12711 static void
12712list_vim_var(i)
12713 int i; /* index in vimvars[] */
12714{
12715 char_u *p;
12716 char_u numbuf[NUMBUFLEN];
12717
12718 if (vimvars[i].type == VAR_NUMBER)
12719 {
12720 p = numbuf;
12721 sprintf((char *)p, "%ld", (long)vimvars[i].val);
12722 }
12723 else if (vimvars[i].val == NULL)
12724 p = (char_u *)"";
12725 else
12726 p = vimvars[i].val;
12727 list_one_var_a((char_u *)"v:", (char_u *)vimvars[i].name,
12728 vimvars[i].type, p);
12729}
12730
12731 static void
12732list_one_var_a(prefix, name, type, string)
12733 char_u *prefix;
12734 char_u *name;
12735 int type;
12736 char_u *string;
12737{
12738 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
12739 if (name != NULL) /* "a:" vars don't have a name stored */
12740 msg_puts(name);
12741 msg_putchar(' ');
12742 msg_advance(22);
12743 if (type == VAR_NUMBER)
12744 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012745 else if (type == VAR_FUNC)
12746 msg_putchar('*');
12747 else if (type == VAR_LIST)
12748 {
12749 msg_putchar('[');
12750 if (*string == '[')
12751 ++string;
12752 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000012753 else if (type == VAR_DICT)
12754 {
12755 msg_putchar('{');
12756 if (*string == '{')
12757 ++string;
12758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759 else
12760 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012761
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012763
12764 if (type == VAR_FUNC)
12765 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766}
12767
12768/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012769 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770 * If the variable already exists, the value is updated.
12771 * Otherwise the variable is created.
12772 */
12773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012774set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012775 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012776 typeval *tv;
12777 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778{
12779 int i;
12780 VAR v;
12781 char_u *varname;
12782 garray_T *gap;
12783
12784 /*
12785 * Handle setting internal v: variables.
12786 */
12787 i = find_vim_var(name, (int)STRLEN(name));
12788 if (i >= 0)
12789 {
12790 if (vimvars[i].flags & VV_RO)
12791 EMSG2(_(e_readonlyvar), name);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012792 else if ((vimvars[i].flags & VV_RO_SBX) && sandbox)
12793 EMSG2(_(e_readonlysbx), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794 else
12795 {
12796 if (vimvars[i].type == VAR_STRING)
12797 {
12798 vim_free(vimvars[i].val);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012799 if (copy || tv->v_type != VAR_STRING)
12800 vimvars[i].val = vim_strsave(get_tv_string(tv));
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012801 else
12802 {
12803 /* Take over the string to avoid an extra alloc/free. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012804 vimvars[i].val = tv->vval.v_string;
12805 tv->vval.v_string = NULL;
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807 }
12808 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809 vimvars[i].val = (char_u *)get_tv_number(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810 }
12811 return;
12812 }
12813
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012814 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012815 {
12816 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
12817 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
12818 ? name[2] : name[0]))
12819 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012820 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012821 return;
12822 }
12823 if (function_exists(name))
12824 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012825 EMSG2(_("705: Variable name conflicts with existing function: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012826 return;
12827 }
12828 }
12829
Bram Moolenaar071d4272004-06-13 20:20:40 +000012830 v = find_var(name, TRUE);
12831 if (v != NULL) /* existing variable, only need to free string */
12832 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012833 if (v->tv.v_type != tv->v_type
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012834 && !((v->tv.v_type == VAR_STRING
12835 || v->tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012836 && (tv->v_type == VAR_STRING
12837 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012838 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012839 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012840 return;
12841 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012842 clear_tv(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843 }
12844 else /* add a new variable */
12845 {
12846 gap = find_var_ga(name, &varname);
12847 if (gap == NULL) /* illegal name */
12848 {
12849 EMSG2(_("E461: Illegal variable name: %s"), name);
12850 return;
12851 }
12852
12853 /* Try to use an empty entry */
12854 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012855 if (VAR_GAP_ENTRY(i, gap).v_name == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856 break;
12857 if (i < 0) /* need to allocate more room */
12858 {
12859 if (ga_grow(gap, 1) == FAIL)
12860 return;
12861 i = gap->ga_len;
12862 }
12863 v = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012864 if ((v->v_name = vim_strsave(varname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865 return;
12866 if (i == gap->ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012868 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012869 if (copy || tv->v_type == VAR_NUMBER)
12870 copy_tv(tv, &v->tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012871 else
12872 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012873 v->tv = *tv;
12874 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876}
12877
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012878/*
12879 * Copy the values from typeval "from" to typeval "to".
12880 * When needed allocates string or increases reference count.
12881 * Does not make a copy of a list!
12882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012884copy_tv(from, to)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012885 typeval *from;
12886 typeval *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012888 to->v_type = from->v_type;
12889 switch (from->v_type)
12890 {
12891 case VAR_NUMBER:
12892 to->vval.v_number = from->vval.v_number;
12893 break;
12894 case VAR_STRING:
12895 case VAR_FUNC:
12896 if (from->vval.v_string == NULL)
12897 to->vval.v_string = NULL;
12898 else
12899 to->vval.v_string = vim_strsave(from->vval.v_string);
12900 break;
12901 case VAR_LIST:
12902 if (from->vval.v_list == NULL)
12903 to->vval.v_list = NULL;
12904 else
12905 {
12906 to->vval.v_list = from->vval.v_list;
12907 ++to->vval.v_list->lv_refcount;
12908 }
12909 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000012910 case VAR_DICT:
12911 if (from->vval.v_dict == NULL)
12912 to->vval.v_dict = NULL;
12913 else
12914 {
12915 to->vval.v_dict = from->vval.v_dict;
12916 ++to->vval.v_dict->dv_refcount;
12917 }
12918 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012919 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012920 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012921 break;
12922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923}
12924
12925/*
12926 * ":echo expr1 ..." print each argument separated with a space, add a
12927 * newline at the end.
12928 * ":echon expr1 ..." print each argument plain.
12929 */
12930 void
12931ex_echo(eap)
12932 exarg_T *eap;
12933{
12934 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012935 typeval rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012936 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937 char_u *p;
12938 int needclr = TRUE;
12939 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012940 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012941
12942 if (eap->skip)
12943 ++emsg_skip;
12944 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
12945 {
12946 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012947 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012948 {
12949 /*
12950 * Report the invalid expression unless the expression evaluation
12951 * has been cancelled due to an aborting error, an interrupt, or an
12952 * exception.
12953 */
12954 if (!aborting())
12955 EMSG2(_(e_invexpr2), p);
12956 break;
12957 }
12958 if (!eap->skip)
12959 {
12960 if (atstart)
12961 {
12962 atstart = FALSE;
12963 /* Call msg_start() after eval1(), evaluating the expression
12964 * may cause a message to appear. */
12965 if (eap->cmdidx == CMD_echo)
12966 msg_start();
12967 }
12968 else if (eap->cmdidx == CMD_echo)
12969 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012970 for (p = echo_string(&rettv, &tofree, numbuf);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012971 *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012972 if (*p == '\n' || *p == '\r' || *p == TAB)
12973 {
12974 if (*p != TAB && needclr)
12975 {
12976 /* remove any text still there from the command */
12977 msg_clr_eos();
12978 needclr = FALSE;
12979 }
12980 msg_putchar_attr(*p, echo_attr);
12981 }
12982 else
12983 {
12984#ifdef FEAT_MBYTE
12985 if (has_mbyte)
12986 {
12987 int i = (*mb_ptr2len_check)(p);
12988
12989 (void)msg_outtrans_len_attr(p, i, echo_attr);
12990 p += i - 1;
12991 }
12992 else
12993#endif
12994 (void)msg_outtrans_len_attr(p, 1, echo_attr);
12995 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012996 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012998 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999 arg = skipwhite(arg);
13000 }
13001 eap->nextcmd = check_nextcmd(arg);
13002
13003 if (eap->skip)
13004 --emsg_skip;
13005 else
13006 {
13007 /* remove text that may still be there from the command */
13008 if (needclr)
13009 msg_clr_eos();
13010 if (eap->cmdidx == CMD_echo)
13011 msg_end();
13012 }
13013}
13014
13015/*
13016 * ":echohl {name}".
13017 */
13018 void
13019ex_echohl(eap)
13020 exarg_T *eap;
13021{
13022 int id;
13023
13024 id = syn_name2id(eap->arg);
13025 if (id == 0)
13026 echo_attr = 0;
13027 else
13028 echo_attr = syn_id2attr(id);
13029}
13030
13031/*
13032 * ":execute expr1 ..." execute the result of an expression.
13033 * ":echomsg expr1 ..." Print a message
13034 * ":echoerr expr1 ..." Print an error
13035 * Each gets spaces around each argument and a newline at the end for
13036 * echo commands
13037 */
13038 void
13039ex_execute(eap)
13040 exarg_T *eap;
13041{
13042 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013043 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013044 int ret = OK;
13045 char_u *p;
13046 garray_T ga;
13047 int len;
13048 int save_did_emsg;
13049
13050 ga_init2(&ga, 1, 80);
13051
13052 if (eap->skip)
13053 ++emsg_skip;
13054 while (*arg != NUL && *arg != '|' && *arg != '\n')
13055 {
13056 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013057 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058 {
13059 /*
13060 * Report the invalid expression unless the expression evaluation
13061 * has been cancelled due to an aborting error, an interrupt, or an
13062 * exception.
13063 */
13064 if (!aborting())
13065 EMSG2(_(e_invexpr2), p);
13066 ret = FAIL;
13067 break;
13068 }
13069
13070 if (!eap->skip)
13071 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013072 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013073 len = (int)STRLEN(p);
13074 if (ga_grow(&ga, len + 2) == FAIL)
13075 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013076 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013077 ret = FAIL;
13078 break;
13079 }
13080 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000013082 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013083 ga.ga_len += len;
13084 }
13085
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013086 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013087 arg = skipwhite(arg);
13088 }
13089
13090 if (ret != FAIL && ga.ga_data != NULL)
13091 {
13092 if (eap->cmdidx == CMD_echomsg)
13093 MSG_ATTR(ga.ga_data, echo_attr);
13094 else if (eap->cmdidx == CMD_echoerr)
13095 {
13096 /* We don't want to abort following commands, restore did_emsg. */
13097 save_did_emsg = did_emsg;
13098 EMSG((char_u *)ga.ga_data);
13099 if (!force_abort)
13100 did_emsg = save_did_emsg;
13101 }
13102 else if (eap->cmdidx == CMD_execute)
13103 do_cmdline((char_u *)ga.ga_data,
13104 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
13105 }
13106
13107 ga_clear(&ga);
13108
13109 if (eap->skip)
13110 --emsg_skip;
13111
13112 eap->nextcmd = check_nextcmd(arg);
13113}
13114
13115/*
13116 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
13117 * "arg" points to the "&" or '+' when called, to "option" when returning.
13118 * Returns NULL when no option name found. Otherwise pointer to the char
13119 * after the option name.
13120 */
13121 static char_u *
13122find_option_end(arg, opt_flags)
13123 char_u **arg;
13124 int *opt_flags;
13125{
13126 char_u *p = *arg;
13127
13128 ++p;
13129 if (*p == 'g' && p[1] == ':')
13130 {
13131 *opt_flags = OPT_GLOBAL;
13132 p += 2;
13133 }
13134 else if (*p == 'l' && p[1] == ':')
13135 {
13136 *opt_flags = OPT_LOCAL;
13137 p += 2;
13138 }
13139 else
13140 *opt_flags = 0;
13141
13142 if (!ASCII_ISALPHA(*p))
13143 return NULL;
13144 *arg = p;
13145
13146 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
13147 p += 4; /* termcap option */
13148 else
13149 while (ASCII_ISALPHA(*p))
13150 ++p;
13151 return p;
13152}
13153
13154/*
13155 * ":function"
13156 */
13157 void
13158ex_function(eap)
13159 exarg_T *eap;
13160{
13161 char_u *theline;
13162 int j;
13163 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013164 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165 char_u *name = NULL;
13166 char_u *p;
13167 char_u *arg;
13168 garray_T newargs;
13169 garray_T newlines;
13170 int varargs = FALSE;
13171 int mustend = FALSE;
13172 int flags = 0;
13173 ufunc_T *fp;
13174 int indent;
13175 int nesting;
13176 char_u *skip_until = NULL;
13177 static char_u e_funcexts[] = N_("E122: Function %s already exists, add ! to replace it");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013178 VAR v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013179
13180 /*
13181 * ":function" without argument: list functions.
13182 */
13183 if (ends_excmd(*eap->arg))
13184 {
13185 if (!eap->skip)
13186 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next)
13187 list_func_head(fp, FALSE);
13188 eap->nextcmd = check_nextcmd(eap->arg);
13189 return;
13190 }
13191
13192 p = eap->arg;
13193 name = trans_function_name(&p, eap->skip, FALSE);
13194 if (name == NULL && !eap->skip)
13195 {
13196 /*
13197 * Return on an invalid expression in braces, unless the expression
13198 * evaluation has been cancelled due to an aborting error, an
13199 * interrupt, or an exception.
13200 */
13201 if (!aborting())
13202 return;
13203 else
13204 eap->skip = TRUE;
13205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013206 /* An error in a function call during evaluation of an expression in magic
13207 * braces should not cause the function not to be defined. */
13208 saved_did_emsg = did_emsg;
13209 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210
13211 /*
13212 * ":function func" with only function name: list function.
13213 */
13214 if (vim_strchr(p, '(') == NULL)
13215 {
13216 if (!ends_excmd(*skipwhite(p)))
13217 {
13218 EMSG(_(e_trailing));
13219 goto erret_name;
13220 }
13221 eap->nextcmd = check_nextcmd(p);
13222 if (eap->nextcmd != NULL)
13223 *p = NUL;
13224 if (!eap->skip && !got_int)
13225 {
13226 fp = find_func(name);
13227 if (fp != NULL)
13228 {
13229 list_func_head(fp, TRUE);
13230 for (j = 0; j < fp->lines.ga_len && !got_int; ++j)
13231 {
13232 msg_putchar('\n');
13233 msg_outnum((long)(j + 1));
13234 if (j < 9)
13235 msg_putchar(' ');
13236 if (j < 99)
13237 msg_putchar(' ');
13238 msg_prt_line(FUNCLINE(fp, j));
13239 out_flush(); /* show a line at a time */
13240 ui_breakcheck();
13241 }
13242 if (!got_int)
13243 {
13244 msg_putchar('\n');
13245 msg_puts((char_u *)" endfunction");
13246 }
13247 }
13248 else
13249 EMSG2(_("E123: Undefined function: %s"), eap->arg);
13250 }
13251 goto erret_name;
13252 }
13253
13254 /*
13255 * ":function name(arg1, arg2)" Define function.
13256 */
13257 p = skipwhite(p);
13258 if (*p != '(')
13259 {
13260 if (!eap->skip)
13261 {
13262 EMSG2(_("E124: Missing '(': %s"), eap->arg);
13263 goto erret_name;
13264 }
13265 /* attempt to continue by skipping some text */
13266 if (vim_strchr(p, '(') != NULL)
13267 p = vim_strchr(p, '(');
13268 }
13269 p = skipwhite(p + 1);
13270
13271 ga_init2(&newargs, (int)sizeof(char_u *), 3);
13272 ga_init2(&newlines, (int)sizeof(char_u *), 3);
13273
13274 /*
13275 * Isolate the arguments: "arg1, arg2, ...)"
13276 */
13277 while (*p != ')')
13278 {
13279 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
13280 {
13281 varargs = TRUE;
13282 p += 3;
13283 mustend = TRUE;
13284 }
13285 else
13286 {
13287 arg = p;
13288 while (ASCII_ISALNUM(*p) || *p == '_')
13289 ++p;
13290 if (arg == p || isdigit(*arg)
13291 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
13292 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
13293 {
13294 if (!eap->skip)
13295 EMSG2(_("E125: Illegal argument: %s"), arg);
13296 break;
13297 }
13298 if (ga_grow(&newargs, 1) == FAIL)
13299 goto erret;
13300 c = *p;
13301 *p = NUL;
13302 arg = vim_strsave(arg);
13303 if (arg == NULL)
13304 goto erret;
13305 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
13306 *p = c;
13307 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013308 if (*p == ',')
13309 ++p;
13310 else
13311 mustend = TRUE;
13312 }
13313 p = skipwhite(p);
13314 if (mustend && *p != ')')
13315 {
13316 if (!eap->skip)
13317 EMSG2(_(e_invarg2), eap->arg);
13318 break;
13319 }
13320 }
13321 ++p; /* skip the ')' */
13322
13323 /* find extra arguments "range" and "abort" */
13324 for (;;)
13325 {
13326 p = skipwhite(p);
13327 if (STRNCMP(p, "range", 5) == 0)
13328 {
13329 flags |= FC_RANGE;
13330 p += 5;
13331 }
13332 else if (STRNCMP(p, "abort", 5) == 0)
13333 {
13334 flags |= FC_ABORT;
13335 p += 5;
13336 }
13337 else
13338 break;
13339 }
13340
13341 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
13342 EMSG(_(e_trailing));
13343
13344 /*
13345 * Read the body of the function, until ":endfunction" is found.
13346 */
13347 if (KeyTyped)
13348 {
13349 /* Check if the function already exists, don't let the user type the
13350 * whole function before telling him it doesn't work! For a script we
13351 * need to skip the body to be able to find what follows. */
13352 if (!eap->skip && !eap->forceit && find_func(name) != NULL)
13353 EMSG2(_(e_funcexts), name);
13354
13355 msg_putchar('\n'); /* don't overwrite the function name */
13356 cmdline_row = msg_row;
13357 }
13358
13359 indent = 2;
13360 nesting = 0;
13361 for (;;)
13362 {
13363 msg_scroll = TRUE;
13364 need_wait_return = FALSE;
13365 if (eap->getline == NULL)
13366 theline = getcmdline(':', 0L, indent);
13367 else
13368 theline = eap->getline(':', eap->cookie, indent);
13369 if (KeyTyped)
13370 lines_left = Rows - 1;
13371 if (theline == NULL)
13372 {
13373 EMSG(_("E126: Missing :endfunction"));
13374 goto erret;
13375 }
13376
13377 if (skip_until != NULL)
13378 {
13379 /* between ":append" and "." and between ":python <<EOF" and "EOF"
13380 * don't check for ":endfunc". */
13381 if (STRCMP(theline, skip_until) == 0)
13382 {
13383 vim_free(skip_until);
13384 skip_until = NULL;
13385 }
13386 }
13387 else
13388 {
13389 /* skip ':' and blanks*/
13390 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
13391 ;
13392
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013393 /* Check for "endfunction". */
13394 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013395 {
13396 vim_free(theline);
13397 break;
13398 }
13399
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013400 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401 * at "end". */
13402 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
13403 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013404 else if (STRNCMP(p, "if", 2) == 0
13405 || STRNCMP(p, "wh", 2) == 0
13406 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407 || STRNCMP(p, "try", 3) == 0)
13408 indent += 2;
13409
13410 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013411 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013413 if (*p == '!')
13414 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415 p += eval_fname_script(p);
13416 if (ASCII_ISALPHA(*p))
13417 {
13418 vim_free(trans_function_name(&p, TRUE, FALSE));
13419 if (*skipwhite(p) == '(')
13420 {
13421 ++nesting;
13422 indent += 2;
13423 }
13424 }
13425 }
13426
13427 /* Check for ":append" or ":insert". */
13428 p = skip_range(p, NULL);
13429 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
13430 || (p[0] == 'i'
13431 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
13432 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
13433 skip_until = vim_strsave((char_u *)".");
13434
13435 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
13436 arg = skipwhite(skiptowhite(p));
13437 if (arg[0] == '<' && arg[1] =='<'
13438 && ((p[0] == 'p' && p[1] == 'y'
13439 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
13440 || (p[0] == 'p' && p[1] == 'e'
13441 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
13442 || (p[0] == 't' && p[1] == 'c'
13443 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
13444 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
13445 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013446 || (p[0] == 'm' && p[1] == 'z'
13447 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448 ))
13449 {
13450 /* ":python <<" continues until a dot, like ":append" */
13451 p = skipwhite(arg + 2);
13452 if (*p == NUL)
13453 skip_until = vim_strsave((char_u *)".");
13454 else
13455 skip_until = vim_strsave(p);
13456 }
13457 }
13458
13459 /* Add the line to the function. */
13460 if (ga_grow(&newlines, 1) == FAIL)
13461 goto erret;
13462 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
13463 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464 }
13465
13466 /* Don't define the function when skipping commands or when an error was
13467 * detected. */
13468 if (eap->skip || did_emsg)
13469 goto erret;
13470
13471 /*
13472 * If there are no errors, add the function
13473 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013474 v = find_var(name, FALSE);
13475 if (v != NULL && v->tv.v_type == VAR_FUNC)
13476 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013477 EMSG2(_("E707: Function name conflicts with variable: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013478 goto erret;
13479 }
13480
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481 fp = find_func(name);
13482 if (fp != NULL)
13483 {
13484 if (!eap->forceit)
13485 {
13486 EMSG2(_(e_funcexts), name);
13487 goto erret;
13488 }
13489 if (fp->calls)
13490 {
13491 EMSG2(_("E127: Cannot redefine function %s: It is in use"), name);
13492 goto erret;
13493 }
13494 /* redefine existing function */
13495 ga_clear_strings(&(fp->args));
13496 ga_clear_strings(&(fp->lines));
13497 vim_free(name);
13498 }
13499 else
13500 {
13501 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T));
13502 if (fp == NULL)
13503 goto erret;
13504 /* insert the new function in the function list */
13505 fp->next = firstfunc;
13506 firstfunc = fp;
13507 fp->name = name;
13508 }
13509 fp->args = newargs;
13510 fp->lines = newlines;
13511 fp->varargs = varargs;
13512 fp->flags = flags;
13513 fp->calls = 0;
13514 fp->script_ID = current_SID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013515 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516 vim_free(skip_until);
13517 return;
13518
13519erret:
13520 vim_free(skip_until);
13521 ga_clear_strings(&newargs);
13522 ga_clear_strings(&newlines);
13523erret_name:
13524 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013526}
13527
13528/*
13529 * Get a function name, translating "<SID>" and "<SNR>".
13530 * Returns the function name in allocated memory, or NULL for failure.
13531 * Advances "pp" to just after the function name (if no error).
13532 */
13533 static char_u *
13534trans_function_name(pp, skip, internal)
13535 char_u **pp;
13536 int skip; /* only find the end, don't evaluate */
13537 int internal; /* TRUE if internal function name OK */
13538{
13539 char_u *name;
13540 char_u *start;
13541 char_u *end;
13542 int lead;
13543 char_u sid_buf[20];
13544 char_u *temp_string = NULL;
13545 char_u *expr_start, *expr_end;
13546 int len;
13547
13548 /* A name starting with "<SID>" or "<SNR>" is local to a script. */
13549 start = *pp;
13550 lead = eval_fname_script(start);
13551 if (lead > 0)
13552 start += lead;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013553 end = find_name_end(start, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554 if (end == start)
13555 {
13556 if (!skip)
13557 EMSG(_("E129: Function name required"));
13558 return NULL;
13559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560 if (expr_start != NULL && !skip)
13561 {
13562 /* expand magic curlies */
13563 temp_string = make_expanded_name(start, expr_start, expr_end, end);
13564 if (temp_string == NULL)
13565 {
13566 /*
13567 * Report an invalid expression in braces, unless the expression
13568 * evaluation has been cancelled due to an aborting error, an
13569 * interrupt, or an exception.
13570 */
13571 if (!aborting())
13572 EMSG2(_(e_invarg2), start);
13573 else
13574 *pp = end;
13575 return NULL;
13576 }
13577 start = temp_string;
13578 len = (int)STRLEN(temp_string);
13579 }
13580 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013581 len = (int)(end - start);
13582
13583 /*
13584 * Copy the function name to allocated memory.
13585 * Accept <SID>name() inside a script, translate into <SNR>123_name().
13586 * Accept <SNR>123_name() outside a script.
13587 */
13588 if (skip)
13589 lead = 0; /* do nothing */
13590 else if (lead > 0)
13591 {
13592 lead = 3;
13593 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
13594 {
13595 if (current_SID <= 0)
13596 {
13597 EMSG(_(e_usingsid));
13598 return NULL;
13599 }
13600 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
13601 lead += (int)STRLEN(sid_buf);
13602 }
13603 }
13604 else if (!internal && !ASCII_ISUPPER(*start))
13605 {
13606 EMSG2(_("E128: Function name must start with a capital: %s"), start);
13607 return NULL;
13608 }
13609 name = alloc((unsigned)(len + lead + 1));
13610 if (name != NULL)
13611 {
13612 if (lead > 0)
13613 {
13614 name[0] = K_SPECIAL;
13615 name[1] = KS_EXTRA;
13616 name[2] = (int)KE_SNR;
13617 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
13618 STRCPY(name + 3, sid_buf);
13619 }
13620 mch_memmove(name + lead, start, (size_t)len);
13621 name[len + lead] = NUL;
13622 }
13623 *pp = end;
13624
13625 vim_free(temp_string);
13626 return name;
13627}
13628
13629/*
13630 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
13631 * Return 2 if "p" starts with "s:".
13632 * Return 0 otherwise.
13633 */
13634 static int
13635eval_fname_script(p)
13636 char_u *p;
13637{
13638 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
13639 || STRNICMP(p + 1, "SNR>", 4) == 0))
13640 return 5;
13641 if (p[0] == 's' && p[1] == ':')
13642 return 2;
13643 return 0;
13644}
13645
13646/*
13647 * Return TRUE if "p" starts with "<SID>" or "s:".
13648 * Only works if eval_fname_script() returned non-zero for "p"!
13649 */
13650 static int
13651eval_fname_sid(p)
13652 char_u *p;
13653{
13654 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
13655}
13656
13657/*
13658 * List the head of the function: "name(arg1, arg2)".
13659 */
13660 static void
13661list_func_head(fp, indent)
13662 ufunc_T *fp;
13663 int indent;
13664{
13665 int j;
13666
13667 msg_start();
13668 if (indent)
13669 MSG_PUTS(" ");
13670 MSG_PUTS("function ");
13671 if (fp->name[0] == K_SPECIAL)
13672 {
13673 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
13674 msg_puts(fp->name + 3);
13675 }
13676 else
13677 msg_puts(fp->name);
13678 msg_putchar('(');
13679 for (j = 0; j < fp->args.ga_len; ++j)
13680 {
13681 if (j)
13682 MSG_PUTS(", ");
13683 msg_puts(FUNCARG(fp, j));
13684 }
13685 if (fp->varargs)
13686 {
13687 if (j)
13688 MSG_PUTS(", ");
13689 MSG_PUTS("...");
13690 }
13691 msg_putchar(')');
13692}
13693
13694/*
13695 * Find a function by name, return pointer to it in ufuncs.
13696 * Return NULL for unknown function.
13697 */
13698 static ufunc_T *
13699find_func(name)
13700 char_u *name;
13701{
13702 ufunc_T *fp;
13703
13704 for (fp = firstfunc; fp != NULL; fp = fp->next)
13705 if (STRCMP(name, fp->name) == 0)
13706 break;
13707 return fp;
13708}
13709
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013710/*
13711 * Return TRUE if a function "name" exists.
13712 */
13713 static int
13714function_exists(name)
13715 char_u *name;
13716{
13717 char_u *p = name;
13718 int n = FALSE;
13719
13720 p = trans_function_name(&p, FALSE, TRUE);
13721 if (p != NULL)
13722 {
13723 if (ASCII_ISUPPER(*p) || p[0] == K_SPECIAL)
13724 n = (find_func(p) != NULL);
13725 else if (ASCII_ISLOWER(*p))
13726 n = (find_internal_func(p) >= 0);
13727 vim_free(p);
13728 }
13729 return n;
13730}
13731
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
13733
13734/*
13735 * Function given to ExpandGeneric() to obtain the list of user defined
13736 * function names.
13737 */
13738 char_u *
13739get_user_func_name(xp, idx)
13740 expand_T *xp;
13741 int idx;
13742{
13743 static ufunc_T *fp = NULL;
13744
13745 if (idx == 0)
13746 fp = firstfunc;
13747 if (fp != NULL)
13748 {
13749 if (STRLEN(fp->name) + 4 >= IOSIZE)
13750 return fp->name; /* prevents overflow */
13751
13752 cat_func_name(IObuff, fp);
13753 if (xp->xp_context != EXPAND_USER_FUNC)
13754 {
13755 STRCAT(IObuff, "(");
13756 if (!fp->varargs && fp->args.ga_len == 0)
13757 STRCAT(IObuff, ")");
13758 }
13759
13760 fp = fp->next;
13761 return IObuff;
13762 }
13763 return NULL;
13764}
13765
13766#endif /* FEAT_CMDL_COMPL */
13767
13768/*
13769 * Copy the function name of "fp" to buffer "buf".
13770 * "buf" must be able to hold the function name plus three bytes.
13771 * Takes care of script-local function names.
13772 */
13773 static void
13774cat_func_name(buf, fp)
13775 char_u *buf;
13776 ufunc_T *fp;
13777{
13778 if (fp->name[0] == K_SPECIAL)
13779 {
13780 STRCPY(buf, "<SNR>");
13781 STRCAT(buf, fp->name + 3);
13782 }
13783 else
13784 STRCPY(buf, fp->name);
13785}
13786
13787/*
13788 * ":delfunction {name}"
13789 */
13790 void
13791ex_delfunction(eap)
13792 exarg_T *eap;
13793{
13794 ufunc_T *fp = NULL, *pfp;
13795 char_u *p;
13796 char_u *name;
13797
13798 p = eap->arg;
13799 name = trans_function_name(&p, eap->skip, FALSE);
13800 if (name == NULL)
13801 return;
13802 if (!ends_excmd(*skipwhite(p)))
13803 {
13804 vim_free(name);
13805 EMSG(_(e_trailing));
13806 return;
13807 }
13808 eap->nextcmd = check_nextcmd(p);
13809 if (eap->nextcmd != NULL)
13810 *p = NUL;
13811
13812 if (!eap->skip)
13813 fp = find_func(name);
13814 vim_free(name);
13815
13816 if (!eap->skip)
13817 {
13818 if (fp == NULL)
13819 {
13820 EMSG2(_("E130: Undefined function: %s"), eap->arg);
13821 return;
13822 }
13823 if (fp->calls)
13824 {
13825 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
13826 return;
13827 }
13828
13829 /* clear this function */
13830 vim_free(fp->name);
13831 ga_clear_strings(&(fp->args));
13832 ga_clear_strings(&(fp->lines));
13833
13834 /* remove the function from the function list */
13835 if (firstfunc == fp)
13836 firstfunc = fp->next;
13837 else
13838 {
13839 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next)
13840 if (pfp->next == fp)
13841 {
13842 pfp->next = fp->next;
13843 break;
13844 }
13845 }
13846 vim_free(fp);
13847 }
13848}
13849
13850/*
13851 * Call a user function.
13852 */
13853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013854call_user_func(fp, argcount, argvars, rettv, firstline, lastline)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855 ufunc_T *fp; /* pointer to function */
13856 int argcount; /* nr of args */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013857 typeval *argvars; /* arguments */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013858 typeval *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859 linenr_T firstline; /* first line of range */
13860 linenr_T lastline; /* last line of range */
13861{
13862 char_u *save_sourcing_name;
13863 linenr_T save_sourcing_lnum;
13864 scid_T save_current_SID;
13865 struct funccall fc;
13866 struct funccall *save_fcp = current_funccal;
13867 int save_did_emsg;
13868 static int depth = 0;
13869
13870 /* If depth of calling is getting too high, don't execute the function */
13871 if (depth >= p_mfd)
13872 {
13873 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013874 rettv->v_type = VAR_NUMBER;
13875 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876 return;
13877 }
13878 ++depth;
13879
13880 line_breakcheck(); /* check for CTRL-C hit */
13881
13882 /* set local variables */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013883 vars_init(&fc.l_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884 fc.func = fp;
13885 fc.argcount = argcount;
13886 fc.argvars = argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013887 fc.rettv = rettv;
13888 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889 fc.linenr = 0;
13890 fc.returned = FALSE;
13891 fc.level = ex_nesting_level;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013892 fc.a0_var.tv.v_type = VAR_NUMBER;
13893 fc.a0_var.tv.vval.v_number = argcount - fp->args.ga_len;
13894 fc.a0_var.v_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895 current_funccal = &fc;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013896 fc.firstline.tv.v_type = VAR_NUMBER;
13897 fc.firstline.tv.vval.v_number = firstline;
13898 fc.firstline.v_name = NULL;
13899 fc.lastline.tv.v_type = VAR_NUMBER;
13900 fc.lastline.tv.vval.v_number = lastline;
13901 fc.lastline.v_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013902 /* Check if this function has a breakpoint. */
13903 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->name, (linenr_T)0);
13904 fc.dbg_tick = debug_tick;
13905
13906 /* Don't redraw while executing the function. */
13907 ++RedrawingDisabled;
13908 save_sourcing_name = sourcing_name;
13909 save_sourcing_lnum = sourcing_lnum;
13910 sourcing_lnum = 1;
13911 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
13912 : STRLEN(save_sourcing_name)) + STRLEN(fp->name) + 13));
13913 if (sourcing_name != NULL)
13914 {
13915 if (save_sourcing_name != NULL
13916 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
13917 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
13918 else
13919 STRCPY(sourcing_name, "function ");
13920 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
13921
13922 if (p_verbose >= 12)
13923 {
13924 ++no_wait_return;
13925 msg_scroll = TRUE; /* always scroll up, don't overwrite */
13926 msg_str((char_u *)_("calling %s"), sourcing_name);
13927 if (p_verbose >= 14)
13928 {
13929 int i;
13930 char_u buf[MSG_BUF_LEN];
13931
13932 msg_puts((char_u *)"(");
13933 for (i = 0; i < argcount; ++i)
13934 {
13935 if (i > 0)
13936 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013937 if (argvars[i].v_type == VAR_NUMBER)
13938 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939 else
13940 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013941 trunc_string(get_tv_string(&argvars[i]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942 buf, MSG_BUF_LEN);
13943 msg_puts((char_u *)"\"");
13944 msg_puts(buf);
13945 msg_puts((char_u *)"\"");
13946 }
13947 }
13948 msg_puts((char_u *)")");
13949 }
13950 msg_puts((char_u *)"\n"); /* don't overwrite this either */
13951 cmdline_row = msg_row;
13952 --no_wait_return;
13953 }
13954 }
13955 save_current_SID = current_SID;
13956 current_SID = fp->script_ID;
13957 save_did_emsg = did_emsg;
13958 did_emsg = FALSE;
13959
13960 /* call do_cmdline() to execute the lines */
13961 do_cmdline(NULL, get_func_line, (void *)&fc,
13962 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
13963
13964 --RedrawingDisabled;
13965
13966 /* when the function was aborted because of an error, return -1 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013967 if ((did_emsg && (fp->flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013969 clear_tv(rettv);
13970 rettv->v_type = VAR_NUMBER;
13971 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972 }
13973
13974 /* when being verbose, mention the return value */
13975 if (p_verbose >= 12)
13976 {
13977 char_u *sn, *val;
13978
13979 ++no_wait_return;
13980 msg_scroll = TRUE; /* always scroll up, don't overwrite */
13981
13982 /* Make sure the output fits in IObuff. */
13983 sn = sourcing_name;
13984 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
13985 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
13986
13987 if (aborting())
13988 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013989 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013991 (long)fc.rettv->vval.v_number);
13992 else if (fc.rettv->v_type == VAR_STRING)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013994 val = get_tv_string(fc.rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995 if (STRLEN(val) > IOSIZE / 2 - 50)
13996 val = val + STRLEN(val) - (IOSIZE / 2 - 50);
13997 smsg((char_u *)_("%s returning \"%s\""), sn, val);
13998 }
13999 msg_puts((char_u *)"\n"); /* don't overwrite this either */
14000 cmdline_row = msg_row;
14001 --no_wait_return;
14002 }
14003
14004 vim_free(sourcing_name);
14005 sourcing_name = save_sourcing_name;
14006 sourcing_lnum = save_sourcing_lnum;
14007 current_SID = save_current_SID;
14008
14009 if (p_verbose >= 12 && sourcing_name != NULL)
14010 {
14011 ++no_wait_return;
14012 msg_scroll = TRUE; /* always scroll up, don't overwrite */
14013 msg_str((char_u *)_("continuing in %s"), sourcing_name);
14014 msg_puts((char_u *)"\n"); /* don't overwrite this either */
14015 cmdline_row = msg_row;
14016 --no_wait_return;
14017 }
14018
14019 did_emsg |= save_did_emsg;
14020 current_funccal = save_fcp;
14021
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014022 vars_clear(&fc.l_vars); /* free all local variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014023 --depth;
14024}
14025
14026/*
14027 * ":return [expr]"
14028 */
14029 void
14030ex_return(eap)
14031 exarg_T *eap;
14032{
14033 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014034 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035 int returning = FALSE;
14036
14037 if (current_funccal == NULL)
14038 {
14039 EMSG(_("E133: :return not inside a function"));
14040 return;
14041 }
14042
14043 if (eap->skip)
14044 ++emsg_skip;
14045
14046 eap->nextcmd = NULL;
14047 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014048 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049 {
14050 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014051 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014052 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014053 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054 }
14055 /* It's safer to return also on error. */
14056 else if (!eap->skip)
14057 {
14058 /*
14059 * Return unless the expression evaluation has been cancelled due to an
14060 * aborting error, an interrupt, or an exception.
14061 */
14062 if (!aborting())
14063 returning = do_return(eap, FALSE, TRUE, NULL);
14064 }
14065
14066 /* When skipping or the return gets pending, advance to the next command
14067 * in this line (!returning). Otherwise, ignore the rest of the line.
14068 * Following lines will be ignored by get_func_line(). */
14069 if (returning)
14070 eap->nextcmd = NULL;
14071 else if (eap->nextcmd == NULL) /* no argument */
14072 eap->nextcmd = check_nextcmd(arg);
14073
14074 if (eap->skip)
14075 --emsg_skip;
14076}
14077
14078/*
14079 * Return from a function. Possibly makes the return pending. Also called
14080 * for a pending return at the ":endtry" or after returning from an extra
14081 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014082 * when called due to a ":return" command. "rettv" may point to a typeval
14083 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084 * FALSE when the return gets pending.
14085 */
14086 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014087do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088 exarg_T *eap;
14089 int reanimate;
14090 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014091 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014092{
14093 int idx;
14094 struct condstack *cstack = eap->cstack;
14095
14096 if (reanimate)
14097 /* Undo the return. */
14098 current_funccal->returned = FALSE;
14099
14100 /*
14101 * Cleanup (and inactivate) conditionals, but stop when a try conditional
14102 * not in its finally clause (which then is to be executed next) is found.
14103 * In this case, make the ":return" pending for execution at the ":endtry".
14104 * Otherwise, return normally.
14105 */
14106 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
14107 if (idx >= 0)
14108 {
14109 cstack->cs_pending[idx] = CSTP_RETURN;
14110
14111 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014112 /* A pending return again gets pending. "rettv" points to an
14113 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000014114 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014115 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116 else
14117 {
14118 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014119 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014121 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014122
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014123 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 {
14125 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014126 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
14127 *(typeval *)cstack->cs_rettv[idx] = *(typeval *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128 else
14129 EMSG(_(e_outofmem));
14130 }
14131 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014132 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014133
14134 if (reanimate)
14135 {
14136 /* The pending return value could be overwritten by a ":return"
14137 * without argument in a finally clause; reset the default
14138 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014139 current_funccal->rettv->v_type = VAR_NUMBER;
14140 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141 }
14142 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014143 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144 }
14145 else
14146 {
14147 current_funccal->returned = TRUE;
14148
14149 /* If the return is carried out now, store the return value. For
14150 * a return immediately after reanimation, the value is already
14151 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014152 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014154 clear_tv(current_funccal->rettv);
14155 *current_funccal->rettv = *(typeval *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014157 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014158 }
14159 }
14160
14161 return idx < 0;
14162}
14163
14164/*
14165 * Free the variable with a pending return value.
14166 */
14167 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014168discard_pending_return(rettv)
14169 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014171 free_tv((typeval *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014172}
14173
14174/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014175 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176 * is an allocated string. Used by report_pending() for verbose messages.
14177 */
14178 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014179get_return_cmd(rettv)
14180 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014182 char_u *s;
14183 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014184 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014186 if (rettv == NULL)
14187 s = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188 else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014189 s = echo_string((typeval *)rettv, &tofree, numbuf);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014190
14191 STRCPY(IObuff, ":return ");
14192 STRNCPY(IObuff + 8, s, IOSIZE - 8);
14193 if (STRLEN(s) + 8 >= IOSIZE)
14194 STRCPY(IObuff + IOSIZE - 4, "...");
14195 vim_free(tofree);
14196 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197}
14198
14199/*
14200 * Get next function line.
14201 * Called by do_cmdline() to get the next line.
14202 * Returns allocated string, or NULL for end of function.
14203 */
14204/* ARGSUSED */
14205 char_u *
14206get_func_line(c, cookie, indent)
14207 int c; /* not used */
14208 void *cookie;
14209 int indent; /* not used */
14210{
14211 struct funccall *fcp = (struct funccall *)cookie;
14212 char_u *retval;
14213 garray_T *gap; /* growarray with function lines */
14214
14215 /* If breakpoints have been added/deleted need to check for it. */
14216 if (fcp->dbg_tick != debug_tick)
14217 {
14218 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
14219 sourcing_lnum);
14220 fcp->dbg_tick = debug_tick;
14221 }
14222
14223 gap = &fcp->func->lines;
14224 if ((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
14225 retval = NULL;
14226 else if (fcp->returned || fcp->linenr >= gap->ga_len)
14227 retval = NULL;
14228 else
14229 {
14230 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
14231 sourcing_lnum = fcp->linenr;
14232 }
14233
14234 /* Did we encounter a breakpoint? */
14235 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
14236 {
14237 dbg_breakpoint(fcp->func->name, sourcing_lnum);
14238 /* Find next breakpoint. */
14239 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
14240 sourcing_lnum);
14241 fcp->dbg_tick = debug_tick;
14242 }
14243
14244 return retval;
14245}
14246
14247/*
14248 * Return TRUE if the currently active function should be ended, because a
14249 * return was encountered or an error occured. Used inside a ":while".
14250 */
14251 int
14252func_has_ended(cookie)
14253 void *cookie;
14254{
14255 struct funccall *fcp = (struct funccall *)cookie;
14256
14257 /* Ignore the "abort" flag if the abortion behavior has been changed due to
14258 * an error inside a try conditional. */
14259 return (((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
14260 || fcp->returned);
14261}
14262
14263/*
14264 * return TRUE if cookie indicates a function which "abort"s on errors.
14265 */
14266 int
14267func_has_abort(cookie)
14268 void *cookie;
14269{
14270 return ((struct funccall *)cookie)->func->flags & FC_ABORT;
14271}
14272
14273#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
14274typedef enum
14275{
14276 VAR_FLAVOUR_DEFAULT,
14277 VAR_FLAVOUR_SESSION,
14278 VAR_FLAVOUR_VIMINFO
14279} var_flavour_T;
14280
14281static var_flavour_T var_flavour __ARGS((char_u *varname));
14282
14283 static var_flavour_T
14284var_flavour(varname)
14285 char_u *varname;
14286{
14287 char_u *p = varname;
14288
14289 if (ASCII_ISUPPER(*p))
14290 {
14291 while (*(++p))
14292 if (ASCII_ISLOWER(*p))
14293 return VAR_FLAVOUR_SESSION;
14294 return VAR_FLAVOUR_VIMINFO;
14295 }
14296 else
14297 return VAR_FLAVOUR_DEFAULT;
14298}
14299#endif
14300
14301#if defined(FEAT_VIMINFO) || defined(PROTO)
14302/*
14303 * Restore global vars that start with a capital from the viminfo file
14304 */
14305 int
14306read_viminfo_varlist(virp, writing)
14307 vir_T *virp;
14308 int writing;
14309{
14310 char_u *tab;
14311 int is_string = FALSE;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014312 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313
14314 if (!writing && (find_viminfo_parameter('!') != NULL))
14315 {
14316 tab = vim_strchr(virp->vir_line + 1, '\t');
14317 if (tab != NULL)
14318 {
14319 *tab++ = '\0'; /* isolate the variable name */
14320 if (*tab == 'S') /* string var */
14321 is_string = TRUE;
14322
14323 tab = vim_strchr(tab, '\t');
14324 if (tab != NULL)
14325 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326 if (is_string)
14327 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014328 tv.v_type = VAR_STRING;
14329 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331 }
14332 else
14333 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014334 tv.v_type = VAR_NUMBER;
14335 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014337 set_var(virp->vir_line + 1, &tv, FALSE);
14338 if (is_string)
14339 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340 }
14341 }
14342 }
14343
14344 return viminfo_readline(virp);
14345}
14346
14347/*
14348 * Write global vars that start with a capital to the viminfo file
14349 */
14350 void
14351write_viminfo_varlist(fp)
14352 FILE *fp;
14353{
14354 garray_T *gap = &variables; /* global variable */
14355 VAR this_var;
14356 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014357 char *s;
14358 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014359 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360
14361 if (find_viminfo_parameter('!') == NULL)
14362 return;
14363
14364 fprintf(fp, _("\n# global variables:\n"));
14365 for (i = gap->ga_len; --i >= 0; )
14366 {
14367 this_var = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014368 if (this_var->v_name != NULL
14369 && var_flavour(this_var->v_name) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014370 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014371 switch (this_var->tv.v_type)
14372 {
14373 case VAR_STRING: s = "STR"; break;
14374 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014375 default: continue;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014376 }
14377 fprintf(fp, "!%s\t%s\t", this_var->v_name, s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014378 viminfo_writestring(fp, echo_string(&this_var->tv,
14379 &tofree, numbuf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014380 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381 }
14382 }
14383}
14384#endif
14385
14386#if defined(FEAT_SESSION) || defined(PROTO)
14387 int
14388store_session_globals(fd)
14389 FILE *fd;
14390{
14391 garray_T *gap = &variables; /* global variable */
14392 VAR this_var;
14393 int i;
14394 char_u *p, *t;
14395
14396 for (i = gap->ga_len; --i >= 0; )
14397 {
14398 this_var = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014399 if (this_var->v_name != NULL
14400 && (this_var->tv.v_type == VAR_NUMBER
14401 || this_var->tv.v_type == VAR_STRING)
14402 && var_flavour(this_var->v_name) == VAR_FLAVOUR_SESSION)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014404 /* Escape special characters with a backslash. Turn a LF and
14405 * CR into \n and \r. */
14406 p = vim_strsave_escaped(get_var_string(this_var),
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407 (char_u *)"\\\"\n\r");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014408 if (p == NULL) /* out of memory */
14409 continue;
14410 for (t = p; *t != NUL; ++t)
14411 if (*t == '\n')
14412 *t = 'n';
14413 else if (*t == '\r')
14414 *t = 'r';
14415 if ((fprintf(fd, "let %s = %c%s%c",
14416 this_var->v_name,
14417 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ',
14418 p,
14419 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ') < 0)
14420 || put_eol(fd) == FAIL)
14421 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422 vim_free(p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014423 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014424 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000014425 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014426 }
14427 }
14428 return OK;
14429}
14430#endif
14431
14432#endif /* FEAT_EVAL */
14433
14434#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
14435
14436
14437#ifdef WIN3264
14438/*
14439 * Functions for ":8" filename modifier: get 8.3 version of a filename.
14440 */
14441static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
14442static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
14443static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
14444
14445/*
14446 * Get the short pathname of a file.
14447 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
14448 */
14449 static int
14450get_short_pathname(fnamep, bufp, fnamelen)
14451 char_u **fnamep;
14452 char_u **bufp;
14453 int *fnamelen;
14454{
14455 int l,len;
14456 char_u *newbuf;
14457
14458 len = *fnamelen;
14459
14460 l = GetShortPathName(*fnamep, *fnamep, len);
14461 if (l > len - 1)
14462 {
14463 /* If that doesn't work (not enough space), then save the string
14464 * and try again with a new buffer big enough
14465 */
14466 newbuf = vim_strnsave(*fnamep, l);
14467 if (newbuf == NULL)
14468 return 0;
14469
14470 vim_free(*bufp);
14471 *fnamep = *bufp = newbuf;
14472
14473 l = GetShortPathName(*fnamep,*fnamep,l+1);
14474
14475 /* Really should always succeed, as the buffer is big enough */
14476 }
14477
14478 *fnamelen = l;
14479 return 1;
14480}
14481
14482/*
14483 * Create a short path name. Returns the length of the buffer it needs.
14484 * Doesn't copy over the end of the buffer passed in.
14485 */
14486 static int
14487shortpath_for_invalid_fname(fname, bufp, fnamelen)
14488 char_u **fname;
14489 char_u **bufp;
14490 int *fnamelen;
14491{
14492 char_u *s, *p, *pbuf2, *pbuf3;
14493 char_u ch;
14494 int l,len,len2,plen,slen;
14495
14496 /* Make a copy */
14497 len2 = *fnamelen;
14498 pbuf2 = vim_strnsave(*fname, len2);
14499 pbuf3 = NULL;
14500
14501 s = pbuf2 + len2 - 1; /* Find the end */
14502 slen = 1;
14503 plen = len2;
14504
14505 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014506 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507 {
14508 --s;
14509 ++slen;
14510 --plen;
14511 }
14512
14513 do
14514 {
14515 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014516 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517 {
14518 --s;
14519 ++slen;
14520 --plen;
14521 }
14522 if (s <= pbuf2)
14523 break;
14524
14525 /* Remeber the character that is about to be blatted */
14526 ch = *s;
14527 *s = 0; /* get_short_pathname requires a null-terminated string */
14528
14529 /* Try it in situ */
14530 p = pbuf2;
14531 if (!get_short_pathname(&p, &pbuf3, &plen))
14532 {
14533 vim_free(pbuf2);
14534 return -1;
14535 }
14536 *s = ch; /* Preserve the string */
14537 } while (plen == 0);
14538
14539 if (plen > 0)
14540 {
14541 /* Remeber the length of the new string. */
14542 *fnamelen = len = plen + slen;
14543 vim_free(*bufp);
14544 if (len > len2)
14545 {
14546 /* If there's not enough space in the currently allocated string,
14547 * then copy it to a buffer big enough.
14548 */
14549 *fname= *bufp = vim_strnsave(p, len);
14550 if (*fname == NULL)
14551 return -1;
14552 }
14553 else
14554 {
14555 /* Transfer pbuf2 to being the main buffer (it's big enough) */
14556 *fname = *bufp = pbuf2;
14557 if (p != pbuf2)
14558 strncpy(*fname, p, plen);
14559 pbuf2 = NULL;
14560 }
14561 /* Concat the next bit */
14562 strncpy(*fname + plen, s, slen);
14563 (*fname)[len] = '\0';
14564 }
14565 vim_free(pbuf3);
14566 vim_free(pbuf2);
14567 return 0;
14568}
14569
14570/*
14571 * Get a pathname for a partial path.
14572 */
14573 static int
14574shortpath_for_partial(fnamep, bufp, fnamelen)
14575 char_u **fnamep;
14576 char_u **bufp;
14577 int *fnamelen;
14578{
14579 int sepcount, len, tflen;
14580 char_u *p;
14581 char_u *pbuf, *tfname;
14582 int hasTilde;
14583
14584 /* Count up the path seperators from the RHS.. so we know which part
14585 * of the path to return.
14586 */
14587 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014588 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014589 if (vim_ispathsep(*p))
14590 ++sepcount;
14591
14592 /* Need full path first (use expand_env() to remove a "~/") */
14593 hasTilde = (**fnamep == '~');
14594 if (hasTilde)
14595 pbuf = tfname = expand_env_save(*fnamep);
14596 else
14597 pbuf = tfname = FullName_save(*fnamep, FALSE);
14598
14599 len = tflen = STRLEN(tfname);
14600
14601 if (!get_short_pathname(&tfname, &pbuf, &len))
14602 return -1;
14603
14604 if (len == 0)
14605 {
14606 /* Don't have a valid filename, so shorten the rest of the
14607 * path if we can. This CAN give us invalid 8.3 filenames, but
14608 * there's not a lot of point in guessing what it might be.
14609 */
14610 len = tflen;
14611 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
14612 return -1;
14613 }
14614
14615 /* Count the paths backward to find the beginning of the desired string. */
14616 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014617 {
14618#ifdef FEAT_MBYTE
14619 if (has_mbyte)
14620 p -= mb_head_off(tfname, p);
14621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014622 if (vim_ispathsep(*p))
14623 {
14624 if (sepcount == 0 || (hasTilde && sepcount == 1))
14625 break;
14626 else
14627 sepcount --;
14628 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014630 if (hasTilde)
14631 {
14632 --p;
14633 if (p >= tfname)
14634 *p = '~';
14635 else
14636 return -1;
14637 }
14638 else
14639 ++p;
14640
14641 /* Copy in the string - p indexes into tfname - allocated at pbuf */
14642 vim_free(*bufp);
14643 *fnamelen = (int)STRLEN(p);
14644 *bufp = pbuf;
14645 *fnamep = p;
14646
14647 return 0;
14648}
14649#endif /* WIN3264 */
14650
14651/*
14652 * Adjust a filename, according to a string of modifiers.
14653 * *fnamep must be NUL terminated when called. When returning, the length is
14654 * determined by *fnamelen.
14655 * Returns valid flags.
14656 * When there is an error, *fnamep is set to NULL.
14657 */
14658 int
14659modify_fname(src, usedlen, fnamep, bufp, fnamelen)
14660 char_u *src; /* string with modifiers */
14661 int *usedlen; /* characters after src that are used */
14662 char_u **fnamep; /* file name so far */
14663 char_u **bufp; /* buffer for allocated file name or NULL */
14664 int *fnamelen; /* length of fnamep */
14665{
14666 int valid = 0;
14667 char_u *tail;
14668 char_u *s, *p, *pbuf;
14669 char_u dirname[MAXPATHL];
14670 int c;
14671 int has_fullname = 0;
14672#ifdef WIN3264
14673 int has_shortname = 0;
14674#endif
14675
14676repeat:
14677 /* ":p" - full path/file_name */
14678 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
14679 {
14680 has_fullname = 1;
14681
14682 valid |= VALID_PATH;
14683 *usedlen += 2;
14684
14685 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
14686 if ((*fnamep)[0] == '~'
14687#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
14688 && ((*fnamep)[1] == '/'
14689# ifdef BACKSLASH_IN_FILENAME
14690 || (*fnamep)[1] == '\\'
14691# endif
14692 || (*fnamep)[1] == NUL)
14693
14694#endif
14695 )
14696 {
14697 *fnamep = expand_env_save(*fnamep);
14698 vim_free(*bufp); /* free any allocated file name */
14699 *bufp = *fnamep;
14700 if (*fnamep == NULL)
14701 return -1;
14702 }
14703
14704 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014705 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014706 {
14707 if (vim_ispathsep(*p)
14708 && p[1] == '.'
14709 && (p[2] == NUL
14710 || vim_ispathsep(p[2])
14711 || (p[2] == '.'
14712 && (p[3] == NUL || vim_ispathsep(p[3])))))
14713 break;
14714 }
14715
14716 /* FullName_save() is slow, don't use it when not needed. */
14717 if (*p != NUL || !vim_isAbsName(*fnamep))
14718 {
14719 *fnamep = FullName_save(*fnamep, *p != NUL);
14720 vim_free(*bufp); /* free any allocated file name */
14721 *bufp = *fnamep;
14722 if (*fnamep == NULL)
14723 return -1;
14724 }
14725
14726 /* Append a path separator to a directory. */
14727 if (mch_isdir(*fnamep))
14728 {
14729 /* Make room for one or two extra characters. */
14730 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
14731 vim_free(*bufp); /* free any allocated file name */
14732 *bufp = *fnamep;
14733 if (*fnamep == NULL)
14734 return -1;
14735 add_pathsep(*fnamep);
14736 }
14737 }
14738
14739 /* ":." - path relative to the current directory */
14740 /* ":~" - path relative to the home directory */
14741 /* ":8" - shortname path - postponed till after */
14742 while (src[*usedlen] == ':'
14743 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
14744 {
14745 *usedlen += 2;
14746 if (c == '8')
14747 {
14748#ifdef WIN3264
14749 has_shortname = 1; /* Postpone this. */
14750#endif
14751 continue;
14752 }
14753 pbuf = NULL;
14754 /* Need full path first (use expand_env() to remove a "~/") */
14755 if (!has_fullname)
14756 {
14757 if (c == '.' && **fnamep == '~')
14758 p = pbuf = expand_env_save(*fnamep);
14759 else
14760 p = pbuf = FullName_save(*fnamep, FALSE);
14761 }
14762 else
14763 p = *fnamep;
14764
14765 has_fullname = 0;
14766
14767 if (p != NULL)
14768 {
14769 if (c == '.')
14770 {
14771 mch_dirname(dirname, MAXPATHL);
14772 s = shorten_fname(p, dirname);
14773 if (s != NULL)
14774 {
14775 *fnamep = s;
14776 if (pbuf != NULL)
14777 {
14778 vim_free(*bufp); /* free any allocated file name */
14779 *bufp = pbuf;
14780 pbuf = NULL;
14781 }
14782 }
14783 }
14784 else
14785 {
14786 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
14787 /* Only replace it when it starts with '~' */
14788 if (*dirname == '~')
14789 {
14790 s = vim_strsave(dirname);
14791 if (s != NULL)
14792 {
14793 *fnamep = s;
14794 vim_free(*bufp);
14795 *bufp = s;
14796 }
14797 }
14798 }
14799 vim_free(pbuf);
14800 }
14801 }
14802
14803 tail = gettail(*fnamep);
14804 *fnamelen = (int)STRLEN(*fnamep);
14805
14806 /* ":h" - head, remove "/file_name", can be repeated */
14807 /* Don't remove the first "/" or "c:\" */
14808 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
14809 {
14810 valid |= VALID_HEAD;
14811 *usedlen += 2;
14812 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014813 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014814 --tail;
14815 *fnamelen = (int)(tail - *fnamep);
14816#ifdef VMS
14817 if (*fnamelen > 0)
14818 *fnamelen += 1; /* the path separator is part of the path */
14819#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014820 while (tail > s && !after_pathsep(s, tail))
14821 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014822 }
14823
14824 /* ":8" - shortname */
14825 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
14826 {
14827 *usedlen += 2;
14828#ifdef WIN3264
14829 has_shortname = 1;
14830#endif
14831 }
14832
14833#ifdef WIN3264
14834 /* Check shortname after we have done 'heads' and before we do 'tails'
14835 */
14836 if (has_shortname)
14837 {
14838 pbuf = NULL;
14839 /* Copy the string if it is shortened by :h */
14840 if (*fnamelen < (int)STRLEN(*fnamep))
14841 {
14842 p = vim_strnsave(*fnamep, *fnamelen);
14843 if (p == 0)
14844 return -1;
14845 vim_free(*bufp);
14846 *bufp = *fnamep = p;
14847 }
14848
14849 /* Split into two implementations - makes it easier. First is where
14850 * there isn't a full name already, second is where there is.
14851 */
14852 if (!has_fullname && !vim_isAbsName(*fnamep))
14853 {
14854 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
14855 return -1;
14856 }
14857 else
14858 {
14859 int l;
14860
14861 /* Simple case, already have the full-name
14862 * Nearly always shorter, so try first time. */
14863 l = *fnamelen;
14864 if (!get_short_pathname(fnamep, bufp, &l))
14865 return -1;
14866
14867 if (l == 0)
14868 {
14869 /* Couldn't find the filename.. search the paths.
14870 */
14871 l = *fnamelen;
14872 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
14873 return -1;
14874 }
14875 *fnamelen = l;
14876 }
14877 }
14878#endif /* WIN3264 */
14879
14880 /* ":t" - tail, just the basename */
14881 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
14882 {
14883 *usedlen += 2;
14884 *fnamelen -= (int)(tail - *fnamep);
14885 *fnamep = tail;
14886 }
14887
14888 /* ":e" - extension, can be repeated */
14889 /* ":r" - root, without extension, can be repeated */
14890 while (src[*usedlen] == ':'
14891 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
14892 {
14893 /* find a '.' in the tail:
14894 * - for second :e: before the current fname
14895 * - otherwise: The last '.'
14896 */
14897 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
14898 s = *fnamep - 2;
14899 else
14900 s = *fnamep + *fnamelen - 1;
14901 for ( ; s > tail; --s)
14902 if (s[0] == '.')
14903 break;
14904 if (src[*usedlen + 1] == 'e') /* :e */
14905 {
14906 if (s > tail)
14907 {
14908 *fnamelen += (int)(*fnamep - (s + 1));
14909 *fnamep = s + 1;
14910#ifdef VMS
14911 /* cut version from the extension */
14912 s = *fnamep + *fnamelen - 1;
14913 for ( ; s > *fnamep; --s)
14914 if (s[0] == ';')
14915 break;
14916 if (s > *fnamep)
14917 *fnamelen = s - *fnamep;
14918#endif
14919 }
14920 else if (*fnamep <= tail)
14921 *fnamelen = 0;
14922 }
14923 else /* :r */
14924 {
14925 if (s > tail) /* remove one extension */
14926 *fnamelen = (int)(s - *fnamep);
14927 }
14928 *usedlen += 2;
14929 }
14930
14931 /* ":s?pat?foo?" - substitute */
14932 /* ":gs?pat?foo?" - global substitute */
14933 if (src[*usedlen] == ':'
14934 && (src[*usedlen + 1] == 's'
14935 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
14936 {
14937 char_u *str;
14938 char_u *pat;
14939 char_u *sub;
14940 int sep;
14941 char_u *flags;
14942 int didit = FALSE;
14943
14944 flags = (char_u *)"";
14945 s = src + *usedlen + 2;
14946 if (src[*usedlen + 1] == 'g')
14947 {
14948 flags = (char_u *)"g";
14949 ++s;
14950 }
14951
14952 sep = *s++;
14953 if (sep)
14954 {
14955 /* find end of pattern */
14956 p = vim_strchr(s, sep);
14957 if (p != NULL)
14958 {
14959 pat = vim_strnsave(s, (int)(p - s));
14960 if (pat != NULL)
14961 {
14962 s = p + 1;
14963 /* find end of substitution */
14964 p = vim_strchr(s, sep);
14965 if (p != NULL)
14966 {
14967 sub = vim_strnsave(s, (int)(p - s));
14968 str = vim_strnsave(*fnamep, *fnamelen);
14969 if (sub != NULL && str != NULL)
14970 {
14971 *usedlen = (int)(p + 1 - src);
14972 s = do_string_sub(str, pat, sub, flags);
14973 if (s != NULL)
14974 {
14975 *fnamep = s;
14976 *fnamelen = (int)STRLEN(s);
14977 vim_free(*bufp);
14978 *bufp = s;
14979 didit = TRUE;
14980 }
14981 }
14982 vim_free(sub);
14983 vim_free(str);
14984 }
14985 vim_free(pat);
14986 }
14987 }
14988 /* after using ":s", repeat all the modifiers */
14989 if (didit)
14990 goto repeat;
14991 }
14992 }
14993
14994 return valid;
14995}
14996
14997/*
14998 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
14999 * "flags" can be "g" to do a global substitute.
15000 * Returns an allocated string, NULL for error.
15001 */
15002 char_u *
15003do_string_sub(str, pat, sub, flags)
15004 char_u *str;
15005 char_u *pat;
15006 char_u *sub;
15007 char_u *flags;
15008{
15009 int sublen;
15010 regmatch_T regmatch;
15011 int i;
15012 int do_all;
15013 char_u *tail;
15014 garray_T ga;
15015 char_u *ret;
15016 char_u *save_cpo;
15017
15018 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
15019 save_cpo = p_cpo;
15020 p_cpo = (char_u *)"";
15021
15022 ga_init2(&ga, 1, 200);
15023
15024 do_all = (flags[0] == 'g');
15025
15026 regmatch.rm_ic = p_ic;
15027 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15028 if (regmatch.regprog != NULL)
15029 {
15030 tail = str;
15031 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
15032 {
15033 /*
15034 * Get some space for a temporary buffer to do the substitution
15035 * into. It will contain:
15036 * - The text up to where the match is.
15037 * - The substituted text.
15038 * - The text after the match.
15039 */
15040 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
15041 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
15042 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
15043 {
15044 ga_clear(&ga);
15045 break;
15046 }
15047
15048 /* copy the text up to where the match is */
15049 i = (int)(regmatch.startp[0] - tail);
15050 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
15051 /* add the substituted text */
15052 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
15053 + ga.ga_len + i, TRUE, TRUE, FALSE);
15054 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055 /* avoid getting stuck on a match with an empty string */
15056 if (tail == regmatch.endp[0])
15057 {
15058 if (*tail == NUL)
15059 break;
15060 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
15061 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015062 }
15063 else
15064 {
15065 tail = regmatch.endp[0];
15066 if (*tail == NUL)
15067 break;
15068 }
15069 if (!do_all)
15070 break;
15071 }
15072
15073 if (ga.ga_data != NULL)
15074 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
15075
15076 vim_free(regmatch.regprog);
15077 }
15078
15079 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
15080 ga_clear(&ga);
15081 p_cpo = save_cpo;
15082
15083 return ret;
15084}
15085
15086#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */