blob: 54215198d21d4ee5d974a3f7a379dc2569d574ae [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");
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000108static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000109static char *e_undefvar = N_("E121: Undefined variable: %s");
110static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000111static char *e_intern2 = N_("E685: Internal error: %s");
112static char *e_listarg = N_("E686: 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
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000194/* used for map() */
195static typeval amp_tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000196
197/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 * Return the name of the executed function.
199 */
200 char_u *
201func_name(cookie)
202 void *cookie;
203{
204 return ((struct funccall *)cookie)->func->name;
205}
206
207/*
208 * Return the address holding the next breakpoint line for a funccall cookie.
209 */
210 linenr_T *
211func_breakpoint(cookie)
212 void *cookie;
213{
214 return &((struct funccall *)cookie)->breakpoint;
215}
216
217/*
218 * Return the address holding the debug tick for a funccall cookie.
219 */
220 int *
221func_dbg_tick(cookie)
222 void *cookie;
223{
224 return &((struct funccall *)cookie)->dbg_tick;
225}
226
227/*
228 * Return the nesting level for a funccall cookie.
229 */
230 int
231func_level(cookie)
232 void *cookie;
233{
234 return ((struct funccall *)cookie)->level;
235}
236
237/* pointer to funccal for currently active function */
238struct funccall *current_funccal = NULL;
239
240/*
241 * Return TRUE when a function was ended by a ":return" command.
242 */
243 int
244current_func_returned()
245{
246 return current_funccal->returned;
247}
248
249
250/*
251 * Array to hold the value of v: variables.
252 */
253#include "version.h"
254
255/* values for flags: */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000256#define VV_COMPAT 1 /* compatible, also used without "v:" */
257#define VV_RO 2 /* read-only */
258#define VV_RO_SBX 4 /* read-only in the sandbox*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260struct vimvar
261{
262 char *name; /* name of variable, without v: */
263 int len; /* length of name */
264 char_u *val; /* current value (can also be a number!) */
265 char type; /* VAR_NUMBER or VAR_STRING */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000266 char flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267} vimvars[VV_LEN] =
268{ /* The order here must match the VV_ defines in vim.h! */
269 {"count", sizeof("count") - 1, NULL, VAR_NUMBER, VV_COMPAT+VV_RO},
270 {"count1", sizeof("count1") - 1, NULL, VAR_NUMBER, VV_RO},
271 {"prevcount", sizeof("prevcount") - 1, NULL, VAR_NUMBER, VV_RO},
272 {"errmsg", sizeof("errmsg") - 1, NULL, VAR_STRING, VV_COMPAT},
273 {"warningmsg", sizeof("warningmsg") - 1, NULL, VAR_STRING, 0},
274 {"statusmsg", sizeof("statusmsg") - 1, NULL, VAR_STRING, 0},
275 {"shell_error", sizeof("shell_error") - 1, NULL, VAR_NUMBER,
276 VV_COMPAT+VV_RO},
277 {"this_session", sizeof("this_session") - 1, NULL, VAR_STRING, VV_COMPAT},
278 {"version", sizeof("version") - 1, (char_u *)VIM_VERSION_100,
279 VAR_NUMBER, VV_COMPAT+VV_RO},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000280 {"lnum", sizeof("lnum") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 {"termresponse", sizeof("termresponse") - 1, NULL, VAR_STRING, VV_RO},
282 {"fname", sizeof("fname") - 1, NULL, VAR_STRING, VV_RO},
283 {"lang", sizeof("lang") - 1, NULL, VAR_STRING, VV_RO},
284 {"lc_time", sizeof("lc_time") - 1, NULL, VAR_STRING, VV_RO},
285 {"ctype", sizeof("ctype") - 1, NULL, VAR_STRING, VV_RO},
286 {"charconvert_from", sizeof("charconvert_from") - 1, NULL, VAR_STRING, VV_RO},
287 {"charconvert_to", sizeof("charconvert_to") - 1, NULL, VAR_STRING, VV_RO},
288 {"fname_in", sizeof("fname_in") - 1, NULL, VAR_STRING, VV_RO},
289 {"fname_out", sizeof("fname_out") - 1, NULL, VAR_STRING, VV_RO},
290 {"fname_new", sizeof("fname_new") - 1, NULL, VAR_STRING, VV_RO},
291 {"fname_diff", sizeof("fname_diff") - 1, NULL, VAR_STRING, VV_RO},
292 {"cmdarg", sizeof("cmdarg") - 1, NULL, VAR_STRING, VV_RO},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +0000293 {"foldstart", sizeof("foldstart") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
294 {"foldend", sizeof("foldend") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
295 {"folddashes", sizeof("folddashes") - 1, NULL, VAR_STRING, VV_RO_SBX},
296 {"foldlevel", sizeof("foldlevel") - 1, NULL, VAR_NUMBER, VV_RO_SBX},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 {"progname", sizeof("progname") - 1, NULL, VAR_STRING, VV_RO},
298 {"servername", sizeof("servername") - 1, NULL, VAR_STRING, VV_RO},
299 {"dying", sizeof("dying") - 1, NULL, VAR_NUMBER, VV_RO},
300 {"exception", sizeof("exception") - 1, NULL, VAR_STRING, VV_RO},
301 {"throwpoint", sizeof("throwpoint") - 1, NULL, VAR_STRING, VV_RO},
302 {"register", sizeof("register") - 1, NULL, VAR_STRING, VV_RO},
303 {"cmdbang", sizeof("cmdbang") - 1, NULL, VAR_NUMBER, VV_RO},
Bram Moolenaar843ee412004-06-30 16:16:41 +0000304 {"insertmode", sizeof("insertmode") - 1, NULL, VAR_STRING, VV_RO},
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305};
306
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000307static int eval0 __ARGS((char_u *arg, typeval *rettv, char_u **nextcmd, int evaluate));
308static int eval1 __ARGS((char_u **arg, typeval *rettv, int evaluate));
309static int eval2 __ARGS((char_u **arg, typeval *rettv, int evaluate));
310static int eval3 __ARGS((char_u **arg, typeval *rettv, int evaluate));
311static int eval4 __ARGS((char_u **arg, typeval *rettv, int evaluate));
312static int eval5 __ARGS((char_u **arg, typeval *rettv, int evaluate));
313static int eval6 __ARGS((char_u **arg, typeval *rettv, int evaluate));
314static int eval7 __ARGS((char_u **arg, typeval *rettv, int evaluate));
315static int eval_index __ARGS((char_u **arg, typeval *rettv, int evaluate));
316static int get_option_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
317static int get_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
318static int get_lit_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000319static int get_sharp_string_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000320static int get_list_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000321static listvar *list_alloc __ARGS((void));
322static void list_unref __ARGS((listvar *l));
323static void list_free __ARGS((listvar *l));
324static listitem *listitem_alloc __ARGS((void));
325static void listitem_free __ARGS((listitem *item));
326static long list_len __ARGS((listvar *l));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000327static int list_equal __ARGS((listvar *l1, listvar *l2, int ic));
328static int tv_equal __ARGS((typeval *tv1, typeval *tv2, int ic));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000329static int string_isa_number __ARGS((char_u *s));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000330static listitem *list_find __ARGS((listvar *l, long n));
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000331static long list_idx_of_item __ARGS((listvar *l, listitem *item));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000332static listitem *list_find_ext __ARGS((listvar *l, long *ip));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000333static void list_append __ARGS((listvar *l, listitem *item));
334static int list_append_tv __ARGS((listvar *l, typeval *tv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000335static int list_insert_tv __ARGS((listvar *l, typeval *tv, listitem *item));
336static int list_extend __ARGS((listvar *l1, listvar *l2, listitem *bef));
337static int list_concat __ARGS((listvar *l1, listvar *l2, typeval *tv));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000338static listvar *list_copy __ARGS((listvar *orig, int deep));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000339static void list_getrem __ARGS((listvar *l, listitem *item, listitem *item2));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000340static char_u *list2string __ARGS((typeval *tv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000341static void list_join __ARGS((garray_T *gap, listvar *l, char_u *sep, int echo));
342
343static char_u *echo_string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000344static char_u *tv2string __ARGS((typeval *tv, char_u **tofree, char_u *numbuf));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000345static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000346static int get_env_tv __ARGS((char_u **arg, typeval *rettv, int evaluate));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347static int find_internal_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000348static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000349static 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));
350static 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 +0000351
352static void f_add __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000353static void f_append __ARGS((typeval *argvars, typeval *rettv));
354static void f_argc __ARGS((typeval *argvars, typeval *rettv));
355static void f_argidx __ARGS((typeval *argvars, typeval *rettv));
356static void f_argv __ARGS((typeval *argvars, typeval *rettv));
357static void f_browse __ARGS((typeval *argvars, typeval *rettv));
358static void f_browsedir __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000359static void f_bufexists __ARGS((typeval *argvars, typeval *rettv));
360static void f_buflisted __ARGS((typeval *argvars, typeval *rettv));
361static void f_bufloaded __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000362static void f_bufname __ARGS((typeval *argvars, typeval *rettv));
363static void f_bufnr __ARGS((typeval *argvars, typeval *rettv));
364static void f_bufwinnr __ARGS((typeval *argvars, typeval *rettv));
365static void f_byte2line __ARGS((typeval *argvars, typeval *rettv));
366static void f_byteidx __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000367static void f_call __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000368static void f_char2nr __ARGS((typeval *argvars, typeval *rettv));
369static void f_cindent __ARGS((typeval *argvars, typeval *rettv));
370static void f_col __ARGS((typeval *argvars, typeval *rettv));
371static void f_confirm __ARGS((typeval *argvars, typeval *rettv));
372static void f_copy __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000373static void f_count __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000374static void f_cscope_connection __ARGS((typeval *argvars, typeval *rettv));
375static void f_cursor __ARGS((typeval *argsvars, typeval *rettv));
376static void f_deepcopy __ARGS((typeval *argvars, typeval *rettv));
377static void f_delete __ARGS((typeval *argvars, typeval *rettv));
378static void f_did_filetype __ARGS((typeval *argvars, typeval *rettv));
379static void f_diff_filler __ARGS((typeval *argvars, typeval *rettv));
380static void f_diff_hlID __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000381static void f_empty __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000382static void f_escape __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000383static void f_eval __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000384static void f_eventhandler __ARGS((typeval *argvars, typeval *rettv));
385static void f_executable __ARGS((typeval *argvars, typeval *rettv));
386static void f_exists __ARGS((typeval *argvars, typeval *rettv));
387static void f_expand __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000388static void f_extend __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000389static void f_filereadable __ARGS((typeval *argvars, typeval *rettv));
390static void f_filewritable __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000391static void f_filter __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000392static void f_finddir __ARGS((typeval *argvars, typeval *rettv));
393static void f_findfile __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000394static void f_fnamemodify __ARGS((typeval *argvars, typeval *rettv));
395static void f_foldclosed __ARGS((typeval *argvars, typeval *rettv));
396static void f_foldclosedend __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000397static void f_foldlevel __ARGS((typeval *argvars, typeval *rettv));
398static void f_foldtext __ARGS((typeval *argvars, typeval *rettv));
399static void f_foldtextresult __ARGS((typeval *argvars, typeval *rettv));
400static void f_foreground __ARGS((typeval *argvars, typeval *rettv));
401static void f_function __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000402static void f_get __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000403static void f_getbufvar __ARGS((typeval *argvars, typeval *rettv));
404static void f_getchar __ARGS((typeval *argvars, typeval *rettv));
405static void f_getcharmod __ARGS((typeval *argvars, typeval *rettv));
406static void f_getcmdline __ARGS((typeval *argvars, typeval *rettv));
407static void f_getcmdpos __ARGS((typeval *argvars, typeval *rettv));
408static void f_getcwd __ARGS((typeval *argvars, typeval *rettv));
409static void f_getfontname __ARGS((typeval *argvars, typeval *rettv));
410static void f_getfperm __ARGS((typeval *argvars, typeval *rettv));
411static void f_getfsize __ARGS((typeval *argvars, typeval *rettv));
412static void f_getftime __ARGS((typeval *argvars, typeval *rettv));
413static void f_getftype __ARGS((typeval *argvars, typeval *rettv));
414static void f_getline __ARGS((typeval *argvars, typeval *rettv));
415static void f_getreg __ARGS((typeval *argvars, typeval *rettv));
416static void f_getregtype __ARGS((typeval *argvars, typeval *rettv));
417static void f_getwinposx __ARGS((typeval *argvars, typeval *rettv));
418static void f_getwinposy __ARGS((typeval *argvars, typeval *rettv));
419static void f_getwinvar __ARGS((typeval *argvars, typeval *rettv));
420static void f_glob __ARGS((typeval *argvars, typeval *rettv));
421static void f_globpath __ARGS((typeval *argvars, typeval *rettv));
422static void f_has __ARGS((typeval *argvars, typeval *rettv));
423static void f_hasmapto __ARGS((typeval *argvars, typeval *rettv));
424static void f_histadd __ARGS((typeval *argvars, typeval *rettv));
425static void f_histdel __ARGS((typeval *argvars, typeval *rettv));
426static void f_histget __ARGS((typeval *argvars, typeval *rettv));
427static void f_histnr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000428static void f_hlID __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000429static void f_hlexists __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000430static void f_hostname __ARGS((typeval *argvars, typeval *rettv));
431static void f_iconv __ARGS((typeval *argvars, typeval *rettv));
432static void f_indent __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar8a283e52005-01-06 23:28:25 +0000433static void f_index __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000434static void f_input __ARGS((typeval *argvars, typeval *rettv));
435static void f_inputdialog __ARGS((typeval *argvars, typeval *rettv));
436static void f_inputrestore __ARGS((typeval *argvars, typeval *rettv));
437static void f_inputsave __ARGS((typeval *argvars, typeval *rettv));
438static void f_inputsecret __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000439static void f_insert __ARGS((typeval *argvars, typeval *rettv));
440static void f_isdirectory __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000441static void f_join __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000442static void f_last_buffer_nr __ARGS((typeval *argvars, typeval *rettv));
443static void f_len __ARGS((typeval *argvars, typeval *rettv));
444static void f_libcall __ARGS((typeval *argvars, typeval *rettv));
445static void f_libcallnr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000446static void f_line __ARGS((typeval *argvars, typeval *rettv));
447static void f_line2byte __ARGS((typeval *argvars, typeval *rettv));
448static void f_lispindent __ARGS((typeval *argvars, typeval *rettv));
449static void f_localtime __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000450static void f_map __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000451static void f_maparg __ARGS((typeval *argvars, typeval *rettv));
452static void f_mapcheck __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000453static void f_match __ARGS((typeval *argvars, typeval *rettv));
454static void f_matchend __ARGS((typeval *argvars, typeval *rettv));
455static void f_matchstr __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000456static void f_max __ARGS((typeval *argvars, typeval *rettv));
457static void f_min __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000458static void f_mode __ARGS((typeval *argvars, typeval *rettv));
459static void f_nextnonblank __ARGS((typeval *argvars, typeval *rettv));
460static void f_nr2char __ARGS((typeval *argvars, typeval *rettv));
461static void f_prevnonblank __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000462static void f_remote_expr __ARGS((typeval *argvars, typeval *rettv));
463static void f_remote_foreground __ARGS((typeval *argvars, typeval *rettv));
464static void f_remote_peek __ARGS((typeval *argvars, typeval *rettv));
465static void f_remote_read __ARGS((typeval *argvars, typeval *rettv));
466static void f_remote_send __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000467static void f_remove __ARGS((typeval *argvars, typeval *rettv));
468static void f_rename __ARGS((typeval *argvars, typeval *rettv));
469static void f_repeat __ARGS((typeval *argvars, typeval *rettv));
470static void f_resolve __ARGS((typeval *argvars, typeval *rettv));
471static void f_reverse __ARGS((typeval *argvars, typeval *rettv));
472static void f_search __ARGS((typeval *argvars, typeval *rettv));
473static void f_searchpair __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000474static void f_server2client __ARGS((typeval *argvars, typeval *rettv));
475static void f_serverlist __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000476static void f_setbufvar __ARGS((typeval *argvars, typeval *rettv));
477static void f_setcmdpos __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000478static void f_setline __ARGS((typeval *argvars, typeval *rettv));
479static void f_setreg __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000480static void f_setwinvar __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000481static void f_simplify __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000482static void f_sort __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000483static void f_split __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000484#ifdef HAVE_STRFTIME
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000485static void f_strftime __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000486#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000487static void f_stridx __ARGS((typeval *argvars, typeval *rettv));
488static void f_string __ARGS((typeval *argvars, typeval *rettv));
489static void f_strlen __ARGS((typeval *argvars, typeval *rettv));
490static void f_strpart __ARGS((typeval *argvars, typeval *rettv));
491static void f_strridx __ARGS((typeval *argvars, typeval *rettv));
492static void f_strtrans __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000493static void f_submatch __ARGS((typeval *argvars, typeval *rettv));
494static void f_substitute __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000495static void f_synID __ARGS((typeval *argvars, typeval *rettv));
496static void f_synIDattr __ARGS((typeval *argvars, typeval *rettv));
497static void f_synIDtrans __ARGS((typeval *argvars, typeval *rettv));
498static void f_system __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000499static void f_tempname __ARGS((typeval *argvars, typeval *rettv));
500static void f_tolower __ARGS((typeval *argvars, typeval *rettv));
501static void f_toupper __ARGS((typeval *argvars, typeval *rettv));
502static void f_tr __ARGS((typeval *argvars, typeval *rettv));
503static void f_type __ARGS((typeval *argvars, typeval *rettv));
504static void f_virtcol __ARGS((typeval *argvars, typeval *rettv));
505static void f_visualmode __ARGS((typeval *argvars, typeval *rettv));
506static void f_winbufnr __ARGS((typeval *argvars, typeval *rettv));
507static void f_wincol __ARGS((typeval *argvars, typeval *rettv));
508static void f_winheight __ARGS((typeval *argvars, typeval *rettv));
509static void f_winline __ARGS((typeval *argvars, typeval *rettv));
510static void f_winnr __ARGS((typeval *argvars, typeval *rettv));
511static void f_winrestcmd __ARGS((typeval *argvars, typeval *rettv));
512static void f_winwidth __ARGS((typeval *argvars, typeval *rettv));
Bram Moolenaar0d660222005-01-07 21:51:51 +0000513
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000514static win_T *find_win_by_nr __ARGS((typeval *vp));
515static pos_T *var2fpos __ARGS((typeval *varp, int lnum));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516static int get_env_len __ARGS((char_u **arg));
517static int get_id_len __ARGS((char_u **arg));
518static int get_func_len __ARGS((char_u **arg, char_u **alias, int evaluate));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000519static 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 +0000520static int eval_isnamec __ARGS((int c));
521static int find_vim_var __ARGS((char_u *name, int len));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000522static int get_var_tv __ARGS((char_u *name, int len, typeval *rettv));
523static typeval *alloc_tv __ARGS((void));
524static typeval *alloc_string_tv __ARGS((char_u *string));
525static void free_tv __ARGS((typeval *varp));
526static void clear_tv __ARGS((typeval *varp));
527static void init_tv __ARGS((typeval *varp));
528static long get_tv_number __ARGS((typeval *varp));
529static linenr_T get_tv_lnum __ARGS((typeval *argvars));
530static char_u *get_tv_string __ARGS((typeval *varp));
531static char_u *get_tv_string_buf __ARGS((typeval *varp, char_u *buf));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000532static int get_amp_tv __ARGS((typeval *rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533static VAR find_var __ARGS((char_u *name, int writing));
534static VAR find_var_in_ga __ARGS((garray_T *gap, char_u *varname));
535static garray_T *find_var_ga __ARGS((char_u *name, char_u **varname));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000536static void clear_var __ARGS((VAR v));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537static void list_one_var __ARGS((VAR v, char_u *prefix));
538static void list_vim_var __ARGS((int i));
539static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000540static void set_var __ARGS((char_u *name, typeval *varp, int copy));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000541static void copy_tv __ARGS((typeval *from, typeval *to));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
543static char_u *trans_function_name __ARGS((char_u **pp, int skip, int internal));
544static int eval_fname_script __ARGS((char_u *p));
545static int eval_fname_sid __ARGS((char_u *p));
546static void list_func_head __ARGS((ufunc_T *fp, int indent));
547static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
548static ufunc_T *find_func __ARGS((char_u *name));
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000549static int function_exists __ARGS((char_u *name));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000550static 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 +0000551
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000552#define get_var_string(p) get_tv_string(&(p)->tv)
553#define get_var_string_buf(p, b) get_tv_string_buf(&(p)->tv, (b))
554#define get_var_number(p) get_tv_number(&((p)->tv))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556static 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 +0000557
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000558static int ex_let_vars __ARGS((char_u *arg, typeval *tv, int copy, int semicolon, int var_count, char_u *nextchars));
559static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
560static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000561static void list_all_vars __ARGS((void));
562static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg));
563static char_u *ex_let_one __ARGS((char_u *arg, typeval *tv, int copy, char_u *endchars));
564static 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 +0000565static void list_add_watch __ARGS((listvar *l, listwatch *lw));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566
567/*
568 * Set an internal variable to a string value. Creates the variable if it does
569 * not already exist.
570 */
571 void
572set_internal_string_var(name, value)
573 char_u *name;
574 char_u *value;
575{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000576 char_u *val;
577 typeval *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578
579 val = vim_strsave(value);
580 if (val != NULL)
581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000582 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000583 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000585 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000586 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 }
588 }
589}
590
591# if defined(FEAT_MBYTE) || defined(PROTO)
592 int
593eval_charconvert(enc_from, enc_to, fname_from, fname_to)
594 char_u *enc_from;
595 char_u *enc_to;
596 char_u *fname_from;
597 char_u *fname_to;
598{
599 int err = FALSE;
600
601 set_vim_var_string(VV_CC_FROM, enc_from, -1);
602 set_vim_var_string(VV_CC_TO, enc_to, -1);
603 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
604 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
605 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
606 err = TRUE;
607 set_vim_var_string(VV_CC_FROM, NULL, -1);
608 set_vim_var_string(VV_CC_TO, NULL, -1);
609 set_vim_var_string(VV_FNAME_IN, NULL, -1);
610 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
611
612 if (err)
613 return FAIL;
614 return OK;
615}
616# endif
617
618# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
619 int
620eval_printexpr(fname, args)
621 char_u *fname;
622 char_u *args;
623{
624 int err = FALSE;
625
626 set_vim_var_string(VV_FNAME_IN, fname, -1);
627 set_vim_var_string(VV_CMDARG, args, -1);
628 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
629 err = TRUE;
630 set_vim_var_string(VV_FNAME_IN, NULL, -1);
631 set_vim_var_string(VV_CMDARG, NULL, -1);
632
633 if (err)
634 {
635 mch_remove(fname);
636 return FAIL;
637 }
638 return OK;
639}
640# endif
641
642# if defined(FEAT_DIFF) || defined(PROTO)
643 void
644eval_diff(origfile, newfile, outfile)
645 char_u *origfile;
646 char_u *newfile;
647 char_u *outfile;
648{
649 int err = FALSE;
650
651 set_vim_var_string(VV_FNAME_IN, origfile, -1);
652 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
653 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
654 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
655 set_vim_var_string(VV_FNAME_IN, NULL, -1);
656 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
657 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
658}
659
660 void
661eval_patch(origfile, difffile, outfile)
662 char_u *origfile;
663 char_u *difffile;
664 char_u *outfile;
665{
666 int err;
667
668 set_vim_var_string(VV_FNAME_IN, origfile, -1);
669 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
670 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
671 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
672 set_vim_var_string(VV_FNAME_IN, NULL, -1);
673 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
674 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
675}
676# endif
677
678/*
679 * Top level evaluation function, returning a boolean.
680 * Sets "error" to TRUE if there was an error.
681 * Return TRUE or FALSE.
682 */
683 int
684eval_to_bool(arg, error, nextcmd, skip)
685 char_u *arg;
686 int *error;
687 char_u **nextcmd;
688 int skip; /* only parse, don't execute */
689{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000690 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 int retval = FALSE;
692
693 if (skip)
694 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000695 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 else
698 {
699 *error = FALSE;
700 if (!skip)
701 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000702 retval = (get_tv_number(&tv) != 0);
703 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 }
705 }
706 if (skip)
707 --emsg_skip;
708
709 return retval;
710}
711
712/*
713 * Top level evaluation function, returning a string. If "skip" is TRUE,
714 * only parsing to "nextcmd" is done, without reporting errors. Return
715 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
716 */
717 char_u *
718eval_to_string_skip(arg, nextcmd, skip)
719 char_u *arg;
720 char_u **nextcmd;
721 int skip; /* only parse, don't execute */
722{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000723 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 char_u *retval;
725
726 if (skip)
727 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000728 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 retval = NULL;
730 else
731 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000732 retval = vim_strsave(get_tv_string(&tv));
733 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 }
735 if (skip)
736 --emsg_skip;
737
738 return retval;
739}
740
741/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000742 * Skip over an expression at "*pp".
743 * Return FAIL for an error, OK otherwise.
744 */
745 int
746skip_expr(pp)
747 char_u **pp;
748{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000749 typeval rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000750
751 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000752 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000753}
754
755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 * Top level evaluation function, returning a string.
757 * Return pointer to allocated memory, or NULL for failure.
758 */
759 char_u *
760eval_to_string(arg, nextcmd)
761 char_u *arg;
762 char_u **nextcmd;
763{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000764 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 char_u *retval;
766
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000767 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 retval = NULL;
769 else
770 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000771 retval = vim_strsave(get_tv_string(&tv));
772 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 }
774
775 return retval;
776}
777
778/*
779 * Call eval_to_string() with "sandbox" set and not using local variables.
780 */
781 char_u *
782eval_to_string_safe(arg, nextcmd)
783 char_u *arg;
784 char_u **nextcmd;
785{
786 char_u *retval;
787 void *save_funccalp;
788
789 save_funccalp = save_funccal();
790 ++sandbox;
791 retval = eval_to_string(arg, nextcmd);
792 --sandbox;
793 restore_funccal(save_funccalp);
794 return retval;
795}
796
797#if 0 /* not used */
798/*
799 * Top level evaluation function, returning a string.
800 * Advances "arg" to the first non-blank after the evaluated expression.
801 * Return pointer to allocated memory, or NULL for failure.
802 * Doesn't give error messages.
803 */
804 char_u *
805eval_arg_to_string(arg)
806 char_u **arg;
807{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000808 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 char_u *retval;
810 int ret;
811
812 ++emsg_off;
813
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000814 ret = eval1(arg, &rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 if (ret == FAIL)
816 retval = NULL;
817 else
818 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000819 retval = vim_strsave(get_tv_string(&rettv));
820 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 }
822
823 --emsg_off;
824
825 return retval;
826}
827#endif
828
829/*
830 * Top level evaluation function, returning a number.
831 * Evaluates "expr" silently.
832 * Returns -1 for an error.
833 */
834 int
835eval_to_number(expr)
836 char_u *expr;
837{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000838 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000840 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841
842 ++emsg_off;
843
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000844 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 retval = -1;
846 else
847 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000848 retval = get_tv_number(&rettv);
849 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851 --emsg_off;
852
853 return retval;
854}
855
856#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
857/*
858 * Call some vimL function and return the result as a string
859 * Uses argv[argc] for the function arguments.
860 */
861 char_u *
862call_vim_function(func, argc, argv, safe)
863 char_u *func;
864 int argc;
865 char_u **argv;
866 int safe; /* use the sandbox */
867{
868 char_u *retval = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000869 typeval rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000870 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 long n;
872 int len;
873 int i;
874 int doesrange;
875 void *save_funccalp = NULL;
876
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000877 argvars = (typeval *)alloc((unsigned)(argc * sizeof(typeval)));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 if (argvars == NULL)
879 return NULL;
880
881 for (i = 0; i < argc; i++)
882 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000883 /* Pass a NULL or empty argument as an empty string */
884 if (argv[i] == NULL || *argv[i] == NUL)
885 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000886 argvars[i].v_type = VAR_STRING;
887 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +0000888 continue;
889 }
890
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 /* Recognize a number argument, the others must be strings. */
892 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
893 if (len != 0 && len == (int)STRLEN(argv[i]))
894 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000895 argvars[i].v_type = VAR_NUMBER;
896 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 }
898 else
899 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000900 argvars[i].v_type = VAR_STRING;
901 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 }
903 }
904
905 if (safe)
906 {
907 save_funccalp = save_funccal();
908 ++sandbox;
909 }
910
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000911 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
912 if (call_func(func, (int)STRLEN(func), &rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
914 &doesrange, TRUE) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000915 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000917 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 vim_free(argvars);
919
920 if (safe)
921 {
922 --sandbox;
923 restore_funccal(save_funccalp);
924 }
925 return retval;
926}
927#endif
928
929/*
930 * Save the current function call pointer, and set it to NULL.
931 * Used when executing autocommands and for ":source".
932 */
933 void *
934save_funccal()
935{
936 struct funccall *fc;
937
938 fc = current_funccal;
939 current_funccal = NULL;
940 return (void *)fc;
941}
942
943 void
944restore_funccal(fc)
945 void *fc;
946{
947 current_funccal = (struct funccall *)fc;
948}
949
950#ifdef FEAT_FOLDING
951/*
952 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
953 * it in "*cp". Doesn't give error messages.
954 */
955 int
956eval_foldexpr(arg, cp)
957 char_u *arg;
958 int *cp;
959{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000960 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 int retval;
962 char_u *s;
963
964 ++emsg_off;
965 ++sandbox;
966 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000967 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 retval = 0;
969 else
970 {
971 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000972 if (tv.v_type == VAR_NUMBER)
973 retval = tv.vval.v_number;
974 else if (tv.v_type == VAR_UNKNOWN
975 || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 retval = 0;
977 else
978 {
979 /* If the result is a string, check if there is a non-digit before
980 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000981 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 if (!VIM_ISDIGIT(*s) && *s != '-')
983 *cp = *s++;
984 retval = atol((char *)s);
985 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000986 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 }
988 --emsg_off;
989 --sandbox;
990
991 return retval;
992}
993#endif
994
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995/*
996 * Expands out the 'magic' {}'s in a variable/function name.
997 * Note that this can call itself recursively, to deal with
998 * constructs like foo{bar}{baz}{bam}
999 * The four pointer arguments point to "foo{expre}ss{ion}bar"
1000 * "in_start" ^
1001 * "expr_start" ^
1002 * "expr_end" ^
1003 * "in_end" ^
1004 *
1005 * Returns a new allocated string, which the caller must free.
1006 * Returns NULL for failure.
1007 */
1008 static char_u *
1009make_expanded_name(in_start, expr_start, expr_end, in_end)
1010 char_u *in_start;
1011 char_u *expr_start;
1012 char_u *expr_end;
1013 char_u *in_end;
1014{
1015 char_u c1;
1016 char_u *retval = NULL;
1017 char_u *temp_result;
1018 char_u *nextcmd = NULL;
1019
1020 if (expr_end == NULL || in_end == NULL)
1021 return NULL;
1022 *expr_start = NUL;
1023 *expr_end = NUL;
1024 c1 = *in_end;
1025 *in_end = NUL;
1026
1027 temp_result = eval_to_string(expr_start + 1, &nextcmd);
1028 if (temp_result != NULL && nextcmd == NULL)
1029 {
1030 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
1031 + (in_end - expr_end) + 1));
1032
1033 if (retval != NULL)
1034 {
1035 STRCPY(retval, in_start);
1036 STRCAT(retval, temp_result);
1037 STRCAT(retval, expr_end + 1);
1038 }
1039 }
1040 vim_free(temp_result);
1041
1042 *in_end = c1; /* put char back for error messages */
1043 *expr_start = '{';
1044 *expr_end = '}';
1045
1046 if (retval != NULL)
1047 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001048 temp_result = find_name_end(retval, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 if (expr_start != NULL)
1050 {
1051 /* Further expansion! */
1052 temp_result = make_expanded_name(retval, expr_start,
1053 expr_end, temp_result);
1054 vim_free(retval);
1055 retval = temp_result;
1056 }
1057 }
1058
1059 return retval;
1060
1061}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
1063/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001064 * ":let" list all variable values
1065 * ":let var1 var2" list variable values
1066 * ":let var = expr" assignment command.
1067 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 */
1069 void
1070ex_let(eap)
1071 exarg_T *eap;
1072{
1073 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001074 char_u *expr = NULL;
1075 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001077 int var_count = 0;
1078 int semicolon = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001080 expr = skip_var_list(arg, &var_count, &semicolon);
1081 if (expr == NULL)
1082 return;
1083 expr = vim_strchr(expr, '=');
1084 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001086 if (*arg == '[')
1087 EMSG(_(e_invarg));
1088 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001089 /* ":let var1 var2" */
1090 arg = list_arg_vars(eap, arg);
1091 else if (!eap->skip)
1092 /* ":let" */
1093 list_all_vars();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 eap->nextcmd = check_nextcmd(arg);
1095 }
1096 else
1097 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001098 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001099
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 if (eap->skip)
1101 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001102 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 if (eap->skip)
1104 {
1105 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001106 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 --emsg_skip;
1108 }
1109 else if (i != FAIL)
1110 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001111 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1112 (char_u *)"=");
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001113 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 }
1115 }
1116}
1117
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001118/*
1119 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1120 * Handles both "var" with any type and "[var, var; var]" with a list type.
1121 * Returns OK or FAIL;
1122 */
1123 static int
1124ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1125 char_u *arg_start;
1126 typeval *tv;
1127 int copy; /* copy values from "tv", don't move */
1128 int semicolon; /* from skip_var_list() */
1129 int var_count; /* from skip_var_list() */
1130 char_u *nextchars; /* characters that must follow or NULL */
1131{
1132 char_u *arg = arg_start;
1133 listvar *l;
1134 int i;
1135 listitem *item;
1136 typeval ltv;
1137
1138 if (*arg != '[')
1139 {
1140 /*
1141 * ":let var = expr" or ":for var in list"
1142 */
1143 if (ex_let_one(arg, tv, copy, nextchars) == NULL)
1144 return FAIL;
1145 return OK;
1146 }
1147
1148 /*
1149 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1150 */
1151 l = tv->vval.v_list;
1152 if (tv->v_type != VAR_LIST || l == NULL)
1153 {
1154 EMSG(_(e_listreq));
1155 return FAIL;
1156 }
1157
1158 i = list_len(l);
1159 if (semicolon == 0 && var_count < i)
1160 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001161 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001162 return FAIL;
1163 }
1164 if (var_count - semicolon > i)
1165 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001166 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001167 return FAIL;
1168 }
1169
1170 item = l->lv_first;
1171 while (*arg != ']')
1172 {
1173 arg = skipwhite(arg + 1);
1174 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]");
1175 item = item->li_next;
1176 if (arg == NULL)
1177 return FAIL;
1178
1179 arg = skipwhite(arg);
1180 if (*arg == ';')
1181 {
1182 /* Put the rest of the list (may be empty) in the var after ';'.
1183 * Create a new list for this. */
1184 l = list_alloc();
1185 if (l == NULL)
1186 return FAIL;
1187 while (item != NULL)
1188 {
1189 list_append_tv(l, &item->li_tv);
1190 item = item->li_next;
1191 }
1192
1193 ltv.v_type = VAR_LIST;
1194 ltv.vval.v_list = l;
1195 l->lv_refcount = 1;
1196
1197 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, (char_u *)"]");
1198 clear_tv(&ltv);
1199 if (arg == NULL)
1200 return FAIL;
1201 break;
1202 }
1203 else if (*arg != ',' && *arg != ']')
1204 {
1205 EMSG2(_(e_intern2), "ex_let_vars()");
1206 return FAIL;
1207 }
1208 }
1209
1210 return OK;
1211}
1212
1213/*
1214 * Skip over assignable variable "var" or list of variables "[var, var]".
1215 * Used for ":let varvar = expr" and ":for varvar in expr".
1216 * For "[var, var]" increment "*var_count" for each variable.
1217 * for "[var, var; var]" set "semicolon".
1218 * Return NULL for an error.
1219 */
1220 static char_u *
1221skip_var_list(arg, var_count, semicolon)
1222 char_u *arg;
1223 int *var_count;
1224 int *semicolon;
1225{
1226 char_u *p, *s;
1227
1228 if (*arg == '[')
1229 {
1230 /* "[var, var]": find the matching ']'. */
1231 p = arg;
1232 while (1)
1233 {
1234 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1235 s = skip_var_one(p);
1236 if (s == p)
1237 {
1238 EMSG2(_(e_invarg2), p);
1239 return NULL;
1240 }
1241 ++*var_count;
1242
1243 p = skipwhite(s);
1244 if (*p == ']')
1245 break;
1246 else if (*p == ';')
1247 {
1248 if (*semicolon == 1)
1249 {
1250 EMSG(_("Double ; in list of variables"));
1251 return NULL;
1252 }
1253 *semicolon = 1;
1254 }
1255 else if (*p != ',')
1256 {
1257 EMSG2(_(e_invarg2), p);
1258 return NULL;
1259 }
1260 }
1261 return p + 1;
1262 }
1263 else
1264 return skip_var_one(arg);
1265}
1266
1267 static char_u *
1268skip_var_one(arg)
1269 char_u *arg;
1270{
1271 if (vim_strchr((char_u *)"$@&", *arg) != NULL)
1272 ++arg;
1273 return find_name_end(arg, NULL, NULL, TRUE);
1274}
1275
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001276 static void
1277list_all_vars()
1278{
1279 int i;
1280
1281 /*
1282 * List all variables.
1283 */
1284 for (i = 0; i < variables.ga_len && !got_int; ++i)
1285 if (VAR_ENTRY(i).v_name != NULL)
1286 list_one_var(&VAR_ENTRY(i), (char_u *)"");
1287 for (i = 0; i < curbuf->b_vars.ga_len && !got_int; ++i)
1288 if (BVAR_ENTRY(i).v_name != NULL)
1289 list_one_var(&BVAR_ENTRY(i), (char_u *)"b:");
1290 for (i = 0; i < curwin->w_vars.ga_len && !got_int; ++i)
1291 if (WVAR_ENTRY(i).v_name != NULL)
1292 list_one_var(&WVAR_ENTRY(i), (char_u *)"w:");
1293 for (i = 0; i < VV_LEN && !got_int; ++i)
1294 if (vimvars[i].type == VAR_NUMBER || vimvars[i].val != NULL)
1295 list_vim_var(i);
1296}
1297
1298/*
1299 * List variables in "arg".
1300 */
1301 static char_u *
1302list_arg_vars(eap, arg)
1303 exarg_T *eap;
1304 char_u *arg;
1305{
1306 int error = FALSE;
1307 char_u *temp_string = NULL;
1308 int arg_len;
1309 char_u *expr_start;
1310 char_u *expr_end;
1311 char_u *name_end;
1312 int c1 = 0, c2;
1313 int i;
1314 VAR varp;
1315 char_u *name;
1316
1317 while (!ends_excmd(*arg) && !got_int)
1318 {
1319 /* Find the end of the name. */
1320 name_end = find_name_end(arg, &expr_start, &expr_end, FALSE);
1321
1322 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
1323 {
1324 emsg_severe = TRUE;
1325 EMSG(_(e_trailing));
1326 break;
1327 }
1328 if (!error && !eap->skip)
1329 {
1330 if (expr_start != NULL)
1331 {
1332 temp_string = make_expanded_name(arg, expr_start,
1333 expr_end, name_end);
1334 if (temp_string == NULL)
1335 {
1336 /*
1337 * Report an invalid expression in braces, unless
1338 * the expression evaluation has been cancelled due
1339 * to an aborting error, an interrupt, or an
1340 * exception.
1341 */
1342 if (!aborting())
1343 {
1344 emsg_severe = TRUE;
1345 EMSG2(_(e_invarg2), arg);
1346 break;
1347 }
1348 error = TRUE;
1349 arg = skipwhite(name_end);
1350 continue;
1351 }
1352 arg = temp_string;
1353 arg_len = STRLEN(temp_string);
1354 }
1355 else
1356 {
1357 c1 = *name_end;
1358 *name_end = NUL;
1359 arg_len = (int)(name_end - arg);
1360 }
1361 i = find_vim_var(arg, arg_len);
1362 if (i >= 0)
1363 list_vim_var(i);
1364 else if (STRCMP("b:changedtick", arg) == 0)
1365 {
1366 char_u numbuf[NUMBUFLEN];
1367
1368 sprintf((char *)numbuf, "%ld",
1369 (long)curbuf->b_changedtick);
1370 list_one_var_a((char_u *)"b:", (char_u *)"changedtick",
1371 VAR_NUMBER, numbuf);
1372 }
1373 else
1374 {
1375 varp = find_var(arg, FALSE);
1376 if (varp == NULL)
1377 {
1378 /* Skip further arguments but do continue to
1379 * search for a trailing command. */
1380 EMSG2(_("E106: Unknown variable: \"%s\""), arg);
1381 error = TRUE;
1382 }
1383 else
1384 {
1385 name = vim_strchr(arg, ':');
1386 if (name != NULL)
1387 {
1388 /* "a:" vars have no name stored, use whole arg */
1389 if (arg[0] == 'a' && arg[1] == ':')
1390 c2 = NUL;
1391 else
1392 {
1393 c2 = *++name;
1394 *name = NUL;
1395 }
1396 list_one_var(varp, arg);
1397 if (c2 != NUL)
1398 *name = c2;
1399 }
1400 else
1401 list_one_var(varp, (char_u *)"");
1402 }
1403 }
1404 if (expr_start != NULL)
1405 vim_free(temp_string);
1406 else
1407 *name_end = c1;
1408 }
1409 arg = skipwhite(name_end);
1410 }
1411
1412 return arg;
1413}
1414
1415/*
1416 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1417 * Returns a pointer to the char just after the var name.
1418 * Returns NULL if there is an error.
1419 */
1420 static char_u *
1421ex_let_one(arg, tv, copy, endchars)
1422 char_u *arg; /* points to variable name */
1423 typeval *tv; /* value to assign to variable */
1424 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001425 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001426{
1427 int c1;
1428 char_u *name;
1429 char_u *p;
1430 char_u *arg_end = NULL;
1431 int len;
1432 int opt_flags;
1433
1434 /*
1435 * ":let $VAR = expr": Set environment variable.
1436 */
1437 if (*arg == '$')
1438 {
1439 /* Find the end of the name. */
1440 ++arg;
1441 name = arg;
1442 len = get_env_len(&arg);
1443 if (len == 0)
1444 EMSG2(_(e_invarg2), name - 1);
1445 else
1446 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001447 if (endchars != NULL
1448 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001449 EMSG(_(e_letunexp));
1450 else
1451 {
1452 c1 = name[len];
1453 name[len] = NUL;
1454 p = get_tv_string(tv);
1455 vim_setenv(name, p);
1456 if (STRICMP(name, "HOME") == 0)
1457 init_homedir();
1458 else if (didset_vim && STRICMP(name, "VIM") == 0)
1459 didset_vim = FALSE;
1460 else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
1461 didset_vimruntime = FALSE;
1462 name[len] = c1;
1463 arg_end = arg;
1464 }
1465 }
1466 }
1467
1468 /*
1469 * ":let &option = expr": Set option value.
1470 * ":let &l:option = expr": Set local option value.
1471 * ":let &g:option = expr": Set global option value.
1472 */
1473 else if (*arg == '&')
1474 {
1475 /* Find the end of the name. */
1476 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001477 if (p == NULL || (endchars != NULL
1478 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001479 EMSG(_(e_letunexp));
1480 else
1481 {
1482 c1 = *p;
1483 *p = NUL;
1484 set_option_value(arg, get_tv_number(tv),
1485 get_tv_string(tv), opt_flags);
1486 *p = c1;
1487 arg_end = p;
1488 }
1489 }
1490
1491 /*
1492 * ":let @r = expr": Set register contents.
1493 */
1494 else if (*arg == '@')
1495 {
1496 ++arg;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001497 if (endchars != NULL
1498 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001499 EMSG(_(e_letunexp));
1500 else
1501 {
1502 write_reg_contents(*arg == '@' ? '"' : *arg,
1503 get_tv_string(tv), -1, FALSE);
1504 arg_end = arg + 1;
1505 }
1506 }
1507
1508 /*
1509 * ":let var = expr": Set internal variable.
1510 */
1511 else if (eval_isnamec(*arg) && !VIM_ISDIGIT(*arg))
1512 {
1513 char_u *exp_name = NULL;
1514 char_u *expr_start, *expr_end;
1515
1516 /* Find the end of the name. */
1517 p = find_name_end(arg, &expr_start, &expr_end, FALSE);
1518 if (expr_start != NULL)
1519 {
1520 exp_name = make_expanded_name(arg, expr_start, expr_end, p);
1521 arg = exp_name;
1522 }
1523
1524 if (arg == NULL)
1525 {
1526 /* Report an invalid expression in braces, unless the
1527 * expression evaluation has been cancelled due to an
1528 * aborting error, an interrupt, or an exception. */
1529 if (!aborting())
1530 EMSG2(_(e_invarg2), arg);
1531 }
1532 else if (*p == '[')
1533 arg_end = set_var_idx(arg, p, tv, copy, endchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001534 else if (endchars != NULL
1535 && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001536 EMSG(_(e_letunexp));
1537 else if (STRNCMP(arg, "b:changedtick", 13) == 0
1538 && !eval_isnamec(arg[13]))
1539 EMSG2(_(e_readonlyvar), arg);
1540 else
1541 {
1542 c1 = *p;
1543 *p = NUL;
1544 set_var(arg, tv, copy);
1545 *p = c1;
1546 arg_end = p;
1547 }
1548
1549 vim_free(exp_name);
1550 }
1551
1552 else
1553 EMSG2(_(e_invarg2), arg);
1554
1555 return arg_end;
1556}
1557
1558/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001559 * Set a variable with an index: "name[expr]", "name[expr:expr]",
1560 * "name[expr][expr]", etc. Only works if "name" is an existing List.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001561 * "ip" points to the first '['.
1562 * Returns a pointer to just after the last used ']'; NULL for error.
1563 */
1564 static char_u *
1565set_var_idx(name, ip, rettv, copy, endchars)
1566 char_u *name;
1567 char_u *ip;
1568 typeval *rettv;
1569 int copy;
1570 char_u *endchars;
1571{
1572 VAR v;
1573 int c1;
1574 char_u *p;
1575 typeval var1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001576 typeval var2;
1577 int range = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001578 typeval *tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001579 long n1 = 0, n2 = 0;
1580 int empty1, empty2 = FALSE;
1581 listitem *item = NULL;
1582 listitem *ni;
1583 listitem *ri;
1584 listvar *l = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001585
1586 c1 = *ip;
1587 *ip = NUL;
1588 v = find_var(name, TRUE);
1589 if (v == NULL)
1590 EMSG2(_(e_undefvar), name);
1591 *ip = c1;
1592 if (v == NULL)
1593 return NULL;
1594
1595 tv = &v->tv;
1596 for (p = ip; *p == '['; p = skipwhite(p + 1))
1597 {
1598 if (tv->v_type != VAR_LIST || tv->vval.v_list == NULL)
1599 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001600 EMSG(_("E689: Can only index a List"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001601 p = NULL;
1602 break;
1603 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001604 if (range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001605 {
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001606 EMSG(_("E708: [:] must come last"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001607 p = NULL;
1608 break;
1609 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001610
1611 /* Get the index [expr] or the first index [expr: ]. */
1612 p = skipwhite(p + 1);
1613 if (*p == ':')
1614 empty1 = TRUE;
1615 else
1616 {
1617 empty1 = FALSE;
1618 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
1619 {
1620 p = NULL;
1621 break;
1622 }
1623 }
1624
1625 /* Optionally get the second index [ :expr]. */
1626 if (*p == ':')
1627 {
1628 if (rettv->v_type != VAR_LIST || rettv->vval.v_list == NULL)
1629 {
1630 EMSG(_("E709: [:] requires a List value"));
1631 p = NULL;
1632 if (!empty1)
1633 clear_tv(&var1);
1634 break;
1635 }
1636 p = skipwhite(p + 1);
1637 if (*p == ']')
1638 empty2 = TRUE;
1639 else
1640 {
1641 empty2 = FALSE;
1642 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1643 {
1644 p = NULL;
1645 if (!empty1)
1646 clear_tv(&var1);
1647 break;
1648 }
1649 }
1650 range = TRUE;
1651 }
1652 else
1653 range = FALSE;
1654
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001655 if (*p != ']')
1656 {
1657 EMSG(_(e_missbrac));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001658 if (!empty1)
1659 clear_tv(&var1);
1660 if (range && !empty2)
1661 clear_tv(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001662 p = NULL;
1663 break;
1664 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001665
1666 /*
1667 * Get the number and item for the only or first index.
1668 */
1669 if (empty1)
1670 n1 = 0;
1671 else
1672 {
1673 n1 = get_tv_number(&var1);
1674 clear_tv(&var1);
1675 }
1676 l = tv->vval.v_list;
1677 item = list_find(l, n1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001678 if (item == NULL)
1679 {
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001680 EMSGN(_(e_listidx), n1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001681 p = NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001682 if (range && !empty2)
1683 clear_tv(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001684 break;
1685 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001686
1687 /*
1688 * May need to find the item or absolute index for the second index of
1689 * a range.
1690 * When no index given: "empty2" is TRUE.
1691 * Otherwise "n2" is set to the second index.
1692 */
1693 if (range && !empty2)
1694 {
1695 n2 = get_tv_number(&var2);
1696 clear_tv(&var2);
1697 if (n2 < 0)
1698 {
1699 ni = list_find(l, n2);
1700 if (ni == NULL)
1701 {
1702 EMSGN(_(e_listidx), n2);
1703 p = NULL;
1704 break;
1705 }
1706 n2 = list_idx_of_item(l, ni);
1707 }
1708
1709 /* Check that n2 isn't before n1. */
1710 if (n1 < 0)
1711 n1 = list_idx_of_item(l, item);
1712 if (n2 < n1)
1713 {
1714 EMSGN(_(e_listidx), n2);
1715 p = NULL;
1716 break;
1717 }
1718 }
1719
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001720 tv = &item->li_tv;
1721 }
1722
1723 if (p != NULL)
1724 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001725 if (endchars != NULL && vim_strchr(endchars, *p) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001726 {
1727 EMSG(_(e_letunexp));
1728 p = NULL;
1729 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001730 else if (range)
1731 {
1732 /*
1733 * Assign the List values to the list items.
1734 */
1735 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
1736 {
1737 clear_tv(&item->li_tv);
1738 copy_tv(&ri->li_tv, &item->li_tv);
1739 ri = ri->li_next;
1740 if (ri == NULL || (!empty2 && n2 == n1))
1741 break;
1742 if (item->li_next == NULL)
1743 {
1744 /* Need to add an empty item. */
1745 ni = listitem_alloc();
1746 if (ni == NULL)
1747 {
1748 ri = NULL;
1749 break;
1750 }
1751 ni->li_tv.v_type = VAR_NUMBER;
1752 ni->li_tv.vval.v_number = 0;
1753 list_append(l, ni);
1754 }
1755 item = item->li_next;
1756 ++n1;
1757 }
1758 if (ri != NULL)
1759 EMSG(_("E710: List value has more items than target"));
1760 else if (empty2 ? item != NULL && item->li_next != NULL : n1 != n2)
1761 EMSG(_("E711: List value has not enough items"));
1762 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001763 else
1764 {
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001765 /*
1766 * Assign the value to the variable or list item.
1767 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001768 clear_tv(tv);
1769 if (copy)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001770 copy_tv(rettv, tv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001771 else
1772 {
1773 *tv = *rettv;
1774 init_tv(rettv);
1775 }
1776 }
1777 }
1778 return p;
1779}
1780
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001781/*
1782 * Add a watcher to a list.
1783 */
1784 static void
1785list_add_watch(l, lw)
1786 listvar *l;
1787 listwatch *lw;
1788{
1789 lw->lw_next = l->lv_watch;
1790 l->lv_watch = lw;
1791}
1792
1793/*
1794 * Remove a watches from a list.
1795 * No warning when it isn't found...
1796 */
1797 static void
1798list_rem_watch(l, lwrem)
1799 listvar *l;
1800 listwatch *lwrem;
1801{
1802 listwatch *lw, **lwp;
1803
1804 lwp = &l->lv_watch;
1805 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
1806 {
1807 if (lw == lwrem)
1808 {
1809 *lwp = lw->lw_next;
1810 break;
1811 }
1812 lwp = &lw->lw_next;
1813 }
1814}
1815
1816/*
1817 * Just before removing an item from a list: advance watchers to the next
1818 * item.
1819 */
1820 static void
1821list_fix_watch(l, item)
1822 listvar *l;
1823 listitem *item;
1824{
1825 listwatch *lw;
1826
1827 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
1828 if (lw->lw_item == item)
1829 lw->lw_item = item->li_next;
1830}
1831
1832/*
1833 * Evaluate the expression used in a ":for var in expr" command.
1834 * "arg" points to "var".
1835 * Set "*errp" to TRUE for an error, FALSE otherwise;
1836 * Return a pointer that holds the info. Null when there is an error.
1837 */
1838 void *
1839eval_for_line(arg, errp, nextcmdp, skip)
1840 char_u *arg;
1841 int *errp;
1842 char_u **nextcmdp;
1843 int skip;
1844{
1845 forinfo *fi;
1846 char_u *expr;
1847 typeval tv;
1848 listvar *l;
1849
1850 *errp = TRUE; /* default: there is an error */
1851
1852 fi = (forinfo *)alloc_clear(sizeof(forinfo));
1853 if (fi == NULL)
1854 return NULL;
1855
1856 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
1857 if (expr == NULL)
1858 return fi;
1859
1860 expr = skipwhite(expr);
1861 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
1862 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001863 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001864 return fi;
1865 }
1866
1867 if (skip)
1868 ++emsg_skip;
1869 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
1870 {
1871 *errp = FALSE;
1872 if (!skip)
1873 {
1874 l = tv.vval.v_list;
1875 if (tv.v_type != VAR_LIST || l == NULL)
1876 EMSG(_(e_listreq));
1877 else
1878 {
1879 fi->fi_list = l;
1880 list_add_watch(l, &fi->fi_lw);
1881 fi->fi_lw.lw_item = l->lv_first;
1882 }
1883 }
1884 }
1885 if (skip)
1886 --emsg_skip;
1887
1888 return fi;
1889}
1890
1891/*
1892 * Use the first item in a ":for" list. Advance to the next.
1893 * Assign the values to the variable (list). "arg" points to the first one.
1894 * Return TRUE when a valid item was found, FALSE when at end of list or
1895 * something wrong.
1896 */
1897 int
1898next_for_item(fi_void, arg)
1899 void *fi_void;
1900 char_u *arg;
1901{
1902 forinfo *fi = (forinfo *)fi_void;
1903 int result;
1904 listitem *item;
1905
1906 item = fi->fi_lw.lw_item;
1907 if (item == NULL)
1908 result = FALSE;
1909 else
1910 {
1911 fi->fi_lw.lw_item = item->li_next;
1912 result = (ex_let_vars(arg, &item->li_tv, TRUE,
1913 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
1914 }
1915 return result;
1916}
1917
1918/*
1919 * Free the structure used to store info used by ":for".
1920 */
1921 void
1922free_for_info(fi_void)
1923 void *fi_void;
1924{
1925 forinfo *fi = (forinfo *)fi_void;
1926
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001927 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 list_rem_watch(fi->fi_list, &fi->fi_lw);
1929 vim_free(fi);
1930}
1931
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
1933
1934 void
1935set_context_for_expression(xp, arg, cmdidx)
1936 expand_T *xp;
1937 char_u *arg;
1938 cmdidx_T cmdidx;
1939{
1940 int got_eq = FALSE;
1941 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944 if (cmdidx == CMD_let)
1945 {
1946 xp->xp_context = EXPAND_USER_VARS;
1947 if (vim_strchr(arg, '=') == NULL)
1948 {
1949 /* ":let var1 var2 ...": find last space. */
1950 for (p = arg + STRLEN(arg); p > arg; )
1951 {
1952 xp->xp_pattern = p;
1953 p = mb_ptr_back(arg, p);
1954 if (vim_iswhite(*p))
1955 break;
1956 }
1957 return;
1958 }
1959 }
1960 else
1961 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1962 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 while ((xp->xp_pattern = vim_strpbrk(arg,
1964 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1965 {
1966 c = *xp->xp_pattern;
1967 if (c == '&')
1968 {
1969 c = xp->xp_pattern[1];
1970 if (c == '&')
1971 {
1972 ++xp->xp_pattern;
1973 xp->xp_context = cmdidx != CMD_let || got_eq
1974 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1975 }
1976 else if (c != ' ')
1977 xp->xp_context = EXPAND_SETTINGS;
1978 }
1979 else if (c == '$')
1980 {
1981 /* environment variable */
1982 xp->xp_context = EXPAND_ENV_VARS;
1983 }
1984 else if (c == '=')
1985 {
1986 got_eq = TRUE;
1987 xp->xp_context = EXPAND_EXPRESSION;
1988 }
1989 else if (c == '<'
1990 && xp->xp_context == EXPAND_FUNCTIONS
1991 && vim_strchr(xp->xp_pattern, '(') == NULL)
1992 {
1993 /* Function name can start with "<SNR>" */
1994 break;
1995 }
1996 else if (cmdidx != CMD_let || got_eq)
1997 {
1998 if (c == '"') /* string */
1999 {
2000 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2001 if (c == '\\' && xp->xp_pattern[1] != NUL)
2002 ++xp->xp_pattern;
2003 xp->xp_context = EXPAND_NOTHING;
2004 }
2005 else if (c == '\'') /* literal string */
2006 {
2007 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2008 /* skip */ ;
2009 xp->xp_context = EXPAND_NOTHING;
2010 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00002011 else if (c == '#') /* sharp string */
2012 {
2013 /* Trick: ## is like stopping and starting a sharp string. */
2014 while ((c = *++xp->xp_pattern) != NUL && c != '#')
2015 /* skip */ ;
2016 xp->xp_context = EXPAND_NOTHING;
2017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 else if (c == '|')
2019 {
2020 if (xp->xp_pattern[1] == '|')
2021 {
2022 ++xp->xp_pattern;
2023 xp->xp_context = EXPAND_EXPRESSION;
2024 }
2025 else
2026 xp->xp_context = EXPAND_COMMANDS;
2027 }
2028 else
2029 xp->xp_context = EXPAND_EXPRESSION;
2030 }
2031 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 /* Doesn't look like something valid, expand as an expression
2033 * anyway. */
2034 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 arg = xp->xp_pattern;
2036 if (*arg != NUL)
2037 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2038 /* skip */ ;
2039 }
2040 xp->xp_pattern = arg;
2041}
2042
2043#endif /* FEAT_CMDL_COMPL */
2044
2045/*
2046 * ":1,25call func(arg1, arg2)" function call.
2047 */
2048 void
2049ex_call(eap)
2050 exarg_T *eap;
2051{
2052 char_u *arg = eap->arg;
2053 char_u *startarg;
2054 char_u *alias;
2055 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002056 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 int len;
2058 linenr_T lnum;
2059 int doesrange;
2060 int failed = FALSE;
2061
2062 name = arg;
2063 len = get_func_len(&arg, &alias, !eap->skip);
2064 if (len == 0)
2065 goto end;
2066 if (alias != NULL)
2067 name = alias;
2068
2069 startarg = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002070 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071
2072 if (*startarg != '(')
2073 {
2074 EMSG2(_("E107: Missing braces: %s"), name);
2075 goto end;
2076 }
2077
2078 /*
2079 * When skipping, evaluate the function once, to find the end of the
2080 * arguments.
2081 * When the function takes a range, this is discovered after the first
2082 * call, and the loop is broken.
2083 */
2084 if (eap->skip)
2085 {
2086 ++emsg_skip;
2087 lnum = eap->line2; /* do it once, also with an invalid range */
2088 }
2089 else
2090 lnum = eap->line1;
2091 for ( ; lnum <= eap->line2; ++lnum)
2092 {
2093 if (!eap->skip && eap->addr_count > 0)
2094 {
2095 curwin->w_cursor.lnum = lnum;
2096 curwin->w_cursor.col = 0;
2097 }
2098 arg = startarg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002099 if (get_func_tv(name, len, &rettv, &arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 eap->line1, eap->line2, &doesrange, !eap->skip) == FAIL)
2101 {
2102 failed = TRUE;
2103 break;
2104 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002105 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 if (doesrange || eap->skip)
2107 break;
2108 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002109 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002110 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002111 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 if (aborting())
2113 break;
2114 }
2115 if (eap->skip)
2116 --emsg_skip;
2117
2118 if (!failed)
2119 {
2120 /* Check for trailing illegal characters and a following command. */
2121 if (!ends_excmd(*arg))
2122 {
2123 emsg_severe = TRUE;
2124 EMSG(_(e_trailing));
2125 }
2126 else
2127 eap->nextcmd = check_nextcmd(arg);
2128 }
2129
2130end:
2131 if (alias != NULL)
2132 vim_free(alias);
2133}
2134
2135/*
2136 * ":unlet[!] var1 ... " command.
2137 */
2138 void
2139ex_unlet(eap)
2140 exarg_T *eap;
2141{
2142 char_u *arg = eap->arg;
2143 char_u *name_end;
2144 char_u cc;
2145 char_u *expr_start;
2146 char_u *expr_end;
2147 int error = FALSE;
2148
2149 do
2150 {
2151 /* Find the end of the name. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002152 name_end = find_name_end(arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153
2154 if (!vim_iswhite(*name_end) && !ends_excmd(*name_end))
2155 {
2156 emsg_severe = TRUE;
2157 EMSG(_(e_trailing));
2158 break;
2159 }
2160
2161 if (!error && !eap->skip)
2162 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 if (expr_start != NULL)
2164 {
2165 char_u *temp_string;
2166
2167 temp_string = make_expanded_name(arg, expr_start,
2168 expr_end, name_end);
2169 if (temp_string == NULL)
2170 {
2171 /*
2172 * Report an invalid expression in braces, unless the
2173 * expression evaluation has been cancelled due to an
2174 * aborting error, an interrupt, or an exception.
2175 */
2176 if (!aborting())
2177 {
2178 emsg_severe = TRUE;
2179 EMSG2(_(e_invarg2), arg);
2180 break;
2181 }
2182 error = TRUE;
2183 }
2184 else
2185 {
2186 if (do_unlet(temp_string) == FAIL && !eap->forceit)
2187 {
2188 EMSG2(_("E108: No such variable: \"%s\""), temp_string);
2189 error = TRUE;
2190 }
2191 vim_free(temp_string);
2192 }
2193 }
2194 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 {
2196 cc = *name_end;
2197 *name_end = NUL;
2198
2199 if (do_unlet(arg) == FAIL && !eap->forceit)
2200 {
2201 EMSG2(_("E108: No such variable: \"%s\""), arg);
2202 error = TRUE;
2203 }
2204
2205 *name_end = cc;
2206 }
2207 }
2208 arg = skipwhite(name_end);
2209 } while (!ends_excmd(*arg));
2210
2211 eap->nextcmd = check_nextcmd(arg);
2212}
2213
2214/*
2215 * "unlet" a variable. Return OK if it existed, FAIL if not.
2216 */
2217 int
2218do_unlet(name)
2219 char_u *name;
2220{
2221 VAR v;
2222
2223 v = find_var(name, TRUE);
2224 if (v != NULL)
2225 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002226 clear_var(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 return OK;
2228 }
2229 return FAIL;
2230}
2231
2232#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2233/*
2234 * Delete all "menutrans_" variables.
2235 */
2236 void
2237del_menutrans_vars()
2238{
2239 int i;
2240
2241 for (i = 0; i < variables.ga_len; ++i)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002242 if (VAR_ENTRY(i).v_name != NULL
2243 && STRNCMP(VAR_ENTRY(i).v_name, "menutrans_", 10) == 0)
2244 clear_var(&VAR_ENTRY(i));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245}
2246#endif
2247
2248#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2249
2250/*
2251 * Local string buffer for the next two functions to store a variable name
2252 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2253 * get_user_var_name().
2254 */
2255
2256static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
2257
2258static char_u *varnamebuf = NULL;
2259static int varnamebuflen = 0;
2260
2261/*
2262 * Function to concatenate a prefix and a variable name.
2263 */
2264 static char_u *
2265cat_prefix_varname(prefix, name)
2266 int prefix;
2267 char_u *name;
2268{
2269 int len;
2270
2271 len = (int)STRLEN(name) + 3;
2272 if (len > varnamebuflen)
2273 {
2274 vim_free(varnamebuf);
2275 len += 10; /* some additional space */
2276 varnamebuf = alloc(len);
2277 if (varnamebuf == NULL)
2278 {
2279 varnamebuflen = 0;
2280 return NULL;
2281 }
2282 varnamebuflen = len;
2283 }
2284 *varnamebuf = prefix;
2285 varnamebuf[1] = ':';
2286 STRCPY(varnamebuf + 2, name);
2287 return varnamebuf;
2288}
2289
2290/*
2291 * Function given to ExpandGeneric() to obtain the list of user defined
2292 * (global/buffer/window/built-in) variable names.
2293 */
2294/*ARGSUSED*/
2295 char_u *
2296get_user_var_name(xp, idx)
2297 expand_T *xp;
2298 int idx;
2299{
2300 static int gidx;
2301 static int bidx;
2302 static int widx;
2303 static int vidx;
2304 char_u *name;
2305
2306 if (idx == 0)
2307 gidx = bidx = widx = vidx = 0;
2308 if (gidx < variables.ga_len) /* Global variables */
2309 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002310 while ((name = VAR_ENTRY(gidx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 && gidx < variables.ga_len)
2312 /* skip */;
2313 if (name != NULL)
2314 {
2315 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2316 return cat_prefix_varname('g', name);
2317 else
2318 return name;
2319 }
2320 }
2321 if (bidx < curbuf->b_vars.ga_len) /* Current buffer variables */
2322 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002323 while ((name = BVAR_ENTRY(bidx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 && bidx < curbuf->b_vars.ga_len)
2325 /* skip */;
2326 if (name != NULL)
2327 return cat_prefix_varname('b', name);
2328 }
2329 if (bidx == curbuf->b_vars.ga_len)
2330 {
2331 ++bidx;
2332 return (char_u *)"b:changedtick";
2333 }
2334 if (widx < curwin->w_vars.ga_len) /* Current window variables */
2335 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002336 while ((name = WVAR_ENTRY(widx++).v_name) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 && widx < curwin->w_vars.ga_len)
2338 /* skip */;
2339 if (name != NULL)
2340 return cat_prefix_varname('w', name);
2341 }
2342 if (vidx < VV_LEN) /* Built-in variables */
2343 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].name);
2344
2345 vim_free(varnamebuf);
2346 varnamebuf = NULL;
2347 varnamebuflen = 0;
2348 return NULL;
2349}
2350
2351#endif /* FEAT_CMDL_COMPL */
2352
2353/*
2354 * types for expressions.
2355 */
2356typedef enum
2357{
2358 TYPE_UNKNOWN = 0
2359 , TYPE_EQUAL /* == */
2360 , TYPE_NEQUAL /* != */
2361 , TYPE_GREATER /* > */
2362 , TYPE_GEQUAL /* >= */
2363 , TYPE_SMALLER /* < */
2364 , TYPE_SEQUAL /* <= */
2365 , TYPE_MATCH /* =~ */
2366 , TYPE_NOMATCH /* !~ */
2367} exptype_T;
2368
2369/*
2370 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2373 */
2374
2375/*
2376 * Handle zero level expression.
2377 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002378 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 * Return OK or FAIL.
2380 */
2381 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 char_u *arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 char_u **nextcmd;
2386 int evaluate;
2387{
2388 int ret;
2389 char_u *p;
2390
2391 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002392 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 if (ret == FAIL || !ends_excmd(*p))
2394 {
2395 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002396 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 /*
2398 * Report the invalid expression unless the expression evaluation has
2399 * been cancelled due to an aborting error, an interrupt, or an
2400 * exception.
2401 */
2402 if (!aborting())
2403 EMSG2(_(e_invexpr2), arg);
2404 ret = FAIL;
2405 }
2406 if (nextcmd != NULL)
2407 *nextcmd = check_nextcmd(p);
2408
2409 return ret;
2410}
2411
2412/*
2413 * Handle top level expression:
2414 * expr1 ? expr0 : expr0
2415 *
2416 * "arg" must point to the first non-white of the expression.
2417 * "arg" is advanced to the next non-white after the recognized expression.
2418 *
2419 * Return OK or FAIL.
2420 */
2421 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 int evaluate;
2426{
2427 int result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002428 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429
2430 /*
2431 * Get the first variable.
2432 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002433 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 return FAIL;
2435
2436 if ((*arg)[0] == '?')
2437 {
2438 result = FALSE;
2439 if (evaluate)
2440 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002441 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002443 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 }
2445
2446 /*
2447 * Get the second variable.
2448 */
2449 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002450 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 return FAIL;
2452
2453 /*
2454 * Check for the ":".
2455 */
2456 if ((*arg)[0] != ':')
2457 {
2458 EMSG(_("E109: Missing ':' after '?'"));
2459 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002460 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 return FAIL;
2462 }
2463
2464 /*
2465 * Get the third variable.
2466 */
2467 *arg = skipwhite(*arg + 1);
2468 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
2469 {
2470 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 return FAIL;
2473 }
2474 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 }
2477
2478 return OK;
2479}
2480
2481/*
2482 * Handle first level expression:
2483 * expr2 || expr2 || expr2 logical OR
2484 *
2485 * "arg" must point to the first non-white of the expression.
2486 * "arg" is advanced to the next non-white after the recognized expression.
2487 *
2488 * Return OK or FAIL.
2489 */
2490 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002491eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002493 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 int evaluate;
2495{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002496 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 long result;
2498 int first;
2499
2500 /*
2501 * Get the first variable.
2502 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002503 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 return FAIL;
2505
2506 /*
2507 * Repeat until there is no following "||".
2508 */
2509 first = TRUE;
2510 result = FALSE;
2511 while ((*arg)[0] == '|' && (*arg)[1] == '|')
2512 {
2513 if (evaluate && first)
2514 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002515 if (get_tv_number(rettv) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 first = FALSE;
2519 }
2520
2521 /*
2522 * Get the second variable.
2523 */
2524 *arg = skipwhite(*arg + 2);
2525 if (eval3(arg, &var2, evaluate && !result) == FAIL)
2526 return FAIL;
2527
2528 /*
2529 * Compute the result.
2530 */
2531 if (evaluate && !result)
2532 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002533 if (get_tv_number(&var2) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002535 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 }
2537 if (evaluate)
2538 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002539 rettv->v_type = VAR_NUMBER;
2540 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 }
2542 }
2543
2544 return OK;
2545}
2546
2547/*
2548 * Handle second level expression:
2549 * expr3 && expr3 && expr3 logical AND
2550 *
2551 * "arg" must point to the first non-white of the expression.
2552 * "arg" is advanced to the next non-white after the recognized expression.
2553 *
2554 * Return OK or FAIL.
2555 */
2556 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002557eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002559 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 int evaluate;
2561{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002562 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 long result;
2564 int first;
2565
2566 /*
2567 * Get the first variable.
2568 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002569 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 return FAIL;
2571
2572 /*
2573 * Repeat until there is no following "&&".
2574 */
2575 first = TRUE;
2576 result = TRUE;
2577 while ((*arg)[0] == '&' && (*arg)[1] == '&')
2578 {
2579 if (evaluate && first)
2580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002581 if (get_tv_number(rettv) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002583 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 first = FALSE;
2585 }
2586
2587 /*
2588 * Get the second variable.
2589 */
2590 *arg = skipwhite(*arg + 2);
2591 if (eval4(arg, &var2, evaluate && result) == FAIL)
2592 return FAIL;
2593
2594 /*
2595 * Compute the result.
2596 */
2597 if (evaluate && result)
2598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599 if (get_tv_number(&var2) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002601 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 }
2603 if (evaluate)
2604 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 rettv->v_type = VAR_NUMBER;
2606 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 }
2608 }
2609
2610 return OK;
2611}
2612
2613/*
2614 * Handle third level expression:
2615 * var1 == var2
2616 * var1 =~ var2
2617 * var1 != var2
2618 * var1 !~ var2
2619 * var1 > var2
2620 * var1 >= var2
2621 * var1 < var2
2622 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002623 * var1 is var2
2624 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 *
2626 * "arg" must point to the first non-white of the expression.
2627 * "arg" is advanced to the next non-white after the recognized expression.
2628 *
2629 * Return OK or FAIL.
2630 */
2631 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002634 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 int evaluate;
2636{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002637 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 char_u *p;
2639 int i;
2640 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002641 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 int len = 2;
2643 long n1, n2;
2644 char_u *s1, *s2;
2645 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2646 regmatch_T regmatch;
2647 int ic;
2648 char_u *save_cpo;
2649
2650 /*
2651 * Get the first variable.
2652 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002653 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 return FAIL;
2655
2656 p = *arg;
2657 switch (p[0])
2658 {
2659 case '=': if (p[1] == '=')
2660 type = TYPE_EQUAL;
2661 else if (p[1] == '~')
2662 type = TYPE_MATCH;
2663 break;
2664 case '!': if (p[1] == '=')
2665 type = TYPE_NEQUAL;
2666 else if (p[1] == '~')
2667 type = TYPE_NOMATCH;
2668 break;
2669 case '>': if (p[1] != '=')
2670 {
2671 type = TYPE_GREATER;
2672 len = 1;
2673 }
2674 else
2675 type = TYPE_GEQUAL;
2676 break;
2677 case '<': if (p[1] != '=')
2678 {
2679 type = TYPE_SMALLER;
2680 len = 1;
2681 }
2682 else
2683 type = TYPE_SEQUAL;
2684 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002685 case 'i': if (p[1] == 's')
2686 {
2687 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2688 len = 5;
2689 if (!vim_isIDc(p[len]))
2690 {
2691 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
2692 type_is = TRUE;
2693 }
2694 }
2695 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 }
2697
2698 /*
2699 * If there is a comparitive operator, use it.
2700 */
2701 if (type != TYPE_UNKNOWN)
2702 {
2703 /* extra question mark appended: ignore case */
2704 if (p[len] == '?')
2705 {
2706 ic = TRUE;
2707 ++len;
2708 }
2709 /* extra '#' appended: match case */
2710 else if (p[len] == '#')
2711 {
2712 ic = FALSE;
2713 ++len;
2714 }
2715 /* nothing appened: use 'ignorecase' */
2716 else
2717 ic = p_ic;
2718
2719 /*
2720 * Get the second variable.
2721 */
2722 *arg = skipwhite(p + len);
2723 if (eval5(arg, &var2, evaluate) == FAIL)
2724 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002725 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 return FAIL;
2727 }
2728
2729 if (evaluate)
2730 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002731 if (type_is && rettv->v_type != var2.v_type)
2732 {
2733 /* For "is" a different type always means FALSE, for "notis"
2734 * it means TRUE. */
2735 n1 = (type == TYPE_NEQUAL);
2736 }
2737 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
2738 {
2739 if (type_is)
2740 {
2741 n1 = (rettv->v_type == var2.v_type
2742 && rettv->vval.v_list == var2.vval.v_list);
2743 if (type == TYPE_NEQUAL)
2744 n1 = !n1;
2745 }
2746 else if (rettv->v_type != var2.v_type
2747 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
2748 {
2749 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002750 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002751 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002752 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002753 clear_tv(rettv);
2754 clear_tv(&var2);
2755 return FAIL;
2756 }
2757 else
2758 {
2759 /* Compare two Lists for being equal or unequal. */
2760 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list, ic);
2761 if (type == TYPE_NEQUAL)
2762 n1 = !n1;
2763 }
2764 }
2765
2766 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
2767 {
2768 if (rettv->v_type != var2.v_type
2769 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
2770 {
2771 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002772 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002773 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002774 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002775 clear_tv(rettv);
2776 clear_tv(&var2);
2777 return FAIL;
2778 }
2779 else
2780 {
2781 /* Compare two Funcrefs for being equal or unequal. */
2782 if (rettv->vval.v_string == NULL
2783 || var2.vval.v_string == NULL)
2784 n1 = FALSE;
2785 else
2786 n1 = STRCMP(rettv->vval.v_string,
2787 var2.vval.v_string) == 0;
2788 if (type == TYPE_NEQUAL)
2789 n1 = !n1;
2790 }
2791 }
2792
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 /*
2794 * If one of the two variables is a number, compare as a number.
2795 * When using "=~" or "!~", always compare as string.
2796 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002797 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 && type != TYPE_MATCH && type != TYPE_NOMATCH)
2799 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002800 n1 = get_tv_number(rettv);
2801 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 switch (type)
2803 {
2804 case TYPE_EQUAL: n1 = (n1 == n2); break;
2805 case TYPE_NEQUAL: n1 = (n1 != n2); break;
2806 case TYPE_GREATER: n1 = (n1 > n2); break;
2807 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
2808 case TYPE_SMALLER: n1 = (n1 < n2); break;
2809 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
2810 case TYPE_UNKNOWN:
2811 case TYPE_MATCH:
2812 case TYPE_NOMATCH: break; /* avoid gcc warning */
2813 }
2814 }
2815 else
2816 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002817 s1 = get_tv_string_buf(rettv, buf1);
2818 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
2820 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
2821 else
2822 i = 0;
2823 n1 = FALSE;
2824 switch (type)
2825 {
2826 case TYPE_EQUAL: n1 = (i == 0); break;
2827 case TYPE_NEQUAL: n1 = (i != 0); break;
2828 case TYPE_GREATER: n1 = (i > 0); break;
2829 case TYPE_GEQUAL: n1 = (i >= 0); break;
2830 case TYPE_SMALLER: n1 = (i < 0); break;
2831 case TYPE_SEQUAL: n1 = (i <= 0); break;
2832
2833 case TYPE_MATCH:
2834 case TYPE_NOMATCH:
2835 /* avoid 'l' flag in 'cpoptions' */
2836 save_cpo = p_cpo;
2837 p_cpo = (char_u *)"";
2838 regmatch.regprog = vim_regcomp(s2,
2839 RE_MAGIC + RE_STRING);
2840 regmatch.rm_ic = ic;
2841 if (regmatch.regprog != NULL)
2842 {
2843 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
2844 vim_free(regmatch.regprog);
2845 if (type == TYPE_NOMATCH)
2846 n1 = !n1;
2847 }
2848 p_cpo = save_cpo;
2849 break;
2850
2851 case TYPE_UNKNOWN: break; /* avoid gcc warning */
2852 }
2853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002854 clear_tv(rettv);
2855 clear_tv(&var2);
2856 rettv->v_type = VAR_NUMBER;
2857 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 }
2859 }
2860
2861 return OK;
2862}
2863
2864/*
2865 * Handle fourth level expression:
2866 * + number addition
2867 * - number subtraction
2868 * . string concatenation
2869 *
2870 * "arg" must point to the first non-white of the expression.
2871 * "arg" is advanced to the next non-white after the recognized expression.
2872 *
2873 * Return OK or FAIL.
2874 */
2875 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002876eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002878 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 int evaluate;
2880{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002881 typeval var2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002882 typeval var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 int op;
2884 long n1, n2;
2885 char_u *s1, *s2;
2886 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2887 char_u *p;
2888
2889 /*
2890 * Get the first variable.
2891 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002892 if (eval6(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 return FAIL;
2894
2895 /*
2896 * Repeat computing, until no '+', '-' or '.' is following.
2897 */
2898 for (;;)
2899 {
2900 op = **arg;
2901 if (op != '+' && op != '-' && op != '.')
2902 break;
2903
2904 /*
2905 * Get the second variable.
2906 */
2907 *arg = skipwhite(*arg + 1);
2908 if (eval6(arg, &var2, evaluate) == FAIL)
2909 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002910 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 return FAIL;
2912 }
2913
2914 if (evaluate)
2915 {
2916 /*
2917 * Compute the result.
2918 */
2919 if (op == '.')
2920 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002921 s1 = get_tv_string_buf(rettv, buf1);
2922 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 op = (int)STRLEN(s1);
2924 p = alloc((unsigned)(op + STRLEN(s2) + 1));
2925 if (p != NULL)
2926 {
2927 STRCPY(p, s1);
2928 STRCPY(p + op, s2);
2929 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002930 clear_tv(rettv);
2931 rettv->v_type = VAR_STRING;
2932 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002934 else if (rettv->v_type == VAR_LIST && var2.v_type == VAR_LIST)
2935 {
2936 /* concatenate Lists */
2937 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
2938 &var3) == FAIL)
2939 {
2940 clear_tv(rettv);
2941 clear_tv(&var2);
2942 return FAIL;
2943 }
2944 clear_tv(rettv);
2945 *rettv = var3;
2946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 else
2948 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002949 n1 = get_tv_number(rettv);
2950 n2 = get_tv_number(&var2);
2951 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 if (op == '+')
2953 n1 = n1 + n2;
2954 else
2955 n1 = n1 - n2;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002956 rettv->v_type = VAR_NUMBER;
2957 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002959 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 }
2961 }
2962 return OK;
2963}
2964
2965/*
2966 * Handle fifth level expression:
2967 * * number multiplication
2968 * / number division
2969 * % number modulo
2970 *
2971 * "arg" must point to the first non-white of the expression.
2972 * "arg" is advanced to the next non-white after the recognized expression.
2973 *
2974 * Return OK or FAIL.
2975 */
2976 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002977eval6(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002979 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 int evaluate;
2981{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002982 typeval var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 int op;
2984 long n1, n2;
2985
2986 /*
2987 * Get the first variable.
2988 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002989 if (eval7(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 return FAIL;
2991
2992 /*
2993 * Repeat computing, until no '*', '/' or '%' is following.
2994 */
2995 for (;;)
2996 {
2997 op = **arg;
2998 if (op != '*' && op != '/' && op != '%')
2999 break;
3000
3001 if (evaluate)
3002 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003003 n1 = get_tv_number(rettv);
3004 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 }
3006 else
3007 n1 = 0;
3008
3009 /*
3010 * Get the second variable.
3011 */
3012 *arg = skipwhite(*arg + 1);
3013 if (eval7(arg, &var2, evaluate) == FAIL)
3014 return FAIL;
3015
3016 if (evaluate)
3017 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003018 n2 = get_tv_number(&var2);
3019 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020
3021 /*
3022 * Compute the result.
3023 */
3024 if (op == '*')
3025 n1 = n1 * n2;
3026 else if (op == '/')
3027 {
3028 if (n2 == 0) /* give an error message? */
3029 n1 = 0x7fffffffL;
3030 else
3031 n1 = n1 / n2;
3032 }
3033 else
3034 {
3035 if (n2 == 0) /* give an error message? */
3036 n1 = 0;
3037 else
3038 n1 = n1 % n2;
3039 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003040 rettv->v_type = VAR_NUMBER;
3041 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 }
3043 }
3044
3045 return OK;
3046}
3047
3048/*
3049 * Handle sixth level expression:
3050 * number number constant
3051 * "string" string contstant
3052 * 'string' literal string contstant
3053 * &option-name option value
3054 * @r register contents
3055 * identifier variable value
3056 * function() function call
3057 * $VAR environment variable
3058 * (expression) nested expression
3059 *
3060 * Also handle:
3061 * ! in front logical NOT
3062 * - in front unary minus
3063 * + in front unary plus (ignored)
3064 * trailing [] subscript in String
3065 *
3066 * "arg" must point to the first non-white of the expression.
3067 * "arg" is advanced to the next non-white after the recognized expression.
3068 *
3069 * Return OK or FAIL.
3070 */
3071 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003072eval7(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003074 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 int evaluate;
3076{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 long n;
3078 int len;
3079 char_u *s;
3080 int val;
3081 char_u *start_leader, *end_leader;
3082 int ret = OK;
3083 char_u *alias;
3084
3085 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003086 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003087 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003089 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090
3091 /*
3092 * Skip '!' and '-' characters. They are handled later.
3093 */
3094 start_leader = *arg;
3095 while (**arg == '!' || **arg == '-' || **arg == '+')
3096 *arg = skipwhite(*arg + 1);
3097 end_leader = *arg;
3098
3099 switch (**arg)
3100 {
3101 /*
3102 * Number constant.
3103 */
3104 case '0':
3105 case '1':
3106 case '2':
3107 case '3':
3108 case '4':
3109 case '5':
3110 case '6':
3111 case '7':
3112 case '8':
3113 case '9':
3114 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
3115 *arg += len;
3116 if (evaluate)
3117 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003118 rettv->v_type = VAR_NUMBER;
3119 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 }
3121 break;
3122
3123 /*
3124 * String constant: "string".
3125 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003126 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 break;
3128
3129 /*
3130 * Literal string constant: 'string'.
3131 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003132 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003133 break;
3134
3135 /*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003136 * Sharp string constant: #str##ing#.
3137 */
3138 case '#': ret = get_sharp_string_tv(arg, rettv, evaluate);
3139 break;
3140
3141 /*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003142 * List: [expr, expr]
3143 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003144 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 break;
3146
3147 /*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003148 * Option value: &name or map() item "&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003150 case '&': if (!ASCII_ISALPHA(*(*arg + 1)))
3151 {
3152 *arg = skipwhite(*arg + 1);
3153 if (evaluate)
3154 ret = get_amp_tv(rettv);
3155 }
3156 else
3157 ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 break;
3159
3160 /*
3161 * Environment variable: $VAR.
3162 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003163 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 break;
3165
3166 /*
3167 * Register contents: @r.
3168 */
3169 case '@': ++*arg;
3170 if (evaluate)
3171 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003172 rettv->v_type = VAR_STRING;
3173 rettv->vval.v_string = get_reg_contents(**arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 }
3175 if (**arg != NUL)
3176 ++*arg;
3177 break;
3178
3179 /*
3180 * nested expression: (expression).
3181 */
3182 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003183 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 if (**arg == ')')
3185 ++*arg;
3186 else if (ret == OK)
3187 {
3188 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003189 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 ret = FAIL;
3191 }
3192 break;
3193
3194 /*
3195 * Must be a variable or function name then.
3196 */
3197 default: s = *arg;
3198 len = get_func_len(arg, &alias, evaluate);
3199 if (alias != NULL)
3200 s = alias;
3201
3202 if (len == 0)
3203 ret = FAIL;
3204 else
3205 {
3206 if (**arg == '(') /* recursive! */
3207 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003208 /* If "s" is the name of a variable of type VAR_FUNC
3209 * use its contents. */
3210 s = deref_func_name(s, &len);
3211
3212 /* Invoke the function. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003213 ret = get_func_tv(s, len, rettv, arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
3215 &len, evaluate);
3216 /* Stop the expression evaluation when immediately
3217 * aborting on error, or when an interrupt occurred or
3218 * an exception was thrown but not caught. */
3219 if (aborting())
3220 {
3221 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003222 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 ret = FAIL;
3224 }
3225 }
3226 else if (evaluate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003227 ret = get_var_tv(s, len, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 }
3229
3230 if (alias != NULL)
3231 vim_free(alias);
3232
3233 break;
3234 }
3235 *arg = skipwhite(*arg);
3236
3237 /*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003238 * Handle expr[expr] and expr[expr:expr] subscript.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003240 while (**arg == '[' && ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003242 if (eval_index(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003244 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 return FAIL;
3246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 }
3248
3249 /*
3250 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3251 */
3252 if (ret == OK && evaluate && end_leader > start_leader)
3253 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003254 val = get_tv_number(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 while (end_leader > start_leader)
3256 {
3257 --end_leader;
3258 if (*end_leader == '!')
3259 val = !val;
3260 else if (*end_leader == '-')
3261 val = -val;
3262 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003263 clear_tv(rettv);
3264 rettv->v_type = VAR_NUMBER;
3265 rettv->vval.v_number = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 }
3267
3268 return ret;
3269}
3270
3271/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003272 * Evaluate an "[expr]" or "[expr:expr]" index.
3273 * "*arg" points to the '['.
3274 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3275 */
3276 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003277eval_index(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003278 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003279 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003280 int evaluate;
3281{
3282 int empty1 = FALSE, empty2 = FALSE;
3283 typeval var1, var2;
3284 long n1, n2 = 0;
3285 long len;
3286 int range;
3287 char_u *s;
3288
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003289 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003290 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003291 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003292 return FAIL;
3293 }
3294
3295 /*
3296 * Get the (first) variable from inside the [].
3297 */
3298 *arg = skipwhite(*arg + 1);
3299 if (**arg == ':')
3300 empty1 = TRUE;
3301 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
3302 return FAIL;
3303
3304 /*
3305 * Get the second variable from inside the [:].
3306 */
3307 if (**arg == ':')
3308 {
3309 range = TRUE;
3310 *arg = skipwhite(*arg + 1);
3311 if (**arg == ']')
3312 empty2 = TRUE;
3313 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
3314 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003315 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003316 return FAIL;
3317 }
3318 }
3319 else
3320 range = FALSE;
3321
3322 /* Check for the ']'. */
3323 if (**arg != ']')
3324 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003325 EMSG(_(e_missbrac));
3326 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003327 if (range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003328 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003329 return FAIL;
3330 }
3331
3332 if (evaluate)
3333 {
3334 if (empty1)
3335 n1 = 0;
3336 else
3337 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003338 n1 = get_tv_number(&var1);
3339 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003340 }
3341 if (range)
3342 {
3343 if (empty2)
3344 n2 = -1;
3345 else
3346 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003347 n2 = get_tv_number(&var2);
3348 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003349 }
3350 }
3351
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003352 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003353 {
3354 case VAR_NUMBER:
3355 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003356 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003357 len = (long)STRLEN(s);
3358 if (range)
3359 {
3360 /* The resulting variable is a substring. If the indexes
3361 * are out of range the result is empty. */
3362 if (n1 < 0)
3363 {
3364 n1 = len + n1;
3365 if (n1 < 0)
3366 n1 = 0;
3367 }
3368 if (n2 < 0)
3369 n2 = len + n2;
3370 else if (n2 >= len)
3371 n2 = len;
3372 if (n1 >= len || n2 < 0 || n1 > n2)
3373 s = NULL;
3374 else
3375 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3376 }
3377 else
3378 {
3379 /* The resulting variable is a string of a single
3380 * character. If the index is too big or negative the
3381 * result is empty. */
3382 if (n1 >= len || n1 < 0)
3383 s = NULL;
3384 else
3385 s = vim_strnsave(s + n1, 1);
3386 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003387 clear_tv(rettv);
3388 rettv->v_type = VAR_STRING;
3389 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003390 break;
3391
3392 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003393 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003394 if (n1 < 0)
3395 n1 = len + n1;
3396 if (!empty1 && (n1 < 0 || n1 >= len))
3397 {
3398 EMSGN(_(e_listidx), n1);
3399 return FAIL;
3400 }
3401 if (range)
3402 {
3403 listvar *l;
3404 listitem *item;
3405
3406 if (n2 < 0)
3407 n2 = len + n2;
3408 if (!empty2 && (n2 < 0 || n2 >= len || n2 < n1))
3409 {
3410 EMSGN(_(e_listidx), n2);
3411 return FAIL;
3412 }
3413 l = list_alloc();
3414 if (l == NULL)
3415 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003416 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003417 n1 <= n2; ++n1)
3418 {
3419 if (list_append_tv(l, &item->li_tv) == FAIL)
3420 {
3421 list_free(l);
3422 return FAIL;
3423 }
3424 item = item->li_next;
3425 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003426 clear_tv(rettv);
3427 rettv->v_type = VAR_LIST;
3428 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00003429 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003430 }
3431 else
3432 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003433 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv,
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003434 &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003435 clear_tv(rettv);
3436 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003437 }
3438 break;
3439 }
3440 }
3441
3442 *arg = skipwhite(*arg + 1); /* skip the ']' */
3443 return OK;
3444}
3445
3446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 * Get an option value.
3448 * "arg" points to the '&' or '+' before the option name.
3449 * "arg" is advanced to character after the option name.
3450 * Return OK or FAIL.
3451 */
3452 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003453get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003455 typeval *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 int evaluate;
3457{
3458 char_u *option_end;
3459 long numval;
3460 char_u *stringval;
3461 int opt_type;
3462 int c;
3463 int working = (**arg == '+'); /* has("+option") */
3464 int ret = OK;
3465 int opt_flags;
3466
3467 /*
3468 * Isolate the option name and find its value.
3469 */
3470 option_end = find_option_end(arg, &opt_flags);
3471 if (option_end == NULL)
3472 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003473 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 EMSG2(_("E112: Option name missing: %s"), *arg);
3475 return FAIL;
3476 }
3477
3478 if (!evaluate)
3479 {
3480 *arg = option_end;
3481 return OK;
3482 }
3483
3484 c = *option_end;
3485 *option_end = NUL;
3486 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003487 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488
3489 if (opt_type == -3) /* invalid name */
3490 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003491 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 EMSG2(_("E113: Unknown option: %s"), *arg);
3493 ret = FAIL;
3494 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003495 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 {
3497 if (opt_type == -2) /* hidden string option */
3498 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003499 rettv->v_type = VAR_STRING;
3500 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
3502 else if (opt_type == -1) /* hidden number option */
3503 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003504 rettv->v_type = VAR_NUMBER;
3505 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 }
3507 else if (opt_type == 1) /* number option */
3508 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003509 rettv->v_type = VAR_NUMBER;
3510 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 }
3512 else /* string option */
3513 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003514 rettv->v_type = VAR_STRING;
3515 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
3517 }
3518 else if (working && (opt_type == -2 || opt_type == -1))
3519 ret = FAIL;
3520
3521 *option_end = c; /* put back for error messages */
3522 *arg = option_end;
3523
3524 return ret;
3525}
3526
3527/*
3528 * Allocate a variable for a string constant.
3529 * Return OK or FAIL.
3530 */
3531 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003532get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003534 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 int evaluate;
3536{
3537 char_u *p;
3538 char_u *name;
3539 int i;
3540 int extra = 0;
3541
3542 /*
3543 * Find the end of the string, skipping backslashed characters.
3544 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003545 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 {
3547 if (*p == '\\' && p[1] != NUL)
3548 {
3549 ++p;
3550 /* A "\<x>" form occupies at least 4 characters, and produces up
3551 * to 6 characters: reserve space for 2 extra */
3552 if (*p == '<')
3553 extra += 2;
3554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 }
3556
3557 if (*p != '"')
3558 {
3559 EMSG2(_("E114: Missing quote: %s"), *arg);
3560 return FAIL;
3561 }
3562
3563 /* If only parsing, set *arg and return here */
3564 if (!evaluate)
3565 {
3566 *arg = p + 1;
3567 return OK;
3568 }
3569
3570 /*
3571 * Copy the string into allocated memory, handling backslashed
3572 * characters.
3573 */
3574 name = alloc((unsigned)(p - *arg + extra));
3575 if (name == NULL)
3576 return FAIL;
3577
3578 i = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003579 for (p = *arg + 1; *p != NUL && *p != '"'; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
3581 if (*p == '\\')
3582 {
3583 switch (*++p)
3584 {
3585 case 'b': name[i++] = BS; break;
3586 case 'e': name[i++] = ESC; break;
3587 case 'f': name[i++] = FF; break;
3588 case 'n': name[i++] = NL; break;
3589 case 'r': name[i++] = CAR; break;
3590 case 't': name[i++] = TAB; break;
3591
3592 case 'X': /* hex: "\x1", "\x12" */
3593 case 'x':
3594 case 'u': /* Unicode: "\u0023" */
3595 case 'U':
3596 if (vim_isxdigit(p[1]))
3597 {
3598 int n, nr;
3599 int c = toupper(*p);
3600
3601 if (c == 'X')
3602 n = 2;
3603 else
3604 n = 4;
3605 nr = 0;
3606 while (--n >= 0 && vim_isxdigit(p[1]))
3607 {
3608 ++p;
3609 nr = (nr << 4) + hex2nr(*p);
3610 }
3611#ifdef FEAT_MBYTE
3612 /* For "\u" store the number according to
3613 * 'encoding'. */
3614 if (c != 'X')
3615 i += (*mb_char2bytes)(nr, name + i);
3616 else
3617#endif
3618 name[i++] = nr;
3619 }
3620 else
3621 name[i++] = *p;
3622 break;
3623
3624 /* octal: "\1", "\12", "\123" */
3625 case '0':
3626 case '1':
3627 case '2':
3628 case '3':
3629 case '4':
3630 case '5':
3631 case '6':
3632 case '7': name[i] = *p - '0';
3633 if (p[1] >= '0' && p[1] <= '7')
3634 {
3635 ++p;
3636 name[i] = (name[i] << 3) + *p - '0';
3637 if (p[1] >= '0' && p[1] <= '7')
3638 {
3639 ++p;
3640 name[i] = (name[i] << 3) + *p - '0';
3641 }
3642 }
3643 ++i;
3644 break;
3645
3646 /* Special key, e.g.: "\<C-W>" */
3647 case '<': extra = trans_special(&p, name + i, TRUE);
3648 if (extra != 0)
3649 {
3650 i += extra;
3651 --p;
3652 break;
3653 }
3654 /* FALLTHROUGH */
3655
3656 default: name[i++] = *p;
3657 break;
3658 }
3659 }
3660 else
3661 name[i++] = *p;
3662
3663#ifdef FEAT_MBYTE
3664 /* For a multi-byte character copy the bytes after the first one. */
3665 if (has_mbyte)
3666 {
3667 int l = (*mb_ptr2len_check)(p);
3668
3669 while (--l > 0)
3670 name[i++] = *++p;
3671 }
3672#endif
3673 }
3674 name[i] = NUL;
3675 *arg = p + 1;
3676
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003677 rettv->v_type = VAR_STRING;
3678 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
3680 return OK;
3681}
3682
3683/*
3684 * Allocate a variable for an backtick-string constant.
3685 * Return OK or FAIL.
3686 */
3687 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003688get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003690 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 int evaluate;
3692{
3693 char_u *p;
3694 char_u *name;
3695
3696 /*
3697 * Find the end of the string.
3698 */
3699 p = vim_strchr(*arg + 1, '\'');
3700 if (p == NULL)
3701 {
3702 EMSG2(_("E115: Missing quote: %s"), *arg);
3703 return FAIL;
3704 }
3705
3706 if (evaluate)
3707 {
3708 /*
3709 * Copy the string into allocated memory.
3710 */
3711 name = vim_strnsave(*arg + 1, (int)(p - (*arg + 1)));
3712 if (name == NULL)
3713 return FAIL;
3714
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003715 rettv->v_type = VAR_STRING;
3716 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 }
3718
3719 *arg = p + 1;
3720
3721 return OK;
3722}
3723
3724/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003725 * Allocate a variable for a #string# constant.
3726 * Return OK or FAIL.
3727 */
3728 static int
3729get_sharp_string_tv(arg, rettv, evaluate)
3730 char_u **arg;
3731 typeval *rettv;
3732 int evaluate;
3733{
3734 char_u *p;
3735 char_u *str;
3736 int i;
3737 int reduce = 0;
3738
3739 /*
3740 * Find the end of the string, skipping ##.
3741 */
3742 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
3743 {
3744 if (*p == '#')
3745 {
3746 if (p[1] != '#')
3747 break;
3748 ++reduce;
3749 ++p;
3750 }
3751 }
3752
3753 if (*p != '#')
3754 {
3755 EMSG2(_("E999: Missing #: %s"), *arg);
3756 return FAIL;
3757 }
3758
3759 /* If only parsing, set *arg and return here */
3760 if (!evaluate)
3761 {
3762 *arg = p + 1;
3763 return OK;
3764 }
3765
3766 /*
3767 * Copy the string into allocated memory, handling ## to # reduction.
3768 */
3769 str = alloc((unsigned)((p - *arg) - reduce));
3770 if (str == NULL)
3771 return FAIL;
3772
3773 i = 0;
3774 for (p = *arg + 1; *p != NUL; ++p)
3775 {
3776 if (*p == '#')
3777 {
3778 if (p[1] != '#')
3779 break;
3780 ++p;
3781 }
3782 str[i++] = *p;
3783
3784#ifdef FEAT_MBYTE
3785 /* For a multi-byte character copy the bytes after the first one. */
3786 if (has_mbyte)
3787 {
3788 int l = (*mb_ptr2len_check)(p);
3789
3790 while (--l > 0)
3791 str[i++] = *++p;
3792 }
3793#endif
3794 }
3795 str[i] = NUL;
3796 *arg = p + 1;
3797
3798 rettv->v_type = VAR_STRING;
3799 rettv->vval.v_string = str;
3800
3801 return OK;
3802}
3803
3804/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003805 * Allocate a variable for a List and fill it from "*arg".
3806 * Return OK or FAIL.
3807 */
3808 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003809get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003810 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003811 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003812 int evaluate;
3813{
3814 listvar *l = NULL;
3815 typeval tv;
3816 listitem *item;
3817
3818 if (evaluate)
3819 {
3820 l = list_alloc();
3821 if (l == NULL)
3822 return FAIL;
3823 }
3824
3825 *arg = skipwhite(*arg + 1);
3826 while (**arg != ']' && **arg != NUL)
3827 {
3828 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
3829 goto failret;
3830 if (evaluate)
3831 {
3832 item = listitem_alloc();
3833 if (item != NULL)
3834 {
3835 item->li_tv = tv;
3836 list_append(l, item);
3837 }
3838 }
3839
3840 if (**arg == ']')
3841 break;
3842 if (**arg != ',')
3843 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003844 EMSG2(_("E696: Missing comma in list: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003845 goto failret;
3846 }
3847 *arg = skipwhite(*arg + 1);
3848 }
3849
3850 if (**arg != ']')
3851 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003852 EMSG2(_("E697: Missing end of list ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003853failret:
3854 if (evaluate)
3855 list_free(l);
3856 return FAIL;
3857 }
3858
3859 *arg = skipwhite(*arg + 1);
3860 if (evaluate)
3861 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003862 rettv->v_type = VAR_LIST;
3863 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003864 ++l->lv_refcount;
3865 }
3866
3867 return OK;
3868}
3869
3870/*
3871 * Allocate an empty header for a list.
3872 */
3873 static listvar *
3874list_alloc()
3875{
3876 return (listvar *)alloc_clear(sizeof(listvar));
3877}
3878
3879/*
3880 * Unreference a list: decrement the reference count and free it when it
3881 * becomes zero.
3882 */
3883 static void
3884list_unref(l)
3885 listvar *l;
3886{
3887 if (l != NULL && --l->lv_refcount <= 0)
3888 list_free(l);
3889}
3890
3891/*
3892 * Free a list, including all items it points to.
3893 * Ignores the reference count.
3894 */
3895 static void
3896list_free(l)
3897 listvar *l;
3898{
3899 listitem *item;
3900 listitem *next;
3901
3902 for (item = l->lv_first; item != NULL; item = next)
3903 {
3904 next = item->li_next;
3905 listitem_free(item);
3906 }
3907 vim_free(l);
3908}
3909
3910/*
3911 * Allocate a list item.
3912 */
3913 static listitem *
3914listitem_alloc()
3915{
3916 return (listitem *)alloc(sizeof(listitem));
3917}
3918
3919/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003920 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003921 */
3922 static void
3923listitem_free(item)
3924 listitem *item;
3925{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003926 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003927 vim_free(item);
3928}
3929
3930/*
3931 * Get the number of items in a list.
3932 */
3933 static long
3934list_len(l)
3935 listvar *l;
3936{
3937 listitem *item;
3938 long len = 0;
3939
3940 if (l == NULL)
3941 return 0L;
3942 for (item = l->lv_first; item != NULL; item = item->li_next)
3943 ++len;
3944 return len;
3945}
3946
3947/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003948 * Return TRUE when two lists have exactly the same values.
3949 */
3950 static int
3951list_equal(l1, l2, ic)
3952 listvar *l1;
3953 listvar *l2;
3954 int ic; /* ignore case for strings */
3955{
3956 listitem *item1, *item2;
3957
3958 for (item1 = l1->lv_first, item2 = l2->lv_first;
3959 item1 != NULL && item2 != NULL;
3960 item1 = item1->li_next, item2 = item2->li_next)
3961 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic))
3962 return FALSE;
3963 return item1 == NULL && item2 == NULL;
3964}
3965
3966/*
3967 * Return TRUE if "tv1" and "tv2" have the same value.
3968 * Compares the items just like "==" would compare them.
3969 */
3970 static int
3971tv_equal(tv1, tv2, ic)
3972 typeval *tv1;
3973 typeval *tv2;
3974 int ic; /* ignore case */
3975{
3976 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3977
3978 if (tv1->v_type == VAR_LIST || tv2->v_type == VAR_LIST)
3979 {
3980 /* recursive! */
3981 if (tv1->v_type != tv2->v_type
3982 || !list_equal(tv1->vval.v_list, tv2->vval.v_list, ic))
3983 return FALSE;
3984 }
3985 else if (tv1->v_type == VAR_FUNC || tv2->v_type == VAR_FUNC)
3986 {
3987 if (tv1->v_type != tv2->v_type
3988 || tv1->vval.v_string == NULL
3989 || tv2->vval.v_string == NULL
3990 || STRCMP(tv1->vval.v_string, tv2->vval.v_string) != 0)
3991 return FALSE;
3992 }
3993 else if (tv1->v_type == VAR_NUMBER || tv2->v_type == VAR_NUMBER)
3994 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003995 /* "4" is equal to 4. But don't consider 'a' and zero to be equal.
3996 * Don't consider "4x" to be equal to 4. */
3997 if ((tv1->v_type == VAR_STRING
3998 && !string_isa_number(tv1->vval.v_string))
3999 || (tv2->v_type == VAR_STRING
4000 && !string_isa_number(tv2->vval.v_string)))
4001 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004002 if (get_tv_number(tv1) != get_tv_number(tv2))
4003 return FALSE;
4004 }
4005 else if (!ic && STRCMP(get_tv_string_buf(tv1, buf1),
4006 get_tv_string_buf(tv2, buf2)) != 0)
4007 return FALSE;
4008 else if (ic && STRICMP(get_tv_string_buf(tv1, buf1),
4009 get_tv_string_buf(tv2, buf2)) != 0)
4010 return FALSE;
4011 return TRUE;
4012}
4013
4014/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004015 * Return TRUE if "tv" is a number without other non-white characters.
4016 */
4017 static int
4018string_isa_number(s)
4019 char_u *s;
4020{
4021 int len;
4022
4023 vim_str2nr(s, NULL, &len, TRUE, TRUE, NULL, NULL);
4024 return len > 0 && *skipwhite(s + len) == NUL;
4025}
4026
4027/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004028 * Locate item with index "n" in list "l" and return it.
4029 * A negative index is counted from the end; -1 is the last item.
4030 * Returns NULL when "n" is out of range.
4031 */
4032 static listitem *
4033list_find(l, n)
4034 listvar *l;
4035 long n;
4036{
4037 listitem *item;
4038 long idx;
4039
4040 if (l == NULL)
4041 return NULL;
4042 if (n < 0)
4043 {
4044 idx = -1; /* search from the end */
4045 for (item = l->lv_last; item != NULL && idx > n; item = item->li_prev)
4046 --idx;
4047 }
4048 else
4049 {
4050 idx = 0; /* search from the start */
4051 for (item = l->lv_first; item != NULL && idx < n; item = item->li_next)
4052 ++idx;
4053 }
4054 if (idx != n)
4055 return NULL;
4056 return item;
4057}
4058
4059/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004060 * Locate "item" list "l" and return its index.
4061 * Returns -1 when "item" is not in the list.
4062 */
4063 static long
4064list_idx_of_item(l, item)
4065 listvar *l;
4066 listitem *item;
4067{
4068 long idx = 0;
4069 listitem *li;
4070
4071 if (l == NULL)
4072 return -1;
4073 idx = 0;
4074 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
4075 ++idx;
4076 if (li == NULL)
4077 return -1;
4078 return idx;;
4079}
4080
4081/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004082 * Like list_find(), but also find an item just past the end.
4083 * "*ip" is the item to find.
4084 * When found "*ip" is set to zero, when not found "*ip" is non-zero.
4085 * Returns NULL when item not found or item is just past the end.
4086 */
4087 static listitem *
4088list_find_ext(l, ip)
4089 listvar *l;
4090 long *ip;
4091{
4092 long n;
4093 listitem *item;
4094
4095 if (*ip < 0)
4096 {
4097 /* Count from the end: -1 is before last item. */
4098 item = l->lv_last;
4099 for (n = *ip + 1; n < 0 && item != NULL; ++n)
4100 item = item->li_prev;
4101 if (item == NULL)
4102 n = 1; /* error! */
4103 }
4104 else
4105 {
4106 item = l->lv_first;
4107 for (n = *ip; n > 0 && item != NULL; --n)
4108 item = item->li_next;
4109 }
4110 *ip = n;
4111 return item;
4112}
4113
4114/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004115 * Append item "item" to the end of list "l".
4116 */
4117 static void
4118list_append(l, item)
4119 listvar *l;
4120 listitem *item;
4121{
4122 if (l->lv_last == NULL)
4123 {
4124 /* empty list */
4125 l->lv_first = item;
4126 l->lv_last = item;
4127 item->li_prev = NULL;
4128 }
4129 else
4130 {
4131 l->lv_last->li_next = item;
4132 item->li_prev = l->lv_last;
4133 l->lv_last = item;
4134 }
4135 item->li_next = NULL;
4136}
4137
4138/*
4139 * Append typeval "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004140 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004141 */
4142 static int
4143list_append_tv(l, tv)
4144 listvar *l;
4145 typeval *tv;
4146{
4147 listitem *ni = listitem_alloc();
4148
4149 if (ni == NULL)
4150 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151 copy_tv(tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004152 list_append(l, ni);
4153 return OK;
4154}
4155
4156/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004157 * Insert typeval "tv" in list "l" before "item".
4158 * If "item" is NULL append at the end.
4159 * Return FAIL when out of memory.
4160 */
4161 static int
4162list_insert_tv(l, tv, item)
4163 listvar *l;
4164 typeval *tv;
4165 listitem *item;
4166{
4167 listitem *ni = listitem_alloc();
4168
4169 if (ni == NULL)
4170 return FAIL;
4171 copy_tv(tv, &ni->li_tv);
4172 if (item == NULL)
4173 /* Append new item at end of list. */
4174 list_append(l, ni);
4175 else
4176 {
4177 /* Insert new item before existing item. */
4178 ni->li_prev = item->li_prev;
4179 ni->li_next = item;
4180 if (item->li_prev == NULL)
4181 l->lv_first = ni;
4182 else
4183 item->li_prev->li_next = ni;
4184 item->li_prev = ni;
4185 }
4186 return OK;
4187}
4188
4189/*
4190 * Extend "l1" with "l2".
4191 * If "bef" is NULL append at the end, otherwise insert before this item.
4192 * Returns FAIL when out of memory.
4193 */
4194 static int
4195list_extend(l1, l2, bef)
4196 listvar *l1;
4197 listvar *l2;
4198 listitem *bef;
4199{
4200 listitem *item;
4201
4202 for (item = l2->lv_first; item != NULL; item = item->li_next)
4203 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
4204 return FAIL;
4205 return OK;
4206}
4207
4208/*
4209 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
4210 * Return FAIL when out of memory.
4211 */
4212 static int
4213list_concat(l1, l2, tv)
4214 listvar *l1;
4215 listvar *l2;
4216 typeval *tv;
4217{
4218 listvar *l;
4219
4220 /* make a copy of the first list. */
4221 l = list_copy(l1, FALSE);
4222 if (l == NULL)
4223 return FAIL;
4224 tv->v_type = VAR_LIST;
4225 tv->vval.v_list = l;
4226
4227 /* append all items from the second list */
4228 return list_extend(l, l2, NULL);
4229}
4230
4231/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004232 * Make a copy of list "l". Shallow if "deep" is FALSE.
4233 * The refcount of the new list is set to 1.
4234 * Returns NULL when out of memory.
4235 */
4236 static listvar *
4237list_copy(orig, deep)
4238 listvar *orig;
4239 int deep;
4240{
4241 listvar *copy;
4242 listitem *item;
4243 listitem *ni;
4244 static int recurse = 0;
4245
4246 if (orig == NULL)
4247 return NULL;
4248 if (recurse >= VAR_LIST_MAXNEST)
4249 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004250 EMSG(_("E698: List nested too deep for making a copy"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004251 return NULL;
4252 }
4253 ++recurse;
4254
4255 copy = list_alloc();
4256 if (copy != NULL)
4257 {
4258 for (item = orig->lv_first; item != NULL; item = item->li_next)
4259 {
4260 ni = listitem_alloc();
4261 if (ni == NULL)
4262 break;
4263 if (deep && item->li_tv.v_type == VAR_LIST)
4264 {
4265 ni->li_tv.v_type = VAR_LIST;
4266 ni->li_tv.vval.v_list = list_copy(item->li_tv.vval.v_list,
4267 TRUE);
4268 if (ni->li_tv.vval.v_list == NULL)
4269 {
4270 vim_free(ni);
4271 break;
4272 }
4273 }
4274 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004275 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004276 list_append(copy, ni);
4277 }
4278 ++copy->lv_refcount;
4279 }
4280
4281 --recurse;
4282 return copy;
4283}
4284
4285/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004286 * Remove items "item" to "item2" from list "l".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004287 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004288 static void
4289list_getrem(l, item, item2)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004290 listvar *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004291 listitem *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004292 listitem *item2;
4293{
4294 listitem *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004295
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004296 /* notify watchers */
4297 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004298 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004299 list_fix_watch(l, ip);
4300 if (ip == item2)
4301 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004302 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004303
4304 if (item2->li_next == NULL)
4305 l->lv_last = item->li_prev;
4306 else
4307 item2->li_next->li_prev = item->li_prev;
4308 if (item->li_prev == NULL)
4309 l->lv_first = item2->li_next;
4310 else
4311 item->li_prev->li_next = item2->li_next;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004312}
4313
4314/*
4315 * Return an allocated string with the string representation of a list.
4316 * May return NULL.
4317 */
4318 static char_u *
4319list2string(tv)
4320 typeval *tv;
4321{
4322 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004323
4324 if (tv->vval.v_list == NULL)
4325 return NULL;
4326 ga_init2(&ga, (int)sizeof(char), 80);
4327 ga_append(&ga, '[');
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004328 list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004329 ga_append(&ga, ']');
4330 ga_append(&ga, NUL);
4331 return (char_u *)ga.ga_data;
4332}
4333
4334/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004335 * Join list "l" into a string in "*gap", using separator "sep".
4336 * When "echo" is TRUE use String as echoed, otherwise as inside a List.
4337 */
4338 static void
4339list_join(gap, l, sep, echo)
4340 garray_T *gap;
4341 listvar *l;
4342 char_u *sep;
4343 int echo;
4344{
4345 int first = TRUE;
4346 char_u *tofree;
4347 char_u numbuf[NUMBUFLEN];
4348 listitem *item;
4349 char_u *s;
4350
4351 for (item = l->lv_first; item != NULL; item = item->li_next)
4352 {
4353 if (first)
4354 first = FALSE;
4355 else
4356 ga_concat(gap, sep);
4357
4358 if (echo)
4359 s = echo_string(&item->li_tv, &tofree, numbuf);
4360 else
4361 s = tv2string(&item->li_tv, &tofree, numbuf);
4362 if (s != NULL)
4363 ga_concat(gap, s);
4364 vim_free(tofree);
4365 }
4366}
4367
4368/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004369 * Return a string with the string representation of a variable.
4370 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004371 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004372 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004373 * May return NULL;
4374 */
4375 static char_u *
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004376echo_string(tv, tofree, numbuf)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004377 typeval *tv;
4378 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004379 char_u *numbuf;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004380{
4381 switch (tv->v_type)
4382 {
4383 case VAR_FUNC:
4384 *tofree = NULL;
4385 return tv->vval.v_string;
4386 case VAR_LIST:
4387 *tofree = list2string(tv);
4388 return *tofree;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004389 case VAR_STRING:
4390 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004391 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004392 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004393 EMSG2(_(e_intern2), "echo_string()");
4394 }
4395 *tofree = NULL;
4396 return get_tv_string_buf(tv, numbuf);
4397}
4398
4399/*
4400 * Return a string with the string representation of a variable.
4401 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4402 * "numbuf" is used for a number.
4403 * Puts quotes around strings, so that they can be parsed back by eval().
4404 * May return NULL;
4405 */
4406 static char_u *
4407tv2string(tv, tofree, numbuf)
4408 typeval *tv;
4409 char_u **tofree;
4410 char_u *numbuf;
4411{
4412 switch (tv->v_type)
4413 {
4414 case VAR_NUMBER:
4415 break;
4416 case VAR_FUNC:
4417 *tofree = string_quote(tv->vval.v_string, TRUE);
4418 return *tofree;
4419 case VAR_STRING:
4420 *tofree = string_quote(tv->vval.v_string, FALSE);
4421 return *tofree;
4422 case VAR_LIST:
4423 *tofree = list2string(tv);
4424 return *tofree;
4425 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004426 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004427 }
4428 *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004429 return get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004430}
4431
4432/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004433 * Return a string in # quotes, doubling # characters.
4434 * If "function" is TRUE make it function(#string#).
4435 */
4436 static char_u *
4437string_quote(str, function)
4438 char_u *str;
4439 int function;
4440{
4441 unsigned len = function ? 13 : 3;
4442 char_u *p, *r, *s;
4443
4444 for (p = str; *p != NUL; mb_ptr_adv(p))
4445 if (*p == '#')
4446 ++len;
4447 s = r = alloc(len);
4448 if (r != NULL)
4449 {
4450 if (function)
4451 {
4452 STRCPY(r, "function(#");
4453 r += 10;
4454 }
4455 else
4456 *r++ = '#';
4457 for (p = str; *p != NUL; ++p)
4458 {
4459 if (*p == '#')
4460 *r++ = '#';
4461 *r++ = *p;
4462#ifdef FEAT_MBYTE
4463 /* For a multi-byte character copy the bytes after the first one. */
4464 if (has_mbyte)
4465 {
4466 int l = (*mb_ptr2len_check)(p);
4467
4468 while (--l > 0)
4469 *r++ = *++p;
4470 }
4471#endif
4472 }
4473 *r++ = '#';
4474 if (function)
4475 *r++ = ')';
4476 *r++ = NUL;
4477 }
4478 return s;
4479}
4480
4481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 * Get the value of an environment variable.
4483 * "arg" is pointing to the '$'. It is advanced to after the name.
4484 * If the environment variable was not set, silently assume it is empty.
4485 * Always return OK.
4486 */
4487 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004488get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 char_u **arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004490 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 int evaluate;
4492{
4493 char_u *string = NULL;
4494 int len;
4495 int cc;
4496 char_u *name;
4497
4498 ++*arg;
4499 name = *arg;
4500 len = get_env_len(arg);
4501 if (evaluate)
4502 {
4503 if (len != 0)
4504 {
4505 cc = name[len];
4506 name[len] = NUL;
4507 /* first try mch_getenv(), fast for normal environment vars */
4508 string = mch_getenv(name);
4509 if (string != NULL && *string != NUL)
4510 string = vim_strsave(string);
4511 else
4512 {
4513 /* next try expanding things like $VIM and ${HOME} */
4514 string = expand_env_save(name - 1);
4515 if (string != NULL && *string == '$')
4516 {
4517 vim_free(string);
4518 string = NULL;
4519 }
4520 }
4521 name[len] = cc;
4522 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004523 rettv->v_type = VAR_STRING;
4524 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526
4527 return OK;
4528}
4529
4530/*
4531 * Array with names and number of arguments of all internal functions
4532 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
4533 */
4534static struct fst
4535{
4536 char *f_name; /* function name */
4537 char f_min_argc; /* minimal number of arguments */
4538 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004539 void (*f_func) __ARGS((typeval *args, typeval *rvar));
4540 /* implemenation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541} functions[] =
4542{
Bram Moolenaar0d660222005-01-07 21:51:51 +00004543 {"add", 2, 2, f_add},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 {"append", 2, 2, f_append},
4545 {"argc", 0, 0, f_argc},
4546 {"argidx", 0, 0, f_argidx},
4547 {"argv", 1, 1, f_argv},
4548 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004549 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 {"bufexists", 1, 1, f_bufexists},
4551 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
4552 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
4553 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
4554 {"buflisted", 1, 1, f_buflisted},
4555 {"bufloaded", 1, 1, f_bufloaded},
4556 {"bufname", 1, 1, f_bufname},
4557 {"bufnr", 1, 1, f_bufnr},
4558 {"bufwinnr", 1, 1, f_bufwinnr},
4559 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00004560 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004561 {"call", 2, 2, f_call},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {"char2nr", 1, 1, f_char2nr},
4563 {"cindent", 1, 1, f_cindent},
4564 {"col", 1, 1, f_col},
4565 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004566 {"copy", 1, 1, f_copy},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004567 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 {"cscope_connection",0,3, f_cscope_connection},
4569 {"cursor", 2, 2, f_cursor},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004570 {"deepcopy", 1, 1, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 {"delete", 1, 1, f_delete},
4572 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00004573 {"diff_filler", 1, 1, f_diff_filler},
4574 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004575 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004577 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {"eventhandler", 0, 0, f_eventhandler},
4579 {"executable", 1, 1, f_executable},
4580 {"exists", 1, 1, f_exists},
4581 {"expand", 1, 2, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004582 {"extend", 2, 3, f_extend},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
4584 {"filereadable", 1, 1, f_filereadable},
4585 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004586 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004587 {"finddir", 1, 3, f_finddir},
4588 {"findfile", 1, 3, f_findfile},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 {"fnamemodify", 2, 2, f_fnamemodify},
4590 {"foldclosed", 1, 1, f_foldclosed},
4591 {"foldclosedend", 1, 1, f_foldclosedend},
4592 {"foldlevel", 1, 1, f_foldlevel},
4593 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00004594 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004596 {"function", 1, 1, f_function},
Bram Moolenaar0d660222005-01-07 21:51:51 +00004597 {"get", 2, 3, f_get},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 {"getbufvar", 2, 2, f_getbufvar},
4599 {"getchar", 0, 1, f_getchar},
4600 {"getcharmod", 0, 0, f_getcharmod},
4601 {"getcmdline", 0, 0, f_getcmdline},
4602 {"getcmdpos", 0, 0, f_getcmdpos},
4603 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00004604 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004605 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 {"getfsize", 1, 1, f_getfsize},
4607 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004608 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00004609 {"getline", 1, 2, f_getline},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 {"getreg", 0, 1, f_getreg},
4611 {"getregtype", 0, 1, f_getregtype},
4612 {"getwinposx", 0, 0, f_getwinposx},
4613 {"getwinposy", 0, 0, f_getwinposy},
4614 {"getwinvar", 2, 2, f_getwinvar},
4615 {"glob", 1, 1, f_glob},
4616 {"globpath", 2, 2, f_globpath},
4617 {"has", 1, 1, f_has},
4618 {"hasmapto", 1, 2, f_hasmapto},
4619 {"highlightID", 1, 1, f_hlID}, /* obsolete */
4620 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
4621 {"histadd", 2, 2, f_histadd},
4622 {"histdel", 1, 2, f_histdel},
4623 {"histget", 1, 2, f_histget},
4624 {"histnr", 1, 1, f_histnr},
4625 {"hlID", 1, 1, f_hlID},
4626 {"hlexists", 1, 1, f_hlexists},
4627 {"hostname", 0, 0, f_hostname},
4628 {"iconv", 3, 3, f_iconv},
4629 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004630 {"index", 2, 4, f_index},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 {"input", 1, 2, f_input},
4632 {"inputdialog", 1, 3, f_inputdialog},
4633 {"inputrestore", 0, 0, f_inputrestore},
4634 {"inputsave", 0, 0, f_inputsave},
4635 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004636 {"insert", 2, 3, f_insert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004638 {"join", 1, 2, f_join},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004640 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 {"libcall", 3, 3, f_libcall},
4642 {"libcallnr", 3, 3, f_libcallnr},
4643 {"line", 1, 1, f_line},
4644 {"line2byte", 1, 1, f_line2byte},
4645 {"lispindent", 1, 1, f_lispindent},
4646 {"localtime", 0, 0, f_localtime},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004647 {"map", 2, 2, f_map},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 {"maparg", 1, 2, f_maparg},
4649 {"mapcheck", 1, 2, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00004650 {"match", 2, 4, f_match},
4651 {"matchend", 2, 4, f_matchend},
4652 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00004653 {"max", 1, 1, f_max},
4654 {"min", 1, 1, f_min},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 {"mode", 0, 0, f_mode},
4656 {"nextnonblank", 1, 1, f_nextnonblank},
4657 {"nr2char", 1, 1, f_nr2char},
4658 {"prevnonblank", 1, 1, f_prevnonblank},
4659 {"remote_expr", 2, 3, f_remote_expr},
4660 {"remote_foreground", 1, 1, f_remote_foreground},
4661 {"remote_peek", 1, 2, f_remote_peek},
4662 {"remote_read", 1, 1, f_remote_read},
4663 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004664 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00004666 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00004668 {"reverse", 1, 1, f_reverse},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 {"search", 1, 2, f_search},
4670 {"searchpair", 3, 5, f_searchpair},
4671 {"server2client", 2, 2, f_server2client},
4672 {"serverlist", 0, 0, f_serverlist},
4673 {"setbufvar", 3, 3, f_setbufvar},
4674 {"setcmdpos", 1, 1, f_setcmdpos},
4675 {"setline", 2, 2, f_setline},
4676 {"setreg", 2, 3, f_setreg},
4677 {"setwinvar", 3, 3, f_setwinvar},
4678 {"simplify", 1, 1, f_simplify},
Bram Moolenaar0d660222005-01-07 21:51:51 +00004679 {"sort", 1, 2, f_sort},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004680 {"split", 1, 2, f_split},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681#ifdef HAVE_STRFTIME
4682 {"strftime", 1, 2, f_strftime},
4683#endif
4684 {"stridx", 2, 2, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004685 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 {"strlen", 1, 1, f_strlen},
4687 {"strpart", 2, 3, f_strpart},
4688 {"strridx", 2, 2, f_strridx},
4689 {"strtrans", 1, 1, f_strtrans},
4690 {"submatch", 1, 1, f_submatch},
4691 {"substitute", 4, 4, f_substitute},
4692 {"synID", 3, 3, f_synID},
4693 {"synIDattr", 2, 3, f_synIDattr},
4694 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004695 {"system", 1, 2, f_system},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 {"tempname", 0, 0, f_tempname},
4697 {"tolower", 1, 1, f_tolower},
4698 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00004699 {"tr", 3, 3, f_tr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {"type", 1, 1, f_type},
4701 {"virtcol", 1, 1, f_virtcol},
4702 {"visualmode", 0, 1, f_visualmode},
4703 {"winbufnr", 1, 1, f_winbufnr},
4704 {"wincol", 0, 0, f_wincol},
4705 {"winheight", 1, 1, f_winheight},
4706 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004707 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {"winrestcmd", 0, 0, f_winrestcmd},
4709 {"winwidth", 1, 1, f_winwidth},
4710};
4711
4712#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4713
4714/*
4715 * Function given to ExpandGeneric() to obtain the list of internal
4716 * or user defined function names.
4717 */
4718 char_u *
4719get_function_name(xp, idx)
4720 expand_T *xp;
4721 int idx;
4722{
4723 static int intidx = -1;
4724 char_u *name;
4725
4726 if (idx == 0)
4727 intidx = -1;
4728 if (intidx < 0)
4729 {
4730 name = get_user_func_name(xp, idx);
4731 if (name != NULL)
4732 return name;
4733 }
4734 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
4735 {
4736 STRCPY(IObuff, functions[intidx].f_name);
4737 STRCAT(IObuff, "(");
4738 if (functions[intidx].f_max_argc == 0)
4739 STRCAT(IObuff, ")");
4740 return IObuff;
4741 }
4742
4743 return NULL;
4744}
4745
4746/*
4747 * Function given to ExpandGeneric() to obtain the list of internal or
4748 * user defined variable or function names.
4749 */
4750/*ARGSUSED*/
4751 char_u *
4752get_expr_name(xp, idx)
4753 expand_T *xp;
4754 int idx;
4755{
4756 static int intidx = -1;
4757 char_u *name;
4758
4759 if (idx == 0)
4760 intidx = -1;
4761 if (intidx < 0)
4762 {
4763 name = get_function_name(xp, idx);
4764 if (name != NULL)
4765 return name;
4766 }
4767 return get_user_var_name(xp, ++intidx);
4768}
4769
4770#endif /* FEAT_CMDL_COMPL */
4771
4772/*
4773 * Find internal function in table above.
4774 * Return index, or -1 if not found
4775 */
4776 static int
4777find_internal_func(name)
4778 char_u *name; /* name of the function */
4779{
4780 int first = 0;
4781 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
4782 int cmp;
4783 int x;
4784
4785 /*
4786 * Find the function name in the table. Binary search.
4787 */
4788 while (first <= last)
4789 {
4790 x = first + ((unsigned)(last - first) >> 1);
4791 cmp = STRCMP(name, functions[x].f_name);
4792 if (cmp < 0)
4793 last = x - 1;
4794 else if (cmp > 0)
4795 first = x + 1;
4796 else
4797 return x;
4798 }
4799 return -1;
4800}
4801
4802/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004803 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
4804 * name it contains, otherwise return "name".
4805 */
4806 static char_u *
4807deref_func_name(name, lenp)
4808 char_u *name;
4809 int *lenp;
4810{
4811 VAR v;
4812 int cc;
4813
4814 cc = name[*lenp];
4815 name[*lenp] = NUL;
4816 v = find_var(name, FALSE);
4817 name[*lenp] = cc;
4818 if (v != NULL && v->tv.v_type == VAR_FUNC)
4819 {
4820 if (v->tv.vval.v_string == NULL)
4821 {
4822 *lenp = 0;
4823 return (char_u *)""; /* just in case */
4824 }
4825 *lenp = STRLEN(v->tv.vval.v_string);
4826 return v->tv.vval.v_string;
4827 }
4828
4829 return name;
4830}
4831
4832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 * Allocate a variable for the result of a function.
4834 * Return OK or FAIL.
4835 */
4836 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004837get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 char_u *name; /* name of the function */
4839 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004840 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 char_u **arg; /* argument, pointing to the '(' */
4842 linenr_T firstline; /* first line of range */
4843 linenr_T lastline; /* last line of range */
4844 int *doesrange; /* return: function handled range */
4845 int evaluate;
4846{
4847 char_u *argp;
4848 int ret = OK;
4849#define MAX_FUNC_ARGS 20
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004850 typeval argvars[MAX_FUNC_ARGS]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 int argcount = 0; /* number of arguments found */
4852
4853 /*
4854 * Get the arguments.
4855 */
4856 argp = *arg;
4857 while (argcount < MAX_FUNC_ARGS)
4858 {
4859 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
4860 if (*argp == ')' || *argp == ',' || *argp == NUL)
4861 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
4863 {
4864 ret = FAIL;
4865 break;
4866 }
4867 ++argcount;
4868 if (*argp != ',')
4869 break;
4870 }
4871 if (*argp == ')')
4872 ++argp;
4873 else
4874 ret = FAIL;
4875
4876 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004877 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 firstline, lastline, doesrange, evaluate);
4879 else if (!aborting())
4880 EMSG2(_("E116: Invalid arguments for function %s"), name);
4881
4882 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004883 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884
4885 *arg = skipwhite(argp);
4886 return ret;
4887}
4888
4889
4890/*
4891 * Call a function with its resolved parameters
4892 * Return OK or FAIL.
4893 */
4894 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004895call_func(name, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 doesrange, evaluate)
4897 char_u *name; /* name of the function */
4898 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004899 typeval *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 int argcount; /* number of "argvars" */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004901 typeval *argvars; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 linenr_T firstline; /* first line of range */
4903 linenr_T lastline; /* last line of range */
4904 int *doesrange; /* return: function handled range */
4905 int evaluate;
4906{
4907 int ret = FAIL;
4908 static char *errors[] =
4909 {N_("E117: Unknown function: %s"),
4910 N_("E118: Too many arguments for function: %s"),
4911 N_("E119: Not enough arguments for function: %s"),
4912 N_("E120: Using <SID> not in a script context: %s"),
4913 };
4914#define ERROR_UNKNOWN 0
4915#define ERROR_TOOMANY 1
4916#define ERROR_TOOFEW 2
4917#define ERROR_SCRIPT 3
4918#define ERROR_NONE 4
4919#define ERROR_OTHER 5
4920 int error = ERROR_NONE;
4921 int i;
4922 int llen;
4923 ufunc_T *fp;
4924 int cc;
4925#define FLEN_FIXED 40
4926 char_u fname_buf[FLEN_FIXED + 1];
4927 char_u *fname;
4928
4929 /*
4930 * In a script change <SID>name() and s:name() to K_SNR 123_name().
4931 * Change <SNR>123_name() to K_SNR 123_name().
4932 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
4933 */
4934 cc = name[len];
4935 name[len] = NUL;
4936 llen = eval_fname_script(name);
4937 if (llen > 0)
4938 {
4939 fname_buf[0] = K_SPECIAL;
4940 fname_buf[1] = KS_EXTRA;
4941 fname_buf[2] = (int)KE_SNR;
4942 i = 3;
4943 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
4944 {
4945 if (current_SID <= 0)
4946 error = ERROR_SCRIPT;
4947 else
4948 {
4949 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
4950 i = (int)STRLEN(fname_buf);
4951 }
4952 }
4953 if (i + STRLEN(name + llen) < FLEN_FIXED)
4954 {
4955 STRCPY(fname_buf + i, name + llen);
4956 fname = fname_buf;
4957 }
4958 else
4959 {
4960 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
4961 if (fname == NULL)
4962 error = ERROR_OTHER;
4963 else
4964 {
4965 mch_memmove(fname, fname_buf, (size_t)i);
4966 STRCPY(fname + i, name + llen);
4967 }
4968 }
4969 }
4970 else
4971 fname = name;
4972
4973 *doesrange = FALSE;
4974
4975
4976 /* execute the function if no errors detected and executing */
4977 if (evaluate && error == ERROR_NONE)
4978 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004979 rettv->v_type = VAR_NUMBER; /* default is number rettv */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 error = ERROR_UNKNOWN;
4981
4982 if (!ASCII_ISLOWER(fname[0]))
4983 {
4984 /*
4985 * User defined function.
4986 */
4987 fp = find_func(fname);
4988#ifdef FEAT_AUTOCMD
4989 if (fp == NULL && apply_autocmds(EVENT_FUNCUNDEFINED,
4990 fname, fname, TRUE, NULL)
4991#ifdef FEAT_EVAL
4992 && !aborting()
4993#endif
4994 )
4995 {
4996 /* executed an autocommand, search for function again */
4997 fp = find_func(fname);
4998 }
4999#endif
5000 if (fp != NULL)
5001 {
5002 if (fp->flags & FC_RANGE)
5003 *doesrange = TRUE;
5004 if (argcount < fp->args.ga_len)
5005 error = ERROR_TOOFEW;
5006 else if (!fp->varargs && argcount > fp->args.ga_len)
5007 error = ERROR_TOOMANY;
5008 else
5009 {
5010 /*
5011 * Call the user function.
5012 * Save and restore search patterns, script variables and
5013 * redo buffer.
5014 */
5015 save_search_patterns();
5016 saveRedobuff();
5017 ++fp->calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005018 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 firstline, lastline);
5020 --fp->calls;
5021 restoreRedobuff();
5022 restore_search_patterns();
5023 error = ERROR_NONE;
5024 }
5025 }
5026 }
5027 else
5028 {
5029 /*
5030 * Find the function name in the table, call its implementation.
5031 */
5032 i = find_internal_func(fname);
5033 if (i >= 0)
5034 {
5035 if (argcount < functions[i].f_min_argc)
5036 error = ERROR_TOOFEW;
5037 else if (argcount > functions[i].f_max_argc)
5038 error = ERROR_TOOMANY;
5039 else
5040 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005041 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005042 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 error = ERROR_NONE;
5044 }
5045 }
5046 }
5047 /*
5048 * The function call (or "FuncUndefined" autocommand sequence) might
5049 * have been aborted by an error, an interrupt, or an explicitly thrown
5050 * exception that has not been caught so far. This situation can be
5051 * tested for by calling aborting(). For an error in an internal
5052 * function or for the "E132" error in call_user_func(), however, the
5053 * throw point at which the "force_abort" flag (temporarily reset by
5054 * emsg()) is normally updated has not been reached yet. We need to
5055 * update that flag first to make aborting() reliable.
5056 */
5057 update_force_abort();
5058 }
5059 if (error == ERROR_NONE)
5060 ret = OK;
5061
5062 /*
5063 * Report an error unless the argument evaluation or function call has been
5064 * cancelled due to an aborting error, an interrupt, or an exception.
5065 */
5066 if (error < ERROR_NONE && !aborting())
5067 EMSG2((char_u *)_(errors[error]), name);
5068
5069 name[len] = cc;
5070 if (fname != name && fname != fname_buf)
5071 vim_free(fname);
5072
5073 return ret;
5074}
5075
5076/*********************************************
5077 * Implementation of the built-in functions
5078 */
5079
5080/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00005081 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 */
5083 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00005084f_add(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005085 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005086 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005088 listvar *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005091 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005093 l = argvars[0].vval.v_list;
5094 if (l != NULL && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005095 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005096 }
5097 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00005098 EMSG(_(e_listreq));
5099}
5100
5101/*
5102 * "append(lnum, string/list)" function
5103 */
5104 static void
5105f_append(argvars, rettv)
5106 typeval *argvars;
5107 typeval *rettv;
5108{
5109 long lnum;
5110 listvar *l = NULL;
5111 listitem *li = NULL;
5112 typeval *tv;
5113 long added = 0;
5114
5115 rettv->vval.v_number = 1; /* Default: Failed */
5116 lnum = get_tv_lnum(argvars);
5117 if (lnum >= 0
5118 && lnum <= curbuf->b_ml.ml_line_count
5119 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005120 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00005121 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005122 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00005123 l = argvars[1].vval.v_list;
5124 if (l == NULL)
5125 return;
5126 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005127 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00005128 for (;;)
5129 {
5130 if (l == NULL)
5131 tv = &argvars[1]; /* append a string */
5132 else if (li == NULL)
5133 break; /* end of list */
5134 else
5135 tv = &li->li_tv; /* append item from list */
5136 ml_append(lnum + added, get_tv_string(tv), (colnr_T)0, FALSE);
5137 ++added;
5138 if (l == NULL)
5139 break;
5140 li = li->li_next;
5141 }
5142
5143 appended_lines_mark(lnum, added);
5144 if (curwin->w_cursor.lnum > lnum)
5145 curwin->w_cursor.lnum += added;
5146 rettv->vval.v_number = 0; /* Success */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 }
5148}
5149
5150/*
5151 * "argc()" function
5152 */
5153/* ARGSUSED */
5154 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005155f_argc(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005156 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005157 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005159 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160}
5161
5162/*
5163 * "argidx()" function
5164 */
5165/* ARGSUSED */
5166 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005167f_argidx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005168 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005169 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005171 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172}
5173
5174/*
5175 * "argv(nr)" function
5176 */
5177 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005178f_argv(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005179 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005180 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181{
5182 int idx;
5183
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005184 idx = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 if (idx >= 0 && idx < ARGCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005186 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005188 rettv->vval.v_string = NULL;
5189 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190}
5191
5192/*
5193 * "browse(save, title, initdir, default)" function
5194 */
5195/* ARGSUSED */
5196 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005197f_browse(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005198 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005199 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200{
5201#ifdef FEAT_BROWSE
5202 int save;
5203 char_u *title;
5204 char_u *initdir;
5205 char_u *defname;
5206 char_u buf[NUMBUFLEN];
5207 char_u buf2[NUMBUFLEN];
5208
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005209 save = get_tv_number(&argvars[0]);
5210 title = get_tv_string(&argvars[1]);
5211 initdir = get_tv_string_buf(&argvars[2], buf);
5212 defname = get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005214 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005215 do_browse(save ? BROWSE_SAVE : 0,
5216 title, defname, NULL, initdir, NULL, curbuf);
5217#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005218 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005219#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005220 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005221}
5222
5223/*
5224 * "browsedir(title, initdir)" function
5225 */
5226/* ARGSUSED */
5227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005228f_browsedir(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005229 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005230 typeval *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005231{
5232#ifdef FEAT_BROWSE
5233 char_u *title;
5234 char_u *initdir;
5235 char_u buf[NUMBUFLEN];
5236
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005237 title = get_tv_string(&argvars[0]);
5238 initdir = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005239
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005240 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00005241 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005243 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005245 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246}
5247
Bram Moolenaar0d660222005-01-07 21:51:51 +00005248static buf_T *find_buffer __ARGS((typeval *avar));
5249
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250/*
5251 * Find a buffer by number or exact name.
5252 */
5253 static buf_T *
5254find_buffer(avar)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005255 typeval *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256{
5257 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005259 if (avar->v_type == VAR_NUMBER)
5260 buf = buflist_findnr((int)avar->vval.v_number);
5261 else if (avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005264 if (buf == NULL)
5265 {
5266 /* No full path name match, try a match with a URL or a "nofile"
5267 * buffer, these don't use the full path. */
5268 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5269 if (buf->b_fname != NULL
5270 && (path_with_url(buf->b_fname)
5271#ifdef FEAT_QUICKFIX
5272 || bt_nofile(buf)
5273#endif
5274 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005276 break;
5277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 }
5279 return buf;
5280}
5281
5282/*
5283 * "bufexists(expr)" function
5284 */
5285 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005286f_bufexists(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005287 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005288 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005290 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291}
5292
5293/*
5294 * "buflisted(expr)" function
5295 */
5296 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005297f_buflisted(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005299 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300{
5301 buf_T *buf;
5302
5303 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005304 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305}
5306
5307/*
5308 * "bufloaded(expr)" function
5309 */
5310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005311f_bufloaded(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005312 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005313 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314{
5315 buf_T *buf;
5316
5317 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005318 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319}
5320
Bram Moolenaar0d660222005-01-07 21:51:51 +00005321static buf_T *get_buf_tv __ARGS((typeval *tv));
5322
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323/*
5324 * Get buffer by number or pattern.
5325 */
5326 static buf_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005327get_buf_tv(tv)
5328 typeval *tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005330 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 int save_magic;
5332 char_u *save_cpo;
5333 buf_T *buf;
5334
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005335 if (tv->v_type == VAR_NUMBER)
5336 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 if (name == NULL || *name == NUL)
5338 return curbuf;
5339 if (name[0] == '$' && name[1] == NUL)
5340 return lastbuf;
5341
5342 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
5343 save_magic = p_magic;
5344 p_magic = TRUE;
5345 save_cpo = p_cpo;
5346 p_cpo = (char_u *)"";
5347
5348 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
5349 TRUE, FALSE));
5350
5351 p_magic = save_magic;
5352 p_cpo = save_cpo;
5353
5354 /* If not found, try expanding the name, like done for bufexists(). */
5355 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005356 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357
5358 return buf;
5359}
5360
5361/*
5362 * "bufname(expr)" function
5363 */
5364 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005365f_bufname(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 buf_T *buf;
5370
5371 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005372 buf = get_buf_tv(&argvars[0]);
5373 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005375 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005377 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 --emsg_off;
5379}
5380
5381/*
5382 * "bufnr(expr)" function
5383 */
5384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005385f_bufnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005387 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388{
5389 buf_T *buf;
5390
5391 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005392 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005394 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005396 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 --emsg_off;
5398}
5399
5400/*
5401 * "bufwinnr(nr)" function
5402 */
5403 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005404f_bufwinnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005406 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407{
5408#ifdef FEAT_WINDOWS
5409 win_T *wp;
5410 int winnr = 0;
5411#endif
5412 buf_T *buf;
5413
5414 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 buf = get_buf_tv(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416#ifdef FEAT_WINDOWS
5417 for (wp = firstwin; wp; wp = wp->w_next)
5418 {
5419 ++winnr;
5420 if (wp->w_buffer == buf)
5421 break;
5422 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005423 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005425 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426#endif
5427 --emsg_off;
5428}
5429
5430/*
5431 * "byte2line(byte)" function
5432 */
5433/*ARGSUSED*/
5434 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005435f_byte2line(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005436 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005437 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438{
5439#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005440 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441#else
5442 long boff = 0;
5443
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005444 boff = get_tv_number(&argvars[0]) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005446 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005448 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 (linenr_T)0, &boff);
5450#endif
5451}
5452
5453/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005454 * "byteidx()" function
5455 */
5456/*ARGSUSED*/
5457 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005458f_byteidx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005460 typeval *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005461{
5462#ifdef FEAT_MBYTE
5463 char_u *t;
5464#endif
5465 char_u *str;
5466 long idx;
5467
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005468 str = get_tv_string(&argvars[0]);
5469 idx = get_tv_number(&argvars[1]);
5470 rettv->vval.v_number = -1;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005471 if (idx < 0)
5472 return;
5473
5474#ifdef FEAT_MBYTE
5475 t = str;
5476 for ( ; idx > 0; idx--)
5477 {
5478 if (*t == NUL) /* EOL reached */
5479 return;
5480 t += mb_ptr2len_check(t);
5481 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005482 rettv->vval.v_number = t - str;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005483#else
5484 if (idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005485 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00005486#endif
5487}
5488
5489/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005490 * "call(func, arglist)" function
5491 */
5492 static void
5493f_call(argvars, rettv)
5494 typeval *argvars;
5495 typeval *rettv;
5496{
5497 char_u *func;
5498 typeval argv[MAX_FUNC_ARGS];
5499 int argc = 0;
5500 listitem *item;
5501 int dummy;
5502
5503 rettv->vval.v_number = 0;
5504 if (argvars[1].v_type != VAR_LIST)
5505 {
5506 EMSG(_(e_listreq));
5507 return;
5508 }
5509 if (argvars[1].vval.v_list == NULL)
5510 return;
5511
5512 if (argvars[0].v_type == VAR_FUNC)
5513 func = argvars[0].vval.v_string;
5514 else
5515 func = get_tv_string(&argvars[0]);
5516
5517 for (item = argvars[1].vval.v_list->lv_first; item != NULL;
5518 item = item->li_next)
5519 {
5520 if (argc == MAX_FUNC_ARGS)
5521 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00005522 EMSG(_("E699: Too many arguments"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005523 break;
5524 }
5525 /* Make a copy of each argument (is this really needed?) */
5526 copy_tv(&item->li_tv, &argv[argc++]);
5527 }
5528
5529 if (item == NULL)
5530 (void)call_func(func, STRLEN(func), rettv, argc, argv,
5531 curwin->w_cursor.lnum, curwin->w_cursor.lnum, &dummy, TRUE);
5532
5533 /* Free the arguments. */
5534 while (argc > 0)
5535 clear_tv(&argv[--argc]);
5536}
5537
5538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 * "char2nr(string)" function
5540 */
5541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005542f_char2nr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005543 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005544 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545{
5546#ifdef FEAT_MBYTE
5547 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005548 rettv->vval.v_number =
5549 (*mb_ptr2char)(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 else
5551#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553}
5554
5555/*
5556 * "cindent(lnum)" function
5557 */
5558 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005559f_cindent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005560 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005561 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562{
5563#ifdef FEAT_CINDENT
5564 pos_T pos;
5565 linenr_T lnum;
5566
5567 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
5570 {
5571 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005572 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 curwin->w_cursor = pos;
5574 }
5575 else
5576#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578}
5579
5580/*
5581 * "col(string)" function
5582 */
5583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584f_col(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005585 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005586 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587{
5588 colnr_T col = 0;
5589 pos_T *fp;
5590
5591 fp = var2fpos(&argvars[0], FALSE);
5592 if (fp != NULL)
5593 {
5594 if (fp->col == MAXCOL)
5595 {
5596 /* '> can be MAXCOL, get the length of the line then */
5597 if (fp->lnum <= curbuf->b_ml.ml_line_count)
5598 col = STRLEN(ml_get(fp->lnum)) + 1;
5599 else
5600 col = MAXCOL;
5601 }
5602 else
5603 {
5604 col = fp->col + 1;
5605#ifdef FEAT_VIRTUALEDIT
5606 /* col(".") when the cursor is on the NUL at the end of the line
5607 * because of "coladd" can be seen as an extra column. */
5608 if (virtual_active() && fp == &curwin->w_cursor)
5609 {
5610 char_u *p = ml_get_cursor();
5611
5612 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
5613 curwin->w_virtcol - curwin->w_cursor.coladd))
5614 {
5615# ifdef FEAT_MBYTE
5616 int l;
5617
5618 if (*p != NUL && p[(l = (*mb_ptr2len_check)(p))] == NUL)
5619 col += l;
5620# else
5621 if (*p != NUL && p[1] == NUL)
5622 ++col;
5623# endif
5624 }
5625 }
5626#endif
5627 }
5628 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005629 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630}
5631
5632/*
5633 * "confirm(message, buttons[, default [, type]])" function
5634 */
5635/*ARGSUSED*/
5636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005637f_confirm(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005638 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005639 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640{
5641#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5642 char_u *message;
5643 char_u *buttons = NULL;
5644 char_u buf[NUMBUFLEN];
5645 char_u buf2[NUMBUFLEN];
5646 int def = 1;
5647 int type = VIM_GENERIC;
5648 int c;
5649
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005650 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005651 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005653 buttons = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005654 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005656 def = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005657 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 {
5659 /* avoid that TOUPPER_ASC calls get_var_string_buf() twice */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005660 c = *get_tv_string_buf(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 switch (TOUPPER_ASC(c))
5662 {
5663 case 'E': type = VIM_ERROR; break;
5664 case 'Q': type = VIM_QUESTION; break;
5665 case 'I': type = VIM_INFO; break;
5666 case 'W': type = VIM_WARNING; break;
5667 case 'G': type = VIM_GENERIC; break;
5668 }
5669 }
5670 }
5671 }
5672
5673 if (buttons == NULL || *buttons == NUL)
5674 buttons = (char_u *)_("&Ok");
5675
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005676 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 def, NULL);
5678#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005679 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680#endif
5681}
5682
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005683/*
5684 * "copy()" function
5685 */
5686 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005687f_copy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005688 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005689 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005690{
5691 if (argvars[0].v_type == VAR_LIST)
5692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005693 rettv->v_type = VAR_LIST;
5694 rettv->vval.v_list = list_copy(argvars[0].vval.v_list, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005695 }
5696 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005697 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005698}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699
5700/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005701 * "count()" function
5702 */
5703 static void
5704f_count(argvars, rettv)
5705 typeval *argvars;
5706 typeval *rettv;
5707{
5708 listitem *li;
5709 long n = 0;
5710 int ic = FALSE;
5711
5712 if (argvars[0].v_type != VAR_LIST)
5713 EMSG(_(e_listreq));
5714 else if (argvars[0].vval.v_list != NULL)
5715 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005716 li = argvars[0].vval.v_list->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005717 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005718 {
5719 for (n = get_tv_number(&argvars[2]); n > 0 && li != NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005720 li = li->li_next)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005721 --n;
5722 if (argvars[3].v_type != VAR_UNKNOWN)
5723 ic = get_tv_number(&argvars[3]);
5724 }
5725
5726 n = 0;
5727 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005728 if (tv_equal(&li->li_tv, &argvars[1], ic))
5729 ++n;
5730 }
5731 rettv->vval.v_number = n;
5732}
5733
5734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
5736 *
5737 * Checks the existence of a cscope connection.
5738 */
5739/*ARGSUSED*/
5740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005741f_cscope_connection(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005742 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005743 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744{
5745#ifdef FEAT_CSCOPE
5746 int num = 0;
5747 char_u *dbpath = NULL;
5748 char_u *prepend = NULL;
5749 char_u buf[NUMBUFLEN];
5750
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005751 if (argvars[0].v_type != VAR_UNKNOWN
5752 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005754 num = (int)get_tv_number(&argvars[0]);
5755 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005756 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005757 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 }
5759
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005760 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005762 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763#endif
5764}
5765
5766/*
5767 * "cursor(lnum, col)" function
5768 *
5769 * Moves the cursor to the specified line and column
5770 */
5771/*ARGSUSED*/
5772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005773f_cursor(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005774 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005775 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776{
5777 long line, col;
5778
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005779 line = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 if (line > 0)
5781 curwin->w_cursor.lnum = line;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005782 col = get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 if (col > 0)
5784 curwin->w_cursor.col = col - 1;
5785#ifdef FEAT_VIRTUALEDIT
5786 curwin->w_cursor.coladd = 0;
5787#endif
5788
5789 /* Make sure the cursor is in a valid position. */
5790 check_cursor();
5791#ifdef FEAT_MBYTE
5792 /* Correct cursor for multi-byte character. */
5793 if (has_mbyte)
5794 mb_adjust_cursor();
5795#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005796
5797 curwin->w_set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798}
5799
5800/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005801 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 */
5803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005804f_deepcopy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005805 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005806 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005808 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005810 rettv->v_type = VAR_LIST;
5811 rettv->vval.v_list = list_copy(argvars[0].vval.v_list, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005813 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005814 copy_tv(&argvars[0], rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815}
5816
5817/*
5818 * "delete()" function
5819 */
5820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005821f_delete(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005822 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005823 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824{
5825 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005826 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005828 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829}
5830
5831/*
5832 * "did_filetype()" function
5833 */
5834/*ARGSUSED*/
5835 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005836f_did_filetype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005837 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005838 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839{
5840#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005841 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005843 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844#endif
5845}
5846
5847/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00005848 * "diff_filler()" function
5849 */
5850/*ARGSUSED*/
5851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005852f_diff_filler(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005853 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005854 typeval *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00005855{
5856#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005857 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00005858#endif
5859}
5860
5861/*
5862 * "diff_hlID()" function
5863 */
5864/*ARGSUSED*/
5865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005866f_diff_hlID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005867 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005868 typeval *rettv;
Bram Moolenaar47136d72004-10-12 20:02:24 +00005869{
5870#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005871 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00005872 static linenr_T prev_lnum = 0;
5873 static int changedtick = 0;
5874 static int fnum = 0;
5875 static int change_start = 0;
5876 static int change_end = 0;
5877 static enum hlf_value hlID = 0;
5878 int filler_lines;
5879 int col;
5880
5881 if (lnum != prev_lnum
5882 || changedtick != curbuf->b_changedtick
5883 || fnum != curbuf->b_fnum)
5884 {
5885 /* New line, buffer, change: need to get the values. */
5886 filler_lines = diff_check(curwin, lnum);
5887 if (filler_lines < 0)
5888 {
5889 if (filler_lines == -1)
5890 {
5891 change_start = MAXCOL;
5892 change_end = -1;
5893 if (diff_find_change(curwin, lnum, &change_start, &change_end))
5894 hlID = HLF_ADD; /* added line */
5895 else
5896 hlID = HLF_CHD; /* changed line */
5897 }
5898 else
5899 hlID = HLF_ADD; /* added line */
5900 }
5901 else
5902 hlID = (enum hlf_value)0;
5903 prev_lnum = lnum;
5904 changedtick = curbuf->b_changedtick;
5905 fnum = curbuf->b_fnum;
5906 }
5907
5908 if (hlID == HLF_CHD || hlID == HLF_TXD)
5909 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005910 col = get_tv_number(&argvars[1]) - 1;
Bram Moolenaar47136d72004-10-12 20:02:24 +00005911 if (col >= change_start && col <= change_end)
5912 hlID = HLF_TXD; /* changed text */
5913 else
5914 hlID = HLF_CHD; /* changed line */
5915 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005916 rettv->vval.v_number = hlID == (enum hlf_value)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00005917#endif
5918}
5919
5920/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00005921 * "empty({expr})" function
5922 */
5923 static void
5924f_empty(argvars, rettv)
5925 typeval *argvars;
5926 typeval *rettv;
5927{
5928 int n;
5929
5930 switch (argvars[0].v_type)
5931 {
5932 case VAR_STRING:
5933 case VAR_FUNC:
5934 n = argvars[0].vval.v_string == NULL
5935 || *argvars[0].vval.v_string == NUL;
5936 break;
5937 case VAR_NUMBER:
5938 n = argvars[0].vval.v_number == 0;
5939 break;
5940 case VAR_LIST:
5941 n = argvars[0].vval.v_list == NULL
5942 || argvars[0].vval.v_list->lv_first == NULL;
5943 break;
5944 default:
5945 EMSG2(_(e_intern2), "f_empty()");
5946 n = 0;
5947 }
5948
5949 rettv->vval.v_number = n;
5950}
5951
5952/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 * "escape({string}, {chars})" function
5954 */
5955 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005956f_escape(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005958 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959{
5960 char_u buf[NUMBUFLEN];
5961
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005962 rettv->vval.v_string =
5963 vim_strsave_escaped(get_tv_string(&argvars[0]),
5964 get_tv_string_buf(&argvars[1], buf));
5965 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966}
5967
5968/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005969 * "eval()" function
5970 */
5971/*ARGSUSED*/
5972 static void
5973f_eval(argvars, rettv)
5974 typeval *argvars;
5975 typeval *rettv;
5976{
5977 char_u *s;
5978
5979 s = get_tv_string(&argvars[0]);
5980 s = skipwhite(s);
5981
5982 if (eval1(&s, rettv, TRUE) == FAIL)
5983 rettv->vval.v_number = 0;
5984 else if (*s != NUL)
5985 EMSG(_(e_trailing));
5986}
5987
5988/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 * "eventhandler()" function
5990 */
5991/*ARGSUSED*/
5992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005993f_eventhandler(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005995 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005997 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998}
5999
6000/*
6001 * "executable()" function
6002 */
6003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006004f_executable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006005 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006006 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006008 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009}
6010
6011/*
6012 * "exists()" function
6013 */
6014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006015f_exists(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006017 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018{
6019 char_u *p;
6020 char_u *name;
6021 int n = FALSE;
6022 int len = 0;
6023
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006024 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 if (*p == '$') /* environment variable */
6026 {
6027 /* first try "normal" environment variables (fast) */
6028 if (mch_getenv(p + 1) != NULL)
6029 n = TRUE;
6030 else
6031 {
6032 /* try expanding things like $VIM and ${HOME} */
6033 p = expand_env_save(p);
6034 if (p != NULL && *p != '$')
6035 n = TRUE;
6036 vim_free(p);
6037 }
6038 }
6039 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006040 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 else if (*p == '*') /* internal or user defined function */
6042 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006043 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 }
6045 else if (*p == ':')
6046 {
6047 n = cmd_exists(p + 1);
6048 }
6049 else if (*p == '#')
6050 {
6051#ifdef FEAT_AUTOCMD
6052 name = p + 1;
6053 p = vim_strchr(name, '#');
6054 if (p != NULL)
6055 n = au_exists(name, p, p + 1);
6056 else
6057 n = au_exists(name, name + STRLEN(name), NULL);
6058#endif
6059 }
6060 else /* internal variable */
6061 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 char_u *expr_start;
6063 char_u *expr_end;
6064 char_u *temp_string = NULL;
6065 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 name = p;
6067
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 /* Find the end of the name. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006069 s = find_name_end(name, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 if (expr_start != NULL)
6071 {
6072 temp_string = make_expanded_name(name, expr_start, expr_end, s);
6073 if (temp_string != NULL)
6074 {
6075 len = STRLEN(temp_string);
6076 name = temp_string;
6077 }
6078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 if (len == 0)
6080 len = get_id_len(&p);
6081 if (len != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006082 n = (get_var_tv(name, len, NULL) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 vim_free(temp_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 }
6086
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006087 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088}
6089
6090/*
6091 * "expand()" function
6092 */
6093 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006094f_expand(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006095 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006096 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097{
6098 char_u *s;
6099 int len;
6100 char_u *errormsg;
6101 int flags = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
6102 expand_T xpc;
6103
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006104 rettv->v_type = VAR_STRING;
6105 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 if (*s == '%' || *s == '#' || *s == '<')
6107 {
6108 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006109 rettv->vval.v_string = eval_vars(s, &len, NULL, &errormsg, s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 --emsg_off;
6111 }
6112 else
6113 {
6114 /* When the optional second argument is non-zero, don't remove matches
6115 * for 'suffixes' and 'wildignore' */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006116 if (argvars[1].v_type != VAR_UNKNOWN && get_tv_number(&argvars[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 flags |= WILD_KEEP_ALL;
6118 ExpandInit(&xpc);
6119 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006120 rettv->vval.v_string = ExpandOne(&xpc, s, NULL, flags, WILD_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 ExpandCleanup(&xpc);
6122 }
6123}
6124
6125/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006126 * "extend(list, list [, idx])" function
6127 */
6128 static void
6129f_extend(argvars, rettv)
6130 typeval *argvars;
6131 typeval *rettv;
6132{
6133 long before;
6134 long n;
6135 listitem *item;
6136 listvar *l1, *l2;
6137
6138 rettv->vval.v_number = 0;
6139 if (argvars[0].v_type != VAR_LIST || argvars[1].v_type != VAR_LIST)
6140 {
6141 EMSG(_(e_listreq));
6142 return;
6143 }
6144 l1 = argvars[0].vval.v_list;
6145 l2 = argvars[1].vval.v_list;
6146 if (l1 != NULL && l2 != NULL)
6147 {
6148 if (argvars[2].v_type != VAR_UNKNOWN)
6149 {
6150 n = before = get_tv_number(&argvars[2]);
6151 item = list_find_ext(l1, &n);
6152 if (n != 0)
6153 {
6154 EMSGN(_(e_listidx), before);
6155 return;
6156 }
6157 }
6158 else
6159 item = NULL;
6160 list_extend(l1, l2, item);
6161
6162 ++l1->lv_refcount;
6163 copy_tv(&argvars[0], rettv);
6164 }
6165}
6166
6167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 * "filereadable()" function
6169 */
6170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006171f_filereadable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006172 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006173 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174{
6175 FILE *fd;
6176 char_u *p;
6177 int n;
6178
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006179 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 if (*p && !mch_isdir(p) && (fd = mch_fopen((char *)p, "r")) != NULL)
6181 {
6182 n = TRUE;
6183 fclose(fd);
6184 }
6185 else
6186 n = FALSE;
6187
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006188 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189}
6190
6191/*
6192 * return 0 for not writable, 1 for writable file, 2 for a dir which we have
6193 * rights to write into.
6194 */
6195 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006196f_filewritable(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006197 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006198 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199{
6200 char_u *p;
6201 int retval = 0;
6202#if defined(UNIX) || defined(VMS)
6203 int perm = 0;
6204#endif
6205
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006206 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207#if defined(UNIX) || defined(VMS)
6208 perm = mch_getperm(p);
6209#endif
6210#ifndef MACOS_CLASSIC /* TODO: get either mch_writable or mch_access */
6211 if (
6212# ifdef WIN3264
6213 mch_writable(p) &&
6214# else
6215# if defined(UNIX) || defined(VMS)
6216 (perm & 0222) &&
6217# endif
6218# endif
6219 mch_access((char *)p, W_OK) == 0
6220 )
6221#endif
6222 {
6223 ++retval;
6224 if (mch_isdir(p))
6225 ++retval;
6226 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006227 rettv->vval.v_number = retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228}
6229
Bram Moolenaar0d660222005-01-07 21:51:51 +00006230static void findfilendir __ARGS((typeval *argvars, typeval *rettv, int dir));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006231
6232 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00006233findfilendir(argvars, rettv, dir)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006234 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006235 typeval *rettv;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006236 int dir;
6237{
6238#ifdef FEAT_SEARCHPATH
6239 char_u *fname;
6240 char_u *fresult = NULL;
6241 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
6242 char_u *p;
6243 char_u pathbuf[NUMBUFLEN];
6244 int count = 1;
6245 int first = TRUE;
6246
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006247 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006248
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006249 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006250 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006251 p = get_tv_string_buf(&argvars[1], pathbuf);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006252 if (*p != NUL)
6253 path = p;
6254
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006255 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006256 count = get_tv_number(&argvars[2]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006257 }
6258
6259 do
6260 {
6261 vim_free(fresult);
6262 fresult = find_file_in_path_option(first ? fname : NULL,
6263 first ? (int)STRLEN(fname) : 0,
6264 0, first, path, dir, NULL);
6265 first = FALSE;
6266 } while (--count > 0 && fresult != NULL);
6267
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006268 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006269#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006270 rettv->vval.v_string = NULL;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006271#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006272 rettv->v_type = VAR_STRING;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006273}
6274
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006275static void filter_map __ARGS((typeval *argvars, typeval *rettv, int map));
6276
6277/*
6278 * Implementation of map() and filter().
6279 */
6280 static void
6281filter_map(argvars, rettv, map)
6282 typeval *argvars;
6283 typeval *rettv;
6284 int map;
6285{
6286 char_u buf[NUMBUFLEN];
6287 char_u *expr, *s;
6288 listitem *li, *nli;
6289 listvar *l;
6290
6291 rettv->vval.v_number = 0;
6292 if (argvars[0].v_type != VAR_LIST)
6293 EMSG(_(e_listreq));
6294 else if ((l = argvars[0].vval.v_list) != NULL)
6295 {
6296 expr = skipwhite(get_tv_string_buf(&argvars[1], buf));
6297 for (li = l->lv_first; li != NULL; li = nli)
6298 {
6299 copy_tv(&li->li_tv, &amp_tv);
6300 s = expr;
6301 if (eval1(&s, rettv, TRUE) == FAIL)
6302 break;
6303 if (*s != NUL) /* check for trailing chars after expr */
6304 {
6305 EMSG2(_(e_invexpr2), s);
6306 break;
6307 }
6308 nli = li->li_next;
6309 if (map)
6310 {
6311 /* map(): replace the list item value */
6312 clear_tv(&li->li_tv);
6313 li->li_tv = *rettv;
6314 }
6315 else
6316 {
6317 /* filter(): when expr is zero remove the item */
6318 if (get_tv_number(rettv) == 0)
6319 {
6320 list_getrem(l, li, li);
6321 clear_tv(&li->li_tv);
6322 }
6323 clear_tv(rettv);
6324 }
6325 clear_tv(&amp_tv);
6326 }
6327
6328 clear_tv(&amp_tv);
6329 amp_tv.v_type = VAR_UNKNOWN;
6330
6331 copy_tv(&argvars[0], rettv);
6332 }
6333}
6334
6335/*
6336 * "filter()" function
6337 */
6338 static void
6339f_filter(argvars, rettv)
6340 typeval *argvars;
6341 typeval *rettv;
6342{
6343 filter_map(argvars, rettv, FALSE);
6344}
6345
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00006346/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006347 * "finddir({fname}[, {path}[, {count}]])" function
6348 */
6349 static void
6350f_finddir(argvars, rettv)
6351 typeval *argvars;
6352 typeval *rettv;
6353{
6354 findfilendir(argvars, rettv, TRUE);
6355}
6356
6357/*
6358 * "findfile({fname}[, {path}[, {count}]])" function
6359 */
6360 static void
6361f_findfile(argvars, rettv)
6362 typeval *argvars;
6363 typeval *rettv;
6364{
6365 findfilendir(argvars, rettv, FALSE);
6366}
6367
6368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 * "fnamemodify({fname}, {mods})" function
6370 */
6371 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006372f_fnamemodify(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006373 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006374 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375{
6376 char_u *fname;
6377 char_u *mods;
6378 int usedlen = 0;
6379 int len;
6380 char_u *fbuf = NULL;
6381 char_u buf[NUMBUFLEN];
6382
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006383 fname = get_tv_string(&argvars[0]);
6384 mods = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 len = (int)STRLEN(fname);
6386
6387 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
6388
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006389 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006391 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006393 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 vim_free(fbuf);
6395}
6396
Bram Moolenaar0d660222005-01-07 21:51:51 +00006397static void foldclosed_both __ARGS((typeval *argvars, typeval *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398
6399/*
6400 * "foldclosed()" function
6401 */
6402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006403foldclosed_both(argvars, rettv, end)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006404 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006405 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 int end;
6407{
6408#ifdef FEAT_FOLDING
6409 linenr_T lnum;
6410 linenr_T first, last;
6411
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006412 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
6414 {
6415 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
6416 {
6417 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006418 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006420 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 return;
6422 }
6423 }
6424#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006425 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426}
6427
6428/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006429 * "foldclosed()" function
6430 */
6431 static void
6432f_foldclosed(argvars, rettv)
6433 typeval *argvars;
6434 typeval *rettv;
6435{
6436 foldclosed_both(argvars, rettv, FALSE);
6437}
6438
6439/*
6440 * "foldclosedend()" function
6441 */
6442 static void
6443f_foldclosedend(argvars, rettv)
6444 typeval *argvars;
6445 typeval *rettv;
6446{
6447 foldclosed_both(argvars, rettv, TRUE);
6448}
6449
6450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 * "foldlevel()" function
6452 */
6453 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006454f_foldlevel(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006455 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006456 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457{
6458#ifdef FEAT_FOLDING
6459 linenr_T lnum;
6460
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006461 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006463 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 else
6465#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006466 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467}
6468
6469/*
6470 * "foldtext()" function
6471 */
6472/*ARGSUSED*/
6473 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006474f_foldtext(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006475 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006476 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477{
6478#ifdef FEAT_FOLDING
6479 linenr_T lnum;
6480 char_u *s;
6481 char_u *r;
6482 int len;
6483 char *txt;
6484#endif
6485
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006486 rettv->v_type = VAR_STRING;
6487 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488#ifdef FEAT_FOLDING
6489 if ((linenr_T)vimvars[VV_FOLDSTART].val > 0
6490 && (linenr_T)vimvars[VV_FOLDEND].val <= curbuf->b_ml.ml_line_count
6491 && vimvars[VV_FOLDDASHES].val != NULL)
6492 {
6493 /* Find first non-empty line in the fold. */
6494 lnum = (linenr_T)vimvars[VV_FOLDSTART].val;
6495 while (lnum < (linenr_T)vimvars[VV_FOLDEND].val)
6496 {
6497 if (!linewhite(lnum))
6498 break;
6499 ++lnum;
6500 }
6501
6502 /* Find interesting text in this line. */
6503 s = skipwhite(ml_get(lnum));
6504 /* skip C comment-start */
6505 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00006506 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00006508 if (*skipwhite(s) == NUL
6509 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].val)
6510 {
6511 s = skipwhite(ml_get(lnum + 1));
6512 if (*s == '*')
6513 s = skipwhite(s + 1);
6514 }
6515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 txt = _("+-%s%3ld lines: ");
6517 r = alloc((unsigned)(STRLEN(txt)
6518 + STRLEN(vimvars[VV_FOLDDASHES].val) /* for %s */
6519 + 20 /* for %3ld */
6520 + STRLEN(s))); /* concatenated */
6521 if (r != NULL)
6522 {
6523 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].val,
6524 (long)((linenr_T)vimvars[VV_FOLDEND].val
6525 - (linenr_T)vimvars[VV_FOLDSTART].val + 1));
6526 len = (int)STRLEN(r);
6527 STRCAT(r, s);
6528 /* remove 'foldmarker' and 'commentstring' */
6529 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006530 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 }
6532 }
6533#endif
6534}
6535
6536/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006537 * "foldtextresult(lnum)" function
6538 */
6539/*ARGSUSED*/
6540 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006541f_foldtextresult(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006542 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006543 typeval *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006544{
6545#ifdef FEAT_FOLDING
6546 linenr_T lnum;
6547 char_u *text;
6548 char_u buf[51];
6549 foldinfo_T foldinfo;
6550 int fold_count;
6551#endif
6552
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006553 rettv->v_type = VAR_STRING;
6554 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006555#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006556 lnum = get_tv_lnum(argvars);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006557 fold_count = foldedCount(curwin, lnum, &foldinfo);
6558 if (fold_count > 0)
6559 {
6560 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
6561 &foldinfo, buf);
6562 if (text == buf)
6563 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006564 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00006565 }
6566#endif
6567}
6568
6569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 * "foreground()" function
6571 */
6572/*ARGSUSED*/
6573 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006574f_foreground(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006575 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006576 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006578 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579#ifdef FEAT_GUI
6580 if (gui.in_use)
6581 gui_mch_set_foreground();
6582#else
6583# ifdef WIN32
6584 win32_set_foreground();
6585# endif
6586#endif
6587}
6588
6589/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006590 * "function()" function
6591 */
6592/*ARGSUSED*/
6593 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006594f_function(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006596 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597{
6598 char_u *s;
6599
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006600 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006601 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006602 EMSG2(_(e_invarg2), s);
6603 else if (!function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +00006604 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605 else
6606 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006607 rettv->vval.v_string = vim_strsave(s);
6608 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609 }
6610}
6611
6612/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00006613 * "get()" function
6614 */
6615 static void
6616f_get(argvars, rettv)
6617 typeval *argvars;
6618 typeval *rettv;
6619{
6620 listitem *item;
6621 listvar *l;
6622
6623 if (argvars[0].v_type != VAR_LIST)
6624 EMSG2(_(e_listarg), "get()");
6625 else if ((l = argvars[0].vval.v_list) != NULL)
6626 {
6627 item = list_find(l, get_tv_number(&argvars[1]));
6628 if (item == NULL)
6629 {
6630 if (argvars[2].v_type == VAR_UNKNOWN)
6631 rettv->vval.v_number = 0;
6632 else
6633 copy_tv(&argvars[2], rettv);
6634 }
6635 else
6636 copy_tv(&item->li_tv, rettv);
6637 }
6638}
6639
6640/*
6641 * "getbufvar()" function
6642 */
6643 static void
6644f_getbufvar(argvars, rettv)
6645 typeval *argvars;
6646 typeval *rettv;
6647{
6648 buf_T *buf;
6649 buf_T *save_curbuf;
6650 char_u *varname;
6651 VAR v;
6652
6653 ++emsg_off;
6654 buf = get_buf_tv(&argvars[0]);
6655 varname = get_tv_string(&argvars[1]);
6656
6657 rettv->v_type = VAR_STRING;
6658 rettv->vval.v_string = NULL;
6659
6660 if (buf != NULL && varname != NULL)
6661 {
6662 if (*varname == '&') /* buffer-local-option */
6663 {
6664 /* set curbuf to be our buf, temporarily */
6665 save_curbuf = curbuf;
6666 curbuf = buf;
6667
6668 get_option_tv(&varname, rettv, TRUE);
6669
6670 /* restore previous notion of curbuf */
6671 curbuf = save_curbuf;
6672 }
6673 else
6674 {
6675 /* look up the variable */
6676 v = find_var_in_ga(&buf->b_vars, varname);
6677 if (v != NULL)
6678 copy_tv(&v->tv, rettv);
6679 }
6680 }
6681
6682 --emsg_off;
6683}
6684
6685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 * "getchar()" function
6687 */
6688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006689f_getchar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006690 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006691 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692{
6693 varnumber_T n;
6694
6695 ++no_mapping;
6696 ++allow_keys;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006697 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 /* getchar(): blocking wait. */
6699 n = safe_vgetc();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006700 else if (get_tv_number(&argvars[0]) == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 /* getchar(1): only check if char avail */
6702 n = vpeekc();
6703 else if (vpeekc() == NUL)
6704 /* getchar(0) and no char avail: return zero */
6705 n = 0;
6706 else
6707 /* getchar(0) and char avail: return char */
6708 n = safe_vgetc();
6709 --no_mapping;
6710 --allow_keys;
6711
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006712 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 if (IS_SPECIAL(n) || mod_mask != 0)
6714 {
6715 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
6716 int i = 0;
6717
6718 /* Turn a special key into three bytes, plus modifier. */
6719 if (mod_mask != 0)
6720 {
6721 temp[i++] = K_SPECIAL;
6722 temp[i++] = KS_MODIFIER;
6723 temp[i++] = mod_mask;
6724 }
6725 if (IS_SPECIAL(n))
6726 {
6727 temp[i++] = K_SPECIAL;
6728 temp[i++] = K_SECOND(n);
6729 temp[i++] = K_THIRD(n);
6730 }
6731#ifdef FEAT_MBYTE
6732 else if (has_mbyte)
6733 i += (*mb_char2bytes)(n, temp + i);
6734#endif
6735 else
6736 temp[i++] = n;
6737 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006738 rettv->v_type = VAR_STRING;
6739 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 }
6741}
6742
6743/*
6744 * "getcharmod()" function
6745 */
6746/*ARGSUSED*/
6747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006748f_getcharmod(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006749 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006750 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006752 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753}
6754
6755/*
6756 * "getcmdline()" function
6757 */
6758/*ARGSUSED*/
6759 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006760f_getcmdline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006761 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006762 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006764 rettv->v_type = VAR_STRING;
6765 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766}
6767
6768/*
6769 * "getcmdpos()" function
6770 */
6771/*ARGSUSED*/
6772 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006773f_getcmdpos(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006774 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006775 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006777 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778}
6779
6780/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 * "getcwd()" function
6782 */
6783/*ARGSUSED*/
6784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006785f_getcwd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006786 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006787 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788{
6789 char_u cwd[MAXPATHL];
6790
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006791 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 if (mch_dirname(cwd, MAXPATHL) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006793 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 else
6795 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006796 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006798 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799#endif
6800 }
6801}
6802
6803/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006804 * "getfontname()" function
6805 */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006806/*ARGSUSED*/
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006807 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006808f_getfontname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006809 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006810 typeval *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006811{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006812 rettv->v_type = VAR_STRING;
6813 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006814#ifdef FEAT_GUI
6815 if (gui.in_use)
6816 {
6817 GuiFont font;
6818 char_u *name = NULL;
6819
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006820 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006821 {
6822 /* Get the "Normal" font. Either the name saved by
6823 * hl_set_font_name() or from the font ID. */
6824 font = gui.norm_font;
6825 name = hl_get_font_name();
6826 }
6827 else
6828 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006829 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006830 if (STRCMP(name, "*") == 0) /* don't use font dialog */
6831 return;
6832 font = gui_mch_get_font(name, FALSE);
6833 if (font == NOFONT)
6834 return; /* Invalid font name, return empty string. */
6835 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006836 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006837 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +00006838 gui_mch_free_font(font);
6839 }
6840#endif
6841}
6842
6843/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006844 * "getfperm({fname})" function
6845 */
6846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006847f_getfperm(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006848 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006849 typeval *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006850{
6851 char_u *fname;
6852 struct stat st;
6853 char_u *perm = NULL;
6854 char_u flags[] = "rwx";
6855 int i;
6856
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006857 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006858
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006859 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006860 if (mch_stat((char *)fname, &st) >= 0)
6861 {
6862 perm = vim_strsave((char_u *)"---------");
6863 if (perm != NULL)
6864 {
6865 for (i = 0; i < 9; i++)
6866 {
6867 if (st.st_mode & (1 << (8 - i)))
6868 perm[i] = flags[i % 3];
6869 }
6870 }
6871 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006872 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006873}
6874
6875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 * "getfsize({fname})" function
6877 */
6878 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006879f_getfsize(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006880 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006881 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882{
6883 char_u *fname;
6884 struct stat st;
6885
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006886 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006888 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889
6890 if (mch_stat((char *)fname, &st) >= 0)
6891 {
6892 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006893 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006895 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 }
6897 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006898 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899}
6900
6901/*
6902 * "getftime({fname})" function
6903 */
6904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006905f_getftime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006906 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006907 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908{
6909 char_u *fname;
6910 struct stat st;
6911
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006912 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913
6914 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006915 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006917 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918}
6919
6920/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006921 * "getftype({fname})" function
6922 */
6923 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006924f_getftype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006925 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006926 typeval *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006927{
6928 char_u *fname;
6929 struct stat st;
6930 char_u *type = NULL;
6931 char *t;
6932
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006933 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006934
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006935 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006936 if (mch_lstat((char *)fname, &st) >= 0)
6937 {
6938#ifdef S_ISREG
6939 if (S_ISREG(st.st_mode))
6940 t = "file";
6941 else if (S_ISDIR(st.st_mode))
6942 t = "dir";
6943# ifdef S_ISLNK
6944 else if (S_ISLNK(st.st_mode))
6945 t = "link";
6946# endif
6947# ifdef S_ISBLK
6948 else if (S_ISBLK(st.st_mode))
6949 t = "bdev";
6950# endif
6951# ifdef S_ISCHR
6952 else if (S_ISCHR(st.st_mode))
6953 t = "cdev";
6954# endif
6955# ifdef S_ISFIFO
6956 else if (S_ISFIFO(st.st_mode))
6957 t = "fifo";
6958# endif
6959# ifdef S_ISSOCK
6960 else if (S_ISSOCK(st.st_mode))
6961 t = "fifo";
6962# endif
6963 else
6964 t = "other";
6965#else
6966# ifdef S_IFMT
6967 switch (st.st_mode & S_IFMT)
6968 {
6969 case S_IFREG: t = "file"; break;
6970 case S_IFDIR: t = "dir"; break;
6971# ifdef S_IFLNK
6972 case S_IFLNK: t = "link"; break;
6973# endif
6974# ifdef S_IFBLK
6975 case S_IFBLK: t = "bdev"; break;
6976# endif
6977# ifdef S_IFCHR
6978 case S_IFCHR: t = "cdev"; break;
6979# endif
6980# ifdef S_IFIFO
6981 case S_IFIFO: t = "fifo"; break;
6982# endif
6983# ifdef S_IFSOCK
6984 case S_IFSOCK: t = "socket"; break;
6985# endif
6986 default: t = "other";
6987 }
6988# else
6989 if (mch_isdir(fname))
6990 t = "dir";
6991 else
6992 t = "file";
6993# endif
6994#endif
6995 type = vim_strsave((char_u *)t);
6996 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006997 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00006998}
6999
7000/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007001 * "getline(lnum)" function
7002 */
7003 static void
7004f_getline(argvars, rettv)
7005 typeval *argvars;
7006 typeval *rettv;
7007{
7008 linenr_T lnum;
7009 linenr_T end;
7010 char_u *p;
7011 listvar *l;
7012 listitem *li;
7013
7014 lnum = get_tv_lnum(argvars);
7015
7016 if (argvars[1].v_type == VAR_UNKNOWN)
7017 {
7018 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
7019 p = ml_get(lnum);
7020 else
7021 p = (char_u *)"";
7022
7023 rettv->v_type = VAR_STRING;
7024 rettv->vval.v_string = vim_strsave(p);
7025 }
7026 else
7027 {
7028 end = get_tv_lnum(&argvars[1]);
7029 if (end < lnum)
7030 {
7031 EMSG(_(e_invrange));
7032 rettv->vval.v_number = 0;
7033 }
7034 else
7035 {
7036 l = list_alloc();
7037 if (l != NULL)
7038 {
7039 if (lnum < 1)
7040 lnum = 1;
7041 if (end > curbuf->b_ml.ml_line_count)
7042 end = curbuf->b_ml.ml_line_count;
7043 while (lnum <= end)
7044 {
7045 li = listitem_alloc();
7046 if (li == NULL)
7047 break;
7048 list_append(l, li);
7049 li->li_tv.v_type = VAR_STRING;
7050 li->li_tv.vval.v_string = vim_strsave(ml_get(lnum++));
7051 }
7052 rettv->vval.v_list = l;
7053 rettv->v_type = VAR_LIST;
7054 ++l->lv_refcount;
7055 }
7056 }
7057 }
7058}
7059
7060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 * "getreg()" function
7062 */
7063 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007064f_getreg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007065 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007066 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067{
7068 char_u *strregname;
7069 int regname;
7070
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007071 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007072 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 else
7074 strregname = vimvars[VV_REG].val;
7075 regname = (strregname == NULL ? '"' : *strregname);
7076 if (regname == 0)
7077 regname = '"';
7078
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007079 rettv->v_type = VAR_STRING;
7080 rettv->vval.v_string = get_reg_contents(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081}
7082
7083/*
7084 * "getregtype()" function
7085 */
7086 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007087f_getregtype(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007088 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007089 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090{
7091 char_u *strregname;
7092 int regname;
7093 char_u buf[NUMBUFLEN + 2];
7094 long reglen = 0;
7095
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007096 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007097 strregname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 else
7099 /* Default to v:register */
7100 strregname = vimvars[VV_REG].val;
7101
7102 regname = (strregname == NULL ? '"' : *strregname);
7103 if (regname == 0)
7104 regname = '"';
7105
7106 buf[0] = NUL;
7107 buf[1] = NUL;
7108 switch (get_reg_type(regname, &reglen))
7109 {
7110 case MLINE: buf[0] = 'V'; break;
7111 case MCHAR: buf[0] = 'v'; break;
7112#ifdef FEAT_VISUAL
7113 case MBLOCK:
7114 buf[0] = Ctrl_V;
7115 sprintf((char *)buf + 1, "%ld", reglen + 1);
7116 break;
7117#endif
7118 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007119 rettv->v_type = VAR_STRING;
7120 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121}
7122
7123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 * "getwinposx()" function
7125 */
7126/*ARGSUSED*/
7127 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007128f_getwinposx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007129 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007130 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007132 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133#ifdef FEAT_GUI
7134 if (gui.in_use)
7135 {
7136 int x, y;
7137
7138 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007139 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 }
7141#endif
7142}
7143
7144/*
7145 * "getwinposy()" function
7146 */
7147/*ARGSUSED*/
7148 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007149f_getwinposy(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007150 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007151 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007153 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154#ifdef FEAT_GUI
7155 if (gui.in_use)
7156 {
7157 int x, y;
7158
7159 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007160 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 }
7162#endif
7163}
7164
7165/*
7166 * "getwinvar()" function
7167 */
7168 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007169f_getwinvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007170 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007171 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172{
7173 win_T *win, *oldcurwin;
7174 char_u *varname;
7175 VAR v;
7176
7177 ++emsg_off;
7178 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007179 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007181 rettv->v_type = VAR_STRING;
7182 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183
7184 if (win != NULL && varname != NULL)
7185 {
7186 if (*varname == '&') /* window-local-option */
7187 {
7188 /* set curwin to be our win, temporarily */
7189 oldcurwin = curwin;
7190 curwin = win;
7191
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007192 get_option_tv(&varname, rettv, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193
7194 /* restore previous notion of curwin */
7195 curwin = oldcurwin;
7196 }
7197 else
7198 {
7199 /* look up the variable */
7200 v = find_var_in_ga(&win->w_vars, varname);
7201 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007202 copy_tv(&v->tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 }
7204 }
7205
7206 --emsg_off;
7207}
7208
7209/*
7210 * "glob()" function
7211 */
7212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007213f_glob(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007214 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007215 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216{
7217 expand_T xpc;
7218
7219 ExpandInit(&xpc);
7220 xpc.xp_context = EXPAND_FILES;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007221 rettv->v_type = VAR_STRING;
7222 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 NULL, WILD_USE_NL|WILD_SILENT, WILD_ALL);
7224 ExpandCleanup(&xpc);
7225}
7226
7227/*
7228 * "globpath()" function
7229 */
7230 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007231f_globpath(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007232 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007233 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234{
7235 char_u buf1[NUMBUFLEN];
7236
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007237 rettv->v_type = VAR_STRING;
7238 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]),
7239 get_tv_string_buf(&argvars[1], buf1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240}
7241
7242/*
7243 * "has()" function
7244 */
7245 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007246f_has(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007247 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007248 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249{
7250 int i;
7251 char_u *name;
7252 int n = FALSE;
7253 static char *(has_list[]) =
7254 {
7255#ifdef AMIGA
7256 "amiga",
7257# ifdef FEAT_ARP
7258 "arp",
7259# endif
7260#endif
7261#ifdef __BEOS__
7262 "beos",
7263#endif
7264#ifdef MSDOS
7265# ifdef DJGPP
7266 "dos32",
7267# else
7268 "dos16",
7269# endif
7270#endif
7271#ifdef MACOS /* TODO: Should we add MACOS_CLASSIC, MACOS_X? (Dany) */
7272 "mac",
7273#endif
7274#if defined(MACOS_X_UNIX)
7275 "macunix",
7276#endif
7277#ifdef OS2
7278 "os2",
7279#endif
7280#ifdef __QNX__
7281 "qnx",
7282#endif
7283#ifdef RISCOS
7284 "riscos",
7285#endif
7286#ifdef UNIX
7287 "unix",
7288#endif
7289#ifdef VMS
7290 "vms",
7291#endif
7292#ifdef WIN16
7293 "win16",
7294#endif
7295#ifdef WIN32
7296 "win32",
7297#endif
7298#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
7299 "win32unix",
7300#endif
7301#ifdef WIN64
7302 "win64",
7303#endif
7304#ifdef EBCDIC
7305 "ebcdic",
7306#endif
7307#ifndef CASE_INSENSITIVE_FILENAME
7308 "fname_case",
7309#endif
7310#ifdef FEAT_ARABIC
7311 "arabic",
7312#endif
7313#ifdef FEAT_AUTOCMD
7314 "autocmd",
7315#endif
7316#ifdef FEAT_BEVAL
7317 "balloon_eval",
7318#endif
7319#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
7320 "builtin_terms",
7321# ifdef ALL_BUILTIN_TCAPS
7322 "all_builtin_terms",
7323# endif
7324#endif
7325#ifdef FEAT_BYTEOFF
7326 "byte_offset",
7327#endif
7328#ifdef FEAT_CINDENT
7329 "cindent",
7330#endif
7331#ifdef FEAT_CLIENTSERVER
7332 "clientserver",
7333#endif
7334#ifdef FEAT_CLIPBOARD
7335 "clipboard",
7336#endif
7337#ifdef FEAT_CMDL_COMPL
7338 "cmdline_compl",
7339#endif
7340#ifdef FEAT_CMDHIST
7341 "cmdline_hist",
7342#endif
7343#ifdef FEAT_COMMENTS
7344 "comments",
7345#endif
7346#ifdef FEAT_CRYPT
7347 "cryptv",
7348#endif
7349#ifdef FEAT_CSCOPE
7350 "cscope",
7351#endif
7352#ifdef DEBUG
7353 "debug",
7354#endif
7355#ifdef FEAT_CON_DIALOG
7356 "dialog_con",
7357#endif
7358#ifdef FEAT_GUI_DIALOG
7359 "dialog_gui",
7360#endif
7361#ifdef FEAT_DIFF
7362 "diff",
7363#endif
7364#ifdef FEAT_DIGRAPHS
7365 "digraphs",
7366#endif
7367#ifdef FEAT_DND
7368 "dnd",
7369#endif
7370#ifdef FEAT_EMACS_TAGS
7371 "emacs_tags",
7372#endif
7373 "eval", /* always present, of course! */
7374#ifdef FEAT_EX_EXTRA
7375 "ex_extra",
7376#endif
7377#ifdef FEAT_SEARCH_EXTRA
7378 "extra_search",
7379#endif
7380#ifdef FEAT_FKMAP
7381 "farsi",
7382#endif
7383#ifdef FEAT_SEARCHPATH
7384 "file_in_path",
7385#endif
7386#ifdef FEAT_FIND_ID
7387 "find_in_path",
7388#endif
7389#ifdef FEAT_FOLDING
7390 "folding",
7391#endif
7392#ifdef FEAT_FOOTER
7393 "footer",
7394#endif
7395#if !defined(USE_SYSTEM) && defined(UNIX)
7396 "fork",
7397#endif
7398#ifdef FEAT_GETTEXT
7399 "gettext",
7400#endif
7401#ifdef FEAT_GUI
7402 "gui",
7403#endif
7404#ifdef FEAT_GUI_ATHENA
7405# ifdef FEAT_GUI_NEXTAW
7406 "gui_neXtaw",
7407# else
7408 "gui_athena",
7409# endif
7410#endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007411#ifdef FEAT_GUI_KDE
7412 "gui_kde",
7413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414#ifdef FEAT_GUI_GTK
7415 "gui_gtk",
7416# ifdef HAVE_GTK2
7417 "gui_gtk2",
7418# endif
7419#endif
7420#ifdef FEAT_GUI_MAC
7421 "gui_mac",
7422#endif
7423#ifdef FEAT_GUI_MOTIF
7424 "gui_motif",
7425#endif
7426#ifdef FEAT_GUI_PHOTON
7427 "gui_photon",
7428#endif
7429#ifdef FEAT_GUI_W16
7430 "gui_win16",
7431#endif
7432#ifdef FEAT_GUI_W32
7433 "gui_win32",
7434#endif
7435#ifdef FEAT_HANGULIN
7436 "hangul_input",
7437#endif
7438#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
7439 "iconv",
7440#endif
7441#ifdef FEAT_INS_EXPAND
7442 "insert_expand",
7443#endif
7444#ifdef FEAT_JUMPLIST
7445 "jumplist",
7446#endif
7447#ifdef FEAT_KEYMAP
7448 "keymap",
7449#endif
7450#ifdef FEAT_LANGMAP
7451 "langmap",
7452#endif
7453#ifdef FEAT_LIBCALL
7454 "libcall",
7455#endif
7456#ifdef FEAT_LINEBREAK
7457 "linebreak",
7458#endif
7459#ifdef FEAT_LISP
7460 "lispindent",
7461#endif
7462#ifdef FEAT_LISTCMDS
7463 "listcmds",
7464#endif
7465#ifdef FEAT_LOCALMAP
7466 "localmap",
7467#endif
7468#ifdef FEAT_MENU
7469 "menu",
7470#endif
7471#ifdef FEAT_SESSION
7472 "mksession",
7473#endif
7474#ifdef FEAT_MODIFY_FNAME
7475 "modify_fname",
7476#endif
7477#ifdef FEAT_MOUSE
7478 "mouse",
7479#endif
7480#ifdef FEAT_MOUSESHAPE
7481 "mouseshape",
7482#endif
7483#if defined(UNIX) || defined(VMS)
7484# ifdef FEAT_MOUSE_DEC
7485 "mouse_dec",
7486# endif
7487# ifdef FEAT_MOUSE_GPM
7488 "mouse_gpm",
7489# endif
7490# ifdef FEAT_MOUSE_JSB
7491 "mouse_jsbterm",
7492# endif
7493# ifdef FEAT_MOUSE_NET
7494 "mouse_netterm",
7495# endif
7496# ifdef FEAT_MOUSE_PTERM
7497 "mouse_pterm",
7498# endif
7499# ifdef FEAT_MOUSE_XTERM
7500 "mouse_xterm",
7501# endif
7502#endif
7503#ifdef FEAT_MBYTE
7504 "multi_byte",
7505#endif
7506#ifdef FEAT_MBYTE_IME
7507 "multi_byte_ime",
7508#endif
7509#ifdef FEAT_MULTI_LANG
7510 "multi_lang",
7511#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007512#ifdef FEAT_MZSCHEME
7513 "mzscheme",
7514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515#ifdef FEAT_OLE
7516 "ole",
7517#endif
7518#ifdef FEAT_OSFILETYPE
7519 "osfiletype",
7520#endif
7521#ifdef FEAT_PATH_EXTRA
7522 "path_extra",
7523#endif
7524#ifdef FEAT_PERL
7525#ifndef DYNAMIC_PERL
7526 "perl",
7527#endif
7528#endif
7529#ifdef FEAT_PYTHON
7530#ifndef DYNAMIC_PYTHON
7531 "python",
7532#endif
7533#endif
7534#ifdef FEAT_POSTSCRIPT
7535 "postscript",
7536#endif
7537#ifdef FEAT_PRINTER
7538 "printer",
7539#endif
7540#ifdef FEAT_QUICKFIX
7541 "quickfix",
7542#endif
7543#ifdef FEAT_RIGHTLEFT
7544 "rightleft",
7545#endif
7546#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
7547 "ruby",
7548#endif
7549#ifdef FEAT_SCROLLBIND
7550 "scrollbind",
7551#endif
7552#ifdef FEAT_CMDL_INFO
7553 "showcmd",
7554 "cmdline_info",
7555#endif
7556#ifdef FEAT_SIGNS
7557 "signs",
7558#endif
7559#ifdef FEAT_SMARTINDENT
7560 "smartindent",
7561#endif
7562#ifdef FEAT_SNIFF
7563 "sniff",
7564#endif
7565#ifdef FEAT_STL_OPT
7566 "statusline",
7567#endif
7568#ifdef FEAT_SUN_WORKSHOP
7569 "sun_workshop",
7570#endif
7571#ifdef FEAT_NETBEANS_INTG
7572 "netbeans_intg",
7573#endif
7574#ifdef FEAT_SYN_HL
7575 "syntax",
7576#endif
7577#if defined(USE_SYSTEM) || !defined(UNIX)
7578 "system",
7579#endif
7580#ifdef FEAT_TAG_BINS
7581 "tag_binary",
7582#endif
7583#ifdef FEAT_TAG_OLDSTATIC
7584 "tag_old_static",
7585#endif
7586#ifdef FEAT_TAG_ANYWHITE
7587 "tag_any_white",
7588#endif
7589#ifdef FEAT_TCL
7590# ifndef DYNAMIC_TCL
7591 "tcl",
7592# endif
7593#endif
7594#ifdef TERMINFO
7595 "terminfo",
7596#endif
7597#ifdef FEAT_TERMRESPONSE
7598 "termresponse",
7599#endif
7600#ifdef FEAT_TEXTOBJ
7601 "textobjects",
7602#endif
7603#ifdef HAVE_TGETENT
7604 "tgetent",
7605#endif
7606#ifdef FEAT_TITLE
7607 "title",
7608#endif
7609#ifdef FEAT_TOOLBAR
7610 "toolbar",
7611#endif
7612#ifdef FEAT_USR_CMDS
7613 "user-commands", /* was accidentally included in 5.4 */
7614 "user_commands",
7615#endif
7616#ifdef FEAT_VIMINFO
7617 "viminfo",
7618#endif
7619#ifdef FEAT_VERTSPLIT
7620 "vertsplit",
7621#endif
7622#ifdef FEAT_VIRTUALEDIT
7623 "virtualedit",
7624#endif
7625#ifdef FEAT_VISUAL
7626 "visual",
7627#endif
7628#ifdef FEAT_VISUALEXTRA
7629 "visualextra",
7630#endif
7631#ifdef FEAT_VREPLACE
7632 "vreplace",
7633#endif
7634#ifdef FEAT_WILDIGN
7635 "wildignore",
7636#endif
7637#ifdef FEAT_WILDMENU
7638 "wildmenu",
7639#endif
7640#ifdef FEAT_WINDOWS
7641 "windows",
7642#endif
7643#ifdef FEAT_WAK
7644 "winaltkeys",
7645#endif
7646#ifdef FEAT_WRITEBACKUP
7647 "writebackup",
7648#endif
7649#ifdef FEAT_XIM
7650 "xim",
7651#endif
7652#ifdef FEAT_XFONTSET
7653 "xfontset",
7654#endif
7655#ifdef USE_XSMP
7656 "xsmp",
7657#endif
7658#ifdef USE_XSMP_INTERACT
7659 "xsmp_interact",
7660#endif
7661#ifdef FEAT_XCLIPBOARD
7662 "xterm_clipboard",
7663#endif
7664#ifdef FEAT_XTERM_SAVE
7665 "xterm_save",
7666#endif
7667#if defined(UNIX) && defined(FEAT_X11)
7668 "X11",
7669#endif
7670 NULL
7671 };
7672
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007673 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 for (i = 0; has_list[i] != NULL; ++i)
7675 if (STRICMP(name, has_list[i]) == 0)
7676 {
7677 n = TRUE;
7678 break;
7679 }
7680
7681 if (n == FALSE)
7682 {
7683 if (STRNICMP(name, "patch", 5) == 0)
7684 n = has_patch(atoi((char *)name + 5));
7685 else if (STRICMP(name, "vim_starting") == 0)
7686 n = (starting != 0);
7687#ifdef DYNAMIC_TCL
7688 else if (STRICMP(name, "tcl") == 0)
7689 n = tcl_enabled(FALSE);
7690#endif
7691#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
7692 else if (STRICMP(name, "iconv") == 0)
7693 n = iconv_enabled(FALSE);
7694#endif
7695#ifdef DYNAMIC_RUBY
7696 else if (STRICMP(name, "ruby") == 0)
7697 n = ruby_enabled(FALSE);
7698#endif
7699#ifdef DYNAMIC_PYTHON
7700 else if (STRICMP(name, "python") == 0)
7701 n = python_enabled(FALSE);
7702#endif
7703#ifdef DYNAMIC_PERL
7704 else if (STRICMP(name, "perl") == 0)
7705 n = perl_enabled(FALSE);
7706#endif
7707#ifdef FEAT_GUI
7708 else if (STRICMP(name, "gui_running") == 0)
7709 n = (gui.in_use || gui.starting);
7710# ifdef FEAT_GUI_W32
7711 else if (STRICMP(name, "gui_win32s") == 0)
7712 n = gui_is_win32s();
7713# endif
7714# ifdef FEAT_BROWSE
7715 else if (STRICMP(name, "browse") == 0)
7716 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
7717# endif
7718#endif
7719#ifdef FEAT_SYN_HL
7720 else if (STRICMP(name, "syntax_items") == 0)
7721 n = syntax_present(curbuf);
7722#endif
7723#if defined(WIN3264)
7724 else if (STRICMP(name, "win95") == 0)
7725 n = mch_windows95();
7726#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +00007727#ifdef FEAT_NETBEANS_INTG
7728 else if (STRICMP(name, "netbeans_enabled") == 0)
7729 n = usingNetbeans;
7730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 }
7732
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007733 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734}
7735
7736/*
7737 * "hasmapto()" function
7738 */
7739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007740f_hasmapto(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007741 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007742 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743{
7744 char_u *name;
7745 char_u *mode;
7746 char_u buf[NUMBUFLEN];
7747
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007748 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007749 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 mode = (char_u *)"nvo";
7751 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007752 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753
7754 if (map_to_exists(name, mode))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007755 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007757 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758}
7759
7760/*
7761 * "histadd()" function
7762 */
7763/*ARGSUSED*/
7764 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007765f_histadd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007766 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007767 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768{
7769#ifdef FEAT_CMDHIST
7770 int histype;
7771 char_u *str;
7772 char_u buf[NUMBUFLEN];
7773#endif
7774
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007775 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 if (check_restricted() || check_secure())
7777 return;
7778#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007779 histype = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 if (histype >= 0)
7781 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007782 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 if (*str != NUL)
7784 {
7785 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007786 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 return;
7788 }
7789 }
7790#endif
7791}
7792
7793/*
7794 * "histdel()" function
7795 */
7796/*ARGSUSED*/
7797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007798f_histdel(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007799 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007800 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801{
7802#ifdef FEAT_CMDHIST
7803 int n;
7804 char_u buf[NUMBUFLEN];
7805
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007806 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 /* only one argument: clear entire history */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007808 n = clr_history(get_histtype(get_tv_string(&argvars[0])));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007809 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 /* index given: remove that entry */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007811 n = del_history_idx(get_histtype(get_tv_string(&argvars[0])),
7812 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 else
7814 /* string given: remove all matching entries */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007815 n = del_history_entry(get_histtype(get_tv_string(&argvars[0])),
7816 get_tv_string_buf(&argvars[1], buf));
7817 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007819 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820#endif
7821}
7822
7823/*
7824 * "histget()" function
7825 */
7826/*ARGSUSED*/
7827 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007828f_histget(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007829 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007830 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831{
7832#ifdef FEAT_CMDHIST
7833 int type;
7834 int idx;
7835
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007836 type = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007837 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 idx = get_history_idx(type);
7839 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007840 idx = (int)get_tv_number(&argvars[1]);
7841 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007843 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007845 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846}
7847
7848/*
7849 * "histnr()" function
7850 */
7851/*ARGSUSED*/
7852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007853f_histnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007854 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007855 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856{
7857 int i;
7858
7859#ifdef FEAT_CMDHIST
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007860 i = get_histtype(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 if (i >= HIST_CMD && i < HIST_COUNT)
7862 i = get_history_idx(i);
7863 else
7864#endif
7865 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007866 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867}
7868
7869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 * "highlightID(name)" function
7871 */
7872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007873f_hlID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007874 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007875 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007877 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878}
7879
7880/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00007881 * "highlight_exists()" function
7882 */
7883 static void
7884f_hlexists(argvars, rettv)
7885 typeval *argvars;
7886 typeval *rettv;
7887{
7888 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
7889}
7890
7891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 * "hostname()" function
7893 */
7894/*ARGSUSED*/
7895 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007896f_hostname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007897 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007898 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899{
7900 char_u hostname[256];
7901
7902 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007903 rettv->v_type = VAR_STRING;
7904 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905}
7906
7907/*
7908 * iconv() function
7909 */
7910/*ARGSUSED*/
7911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007912f_iconv(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007913 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007914 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915{
7916#ifdef FEAT_MBYTE
7917 char_u buf1[NUMBUFLEN];
7918 char_u buf2[NUMBUFLEN];
7919 char_u *from, *to, *str;
7920 vimconv_T vimconv;
7921#endif
7922
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007923 rettv->v_type = VAR_STRING;
7924 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925
7926#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007927 str = get_tv_string(&argvars[0]);
7928 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
7929 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 vimconv.vc_type = CONV_NONE;
7931 convert_setup(&vimconv, from, to);
7932
7933 /* If the encodings are equal, no conversion needed. */
7934 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007935 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007937 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938
7939 convert_setup(&vimconv, NULL, NULL);
7940 vim_free(from);
7941 vim_free(to);
7942#endif
7943}
7944
7945/*
7946 * "indent()" function
7947 */
7948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007949f_indent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007950 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007951 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952{
7953 linenr_T lnum;
7954
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007955 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007957 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007959 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960}
7961
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007962/*
7963 * "index()" function
7964 */
7965 static void
7966f_index(argvars, rettv)
7967 typeval *argvars;
7968 typeval *rettv;
7969{
7970 listvar *l;
7971 listitem *item;
7972 long idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007973 long min_idx = 0;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007974 int ic = FALSE;
7975
7976 rettv->vval.v_number = -1;
7977 if (argvars[0].v_type != VAR_LIST)
7978 {
7979 EMSG(_(e_listreq));
7980 return;
7981 }
7982 l = argvars[0].vval.v_list;
7983 if (l != NULL)
7984 {
7985 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007986 {
7987 min_idx = get_tv_number(&argvars[2]);
7988 if (argvars[3].v_type != VAR_UNKNOWN)
7989 ic = get_tv_number(&argvars[3]);
7990 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007991
7992 for (item = l->lv_first; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007993 if (idx >= min_idx && tv_equal(&item->li_tv, &argvars[1], ic))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007994 {
7995 rettv->vval.v_number = idx;
7996 break;
7997 }
7998 }
7999}
8000
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001static int inputsecret_flag = 0;
8002
8003/*
8004 * "input()" function
8005 * Also handles inputsecret() when inputsecret is set.
8006 */
8007 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008008f_input(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008009 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008010 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008012 char_u *prompt = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 char_u *p = NULL;
8014 int c;
8015 char_u buf[NUMBUFLEN];
8016 int cmd_silent_save = cmd_silent;
8017
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008018 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019
8020#ifdef NO_CONSOLE_INPUT
8021 /* While starting up, there is no place to enter text. */
8022 if (no_console_input())
8023 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008024 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 return;
8026 }
8027#endif
8028
8029 cmd_silent = FALSE; /* Want to see the prompt. */
8030 if (prompt != NULL)
8031 {
8032 /* Only the part of the message after the last NL is considered as
8033 * prompt for the command line */
8034 p = vim_strrchr(prompt, '\n');
8035 if (p == NULL)
8036 p = prompt;
8037 else
8038 {
8039 ++p;
8040 c = *p;
8041 *p = NUL;
8042 msg_start();
8043 msg_clr_eos();
8044 msg_puts_attr(prompt, echo_attr);
8045 msg_didout = FALSE;
8046 msg_starthere();
8047 *p = c;
8048 }
8049 cmdline_row = msg_row;
8050 }
8051
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008052 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008053 stuffReadbuffSpec(get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008055 rettv->vval.v_string =
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr);
8057
8058 /* since the user typed this, no need to wait for return */
8059 need_wait_return = FALSE;
8060 msg_didout = FALSE;
8061 cmd_silent = cmd_silent_save;
8062}
8063
8064/*
8065 * "inputdialog()" function
8066 */
8067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008068f_inputdialog(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008069 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008070 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071{
8072#if defined(FEAT_GUI_TEXTDIALOG)
8073 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
8074 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
8075 {
8076 char_u *message;
8077 char_u buf[NUMBUFLEN];
8078
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008079 message = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008080 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008082 STRNCPY(IObuff, get_tv_string_buf(&argvars[1], buf), IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 IObuff[IOSIZE - 1] = NUL;
8084 }
8085 else
8086 IObuff[0] = NUL;
8087 if (do_dialog(VIM_QUESTION, NULL, message, (char_u *)_("&OK\n&Cancel"),
8088 1, IObuff) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008089 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 else
8091 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008092 if (argvars[1].v_type != VAR_UNKNOWN
8093 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008094 rettv->vval.v_string = vim_strsave(
8095 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008097 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008099 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 }
8101 else
8102#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008103 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104}
8105
8106static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
8107
8108/*
8109 * "inputrestore()" function
8110 */
8111/*ARGSUSED*/
8112 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008113f_inputrestore(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008114 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008115 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116{
8117 if (ga_userinput.ga_len > 0)
8118 {
8119 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
8121 + ga_userinput.ga_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008122 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 }
8124 else if (p_verbose > 1)
8125 {
8126 msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008127 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 }
8129}
8130
8131/*
8132 * "inputsave()" function
8133 */
8134/*ARGSUSED*/
8135 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008136f_inputsave(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008137 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008138 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139{
8140 /* Add an entry to the stack of typehead storage. */
8141 if (ga_grow(&ga_userinput, 1) == OK)
8142 {
8143 save_typeahead((tasave_T *)(ga_userinput.ga_data)
8144 + ga_userinput.ga_len);
8145 ++ga_userinput.ga_len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008146 rettv->vval.v_number = 0; /* OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 }
8148 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008149 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150}
8151
8152/*
8153 * "inputsecret()" function
8154 */
8155 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008156f_inputsecret(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008157 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008158 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159{
8160 ++cmdline_star;
8161 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008162 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 --cmdline_star;
8164 --inputsecret_flag;
8165}
8166
8167/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008168 * "insert()" function
8169 */
8170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008171f_insert(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008172 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008173 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008174{
8175 long before = 0;
8176 long n;
8177 listitem *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008178 listvar *l;
8179
8180 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00008181 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008182 else if ((l = argvars[0].vval.v_list) != NULL)
8183 {
8184 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008185 before = get_tv_number(&argvars[2]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008186
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008187 n = before;
8188 item = list_find_ext(l, &n);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008189 if (n > 0)
8190 EMSGN(_(e_listidx), before);
8191 else
8192 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008193 list_insert_tv(l, &argvars[1], item);
8194 ++l->lv_refcount;
8195 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008196 }
8197 }
8198}
8199
8200/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 * "isdirectory()" function
8202 */
8203 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008204f_isdirectory(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008205 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008206 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008208 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209}
8210
8211/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008212 * "join()" function
8213 */
8214 static void
8215f_join(argvars, rettv)
8216 typeval *argvars;
8217 typeval *rettv;
8218{
8219 garray_T ga;
8220 char_u *sep;
8221
8222 rettv->vval.v_number = 0;
8223 if (argvars[0].v_type != VAR_LIST)
8224 {
8225 EMSG(_(e_listreq));
8226 return;
8227 }
8228 if (argvars[0].vval.v_list == NULL)
8229 return;
8230 if (argvars[1].v_type == VAR_UNKNOWN)
8231 sep = (char_u *)" ";
8232 else
8233 sep = get_tv_string(&argvars[1]);
8234
8235 ga_init2(&ga, (int)sizeof(char), 80);
8236 list_join(&ga, argvars[0].vval.v_list, sep, TRUE);
8237 ga_append(&ga, NUL);
8238
8239 rettv->v_type = VAR_STRING;
8240 rettv->vval.v_string = (char_u *)ga.ga_data;
8241}
8242
8243/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 * "last_buffer_nr()" function.
8245 */
8246/*ARGSUSED*/
8247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008248f_last_buffer_nr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008249 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008250 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251{
8252 int n = 0;
8253 buf_T *buf;
8254
8255 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8256 if (n < buf->b_fnum)
8257 n = buf->b_fnum;
8258
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008259 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008260}
8261
8262/*
8263 * "len()" function
8264 */
8265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008266f_len(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008267 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008268 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008269{
8270 switch (argvars[0].v_type)
8271 {
8272 case VAR_STRING:
8273 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008274 rettv->vval.v_number = (varnumber_T)STRLEN(
8275 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008276 break;
8277 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008278 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008279 break;
8280 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008281 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008282 break;
8283 }
8284}
8285
Bram Moolenaar0d660222005-01-07 21:51:51 +00008286static void libcall_common __ARGS((typeval *argvars, typeval *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008287
8288 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008289libcall_common(argvars, rettv, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008290 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008291 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008292 int type;
8293{
8294#ifdef FEAT_LIBCALL
8295 char_u *string_in;
8296 char_u **string_result;
8297 int nr_result;
8298#endif
8299
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008300 rettv->v_type = type;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008301 if (type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008302 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008303 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008304 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008305
8306 if (check_restricted() || check_secure())
8307 return;
8308
8309#ifdef FEAT_LIBCALL
8310 /* The first two args must be strings, otherwise its meaningless */
8311 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
8312 {
8313 if (argvars[2].v_type == VAR_NUMBER)
8314 string_in = NULL;
8315 else
8316 string_in = argvars[2].vval.v_string;
8317 if (type == VAR_NUMBER)
8318 string_result = NULL;
8319 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008320 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008321 if (mch_libcall(argvars[0].vval.v_string,
8322 argvars[1].vval.v_string,
8323 string_in,
8324 argvars[2].vval.v_number,
8325 string_result,
8326 &nr_result) == OK
8327 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008328 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008329 }
8330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331}
8332
8333/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008334 * "libcall()" function
8335 */
8336 static void
8337f_libcall(argvars, rettv)
8338 typeval *argvars;
8339 typeval *rettv;
8340{
8341 libcall_common(argvars, rettv, VAR_STRING);
8342}
8343
8344/*
8345 * "libcallnr()" function
8346 */
8347 static void
8348f_libcallnr(argvars, rettv)
8349 typeval *argvars;
8350 typeval *rettv;
8351{
8352 libcall_common(argvars, rettv, VAR_NUMBER);
8353}
8354
8355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 * "line(string)" function
8357 */
8358 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008359f_line(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008360 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008361 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362{
8363 linenr_T lnum = 0;
8364 pos_T *fp;
8365
8366 fp = var2fpos(&argvars[0], TRUE);
8367 if (fp != NULL)
8368 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008369 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370}
8371
8372/*
8373 * "line2byte(lnum)" function
8374 */
8375/*ARGSUSED*/
8376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008377f_line2byte(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008378 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008379 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380{
8381#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008382 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383#else
8384 linenr_T lnum;
8385
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008386 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008388 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008390 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
8391 if (rettv->vval.v_number >= 0)
8392 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393#endif
8394}
8395
8396/*
8397 * "lispindent(lnum)" function
8398 */
8399 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008400f_lispindent(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008401 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008402 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403{
8404#ifdef FEAT_LISP
8405 pos_T pos;
8406 linenr_T lnum;
8407
8408 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008409 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
8411 {
8412 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008413 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 curwin->w_cursor = pos;
8415 }
8416 else
8417#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008418 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419}
8420
8421/*
8422 * "localtime()" function
8423 */
8424/*ARGSUSED*/
8425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008426f_localtime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008427 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008428 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008430 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431}
8432
Bram Moolenaar0d660222005-01-07 21:51:51 +00008433static void get_maparg __ARGS((typeval *argvars, typeval *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434
8435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008436get_maparg(argvars, rettv, exact)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008437 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008438 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 int exact;
8440{
8441 char_u *keys;
8442 char_u *which;
8443 char_u buf[NUMBUFLEN];
8444 char_u *keys_buf = NULL;
8445 char_u *rhs;
8446 int mode;
8447 garray_T ga;
8448
8449 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008450 rettv->v_type = VAR_STRING;
8451 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008453 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 if (*keys == NUL)
8455 return;
8456
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008457 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008458 which = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 else
8460 which = (char_u *)"";
8461 mode = get_map_mode(&which, 0);
8462
8463 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE);
8464 rhs = check_map(keys, mode, exact);
8465 vim_free(keys_buf);
8466 if (rhs != NULL)
8467 {
8468 ga_init(&ga);
8469 ga.ga_itemsize = 1;
8470 ga.ga_growsize = 40;
8471
8472 while (*rhs != NUL)
8473 ga_concat(&ga, str2special(&rhs, FALSE));
8474
8475 ga_append(&ga, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008476 rettv->vval.v_string = (char_u *)ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 }
8478}
8479
8480/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008481 * "map()" function
8482 */
8483 static void
8484f_map(argvars, rettv)
8485 typeval *argvars;
8486 typeval *rettv;
8487{
8488 filter_map(argvars, rettv, TRUE);
8489}
8490
8491/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008492 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 */
8494 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008495f_maparg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008496 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008497 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498{
Bram Moolenaar0d660222005-01-07 21:51:51 +00008499 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500}
8501
8502/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008503 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 */
8505 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008506f_mapcheck(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008507 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008508 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509{
Bram Moolenaar0d660222005-01-07 21:51:51 +00008510 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511}
8512
Bram Moolenaar0d660222005-01-07 21:51:51 +00008513static void find_some_match __ARGS((typeval *argvars, typeval *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514
8515 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008516find_some_match(argvars, rettv, type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008517 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008518 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 int type;
8520{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008521 char_u *str = NULL;
8522 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 char_u *pat;
8524 regmatch_T regmatch;
8525 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008526 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 char_u *save_cpo;
8528 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008529 long nth = 1;
8530 int match;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008531 listvar *l = NULL;
8532 listitem *li = NULL;
8533 long idx = 0;
8534 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535
8536 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
8537 save_cpo = p_cpo;
8538 p_cpo = (char_u *)"";
8539
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 if (type == 2)
8541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008542 rettv->v_type = VAR_STRING;
8543 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544 }
8545 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008546 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008548 if (argvars[0].v_type == VAR_LIST)
8549 {
8550 if ((l = argvars[0].vval.v_list) == NULL)
8551 goto theend;
8552 li = l->lv_first;
8553 }
8554 else
8555 expr = str = get_tv_string(&argvars[0]);
8556
8557 pat = get_tv_string_buf(&argvars[1], patbuf);
8558
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008559 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008561 start = get_tv_number(&argvars[2]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008562 if (l != NULL)
8563 {
8564 li = list_find(l, start);
8565 if (li == NULL)
8566 goto theend;
8567 if (start < 0)
8568 {
8569 listitem *ni;
8570
8571 /* Need to compute the index. */
8572 for (ni = li; ni->li_prev != NULL; ni = ni->li_prev)
8573 ++idx;
8574 }
8575 else
8576 idx = start;
8577 }
8578 else
8579 {
8580 if (start < 0)
8581 start = 0;
8582 if (start > (long)STRLEN(str))
8583 goto theend;
8584 str += start;
8585 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008586
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008587 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008588 nth = get_tv_number(&argvars[3]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 }
8590
8591 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
8592 if (regmatch.regprog != NULL)
8593 {
8594 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008595
8596 while (1)
8597 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008598 if (l != NULL)
8599 {
8600 if (li == NULL)
8601 {
8602 match = FALSE;
8603 break;
8604 }
8605 str = echo_string(&li->li_tv, &tofree, strbuf);
8606 }
8607
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008608 match = vim_regexec_nl(&regmatch, str, (colnr_T)0);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008609
8610 if (l != NULL)
8611 vim_free(tofree);
8612 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008613 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008614 if (l == NULL && !match)
8615 break;
8616
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008617 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008618 if (l != NULL)
8619 {
8620 li = li->li_next;
8621 ++idx;
8622 }
8623 else
8624 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008625#ifdef FEAT_MBYTE
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008626 str = regmatch.startp[0] + mb_ptr2len_check(regmatch.startp[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008627#else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008628 str = regmatch.startp[0] + 1;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008629#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008630 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008631 }
8632
8633 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 {
8635 if (type == 2)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008636 {
8637 if (l != NULL)
8638 copy_tv(&li->li_tv, rettv);
8639 else
8640 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008642 }
8643 else if (l != NULL)
8644 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 else
8646 {
8647 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008648 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 (varnumber_T)(regmatch.startp[0] - str);
8650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008651 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008653 rettv->vval.v_number += str - expr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 }
8655 }
8656 vim_free(regmatch.regprog);
8657 }
8658
8659theend:
8660 p_cpo = save_cpo;
8661}
8662
8663/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008664 * "match()" function
8665 */
8666 static void
8667f_match(argvars, rettv)
8668 typeval *argvars;
8669 typeval *rettv;
8670{
8671 find_some_match(argvars, rettv, 1);
8672}
8673
8674/*
8675 * "matchend()" function
8676 */
8677 static void
8678f_matchend(argvars, rettv)
8679 typeval *argvars;
8680 typeval *rettv;
8681{
8682 find_some_match(argvars, rettv, 0);
8683}
8684
8685/*
8686 * "matchstr()" function
8687 */
8688 static void
8689f_matchstr(argvars, rettv)
8690 typeval *argvars;
8691 typeval *rettv;
8692{
8693 find_some_match(argvars, rettv, 2);
8694}
8695
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008696static void max_min __ARGS((typeval *argvars, typeval *rettv, int domax));
8697
8698 static void
8699max_min(argvars, rettv, domax)
8700 typeval *argvars;
8701 typeval *rettv;
8702 int domax;
8703{
8704 listvar *l;
8705 listitem *li;
8706 long n = 0;
8707 long i;
8708
8709 if (argvars[0].v_type == VAR_LIST)
8710 {
8711 l = argvars[0].vval.v_list;
8712 if (l != NULL)
8713 {
8714 li = l->lv_first;
8715 if (li != NULL)
8716 {
8717 n = get_tv_number(&li->li_tv);
8718 while (1)
8719 {
8720 li = li->li_next;
8721 if (li == NULL)
8722 break;
8723 i = get_tv_number(&li->li_tv);
8724 if (domax ? i > n : i < n)
8725 n = i;
8726 }
8727 }
8728 }
8729 }
8730 else
8731 EMSG(_(e_listreq));
8732 rettv->vval.v_number = n;
8733}
8734
8735/*
8736 * "max()" function
8737 */
8738 static void
8739f_max(argvars, rettv)
8740 typeval *argvars;
8741 typeval *rettv;
8742{
8743 max_min(argvars, rettv, TRUE);
8744}
8745
8746/*
8747 * "min()" function
8748 */
8749 static void
8750f_min(argvars, rettv)
8751 typeval *argvars;
8752 typeval *rettv;
8753{
8754 max_min(argvars, rettv, FALSE);
8755}
8756
Bram Moolenaar0d660222005-01-07 21:51:51 +00008757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 * "mode()" function
8759 */
8760/*ARGSUSED*/
8761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008762f_mode(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008763 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008764 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765{
8766 char_u buf[2];
8767
8768#ifdef FEAT_VISUAL
8769 if (VIsual_active)
8770 {
8771 if (VIsual_select)
8772 buf[0] = VIsual_mode + 's' - 'v';
8773 else
8774 buf[0] = VIsual_mode;
8775 }
8776 else
8777#endif
8778 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE)
8779 buf[0] = 'r';
8780 else if (State & INSERT)
8781 {
8782 if (State & REPLACE_FLAG)
8783 buf[0] = 'R';
8784 else
8785 buf[0] = 'i';
8786 }
8787 else if (State & CMDLINE)
8788 buf[0] = 'c';
8789 else
8790 buf[0] = 'n';
8791
8792 buf[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008793 rettv->vval.v_string = vim_strsave(buf);
8794 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795}
8796
8797/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008798 * "nextnonblank()" function
8799 */
8800 static void
8801f_nextnonblank(argvars, rettv)
8802 typeval *argvars;
8803 typeval *rettv;
8804{
8805 linenr_T lnum;
8806
8807 for (lnum = get_tv_lnum(argvars); ; ++lnum)
8808 {
8809 if (lnum > curbuf->b_ml.ml_line_count)
8810 {
8811 lnum = 0;
8812 break;
8813 }
8814 if (*skipwhite(ml_get(lnum)) != NUL)
8815 break;
8816 }
8817 rettv->vval.v_number = lnum;
8818}
8819
8820/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 * "nr2char()" function
8822 */
8823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008824f_nr2char(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008825 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008826 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827{
8828 char_u buf[NUMBUFLEN];
8829
8830#ifdef FEAT_MBYTE
8831 if (has_mbyte)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008832 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 else
8834#endif
8835 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008836 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 buf[1] = NUL;
8838 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008839 rettv->v_type = VAR_STRING;
8840 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008841}
8842
8843/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008844 * "prevnonblank()" function
8845 */
8846 static void
8847f_prevnonblank(argvars, rettv)
8848 typeval *argvars;
8849 typeval *rettv;
8850{
8851 linenr_T lnum;
8852
8853 lnum = get_tv_lnum(argvars);
8854 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
8855 lnum = 0;
8856 else
8857 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
8858 --lnum;
8859 rettv->vval.v_number = lnum;
8860}
8861
8862#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
8863static void make_connection __ARGS((void));
8864static int check_connection __ARGS((void));
8865
8866 static void
8867make_connection()
8868{
8869 if (X_DISPLAY == NULL
8870# ifdef FEAT_GUI
8871 && !gui.in_use
8872# endif
8873 )
8874 {
8875 x_force_connect = TRUE;
8876 setup_term_clip();
8877 x_force_connect = FALSE;
8878 }
8879}
8880
8881 static int
8882check_connection()
8883{
8884 make_connection();
8885 if (X_DISPLAY == NULL)
8886 {
8887 EMSG(_("E240: No connection to Vim server"));
8888 return FAIL;
8889 }
8890 return OK;
8891}
8892#endif
8893
8894#ifdef FEAT_CLIENTSERVER
8895static void remote_common __ARGS((typeval *argvars, typeval *rettv, int expr));
8896
8897 static void
8898remote_common(argvars, rettv, expr)
8899 typeval *argvars;
8900 typeval *rettv;
8901 int expr;
8902{
8903 char_u *server_name;
8904 char_u *keys;
8905 char_u *r = NULL;
8906 char_u buf[NUMBUFLEN];
8907# ifdef WIN32
8908 HWND w;
8909# else
8910 Window w;
8911# endif
8912
8913 if (check_restricted() || check_secure())
8914 return;
8915
8916# ifdef FEAT_X11
8917 if (check_connection() == FAIL)
8918 return;
8919# endif
8920
8921 server_name = get_tv_string(&argvars[0]);
8922 keys = get_tv_string_buf(&argvars[1], buf);
8923# ifdef WIN32
8924 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
8925# else
8926 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
8927 < 0)
8928# endif
8929 {
8930 if (r != NULL)
8931 EMSG(r); /* sending worked but evaluation failed */
8932 else
8933 EMSG2(_("E241: Unable to send to %s"), server_name);
8934 return;
8935 }
8936
8937 rettv->vval.v_string = r;
8938
8939 if (argvars[2].v_type != VAR_UNKNOWN)
8940 {
8941 var v;
8942 char_u str[30];
8943
8944 sprintf((char *)str, "0x%x", (unsigned int)w);
8945 v.tv.v_type = VAR_STRING;
8946 v.tv.vval.v_string = vim_strsave(str);
8947 set_var(get_tv_string(&argvars[2]), &v.tv, FALSE);
8948 vim_free(v.tv.vval.v_string);
8949 }
8950}
8951#endif
8952
8953/*
8954 * "remote_expr()" function
8955 */
8956/*ARGSUSED*/
8957 static void
8958f_remote_expr(argvars, rettv)
8959 typeval *argvars;
8960 typeval *rettv;
8961{
8962 rettv->v_type = VAR_STRING;
8963 rettv->vval.v_string = NULL;
8964#ifdef FEAT_CLIENTSERVER
8965 remote_common(argvars, rettv, TRUE);
8966#endif
8967}
8968
8969/*
8970 * "remote_foreground()" function
8971 */
8972/*ARGSUSED*/
8973 static void
8974f_remote_foreground(argvars, rettv)
8975 typeval *argvars;
8976 typeval *rettv;
8977{
8978 rettv->vval.v_number = 0;
8979#ifdef FEAT_CLIENTSERVER
8980# ifdef WIN32
8981 /* On Win32 it's done in this application. */
8982 serverForeground(get_tv_string(&argvars[0]));
8983# else
8984 /* Send a foreground() expression to the server. */
8985 argvars[1].v_type = VAR_STRING;
8986 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
8987 argvars[2].v_type = VAR_UNKNOWN;
8988 remote_common(argvars, rettv, TRUE);
8989 vim_free(argvars[1].vval.v_string);
8990# endif
8991#endif
8992}
8993
8994/*ARGSUSED*/
8995 static void
8996f_remote_peek(argvars, rettv)
8997 typeval *argvars;
8998 typeval *rettv;
8999{
9000#ifdef FEAT_CLIENTSERVER
9001 var v;
9002 char_u *s = NULL;
9003# ifdef WIN32
9004 int n = 0;
9005# endif
9006
9007 if (check_restricted() || check_secure())
9008 {
9009 rettv->vval.v_number = -1;
9010 return;
9011 }
9012# ifdef WIN32
9013 sscanf(get_tv_string(&argvars[0]), "%x", &n);
9014 if (n == 0)
9015 rettv->vval.v_number = -1;
9016 else
9017 {
9018 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
9019 rettv->vval.v_number = (s != NULL);
9020 }
9021# else
9022 rettv->vval.v_number = 0;
9023 if (check_connection() == FAIL)
9024 return;
9025
9026 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
9027 serverStrToWin(get_tv_string(&argvars[0])), &s);
9028# endif
9029
9030 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
9031 {
9032 v.tv.v_type = VAR_STRING;
9033 v.tv.vval.v_string = vim_strsave(s);
9034 set_var(get_tv_string(&argvars[1]), &v.tv, FALSE);
9035 vim_free(v.tv.vval.v_string);
9036 }
9037#else
9038 rettv->vval.v_number = -1;
9039#endif
9040}
9041
9042/*ARGSUSED*/
9043 static void
9044f_remote_read(argvars, rettv)
9045 typeval *argvars;
9046 typeval *rettv;
9047{
9048 char_u *r = NULL;
9049
9050#ifdef FEAT_CLIENTSERVER
9051 if (!check_restricted() && !check_secure())
9052 {
9053# ifdef WIN32
9054 /* The server's HWND is encoded in the 'id' parameter */
9055 int n = 0;
9056
9057 sscanf(get_tv_string(&argvars[0]), "%x", &n);
9058 if (n != 0)
9059 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
9060 if (r == NULL)
9061# else
9062 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
9063 serverStrToWin(get_tv_string(&argvars[0])), &r, FALSE) < 0)
9064# endif
9065 EMSG(_("E277: Unable to read a server reply"));
9066 }
9067#endif
9068 rettv->v_type = VAR_STRING;
9069 rettv->vval.v_string = r;
9070}
9071
9072/*
9073 * "remote_send()" function
9074 */
9075/*ARGSUSED*/
9076 static void
9077f_remote_send(argvars, rettv)
9078 typeval *argvars;
9079 typeval *rettv;
9080{
9081 rettv->v_type = VAR_STRING;
9082 rettv->vval.v_string = NULL;
9083#ifdef FEAT_CLIENTSERVER
9084 remote_common(argvars, rettv, FALSE);
9085#endif
9086}
9087
9088/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009089 * "remove({list}, {idx} [, {end}])" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009090 */
9091 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009092f_remove(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009093 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009094 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009095{
9096 listvar *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009097 listitem *item, *item2;
9098 listitem *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009099 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009100 long end;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009101
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009102 rettv->vval.v_number = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009103 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009104 EMSG2(_(e_listarg), "remove()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009105 else if ((l = argvars[0].vval.v_list) != NULL)
9106 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009107 idx = get_tv_number(&argvars[1]);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009108 item = list_find(l, idx);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009109 if (item == NULL)
9110 EMSGN(_(e_listidx), idx);
9111 else
9112 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009113 if (argvars[2].v_type == VAR_UNKNOWN)
9114 {
9115 /* Remove one item, return its value. */
9116 list_getrem(l, item, item);
9117 *rettv = item->li_tv;
9118 vim_free(item);
9119 }
9120 else
9121 {
9122 /* Remove range of items, return list with values. */
9123 end = get_tv_number(&argvars[2]);
9124 item2 = list_find(l, end);
9125 if (item2 == NULL)
9126 EMSGN(_(e_listidx), end);
9127 else
9128 {
9129 for (li = item; li != item2 && li != NULL; li = li->li_next)
9130 ;
9131 if (li == NULL) /* didn't find "item2" after "item" */
9132 EMSG(_(e_invrange));
9133 else
9134 {
9135 list_getrem(l, item, item2);
9136 l = list_alloc();
9137 if (l != NULL)
9138 {
9139 rettv->v_type = VAR_LIST;
9140 rettv->vval.v_list = l;
9141 l->lv_first = item;
9142 l->lv_last = item2;
9143 l->lv_refcount = 1;
9144 item->li_prev = NULL;
9145 item2->li_next = NULL;
9146 }
9147 }
9148 }
9149 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009150 }
9151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152}
9153
9154/*
9155 * "rename({from}, {to})" function
9156 */
9157 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009158f_rename(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009159 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009160 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161{
9162 char_u buf[NUMBUFLEN];
9163
9164 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009165 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009167 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
9168 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169}
9170
9171/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009172 * "repeat()" function
9173 */
9174/*ARGSUSED*/
9175 static void
9176f_repeat(argvars, rettv)
9177 typeval *argvars;
9178 typeval *rettv;
9179{
9180 char_u *p;
9181 int n;
9182 int slen;
9183 int len;
9184 char_u *r;
9185 int i;
9186 listvar *l;
9187
9188 n = get_tv_number(&argvars[1]);
9189 if (argvars[0].v_type == VAR_LIST)
9190 {
9191 l = list_alloc();
9192 if (l != NULL && argvars[0].vval.v_list != NULL)
9193 {
9194 l->lv_refcount = 1;
9195 while (n-- > 0)
9196 if (list_extend(l, argvars[0].vval.v_list, NULL) == FAIL)
9197 break;
9198 }
9199 rettv->v_type = VAR_LIST;
9200 rettv->vval.v_list = l;
9201 }
9202 else
9203 {
9204 p = get_tv_string(&argvars[0]);
9205 rettv->v_type = VAR_STRING;
9206 rettv->vval.v_string = NULL;
9207
9208 slen = (int)STRLEN(p);
9209 len = slen * n;
9210 if (len <= 0)
9211 return;
9212
9213 r = alloc(len + 1);
9214 if (r != NULL)
9215 {
9216 for (i = 0; i < n; i++)
9217 mch_memmove(r + i * slen, p, (size_t)slen);
9218 r[len] = NUL;
9219 }
9220
9221 rettv->vval.v_string = r;
9222 }
9223}
9224
9225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 * "resolve()" function
9227 */
9228 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009229f_resolve(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009230 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009231 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232{
9233 char_u *p;
9234
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009235 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236#ifdef FEAT_SHORTCUT
9237 {
9238 char_u *v = NULL;
9239
9240 v = mch_resolve_shortcut(p);
9241 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009242 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009244 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245 }
9246#else
9247# ifdef HAVE_READLINK
9248 {
9249 char_u buf[MAXPATHL + 1];
9250 char_u *cpy;
9251 int len;
9252 char_u *remain = NULL;
9253 char_u *q;
9254 int is_relative_to_current = FALSE;
9255 int has_trailing_pathsep = FALSE;
9256 int limit = 100;
9257
9258 p = vim_strsave(p);
9259
9260 if (p[0] == '.' && (vim_ispathsep(p[1])
9261 || (p[1] == '.' && (vim_ispathsep(p[2])))))
9262 is_relative_to_current = TRUE;
9263
9264 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009265 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 has_trailing_pathsep = TRUE;
9267
9268 q = getnextcomp(p);
9269 if (*q != NUL)
9270 {
9271 /* Separate the first path component in "p", and keep the
9272 * remainder (beginning with the path separator). */
9273 remain = vim_strsave(q - 1);
9274 q[-1] = NUL;
9275 }
9276
9277 for (;;)
9278 {
9279 for (;;)
9280 {
9281 len = readlink((char *)p, (char *)buf, MAXPATHL);
9282 if (len <= 0)
9283 break;
9284 buf[len] = NUL;
9285
9286 if (limit-- == 0)
9287 {
9288 vim_free(p);
9289 vim_free(remain);
9290 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009291 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 goto fail;
9293 }
9294
9295 /* Ensure that the result will have a trailing path separator
9296 * if the argument has one. */
9297 if (remain == NULL && has_trailing_pathsep)
9298 add_pathsep(buf);
9299
9300 /* Separate the first path component in the link value and
9301 * concatenate the remainders. */
9302 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
9303 if (*q != NUL)
9304 {
9305 if (remain == NULL)
9306 remain = vim_strsave(q - 1);
9307 else
9308 {
9309 cpy = vim_strnsave(q-1, STRLEN(q-1)+STRLEN(remain));
9310 if (cpy != NULL)
9311 {
9312 STRCAT(cpy, remain);
9313 vim_free(remain);
9314 remain = cpy;
9315 }
9316 }
9317 q[-1] = NUL;
9318 }
9319
9320 q = gettail(p);
9321 if (q > p && *q == NUL)
9322 {
9323 /* Ignore trailing path separator. */
9324 q[-1] = NUL;
9325 q = gettail(p);
9326 }
9327 if (q > p && !mch_isFullName(buf))
9328 {
9329 /* symlink is relative to directory of argument */
9330 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
9331 if (cpy != NULL)
9332 {
9333 STRCPY(cpy, p);
9334 STRCPY(gettail(cpy), buf);
9335 vim_free(p);
9336 p = cpy;
9337 }
9338 }
9339 else
9340 {
9341 vim_free(p);
9342 p = vim_strsave(buf);
9343 }
9344 }
9345
9346 if (remain == NULL)
9347 break;
9348
9349 /* Append the first path component of "remain" to "p". */
9350 q = getnextcomp(remain + 1);
9351 len = q - remain - (*q != NUL);
9352 cpy = vim_strnsave(p, STRLEN(p) + len);
9353 if (cpy != NULL)
9354 {
9355 STRNCAT(cpy, remain, len);
9356 vim_free(p);
9357 p = cpy;
9358 }
9359 /* Shorten "remain". */
9360 if (*q != NUL)
9361 STRCPY(remain, q - 1);
9362 else
9363 {
9364 vim_free(remain);
9365 remain = NULL;
9366 }
9367 }
9368
9369 /* If the result is a relative path name, make it explicitly relative to
9370 * the current directory if and only if the argument had this form. */
9371 if (!vim_ispathsep(*p))
9372 {
9373 if (is_relative_to_current
9374 && *p != NUL
9375 && !(p[0] == '.'
9376 && (p[1] == NUL
9377 || vim_ispathsep(p[1])
9378 || (p[1] == '.'
9379 && (p[2] == NUL
9380 || vim_ispathsep(p[2]))))))
9381 {
9382 /* Prepend "./". */
9383 cpy = vim_strnsave((char_u *)"./", 2 + STRLEN(p));
9384 if (cpy != NULL)
9385 {
9386 STRCAT(cpy, p);
9387 vim_free(p);
9388 p = cpy;
9389 }
9390 }
9391 else if (!is_relative_to_current)
9392 {
9393 /* Strip leading "./". */
9394 q = p;
9395 while (q[0] == '.' && vim_ispathsep(q[1]))
9396 q += 2;
9397 if (q > p)
9398 mch_memmove(p, p + 2, STRLEN(p + 2) + (size_t)1);
9399 }
9400 }
9401
9402 /* Ensure that the result will have no trailing path separator
9403 * if the argument had none. But keep "/" or "//". */
9404 if (!has_trailing_pathsep)
9405 {
9406 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00009407 if (after_pathsep(p, q))
9408 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 }
9410
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009411 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009412 }
9413# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009414 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415# endif
9416#endif
9417
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009418 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419
9420#ifdef HAVE_READLINK
9421fail:
9422#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009423 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424}
9425
9426/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009427 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009428 */
9429 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009430f_reverse(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009431 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009432 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009434 listvar *l;
9435 listitem *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436
Bram Moolenaar0d660222005-01-07 21:51:51 +00009437 rettv->vval.v_number = 0;
9438 if (argvars[0].v_type != VAR_LIST)
9439 EMSG2(_(e_listarg), "reverse()");
9440 else if ((l = argvars[0].vval.v_list) != NULL)
9441 {
9442 li = l->lv_last;
9443 l->lv_first = l->lv_last = li;
9444 while (li != NULL)
9445 {
9446 ni = li->li_prev;
9447 list_append(l, li);
9448 li = ni;
9449 }
9450 rettv->vval.v_list = l;
9451 rettv->v_type = VAR_LIST;
9452 ++l->lv_refcount;
9453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454}
9455
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009456#define SP_NOMOVE 1 /* don't move cursor */
9457#define SP_REPEAT 2 /* repeat to find outer pair */
9458#define SP_RETCOUNT 4 /* return matchcount */
9459
Bram Moolenaar0d660222005-01-07 21:51:51 +00009460static int get_search_arg __ARGS((typeval *varp, int *flagsp));
9461
9462/*
9463 * Get flags for a search function.
9464 * Possibly sets "p_ws".
9465 * Returns BACKWARD, FORWARD or zero (for an error).
9466 */
9467 static int
9468get_search_arg(varp, flagsp)
9469 typeval *varp;
9470 int *flagsp;
9471{
9472 int dir = FORWARD;
9473 char_u *flags;
9474 char_u nbuf[NUMBUFLEN];
9475 int mask;
9476
9477 if (varp->v_type != VAR_UNKNOWN)
9478 {
9479 flags = get_tv_string_buf(varp, nbuf);
9480 while (*flags != NUL)
9481 {
9482 switch (*flags)
9483 {
9484 case 'b': dir = BACKWARD; break;
9485 case 'w': p_ws = TRUE; break;
9486 case 'W': p_ws = FALSE; break;
9487 default: mask = 0;
9488 if (flagsp != NULL)
9489 switch (*flags)
9490 {
9491 case 'n': mask = SP_NOMOVE; break;
9492 case 'r': mask = SP_REPEAT; break;
9493 case 'm': mask = SP_RETCOUNT; break;
9494 }
9495 if (mask == 0)
9496 {
9497 EMSG2(_(e_invarg2), flags);
9498 dir = 0;
9499 }
9500 else
9501 *flagsp |= mask;
9502 }
9503 if (dir == 0)
9504 break;
9505 ++flags;
9506 }
9507 }
9508 return dir;
9509}
9510
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511/*
9512 * "search()" function
9513 */
9514 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009515f_search(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009516 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009517 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009518{
9519 char_u *pat;
9520 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009521 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009522 int save_p_ws = p_ws;
9523 int dir;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009524 int flags = 0;
9525
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009526 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009528 pat = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009529 dir = get_search_arg(&argvars[1], &flags); /* may set p_ws */
9530 if (dir == 0)
9531 goto theend;
9532 if ((flags & ~SP_NOMOVE) != 0)
9533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009534 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009535 goto theend;
9536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009538 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009539 if (searchit(curwin, curbuf, &pos, dir, pat, 1L,
9540 SEARCH_KEEP, RE_SEARCH) != FAIL)
9541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009542 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 curwin->w_cursor = pos;
9544 /* "/$" will put the cursor after the end of the line, may need to
9545 * correct that here */
9546 check_cursor();
9547 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009548
9549 /* If 'n' flag is used: restore cursor position. */
9550 if (flags & SP_NOMOVE)
9551 curwin->w_cursor = save_cursor;
9552theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553 p_ws = save_p_ws;
9554}
9555
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556/*
9557 * "searchpair()" function
9558 */
9559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560f_searchpair(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009561 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009562 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009563{
9564 char_u *spat, *mpat, *epat;
9565 char_u *skip;
9566 char_u *pat, *pat2, *pat3;
9567 pos_T pos;
9568 pos_T firstpos;
9569 pos_T save_cursor;
9570 pos_T save_pos;
9571 int save_p_ws = p_ws;
9572 char_u *save_cpo;
9573 int dir;
9574 int flags = 0;
9575 char_u nbuf1[NUMBUFLEN];
9576 char_u nbuf2[NUMBUFLEN];
9577 char_u nbuf3[NUMBUFLEN];
9578 int n;
9579 int r;
9580 int nest = 1;
9581 int err;
9582
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009583 rettv->vval.v_number = 0; /* default: FAIL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584
9585 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
9586 save_cpo = p_cpo;
9587 p_cpo = (char_u *)"";
9588
9589 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009590 spat = get_tv_string(&argvars[0]);
9591 mpat = get_tv_string_buf(&argvars[1], nbuf1);
9592 epat = get_tv_string_buf(&argvars[2], nbuf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593
9594 /* Make two search patterns: start/end (pat2, for in nested pairs) and
9595 * start/middle/end (pat3, for the top pair). */
9596 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
9597 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
9598 if (pat2 == NULL || pat3 == NULL)
9599 goto theend;
9600 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
9601 if (*mpat == NUL)
9602 STRCPY(pat3, pat2);
9603 else
9604 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
9605 spat, epat, mpat);
9606
9607 /* Handle the optional fourth argument: flags */
9608 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00009609 if (dir == 0)
9610 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611
9612 /* Optional fifth argument: skip expresion */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009613 if (argvars[3].v_type == VAR_UNKNOWN
9614 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 skip = (char_u *)"";
9616 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009617 skip = get_tv_string_buf(&argvars[4], nbuf3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009618
9619 save_cursor = curwin->w_cursor;
9620 pos = curwin->w_cursor;
9621 firstpos.lnum = 0;
9622 pat = pat3;
9623 for (;;)
9624 {
9625 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
9626 SEARCH_KEEP, RE_SEARCH);
9627 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
9628 /* didn't find it or found the first match again: FAIL */
9629 break;
9630
9631 if (firstpos.lnum == 0)
9632 firstpos = pos;
9633
9634 /* If the skip pattern matches, ignore this match. */
9635 if (*skip != NUL)
9636 {
9637 save_pos = curwin->w_cursor;
9638 curwin->w_cursor = pos;
9639 r = eval_to_bool(skip, &err, NULL, FALSE);
9640 curwin->w_cursor = save_pos;
9641 if (err)
9642 {
9643 /* Evaluating {skip} caused an error, break here. */
9644 curwin->w_cursor = save_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009645 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 break;
9647 }
9648 if (r)
9649 continue;
9650 }
9651
9652 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
9653 {
9654 /* Found end when searching backwards or start when searching
9655 * forward: nested pair. */
9656 ++nest;
9657 pat = pat2; /* nested, don't search for middle */
9658 }
9659 else
9660 {
9661 /* Found end when searching forward or start when searching
9662 * backward: end of (nested) pair; or found middle in outer pair. */
9663 if (--nest == 1)
9664 pat = pat3; /* outer level, search for middle */
9665 }
9666
9667 if (nest == 0)
9668 {
9669 /* Found the match: return matchcount or line number. */
9670 if (flags & SP_RETCOUNT)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009671 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009673 rettv->vval.v_number = pos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674 curwin->w_cursor = pos;
9675 if (!(flags & SP_REPEAT))
9676 break;
9677 nest = 1; /* search for next unmatched */
9678 }
9679 }
9680
9681 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009682 if ((flags & SP_NOMOVE) || rettv->vval.v_number == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009683 curwin->w_cursor = save_cursor;
9684
9685theend:
9686 vim_free(pat2);
9687 vim_free(pat3);
9688 p_ws = save_p_ws;
9689 p_cpo = save_cpo;
9690}
9691
Bram Moolenaar0d660222005-01-07 21:51:51 +00009692/*ARGSUSED*/
9693 static void
9694f_server2client(argvars, rettv)
9695 typeval *argvars;
9696 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009698#ifdef FEAT_CLIENTSERVER
9699 char_u buf[NUMBUFLEN];
9700 char_u *server = get_tv_string(&argvars[0]);
9701 char_u *reply = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702
Bram Moolenaar0d660222005-01-07 21:51:51 +00009703 rettv->vval.v_number = -1;
9704 if (check_restricted() || check_secure())
9705 return;
9706# ifdef FEAT_X11
9707 if (check_connection() == FAIL)
9708 return;
9709# endif
9710
9711 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009713 EMSG(_("E258: Unable to send to client"));
9714 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009716 rettv->vval.v_number = 0;
9717#else
9718 rettv->vval.v_number = -1;
9719#endif
9720}
9721
9722/*ARGSUSED*/
9723 static void
9724f_serverlist(argvars, rettv)
9725 typeval *argvars;
9726 typeval *rettv;
9727{
9728 char_u *r = NULL;
9729
9730#ifdef FEAT_CLIENTSERVER
9731# ifdef WIN32
9732 r = serverGetVimNames();
9733# else
9734 make_connection();
9735 if (X_DISPLAY != NULL)
9736 r = serverGetVimNames(X_DISPLAY);
9737# endif
9738#endif
9739 rettv->v_type = VAR_STRING;
9740 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741}
9742
9743/*
9744 * "setbufvar()" function
9745 */
9746/*ARGSUSED*/
9747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009748f_setbufvar(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 buf_T *buf;
9753#ifdef FEAT_AUTOCMD
9754 aco_save_T aco;
9755#else
9756 buf_T *save_curbuf;
9757#endif
9758 char_u *varname, *bufvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009759 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 char_u nbuf[NUMBUFLEN];
9761
9762 if (check_restricted() || check_secure())
9763 return;
9764 ++emsg_off;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009765 buf = get_buf_tv(&argvars[0]);
9766 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 varp = &argvars[2];
9768
9769 if (buf != NULL && varname != NULL && varp != NULL)
9770 {
9771 /* set curbuf to be our buf, temporarily */
9772#ifdef FEAT_AUTOCMD
9773 aucmd_prepbuf(&aco, buf);
9774#else
9775 save_curbuf = curbuf;
9776 curbuf = buf;
9777#endif
9778
9779 if (*varname == '&')
9780 {
9781 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009782 set_option_value(varname, get_tv_number(varp),
9783 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 }
9785 else
9786 {
9787 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
9788 if (bufvarname != NULL)
9789 {
9790 STRCPY(bufvarname, "b:");
9791 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00009792 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 vim_free(bufvarname);
9794 }
9795 }
9796
9797 /* reset notion of buffer */
9798#ifdef FEAT_AUTOCMD
9799 aucmd_restbuf(&aco);
9800#else
9801 curbuf = save_curbuf;
9802#endif
9803 }
9804 --emsg_off;
9805}
9806
9807/*
9808 * "setcmdpos()" function
9809 */
9810 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009811f_setcmdpos(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009812 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009813 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009815 rettv->vval.v_number = set_cmdline_pos(
9816 (int)get_tv_number(&argvars[0]) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817}
9818
9819/*
9820 * "setline()" function
9821 */
9822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009823f_setline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009824 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826{
9827 linenr_T lnum;
9828 char_u *line;
9829
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009830 lnum = get_tv_lnum(argvars);
9831 line = get_tv_string(&argvars[1]);
9832 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833
9834 if (lnum >= 1
9835 && lnum <= curbuf->b_ml.ml_line_count
9836 && u_savesub(lnum) == OK
9837 && ml_replace(lnum, line, TRUE) == OK)
9838 {
9839 changed_bytes(lnum, 0);
9840 check_cursor_col();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009841 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 }
9843}
9844
9845/*
9846 * "setreg()" function
9847 */
9848 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009849f_setreg(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009850 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009851 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852{
9853 int regname;
9854 char_u *strregname;
9855 char_u *stropt;
9856 int append;
9857 char_u yank_type;
9858 long block_len;
9859
9860 block_len = -1;
9861 yank_type = MAUTO;
9862 append = FALSE;
9863
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009864 strregname = get_tv_string(argvars);
9865 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866
9867 regname = (strregname == NULL ? '"' : *strregname);
9868 if (regname == 0 || regname == '@')
9869 regname = '"';
9870 else if (regname == '=')
9871 return;
9872
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009873 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009875 for (stropt = get_tv_string(&argvars[2]); *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009876 switch (*stropt)
9877 {
9878 case 'a': case 'A': /* append */
9879 append = TRUE;
9880 break;
9881 case 'v': case 'c': /* character-wise selection */
9882 yank_type = MCHAR;
9883 break;
9884 case 'V': case 'l': /* line-wise selection */
9885 yank_type = MLINE;
9886 break;
9887#ifdef FEAT_VISUAL
9888 case 'b': case Ctrl_V: /* block-wise selection */
9889 yank_type = MBLOCK;
9890 if (VIM_ISDIGIT(stropt[1]))
9891 {
9892 ++stropt;
9893 block_len = getdigits(&stropt) - 1;
9894 --stropt;
9895 }
9896 break;
9897#endif
9898 }
9899 }
9900
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009901 write_reg_contents_ex(regname, get_tv_string(&argvars[1]), -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009903 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904}
9905
9906
9907/*
9908 * "setwinvar(expr)" function
9909 */
9910/*ARGSUSED*/
9911 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009912f_setwinvar(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009913 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009914 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915{
9916 win_T *win;
9917#ifdef FEAT_WINDOWS
9918 win_T *save_curwin;
9919#endif
9920 char_u *varname, *winvarname;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009921 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922 char_u nbuf[NUMBUFLEN];
9923
9924 if (check_restricted() || check_secure())
9925 return;
9926 ++emsg_off;
9927 win = find_win_by_nr(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009928 varname = get_tv_string(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929 varp = &argvars[2];
9930
9931 if (win != NULL && varname != NULL && varp != NULL)
9932 {
9933#ifdef FEAT_WINDOWS
9934 /* set curwin to be our win, temporarily */
9935 save_curwin = curwin;
9936 curwin = win;
9937 curbuf = curwin->w_buffer;
9938#endif
9939
9940 if (*varname == '&')
9941 {
9942 ++varname;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009943 set_option_value(varname, get_tv_number(varp),
9944 get_tv_string_buf(varp, nbuf), OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945 }
9946 else
9947 {
9948 winvarname = alloc((unsigned)STRLEN(varname) + 3);
9949 if (winvarname != NULL)
9950 {
9951 STRCPY(winvarname, "w:");
9952 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00009953 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 vim_free(winvarname);
9955 }
9956 }
9957
9958#ifdef FEAT_WINDOWS
9959 /* Restore current window, if it's still valid (autocomands can make
9960 * it invalid). */
9961 if (win_valid(save_curwin))
9962 {
9963 curwin = save_curwin;
9964 curbuf = curwin->w_buffer;
9965 }
9966#endif
9967 }
9968 --emsg_off;
9969}
9970
9971/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009972 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 */
9974 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00009975f_simplify(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009976 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009977 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978{
Bram Moolenaar0d660222005-01-07 21:51:51 +00009979 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980
Bram Moolenaar0d660222005-01-07 21:51:51 +00009981 p = get_tv_string(&argvars[0]);
9982 rettv->vval.v_string = vim_strsave(p);
9983 simplify_filename(rettv->vval.v_string); /* simplify in place */
9984 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985}
9986
Bram Moolenaar0d660222005-01-07 21:51:51 +00009987static int
9988#ifdef __BORLANDC__
9989 _RTLENTRYF
9990#endif
9991 item_compare __ARGS((const void *s1, const void *s2));
9992static int
9993#ifdef __BORLANDC__
9994 _RTLENTRYF
9995#endif
9996 item_compare2 __ARGS((const void *s1, const void *s2));
9997
9998static int item_compare_ic;
9999static char_u *item_compare_func;
10000#define ITEM_COMPARE_FAIL 999
10001
Bram Moolenaar071d4272004-06-13 20:20:40 +000010002/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010003 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000010005 static int
10006#ifdef __BORLANDC__
10007_RTLENTRYF
10008#endif
10009item_compare(s1, s2)
10010 const void *s1;
10011 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010013 char_u *p1, *p2;
10014 char_u *tofree1, *tofree2;
10015 int res;
10016 char_u numbuf1[NUMBUFLEN];
10017 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018
Bram Moolenaar0d660222005-01-07 21:51:51 +000010019 p1 = tv2string(&(*(listitem **)s1)->li_tv, &tofree1, numbuf1);
10020 p2 = tv2string(&(*(listitem **)s2)->li_tv, &tofree2, numbuf2);
10021 if (item_compare_ic)
10022 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000010024 res = STRCMP(p1, p2);
10025 vim_free(tofree1);
10026 vim_free(tofree2);
10027 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028}
10029
10030 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000010031#ifdef __BORLANDC__
10032_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000010033#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000010034item_compare2(s1, s2)
10035 const void *s1;
10036 const void *s2;
10037{
10038 int res;
10039 typeval rettv;
10040 typeval argv[2];
10041 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042
Bram Moolenaar0d660222005-01-07 21:51:51 +000010043 /* copy the values (is this really needed?) */
10044 copy_tv(&(*(listitem **)s1)->li_tv, &argv[0]);
10045 copy_tv(&(*(listitem **)s2)->li_tv, &argv[1]);
10046
10047 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
10048 res = call_func(item_compare_func, STRLEN(item_compare_func),
10049 &rettv, 2, argv, 0L, 0L, &dummy, TRUE);
10050 clear_tv(&argv[0]);
10051 clear_tv(&argv[1]);
10052
10053 if (res == FAIL)
10054 res = ITEM_COMPARE_FAIL;
10055 else
10056 res = get_tv_number(&rettv);
10057 clear_tv(&rettv);
10058 return res;
10059}
10060
10061/*
10062 * "sort({list})" function
10063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000010065f_sort(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010066 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010067 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068{
Bram Moolenaar0d660222005-01-07 21:51:51 +000010069 listvar *l;
10070 listitem *li;
10071 listitem **ptrs;
10072 long len;
10073 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074
Bram Moolenaar0d660222005-01-07 21:51:51 +000010075 rettv->vval.v_number = 0;
10076 if (argvars[0].v_type != VAR_LIST)
10077 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 else
10079 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010080 l = argvars[0].vval.v_list;
10081 if (l == NULL)
10082 return;
10083 rettv->vval.v_list = l;
10084 rettv->v_type = VAR_LIST;
10085 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086
Bram Moolenaar0d660222005-01-07 21:51:51 +000010087 len = list_len(l);
10088 if (len <= 1)
10089 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010090
Bram Moolenaar0d660222005-01-07 21:51:51 +000010091 item_compare_ic = FALSE;
10092 item_compare_func = NULL;
10093 if (argvars[1].v_type != VAR_UNKNOWN)
10094 {
10095 if (argvars[1].v_type == VAR_FUNC)
10096 item_compare_func = argvars[0].vval.v_string;
10097 else
10098 {
10099 i = get_tv_number(&argvars[1]);
10100 if (i == 1)
10101 item_compare_ic = TRUE;
10102 else
10103 item_compare_func = get_tv_string(&argvars[1]);
10104 }
10105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106
Bram Moolenaar0d660222005-01-07 21:51:51 +000010107 /* Make an array with each entry pointing to an item in the List. */
10108 ptrs = (listitem **)alloc((int)(len * sizeof(listitem *)));
10109 if (ptrs == NULL)
10110 return;
10111 i = 0;
10112 for (li = l->lv_first; li != NULL; li = li->li_next)
10113 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114
Bram Moolenaar0d660222005-01-07 21:51:51 +000010115 /* test the compare function */
10116 if (item_compare_func != NULL
10117 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
10118 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010119 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000010121 {
10122 /* Sort the array with item pointers. */
10123 qsort((void *)ptrs, (size_t)len, sizeof(listitem *),
10124 item_compare_func == NULL ? item_compare : item_compare2);
10125
10126 /* Clear the List and append the items in the sorted order. */
10127 l->lv_first = l->lv_last = NULL;
10128 for (i = 0; i < len; ++i)
10129 list_append(l, ptrs[i]);
10130 }
10131
10132 vim_free(ptrs);
10133 }
10134}
10135
10136 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010137f_split(argvars, rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000010138 typeval *argvars;
10139 typeval *rettv;
10140{
10141 char_u *str;
10142 char_u *end;
10143 char_u *pat;
10144 regmatch_T regmatch;
10145 char_u patbuf[NUMBUFLEN];
10146 char_u *save_cpo;
10147 int match;
10148 listitem *ni;
10149 listvar *l;
10150 colnr_T col = 0;
10151
10152 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
10153 save_cpo = p_cpo;
10154 p_cpo = (char_u *)"";
10155
10156 str = get_tv_string(&argvars[0]);
10157 if (argvars[1].v_type == VAR_UNKNOWN)
10158 pat = (char_u *)"[\\x01- ]\\+";
10159 else
10160 pat = get_tv_string_buf(&argvars[1], patbuf);
10161
10162 l = list_alloc();
10163 if (l == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010164 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010165 rettv->v_type = VAR_LIST;
10166 rettv->vval.v_list = l;
10167 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168
Bram Moolenaar0d660222005-01-07 21:51:51 +000010169 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10170 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010171 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000010172 regmatch.rm_ic = FALSE;
10173 while (*str != NUL)
10174 {
10175 match = vim_regexec_nl(&regmatch, str, col);
10176 if (match)
10177 end = regmatch.startp[0];
10178 else
10179 end = str + STRLEN(str);
10180 if (end > str)
10181 {
10182 ni = listitem_alloc();
10183 if (ni == NULL)
10184 break;
10185 ni->li_tv.v_type = VAR_STRING;
10186 ni->li_tv.vval.v_string = vim_strnsave(str, end - str);
10187 list_append(l, ni);
10188 }
10189 if (!match)
10190 break;
10191 /* Advance to just after the match. */
10192 if (regmatch.endp[0] > str)
10193 col = 0;
10194 else
10195 {
10196 /* Don't get stuck at the same match. */
10197#ifdef FEAT_MBYTE
10198 col = mb_ptr2len_check(regmatch.endp[0]);
10199#else
10200 col = 1;
10201#endif
10202 }
10203 str = regmatch.endp[0];
10204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205
Bram Moolenaar0d660222005-01-07 21:51:51 +000010206 vim_free(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208
Bram Moolenaar0d660222005-01-07 21:51:51 +000010209 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210}
10211
10212#ifdef HAVE_STRFTIME
10213/*
10214 * "strftime({format}[, {time}])" function
10215 */
10216 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010217f_strftime(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010218 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010219 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220{
10221 char_u result_buf[256];
10222 struct tm *curtime;
10223 time_t seconds;
10224 char_u *p;
10225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010226 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010228 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010229 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 seconds = time(NULL);
10231 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010232 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 curtime = localtime(&seconds);
10234 /* MSVC returns NULL for an invalid value of seconds. */
10235 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010236 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010237 else
10238 {
10239# ifdef FEAT_MBYTE
10240 vimconv_T conv;
10241 char_u *enc;
10242
10243 conv.vc_type = CONV_NONE;
10244 enc = enc_locale();
10245 convert_setup(&conv, p_enc, enc);
10246 if (conv.vc_type != CONV_NONE)
10247 p = string_convert(&conv, p, NULL);
10248# endif
10249 if (p != NULL)
10250 (void)strftime((char *)result_buf, sizeof(result_buf),
10251 (char *)p, curtime);
10252 else
10253 result_buf[0] = NUL;
10254
10255# ifdef FEAT_MBYTE
10256 if (conv.vc_type != CONV_NONE)
10257 vim_free(p);
10258 convert_setup(&conv, enc, p_enc);
10259 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010260 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261 else
10262# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010263 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264
10265# ifdef FEAT_MBYTE
10266 /* Release conversion descriptors */
10267 convert_setup(&conv, NULL, NULL);
10268 vim_free(enc);
10269# endif
10270 }
10271}
10272#endif
10273
10274/*
10275 * "stridx()" function
10276 */
10277 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010278f_stridx(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010279 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010280 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281{
10282 char_u buf[NUMBUFLEN];
10283 char_u *needle;
10284 char_u *haystack;
10285 char_u *pos;
10286
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010287 needle = get_tv_string(&argvars[1]);
10288 haystack = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 pos = (char_u *)strstr((char *)haystack, (char *)needle);
10290
10291 if (pos == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010292 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010294 rettv->vval.v_number = (varnumber_T) (pos - haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295}
10296
10297/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010298 * "string()" function
10299 */
10300 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010301f_string(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010302 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010303 typeval *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010304{
10305 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010306 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010307
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010308 rettv->v_type = VAR_STRING;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010309 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010310 if (tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010311 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312}
10313
10314/*
10315 * "strlen()" function
10316 */
10317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010318f_strlen(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010319 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010320 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010322 rettv->vval.v_number = (varnumber_T)(STRLEN(
10323 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324}
10325
10326/*
10327 * "strpart()" function
10328 */
10329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010330f_strpart(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010331 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010332 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333{
10334 char_u *p;
10335 int n;
10336 int len;
10337 int slen;
10338
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010339 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010340 slen = (int)STRLEN(p);
10341
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010342 n = get_tv_number(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010343 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010344 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345 else
10346 len = slen - n; /* default len: all bytes that are available. */
10347
10348 /*
10349 * Only return the overlap between the specified part and the actual
10350 * string.
10351 */
10352 if (n < 0)
10353 {
10354 len += n;
10355 n = 0;
10356 }
10357 else if (n > slen)
10358 n = slen;
10359 if (len < 0)
10360 len = 0;
10361 else if (n + len > slen)
10362 len = slen - n;
10363
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010364 rettv->v_type = VAR_STRING;
10365 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010366}
10367
10368/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010369 * "strridx()" function
10370 */
10371 static void
10372f_strridx(argvars, rettv)
10373 typeval *argvars;
10374 typeval *rettv;
10375{
10376 char_u buf[NUMBUFLEN];
10377 char_u *needle;
10378 char_u *haystack;
10379 char_u *rest;
10380 char_u *lastmatch = NULL;
10381
10382 needle = get_tv_string(&argvars[1]);
10383 haystack = get_tv_string_buf(&argvars[0], buf);
10384 if (*needle == NUL)
10385 /* Empty string matches past the end. */
10386 lastmatch = haystack + STRLEN(haystack);
10387 else
10388 for (rest = haystack; *rest != '\0'; ++rest)
10389 {
10390 rest = (char_u *)strstr((char *)rest, (char *)needle);
10391 if (rest == NULL)
10392 break;
10393 lastmatch = rest;
10394 }
10395
10396 if (lastmatch == NULL)
10397 rettv->vval.v_number = -1;
10398 else
10399 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
10400}
10401
10402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 * "strtrans()" function
10404 */
10405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010406f_strtrans(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010407 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010408 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010410 rettv->v_type = VAR_STRING;
10411 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010412}
10413
10414/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010415 * "submatch()" function
10416 */
10417 static void
10418f_submatch(argvars, rettv)
10419 typeval *argvars;
10420 typeval *rettv;
10421{
10422 rettv->v_type = VAR_STRING;
10423 rettv->vval.v_string = reg_submatch((int)get_tv_number(&argvars[0]));
10424}
10425
10426/*
10427 * "substitute()" function
10428 */
10429 static void
10430f_substitute(argvars, rettv)
10431 typeval *argvars;
10432 typeval *rettv;
10433{
10434 char_u patbuf[NUMBUFLEN];
10435 char_u subbuf[NUMBUFLEN];
10436 char_u flagsbuf[NUMBUFLEN];
10437
10438 rettv->v_type = VAR_STRING;
10439 rettv->vval.v_string = do_string_sub(
10440 get_tv_string(&argvars[0]),
10441 get_tv_string_buf(&argvars[1], patbuf),
10442 get_tv_string_buf(&argvars[2], subbuf),
10443 get_tv_string_buf(&argvars[3], flagsbuf));
10444}
10445
10446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 * "synID(line, col, trans)" function
10448 */
10449/*ARGSUSED*/
10450 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010451f_synID(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010452 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010453 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454{
10455 int id = 0;
10456#ifdef FEAT_SYN_HL
10457 long line;
10458 long col;
10459 int trans;
10460
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010461 line = get_tv_lnum(argvars);
10462 col = get_tv_number(&argvars[1]) - 1;
10463 trans = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464
10465 if (line >= 1 && line <= curbuf->b_ml.ml_line_count
10466 && col >= 0 && col < (long)STRLEN(ml_get(line)))
10467 id = syn_get_id(line, col, trans);
10468#endif
10469
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010470 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471}
10472
10473/*
10474 * "synIDattr(id, what [, mode])" function
10475 */
10476/*ARGSUSED*/
10477 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010478f_synIDattr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010479 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010480 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481{
10482 char_u *p = NULL;
10483#ifdef FEAT_SYN_HL
10484 int id;
10485 char_u *what;
10486 char_u *mode;
10487 char_u modebuf[NUMBUFLEN];
10488 int modec;
10489
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010490 id = get_tv_number(&argvars[0]);
10491 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010492 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010493 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010494 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 modec = TOLOWER_ASC(mode[0]);
10496 if (modec != 't' && modec != 'c'
10497#ifdef FEAT_GUI
10498 && modec != 'g'
10499#endif
10500 )
10501 modec = 0; /* replace invalid with current */
10502 }
10503 else
10504 {
10505#ifdef FEAT_GUI
10506 if (gui.in_use)
10507 modec = 'g';
10508 else
10509#endif
10510 if (t_colors > 1)
10511 modec = 'c';
10512 else
10513 modec = 't';
10514 }
10515
10516
10517 switch (TOLOWER_ASC(what[0]))
10518 {
10519 case 'b':
10520 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
10521 p = highlight_color(id, what, modec);
10522 else /* bold */
10523 p = highlight_has_attr(id, HL_BOLD, modec);
10524 break;
10525
10526 case 'f': /* fg[#] */
10527 p = highlight_color(id, what, modec);
10528 break;
10529
10530 case 'i':
10531 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
10532 p = highlight_has_attr(id, HL_INVERSE, modec);
10533 else /* italic */
10534 p = highlight_has_attr(id, HL_ITALIC, modec);
10535 break;
10536
10537 case 'n': /* name */
10538 p = get_highlight_name(NULL, id - 1);
10539 break;
10540
10541 case 'r': /* reverse */
10542 p = highlight_has_attr(id, HL_INVERSE, modec);
10543 break;
10544
10545 case 's': /* standout */
10546 p = highlight_has_attr(id, HL_STANDOUT, modec);
10547 break;
10548
10549 case 'u': /* underline */
10550 p = highlight_has_attr(id, HL_UNDERLINE, modec);
10551 break;
10552 }
10553
10554 if (p != NULL)
10555 p = vim_strsave(p);
10556#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010557 rettv->v_type = VAR_STRING;
10558 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559}
10560
10561/*
10562 * "synIDtrans(id)" function
10563 */
10564/*ARGSUSED*/
10565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010566f_synIDtrans(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010567 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010568 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569{
10570 int id;
10571
10572#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010573 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010574
10575 if (id > 0)
10576 id = syn_get_final_id(id);
10577 else
10578#endif
10579 id = 0;
10580
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010581 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582}
10583
10584/*
10585 * "system()" function
10586 */
10587 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010588f_system(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010589 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010590 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010592 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010594 char_u *infile = NULL;
10595 char_u buf[NUMBUFLEN];
10596 int err = FALSE;
10597 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010599 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010600 {
10601 /*
10602 * Write the string to a temp file, to be used for input of the shell
10603 * command.
10604 */
10605 if ((infile = vim_tempname('i')) == NULL)
10606 {
10607 EMSG(_(e_notmp));
10608 return;
10609 }
10610
10611 fd = mch_fopen((char *)infile, WRITEBIN);
10612 if (fd == NULL)
10613 {
10614 EMSG2(_(e_notopen), infile);
10615 goto done;
10616 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010617 p = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010618 if (fwrite(p, STRLEN(p), 1, fd) != 1)
10619 err = TRUE;
10620 if (fclose(fd) != 0)
10621 err = TRUE;
10622 if (err)
10623 {
10624 EMSG(_("E677: Error writing temp file"));
10625 goto done;
10626 }
10627 }
10628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010629 res = get_cmd_output(get_tv_string(&argvars[0]), infile, SHELL_SILENT);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010630
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631#ifdef USE_CR
10632 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010633 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010634 {
10635 char_u *s;
10636
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010637 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 {
10639 if (*s == CAR)
10640 *s = NL;
10641 }
10642 }
10643#else
10644# ifdef USE_CRNL
10645 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010646 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 {
10648 char_u *s, *d;
10649
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010650 d = res;
10651 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652 {
10653 if (s[0] == CAR && s[1] == NL)
10654 ++s;
10655 *d++ = *s;
10656 }
10657 *d = NUL;
10658 }
10659# endif
10660#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000010661
10662done:
10663 if (infile != NULL)
10664 {
10665 mch_remove(infile);
10666 vim_free(infile);
10667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010668 rettv->v_type = VAR_STRING;
10669 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670}
10671
10672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673 * "tempname()" function
10674 */
10675/*ARGSUSED*/
10676 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010677f_tempname(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010678 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010679 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010680{
10681 static int x = 'A';
10682
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010683 rettv->v_type = VAR_STRING;
10684 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685
10686 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
10687 * names. Skip 'I' and 'O', they are used for shell redirection. */
10688 do
10689 {
10690 if (x == 'Z')
10691 x = '0';
10692 else if (x == '9')
10693 x = 'A';
10694 else
10695 {
10696#ifdef EBCDIC
10697 if (x == 'I')
10698 x = 'J';
10699 else if (x == 'R')
10700 x = 'S';
10701 else
10702#endif
10703 ++x;
10704 }
10705 } while (x == 'I' || x == 'O');
10706}
10707
10708/*
10709 * "tolower(string)" function
10710 */
10711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010712f_tolower(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010713 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010714 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715{
10716 char_u *p;
10717
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010718 p = vim_strsave(get_tv_string(&argvars[0]));
10719 rettv->v_type = VAR_STRING;
10720 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721
10722 if (p != NULL)
10723 while (*p != NUL)
10724 {
10725#ifdef FEAT_MBYTE
10726 int l;
10727
10728 if (enc_utf8)
10729 {
10730 int c, lc;
10731
10732 c = utf_ptr2char(p);
10733 lc = utf_tolower(c);
10734 l = utf_ptr2len_check(p);
10735 /* TODO: reallocate string when byte count changes. */
10736 if (utf_char2len(lc) == l)
10737 utf_char2bytes(lc, p);
10738 p += l;
10739 }
10740 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
10741 p += l; /* skip multi-byte character */
10742 else
10743#endif
10744 {
10745 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
10746 ++p;
10747 }
10748 }
10749}
10750
10751/*
10752 * "toupper(string)" function
10753 */
10754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010755f_toupper(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010756 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010757 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010758{
10759 char_u *p;
10760
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010761 p = vim_strsave(get_tv_string(&argvars[0]));
10762 rettv->v_type = VAR_STRING;
10763 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764
10765 if (p != NULL)
10766 while (*p != NUL)
10767 {
10768#ifdef FEAT_MBYTE
10769 int l;
10770
10771 if (enc_utf8)
10772 {
10773 int c, uc;
10774
10775 c = utf_ptr2char(p);
10776 uc = utf_toupper(c);
10777 l = utf_ptr2len_check(p);
10778 /* TODO: reallocate string when byte count changes. */
10779 if (utf_char2len(uc) == l)
10780 utf_char2bytes(uc, p);
10781 p += l;
10782 }
10783 else if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
10784 p += l; /* skip multi-byte character */
10785 else
10786#endif
10787 {
10788 *p = TOUPPER_LOC(*p); /* note that toupper() can be a macro */
10789 p++;
10790 }
10791 }
10792}
10793
10794/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000010795 * "tr(string, fromstr, tostr)" function
10796 */
10797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010798f_tr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010799 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010800 typeval *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000010801{
10802 char_u *instr;
10803 char_u *fromstr;
10804 char_u *tostr;
10805 char_u *p;
10806#ifdef FEAT_MBYTE
10807 int inlen;
10808 int fromlen;
10809 int tolen;
10810 int idx;
10811 char_u *cpstr;
10812 int cplen;
10813 int first = TRUE;
10814#endif
10815 char_u buf[NUMBUFLEN];
10816 char_u buf2[NUMBUFLEN];
10817 garray_T ga;
10818
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010819 instr = get_tv_string(&argvars[0]);
10820 fromstr = get_tv_string_buf(&argvars[1], buf);
10821 tostr = get_tv_string_buf(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000010822
10823 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010824 rettv->v_type = VAR_STRING;
10825 rettv->vval.v_string = NULL;
Bram Moolenaar8299df92004-07-10 09:47:34 +000010826 ga_init2(&ga, (int)sizeof(char), 80);
10827
10828#ifdef FEAT_MBYTE
10829 if (!has_mbyte)
10830#endif
10831 /* not multi-byte: fromstr and tostr must be the same length */
10832 if (STRLEN(fromstr) != STRLEN(tostr))
10833 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010834#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000010835error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000010836#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000010837 EMSG2(_(e_invarg2), fromstr);
10838 ga_clear(&ga);
10839 return;
10840 }
10841
10842 /* fromstr and tostr have to contain the same number of chars */
10843 while (*instr != NUL)
10844 {
10845#ifdef FEAT_MBYTE
10846 if (has_mbyte)
10847 {
10848 inlen = mb_ptr2len_check(instr);
10849 cpstr = instr;
10850 cplen = inlen;
10851 idx = 0;
10852 for (p = fromstr; *p != NUL; p += fromlen)
10853 {
10854 fromlen = mb_ptr2len_check(p);
10855 if (fromlen == inlen && STRNCMP(instr, p, inlen) == 0)
10856 {
10857 for (p = tostr; *p != NUL; p += tolen)
10858 {
10859 tolen = mb_ptr2len_check(p);
10860 if (idx-- == 0)
10861 {
10862 cplen = tolen;
10863 cpstr = p;
10864 break;
10865 }
10866 }
10867 if (*p == NUL) /* tostr is shorter than fromstr */
10868 goto error;
10869 break;
10870 }
10871 ++idx;
10872 }
10873
10874 if (first && cpstr == instr)
10875 {
10876 /* Check that fromstr and tostr have the same number of
10877 * (multi-byte) characters. Done only once when a character
10878 * of instr doesn't appear in fromstr. */
10879 first = FALSE;
10880 for (p = tostr; *p != NUL; p += tolen)
10881 {
10882 tolen = mb_ptr2len_check(p);
10883 --idx;
10884 }
10885 if (idx != 0)
10886 goto error;
10887 }
10888
10889 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000010890 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000010891 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000010892
10893 instr += inlen;
10894 }
10895 else
10896#endif
10897 {
10898 /* When not using multi-byte chars we can do it faster. */
10899 p = vim_strchr(fromstr, *instr);
10900 if (p != NULL)
10901 ga_append(&ga, tostr[p - fromstr]);
10902 else
10903 ga_append(&ga, *instr);
10904 ++instr;
10905 }
10906 }
10907
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010908 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000010909}
10910
10911/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912 * "type(expr)" function
10913 */
10914 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010915f_type(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010916 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010917 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000010919 int n;
10920
10921 switch (argvars[0].v_type)
10922 {
10923 case VAR_NUMBER: n = 0; break;
10924 case VAR_STRING: n = 1; break;
10925 case VAR_FUNC: n = 2; break;
10926 case VAR_LIST: n = 3; break;
10927 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
10928 }
10929 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010930}
10931
10932/*
10933 * "virtcol(string)" function
10934 */
10935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010936f_virtcol(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010937 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010938 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939{
10940 colnr_T vcol = 0;
10941 pos_T *fp;
10942
10943 fp = var2fpos(&argvars[0], FALSE);
10944 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count)
10945 {
10946 getvvcol(curwin, fp, NULL, NULL, &vcol);
10947 ++vcol;
10948 }
10949
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010950 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010951}
10952
10953/*
10954 * "visualmode()" function
10955 */
10956/*ARGSUSED*/
10957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010958f_visualmode(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010959 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010960 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961{
10962#ifdef FEAT_VISUAL
10963 char_u str[2];
10964
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010965 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966 str[0] = curbuf->b_visual_mode_eval;
10967 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010968 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969
10970 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010971 if ((argvars[0].v_type == VAR_NUMBER
10972 && argvars[0].vval.v_number != 0)
10973 || (argvars[0].v_type == VAR_STRING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010974 && *get_tv_string(&argvars[0]) != NUL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975 curbuf->b_visual_mode_eval = NUL;
10976#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010977 rettv->vval.v_number = 0; /* return anything, it won't work anyway */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010978#endif
10979}
10980
10981/*
10982 * "winbufnr(nr)" function
10983 */
10984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010985f_winbufnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010986 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010987 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988{
10989 win_T *wp;
10990
10991 wp = find_win_by_nr(&argvars[0]);
10992 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010993 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010995 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010996}
10997
10998/*
10999 * "wincol()" function
11000 */
11001/*ARGSUSED*/
11002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011003f_wincol(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011004 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011005 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006{
11007 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011008 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009}
11010
11011/*
11012 * "winheight(nr)" function
11013 */
11014 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011015f_winheight(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011016 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011017 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018{
11019 win_T *wp;
11020
11021 wp = find_win_by_nr(&argvars[0]);
11022 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011023 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011025 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026}
11027
11028/*
11029 * "winline()" function
11030 */
11031/*ARGSUSED*/
11032 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011033f_winline(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011034 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011035 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011036{
11037 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011038 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039}
11040
11041/*
11042 * "winnr()" function
11043 */
11044/* ARGSUSED */
11045 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011046f_winnr(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011047 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011048 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049{
11050 int nr = 1;
11051#ifdef FEAT_WINDOWS
11052 win_T *wp;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011053 win_T *twin = curwin;
11054 char_u *arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011055
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011056 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011057 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011058 arg = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011059 if (STRCMP(arg, "$") == 0)
11060 twin = lastwin;
11061 else if (STRCMP(arg, "#") == 0)
11062 {
11063 twin = prevwin;
11064 if (prevwin == NULL)
11065 nr = 0;
11066 }
11067 else
11068 {
11069 EMSG2(_(e_invexpr2), arg);
11070 nr = 0;
11071 }
11072 }
11073
11074 if (nr > 0)
11075 for (wp = firstwin; wp != twin; wp = wp->w_next)
11076 ++nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011078 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079}
11080
11081/*
11082 * "winrestcmd()" function
11083 */
11084/* ARGSUSED */
11085 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011086f_winrestcmd(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011087 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011088 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011089{
11090#ifdef FEAT_WINDOWS
11091 win_T *wp;
11092 int winnr = 1;
11093 garray_T ga;
11094 char_u buf[50];
11095
11096 ga_init2(&ga, (int)sizeof(char), 70);
11097 for (wp = firstwin; wp != NULL; wp = wp->w_next)
11098 {
11099 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
11100 ga_concat(&ga, buf);
11101# ifdef FEAT_VERTSPLIT
11102 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
11103 ga_concat(&ga, buf);
11104# endif
11105 ++winnr;
11106 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000011107 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011108
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011109 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011110#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011111 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011112#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011113 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114}
11115
11116/*
11117 * "winwidth(nr)" function
11118 */
11119 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011120f_winwidth(argvars, rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011121 typeval *argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011122 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123{
11124 win_T *wp;
11125
11126 wp = find_win_by_nr(&argvars[0]);
11127 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011128 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129 else
11130#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011131 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011132#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011133 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011134#endif
11135}
11136
11137 static win_T *
11138find_win_by_nr(vp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011139 typeval *vp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140{
11141#ifdef FEAT_WINDOWS
11142 win_T *wp;
11143#endif
11144 int nr;
11145
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011146 nr = get_tv_number(vp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147
11148#ifdef FEAT_WINDOWS
11149 if (nr == 0)
11150 return curwin;
11151
11152 for (wp = firstwin; wp != NULL; wp = wp->w_next)
11153 if (--nr <= 0)
11154 break;
11155 return wp;
11156#else
11157 if (nr == 0 || nr == 1)
11158 return curwin;
11159 return NULL;
11160#endif
11161}
11162
11163/*
11164 * Translate a String variable into a position.
11165 */
11166 static pos_T *
11167var2fpos(varp, lnum)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011168 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169 int lnum; /* TRUE when $ is last line */
11170{
11171 char_u *name;
11172 static pos_T pos;
11173 pos_T *pp;
11174
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011175 name = get_tv_string(varp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011176 if (name[0] == '.') /* cursor */
11177 return &curwin->w_cursor;
11178 if (name[0] == '\'') /* mark */
11179 {
11180 pp = getmark(name[1], FALSE);
11181 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
11182 return NULL;
11183 return pp;
11184 }
11185 if (name[0] == '$') /* last column or line */
11186 {
11187 if (lnum)
11188 {
11189 pos.lnum = curbuf->b_ml.ml_line_count;
11190 pos.col = 0;
11191 }
11192 else
11193 {
11194 pos.lnum = curwin->w_cursor.lnum;
11195 pos.col = (colnr_T)STRLEN(ml_get_curline());
11196 }
11197 return &pos;
11198 }
11199 return NULL;
11200}
11201
11202/*
11203 * Get the length of an environment variable name.
11204 * Advance "arg" to the first character after the name.
11205 * Return 0 for error.
11206 */
11207 static int
11208get_env_len(arg)
11209 char_u **arg;
11210{
11211 char_u *p;
11212 int len;
11213
11214 for (p = *arg; vim_isIDc(*p); ++p)
11215 ;
11216 if (p == *arg) /* no name found */
11217 return 0;
11218
11219 len = (int)(p - *arg);
11220 *arg = p;
11221 return len;
11222}
11223
11224/*
11225 * Get the length of the name of a function or internal variable.
11226 * "arg" is advanced to the first non-white character after the name.
11227 * Return 0 if something is wrong.
11228 */
11229 static int
11230get_id_len(arg)
11231 char_u **arg;
11232{
11233 char_u *p;
11234 int len;
11235
11236 /* Find the end of the name. */
11237 for (p = *arg; eval_isnamec(*p); ++p)
11238 ;
11239 if (p == *arg) /* no name found */
11240 return 0;
11241
11242 len = (int)(p - *arg);
11243 *arg = skipwhite(p);
11244
11245 return len;
11246}
11247
11248/*
11249 * Get the length of the name of a function.
11250 * "arg" is advanced to the first non-white character after the name.
11251 * Return 0 if something is wrong.
11252 * If the name contains 'magic' {}'s, expand them and return the
11253 * expanded name in an allocated string via 'alias' - caller must free.
11254 */
11255 static int
11256get_func_len(arg, alias, evaluate)
11257 char_u **arg;
11258 char_u **alias;
11259 int evaluate;
11260{
11261 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011262 char_u *p;
11263 char_u *expr_start;
11264 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265
11266 *alias = NULL; /* default to no alias */
11267
11268 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
11269 && (*arg)[2] == (int)KE_SNR)
11270 {
11271 /* hard coded <SNR>, already translated */
11272 *arg += 3;
11273 return get_id_len(arg) + 3;
11274 }
11275 len = eval_fname_script(*arg);
11276 if (len > 0)
11277 {
11278 /* literal "<SID>", "s:" or "<SNR>" */
11279 *arg += len;
11280 }
11281
Bram Moolenaar071d4272004-06-13 20:20:40 +000011282 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011283 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011284 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011285 p = find_name_end(*arg, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011286 if (expr_start != NULL)
11287 {
11288 char_u *temp_string;
11289
11290 if (!evaluate)
11291 {
11292 len += (int)(p - *arg);
11293 *arg = skipwhite(p);
11294 return len;
11295 }
11296
11297 /*
11298 * Include any <SID> etc in the expanded string:
11299 * Thus the -len here.
11300 */
11301 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
11302 if (temp_string == NULL)
11303 return 0;
11304 *alias = temp_string;
11305 *arg = skipwhite(p);
11306 return (int)STRLEN(temp_string);
11307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011308
11309 len += get_id_len(arg);
11310 if (len == 0)
11311 EMSG2(_(e_invexpr2), *arg);
11312
11313 return len;
11314}
11315
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011316/*
11317 * Find the end of a variable or function name, taking care of magic braces.
11318 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
11319 * start and end of the first magic braces item.
11320 * Return a pointer to just after the name. Equal to "arg" if there is no
11321 * valid name.
11322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011324find_name_end(arg, expr_start, expr_end, incl_br)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325 char_u *arg;
11326 char_u **expr_start;
11327 char_u **expr_end;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011328 int incl_br; /* Include [] indexes */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011330 int mb_nest = 0;
11331 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011332 char_u *p;
11333
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011334 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011335 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011336 *expr_start = NULL;
11337 *expr_end = NULL;
11338 }
11339
11340 for (p = arg; *p != NUL
11341 && (eval_isnamec(*p)
11342 || (*p == '[' && incl_br)
11343 || mb_nest != 0
11344 || br_nest != 0); ++p)
11345 {
11346 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011348 if (*p == '[')
11349 ++br_nest;
11350 else if (*p == ']')
11351 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011353 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011355 if (*p == '{')
11356 {
11357 mb_nest++;
11358 if (expr_start != NULL && *expr_start == NULL)
11359 *expr_start = p;
11360 }
11361 else if (*p == '}')
11362 {
11363 mb_nest--;
11364 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
11365 *expr_end = p;
11366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 }
11369
11370 return p;
11371}
11372
11373/*
11374 * Return TRUE if character "c" can be used in a variable or function name.
11375 */
11376 static int
11377eval_isnamec(c)
11378 int c;
11379{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011380 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == '{' || c == '}');
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381}
11382
11383/*
11384 * Find a v: variable.
11385 * Return it's index, or -1 if not found.
11386 */
11387 static int
11388find_vim_var(name, len)
11389 char_u *name;
11390 int len; /* length of "name" */
11391{
11392 char_u *vname;
11393 int vlen;
11394 int i;
11395
11396 /*
11397 * Ignore "v:" for old built-in variables, require it for new ones.
11398 */
11399 if (name[0] == 'v' && name[1] == ':')
11400 {
11401 vname = name + 2;
11402 vlen = len - 2;
11403 }
11404 else
11405 {
11406 vname = name;
11407 vlen = len;
11408 }
11409 for (i = 0; i < VV_LEN; ++i)
11410 if (vlen == vimvars[i].len && STRCMP(vname, vimvars[i].name) == 0
11411 && ((vimvars[i].flags & VV_COMPAT) || vname != name))
11412 return i;
11413 return -1;
11414}
11415
11416/*
11417 * Set number v: variable to "val".
11418 */
11419 void
11420set_vim_var_nr(idx, val)
11421 int idx;
11422 long val;
11423{
11424 vimvars[idx].val = (char_u *)val;
11425}
11426
11427/*
11428 * Get number v: variable value;
11429 */
11430 long
11431get_vim_var_nr(idx)
11432 int idx;
11433{
11434 return (long)vimvars[idx].val;
11435}
11436
11437/*
11438 * Set v:count, v:count1 and v:prevcount.
11439 */
11440 void
11441set_vcount(count, count1)
11442 long count;
11443 long count1;
11444{
11445 vimvars[VV_PREVCOUNT].val = vimvars[VV_COUNT].val;
11446 vimvars[VV_COUNT].val = (char_u *)count;
11447 vimvars[VV_COUNT1].val = (char_u *)count1;
11448}
11449
11450/*
11451 * Set string v: variable to a copy of "val".
11452 */
11453 void
11454set_vim_var_string(idx, val, len)
11455 int idx;
11456 char_u *val;
11457 int len; /* length of "val" to use or -1 (whole string) */
11458{
11459 vim_free(vimvars[idx].val);
11460 if (val == NULL)
11461 vimvars[idx].val = NULL;
11462 else if (len == -1)
11463 vimvars[idx].val = vim_strsave(val);
11464 else
11465 vimvars[idx].val = vim_strnsave(val, len);
11466}
11467
11468/*
11469 * Set v:register if needed.
11470 */
11471 void
11472set_reg_var(c)
11473 int c;
11474{
11475 char_u regname;
11476
11477 if (c == 0 || c == ' ')
11478 regname = '"';
11479 else
11480 regname = c;
11481 /* Avoid free/alloc when the value is already right. */
11482 if (vimvars[VV_REG].val == NULL || vimvars[VV_REG].val[0] != c)
11483 set_vim_var_string(VV_REG, &regname, 1);
11484}
11485
11486/*
11487 * Get or set v:exception. If "oldval" == NULL, return the current value.
11488 * Otherwise, restore the value to "oldval" and return NULL.
11489 * Must always be called in pairs to save and restore v:exception! Does not
11490 * take care of memory allocations.
11491 */
11492 char_u *
11493v_exception(oldval)
11494 char_u *oldval;
11495{
11496 if (oldval == NULL)
11497 return vimvars[VV_EXCEPTION].val;
11498
11499 vimvars[VV_EXCEPTION].val = oldval;
11500 return NULL;
11501}
11502
11503/*
11504 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
11505 * Otherwise, restore the value to "oldval" and return NULL.
11506 * Must always be called in pairs to save and restore v:throwpoint! Does not
11507 * take care of memory allocations.
11508 */
11509 char_u *
11510v_throwpoint(oldval)
11511 char_u *oldval;
11512{
11513 if (oldval == NULL)
11514 return vimvars[VV_THROWPOINT].val;
11515
11516 vimvars[VV_THROWPOINT].val = oldval;
11517 return NULL;
11518}
11519
11520#if defined(FEAT_AUTOCMD) || defined(PROTO)
11521/*
11522 * Set v:cmdarg.
11523 * If "eap" != NULL, use "eap" to generate the value and return the old value.
11524 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
11525 * Must always be called in pairs!
11526 */
11527 char_u *
11528set_cmdarg(eap, oldarg)
11529 exarg_T *eap;
11530 char_u *oldarg;
11531{
11532 char_u *oldval;
11533 char_u *newval;
11534 unsigned len;
11535
11536 oldval = vimvars[VV_CMDARG].val;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000011537 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000011539 vim_free(oldval);
11540 vimvars[VV_CMDARG].val = oldarg;
11541 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542 }
11543
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000011544 if (eap->force_bin == FORCE_BIN)
11545 len = 6;
11546 else if (eap->force_bin == FORCE_NOBIN)
11547 len = 8;
11548 else
11549 len = 0;
11550 if (eap->force_ff != 0)
11551 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
11552# ifdef FEAT_MBYTE
11553 if (eap->force_enc != 0)
11554 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
11555# endif
11556
11557 newval = alloc(len + 1);
11558 if (newval == NULL)
11559 return NULL;
11560
11561 if (eap->force_bin == FORCE_BIN)
11562 sprintf((char *)newval, " ++bin");
11563 else if (eap->force_bin == FORCE_NOBIN)
11564 sprintf((char *)newval, " ++nobin");
11565 else
11566 *newval = NUL;
11567 if (eap->force_ff != 0)
11568 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
11569 eap->cmd + eap->force_ff);
11570# ifdef FEAT_MBYTE
11571 if (eap->force_enc != 0)
11572 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
11573 eap->cmd + eap->force_enc);
11574# endif
11575 vimvars[VV_CMDARG].val = newval;
11576 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011577}
11578#endif
11579
11580/*
11581 * Get the value of internal variable "name".
11582 * Return OK or FAIL.
11583 */
11584 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011585get_var_tv(name, len, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011586 char_u *name;
11587 int len; /* length of "name" */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011588 typeval *rettv; /* NULL when only checking existence */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011589{
11590 int ret = OK;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011591 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011592 VAR v;
11593 int cc;
11594 int i;
11595
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011596 tv.v_type = VAR_UNKNOWN;
11597
Bram Moolenaar071d4272004-06-13 20:20:40 +000011598 /* truncate the name, so that we can use strcmp() */
11599 cc = name[len];
11600 name[len] = NUL;
11601
11602 /*
11603 * Check for "b:changedtick".
11604 */
11605 if (STRCMP(name, "b:changedtick") == 0)
11606 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011607 tv.v_type = VAR_NUMBER;
11608 tv.vval.v_number = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011609 }
11610
11611 /*
11612 * Check for built-in v: variables.
11613 */
11614 else if ((i = find_vim_var(name, len)) >= 0)
11615 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011616 tv.v_type = vimvars[i].type;
11617 if (tv.v_type == VAR_NUMBER)
11618 tv.vval.v_number = (long)vimvars[i].val;
11619 else
11620 tv.vval.v_string = vimvars[i].val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621 }
11622
11623 /*
11624 * Check for user-defined variables.
11625 */
11626 else
11627 {
11628 v = find_var(name, FALSE);
11629 if (v != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011630 tv = v->tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631 }
11632
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011633 if (tv.v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011634 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011635 if (rettv != NULL)
11636 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011637 ret = FAIL;
11638 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011639 else if (rettv != NULL)
11640 copy_tv(&tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011641
11642 name[len] = cc;
11643
11644 return ret;
11645}
11646
11647/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011648 * Allocate memory for a variable type-value, and make it emtpy (0 or NULL
11649 * value).
11650 */
11651 static typeval *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011652alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011653{
11654 return (typeval *)alloc_clear((unsigned)sizeof(typeval));
11655}
11656
11657/*
11658 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659 * The string "s" must have been allocated, it is consumed.
11660 * Return NULL for out of memory, the variable otherwise.
11661 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011662 static typeval *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011663alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011664 char_u *s;
11665{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011666 typeval *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011667
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011668 rettv = alloc_tv();
11669 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011671 rettv->v_type = VAR_STRING;
11672 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011673 }
11674 else
11675 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011676 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011677}
11678
11679/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011680 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011681 */
11682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011683free_tv(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011684 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011685{
11686 if (varp != NULL)
11687 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011688 switch (varp->v_type)
11689 {
11690 case VAR_STRING:
11691 case VAR_FUNC:
11692 vim_free(varp->vval.v_string);
11693 break;
11694 case VAR_LIST:
11695 list_unref(varp->vval.v_list);
11696 break;
11697 default:
11698 break;
11699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700 vim_free(varp);
11701 }
11702}
11703
11704/*
11705 * Free the memory for a variable value and set the value to NULL or 0.
11706 */
11707 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011708clear_tv(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011709 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710{
11711 if (varp != NULL)
11712 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011713 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011714 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011715 case VAR_STRING:
11716 case VAR_FUNC:
11717 vim_free(varp->vval.v_string);
11718 varp->vval.v_string = NULL;
11719 break;
11720 case VAR_LIST:
11721 list_unref(varp->vval.v_list);
11722 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011723 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011724 varp->vval.v_number = 0;
11725 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011726 case VAR_UNKNOWN:
11727 break;
11728 default:
11729 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 }
11732}
11733
11734/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011735 * Set the value of a variable to NULL without freeing items.
11736 */
11737 static void
11738init_tv(varp)
11739 typeval *varp;
11740{
11741 if (varp != NULL)
11742 vim_memset(varp, 0, sizeof(typeval));
11743}
11744
11745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746 * Get the number value of a variable.
11747 * If it is a String variable, uses vim_str2nr().
11748 */
11749 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011750get_tv_number(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011751 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011752{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011753 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011755 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011757 case VAR_NUMBER:
11758 n = (long)(varp->vval.v_number);
11759 break;
11760 case VAR_FUNC:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011761 EMSG(_("E703: Using a Funcref as a number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011762 break;
11763 case VAR_STRING:
11764 if (varp->vval.v_string != NULL)
11765 vim_str2nr(varp->vval.v_string, NULL, NULL,
11766 TRUE, TRUE, &n, NULL);
11767 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011768 case VAR_LIST:
11769 EMSG(_("E703: Using a List as a number"));
11770 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011771 default:
11772 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011773 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011774 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775}
11776
11777/*
11778 * Get the lnum from the first argument. Also accepts ".", "$", etc.
11779 */
11780 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011781get_tv_lnum(argvars)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011782 typeval *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011784 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785 linenr_T lnum;
11786
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011787 lnum = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011788 if (lnum == 0) /* no valid number, try using line() */
11789 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011790 rettv.v_type = VAR_NUMBER;
11791 f_line(argvars, &rettv);
11792 lnum = rettv.vval.v_number;
11793 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011794 }
11795 return lnum;
11796}
11797
11798/*
11799 * Get the string value of a variable.
11800 * If it is a Number variable, the number is converted into a string.
11801 * get_var_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
11802 * get_var_string_buf() uses a given buffer.
11803 * If the String variable has never been set, return an empty string.
11804 * Never returns NULL;
11805 */
11806 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011807get_tv_string(varp)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011808 typeval *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809{
11810 static char_u mybuf[NUMBUFLEN];
11811
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011812 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813}
11814
11815 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011816get_tv_string_buf(varp, buf)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011817 typeval *varp;
11818 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011820 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011822 case VAR_NUMBER:
11823 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
11824 return buf;
11825 case VAR_FUNC:
11826 EMSG(_("E99: using Funcref as a String"));
11827 break;
11828 case VAR_LIST:
11829 EMSG(_("E99: using List as a String"));
11830 break;
11831 case VAR_STRING:
11832 if (varp->vval.v_string != NULL)
11833 return varp->vval.v_string;
11834 break;
11835 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011836 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011837 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011839 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840}
11841
11842/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011843 * Get value for "&", as used in map() and filter().
11844 */
11845 static int
11846get_amp_tv(rettv)
11847 typeval *rettv;
11848{
11849 if (amp_tv.v_type == VAR_UNKNOWN)
11850 {
11851 EMSG(_("E712: Using & outside of map() or filter()"));
11852 return FAIL;
11853 }
11854 copy_tv(&amp_tv, rettv);
11855 return OK;
11856}
11857
11858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859 * Find variable "name" in the list of variables.
11860 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011861 * Careful: "a:0" variables don't have a name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011862 */
11863 static VAR
11864find_var(name, writing)
11865 char_u *name;
11866 int writing;
11867{
11868 int i;
11869 char_u *varname;
11870 garray_T *gap;
11871
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872 if (name[0] == 'a' && name[1] == ':')
11873 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011874 /* Function arguments "a:".
11875 * NOTE: We use a typecast, because function arguments don't have a
11876 * name. The caller must not try to access the name! */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 if (writing)
11878 {
11879 EMSG2(_(e_readonlyvar), name);
11880 return NULL;
11881 }
11882 name += 2;
11883 if (current_funccal == NULL)
11884 return NULL;
11885 if (VIM_ISDIGIT(*name))
11886 {
11887 i = atol((char *)name);
11888 if (i == 0) /* a:0 */
11889 return &current_funccal->a0_var;
11890 i += current_funccal->func->args.ga_len;
11891 if (i > current_funccal->argcount) /* a:999 */
11892 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011893 return (VAR)&(current_funccal->argvars[i - 1]); /* a:1, a:2, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011894 }
11895 if (STRCMP(name, "firstline") == 0)
11896 return &(current_funccal->firstline);
11897 if (STRCMP(name, "lastline") == 0)
11898 return &(current_funccal->lastline);
11899 for (i = 0; i < current_funccal->func->args.ga_len; ++i)
11900 if (STRCMP(name, ((char_u **)
11901 (current_funccal->func->args.ga_data))[i]) == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011902 return (VAR)&(current_funccal->argvars[i]); /* a:name */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903 return NULL;
11904 }
11905
11906 gap = find_var_ga(name, &varname);
11907 if (gap == NULL)
11908 return NULL;
11909 return find_var_in_ga(gap, varname);
11910}
11911
11912 static VAR
11913find_var_in_ga(gap, varname)
11914 garray_T *gap;
11915 char_u *varname;
11916{
11917 int i;
11918
11919 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011920 if (VAR_GAP_ENTRY(i, gap).v_name != NULL
11921 && STRCMP(VAR_GAP_ENTRY(i, gap).v_name, varname) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922 break;
11923 if (i < 0)
11924 return NULL;
11925 return &VAR_GAP_ENTRY(i, gap);
11926}
11927
11928/*
11929 * Find the growarray and start of name without ':' for a variable name.
11930 */
11931 static garray_T *
11932find_var_ga(name, varname)
11933 char_u *name;
11934 char_u **varname;
11935{
11936 if (name[1] != ':')
11937 {
11938 /* If not "x:name" there must not be any ":" in the name. */
11939 if (vim_strchr(name, ':') != NULL)
11940 return NULL;
11941 *varname = name;
11942 if (current_funccal == NULL)
11943 return &variables; /* global variable */
11944 return &current_funccal->l_vars; /* local function variable */
11945 }
11946 *varname = name + 2;
11947 if (*name == 'b') /* buffer variable */
11948 return &curbuf->b_vars;
11949 if (*name == 'w') /* window variable */
11950 return &curwin->w_vars;
11951 if (*name == 'g') /* global variable */
11952 return &variables;
11953 if (*name == 'l' && current_funccal != NULL)/* local function variable */
11954 return &current_funccal->l_vars;
11955 if (*name == 's' /* script variable */
11956 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
11957 return &SCRIPT_VARS(current_SID);
11958 return NULL;
11959}
11960
11961/*
11962 * Get the string value of a (global/local) variable.
11963 * Returns NULL when it doesn't exist.
11964 */
11965 char_u *
11966get_var_value(name)
11967 char_u *name;
11968{
11969 VAR v;
11970
11971 v = find_var(name, FALSE);
11972 if (v == NULL)
11973 return NULL;
11974 return get_var_string(v);
11975}
11976
11977/*
11978 * Allocate a new growarry for a sourced script. It will be used while
11979 * sourcing this script and when executing functions defined in the script.
11980 */
11981 void
11982new_script_vars(id)
11983 scid_T id;
11984{
11985 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
11986 {
11987 while (ga_scripts.ga_len < id)
11988 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011989 vars_init(&SCRIPT_VARS(ga_scripts.ga_len + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011990 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011991 }
11992 }
11993}
11994
11995/*
11996 * Initialize internal variables for use.
11997 */
11998 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011999vars_init(gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000 garray_T *gap;
12001{
12002 ga_init2(gap, (int)sizeof(var), 4);
12003}
12004
12005/*
12006 * Clean up a list of internal variables.
12007 */
12008 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012009vars_clear(gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010 garray_T *gap;
12011{
12012 int i;
12013
12014 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012015 clear_var(&VAR_GAP_ENTRY(i, gap));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012016 ga_clear(gap);
12017}
12018
12019 static void
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012020clear_var(v)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012021 VAR v;
12022{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012023 vim_free(v->v_name);
12024 v->v_name = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012025 clear_tv(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012026}
12027
12028/*
12029 * List the value of one internal variable.
12030 */
12031 static void
12032list_one_var(v, prefix)
12033 VAR v;
12034 char_u *prefix;
12035{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012036 char_u *tofree;
12037 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012038 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012039
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012040 s = echo_string(&v->tv, &tofree, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012041 list_one_var_a(prefix, v->v_name, v->tv.v_type,
12042 s == NULL ? (char_u *)"" : s);
12043 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012044}
12045
12046/*
12047 * List the value of one "v:" variable.
12048 */
12049 static void
12050list_vim_var(i)
12051 int i; /* index in vimvars[] */
12052{
12053 char_u *p;
12054 char_u numbuf[NUMBUFLEN];
12055
12056 if (vimvars[i].type == VAR_NUMBER)
12057 {
12058 p = numbuf;
12059 sprintf((char *)p, "%ld", (long)vimvars[i].val);
12060 }
12061 else if (vimvars[i].val == NULL)
12062 p = (char_u *)"";
12063 else
12064 p = vimvars[i].val;
12065 list_one_var_a((char_u *)"v:", (char_u *)vimvars[i].name,
12066 vimvars[i].type, p);
12067}
12068
12069 static void
12070list_one_var_a(prefix, name, type, string)
12071 char_u *prefix;
12072 char_u *name;
12073 int type;
12074 char_u *string;
12075{
12076 msg_attr(prefix, 0); /* don't use msg(), it overwrites "v:statusmsg" */
12077 if (name != NULL) /* "a:" vars don't have a name stored */
12078 msg_puts(name);
12079 msg_putchar(' ');
12080 msg_advance(22);
12081 if (type == VAR_NUMBER)
12082 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012083 else if (type == VAR_FUNC)
12084 msg_putchar('*');
12085 else if (type == VAR_LIST)
12086 {
12087 msg_putchar('[');
12088 if (*string == '[')
12089 ++string;
12090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012091 else
12092 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012093
Bram Moolenaar071d4272004-06-13 20:20:40 +000012094 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012095
12096 if (type == VAR_FUNC)
12097 msg_puts((char_u *)"()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098}
12099
12100/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012101 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000012102 * If the variable already exists, the value is updated.
12103 * Otherwise the variable is created.
12104 */
12105 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012106set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107 char_u *name;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012108 typeval *tv;
12109 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012110{
12111 int i;
12112 VAR v;
12113 char_u *varname;
12114 garray_T *gap;
12115
12116 /*
12117 * Handle setting internal v: variables.
12118 */
12119 i = find_vim_var(name, (int)STRLEN(name));
12120 if (i >= 0)
12121 {
12122 if (vimvars[i].flags & VV_RO)
12123 EMSG2(_(e_readonlyvar), name);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012124 else if ((vimvars[i].flags & VV_RO_SBX) && sandbox)
12125 EMSG2(_(e_readonlysbx), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012126 else
12127 {
12128 if (vimvars[i].type == VAR_STRING)
12129 {
12130 vim_free(vimvars[i].val);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012131 if (copy || tv->v_type != VAR_STRING)
12132 vimvars[i].val = vim_strsave(get_tv_string(tv));
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012133 else
12134 {
12135 /* Take over the string to avoid an extra alloc/free. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012136 vimvars[i].val = tv->vval.v_string;
12137 tv->vval.v_string = NULL;
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139 }
12140 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012141 vimvars[i].val = (char_u *)get_tv_number(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142 }
12143 return;
12144 }
12145
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012146 if (tv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012147 {
12148 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
12149 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
12150 ? name[2] : name[0]))
12151 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012152 EMSG2(_("E704: Funcref variable name must start with a capital: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012153 return;
12154 }
12155 if (function_exists(name))
12156 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012157 EMSG2(_("705: Variable name conflicts with existing function: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012158 return;
12159 }
12160 }
12161
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162 v = find_var(name, TRUE);
12163 if (v != NULL) /* existing variable, only need to free string */
12164 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012165 if (v->tv.v_type != tv->v_type
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012166 && !((v->tv.v_type == VAR_STRING
12167 || v->tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012168 && (tv->v_type == VAR_STRING
12169 || tv->v_type == VAR_NUMBER)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012170 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012171 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012172 return;
12173 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012174 clear_tv(&v->tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012175 }
12176 else /* add a new variable */
12177 {
12178 gap = find_var_ga(name, &varname);
12179 if (gap == NULL) /* illegal name */
12180 {
12181 EMSG2(_("E461: Illegal variable name: %s"), name);
12182 return;
12183 }
12184
12185 /* Try to use an empty entry */
12186 for (i = gap->ga_len; --i >= 0; )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012187 if (VAR_GAP_ENTRY(i, gap).v_name == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188 break;
12189 if (i < 0) /* need to allocate more room */
12190 {
12191 if (ga_grow(gap, 1) == FAIL)
12192 return;
12193 i = gap->ga_len;
12194 }
12195 v = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012196 if ((v->v_name = vim_strsave(varname)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197 return;
12198 if (i == gap->ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012201 if (copy || tv->v_type == VAR_NUMBER)
12202 copy_tv(tv, &v->tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012203 else
12204 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012205 v->tv = *tv;
12206 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000012207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012208}
12209
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012210/*
12211 * Copy the values from typeval "from" to typeval "to".
12212 * When needed allocates string or increases reference count.
12213 * Does not make a copy of a list!
12214 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012216copy_tv(from, to)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012217 typeval *from;
12218 typeval *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012219{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012220 to->v_type = from->v_type;
12221 switch (from->v_type)
12222 {
12223 case VAR_NUMBER:
12224 to->vval.v_number = from->vval.v_number;
12225 break;
12226 case VAR_STRING:
12227 case VAR_FUNC:
12228 if (from->vval.v_string == NULL)
12229 to->vval.v_string = NULL;
12230 else
12231 to->vval.v_string = vim_strsave(from->vval.v_string);
12232 break;
12233 case VAR_LIST:
12234 if (from->vval.v_list == NULL)
12235 to->vval.v_list = NULL;
12236 else
12237 {
12238 to->vval.v_list = from->vval.v_list;
12239 ++to->vval.v_list->lv_refcount;
12240 }
12241 break;
12242 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012243 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012244 break;
12245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012246}
12247
12248/*
12249 * ":echo expr1 ..." print each argument separated with a space, add a
12250 * newline at the end.
12251 * ":echon expr1 ..." print each argument plain.
12252 */
12253 void
12254ex_echo(eap)
12255 exarg_T *eap;
12256{
12257 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012258 typeval rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012259 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260 char_u *p;
12261 int needclr = TRUE;
12262 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012263 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264
12265 if (eap->skip)
12266 ++emsg_skip;
12267 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
12268 {
12269 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012270 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271 {
12272 /*
12273 * Report the invalid expression unless the expression evaluation
12274 * has been cancelled due to an aborting error, an interrupt, or an
12275 * exception.
12276 */
12277 if (!aborting())
12278 EMSG2(_(e_invexpr2), p);
12279 break;
12280 }
12281 if (!eap->skip)
12282 {
12283 if (atstart)
12284 {
12285 atstart = FALSE;
12286 /* Call msg_start() after eval1(), evaluating the expression
12287 * may cause a message to appear. */
12288 if (eap->cmdidx == CMD_echo)
12289 msg_start();
12290 }
12291 else if (eap->cmdidx == CMD_echo)
12292 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012293 for (p = echo_string(&rettv, &tofree, numbuf);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012294 *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295 if (*p == '\n' || *p == '\r' || *p == TAB)
12296 {
12297 if (*p != TAB && needclr)
12298 {
12299 /* remove any text still there from the command */
12300 msg_clr_eos();
12301 needclr = FALSE;
12302 }
12303 msg_putchar_attr(*p, echo_attr);
12304 }
12305 else
12306 {
12307#ifdef FEAT_MBYTE
12308 if (has_mbyte)
12309 {
12310 int i = (*mb_ptr2len_check)(p);
12311
12312 (void)msg_outtrans_len_attr(p, i, echo_attr);
12313 p += i - 1;
12314 }
12315 else
12316#endif
12317 (void)msg_outtrans_len_attr(p, 1, echo_attr);
12318 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012319 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012321 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322 arg = skipwhite(arg);
12323 }
12324 eap->nextcmd = check_nextcmd(arg);
12325
12326 if (eap->skip)
12327 --emsg_skip;
12328 else
12329 {
12330 /* remove text that may still be there from the command */
12331 if (needclr)
12332 msg_clr_eos();
12333 if (eap->cmdidx == CMD_echo)
12334 msg_end();
12335 }
12336}
12337
12338/*
12339 * ":echohl {name}".
12340 */
12341 void
12342ex_echohl(eap)
12343 exarg_T *eap;
12344{
12345 int id;
12346
12347 id = syn_name2id(eap->arg);
12348 if (id == 0)
12349 echo_attr = 0;
12350 else
12351 echo_attr = syn_id2attr(id);
12352}
12353
12354/*
12355 * ":execute expr1 ..." execute the result of an expression.
12356 * ":echomsg expr1 ..." Print a message
12357 * ":echoerr expr1 ..." Print an error
12358 * Each gets spaces around each argument and a newline at the end for
12359 * echo commands
12360 */
12361 void
12362ex_execute(eap)
12363 exarg_T *eap;
12364{
12365 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012366 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012367 int ret = OK;
12368 char_u *p;
12369 garray_T ga;
12370 int len;
12371 int save_did_emsg;
12372
12373 ga_init2(&ga, 1, 80);
12374
12375 if (eap->skip)
12376 ++emsg_skip;
12377 while (*arg != NUL && *arg != '|' && *arg != '\n')
12378 {
12379 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012380 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381 {
12382 /*
12383 * Report the invalid expression unless the expression evaluation
12384 * has been cancelled due to an aborting error, an interrupt, or an
12385 * exception.
12386 */
12387 if (!aborting())
12388 EMSG2(_(e_invexpr2), p);
12389 ret = FAIL;
12390 break;
12391 }
12392
12393 if (!eap->skip)
12394 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012395 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396 len = (int)STRLEN(p);
12397 if (ga_grow(&ga, len + 2) == FAIL)
12398 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012399 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012400 ret = FAIL;
12401 break;
12402 }
12403 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406 ga.ga_len += len;
12407 }
12408
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012409 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410 arg = skipwhite(arg);
12411 }
12412
12413 if (ret != FAIL && ga.ga_data != NULL)
12414 {
12415 if (eap->cmdidx == CMD_echomsg)
12416 MSG_ATTR(ga.ga_data, echo_attr);
12417 else if (eap->cmdidx == CMD_echoerr)
12418 {
12419 /* We don't want to abort following commands, restore did_emsg. */
12420 save_did_emsg = did_emsg;
12421 EMSG((char_u *)ga.ga_data);
12422 if (!force_abort)
12423 did_emsg = save_did_emsg;
12424 }
12425 else if (eap->cmdidx == CMD_execute)
12426 do_cmdline((char_u *)ga.ga_data,
12427 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
12428 }
12429
12430 ga_clear(&ga);
12431
12432 if (eap->skip)
12433 --emsg_skip;
12434
12435 eap->nextcmd = check_nextcmd(arg);
12436}
12437
12438/*
12439 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
12440 * "arg" points to the "&" or '+' when called, to "option" when returning.
12441 * Returns NULL when no option name found. Otherwise pointer to the char
12442 * after the option name.
12443 */
12444 static char_u *
12445find_option_end(arg, opt_flags)
12446 char_u **arg;
12447 int *opt_flags;
12448{
12449 char_u *p = *arg;
12450
12451 ++p;
12452 if (*p == 'g' && p[1] == ':')
12453 {
12454 *opt_flags = OPT_GLOBAL;
12455 p += 2;
12456 }
12457 else if (*p == 'l' && p[1] == ':')
12458 {
12459 *opt_flags = OPT_LOCAL;
12460 p += 2;
12461 }
12462 else
12463 *opt_flags = 0;
12464
12465 if (!ASCII_ISALPHA(*p))
12466 return NULL;
12467 *arg = p;
12468
12469 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
12470 p += 4; /* termcap option */
12471 else
12472 while (ASCII_ISALPHA(*p))
12473 ++p;
12474 return p;
12475}
12476
12477/*
12478 * ":function"
12479 */
12480 void
12481ex_function(eap)
12482 exarg_T *eap;
12483{
12484 char_u *theline;
12485 int j;
12486 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012488 char_u *name = NULL;
12489 char_u *p;
12490 char_u *arg;
12491 garray_T newargs;
12492 garray_T newlines;
12493 int varargs = FALSE;
12494 int mustend = FALSE;
12495 int flags = 0;
12496 ufunc_T *fp;
12497 int indent;
12498 int nesting;
12499 char_u *skip_until = NULL;
12500 static char_u e_funcexts[] = N_("E122: Function %s already exists, add ! to replace it");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012501 VAR v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502
12503 /*
12504 * ":function" without argument: list functions.
12505 */
12506 if (ends_excmd(*eap->arg))
12507 {
12508 if (!eap->skip)
12509 for (fp = firstfunc; fp != NULL && !got_int; fp = fp->next)
12510 list_func_head(fp, FALSE);
12511 eap->nextcmd = check_nextcmd(eap->arg);
12512 return;
12513 }
12514
12515 p = eap->arg;
12516 name = trans_function_name(&p, eap->skip, FALSE);
12517 if (name == NULL && !eap->skip)
12518 {
12519 /*
12520 * Return on an invalid expression in braces, unless the expression
12521 * evaluation has been cancelled due to an aborting error, an
12522 * interrupt, or an exception.
12523 */
12524 if (!aborting())
12525 return;
12526 else
12527 eap->skip = TRUE;
12528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012529 /* An error in a function call during evaluation of an expression in magic
12530 * braces should not cause the function not to be defined. */
12531 saved_did_emsg = did_emsg;
12532 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012533
12534 /*
12535 * ":function func" with only function name: list function.
12536 */
12537 if (vim_strchr(p, '(') == NULL)
12538 {
12539 if (!ends_excmd(*skipwhite(p)))
12540 {
12541 EMSG(_(e_trailing));
12542 goto erret_name;
12543 }
12544 eap->nextcmd = check_nextcmd(p);
12545 if (eap->nextcmd != NULL)
12546 *p = NUL;
12547 if (!eap->skip && !got_int)
12548 {
12549 fp = find_func(name);
12550 if (fp != NULL)
12551 {
12552 list_func_head(fp, TRUE);
12553 for (j = 0; j < fp->lines.ga_len && !got_int; ++j)
12554 {
12555 msg_putchar('\n');
12556 msg_outnum((long)(j + 1));
12557 if (j < 9)
12558 msg_putchar(' ');
12559 if (j < 99)
12560 msg_putchar(' ');
12561 msg_prt_line(FUNCLINE(fp, j));
12562 out_flush(); /* show a line at a time */
12563 ui_breakcheck();
12564 }
12565 if (!got_int)
12566 {
12567 msg_putchar('\n');
12568 msg_puts((char_u *)" endfunction");
12569 }
12570 }
12571 else
12572 EMSG2(_("E123: Undefined function: %s"), eap->arg);
12573 }
12574 goto erret_name;
12575 }
12576
12577 /*
12578 * ":function name(arg1, arg2)" Define function.
12579 */
12580 p = skipwhite(p);
12581 if (*p != '(')
12582 {
12583 if (!eap->skip)
12584 {
12585 EMSG2(_("E124: Missing '(': %s"), eap->arg);
12586 goto erret_name;
12587 }
12588 /* attempt to continue by skipping some text */
12589 if (vim_strchr(p, '(') != NULL)
12590 p = vim_strchr(p, '(');
12591 }
12592 p = skipwhite(p + 1);
12593
12594 ga_init2(&newargs, (int)sizeof(char_u *), 3);
12595 ga_init2(&newlines, (int)sizeof(char_u *), 3);
12596
12597 /*
12598 * Isolate the arguments: "arg1, arg2, ...)"
12599 */
12600 while (*p != ')')
12601 {
12602 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
12603 {
12604 varargs = TRUE;
12605 p += 3;
12606 mustend = TRUE;
12607 }
12608 else
12609 {
12610 arg = p;
12611 while (ASCII_ISALNUM(*p) || *p == '_')
12612 ++p;
12613 if (arg == p || isdigit(*arg)
12614 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
12615 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
12616 {
12617 if (!eap->skip)
12618 EMSG2(_("E125: Illegal argument: %s"), arg);
12619 break;
12620 }
12621 if (ga_grow(&newargs, 1) == FAIL)
12622 goto erret;
12623 c = *p;
12624 *p = NUL;
12625 arg = vim_strsave(arg);
12626 if (arg == NULL)
12627 goto erret;
12628 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
12629 *p = c;
12630 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631 if (*p == ',')
12632 ++p;
12633 else
12634 mustend = TRUE;
12635 }
12636 p = skipwhite(p);
12637 if (mustend && *p != ')')
12638 {
12639 if (!eap->skip)
12640 EMSG2(_(e_invarg2), eap->arg);
12641 break;
12642 }
12643 }
12644 ++p; /* skip the ')' */
12645
12646 /* find extra arguments "range" and "abort" */
12647 for (;;)
12648 {
12649 p = skipwhite(p);
12650 if (STRNCMP(p, "range", 5) == 0)
12651 {
12652 flags |= FC_RANGE;
12653 p += 5;
12654 }
12655 else if (STRNCMP(p, "abort", 5) == 0)
12656 {
12657 flags |= FC_ABORT;
12658 p += 5;
12659 }
12660 else
12661 break;
12662 }
12663
12664 if (*p != NUL && *p != '"' && *p != '\n' && !eap->skip && !did_emsg)
12665 EMSG(_(e_trailing));
12666
12667 /*
12668 * Read the body of the function, until ":endfunction" is found.
12669 */
12670 if (KeyTyped)
12671 {
12672 /* Check if the function already exists, don't let the user type the
12673 * whole function before telling him it doesn't work! For a script we
12674 * need to skip the body to be able to find what follows. */
12675 if (!eap->skip && !eap->forceit && find_func(name) != NULL)
12676 EMSG2(_(e_funcexts), name);
12677
12678 msg_putchar('\n'); /* don't overwrite the function name */
12679 cmdline_row = msg_row;
12680 }
12681
12682 indent = 2;
12683 nesting = 0;
12684 for (;;)
12685 {
12686 msg_scroll = TRUE;
12687 need_wait_return = FALSE;
12688 if (eap->getline == NULL)
12689 theline = getcmdline(':', 0L, indent);
12690 else
12691 theline = eap->getline(':', eap->cookie, indent);
12692 if (KeyTyped)
12693 lines_left = Rows - 1;
12694 if (theline == NULL)
12695 {
12696 EMSG(_("E126: Missing :endfunction"));
12697 goto erret;
12698 }
12699
12700 if (skip_until != NULL)
12701 {
12702 /* between ":append" and "." and between ":python <<EOF" and "EOF"
12703 * don't check for ":endfunc". */
12704 if (STRCMP(theline, skip_until) == 0)
12705 {
12706 vim_free(skip_until);
12707 skip_until = NULL;
12708 }
12709 }
12710 else
12711 {
12712 /* skip ':' and blanks*/
12713 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
12714 ;
12715
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012716 /* Check for "endfunction". */
12717 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012718 {
12719 vim_free(theline);
12720 break;
12721 }
12722
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012723 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724 * at "end". */
12725 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
12726 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012727 else if (STRNCMP(p, "if", 2) == 0
12728 || STRNCMP(p, "wh", 2) == 0
12729 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000012730 || STRNCMP(p, "try", 3) == 0)
12731 indent += 2;
12732
12733 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012734 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012735 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012736 if (*p == '!')
12737 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738 p += eval_fname_script(p);
12739 if (ASCII_ISALPHA(*p))
12740 {
12741 vim_free(trans_function_name(&p, TRUE, FALSE));
12742 if (*skipwhite(p) == '(')
12743 {
12744 ++nesting;
12745 indent += 2;
12746 }
12747 }
12748 }
12749
12750 /* Check for ":append" or ":insert". */
12751 p = skip_range(p, NULL);
12752 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
12753 || (p[0] == 'i'
12754 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
12755 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
12756 skip_until = vim_strsave((char_u *)".");
12757
12758 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
12759 arg = skipwhite(skiptowhite(p));
12760 if (arg[0] == '<' && arg[1] =='<'
12761 && ((p[0] == 'p' && p[1] == 'y'
12762 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
12763 || (p[0] == 'p' && p[1] == 'e'
12764 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
12765 || (p[0] == 't' && p[1] == 'c'
12766 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
12767 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
12768 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012769 || (p[0] == 'm' && p[1] == 'z'
12770 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771 ))
12772 {
12773 /* ":python <<" continues until a dot, like ":append" */
12774 p = skipwhite(arg + 2);
12775 if (*p == NUL)
12776 skip_until = vim_strsave((char_u *)".");
12777 else
12778 skip_until = vim_strsave(p);
12779 }
12780 }
12781
12782 /* Add the line to the function. */
12783 if (ga_grow(&newlines, 1) == FAIL)
12784 goto erret;
12785 ((char_u **)(newlines.ga_data))[newlines.ga_len] = theline;
12786 newlines.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012787 }
12788
12789 /* Don't define the function when skipping commands or when an error was
12790 * detected. */
12791 if (eap->skip || did_emsg)
12792 goto erret;
12793
12794 /*
12795 * If there are no errors, add the function
12796 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012797 v = find_var(name, FALSE);
12798 if (v != NULL && v->tv.v_type == VAR_FUNC)
12799 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012800 EMSG2(_("E707: Function name conflicts with variable: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012801 goto erret;
12802 }
12803
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804 fp = find_func(name);
12805 if (fp != NULL)
12806 {
12807 if (!eap->forceit)
12808 {
12809 EMSG2(_(e_funcexts), name);
12810 goto erret;
12811 }
12812 if (fp->calls)
12813 {
12814 EMSG2(_("E127: Cannot redefine function %s: It is in use"), name);
12815 goto erret;
12816 }
12817 /* redefine existing function */
12818 ga_clear_strings(&(fp->args));
12819 ga_clear_strings(&(fp->lines));
12820 vim_free(name);
12821 }
12822 else
12823 {
12824 fp = (ufunc_T *)alloc((unsigned)sizeof(ufunc_T));
12825 if (fp == NULL)
12826 goto erret;
12827 /* insert the new function in the function list */
12828 fp->next = firstfunc;
12829 firstfunc = fp;
12830 fp->name = name;
12831 }
12832 fp->args = newargs;
12833 fp->lines = newlines;
12834 fp->varargs = varargs;
12835 fp->flags = flags;
12836 fp->calls = 0;
12837 fp->script_ID = current_SID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012839 vim_free(skip_until);
12840 return;
12841
12842erret:
12843 vim_free(skip_until);
12844 ga_clear_strings(&newargs);
12845 ga_clear_strings(&newlines);
12846erret_name:
12847 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849}
12850
12851/*
12852 * Get a function name, translating "<SID>" and "<SNR>".
12853 * Returns the function name in allocated memory, or NULL for failure.
12854 * Advances "pp" to just after the function name (if no error).
12855 */
12856 static char_u *
12857trans_function_name(pp, skip, internal)
12858 char_u **pp;
12859 int skip; /* only find the end, don't evaluate */
12860 int internal; /* TRUE if internal function name OK */
12861{
12862 char_u *name;
12863 char_u *start;
12864 char_u *end;
12865 int lead;
12866 char_u sid_buf[20];
12867 char_u *temp_string = NULL;
12868 char_u *expr_start, *expr_end;
12869 int len;
12870
12871 /* A name starting with "<SID>" or "<SNR>" is local to a script. */
12872 start = *pp;
12873 lead = eval_fname_script(start);
12874 if (lead > 0)
12875 start += lead;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012876 end = find_name_end(start, &expr_start, &expr_end, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012877 if (end == start)
12878 {
12879 if (!skip)
12880 EMSG(_("E129: Function name required"));
12881 return NULL;
12882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012883 if (expr_start != NULL && !skip)
12884 {
12885 /* expand magic curlies */
12886 temp_string = make_expanded_name(start, expr_start, expr_end, end);
12887 if (temp_string == NULL)
12888 {
12889 /*
12890 * Report an invalid expression in braces, unless the expression
12891 * evaluation has been cancelled due to an aborting error, an
12892 * interrupt, or an exception.
12893 */
12894 if (!aborting())
12895 EMSG2(_(e_invarg2), start);
12896 else
12897 *pp = end;
12898 return NULL;
12899 }
12900 start = temp_string;
12901 len = (int)STRLEN(temp_string);
12902 }
12903 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904 len = (int)(end - start);
12905
12906 /*
12907 * Copy the function name to allocated memory.
12908 * Accept <SID>name() inside a script, translate into <SNR>123_name().
12909 * Accept <SNR>123_name() outside a script.
12910 */
12911 if (skip)
12912 lead = 0; /* do nothing */
12913 else if (lead > 0)
12914 {
12915 lead = 3;
12916 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
12917 {
12918 if (current_SID <= 0)
12919 {
12920 EMSG(_(e_usingsid));
12921 return NULL;
12922 }
12923 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
12924 lead += (int)STRLEN(sid_buf);
12925 }
12926 }
12927 else if (!internal && !ASCII_ISUPPER(*start))
12928 {
12929 EMSG2(_("E128: Function name must start with a capital: %s"), start);
12930 return NULL;
12931 }
12932 name = alloc((unsigned)(len + lead + 1));
12933 if (name != NULL)
12934 {
12935 if (lead > 0)
12936 {
12937 name[0] = K_SPECIAL;
12938 name[1] = KS_EXTRA;
12939 name[2] = (int)KE_SNR;
12940 if (eval_fname_sid(*pp)) /* If it's "<SID>" */
12941 STRCPY(name + 3, sid_buf);
12942 }
12943 mch_memmove(name + lead, start, (size_t)len);
12944 name[len + lead] = NUL;
12945 }
12946 *pp = end;
12947
12948 vim_free(temp_string);
12949 return name;
12950}
12951
12952/*
12953 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
12954 * Return 2 if "p" starts with "s:".
12955 * Return 0 otherwise.
12956 */
12957 static int
12958eval_fname_script(p)
12959 char_u *p;
12960{
12961 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
12962 || STRNICMP(p + 1, "SNR>", 4) == 0))
12963 return 5;
12964 if (p[0] == 's' && p[1] == ':')
12965 return 2;
12966 return 0;
12967}
12968
12969/*
12970 * Return TRUE if "p" starts with "<SID>" or "s:".
12971 * Only works if eval_fname_script() returned non-zero for "p"!
12972 */
12973 static int
12974eval_fname_sid(p)
12975 char_u *p;
12976{
12977 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
12978}
12979
12980/*
12981 * List the head of the function: "name(arg1, arg2)".
12982 */
12983 static void
12984list_func_head(fp, indent)
12985 ufunc_T *fp;
12986 int indent;
12987{
12988 int j;
12989
12990 msg_start();
12991 if (indent)
12992 MSG_PUTS(" ");
12993 MSG_PUTS("function ");
12994 if (fp->name[0] == K_SPECIAL)
12995 {
12996 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
12997 msg_puts(fp->name + 3);
12998 }
12999 else
13000 msg_puts(fp->name);
13001 msg_putchar('(');
13002 for (j = 0; j < fp->args.ga_len; ++j)
13003 {
13004 if (j)
13005 MSG_PUTS(", ");
13006 msg_puts(FUNCARG(fp, j));
13007 }
13008 if (fp->varargs)
13009 {
13010 if (j)
13011 MSG_PUTS(", ");
13012 MSG_PUTS("...");
13013 }
13014 msg_putchar(')');
13015}
13016
13017/*
13018 * Find a function by name, return pointer to it in ufuncs.
13019 * Return NULL for unknown function.
13020 */
13021 static ufunc_T *
13022find_func(name)
13023 char_u *name;
13024{
13025 ufunc_T *fp;
13026
13027 for (fp = firstfunc; fp != NULL; fp = fp->next)
13028 if (STRCMP(name, fp->name) == 0)
13029 break;
13030 return fp;
13031}
13032
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013033/*
13034 * Return TRUE if a function "name" exists.
13035 */
13036 static int
13037function_exists(name)
13038 char_u *name;
13039{
13040 char_u *p = name;
13041 int n = FALSE;
13042
13043 p = trans_function_name(&p, FALSE, TRUE);
13044 if (p != NULL)
13045 {
13046 if (ASCII_ISUPPER(*p) || p[0] == K_SPECIAL)
13047 n = (find_func(p) != NULL);
13048 else if (ASCII_ISLOWER(*p))
13049 n = (find_internal_func(p) >= 0);
13050 vim_free(p);
13051 }
13052 return n;
13053}
13054
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
13056
13057/*
13058 * Function given to ExpandGeneric() to obtain the list of user defined
13059 * function names.
13060 */
13061 char_u *
13062get_user_func_name(xp, idx)
13063 expand_T *xp;
13064 int idx;
13065{
13066 static ufunc_T *fp = NULL;
13067
13068 if (idx == 0)
13069 fp = firstfunc;
13070 if (fp != NULL)
13071 {
13072 if (STRLEN(fp->name) + 4 >= IOSIZE)
13073 return fp->name; /* prevents overflow */
13074
13075 cat_func_name(IObuff, fp);
13076 if (xp->xp_context != EXPAND_USER_FUNC)
13077 {
13078 STRCAT(IObuff, "(");
13079 if (!fp->varargs && fp->args.ga_len == 0)
13080 STRCAT(IObuff, ")");
13081 }
13082
13083 fp = fp->next;
13084 return IObuff;
13085 }
13086 return NULL;
13087}
13088
13089#endif /* FEAT_CMDL_COMPL */
13090
13091/*
13092 * Copy the function name of "fp" to buffer "buf".
13093 * "buf" must be able to hold the function name plus three bytes.
13094 * Takes care of script-local function names.
13095 */
13096 static void
13097cat_func_name(buf, fp)
13098 char_u *buf;
13099 ufunc_T *fp;
13100{
13101 if (fp->name[0] == K_SPECIAL)
13102 {
13103 STRCPY(buf, "<SNR>");
13104 STRCAT(buf, fp->name + 3);
13105 }
13106 else
13107 STRCPY(buf, fp->name);
13108}
13109
13110/*
13111 * ":delfunction {name}"
13112 */
13113 void
13114ex_delfunction(eap)
13115 exarg_T *eap;
13116{
13117 ufunc_T *fp = NULL, *pfp;
13118 char_u *p;
13119 char_u *name;
13120
13121 p = eap->arg;
13122 name = trans_function_name(&p, eap->skip, FALSE);
13123 if (name == NULL)
13124 return;
13125 if (!ends_excmd(*skipwhite(p)))
13126 {
13127 vim_free(name);
13128 EMSG(_(e_trailing));
13129 return;
13130 }
13131 eap->nextcmd = check_nextcmd(p);
13132 if (eap->nextcmd != NULL)
13133 *p = NUL;
13134
13135 if (!eap->skip)
13136 fp = find_func(name);
13137 vim_free(name);
13138
13139 if (!eap->skip)
13140 {
13141 if (fp == NULL)
13142 {
13143 EMSG2(_("E130: Undefined function: %s"), eap->arg);
13144 return;
13145 }
13146 if (fp->calls)
13147 {
13148 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
13149 return;
13150 }
13151
13152 /* clear this function */
13153 vim_free(fp->name);
13154 ga_clear_strings(&(fp->args));
13155 ga_clear_strings(&(fp->lines));
13156
13157 /* remove the function from the function list */
13158 if (firstfunc == fp)
13159 firstfunc = fp->next;
13160 else
13161 {
13162 for (pfp = firstfunc; pfp != NULL; pfp = pfp->next)
13163 if (pfp->next == fp)
13164 {
13165 pfp->next = fp->next;
13166 break;
13167 }
13168 }
13169 vim_free(fp);
13170 }
13171}
13172
13173/*
13174 * Call a user function.
13175 */
13176 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013177call_user_func(fp, argcount, argvars, rettv, firstline, lastline)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178 ufunc_T *fp; /* pointer to function */
13179 int argcount; /* nr of args */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013180 typeval *argvars; /* arguments */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013181 typeval *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013182 linenr_T firstline; /* first line of range */
13183 linenr_T lastline; /* last line of range */
13184{
13185 char_u *save_sourcing_name;
13186 linenr_T save_sourcing_lnum;
13187 scid_T save_current_SID;
13188 struct funccall fc;
13189 struct funccall *save_fcp = current_funccal;
13190 int save_did_emsg;
13191 static int depth = 0;
13192
13193 /* If depth of calling is getting too high, don't execute the function */
13194 if (depth >= p_mfd)
13195 {
13196 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013197 rettv->v_type = VAR_NUMBER;
13198 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199 return;
13200 }
13201 ++depth;
13202
13203 line_breakcheck(); /* check for CTRL-C hit */
13204
13205 /* set local variables */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013206 vars_init(&fc.l_vars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207 fc.func = fp;
13208 fc.argcount = argcount;
13209 fc.argvars = argvars;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013210 fc.rettv = rettv;
13211 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212 fc.linenr = 0;
13213 fc.returned = FALSE;
13214 fc.level = ex_nesting_level;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013215 fc.a0_var.tv.v_type = VAR_NUMBER;
13216 fc.a0_var.tv.vval.v_number = argcount - fp->args.ga_len;
13217 fc.a0_var.v_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013218 current_funccal = &fc;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013219 fc.firstline.tv.v_type = VAR_NUMBER;
13220 fc.firstline.tv.vval.v_number = firstline;
13221 fc.firstline.v_name = NULL;
13222 fc.lastline.tv.v_type = VAR_NUMBER;
13223 fc.lastline.tv.vval.v_number = lastline;
13224 fc.lastline.v_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225 /* Check if this function has a breakpoint. */
13226 fc.breakpoint = dbg_find_breakpoint(FALSE, fp->name, (linenr_T)0);
13227 fc.dbg_tick = debug_tick;
13228
13229 /* Don't redraw while executing the function. */
13230 ++RedrawingDisabled;
13231 save_sourcing_name = sourcing_name;
13232 save_sourcing_lnum = sourcing_lnum;
13233 sourcing_lnum = 1;
13234 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
13235 : STRLEN(save_sourcing_name)) + STRLEN(fp->name) + 13));
13236 if (sourcing_name != NULL)
13237 {
13238 if (save_sourcing_name != NULL
13239 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
13240 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
13241 else
13242 STRCPY(sourcing_name, "function ");
13243 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
13244
13245 if (p_verbose >= 12)
13246 {
13247 ++no_wait_return;
13248 msg_scroll = TRUE; /* always scroll up, don't overwrite */
13249 msg_str((char_u *)_("calling %s"), sourcing_name);
13250 if (p_verbose >= 14)
13251 {
13252 int i;
13253 char_u buf[MSG_BUF_LEN];
13254
13255 msg_puts((char_u *)"(");
13256 for (i = 0; i < argcount; ++i)
13257 {
13258 if (i > 0)
13259 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013260 if (argvars[i].v_type == VAR_NUMBER)
13261 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013262 else
13263 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013264 trunc_string(get_tv_string(&argvars[i]),
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265 buf, MSG_BUF_LEN);
13266 msg_puts((char_u *)"\"");
13267 msg_puts(buf);
13268 msg_puts((char_u *)"\"");
13269 }
13270 }
13271 msg_puts((char_u *)")");
13272 }
13273 msg_puts((char_u *)"\n"); /* don't overwrite this either */
13274 cmdline_row = msg_row;
13275 --no_wait_return;
13276 }
13277 }
13278 save_current_SID = current_SID;
13279 current_SID = fp->script_ID;
13280 save_did_emsg = did_emsg;
13281 did_emsg = FALSE;
13282
13283 /* call do_cmdline() to execute the lines */
13284 do_cmdline(NULL, get_func_line, (void *)&fc,
13285 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
13286
13287 --RedrawingDisabled;
13288
13289 /* when the function was aborted because of an error, return -1 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013290 if ((did_emsg && (fp->flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013291 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013292 clear_tv(rettv);
13293 rettv->v_type = VAR_NUMBER;
13294 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295 }
13296
13297 /* when being verbose, mention the return value */
13298 if (p_verbose >= 12)
13299 {
13300 char_u *sn, *val;
13301
13302 ++no_wait_return;
13303 msg_scroll = TRUE; /* always scroll up, don't overwrite */
13304
13305 /* Make sure the output fits in IObuff. */
13306 sn = sourcing_name;
13307 if (STRLEN(sourcing_name) > IOSIZE / 2 - 50)
13308 sn = sourcing_name + STRLEN(sourcing_name) - (IOSIZE / 2 - 50);
13309
13310 if (aborting())
13311 smsg((char_u *)_("%s aborted"), sn);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013312 else if (fc.rettv->v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313 smsg((char_u *)_("%s returning #%ld"), sn,
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013314 (long)fc.rettv->vval.v_number);
13315 else if (fc.rettv->v_type == VAR_STRING)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013316 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013317 val = get_tv_string(fc.rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013318 if (STRLEN(val) > IOSIZE / 2 - 50)
13319 val = val + STRLEN(val) - (IOSIZE / 2 - 50);
13320 smsg((char_u *)_("%s returning \"%s\""), sn, val);
13321 }
13322 msg_puts((char_u *)"\n"); /* don't overwrite this either */
13323 cmdline_row = msg_row;
13324 --no_wait_return;
13325 }
13326
13327 vim_free(sourcing_name);
13328 sourcing_name = save_sourcing_name;
13329 sourcing_lnum = save_sourcing_lnum;
13330 current_SID = save_current_SID;
13331
13332 if (p_verbose >= 12 && sourcing_name != NULL)
13333 {
13334 ++no_wait_return;
13335 msg_scroll = TRUE; /* always scroll up, don't overwrite */
13336 msg_str((char_u *)_("continuing in %s"), sourcing_name);
13337 msg_puts((char_u *)"\n"); /* don't overwrite this either */
13338 cmdline_row = msg_row;
13339 --no_wait_return;
13340 }
13341
13342 did_emsg |= save_did_emsg;
13343 current_funccal = save_fcp;
13344
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013345 vars_clear(&fc.l_vars); /* free all local variables */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013346 --depth;
13347}
13348
13349/*
13350 * ":return [expr]"
13351 */
13352 void
13353ex_return(eap)
13354 exarg_T *eap;
13355{
13356 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013357 typeval rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013358 int returning = FALSE;
13359
13360 if (current_funccal == NULL)
13361 {
13362 EMSG(_("E133: :return not inside a function"));
13363 return;
13364 }
13365
13366 if (eap->skip)
13367 ++emsg_skip;
13368
13369 eap->nextcmd = NULL;
13370 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013371 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372 {
13373 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013374 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013375 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013376 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013377 }
13378 /* It's safer to return also on error. */
13379 else if (!eap->skip)
13380 {
13381 /*
13382 * Return unless the expression evaluation has been cancelled due to an
13383 * aborting error, an interrupt, or an exception.
13384 */
13385 if (!aborting())
13386 returning = do_return(eap, FALSE, TRUE, NULL);
13387 }
13388
13389 /* When skipping or the return gets pending, advance to the next command
13390 * in this line (!returning). Otherwise, ignore the rest of the line.
13391 * Following lines will be ignored by get_func_line(). */
13392 if (returning)
13393 eap->nextcmd = NULL;
13394 else if (eap->nextcmd == NULL) /* no argument */
13395 eap->nextcmd = check_nextcmd(arg);
13396
13397 if (eap->skip)
13398 --emsg_skip;
13399}
13400
13401/*
13402 * Return from a function. Possibly makes the return pending. Also called
13403 * for a pending return at the ":endtry" or after returning from an extra
13404 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013405 * when called due to a ":return" command. "rettv" may point to a typeval
13406 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013407 * FALSE when the return gets pending.
13408 */
13409 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013410do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013411 exarg_T *eap;
13412 int reanimate;
13413 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013414 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415{
13416 int idx;
13417 struct condstack *cstack = eap->cstack;
13418
13419 if (reanimate)
13420 /* Undo the return. */
13421 current_funccal->returned = FALSE;
13422
13423 /*
13424 * Cleanup (and inactivate) conditionals, but stop when a try conditional
13425 * not in its finally clause (which then is to be executed next) is found.
13426 * In this case, make the ":return" pending for execution at the ":endtry".
13427 * Otherwise, return normally.
13428 */
13429 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
13430 if (idx >= 0)
13431 {
13432 cstack->cs_pending[idx] = CSTP_RETURN;
13433
13434 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013435 /* A pending return again gets pending. "rettv" points to an
13436 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000013437 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013438 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439 else
13440 {
13441 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013442 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013444 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013446 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013447 {
13448 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013449 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
13450 *(typeval *)cstack->cs_rettv[idx] = *(typeval *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451 else
13452 EMSG(_(e_outofmem));
13453 }
13454 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013455 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456
13457 if (reanimate)
13458 {
13459 /* The pending return value could be overwritten by a ":return"
13460 * without argument in a finally clause; reset the default
13461 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013462 current_funccal->rettv->v_type = VAR_NUMBER;
13463 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464 }
13465 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013466 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467 }
13468 else
13469 {
13470 current_funccal->returned = TRUE;
13471
13472 /* If the return is carried out now, store the return value. For
13473 * a return immediately after reanimation, the value is already
13474 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013475 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013477 clear_tv(current_funccal->rettv);
13478 *current_funccal->rettv = *(typeval *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013479 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013480 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481 }
13482 }
13483
13484 return idx < 0;
13485}
13486
13487/*
13488 * Free the variable with a pending return value.
13489 */
13490 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013491discard_pending_return(rettv)
13492 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013494 free_tv((typeval *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495}
13496
13497/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013498 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499 * is an allocated string. Used by report_pending() for verbose messages.
13500 */
13501 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013502get_return_cmd(rettv)
13503 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013505 char_u *s;
13506 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013507 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013508
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013509 if (rettv == NULL)
13510 s = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013511 else
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013512 s = echo_string((typeval *)rettv, &tofree, numbuf);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013513
13514 STRCPY(IObuff, ":return ");
13515 STRNCPY(IObuff + 8, s, IOSIZE - 8);
13516 if (STRLEN(s) + 8 >= IOSIZE)
13517 STRCPY(IObuff + IOSIZE - 4, "...");
13518 vim_free(tofree);
13519 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013520}
13521
13522/*
13523 * Get next function line.
13524 * Called by do_cmdline() to get the next line.
13525 * Returns allocated string, or NULL for end of function.
13526 */
13527/* ARGSUSED */
13528 char_u *
13529get_func_line(c, cookie, indent)
13530 int c; /* not used */
13531 void *cookie;
13532 int indent; /* not used */
13533{
13534 struct funccall *fcp = (struct funccall *)cookie;
13535 char_u *retval;
13536 garray_T *gap; /* growarray with function lines */
13537
13538 /* If breakpoints have been added/deleted need to check for it. */
13539 if (fcp->dbg_tick != debug_tick)
13540 {
13541 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
13542 sourcing_lnum);
13543 fcp->dbg_tick = debug_tick;
13544 }
13545
13546 gap = &fcp->func->lines;
13547 if ((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
13548 retval = NULL;
13549 else if (fcp->returned || fcp->linenr >= gap->ga_len)
13550 retval = NULL;
13551 else
13552 {
13553 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
13554 sourcing_lnum = fcp->linenr;
13555 }
13556
13557 /* Did we encounter a breakpoint? */
13558 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
13559 {
13560 dbg_breakpoint(fcp->func->name, sourcing_lnum);
13561 /* Find next breakpoint. */
13562 fcp->breakpoint = dbg_find_breakpoint(FALSE, fcp->func->name,
13563 sourcing_lnum);
13564 fcp->dbg_tick = debug_tick;
13565 }
13566
13567 return retval;
13568}
13569
13570/*
13571 * Return TRUE if the currently active function should be ended, because a
13572 * return was encountered or an error occured. Used inside a ":while".
13573 */
13574 int
13575func_has_ended(cookie)
13576 void *cookie;
13577{
13578 struct funccall *fcp = (struct funccall *)cookie;
13579
13580 /* Ignore the "abort" flag if the abortion behavior has been changed due to
13581 * an error inside a try conditional. */
13582 return (((fcp->func->flags & FC_ABORT) && did_emsg && !aborted_in_try())
13583 || fcp->returned);
13584}
13585
13586/*
13587 * return TRUE if cookie indicates a function which "abort"s on errors.
13588 */
13589 int
13590func_has_abort(cookie)
13591 void *cookie;
13592{
13593 return ((struct funccall *)cookie)->func->flags & FC_ABORT;
13594}
13595
13596#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
13597typedef enum
13598{
13599 VAR_FLAVOUR_DEFAULT,
13600 VAR_FLAVOUR_SESSION,
13601 VAR_FLAVOUR_VIMINFO
13602} var_flavour_T;
13603
13604static var_flavour_T var_flavour __ARGS((char_u *varname));
13605
13606 static var_flavour_T
13607var_flavour(varname)
13608 char_u *varname;
13609{
13610 char_u *p = varname;
13611
13612 if (ASCII_ISUPPER(*p))
13613 {
13614 while (*(++p))
13615 if (ASCII_ISLOWER(*p))
13616 return VAR_FLAVOUR_SESSION;
13617 return VAR_FLAVOUR_VIMINFO;
13618 }
13619 else
13620 return VAR_FLAVOUR_DEFAULT;
13621}
13622#endif
13623
13624#if defined(FEAT_VIMINFO) || defined(PROTO)
13625/*
13626 * Restore global vars that start with a capital from the viminfo file
13627 */
13628 int
13629read_viminfo_varlist(virp, writing)
13630 vir_T *virp;
13631 int writing;
13632{
13633 char_u *tab;
13634 int is_string = FALSE;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013635 typeval tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636
13637 if (!writing && (find_viminfo_parameter('!') != NULL))
13638 {
13639 tab = vim_strchr(virp->vir_line + 1, '\t');
13640 if (tab != NULL)
13641 {
13642 *tab++ = '\0'; /* isolate the variable name */
13643 if (*tab == 'S') /* string var */
13644 is_string = TRUE;
13645
13646 tab = vim_strchr(tab, '\t');
13647 if (tab != NULL)
13648 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649 if (is_string)
13650 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013651 tv.v_type = VAR_STRING;
13652 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654 }
13655 else
13656 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013657 tv.v_type = VAR_NUMBER;
13658 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013660 set_var(virp->vir_line + 1, &tv, FALSE);
13661 if (is_string)
13662 vim_free(tv.vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663 }
13664 }
13665 }
13666
13667 return viminfo_readline(virp);
13668}
13669
13670/*
13671 * Write global vars that start with a capital to the viminfo file
13672 */
13673 void
13674write_viminfo_varlist(fp)
13675 FILE *fp;
13676{
13677 garray_T *gap = &variables; /* global variable */
13678 VAR this_var;
13679 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013680 char *s;
13681 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013682 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683
13684 if (find_viminfo_parameter('!') == NULL)
13685 return;
13686
13687 fprintf(fp, _("\n# global variables:\n"));
13688 for (i = gap->ga_len; --i >= 0; )
13689 {
13690 this_var = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013691 if (this_var->v_name != NULL
13692 && var_flavour(this_var->v_name) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013694 switch (this_var->tv.v_type)
13695 {
13696 case VAR_STRING: s = "STR"; break;
13697 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013698 default: continue;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013699 }
13700 fprintf(fp, "!%s\t%s\t", this_var->v_name, s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013701 viminfo_writestring(fp, echo_string(&this_var->tv,
13702 &tofree, numbuf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013703 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704 }
13705 }
13706}
13707#endif
13708
13709#if defined(FEAT_SESSION) || defined(PROTO)
13710 int
13711store_session_globals(fd)
13712 FILE *fd;
13713{
13714 garray_T *gap = &variables; /* global variable */
13715 VAR this_var;
13716 int i;
13717 char_u *p, *t;
13718
13719 for (i = gap->ga_len; --i >= 0; )
13720 {
13721 this_var = &VAR_GAP_ENTRY(i, gap);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013722 if (this_var->v_name != NULL
13723 && (this_var->tv.v_type == VAR_NUMBER
13724 || this_var->tv.v_type == VAR_STRING)
13725 && var_flavour(this_var->v_name) == VAR_FLAVOUR_SESSION)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013727 /* Escape special characters with a backslash. Turn a LF and
13728 * CR into \n and \r. */
13729 p = vim_strsave_escaped(get_var_string(this_var),
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730 (char_u *)"\\\"\n\r");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013731 if (p == NULL) /* out of memory */
13732 continue;
13733 for (t = p; *t != NUL; ++t)
13734 if (*t == '\n')
13735 *t = 'n';
13736 else if (*t == '\r')
13737 *t = 'r';
13738 if ((fprintf(fd, "let %s = %c%s%c",
13739 this_var->v_name,
13740 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ',
13741 p,
13742 (this_var->tv.v_type == VAR_STRING) ? '"' : ' ') < 0)
13743 || put_eol(fd) == FAIL)
13744 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745 vim_free(p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013746 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000013748 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013749 }
13750 }
13751 return OK;
13752}
13753#endif
13754
13755#endif /* FEAT_EVAL */
13756
13757#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
13758
13759
13760#ifdef WIN3264
13761/*
13762 * Functions for ":8" filename modifier: get 8.3 version of a filename.
13763 */
13764static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
13765static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
13766static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
13767
13768/*
13769 * Get the short pathname of a file.
13770 * Returns 1 on success. *fnamelen is 0 for nonexistant path.
13771 */
13772 static int
13773get_short_pathname(fnamep, bufp, fnamelen)
13774 char_u **fnamep;
13775 char_u **bufp;
13776 int *fnamelen;
13777{
13778 int l,len;
13779 char_u *newbuf;
13780
13781 len = *fnamelen;
13782
13783 l = GetShortPathName(*fnamep, *fnamep, len);
13784 if (l > len - 1)
13785 {
13786 /* If that doesn't work (not enough space), then save the string
13787 * and try again with a new buffer big enough
13788 */
13789 newbuf = vim_strnsave(*fnamep, l);
13790 if (newbuf == NULL)
13791 return 0;
13792
13793 vim_free(*bufp);
13794 *fnamep = *bufp = newbuf;
13795
13796 l = GetShortPathName(*fnamep,*fnamep,l+1);
13797
13798 /* Really should always succeed, as the buffer is big enough */
13799 }
13800
13801 *fnamelen = l;
13802 return 1;
13803}
13804
13805/*
13806 * Create a short path name. Returns the length of the buffer it needs.
13807 * Doesn't copy over the end of the buffer passed in.
13808 */
13809 static int
13810shortpath_for_invalid_fname(fname, bufp, fnamelen)
13811 char_u **fname;
13812 char_u **bufp;
13813 int *fnamelen;
13814{
13815 char_u *s, *p, *pbuf2, *pbuf3;
13816 char_u ch;
13817 int l,len,len2,plen,slen;
13818
13819 /* Make a copy */
13820 len2 = *fnamelen;
13821 pbuf2 = vim_strnsave(*fname, len2);
13822 pbuf3 = NULL;
13823
13824 s = pbuf2 + len2 - 1; /* Find the end */
13825 slen = 1;
13826 plen = len2;
13827
13828 l = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013829 if (after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830 {
13831 --s;
13832 ++slen;
13833 --plen;
13834 }
13835
13836 do
13837 {
13838 /* Go back one path-seperator */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013839 while (s > pbuf2 && !after_pathsep(pbuf2, s + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840 {
13841 --s;
13842 ++slen;
13843 --plen;
13844 }
13845 if (s <= pbuf2)
13846 break;
13847
13848 /* Remeber the character that is about to be blatted */
13849 ch = *s;
13850 *s = 0; /* get_short_pathname requires a null-terminated string */
13851
13852 /* Try it in situ */
13853 p = pbuf2;
13854 if (!get_short_pathname(&p, &pbuf3, &plen))
13855 {
13856 vim_free(pbuf2);
13857 return -1;
13858 }
13859 *s = ch; /* Preserve the string */
13860 } while (plen == 0);
13861
13862 if (plen > 0)
13863 {
13864 /* Remeber the length of the new string. */
13865 *fnamelen = len = plen + slen;
13866 vim_free(*bufp);
13867 if (len > len2)
13868 {
13869 /* If there's not enough space in the currently allocated string,
13870 * then copy it to a buffer big enough.
13871 */
13872 *fname= *bufp = vim_strnsave(p, len);
13873 if (*fname == NULL)
13874 return -1;
13875 }
13876 else
13877 {
13878 /* Transfer pbuf2 to being the main buffer (it's big enough) */
13879 *fname = *bufp = pbuf2;
13880 if (p != pbuf2)
13881 strncpy(*fname, p, plen);
13882 pbuf2 = NULL;
13883 }
13884 /* Concat the next bit */
13885 strncpy(*fname + plen, s, slen);
13886 (*fname)[len] = '\0';
13887 }
13888 vim_free(pbuf3);
13889 vim_free(pbuf2);
13890 return 0;
13891}
13892
13893/*
13894 * Get a pathname for a partial path.
13895 */
13896 static int
13897shortpath_for_partial(fnamep, bufp, fnamelen)
13898 char_u **fnamep;
13899 char_u **bufp;
13900 int *fnamelen;
13901{
13902 int sepcount, len, tflen;
13903 char_u *p;
13904 char_u *pbuf, *tfname;
13905 int hasTilde;
13906
13907 /* Count up the path seperators from the RHS.. so we know which part
13908 * of the path to return.
13909 */
13910 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013911 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912 if (vim_ispathsep(*p))
13913 ++sepcount;
13914
13915 /* Need full path first (use expand_env() to remove a "~/") */
13916 hasTilde = (**fnamep == '~');
13917 if (hasTilde)
13918 pbuf = tfname = expand_env_save(*fnamep);
13919 else
13920 pbuf = tfname = FullName_save(*fnamep, FALSE);
13921
13922 len = tflen = STRLEN(tfname);
13923
13924 if (!get_short_pathname(&tfname, &pbuf, &len))
13925 return -1;
13926
13927 if (len == 0)
13928 {
13929 /* Don't have a valid filename, so shorten the rest of the
13930 * path if we can. This CAN give us invalid 8.3 filenames, but
13931 * there's not a lot of point in guessing what it might be.
13932 */
13933 len = tflen;
13934 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == -1)
13935 return -1;
13936 }
13937
13938 /* Count the paths backward to find the beginning of the desired string. */
13939 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013940 {
13941#ifdef FEAT_MBYTE
13942 if (has_mbyte)
13943 p -= mb_head_off(tfname, p);
13944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945 if (vim_ispathsep(*p))
13946 {
13947 if (sepcount == 0 || (hasTilde && sepcount == 1))
13948 break;
13949 else
13950 sepcount --;
13951 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000013952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953 if (hasTilde)
13954 {
13955 --p;
13956 if (p >= tfname)
13957 *p = '~';
13958 else
13959 return -1;
13960 }
13961 else
13962 ++p;
13963
13964 /* Copy in the string - p indexes into tfname - allocated at pbuf */
13965 vim_free(*bufp);
13966 *fnamelen = (int)STRLEN(p);
13967 *bufp = pbuf;
13968 *fnamep = p;
13969
13970 return 0;
13971}
13972#endif /* WIN3264 */
13973
13974/*
13975 * Adjust a filename, according to a string of modifiers.
13976 * *fnamep must be NUL terminated when called. When returning, the length is
13977 * determined by *fnamelen.
13978 * Returns valid flags.
13979 * When there is an error, *fnamep is set to NULL.
13980 */
13981 int
13982modify_fname(src, usedlen, fnamep, bufp, fnamelen)
13983 char_u *src; /* string with modifiers */
13984 int *usedlen; /* characters after src that are used */
13985 char_u **fnamep; /* file name so far */
13986 char_u **bufp; /* buffer for allocated file name or NULL */
13987 int *fnamelen; /* length of fnamep */
13988{
13989 int valid = 0;
13990 char_u *tail;
13991 char_u *s, *p, *pbuf;
13992 char_u dirname[MAXPATHL];
13993 int c;
13994 int has_fullname = 0;
13995#ifdef WIN3264
13996 int has_shortname = 0;
13997#endif
13998
13999repeat:
14000 /* ":p" - full path/file_name */
14001 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
14002 {
14003 has_fullname = 1;
14004
14005 valid |= VALID_PATH;
14006 *usedlen += 2;
14007
14008 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
14009 if ((*fnamep)[0] == '~'
14010#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
14011 && ((*fnamep)[1] == '/'
14012# ifdef BACKSLASH_IN_FILENAME
14013 || (*fnamep)[1] == '\\'
14014# endif
14015 || (*fnamep)[1] == NUL)
14016
14017#endif
14018 )
14019 {
14020 *fnamep = expand_env_save(*fnamep);
14021 vim_free(*bufp); /* free any allocated file name */
14022 *bufp = *fnamep;
14023 if (*fnamep == NULL)
14024 return -1;
14025 }
14026
14027 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014028 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029 {
14030 if (vim_ispathsep(*p)
14031 && p[1] == '.'
14032 && (p[2] == NUL
14033 || vim_ispathsep(p[2])
14034 || (p[2] == '.'
14035 && (p[3] == NUL || vim_ispathsep(p[3])))))
14036 break;
14037 }
14038
14039 /* FullName_save() is slow, don't use it when not needed. */
14040 if (*p != NUL || !vim_isAbsName(*fnamep))
14041 {
14042 *fnamep = FullName_save(*fnamep, *p != NUL);
14043 vim_free(*bufp); /* free any allocated file name */
14044 *bufp = *fnamep;
14045 if (*fnamep == NULL)
14046 return -1;
14047 }
14048
14049 /* Append a path separator to a directory. */
14050 if (mch_isdir(*fnamep))
14051 {
14052 /* Make room for one or two extra characters. */
14053 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
14054 vim_free(*bufp); /* free any allocated file name */
14055 *bufp = *fnamep;
14056 if (*fnamep == NULL)
14057 return -1;
14058 add_pathsep(*fnamep);
14059 }
14060 }
14061
14062 /* ":." - path relative to the current directory */
14063 /* ":~" - path relative to the home directory */
14064 /* ":8" - shortname path - postponed till after */
14065 while (src[*usedlen] == ':'
14066 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
14067 {
14068 *usedlen += 2;
14069 if (c == '8')
14070 {
14071#ifdef WIN3264
14072 has_shortname = 1; /* Postpone this. */
14073#endif
14074 continue;
14075 }
14076 pbuf = NULL;
14077 /* Need full path first (use expand_env() to remove a "~/") */
14078 if (!has_fullname)
14079 {
14080 if (c == '.' && **fnamep == '~')
14081 p = pbuf = expand_env_save(*fnamep);
14082 else
14083 p = pbuf = FullName_save(*fnamep, FALSE);
14084 }
14085 else
14086 p = *fnamep;
14087
14088 has_fullname = 0;
14089
14090 if (p != NULL)
14091 {
14092 if (c == '.')
14093 {
14094 mch_dirname(dirname, MAXPATHL);
14095 s = shorten_fname(p, dirname);
14096 if (s != NULL)
14097 {
14098 *fnamep = s;
14099 if (pbuf != NULL)
14100 {
14101 vim_free(*bufp); /* free any allocated file name */
14102 *bufp = pbuf;
14103 pbuf = NULL;
14104 }
14105 }
14106 }
14107 else
14108 {
14109 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
14110 /* Only replace it when it starts with '~' */
14111 if (*dirname == '~')
14112 {
14113 s = vim_strsave(dirname);
14114 if (s != NULL)
14115 {
14116 *fnamep = s;
14117 vim_free(*bufp);
14118 *bufp = s;
14119 }
14120 }
14121 }
14122 vim_free(pbuf);
14123 }
14124 }
14125
14126 tail = gettail(*fnamep);
14127 *fnamelen = (int)STRLEN(*fnamep);
14128
14129 /* ":h" - head, remove "/file_name", can be repeated */
14130 /* Don't remove the first "/" or "c:\" */
14131 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
14132 {
14133 valid |= VALID_HEAD;
14134 *usedlen += 2;
14135 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014136 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar071d4272004-06-13 20:20:40 +000014137 --tail;
14138 *fnamelen = (int)(tail - *fnamep);
14139#ifdef VMS
14140 if (*fnamelen > 0)
14141 *fnamelen += 1; /* the path separator is part of the path */
14142#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000014143 while (tail > s && !after_pathsep(s, tail))
14144 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014145 }
14146
14147 /* ":8" - shortname */
14148 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
14149 {
14150 *usedlen += 2;
14151#ifdef WIN3264
14152 has_shortname = 1;
14153#endif
14154 }
14155
14156#ifdef WIN3264
14157 /* Check shortname after we have done 'heads' and before we do 'tails'
14158 */
14159 if (has_shortname)
14160 {
14161 pbuf = NULL;
14162 /* Copy the string if it is shortened by :h */
14163 if (*fnamelen < (int)STRLEN(*fnamep))
14164 {
14165 p = vim_strnsave(*fnamep, *fnamelen);
14166 if (p == 0)
14167 return -1;
14168 vim_free(*bufp);
14169 *bufp = *fnamep = p;
14170 }
14171
14172 /* Split into two implementations - makes it easier. First is where
14173 * there isn't a full name already, second is where there is.
14174 */
14175 if (!has_fullname && !vim_isAbsName(*fnamep))
14176 {
14177 if (shortpath_for_partial(fnamep, bufp, fnamelen) == -1)
14178 return -1;
14179 }
14180 else
14181 {
14182 int l;
14183
14184 /* Simple case, already have the full-name
14185 * Nearly always shorter, so try first time. */
14186 l = *fnamelen;
14187 if (!get_short_pathname(fnamep, bufp, &l))
14188 return -1;
14189
14190 if (l == 0)
14191 {
14192 /* Couldn't find the filename.. search the paths.
14193 */
14194 l = *fnamelen;
14195 if (shortpath_for_invalid_fname(fnamep, bufp, &l ) == -1)
14196 return -1;
14197 }
14198 *fnamelen = l;
14199 }
14200 }
14201#endif /* WIN3264 */
14202
14203 /* ":t" - tail, just the basename */
14204 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
14205 {
14206 *usedlen += 2;
14207 *fnamelen -= (int)(tail - *fnamep);
14208 *fnamep = tail;
14209 }
14210
14211 /* ":e" - extension, can be repeated */
14212 /* ":r" - root, without extension, can be repeated */
14213 while (src[*usedlen] == ':'
14214 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
14215 {
14216 /* find a '.' in the tail:
14217 * - for second :e: before the current fname
14218 * - otherwise: The last '.'
14219 */
14220 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
14221 s = *fnamep - 2;
14222 else
14223 s = *fnamep + *fnamelen - 1;
14224 for ( ; s > tail; --s)
14225 if (s[0] == '.')
14226 break;
14227 if (src[*usedlen + 1] == 'e') /* :e */
14228 {
14229 if (s > tail)
14230 {
14231 *fnamelen += (int)(*fnamep - (s + 1));
14232 *fnamep = s + 1;
14233#ifdef VMS
14234 /* cut version from the extension */
14235 s = *fnamep + *fnamelen - 1;
14236 for ( ; s > *fnamep; --s)
14237 if (s[0] == ';')
14238 break;
14239 if (s > *fnamep)
14240 *fnamelen = s - *fnamep;
14241#endif
14242 }
14243 else if (*fnamep <= tail)
14244 *fnamelen = 0;
14245 }
14246 else /* :r */
14247 {
14248 if (s > tail) /* remove one extension */
14249 *fnamelen = (int)(s - *fnamep);
14250 }
14251 *usedlen += 2;
14252 }
14253
14254 /* ":s?pat?foo?" - substitute */
14255 /* ":gs?pat?foo?" - global substitute */
14256 if (src[*usedlen] == ':'
14257 && (src[*usedlen + 1] == 's'
14258 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
14259 {
14260 char_u *str;
14261 char_u *pat;
14262 char_u *sub;
14263 int sep;
14264 char_u *flags;
14265 int didit = FALSE;
14266
14267 flags = (char_u *)"";
14268 s = src + *usedlen + 2;
14269 if (src[*usedlen + 1] == 'g')
14270 {
14271 flags = (char_u *)"g";
14272 ++s;
14273 }
14274
14275 sep = *s++;
14276 if (sep)
14277 {
14278 /* find end of pattern */
14279 p = vim_strchr(s, sep);
14280 if (p != NULL)
14281 {
14282 pat = vim_strnsave(s, (int)(p - s));
14283 if (pat != NULL)
14284 {
14285 s = p + 1;
14286 /* find end of substitution */
14287 p = vim_strchr(s, sep);
14288 if (p != NULL)
14289 {
14290 sub = vim_strnsave(s, (int)(p - s));
14291 str = vim_strnsave(*fnamep, *fnamelen);
14292 if (sub != NULL && str != NULL)
14293 {
14294 *usedlen = (int)(p + 1 - src);
14295 s = do_string_sub(str, pat, sub, flags);
14296 if (s != NULL)
14297 {
14298 *fnamep = s;
14299 *fnamelen = (int)STRLEN(s);
14300 vim_free(*bufp);
14301 *bufp = s;
14302 didit = TRUE;
14303 }
14304 }
14305 vim_free(sub);
14306 vim_free(str);
14307 }
14308 vim_free(pat);
14309 }
14310 }
14311 /* after using ":s", repeat all the modifiers */
14312 if (didit)
14313 goto repeat;
14314 }
14315 }
14316
14317 return valid;
14318}
14319
14320/*
14321 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
14322 * "flags" can be "g" to do a global substitute.
14323 * Returns an allocated string, NULL for error.
14324 */
14325 char_u *
14326do_string_sub(str, pat, sub, flags)
14327 char_u *str;
14328 char_u *pat;
14329 char_u *sub;
14330 char_u *flags;
14331{
14332 int sublen;
14333 regmatch_T regmatch;
14334 int i;
14335 int do_all;
14336 char_u *tail;
14337 garray_T ga;
14338 char_u *ret;
14339 char_u *save_cpo;
14340
14341 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
14342 save_cpo = p_cpo;
14343 p_cpo = (char_u *)"";
14344
14345 ga_init2(&ga, 1, 200);
14346
14347 do_all = (flags[0] == 'g');
14348
14349 regmatch.rm_ic = p_ic;
14350 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
14351 if (regmatch.regprog != NULL)
14352 {
14353 tail = str;
14354 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
14355 {
14356 /*
14357 * Get some space for a temporary buffer to do the substitution
14358 * into. It will contain:
14359 * - The text up to where the match is.
14360 * - The substituted text.
14361 * - The text after the match.
14362 */
14363 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
14364 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
14365 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
14366 {
14367 ga_clear(&ga);
14368 break;
14369 }
14370
14371 /* copy the text up to where the match is */
14372 i = (int)(regmatch.startp[0] - tail);
14373 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
14374 /* add the substituted text */
14375 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
14376 + ga.ga_len + i, TRUE, TRUE, FALSE);
14377 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378 /* avoid getting stuck on a match with an empty string */
14379 if (tail == regmatch.endp[0])
14380 {
14381 if (*tail == NUL)
14382 break;
14383 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
14384 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385 }
14386 else
14387 {
14388 tail = regmatch.endp[0];
14389 if (*tail == NUL)
14390 break;
14391 }
14392 if (!do_all)
14393 break;
14394 }
14395
14396 if (ga.ga_data != NULL)
14397 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
14398
14399 vim_free(regmatch.regprog);
14400 }
14401
14402 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
14403 ga_clear(&ga);
14404 p_cpo = save_cpo;
14405
14406 return ret;
14407}
14408
14409#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */