blob: e28e5d6773e17ffe2b5e761a7d3649e9b41e8455 [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!) */
50 } vval;
51} typeval;
52
53/* Values for "v_type". */
54#define VAR_UNKNOWN 0
55#define VAR_NUMBER 1 /* "v_number" is used */
56#define VAR_STRING 2 /* "v_string" is used */
57#define VAR_FUNC 3 /* "v_string" is function name */
58#define VAR_LIST 4 /* "v_list" is used */
59
60/*
61 * Structure to hold an internal variable with a name.
62 * The "tv" must come first, so that this can be used as a "typeval" as well.
63 */
64typedef struct
65{
66 typeval tv; /* type and value of the variable */
67 char_u *v_name; /* name of variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000068} var;
69
Bram Moolenaar071d4272004-06-13 20:20:40 +000070typedef var * VAR;
71
72/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000073 * Structure to hold an item of a list: an internal variable without a name.
74 */
75struct listitem_S
76{
77 struct listitem_S *li_next; /* next item in list */
78 struct listitem_S *li_prev; /* previous item in list */
79 typeval li_tv; /* type and value of the variable */
80};
81
82typedef struct listitem_S listitem;
83
84/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000085 * Struct used by those that are using an item in a list.
86 */
87typedef struct listwatch_S
88{
89 listitem *lw_item; /* item being watched */
90 struct listwatch_S *lw_next; /* next watcher */
91} listwatch;
92
93/*
94 * Structure to hold info about a list.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000095 */
96struct listvar_S
97{
98 int lv_refcount; /* reference count */
99 listitem *lv_first; /* first item, NULL if none */
100 listitem *lv_last; /* last item, NULL if none */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000101 listwatch *lv_watch; /* first watcher, NULL if none */
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000102};
103
104typedef struct listvar_S listvar;
105
106#define VAR_LIST_MAXNEST 100 /* maximum nesting of lists */
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000107static char *e_letunexp = N_("E18: Unexpected characters in :let");
108static char *e_listidx = N_("E999: list index out of range: %ld");
109static char *e_undefvar = N_("E121: Undefined variable: %s");
110static char *e_missbrac = N_("E111: Missing ']'");
111static char *e_intern2 = N_("E999: Internal error: %s");
Bram Moolenaar0d660222005-01-07 21:51:51 +0000112static char *e_listarg = N_("E999: Argument of %s must be a list");
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000113
114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 * All user-defined global variables are stored in "variables".
116 */
117garray_T variables = {0, 0, sizeof(var), 4, NULL};
118
119/*
120 * Array to hold an array with variables local to each sourced script.
121 */
122static garray_T ga_scripts = {0, 0, sizeof(garray_T), 4, NULL};
123#define SCRIPT_VARS(id) (((garray_T *)ga_scripts.ga_data)[(id) - 1])
124
125
126#define VAR_ENTRY(idx) (((VAR)(variables.ga_data))[idx])
127#define VAR_GAP_ENTRY(idx, gap) (((VAR)(gap->ga_data))[idx])
128#define BVAR_ENTRY(idx) (((VAR)(curbuf->b_vars.ga_data))[idx])
129#define WVAR_ENTRY(idx) (((VAR)(curwin->w_vars.ga_data))[idx])
130
131static int echo_attr = 0; /* attributes used for ":echo" */
132
133/*
134 * Structure to hold info for a user function.
135 */
136typedef struct ufunc ufunc_T;
137
138struct ufunc
139{
140 ufunc_T *next; /* next function in list */
141 char_u *name; /* name of function; can start with <SNR>123_
142 (<SNR> is K_SPECIAL KS_EXTRA KE_SNR) */
143 int varargs; /* variable nr of arguments */
144 int flags;
145 int calls; /* nr of active calls */
146 garray_T args; /* arguments */
147 garray_T lines; /* function lines */
148 scid_T script_ID; /* ID of script where function was defined,
149 used for s: variables */
150};
151
152/* function flags */
153#define FC_ABORT 1 /* abort function on error */
154#define FC_RANGE 2 /* function accepts range */
155
156/*
157 * All user-defined functions are found in the forward-linked function list.
158 * The first function is pointed at by firstfunc.
159 */
160ufunc_T *firstfunc = NULL;
161
162#define FUNCARG(fp, j) ((char_u **)(fp->args.ga_data))[j]
163#define FUNCLINE(fp, j) ((char_u **)(fp->lines.ga_data))[j]
164
165/* structure to hold info for a function that is currently being executed. */
166struct funccall
167{
168 ufunc_T *func; /* function being called */
169 int linenr; /* next line to be executed */
170 int returned; /* ":return" used */
171 int argcount; /* nr of arguments */
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000172 typeval *argvars; /* arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 var a0_var; /* "a:0" variable */
174 var firstline; /* "a:firstline" variable */
175 var lastline; /* "a:lastline" variable */
176 garray_T l_vars; /* local function variables */
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000177 typeval *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 linenr_T breakpoint; /* next line with breakpoint or zero */
179 int dbg_tick; /* debug_tick when breakpoint was set */
180 int level; /* top nesting level of executed function */
181};
182
183/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000184 * Info used by a ":for" loop.
185 */
186typedef struct forinfo_S
187{
188 int fi_semicolon; /* TRUE if ending in '; var]' */
189 int fi_varcount; /* nr of variables in the list */
190 listwatch fi_lw; /* keep an eye on the item used. */
191 listvar *fi_list; /* list being used */
192} forinfo;
193
194
195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 * Return the name of the executed function.
197 */
198 char_u *
199func_name(cookie)
200 void *cookie;
201{
202 return ((struct funccall *)cookie)->func->name;
203}
204
205/*
206 * Return the address holding the next breakpoint line for a funccall cookie.
207 */
208 linenr_T *
209func_breakpoint(cookie)
210 void *cookie;
211{
212 return &((struct funccall *)cookie)->breakpoint;
213}
214
215/*
216 * Return the address holding the debug tick for a funccall cookie.
217 */
218 int *
219func_dbg_tick(cookie)
220 void *cookie;
221{
222 return &((struct funccall *)cookie)->dbg_tick;
223}
224
225/*
226 * Return the nesting level for a funccall cookie.
227 */
228 int
229func_level(cookie)
230 void *cookie;
231{
232 return ((struct funccall *)cookie)->level;
233}
234
235/* pointer to funccal for currently active function */
236struct funccall *current_funccal = NULL;
237
238/*
239 * Return TRUE when a function was ended by a ":return" command.
240 */
241 int
242current_func_returned()
243{
244 return current_funccal->returned;
245}
246
247
248/*
249 * Array to hold the value of v: variables.
250 */
251#include "version.h"
252
253/* values for flags: */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000254#define VV_COMPAT 1 /* compatible, also used without "v:" */
255#define VV_RO 2 /* read-only */
256#define VV_RO_SBX 4 /* read-only in the sandbox*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258struct vimvar
259{
260 char *name; /* name of variable, without v: */
261 int len; /* length of name */
262 char_u *val; /* current value (can also be a number!) */
263 char type; /* VAR_NUMBER or VAR_STRING */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000264 char flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265} vimvars[VV_LEN] =
266{ /* The order here must match the VV_ defines in vim.h! */
267 {"count", sizeof("count") - 1, NULL, VAR_NUMBER, VV_COMPAT+VV_RO},
268 {"count1", sizeof("count1") - 1, NULL, VAR_NUMBER, VV_RO},
269 {"prevcount", sizeof("prevcount") - 1, NULL, VAR_NUMBER, VV_RO},
270 {"errmsg", sizeof("errmsg") - 1, NULL, VAR_STRING, VV_COMPAT},
271 {"warningmsg", sizeof("warningmsg") - 1, NULL, VAR_STRING, 0},
272 {"statusmsg", sizeof("statusmsg") - 1, NULL, VAR_STRING, 0},
273 {"shell_error", sizeof("shell_error") - 1, NULL, VAR_NUMBER,
274 VV_COMPAT+VV_RO},
275 {"this_session", sizeof("this_session") - 1, NULL, VAR_STRING, VV_COMPAT},
276 {"version", sizeof("version") - 1, (char_u *)VIM_VERSION_100,
277 VAR_NUMBER, VV_COMPAT+VV_RO},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000278 {"lnum", sizeof("lnum") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279 {"termresponse", sizeof("termresponse") - 1, NULL, VAR_STRING, VV_RO},
280 {"fname", sizeof("fname") - 1, NULL, VAR_STRING, VV_RO},
281 {"lang", sizeof("lang") - 1, NULL, VAR_STRING, VV_RO},
282 {"lc_time", sizeof("lc_time") - 1, NULL, VAR_STRING, VV_RO},
283 {"ctype", sizeof("ctype") - 1, NULL, VAR_STRING, VV_RO},
284 {"charconvert_from", sizeof("charconvert_from") - 1, NULL, VAR_STRING, VV_RO},
285 {"charconvert_to", sizeof("charconvert_to") - 1, NULL, VAR_STRING, VV_RO},
286 {"fname_in", sizeof("fname_in") - 1, NULL, VAR_STRING, VV_RO},
287 {"fname_out", sizeof("fname_out") - 1, NULL, VAR_STRING, VV_RO},
288 {"fname_new", sizeof("fname_new") - 1, NULL, VAR_STRING, VV_RO},
289 {"fname_diff", sizeof("fname_diff") - 1, NULL, VAR_STRING, VV_RO},
290 {"cmdarg", sizeof("cmdarg") - 1, NULL, VAR_STRING, VV_RO},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000291 {"foldstart", sizeof("foldstart") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
292 {"foldend", sizeof("foldend") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
293 {"folddashes", sizeof("folddashes") - 1, NULL, VAR_STRING, VV_RO_SBX},
294 {"foldlevel", sizeof("foldlevel") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 {"progname", sizeof("progname") - 1, NULL, VAR_STRING, VV_RO},
296 {"servername", sizeof("servername") - 1, NULL, VAR_STRING, VV_RO},
297 {"dying", sizeof("dying") - 1, NULL, VAR_NUMBER, VV_RO},
298 {"exception", sizeof("exception") - 1, NULL, VAR_STRING, VV_RO},
299 {"throwpoint", sizeof("throwpoint") - 1, NULL, VAR_STRING, VV_RO},
300 {"register", sizeof("register") - 1, NULL, VAR_STRING, VV_RO},
301 {"cmdbang", sizeof("cmdbang") - 1, NULL, VAR_NUMBER, VV_RO},
Bram Moolenaar843ee412004-06-30 16:16:41 +0000302 {"insertmode", sizeof("insertmode") - 1, NULL, VAR_STRING, VV_RO},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303};
304
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000305static int eval0 __ARGS((char_u *arg, typeval *rettv, char_u **nextcmd, int evaluate));
306static int eval1 __ARGS((char_u **arg, typeval *rettv, int evaluate));
307static int eval2 __ARGS((char_u **arg, typeval *rettv, int evaluate));
308static int eval3 __ARGS((char_u **arg, typeval *rettv, int evaluate));
309static int eval4 __ARGS((char_u **arg, typeval *rettv, int evaluate));
310static int eval5 __ARGS((char_u **arg, typeval *rettv, int evaluate));
311static int eval6 __ARGS((char_u **arg, typeval *rettv, int evaluate));
312static int eval7 __ARGS((char_u **arg, typeval *rettv, int evaluate));
313static int eval_index __ARGS((char_u **arg, typeval *rettv, int evaluate));
314static int get_option_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
315static int get_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
316static int get_lit_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
317static int get_list_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000318static listvar *list_alloc __ARGS((void));
319static void list_unref __ARGS((listvar *l));
320static void list_free __ARGS((listvar *l));
321static listitem *listitem_alloc __ARGS((void));
322static void listitem_free __ARGS((listitem *item));
323static long list_len __ARGS((listvar *l));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000324static int list_equal __ARGS((listvar *l1, listvar *l2, int ic));
325static int tv_equal __ARGS((typeval *tv1, typeval *tv2, int ic));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000326static listitem *list_find __ARGS((listvar *l, long n));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000327static listitem *list_find_ext __ARGS((listvar *l, long *ip));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000328static void list_append __ARGS((listvar *l, listitem *item));
329static int list_append_tv __ARGS((listvar *l, typeval *tv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000330static int list_insert_tv __ARGS((listvar *l, typeval *tv, listitem *item));
331static int list_extend __ARGS((listvar *l1, listvar *l2, listitem *bef));
332static int list_concat __ARGS((listvar *l1, listvar *l2, typeval *tv));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000333static listvar *list_copy __ARGS((listvar *orig, int deep));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000334static void list_getrem __ARGS((listvar *l, listitem *item, listitem *item2));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000335static char_u *list2string __ARGS((typeval *tv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000336static char_u *tv2string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000337static int get_env_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000339static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000340static 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));
341static 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 +0000342
343static void f_add __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000344static void f_append __ARGS((typeval *argvars, typeval *rettv));
345static void f_argc __ARGS((typeval *argvars, typeval *rettv));
346static void f_argidx __ARGS((typeval *argvars, typeval *rettv));
347static void f_argv __ARGS((typeval *argvars, typeval *rettv));
348static void f_browse __ARGS((typeval *argvars, typeval *rettv));
349static void f_browsedir __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000350static void f_bufexists __ARGS((typeval *argvars, typeval *rettv));
351static void f_buflisted __ARGS((typeval *argvars, typeval *rettv));
352static void f_bufloaded __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000353static void f_bufname __ARGS((typeval *argvars, typeval *rettv));
354static void f_bufnr __ARGS((typeval *argvars, typeval *rettv));
355static void f_bufwinnr __ARGS((typeval *argvars, typeval *rettv));
356static void f_byte2line __ARGS((typeval *argvars, typeval *rettv));
357static void f_byteidx __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000358static void f_call __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000359static void f_char2nr __ARGS((typeval *argvars, typeval *rettv));
360static void f_cindent __ARGS((typeval *argvars, typeval *rettv));
361static void f_col __ARGS((typeval *argvars, typeval *rettv));
362static void f_confirm __ARGS((typeval *argvars, typeval *rettv));
363static void f_copy __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000364static void f_count __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000365static void f_cscope_connection __ARGS((typeval *argvars, typeval *rettv));
366static void f_cursor __ARGS((typeval *argsvars, typeval *rettv));
367static void f_deepcopy __ARGS((typeval *argvars, typeval *rettv));
368static void f_delete __ARGS((typeval *argvars, typeval *rettv));
369static void f_did_filetype __ARGS((typeval *argvars, typeval *rettv));
370static void f_diff_filler __ARGS((typeval *argvars, typeval *rettv));
371static void f_diff_hlID __ARGS((typeval *argvars, typeval *rettv));
372static void f_escape __ARGS((typeval *argvars, typeval *rettv));
373static void f_eventhandler __ARGS((typeval *argvars, typeval *rettv));
374static void f_executable __ARGS((typeval *argvars, typeval *rettv));
375static void f_exists __ARGS((typeval *argvars, typeval *rettv));
376static void f_expand __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000377static void f_extend __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000378static void f_filereadable __ARGS((typeval *argvars, typeval *rettv));
379static void f_filewritable __ARGS((typeval *argvars, typeval *rettv));
380static void f_finddir __ARGS((typeval *argvars, typeval *rettv));
381static void f_findfile __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000382static void f_fnamemodify __ARGS((typeval *argvars, typeval *rettv));
383static void f_foldclosed __ARGS((typeval *argvars, typeval *rettv));
384static void f_foldclosedend __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000385static void f_foldlevel __ARGS((typeval *argvars, typeval *rettv));
386static void f_foldtext __ARGS((typeval *argvars, typeval *rettv));
387static void f_foldtextresult __ARGS((typeval *argvars, typeval *rettv));
388static void f_foreground __ARGS((typeval *argvars, typeval *rettv));
389static void f_function __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000390static void f_get __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000391static void f_getbufvar __ARGS((typeval *argvars, typeval *rettv));
392static void f_getchar __ARGS((typeval *argvars, typeval *rettv));
393static void f_getcharmod __ARGS((typeval *argvars, typeval *rettv));
394static void f_getcmdline __ARGS((typeval *argvars, typeval *rettv));
395static void f_getcmdpos __ARGS((typeval *argvars, typeval *rettv));
396static void f_getcwd __ARGS((typeval *argvars, typeval *rettv));
397static void f_getfontname __ARGS((typeval *argvars, typeval *rettv));
398static void f_getfperm __ARGS((typeval *argvars, typeval *rettv));
399static void f_getfsize __ARGS((typeval *argvars, typeval *rettv));
400static void f_getftime __ARGS((typeval *argvars, typeval *rettv));
401static void f_getftype __ARGS((typeval *argvars, typeval *rettv));
402static void f_getline __ARGS((typeval *argvars, typeval *rettv));
403static void f_getreg __ARGS((typeval *argvars, typeval *rettv));
404static void f_getregtype __ARGS((typeval *argvars, typeval *rettv));
405static void f_getwinposx __ARGS((typeval *argvars, typeval *rettv));
406static void f_getwinposy __ARGS((typeval *argvars, typeval *rettv));
407static void f_getwinvar __ARGS((typeval *argvars, typeval *rettv));
408static void f_glob __ARGS((typeval *argvars, typeval *rettv));
409static void f_globpath __ARGS((typeval *argvars, typeval *rettv));
410static void f_has __ARGS((typeval *argvars, typeval *rettv));
411static void f_hasmapto __ARGS((typeval *argvars, typeval *rettv));
412static void f_histadd __ARGS((typeval *argvars, typeval *rettv));
413static void f_histdel __ARGS((typeval *argvars, typeval *rettv));
414static void f_histget __ARGS((typeval *argvars, typeval *rettv));
415static void f_histnr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000416static void f_hlID __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000417static void f_hlexists __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000418static void f_hostname __ARGS((typeval *argvars, typeval *rettv));
419static void f_iconv __ARGS((typeval *argvars, typeval *rettv));
420static void f_indent __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000421static void f_index __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000422static void f_input __ARGS((typeval *argvars, typeval *rettv));
423static void f_inputdialog __ARGS((typeval *argvars, typeval *rettv));
424static void f_inputrestore __ARGS((typeval *argvars, typeval *rettv));
425static void f_inputsave __ARGS((typeval *argvars, typeval *rettv));
426static void f_inputsecret __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000427static void f_insert __ARGS((typeval *argvars, typeval *rettv));
428static void f_isdirectory __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000429static void f_last_buffer_nr __ARGS((typeval *argvars, typeval *rettv));
430static void f_len __ARGS((typeval *argvars, typeval *rettv));
431static void f_libcall __ARGS((typeval *argvars, typeval *rettv));
432static void f_libcallnr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000433static void f_line __ARGS((typeval *argvars, typeval *rettv));
434static void f_line2byte __ARGS((typeval *argvars, typeval *rettv));
435static void f_lispindent __ARGS((typeval *argvars, typeval *rettv));
436static void f_localtime __ARGS((typeval *argvars, typeval *rettv));
437static void f_maparg __ARGS((typeval *argvars, typeval *rettv));
438static void f_mapcheck __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000439static void f_match __ARGS((typeval *argvars, typeval *rettv));
440static void f_matchend __ARGS((typeval *argvars, typeval *rettv));
441static void f_matchstr __ARGS((typeval *argvars, typeval *rettv));
442static void f_mode __ARGS((typeval *argvars, typeval *rettv));
443static void f_nextnonblank __ARGS((typeval *argvars, typeval *rettv));
444static void f_nr2char __ARGS((typeval *argvars, typeval *rettv));
445static void f_prevnonblank __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000446static void f_remote_expr __ARGS((typeval *argvars, typeval *rettv));
447static void f_remote_foreground __ARGS((typeval *argvars, typeval *rettv));
448static void f_remote_peek __ARGS((typeval *argvars, typeval *rettv));
449static void f_remote_read __ARGS((typeval *argvars, typeval *rettv));
450static void f_remote_send __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000451static void f_remove __ARGS((typeval *argvars, typeval *rettv));
452static void f_rename __ARGS((typeval *argvars, typeval *rettv));
453static void f_repeat __ARGS((typeval *argvars, typeval *rettv));
454static void f_resolve __ARGS((typeval *argvars, typeval *rettv));
455static void f_reverse __ARGS((typeval *argvars, typeval *rettv));
456static void f_search __ARGS((typeval *argvars, typeval *rettv));
457static void f_searchpair __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000458static void f_server2client __ARGS((typeval *argvars, typeval *rettv));
459static void f_serverlist __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000460static void f_setbufvar __ARGS((typeval *argvars, typeval *rettv));
461static void f_setcmdpos __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000462static void f_setline __ARGS((typeval *argvars, typeval *rettv));
463static void f_setreg __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000464static void f_setwinvar __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000465static void f_simplify __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000466static void f_sort __ARGS((typeval *argvars, typeval *rettv));
467static void f_str2list __ARGS((typeval *argvars, typeval *rettv));
468#ifdef HAVE_STRFTIME
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000469static void f_strftime __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000470#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000471static void f_stridx __ARGS((typeval *argvars, typeval *rettv));
472static void f_string __ARGS((typeval *argvars, typeval *rettv));
473static void f_strlen __ARGS((typeval *argvars, typeval *rettv));
474static void f_strpart __ARGS((typeval *argvars, typeval *rettv));
475static void f_strridx __ARGS((typeval *argvars, typeval *rettv));
476static void f_strtrans __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000477static void f_submatch __ARGS((typeval *argvars, typeval *rettv));
478static void f_substitute __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000479static void f_synID __ARGS((typeval *argvars, typeval *rettv));
480static void f_synIDattr __ARGS((typeval *argvars, typeval *rettv));
481static void f_synIDtrans __ARGS((typeval *argvars, typeval *rettv));
482static void f_system __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000483static void f_tempname __ARGS((typeval *argvars, typeval *rettv));
484static void f_tolower __ARGS((typeval *argvars, typeval *rettv));
485static void f_toupper __ARGS((typeval *argvars, typeval *rettv));
486static void f_tr __ARGS((typeval *argvars, typeval *rettv));
487static void f_type __ARGS((typeval *argvars, typeval *rettv));
488static void f_virtcol __ARGS((typeval *argvars, typeval *rettv));
489static void f_visualmode __ARGS((typeval *argvars, typeval *rettv));
490static void f_winbufnr __ARGS((typeval *argvars, typeval *rettv));
491static void f_wincol __ARGS((typeval *argvars, typeval *rettv));
492static void f_winheight __ARGS((typeval *argvars, typeval *rettv));
493static void f_winline __ARGS((typeval *argvars, typeval *rettv));
494static void f_winnr __ARGS((typeval *argvars, typeval *rettv));
495static void f_winrestcmd __ARGS((typeval *argvars, typeval *rettv));
496static void f_winwidth __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000497
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000498static win_T *find_win_by_nr __ARGS((typeval *vp));
499static pos_T *var2fpos __ARGS((typeval *varp, int lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500static int get_env_len __ARGS((char_u **arg));
501static int get_id_len __ARGS((char_u **arg));
502static int get_func_len __ARGS((char_u **arg, char_u **alias, int evaluate));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000503static 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 +0000504static int eval_isnamec __ARGS((int c));
505static int find_vim_var __ARGS((char_u *name, int len));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000506static int get_var_tv __ARGS((char_u *name, int len, typeval *rettv));
507static typeval *alloc_tv __ARGS((void));
508static typeval *alloc_string_tv __ARGS((char_u *string));
509static void free_tv __ARGS((typeval *varp));
510static void clear_tv __ARGS((typeval *varp));
511static void init_tv __ARGS((typeval *varp));
512static long get_tv_number __ARGS((typeval *varp));
513static linenr_T get_tv_lnum __ARGS((typeval *argvars));
514static char_u *get_tv_string __ARGS((typeval *varp));
515static char_u *get_tv_string_buf __ARGS((typeval *varp, char_u *buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516static VAR find_var __ARGS((char_u *name, int writing));
517static VAR find_var_in_ga __ARGS((garray_T *gap, char_u *varname));
518static garray_T *find_var_ga __ARGS((char_u *name, char_u **varname));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000519static void clear_var __ARGS((VAR v));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520static void list_one_var __ARGS((VAR v, char_u *prefix));
521static void list_vim_var __ARGS((int i));
522static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000523static void set_var __ARGS((char_u *name, typeval *varp, int copy));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000524static void copy_tv __ARGS((typeval *from, typeval *to));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
526static char_u *trans_function_name __ARGS((char_u **pp, int skip, int internal));
527static int eval_fname_script __ARGS((char_u *p));
528static int eval_fname_sid __ARGS((char_u *p));
529static void list_func_head __ARGS((ufunc_T *fp, int indent));
530static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
531static ufunc_T *find_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000532static int function_exists __ARGS((char_u *name));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000533static 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 +0000534
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000535#define get_var_string(p) get_tv_string(&(p)->tv)
536#define get_var_string_buf(p, b) get_tv_string_buf(&(p)->tv, (b))
537#define get_var_number(p) get_tv_number(&((p)->tv))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539static 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 +0000540
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000541static int ex_let_vars __ARGS((char_u *arg, typeval *tv, int copy, int semicolon, int var_count, char_u *nextchars));
542static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
543static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000544static void list_all_vars __ARGS((void));
545static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
546static char_u *ex_let_one __ARGS((char_u *arg, typeval *tv, int copy, char_u *endchars));
547static 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 +0000548static void list_add_watch __ARGS((listvar *l, listwatch *lw));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549
550/*
551 * Set an internal variable to a string value. Creates the variable if it does
552 * not already exist.
553 */
554 void
555set_internal_string_var(name, value)
556 char_u *name;
557 char_u *value;
558{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000559 char_u *val;
560 typeval *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561
562 val = vim_strsave(value);
563 if (val != NULL)
564 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000565 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000566 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000568 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000569 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 }
571 }
572}
573
574# if defined(FEAT_MBYTE) || defined(PROTO)
575 int
576eval_charconvert(enc_from, enc_to, fname_from, fname_to)
577 char_u *enc_from;
578 char_u *enc_to;
579 char_u *fname_from;
580 char_u *fname_to;
581{
582 int err = FALSE;
583
584 set_vim_var_string(VV_CC_FROM, enc_from, -1);
585 set_vim_var_string(VV_CC_TO, enc_to, -1);
586 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
587 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
588 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
589 err = TRUE;
590 set_vim_var_string(VV_CC_FROM, NULL, -1);
591 set_vim_var_string(VV_CC_TO, NULL, -1);
592 set_vim_var_string(VV_FNAME_IN, NULL, -1);
593 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
594
595 if (err)
596 return FAIL;
597 return OK;
598}
599# endif
600
601# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
602 int
603eval_printexpr(fname, args)
604 char_u *fname;
605 char_u *args;
606{
607 int err = FALSE;
608
609 set_vim_var_string(VV_FNAME_IN, fname, -1);
610 set_vim_var_string(VV_CMDARG, args, -1);
611 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
612 err = TRUE;
613 set_vim_var_string(VV_FNAME_IN, NULL, -1);
614 set_vim_var_string(VV_CMDARG, NULL, -1);
615
616 if (err)
617 {
618 mch_remove(fname);
619 return FAIL;
620 }
621 return OK;
622}
623# endif
624
625# if defined(FEAT_DIFF) || defined(PROTO)
626 void
627eval_diff(origfile, newfile, outfile)
628 char_u *origfile;
629 char_u *newfile;
630 char_u *outfile;
631{
632 int err = FALSE;
633
634 set_vim_var_string(VV_FNAME_IN, origfile, -1);
635 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
636 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
637 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
638 set_vim_var_string(VV_FNAME_IN, NULL, -1);
639 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
640 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
641}
642
643 void
644eval_patch(origfile, difffile, outfile)
645 char_u *origfile;
646 char_u *difffile;
647 char_u *outfile;
648{
649 int err;
650
651 set_vim_var_string(VV_FNAME_IN, origfile, -1);
652 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
653 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
654 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
655 set_vim_var_string(VV_FNAME_IN, NULL, -1);
656 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
657 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
658}
659# endif
660
661/*
662 * Top level evaluation function, returning a boolean.
663 * Sets "error" to TRUE if there was an error.
664 * Return TRUE or FALSE.
665 */
666 int
667eval_to_bool(arg, error, nextcmd, skip)
668 char_u *arg;
669 int *error;
670 char_u **nextcmd;
671 int skip; /* only parse, don't execute */
672{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000673 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 int retval = FALSE;
675
676 if (skip)
677 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000678 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 else
681 {
682 *error = FALSE;
683 if (!skip)
684 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000685 retval = (get_tv_number(&tv) != 0);
686 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 }
688 }
689 if (skip)
690 --emsg_skip;
691
692 return retval;
693}
694
695/*
696 * Top level evaluation function, returning a string. If "skip" is TRUE,
697 * only parsing to "nextcmd" is done, without reporting errors. Return
698 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
699 */
700 char_u *
701eval_to_string_skip(arg, nextcmd, skip)
702 char_u *arg;
703 char_u **nextcmd;
704 int skip; /* only parse, don't execute */
705{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000706 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 char_u *retval;
708
709 if (skip)
710 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000711 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 retval = NULL;
713 else
714 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000715 retval = vim_strsave(get_tv_string(&tv));
716 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 }
718 if (skip)
719 --emsg_skip;
720
721 return retval;
722}
723
724/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000725 * Skip over an expression at "*pp".
726 * Return FAIL for an error, OK otherwise.
727 */
728 int
729skip_expr(pp)
730 char_u **pp;
731{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000732 typeval rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000733
734 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000735 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000736}
737
738/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 * Top level evaluation function, returning a string.
740 * Return pointer to allocated memory, or NULL for failure.
741 */
742 char_u *
743eval_to_string(arg, nextcmd)
744 char_u *arg;
745 char_u **nextcmd;
746{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000747 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 char_u *retval;
749
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000750 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 retval = NULL;
752 else
753 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000754 retval = vim_strsave(get_tv_string(&tv));
755 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 }
757
758 return retval;
759}
760
761/*
762 * Call eval_to_string() with "sandbox" set and not using local variables.
763 */
764 char_u *
765eval_to_string_safe(arg, nextcmd)
766 char_u *arg;
767 char_u **nextcmd;
768{
769 char_u *retval;
770 void *save_funccalp;
771
772 save_funccalp = save_funccal();
773 ++sandbox;
774 retval = eval_to_string(arg, nextcmd);
775 --sandbox;
776 restore_funccal(save_funccalp);
777 return retval;
778}
779
780#if 0 /* not used */
781/*
782 * Top level evaluation function, returning a string.
783 * Advances "arg" to the first non-blank after the evaluated expression.
784 * Return pointer to allocated memory, or NULL for failure.
785 * Doesn't give error messages.
786 */
787 char_u *
788eval_arg_to_string(arg)
789 char_u **arg;
790{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000791 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 char_u *retval;
793 int ret;
794
795 ++emsg_off;
796
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000797 ret = eval1(arg, &rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 if (ret == FAIL)
799 retval = NULL;
800 else
801 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000802 retval = vim_strsave(get_tv_string(&rettv));
803 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 }
805
806 --emsg_off;
807
808 return retval;
809}
810#endif
811
812/*
813 * Top level evaluation function, returning a number.
814 * Evaluates "expr" silently.
815 * Returns -1 for an error.
816 */
817 int
818eval_to_number(expr)
819 char_u *expr;
820{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000821 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 int retval;
823 char_u *p = expr;
824
825 ++emsg_off;
826
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000827 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 retval = -1;
829 else
830 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000831 retval = get_tv_number(&rettv);
832 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 }
834 --emsg_off;
835
836 return retval;
837}
838
839#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
840/*
841 * Call some vimL function and return the result as a string
842 * Uses argv[argc] for the function arguments.
843 */
844 char_u *
845call_vim_function(func, argc, argv, safe)
846 char_u *func;
847 int argc;
848 char_u **argv;
849 int safe; /* use the sandbox */
850{
851 char_u *retval = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000852 typeval rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000853 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 long n;
855 int len;
856 int i;
857 int doesrange;
858 void *save_funccalp = NULL;
859
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000860 argvars = (typeval *)alloc((unsigned)(argc * sizeof(typeval)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 if (argvars == NULL)
862 return NULL;
863
864 for (i = 0; i < argc; i++)
865 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000866 /* Pass a NULL or empty argument as an empty string */
867 if (argv[i] == NULL || *argv[i] == NUL)
868 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000869 argvars[i].v_type = VAR_STRING;
870 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000871 continue;
872 }
873
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 /* Recognize a number argument, the others must be strings. */
875 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
876 if (len != 0 && len == (int)STRLEN(argv[i]))
877 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000878 argvars[i].v_type = VAR_NUMBER;
879 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 }
881 else
882 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000883 argvars[i].v_type = VAR_STRING;
884 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 }
886 }
887
888 if (safe)
889 {
890 save_funccalp = save_funccal();
891 ++sandbox;
892 }
893
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000894 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
895 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
897 &doesrange, TRUE) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000898 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000900 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 vim_free(argvars);
902
903 if (safe)
904 {
905 --sandbox;
906 restore_funccal(save_funccalp);
907 }
908 return retval;
909}
910#endif
911
912/*
913 * Save the current function call pointer, and set it to NULL.
914 * Used when executing autocommands and for ":source".
915 */
916 void *
917save_funccal()
918{
919 struct funccall *fc;
920
921 fc = current_funccal;
922 current_funccal = NULL;
923 return (void *)fc;
924}
925
926 void
927restore_funccal(fc)
928 void *fc;
929{
930 current_funccal = (struct funccall *)fc;
931}
932
933#ifdef FEAT_FOLDING
934/*
935 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
936 * it in "*cp". Doesn't give error messages.
937 */
938 int
939eval_foldexpr(arg, cp)
940 char_u *arg;
941 int *cp;
942{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000943 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 int retval;
945 char_u *s;
946
947 ++emsg_off;
948 ++sandbox;
949 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000950 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 retval = 0;
952 else
953 {
954 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000955 if (tv.v_type == VAR_NUMBER)
956 retval = tv.vval.v_number;
957 else if (tv.v_type == VAR_UNKNOWN
958 || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 retval = 0;
960 else
961 {
962 /* If the result is a string, check if there is a non-digit before
963 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000964 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 if (!VIM_ISDIGIT(*s) && *s != '-')
966 *cp = *s++;
967 retval = atol((char *)s);
968 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000969 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 }
971 --emsg_off;
972 --sandbox;
973
974 return retval;
975}
976#endif
977
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978/*
979 * Expands out the 'magic' {}'s in a variable/function name.
980 * Note that this can call itself recursively, to deal with
981 * constructs like foo{bar}{baz}{bam}
982 * The four pointer arguments point to "foo{expre}ss{ion}bar"
983 * "in_start" ^
984 * "expr_start" ^
985 * "expr_end" ^
986 * "in_end" ^
987 *
988 * Returns a new allocated string, which the caller must free.
989 * Returns NULL for failure.
990 */
991 static char_u *
992make_expanded_name(in_start, expr_start, expr_end, in_end)
993 char_u *in_start;
994 char_u *expr_start;
995 char_u *expr_end;
996 char_u *in_end;
997{
998 char_u c1;
999 char_u *retval = NULL;
1000 char_u *temp_result;
1001 char_u *nextcmd = NULL;
1002
1003 if (expr_end == NULL || in_end == NULL)
1004 return NULL;
1005 *expr_start = NUL;
1006 *expr_end = NUL;
1007 c1 = *in_end;
1008 *in_end = NUL;
1009
1010 temp_result = eval_to_string(expr_start + 1, &nextcmd);
1011 if (temp_result != NULL && nextcmd == NULL)
1012 {
1013 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
1014 + (in_end - expr_end) + 1));
1015
1016 if (retval != NULL)
1017 {
1018 STRCPY(retval, in_start);
1019 STRCAT(retval, temp_result);
1020 STRCAT(retval, expr_end + 1);
1021 }
1022 }
1023 vim_free(temp_result);
1024
1025 *in_end = c1; /* put char back for error messages */
1026 *expr_start = '{';
1027 *expr_end = '}';
1028
1029 if (retval != NULL)
1030 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001031 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 if (expr_start != NULL)
1033 {
1034 /* Further expansion! */
1035 temp_result = make_expanded_name(retval, expr_start,
1036 expr_end, temp_result);
1037 vim_free(retval);
1038 retval = temp_result;
1039 }
1040 }
1041
1042 return retval;
1043
1044}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045
1046/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001047 * ":let" list all variable values
1048 * ":let var1 var2" list variable values
1049 * ":let var = expr" assignment command.
1050 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 */
1052 void
1053ex_let(eap)
1054 exarg_T *eap;
1055{
1056 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001057 char_u *expr = NULL;
1058 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001060 int var_count = 0;
1061 int semicolon = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001063 expr = skip_var_list(arg, &var_count, &semicolon);
1064 if (expr == NULL)
1065 return;
1066 expr = vim_strchr(expr, '=');
1067 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001069 if (*arg == '[')
1070 EMSG(_(e_invarg));
1071 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001072 /* ":let var1 var2" */
1073 arg = list_arg_vars(eap, arg);
1074 else if (!eap->skip)
1075 /* ":let" */
1076 list_all_vars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 eap->nextcmd = check_nextcmd(arg);
1078 }
1079 else
1080 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001081 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001082
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 if (eap->skip)
1084 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001085 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 if (eap->skip)
1087 {
1088 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001089 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 --emsg_skip;
1091 }
1092 else if (i != FAIL)
1093 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001094 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1095 (char_u *)"=");
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001096 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 }
1098 }
1099}
1100
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001101/*
1102 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1103 * Handles both "var" with any type and "[var, var; var]" with a list type.
1104 * Returns OK or FAIL;
1105 */
1106 static int
1107ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1108 char_u *arg_start;
1109 typeval *tv;
1110 int copy; /* copy values from "tv", don't move */
1111 int semicolon; /* from skip_var_list() */
1112 int var_count; /* from skip_var_list() */
1113 char_u *nextchars; /* characters that must follow or NULL */
1114{
1115 char_u *arg = arg_start;
1116 listvar *l;
1117 int i;
1118 listitem *item;
1119 typeval ltv;
1120
1121 if (*arg != '[')
1122 {
1123 /*
1124 * ":let var = expr" or ":for var in list"
1125 */
1126 if (ex_let_one(arg, tv, copy, nextchars) == NULL)
1127 return FAIL;
1128 return OK;
1129 }
1130
1131 /*
1132 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1133 */
1134 l = tv->vval.v_list;
1135 if (tv->v_type != VAR_LIST || l == NULL)
1136 {
1137 EMSG(_(e_listreq));
1138 return FAIL;
1139 }
1140
1141 i = list_len(l);
1142 if (semicolon == 0 && var_count < i)
1143 {
1144 EMSG(_("E999: Less targets than List items"));
1145 return FAIL;
1146 }
1147 if (var_count - semicolon > i)
1148 {
1149 EMSG(_("E999: More targets than List items"));
1150 return FAIL;
1151 }
1152
1153 item = l->lv_first;
1154 while (*arg != ']')
1155 {
1156 arg = skipwhite(arg + 1);
1157 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]");
1158 item = item->li_next;
1159 if (arg == NULL)
1160 return FAIL;
1161
1162 arg = skipwhite(arg);
1163 if (*arg == ';')
1164 {
1165 /* Put the rest of the list (may be empty) in the var after ';'.
1166 * Create a new list for this. */
1167 l = list_alloc();
1168 if (l == NULL)
1169 return FAIL;
1170 while (item != NULL)
1171 {
1172 list_append_tv(l, &item->li_tv);
1173 item = item->li_next;
1174 }
1175
1176 ltv.v_type = VAR_LIST;
1177 ltv.vval.v_list = l;
1178 l->lv_refcount = 1;
1179
1180 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, (char_u *)"]");
1181 clear_tv(&ltv);
1182 if (arg == NULL)
1183 return FAIL;
1184 break;
1185 }
1186 else if (*arg != ',' && *arg != ']')
1187 {
1188 EMSG2(_(e_intern2), "ex_let_vars()");
1189 return FAIL;
1190 }
1191 }
1192
1193 return OK;
1194}
1195
1196/*
1197 * Skip over assignable variable "var" or list of variables "[var, var]".
1198 * Used for ":let varvar = expr" and ":for varvar in expr".
1199 * For "[var, var]" increment "*var_count" for each variable.
1200 * for "[var, var; var]" set "semicolon".
1201 * Return NULL for an error.
1202 */
1203 static char_u *
1204skip_var_list(arg, var_count, semicolon)
1205 char_u *arg;
1206 int *var_count;
1207 int *semicolon;
1208{
1209 char_u *p, *s;
1210
1211 if (*arg == '[')
1212 {
1213 /* "[var, var]": find the matching ']'. */
1214 p = arg;
1215 while (1)
1216 {
1217 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1218 s = skip_var_one(p);
1219 if (s == p)
1220 {
1221 EMSG2(_(e_invarg2), p);
1222 return NULL;
1223 }
1224 ++*var_count;
1225
1226 p = skipwhite(s);
1227 if (*p == ']')
1228 break;
1229 else if (*p == ';')
1230 {
1231 if (*semicolon == 1)
1232 {
1233 EMSG(_("Double ; in list of variables"));
1234 return NULL;
1235 }
1236 *semicolon = 1;
1237 }
1238 else if (*p != ',')
1239 {
1240 EMSG2(_(e_invarg2), p);
1241 return NULL;
1242 }
1243 }
1244 return p + 1;
1245 }
1246 else
1247 return skip_var_one(arg);
1248}
1249
1250 static char_u *
1251skip_var_one(arg)
1252 char_u *arg;
1253{
1254 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1255 ++arg;
1256 return find_name_end(arg, NULL, NULL, TRUE);
1257}
1258
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001259 static void
1260list_all_vars()
1261{
1262 int i;
1263
1264 /*
1265 * List all variables.
1266 */
1267 for (i = 0; i < variables.ga_len && !got_int; ++i)
1268 if (VAR_ENTRY(i).v_name != NULL)
1269 list_one_var(&VAR_ENTRY(i), (char_u *)"");
1270 for (i = 0; i < curbuf->b_vars.ga_len && !got_int; ++i)
1271 if (BVAR_ENTRY(i).v_name != NULL)
1272 list_one_var(&BVAR_ENTRY(i), (char_u *)"b:");
1273 for (i = 0; i < curwin->w_vars.ga_len && !got_int; ++i)
1274 if (WVAR_ENTRY(i).v_name != NULL)
1275 list_one_var(&WVAR_ENTRY(i), (char_u *)"w:");
1276 for (i = 0; i < VV_LEN && !got_int; ++i)
1277 if (vimvars[i].type == VAR_NUMBER || vimvars[i].val != NULL)
1278 list_vim_var(i);
1279}
1280
1281/*
1282 * List variables in "arg".
1283 */
1284 static char_u *
1285list_arg_vars(eap, arg)
1286 exarg_T *eap;
1287 char_u *arg;
1288{
1289 int error = FALSE;
1290 char_u *temp_string = NULL;
1291 int arg_len;
1292 char_u *expr_start;
1293 char_u *expr_end;
1294 char_u *name_end;
1295 int c1 = 0, c2;
1296 int i;
1297 VAR varp;
1298 char_u *name;
1299
1300 while (!ends_excmd(*arg) && !got_int)
1301 {
1302 /* Find the end of the name. */
1303 name_end = find_name_end(arg, &expr_start, &expr_end, FALSE);
1304
1305 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
1306 {
1307 emsg_severe = TRUE;
1308 EMSG(_(e_trailing));
1309 break;
1310 }
1311 if (!error && !eap->skip)
1312 {
1313 if (expr_start != NULL)
1314 {
1315 temp_string = make_expanded_name(arg, expr_start,
1316 expr_end, name_end);
1317 if (temp_string == NULL)
1318 {
1319 /*
1320 * Report an invalid expression in braces, unless
1321 * the expression evaluation has been cancelled due
1322 * to an aborting error, an interrupt, or an
1323 * exception.
1324 */
1325 if (!aborting())
1326 {
1327 emsg_severe = TRUE;
1328 EMSG2(_(e_invarg2), arg);
1329 break;
1330 }
1331 error = TRUE;
1332 arg = skipwhite(name_end);
1333 continue;
1334 }
1335 arg = temp_string;
1336 arg_len = STRLEN(temp_string);
1337 }
1338 else
1339 {
1340 c1 = *name_end;
1341 *name_end = NUL;
1342 arg_len = (int)(name_end - arg);
1343 }
1344 i = find_vim_var(arg, arg_len);
1345 if (i >= 0)
1346 list_vim_var(i);
1347 else if (STRCMP("b:changedtick", arg) == 0)
1348 {
1349 char_u numbuf[NUMBUFLEN];
1350
1351 sprintf((char *)numbuf, "%ld",
1352 (long)curbuf->b_changedtick);
1353 list_one_var_a((char_u *)"b:", (char_u *)"changedtick",
1354 VAR_NUMBER, numbuf);
1355 }
1356 else
1357 {
1358 varp = find_var(arg, FALSE);
1359 if (varp == NULL)
1360 {
1361 /* Skip further arguments but do continue to
1362 * search for a trailing command. */
1363 EMSG2(_("E106: Unknown variable: \"%s\""), arg);
1364 error = TRUE;
1365 }
1366 else
1367 {
1368 name = vim_strchr(arg, ':');
1369 if (name != NULL)
1370 {
1371 /* "a:" vars have no name stored, use whole arg */
1372 if (arg[0] == 'a' && arg[1] == ':')
1373 c2 = NUL;
1374 else
1375 {
1376 c2 = *++name;
1377 *name = NUL;
1378 }
1379 list_one_var(varp, arg);
1380 if (c2 != NUL)
1381 *name = c2;
1382 }
1383 else
1384 list_one_var(varp, (char_u *)"");
1385 }
1386 }
1387 if (expr_start != NULL)
1388 vim_free(temp_string);
1389 else
1390 *name_end = c1;
1391 }
1392 arg = skipwhite(name_end);
1393 }
1394
1395 return arg;
1396}
1397
1398/*
1399 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1400 * Returns a pointer to the char just after the var name.
1401 * Returns NULL if there is an error.
1402 */
1403 static char_u *
1404ex_let_one(arg, tv, copy, endchars)
1405 char_u *arg; /* points to variable name */
1406 typeval *tv; /* value to assign to variable */
1407 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001408 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001409{
1410 int c1;
1411 char_u *name;
1412 char_u *p;
1413 char_u *arg_end = NULL;
1414 int len;
1415 int opt_flags;
1416
1417 /*
1418 * ":let $VAR = expr": Set environment variable.
1419 */
1420 if (*arg == '$')
1421 {
1422 /* Find the end of the name. */
1423 ++arg;
1424 name = arg;
1425 len = get_env_len(&arg);
1426 if (len == 0)
1427 EMSG2(_(e_invarg2), name - 1);
1428 else
1429 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001430 if (endchars != NULL
1431 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001432 EMSG(_(e_letunexp));
1433 else
1434 {
1435 c1 = name[len];
1436 name[len] = NUL;
1437 p = get_tv_string(tv);
1438 vim_setenv(name, p);
1439 if (STRICMP(name, "HOME") == 0)
1440 init_homedir();
1441 else if (didset_vim && STRICMP(name, "VIM") == 0)
1442 didset_vim = FALSE;
1443 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1444 didset_vimruntime = FALSE;
1445 name[len] = c1;
1446 arg_end = arg;
1447 }
1448 }
1449 }
1450
1451 /*
1452 * ":let &option = expr": Set option value.
1453 * ":let &l:option = expr": Set local option value.
1454 * ":let &g:option = expr": Set global option value.
1455 */
1456 else if (*arg == '&')
1457 {
1458 /* Find the end of the name. */
1459 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001460 if (p == NULL || (endchars != NULL
1461 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001462 EMSG(_(e_letunexp));
1463 else
1464 {
1465 c1 = *p;
1466 *p = NUL;
1467 set_option_value(arg, get_tv_number(tv),
1468 get_tv_string(tv), opt_flags);
1469 *p = c1;
1470 arg_end = p;
1471 }
1472 }
1473
1474 /*
1475 * ":let @r = expr": Set register contents.
1476 */
1477 else if (*arg == '@')
1478 {
1479 ++arg;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001480 if (endchars != NULL
1481 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001482 EMSG(_(e_letunexp));
1483 else
1484 {
1485 write_reg_contents(*arg == '@' ? '"' : *arg,
1486 get_tv_string(tv), -1, FALSE);
1487 arg_end = arg + 1;
1488 }
1489 }
1490
1491 /*
1492 * ":let var = expr": Set internal variable.
1493 */
1494 else if (eval_isnamec(*arg) && !VIM_ISDIGIT(*arg))
1495 {
1496 char_u *exp_name = NULL;
1497 char_u *expr_start, *expr_end;
1498
1499 /* Find the end of the name. */
1500 p = find_name_end(arg, &expr_start, &expr_end, FALSE);
1501 if (expr_start != NULL)
1502 {
1503 exp_name = make_expanded_name(arg, expr_start, expr_end, p);
1504 arg = exp_name;
1505 }
1506
1507 if (arg == NULL)
1508 {
1509 /* Report an invalid expression in braces, unless the
1510 * expression evaluation has been cancelled due to an
1511 * aborting error, an interrupt, or an exception. */
1512 if (!aborting())
1513 EMSG2(_(e_invarg2), arg);
1514 }
1515 else if (*p == '[')
1516 arg_end = set_var_idx(arg, p, tv, copy, endchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001517 else if (endchars != NULL
1518 && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001519 EMSG(_(e_letunexp));
1520 else if (STRNCMP(arg, "b:changedtick", 13) == 0
1521 && !eval_isnamec(arg[13]))
1522 EMSG2(_(e_readonlyvar), arg);
1523 else
1524 {
1525 c1 = *p;
1526 *p = NUL;
1527 set_var(arg, tv, copy);
1528 *p = c1;
1529 arg_end = p;
1530 }
1531
1532 vim_free(exp_name);
1533 }
1534
1535 else
1536 EMSG2(_(e_invarg2), arg);
1537
1538 return arg_end;
1539}
1540
1541/*
1542 * Set a variable with an index: "name[expr]", "name[expr][expr]", etc.
1543 * Only works if "name" is an existing List.
1544 * "ip" points to the first '['.
1545 * Returns a pointer to just after the last used ']'; NULL for error.
1546 */
1547 static char_u *
1548set_var_idx(name, ip, rettv, copy, endchars)
1549 char_u *name;
1550 char_u *ip;
1551 typeval *rettv;
1552 int copy;
1553 char_u *endchars;
1554{
1555 VAR v;
1556 int c1;
1557 char_u *p;
1558 typeval var1;
1559 typeval *tv;
1560 long n;
1561 listitem *item;
1562
1563 c1 = *ip;
1564 *ip = NUL;
1565 v = find_var(name, TRUE);
1566 if (v == NULL)
1567 EMSG2(_(e_undefvar), name);
1568 *ip = c1;
1569 if (v == NULL)
1570 return NULL;
1571
1572 tv = &v->tv;
1573 for (p = ip; *p == '['; p = skipwhite(p + 1))
1574 {
1575 if (tv->v_type != VAR_LIST || tv->vval.v_list == NULL)
1576 {
1577 EMSG(_("E999: Can only index a List"));
1578 p = NULL;
1579 break;
1580 }
1581 p = skipwhite(p + 1);
1582 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
1583 {
1584 p = NULL;
1585 break;
1586 }
1587 if (*p != ']')
1588 {
1589 EMSG(_(e_missbrac));
1590 clear_tv(&var1);
1591 p = NULL;
1592 break;
1593 }
1594 n = get_tv_number(&var1);
1595 clear_tv(&var1);
1596 item = list_find(tv->vval.v_list, n);
1597 if (item == NULL)
1598 {
1599 EMSGN(_(e_listidx), n);
1600 p = NULL;
1601 break;
1602 }
1603 tv = &item->li_tv;
1604 }
1605
1606 if (p != NULL)
1607 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001608 if (endchars != NULL && vim_strchr(endchars, *p) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001609 {
1610 EMSG(_(e_letunexp));
1611 p = NULL;
1612 }
1613 else
1614 {
1615 clear_tv(tv);
1616 if (copy)
1617 copy_tv(tv, rettv);
1618 else
1619 {
1620 *tv = *rettv;
1621 init_tv(rettv);
1622 }
1623 }
1624 }
1625 return p;
1626}
1627
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001628/*
1629 * Add a watcher to a list.
1630 */
1631 static void
1632list_add_watch(l, lw)
1633 listvar *l;
1634 listwatch *lw;
1635{
1636 lw->lw_next = l->lv_watch;
1637 l->lv_watch = lw;
1638}
1639
1640/*
1641 * Remove a watches from a list.
1642 * No warning when it isn't found...
1643 */
1644 static void
1645list_rem_watch(l, lwrem)
1646 listvar *l;
1647 listwatch *lwrem;
1648{
1649 listwatch *lw, **lwp;
1650
1651 lwp = &l->lv_watch;
1652 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
1653 {
1654 if (lw == lwrem)
1655 {
1656 *lwp = lw->lw_next;
1657 break;
1658 }
1659 lwp = &lw->lw_next;
1660 }
1661}
1662
1663/*
1664 * Just before removing an item from a list: advance watchers to the next
1665 * item.
1666 */
1667 static void
1668list_fix_watch(l, item)
1669 listvar *l;
1670 listitem *item;
1671{
1672 listwatch *lw;
1673
1674 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
1675 if (lw->lw_item == item)
1676 lw->lw_item = item->li_next;
1677}
1678
1679/*
1680 * Evaluate the expression used in a ":for var in expr" command.
1681 * "arg" points to "var".
1682 * Set "*errp" to TRUE for an error, FALSE otherwise;
1683 * Return a pointer that holds the info. Null when there is an error.
1684 */
1685 void *
1686eval_for_line(arg, errp, nextcmdp, skip)
1687 char_u *arg;
1688 int *errp;
1689 char_u **nextcmdp;
1690 int skip;
1691{
1692 forinfo *fi;
1693 char_u *expr;
1694 typeval tv;
1695 listvar *l;
1696
1697 *errp = TRUE; /* default: there is an error */
1698
1699 fi = (forinfo *)alloc_clear(sizeof(forinfo));
1700 if (fi == NULL)
1701 return NULL;
1702
1703 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
1704 if (expr == NULL)
1705 return fi;
1706
1707 expr = skipwhite(expr);
1708 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
1709 {
1710 EMSG(_("E999: Missing \"in\" after :for"));
1711 return fi;
1712 }
1713
1714 if (skip)
1715 ++emsg_skip;
1716 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
1717 {
1718 *errp = FALSE;
1719 if (!skip)
1720 {
1721 l = tv.vval.v_list;
1722 if (tv.v_type != VAR_LIST || l == NULL)
1723 EMSG(_(e_listreq));
1724 else
1725 {
1726 fi->fi_list = l;
1727 list_add_watch(l, &fi->fi_lw);
1728 fi->fi_lw.lw_item = l->lv_first;
1729 }
1730 }
1731 }
1732 if (skip)
1733 --emsg_skip;
1734
1735 return fi;
1736}
1737
1738/*
1739 * Use the first item in a ":for" list. Advance to the next.
1740 * Assign the values to the variable (list). "arg" points to the first one.
1741 * Return TRUE when a valid item was found, FALSE when at end of list or
1742 * something wrong.
1743 */
1744 int
1745next_for_item(fi_void, arg)
1746 void *fi_void;
1747 char_u *arg;
1748{
1749 forinfo *fi = (forinfo *)fi_void;
1750 int result;
1751 listitem *item;
1752
1753 item = fi->fi_lw.lw_item;
1754 if (item == NULL)
1755 result = FALSE;
1756 else
1757 {
1758 fi->fi_lw.lw_item = item->li_next;
1759 result = (ex_let_vars(arg, &item->li_tv, TRUE,
1760 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
1761 }
1762 return result;
1763}
1764
1765/*
1766 * Free the structure used to store info used by ":for".
1767 */
1768 void
1769free_for_info(fi_void)
1770 void *fi_void;
1771{
1772 forinfo *fi = (forinfo *)fi_void;
1773
1774 if (fi->fi_list != NULL)
1775 list_rem_watch(fi->fi_list, &fi->fi_lw);
1776 vim_free(fi);
1777}
1778
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1780
1781 void
1782set_context_for_expression(xp, arg, cmdidx)
1783 expand_T *xp;
1784 char_u *arg;
1785 cmdidx_T cmdidx;
1786{
1787 int got_eq = FALSE;
1788 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001789 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001791 if (cmdidx == CMD_let)
1792 {
1793 xp->xp_context = EXPAND_USER_VARS;
1794 if (vim_strchr(arg, '=') == NULL)
1795 {
1796 /* ":let var1 var2 ...": find last space. */
1797 for (p = arg + STRLEN(arg); p > arg; )
1798 {
1799 xp->xp_pattern = p;
1800 p = mb_ptr_back(arg, p);
1801 if (vim_iswhite(*p))
1802 break;
1803 }
1804 return;
1805 }
1806 }
1807 else
1808 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1809 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 while ((xp->xp_pattern = vim_strpbrk(arg,
1811 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1812 {
1813 c = *xp->xp_pattern;
1814 if (c == '&')
1815 {
1816 c = xp->xp_pattern[1];
1817 if (c == '&')
1818 {
1819 ++xp->xp_pattern;
1820 xp->xp_context = cmdidx != CMD_let || got_eq
1821 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1822 }
1823 else if (c != ' ')
1824 xp->xp_context = EXPAND_SETTINGS;
1825 }
1826 else if (c == '$')
1827 {
1828 /* environment variable */
1829 xp->xp_context = EXPAND_ENV_VARS;
1830 }
1831 else if (c == '=')
1832 {
1833 got_eq = TRUE;
1834 xp->xp_context = EXPAND_EXPRESSION;
1835 }
1836 else if (c == '<'
1837 && xp->xp_context == EXPAND_FUNCTIONS
1838 && vim_strchr(xp->xp_pattern, '(') == NULL)
1839 {
1840 /* Function name can start with "<SNR>" */
1841 break;
1842 }
1843 else if (cmdidx != CMD_let || got_eq)
1844 {
1845 if (c == '"') /* string */
1846 {
1847 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1848 if (c == '\\' && xp->xp_pattern[1] != NUL)
1849 ++xp->xp_pattern;
1850 xp->xp_context = EXPAND_NOTHING;
1851 }
1852 else if (c == '\'') /* literal string */
1853 {
1854 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1855 /* skip */ ;
1856 xp->xp_context = EXPAND_NOTHING;
1857 }
1858 else if (c == '|')
1859 {
1860 if (xp->xp_pattern[1] == '|')
1861 {
1862 ++xp->xp_pattern;
1863 xp->xp_context = EXPAND_EXPRESSION;
1864 }
1865 else
1866 xp->xp_context = EXPAND_COMMANDS;
1867 }
1868 else
1869 xp->xp_context = EXPAND_EXPRESSION;
1870 }
1871 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001872 /* Doesn't look like something valid, expand as an expression
1873 * anyway. */
1874 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 arg = xp->xp_pattern;
1876 if (*arg != NUL)
1877 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1878 /* skip */ ;
1879 }
1880 xp->xp_pattern = arg;
1881}
1882
1883#endif /* FEAT_CMDL_COMPL */
1884
1885/*
1886 * ":1,25call func(arg1, arg2)" function call.
1887 */
1888 void
1889ex_call(eap)
1890 exarg_T *eap;
1891{
1892 char_u *arg = eap->arg;
1893 char_u *startarg;
1894 char_u *alias;
1895 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 int len;
1898 linenr_T lnum;
1899 int doesrange;
1900 int failed = FALSE;
1901
1902 name = arg;
1903 len = get_func_len(&arg, &alias, !eap->skip);
1904 if (len == 0)
1905 goto end;
1906 if (alias != NULL)
1907 name = alias;
1908
1909 startarg = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001910 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911
1912 if (*startarg != '(')
1913 {
1914 EMSG2(_("E107: Missing braces: %s"), name);
1915 goto end;
1916 }
1917
1918 /*
1919 * When skipping, evaluate the function once, to find the end of the
1920 * arguments.
1921 * When the function takes a range, this is discovered after the first
1922 * call, and the loop is broken.
1923 */
1924 if (eap->skip)
1925 {
1926 ++emsg_skip;
1927 lnum = eap->line2; /* do it once, also with an invalid range */
1928 }
1929 else
1930 lnum = eap->line1;
1931 for ( ; lnum <= eap->line2; ++lnum)
1932 {
1933 if (!eap->skip && eap->addr_count > 0)
1934 {
1935 curwin->w_cursor.lnum = lnum;
1936 curwin->w_cursor.col = 0;
1937 }
1938 arg = startarg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001939 if (get_func_tv(name, len, &rettv, &arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 eap->line1, eap->line2, &doesrange, !eap->skip) == FAIL)
1941 {
1942 failed = TRUE;
1943 break;
1944 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001945 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 if (doesrange || eap->skip)
1947 break;
1948 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001949 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001950 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001951 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if (aborting())
1953 break;
1954 }
1955 if (eap->skip)
1956 --emsg_skip;
1957
1958 if (!failed)
1959 {
1960 /* Check for trailing illegal characters and a following command. */
1961 if (!ends_excmd(*arg))
1962 {
1963 emsg_severe = TRUE;
1964 EMSG(_(e_trailing));
1965 }
1966 else
1967 eap->nextcmd = check_nextcmd(arg);
1968 }
1969
1970end:
1971 if (alias != NULL)
1972 vim_free(alias);
1973}
1974
1975/*
1976 * ":unlet[!] var1 ... " command.
1977 */
1978 void
1979ex_unlet(eap)
1980 exarg_T *eap;
1981{
1982 char_u *arg = eap->arg;
1983 char_u *name_end;
1984 char_u cc;
1985 char_u *expr_start;
1986 char_u *expr_end;
1987 int error = FALSE;
1988
1989 do
1990 {
1991 /* Find the end of the name. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001992 name_end = find_name_end(arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993
1994 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
1995 {
1996 emsg_severe = TRUE;
1997 EMSG(_(e_trailing));
1998 break;
1999 }
2000
2001 if (!error && !eap->skip)
2002 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 if (expr_start != NULL)
2004 {
2005 char_u *temp_string;
2006
2007 temp_string = make_expanded_name(arg, expr_start,
2008 expr_end, name_end);
2009 if (temp_string == NULL)
2010 {
2011 /*
2012 * Report an invalid expression in braces, unless the
2013 * expression evaluation has been cancelled due to an
2014 * aborting error, an interrupt, or an exception.
2015 */
2016 if (!aborting())
2017 {
2018 emsg_severe = TRUE;
2019 EMSG2(_(e_invarg2), arg);
2020 break;
2021 }
2022 error = TRUE;
2023 }
2024 else
2025 {
2026 if (do_unlet(temp_string) == FAIL && !eap->forceit)
2027 {
2028 EMSG2(_("E108: No such variable: \"%s\""), temp_string);
2029 error = TRUE;
2030 }
2031 vim_free(temp_string);
2032 }
2033 }
2034 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 {
2036 cc = *name_end;
2037 *name_end = NUL;
2038
2039 if (do_unlet(arg) == FAIL && !eap->forceit)
2040 {
2041 EMSG2(_("E108: No such variable: \"%s\""), arg);
2042 error = TRUE;
2043 }
2044
2045 *name_end = cc;
2046 }
2047 }
2048 arg = skipwhite(name_end);
2049 } while (!ends_excmd(*arg));
2050
2051 eap->nextcmd = check_nextcmd(arg);
2052}
2053
2054/*
2055 * "unlet" a variable. Return OK if it existed, FAIL if not.
2056 */
2057 int
2058do_unlet(name)
2059 char_u *name;
2060{
2061 VAR v;
2062
2063 v = find_var(name, TRUE);
2064 if (v != NULL)
2065 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002066 clear_var(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 return OK;
2068 }
2069 return FAIL;
2070}
2071
2072#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2073/*
2074 * Delete all "menutrans_" variables.
2075 */
2076 void
2077del_menutrans_vars()
2078{
2079 int i;
2080
2081 for (i = 0; i < variables.ga_len; ++i)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002082 if (VAR_ENTRY(i).v_name != NULL
2083 && STRNCMP(VAR_ENTRY(i).v_name, "menutrans_", 10) == 0)
2084 clear_var(&VAR_ENTRY(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085}
2086#endif
2087
2088#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2089
2090/*
2091 * Local string buffer for the next two functions to store a variable name
2092 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2093 * get_user_var_name().
2094 */
2095
2096static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
2097
2098static char_u *varnamebuf = NULL;
2099static int varnamebuflen = 0;
2100
2101/*
2102 * Function to concatenate a prefix and a variable name.
2103 */
2104 static char_u *
2105cat_prefix_varname(prefix, name)
2106 int prefix;
2107 char_u *name;
2108{
2109 int len;
2110
2111 len = (int)STRLEN(name) + 3;
2112 if (len > varnamebuflen)
2113 {
2114 vim_free(varnamebuf);
2115 len += 10; /* some additional space */
2116 varnamebuf = alloc(len);
2117 if (varnamebuf == NULL)
2118 {
2119 varnamebuflen = 0;
2120 return NULL;
2121 }
2122 varnamebuflen = len;
2123 }
2124 *varnamebuf = prefix;
2125 varnamebuf[1] = ':';
2126 STRCPY(varnamebuf + 2, name);
2127 return varnamebuf;
2128}
2129
2130/*
2131 * Function given to ExpandGeneric() to obtain the list of user defined
2132 * (global/buffer/window/built-in) variable names.
2133 */
2134/*ARGSUSED*/
2135 char_u *
2136get_user_var_name(xp, idx)
2137 expand_T *xp;
2138 int idx;
2139{
2140 static int gidx;
2141 static int bidx;
2142 static int widx;
2143 static int vidx;
2144 char_u *name;
2145
2146 if (idx == 0)
2147 gidx = bidx = widx = vidx = 0;
2148 if (gidx < variables.ga_len) /* Global variables */
2149 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002150 while ((name = VAR_ENTRY(gidx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 && gidx < variables.ga_len)
2152 /* skip */;
2153 if (name != NULL)
2154 {
2155 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2156 return cat_prefix_varname('g', name);
2157 else
2158 return name;
2159 }
2160 }
2161 if (bidx < curbuf->b_vars.ga_len) /* Current buffer variables */
2162 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002163 while ((name = BVAR_ENTRY(bidx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 && bidx < curbuf->b_vars.ga_len)
2165 /* skip */;
2166 if (name != NULL)
2167 return cat_prefix_varname('b', name);
2168 }
2169 if (bidx == curbuf->b_vars.ga_len)
2170 {
2171 ++bidx;
2172 return (char_u *)"b:changedtick";
2173 }
2174 if (widx < curwin->w_vars.ga_len) /* Current window variables */
2175 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002176 while ((name = WVAR_ENTRY(widx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 && widx < curwin->w_vars.ga_len)
2178 /* skip */;
2179 if (name != NULL)
2180 return cat_prefix_varname('w', name);
2181 }
2182 if (vidx < VV_LEN) /* Built-in variables */
2183 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].name);
2184
2185 vim_free(varnamebuf);
2186 varnamebuf = NULL;
2187 varnamebuflen = 0;
2188 return NULL;
2189}
2190
2191#endif /* FEAT_CMDL_COMPL */
2192
2193/*
2194 * types for expressions.
2195 */
2196typedef enum
2197{
2198 TYPE_UNKNOWN = 0
2199 , TYPE_EQUAL /* == */
2200 , TYPE_NEQUAL /* != */
2201 , TYPE_GREATER /* > */
2202 , TYPE_GEQUAL /* >= */
2203 , TYPE_SMALLER /* < */
2204 , TYPE_SEQUAL /* <= */
2205 , TYPE_MATCH /* =~ */
2206 , TYPE_NOMATCH /* !~ */
2207} exptype_T;
2208
2209/*
2210 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002211 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2213 */
2214
2215/*
2216 * Handle zero level expression.
2217 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 * Return OK or FAIL.
2220 */
2221 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 char_u *arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 char_u **nextcmd;
2226 int evaluate;
2227{
2228 int ret;
2229 char_u *p;
2230
2231 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 if (ret == FAIL || !ends_excmd(*p))
2234 {
2235 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002236 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 /*
2238 * Report the invalid expression unless the expression evaluation has
2239 * been cancelled due to an aborting error, an interrupt, or an
2240 * exception.
2241 */
2242 if (!aborting())
2243 EMSG2(_(e_invexpr2), arg);
2244 ret = FAIL;
2245 }
2246 if (nextcmd != NULL)
2247 *nextcmd = check_nextcmd(p);
2248
2249 return ret;
2250}
2251
2252/*
2253 * Handle top level expression:
2254 * expr1 ? expr0 : expr0
2255 *
2256 * "arg" must point to the first non-white of the expression.
2257 * "arg" is advanced to the next non-white after the recognized expression.
2258 *
2259 * Return OK or FAIL.
2260 */
2261 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002262eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 int evaluate;
2266{
2267 int result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002268 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269
2270 /*
2271 * Get the first variable.
2272 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 return FAIL;
2275
2276 if ((*arg)[0] == '?')
2277 {
2278 result = FALSE;
2279 if (evaluate)
2280 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002281 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 }
2285
2286 /*
2287 * Get the second variable.
2288 */
2289 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002290 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 return FAIL;
2292
2293 /*
2294 * Check for the ":".
2295 */
2296 if ((*arg)[0] != ':')
2297 {
2298 EMSG(_("E109: Missing ':' after '?'"));
2299 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002300 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 return FAIL;
2302 }
2303
2304 /*
2305 * Get the third variable.
2306 */
2307 *arg = skipwhite(*arg + 1);
2308 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
2309 {
2310 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 return FAIL;
2313 }
2314 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 }
2317
2318 return OK;
2319}
2320
2321/*
2322 * Handle first level expression:
2323 * expr2 || expr2 || expr2 logical OR
2324 *
2325 * "arg" must point to the first non-white of the expression.
2326 * "arg" is advanced to the next non-white after the recognized expression.
2327 *
2328 * Return OK or FAIL.
2329 */
2330 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002331eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002333 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 int evaluate;
2335{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002336 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 long result;
2338 int first;
2339
2340 /*
2341 * Get the first variable.
2342 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002343 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 return FAIL;
2345
2346 /*
2347 * Repeat until there is no following "||".
2348 */
2349 first = TRUE;
2350 result = FALSE;
2351 while ((*arg)[0] == '|' && (*arg)[1] == '|')
2352 {
2353 if (evaluate && first)
2354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002355 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002357 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 first = FALSE;
2359 }
2360
2361 /*
2362 * Get the second variable.
2363 */
2364 *arg = skipwhite(*arg + 2);
2365 if (eval3(arg, &var2, evaluate && !result) == FAIL)
2366 return FAIL;
2367
2368 /*
2369 * Compute the result.
2370 */
2371 if (evaluate && !result)
2372 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002375 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 }
2377 if (evaluate)
2378 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379 rettv->v_type = VAR_NUMBER;
2380 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 }
2382 }
2383
2384 return OK;
2385}
2386
2387/*
2388 * Handle second level expression:
2389 * expr3 && expr3 && expr3 logical AND
2390 *
2391 * "arg" must point to the first non-white of the expression.
2392 * "arg" is advanced to the next non-white after the recognized expression.
2393 *
2394 * Return OK or FAIL.
2395 */
2396 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002399 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 int evaluate;
2401{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002402 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 long result;
2404 int first;
2405
2406 /*
2407 * Get the first variable.
2408 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002409 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 return FAIL;
2411
2412 /*
2413 * Repeat until there is no following "&&".
2414 */
2415 first = TRUE;
2416 result = TRUE;
2417 while ((*arg)[0] == '&' && (*arg)[1] == '&')
2418 {
2419 if (evaluate && first)
2420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 first = FALSE;
2425 }
2426
2427 /*
2428 * Get the second variable.
2429 */
2430 *arg = skipwhite(*arg + 2);
2431 if (eval4(arg, &var2, evaluate && result) == FAIL)
2432 return FAIL;
2433
2434 /*
2435 * Compute the result.
2436 */
2437 if (evaluate && result)
2438 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002441 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 }
2443 if (evaluate)
2444 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002445 rettv->v_type = VAR_NUMBER;
2446 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 }
2448 }
2449
2450 return OK;
2451}
2452
2453/*
2454 * Handle third level expression:
2455 * var1 == var2
2456 * var1 =~ var2
2457 * var1 != var2
2458 * var1 !~ var2
2459 * var1 > var2
2460 * var1 >= var2
2461 * var1 < var2
2462 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002463 * var1 is var2
2464 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 *
2466 * "arg" must point to the first non-white of the expression.
2467 * "arg" is advanced to the next non-white after the recognized expression.
2468 *
2469 * Return OK or FAIL.
2470 */
2471 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002472eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 int evaluate;
2476{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002477 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 char_u *p;
2479 int i;
2480 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002481 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 int len = 2;
2483 long n1, n2;
2484 char_u *s1, *s2;
2485 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2486 regmatch_T regmatch;
2487 int ic;
2488 char_u *save_cpo;
2489
2490 /*
2491 * Get the first variable.
2492 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002493 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 return FAIL;
2495
2496 p = *arg;
2497 switch (p[0])
2498 {
2499 case '=': if (p[1] == '=')
2500 type = TYPE_EQUAL;
2501 else if (p[1] == '~')
2502 type = TYPE_MATCH;
2503 break;
2504 case '!': if (p[1] == '=')
2505 type = TYPE_NEQUAL;
2506 else if (p[1] == '~')
2507 type = TYPE_NOMATCH;
2508 break;
2509 case '>': if (p[1] != '=')
2510 {
2511 type = TYPE_GREATER;
2512 len = 1;
2513 }
2514 else
2515 type = TYPE_GEQUAL;
2516 break;
2517 case '<': if (p[1] != '=')
2518 {
2519 type = TYPE_SMALLER;
2520 len = 1;
2521 }
2522 else
2523 type = TYPE_SEQUAL;
2524 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002525 case 'i': if (p[1] == 's')
2526 {
2527 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2528 len = 5;
2529 if (!vim_isIDc(p[len]))
2530 {
2531 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
2532 type_is = TRUE;
2533 }
2534 }
2535 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 }
2537
2538 /*
2539 * If there is a comparitive operator, use it.
2540 */
2541 if (type != TYPE_UNKNOWN)
2542 {
2543 /* extra question mark appended: ignore case */
2544 if (p[len] == '?')
2545 {
2546 ic = TRUE;
2547 ++len;
2548 }
2549 /* extra '#' appended: match case */
2550 else if (p[len] == '#')
2551 {
2552 ic = FALSE;
2553 ++len;
2554 }
2555 /* nothing appened: use 'ignorecase' */
2556 else
2557 ic = p_ic;
2558
2559 /*
2560 * Get the second variable.
2561 */
2562 *arg = skipwhite(p + len);
2563 if (eval5(arg, &var2, evaluate) == FAIL)
2564 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002565 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 return FAIL;
2567 }
2568
2569 if (evaluate)
2570 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002571 if (type_is && rettv->v_type != var2.v_type)
2572 {
2573 /* For "is" a different type always means FALSE, for "notis"
2574 * it means TRUE. */
2575 n1 = (type == TYPE_NEQUAL);
2576 }
2577 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
2578 {
2579 if (type_is)
2580 {
2581 n1 = (rettv->v_type == var2.v_type
2582 && rettv->vval.v_list == var2.vval.v_list);
2583 if (type == TYPE_NEQUAL)
2584 n1 = !n1;
2585 }
2586 else if (rettv->v_type != var2.v_type
2587 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
2588 {
2589 if (rettv->v_type != var2.v_type)
2590 EMSG(_("E999: Can only compare List with List"));
2591 else
2592 EMSG(_("E999: Invalid operation for Lists"));
2593 clear_tv(rettv);
2594 clear_tv(&var2);
2595 return FAIL;
2596 }
2597 else
2598 {
2599 /* Compare two Lists for being equal or unequal. */
2600 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
2601 if (type == TYPE_NEQUAL)
2602 n1 = !n1;
2603 }
2604 }
2605
2606 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
2607 {
2608 if (rettv->v_type != var2.v_type
2609 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
2610 {
2611 if (rettv->v_type != var2.v_type)
2612 EMSG(_("E999: Can only compare Funcref with Funcref"));
2613 else
2614 EMSG(_("E999: Invalid operation for Funcrefs"));
2615 clear_tv(rettv);
2616 clear_tv(&var2);
2617 return FAIL;
2618 }
2619 else
2620 {
2621 /* Compare two Funcrefs for being equal or unequal. */
2622 if (rettv->vval.v_string == NULL
2623 || var2.vval.v_string == NULL)
2624 n1 = FALSE;
2625 else
2626 n1 = STRCMP(rettv->vval.v_string,
2627 var2.vval.v_string) == 0;
2628 if (type == TYPE_NEQUAL)
2629 n1 = !n1;
2630 }
2631 }
2632
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 /*
2634 * If one of the two variables is a number, compare as a number.
2635 * When using "=~" or "!~", always compare as string.
2636 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002637 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 && type != TYPE_MATCH && type != TYPE_NOMATCH)
2639 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002640 n1 = get_tv_number(rettv);
2641 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 switch (type)
2643 {
2644 case TYPE_EQUAL: n1 = (n1 == n2); break;
2645 case TYPE_NEQUAL: n1 = (n1 != n2); break;
2646 case TYPE_GREATER: n1 = (n1 > n2); break;
2647 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
2648 case TYPE_SMALLER: n1 = (n1 < n2); break;
2649 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
2650 case TYPE_UNKNOWN:
2651 case TYPE_MATCH:
2652 case TYPE_NOMATCH: break; /* avoid gcc warning */
2653 }
2654 }
2655 else
2656 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002657 s1 = get_tv_string_buf(rettv, buf1);
2658 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
2660 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
2661 else
2662 i = 0;
2663 n1 = FALSE;
2664 switch (type)
2665 {
2666 case TYPE_EQUAL: n1 = (i == 0); break;
2667 case TYPE_NEQUAL: n1 = (i != 0); break;
2668 case TYPE_GREATER: n1 = (i > 0); break;
2669 case TYPE_GEQUAL: n1 = (i >= 0); break;
2670 case TYPE_SMALLER: n1 = (i < 0); break;
2671 case TYPE_SEQUAL: n1 = (i <= 0); break;
2672
2673 case TYPE_MATCH:
2674 case TYPE_NOMATCH:
2675 /* avoid 'l' flag in 'cpoptions' */
2676 save_cpo = p_cpo;
2677 p_cpo = (char_u *)"";
2678 regmatch.regprog = vim_regcomp(s2,
2679 RE_MAGIC + RE_STRING);
2680 regmatch.rm_ic = ic;
2681 if (regmatch.regprog != NULL)
2682 {
2683 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
2684 vim_free(regmatch.regprog);
2685 if (type == TYPE_NOMATCH)
2686 n1 = !n1;
2687 }
2688 p_cpo = save_cpo;
2689 break;
2690
2691 case TYPE_UNKNOWN: break; /* avoid gcc warning */
2692 }
2693 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002694 clear_tv(rettv);
2695 clear_tv(&var2);
2696 rettv->v_type = VAR_NUMBER;
2697 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 }
2699 }
2700
2701 return OK;
2702}
2703
2704/*
2705 * Handle fourth level expression:
2706 * + number addition
2707 * - number subtraction
2708 * . string concatenation
2709 *
2710 * "arg" must point to the first non-white of the expression.
2711 * "arg" is advanced to the next non-white after the recognized expression.
2712 *
2713 * Return OK or FAIL.
2714 */
2715 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002716eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002718 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 int evaluate;
2720{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002721 typeval var2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002722 typeval var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 int op;
2724 long n1, n2;
2725 char_u *s1, *s2;
2726 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2727 char_u *p;
2728
2729 /*
2730 * Get the first variable.
2731 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002732 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 return FAIL;
2734
2735 /*
2736 * Repeat computing, until no '+', '-' or '.' is following.
2737 */
2738 for (;;)
2739 {
2740 op = **arg;
2741 if (op != '+' && op != '-' && op != '.')
2742 break;
2743
2744 /*
2745 * Get the second variable.
2746 */
2747 *arg = skipwhite(*arg + 1);
2748 if (eval6(arg, &var2, evaluate) == FAIL)
2749 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002750 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 return FAIL;
2752 }
2753
2754 if (evaluate)
2755 {
2756 /*
2757 * Compute the result.
2758 */
2759 if (op == '.')
2760 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002761 s1 = get_tv_string_buf(rettv, buf1);
2762 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 op = (int)STRLEN(s1);
2764 p = alloc((unsigned)(op + STRLEN(s2) + 1));
2765 if (p != NULL)
2766 {
2767 STRCPY(p, s1);
2768 STRCPY(p + op, s2);
2769 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002770 clear_tv(rettv);
2771 rettv->v_type = VAR_STRING;
2772 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002774 else if (rettv->v_type == VAR_LIST && var2.v_type == VAR_LIST)
2775 {
2776 /* concatenate Lists */
2777 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
2778 &var3) == FAIL)
2779 {
2780 clear_tv(rettv);
2781 clear_tv(&var2);
2782 return FAIL;
2783 }
2784 clear_tv(rettv);
2785 *rettv = var3;
2786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 else
2788 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002789 n1 = get_tv_number(rettv);
2790 n2 = get_tv_number(&var2);
2791 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if (op == '+')
2793 n1 = n1 + n2;
2794 else
2795 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002796 rettv->v_type = VAR_NUMBER;
2797 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002799 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 }
2801 }
2802 return OK;
2803}
2804
2805/*
2806 * Handle fifth level expression:
2807 * * number multiplication
2808 * / number division
2809 * % number modulo
2810 *
2811 * "arg" must point to the first non-white of the expression.
2812 * "arg" is advanced to the next non-white after the recognized expression.
2813 *
2814 * Return OK or FAIL.
2815 */
2816 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002817eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002819 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 int evaluate;
2821{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002822 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 int op;
2824 long n1, n2;
2825
2826 /*
2827 * Get the first variable.
2828 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002829 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 return FAIL;
2831
2832 /*
2833 * Repeat computing, until no '*', '/' or '%' is following.
2834 */
2835 for (;;)
2836 {
2837 op = **arg;
2838 if (op != '*' && op != '/' && op != '%')
2839 break;
2840
2841 if (evaluate)
2842 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002843 n1 = get_tv_number(rettv);
2844 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 }
2846 else
2847 n1 = 0;
2848
2849 /*
2850 * Get the second variable.
2851 */
2852 *arg = skipwhite(*arg + 1);
2853 if (eval7(arg, &var2, evaluate) == FAIL)
2854 return FAIL;
2855
2856 if (evaluate)
2857 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002858 n2 = get_tv_number(&var2);
2859 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860
2861 /*
2862 * Compute the result.
2863 */
2864 if (op == '*')
2865 n1 = n1 * n2;
2866 else if (op == '/')
2867 {
2868 if (n2 == 0) /* give an error message? */
2869 n1 = 0x7fffffffL;
2870 else
2871 n1 = n1 / n2;
2872 }
2873 else
2874 {
2875 if (n2 == 0) /* give an error message? */
2876 n1 = 0;
2877 else
2878 n1 = n1 % n2;
2879 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002880 rettv->v_type = VAR_NUMBER;
2881 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 }
2883 }
2884
2885 return OK;
2886}
2887
2888/*
2889 * Handle sixth level expression:
2890 * number number constant
2891 * "string" string contstant
2892 * 'string' literal string contstant
2893 * &option-name option value
2894 * @r register contents
2895 * identifier variable value
2896 * function() function call
2897 * $VAR environment variable
2898 * (expression) nested expression
2899 *
2900 * Also handle:
2901 * ! in front logical NOT
2902 * - in front unary minus
2903 * + in front unary plus (ignored)
2904 * trailing [] subscript in String
2905 *
2906 * "arg" must point to the first non-white of the expression.
2907 * "arg" is advanced to the next non-white after the recognized expression.
2908 *
2909 * Return OK or FAIL.
2910 */
2911 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002912eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002914 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 int evaluate;
2916{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 long n;
2918 int len;
2919 char_u *s;
2920 int val;
2921 char_u *start_leader, *end_leader;
2922 int ret = OK;
2923 char_u *alias;
2924
2925 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002926 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002927 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002929 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930
2931 /*
2932 * Skip '!' and '-' characters. They are handled later.
2933 */
2934 start_leader = *arg;
2935 while (**arg == '!' || **arg == '-' || **arg == '+')
2936 *arg = skipwhite(*arg + 1);
2937 end_leader = *arg;
2938
2939 switch (**arg)
2940 {
2941 /*
2942 * Number constant.
2943 */
2944 case '0':
2945 case '1':
2946 case '2':
2947 case '3':
2948 case '4':
2949 case '5':
2950 case '6':
2951 case '7':
2952 case '8':
2953 case '9':
2954 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
2955 *arg += len;
2956 if (evaluate)
2957 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002958 rettv->v_type = VAR_NUMBER;
2959 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 }
2961 break;
2962
2963 /*
2964 * String constant: "string".
2965 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002966 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 break;
2968
2969 /*
2970 * Literal string constant: 'string'.
2971 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002972 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002973 break;
2974
2975 /*
2976 * List: [expr, expr]
2977 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002978 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 break;
2980
2981 /*
2982 * Option value: &name
2983 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002984 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 break;
2986
2987 /*
2988 * Environment variable: $VAR.
2989 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002990 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 break;
2992
2993 /*
2994 * Register contents: @r.
2995 */
2996 case '@': ++*arg;
2997 if (evaluate)
2998 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002999 rettv->v_type = VAR_STRING;
3000 rettv->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 }
3002 if (**arg != NUL)
3003 ++*arg;
3004 break;
3005
3006 /*
3007 * nested expression: (expression).
3008 */
3009 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003010 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 if (**arg == ')')
3012 ++*arg;
3013 else if (ret == OK)
3014 {
3015 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003016 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 ret = FAIL;
3018 }
3019 break;
3020
3021 /*
3022 * Must be a variable or function name then.
3023 */
3024 default: s = *arg;
3025 len = get_func_len(arg, &alias, evaluate);
3026 if (alias != NULL)
3027 s = alias;
3028
3029 if (len == 0)
3030 ret = FAIL;
3031 else
3032 {
3033 if (**arg == '(') /* recursive! */
3034 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003035 /* If "s" is the name of a variable of type VAR_FUNC
3036 * use its contents. */
3037 s = deref_func_name(s, &len);
3038
3039 /* Invoke the function. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003040 ret = get_func_tv(s, len, rettv, arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
3042 &len, evaluate);
3043 /* Stop the expression evaluation when immediately
3044 * aborting on error, or when an interrupt occurred or
3045 * an exception was thrown but not caught. */
3046 if (aborting())
3047 {
3048 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003049 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 ret = FAIL;
3051 }
3052 }
3053 else if (evaluate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003054 ret = get_var_tv(s, len, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 }
3056
3057 if (alias != NULL)
3058 vim_free(alias);
3059
3060 break;
3061 }
3062 *arg = skipwhite(*arg);
3063
3064 /*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003065 * Handle expr[expr] and expr[expr:expr] subscript.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003067 while (**arg == '[' && ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003069 if (eval_index(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003071 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 return FAIL;
3073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 }
3075
3076 /*
3077 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3078 */
3079 if (ret == OK && evaluate && end_leader > start_leader)
3080 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003081 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 while (end_leader > start_leader)
3083 {
3084 --end_leader;
3085 if (*end_leader == '!')
3086 val = !val;
3087 else if (*end_leader == '-')
3088 val = -val;
3089 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003090 clear_tv(rettv);
3091 rettv->v_type = VAR_NUMBER;
3092 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 }
3094
3095 return ret;
3096}
3097
3098/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003099 * Evaluate an "[expr]" or "[expr:expr]" index.
3100 * "*arg" points to the '['.
3101 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3102 */
3103 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003104eval_index(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003105 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003106 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003107 int evaluate;
3108{
3109 int empty1 = FALSE, empty2 = FALSE;
3110 typeval var1, var2;
3111 long n1, n2 = 0;
3112 long len;
3113 int range;
3114 char_u *s;
3115
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003116 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003117 {
3118 EMSG(_("E999: Cannot index a Funcref"));
3119 return FAIL;
3120 }
3121
3122 /*
3123 * Get the (first) variable from inside the [].
3124 */
3125 *arg = skipwhite(*arg + 1);
3126 if (**arg == ':')
3127 empty1 = TRUE;
3128 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
3129 return FAIL;
3130
3131 /*
3132 * Get the second variable from inside the [:].
3133 */
3134 if (**arg == ':')
3135 {
3136 range = TRUE;
3137 *arg = skipwhite(*arg + 1);
3138 if (**arg == ']')
3139 empty2 = TRUE;
3140 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
3141 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003142 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003143 return FAIL;
3144 }
3145 }
3146 else
3147 range = FALSE;
3148
3149 /* Check for the ']'. */
3150 if (**arg != ']')
3151 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003152 EMSG(_(e_missbrac));
3153 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003154 if (range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003155 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003156 return FAIL;
3157 }
3158
3159 if (evaluate)
3160 {
3161 if (empty1)
3162 n1 = 0;
3163 else
3164 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003165 n1 = get_tv_number(&var1);
3166 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003167 }
3168 if (range)
3169 {
3170 if (empty2)
3171 n2 = -1;
3172 else
3173 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003174 n2 = get_tv_number(&var2);
3175 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003176 }
3177 }
3178
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003179 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003180 {
3181 case VAR_NUMBER:
3182 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003183 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003184 len = (long)STRLEN(s);
3185 if (range)
3186 {
3187 /* The resulting variable is a substring. If the indexes
3188 * are out of range the result is empty. */
3189 if (n1 < 0)
3190 {
3191 n1 = len + n1;
3192 if (n1 < 0)
3193 n1 = 0;
3194 }
3195 if (n2 < 0)
3196 n2 = len + n2;
3197 else if (n2 >= len)
3198 n2 = len;
3199 if (n1 >= len || n2 < 0 || n1 > n2)
3200 s = NULL;
3201 else
3202 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3203 }
3204 else
3205 {
3206 /* The resulting variable is a string of a single
3207 * character. If the index is too big or negative the
3208 * result is empty. */
3209 if (n1 >= len || n1 < 0)
3210 s = NULL;
3211 else
3212 s = vim_strnsave(s + n1, 1);
3213 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003214 clear_tv(rettv);
3215 rettv->v_type = VAR_STRING;
3216 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003217 break;
3218
3219 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003220 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003221 if (n1 < 0)
3222 n1 = len + n1;
3223 if (!empty1 && (n1 < 0 || n1 >= len))
3224 {
3225 EMSGN(_(e_listidx), n1);
3226 return FAIL;
3227 }
3228 if (range)
3229 {
3230 listvar *l;
3231 listitem *item;
3232
3233 if (n2 < 0)
3234 n2 = len + n2;
3235 if (!empty2 && (n2 < 0 || n2 >= len || n2 < n1))
3236 {
3237 EMSGN(_(e_listidx), n2);
3238 return FAIL;
3239 }
3240 l = list_alloc();
3241 if (l == NULL)
3242 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003243 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003244 n1 <= n2; ++n1)
3245 {
3246 if (list_append_tv(l, &item->li_tv) == FAIL)
3247 {
3248 list_free(l);
3249 return FAIL;
3250 }
3251 item = item->li_next;
3252 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003253 clear_tv(rettv);
3254 rettv->v_type = VAR_LIST;
3255 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00003256 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003257 }
3258 else
3259 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003260 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003261 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003262 clear_tv(rettv);
3263 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003264 }
3265 break;
3266 }
3267 }
3268
3269 *arg = skipwhite(*arg + 1); /* skip the ']' */
3270 return OK;
3271}
3272
3273/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 * Get an option value.
3275 * "arg" points to the '&' or '+' before the option name.
3276 * "arg" is advanced to character after the option name.
3277 * Return OK or FAIL.
3278 */
3279 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003280get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003282 typeval *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 int evaluate;
3284{
3285 char_u *option_end;
3286 long numval;
3287 char_u *stringval;
3288 int opt_type;
3289 int c;
3290 int working = (**arg == '+'); /* has("+option") */
3291 int ret = OK;
3292 int opt_flags;
3293
3294 /*
3295 * Isolate the option name and find its value.
3296 */
3297 option_end = find_option_end(arg, &opt_flags);
3298 if (option_end == NULL)
3299 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003300 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 EMSG2(_("E112: Option name missing: %s"), *arg);
3302 return FAIL;
3303 }
3304
3305 if (!evaluate)
3306 {
3307 *arg = option_end;
3308 return OK;
3309 }
3310
3311 c = *option_end;
3312 *option_end = NUL;
3313 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003314 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315
3316 if (opt_type == -3) /* invalid name */
3317 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003318 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 EMSG2(_("E113: Unknown option: %s"), *arg);
3320 ret = FAIL;
3321 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003322 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 {
3324 if (opt_type == -2) /* hidden string option */
3325 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003326 rettv->v_type = VAR_STRING;
3327 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 }
3329 else if (opt_type == -1) /* hidden number option */
3330 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003331 rettv->v_type = VAR_NUMBER;
3332 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 }
3334 else if (opt_type == 1) /* number option */
3335 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003336 rettv->v_type = VAR_NUMBER;
3337 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 }
3339 else /* string option */
3340 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003341 rettv->v_type = VAR_STRING;
3342 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 }
3344 }
3345 else if (working && (opt_type == -2 || opt_type == -1))
3346 ret = FAIL;
3347
3348 *option_end = c; /* put back for error messages */
3349 *arg = option_end;
3350
3351 return ret;
3352}
3353
3354/*
3355 * Allocate a variable for a string constant.
3356 * Return OK or FAIL.
3357 */
3358 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003359get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003361 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 int evaluate;
3363{
3364 char_u *p;
3365 char_u *name;
3366 int i;
3367 int extra = 0;
3368
3369 /*
3370 * Find the end of the string, skipping backslashed characters.
3371 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003372 for (p = *arg + 1; *p && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 {
3374 if (*p == '\\' && p[1] != NUL)
3375 {
3376 ++p;
3377 /* A "\<x>" form occupies at least 4 characters, and produces up
3378 * to 6 characters: reserve space for 2 extra */
3379 if (*p == '<')
3380 extra += 2;
3381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
3383
3384 if (*p != '"')
3385 {
3386 EMSG2(_("E114: Missing quote: %s"), *arg);
3387 return FAIL;
3388 }
3389
3390 /* If only parsing, set *arg and return here */
3391 if (!evaluate)
3392 {
3393 *arg = p + 1;
3394 return OK;
3395 }
3396
3397 /*
3398 * Copy the string into allocated memory, handling backslashed
3399 * characters.
3400 */
3401 name = alloc((unsigned)(p - *arg + extra));
3402 if (name == NULL)
3403 return FAIL;
3404
3405 i = 0;
3406 for (p = *arg + 1; *p && *p != '"'; ++p)
3407 {
3408 if (*p == '\\')
3409 {
3410 switch (*++p)
3411 {
3412 case 'b': name[i++] = BS; break;
3413 case 'e': name[i++] = ESC; break;
3414 case 'f': name[i++] = FF; break;
3415 case 'n': name[i++] = NL; break;
3416 case 'r': name[i++] = CAR; break;
3417 case 't': name[i++] = TAB; break;
3418
3419 case 'X': /* hex: "\x1", "\x12" */
3420 case 'x':
3421 case 'u': /* Unicode: "\u0023" */
3422 case 'U':
3423 if (vim_isxdigit(p[1]))
3424 {
3425 int n, nr;
3426 int c = toupper(*p);
3427
3428 if (c == 'X')
3429 n = 2;
3430 else
3431 n = 4;
3432 nr = 0;
3433 while (--n >= 0 && vim_isxdigit(p[1]))
3434 {
3435 ++p;
3436 nr = (nr << 4) + hex2nr(*p);
3437 }
3438#ifdef FEAT_MBYTE
3439 /* For "\u" store the number according to
3440 * 'encoding'. */
3441 if (c != 'X')
3442 i += (*mb_char2bytes)(nr, name + i);
3443 else
3444#endif
3445 name[i++] = nr;
3446 }
3447 else
3448 name[i++] = *p;
3449 break;
3450
3451 /* octal: "\1", "\12", "\123" */
3452 case '0':
3453 case '1':
3454 case '2':
3455 case '3':
3456 case '4':
3457 case '5':
3458 case '6':
3459 case '7': name[i] = *p - '0';
3460 if (p[1] >= '0' && p[1] <= '7')
3461 {
3462 ++p;
3463 name[i] = (name[i] << 3) + *p - '0';
3464 if (p[1] >= '0' && p[1] <= '7')
3465 {
3466 ++p;
3467 name[i] = (name[i] << 3) + *p - '0';
3468 }
3469 }
3470 ++i;
3471 break;
3472
3473 /* Special key, e.g.: "\<C-W>" */
3474 case '<': extra = trans_special(&p, name + i, TRUE);
3475 if (extra != 0)
3476 {
3477 i += extra;
3478 --p;
3479 break;
3480 }
3481 /* FALLTHROUGH */
3482
3483 default: name[i++] = *p;
3484 break;
3485 }
3486 }
3487 else
3488 name[i++] = *p;
3489
3490#ifdef FEAT_MBYTE
3491 /* For a multi-byte character copy the bytes after the first one. */
3492 if (has_mbyte)
3493 {
3494 int l = (*mb_ptr2len_check)(p);
3495
3496 while (--l > 0)
3497 name[i++] = *++p;
3498 }
3499#endif
3500 }
3501 name[i] = NUL;
3502 *arg = p + 1;
3503
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003504 rettv->v_type = VAR_STRING;
3505 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
3507 return OK;
3508}
3509
3510/*
3511 * Allocate a variable for an backtick-string constant.
3512 * Return OK or FAIL.
3513 */
3514 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003515get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003517 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 int evaluate;
3519{
3520 char_u *p;
3521 char_u *name;
3522
3523 /*
3524 * Find the end of the string.
3525 */
3526 p = vim_strchr(*arg + 1, '\'');
3527 if (p == NULL)
3528 {
3529 EMSG2(_("E115: Missing quote: %s"), *arg);
3530 return FAIL;
3531 }
3532
3533 if (evaluate)
3534 {
3535 /*
3536 * Copy the string into allocated memory.
3537 */
3538 name = vim_strnsave(*arg + 1, (int)(p - (*arg + 1)));
3539 if (name == NULL)
3540 return FAIL;
3541
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003542 rettv->v_type = VAR_STRING;
3543 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
3545
3546 *arg = p + 1;
3547
3548 return OK;
3549}
3550
3551/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003552 * Allocate a variable for a List and fill it from "*arg".
3553 * Return OK or FAIL.
3554 */
3555 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003556get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003557 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003558 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003559 int evaluate;
3560{
3561 listvar *l = NULL;
3562 typeval tv;
3563 listitem *item;
3564
3565 if (evaluate)
3566 {
3567 l = list_alloc();
3568 if (l == NULL)
3569 return FAIL;
3570 }
3571
3572 *arg = skipwhite(*arg + 1);
3573 while (**arg != ']' && **arg != NUL)
3574 {
3575 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
3576 goto failret;
3577 if (evaluate)
3578 {
3579 item = listitem_alloc();
3580 if (item != NULL)
3581 {
3582 item->li_tv = tv;
3583 list_append(l, item);
3584 }
3585 }
3586
3587 if (**arg == ']')
3588 break;
3589 if (**arg != ',')
3590 {
3591 EMSG2(_("E999: Missing comma in list: %s"), *arg);
3592 goto failret;
3593 }
3594 *arg = skipwhite(*arg + 1);
3595 }
3596
3597 if (**arg != ']')
3598 {
3599 EMSG2(_("E999: Missing end of list ']': %s"), *arg);
3600failret:
3601 if (evaluate)
3602 list_free(l);
3603 return FAIL;
3604 }
3605
3606 *arg = skipwhite(*arg + 1);
3607 if (evaluate)
3608 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003609 rettv->v_type = VAR_LIST;
3610 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003611 ++l->lv_refcount;
3612 }
3613
3614 return OK;
3615}
3616
3617/*
3618 * Allocate an empty header for a list.
3619 */
3620 static listvar *
3621list_alloc()
3622{
3623 return (listvar *)alloc_clear(sizeof(listvar));
3624}
3625
3626/*
3627 * Unreference a list: decrement the reference count and free it when it
3628 * becomes zero.
3629 */
3630 static void
3631list_unref(l)
3632 listvar *l;
3633{
3634 if (l != NULL && --l->lv_refcount <= 0)
3635 list_free(l);
3636}
3637
3638/*
3639 * Free a list, including all items it points to.
3640 * Ignores the reference count.
3641 */
3642 static void
3643list_free(l)
3644 listvar *l;
3645{
3646 listitem *item;
3647 listitem *next;
3648
3649 for (item = l->lv_first; item != NULL; item = next)
3650 {
3651 next = item->li_next;
3652 listitem_free(item);
3653 }
3654 vim_free(l);
3655}
3656
3657/*
3658 * Allocate a list item.
3659 */
3660 static listitem *
3661listitem_alloc()
3662{
3663 return (listitem *)alloc(sizeof(listitem));
3664}
3665
3666/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003667 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003668 */
3669 static void
3670listitem_free(item)
3671 listitem *item;
3672{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003673 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003674 vim_free(item);
3675}
3676
3677/*
3678 * Get the number of items in a list.
3679 */
3680 static long
3681list_len(l)
3682 listvar *l;
3683{
3684 listitem *item;
3685 long len = 0;
3686
3687 if (l == NULL)
3688 return 0L;
3689 for (item = l->lv_first; item != NULL; item = item->li_next)
3690 ++len;
3691 return len;
3692}
3693
3694/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003695 * Return TRUE when two lists have exactly the same values.
3696 */
3697 static int
3698list_equal(l1, l2, ic)
3699 listvar *l1;
3700 listvar *l2;
3701 int ic; /* ignore case for strings */
3702{
3703 listitem *item1, *item2;
3704
3705 for (item1 = l1->lv_first, item2 = l2->lv_first;
3706 item1 != NULL && item2 != NULL;
3707 item1 = item1->li_next, item2 = item2->li_next)
3708 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
3709 return FALSE;
3710 return item1 == NULL && item2 == NULL;
3711}
3712
3713/*
3714 * Return TRUE if "tv1" and "tv2" have the same value.
3715 * Compares the items just like "==" would compare them.
3716 */
3717 static int
3718tv_equal(tv1, tv2, ic)
3719 typeval *tv1;
3720 typeval *tv2;
3721 int ic; /* ignore case */
3722{
3723 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3724
3725 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
3726 {
3727 /* recursive! */
3728 if (tv1->v_type != tv2->v_type
3729 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
3730 return FALSE;
3731 }
3732 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
3733 {
3734 if (tv1->v_type != tv2->v_type
3735 || tv1->vval.v_string == NULL
3736 || tv2->vval.v_string == NULL
3737 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
3738 return FALSE;
3739 }
3740 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
3741 {
3742 if (get_tv_number(tv1) != get_tv_number(tv2))
3743 return FALSE;
3744 }
3745 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
3746 get_tv_string_buf(tv2, buf2)) != 0)
3747 return FALSE;
3748 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
3749 get_tv_string_buf(tv2, buf2)) != 0)
3750 return FALSE;
3751 return TRUE;
3752}
3753
3754/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003755 * Locate item with index "n" in list "l" and return it.
3756 * A negative index is counted from the end; -1 is the last item.
3757 * Returns NULL when "n" is out of range.
3758 */
3759 static listitem *
3760list_find(l, n)
3761 listvar *l;
3762 long n;
3763{
3764 listitem *item;
3765 long idx;
3766
3767 if (l == NULL)
3768 return NULL;
3769 if (n < 0)
3770 {
3771 idx = -1; /* search from the end */
3772 for (item = l->lv_last; item != NULL && idx > n; item = item->li_prev)
3773 --idx;
3774 }
3775 else
3776 {
3777 idx = 0; /* search from the start */
3778 for (item = l->lv_first; item != NULL && idx < n; item = item->li_next)
3779 ++idx;
3780 }
3781 if (idx != n)
3782 return NULL;
3783 return item;
3784}
3785
3786/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003787 * Like list_find(), but also find an item just past the end.
3788 * "*ip" is the item to find.
3789 * When found "*ip" is set to zero, when not found "*ip" is non-zero.
3790 * Returns NULL when item not found or item is just past the end.
3791 */
3792 static listitem *
3793list_find_ext(l, ip)
3794 listvar *l;
3795 long *ip;
3796{
3797 long n;
3798 listitem *item;
3799
3800 if (*ip < 0)
3801 {
3802 /* Count from the end: -1 is before last item. */
3803 item = l->lv_last;
3804 for (n = *ip + 1; n < 0 && item != NULL; ++n)
3805 item = item->li_prev;
3806 if (item == NULL)
3807 n = 1; /* error! */
3808 }
3809 else
3810 {
3811 item = l->lv_first;
3812 for (n = *ip; n > 0 && item != NULL; --n)
3813 item = item->li_next;
3814 }
3815 *ip = n;
3816 return item;
3817}
3818
3819/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003820 * Append item "item" to the end of list "l".
3821 */
3822 static void
3823list_append(l, item)
3824 listvar *l;
3825 listitem *item;
3826{
3827 if (l->lv_last == NULL)
3828 {
3829 /* empty list */
3830 l->lv_first = item;
3831 l->lv_last = item;
3832 item->li_prev = NULL;
3833 }
3834 else
3835 {
3836 l->lv_last->li_next = item;
3837 item->li_prev = l->lv_last;
3838 l->lv_last = item;
3839 }
3840 item->li_next = NULL;
3841}
3842
3843/*
3844 * Append typeval "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003845 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003846 */
3847 static int
3848list_append_tv(l, tv)
3849 listvar *l;
3850 typeval *tv;
3851{
3852 listitem *ni = listitem_alloc();
3853
3854 if (ni == NULL)
3855 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003856 copy_tv(tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003857 list_append(l, ni);
3858 return OK;
3859}
3860
3861/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003862 * Insert typeval "tv" in list "l" before "item".
3863 * If "item" is NULL append at the end.
3864 * Return FAIL when out of memory.
3865 */
3866 static int
3867list_insert_tv(l, tv, item)
3868 listvar *l;
3869 typeval *tv;
3870 listitem *item;
3871{
3872 listitem *ni = listitem_alloc();
3873
3874 if (ni == NULL)
3875 return FAIL;
3876 copy_tv(tv, &ni->li_tv);
3877 if (item == NULL)
3878 /* Append new item at end of list. */
3879 list_append(l, ni);
3880 else
3881 {
3882 /* Insert new item before existing item. */
3883 ni->li_prev = item->li_prev;
3884 ni->li_next = item;
3885 if (item->li_prev == NULL)
3886 l->lv_first = ni;
3887 else
3888 item->li_prev->li_next = ni;
3889 item->li_prev = ni;
3890 }
3891 return OK;
3892}
3893
3894/*
3895 * Extend "l1" with "l2".
3896 * If "bef" is NULL append at the end, otherwise insert before this item.
3897 * Returns FAIL when out of memory.
3898 */
3899 static int
3900list_extend(l1, l2, bef)
3901 listvar *l1;
3902 listvar *l2;
3903 listitem *bef;
3904{
3905 listitem *item;
3906
3907 for (item = l2->lv_first; item != NULL; item = item->li_next)
3908 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
3909 return FAIL;
3910 return OK;
3911}
3912
3913/*
3914 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
3915 * Return FAIL when out of memory.
3916 */
3917 static int
3918list_concat(l1, l2, tv)
3919 listvar *l1;
3920 listvar *l2;
3921 typeval *tv;
3922{
3923 listvar *l;
3924
3925 /* make a copy of the first list. */
3926 l = list_copy(l1, FALSE);
3927 if (l == NULL)
3928 return FAIL;
3929 tv->v_type = VAR_LIST;
3930 tv->vval.v_list = l;
3931
3932 /* append all items from the second list */
3933 return list_extend(l, l2, NULL);
3934}
3935
3936/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003937 * Make a copy of list "l". Shallow if "deep" is FALSE.
3938 * The refcount of the new list is set to 1.
3939 * Returns NULL when out of memory.
3940 */
3941 static listvar *
3942list_copy(orig, deep)
3943 listvar *orig;
3944 int deep;
3945{
3946 listvar *copy;
3947 listitem *item;
3948 listitem *ni;
3949 static int recurse = 0;
3950
3951 if (orig == NULL)
3952 return NULL;
3953 if (recurse >= VAR_LIST_MAXNEST)
3954 {
3955 EMSG(_("E999: List nested too deep for making a copy"));
3956 return NULL;
3957 }
3958 ++recurse;
3959
3960 copy = list_alloc();
3961 if (copy != NULL)
3962 {
3963 for (item = orig->lv_first; item != NULL; item = item->li_next)
3964 {
3965 ni = listitem_alloc();
3966 if (ni == NULL)
3967 break;
3968 if (deep && item->li_tv.v_type == VAR_LIST)
3969 {
3970 ni->li_tv.v_type = VAR_LIST;
3971 ni->li_tv.vval.v_list = list_copy(item->li_tv.vval.v_list,
3972 TRUE);
3973 if (ni->li_tv.vval.v_list == NULL)
3974 {
3975 vim_free(ni);
3976 break;
3977 }
3978 }
3979 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003980 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003981 list_append(copy, ni);
3982 }
3983 ++copy->lv_refcount;
3984 }
3985
3986 --recurse;
3987 return copy;
3988}
3989
3990/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003991 * Remove items "item" to "item2" from list "l".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003992 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003993 static void
3994list_getrem(l, item, item2)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003995 listvar *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003996 listitem *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003997 listitem *item2;
3998{
3999 listitem *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004000
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004001 /* notify watchers */
4002 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004003 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004004 list_fix_watch(l, ip);
4005 if (ip == item2)
4006 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004007 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004008
4009 if (item2->li_next == NULL)
4010 l->lv_last = item->li_prev;
4011 else
4012 item2->li_next->li_prev = item->li_prev;
4013 if (item->li_prev == NULL)
4014 l->lv_first = item2->li_next;
4015 else
4016 item->li_prev->li_next = item2->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004017}
4018
4019/*
4020 * Return an allocated string with the string representation of a list.
4021 * May return NULL.
4022 */
4023 static char_u *
4024list2string(tv)
4025 typeval *tv;
4026{
4027 garray_T ga;
4028 listitem *item;
4029 int first = TRUE;
4030 char_u *tofree;
4031 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004032 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004033
4034 if (tv->vval.v_list == NULL)
4035 return NULL;
4036 ga_init2(&ga, (int)sizeof(char), 80);
4037 ga_append(&ga, '[');
4038
4039 for (item = tv->vval.v_list->lv_first; item != NULL; item = item->li_next)
4040 {
4041 if (first)
4042 first = FALSE;
4043 else
4044 ga_concat(&ga, (char_u *)", ");
4045
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004046 s = tv2string(&item->li_tv, &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004047 if (s != NULL)
4048 ga_concat(&ga, s);
4049 vim_free(tofree);
4050 }
4051
4052 ga_append(&ga, ']');
4053 ga_append(&ga, NUL);
4054 return (char_u *)ga.ga_data;
4055}
4056
4057/*
4058 * Return a string with the string representation of a variable.
4059 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004060 * "numbuf" is used for a number.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004061 * May return NULL;
4062 */
4063 static char_u *
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004064tv2string(tv, tofree, numbuf)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004065 typeval *tv;
4066 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004067 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004068{
4069 switch (tv->v_type)
4070 {
4071 case VAR_FUNC:
4072 *tofree = NULL;
4073 return tv->vval.v_string;
4074 case VAR_LIST:
4075 *tofree = list2string(tv);
4076 return *tofree;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004077 case VAR_STRING:
4078 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004079 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004080 default:
4081 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004082 }
4083 *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004084 return get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004085}
4086
4087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 * Get the value of an environment variable.
4089 * "arg" is pointing to the '$'. It is advanced to after the name.
4090 * If the environment variable was not set, silently assume it is empty.
4091 * Always return OK.
4092 */
4093 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004094get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004096 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 int evaluate;
4098{
4099 char_u *string = NULL;
4100 int len;
4101 int cc;
4102 char_u *name;
4103
4104 ++*arg;
4105 name = *arg;
4106 len = get_env_len(arg);
4107 if (evaluate)
4108 {
4109 if (len != 0)
4110 {
4111 cc = name[len];
4112 name[len] = NUL;
4113 /* first try mch_getenv(), fast for normal environment vars */
4114 string = mch_getenv(name);
4115 if (string != NULL && *string != NUL)
4116 string = vim_strsave(string);
4117 else
4118 {
4119 /* next try expanding things like $VIM and ${HOME} */
4120 string = expand_env_save(name - 1);
4121 if (string != NULL && *string == '$')
4122 {
4123 vim_free(string);
4124 string = NULL;
4125 }
4126 }
4127 name[len] = cc;
4128 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 rettv->v_type = VAR_STRING;
4130 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 }
4132
4133 return OK;
4134}
4135
4136/*
4137 * Array with names and number of arguments of all internal functions
4138 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
4139 */
4140static struct fst
4141{
4142 char *f_name; /* function name */
4143 char f_min_argc; /* minimal number of arguments */
4144 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004145 void (*f_func) __ARGS((typeval *args, typeval *rvar));
4146 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147} functions[] =
4148{
Bram Moolenaar0d660222005-01-07 21:51:51 +00004149 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 {"append", 2, 2, f_append},
4151 {"argc", 0, 0, f_argc},
4152 {"argidx", 0, 0, f_argidx},
4153 {"argv", 1, 1, f_argv},
4154 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004155 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {"bufexists", 1, 1, f_bufexists},
4157 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
4158 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
4159 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
4160 {"buflisted", 1, 1, f_buflisted},
4161 {"bufloaded", 1, 1, f_bufloaded},
4162 {"bufname", 1, 1, f_bufname},
4163 {"bufnr", 1, 1, f_bufnr},
4164 {"bufwinnr", 1, 1, f_bufwinnr},
4165 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00004166 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004167 {"call", 2, 2, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 {"char2nr", 1, 1, f_char2nr},
4169 {"cindent", 1, 1, f_cindent},
4170 {"col", 1, 1, f_col},
4171 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004172 {"copy", 1, 1, f_copy},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004173 {"count", 2, 3, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 {"cscope_connection",0,3, f_cscope_connection},
4175 {"cursor", 2, 2, f_cursor},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004176 {"deepcopy", 1, 1, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 {"delete", 1, 1, f_delete},
4178 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00004179 {"diff_filler", 1, 1, f_diff_filler},
4180 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 {"escape", 2, 2, f_escape},
4182 {"eventhandler", 0, 0, f_eventhandler},
4183 {"executable", 1, 1, f_executable},
4184 {"exists", 1, 1, f_exists},
4185 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004186 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
4188 {"filereadable", 1, 1, f_filereadable},
4189 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004190 {"finddir", 1, 3, f_finddir},
4191 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 {"fnamemodify", 2, 2, f_fnamemodify},
4193 {"foldclosed", 1, 1, f_foldclosed},
4194 {"foldclosedend", 1, 1, f_foldclosedend},
4195 {"foldlevel", 1, 1, f_foldlevel},
4196 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004197 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004199 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00004200 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 {"getbufvar", 2, 2, f_getbufvar},
4202 {"getchar", 0, 1, f_getchar},
4203 {"getcharmod", 0, 0, f_getcharmod},
4204 {"getcmdline", 0, 0, f_getcmdline},
4205 {"getcmdpos", 0, 0, f_getcmdpos},
4206 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004207 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004208 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 {"getfsize", 1, 1, f_getfsize},
4210 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004211 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00004212 {"getline", 1, 2, f_getline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 {"getreg", 0, 1, f_getreg},
4214 {"getregtype", 0, 1, f_getregtype},
4215 {"getwinposx", 0, 0, f_getwinposx},
4216 {"getwinposy", 0, 0, f_getwinposy},
4217 {"getwinvar", 2, 2, f_getwinvar},
4218 {"glob", 1, 1, f_glob},
4219 {"globpath", 2, 2, f_globpath},
4220 {"has", 1, 1, f_has},
4221 {"hasmapto", 1, 2, f_hasmapto},
4222 {"highlightID", 1, 1, f_hlID}, /* obsolete */
4223 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
4224 {"histadd", 2, 2, f_histadd},
4225 {"histdel", 1, 2, f_histdel},
4226 {"histget", 1, 2, f_histget},
4227 {"histnr", 1, 1, f_histnr},
4228 {"hlID", 1, 1, f_hlID},
4229 {"hlexists", 1, 1, f_hlexists},
4230 {"hostname", 0, 0, f_hostname},
4231 {"iconv", 3, 3, f_iconv},
4232 {"indent", 1, 1, f_indent},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004233 {"index", 2, 3, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 {"input", 1, 2, f_input},
4235 {"inputdialog", 1, 3, f_inputdialog},
4236 {"inputrestore", 0, 0, f_inputrestore},
4237 {"inputsave", 0, 0, f_inputsave},
4238 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004239 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 {"isdirectory", 1, 1, f_isdirectory},
4241 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004242 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 {"libcall", 3, 3, f_libcall},
4244 {"libcallnr", 3, 3, f_libcallnr},
4245 {"line", 1, 1, f_line},
4246 {"line2byte", 1, 1, f_line2byte},
4247 {"lispindent", 1, 1, f_lispindent},
4248 {"localtime", 0, 0, f_localtime},
4249 {"maparg", 1, 2, f_maparg},
4250 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004251 {"match", 2, 4, f_match},
4252 {"matchend", 2, 4, f_matchend},
4253 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {"mode", 0, 0, f_mode},
4255 {"nextnonblank", 1, 1, f_nextnonblank},
4256 {"nr2char", 1, 1, f_nr2char},
4257 {"prevnonblank", 1, 1, f_prevnonblank},
4258 {"remote_expr", 2, 3, f_remote_expr},
4259 {"remote_foreground", 1, 1, f_remote_foreground},
4260 {"remote_peek", 1, 2, f_remote_peek},
4261 {"remote_read", 1, 1, f_remote_read},
4262 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004263 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00004265 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00004267 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 {"search", 1, 2, f_search},
4269 {"searchpair", 3, 5, f_searchpair},
4270 {"server2client", 2, 2, f_server2client},
4271 {"serverlist", 0, 0, f_serverlist},
4272 {"setbufvar", 3, 3, f_setbufvar},
4273 {"setcmdpos", 1, 1, f_setcmdpos},
4274 {"setline", 2, 2, f_setline},
4275 {"setreg", 2, 3, f_setreg},
4276 {"setwinvar", 3, 3, f_setwinvar},
4277 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00004278 {"sort", 1, 2, f_sort},
4279 {"str2list", 1, 2, f_str2list},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280#ifdef HAVE_STRFTIME
4281 {"strftime", 1, 2, f_strftime},
4282#endif
4283 {"stridx", 2, 2, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004284 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 {"strlen", 1, 1, f_strlen},
4286 {"strpart", 2, 3, f_strpart},
4287 {"strridx", 2, 2, f_strridx},
4288 {"strtrans", 1, 1, f_strtrans},
4289 {"submatch", 1, 1, f_submatch},
4290 {"substitute", 4, 4, f_substitute},
4291 {"synID", 3, 3, f_synID},
4292 {"synIDattr", 2, 3, f_synIDattr},
4293 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004294 {"system", 1, 2, f_system},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 {"tempname", 0, 0, f_tempname},
4296 {"tolower", 1, 1, f_tolower},
4297 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00004298 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 {"type", 1, 1, f_type},
4300 {"virtcol", 1, 1, f_virtcol},
4301 {"visualmode", 0, 1, f_visualmode},
4302 {"winbufnr", 1, 1, f_winbufnr},
4303 {"wincol", 0, 0, f_wincol},
4304 {"winheight", 1, 1, f_winheight},
4305 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004306 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 {"winrestcmd", 0, 0, f_winrestcmd},
4308 {"winwidth", 1, 1, f_winwidth},
4309};
4310
4311#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4312
4313/*
4314 * Function given to ExpandGeneric() to obtain the list of internal
4315 * or user defined function names.
4316 */
4317 char_u *
4318get_function_name(xp, idx)
4319 expand_T *xp;
4320 int idx;
4321{
4322 static int intidx = -1;
4323 char_u *name;
4324
4325 if (idx == 0)
4326 intidx = -1;
4327 if (intidx < 0)
4328 {
4329 name = get_user_func_name(xp, idx);
4330 if (name != NULL)
4331 return name;
4332 }
4333 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
4334 {
4335 STRCPY(IObuff, functions[intidx].f_name);
4336 STRCAT(IObuff, "(");
4337 if (functions[intidx].f_max_argc == 0)
4338 STRCAT(IObuff, ")");
4339 return IObuff;
4340 }
4341
4342 return NULL;
4343}
4344
4345/*
4346 * Function given to ExpandGeneric() to obtain the list of internal or
4347 * user defined variable or function names.
4348 */
4349/*ARGSUSED*/
4350 char_u *
4351get_expr_name(xp, idx)
4352 expand_T *xp;
4353 int idx;
4354{
4355 static int intidx = -1;
4356 char_u *name;
4357
4358 if (idx == 0)
4359 intidx = -1;
4360 if (intidx < 0)
4361 {
4362 name = get_function_name(xp, idx);
4363 if (name != NULL)
4364 return name;
4365 }
4366 return get_user_var_name(xp, ++intidx);
4367}
4368
4369#endif /* FEAT_CMDL_COMPL */
4370
4371/*
4372 * Find internal function in table above.
4373 * Return index, or -1 if not found
4374 */
4375 static int
4376find_internal_func(name)
4377 char_u *name; /* name of the function */
4378{
4379 int first = 0;
4380 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
4381 int cmp;
4382 int x;
4383
4384 /*
4385 * Find the function name in the table. Binary search.
4386 */
4387 while (first <= last)
4388 {
4389 x = first + ((unsigned)(last - first) >> 1);
4390 cmp = STRCMP(name, functions[x].f_name);
4391 if (cmp < 0)
4392 last = x - 1;
4393 else if (cmp > 0)
4394 first = x + 1;
4395 else
4396 return x;
4397 }
4398 return -1;
4399}
4400
4401/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004402 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
4403 * name it contains, otherwise return "name".
4404 */
4405 static char_u *
4406deref_func_name(name, lenp)
4407 char_u *name;
4408 int *lenp;
4409{
4410 VAR v;
4411 int cc;
4412
4413 cc = name[*lenp];
4414 name[*lenp] = NUL;
4415 v = find_var(name, FALSE);
4416 name[*lenp] = cc;
4417 if (v != NULL && v->tv.v_type == VAR_FUNC)
4418 {
4419 if (v->tv.vval.v_string == NULL)
4420 {
4421 *lenp = 0;
4422 return (char_u *)""; /* just in case */
4423 }
4424 *lenp = STRLEN(v->tv.vval.v_string);
4425 return v->tv.vval.v_string;
4426 }
4427
4428 return name;
4429}
4430
4431/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 * Allocate a variable for the result of a function.
4433 * Return OK or FAIL.
4434 */
4435 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004436get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 char_u *name; /* name of the function */
4438 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004439 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 char_u **arg; /* argument, pointing to the '(' */
4441 linenr_T firstline; /* first line of range */
4442 linenr_T lastline; /* last line of range */
4443 int *doesrange; /* return: function handled range */
4444 int evaluate;
4445{
4446 char_u *argp;
4447 int ret = OK;
4448#define MAX_FUNC_ARGS 20
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004449 typeval argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 int argcount = 0; /* number of arguments found */
4451
4452 /*
4453 * Get the arguments.
4454 */
4455 argp = *arg;
4456 while (argcount < MAX_FUNC_ARGS)
4457 {
4458 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
4459 if (*argp == ')' || *argp == ',' || *argp == NUL)
4460 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
4462 {
4463 ret = FAIL;
4464 break;
4465 }
4466 ++argcount;
4467 if (*argp != ',')
4468 break;
4469 }
4470 if (*argp == ')')
4471 ++argp;
4472 else
4473 ret = FAIL;
4474
4475 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004476 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 firstline, lastline, doesrange, evaluate);
4478 else if (!aborting())
4479 EMSG2(_("E116: Invalid arguments for function %s"), name);
4480
4481 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004482 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483
4484 *arg = skipwhite(argp);
4485 return ret;
4486}
4487
4488
4489/*
4490 * Call a function with its resolved parameters
4491 * Return OK or FAIL.
4492 */
4493 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004494call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 doesrange, evaluate)
4496 char_u *name; /* name of the function */
4497 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004498 typeval *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 int argcount; /* number of "argvars" */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004500 typeval *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 linenr_T firstline; /* first line of range */
4502 linenr_T lastline; /* last line of range */
4503 int *doesrange; /* return: function handled range */
4504 int evaluate;
4505{
4506 int ret = FAIL;
4507 static char *errors[] =
4508 {N_("E117: Unknown function: %s"),
4509 N_("E118: Too many arguments for function: %s"),
4510 N_("E119: Not enough arguments for function: %s"),
4511 N_("E120: Using <SID> not in a script context: %s"),
4512 };
4513#define ERROR_UNKNOWN 0
4514#define ERROR_TOOMANY 1
4515#define ERROR_TOOFEW 2
4516#define ERROR_SCRIPT 3
4517#define ERROR_NONE 4
4518#define ERROR_OTHER 5
4519 int error = ERROR_NONE;
4520 int i;
4521 int llen;
4522 ufunc_T *fp;
4523 int cc;
4524#define FLEN_FIXED 40
4525 char_u fname_buf[FLEN_FIXED + 1];
4526 char_u *fname;
4527
4528 /*
4529 * In a script change <SID>name() and s:name() to K_SNR 123_name().
4530 * Change <SNR>123_name() to K_SNR 123_name().
4531 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
4532 */
4533 cc = name[len];
4534 name[len] = NUL;
4535 llen = eval_fname_script(name);
4536 if (llen > 0)
4537 {
4538 fname_buf[0] = K_SPECIAL;
4539 fname_buf[1] = KS_EXTRA;
4540 fname_buf[2] = (int)KE_SNR;
4541 i = 3;
4542 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
4543 {
4544 if (current_SID <= 0)
4545 error = ERROR_SCRIPT;
4546 else
4547 {
4548 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
4549 i = (int)STRLEN(fname_buf);
4550 }
4551 }
4552 if (i + STRLEN(name + llen) < FLEN_FIXED)
4553 {
4554 STRCPY(fname_buf + i, name + llen);
4555 fname = fname_buf;
4556 }
4557 else
4558 {
4559 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
4560 if (fname == NULL)
4561 error = ERROR_OTHER;
4562 else
4563 {
4564 mch_memmove(fname, fname_buf, (size_t)i);
4565 STRCPY(fname + i, name + llen);
4566 }
4567 }
4568 }
4569 else
4570 fname = name;
4571
4572 *doesrange = FALSE;
4573
4574
4575 /* execute the function if no errors detected and executing */
4576 if (evaluate && error == ERROR_NONE)
4577 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004578 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 error = ERROR_UNKNOWN;
4580
4581 if (!ASCII_ISLOWER(fname[0]))
4582 {
4583 /*
4584 * User defined function.
4585 */
4586 fp = find_func(fname);
4587#ifdef FEAT_AUTOCMD
4588 if (fp == NULL && apply_autocmds(EVENT_FUNCUNDEFINED,
4589 fname, fname, TRUE, NULL)
4590#ifdef FEAT_EVAL
4591 && !aborting()
4592#endif
4593 )
4594 {
4595 /* executed an autocommand, search for function again */
4596 fp = find_func(fname);
4597 }
4598#endif
4599 if (fp != NULL)
4600 {
4601 if (fp->flags & FC_RANGE)
4602 *doesrange = TRUE;
4603 if (argcount < fp->args.ga_len)
4604 error = ERROR_TOOFEW;
4605 else if (!fp->varargs && argcount > fp->args.ga_len)
4606 error = ERROR_TOOMANY;
4607 else
4608 {
4609 /*
4610 * Call the user function.
4611 * Save and restore search patterns, script variables and
4612 * redo buffer.
4613 */
4614 save_search_patterns();
4615 saveRedobuff();
4616 ++fp->calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004617 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 firstline, lastline);
4619 --fp->calls;
4620 restoreRedobuff();
4621 restore_search_patterns();
4622 error = ERROR_NONE;
4623 }
4624 }
4625 }
4626 else
4627 {
4628 /*
4629 * Find the function name in the table, call its implementation.
4630 */
4631 i = find_internal_func(fname);
4632 if (i >= 0)
4633 {
4634 if (argcount < functions[i].f_min_argc)
4635 error = ERROR_TOOFEW;
4636 else if (argcount > functions[i].f_max_argc)
4637 error = ERROR_TOOMANY;
4638 else
4639 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004640 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004641 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 error = ERROR_NONE;
4643 }
4644 }
4645 }
4646 /*
4647 * The function call (or "FuncUndefined" autocommand sequence) might
4648 * have been aborted by an error, an interrupt, or an explicitly thrown
4649 * exception that has not been caught so far. This situation can be
4650 * tested for by calling aborting(). For an error in an internal
4651 * function or for the "E132" error in call_user_func(), however, the
4652 * throw point at which the "force_abort" flag (temporarily reset by
4653 * emsg()) is normally updated has not been reached yet. We need to
4654 * update that flag first to make aborting() reliable.
4655 */
4656 update_force_abort();
4657 }
4658 if (error == ERROR_NONE)
4659 ret = OK;
4660
4661 /*
4662 * Report an error unless the argument evaluation or function call has been
4663 * cancelled due to an aborting error, an interrupt, or an exception.
4664 */
4665 if (error < ERROR_NONE && !aborting())
4666 EMSG2((char_u *)_(errors[error]), name);
4667
4668 name[len] = cc;
4669 if (fname != name && fname != fname_buf)
4670 vim_free(fname);
4671
4672 return ret;
4673}
4674
4675/*********************************************
4676 * Implementation of the built-in functions
4677 */
4678
4679/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00004680 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 */
4682 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00004683f_add(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004684 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004685 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004687 listvar *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004689 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004690 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004692 l = argvars[0].vval.v_list;
4693 if (l != NULL && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004694 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004695 }
4696 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00004697 EMSG(_(e_listreq));
4698}
4699
4700/*
4701 * "append(lnum, string/list)" function
4702 */
4703 static void
4704f_append(argvars, rettv)
4705 typeval *argvars;
4706 typeval *rettv;
4707{
4708 long lnum;
4709 listvar *l = NULL;
4710 listitem *li = NULL;
4711 typeval *tv;
4712 long added = 0;
4713
4714 rettv->vval.v_number = 1; /* Default: Failed */
4715 lnum = get_tv_lnum(argvars);
4716 if (lnum >= 0
4717 && lnum <= curbuf->b_ml.ml_line_count
4718 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004719 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00004720 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00004722 l = argvars[1].vval.v_list;
4723 if (l == NULL)
4724 return;
4725 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004726 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00004727 for (;;)
4728 {
4729 if (l == NULL)
4730 tv = &argvars[1]; /* append a string */
4731 else if (li == NULL)
4732 break; /* end of list */
4733 else
4734 tv = &li->li_tv; /* append item from list */
4735 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
4736 ++added;
4737 if (l == NULL)
4738 break;
4739 li = li->li_next;
4740 }
4741
4742 appended_lines_mark(lnum, added);
4743 if (curwin->w_cursor.lnum > lnum)
4744 curwin->w_cursor.lnum += added;
4745 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 }
4747}
4748
4749/*
4750 * "argc()" function
4751 */
4752/* ARGSUSED */
4753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004754f_argc(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004755 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004756 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004758 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759}
4760
4761/*
4762 * "argidx()" function
4763 */
4764/* ARGSUSED */
4765 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004766f_argidx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004767 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004768 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004770 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771}
4772
4773/*
4774 * "argv(nr)" function
4775 */
4776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004777f_argv(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004778 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004779 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780{
4781 int idx;
4782
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004783 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004785 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004787 rettv->vval.v_string = NULL;
4788 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789}
4790
4791/*
4792 * "browse(save, title, initdir, default)" function
4793 */
4794/* ARGSUSED */
4795 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004796f_browse(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004797 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004798 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799{
4800#ifdef FEAT_BROWSE
4801 int save;
4802 char_u *title;
4803 char_u *initdir;
4804 char_u *defname;
4805 char_u buf[NUMBUFLEN];
4806 char_u buf2[NUMBUFLEN];
4807
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004808 save = get_tv_number(&argvars[0]);
4809 title = get_tv_string(&argvars[1]);
4810 initdir = get_tv_string_buf(&argvars[2], buf);
4811 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004813 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004814 do_browse(save ? BROWSE_SAVE : 0,
4815 title, defname, NULL, initdir, NULL, curbuf);
4816#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004817 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004818#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004819 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004820}
4821
4822/*
4823 * "browsedir(title, initdir)" function
4824 */
4825/* ARGSUSED */
4826 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004827f_browsedir(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004828 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004829 typeval *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004830{
4831#ifdef FEAT_BROWSE
4832 char_u *title;
4833 char_u *initdir;
4834 char_u buf[NUMBUFLEN];
4835
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004836 title = get_tv_string(&argvars[0]);
4837 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004838
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004839 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004840 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004842 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004844 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845}
4846
Bram Moolenaar0d660222005-01-07 21:51:51 +00004847static buf_T *find_buffer __ARGS((typeval *avar));
4848
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849/*
4850 * Find a buffer by number or exact name.
4851 */
4852 static buf_T *
4853find_buffer(avar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004854 typeval *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855{
4856 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004858 if (avar->v_type == VAR_NUMBER)
4859 buf = buflist_findnr((int)avar->vval.v_number);
4860 else if (avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004862 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004863 if (buf == NULL)
4864 {
4865 /* No full path name match, try a match with a URL or a "nofile"
4866 * buffer, these don't use the full path. */
4867 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
4868 if (buf->b_fname != NULL
4869 && (path_with_url(buf->b_fname)
4870#ifdef FEAT_QUICKFIX
4871 || bt_nofile(buf)
4872#endif
4873 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004874 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004875 break;
4876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
4878 return buf;
4879}
4880
4881/*
4882 * "bufexists(expr)" function
4883 */
4884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004885f_bufexists(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004886 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004887 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004889 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890}
4891
4892/*
4893 * "buflisted(expr)" function
4894 */
4895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004896f_buflisted(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004897 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004898 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899{
4900 buf_T *buf;
4901
4902 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004903 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904}
4905
4906/*
4907 * "bufloaded(expr)" function
4908 */
4909 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004910f_bufloaded(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004911 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004912 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913{
4914 buf_T *buf;
4915
4916 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004917 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918}
4919
Bram Moolenaar0d660222005-01-07 21:51:51 +00004920static buf_T *get_buf_tv __ARGS((typeval *tv));
4921
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922/*
4923 * Get buffer by number or pattern.
4924 */
4925 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004926get_buf_tv(tv)
4927 typeval *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004929 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 int save_magic;
4931 char_u *save_cpo;
4932 buf_T *buf;
4933
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004934 if (tv->v_type == VAR_NUMBER)
4935 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 if (name == NULL || *name == NUL)
4937 return curbuf;
4938 if (name[0] == '$' && name[1] == NUL)
4939 return lastbuf;
4940
4941 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
4942 save_magic = p_magic;
4943 p_magic = TRUE;
4944 save_cpo = p_cpo;
4945 p_cpo = (char_u *)"";
4946
4947 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
4948 TRUE, FALSE));
4949
4950 p_magic = save_magic;
4951 p_cpo = save_cpo;
4952
4953 /* If not found, try expanding the name, like done for bufexists(). */
4954 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004955 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956
4957 return buf;
4958}
4959
4960/*
4961 * "bufname(expr)" function
4962 */
4963 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004964f_bufname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004965 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004966 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967{
4968 buf_T *buf;
4969
4970 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004971 buf = get_buf_tv(&argvars[0]);
4972 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004974 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004976 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 --emsg_off;
4978}
4979
4980/*
4981 * "bufnr(expr)" function
4982 */
4983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004984f_bufnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004985 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004986 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987{
4988 buf_T *buf;
4989
4990 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004991 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004993 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004995 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 --emsg_off;
4997}
4998
4999/*
5000 * "bufwinnr(nr)" function
5001 */
5002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005003f_bufwinnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005004 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005005 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006{
5007#ifdef FEAT_WINDOWS
5008 win_T *wp;
5009 int winnr = 0;
5010#endif
5011 buf_T *buf;
5012
5013 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005014 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015#ifdef FEAT_WINDOWS
5016 for (wp = firstwin; wp; wp = wp->w_next)
5017 {
5018 ++winnr;
5019 if (wp->w_buffer == buf)
5020 break;
5021 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005022 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005024 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025#endif
5026 --emsg_off;
5027}
5028
5029/*
5030 * "byte2line(byte)" function
5031 */
5032/*ARGSUSED*/
5033 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005034f_byte2line(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005035 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005036 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037{
5038#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005039 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040#else
5041 long boff = 0;
5042
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005043 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005045 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005047 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 (linenr_T)0, &boff);
5049#endif
5050}
5051
5052/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005053 * "byteidx()" function
5054 */
5055/*ARGSUSED*/
5056 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005057f_byteidx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005058 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005059 typeval *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005060{
5061#ifdef FEAT_MBYTE
5062 char_u *t;
5063#endif
5064 char_u *str;
5065 long idx;
5066
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005067 str = get_tv_string(&argvars[0]);
5068 idx = get_tv_number(&argvars[1]);
5069 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005070 if (idx < 0)
5071 return;
5072
5073#ifdef FEAT_MBYTE
5074 t = str;
5075 for ( ; idx > 0; idx--)
5076 {
5077 if (*t == NUL) /* EOL reached */
5078 return;
5079 t += mb_ptr2len_check(t);
5080 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005081 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005082#else
5083 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005084 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005085#endif
5086}
5087
5088/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005089 * "call(func, arglist)" function
5090 */
5091 static void
5092f_call(argvars, rettv)
5093 typeval *argvars;
5094 typeval *rettv;
5095{
5096 char_u *func;
5097 typeval argv[MAX_FUNC_ARGS];
5098 int argc = 0;
5099 listitem *item;
5100 int dummy;
5101
5102 rettv->vval.v_number = 0;
5103 if (argvars[1].v_type != VAR_LIST)
5104 {
5105 EMSG(_(e_listreq));
5106 return;
5107 }
5108 if (argvars[1].vval.v_list == NULL)
5109 return;
5110
5111 if (argvars[0].v_type == VAR_FUNC)
5112 func = argvars[0].vval.v_string;
5113 else
5114 func = get_tv_string(&argvars[0]);
5115
5116 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
5117 item = item->li_next)
5118 {
5119 if (argc == MAX_FUNC_ARGS)
5120 {
5121 EMSG(_("E999: Too many arguments"));
5122 break;
5123 }
5124 /* Make a copy of each argument (is this really needed?) */
5125 copy_tv(&item->li_tv, &argv[argc++]);
5126 }
5127
5128 if (item == NULL)
5129 (void)call_func(func, STRLEN(func), rettv, argc, argv,
5130 curwin->w_cursor.lnum, curwin->w_cursor.lnum, &dummy, TRUE);
5131
5132 /* Free the arguments. */
5133 while (argc > 0)
5134 clear_tv(&argv[--argc]);
5135}
5136
5137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 * "char2nr(string)" function
5139 */
5140 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005141f_char2nr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005142 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005143 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144{
5145#ifdef FEAT_MBYTE
5146 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005147 rettv->vval.v_number =
5148 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 else
5150#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005151 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152}
5153
5154/*
5155 * "cindent(lnum)" function
5156 */
5157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005158f_cindent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005159 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005160 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161{
5162#ifdef FEAT_CINDENT
5163 pos_T pos;
5164 linenr_T lnum;
5165
5166 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005167 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
5169 {
5170 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005171 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 curwin->w_cursor = pos;
5173 }
5174 else
5175#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005176 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177}
5178
5179/*
5180 * "col(string)" function
5181 */
5182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005183f_col(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005184 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005185 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186{
5187 colnr_T col = 0;
5188 pos_T *fp;
5189
5190 fp = var2fpos(&argvars[0], FALSE);
5191 if (fp != NULL)
5192 {
5193 if (fp->col == MAXCOL)
5194 {
5195 /* '> can be MAXCOL, get the length of the line then */
5196 if (fp->lnum <= curbuf->b_ml.ml_line_count)
5197 col = STRLEN(ml_get(fp->lnum)) + 1;
5198 else
5199 col = MAXCOL;
5200 }
5201 else
5202 {
5203 col = fp->col + 1;
5204#ifdef FEAT_VIRTUALEDIT
5205 /* col(".") when the cursor is on the NUL at the end of the line
5206 * because of "coladd" can be seen as an extra column. */
5207 if (virtual_active() && fp == &curwin->w_cursor)
5208 {
5209 char_u *p = ml_get_cursor();
5210
5211 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
5212 curwin->w_virtcol - curwin->w_cursor.coladd))
5213 {
5214# ifdef FEAT_MBYTE
5215 int l;
5216
5217 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
5218 col += l;
5219# else
5220 if (*p != NUL && p[1] == NUL)
5221 ++col;
5222# endif
5223 }
5224 }
5225#endif
5226 }
5227 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005228 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229}
5230
5231/*
5232 * "confirm(message, buttons[, default [, type]])" function
5233 */
5234/*ARGSUSED*/
5235 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005236f_confirm(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005237 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005238 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239{
5240#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5241 char_u *message;
5242 char_u *buttons = NULL;
5243 char_u buf[NUMBUFLEN];
5244 char_u buf2[NUMBUFLEN];
5245 int def = 1;
5246 int type = VIM_GENERIC;
5247 int c;
5248
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005249 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005250 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005252 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005253 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005255 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005256 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 {
5258 /* avoid that TOUPPER_ASC calls get_var_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005259 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 switch (TOUPPER_ASC(c))
5261 {
5262 case 'E': type = VIM_ERROR; break;
5263 case 'Q': type = VIM_QUESTION; break;
5264 case 'I': type = VIM_INFO; break;
5265 case 'W': type = VIM_WARNING; break;
5266 case 'G': type = VIM_GENERIC; break;
5267 }
5268 }
5269 }
5270 }
5271
5272 if (buttons == NULL || *buttons == NUL)
5273 buttons = (char_u *)_("&Ok");
5274
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005275 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 def, NULL);
5277#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005278 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279#endif
5280}
5281
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005282/*
5283 * "copy()" function
5284 */
5285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005286f_copy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005288 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289{
5290 if (argvars[0].v_type == VAR_LIST)
5291 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005292 rettv->v_type = VAR_LIST;
5293 rettv->vval.v_list = list_copy(argvars[0].vval.v_list, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005294 }
5295 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005296 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005297}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298
5299/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005300 * "count()" function
5301 */
5302 static void
5303f_count(argvars, rettv)
5304 typeval *argvars;
5305 typeval *rettv;
5306{
5307 listitem *li;
5308 long n = 0;
5309 int ic = FALSE;
5310
5311 if (argvars[0].v_type != VAR_LIST)
5312 EMSG(_(e_listreq));
5313 else if (argvars[0].vval.v_list != NULL)
5314 {
5315 if (argvars[2].v_type != VAR_UNKNOWN)
5316 ic = get_tv_number(&argvars[2]);
5317
5318 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
5319 li = li->li_next)
5320 if (tv_equal(&li->li_tv, &argvars[1], ic))
5321 ++n;
5322 }
5323 rettv->vval.v_number = n;
5324}
5325
5326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
5328 *
5329 * Checks the existence of a cscope connection.
5330 */
5331/*ARGSUSED*/
5332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005333f_cscope_connection(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005334 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005335 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336{
5337#ifdef FEAT_CSCOPE
5338 int num = 0;
5339 char_u *dbpath = NULL;
5340 char_u *prepend = NULL;
5341 char_u buf[NUMBUFLEN];
5342
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343 if (argvars[0].v_type != VAR_UNKNOWN
5344 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005346 num = (int)get_tv_number(&argvars[0]);
5347 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005348 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005349 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 }
5351
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005352 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005354 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355#endif
5356}
5357
5358/*
5359 * "cursor(lnum, col)" function
5360 *
5361 * Moves the cursor to the specified line and column
5362 */
5363/*ARGSUSED*/
5364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005365f_cursor(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005366 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005367 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368{
5369 long line, col;
5370
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005371 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 if (line > 0)
5373 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005374 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 if (col > 0)
5376 curwin->w_cursor.col = col - 1;
5377#ifdef FEAT_VIRTUALEDIT
5378 curwin->w_cursor.coladd = 0;
5379#endif
5380
5381 /* Make sure the cursor is in a valid position. */
5382 check_cursor();
5383#ifdef FEAT_MBYTE
5384 /* Correct cursor for multi-byte character. */
5385 if (has_mbyte)
5386 mb_adjust_cursor();
5387#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005388
5389 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390}
5391
5392/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 */
5395 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005396f_deepcopy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005398 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005402 rettv->v_type = VAR_LIST;
5403 rettv->vval.v_list = list_copy(argvars[0].vval.v_list, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005406 copy_tv(&argvars[0], rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407}
5408
5409/*
5410 * "delete()" function
5411 */
5412 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005413f_delete(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416{
5417 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005418 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005420 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421}
5422
5423/*
5424 * "did_filetype()" function
5425 */
5426/*ARGSUSED*/
5427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005428f_did_filetype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005430 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431{
5432#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005433 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005435 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436#endif
5437}
5438
5439/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00005440 * "diff_filler()" function
5441 */
5442/*ARGSUSED*/
5443 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005444f_diff_filler(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005446 typeval *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00005447{
5448#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005449 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00005450#endif
5451}
5452
5453/*
5454 * "diff_hlID()" function
5455 */
5456/*ARGSUSED*/
5457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005458f_diff_hlID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005460 typeval *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00005461{
5462#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005463 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00005464 static linenr_T prev_lnum = 0;
5465 static int changedtick = 0;
5466 static int fnum = 0;
5467 static int change_start = 0;
5468 static int change_end = 0;
5469 static enum hlf_value hlID = 0;
5470 int filler_lines;
5471 int col;
5472
5473 if (lnum != prev_lnum
5474 || changedtick != curbuf->b_changedtick
5475 || fnum != curbuf->b_fnum)
5476 {
5477 /* New line, buffer, change: need to get the values. */
5478 filler_lines = diff_check(curwin, lnum);
5479 if (filler_lines < 0)
5480 {
5481 if (filler_lines == -1)
5482 {
5483 change_start = MAXCOL;
5484 change_end = -1;
5485 if (diff_find_change(curwin, lnum, &change_start, &change_end))
5486 hlID = HLF_ADD; /* added line */
5487 else
5488 hlID = HLF_CHD; /* changed line */
5489 }
5490 else
5491 hlID = HLF_ADD; /* added line */
5492 }
5493 else
5494 hlID = (enum hlf_value)0;
5495 prev_lnum = lnum;
5496 changedtick = curbuf->b_changedtick;
5497 fnum = curbuf->b_fnum;
5498 }
5499
5500 if (hlID == HLF_CHD || hlID == HLF_TXD)
5501 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005502 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00005503 if (col >= change_start && col <= change_end)
5504 hlID = HLF_TXD; /* changed text */
5505 else
5506 hlID = HLF_CHD; /* changed line */
5507 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005508 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00005509#endif
5510}
5511
5512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 * "escape({string}, {chars})" function
5514 */
5515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005516f_escape(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005517 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005518 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519{
5520 char_u buf[NUMBUFLEN];
5521
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005522 rettv->vval.v_string =
5523 vim_strsave_escaped(get_tv_string(&argvars[0]),
5524 get_tv_string_buf(&argvars[1], buf));
5525 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526}
5527
5528/*
5529 * "eventhandler()" function
5530 */
5531/*ARGSUSED*/
5532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533f_eventhandler(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005534 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005535 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005537 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538}
5539
5540/*
5541 * "executable()" function
5542 */
5543 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005544f_executable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005546 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005548 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549}
5550
5551/*
5552 * "exists()" function
5553 */
5554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555f_exists(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005557 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558{
5559 char_u *p;
5560 char_u *name;
5561 int n = FALSE;
5562 int len = 0;
5563
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005564 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 if (*p == '$') /* environment variable */
5566 {
5567 /* first try "normal" environment variables (fast) */
5568 if (mch_getenv(p + 1) != NULL)
5569 n = TRUE;
5570 else
5571 {
5572 /* try expanding things like $VIM and ${HOME} */
5573 p = expand_env_save(p);
5574 if (p != NULL && *p != '$')
5575 n = TRUE;
5576 vim_free(p);
5577 }
5578 }
5579 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005580 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 else if (*p == '*') /* internal or user defined function */
5582 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005583 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 }
5585 else if (*p == ':')
5586 {
5587 n = cmd_exists(p + 1);
5588 }
5589 else if (*p == '#')
5590 {
5591#ifdef FEAT_AUTOCMD
5592 name = p + 1;
5593 p = vim_strchr(name, '#');
5594 if (p != NULL)
5595 n = au_exists(name, p, p + 1);
5596 else
5597 n = au_exists(name, name + STRLEN(name), NULL);
5598#endif
5599 }
5600 else /* internal variable */
5601 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 char_u *expr_start;
5603 char_u *expr_end;
5604 char_u *temp_string = NULL;
5605 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 name = p;
5607
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 /* Find the end of the name. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005609 s = find_name_end(name, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 if (expr_start != NULL)
5611 {
5612 temp_string = make_expanded_name(name, expr_start, expr_end, s);
5613 if (temp_string != NULL)
5614 {
5615 len = STRLEN(temp_string);
5616 name = temp_string;
5617 }
5618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 if (len == 0)
5620 len = get_id_len(&p);
5621 if (len != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005622 n = (get_var_tv(name, len, NULL) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 vim_free(temp_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 }
5626
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005627 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628}
5629
5630/*
5631 * "expand()" function
5632 */
5633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005634f_expand(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005635 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005636 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
5638 char_u *s;
5639 int len;
5640 char_u *errormsg;
5641 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
5642 expand_T xpc;
5643
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005644 rettv->v_type = VAR_STRING;
5645 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 if (*s == '%' || *s == '#' || *s == '<')
5647 {
5648 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005649 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 --emsg_off;
5651 }
5652 else
5653 {
5654 /* When the optional second argument is non-zero, don't remove matches
5655 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005656 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 flags |= WILD_KEEP_ALL;
5658 ExpandInit(&xpc);
5659 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005660 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 ExpandCleanup(&xpc);
5662 }
5663}
5664
5665/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005666 * "extend(list, list [, idx])" function
5667 */
5668 static void
5669f_extend(argvars, rettv)
5670 typeval *argvars;
5671 typeval *rettv;
5672{
5673 long before;
5674 long n;
5675 listitem *item;
5676 listvar *l1, *l2;
5677
5678 rettv->vval.v_number = 0;
5679 if (argvars[0].v_type != VAR_LIST || argvars[1].v_type != VAR_LIST)
5680 {
5681 EMSG(_(e_listreq));
5682 return;
5683 }
5684 l1 = argvars[0].vval.v_list;
5685 l2 = argvars[1].vval.v_list;
5686 if (l1 != NULL && l2 != NULL)
5687 {
5688 if (argvars[2].v_type != VAR_UNKNOWN)
5689 {
5690 n = before = get_tv_number(&argvars[2]);
5691 item = list_find_ext(l1, &n);
5692 if (n != 0)
5693 {
5694 EMSGN(_(e_listidx), before);
5695 return;
5696 }
5697 }
5698 else
5699 item = NULL;
5700 list_extend(l1, l2, item);
5701
5702 ++l1->lv_refcount;
5703 copy_tv(&argvars[0], rettv);
5704 }
5705}
5706
5707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 * "filereadable()" function
5709 */
5710 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005711f_filereadable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005712 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005713 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714{
5715 FILE *fd;
5716 char_u *p;
5717 int n;
5718
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005719 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
5721 {
5722 n = TRUE;
5723 fclose(fd);
5724 }
5725 else
5726 n = FALSE;
5727
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005728 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729}
5730
5731/*
5732 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
5733 * rights to write into.
5734 */
5735 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005736f_filewritable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005737 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005738 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
5740 char_u *p;
5741 int retval = 0;
5742#if defined(UNIX) || defined(VMS)
5743 int perm = 0;
5744#endif
5745
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005746 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747#if defined(UNIX) || defined(VMS)
5748 perm = mch_getperm(p);
5749#endif
5750#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
5751 if (
5752# ifdef WIN3264
5753 mch_writable(p) &&
5754# else
5755# if defined(UNIX) || defined(VMS)
5756 (perm & 0222) &&
5757# endif
5758# endif
5759 mch_access((char *)p, W_OK) == 0
5760 )
5761#endif
5762 {
5763 ++retval;
5764 if (mch_isdir(p))
5765 ++retval;
5766 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005767 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768}
5769
Bram Moolenaar0d660222005-01-07 21:51:51 +00005770static void findfilendir __ARGS((typeval *argvars, typeval *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005771
5772 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00005773findfilendir(argvars, rettv, dir)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005774 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005775 typeval *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005776 int dir;
5777{
5778#ifdef FEAT_SEARCHPATH
5779 char_u *fname;
5780 char_u *fresult = NULL;
5781 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
5782 char_u *p;
5783 char_u pathbuf[NUMBUFLEN];
5784 int count = 1;
5785 int first = TRUE;
5786
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005787 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005788
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005789 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005790 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005791 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005792 if (*p != NUL)
5793 path = p;
5794
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005795 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005796 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005797 }
5798
5799 do
5800 {
5801 vim_free(fresult);
5802 fresult = find_file_in_path_option(first ? fname : NULL,
5803 first ? (int)STRLEN(fname) : 0,
5804 0, first, path, dir, NULL);
5805 first = FALSE;
5806 } while (--count > 0 && fresult != NULL);
5807
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005808 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005809#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005810 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005811#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005812 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00005813}
5814
5815/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00005816 * "finddir({fname}[, {path}[, {count}]])" function
5817 */
5818 static void
5819f_finddir(argvars, rettv)
5820 typeval *argvars;
5821 typeval *rettv;
5822{
5823 findfilendir(argvars, rettv, TRUE);
5824}
5825
5826/*
5827 * "findfile({fname}[, {path}[, {count}]])" function
5828 */
5829 static void
5830f_findfile(argvars, rettv)
5831 typeval *argvars;
5832 typeval *rettv;
5833{
5834 findfilendir(argvars, rettv, FALSE);
5835}
5836
5837/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 * "fnamemodify({fname}, {mods})" function
5839 */
5840 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005841f_fnamemodify(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005842 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005843 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844{
5845 char_u *fname;
5846 char_u *mods;
5847 int usedlen = 0;
5848 int len;
5849 char_u *fbuf = NULL;
5850 char_u buf[NUMBUFLEN];
5851
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005852 fname = get_tv_string(&argvars[0]);
5853 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 len = (int)STRLEN(fname);
5855
5856 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
5857
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005858 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005860 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005862 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 vim_free(fbuf);
5864}
5865
Bram Moolenaar0d660222005-01-07 21:51:51 +00005866static void foldclosed_both __ARGS((typeval *argvars, typeval *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867
5868/*
5869 * "foldclosed()" function
5870 */
5871 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005872foldclosed_both(argvars, rettv, end)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005874 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 int end;
5876{
5877#ifdef FEAT_FOLDING
5878 linenr_T lnum;
5879 linenr_T first, last;
5880
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005881 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
5883 {
5884 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
5885 {
5886 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005887 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005889 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 return;
5891 }
5892 }
5893#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005894 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895}
5896
5897/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00005898 * "foldclosed()" function
5899 */
5900 static void
5901f_foldclosed(argvars, rettv)
5902 typeval *argvars;
5903 typeval *rettv;
5904{
5905 foldclosed_both(argvars, rettv, FALSE);
5906}
5907
5908/*
5909 * "foldclosedend()" function
5910 */
5911 static void
5912f_foldclosedend(argvars, rettv)
5913 typeval *argvars;
5914 typeval *rettv;
5915{
5916 foldclosed_both(argvars, rettv, TRUE);
5917}
5918
5919/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 * "foldlevel()" function
5921 */
5922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005923f_foldlevel(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005924 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005925 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926{
5927#ifdef FEAT_FOLDING
5928 linenr_T lnum;
5929
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005930 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005932 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 else
5934#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005935 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936}
5937
5938/*
5939 * "foldtext()" function
5940 */
5941/*ARGSUSED*/
5942 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005943f_foldtext(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005945 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946{
5947#ifdef FEAT_FOLDING
5948 linenr_T lnum;
5949 char_u *s;
5950 char_u *r;
5951 int len;
5952 char *txt;
5953#endif
5954
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005955 rettv->v_type = VAR_STRING;
5956 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957#ifdef FEAT_FOLDING
5958 if ((linenr_T)vimvars[VV_FOLDSTART].val > 0
5959 && (linenr_T)vimvars[VV_FOLDEND].val <= curbuf->b_ml.ml_line_count
5960 && vimvars[VV_FOLDDASHES].val != NULL)
5961 {
5962 /* Find first non-empty line in the fold. */
5963 lnum = (linenr_T)vimvars[VV_FOLDSTART].val;
5964 while (lnum < (linenr_T)vimvars[VV_FOLDEND].val)
5965 {
5966 if (!linewhite(lnum))
5967 break;
5968 ++lnum;
5969 }
5970
5971 /* Find interesting text in this line. */
5972 s = skipwhite(ml_get(lnum));
5973 /* skip C comment-start */
5974 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005975 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00005977 if (*skipwhite(s) == NUL
5978 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].val)
5979 {
5980 s = skipwhite(ml_get(lnum + 1));
5981 if (*s == '*')
5982 s = skipwhite(s + 1);
5983 }
5984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 txt = _("+-%s%3ld lines: ");
5986 r = alloc((unsigned)(STRLEN(txt)
5987 + STRLEN(vimvars[VV_FOLDDASHES].val) /* for %s */
5988 + 20 /* for %3ld */
5989 + STRLEN(s))); /* concatenated */
5990 if (r != NULL)
5991 {
5992 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].val,
5993 (long)((linenr_T)vimvars[VV_FOLDEND].val
5994 - (linenr_T)vimvars[VV_FOLDSTART].val + 1));
5995 len = (int)STRLEN(r);
5996 STRCAT(r, s);
5997 /* remove 'foldmarker' and 'commentstring' */
5998 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005999 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 }
6001 }
6002#endif
6003}
6004
6005/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006006 * "foldtextresult(lnum)" function
6007 */
6008/*ARGSUSED*/
6009 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006010f_foldtextresult(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006011 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006012 typeval *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006013{
6014#ifdef FEAT_FOLDING
6015 linenr_T lnum;
6016 char_u *text;
6017 char_u buf[51];
6018 foldinfo_T foldinfo;
6019 int fold_count;
6020#endif
6021
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006022 rettv->v_type = VAR_STRING;
6023 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006024#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006025 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006026 fold_count = foldedCount(curwin, lnum, &foldinfo);
6027 if (fold_count > 0)
6028 {
6029 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
6030 &foldinfo, buf);
6031 if (text == buf)
6032 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006033 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006034 }
6035#endif
6036}
6037
6038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 * "foreground()" function
6040 */
6041/*ARGSUSED*/
6042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006043f_foreground(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006045 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006047 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048#ifdef FEAT_GUI
6049 if (gui.in_use)
6050 gui_mch_set_foreground();
6051#else
6052# ifdef WIN32
6053 win32_set_foreground();
6054# endif
6055#endif
6056}
6057
6058/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059 * "function()" function
6060 */
6061/*ARGSUSED*/
6062 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006063f_function(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006065 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066{
6067 char_u *s;
6068
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006069 s = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070 if (s == NULL || *s == NUL || isdigit(*s))
6071 EMSG2(_(e_invarg2), s);
6072 else if (!function_exists(s))
6073 EMSG2(_("E999: Unknown function: %s"), s);
6074 else
6075 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006076 rettv->vval.v_string = vim_strsave(s);
6077 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006078 }
6079}
6080
6081/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006082 * "get()" function
6083 */
6084 static void
6085f_get(argvars, rettv)
6086 typeval *argvars;
6087 typeval *rettv;
6088{
6089 listitem *item;
6090 listvar *l;
6091
6092 if (argvars[0].v_type != VAR_LIST)
6093 EMSG2(_(e_listarg), "get()");
6094 else if ((l = argvars[0].vval.v_list) != NULL)
6095 {
6096 item = list_find(l, get_tv_number(&argvars[1]));
6097 if (item == NULL)
6098 {
6099 if (argvars[2].v_type == VAR_UNKNOWN)
6100 rettv->vval.v_number = 0;
6101 else
6102 copy_tv(&argvars[2], rettv);
6103 }
6104 else
6105 copy_tv(&item->li_tv, rettv);
6106 }
6107}
6108
6109/*
6110 * "getbufvar()" function
6111 */
6112 static void
6113f_getbufvar(argvars, rettv)
6114 typeval *argvars;
6115 typeval *rettv;
6116{
6117 buf_T *buf;
6118 buf_T *save_curbuf;
6119 char_u *varname;
6120 VAR v;
6121
6122 ++emsg_off;
6123 buf = get_buf_tv(&argvars[0]);
6124 varname = get_tv_string(&argvars[1]);
6125
6126 rettv->v_type = VAR_STRING;
6127 rettv->vval.v_string = NULL;
6128
6129 if (buf != NULL && varname != NULL)
6130 {
6131 if (*varname == '&') /* buffer-local-option */
6132 {
6133 /* set curbuf to be our buf, temporarily */
6134 save_curbuf = curbuf;
6135 curbuf = buf;
6136
6137 get_option_tv(&varname, rettv, TRUE);
6138
6139 /* restore previous notion of curbuf */
6140 curbuf = save_curbuf;
6141 }
6142 else
6143 {
6144 /* look up the variable */
6145 v = find_var_in_ga(&buf->b_vars, varname);
6146 if (v != NULL)
6147 copy_tv(&v->tv, rettv);
6148 }
6149 }
6150
6151 --emsg_off;
6152}
6153
6154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 * "getchar()" function
6156 */
6157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006158f_getchar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006159 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006160 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161{
6162 varnumber_T n;
6163
6164 ++no_mapping;
6165 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006166 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 /* getchar(): blocking wait. */
6168 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006169 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 /* getchar(1): only check if char avail */
6171 n = vpeekc();
6172 else if (vpeekc() == NUL)
6173 /* getchar(0) and no char avail: return zero */
6174 n = 0;
6175 else
6176 /* getchar(0) and char avail: return char */
6177 n = safe_vgetc();
6178 --no_mapping;
6179 --allow_keys;
6180
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006181 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 if (IS_SPECIAL(n) || mod_mask != 0)
6183 {
6184 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
6185 int i = 0;
6186
6187 /* Turn a special key into three bytes, plus modifier. */
6188 if (mod_mask != 0)
6189 {
6190 temp[i++] = K_SPECIAL;
6191 temp[i++] = KS_MODIFIER;
6192 temp[i++] = mod_mask;
6193 }
6194 if (IS_SPECIAL(n))
6195 {
6196 temp[i++] = K_SPECIAL;
6197 temp[i++] = K_SECOND(n);
6198 temp[i++] = K_THIRD(n);
6199 }
6200#ifdef FEAT_MBYTE
6201 else if (has_mbyte)
6202 i += (*mb_char2bytes)(n, temp + i);
6203#endif
6204 else
6205 temp[i++] = n;
6206 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006207 rettv->v_type = VAR_STRING;
6208 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 }
6210}
6211
6212/*
6213 * "getcharmod()" function
6214 */
6215/*ARGSUSED*/
6216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006217f_getcharmod(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006218 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006219 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006221 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222}
6223
6224/*
6225 * "getcmdline()" function
6226 */
6227/*ARGSUSED*/
6228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006229f_getcmdline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006230 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006231 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006233 rettv->v_type = VAR_STRING;
6234 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235}
6236
6237/*
6238 * "getcmdpos()" function
6239 */
6240/*ARGSUSED*/
6241 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006242f_getcmdpos(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006243 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006244 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006246 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247}
6248
6249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 * "getcwd()" function
6251 */
6252/*ARGSUSED*/
6253 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006254f_getcwd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006255 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006256 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257{
6258 char_u cwd[MAXPATHL];
6259
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006260 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006262 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 else
6264 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006265 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006267 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268#endif
6269 }
6270}
6271
6272/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006273 * "getfontname()" function
6274 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006275/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006276 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006277f_getfontname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006278 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006279 typeval *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006280{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006281 rettv->v_type = VAR_STRING;
6282 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006283#ifdef FEAT_GUI
6284 if (gui.in_use)
6285 {
6286 GuiFont font;
6287 char_u *name = NULL;
6288
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006289 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006290 {
6291 /* Get the "Normal" font. Either the name saved by
6292 * hl_set_font_name() or from the font ID. */
6293 font = gui.norm_font;
6294 name = hl_get_font_name();
6295 }
6296 else
6297 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006298 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006299 if (STRCMP(name, "*") == 0) /* don't use font dialog */
6300 return;
6301 font = gui_mch_get_font(name, FALSE);
6302 if (font == NOFONT)
6303 return; /* Invalid font name, return empty string. */
6304 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006305 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006306 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006307 gui_mch_free_font(font);
6308 }
6309#endif
6310}
6311
6312/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006313 * "getfperm({fname})" function
6314 */
6315 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006316f_getfperm(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006317 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006318 typeval *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006319{
6320 char_u *fname;
6321 struct stat st;
6322 char_u *perm = NULL;
6323 char_u flags[] = "rwx";
6324 int i;
6325
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006326 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006327
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006328 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006329 if (mch_stat((char *)fname, &st) >= 0)
6330 {
6331 perm = vim_strsave((char_u *)"---------");
6332 if (perm != NULL)
6333 {
6334 for (i = 0; i < 9; i++)
6335 {
6336 if (st.st_mode & (1 << (8 - i)))
6337 perm[i] = flags[i % 3];
6338 }
6339 }
6340 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006341 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006342}
6343
6344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 * "getfsize({fname})" function
6346 */
6347 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006348f_getfsize(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006349 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006350 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351{
6352 char_u *fname;
6353 struct stat st;
6354
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006355 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006357 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358
6359 if (mch_stat((char *)fname, &st) >= 0)
6360 {
6361 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006362 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006364 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 }
6366 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006367 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368}
6369
6370/*
6371 * "getftime({fname})" function
6372 */
6373 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006374f_getftime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006375 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006376 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377{
6378 char_u *fname;
6379 struct stat st;
6380
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006381 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382
6383 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006384 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006386 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387}
6388
6389/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006390 * "getftype({fname})" function
6391 */
6392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006393f_getftype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006394 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006395 typeval *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006396{
6397 char_u *fname;
6398 struct stat st;
6399 char_u *type = NULL;
6400 char *t;
6401
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006402 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006403
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006404 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006405 if (mch_lstat((char *)fname, &st) >= 0)
6406 {
6407#ifdef S_ISREG
6408 if (S_ISREG(st.st_mode))
6409 t = "file";
6410 else if (S_ISDIR(st.st_mode))
6411 t = "dir";
6412# ifdef S_ISLNK
6413 else if (S_ISLNK(st.st_mode))
6414 t = "link";
6415# endif
6416# ifdef S_ISBLK
6417 else if (S_ISBLK(st.st_mode))
6418 t = "bdev";
6419# endif
6420# ifdef S_ISCHR
6421 else if (S_ISCHR(st.st_mode))
6422 t = "cdev";
6423# endif
6424# ifdef S_ISFIFO
6425 else if (S_ISFIFO(st.st_mode))
6426 t = "fifo";
6427# endif
6428# ifdef S_ISSOCK
6429 else if (S_ISSOCK(st.st_mode))
6430 t = "fifo";
6431# endif
6432 else
6433 t = "other";
6434#else
6435# ifdef S_IFMT
6436 switch (st.st_mode & S_IFMT)
6437 {
6438 case S_IFREG: t = "file"; break;
6439 case S_IFDIR: t = "dir"; break;
6440# ifdef S_IFLNK
6441 case S_IFLNK: t = "link"; break;
6442# endif
6443# ifdef S_IFBLK
6444 case S_IFBLK: t = "bdev"; break;
6445# endif
6446# ifdef S_IFCHR
6447 case S_IFCHR: t = "cdev"; break;
6448# endif
6449# ifdef S_IFIFO
6450 case S_IFIFO: t = "fifo"; break;
6451# endif
6452# ifdef S_IFSOCK
6453 case S_IFSOCK: t = "socket"; break;
6454# endif
6455 default: t = "other";
6456 }
6457# else
6458 if (mch_isdir(fname))
6459 t = "dir";
6460 else
6461 t = "file";
6462# endif
6463#endif
6464 type = vim_strsave((char_u *)t);
6465 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006466 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006467}
6468
6469/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006470 * "getline(lnum)" function
6471 */
6472 static void
6473f_getline(argvars, rettv)
6474 typeval *argvars;
6475 typeval *rettv;
6476{
6477 linenr_T lnum;
6478 linenr_T end;
6479 char_u *p;
6480 listvar *l;
6481 listitem *li;
6482
6483 lnum = get_tv_lnum(argvars);
6484
6485 if (argvars[1].v_type == VAR_UNKNOWN)
6486 {
6487 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6488 p = ml_get(lnum);
6489 else
6490 p = (char_u *)"";
6491
6492 rettv->v_type = VAR_STRING;
6493 rettv->vval.v_string = vim_strsave(p);
6494 }
6495 else
6496 {
6497 end = get_tv_lnum(&argvars[1]);
6498 if (end < lnum)
6499 {
6500 EMSG(_(e_invrange));
6501 rettv->vval.v_number = 0;
6502 }
6503 else
6504 {
6505 l = list_alloc();
6506 if (l != NULL)
6507 {
6508 if (lnum < 1)
6509 lnum = 1;
6510 if (end > curbuf->b_ml.ml_line_count)
6511 end = curbuf->b_ml.ml_line_count;
6512 while (lnum <= end)
6513 {
6514 li = listitem_alloc();
6515 if (li == NULL)
6516 break;
6517 list_append(l, li);
6518 li->li_tv.v_type = VAR_STRING;
6519 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
6520 }
6521 rettv->vval.v_list = l;
6522 rettv->v_type = VAR_LIST;
6523 ++l->lv_refcount;
6524 }
6525 }
6526 }
6527}
6528
6529/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 * "getreg()" function
6531 */
6532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006533f_getreg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006534 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006535 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536{
6537 char_u *strregname;
6538 int regname;
6539
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006540 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006541 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 else
6543 strregname = vimvars[VV_REG].val;
6544 regname = (strregname == NULL ? '"' : *strregname);
6545 if (regname == 0)
6546 regname = '"';
6547
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006548 rettv->v_type = VAR_STRING;
6549 rettv->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550}
6551
6552/*
6553 * "getregtype()" function
6554 */
6555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006556f_getregtype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006558 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559{
6560 char_u *strregname;
6561 int regname;
6562 char_u buf[NUMBUFLEN + 2];
6563 long reglen = 0;
6564
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006565 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006566 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 else
6568 /* Default to v:register */
6569 strregname = vimvars[VV_REG].val;
6570
6571 regname = (strregname == NULL ? '"' : *strregname);
6572 if (regname == 0)
6573 regname = '"';
6574
6575 buf[0] = NUL;
6576 buf[1] = NUL;
6577 switch (get_reg_type(regname, &reglen))
6578 {
6579 case MLINE: buf[0] = 'V'; break;
6580 case MCHAR: buf[0] = 'v'; break;
6581#ifdef FEAT_VISUAL
6582 case MBLOCK:
6583 buf[0] = Ctrl_V;
6584 sprintf((char *)buf + 1, "%ld", reglen + 1);
6585 break;
6586#endif
6587 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006588 rettv->v_type = VAR_STRING;
6589 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590}
6591
6592/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 * "getwinposx()" function
6594 */
6595/*ARGSUSED*/
6596 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006597f_getwinposx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006599 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006601 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602#ifdef FEAT_GUI
6603 if (gui.in_use)
6604 {
6605 int x, y;
6606
6607 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006608 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 }
6610#endif
6611}
6612
6613/*
6614 * "getwinposy()" function
6615 */
6616/*ARGSUSED*/
6617 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006618f_getwinposy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006620 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006622 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623#ifdef FEAT_GUI
6624 if (gui.in_use)
6625 {
6626 int x, y;
6627
6628 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006629 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 }
6631#endif
6632}
6633
6634/*
6635 * "getwinvar()" function
6636 */
6637 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006638f_getwinvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006639 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006640 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641{
6642 win_T *win, *oldcurwin;
6643 char_u *varname;
6644 VAR v;
6645
6646 ++emsg_off;
6647 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006648 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006650 rettv->v_type = VAR_STRING;
6651 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652
6653 if (win != NULL && varname != NULL)
6654 {
6655 if (*varname == '&') /* window-local-option */
6656 {
6657 /* set curwin to be our win, temporarily */
6658 oldcurwin = curwin;
6659 curwin = win;
6660
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006661 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662
6663 /* restore previous notion of curwin */
6664 curwin = oldcurwin;
6665 }
6666 else
6667 {
6668 /* look up the variable */
6669 v = find_var_in_ga(&win->w_vars, varname);
6670 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006671 copy_tv(&v->tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 }
6673 }
6674
6675 --emsg_off;
6676}
6677
6678/*
6679 * "glob()" function
6680 */
6681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006682f_glob(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006683 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006684 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685{
6686 expand_T xpc;
6687
6688 ExpandInit(&xpc);
6689 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006690 rettv->v_type = VAR_STRING;
6691 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
6693 ExpandCleanup(&xpc);
6694}
6695
6696/*
6697 * "globpath()" function
6698 */
6699 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006700f_globpath(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006701 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006702 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703{
6704 char_u buf1[NUMBUFLEN];
6705
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006706 rettv->v_type = VAR_STRING;
6707 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
6708 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709}
6710
6711/*
6712 * "has()" function
6713 */
6714 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006715f_has(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006716 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006717 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718{
6719 int i;
6720 char_u *name;
6721 int n = FALSE;
6722 static char *(has_list[]) =
6723 {
6724#ifdef AMIGA
6725 "amiga",
6726# ifdef FEAT_ARP
6727 "arp",
6728# endif
6729#endif
6730#ifdef __BEOS__
6731 "beos",
6732#endif
6733#ifdef MSDOS
6734# ifdef DJGPP
6735 "dos32",
6736# else
6737 "dos16",
6738# endif
6739#endif
6740#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
6741 "mac",
6742#endif
6743#if defined(MACOS_X_UNIX)
6744 "macunix",
6745#endif
6746#ifdef OS2
6747 "os2",
6748#endif
6749#ifdef __QNX__
6750 "qnx",
6751#endif
6752#ifdef RISCOS
6753 "riscos",
6754#endif
6755#ifdef UNIX
6756 "unix",
6757#endif
6758#ifdef VMS
6759 "vms",
6760#endif
6761#ifdef WIN16
6762 "win16",
6763#endif
6764#ifdef WIN32
6765 "win32",
6766#endif
6767#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
6768 "win32unix",
6769#endif
6770#ifdef WIN64
6771 "win64",
6772#endif
6773#ifdef EBCDIC
6774 "ebcdic",
6775#endif
6776#ifndef CASE_INSENSITIVE_FILENAME
6777 "fname_case",
6778#endif
6779#ifdef FEAT_ARABIC
6780 "arabic",
6781#endif
6782#ifdef FEAT_AUTOCMD
6783 "autocmd",
6784#endif
6785#ifdef FEAT_BEVAL
6786 "balloon_eval",
6787#endif
6788#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
6789 "builtin_terms",
6790# ifdef ALL_BUILTIN_TCAPS
6791 "all_builtin_terms",
6792# endif
6793#endif
6794#ifdef FEAT_BYTEOFF
6795 "byte_offset",
6796#endif
6797#ifdef FEAT_CINDENT
6798 "cindent",
6799#endif
6800#ifdef FEAT_CLIENTSERVER
6801 "clientserver",
6802#endif
6803#ifdef FEAT_CLIPBOARD
6804 "clipboard",
6805#endif
6806#ifdef FEAT_CMDL_COMPL
6807 "cmdline_compl",
6808#endif
6809#ifdef FEAT_CMDHIST
6810 "cmdline_hist",
6811#endif
6812#ifdef FEAT_COMMENTS
6813 "comments",
6814#endif
6815#ifdef FEAT_CRYPT
6816 "cryptv",
6817#endif
6818#ifdef FEAT_CSCOPE
6819 "cscope",
6820#endif
6821#ifdef DEBUG
6822 "debug",
6823#endif
6824#ifdef FEAT_CON_DIALOG
6825 "dialog_con",
6826#endif
6827#ifdef FEAT_GUI_DIALOG
6828 "dialog_gui",
6829#endif
6830#ifdef FEAT_DIFF
6831 "diff",
6832#endif
6833#ifdef FEAT_DIGRAPHS
6834 "digraphs",
6835#endif
6836#ifdef FEAT_DND
6837 "dnd",
6838#endif
6839#ifdef FEAT_EMACS_TAGS
6840 "emacs_tags",
6841#endif
6842 "eval", /* always present, of course! */
6843#ifdef FEAT_EX_EXTRA
6844 "ex_extra",
6845#endif
6846#ifdef FEAT_SEARCH_EXTRA
6847 "extra_search",
6848#endif
6849#ifdef FEAT_FKMAP
6850 "farsi",
6851#endif
6852#ifdef FEAT_SEARCHPATH
6853 "file_in_path",
6854#endif
6855#ifdef FEAT_FIND_ID
6856 "find_in_path",
6857#endif
6858#ifdef FEAT_FOLDING
6859 "folding",
6860#endif
6861#ifdef FEAT_FOOTER
6862 "footer",
6863#endif
6864#if !defined(USE_SYSTEM) && defined(UNIX)
6865 "fork",
6866#endif
6867#ifdef FEAT_GETTEXT
6868 "gettext",
6869#endif
6870#ifdef FEAT_GUI
6871 "gui",
6872#endif
6873#ifdef FEAT_GUI_ATHENA
6874# ifdef FEAT_GUI_NEXTAW
6875 "gui_neXtaw",
6876# else
6877 "gui_athena",
6878# endif
6879#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00006880#ifdef FEAT_GUI_KDE
6881 "gui_kde",
6882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883#ifdef FEAT_GUI_GTK
6884 "gui_gtk",
6885# ifdef HAVE_GTK2
6886 "gui_gtk2",
6887# endif
6888#endif
6889#ifdef FEAT_GUI_MAC
6890 "gui_mac",
6891#endif
6892#ifdef FEAT_GUI_MOTIF
6893 "gui_motif",
6894#endif
6895#ifdef FEAT_GUI_PHOTON
6896 "gui_photon",
6897#endif
6898#ifdef FEAT_GUI_W16
6899 "gui_win16",
6900#endif
6901#ifdef FEAT_GUI_W32
6902 "gui_win32",
6903#endif
6904#ifdef FEAT_HANGULIN
6905 "hangul_input",
6906#endif
6907#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
6908 "iconv",
6909#endif
6910#ifdef FEAT_INS_EXPAND
6911 "insert_expand",
6912#endif
6913#ifdef FEAT_JUMPLIST
6914 "jumplist",
6915#endif
6916#ifdef FEAT_KEYMAP
6917 "keymap",
6918#endif
6919#ifdef FEAT_LANGMAP
6920 "langmap",
6921#endif
6922#ifdef FEAT_LIBCALL
6923 "libcall",
6924#endif
6925#ifdef FEAT_LINEBREAK
6926 "linebreak",
6927#endif
6928#ifdef FEAT_LISP
6929 "lispindent",
6930#endif
6931#ifdef FEAT_LISTCMDS
6932 "listcmds",
6933#endif
6934#ifdef FEAT_LOCALMAP
6935 "localmap",
6936#endif
6937#ifdef FEAT_MENU
6938 "menu",
6939#endif
6940#ifdef FEAT_SESSION
6941 "mksession",
6942#endif
6943#ifdef FEAT_MODIFY_FNAME
6944 "modify_fname",
6945#endif
6946#ifdef FEAT_MOUSE
6947 "mouse",
6948#endif
6949#ifdef FEAT_MOUSESHAPE
6950 "mouseshape",
6951#endif
6952#if defined(UNIX) || defined(VMS)
6953# ifdef FEAT_MOUSE_DEC
6954 "mouse_dec",
6955# endif
6956# ifdef FEAT_MOUSE_GPM
6957 "mouse_gpm",
6958# endif
6959# ifdef FEAT_MOUSE_JSB
6960 "mouse_jsbterm",
6961# endif
6962# ifdef FEAT_MOUSE_NET
6963 "mouse_netterm",
6964# endif
6965# ifdef FEAT_MOUSE_PTERM
6966 "mouse_pterm",
6967# endif
6968# ifdef FEAT_MOUSE_XTERM
6969 "mouse_xterm",
6970# endif
6971#endif
6972#ifdef FEAT_MBYTE
6973 "multi_byte",
6974#endif
6975#ifdef FEAT_MBYTE_IME
6976 "multi_byte_ime",
6977#endif
6978#ifdef FEAT_MULTI_LANG
6979 "multi_lang",
6980#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00006981#ifdef FEAT_MZSCHEME
6982 "mzscheme",
6983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984#ifdef FEAT_OLE
6985 "ole",
6986#endif
6987#ifdef FEAT_OSFILETYPE
6988 "osfiletype",
6989#endif
6990#ifdef FEAT_PATH_EXTRA
6991 "path_extra",
6992#endif
6993#ifdef FEAT_PERL
6994#ifndef DYNAMIC_PERL
6995 "perl",
6996#endif
6997#endif
6998#ifdef FEAT_PYTHON
6999#ifndef DYNAMIC_PYTHON
7000 "python",
7001#endif
7002#endif
7003#ifdef FEAT_POSTSCRIPT
7004 "postscript",
7005#endif
7006#ifdef FEAT_PRINTER
7007 "printer",
7008#endif
7009#ifdef FEAT_QUICKFIX
7010 "quickfix",
7011#endif
7012#ifdef FEAT_RIGHTLEFT
7013 "rightleft",
7014#endif
7015#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
7016 "ruby",
7017#endif
7018#ifdef FEAT_SCROLLBIND
7019 "scrollbind",
7020#endif
7021#ifdef FEAT_CMDL_INFO
7022 "showcmd",
7023 "cmdline_info",
7024#endif
7025#ifdef FEAT_SIGNS
7026 "signs",
7027#endif
7028#ifdef FEAT_SMARTINDENT
7029 "smartindent",
7030#endif
7031#ifdef FEAT_SNIFF
7032 "sniff",
7033#endif
7034#ifdef FEAT_STL_OPT
7035 "statusline",
7036#endif
7037#ifdef FEAT_SUN_WORKSHOP
7038 "sun_workshop",
7039#endif
7040#ifdef FEAT_NETBEANS_INTG
7041 "netbeans_intg",
7042#endif
7043#ifdef FEAT_SYN_HL
7044 "syntax",
7045#endif
7046#if defined(USE_SYSTEM) || !defined(UNIX)
7047 "system",
7048#endif
7049#ifdef FEAT_TAG_BINS
7050 "tag_binary",
7051#endif
7052#ifdef FEAT_TAG_OLDSTATIC
7053 "tag_old_static",
7054#endif
7055#ifdef FEAT_TAG_ANYWHITE
7056 "tag_any_white",
7057#endif
7058#ifdef FEAT_TCL
7059# ifndef DYNAMIC_TCL
7060 "tcl",
7061# endif
7062#endif
7063#ifdef TERMINFO
7064 "terminfo",
7065#endif
7066#ifdef FEAT_TERMRESPONSE
7067 "termresponse",
7068#endif
7069#ifdef FEAT_TEXTOBJ
7070 "textobjects",
7071#endif
7072#ifdef HAVE_TGETENT
7073 "tgetent",
7074#endif
7075#ifdef FEAT_TITLE
7076 "title",
7077#endif
7078#ifdef FEAT_TOOLBAR
7079 "toolbar",
7080#endif
7081#ifdef FEAT_USR_CMDS
7082 "user-commands", /* was accidentally included in 5.4 */
7083 "user_commands",
7084#endif
7085#ifdef FEAT_VIMINFO
7086 "viminfo",
7087#endif
7088#ifdef FEAT_VERTSPLIT
7089 "vertsplit",
7090#endif
7091#ifdef FEAT_VIRTUALEDIT
7092 "virtualedit",
7093#endif
7094#ifdef FEAT_VISUAL
7095 "visual",
7096#endif
7097#ifdef FEAT_VISUALEXTRA
7098 "visualextra",
7099#endif
7100#ifdef FEAT_VREPLACE
7101 "vreplace",
7102#endif
7103#ifdef FEAT_WILDIGN
7104 "wildignore",
7105#endif
7106#ifdef FEAT_WILDMENU
7107 "wildmenu",
7108#endif
7109#ifdef FEAT_WINDOWS
7110 "windows",
7111#endif
7112#ifdef FEAT_WAK
7113 "winaltkeys",
7114#endif
7115#ifdef FEAT_WRITEBACKUP
7116 "writebackup",
7117#endif
7118#ifdef FEAT_XIM
7119 "xim",
7120#endif
7121#ifdef FEAT_XFONTSET
7122 "xfontset",
7123#endif
7124#ifdef USE_XSMP
7125 "xsmp",
7126#endif
7127#ifdef USE_XSMP_INTERACT
7128 "xsmp_interact",
7129#endif
7130#ifdef FEAT_XCLIPBOARD
7131 "xterm_clipboard",
7132#endif
7133#ifdef FEAT_XTERM_SAVE
7134 "xterm_save",
7135#endif
7136#if defined(UNIX) && defined(FEAT_X11)
7137 "X11",
7138#endif
7139 NULL
7140 };
7141
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007142 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 for (i = 0; has_list[i] != NULL; ++i)
7144 if (STRICMP(name, has_list[i]) == 0)
7145 {
7146 n = TRUE;
7147 break;
7148 }
7149
7150 if (n == FALSE)
7151 {
7152 if (STRNICMP(name, "patch", 5) == 0)
7153 n = has_patch(atoi((char *)name + 5));
7154 else if (STRICMP(name, "vim_starting") == 0)
7155 n = (starting != 0);
7156#ifdef DYNAMIC_TCL
7157 else if (STRICMP(name, "tcl") == 0)
7158 n = tcl_enabled(FALSE);
7159#endif
7160#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
7161 else if (STRICMP(name, "iconv") == 0)
7162 n = iconv_enabled(FALSE);
7163#endif
7164#ifdef DYNAMIC_RUBY
7165 else if (STRICMP(name, "ruby") == 0)
7166 n = ruby_enabled(FALSE);
7167#endif
7168#ifdef DYNAMIC_PYTHON
7169 else if (STRICMP(name, "python") == 0)
7170 n = python_enabled(FALSE);
7171#endif
7172#ifdef DYNAMIC_PERL
7173 else if (STRICMP(name, "perl") == 0)
7174 n = perl_enabled(FALSE);
7175#endif
7176#ifdef FEAT_GUI
7177 else if (STRICMP(name, "gui_running") == 0)
7178 n = (gui.in_use || gui.starting);
7179# ifdef FEAT_GUI_W32
7180 else if (STRICMP(name, "gui_win32s") == 0)
7181 n = gui_is_win32s();
7182# endif
7183# ifdef FEAT_BROWSE
7184 else if (STRICMP(name, "browse") == 0)
7185 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
7186# endif
7187#endif
7188#ifdef FEAT_SYN_HL
7189 else if (STRICMP(name, "syntax_items") == 0)
7190 n = syntax_present(curbuf);
7191#endif
7192#if defined(WIN3264)
7193 else if (STRICMP(name, "win95") == 0)
7194 n = mch_windows95();
7195#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00007196#ifdef FEAT_NETBEANS_INTG
7197 else if (STRICMP(name, "netbeans_enabled") == 0)
7198 n = usingNetbeans;
7199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 }
7201
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007202 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203}
7204
7205/*
7206 * "hasmapto()" function
7207 */
7208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007209f_hasmapto(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007210 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007211 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212{
7213 char_u *name;
7214 char_u *mode;
7215 char_u buf[NUMBUFLEN];
7216
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007217 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007218 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 mode = (char_u *)"nvo";
7220 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007221 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222
7223 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007224 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007226 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227}
7228
7229/*
7230 * "histadd()" function
7231 */
7232/*ARGSUSED*/
7233 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007234f_histadd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007235 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007236 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237{
7238#ifdef FEAT_CMDHIST
7239 int histype;
7240 char_u *str;
7241 char_u buf[NUMBUFLEN];
7242#endif
7243
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007244 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 if (check_restricted() || check_secure())
7246 return;
7247#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007248 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 if (histype >= 0)
7250 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007251 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 if (*str != NUL)
7253 {
7254 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007255 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 return;
7257 }
7258 }
7259#endif
7260}
7261
7262/*
7263 * "histdel()" function
7264 */
7265/*ARGSUSED*/
7266 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007267f_histdel(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007268 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007269 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270{
7271#ifdef FEAT_CMDHIST
7272 int n;
7273 char_u buf[NUMBUFLEN];
7274
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007275 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007277 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007278 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007280 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
7281 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 else
7283 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007284 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
7285 get_tv_string_buf(&argvars[1], buf));
7286 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007288 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289#endif
7290}
7291
7292/*
7293 * "histget()" function
7294 */
7295/*ARGSUSED*/
7296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007297f_histget(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007298 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007299 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300{
7301#ifdef FEAT_CMDHIST
7302 int type;
7303 int idx;
7304
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007305 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007306 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 idx = get_history_idx(type);
7308 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007309 idx = (int)get_tv_number(&argvars[1]);
7310 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007312 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007314 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315}
7316
7317/*
7318 * "histnr()" function
7319 */
7320/*ARGSUSED*/
7321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007322f_histnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007323 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007324 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325{
7326 int i;
7327
7328#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007329 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 if (i >= HIST_CMD && i < HIST_COUNT)
7331 i = get_history_idx(i);
7332 else
7333#endif
7334 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007335 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336}
7337
7338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 * "highlightID(name)" function
7340 */
7341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007342f_hlID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007343 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007344 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007346 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347}
7348
7349/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007350 * "highlight_exists()" function
7351 */
7352 static void
7353f_hlexists(argvars, rettv)
7354 typeval *argvars;
7355 typeval *rettv;
7356{
7357 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
7358}
7359
7360/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 * "hostname()" function
7362 */
7363/*ARGSUSED*/
7364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007365f_hostname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007366 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007367 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368{
7369 char_u hostname[256];
7370
7371 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007372 rettv->v_type = VAR_STRING;
7373 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374}
7375
7376/*
7377 * iconv() function
7378 */
7379/*ARGSUSED*/
7380 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007381f_iconv(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007382 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007383 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384{
7385#ifdef FEAT_MBYTE
7386 char_u buf1[NUMBUFLEN];
7387 char_u buf2[NUMBUFLEN];
7388 char_u *from, *to, *str;
7389 vimconv_T vimconv;
7390#endif
7391
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007392 rettv->v_type = VAR_STRING;
7393 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394
7395#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007396 str = get_tv_string(&argvars[0]);
7397 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
7398 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 vimconv.vc_type = CONV_NONE;
7400 convert_setup(&vimconv, from, to);
7401
7402 /* If the encodings are equal, no conversion needed. */
7403 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007404 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007406 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407
7408 convert_setup(&vimconv, NULL, NULL);
7409 vim_free(from);
7410 vim_free(to);
7411#endif
7412}
7413
7414/*
7415 * "indent()" function
7416 */
7417 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007418f_indent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007419 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007420 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421{
7422 linenr_T lnum;
7423
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007424 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007426 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007428 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429}
7430
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007431/*
7432 * "index()" function
7433 */
7434 static void
7435f_index(argvars, rettv)
7436 typeval *argvars;
7437 typeval *rettv;
7438{
7439 listvar *l;
7440 listitem *item;
7441 long idx = 0;
7442 int ic = FALSE;
7443
7444 rettv->vval.v_number = -1;
7445 if (argvars[0].v_type != VAR_LIST)
7446 {
7447 EMSG(_(e_listreq));
7448 return;
7449 }
7450 l = argvars[0].vval.v_list;
7451 if (l != NULL)
7452 {
7453 if (argvars[2].v_type != VAR_UNKNOWN)
7454 ic = get_tv_number(&argvars[2]);
7455
7456 for (item = l->lv_first; item != NULL; item = item->li_next, ++idx)
7457 if (tv_equal(&item->li_tv, &argvars[1], ic))
7458 {
7459 rettv->vval.v_number = idx;
7460 break;
7461 }
7462 }
7463}
7464
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465static int inputsecret_flag = 0;
7466
7467/*
7468 * "input()" function
7469 * Also handles inputsecret() when inputsecret is set.
7470 */
7471 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007472f_input(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007473 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007474 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007476 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 char_u *p = NULL;
7478 int c;
7479 char_u buf[NUMBUFLEN];
7480 int cmd_silent_save = cmd_silent;
7481
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007482 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483
7484#ifdef NO_CONSOLE_INPUT
7485 /* While starting up, there is no place to enter text. */
7486 if (no_console_input())
7487 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007488 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 return;
7490 }
7491#endif
7492
7493 cmd_silent = FALSE; /* Want to see the prompt. */
7494 if (prompt != NULL)
7495 {
7496 /* Only the part of the message after the last NL is considered as
7497 * prompt for the command line */
7498 p = vim_strrchr(prompt, '\n');
7499 if (p == NULL)
7500 p = prompt;
7501 else
7502 {
7503 ++p;
7504 c = *p;
7505 *p = NUL;
7506 msg_start();
7507 msg_clr_eos();
7508 msg_puts_attr(prompt, echo_attr);
7509 msg_didout = FALSE;
7510 msg_starthere();
7511 *p = c;
7512 }
7513 cmdline_row = msg_row;
7514 }
7515
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007516 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007517 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007519 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
7521
7522 /* since the user typed this, no need to wait for return */
7523 need_wait_return = FALSE;
7524 msg_didout = FALSE;
7525 cmd_silent = cmd_silent_save;
7526}
7527
7528/*
7529 * "inputdialog()" function
7530 */
7531 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007532f_inputdialog(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007533 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007534 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535{
7536#if defined(FEAT_GUI_TEXTDIALOG)
7537 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
7538 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
7539 {
7540 char_u *message;
7541 char_u buf[NUMBUFLEN];
7542
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007543 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007544 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007546 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 IObuff[IOSIZE - 1] = NUL;
7548 }
7549 else
7550 IObuff[0] = NUL;
7551 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
7552 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007553 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 else
7555 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007556 if (argvars[1].v_type != VAR_UNKNOWN
7557 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007558 rettv->vval.v_string = vim_strsave(
7559 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007561 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007563 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 }
7565 else
7566#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007567 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568}
7569
7570static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
7571
7572/*
7573 * "inputrestore()" function
7574 */
7575/*ARGSUSED*/
7576 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007577f_inputrestore(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007578 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007579 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580{
7581 if (ga_userinput.ga_len > 0)
7582 {
7583 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
7585 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007586 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 }
7588 else if (p_verbose > 1)
7589 {
7590 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007591 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 }
7593}
7594
7595/*
7596 * "inputsave()" function
7597 */
7598/*ARGSUSED*/
7599 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007600f_inputsave(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007601 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007602 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603{
7604 /* Add an entry to the stack of typehead storage. */
7605 if (ga_grow(&ga_userinput, 1) == OK)
7606 {
7607 save_typeahead((tasave_T *)(ga_userinput.ga_data)
7608 + ga_userinput.ga_len);
7609 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007610 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 }
7612 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007613 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614}
7615
7616/*
7617 * "inputsecret()" function
7618 */
7619 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007620f_inputsecret(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007621 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007622 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623{
7624 ++cmdline_star;
7625 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007626 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 --cmdline_star;
7628 --inputsecret_flag;
7629}
7630
7631/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007632 * "insert()" function
7633 */
7634 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007635f_insert(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007636 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007637 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007638{
7639 long before = 0;
7640 long n;
7641 listitem *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007642 listvar *l;
7643
7644 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00007645 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007646 else if ((l = argvars[0].vval.v_list) != NULL)
7647 {
7648 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007649 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007650
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007651 n = before;
7652 item = list_find_ext(l, &n);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007653 if (n > 0)
7654 EMSGN(_(e_listidx), before);
7655 else
7656 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007657 list_insert_tv(l, &argvars[1], item);
7658 ++l->lv_refcount;
7659 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007660 }
7661 }
7662}
7663
7664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 * "isdirectory()" function
7666 */
7667 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007668f_isdirectory(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007669 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007670 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007672 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673}
7674
7675/*
7676 * "last_buffer_nr()" function.
7677 */
7678/*ARGSUSED*/
7679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007680f_last_buffer_nr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007681 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007682 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683{
7684 int n = 0;
7685 buf_T *buf;
7686
7687 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
7688 if (n < buf->b_fnum)
7689 n = buf->b_fnum;
7690
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007691 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007692}
7693
7694/*
7695 * "len()" function
7696 */
7697 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007698f_len(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007699 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007700 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007701{
7702 switch (argvars[0].v_type)
7703 {
7704 case VAR_STRING:
7705 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007706 rettv->vval.v_number = (varnumber_T)STRLEN(
7707 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007708 break;
7709 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007710 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007711 break;
7712 default:
7713 EMSG(_("E999: Invalid type for len()"));
7714 break;
7715 }
7716}
7717
Bram Moolenaar0d660222005-01-07 21:51:51 +00007718static void libcall_common __ARGS((typeval *argvars, typeval *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007719
7720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007721libcall_common(argvars, rettv, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007722 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007723 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007724 int type;
7725{
7726#ifdef FEAT_LIBCALL
7727 char_u *string_in;
7728 char_u **string_result;
7729 int nr_result;
7730#endif
7731
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007732 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007733 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007734 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007735 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007736 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007737
7738 if (check_restricted() || check_secure())
7739 return;
7740
7741#ifdef FEAT_LIBCALL
7742 /* The first two args must be strings, otherwise its meaningless */
7743 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
7744 {
7745 if (argvars[2].v_type == VAR_NUMBER)
7746 string_in = NULL;
7747 else
7748 string_in = argvars[2].vval.v_string;
7749 if (type == VAR_NUMBER)
7750 string_result = NULL;
7751 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007752 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007753 if (mch_libcall(argvars[0].vval.v_string,
7754 argvars[1].vval.v_string,
7755 string_in,
7756 argvars[2].vval.v_number,
7757 string_result,
7758 &nr_result) == OK
7759 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007760 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007761 }
7762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763}
7764
7765/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007766 * "libcall()" function
7767 */
7768 static void
7769f_libcall(argvars, rettv)
7770 typeval *argvars;
7771 typeval *rettv;
7772{
7773 libcall_common(argvars, rettv, VAR_STRING);
7774}
7775
7776/*
7777 * "libcallnr()" function
7778 */
7779 static void
7780f_libcallnr(argvars, rettv)
7781 typeval *argvars;
7782 typeval *rettv;
7783{
7784 libcall_common(argvars, rettv, VAR_NUMBER);
7785}
7786
7787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 * "line(string)" function
7789 */
7790 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007791f_line(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007792 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007793 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007794{
7795 linenr_T lnum = 0;
7796 pos_T *fp;
7797
7798 fp = var2fpos(&argvars[0], TRUE);
7799 if (fp != NULL)
7800 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007801 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802}
7803
7804/*
7805 * "line2byte(lnum)" function
7806 */
7807/*ARGSUSED*/
7808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007809f_line2byte(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007810 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007811 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
7813#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007814 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815#else
7816 linenr_T lnum;
7817
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007818 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007820 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007822 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
7823 if (rettv->vval.v_number >= 0)
7824 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825#endif
7826}
7827
7828/*
7829 * "lispindent(lnum)" function
7830 */
7831 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007832f_lispindent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007833 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007834 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835{
7836#ifdef FEAT_LISP
7837 pos_T pos;
7838 linenr_T lnum;
7839
7840 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007841 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7843 {
7844 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007845 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 curwin->w_cursor = pos;
7847 }
7848 else
7849#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007850 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851}
7852
7853/*
7854 * "localtime()" function
7855 */
7856/*ARGSUSED*/
7857 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007858f_localtime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007859 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007860 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007862 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863}
7864
Bram Moolenaar0d660222005-01-07 21:51:51 +00007865static void get_maparg __ARGS((typeval *argvars, typeval *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866
7867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007868get_maparg(argvars, rettv, exact)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007869 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007870 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 int exact;
7872{
7873 char_u *keys;
7874 char_u *which;
7875 char_u buf[NUMBUFLEN];
7876 char_u *keys_buf = NULL;
7877 char_u *rhs;
7878 int mode;
7879 garray_T ga;
7880
7881 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007882 rettv->v_type = VAR_STRING;
7883 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007885 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 if (*keys == NUL)
7887 return;
7888
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007889 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007890 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 else
7892 which = (char_u *)"";
7893 mode = get_map_mode(&which, 0);
7894
7895 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
7896 rhs = check_map(keys, mode, exact);
7897 vim_free(keys_buf);
7898 if (rhs != NULL)
7899 {
7900 ga_init(&ga);
7901 ga.ga_itemsize = 1;
7902 ga.ga_growsize = 40;
7903
7904 while (*rhs != NUL)
7905 ga_concat(&ga, str2special(&rhs, FALSE));
7906
7907 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007908 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909 }
7910}
7911
7912/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007913 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 */
7915 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007916f_maparg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007917 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007918 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007920 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921}
7922
7923/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007924 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 */
7926 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00007927f_mapcheck(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007928 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007929 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930{
Bram Moolenaar0d660222005-01-07 21:51:51 +00007931 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932}
7933
Bram Moolenaar0d660222005-01-07 21:51:51 +00007934static void find_some_match __ARGS((typeval *argvars, typeval *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935
7936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007937find_some_match(argvars, rettv, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007938 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007939 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 int type;
7941{
7942 char_u *str;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007943 char_u *expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 char_u *pat;
7945 regmatch_T regmatch;
7946 char_u patbuf[NUMBUFLEN];
7947 char_u *save_cpo;
7948 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007949 long nth = 1;
7950 int match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951
7952 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
7953 save_cpo = p_cpo;
7954 p_cpo = (char_u *)"";
7955
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007956 expr = str = get_tv_string(&argvars[0]);
7957 pat = get_tv_string_buf(&argvars[1], patbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958
7959 if (type == 2)
7960 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007961 rettv->v_type = VAR_STRING;
7962 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 }
7964 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007965 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007967 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007969 start = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 if (start < 0)
7971 start = 0;
7972 if (start > (long)STRLEN(str))
7973 goto theend;
7974 str += start;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007975
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007976 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007977 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 }
7979
7980 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7981 if (regmatch.regprog != NULL)
7982 {
7983 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007984
7985 while (1)
7986 {
7987 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
7988 if (!match || --nth <= 0)
7989 break;
7990 /* Advance to just after the match. */
7991#ifdef FEAT_MBYTE
7992 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
7993#else
7994 str = regmatch.startp[0] + 1;
7995#endif
7996 }
7997
7998 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 {
8000 if (type == 2)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008001 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 (int)(regmatch.endp[0] - regmatch.startp[0]));
8003 else
8004 {
8005 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008006 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 (varnumber_T)(regmatch.startp[0] - str);
8008 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008009 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008011 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 }
8013 }
8014 vim_free(regmatch.regprog);
8015 }
8016
8017theend:
8018 p_cpo = save_cpo;
8019}
8020
8021/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008022 * "match()" function
8023 */
8024 static void
8025f_match(argvars, rettv)
8026 typeval *argvars;
8027 typeval *rettv;
8028{
8029 find_some_match(argvars, rettv, 1);
8030}
8031
8032/*
8033 * "matchend()" function
8034 */
8035 static void
8036f_matchend(argvars, rettv)
8037 typeval *argvars;
8038 typeval *rettv;
8039{
8040 find_some_match(argvars, rettv, 0);
8041}
8042
8043/*
8044 * "matchstr()" function
8045 */
8046 static void
8047f_matchstr(argvars, rettv)
8048 typeval *argvars;
8049 typeval *rettv;
8050{
8051 find_some_match(argvars, rettv, 2);
8052}
8053
8054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 * "mode()" function
8056 */
8057/*ARGSUSED*/
8058 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008059f_mode(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008060 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008061 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062{
8063 char_u buf[2];
8064
8065#ifdef FEAT_VISUAL
8066 if (VIsual_active)
8067 {
8068 if (VIsual_select)
8069 buf[0] = VIsual_mode + 's' - 'v';
8070 else
8071 buf[0] = VIsual_mode;
8072 }
8073 else
8074#endif
8075 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
8076 buf[0] = 'r';
8077 else if (State & INSERT)
8078 {
8079 if (State & REPLACE_FLAG)
8080 buf[0] = 'R';
8081 else
8082 buf[0] = 'i';
8083 }
8084 else if (State & CMDLINE)
8085 buf[0] = 'c';
8086 else
8087 buf[0] = 'n';
8088
8089 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008090 rettv->vval.v_string = vim_strsave(buf);
8091 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092}
8093
8094/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008095 * "nextnonblank()" function
8096 */
8097 static void
8098f_nextnonblank(argvars, rettv)
8099 typeval *argvars;
8100 typeval *rettv;
8101{
8102 linenr_T lnum;
8103
8104 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8105 {
8106 if (lnum > curbuf->b_ml.ml_line_count)
8107 {
8108 lnum = 0;
8109 break;
8110 }
8111 if (*skipwhite(ml_get(lnum)) != NUL)
8112 break;
8113 }
8114 rettv->vval.v_number = lnum;
8115}
8116
8117/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 * "nr2char()" function
8119 */
8120 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008121f_nr2char(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008122 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008123 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124{
8125 char_u buf[NUMBUFLEN];
8126
8127#ifdef FEAT_MBYTE
8128 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008129 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 else
8131#endif
8132 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008133 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 buf[1] = NUL;
8135 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008136 rettv->v_type = VAR_STRING;
8137 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008138}
8139
8140/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008141 * "prevnonblank()" function
8142 */
8143 static void
8144f_prevnonblank(argvars, rettv)
8145 typeval *argvars;
8146 typeval *rettv;
8147{
8148 linenr_T lnum;
8149
8150 lnum = get_tv_lnum(argvars);
8151 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8152 lnum = 0;
8153 else
8154 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8155 --lnum;
8156 rettv->vval.v_number = lnum;
8157}
8158
8159#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8160static void make_connection __ARGS((void));
8161static int check_connection __ARGS((void));
8162
8163 static void
8164make_connection()
8165{
8166 if (X_DISPLAY == NULL
8167# ifdef FEAT_GUI
8168 && !gui.in_use
8169# endif
8170 )
8171 {
8172 x_force_connect = TRUE;
8173 setup_term_clip();
8174 x_force_connect = FALSE;
8175 }
8176}
8177
8178 static int
8179check_connection()
8180{
8181 make_connection();
8182 if (X_DISPLAY == NULL)
8183 {
8184 EMSG(_("E240: No connection to Vim server"));
8185 return FAIL;
8186 }
8187 return OK;
8188}
8189#endif
8190
8191#ifdef FEAT_CLIENTSERVER
8192static void remote_common __ARGS((typeval *argvars, typeval *rettv, int expr));
8193
8194 static void
8195remote_common(argvars, rettv, expr)
8196 typeval *argvars;
8197 typeval *rettv;
8198 int expr;
8199{
8200 char_u *server_name;
8201 char_u *keys;
8202 char_u *r = NULL;
8203 char_u buf[NUMBUFLEN];
8204# ifdef WIN32
8205 HWND w;
8206# else
8207 Window w;
8208# endif
8209
8210 if (check_restricted() || check_secure())
8211 return;
8212
8213# ifdef FEAT_X11
8214 if (check_connection() == FAIL)
8215 return;
8216# endif
8217
8218 server_name = get_tv_string(&argvars[0]);
8219 keys = get_tv_string_buf(&argvars[1], buf);
8220# ifdef WIN32
8221 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
8222# else
8223 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
8224 < 0)
8225# endif
8226 {
8227 if (r != NULL)
8228 EMSG(r); /* sending worked but evaluation failed */
8229 else
8230 EMSG2(_("E241: Unable to send to %s"), server_name);
8231 return;
8232 }
8233
8234 rettv->vval.v_string = r;
8235
8236 if (argvars[2].v_type != VAR_UNKNOWN)
8237 {
8238 var v;
8239 char_u str[30];
8240
8241 sprintf((char *)str, "0x%x", (unsigned int)w);
8242 v.tv.v_type = VAR_STRING;
8243 v.tv.vval.v_string = vim_strsave(str);
8244 set_var(get_tv_string(&argvars[2]), &v.tv, FALSE);
8245 vim_free(v.tv.vval.v_string);
8246 }
8247}
8248#endif
8249
8250/*
8251 * "remote_expr()" function
8252 */
8253/*ARGSUSED*/
8254 static void
8255f_remote_expr(argvars, rettv)
8256 typeval *argvars;
8257 typeval *rettv;
8258{
8259 rettv->v_type = VAR_STRING;
8260 rettv->vval.v_string = NULL;
8261#ifdef FEAT_CLIENTSERVER
8262 remote_common(argvars, rettv, TRUE);
8263#endif
8264}
8265
8266/*
8267 * "remote_foreground()" function
8268 */
8269/*ARGSUSED*/
8270 static void
8271f_remote_foreground(argvars, rettv)
8272 typeval *argvars;
8273 typeval *rettv;
8274{
8275 rettv->vval.v_number = 0;
8276#ifdef FEAT_CLIENTSERVER
8277# ifdef WIN32
8278 /* On Win32 it's done in this application. */
8279 serverForeground(get_tv_string(&argvars[0]));
8280# else
8281 /* Send a foreground() expression to the server. */
8282 argvars[1].v_type = VAR_STRING;
8283 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8284 argvars[2].v_type = VAR_UNKNOWN;
8285 remote_common(argvars, rettv, TRUE);
8286 vim_free(argvars[1].vval.v_string);
8287# endif
8288#endif
8289}
8290
8291/*ARGSUSED*/
8292 static void
8293f_remote_peek(argvars, rettv)
8294 typeval *argvars;
8295 typeval *rettv;
8296{
8297#ifdef FEAT_CLIENTSERVER
8298 var v;
8299 char_u *s = NULL;
8300# ifdef WIN32
8301 int n = 0;
8302# endif
8303
8304 if (check_restricted() || check_secure())
8305 {
8306 rettv->vval.v_number = -1;
8307 return;
8308 }
8309# ifdef WIN32
8310 sscanf(get_tv_string(&argvars[0]), "%x", &n);
8311 if (n == 0)
8312 rettv->vval.v_number = -1;
8313 else
8314 {
8315 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
8316 rettv->vval.v_number = (s != NULL);
8317 }
8318# else
8319 rettv->vval.v_number = 0;
8320 if (check_connection() == FAIL)
8321 return;
8322
8323 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
8324 serverStrToWin(get_tv_string(&argvars[0])), &s);
8325# endif
8326
8327 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
8328 {
8329 v.tv.v_type = VAR_STRING;
8330 v.tv.vval.v_string = vim_strsave(s);
8331 set_var(get_tv_string(&argvars[1]), &v.tv, FALSE);
8332 vim_free(v.tv.vval.v_string);
8333 }
8334#else
8335 rettv->vval.v_number = -1;
8336#endif
8337}
8338
8339/*ARGSUSED*/
8340 static void
8341f_remote_read(argvars, rettv)
8342 typeval *argvars;
8343 typeval *rettv;
8344{
8345 char_u *r = NULL;
8346
8347#ifdef FEAT_CLIENTSERVER
8348 if (!check_restricted() && !check_secure())
8349 {
8350# ifdef WIN32
8351 /* The server's HWND is encoded in the 'id' parameter */
8352 int n = 0;
8353
8354 sscanf(get_tv_string(&argvars[0]), "%x", &n);
8355 if (n != 0)
8356 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
8357 if (r == NULL)
8358# else
8359 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
8360 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
8361# endif
8362 EMSG(_("E277: Unable to read a server reply"));
8363 }
8364#endif
8365 rettv->v_type = VAR_STRING;
8366 rettv->vval.v_string = r;
8367}
8368
8369/*
8370 * "remote_send()" function
8371 */
8372/*ARGSUSED*/
8373 static void
8374f_remote_send(argvars, rettv)
8375 typeval *argvars;
8376 typeval *rettv;
8377{
8378 rettv->v_type = VAR_STRING;
8379 rettv->vval.v_string = NULL;
8380#ifdef FEAT_CLIENTSERVER
8381 remote_common(argvars, rettv, FALSE);
8382#endif
8383}
8384
8385/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008386 * "remove({list}, {idx} [, {end}])" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008387 */
8388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008389f_remove(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008390 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008391 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008392{
8393 listvar *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008394 listitem *item, *item2;
8395 listitem *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008396 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008397 long end;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008398
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008399 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008400 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008401 EMSG2(_(e_listarg), "remove()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008402 else if ((l = argvars[0].vval.v_list) != NULL)
8403 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008404 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008405 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008406 if (item == NULL)
8407 EMSGN(_(e_listidx), idx);
8408 else
8409 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008410 if (argvars[2].v_type == VAR_UNKNOWN)
8411 {
8412 /* Remove one item, return its value. */
8413 list_getrem(l, item, item);
8414 *rettv = item->li_tv;
8415 vim_free(item);
8416 }
8417 else
8418 {
8419 /* Remove range of items, return list with values. */
8420 end = get_tv_number(&argvars[2]);
8421 item2 = list_find(l, end);
8422 if (item2 == NULL)
8423 EMSGN(_(e_listidx), end);
8424 else
8425 {
8426 for (li = item; li != item2 && li != NULL; li = li->li_next)
8427 ;
8428 if (li == NULL) /* didn't find "item2" after "item" */
8429 EMSG(_(e_invrange));
8430 else
8431 {
8432 list_getrem(l, item, item2);
8433 l = list_alloc();
8434 if (l != NULL)
8435 {
8436 rettv->v_type = VAR_LIST;
8437 rettv->vval.v_list = l;
8438 l->lv_first = item;
8439 l->lv_last = item2;
8440 l->lv_refcount = 1;
8441 item->li_prev = NULL;
8442 item2->li_next = NULL;
8443 }
8444 }
8445 }
8446 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008447 }
8448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449}
8450
8451/*
8452 * "rename({from}, {to})" function
8453 */
8454 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008455f_rename(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008456 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008457 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458{
8459 char_u buf[NUMBUFLEN];
8460
8461 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008462 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008464 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
8465 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466}
8467
8468/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008469 * "repeat()" function
8470 */
8471/*ARGSUSED*/
8472 static void
8473f_repeat(argvars, rettv)
8474 typeval *argvars;
8475 typeval *rettv;
8476{
8477 char_u *p;
8478 int n;
8479 int slen;
8480 int len;
8481 char_u *r;
8482 int i;
8483 listvar *l;
8484
8485 n = get_tv_number(&argvars[1]);
8486 if (argvars[0].v_type == VAR_LIST)
8487 {
8488 l = list_alloc();
8489 if (l != NULL && argvars[0].vval.v_list != NULL)
8490 {
8491 l->lv_refcount = 1;
8492 while (n-- > 0)
8493 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
8494 break;
8495 }
8496 rettv->v_type = VAR_LIST;
8497 rettv->vval.v_list = l;
8498 }
8499 else
8500 {
8501 p = get_tv_string(&argvars[0]);
8502 rettv->v_type = VAR_STRING;
8503 rettv->vval.v_string = NULL;
8504
8505 slen = (int)STRLEN(p);
8506 len = slen * n;
8507 if (len <= 0)
8508 return;
8509
8510 r = alloc(len + 1);
8511 if (r != NULL)
8512 {
8513 for (i = 0; i < n; i++)
8514 mch_memmove(r + i * slen, p, (size_t)slen);
8515 r[len] = NUL;
8516 }
8517
8518 rettv->vval.v_string = r;
8519 }
8520}
8521
8522/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 * "resolve()" function
8524 */
8525 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008526f_resolve(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008527 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008528 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529{
8530 char_u *p;
8531
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008532 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533#ifdef FEAT_SHORTCUT
8534 {
8535 char_u *v = NULL;
8536
8537 v = mch_resolve_shortcut(p);
8538 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008539 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008541 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 }
8543#else
8544# ifdef HAVE_READLINK
8545 {
8546 char_u buf[MAXPATHL + 1];
8547 char_u *cpy;
8548 int len;
8549 char_u *remain = NULL;
8550 char_u *q;
8551 int is_relative_to_current = FALSE;
8552 int has_trailing_pathsep = FALSE;
8553 int limit = 100;
8554
8555 p = vim_strsave(p);
8556
8557 if (p[0] == '.' && (vim_ispathsep(p[1])
8558 || (p[1] == '.' && (vim_ispathsep(p[2])))))
8559 is_relative_to_current = TRUE;
8560
8561 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008562 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 has_trailing_pathsep = TRUE;
8564
8565 q = getnextcomp(p);
8566 if (*q != NUL)
8567 {
8568 /* Separate the first path component in "p", and keep the
8569 * remainder (beginning with the path separator). */
8570 remain = vim_strsave(q - 1);
8571 q[-1] = NUL;
8572 }
8573
8574 for (;;)
8575 {
8576 for (;;)
8577 {
8578 len = readlink((char *)p, (char *)buf, MAXPATHL);
8579 if (len <= 0)
8580 break;
8581 buf[len] = NUL;
8582
8583 if (limit-- == 0)
8584 {
8585 vim_free(p);
8586 vim_free(remain);
8587 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008588 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 goto fail;
8590 }
8591
8592 /* Ensure that the result will have a trailing path separator
8593 * if the argument has one. */
8594 if (remain == NULL && has_trailing_pathsep)
8595 add_pathsep(buf);
8596
8597 /* Separate the first path component in the link value and
8598 * concatenate the remainders. */
8599 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
8600 if (*q != NUL)
8601 {
8602 if (remain == NULL)
8603 remain = vim_strsave(q - 1);
8604 else
8605 {
8606 cpy = vim_strnsave(q-1, STRLEN(q-1)+STRLEN(remain));
8607 if (cpy != NULL)
8608 {
8609 STRCAT(cpy, remain);
8610 vim_free(remain);
8611 remain = cpy;
8612 }
8613 }
8614 q[-1] = NUL;
8615 }
8616
8617 q = gettail(p);
8618 if (q > p && *q == NUL)
8619 {
8620 /* Ignore trailing path separator. */
8621 q[-1] = NUL;
8622 q = gettail(p);
8623 }
8624 if (q > p && !mch_isFullName(buf))
8625 {
8626 /* symlink is relative to directory of argument */
8627 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
8628 if (cpy != NULL)
8629 {
8630 STRCPY(cpy, p);
8631 STRCPY(gettail(cpy), buf);
8632 vim_free(p);
8633 p = cpy;
8634 }
8635 }
8636 else
8637 {
8638 vim_free(p);
8639 p = vim_strsave(buf);
8640 }
8641 }
8642
8643 if (remain == NULL)
8644 break;
8645
8646 /* Append the first path component of "remain" to "p". */
8647 q = getnextcomp(remain + 1);
8648 len = q - remain - (*q != NUL);
8649 cpy = vim_strnsave(p, STRLEN(p) + len);
8650 if (cpy != NULL)
8651 {
8652 STRNCAT(cpy, remain, len);
8653 vim_free(p);
8654 p = cpy;
8655 }
8656 /* Shorten "remain". */
8657 if (*q != NUL)
8658 STRCPY(remain, q - 1);
8659 else
8660 {
8661 vim_free(remain);
8662 remain = NULL;
8663 }
8664 }
8665
8666 /* If the result is a relative path name, make it explicitly relative to
8667 * the current directory if and only if the argument had this form. */
8668 if (!vim_ispathsep(*p))
8669 {
8670 if (is_relative_to_current
8671 && *p != NUL
8672 && !(p[0] == '.'
8673 && (p[1] == NUL
8674 || vim_ispathsep(p[1])
8675 || (p[1] == '.'
8676 && (p[2] == NUL
8677 || vim_ispathsep(p[2]))))))
8678 {
8679 /* Prepend "./". */
8680 cpy = vim_strnsave((char_u *)"./", 2 + STRLEN(p));
8681 if (cpy != NULL)
8682 {
8683 STRCAT(cpy, p);
8684 vim_free(p);
8685 p = cpy;
8686 }
8687 }
8688 else if (!is_relative_to_current)
8689 {
8690 /* Strip leading "./". */
8691 q = p;
8692 while (q[0] == '.' && vim_ispathsep(q[1]))
8693 q += 2;
8694 if (q > p)
8695 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
8696 }
8697 }
8698
8699 /* Ensure that the result will have no trailing path separator
8700 * if the argument had none. But keep "/" or "//". */
8701 if (!has_trailing_pathsep)
8702 {
8703 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00008704 if (after_pathsep(p, q))
8705 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 }
8707
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008708 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 }
8710# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008711 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712# endif
8713#endif
8714
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008715 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716
8717#ifdef HAVE_READLINK
8718fail:
8719#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008720 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721}
8722
8723/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008724 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 */
8726 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008727f_reverse(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008728 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008729 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730{
Bram Moolenaar0d660222005-01-07 21:51:51 +00008731 listvar *l;
8732 listitem *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733
Bram Moolenaar0d660222005-01-07 21:51:51 +00008734 rettv->vval.v_number = 0;
8735 if (argvars[0].v_type != VAR_LIST)
8736 EMSG2(_(e_listarg), "reverse()");
8737 else if ((l = argvars[0].vval.v_list) != NULL)
8738 {
8739 li = l->lv_last;
8740 l->lv_first = l->lv_last = li;
8741 while (li != NULL)
8742 {
8743 ni = li->li_prev;
8744 list_append(l, li);
8745 li = ni;
8746 }
8747 rettv->vval.v_list = l;
8748 rettv->v_type = VAR_LIST;
8749 ++l->lv_refcount;
8750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751}
8752
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008753#define SP_NOMOVE 1 /* don't move cursor */
8754#define SP_REPEAT 2 /* repeat to find outer pair */
8755#define SP_RETCOUNT 4 /* return matchcount */
8756
Bram Moolenaar0d660222005-01-07 21:51:51 +00008757static int get_search_arg __ARGS((typeval *varp, int *flagsp));
8758
8759/*
8760 * Get flags for a search function.
8761 * Possibly sets "p_ws".
8762 * Returns BACKWARD, FORWARD or zero (for an error).
8763 */
8764 static int
8765get_search_arg(varp, flagsp)
8766 typeval *varp;
8767 int *flagsp;
8768{
8769 int dir = FORWARD;
8770 char_u *flags;
8771 char_u nbuf[NUMBUFLEN];
8772 int mask;
8773
8774 if (varp->v_type != VAR_UNKNOWN)
8775 {
8776 flags = get_tv_string_buf(varp, nbuf);
8777 while (*flags != NUL)
8778 {
8779 switch (*flags)
8780 {
8781 case 'b': dir = BACKWARD; break;
8782 case 'w': p_ws = TRUE; break;
8783 case 'W': p_ws = FALSE; break;
8784 default: mask = 0;
8785 if (flagsp != NULL)
8786 switch (*flags)
8787 {
8788 case 'n': mask = SP_NOMOVE; break;
8789 case 'r': mask = SP_REPEAT; break;
8790 case 'm': mask = SP_RETCOUNT; break;
8791 }
8792 if (mask == 0)
8793 {
8794 EMSG2(_(e_invarg2), flags);
8795 dir = 0;
8796 }
8797 else
8798 *flagsp |= mask;
8799 }
8800 if (dir == 0)
8801 break;
8802 ++flags;
8803 }
8804 }
8805 return dir;
8806}
8807
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808/*
8809 * "search()" function
8810 */
8811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008812f_search(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008813 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008814 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815{
8816 char_u *pat;
8817 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008818 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 int save_p_ws = p_ws;
8820 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008821 int flags = 0;
8822
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008823 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008825 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008826 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
8827 if (dir == 0)
8828 goto theend;
8829 if ((flags & ~SP_NOMOVE) != 0)
8830 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008831 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008832 goto theend;
8833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008835 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
8837 SEARCH_KEEP, RE_SEARCH) != FAIL)
8838 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008839 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 curwin->w_cursor = pos;
8841 /* "/$" will put the cursor after the end of the line, may need to
8842 * correct that here */
8843 check_cursor();
8844 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008845
8846 /* If 'n' flag is used: restore cursor position. */
8847 if (flags & SP_NOMOVE)
8848 curwin->w_cursor = save_cursor;
8849theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 p_ws = save_p_ws;
8851}
8852
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853/*
8854 * "searchpair()" function
8855 */
8856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008857f_searchpair(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008858 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008859 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860{
8861 char_u *spat, *mpat, *epat;
8862 char_u *skip;
8863 char_u *pat, *pat2, *pat3;
8864 pos_T pos;
8865 pos_T firstpos;
8866 pos_T save_cursor;
8867 pos_T save_pos;
8868 int save_p_ws = p_ws;
8869 char_u *save_cpo;
8870 int dir;
8871 int flags = 0;
8872 char_u nbuf1[NUMBUFLEN];
8873 char_u nbuf2[NUMBUFLEN];
8874 char_u nbuf3[NUMBUFLEN];
8875 int n;
8876 int r;
8877 int nest = 1;
8878 int err;
8879
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008880 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881
8882 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8883 save_cpo = p_cpo;
8884 p_cpo = (char_u *)"";
8885
8886 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008887 spat = get_tv_string(&argvars[0]);
8888 mpat = get_tv_string_buf(&argvars[1], nbuf1);
8889 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890
8891 /* Make two search patterns: start/end (pat2, for in nested pairs) and
8892 * start/middle/end (pat3, for the top pair). */
8893 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
8894 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
8895 if (pat2 == NULL || pat3 == NULL)
8896 goto theend;
8897 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
8898 if (*mpat == NUL)
8899 STRCPY(pat3, pat2);
8900 else
8901 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
8902 spat, epat, mpat);
8903
8904 /* Handle the optional fourth argument: flags */
8905 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008906 if (dir == 0)
8907 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908
8909 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008910 if (argvars[3].v_type == VAR_UNKNOWN
8911 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 skip = (char_u *)"";
8913 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008914 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915
8916 save_cursor = curwin->w_cursor;
8917 pos = curwin->w_cursor;
8918 firstpos.lnum = 0;
8919 pat = pat3;
8920 for (;;)
8921 {
8922 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
8923 SEARCH_KEEP, RE_SEARCH);
8924 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
8925 /* didn't find it or found the first match again: FAIL */
8926 break;
8927
8928 if (firstpos.lnum == 0)
8929 firstpos = pos;
8930
8931 /* If the skip pattern matches, ignore this match. */
8932 if (*skip != NUL)
8933 {
8934 save_pos = curwin->w_cursor;
8935 curwin->w_cursor = pos;
8936 r = eval_to_bool(skip, &err, NULL, FALSE);
8937 curwin->w_cursor = save_pos;
8938 if (err)
8939 {
8940 /* Evaluating {skip} caused an error, break here. */
8941 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008942 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943 break;
8944 }
8945 if (r)
8946 continue;
8947 }
8948
8949 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
8950 {
8951 /* Found end when searching backwards or start when searching
8952 * forward: nested pair. */
8953 ++nest;
8954 pat = pat2; /* nested, don't search for middle */
8955 }
8956 else
8957 {
8958 /* Found end when searching forward or start when searching
8959 * backward: end of (nested) pair; or found middle in outer pair. */
8960 if (--nest == 1)
8961 pat = pat3; /* outer level, search for middle */
8962 }
8963
8964 if (nest == 0)
8965 {
8966 /* Found the match: return matchcount or line number. */
8967 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008968 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008970 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 curwin->w_cursor = pos;
8972 if (!(flags & SP_REPEAT))
8973 break;
8974 nest = 1; /* search for next unmatched */
8975 }
8976 }
8977
8978 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008979 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008980 curwin->w_cursor = save_cursor;
8981
8982theend:
8983 vim_free(pat2);
8984 vim_free(pat3);
8985 p_ws = save_p_ws;
8986 p_cpo = save_cpo;
8987}
8988
Bram Moolenaar0d660222005-01-07 21:51:51 +00008989/*ARGSUSED*/
8990 static void
8991f_server2client(argvars, rettv)
8992 typeval *argvars;
8993 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994{
Bram Moolenaar0d660222005-01-07 21:51:51 +00008995#ifdef FEAT_CLIENTSERVER
8996 char_u buf[NUMBUFLEN];
8997 char_u *server = get_tv_string(&argvars[0]);
8998 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999
Bram Moolenaar0d660222005-01-07 21:51:51 +00009000 rettv->vval.v_number = -1;
9001 if (check_restricted() || check_secure())
9002 return;
9003# ifdef FEAT_X11
9004 if (check_connection() == FAIL)
9005 return;
9006# endif
9007
9008 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009010 EMSG(_("E258: Unable to send to client"));
9011 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009013 rettv->vval.v_number = 0;
9014#else
9015 rettv->vval.v_number = -1;
9016#endif
9017}
9018
9019/*ARGSUSED*/
9020 static void
9021f_serverlist(argvars, rettv)
9022 typeval *argvars;
9023 typeval *rettv;
9024{
9025 char_u *r = NULL;
9026
9027#ifdef FEAT_CLIENTSERVER
9028# ifdef WIN32
9029 r = serverGetVimNames();
9030# else
9031 make_connection();
9032 if (X_DISPLAY != NULL)
9033 r = serverGetVimNames(X_DISPLAY);
9034# endif
9035#endif
9036 rettv->v_type = VAR_STRING;
9037 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038}
9039
9040/*
9041 * "setbufvar()" function
9042 */
9043/*ARGSUSED*/
9044 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009045f_setbufvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009046 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009047 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048{
9049 buf_T *buf;
9050#ifdef FEAT_AUTOCMD
9051 aco_save_T aco;
9052#else
9053 buf_T *save_curbuf;
9054#endif
9055 char_u *varname, *bufvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009056 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057 char_u nbuf[NUMBUFLEN];
9058
9059 if (check_restricted() || check_secure())
9060 return;
9061 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009062 buf = get_buf_tv(&argvars[0]);
9063 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 varp = &argvars[2];
9065
9066 if (buf != NULL && varname != NULL && varp != NULL)
9067 {
9068 /* set curbuf to be our buf, temporarily */
9069#ifdef FEAT_AUTOCMD
9070 aucmd_prepbuf(&aco, buf);
9071#else
9072 save_curbuf = curbuf;
9073 curbuf = buf;
9074#endif
9075
9076 if (*varname == '&')
9077 {
9078 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009079 set_option_value(varname, get_tv_number(varp),
9080 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 }
9082 else
9083 {
9084 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
9085 if (bufvarname != NULL)
9086 {
9087 STRCPY(bufvarname, "b:");
9088 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00009089 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 vim_free(bufvarname);
9091 }
9092 }
9093
9094 /* reset notion of buffer */
9095#ifdef FEAT_AUTOCMD
9096 aucmd_restbuf(&aco);
9097#else
9098 curbuf = save_curbuf;
9099#endif
9100 }
9101 --emsg_off;
9102}
9103
9104/*
9105 * "setcmdpos()" function
9106 */
9107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009108f_setcmdpos(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009109 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009110 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009112 rettv->vval.v_number = set_cmdline_pos(
9113 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114}
9115
9116/*
9117 * "setline()" function
9118 */
9119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009120f_setline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009121 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123{
9124 linenr_T lnum;
9125 char_u *line;
9126
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009127 lnum = get_tv_lnum(argvars);
9128 line = get_tv_string(&argvars[1]);
9129 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130
9131 if (lnum >= 1
9132 && lnum <= curbuf->b_ml.ml_line_count
9133 && u_savesub(lnum) == OK
9134 && ml_replace(lnum, line, TRUE) == OK)
9135 {
9136 changed_bytes(lnum, 0);
9137 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009138 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 }
9140}
9141
9142/*
9143 * "setreg()" function
9144 */
9145 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009146f_setreg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009147 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009148 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149{
9150 int regname;
9151 char_u *strregname;
9152 char_u *stropt;
9153 int append;
9154 char_u yank_type;
9155 long block_len;
9156
9157 block_len = -1;
9158 yank_type = MAUTO;
9159 append = FALSE;
9160
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161 strregname = get_tv_string(argvars);
9162 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163
9164 regname = (strregname == NULL ? '"' : *strregname);
9165 if (regname == 0 || regname == '@')
9166 regname = '"';
9167 else if (regname == '=')
9168 return;
9169
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009170 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009172 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 switch (*stropt)
9174 {
9175 case 'a': case 'A': /* append */
9176 append = TRUE;
9177 break;
9178 case 'v': case 'c': /* character-wise selection */
9179 yank_type = MCHAR;
9180 break;
9181 case 'V': case 'l': /* line-wise selection */
9182 yank_type = MLINE;
9183 break;
9184#ifdef FEAT_VISUAL
9185 case 'b': case Ctrl_V: /* block-wise selection */
9186 yank_type = MBLOCK;
9187 if (VIM_ISDIGIT(stropt[1]))
9188 {
9189 ++stropt;
9190 block_len = getdigits(&stropt) - 1;
9191 --stropt;
9192 }
9193 break;
9194#endif
9195 }
9196 }
9197
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009198 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009200 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201}
9202
9203
9204/*
9205 * "setwinvar(expr)" function
9206 */
9207/*ARGSUSED*/
9208 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009209f_setwinvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009210 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009211 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212{
9213 win_T *win;
9214#ifdef FEAT_WINDOWS
9215 win_T *save_curwin;
9216#endif
9217 char_u *varname, *winvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009218 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 char_u nbuf[NUMBUFLEN];
9220
9221 if (check_restricted() || check_secure())
9222 return;
9223 ++emsg_off;
9224 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009225 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 varp = &argvars[2];
9227
9228 if (win != NULL && varname != NULL && varp != NULL)
9229 {
9230#ifdef FEAT_WINDOWS
9231 /* set curwin to be our win, temporarily */
9232 save_curwin = curwin;
9233 curwin = win;
9234 curbuf = curwin->w_buffer;
9235#endif
9236
9237 if (*varname == '&')
9238 {
9239 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009240 set_option_value(varname, get_tv_number(varp),
9241 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 }
9243 else
9244 {
9245 winvarname = alloc((unsigned)STRLEN(varname) + 3);
9246 if (winvarname != NULL)
9247 {
9248 STRCPY(winvarname, "w:");
9249 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00009250 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 vim_free(winvarname);
9252 }
9253 }
9254
9255#ifdef FEAT_WINDOWS
9256 /* Restore current window, if it's still valid (autocomands can make
9257 * it invalid). */
9258 if (win_valid(save_curwin))
9259 {
9260 curwin = save_curwin;
9261 curbuf = curwin->w_buffer;
9262 }
9263#endif
9264 }
9265 --emsg_off;
9266}
9267
9268/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009269 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009270 */
9271 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009272f_simplify(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009273 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009274 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009276 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009277
Bram Moolenaar0d660222005-01-07 21:51:51 +00009278 p = get_tv_string(&argvars[0]);
9279 rettv->vval.v_string = vim_strsave(p);
9280 simplify_filename(rettv->vval.v_string); /* simplify in place */
9281 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282}
9283
Bram Moolenaar0d660222005-01-07 21:51:51 +00009284static int
9285#ifdef __BORLANDC__
9286 _RTLENTRYF
9287#endif
9288 item_compare __ARGS((const void *s1, const void *s2));
9289static int
9290#ifdef __BORLANDC__
9291 _RTLENTRYF
9292#endif
9293 item_compare2 __ARGS((const void *s1, const void *s2));
9294
9295static int item_compare_ic;
9296static char_u *item_compare_func;
9297#define ITEM_COMPARE_FAIL 999
9298
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009300 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 */
Bram Moolenaar0d660222005-01-07 21:51:51 +00009302 static int
9303#ifdef __BORLANDC__
9304_RTLENTRYF
9305#endif
9306item_compare(s1, s2)
9307 const void *s1;
9308 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009310 char_u *p1, *p2;
9311 char_u *tofree1, *tofree2;
9312 int res;
9313 char_u numbuf1[NUMBUFLEN];
9314 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315
Bram Moolenaar0d660222005-01-07 21:51:51 +00009316 p1 = tv2string(&(*(listitem **)s1)->li_tv, &tofree1, numbuf1);
9317 p2 = tv2string(&(*(listitem **)s2)->li_tv, &tofree2, numbuf2);
9318 if (item_compare_ic)
9319 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009321 res = STRCMP(p1, p2);
9322 vim_free(tofree1);
9323 vim_free(tofree2);
9324 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325}
9326
9327 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +00009328#ifdef __BORLANDC__
9329_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00009331item_compare2(s1, s2)
9332 const void *s1;
9333 const void *s2;
9334{
9335 int res;
9336 typeval rettv;
9337 typeval argv[2];
9338 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009339
Bram Moolenaar0d660222005-01-07 21:51:51 +00009340 /* copy the values (is this really needed?) */
9341 copy_tv(&(*(listitem **)s1)->li_tv, &argv[0]);
9342 copy_tv(&(*(listitem **)s2)->li_tv, &argv[1]);
9343
9344 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
9345 res = call_func(item_compare_func, STRLEN(item_compare_func),
9346 &rettv, 2, argv, 0L, 0L, &dummy, TRUE);
9347 clear_tv(&argv[0]);
9348 clear_tv(&argv[1]);
9349
9350 if (res == FAIL)
9351 res = ITEM_COMPARE_FAIL;
9352 else
9353 res = get_tv_number(&rettv);
9354 clear_tv(&rettv);
9355 return res;
9356}
9357
9358/*
9359 * "sort({list})" function
9360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009362f_sort(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009363 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009364 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009366 listvar *l;
9367 listitem *li;
9368 listitem **ptrs;
9369 long len;
9370 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371
Bram Moolenaar0d660222005-01-07 21:51:51 +00009372 rettv->vval.v_number = 0;
9373 if (argvars[0].v_type != VAR_LIST)
9374 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 else
9376 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009377 l = argvars[0].vval.v_list;
9378 if (l == NULL)
9379 return;
9380 rettv->vval.v_list = l;
9381 rettv->v_type = VAR_LIST;
9382 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383
Bram Moolenaar0d660222005-01-07 21:51:51 +00009384 len = list_len(l);
9385 if (len <= 1)
9386 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387
Bram Moolenaar0d660222005-01-07 21:51:51 +00009388 item_compare_ic = FALSE;
9389 item_compare_func = NULL;
9390 if (argvars[1].v_type != VAR_UNKNOWN)
9391 {
9392 if (argvars[1].v_type == VAR_FUNC)
9393 item_compare_func = argvars[0].vval.v_string;
9394 else
9395 {
9396 i = get_tv_number(&argvars[1]);
9397 if (i == 1)
9398 item_compare_ic = TRUE;
9399 else
9400 item_compare_func = get_tv_string(&argvars[1]);
9401 }
9402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403
Bram Moolenaar0d660222005-01-07 21:51:51 +00009404 /* Make an array with each entry pointing to an item in the List. */
9405 ptrs = (listitem **)alloc((int)(len * sizeof(listitem *)));
9406 if (ptrs == NULL)
9407 return;
9408 i = 0;
9409 for (li = l->lv_first; li != NULL; li = li->li_next)
9410 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411
Bram Moolenaar0d660222005-01-07 21:51:51 +00009412 /* test the compare function */
9413 if (item_compare_func != NULL
9414 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
9415 == ITEM_COMPARE_FAIL)
9416 EMSG(_("E999: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009417 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009418 {
9419 /* Sort the array with item pointers. */
9420 qsort((void *)ptrs, (size_t)len, sizeof(listitem *),
9421 item_compare_func == NULL ? item_compare : item_compare2);
9422
9423 /* Clear the List and append the items in the sorted order. */
9424 l->lv_first = l->lv_last = NULL;
9425 for (i = 0; i < len; ++i)
9426 list_append(l, ptrs[i]);
9427 }
9428
9429 vim_free(ptrs);
9430 }
9431}
9432
9433 static void
9434f_str2list(argvars, rettv)
9435 typeval *argvars;
9436 typeval *rettv;
9437{
9438 char_u *str;
9439 char_u *end;
9440 char_u *pat;
9441 regmatch_T regmatch;
9442 char_u patbuf[NUMBUFLEN];
9443 char_u *save_cpo;
9444 int match;
9445 listitem *ni;
9446 listvar *l;
9447 colnr_T col = 0;
9448
9449 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9450 save_cpo = p_cpo;
9451 p_cpo = (char_u *)"";
9452
9453 str = get_tv_string(&argvars[0]);
9454 if (argvars[1].v_type == VAR_UNKNOWN)
9455 pat = (char_u *)"[\\x01- ]\\+";
9456 else
9457 pat = get_tv_string_buf(&argvars[1], patbuf);
9458
9459 l = list_alloc();
9460 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009462 rettv->v_type = VAR_LIST;
9463 rettv->vval.v_list = l;
9464 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465
Bram Moolenaar0d660222005-01-07 21:51:51 +00009466 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
9467 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009468 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009469 regmatch.rm_ic = FALSE;
9470 while (*str != NUL)
9471 {
9472 match = vim_regexec_nl(&regmatch, str, col);
9473 if (match)
9474 end = regmatch.startp[0];
9475 else
9476 end = str + STRLEN(str);
9477 if (end > str)
9478 {
9479 ni = listitem_alloc();
9480 if (ni == NULL)
9481 break;
9482 ni->li_tv.v_type = VAR_STRING;
9483 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
9484 list_append(l, ni);
9485 }
9486 if (!match)
9487 break;
9488 /* Advance to just after the match. */
9489 if (regmatch.endp[0] > str)
9490 col = 0;
9491 else
9492 {
9493 /* Don't get stuck at the same match. */
9494#ifdef FEAT_MBYTE
9495 col = mb_ptr2len_check(regmatch.endp[0]);
9496#else
9497 col = 1;
9498#endif
9499 }
9500 str = regmatch.endp[0];
9501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502
Bram Moolenaar0d660222005-01-07 21:51:51 +00009503 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505
Bram Moolenaar0d660222005-01-07 21:51:51 +00009506 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507}
9508
9509#ifdef HAVE_STRFTIME
9510/*
9511 * "strftime({format}[, {time}])" function
9512 */
9513 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009514f_strftime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009515 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009516 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517{
9518 char_u result_buf[256];
9519 struct tm *curtime;
9520 time_t seconds;
9521 char_u *p;
9522
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009523 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009525 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009526 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 seconds = time(NULL);
9528 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009529 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 curtime = localtime(&seconds);
9531 /* MSVC returns NULL for an invalid value of seconds. */
9532 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009533 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 else
9535 {
9536# ifdef FEAT_MBYTE
9537 vimconv_T conv;
9538 char_u *enc;
9539
9540 conv.vc_type = CONV_NONE;
9541 enc = enc_locale();
9542 convert_setup(&conv, p_enc, enc);
9543 if (conv.vc_type != CONV_NONE)
9544 p = string_convert(&conv, p, NULL);
9545# endif
9546 if (p != NULL)
9547 (void)strftime((char *)result_buf, sizeof(result_buf),
9548 (char *)p, curtime);
9549 else
9550 result_buf[0] = NUL;
9551
9552# ifdef FEAT_MBYTE
9553 if (conv.vc_type != CONV_NONE)
9554 vim_free(p);
9555 convert_setup(&conv, enc, p_enc);
9556 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009557 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 else
9559# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561
9562# ifdef FEAT_MBYTE
9563 /* Release conversion descriptors */
9564 convert_setup(&conv, NULL, NULL);
9565 vim_free(enc);
9566# endif
9567 }
9568}
9569#endif
9570
9571/*
9572 * "stridx()" function
9573 */
9574 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009575f_stridx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009576 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009577 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009578{
9579 char_u buf[NUMBUFLEN];
9580 char_u *needle;
9581 char_u *haystack;
9582 char_u *pos;
9583
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009584 needle = get_tv_string(&argvars[1]);
9585 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 pos = (char_u *)strstr((char *)haystack, (char *)needle);
9587
9588 if (pos == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009589 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009591 rettv->vval.v_number = (varnumber_T) (pos - haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592}
9593
9594/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009595 * "string()" function
9596 */
9597 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009598f_string(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009599 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009600 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009601{
9602 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009603 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009604
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009605 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009606 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009607 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009608 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609}
9610
9611/*
9612 * "strlen()" function
9613 */
9614 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009615f_strlen(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009616 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009617 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009619 rettv->vval.v_number = (varnumber_T)(STRLEN(
9620 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621}
9622
9623/*
9624 * "strpart()" function
9625 */
9626 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009627f_strpart(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009628 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630{
9631 char_u *p;
9632 int n;
9633 int len;
9634 int slen;
9635
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009636 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 slen = (int)STRLEN(p);
9638
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009639 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009640 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009641 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 else
9643 len = slen - n; /* default len: all bytes that are available. */
9644
9645 /*
9646 * Only return the overlap between the specified part and the actual
9647 * string.
9648 */
9649 if (n < 0)
9650 {
9651 len += n;
9652 n = 0;
9653 }
9654 else if (n > slen)
9655 n = slen;
9656 if (len < 0)
9657 len = 0;
9658 else if (n + len > slen)
9659 len = slen - n;
9660
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009661 rettv->v_type = VAR_STRING;
9662 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663}
9664
9665/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009666 * "strridx()" function
9667 */
9668 static void
9669f_strridx(argvars, rettv)
9670 typeval *argvars;
9671 typeval *rettv;
9672{
9673 char_u buf[NUMBUFLEN];
9674 char_u *needle;
9675 char_u *haystack;
9676 char_u *rest;
9677 char_u *lastmatch = NULL;
9678
9679 needle = get_tv_string(&argvars[1]);
9680 haystack = get_tv_string_buf(&argvars[0], buf);
9681 if (*needle == NUL)
9682 /* Empty string matches past the end. */
9683 lastmatch = haystack + STRLEN(haystack);
9684 else
9685 for (rest = haystack; *rest != '\0'; ++rest)
9686 {
9687 rest = (char_u *)strstr((char *)rest, (char *)needle);
9688 if (rest == NULL)
9689 break;
9690 lastmatch = rest;
9691 }
9692
9693 if (lastmatch == NULL)
9694 rettv->vval.v_number = -1;
9695 else
9696 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
9697}
9698
9699/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 * "strtrans()" function
9701 */
9702 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009703f_strtrans(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009704 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707 rettv->v_type = VAR_STRING;
9708 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709}
9710
9711/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009712 * "submatch()" function
9713 */
9714 static void
9715f_submatch(argvars, rettv)
9716 typeval *argvars;
9717 typeval *rettv;
9718{
9719 rettv->v_type = VAR_STRING;
9720 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
9721}
9722
9723/*
9724 * "substitute()" function
9725 */
9726 static void
9727f_substitute(argvars, rettv)
9728 typeval *argvars;
9729 typeval *rettv;
9730{
9731 char_u patbuf[NUMBUFLEN];
9732 char_u subbuf[NUMBUFLEN];
9733 char_u flagsbuf[NUMBUFLEN];
9734
9735 rettv->v_type = VAR_STRING;
9736 rettv->vval.v_string = do_string_sub(
9737 get_tv_string(&argvars[0]),
9738 get_tv_string_buf(&argvars[1], patbuf),
9739 get_tv_string_buf(&argvars[2], subbuf),
9740 get_tv_string_buf(&argvars[3], flagsbuf));
9741}
9742
9743/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 * "synID(line, col, trans)" function
9745 */
9746/*ARGSUSED*/
9747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009748f_synID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009749 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751{
9752 int id = 0;
9753#ifdef FEAT_SYN_HL
9754 long line;
9755 long col;
9756 int trans;
9757
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009758 line = get_tv_lnum(argvars);
9759 col = get_tv_number(&argvars[1]) - 1;
9760 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761
9762 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
9763 && col >= 0 && col < (long)STRLEN(ml_get(line)))
9764 id = syn_get_id(line, col, trans);
9765#endif
9766
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009767 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768}
9769
9770/*
9771 * "synIDattr(id, what [, mode])" function
9772 */
9773/*ARGSUSED*/
9774 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009775f_synIDattr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009776 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009777 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778{
9779 char_u *p = NULL;
9780#ifdef FEAT_SYN_HL
9781 int id;
9782 char_u *what;
9783 char_u *mode;
9784 char_u modebuf[NUMBUFLEN];
9785 int modec;
9786
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009787 id = get_tv_number(&argvars[0]);
9788 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009789 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009791 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 modec = TOLOWER_ASC(mode[0]);
9793 if (modec != 't' && modec != 'c'
9794#ifdef FEAT_GUI
9795 && modec != 'g'
9796#endif
9797 )
9798 modec = 0; /* replace invalid with current */
9799 }
9800 else
9801 {
9802#ifdef FEAT_GUI
9803 if (gui.in_use)
9804 modec = 'g';
9805 else
9806#endif
9807 if (t_colors > 1)
9808 modec = 'c';
9809 else
9810 modec = 't';
9811 }
9812
9813
9814 switch (TOLOWER_ASC(what[0]))
9815 {
9816 case 'b':
9817 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
9818 p = highlight_color(id, what, modec);
9819 else /* bold */
9820 p = highlight_has_attr(id, HL_BOLD, modec);
9821 break;
9822
9823 case 'f': /* fg[#] */
9824 p = highlight_color(id, what, modec);
9825 break;
9826
9827 case 'i':
9828 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
9829 p = highlight_has_attr(id, HL_INVERSE, modec);
9830 else /* italic */
9831 p = highlight_has_attr(id, HL_ITALIC, modec);
9832 break;
9833
9834 case 'n': /* name */
9835 p = get_highlight_name(NULL, id - 1);
9836 break;
9837
9838 case 'r': /* reverse */
9839 p = highlight_has_attr(id, HL_INVERSE, modec);
9840 break;
9841
9842 case 's': /* standout */
9843 p = highlight_has_attr(id, HL_STANDOUT, modec);
9844 break;
9845
9846 case 'u': /* underline */
9847 p = highlight_has_attr(id, HL_UNDERLINE, modec);
9848 break;
9849 }
9850
9851 if (p != NULL)
9852 p = vim_strsave(p);
9853#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009854 rettv->v_type = VAR_STRING;
9855 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856}
9857
9858/*
9859 * "synIDtrans(id)" function
9860 */
9861/*ARGSUSED*/
9862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009863f_synIDtrans(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009864 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009865 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866{
9867 int id;
9868
9869#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009870 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871
9872 if (id > 0)
9873 id = syn_get_final_id(id);
9874 else
9875#endif
9876 id = 0;
9877
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009878 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879}
9880
9881/*
9882 * "system()" function
9883 */
9884 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009885f_system(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009886 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009887 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888{
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009889 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009891 char_u *infile = NULL;
9892 char_u buf[NUMBUFLEN];
9893 int err = FALSE;
9894 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009895
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009896 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009897 {
9898 /*
9899 * Write the string to a temp file, to be used for input of the shell
9900 * command.
9901 */
9902 if ((infile = vim_tempname('i')) == NULL)
9903 {
9904 EMSG(_(e_notmp));
9905 return;
9906 }
9907
9908 fd = mch_fopen((char *)infile, WRITEBIN);
9909 if (fd == NULL)
9910 {
9911 EMSG2(_(e_notopen), infile);
9912 goto done;
9913 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009914 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009915 if (fwrite(p, STRLEN(p), 1, fd) != 1)
9916 err = TRUE;
9917 if (fclose(fd) != 0)
9918 err = TRUE;
9919 if (err)
9920 {
9921 EMSG(_("E677: Error writing temp file"));
9922 goto done;
9923 }
9924 }
9925
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009926 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009927
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928#ifdef USE_CR
9929 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009930 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 {
9932 char_u *s;
9933
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009934 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009935 {
9936 if (*s == CAR)
9937 *s = NL;
9938 }
9939 }
9940#else
9941# ifdef USE_CRNL
9942 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009943 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009944 {
9945 char_u *s, *d;
9946
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009947 d = res;
9948 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 {
9950 if (s[0] == CAR && s[1] == NL)
9951 ++s;
9952 *d++ = *s;
9953 }
9954 *d = NUL;
9955 }
9956# endif
9957#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +00009958
9959done:
9960 if (infile != NULL)
9961 {
9962 mch_remove(infile);
9963 vim_free(infile);
9964 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009965 rettv->v_type = VAR_STRING;
9966 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967}
9968
9969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 * "tempname()" function
9971 */
9972/*ARGSUSED*/
9973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009974f_tempname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009975 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009976 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977{
9978 static int x = 'A';
9979
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009980 rettv->v_type = VAR_STRING;
9981 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982
9983 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
9984 * names. Skip 'I' and 'O', they are used for shell redirection. */
9985 do
9986 {
9987 if (x == 'Z')
9988 x = '0';
9989 else if (x == '9')
9990 x = 'A';
9991 else
9992 {
9993#ifdef EBCDIC
9994 if (x == 'I')
9995 x = 'J';
9996 else if (x == 'R')
9997 x = 'S';
9998 else
9999#endif
10000 ++x;
10001 }
10002 } while (x == 'I' || x == 'O');
10003}
10004
10005/*
10006 * "tolower(string)" function
10007 */
10008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010009f_tolower(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010010 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010011 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012{
10013 char_u *p;
10014
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010015 p = vim_strsave(get_tv_string(&argvars[0]));
10016 rettv->v_type = VAR_STRING;
10017 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018
10019 if (p != NULL)
10020 while (*p != NUL)
10021 {
10022#ifdef FEAT_MBYTE
10023 int l;
10024
10025 if (enc_utf8)
10026 {
10027 int c, lc;
10028
10029 c = utf_ptr2char(p);
10030 lc = utf_tolower(c);
10031 l = utf_ptr2len_check(p);
10032 /* TODO: reallocate string when byte count changes. */
10033 if (utf_char2len(lc) == l)
10034 utf_char2bytes(lc, p);
10035 p += l;
10036 }
10037 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
10038 p += l; /* skip multi-byte character */
10039 else
10040#endif
10041 {
10042 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
10043 ++p;
10044 }
10045 }
10046}
10047
10048/*
10049 * "toupper(string)" function
10050 */
10051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010052f_toupper(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010053 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010054 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055{
10056 char_u *p;
10057
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010058 p = vim_strsave(get_tv_string(&argvars[0]));
10059 rettv->v_type = VAR_STRING;
10060 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061
10062 if (p != NULL)
10063 while (*p != NUL)
10064 {
10065#ifdef FEAT_MBYTE
10066 int l;
10067
10068 if (enc_utf8)
10069 {
10070 int c, uc;
10071
10072 c = utf_ptr2char(p);
10073 uc = utf_toupper(c);
10074 l = utf_ptr2len_check(p);
10075 /* TODO: reallocate string when byte count changes. */
10076 if (utf_char2len(uc) == l)
10077 utf_char2bytes(uc, p);
10078 p += l;
10079 }
10080 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
10081 p += l; /* skip multi-byte character */
10082 else
10083#endif
10084 {
10085 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
10086 p++;
10087 }
10088 }
10089}
10090
10091/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000010092 * "tr(string, fromstr, tostr)" function
10093 */
10094 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010095f_tr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010096 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010097 typeval *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000010098{
10099 char_u *instr;
10100 char_u *fromstr;
10101 char_u *tostr;
10102 char_u *p;
10103#ifdef FEAT_MBYTE
10104 int inlen;
10105 int fromlen;
10106 int tolen;
10107 int idx;
10108 char_u *cpstr;
10109 int cplen;
10110 int first = TRUE;
10111#endif
10112 char_u buf[NUMBUFLEN];
10113 char_u buf2[NUMBUFLEN];
10114 garray_T ga;
10115
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010116 instr = get_tv_string(&argvars[0]);
10117 fromstr = get_tv_string_buf(&argvars[1], buf);
10118 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000010119
10120 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010121 rettv->v_type = VAR_STRING;
10122 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000010123 ga_init2(&ga, (int)sizeof(char), 80);
10124
10125#ifdef FEAT_MBYTE
10126 if (!has_mbyte)
10127#endif
10128 /* not multi-byte: fromstr and tostr must be the same length */
10129 if (STRLEN(fromstr) != STRLEN(tostr))
10130 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010131#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000010132error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010133#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000010134 EMSG2(_(e_invarg2), fromstr);
10135 ga_clear(&ga);
10136 return;
10137 }
10138
10139 /* fromstr and tostr have to contain the same number of chars */
10140 while (*instr != NUL)
10141 {
10142#ifdef FEAT_MBYTE
10143 if (has_mbyte)
10144 {
10145 inlen = mb_ptr2len_check(instr);
10146 cpstr = instr;
10147 cplen = inlen;
10148 idx = 0;
10149 for (p = fromstr; *p != NUL; p += fromlen)
10150 {
10151 fromlen = mb_ptr2len_check(p);
10152 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
10153 {
10154 for (p = tostr; *p != NUL; p += tolen)
10155 {
10156 tolen = mb_ptr2len_check(p);
10157 if (idx-- == 0)
10158 {
10159 cplen = tolen;
10160 cpstr = p;
10161 break;
10162 }
10163 }
10164 if (*p == NUL) /* tostr is shorter than fromstr */
10165 goto error;
10166 break;
10167 }
10168 ++idx;
10169 }
10170
10171 if (first && cpstr == instr)
10172 {
10173 /* Check that fromstr and tostr have the same number of
10174 * (multi-byte) characters. Done only once when a character
10175 * of instr doesn't appear in fromstr. */
10176 first = FALSE;
10177 for (p = tostr; *p != NUL; p += tolen)
10178 {
10179 tolen = mb_ptr2len_check(p);
10180 --idx;
10181 }
10182 if (idx != 0)
10183 goto error;
10184 }
10185
10186 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000010187 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000010188 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000010189
10190 instr += inlen;
10191 }
10192 else
10193#endif
10194 {
10195 /* When not using multi-byte chars we can do it faster. */
10196 p = vim_strchr(fromstr, *instr);
10197 if (p != NULL)
10198 ga_append(&ga, tostr[p - fromstr]);
10199 else
10200 ga_append(&ga, *instr);
10201 ++instr;
10202 }
10203 }
10204
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010205 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000010206}
10207
10208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 * "type(expr)" function
10210 */
10211 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010212f_type(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010213 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010214 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010216 if (argvars[0].v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010217 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010219 rettv->vval.v_number = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220}
10221
10222/*
10223 * "virtcol(string)" function
10224 */
10225 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010226f_virtcol(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010227 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010228 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229{
10230 colnr_T vcol = 0;
10231 pos_T *fp;
10232
10233 fp = var2fpos(&argvars[0], FALSE);
10234 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
10235 {
10236 getvvcol(curwin, fp, NULL, NULL, &vcol);
10237 ++vcol;
10238 }
10239
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010240 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241}
10242
10243/*
10244 * "visualmode()" function
10245 */
10246/*ARGSUSED*/
10247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010248f_visualmode(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010249 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010250 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251{
10252#ifdef FEAT_VISUAL
10253 char_u str[2];
10254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010255 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256 str[0] = curbuf->b_visual_mode_eval;
10257 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010258 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259
10260 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010261 if ((argvars[0].v_type == VAR_NUMBER
10262 && argvars[0].vval.v_number != 0)
10263 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010264 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 curbuf->b_visual_mode_eval = NUL;
10266#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010267 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268#endif
10269}
10270
10271/*
10272 * "winbufnr(nr)" function
10273 */
10274 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010275f_winbufnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010276 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010277 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010278{
10279 win_T *wp;
10280
10281 wp = find_win_by_nr(&argvars[0]);
10282 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010283 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010284 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010285 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010286}
10287
10288/*
10289 * "wincol()" function
10290 */
10291/*ARGSUSED*/
10292 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010293f_wincol(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010294 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010295 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296{
10297 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010298 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299}
10300
10301/*
10302 * "winheight(nr)" function
10303 */
10304 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010305f_winheight(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010306 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010307 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308{
10309 win_T *wp;
10310
10311 wp = find_win_by_nr(&argvars[0]);
10312 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010313 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010315 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316}
10317
10318/*
10319 * "winline()" function
10320 */
10321/*ARGSUSED*/
10322 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010323f_winline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010324 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010325 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326{
10327 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010328 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010329}
10330
10331/*
10332 * "winnr()" function
10333 */
10334/* ARGSUSED */
10335 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010336f_winnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010337 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010338 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339{
10340 int nr = 1;
10341#ifdef FEAT_WINDOWS
10342 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010343 win_T *twin = curwin;
10344 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010346 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010347 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010348 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010349 if (STRCMP(arg, "$") == 0)
10350 twin = lastwin;
10351 else if (STRCMP(arg, "#") == 0)
10352 {
10353 twin = prevwin;
10354 if (prevwin == NULL)
10355 nr = 0;
10356 }
10357 else
10358 {
10359 EMSG2(_(e_invexpr2), arg);
10360 nr = 0;
10361 }
10362 }
10363
10364 if (nr > 0)
10365 for (wp = firstwin; wp != twin; wp = wp->w_next)
10366 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010368 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369}
10370
10371/*
10372 * "winrestcmd()" function
10373 */
10374/* ARGSUSED */
10375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010376f_winrestcmd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010377 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010378 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379{
10380#ifdef FEAT_WINDOWS
10381 win_T *wp;
10382 int winnr = 1;
10383 garray_T ga;
10384 char_u buf[50];
10385
10386 ga_init2(&ga, (int)sizeof(char), 70);
10387 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10388 {
10389 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
10390 ga_concat(&ga, buf);
10391# ifdef FEAT_VERTSPLIT
10392 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
10393 ga_concat(&ga, buf);
10394# endif
10395 ++winnr;
10396 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000010397 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010399 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010401 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010403 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404}
10405
10406/*
10407 * "winwidth(nr)" function
10408 */
10409 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010410f_winwidth(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010411 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010412 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413{
10414 win_T *wp;
10415
10416 wp = find_win_by_nr(&argvars[0]);
10417 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010418 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010419 else
10420#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010421 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010422#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010423 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424#endif
10425}
10426
10427 static win_T *
10428find_win_by_nr(vp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010429 typeval *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430{
10431#ifdef FEAT_WINDOWS
10432 win_T *wp;
10433#endif
10434 int nr;
10435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010436 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437
10438#ifdef FEAT_WINDOWS
10439 if (nr == 0)
10440 return curwin;
10441
10442 for (wp = firstwin; wp != NULL; wp = wp->w_next)
10443 if (--nr <= 0)
10444 break;
10445 return wp;
10446#else
10447 if (nr == 0 || nr == 1)
10448 return curwin;
10449 return NULL;
10450#endif
10451}
10452
10453/*
10454 * Translate a String variable into a position.
10455 */
10456 static pos_T *
10457var2fpos(varp, lnum)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010458 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 int lnum; /* TRUE when $ is last line */
10460{
10461 char_u *name;
10462 static pos_T pos;
10463 pos_T *pp;
10464
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010465 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010466 if (name[0] == '.') /* cursor */
10467 return &curwin->w_cursor;
10468 if (name[0] == '\'') /* mark */
10469 {
10470 pp = getmark(name[1], FALSE);
10471 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
10472 return NULL;
10473 return pp;
10474 }
10475 if (name[0] == '$') /* last column or line */
10476 {
10477 if (lnum)
10478 {
10479 pos.lnum = curbuf->b_ml.ml_line_count;
10480 pos.col = 0;
10481 }
10482 else
10483 {
10484 pos.lnum = curwin->w_cursor.lnum;
10485 pos.col = (colnr_T)STRLEN(ml_get_curline());
10486 }
10487 return &pos;
10488 }
10489 return NULL;
10490}
10491
10492/*
10493 * Get the length of an environment variable name.
10494 * Advance "arg" to the first character after the name.
10495 * Return 0 for error.
10496 */
10497 static int
10498get_env_len(arg)
10499 char_u **arg;
10500{
10501 char_u *p;
10502 int len;
10503
10504 for (p = *arg; vim_isIDc(*p); ++p)
10505 ;
10506 if (p == *arg) /* no name found */
10507 return 0;
10508
10509 len = (int)(p - *arg);
10510 *arg = p;
10511 return len;
10512}
10513
10514/*
10515 * Get the length of the name of a function or internal variable.
10516 * "arg" is advanced to the first non-white character after the name.
10517 * Return 0 if something is wrong.
10518 */
10519 static int
10520get_id_len(arg)
10521 char_u **arg;
10522{
10523 char_u *p;
10524 int len;
10525
10526 /* Find the end of the name. */
10527 for (p = *arg; eval_isnamec(*p); ++p)
10528 ;
10529 if (p == *arg) /* no name found */
10530 return 0;
10531
10532 len = (int)(p - *arg);
10533 *arg = skipwhite(p);
10534
10535 return len;
10536}
10537
10538/*
10539 * Get the length of the name of a function.
10540 * "arg" is advanced to the first non-white character after the name.
10541 * Return 0 if something is wrong.
10542 * If the name contains 'magic' {}'s, expand them and return the
10543 * expanded name in an allocated string via 'alias' - caller must free.
10544 */
10545 static int
10546get_func_len(arg, alias, evaluate)
10547 char_u **arg;
10548 char_u **alias;
10549 int evaluate;
10550{
10551 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010552 char_u *p;
10553 char_u *expr_start;
10554 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555
10556 *alias = NULL; /* default to no alias */
10557
10558 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
10559 && (*arg)[2] == (int)KE_SNR)
10560 {
10561 /* hard coded <SNR>, already translated */
10562 *arg += 3;
10563 return get_id_len(arg) + 3;
10564 }
10565 len = eval_fname_script(*arg);
10566 if (len > 0)
10567 {
10568 /* literal "<SID>", "s:" or "<SNR>" */
10569 *arg += len;
10570 }
10571
Bram Moolenaar071d4272004-06-13 20:20:40 +000010572 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010573 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010575 p = find_name_end(*arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010576 if (expr_start != NULL)
10577 {
10578 char_u *temp_string;
10579
10580 if (!evaluate)
10581 {
10582 len += (int)(p - *arg);
10583 *arg = skipwhite(p);
10584 return len;
10585 }
10586
10587 /*
10588 * Include any <SID> etc in the expanded string:
10589 * Thus the -len here.
10590 */
10591 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
10592 if (temp_string == NULL)
10593 return 0;
10594 *alias = temp_string;
10595 *arg = skipwhite(p);
10596 return (int)STRLEN(temp_string);
10597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598
10599 len += get_id_len(arg);
10600 if (len == 0)
10601 EMSG2(_(e_invexpr2), *arg);
10602
10603 return len;
10604}
10605
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010606/*
10607 * Find the end of a variable or function name, taking care of magic braces.
10608 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
10609 * start and end of the first magic braces item.
10610 * Return a pointer to just after the name. Equal to "arg" if there is no
10611 * valid name.
10612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010614find_name_end(arg, expr_start, expr_end, incl_br)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010615 char_u *arg;
10616 char_u **expr_start;
10617 char_u **expr_end;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010618 int incl_br; /* Include [] indexes */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010620 int mb_nest = 0;
10621 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 char_u *p;
10623
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010624 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010625 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010626 *expr_start = NULL;
10627 *expr_end = NULL;
10628 }
10629
10630 for (p = arg; *p != NUL
10631 && (eval_isnamec(*p)
10632 || (*p == '[' && incl_br)
10633 || mb_nest != 0
10634 || br_nest != 0); ++p)
10635 {
10636 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010637 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010638 if (*p == '[')
10639 ++br_nest;
10640 else if (*p == ']')
10641 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010643 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010645 if (*p == '{')
10646 {
10647 mb_nest++;
10648 if (expr_start != NULL && *expr_start == NULL)
10649 *expr_start = p;
10650 }
10651 else if (*p == '}')
10652 {
10653 mb_nest--;
10654 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
10655 *expr_end = p;
10656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010658 }
10659
10660 return p;
10661}
10662
10663/*
10664 * Return TRUE if character "c" can be used in a variable or function name.
10665 */
10666 static int
10667eval_isnamec(c)
10668 int c;
10669{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010670 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == '{' || c == '}');
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671}
10672
10673/*
10674 * Find a v: variable.
10675 * Return it's index, or -1 if not found.
10676 */
10677 static int
10678find_vim_var(name, len)
10679 char_u *name;
10680 int len; /* length of "name" */
10681{
10682 char_u *vname;
10683 int vlen;
10684 int i;
10685
10686 /*
10687 * Ignore "v:" for old built-in variables, require it for new ones.
10688 */
10689 if (name[0] == 'v' && name[1] == ':')
10690 {
10691 vname = name + 2;
10692 vlen = len - 2;
10693 }
10694 else
10695 {
10696 vname = name;
10697 vlen = len;
10698 }
10699 for (i = 0; i < VV_LEN; ++i)
10700 if (vlen == vimvars[i].len && STRCMP(vname, vimvars[i].name) == 0
10701 && ((vimvars[i].flags & VV_COMPAT) || vname != name))
10702 return i;
10703 return -1;
10704}
10705
10706/*
10707 * Set number v: variable to "val".
10708 */
10709 void
10710set_vim_var_nr(idx, val)
10711 int idx;
10712 long val;
10713{
10714 vimvars[idx].val = (char_u *)val;
10715}
10716
10717/*
10718 * Get number v: variable value;
10719 */
10720 long
10721get_vim_var_nr(idx)
10722 int idx;
10723{
10724 return (long)vimvars[idx].val;
10725}
10726
10727/*
10728 * Set v:count, v:count1 and v:prevcount.
10729 */
10730 void
10731set_vcount(count, count1)
10732 long count;
10733 long count1;
10734{
10735 vimvars[VV_PREVCOUNT].val = vimvars[VV_COUNT].val;
10736 vimvars[VV_COUNT].val = (char_u *)count;
10737 vimvars[VV_COUNT1].val = (char_u *)count1;
10738}
10739
10740/*
10741 * Set string v: variable to a copy of "val".
10742 */
10743 void
10744set_vim_var_string(idx, val, len)
10745 int idx;
10746 char_u *val;
10747 int len; /* length of "val" to use or -1 (whole string) */
10748{
10749 vim_free(vimvars[idx].val);
10750 if (val == NULL)
10751 vimvars[idx].val = NULL;
10752 else if (len == -1)
10753 vimvars[idx].val = vim_strsave(val);
10754 else
10755 vimvars[idx].val = vim_strnsave(val, len);
10756}
10757
10758/*
10759 * Set v:register if needed.
10760 */
10761 void
10762set_reg_var(c)
10763 int c;
10764{
10765 char_u regname;
10766
10767 if (c == 0 || c == ' ')
10768 regname = '"';
10769 else
10770 regname = c;
10771 /* Avoid free/alloc when the value is already right. */
10772 if (vimvars[VV_REG].val == NULL || vimvars[VV_REG].val[0] != c)
10773 set_vim_var_string(VV_REG, &regname, 1);
10774}
10775
10776/*
10777 * Get or set v:exception. If "oldval" == NULL, return the current value.
10778 * Otherwise, restore the value to "oldval" and return NULL.
10779 * Must always be called in pairs to save and restore v:exception! Does not
10780 * take care of memory allocations.
10781 */
10782 char_u *
10783v_exception(oldval)
10784 char_u *oldval;
10785{
10786 if (oldval == NULL)
10787 return vimvars[VV_EXCEPTION].val;
10788
10789 vimvars[VV_EXCEPTION].val = oldval;
10790 return NULL;
10791}
10792
10793/*
10794 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
10795 * Otherwise, restore the value to "oldval" and return NULL.
10796 * Must always be called in pairs to save and restore v:throwpoint! Does not
10797 * take care of memory allocations.
10798 */
10799 char_u *
10800v_throwpoint(oldval)
10801 char_u *oldval;
10802{
10803 if (oldval == NULL)
10804 return vimvars[VV_THROWPOINT].val;
10805
10806 vimvars[VV_THROWPOINT].val = oldval;
10807 return NULL;
10808}
10809
10810#if defined(FEAT_AUTOCMD) || defined(PROTO)
10811/*
10812 * Set v:cmdarg.
10813 * If "eap" != NULL, use "eap" to generate the value and return the old value.
10814 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
10815 * Must always be called in pairs!
10816 */
10817 char_u *
10818set_cmdarg(eap, oldarg)
10819 exarg_T *eap;
10820 char_u *oldarg;
10821{
10822 char_u *oldval;
10823 char_u *newval;
10824 unsigned len;
10825
10826 oldval = vimvars[VV_CMDARG].val;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000010827 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000010829 vim_free(oldval);
10830 vimvars[VV_CMDARG].val = oldarg;
10831 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 }
10833
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000010834 if (eap->force_bin == FORCE_BIN)
10835 len = 6;
10836 else if (eap->force_bin == FORCE_NOBIN)
10837 len = 8;
10838 else
10839 len = 0;
10840 if (eap->force_ff != 0)
10841 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
10842# ifdef FEAT_MBYTE
10843 if (eap->force_enc != 0)
10844 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
10845# endif
10846
10847 newval = alloc(len + 1);
10848 if (newval == NULL)
10849 return NULL;
10850
10851 if (eap->force_bin == FORCE_BIN)
10852 sprintf((char *)newval, " ++bin");
10853 else if (eap->force_bin == FORCE_NOBIN)
10854 sprintf((char *)newval, " ++nobin");
10855 else
10856 *newval = NUL;
10857 if (eap->force_ff != 0)
10858 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
10859 eap->cmd + eap->force_ff);
10860# ifdef FEAT_MBYTE
10861 if (eap->force_enc != 0)
10862 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
10863 eap->cmd + eap->force_enc);
10864# endif
10865 vimvars[VV_CMDARG].val = newval;
10866 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867}
10868#endif
10869
10870/*
10871 * Get the value of internal variable "name".
10872 * Return OK or FAIL.
10873 */
10874 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010875get_var_tv(name, len, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 char_u *name;
10877 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010878 typeval *rettv; /* NULL when only checking existence */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879{
10880 int ret = OK;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010881 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 VAR v;
10883 int cc;
10884 int i;
10885
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010886 tv.v_type = VAR_UNKNOWN;
10887
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 /* truncate the name, so that we can use strcmp() */
10889 cc = name[len];
10890 name[len] = NUL;
10891
10892 /*
10893 * Check for "b:changedtick".
10894 */
10895 if (STRCMP(name, "b:changedtick") == 0)
10896 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010897 tv.v_type = VAR_NUMBER;
10898 tv.vval.v_number = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 }
10900
10901 /*
10902 * Check for built-in v: variables.
10903 */
10904 else if ((i = find_vim_var(name, len)) >= 0)
10905 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010906 tv.v_type = vimvars[i].type;
10907 if (tv.v_type == VAR_NUMBER)
10908 tv.vval.v_number = (long)vimvars[i].val;
10909 else
10910 tv.vval.v_string = vimvars[i].val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911 }
10912
10913 /*
10914 * Check for user-defined variables.
10915 */
10916 else
10917 {
10918 v = find_var(name, FALSE);
10919 if (v != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010920 tv = v->tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921 }
10922
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010923 if (tv.v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010925 if (rettv != NULL)
10926 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 ret = FAIL;
10928 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010929 else if (rettv != NULL)
10930 copy_tv(&tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931
10932 name[len] = cc;
10933
10934 return ret;
10935}
10936
10937/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010938 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
10939 * value).
10940 */
10941 static typeval *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010942alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010943{
10944 return (typeval *)alloc_clear((unsigned)sizeof(typeval));
10945}
10946
10947/*
10948 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010949 * The string "s" must have been allocated, it is consumed.
10950 * Return NULL for out of memory, the variable otherwise.
10951 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010952 static typeval *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010953alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 char_u *s;
10955{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010956 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010958 rettv = alloc_tv();
10959 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010961 rettv->v_type = VAR_STRING;
10962 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963 }
10964 else
10965 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010966 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967}
10968
10969/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010970 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 */
10972 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010973free_tv(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010974 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975{
10976 if (varp != NULL)
10977 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010978 switch (varp->v_type)
10979 {
10980 case VAR_STRING:
10981 case VAR_FUNC:
10982 vim_free(varp->vval.v_string);
10983 break;
10984 case VAR_LIST:
10985 list_unref(varp->vval.v_list);
10986 break;
10987 default:
10988 break;
10989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 vim_free(varp);
10991 }
10992}
10993
10994/*
10995 * Free the memory for a variable value and set the value to NULL or 0.
10996 */
10997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010998clear_tv(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010999 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000{
11001 if (varp != NULL)
11002 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011003 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011005 case VAR_STRING:
11006 case VAR_FUNC:
11007 vim_free(varp->vval.v_string);
11008 varp->vval.v_string = NULL;
11009 break;
11010 case VAR_LIST:
11011 list_unref(varp->vval.v_list);
11012 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011013 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011014 varp->vval.v_number = 0;
11015 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011016 case VAR_UNKNOWN:
11017 break;
11018 default:
11019 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 }
11022}
11023
11024/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011025 * Set the value of a variable to NULL without freeing items.
11026 */
11027 static void
11028init_tv(varp)
11029 typeval *varp;
11030{
11031 if (varp != NULL)
11032 vim_memset(varp, 0, sizeof(typeval));
11033}
11034
11035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036 * Get the number value of a variable.
11037 * If it is a String variable, uses vim_str2nr().
11038 */
11039 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011040get_tv_number(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011041 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011043 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011044
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011045 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011047 case VAR_NUMBER:
11048 n = (long)(varp->vval.v_number);
11049 break;
11050 case VAR_FUNC:
11051 EMSG(_("E999: Using function reference as a number"));
11052 break;
11053 case VAR_STRING:
11054 if (varp->vval.v_string != NULL)
11055 vim_str2nr(varp->vval.v_string, NULL, NULL,
11056 TRUE, TRUE, &n, NULL);
11057 break;
11058 default:
11059 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011061 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062}
11063
11064/*
11065 * Get the lnum from the first argument. Also accepts ".", "$", etc.
11066 */
11067 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011068get_tv_lnum(argvars)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011069 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011071 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 linenr_T lnum;
11073
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011074 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 if (lnum == 0) /* no valid number, try using line() */
11076 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011077 rettv.v_type = VAR_NUMBER;
11078 f_line(argvars, &rettv);
11079 lnum = rettv.vval.v_number;
11080 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 }
11082 return lnum;
11083}
11084
11085/*
11086 * Get the string value of a variable.
11087 * If it is a Number variable, the number is converted into a string.
11088 * get_var_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
11089 * get_var_string_buf() uses a given buffer.
11090 * If the String variable has never been set, return an empty string.
11091 * Never returns NULL;
11092 */
11093 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011094get_tv_string(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011095 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011096{
11097 static char_u mybuf[NUMBUFLEN];
11098
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011099 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100}
11101
11102 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011103get_tv_string_buf(varp, buf)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011104 typeval *varp;
11105 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011107 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011109 case VAR_NUMBER:
11110 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
11111 return buf;
11112 case VAR_FUNC:
11113 EMSG(_("E99: using Funcref as a String"));
11114 break;
11115 case VAR_LIST:
11116 EMSG(_("E99: using List as a String"));
11117 break;
11118 case VAR_STRING:
11119 if (varp->vval.v_string != NULL)
11120 return varp->vval.v_string;
11121 break;
11122 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011123 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011124 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011125 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011126 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127}
11128
11129/*
11130 * Find variable "name" in the list of variables.
11131 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011132 * Careful: "a:0" variables don't have a name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011133 */
11134 static VAR
11135find_var(name, writing)
11136 char_u *name;
11137 int writing;
11138{
11139 int i;
11140 char_u *varname;
11141 garray_T *gap;
11142
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 if (name[0] == 'a' && name[1] == ':')
11144 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011145 /* Function arguments "a:".
11146 * NOTE: We use a typecast, because function arguments don't have a
11147 * name. The caller must not try to access the name! */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148 if (writing)
11149 {
11150 EMSG2(_(e_readonlyvar), name);
11151 return NULL;
11152 }
11153 name += 2;
11154 if (current_funccal == NULL)
11155 return NULL;
11156 if (VIM_ISDIGIT(*name))
11157 {
11158 i = atol((char *)name);
11159 if (i == 0) /* a:0 */
11160 return &current_funccal->a0_var;
11161 i += current_funccal->func->args.ga_len;
11162 if (i > current_funccal->argcount) /* a:999 */
11163 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011164 return (VAR)&(current_funccal->argvars[i - 1]); /* a:1, a:2, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165 }
11166 if (STRCMP(name, "firstline") == 0)
11167 return &(current_funccal->firstline);
11168 if (STRCMP(name, "lastline") == 0)
11169 return &(current_funccal->lastline);
11170 for (i = 0; i < current_funccal->func->args.ga_len; ++i)
11171 if (STRCMP(name, ((char_u **)
11172 (current_funccal->func->args.ga_data))[i]) == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011173 return (VAR)&(current_funccal->argvars[i]); /* a:name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011174 return NULL;
11175 }
11176
11177 gap = find_var_ga(name, &varname);
11178 if (gap == NULL)
11179 return NULL;
11180 return find_var_in_ga(gap, varname);
11181}
11182
11183 static VAR
11184find_var_in_ga(gap, varname)
11185 garray_T *gap;
11186 char_u *varname;
11187{
11188 int i;
11189
11190 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011191 if (VAR_GAP_ENTRY(i, gap).v_name != NULL
11192 && STRCMP(VAR_GAP_ENTRY(i, gap).v_name, varname) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011193 break;
11194 if (i < 0)
11195 return NULL;
11196 return &VAR_GAP_ENTRY(i, gap);
11197}
11198
11199/*
11200 * Find the growarray and start of name without ':' for a variable name.
11201 */
11202 static garray_T *
11203find_var_ga(name, varname)
11204 char_u *name;
11205 char_u **varname;
11206{
11207 if (name[1] != ':')
11208 {
11209 /* If not "x:name" there must not be any ":" in the name. */
11210 if (vim_strchr(name, ':') != NULL)
11211 return NULL;
11212 *varname = name;
11213 if (current_funccal == NULL)
11214 return &variables; /* global variable */
11215 return &current_funccal->l_vars; /* local function variable */
11216 }
11217 *varname = name + 2;
11218 if (*name == 'b') /* buffer variable */
11219 return &curbuf->b_vars;
11220 if (*name == 'w') /* window variable */
11221 return &curwin->w_vars;
11222 if (*name == 'g') /* global variable */
11223 return &variables;
11224 if (*name == 'l' && current_funccal != NULL)/* local function variable */
11225 return &current_funccal->l_vars;
11226 if (*name == 's' /* script variable */
11227 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
11228 return &SCRIPT_VARS(current_SID);
11229 return NULL;
11230}
11231
11232/*
11233 * Get the string value of a (global/local) variable.
11234 * Returns NULL when it doesn't exist.
11235 */
11236 char_u *
11237get_var_value(name)
11238 char_u *name;
11239{
11240 VAR v;
11241
11242 v = find_var(name, FALSE);
11243 if (v == NULL)
11244 return NULL;
11245 return get_var_string(v);
11246}
11247
11248/*
11249 * Allocate a new growarry for a sourced script. It will be used while
11250 * sourcing this script and when executing functions defined in the script.
11251 */
11252 void
11253new_script_vars(id)
11254 scid_T id;
11255{
11256 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
11257 {
11258 while (ga_scripts.ga_len < id)
11259 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011260 vars_init(&SCRIPT_VARS(ga_scripts.ga_len + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011261 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262 }
11263 }
11264}
11265
11266/*
11267 * Initialize internal variables for use.
11268 */
11269 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011270vars_init(gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011271 garray_T *gap;
11272{
11273 ga_init2(gap, (int)sizeof(var), 4);
11274}
11275
11276/*
11277 * Clean up a list of internal variables.
11278 */
11279 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011280vars_clear(gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011281 garray_T *gap;
11282{
11283 int i;
11284
11285 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011286 clear_var(&VAR_GAP_ENTRY(i, gap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011287 ga_clear(gap);
11288}
11289
11290 static void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011291clear_var(v)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011292 VAR v;
11293{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011294 vim_free(v->v_name);
11295 v->v_name = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011296 clear_tv(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011297}
11298
11299/*
11300 * List the value of one internal variable.
11301 */
11302 static void
11303list_one_var(v, prefix)
11304 VAR v;
11305 char_u *prefix;
11306{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011307 char_u *tofree;
11308 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011309 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011310
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011311 s = tv2string(&v->tv, &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011312 list_one_var_a(prefix, v->v_name, v->tv.v_type,
11313 s == NULL ? (char_u *)"" : s);
11314 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011315}
11316
11317/*
11318 * List the value of one "v:" variable.
11319 */
11320 static void
11321list_vim_var(i)
11322 int i; /* index in vimvars[] */
11323{
11324 char_u *p;
11325 char_u numbuf[NUMBUFLEN];
11326
11327 if (vimvars[i].type == VAR_NUMBER)
11328 {
11329 p = numbuf;
11330 sprintf((char *)p, "%ld", (long)vimvars[i].val);
11331 }
11332 else if (vimvars[i].val == NULL)
11333 p = (char_u *)"";
11334 else
11335 p = vimvars[i].val;
11336 list_one_var_a((char_u *)"v:", (char_u *)vimvars[i].name,
11337 vimvars[i].type, p);
11338}
11339
11340 static void
11341list_one_var_a(prefix, name, type, string)
11342 char_u *prefix;
11343 char_u *name;
11344 int type;
11345 char_u *string;
11346{
11347 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
11348 if (name != NULL) /* "a:" vars don't have a name stored */
11349 msg_puts(name);
11350 msg_putchar(' ');
11351 msg_advance(22);
11352 if (type == VAR_NUMBER)
11353 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011354 else if (type == VAR_FUNC)
11355 msg_putchar('*');
11356 else if (type == VAR_LIST)
11357 {
11358 msg_putchar('[');
11359 if (*string == '[')
11360 ++string;
11361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011362 else
11363 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011364
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011366
11367 if (type == VAR_FUNC)
11368 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369}
11370
11371/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011372 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373 * If the variable already exists, the value is updated.
11374 * Otherwise the variable is created.
11375 */
11376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011377set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011378 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011379 typeval *tv;
11380 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381{
11382 int i;
11383 VAR v;
11384 char_u *varname;
11385 garray_T *gap;
11386
11387 /*
11388 * Handle setting internal v: variables.
11389 */
11390 i = find_vim_var(name, (int)STRLEN(name));
11391 if (i >= 0)
11392 {
11393 if (vimvars[i].flags & VV_RO)
11394 EMSG2(_(e_readonlyvar), name);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011395 else if ((vimvars[i].flags & VV_RO_SBX) && sandbox)
11396 EMSG2(_(e_readonlysbx), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397 else
11398 {
11399 if (vimvars[i].type == VAR_STRING)
11400 {
11401 vim_free(vimvars[i].val);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011402 if (copy || tv->v_type != VAR_STRING)
11403 vimvars[i].val = vim_strsave(get_tv_string(tv));
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011404 else
11405 {
11406 /* Take over the string to avoid an extra alloc/free. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011407 vimvars[i].val = tv->vval.v_string;
11408 tv->vval.v_string = NULL;
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410 }
11411 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011412 vimvars[i].val = (char_u *)get_tv_number(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413 }
11414 return;
11415 }
11416
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011417 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011418 {
11419 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
11420 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
11421 ? name[2] : name[0]))
11422 {
11423 EMSG2(_("E999: Funcref variable name must start with a capital: %s"), name);
11424 return;
11425 }
11426 if (function_exists(name))
11427 {
11428 EMSG2(_("E999: Variable name conflicts with existing function: %s"), name);
11429 return;
11430 }
11431 }
11432
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433 v = find_var(name, TRUE);
11434 if (v != NULL) /* existing variable, only need to free string */
11435 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436 if (v->tv.v_type != tv->v_type
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011437 && !((v->tv.v_type == VAR_STRING
11438 || v->tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011439 && (tv->v_type == VAR_STRING
11440 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011441 {
11442 EMSG2(_("E999: Variable type mismatch for: %s"), name);
11443 return;
11444 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445 clear_tv(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011446 }
11447 else /* add a new variable */
11448 {
11449 gap = find_var_ga(name, &varname);
11450 if (gap == NULL) /* illegal name */
11451 {
11452 EMSG2(_("E461: Illegal variable name: %s"), name);
11453 return;
11454 }
11455
11456 /* Try to use an empty entry */
11457 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011458 if (VAR_GAP_ENTRY(i, gap).v_name == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011459 break;
11460 if (i < 0) /* need to allocate more room */
11461 {
11462 if (ga_grow(gap, 1) == FAIL)
11463 return;
11464 i = gap->ga_len;
11465 }
11466 v = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011467 if ((v->v_name = vim_strsave(varname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468 return;
11469 if (i == gap->ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011472 if (copy || tv->v_type == VAR_NUMBER)
11473 copy_tv(tv, &v->tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011474 else
11475 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011476 v->tv = *tv;
11477 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000011478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479}
11480
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011481/*
11482 * Copy the values from typeval "from" to typeval "to".
11483 * When needed allocates string or increases reference count.
11484 * Does not make a copy of a list!
11485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011487copy_tv(from, to)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011488 typeval *from;
11489 typeval *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011491 to->v_type = from->v_type;
11492 switch (from->v_type)
11493 {
11494 case VAR_NUMBER:
11495 to->vval.v_number = from->vval.v_number;
11496 break;
11497 case VAR_STRING:
11498 case VAR_FUNC:
11499 if (from->vval.v_string == NULL)
11500 to->vval.v_string = NULL;
11501 else
11502 to->vval.v_string = vim_strsave(from->vval.v_string);
11503 break;
11504 case VAR_LIST:
11505 if (from->vval.v_list == NULL)
11506 to->vval.v_list = NULL;
11507 else
11508 {
11509 to->vval.v_list = from->vval.v_list;
11510 ++to->vval.v_list->lv_refcount;
11511 }
11512 break;
11513 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011514 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011515 break;
11516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011517}
11518
11519/*
11520 * ":echo expr1 ..." print each argument separated with a space, add a
11521 * newline at the end.
11522 * ":echon expr1 ..." print each argument plain.
11523 */
11524 void
11525ex_echo(eap)
11526 exarg_T *eap;
11527{
11528 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011529 typeval rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011530 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011531 char_u *p;
11532 int needclr = TRUE;
11533 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011534 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000011535
11536 if (eap->skip)
11537 ++emsg_skip;
11538 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
11539 {
11540 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011541 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542 {
11543 /*
11544 * Report the invalid expression unless the expression evaluation
11545 * has been cancelled due to an aborting error, an interrupt, or an
11546 * exception.
11547 */
11548 if (!aborting())
11549 EMSG2(_(e_invexpr2), p);
11550 break;
11551 }
11552 if (!eap->skip)
11553 {
11554 if (atstart)
11555 {
11556 atstart = FALSE;
11557 /* Call msg_start() after eval1(), evaluating the expression
11558 * may cause a message to appear. */
11559 if (eap->cmdidx == CMD_echo)
11560 msg_start();
11561 }
11562 else if (eap->cmdidx == CMD_echo)
11563 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011564 for (p = tv2string(&rettv, &tofree, numbuf);
11565 *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011566 if (*p == '\n' || *p == '\r' || *p == TAB)
11567 {
11568 if (*p != TAB && needclr)
11569 {
11570 /* remove any text still there from the command */
11571 msg_clr_eos();
11572 needclr = FALSE;
11573 }
11574 msg_putchar_attr(*p, echo_attr);
11575 }
11576 else
11577 {
11578#ifdef FEAT_MBYTE
11579 if (has_mbyte)
11580 {
11581 int i = (*mb_ptr2len_check)(p);
11582
11583 (void)msg_outtrans_len_attr(p, i, echo_attr);
11584 p += i - 1;
11585 }
11586 else
11587#endif
11588 (void)msg_outtrans_len_attr(p, 1, echo_attr);
11589 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011590 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011591 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011592 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011593 arg = skipwhite(arg);
11594 }
11595 eap->nextcmd = check_nextcmd(arg);
11596
11597 if (eap->skip)
11598 --emsg_skip;
11599 else
11600 {
11601 /* remove text that may still be there from the command */
11602 if (needclr)
11603 msg_clr_eos();
11604 if (eap->cmdidx == CMD_echo)
11605 msg_end();
11606 }
11607}
11608
11609/*
11610 * ":echohl {name}".
11611 */
11612 void
11613ex_echohl(eap)
11614 exarg_T *eap;
11615{
11616 int id;
11617
11618 id = syn_name2id(eap->arg);
11619 if (id == 0)
11620 echo_attr = 0;
11621 else
11622 echo_attr = syn_id2attr(id);
11623}
11624
11625/*
11626 * ":execute expr1 ..." execute the result of an expression.
11627 * ":echomsg expr1 ..." Print a message
11628 * ":echoerr expr1 ..." Print an error
11629 * Each gets spaces around each argument and a newline at the end for
11630 * echo commands
11631 */
11632 void
11633ex_execute(eap)
11634 exarg_T *eap;
11635{
11636 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011637 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011638 int ret = OK;
11639 char_u *p;
11640 garray_T ga;
11641 int len;
11642 int save_did_emsg;
11643
11644 ga_init2(&ga, 1, 80);
11645
11646 if (eap->skip)
11647 ++emsg_skip;
11648 while (*arg != NUL && *arg != '|' && *arg != '\n')
11649 {
11650 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011651 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652 {
11653 /*
11654 * Report the invalid expression unless the expression evaluation
11655 * has been cancelled due to an aborting error, an interrupt, or an
11656 * exception.
11657 */
11658 if (!aborting())
11659 EMSG2(_(e_invexpr2), p);
11660 ret = FAIL;
11661 break;
11662 }
11663
11664 if (!eap->skip)
11665 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011666 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667 len = (int)STRLEN(p);
11668 if (ga_grow(&ga, len + 2) == FAIL)
11669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011670 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011671 ret = FAIL;
11672 break;
11673 }
11674 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011675 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000011676 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677 ga.ga_len += len;
11678 }
11679
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011680 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 arg = skipwhite(arg);
11682 }
11683
11684 if (ret != FAIL && ga.ga_data != NULL)
11685 {
11686 if (eap->cmdidx == CMD_echomsg)
11687 MSG_ATTR(ga.ga_data, echo_attr);
11688 else if (eap->cmdidx == CMD_echoerr)
11689 {
11690 /* We don't want to abort following commands, restore did_emsg. */
11691 save_did_emsg = did_emsg;
11692 EMSG((char_u *)ga.ga_data);
11693 if (!force_abort)
11694 did_emsg = save_did_emsg;
11695 }
11696 else if (eap->cmdidx == CMD_execute)
11697 do_cmdline((char_u *)ga.ga_data,
11698 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
11699 }
11700
11701 ga_clear(&ga);
11702
11703 if (eap->skip)
11704 --emsg_skip;
11705
11706 eap->nextcmd = check_nextcmd(arg);
11707}
11708
11709/*
11710 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
11711 * "arg" points to the "&" or '+' when called, to "option" when returning.
11712 * Returns NULL when no option name found. Otherwise pointer to the char
11713 * after the option name.
11714 */
11715 static char_u *
11716find_option_end(arg, opt_flags)
11717 char_u **arg;
11718 int *opt_flags;
11719{
11720 char_u *p = *arg;
11721
11722 ++p;
11723 if (*p == 'g' && p[1] == ':')
11724 {
11725 *opt_flags = OPT_GLOBAL;
11726 p += 2;
11727 }
11728 else if (*p == 'l' && p[1] == ':')
11729 {
11730 *opt_flags = OPT_LOCAL;
11731 p += 2;
11732 }
11733 else
11734 *opt_flags = 0;
11735
11736 if (!ASCII_ISALPHA(*p))
11737 return NULL;
11738 *arg = p;
11739
11740 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
11741 p += 4; /* termcap option */
11742 else
11743 while (ASCII_ISALPHA(*p))
11744 ++p;
11745 return p;
11746}
11747
11748/*
11749 * ":function"
11750 */
11751 void
11752ex_function(eap)
11753 exarg_T *eap;
11754{
11755 char_u *theline;
11756 int j;
11757 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759 char_u *name = NULL;
11760 char_u *p;
11761 char_u *arg;
11762 garray_T newargs;
11763 garray_T newlines;
11764 int varargs = FALSE;
11765 int mustend = FALSE;
11766 int flags = 0;
11767 ufunc_T *fp;
11768 int indent;
11769 int nesting;
11770 char_u *skip_until = NULL;
11771 static char_u e_funcexts[] = N_("E122: Function %s already exists, add ! to replace it");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011772 VAR v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011773
11774 /*
11775 * ":function" without argument: list functions.
11776 */
11777 if (ends_excmd(*eap->arg))
11778 {
11779 if (!eap->skip)
11780 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next)
11781 list_func_head(fp, FALSE);
11782 eap->nextcmd = check_nextcmd(eap->arg);
11783 return;
11784 }
11785
11786 p = eap->arg;
11787 name = trans_function_name(&p, eap->skip, FALSE);
11788 if (name == NULL && !eap->skip)
11789 {
11790 /*
11791 * Return on an invalid expression in braces, unless the expression
11792 * evaluation has been cancelled due to an aborting error, an
11793 * interrupt, or an exception.
11794 */
11795 if (!aborting())
11796 return;
11797 else
11798 eap->skip = TRUE;
11799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011800 /* An error in a function call during evaluation of an expression in magic
11801 * braces should not cause the function not to be defined. */
11802 saved_did_emsg = did_emsg;
11803 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804
11805 /*
11806 * ":function func" with only function name: list function.
11807 */
11808 if (vim_strchr(p, '(') == NULL)
11809 {
11810 if (!ends_excmd(*skipwhite(p)))
11811 {
11812 EMSG(_(e_trailing));
11813 goto erret_name;
11814 }
11815 eap->nextcmd = check_nextcmd(p);
11816 if (eap->nextcmd != NULL)
11817 *p = NUL;
11818 if (!eap->skip && !got_int)
11819 {
11820 fp = find_func(name);
11821 if (fp != NULL)
11822 {
11823 list_func_head(fp, TRUE);
11824 for (j = 0; j < fp->lines.ga_len && !got_int; ++j)
11825 {
11826 msg_putchar('\n');
11827 msg_outnum((long)(j + 1));
11828 if (j < 9)
11829 msg_putchar(' ');
11830 if (j < 99)
11831 msg_putchar(' ');
11832 msg_prt_line(FUNCLINE(fp, j));
11833 out_flush(); /* show a line at a time */
11834 ui_breakcheck();
11835 }
11836 if (!got_int)
11837 {
11838 msg_putchar('\n');
11839 msg_puts((char_u *)" endfunction");
11840 }
11841 }
11842 else
11843 EMSG2(_("E123: Undefined function: %s"), eap->arg);
11844 }
11845 goto erret_name;
11846 }
11847
11848 /*
11849 * ":function name(arg1, arg2)" Define function.
11850 */
11851 p = skipwhite(p);
11852 if (*p != '(')
11853 {
11854 if (!eap->skip)
11855 {
11856 EMSG2(_("E124: Missing '(': %s"), eap->arg);
11857 goto erret_name;
11858 }
11859 /* attempt to continue by skipping some text */
11860 if (vim_strchr(p, '(') != NULL)
11861 p = vim_strchr(p, '(');
11862 }
11863 p = skipwhite(p + 1);
11864
11865 ga_init2(&newargs, (int)sizeof(char_u *), 3);
11866 ga_init2(&newlines, (int)sizeof(char_u *), 3);
11867
11868 /*
11869 * Isolate the arguments: "arg1, arg2, ...)"
11870 */
11871 while (*p != ')')
11872 {
11873 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
11874 {
11875 varargs = TRUE;
11876 p += 3;
11877 mustend = TRUE;
11878 }
11879 else
11880 {
11881 arg = p;
11882 while (ASCII_ISALNUM(*p) || *p == '_')
11883 ++p;
11884 if (arg == p || isdigit(*arg)
11885 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
11886 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
11887 {
11888 if (!eap->skip)
11889 EMSG2(_("E125: Illegal argument: %s"), arg);
11890 break;
11891 }
11892 if (ga_grow(&newargs, 1) == FAIL)
11893 goto erret;
11894 c = *p;
11895 *p = NUL;
11896 arg = vim_strsave(arg);
11897 if (arg == NULL)
11898 goto erret;
11899 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
11900 *p = c;
11901 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011902 if (*p == ',')
11903 ++p;
11904 else
11905 mustend = TRUE;
11906 }
11907 p = skipwhite(p);
11908 if (mustend && *p != ')')
11909 {
11910 if (!eap->skip)
11911 EMSG2(_(e_invarg2), eap->arg);
11912 break;
11913 }
11914 }
11915 ++p; /* skip the ')' */
11916
11917 /* find extra arguments "range" and "abort" */
11918 for (;;)
11919 {
11920 p = skipwhite(p);
11921 if (STRNCMP(p, "range", 5) == 0)
11922 {
11923 flags |= FC_RANGE;
11924 p += 5;
11925 }
11926 else if (STRNCMP(p, "abort", 5) == 0)
11927 {
11928 flags |= FC_ABORT;
11929 p += 5;
11930 }
11931 else
11932 break;
11933 }
11934
11935 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
11936 EMSG(_(e_trailing));
11937
11938 /*
11939 * Read the body of the function, until ":endfunction" is found.
11940 */
11941 if (KeyTyped)
11942 {
11943 /* Check if the function already exists, don't let the user type the
11944 * whole function before telling him it doesn't work! For a script we
11945 * need to skip the body to be able to find what follows. */
11946 if (!eap->skip && !eap->forceit && find_func(name) != NULL)
11947 EMSG2(_(e_funcexts), name);
11948
11949 msg_putchar('\n'); /* don't overwrite the function name */
11950 cmdline_row = msg_row;
11951 }
11952
11953 indent = 2;
11954 nesting = 0;
11955 for (;;)
11956 {
11957 msg_scroll = TRUE;
11958 need_wait_return = FALSE;
11959 if (eap->getline == NULL)
11960 theline = getcmdline(':', 0L, indent);
11961 else
11962 theline = eap->getline(':', eap->cookie, indent);
11963 if (KeyTyped)
11964 lines_left = Rows - 1;
11965 if (theline == NULL)
11966 {
11967 EMSG(_("E126: Missing :endfunction"));
11968 goto erret;
11969 }
11970
11971 if (skip_until != NULL)
11972 {
11973 /* between ":append" and "." and between ":python <<EOF" and "EOF"
11974 * don't check for ":endfunc". */
11975 if (STRCMP(theline, skip_until) == 0)
11976 {
11977 vim_free(skip_until);
11978 skip_until = NULL;
11979 }
11980 }
11981 else
11982 {
11983 /* skip ':' and blanks*/
11984 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
11985 ;
11986
11987 /* Check for "endfunction" (should be more strict...). */
11988 if (STRNCMP(p, "endf", 4) == 0 && nesting-- == 0)
11989 {
11990 vim_free(theline);
11991 break;
11992 }
11993
11994 /* Increase indent inside "if", "while", and "try", decrease
11995 * at "end". */
11996 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
11997 indent -= 2;
11998 else if (STRNCMP(p, "if", 2) == 0 || STRNCMP(p, "wh", 2) == 0
11999 || STRNCMP(p, "try", 3) == 0)
12000 indent += 2;
12001
12002 /* Check for defining a function inside this function. */
12003 if (STRNCMP(p, "fu", 2) == 0)
12004 {
12005 p = skipwhite(skiptowhite(p));
12006 p += eval_fname_script(p);
12007 if (ASCII_ISALPHA(*p))
12008 {
12009 vim_free(trans_function_name(&p, TRUE, FALSE));
12010 if (*skipwhite(p) == '(')
12011 {
12012 ++nesting;
12013 indent += 2;
12014 }
12015 }
12016 }
12017
12018 /* Check for ":append" or ":insert". */
12019 p = skip_range(p, NULL);
12020 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
12021 || (p[0] == 'i'
12022 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
12023 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
12024 skip_until = vim_strsave((char_u *)".");
12025
12026 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
12027 arg = skipwhite(skiptowhite(p));
12028 if (arg[0] == '<' && arg[1] =='<'
12029 && ((p[0] == 'p' && p[1] == 'y'
12030 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
12031 || (p[0] == 'p' && p[1] == 'e'
12032 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
12033 || (p[0] == 't' && p[1] == 'c'
12034 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
12035 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
12036 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012037 || (p[0] == 'm' && p[1] == 'z'
12038 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012039 ))
12040 {
12041 /* ":python <<" continues until a dot, like ":append" */
12042 p = skipwhite(arg + 2);
12043 if (*p == NUL)
12044 skip_until = vim_strsave((char_u *)".");
12045 else
12046 skip_until = vim_strsave(p);
12047 }
12048 }
12049
12050 /* Add the line to the function. */
12051 if (ga_grow(&newlines, 1) == FAIL)
12052 goto erret;
12053 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
12054 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055 }
12056
12057 /* Don't define the function when skipping commands or when an error was
12058 * detected. */
12059 if (eap->skip || did_emsg)
12060 goto erret;
12061
12062 /*
12063 * If there are no errors, add the function
12064 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012065 v = find_var(name, FALSE);
12066 if (v != NULL && v->tv.v_type == VAR_FUNC)
12067 {
12068 EMSG2(_("E999: Function name conflicts with variable: %s"), name);
12069 goto erret;
12070 }
12071
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072 fp = find_func(name);
12073 if (fp != NULL)
12074 {
12075 if (!eap->forceit)
12076 {
12077 EMSG2(_(e_funcexts), name);
12078 goto erret;
12079 }
12080 if (fp->calls)
12081 {
12082 EMSG2(_("E127: Cannot redefine function %s: It is in use"), name);
12083 goto erret;
12084 }
12085 /* redefine existing function */
12086 ga_clear_strings(&(fp->args));
12087 ga_clear_strings(&(fp->lines));
12088 vim_free(name);
12089 }
12090 else
12091 {
12092 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T));
12093 if (fp == NULL)
12094 goto erret;
12095 /* insert the new function in the function list */
12096 fp->next = firstfunc;
12097 firstfunc = fp;
12098 fp->name = name;
12099 }
12100 fp->args = newargs;
12101 fp->lines = newlines;
12102 fp->varargs = varargs;
12103 fp->flags = flags;
12104 fp->calls = 0;
12105 fp->script_ID = current_SID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107 vim_free(skip_until);
12108 return;
12109
12110erret:
12111 vim_free(skip_until);
12112 ga_clear_strings(&newargs);
12113 ga_clear_strings(&newlines);
12114erret_name:
12115 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012117}
12118
12119/*
12120 * Get a function name, translating "<SID>" and "<SNR>".
12121 * Returns the function name in allocated memory, or NULL for failure.
12122 * Advances "pp" to just after the function name (if no error).
12123 */
12124 static char_u *
12125trans_function_name(pp, skip, internal)
12126 char_u **pp;
12127 int skip; /* only find the end, don't evaluate */
12128 int internal; /* TRUE if internal function name OK */
12129{
12130 char_u *name;
12131 char_u *start;
12132 char_u *end;
12133 int lead;
12134 char_u sid_buf[20];
12135 char_u *temp_string = NULL;
12136 char_u *expr_start, *expr_end;
12137 int len;
12138
12139 /* A name starting with "<SID>" or "<SNR>" is local to a script. */
12140 start = *pp;
12141 lead = eval_fname_script(start);
12142 if (lead > 0)
12143 start += lead;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012144 end = find_name_end(start, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145 if (end == start)
12146 {
12147 if (!skip)
12148 EMSG(_("E129: Function name required"));
12149 return NULL;
12150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012151 if (expr_start != NULL && !skip)
12152 {
12153 /* expand magic curlies */
12154 temp_string = make_expanded_name(start, expr_start, expr_end, end);
12155 if (temp_string == NULL)
12156 {
12157 /*
12158 * Report an invalid expression in braces, unless the expression
12159 * evaluation has been cancelled due to an aborting error, an
12160 * interrupt, or an exception.
12161 */
12162 if (!aborting())
12163 EMSG2(_(e_invarg2), start);
12164 else
12165 *pp = end;
12166 return NULL;
12167 }
12168 start = temp_string;
12169 len = (int)STRLEN(temp_string);
12170 }
12171 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172 len = (int)(end - start);
12173
12174 /*
12175 * Copy the function name to allocated memory.
12176 * Accept <SID>name() inside a script, translate into <SNR>123_name().
12177 * Accept <SNR>123_name() outside a script.
12178 */
12179 if (skip)
12180 lead = 0; /* do nothing */
12181 else if (lead > 0)
12182 {
12183 lead = 3;
12184 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
12185 {
12186 if (current_SID <= 0)
12187 {
12188 EMSG(_(e_usingsid));
12189 return NULL;
12190 }
12191 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
12192 lead += (int)STRLEN(sid_buf);
12193 }
12194 }
12195 else if (!internal && !ASCII_ISUPPER(*start))
12196 {
12197 EMSG2(_("E128: Function name must start with a capital: %s"), start);
12198 return NULL;
12199 }
12200 name = alloc((unsigned)(len + lead + 1));
12201 if (name != NULL)
12202 {
12203 if (lead > 0)
12204 {
12205 name[0] = K_SPECIAL;
12206 name[1] = KS_EXTRA;
12207 name[2] = (int)KE_SNR;
12208 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
12209 STRCPY(name + 3, sid_buf);
12210 }
12211 mch_memmove(name + lead, start, (size_t)len);
12212 name[len + lead] = NUL;
12213 }
12214 *pp = end;
12215
12216 vim_free(temp_string);
12217 return name;
12218}
12219
12220/*
12221 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
12222 * Return 2 if "p" starts with "s:".
12223 * Return 0 otherwise.
12224 */
12225 static int
12226eval_fname_script(p)
12227 char_u *p;
12228{
12229 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
12230 || STRNICMP(p + 1, "SNR>", 4) == 0))
12231 return 5;
12232 if (p[0] == 's' && p[1] == ':')
12233 return 2;
12234 return 0;
12235}
12236
12237/*
12238 * Return TRUE if "p" starts with "<SID>" or "s:".
12239 * Only works if eval_fname_script() returned non-zero for "p"!
12240 */
12241 static int
12242eval_fname_sid(p)
12243 char_u *p;
12244{
12245 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
12246}
12247
12248/*
12249 * List the head of the function: "name(arg1, arg2)".
12250 */
12251 static void
12252list_func_head(fp, indent)
12253 ufunc_T *fp;
12254 int indent;
12255{
12256 int j;
12257
12258 msg_start();
12259 if (indent)
12260 MSG_PUTS(" ");
12261 MSG_PUTS("function ");
12262 if (fp->name[0] == K_SPECIAL)
12263 {
12264 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
12265 msg_puts(fp->name + 3);
12266 }
12267 else
12268 msg_puts(fp->name);
12269 msg_putchar('(');
12270 for (j = 0; j < fp->args.ga_len; ++j)
12271 {
12272 if (j)
12273 MSG_PUTS(", ");
12274 msg_puts(FUNCARG(fp, j));
12275 }
12276 if (fp->varargs)
12277 {
12278 if (j)
12279 MSG_PUTS(", ");
12280 MSG_PUTS("...");
12281 }
12282 msg_putchar(')');
12283}
12284
12285/*
12286 * Find a function by name, return pointer to it in ufuncs.
12287 * Return NULL for unknown function.
12288 */
12289 static ufunc_T *
12290find_func(name)
12291 char_u *name;
12292{
12293 ufunc_T *fp;
12294
12295 for (fp = firstfunc; fp != NULL; fp = fp->next)
12296 if (STRCMP(name, fp->name) == 0)
12297 break;
12298 return fp;
12299}
12300
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012301/*
12302 * Return TRUE if a function "name" exists.
12303 */
12304 static int
12305function_exists(name)
12306 char_u *name;
12307{
12308 char_u *p = name;
12309 int n = FALSE;
12310
12311 p = trans_function_name(&p, FALSE, TRUE);
12312 if (p != NULL)
12313 {
12314 if (ASCII_ISUPPER(*p) || p[0] == K_SPECIAL)
12315 n = (find_func(p) != NULL);
12316 else if (ASCII_ISLOWER(*p))
12317 n = (find_internal_func(p) >= 0);
12318 vim_free(p);
12319 }
12320 return n;
12321}
12322
Bram Moolenaar071d4272004-06-13 20:20:40 +000012323#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
12324
12325/*
12326 * Function given to ExpandGeneric() to obtain the list of user defined
12327 * function names.
12328 */
12329 char_u *
12330get_user_func_name(xp, idx)
12331 expand_T *xp;
12332 int idx;
12333{
12334 static ufunc_T *fp = NULL;
12335
12336 if (idx == 0)
12337 fp = firstfunc;
12338 if (fp != NULL)
12339 {
12340 if (STRLEN(fp->name) + 4 >= IOSIZE)
12341 return fp->name; /* prevents overflow */
12342
12343 cat_func_name(IObuff, fp);
12344 if (xp->xp_context != EXPAND_USER_FUNC)
12345 {
12346 STRCAT(IObuff, "(");
12347 if (!fp->varargs && fp->args.ga_len == 0)
12348 STRCAT(IObuff, ")");
12349 }
12350
12351 fp = fp->next;
12352 return IObuff;
12353 }
12354 return NULL;
12355}
12356
12357#endif /* FEAT_CMDL_COMPL */
12358
12359/*
12360 * Copy the function name of "fp" to buffer "buf".
12361 * "buf" must be able to hold the function name plus three bytes.
12362 * Takes care of script-local function names.
12363 */
12364 static void
12365cat_func_name(buf, fp)
12366 char_u *buf;
12367 ufunc_T *fp;
12368{
12369 if (fp->name[0] == K_SPECIAL)
12370 {
12371 STRCPY(buf, "<SNR>");
12372 STRCAT(buf, fp->name + 3);
12373 }
12374 else
12375 STRCPY(buf, fp->name);
12376}
12377
12378/*
12379 * ":delfunction {name}"
12380 */
12381 void
12382ex_delfunction(eap)
12383 exarg_T *eap;
12384{
12385 ufunc_T *fp = NULL, *pfp;
12386 char_u *p;
12387 char_u *name;
12388
12389 p = eap->arg;
12390 name = trans_function_name(&p, eap->skip, FALSE);
12391 if (name == NULL)
12392 return;
12393 if (!ends_excmd(*skipwhite(p)))
12394 {
12395 vim_free(name);
12396 EMSG(_(e_trailing));
12397 return;
12398 }
12399 eap->nextcmd = check_nextcmd(p);
12400 if (eap->nextcmd != NULL)
12401 *p = NUL;
12402
12403 if (!eap->skip)
12404 fp = find_func(name);
12405 vim_free(name);
12406
12407 if (!eap->skip)
12408 {
12409 if (fp == NULL)
12410 {
12411 EMSG2(_("E130: Undefined function: %s"), eap->arg);
12412 return;
12413 }
12414 if (fp->calls)
12415 {
12416 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
12417 return;
12418 }
12419
12420 /* clear this function */
12421 vim_free(fp->name);
12422 ga_clear_strings(&(fp->args));
12423 ga_clear_strings(&(fp->lines));
12424
12425 /* remove the function from the function list */
12426 if (firstfunc == fp)
12427 firstfunc = fp->next;
12428 else
12429 {
12430 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next)
12431 if (pfp->next == fp)
12432 {
12433 pfp->next = fp->next;
12434 break;
12435 }
12436 }
12437 vim_free(fp);
12438 }
12439}
12440
12441/*
12442 * Call a user function.
12443 */
12444 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012445call_user_func(fp, argcount, argvars, rettv, firstline, lastline)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446 ufunc_T *fp; /* pointer to function */
12447 int argcount; /* nr of args */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012448 typeval *argvars; /* arguments */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012449 typeval *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012450 linenr_T firstline; /* first line of range */
12451 linenr_T lastline; /* last line of range */
12452{
12453 char_u *save_sourcing_name;
12454 linenr_T save_sourcing_lnum;
12455 scid_T save_current_SID;
12456 struct funccall fc;
12457 struct funccall *save_fcp = current_funccal;
12458 int save_did_emsg;
12459 static int depth = 0;
12460
12461 /* If depth of calling is getting too high, don't execute the function */
12462 if (depth >= p_mfd)
12463 {
12464 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012465 rettv->v_type = VAR_NUMBER;
12466 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012467 return;
12468 }
12469 ++depth;
12470
12471 line_breakcheck(); /* check for CTRL-C hit */
12472
12473 /* set local variables */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012474 vars_init(&fc.l_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475 fc.func = fp;
12476 fc.argcount = argcount;
12477 fc.argvars = argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012478 fc.rettv = rettv;
12479 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480 fc.linenr = 0;
12481 fc.returned = FALSE;
12482 fc.level = ex_nesting_level;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012483 fc.a0_var.tv.v_type = VAR_NUMBER;
12484 fc.a0_var.tv.vval.v_number = argcount - fp->args.ga_len;
12485 fc.a0_var.v_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486 current_funccal = &fc;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012487 fc.firstline.tv.v_type = VAR_NUMBER;
12488 fc.firstline.tv.vval.v_number = firstline;
12489 fc.firstline.v_name = NULL;
12490 fc.lastline.tv.v_type = VAR_NUMBER;
12491 fc.lastline.tv.vval.v_number = lastline;
12492 fc.lastline.v_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012493 /* Check if this function has a breakpoint. */
12494 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->name, (linenr_T)0);
12495 fc.dbg_tick = debug_tick;
12496
12497 /* Don't redraw while executing the function. */
12498 ++RedrawingDisabled;
12499 save_sourcing_name = sourcing_name;
12500 save_sourcing_lnum = sourcing_lnum;
12501 sourcing_lnum = 1;
12502 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
12503 : STRLEN(save_sourcing_name)) + STRLEN(fp->name) + 13));
12504 if (sourcing_name != NULL)
12505 {
12506 if (save_sourcing_name != NULL
12507 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
12508 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
12509 else
12510 STRCPY(sourcing_name, "function ");
12511 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
12512
12513 if (p_verbose >= 12)
12514 {
12515 ++no_wait_return;
12516 msg_scroll = TRUE; /* always scroll up, don't overwrite */
12517 msg_str((char_u *)_("calling %s"), sourcing_name);
12518 if (p_verbose >= 14)
12519 {
12520 int i;
12521 char_u buf[MSG_BUF_LEN];
12522
12523 msg_puts((char_u *)"(");
12524 for (i = 0; i < argcount; ++i)
12525 {
12526 if (i > 0)
12527 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012528 if (argvars[i].v_type == VAR_NUMBER)
12529 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012530 else
12531 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012532 trunc_string(get_tv_string(&argvars[i]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533 buf, MSG_BUF_LEN);
12534 msg_puts((char_u *)"\"");
12535 msg_puts(buf);
12536 msg_puts((char_u *)"\"");
12537 }
12538 }
12539 msg_puts((char_u *)")");
12540 }
12541 msg_puts((char_u *)"\n"); /* don't overwrite this either */
12542 cmdline_row = msg_row;
12543 --no_wait_return;
12544 }
12545 }
12546 save_current_SID = current_SID;
12547 current_SID = fp->script_ID;
12548 save_did_emsg = did_emsg;
12549 did_emsg = FALSE;
12550
12551 /* call do_cmdline() to execute the lines */
12552 do_cmdline(NULL, get_func_line, (void *)&fc,
12553 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
12554
12555 --RedrawingDisabled;
12556
12557 /* when the function was aborted because of an error, return -1 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012558 if ((did_emsg && (fp->flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012560 clear_tv(rettv);
12561 rettv->v_type = VAR_NUMBER;
12562 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012563 }
12564
12565 /* when being verbose, mention the return value */
12566 if (p_verbose >= 12)
12567 {
12568 char_u *sn, *val;
12569
12570 ++no_wait_return;
12571 msg_scroll = TRUE; /* always scroll up, don't overwrite */
12572
12573 /* Make sure the output fits in IObuff. */
12574 sn = sourcing_name;
12575 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
12576 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
12577
12578 if (aborting())
12579 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012580 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012581 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012582 (long)fc.rettv->vval.v_number);
12583 else if (fc.rettv->v_type == VAR_STRING)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012584 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012585 val = get_tv_string(fc.rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012586 if (STRLEN(val) > IOSIZE / 2 - 50)
12587 val = val + STRLEN(val) - (IOSIZE / 2 - 50);
12588 smsg((char_u *)_("%s returning \"%s\""), sn, val);
12589 }
12590 msg_puts((char_u *)"\n"); /* don't overwrite this either */
12591 cmdline_row = msg_row;
12592 --no_wait_return;
12593 }
12594
12595 vim_free(sourcing_name);
12596 sourcing_name = save_sourcing_name;
12597 sourcing_lnum = save_sourcing_lnum;
12598 current_SID = save_current_SID;
12599
12600 if (p_verbose >= 12 && sourcing_name != NULL)
12601 {
12602 ++no_wait_return;
12603 msg_scroll = TRUE; /* always scroll up, don't overwrite */
12604 msg_str((char_u *)_("continuing in %s"), sourcing_name);
12605 msg_puts((char_u *)"\n"); /* don't overwrite this either */
12606 cmdline_row = msg_row;
12607 --no_wait_return;
12608 }
12609
12610 did_emsg |= save_did_emsg;
12611 current_funccal = save_fcp;
12612
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012613 vars_clear(&fc.l_vars); /* free all local variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614 --depth;
12615}
12616
12617/*
12618 * ":return [expr]"
12619 */
12620 void
12621ex_return(eap)
12622 exarg_T *eap;
12623{
12624 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012625 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626 int returning = FALSE;
12627
12628 if (current_funccal == NULL)
12629 {
12630 EMSG(_("E133: :return not inside a function"));
12631 return;
12632 }
12633
12634 if (eap->skip)
12635 ++emsg_skip;
12636
12637 eap->nextcmd = NULL;
12638 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012639 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012640 {
12641 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012642 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012644 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012645 }
12646 /* It's safer to return also on error. */
12647 else if (!eap->skip)
12648 {
12649 /*
12650 * Return unless the expression evaluation has been cancelled due to an
12651 * aborting error, an interrupt, or an exception.
12652 */
12653 if (!aborting())
12654 returning = do_return(eap, FALSE, TRUE, NULL);
12655 }
12656
12657 /* When skipping or the return gets pending, advance to the next command
12658 * in this line (!returning). Otherwise, ignore the rest of the line.
12659 * Following lines will be ignored by get_func_line(). */
12660 if (returning)
12661 eap->nextcmd = NULL;
12662 else if (eap->nextcmd == NULL) /* no argument */
12663 eap->nextcmd = check_nextcmd(arg);
12664
12665 if (eap->skip)
12666 --emsg_skip;
12667}
12668
12669/*
12670 * Return from a function. Possibly makes the return pending. Also called
12671 * for a pending return at the ":endtry" or after returning from an extra
12672 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012673 * when called due to a ":return" command. "rettv" may point to a typeval
12674 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000012675 * FALSE when the return gets pending.
12676 */
12677 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012678do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679 exarg_T *eap;
12680 int reanimate;
12681 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012682 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683{
12684 int idx;
12685 struct condstack *cstack = eap->cstack;
12686
12687 if (reanimate)
12688 /* Undo the return. */
12689 current_funccal->returned = FALSE;
12690
12691 /*
12692 * Cleanup (and inactivate) conditionals, but stop when a try conditional
12693 * not in its finally clause (which then is to be executed next) is found.
12694 * In this case, make the ":return" pending for execution at the ":endtry".
12695 * Otherwise, return normally.
12696 */
12697 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
12698 if (idx >= 0)
12699 {
12700 cstack->cs_pending[idx] = CSTP_RETURN;
12701
12702 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012703 /* A pending return again gets pending. "rettv" points to an
12704 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012706 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012707 else
12708 {
12709 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012710 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012712 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715 {
12716 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012717 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
12718 *(typeval *)cstack->cs_rettv[idx] = *(typeval *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719 else
12720 EMSG(_(e_outofmem));
12721 }
12722 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012723 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724
12725 if (reanimate)
12726 {
12727 /* The pending return value could be overwritten by a ":return"
12728 * without argument in a finally clause; reset the default
12729 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012730 current_funccal->rettv->v_type = VAR_NUMBER;
12731 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732 }
12733 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012734 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735 }
12736 else
12737 {
12738 current_funccal->returned = TRUE;
12739
12740 /* If the return is carried out now, store the return value. For
12741 * a return immediately after reanimation, the value is already
12742 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012743 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012745 clear_tv(current_funccal->rettv);
12746 *current_funccal->rettv = *(typeval *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012747 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012748 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749 }
12750 }
12751
12752 return idx < 0;
12753}
12754
12755/*
12756 * Free the variable with a pending return value.
12757 */
12758 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012759discard_pending_return(rettv)
12760 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012761{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762 free_tv((typeval *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763}
12764
12765/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012766 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000012767 * is an allocated string. Used by report_pending() for verbose messages.
12768 */
12769 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012770get_return_cmd(rettv)
12771 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012773 char_u *s;
12774 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012775 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012777 if (rettv == NULL)
12778 s = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779 else
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012780 s = tv2string((typeval *)rettv, &tofree, numbuf);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012781
12782 STRCPY(IObuff, ":return ");
12783 STRNCPY(IObuff + 8, s, IOSIZE - 8);
12784 if (STRLEN(s) + 8 >= IOSIZE)
12785 STRCPY(IObuff + IOSIZE - 4, "...");
12786 vim_free(tofree);
12787 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788}
12789
12790/*
12791 * Get next function line.
12792 * Called by do_cmdline() to get the next line.
12793 * Returns allocated string, or NULL for end of function.
12794 */
12795/* ARGSUSED */
12796 char_u *
12797get_func_line(c, cookie, indent)
12798 int c; /* not used */
12799 void *cookie;
12800 int indent; /* not used */
12801{
12802 struct funccall *fcp = (struct funccall *)cookie;
12803 char_u *retval;
12804 garray_T *gap; /* growarray with function lines */
12805
12806 /* If breakpoints have been added/deleted need to check for it. */
12807 if (fcp->dbg_tick != debug_tick)
12808 {
12809 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
12810 sourcing_lnum);
12811 fcp->dbg_tick = debug_tick;
12812 }
12813
12814 gap = &fcp->func->lines;
12815 if ((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
12816 retval = NULL;
12817 else if (fcp->returned || fcp->linenr >= gap->ga_len)
12818 retval = NULL;
12819 else
12820 {
12821 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
12822 sourcing_lnum = fcp->linenr;
12823 }
12824
12825 /* Did we encounter a breakpoint? */
12826 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
12827 {
12828 dbg_breakpoint(fcp->func->name, sourcing_lnum);
12829 /* Find next breakpoint. */
12830 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
12831 sourcing_lnum);
12832 fcp->dbg_tick = debug_tick;
12833 }
12834
12835 return retval;
12836}
12837
12838/*
12839 * Return TRUE if the currently active function should be ended, because a
12840 * return was encountered or an error occured. Used inside a ":while".
12841 */
12842 int
12843func_has_ended(cookie)
12844 void *cookie;
12845{
12846 struct funccall *fcp = (struct funccall *)cookie;
12847
12848 /* Ignore the "abort" flag if the abortion behavior has been changed due to
12849 * an error inside a try conditional. */
12850 return (((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
12851 || fcp->returned);
12852}
12853
12854/*
12855 * return TRUE if cookie indicates a function which "abort"s on errors.
12856 */
12857 int
12858func_has_abort(cookie)
12859 void *cookie;
12860{
12861 return ((struct funccall *)cookie)->func->flags & FC_ABORT;
12862}
12863
12864#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
12865typedef enum
12866{
12867 VAR_FLAVOUR_DEFAULT,
12868 VAR_FLAVOUR_SESSION,
12869 VAR_FLAVOUR_VIMINFO
12870} var_flavour_T;
12871
12872static var_flavour_T var_flavour __ARGS((char_u *varname));
12873
12874 static var_flavour_T
12875var_flavour(varname)
12876 char_u *varname;
12877{
12878 char_u *p = varname;
12879
12880 if (ASCII_ISUPPER(*p))
12881 {
12882 while (*(++p))
12883 if (ASCII_ISLOWER(*p))
12884 return VAR_FLAVOUR_SESSION;
12885 return VAR_FLAVOUR_VIMINFO;
12886 }
12887 else
12888 return VAR_FLAVOUR_DEFAULT;
12889}
12890#endif
12891
12892#if defined(FEAT_VIMINFO) || defined(PROTO)
12893/*
12894 * Restore global vars that start with a capital from the viminfo file
12895 */
12896 int
12897read_viminfo_varlist(virp, writing)
12898 vir_T *virp;
12899 int writing;
12900{
12901 char_u *tab;
12902 int is_string = FALSE;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000012903 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904
12905 if (!writing && (find_viminfo_parameter('!') != NULL))
12906 {
12907 tab = vim_strchr(virp->vir_line + 1, '\t');
12908 if (tab != NULL)
12909 {
12910 *tab++ = '\0'; /* isolate the variable name */
12911 if (*tab == 'S') /* string var */
12912 is_string = TRUE;
12913
12914 tab = vim_strchr(tab, '\t');
12915 if (tab != NULL)
12916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917 if (is_string)
12918 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000012919 tv.v_type = VAR_STRING;
12920 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922 }
12923 else
12924 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000012925 tv.v_type = VAR_NUMBER;
12926 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000012928 set_var(virp->vir_line + 1, &tv, FALSE);
12929 if (is_string)
12930 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931 }
12932 }
12933 }
12934
12935 return viminfo_readline(virp);
12936}
12937
12938/*
12939 * Write global vars that start with a capital to the viminfo file
12940 */
12941 void
12942write_viminfo_varlist(fp)
12943 FILE *fp;
12944{
12945 garray_T *gap = &variables; /* global variable */
12946 VAR this_var;
12947 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012948 char *s;
12949 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012950 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951
12952 if (find_viminfo_parameter('!') == NULL)
12953 return;
12954
12955 fprintf(fp, _("\n# global variables:\n"));
12956 for (i = gap->ga_len; --i >= 0; )
12957 {
12958 this_var = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012959 if (this_var->v_name != NULL
12960 && var_flavour(this_var->v_name) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012962 switch (this_var->tv.v_type)
12963 {
12964 case VAR_STRING: s = "STR"; break;
12965 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000012966 default: continue;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012967 }
12968 fprintf(fp, "!%s\t%s\t", this_var->v_name, s);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012969 viminfo_writestring(fp, tv2string(&this_var->tv, &tofree, numbuf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012970 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971 }
12972 }
12973}
12974#endif
12975
12976#if defined(FEAT_SESSION) || defined(PROTO)
12977 int
12978store_session_globals(fd)
12979 FILE *fd;
12980{
12981 garray_T *gap = &variables; /* global variable */
12982 VAR this_var;
12983 int i;
12984 char_u *p, *t;
12985
12986 for (i = gap->ga_len; --i >= 0; )
12987 {
12988 this_var = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000012989 if (this_var->v_name != NULL
12990 && (this_var->tv.v_type == VAR_NUMBER
12991 || this_var->tv.v_type == VAR_STRING)
12992 && var_flavour(this_var->v_name) == VAR_FLAVOUR_SESSION)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000012994 /* Escape special characters with a backslash. Turn a LF and
12995 * CR into \n and \r. */
12996 p = vim_strsave_escaped(get_var_string(this_var),
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997 (char_u *)"\\\"\n\r");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000012998 if (p == NULL) /* out of memory */
12999 continue;
13000 for (t = p; *t != NUL; ++t)
13001 if (*t == '\n')
13002 *t = 'n';
13003 else if (*t == '\r')
13004 *t = 'r';
13005 if ((fprintf(fd, "let %s = %c%s%c",
13006 this_var->v_name,
13007 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ',
13008 p,
13009 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ') < 0)
13010 || put_eol(fd) == FAIL)
13011 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013012 vim_free(p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013013 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013015 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016 }
13017 }
13018 return OK;
13019}
13020#endif
13021
13022#endif /* FEAT_EVAL */
13023
13024#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
13025
13026
13027#ifdef WIN3264
13028/*
13029 * Functions for ":8" filename modifier: get 8.3 version of a filename.
13030 */
13031static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
13032static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
13033static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
13034
13035/*
13036 * Get the short pathname of a file.
13037 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
13038 */
13039 static int
13040get_short_pathname(fnamep, bufp, fnamelen)
13041 char_u **fnamep;
13042 char_u **bufp;
13043 int *fnamelen;
13044{
13045 int l,len;
13046 char_u *newbuf;
13047
13048 len = *fnamelen;
13049
13050 l = GetShortPathName(*fnamep, *fnamep, len);
13051 if (l > len - 1)
13052 {
13053 /* If that doesn't work (not enough space), then save the string
13054 * and try again with a new buffer big enough
13055 */
13056 newbuf = vim_strnsave(*fnamep, l);
13057 if (newbuf == NULL)
13058 return 0;
13059
13060 vim_free(*bufp);
13061 *fnamep = *bufp = newbuf;
13062
13063 l = GetShortPathName(*fnamep,*fnamep,l+1);
13064
13065 /* Really should always succeed, as the buffer is big enough */
13066 }
13067
13068 *fnamelen = l;
13069 return 1;
13070}
13071
13072/*
13073 * Create a short path name. Returns the length of the buffer it needs.
13074 * Doesn't copy over the end of the buffer passed in.
13075 */
13076 static int
13077shortpath_for_invalid_fname(fname, bufp, fnamelen)
13078 char_u **fname;
13079 char_u **bufp;
13080 int *fnamelen;
13081{
13082 char_u *s, *p, *pbuf2, *pbuf3;
13083 char_u ch;
13084 int l,len,len2,plen,slen;
13085
13086 /* Make a copy */
13087 len2 = *fnamelen;
13088 pbuf2 = vim_strnsave(*fname, len2);
13089 pbuf3 = NULL;
13090
13091 s = pbuf2 + len2 - 1; /* Find the end */
13092 slen = 1;
13093 plen = len2;
13094
13095 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013096 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013097 {
13098 --s;
13099 ++slen;
13100 --plen;
13101 }
13102
13103 do
13104 {
13105 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013106 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107 {
13108 --s;
13109 ++slen;
13110 --plen;
13111 }
13112 if (s <= pbuf2)
13113 break;
13114
13115 /* Remeber the character that is about to be blatted */
13116 ch = *s;
13117 *s = 0; /* get_short_pathname requires a null-terminated string */
13118
13119 /* Try it in situ */
13120 p = pbuf2;
13121 if (!get_short_pathname(&p, &pbuf3, &plen))
13122 {
13123 vim_free(pbuf2);
13124 return -1;
13125 }
13126 *s = ch; /* Preserve the string */
13127 } while (plen == 0);
13128
13129 if (plen > 0)
13130 {
13131 /* Remeber the length of the new string. */
13132 *fnamelen = len = plen + slen;
13133 vim_free(*bufp);
13134 if (len > len2)
13135 {
13136 /* If there's not enough space in the currently allocated string,
13137 * then copy it to a buffer big enough.
13138 */
13139 *fname= *bufp = vim_strnsave(p, len);
13140 if (*fname == NULL)
13141 return -1;
13142 }
13143 else
13144 {
13145 /* Transfer pbuf2 to being the main buffer (it's big enough) */
13146 *fname = *bufp = pbuf2;
13147 if (p != pbuf2)
13148 strncpy(*fname, p, plen);
13149 pbuf2 = NULL;
13150 }
13151 /* Concat the next bit */
13152 strncpy(*fname + plen, s, slen);
13153 (*fname)[len] = '\0';
13154 }
13155 vim_free(pbuf3);
13156 vim_free(pbuf2);
13157 return 0;
13158}
13159
13160/*
13161 * Get a pathname for a partial path.
13162 */
13163 static int
13164shortpath_for_partial(fnamep, bufp, fnamelen)
13165 char_u **fnamep;
13166 char_u **bufp;
13167 int *fnamelen;
13168{
13169 int sepcount, len, tflen;
13170 char_u *p;
13171 char_u *pbuf, *tfname;
13172 int hasTilde;
13173
13174 /* Count up the path seperators from the RHS.. so we know which part
13175 * of the path to return.
13176 */
13177 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013178 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013179 if (vim_ispathsep(*p))
13180 ++sepcount;
13181
13182 /* Need full path first (use expand_env() to remove a "~/") */
13183 hasTilde = (**fnamep == '~');
13184 if (hasTilde)
13185 pbuf = tfname = expand_env_save(*fnamep);
13186 else
13187 pbuf = tfname = FullName_save(*fnamep, FALSE);
13188
13189 len = tflen = STRLEN(tfname);
13190
13191 if (!get_short_pathname(&tfname, &pbuf, &len))
13192 return -1;
13193
13194 if (len == 0)
13195 {
13196 /* Don't have a valid filename, so shorten the rest of the
13197 * path if we can. This CAN give us invalid 8.3 filenames, but
13198 * there's not a lot of point in guessing what it might be.
13199 */
13200 len = tflen;
13201 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
13202 return -1;
13203 }
13204
13205 /* Count the paths backward to find the beginning of the desired string. */
13206 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013207 {
13208#ifdef FEAT_MBYTE
13209 if (has_mbyte)
13210 p -= mb_head_off(tfname, p);
13211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212 if (vim_ispathsep(*p))
13213 {
13214 if (sepcount == 0 || (hasTilde && sepcount == 1))
13215 break;
13216 else
13217 sepcount --;
13218 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220 if (hasTilde)
13221 {
13222 --p;
13223 if (p >= tfname)
13224 *p = '~';
13225 else
13226 return -1;
13227 }
13228 else
13229 ++p;
13230
13231 /* Copy in the string - p indexes into tfname - allocated at pbuf */
13232 vim_free(*bufp);
13233 *fnamelen = (int)STRLEN(p);
13234 *bufp = pbuf;
13235 *fnamep = p;
13236
13237 return 0;
13238}
13239#endif /* WIN3264 */
13240
13241/*
13242 * Adjust a filename, according to a string of modifiers.
13243 * *fnamep must be NUL terminated when called. When returning, the length is
13244 * determined by *fnamelen.
13245 * Returns valid flags.
13246 * When there is an error, *fnamep is set to NULL.
13247 */
13248 int
13249modify_fname(src, usedlen, fnamep, bufp, fnamelen)
13250 char_u *src; /* string with modifiers */
13251 int *usedlen; /* characters after src that are used */
13252 char_u **fnamep; /* file name so far */
13253 char_u **bufp; /* buffer for allocated file name or NULL */
13254 int *fnamelen; /* length of fnamep */
13255{
13256 int valid = 0;
13257 char_u *tail;
13258 char_u *s, *p, *pbuf;
13259 char_u dirname[MAXPATHL];
13260 int c;
13261 int has_fullname = 0;
13262#ifdef WIN3264
13263 int has_shortname = 0;
13264#endif
13265
13266repeat:
13267 /* ":p" - full path/file_name */
13268 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
13269 {
13270 has_fullname = 1;
13271
13272 valid |= VALID_PATH;
13273 *usedlen += 2;
13274
13275 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
13276 if ((*fnamep)[0] == '~'
13277#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
13278 && ((*fnamep)[1] == '/'
13279# ifdef BACKSLASH_IN_FILENAME
13280 || (*fnamep)[1] == '\\'
13281# endif
13282 || (*fnamep)[1] == NUL)
13283
13284#endif
13285 )
13286 {
13287 *fnamep = expand_env_save(*fnamep);
13288 vim_free(*bufp); /* free any allocated file name */
13289 *bufp = *fnamep;
13290 if (*fnamep == NULL)
13291 return -1;
13292 }
13293
13294 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013295 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296 {
13297 if (vim_ispathsep(*p)
13298 && p[1] == '.'
13299 && (p[2] == NUL
13300 || vim_ispathsep(p[2])
13301 || (p[2] == '.'
13302 && (p[3] == NUL || vim_ispathsep(p[3])))))
13303 break;
13304 }
13305
13306 /* FullName_save() is slow, don't use it when not needed. */
13307 if (*p != NUL || !vim_isAbsName(*fnamep))
13308 {
13309 *fnamep = FullName_save(*fnamep, *p != NUL);
13310 vim_free(*bufp); /* free any allocated file name */
13311 *bufp = *fnamep;
13312 if (*fnamep == NULL)
13313 return -1;
13314 }
13315
13316 /* Append a path separator to a directory. */
13317 if (mch_isdir(*fnamep))
13318 {
13319 /* Make room for one or two extra characters. */
13320 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
13321 vim_free(*bufp); /* free any allocated file name */
13322 *bufp = *fnamep;
13323 if (*fnamep == NULL)
13324 return -1;
13325 add_pathsep(*fnamep);
13326 }
13327 }
13328
13329 /* ":." - path relative to the current directory */
13330 /* ":~" - path relative to the home directory */
13331 /* ":8" - shortname path - postponed till after */
13332 while (src[*usedlen] == ':'
13333 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
13334 {
13335 *usedlen += 2;
13336 if (c == '8')
13337 {
13338#ifdef WIN3264
13339 has_shortname = 1; /* Postpone this. */
13340#endif
13341 continue;
13342 }
13343 pbuf = NULL;
13344 /* Need full path first (use expand_env() to remove a "~/") */
13345 if (!has_fullname)
13346 {
13347 if (c == '.' && **fnamep == '~')
13348 p = pbuf = expand_env_save(*fnamep);
13349 else
13350 p = pbuf = FullName_save(*fnamep, FALSE);
13351 }
13352 else
13353 p = *fnamep;
13354
13355 has_fullname = 0;
13356
13357 if (p != NULL)
13358 {
13359 if (c == '.')
13360 {
13361 mch_dirname(dirname, MAXPATHL);
13362 s = shorten_fname(p, dirname);
13363 if (s != NULL)
13364 {
13365 *fnamep = s;
13366 if (pbuf != NULL)
13367 {
13368 vim_free(*bufp); /* free any allocated file name */
13369 *bufp = pbuf;
13370 pbuf = NULL;
13371 }
13372 }
13373 }
13374 else
13375 {
13376 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
13377 /* Only replace it when it starts with '~' */
13378 if (*dirname == '~')
13379 {
13380 s = vim_strsave(dirname);
13381 if (s != NULL)
13382 {
13383 *fnamep = s;
13384 vim_free(*bufp);
13385 *bufp = s;
13386 }
13387 }
13388 }
13389 vim_free(pbuf);
13390 }
13391 }
13392
13393 tail = gettail(*fnamep);
13394 *fnamelen = (int)STRLEN(*fnamep);
13395
13396 /* ":h" - head, remove "/file_name", can be repeated */
13397 /* Don't remove the first "/" or "c:\" */
13398 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
13399 {
13400 valid |= VALID_HEAD;
13401 *usedlen += 2;
13402 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013403 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013404 --tail;
13405 *fnamelen = (int)(tail - *fnamep);
13406#ifdef VMS
13407 if (*fnamelen > 0)
13408 *fnamelen += 1; /* the path separator is part of the path */
13409#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013410 while (tail > s && !after_pathsep(s, tail))
13411 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013412 }
13413
13414 /* ":8" - shortname */
13415 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
13416 {
13417 *usedlen += 2;
13418#ifdef WIN3264
13419 has_shortname = 1;
13420#endif
13421 }
13422
13423#ifdef WIN3264
13424 /* Check shortname after we have done 'heads' and before we do 'tails'
13425 */
13426 if (has_shortname)
13427 {
13428 pbuf = NULL;
13429 /* Copy the string if it is shortened by :h */
13430 if (*fnamelen < (int)STRLEN(*fnamep))
13431 {
13432 p = vim_strnsave(*fnamep, *fnamelen);
13433 if (p == 0)
13434 return -1;
13435 vim_free(*bufp);
13436 *bufp = *fnamep = p;
13437 }
13438
13439 /* Split into two implementations - makes it easier. First is where
13440 * there isn't a full name already, second is where there is.
13441 */
13442 if (!has_fullname && !vim_isAbsName(*fnamep))
13443 {
13444 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
13445 return -1;
13446 }
13447 else
13448 {
13449 int l;
13450
13451 /* Simple case, already have the full-name
13452 * Nearly always shorter, so try first time. */
13453 l = *fnamelen;
13454 if (!get_short_pathname(fnamep, bufp, &l))
13455 return -1;
13456
13457 if (l == 0)
13458 {
13459 /* Couldn't find the filename.. search the paths.
13460 */
13461 l = *fnamelen;
13462 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
13463 return -1;
13464 }
13465 *fnamelen = l;
13466 }
13467 }
13468#endif /* WIN3264 */
13469
13470 /* ":t" - tail, just the basename */
13471 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
13472 {
13473 *usedlen += 2;
13474 *fnamelen -= (int)(tail - *fnamep);
13475 *fnamep = tail;
13476 }
13477
13478 /* ":e" - extension, can be repeated */
13479 /* ":r" - root, without extension, can be repeated */
13480 while (src[*usedlen] == ':'
13481 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
13482 {
13483 /* find a '.' in the tail:
13484 * - for second :e: before the current fname
13485 * - otherwise: The last '.'
13486 */
13487 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
13488 s = *fnamep - 2;
13489 else
13490 s = *fnamep + *fnamelen - 1;
13491 for ( ; s > tail; --s)
13492 if (s[0] == '.')
13493 break;
13494 if (src[*usedlen + 1] == 'e') /* :e */
13495 {
13496 if (s > tail)
13497 {
13498 *fnamelen += (int)(*fnamep - (s + 1));
13499 *fnamep = s + 1;
13500#ifdef VMS
13501 /* cut version from the extension */
13502 s = *fnamep + *fnamelen - 1;
13503 for ( ; s > *fnamep; --s)
13504 if (s[0] == ';')
13505 break;
13506 if (s > *fnamep)
13507 *fnamelen = s - *fnamep;
13508#endif
13509 }
13510 else if (*fnamep <= tail)
13511 *fnamelen = 0;
13512 }
13513 else /* :r */
13514 {
13515 if (s > tail) /* remove one extension */
13516 *fnamelen = (int)(s - *fnamep);
13517 }
13518 *usedlen += 2;
13519 }
13520
13521 /* ":s?pat?foo?" - substitute */
13522 /* ":gs?pat?foo?" - global substitute */
13523 if (src[*usedlen] == ':'
13524 && (src[*usedlen + 1] == 's'
13525 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
13526 {
13527 char_u *str;
13528 char_u *pat;
13529 char_u *sub;
13530 int sep;
13531 char_u *flags;
13532 int didit = FALSE;
13533
13534 flags = (char_u *)"";
13535 s = src + *usedlen + 2;
13536 if (src[*usedlen + 1] == 'g')
13537 {
13538 flags = (char_u *)"g";
13539 ++s;
13540 }
13541
13542 sep = *s++;
13543 if (sep)
13544 {
13545 /* find end of pattern */
13546 p = vim_strchr(s, sep);
13547 if (p != NULL)
13548 {
13549 pat = vim_strnsave(s, (int)(p - s));
13550 if (pat != NULL)
13551 {
13552 s = p + 1;
13553 /* find end of substitution */
13554 p = vim_strchr(s, sep);
13555 if (p != NULL)
13556 {
13557 sub = vim_strnsave(s, (int)(p - s));
13558 str = vim_strnsave(*fnamep, *fnamelen);
13559 if (sub != NULL && str != NULL)
13560 {
13561 *usedlen = (int)(p + 1 - src);
13562 s = do_string_sub(str, pat, sub, flags);
13563 if (s != NULL)
13564 {
13565 *fnamep = s;
13566 *fnamelen = (int)STRLEN(s);
13567 vim_free(*bufp);
13568 *bufp = s;
13569 didit = TRUE;
13570 }
13571 }
13572 vim_free(sub);
13573 vim_free(str);
13574 }
13575 vim_free(pat);
13576 }
13577 }
13578 /* after using ":s", repeat all the modifiers */
13579 if (didit)
13580 goto repeat;
13581 }
13582 }
13583
13584 return valid;
13585}
13586
13587/*
13588 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
13589 * "flags" can be "g" to do a global substitute.
13590 * Returns an allocated string, NULL for error.
13591 */
13592 char_u *
13593do_string_sub(str, pat, sub, flags)
13594 char_u *str;
13595 char_u *pat;
13596 char_u *sub;
13597 char_u *flags;
13598{
13599 int sublen;
13600 regmatch_T regmatch;
13601 int i;
13602 int do_all;
13603 char_u *tail;
13604 garray_T ga;
13605 char_u *ret;
13606 char_u *save_cpo;
13607
13608 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
13609 save_cpo = p_cpo;
13610 p_cpo = (char_u *)"";
13611
13612 ga_init2(&ga, 1, 200);
13613
13614 do_all = (flags[0] == 'g');
13615
13616 regmatch.rm_ic = p_ic;
13617 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13618 if (regmatch.regprog != NULL)
13619 {
13620 tail = str;
13621 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
13622 {
13623 /*
13624 * Get some space for a temporary buffer to do the substitution
13625 * into. It will contain:
13626 * - The text up to where the match is.
13627 * - The substituted text.
13628 * - The text after the match.
13629 */
13630 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
13631 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
13632 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
13633 {
13634 ga_clear(&ga);
13635 break;
13636 }
13637
13638 /* copy the text up to where the match is */
13639 i = (int)(regmatch.startp[0] - tail);
13640 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
13641 /* add the substituted text */
13642 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
13643 + ga.ga_len + i, TRUE, TRUE, FALSE);
13644 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645 /* avoid getting stuck on a match with an empty string */
13646 if (tail == regmatch.endp[0])
13647 {
13648 if (*tail == NUL)
13649 break;
13650 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
13651 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013652 }
13653 else
13654 {
13655 tail = regmatch.endp[0];
13656 if (*tail == NUL)
13657 break;
13658 }
13659 if (!do_all)
13660 break;
13661 }
13662
13663 if (ga.ga_data != NULL)
13664 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
13665
13666 vim_free(regmatch.regprog);
13667 }
13668
13669 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
13670 ga_clear(&ga);
13671 p_cpo = save_cpo;
13672
13673 return ret;
13674}
13675
13676#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */